pk-crypto.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. ;;; guile-gcrypt --- crypto tooling for guile
  2. ;;; Copyright © 2013, 2014, 2017, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of guile-gcrypt.
  5. ;;;
  6. ;;; guile-gcrypt is free software; you can redistribute it and/or
  7. ;;; modify it under the terms of the GNU Lesser General Public License
  8. ;;; as published by the Free Software Foundation; either version 3 of
  9. ;;; the License, or (at your option) any later version.
  10. ;;;
  11. ;;; guile-gcrypt is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ;;; Lesser General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU Lesser General Public License
  17. ;;; along with guile-gcrypt. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (test-pk-crypto)
  19. #:use-module (gcrypt pk-crypto)
  20. #:use-module (gcrypt utils)
  21. #:use-module (gcrypt base16)
  22. #:use-module (gcrypt hash)
  23. #:use-module (gcrypt common) ;error codes
  24. #:use-module (srfi srfi-1)
  25. #:use-module (srfi srfi-11)
  26. #:use-module (srfi srfi-26)
  27. #:use-module (srfi srfi-64)
  28. #:use-module (rnrs bytevectors)
  29. #:use-module (rnrs io ports)
  30. #:use-module (ice-9 match))
  31. ;; Test the (guix pk-crypto) module.
  32. (define %key-pair
  33. ;; RSA key pair that was generated with:
  34. ;; (generate-key (string->canonical-sexp "(genkey (rsa (nbits 4:1024)))"))
  35. ;; which takes a bit of time.
  36. "(key-data
  37. (public-key
  38. (rsa
  39. (n #00C1F764069F54FFE93A126B02328903E984E4AE3AF6DF402B5B6B3907911B88C385F1BA76A002EC9DEA109A5228EF0E62EE31A06D1A5861CAB474F6C857AC66EB65A1905F25BBA1869579E73A3B7FED13AF5A1667326F88CDFC2FF24B03C14FD1384AA7E73CA89572880B606E3A974E15347963FC7B6378574936A47580DBCB45#)
  40. (e #010001#)))
  41. (private-key
  42. (rsa
  43. (n #00C1F764069F54FFE93A126B02328903E984E4AE3AF6DF402B5B6B3907911B88C385F1BA76A002EC9DEA109A5228EF0E62EE31A06D1A5861CAB474F6C857AC66EB65A1905F25BBA1869579E73A3B7FED13AF5A1667326F88CDFC2FF24B03C14FD1384AA7E73CA89572880B606E3A974E15347963FC7B6378574936A47580DBCB45#)
  44. (e #010001#)
  45. (d #58CAD84653D0046A8EC3F9AA82D9C829B145422109FC3F12DA01A694B92FA296E70D366FB166454D30E632CEE3A033B4C41781BA10325F69FCDC0250CA19C8EEB352FA085992494098DB133E682ED38A931701F0DED1A1E508F4341A4FB446A04F019427C7CB3C44F251EEA9D386100DA80F125E0FD5CE1B0DFEC6D21516EACD#)
  46. (p #00D47F185147EC39393CCDA4E7323FFC20FC8B8073E2A54DD63BA392A66975E4204CA48572496A9DFD7522436B852C07472A5AB25B7706F7C14E6F33FBC420FF3B#)
  47. (q #00E9AD22F158060BC9AE3601DA623AFC60FFF3058795802CA92371C00097335CF9A23D7782DE353C9DBA93D7BB99E6A24A411107605E722481C5C191F80D7EB77F#)
  48. (u #59B45B95AE01A7A7370FAFDB08FE73A4793CE37F228961B09B1B1E7DDAD9F8D3E28F5C5E8B4B067E6B8E0BBF3F690B42991A79E46108DDCDA2514323A66964DE#))))")
  49. (define %ecc-key-pair
  50. ;; Ed25519 key pair generated with:
  51. ;; (generate-key (string->canonical-sexp "(genkey (ecdsa (curve Ed25519) (flags rfc6979 transient)))"))
  52. "(key-data
  53. (public-key
  54. (ecc
  55. (curve Ed25519)
  56. (q #94869C1B9E69DB8DD910B7F7F4D6E56A63A964A59AE8F90F6703ACDDF6F50C81#)))
  57. (private-key
  58. (ecc
  59. (curve Ed25519)
  60. (q #94869C1B9E69DB8DD910B7F7F4D6E56A63A964A59AE8F90F6703ACDDF6F50C81#)
  61. (d #6EFB32D0B4EC6B3237B523539F1979379B82726AAA605EB2FBA6775B2B777B78#))))")
  62. (test-begin "pk-crypto")
  63. (test-assert "version"
  64. (gcrypt-version))
  65. (let ((sexps '("(foo bar)"
  66. ;; In Libgcrypt 1.5.3 the following integer is rendered as
  67. ;; binary, whereas in 1.6.0 it's rendered as is (hexadecimal.)
  68. ;;"#C0FFEE#"
  69. "(genkey \n (rsa \n (nbits \"1024\")\n )\n )")))
  70. (test-equal "string->canonical-sexp->string"
  71. sexps
  72. (let ((sexps (map string->canonical-sexp sexps)))
  73. (and (every canonical-sexp? sexps)
  74. (map (compose string-trim-both canonical-sexp->string) sexps)))))
  75. (gc) ; stress test!
  76. (let ((sexps `(("(foo bar)" foo -> "(foo bar)")
  77. ("(foo (bar (baz 3:123)))" baz -> "(baz \"123\")")
  78. ("(foo (bar 3:123))" baz -> #f))))
  79. (test-equal "find-sexp-token"
  80. (map (match-lambda
  81. ((_ _ '-> expected)
  82. expected))
  83. sexps)
  84. (map (match-lambda
  85. ((input token '-> _)
  86. (let ((sexp (find-sexp-token (string->canonical-sexp input) token)))
  87. (and sexp
  88. (string-trim-both (canonical-sexp->string sexp))))))
  89. sexps)))
  90. (gc)
  91. (test-equal "canonical-sexp-length"
  92. '(0 1 2 4 0 0)
  93. (map (compose canonical-sexp-length string->canonical-sexp)
  94. '("()" "(a)" "(a b)" "(a #616263# b #C001#)" "a" "#123456#")))
  95. (test-equal "canonical-sexp-list?"
  96. '(#t #f #t #f)
  97. (map (compose canonical-sexp-list? string->canonical-sexp)
  98. '("()" "\"abc\"" "(a b c)" "#123456#")))
  99. (gc)
  100. (test-equal "canonical-sexp-car + cdr"
  101. '("(b \n (c xyz)\n )")
  102. (let ((lst (string->canonical-sexp "(a (b (c xyz)))")))
  103. (map (lambda (sexp)
  104. (and sexp (string-trim-both (canonical-sexp->string sexp))))
  105. ;; Note: 'car' returns #f when the first element is an atom.
  106. (list (canonical-sexp-car (canonical-sexp-cdr lst))))))
  107. (gc)
  108. (test-equal "canonical-sexp-nth"
  109. '("(b pqr)" "(c \"456\")" "(d xyz)" #f #f)
  110. (let ((lst (string->canonical-sexp "(a (b 3:pqr) (c 3:456) (d 3:xyz))")))
  111. ;; XXX: In Libgcrypt 1.5.3, (canonical-sexp-nth lst 0) returns LST, whereas in
  112. ;; 1.6.0 it returns #f.
  113. (map (lambda (sexp)
  114. (and sexp (string-trim-both (canonical-sexp->string sexp))))
  115. (unfold (cut > <> 5)
  116. (cut canonical-sexp-nth lst <>)
  117. 1+
  118. 1))))
  119. (gc)
  120. (test-equal "canonical-sexp-nth-data"
  121. `(Name Otto Meier #f ,(base16-string->bytevector "123456") #f)
  122. (let ((lst (string->canonical-sexp
  123. "(Name Otto Meier (address Burgplatz) #123456#)")))
  124. (unfold (cut > <> 5)
  125. (cut canonical-sexp-nth-data lst <>)
  126. 1+
  127. 0)))
  128. (let ((bv (base16-string->bytevector
  129. "5eff0b55c9c5f5e87b4e34cd60a2d5654ca1eb78c7b3c67c3179fed1cff07b4c")))
  130. (test-equal "hash corrupt due to restrictive locale encoding"
  131. bv
  132. ;; In Guix up to 0.6 included this test would fail because at some point
  133. ;; the hash value would be cropped to ASCII. In practice 'guix
  134. ;; authenticate' would produce invalid signatures that would fail
  135. ;; signature verification. See <http://bugs.gnu.org/17312>.
  136. (let ((locale (setlocale LC_ALL)))
  137. (dynamic-wind
  138. (lambda ()
  139. (setlocale LC_ALL "C"))
  140. (lambda ()
  141. (hash-data->bytevector
  142. (string->canonical-sexp
  143. (canonical-sexp->string
  144. (bytevector->hash-data bv "sha256")))))
  145. (lambda ()
  146. (setlocale LC_ALL locale))))))
  147. (gc)
  148. ;; XXX: The test below is typically too long as it needs to gather enough entropy.
  149. ;; (test-assert "generate-key"
  150. ;; (let ((key (generate-key (string->canonical-sexp
  151. ;; "(genkey (rsa (nbits 3:128)))"))))
  152. ;; (and (canonical-sexp? key)
  153. ;; (find-sexp-token key 'key-data)
  154. ;; (find-sexp-token key 'public-key)
  155. ;; (find-sexp-token key 'private-key))))
  156. (test-assert "bytevector->hash-data->bytevector"
  157. (let* ((bv (sha256 (string->utf8 "Hello, world.")))
  158. (data (bytevector->hash-data bv "sha256")))
  159. (and (canonical-sexp? data)
  160. (let-values (((value algo) (hash-data->bytevector data)))
  161. (and (string=? algo "sha256")
  162. (bytevector=? value bv))))))
  163. (test-equal "key-type"
  164. '(rsa ecc)
  165. (map (compose key-type
  166. (cut find-sexp-token <> 'public-key)
  167. string->canonical-sexp)
  168. (list %key-pair %ecc-key-pair)))
  169. (test-equal "sign + verify, bogus signature"
  170. `(verify . ,(gcrypt-error error/invalid-object))
  171. (catch 'gcry-error
  172. (lambda ()
  173. (let* ((pair (string->canonical-sexp %key-pair))
  174. (secret (find-sexp-token pair 'private-key))
  175. (public (find-sexp-token pair 'public-key))
  176. (data (bytevector->hash-data
  177. (sha256 (string->utf8 "Hello, world."))
  178. #:key-type (key-type public)))
  179. (bogus (string->canonical-sexp "(bogus sig)")))
  180. (verify bogus data public)))
  181. (lambda (key proc error)
  182. (cons proc error))))
  183. (test-assert "sign + verify"
  184. (let* ((pair (string->canonical-sexp %key-pair))
  185. (secret (find-sexp-token pair 'private-key))
  186. (public (find-sexp-token pair 'public-key))
  187. (data (bytevector->hash-data
  188. (sha256 (string->utf8 "Hello, world."))
  189. #:key-type (key-type public)))
  190. (sig (sign data secret)))
  191. (and (verify sig data public)
  192. (not (verify sig
  193. (bytevector->hash-data
  194. (sha256 (string->utf8 "Hi!"))
  195. #:key-type (key-type public))
  196. public)))))
  197. (test-assert "sign + verify, Ed25519"
  198. (let* ((pair (string->canonical-sexp %ecc-key-pair))
  199. (secret (find-sexp-token pair 'private-key))
  200. (public (find-sexp-token pair 'public-key))
  201. (data (bytevector->hash-data
  202. (sha256 (string->utf8 "Hello, world."))))
  203. (sig (sign data secret)))
  204. (and (verify sig data public)
  205. (not (verify sig
  206. (bytevector->hash-data
  207. (sha256 (string->utf8 "Hi!")))
  208. public)))))
  209. (gc)
  210. (test-equal "canonical-sexp->sexp"
  211. `((data
  212. (flags pkcs1)
  213. (hash sha256
  214. ,(base16-string->bytevector
  215. "2749f0ea9f26c6c7be746a9cff8fa4c2f2a02b000070dba78429e9a11f87c6eb")))
  216. (public-key
  217. (rsa
  218. (n ,(base16-string->bytevector
  219. (string-downcase
  220. "00C1F764069F54FFE93A126B02328903E984E4AE3AF6DF402B5B6B3907911B88C385F1BA76A002EC9DEA109A5228EF0E62EE31A06D1A5861CAB474F6C857AC66EB65A1905F25BBA1869579E73A3B7FED13AF5A1667326F88CDFC2FF24B03C14FD1384AA7E73CA89572880B606E3A974E15347963FC7B6378574936A47580DBCB45")))
  221. (e ,(base16-string->bytevector
  222. "010001")))))
  223. (list (canonical-sexp->sexp
  224. (string->canonical-sexp
  225. "(data
  226. (flags pkcs1)
  227. (hash \"sha256\"
  228. #2749f0ea9f26c6c7be746a9cff8fa4c2f2a02b000070dba78429e9a11f87c6eb#))"))
  229. (canonical-sexp->sexp
  230. (find-sexp-token (string->canonical-sexp %key-pair)
  231. 'public-key))))
  232. (let ((lst
  233. `((data
  234. (flags pkcs1)
  235. (hash sha256
  236. ,(base16-string->bytevector
  237. "2749f0ea9f26c6c7be746a9cff8fa4c2f2a02b000070dba78429e9a11f87c6eb")))
  238. (public-key
  239. (rsa
  240. (n ,(base16-string->bytevector
  241. (string-downcase
  242. "00C1F764069F54FFE93A126B02328903E984E4AE3AF6DF402B5B6B3907911B88C385F1BA76A002EC9DEA109A5228EF0E62EE31A06D1A5861CAB474F6C857AC66EB65A1905F25BBA1869579E73A3B7FED13AF5A1667326F88CDFC2FF24B03C14FD1384AA7E73CA89572880B606E3A974E15347963FC7B6378574936A47580DBCB45")))
  243. (e ,(base16-string->bytevector
  244. "010001"))))
  245. ,(base16-string->bytevector
  246. "2749f0ea9f26c6c7be746a9cff8fa4c2f2a02b000070dba78429e9a11f87c6eb"))))
  247. (test-equal "sexp->canonical-sexp->sexp"
  248. lst
  249. (map (compose canonical-sexp->sexp sexp->canonical-sexp)
  250. lst)))
  251. (let ((sexp `(signature
  252. (public-key
  253. (rsa
  254. (n ,(make-bytevector 1024 1))
  255. (e ,(base16-string->bytevector "010001")))))))
  256. (test-equal "https://dev.gnupg.org/T1594"
  257. ;; The gcrypt bug above was primarily affecting our uses in
  258. ;; 'canonical-sexp->sexp', typically when applied to a signature sexp (in
  259. ;; 'guix authenticate -verify') with a "big" RSA key, such as 4096 bits.
  260. sexp
  261. (canonical-sexp->sexp (sexp->canonical-sexp sexp))))
  262. ;; In Guile-Gcrypt <= 0.2.1, 'canonical-sexp->sexp' did not support integers.
  263. (test-equal "sexp->canonical-sexp, big integers"
  264. '(a (b #vu8(#x01 #x23 #x45 #x67 #x89))
  265. (c #vu8(#x98 #x76 #x54 #x32 #x10)))
  266. (canonical-sexp->sexp
  267. (sexp->canonical-sexp
  268. '(a (b #x123456789) (c #x9876543210)))))
  269. (test-end)