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