hash.scm 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. ;;; guile-gcrypt --- crypto tooling for guile
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2019, 2020, 2022 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
  8. ;;; modify it under the terms of the GNU Lesser General Public License
  9. ;;; as published by the Free Software Foundation; either version 3 of
  10. ;;; the License, or (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. ;;; Lesser General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU Lesser 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.8.
  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. (sm3 326 32)
  111. (sha512-256 327 32)
  112. (sha512-224 328 28))
  113. (define bytevector-hash
  114. (let ((proc (libgcrypt->procedure void
  115. "gcry_md_hash_buffer"
  116. `(,int * * ,size_t))))
  117. (lambda (bv algorithm)
  118. "Return the hash ALGORITHM of BV as a bytevector."
  119. (let ((digest (make-bytevector (hash-size algorithm))))
  120. (proc algorithm (bytevector->pointer digest)
  121. (bytevector->pointer bv) (bytevector-length bv))
  122. digest))))
  123. (define open-md
  124. (let ((proc (libgcrypt->procedure int
  125. "gcry_md_open"
  126. `(* ,int ,unsigned-int))))
  127. (lambda (algorithm)
  128. (let* ((md (bytevector->pointer (make-bytevector (sizeof '*))))
  129. (err (proc md algorithm 0)))
  130. (if (zero? err)
  131. (dereference-pointer md)
  132. (throw 'gcrypt-error err))))))
  133. (define md-write
  134. (libgcrypt->procedure void "gcry_md_write" `(* * ,size_t)))
  135. (define md-read
  136. (libgcrypt->procedure '* "gcry_md_read" `(* ,int)))
  137. (define md-close
  138. (libgcrypt->procedure void "gcry_md_close" '(*)))
  139. (define (open-hash-port algorithm)
  140. "Return two values: an output port, and a thunk. When the thunk is called,
  141. it returns the hash (a bytevector) for ALGORITHM of all the data written to the
  142. output port."
  143. (define md
  144. (open-md algorithm))
  145. (define md-size
  146. (hash-size algorithm))
  147. (define digest #f)
  148. (define position 0)
  149. (define (finalize!)
  150. (let ((ptr (md-read md 0)))
  151. (set! digest (bytevector-copy (pointer->bytevector ptr md-size)))
  152. (md-close md)))
  153. (define (write! bv offset len)
  154. (if (zero? len)
  155. (begin
  156. (finalize!)
  157. 0)
  158. (let ((ptr (bytevector->pointer bv offset)))
  159. (md-write md ptr len)
  160. (set! position (+ position len))
  161. len)))
  162. (define (get-position)
  163. position)
  164. (define (close)
  165. (unless digest
  166. (finalize!)))
  167. (values (make-custom-binary-output-port "hash"
  168. write! get-position #f
  169. close)
  170. (lambda ()
  171. (unless digest
  172. (finalize!))
  173. digest)))
  174. (define (port-hash algorithm port)
  175. "Return the ALGORITHM hash (a bytevector) of all the data drained from
  176. PORT."
  177. (let-values (((out get)
  178. (open-hash-port algorithm)))
  179. (dump-port port out)
  180. (close-port out)
  181. (get)))
  182. (define (file-hash algorithm file)
  183. "Return the ALGORITHM hash (a bytevector) of FILE."
  184. (call-with-input-file file
  185. (cut port-hash algorithm <>)))
  186. (define (open-hash-input-port algorithm port)
  187. "Return an input port that wraps PORT and a thunk to get the hash of all the
  188. data read from PORT. The thunk always returns the same value."
  189. (define md
  190. (open-md algorithm))
  191. (define md-size
  192. (hash-size algorithm))
  193. (define (read! bv start count)
  194. (let ((n (get-bytevector-n! port bv start count)))
  195. (if (eof-object? n)
  196. 0
  197. (begin
  198. (unless digest
  199. (let ((ptr (bytevector->pointer bv start)))
  200. (md-write md ptr n)))
  201. n))))
  202. (define digest #f)
  203. (define (finalize!)
  204. (let ((ptr (md-read md 0)))
  205. (set! digest (bytevector-copy (pointer->bytevector ptr md-size)))
  206. (md-close md)))
  207. (define (get-hash)
  208. (unless digest
  209. (finalize!))
  210. digest)
  211. (define (unbuffered port)
  212. ;; Guile <= 2.0.9 does not support 'setvbuf' on custom binary input ports.
  213. ;; If you get a wrong-type-arg error here, the fix is to upgrade Guile. :-)
  214. (setvbuf port
  215. (cond-expand ((and guile-2 (not guile-2.2)) _IONBF)
  216. (else 'none)))
  217. port)
  218. (values (unbuffered (make-custom-binary-input-port "hash-input"
  219. read! #f #f #f))
  220. get-hash))
  221. (define open-sha256-port
  222. (cut open-hash-port (hash-algorithm sha256)))
  223. (define port-sha256
  224. (cut port-hash (hash-algorithm sha256) <>))
  225. (define file-sha256
  226. (cut file-hash (hash-algorithm sha256) <>))
  227. (define open-sha256-input-port
  228. (cut open-hash-input-port (hash-algorithm sha256) <>))
  229. ;;; hash.scm ends here