You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

291 lines
11 KiB

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