hash.scm 7.5 KB

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