mac.scm 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. ;;; guile-gcrypt --- crypto tooling for guile
  2. ;;; Copyright © 2016 Christopher Allan Webber <cwebber@dustycloud.org>
  3. ;;; Copyright © 2019 Mathieu Othacehe <m.othacehe@gmail.com>
  4. ;;; Copyright © 2019, 2020 Ludovic Courtès <ludo@gnu.org>
  5. ;;;
  6. ;;; This file is part of guile-gcrypt.
  7. ;;;
  8. ;;; guile-gcrypt is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or
  11. ;;; (at your option) any later version.
  12. ;;;
  13. ;;; guile-gcrypt is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. ;;; General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with guile-gcrypt. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (gcrypt mac)
  21. #:use-module (ice-9 format)
  22. #:use-module (ice-9 match)
  23. #:use-module (gcrypt base64)
  24. #:use-module (gcrypt internal)
  25. #:use-module (gcrypt random)
  26. #:use-module (rnrs bytevectors)
  27. #:use-module (system foreign)
  28. #:export (mac-algorithm
  29. lookup-mac-algorithm
  30. mac-algorithm-name
  31. mac-size
  32. sign-data
  33. sign-data-base64
  34. valid-signature?
  35. valid-base64-signature?
  36. generate-signing-key))
  37. (define-syntax-rule (define-syntax-public name value)
  38. (begin
  39. (define-syntax name value)
  40. (export name)))
  41. (define-syntax-rule (define-mac-algorithms name->integer
  42. symbol->integer integer->symbol mac-size
  43. (name id size) ...)
  44. "Define hash algorithms with their NAME, numerical ID, and SIZE in bytes."
  45. (begin
  46. ;; Make sure NAME is bound to follow best practices for syntax matching
  47. ;; (info "(guile) Syntax Rules").
  48. (define-syntax-public name
  49. (lambda (s)
  50. (syntax-violation 'name "\
  51. syntactic keyword is meant to be used with 'mac-algorithm'"
  52. s s)))
  53. ...
  54. (define-enumerate-type name->integer symbol->integer integer->symbol
  55. (name id) ...)
  56. (define-lookup-procedure mac-size
  57. "Return the size in bytes of a digest of the given hash algorithm."
  58. (id size) ...)))
  59. (define-mac-algorithms mac-algorithm
  60. lookup-mac-algorithm mac-algorithm-name
  61. mac-size
  62. ;; GCRY_MAC_*
  63. (hmac-sha256 101 32)
  64. (hmac-sha224 102 28)
  65. (hmac-sha512 103 64)
  66. (hmac-sha384 104 48)
  67. (hmac-sha1 105 20)
  68. (hmac-md5 106 16)
  69. (hmac-md4 107 16)
  70. (hmac-rmd160 108 20)
  71. (hmac-tiger1 109 24)
  72. (hmac-whirlpool 110 64)
  73. (hmac-gostr3411-94 111 32)
  74. (hmac-stribog256 112 32)
  75. (hmac-stribog512 113 64)
  76. ;; (hmac-md2 114 0)
  77. (hmac-sha3-224 115 28)
  78. (hmac-sha3-256 116 32)
  79. (hmac-sha3-384 117 48)
  80. (hmac-sha3-512 118 64)
  81. (cmac-aes 201 16)
  82. (cmac-3des 202 8)
  83. (cmac-camellia 203 16)
  84. (cmac-cast5 204 8)
  85. (cmac-blowfish 205 8)
  86. (cmac-twofish 206 16)
  87. (cmac-serpent 207 16)
  88. (cmac-seed 208 16)
  89. (cmac-rfc2268 209 8)
  90. (cmac-idea 210 8)
  91. (cmac-gost28147 211 8)
  92. (gmac-aes 401 16)
  93. (gmac-camellia 402 16)
  94. (gmac-twofish 403 16)
  95. (gmac-serpent 404 16)
  96. (gmac-seed 405 16)
  97. (poly1305 501 16)
  98. (poly1305-aes 502 16)
  99. (poly1305-camellia 503 16)
  100. (poly1305-twofish 504 16)
  101. (poly1305-serpent 505 16)
  102. (poly1305-seed 506 16))
  103. (define mac-algo-maclen
  104. ;; This procedure was used to double-check the hash sizes above. (We
  105. ;; cannot use it at macro-expansion time because it wouldn't work when
  106. ;; cross-compiling.)
  107. (libgcrypt->procedure int "gcry_mac_get_algo_maclen" `(,int)))
  108. (define %no-error 0) ; GPG_ERR_NO_ERROR
  109. (define-wrapped-pointer-type <mac>
  110. mac?
  111. pointer->mac mac->pointer
  112. (lambda (mac port)
  113. (format port "#<mac ~x>"
  114. (pointer-address (mac->pointer mac)))))
  115. (define %gcry-mac-open
  116. (libgcrypt->procedure int "gcry_mac_open"
  117. ;; gcry_mac_hd_t *HD, int ALGO,
  118. ;; unsigned int FLAGS, gcry_ctx_t CTX
  119. `(* ,int ,unsigned-int *)))
  120. (define (mac-open algorithm)
  121. "Create a <mac> object set to use ALGORITHM"
  122. (let* ((mac (bytevector->pointer (make-bytevector (sizeof '*))))
  123. (err (%gcry-mac-open mac algorithm 0 %null-pointer)))
  124. (if (= err 0)
  125. (pointer->mac (dereference-pointer mac))
  126. (throw 'gcry-error 'mac-open err))))
  127. (define %gcry-mac-setkey
  128. (libgcrypt->procedure int "gcry_mac_setkey" `(* * ,size_t)))
  129. (define (mac-setkey mac key)
  130. "Set the KEY on <mac> object MAC
  131. In our case, KEY is either a string or a bytevector."
  132. (let* ((key (match key
  133. ((? bytevector? key)
  134. key)
  135. ((? string? key)
  136. (string->utf8 key))))
  137. (err (%gcry-mac-setkey (mac->pointer mac)
  138. (bytevector->pointer key)
  139. (bytevector-length key))))
  140. (if (= err 0)
  141. #t
  142. (throw 'gcry-error 'mac-setkey err))))
  143. (define mac-close
  144. (let ((proc (libgcrypt->procedure void
  145. "gcry_mac_close"
  146. '(*)))) ; gcry_mac_hd_t H
  147. (lambda (mac)
  148. "Release all resources of MAC.
  149. Running this on an already closed <mac> might segfault :)"
  150. (proc (mac->pointer mac)))))
  151. (define mac-write
  152. (let ((proc (libgcrypt->procedure int
  153. "gcry_mac_write"
  154. `(* * ,size_t))))
  155. (lambda (mac obj)
  156. "Writes string or bytevector OBJ to MAC"
  157. (let* ((bv (match obj
  158. ((? bytevector? obj)
  159. obj)
  160. ((? string? obj)
  161. (string->utf8 obj))))
  162. (err (proc (mac->pointer mac)
  163. (bytevector->pointer bv)
  164. (bytevector-length bv))))
  165. (if (= err 0)
  166. #t
  167. (throw 'gcry-error 'mac-write err))))))
  168. (define mac-read
  169. (let ((proc (libgcrypt->procedure int
  170. "gcry_mac_read"
  171. `(* * *))))
  172. (lambda (mac algorithm)
  173. "Get bytevector representing result of MAC's written, signed data"
  174. (define (int-bv* n)
  175. ;; Get the pointer to a bytevector holding an integer with this number
  176. (let ((bv (make-bytevector (sizeof int))))
  177. (bytevector-uint-set! bv 0 n (native-endianness) (sizeof int))
  178. (bytevector->pointer bv)))
  179. (let* ((bv-len (mac-size algorithm))
  180. (bv (make-bytevector bv-len))
  181. (err (proc (mac->pointer mac)
  182. (bytevector->pointer bv)
  183. (int-bv* bv-len))))
  184. (if (= err 0)
  185. bv
  186. (throw 'gcry-error 'mac-read err))))))
  187. ;; GPG_ERR_CHECKSUM *should* be 10, but it seems to return here as
  188. ;; 16777226... unfortunately this is because we're pulling back an integer
  189. ;; rather than the gcry_error_t type.
  190. (define mac-verify
  191. (let ((proc (libgcrypt->procedure int
  192. "gcry_mac_verify"
  193. `(* * ,size_t))))
  194. (lambda (mac bv)
  195. "Verify that BV matches result calculated in MAC
  196. BV should be a bytevector with previously calculated data."
  197. (let ((err (proc (mac->pointer mac)
  198. (bytevector->pointer bv)
  199. (bytevector-length bv))))
  200. (if (= err 0)
  201. (values #t err)
  202. ;; TODO: This is WRONG! See the comment above
  203. ;; this procedure's definition for why. If we could
  204. ;; parse it as the appropriate GPG error, GPG_ERR_CHECKSUM
  205. ;; should be 10.
  206. (values #f err))))))
  207. (define* (sign-data key data #:key
  208. (algorithm (mac-algorithm hmac-sha512)))
  209. "Signs DATA with KEY for ALGORITHM. Returns a bytevector."
  210. (let ((mac (mac-open algorithm)))
  211. (mac-setkey mac key)
  212. (mac-write mac data)
  213. (let ((result (mac-read mac algorithm)))
  214. (mac-close mac)
  215. result)))
  216. (define* (sign-data-base64 key data #:key
  217. (algorithm (mac-algorithm hmac-sha512)))
  218. "Like sign-data, but conveniently encodes to base64."
  219. (base64-encode (sign-data key data #:algorithm algorithm)))
  220. (define* (valid-signature? key data sig
  221. #:key (algorithm (mac-algorithm hmac-sha512)))
  222. "Verify that DATA with KEY matches previous signature SIG for ALGORITHM."
  223. (let ((mac (mac-open algorithm)))
  224. (mac-setkey mac key)
  225. (mac-write mac data)
  226. (let ((result (mac-verify mac sig)))
  227. (mac-close mac)
  228. result)))
  229. (define* (valid-base64-signature? key data b64-sig
  230. #:key
  231. (algorithm (mac-algorithm hmac-sha512)))
  232. (valid-signature? key data
  233. (base64-decode b64-sig)
  234. #:algorithm algorithm))
  235. (define* (generate-signing-key #:optional (key-length 128))
  236. "Generate a signing key (a bytevector).
  237. KEY-LENGTH is the length, in bytes, of the key. The default is 128.
  238. This should be a multiple of 8."
  239. (gen-random-bv key-length %gcry-very-strong-random))