hash.scm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. ;;; guile-gcrypt --- crypto tooling for guile
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2019 Mathieu Othacehe <m.othacehe@gmail.com>
  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 utils)
  21. #:use-module (gcrypt internal)
  22. #:use-module (rnrs bytevectors)
  23. #:use-module (ice-9 binary-ports)
  24. #:use-module (system foreign)
  25. #:use-module (srfi srfi-11)
  26. #:use-module (srfi srfi-26)
  27. #:export (hash-algorithm
  28. lookup-hash-algorithm
  29. hash-algorithm-name
  30. hash-size
  31. bytevector-hash
  32. open-hash-port
  33. port-hash
  34. file-hash
  35. open-hash-input-port
  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. (define-syntax-rule (define-hash-algorithms name->integer
  49. symbol->integer integer->symbol hash-size
  50. (name id size) ...)
  51. "Define hash algorithms with their NAME, numerical ID, and SIZE in bytes."
  52. (begin
  53. ;; Make sure NAME is bound to follow best practices for syntax matching
  54. ;; (info "(guile) Syntax Rules"). As a bonus, it provides convenient
  55. ;; shorthand procedures.
  56. (define-public name
  57. (cut bytevector-hash <> id))
  58. ...
  59. (define-enumerate-type name->integer symbol->integer integer->symbol
  60. (name id) ...)
  61. (define-lookup-procedure hash-size
  62. "Return the size in bytes of a digest of the given hash algorithm."
  63. (id size) ...)))
  64. (define %hash-size
  65. ;; This procedure was used to double-check the hash sizes below. (We
  66. ;; cannot use it at macro-expansion time because it wouldn't work when
  67. ;; cross-compiling.)
  68. (libgcrypt->procedure unsigned-int
  69. "gcry_md_get_algo_dlen"
  70. (list int)))
  71. ;; 'GCRY_MD_' values as of Libgcrypt 1.8.3.
  72. (define-hash-algorithms hash-algorithm
  73. lookup-hash-algorithm hash-algorithm-name
  74. hash-size
  75. (md5 1 16)
  76. (sha1 2 20)
  77. (rmd160 3 20)
  78. ;; (md2 5 0)
  79. (tiger 6 24) ;TIGER/192 as used by gpg <= 1.3.2
  80. (haval 7 20) ;HAVAL, 5 pass, 160 bit
  81. (sha256 8 32)
  82. (sha384 9 48)
  83. (sha512 10 64)
  84. (sha224 11 28)
  85. (md4 301 16)
  86. (crc32 302 4)
  87. (crc32-rfc1510 303 4)
  88. (crc24-rfc2440 304 3)
  89. (whirlpool 305 64)
  90. (tiger1 306 24) ;TIGER fixed
  91. (tiger2 307 24) ;TIGER2 variant
  92. (gostr3411-94 308 32) ;GOST R 34.11-94
  93. (stribog256 309 32) ;GOST R 34.11-2012, 256 bit
  94. (stribog512 310 64) ;GOST R 34.11-2012, 512 bit
  95. (gostr3411-cp 311 32) ;GOST R 34.11-94 with CryptoPro-A S-Box
  96. (sha3-224 312 28)
  97. (sha3-256 313 32)
  98. (sha3-384 314 48)
  99. (sha3-512 315 64)
  100. ;; (shake128 316 0)
  101. ;; (shake256 317 0)
  102. (blake2b-512 318 64)
  103. (blake2b-384 319 48)
  104. (blake2b-256 320 32)
  105. (blake2b-160 321 20)
  106. (blake2s-256 322 32)
  107. (blake2s-224 323 28)
  108. (blake2s-160 324 20)
  109. (blake2s-128 325 16))
  110. (define bytevector-hash
  111. (let ((proc (libgcrypt->procedure void
  112. "gcry_md_hash_buffer"
  113. `(,int * * ,size_t))))
  114. (lambda (bv algorithm)
  115. "Return the hash ALGORITHM of BV as a bytevector."
  116. (let ((digest (make-bytevector (hash-size algorithm))))
  117. (proc algorithm (bytevector->pointer digest)
  118. (bytevector->pointer bv) (bytevector-length bv))
  119. digest))))
  120. (define open-md
  121. (let ((proc (libgcrypt->procedure int
  122. "gcry_md_open"
  123. `(* ,int ,unsigned-int))))
  124. (lambda (algorithm)
  125. (let* ((md (bytevector->pointer (make-bytevector (sizeof '*))))
  126. (err (proc md algorithm 0)))
  127. (if (zero? err)
  128. (dereference-pointer md)
  129. (throw 'gcrypt-error err))))))
  130. (define md-write
  131. (libgcrypt->procedure void "gcry_md_write" `(* * ,size_t)))
  132. (define md-read
  133. (libgcrypt->procedure '* "gcry_md_read" `(* ,int)))
  134. (define md-close
  135. (libgcrypt->procedure void "gcry_md_close" '(*)))
  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) for ALGORITHM of all the data written to the
  139. output port."
  140. (define md
  141. (open-md algorithm))
  142. (define md-size
  143. (hash-size algorithm))
  144. (define digest #f)
  145. (define position 0)
  146. (define (finalize!)
  147. (let ((ptr (md-read md 0)))
  148. (set! digest (bytevector-copy (pointer->bytevector ptr md-size)))
  149. (md-close md)))
  150. (define (write! bv offset len)
  151. (if (zero? len)
  152. (begin
  153. (finalize!)
  154. 0)
  155. (let ((ptr (bytevector->pointer bv offset)))
  156. (md-write md ptr len)
  157. (set! position (+ position len))
  158. len)))
  159. (define (get-position)
  160. position)
  161. (define (close)
  162. (unless digest
  163. (finalize!)))
  164. (values (make-custom-binary-output-port "hash"
  165. write! get-position #f
  166. close)
  167. (lambda ()
  168. (unless digest
  169. (finalize!))
  170. digest)))
  171. (define (port-hash algorithm port)
  172. "Return the ALGORITHM hash (a bytevector) of all the data drained from
  173. PORT."
  174. (let-values (((out get)
  175. (open-hash-port algorithm)))
  176. (dump-port port out)
  177. (close-port out)
  178. (get)))
  179. (define (file-hash algorithm file)
  180. "Return the ALGORITHM hash (a bytevector) of FILE."
  181. (call-with-input-file file
  182. (cut port-hash algorithm <>)))
  183. (define (open-hash-input-port algorithm port)
  184. "Return an input port that wraps PORT and a thunk to get the hash of all the
  185. data read from PORT. The thunk always returns the same value."
  186. (define md
  187. (open-md algorithm))
  188. (define md-size
  189. (hash-size algorithm))
  190. (define (read! bv start count)
  191. (let ((n (get-bytevector-n! port bv start count)))
  192. (if (eof-object? n)
  193. 0
  194. (begin
  195. (unless digest
  196. (let ((ptr (bytevector->pointer bv start)))
  197. (md-write md ptr n)))
  198. n))))
  199. (define digest #f)
  200. (define (finalize!)
  201. (let ((ptr (md-read md 0)))
  202. (set! digest (bytevector-copy (pointer->bytevector ptr md-size)))
  203. (md-close md)))
  204. (define (get-hash)
  205. (unless digest
  206. (finalize!))
  207. digest)
  208. (define (unbuffered port)
  209. ;; Guile <= 2.0.9 does not support 'setvbuf' on custom binary input ports.
  210. ;; If you get a wrong-type-arg error here, the fix is to upgrade Guile. :-)
  211. (setvbuf port
  212. (cond-expand ((and guile-2 (not guile-2.2)) _IONBF)
  213. (else 'none)))
  214. port)
  215. (values (unbuffered (make-custom-binary-input-port "hash-input"
  216. read! #f #f #f))
  217. get-hash))
  218. (define open-sha256-port
  219. (cut open-hash-port (hash-algorithm sha256)))
  220. (define port-sha256
  221. (cut port-hash (hash-algorithm sha256) <>))
  222. (define file-sha256
  223. (cut file-hash (hash-algorithm sha256) <>))
  224. (define open-sha256-input-port
  225. (cut open-hash-input-port (hash-algorithm sha256) <>))
  226. ;;; hash.scm ends here