123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- ;;; guile-gcrypt --- crypto tooling for guile
- ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2019, 2020, 2022 Ludovic Courtès <ludo@gnu.org>
- ;;; Copyright © 2019 Mathieu Othacehe <m.othacehe@gmail.com>
- ;;;
- ;;; This file is part of guile-gcrypt.
- ;;;
- ;;; guile-gcrypt is free software; you can redistribute it and/or
- ;;; modify it under the terms of the GNU Lesser General Public License
- ;;; as published by the Free Software Foundation; either version 3 of
- ;;; the License, or (at your option) any later version.
- ;;;
- ;;; guile-gcrypt is distributed in the hope that it will be useful, but
- ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
- ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- ;;; Lesser General Public License for more details.
- ;;;
- ;;; You should have received a copy of the GNU Lesser General Public License
- ;;; along with guile-gcrypt. If not, see <http://www.gnu.org/licenses/>.
- (define-module (gcrypt hash)
- #:use-module (gcrypt utils)
- #:use-module (gcrypt internal)
- #:use-module (rnrs bytevectors)
- #:use-module (ice-9 binary-ports)
- #:use-module (system foreign)
- #:use-module (srfi srfi-11)
- #:use-module (srfi srfi-26)
- #:export (hash-algorithm
- lookup-hash-algorithm
- hash-algorithm-name
- hash-size
- bytevector-hash
- open-hash-port
- port-hash
- file-hash
- open-hash-input-port
- open-sha256-port
- port-sha256
- file-sha256
- open-sha256-input-port))
- ;;; Commentary:
- ;;;
- ;;; Cryptographic hashes.
- ;;;
- ;;; Code:
- ;;;
- ;;; Hash.
- ;;;
- (define-syntax-rule (define-hash-algorithms name->integer
- symbol->integer integer->symbol hash-size
- (name id size) ...)
- "Define hash algorithms with their NAME, numerical ID, and SIZE in bytes."
- (begin
- ;; Make sure NAME is bound to follow best practices for syntax matching
- ;; (info "(guile) Syntax Rules"). As a bonus, it provides convenient
- ;; shorthand procedures.
- (define-public name
- (cut bytevector-hash <> id))
- ...
- (define-enumerate-type name->integer symbol->integer integer->symbol
- (name id) ...)
- (define-lookup-procedure hash-size
- "Return the size in bytes of a digest of the given hash algorithm."
- (id size) ...)))
- (define %hash-size
- ;; This procedure was used to double-check the hash sizes below. (We
- ;; cannot use it at macro-expansion time because it wouldn't work when
- ;; cross-compiling.)
- (libgcrypt->procedure unsigned-int
- "gcry_md_get_algo_dlen"
- (list int)))
- ;; 'GCRY_MD_' values as of Libgcrypt 1.8.8.
- (define-hash-algorithms hash-algorithm
- lookup-hash-algorithm hash-algorithm-name
- hash-size
- (md5 1 16)
- (sha1 2 20)
- (rmd160 3 20)
- ;; (md2 5 0)
- (tiger 6 24) ;TIGER/192 as used by gpg <= 1.3.2
- (haval 7 20) ;HAVAL, 5 pass, 160 bit
- (sha256 8 32)
- (sha384 9 48)
- (sha512 10 64)
- (sha224 11 28)
- (md4 301 16)
- (crc32 302 4)
- (crc32-rfc1510 303 4)
- (crc24-rfc2440 304 3)
- (whirlpool 305 64)
- (tiger1 306 24) ;TIGER fixed
- (tiger2 307 24) ;TIGER2 variant
- (gostr3411-94 308 32) ;GOST R 34.11-94
- (stribog256 309 32) ;GOST R 34.11-2012, 256 bit
- (stribog512 310 64) ;GOST R 34.11-2012, 512 bit
- (gostr3411-cp 311 32) ;GOST R 34.11-94 with CryptoPro-A S-Box
- (sha3-224 312 28)
- (sha3-256 313 32)
- (sha3-384 314 48)
- (sha3-512 315 64)
- ;; (shake128 316 0)
- ;; (shake256 317 0)
- (blake2b-512 318 64)
- (blake2b-384 319 48)
- (blake2b-256 320 32)
- (blake2b-160 321 20)
- (blake2s-256 322 32)
- (blake2s-224 323 28)
- (blake2s-160 324 20)
- (blake2s-128 325 16)
- (sm3 326 32)
- (sha512-256 327 32)
- (sha512-224 328 28))
- (define bytevector-hash
- (let ((proc (libgcrypt->procedure void
- "gcry_md_hash_buffer"
- `(,int * * ,size_t))))
- (lambda (bv algorithm)
- "Return the hash ALGORITHM of BV as a bytevector."
- (let ((digest (make-bytevector (hash-size algorithm))))
- (proc algorithm (bytevector->pointer digest)
- (bytevector->pointer bv) (bytevector-length bv))
- digest))))
- (define open-md
- (let ((proc (libgcrypt->procedure int
- "gcry_md_open"
- `(* ,int ,unsigned-int))))
- (lambda (algorithm)
- (let* ((md (bytevector->pointer (make-bytevector (sizeof '*))))
- (err (proc md algorithm 0)))
- (if (zero? err)
- (dereference-pointer md)
- (throw 'gcrypt-error err))))))
- (define md-write
- (libgcrypt->procedure void "gcry_md_write" `(* * ,size_t)))
- (define md-read
- (libgcrypt->procedure '* "gcry_md_read" `(* ,int)))
- (define md-close
- (libgcrypt->procedure void "gcry_md_close" '(*)))
- (define (open-hash-port algorithm)
- "Return two values: an output port, and a thunk. When the thunk is called,
- it returns the hash (a bytevector) for ALGORITHM of all the data written to the
- output port."
- (define md
- (open-md algorithm))
- (define md-size
- (hash-size algorithm))
- (define digest #f)
- (define position 0)
- (define (finalize!)
- (let ((ptr (md-read md 0)))
- (set! digest (bytevector-copy (pointer->bytevector ptr md-size)))
- (md-close md)))
- (define (write! bv offset len)
- (if (zero? len)
- (begin
- (finalize!)
- 0)
- (let ((ptr (bytevector->pointer bv offset)))
- (md-write md ptr len)
- (set! position (+ position len))
- len)))
- (define (get-position)
- position)
- (define (close)
- (unless digest
- (finalize!)))
- (values (make-custom-binary-output-port "hash"
- write! get-position #f
- close)
- (lambda ()
- (unless digest
- (finalize!))
- digest)))
- (define (port-hash algorithm port)
- "Return the ALGORITHM hash (a bytevector) of all the data drained from
- PORT."
- (let-values (((out get)
- (open-hash-port algorithm)))
- (dump-port port out)
- (close-port out)
- (get)))
- (define (file-hash algorithm file)
- "Return the ALGORITHM hash (a bytevector) of FILE."
- (call-with-input-file file
- (cut port-hash algorithm <>)))
- (define (open-hash-input-port algorithm port)
- "Return an input port that wraps PORT and a thunk to get the hash of all the
- data read from PORT. The thunk always returns the same value."
- (define md
- (open-md algorithm))
- (define md-size
- (hash-size algorithm))
- (define (read! bv start count)
- (let ((n (get-bytevector-n! port bv start count)))
- (if (eof-object? n)
- 0
- (begin
- (unless digest
- (let ((ptr (bytevector->pointer bv start)))
- (md-write md ptr n)))
- n))))
- (define digest #f)
- (define (finalize!)
- (let ((ptr (md-read md 0)))
- (set! digest (bytevector-copy (pointer->bytevector ptr md-size)))
- (md-close md)))
- (define (get-hash)
- (unless digest
- (finalize!))
- digest)
- (define (unbuffered port)
- ;; Guile <= 2.0.9 does not support 'setvbuf' on custom binary input ports.
- ;; If you get a wrong-type-arg error here, the fix is to upgrade Guile. :-)
- (setvbuf port
- (cond-expand ((and guile-2 (not guile-2.2)) _IONBF)
- (else 'none)))
- port)
- (values (unbuffered (make-custom-binary-input-port "hash-input"
- read! #f #f #f))
- get-hash))
- (define open-sha256-port
- (cut open-hash-port (hash-algorithm sha256)))
- (define port-sha256
- (cut port-hash (hash-algorithm sha256) <>))
- (define file-sha256
- (cut file-hash (hash-algorithm sha256) <>))
- (define open-sha256-input-port
- (cut open-hash-input-port (hash-algorithm sha256) <>))
- ;;; hash.scm ends here
|