mac.scm 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 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-size
  31. sign-data
  32. sign-data-base64
  33. valid-signature?
  34. valid-base64-signature?
  35. generate-signing-key))
  36. (define-syntax-rule (define-mac-algorithms name->integer
  37. symbol->integer mac-size
  38. (name id size) ...)
  39. "Define hash algorithms with their NAME, numerical ID, and SIZE in bytes."
  40. (begin
  41. (define-enumerate-type name->integer symbol->integer
  42. (name id) ...)
  43. (define-lookup-procedure mac-size
  44. "Return the size in bytes of a digest of the given hash algorithm."
  45. (id size) ...)))
  46. (define-mac-algorithms mac-algorithm
  47. lookup-mac-algorithm
  48. mac-size
  49. ;; GCRY_MAC_*
  50. (hmac-sha256 101 32)
  51. (hmac-sha224 102 28)
  52. (hmac-sha512 103 64)
  53. (hmac-sha384 104 48)
  54. (hmac-sha1 105 20)
  55. (hmac-md5 106 16)
  56. (hmac-md4 107 16)
  57. (hmac-rmd160 108 20)
  58. (hmac-tiger1 109 24)
  59. (hmac-whirlpool 110 64)
  60. (hmac-gostr3411-94 111 32)
  61. (hmac-stribog256 112 32)
  62. (hmac-stribog512 113 64)
  63. ;; (hmac-md2 114 0)
  64. (hmac-sha3-224 115 28)
  65. (hmac-sha3-256 116 32)
  66. (hmac-sha3-384 117 48)
  67. (hmac-sha3-512 118 64)
  68. (cmac-aes 201 16)
  69. (cmac-3des 202 8)
  70. (cmac-camellia 203 16)
  71. (cmac-cast5 204 8)
  72. (cmac-blowfish 205 8)
  73. (cmac-twofish 206 16)
  74. (cmac-serpent 207 16)
  75. (cmac-seed 208 16)
  76. (cmac-rfc2268 209 8)
  77. (cmac-idea 210 8)
  78. (cmac-gost28147 211 8)
  79. (gmac-aes 401 16)
  80. (gmac-camellia 402 16)
  81. (gmac-twofish 403 16)
  82. (gmac-serpent 404 16)
  83. (gmac-seed 405 16)
  84. (poly1305 501 16)
  85. (poly1305-aes 502 16)
  86. (poly1305-camellia 503 16)
  87. (poly1305-twofish 504 16)
  88. (poly1305-serpent 505 16)
  89. (poly1305-seed 506 16))
  90. (define mac-algo-maclen
  91. ;; This procedure was used to double-check the hash sizes above. (We
  92. ;; cannot use it at macro-expansion time because it wouldn't work when
  93. ;; cross-compiling.)
  94. (libgcrypt->procedure int "gcry_mac_get_algo_maclen" `(,int)))
  95. (define %no-error 0) ; GPG_ERR_NO_ERROR
  96. (define-wrapped-pointer-type <mac>
  97. mac?
  98. pointer->mac mac->pointer
  99. (lambda (mac port)
  100. (format port "#<mac ~x>"
  101. (pointer-address (mac->pointer mac)))))
  102. (define %gcry-mac-open
  103. (libgcrypt->procedure int "gcry_mac_open"
  104. ;; gcry_mac_hd_t *HD, int ALGO,
  105. ;; unsigned int FLAGS, gcry_ctx_t CTX
  106. `(* ,int ,unsigned-int *)))
  107. (define (mac-open algorithm)
  108. "Create a <mac> object set to use ALGORITHM"
  109. (let* ((mac (bytevector->pointer (make-bytevector (sizeof '*))))
  110. (err (%gcry-mac-open mac algorithm 0 %null-pointer)))
  111. (if (= err 0)
  112. (pointer->mac (dereference-pointer mac))
  113. (throw 'gcry-error 'mac-open err))))
  114. (define %gcry-mac-setkey
  115. (libgcrypt->procedure int "gcry_mac_setkey" `(* * ,size_t)))
  116. (define (mac-setkey mac key)
  117. "Set the KEY on <mac> object MAC
  118. In our case, KEY is either a string or a bytevector."
  119. (let* ((key (match key
  120. ((? bytevector? key)
  121. key)
  122. ((? string? key)
  123. (string->utf8 key))))
  124. (err (%gcry-mac-setkey (mac->pointer mac)
  125. (bytevector->pointer key)
  126. (bytevector-length key))))
  127. (if (= err 0)
  128. #t
  129. (throw 'gcry-error 'mac-setkey err))))
  130. (define mac-close
  131. (let ((proc (libgcrypt->procedure void
  132. "gcry_mac_close"
  133. '(*)))) ; gcry_mac_hd_t H
  134. (lambda (mac)
  135. "Release all resources of MAC.
  136. Running this on an already closed <mac> might segfault :)"
  137. (proc (mac->pointer mac)))))
  138. (define mac-write
  139. (let ((proc (libgcrypt->procedure int
  140. "gcry_mac_write"
  141. `(* * ,size_t))))
  142. (lambda (mac obj)
  143. "Writes string or bytevector OBJ to MAC"
  144. (let* ((bv (match obj
  145. ((? bytevector? obj)
  146. obj)
  147. ((? string? obj)
  148. (string->utf8 obj))))
  149. (err (proc (mac->pointer mac)
  150. (bytevector->pointer bv)
  151. (bytevector-length bv))))
  152. (if (= err 0)
  153. #t
  154. (throw 'gcry-error 'mac-write err))))))
  155. (define mac-read
  156. (let ((proc (libgcrypt->procedure int
  157. "gcry_mac_read"
  158. `(* * *))))
  159. (lambda (mac algorithm)
  160. "Get bytevector representing result of MAC's written, signed data"
  161. (define (int-bv* n)
  162. ;; Get the pointer to a bytevector holding an integer with this number
  163. (let ((bv (make-bytevector (sizeof int))))
  164. (bytevector-uint-set! bv 0 n (native-endianness) (sizeof int))
  165. (bytevector->pointer bv)))
  166. (let* ((bv-len (mac-size algorithm))
  167. (bv (make-bytevector bv-len))
  168. (err (proc (mac->pointer mac)
  169. (bytevector->pointer bv)
  170. (int-bv* bv-len))))
  171. (if (= err 0)
  172. bv
  173. (throw 'gcry-error 'mac-read err))))))
  174. ;; GPG_ERR_CHECKSUM *should* be 10, but it seems to return here as
  175. ;; 16777226... unfortunately this is because we're pulling back an integer
  176. ;; rather than the gcry_error_t type.
  177. (define mac-verify
  178. (let ((proc (libgcrypt->procedure int
  179. "gcry_mac_verify"
  180. `(* * ,size_t))))
  181. (lambda (mac bv)
  182. "Verify that BV matches result calculated in MAC
  183. BV should be a bytevector with previously calculated data."
  184. (let ((err (proc (mac->pointer mac)
  185. (bytevector->pointer bv)
  186. (bytevector-length bv))))
  187. (if (= err 0)
  188. (values #t err)
  189. ;; TODO: This is WRONG! See the comment above
  190. ;; this procedure's definition for why. If we could
  191. ;; parse it as the appropriate GPG error, GPG_ERR_CHECKSUM
  192. ;; should be 10.
  193. (values #f err))))))
  194. (define* (sign-data key data #:key
  195. (algorithm (mac-algorithm hmac-sha512)))
  196. "Signs DATA with KEY for ALGORITHM. Returns a bytevector."
  197. (let ((mac (mac-open algorithm)))
  198. (mac-setkey mac key)
  199. (mac-write mac data)
  200. (let ((result (mac-read mac algorithm)))
  201. (mac-close mac)
  202. result)))
  203. (define* (sign-data-base64 key data #:key
  204. (algorithm (mac-algorithm hmac-sha512)))
  205. "Like sign-data, but conveniently encodes to base64."
  206. (base64-encode (sign-data key data #:algorithm algorithm)))
  207. (define* (valid-signature? key data sig
  208. #:key (algorithm (mac-algorithm hmac-sha512)))
  209. "Verify that DATA with KEY matches previous signature SIG for ALGORITHM."
  210. (let ((mac (mac-open algorithm)))
  211. (mac-setkey mac key)
  212. (mac-write mac data)
  213. (let ((result (mac-verify mac sig)))
  214. (mac-close mac)
  215. result)))
  216. (define* (valid-base64-signature? key data b64-sig
  217. #:key
  218. (algorithm (mac-algorithm hmac-sha512)))
  219. (valid-signature? key data
  220. (base64-decode b64-sig)
  221. #:algorithm algorithm))
  222. (define* (generate-signing-key #:optional (key-length 128))
  223. "Generate a signing key (a bytevector).
  224. KEY-LENGTH is the length, in bytes, of the key. The default is 128.
  225. This should be a multiple of 8."
  226. (gen-random-bv key-length %gcry-very-strong-random))