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