hash.scm 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. ;;; guile-gcrypt --- crypto tooling for guile
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2019 Brendan Tildesley <mail@brendan.scot>
  4. ;;;
  5. ;;; This file is part of guile-gcrypt.
  6. ;;;
  7. ;;; guile-gcrypt is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or
  10. ;;; (at your option) any later version.
  11. ;;;
  12. ;;; guile-gcrypt is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. ;;; General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with guile-gcrypt. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (gcrypt hash)
  20. #:use-module (gcrypt common)
  21. #:use-module (gcrypt utils)
  22. #:use-module (rnrs bytevectors)
  23. #:use-module (ice-9 binary-ports)
  24. #:use-module (ice-9 hash-table)
  25. #:use-module (system foreign)
  26. #:use-module (srfi srfi-11)
  27. #:use-module (srfi srfi-26)
  28. #:export (hash
  29. open-hash-input-port
  30. open-hash-port
  31. port-hash
  32. file-hash
  33. ;; Deprecated
  34. sha1
  35. sha256
  36. open-sha256-port
  37. port-sha256
  38. file-sha256
  39. open-sha256-input-port))
  40. ;;; Commentary:
  41. ;;;
  42. ;;; Cryptographic hashes.
  43. ;;;
  44. ;;; Code:
  45. ;;;
  46. ;;; Hash.
  47. ;;;
  48. ;; Algorithm ID's can be found in libgcrypt's src/gcrypt.h.in.
  49. ;; I tried using gcry_md_get_algo_dlen get the hash lengths, but it does not
  50. ;; work for every available algorithm, so they are embedded here.
  51. (define get-algorithm-data
  52. (let ((data
  53. (alist->hashq-table
  54. (list ;; Algorithm ID | Hash length (bytes).
  55. (cons 'md5 (cons 1 16))
  56. (cons 'sha1 (cons 2 20))
  57. (cons 'rmd160 (cons 3 20))
  58. ;; 6 is TIGER from GnuPG < 1.3.2, so we use the tiger symbol for the updated
  59. ;; TIGER1.
  60. (cons 'sha256 (cons 8 32))
  61. (cons 'sha384 (cons 9 48))
  62. (cons 'sha512 (cons 10 64))
  63. (cons 'sha224 (cons 11 28))
  64. (cons 'md4 (cons 301 16))
  65. (cons 'crc32 (cons 302 4))
  66. (cons 'crc32-rfc1510 (cons 303 4))
  67. (cons 'crc24-rfc2440 (cons 304 3))
  68. (cons 'whirlpool (cons 305 64))
  69. (cons 'tiger (cons 306 24)) ; GCRY_MD_TIGER1
  70. (cons 'tiger2 (cons 307 24))
  71. (cons 'gostr3411-94 (cons 308 32))
  72. (cons 'stribog256 (cons 309 32))
  73. (cons 'stribog512 (cons 310 64))
  74. (cons 'gostr3411cp (cons 311 32))
  75. (cons 'sha3-224 (cons 312 28))
  76. (cons 'sha3-256 (cons 313 32))
  77. (cons 'sha3-384 (cons 314 48))
  78. (cons 'sha3-512 (cons 315 64))
  79. (cons 'blake2b-512 (cons 318 64))
  80. (cons 'blake2b-384 (cons 319 48))
  81. (cons 'blake2b-256 (cons 320 32))
  82. (cons 'blake2b-160 (cons 321 20))
  83. (cons 'blake2s-256 (cons 322 32))
  84. (cons 'blake2s-224 (cons 323 28))
  85. (cons 'blake2s-160 (cons 324 20))
  86. (cons 'blake2s-128 (cons 325 16))))))
  87. (lambda (algorithm)
  88. (let ((data (hashq-ref data algorithm)))
  89. (if data
  90. data
  91. (error (string-append
  92. (symbol->string algorithm)
  93. " does not appear to be a supported digest algorithm.")))))))
  94. (define (hash-size algorithm)
  95. (cdr (get-algorithm-data algorithm)))
  96. (define (algorithm-id algorithm)
  97. (car (get-algorithm-data algorithm)))
  98. (define hash
  99. (let ((hash (pointer->procedure void
  100. (libgcrypt-func "gcry_md_hash_buffer")
  101. `(,int * * ,size_t))))
  102. (lambda* (bv algorithm #:optional size)
  103. "Return the hash HASH, using the digest ALGORITHM, of BV as a
  104. bytevector. For variable length hashes, SIZE must be specified."
  105. (let* ((size (if size size (hash-size algorithm)))
  106. (digest (make-bytevector size)))
  107. (hash (algorithm-id algorithm)
  108. (bytevector->pointer digest)
  109. (bytevector->pointer bv)
  110. (bytevector-length bv))
  111. digest))))
  112. (define (sha1 bv) (hash bv 'sha1))
  113. (define (sha256 bv) (hash bv 'sha256))
  114. (define open-hash-md
  115. (let ((open (pointer->procedure int
  116. (libgcrypt-func "gcry_md_open")
  117. `(* ,int ,unsigned-int))))
  118. (lambda (algorithm)
  119. (let* ((md (bytevector->pointer (make-bytevector (sizeof '*))))
  120. (err (open md (algorithm-id algorithm) 0)))
  121. (if (zero? err)
  122. (dereference-pointer md)
  123. (throw 'gcrypt-error err))))))
  124. (define md-write
  125. (pointer->procedure void
  126. (libgcrypt-func "gcry_md_write")
  127. `(* * ,size_t)))
  128. (define md-read
  129. (pointer->procedure '*
  130. (libgcrypt-func "gcry_md_read")
  131. `(* ,int)))
  132. (define md-close
  133. (pointer->procedure void
  134. (libgcrypt-func "gcry_md_close")
  135. '(*)))
  136. (define (open-hash-port algorithm)
  137. "Return two values: an output port, and a thunk. When the thunk is called,
  138. it returns the hash (a bytevector) of all the data written to the
  139. output port with the specified hash algorithm."
  140. (define hash-md
  141. (open-hash-md algorithm))
  142. (define digest #f)
  143. (define position 0)
  144. (define (finalize!)
  145. (let ((ptr (md-read hash-md 0)))
  146. (set! digest (bytevector-copy
  147. (pointer->bytevector ptr (hash-size algorithm))))
  148. (md-close hash-md)))
  149. (define (write! bv offset len)
  150. (if (zero? len)
  151. (begin
  152. (finalize!)
  153. 0)
  154. (let ((ptr (bytevector->pointer bv offset)))
  155. (md-write hash-md ptr len)
  156. (set! position (+ position len))
  157. len)))
  158. (define (get-position)
  159. position)
  160. (define (close)
  161. (unless digest
  162. (finalize!)))
  163. (values (make-custom-binary-output-port (symbol->string algorithm)
  164. write! get-position #f
  165. close)
  166. (lambda ()
  167. (unless digest
  168. (finalize!))
  169. digest)))
  170. (define (port-hash algorithm port)
  171. "Return the hash (a bytevector) of all the data drained from PORT with the
  172. specified hash algorithm."
  173. (let-values (((out get)
  174. (open-hash-port algorithm)))
  175. (dump-port port out)
  176. (close-port out)
  177. (get)))
  178. (define (open-hash-input-port algorithm port)
  179. "Return an input port that wraps PORT and a thunk to get the hash of all the
  180. data read from PORT. The thunk always returns the same value."
  181. (define md
  182. (open-hash-md algorithm))
  183. (define (read! bv start count)
  184. (let ((n (get-bytevector-n! port bv start count)))
  185. (if (eof-object? n)
  186. 0
  187. (begin
  188. (unless digest
  189. (let ((ptr (bytevector->pointer bv start)))
  190. (md-write md ptr n)))
  191. n))))
  192. (define digest #f)
  193. (define (finalize!)
  194. (let ((ptr (md-read md 0)))
  195. (set! digest (bytevector-copy
  196. (pointer->bytevector ptr (hash-size algorithm))))
  197. (md-close md)))
  198. (define (get-hash)
  199. (unless digest
  200. (finalize!))
  201. digest)
  202. (define (unbuffered port)
  203. ;; Guile <= 2.0.9 does not support 'setvbuf' on custom binary input ports.
  204. ;; If you get a wrong-type-arg error here, the fix is to upgrade Guile. :-)
  205. (setvbuf port _IONBF)
  206. port)
  207. (values (unbuffered (make-custom-binary-input-port (symbol->string algorithm) read! #f #f #f))
  208. get-hash))
  209. (define (file-hash algorithm file)
  210. (call-with-input-file file (cut port-hash algorithm <>)))
  211. (define (open-sha256-input-port port) (open-hash-input-port 'sha256 port))
  212. (define (open-sha256-port) (open-hash-port 'sha256))
  213. (define (port-sha256 port) (port-hash 'sha256 port))
  214. (define (file-sha256 file) (file-hash 'sha256 file))
  215. ;;; hash.scm ends here