chiark / gitweb /
doc: Document summary values of TOFU_STATS
[gnupg2.git] / tests / openpgp / ecc.scm
1 #!/usr/bin/env gpgscm
2
3 ;; Copyright (C) 2016 g10 Code GmbH
4 ;;
5 ;; This file is part of GnuPG.
6 ;;
7 ;; GnuPG is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 3 of the License, or
10 ;; (at your option) any later version.
11 ;;
12 ;; GnuPG is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 ;; GNU General Public License for more details.
16 ;;
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program; if not, see <http://www.gnu.org/licenses/>.
19
20 (load (with-path "defs.scm"))
21 (setup-legacy-environment)
22
23 (define keygrips '("8E06A180EFFE4C65B812150CAF19BF30C0689A4C"
24                    "E4403F3FD7A443FAC29FEF288FA0D20AC212851E"
25                    "0B7554421FFB14A06CB9F63FB49A85A58E97ABAC"
26                    "303ACC892C2D786C8A789677C0BE54DA8538F903"
27                    "9FE5C36985351524B6AFA19FDCBC1A3A750B6F5F"
28                    "145A52CC7ED3FD41C5B0A26BE220FEED36AF24DE"))
29 (define mainkeyids '("BAA59D9C" "0F54719F" "45AF2FFE"))
30
31 (unless (have-pubkey-algo? "ECDH")
32         (skip "No ECC support due to an old Libgcrypt"))
33
34 (info "Preparing for ECC test")
35 (for-each
36  (lambda (grip)
37    (catch '() (unlink (string-append "private-keys-v1.d/" grip ".key")))
38    (call-check `(,(tool 'gpg-preset-passphrase)
39                  --preset --passphrase ecc ,grip)))
40  keygrips)
41
42 (info "Importing ECC public keys")
43 (for-each
44  (lambda (keyid)
45    (call `(,(tool 'gpg) --delete-key --batch --yes ,keyid)))
46  mainkeyids)
47
48 (for-each
49  (lambda (n)
50    (call-check `(,(tool 'gpg) --import
51                  ,(in-srcdir (string-append
52                               "samplekeys/ecc-sample-"
53                               (number->string n)
54                               "-pub.asc")))))
55  '(1 2 3))
56
57 ;; The following is an opaque ECDSA signature on a message "This is one
58 ;; line\n" (17 byte long) by the primary 256 bit key:
59 (define msg_opaque_signed_256 "-----BEGIN PGP MESSAGE-----
60 Version: GnuPG v2.1.0-ecc (GNU/Linux)
61
62 owGbwMvMwCHMvVT3w66lc+cwrlFK4k5N1k3KT6nUK6ko8Zl8MSEkI7NYAYjy81IV
63 cjLzUrk64lgYhDkY2FiZQNIMXJwCMO31rxgZ+tW/zesUPxWzdKWrtLGW/LkP5rXL
64 V/Yvnr/EKjBbQuvZSYa/klsum6XFmTze+maVgclT6Rc6hzqqxNy6o6qdTTmLJuvp
65 AQA=
66 =GDv4
67 -----END PGP MESSAGE----")
68
69 ;; The following is an opaque ECDSA signature on a message "This is one
70 ;; line\n" (17 byte long) by the primary 384 bit key:
71 (define msg_opaque_signed_384 "-----BEGIN PGP MESSAGE-----
72 Version: PGP Command Line v10.0.0 (Linux)
73
74 qANQR1DIqwE7wsvMwCnM2WDcwR9SOJ/xtFISd25qcXFieqpeSUUJAxCEZGQWKwBR
75 fl6qQk5mXirXoXJmVgbfYC5xmC5hzsDPjHXqbDLzpXpTBXSZV3L6bAgP3Kq7Ykmo
76 7Ds1v4UfBS+3CSSon7Pzq79WLjzXXEH54MkjPxnrw+8cfMVnY7Bi18J702Nnsa7a
77 9lMv/PM0/ao9CZ3KX7Q+Tv1rllTZ5Hj4V1frw431QnHfAA==
78 =elKT
79 -----END PGP MESSAGE-----")
80
81 ;; The following is an opaque ECDSA signature on a message "This is one
82 ;; line\n" (17 byte long) by the primary 521 bit key:
83 (define msg_opaque_signed_521 "-----BEGIN PGP MESSAGE-----
84 Version: PGP Command Line v10.0.0 (Linux)
85
86 qANQR1DIwA8BO8LLzMAlnO3Y8tB1vf4/xtNKSdy5qcXFiempeiUVJQxAEJKRWawA
87 RPl5qQo5mXmpXIdmMLMy+AaLnoLpEubatpeJY2Lystd7Qt32q2UcvRS5kNPWtDB7
88 ryufvcrWtFM7Jx8qXKDxZuqr7b9PGv1Ssk+I8TzB2O9dZC+n/jv+PAdbuu7mLe33
89 Gf9pLd3weV3Qno6FOqxGa5ZszQx+uer2xH3/El9x/2pVeO4l15ScsL7qWMTmffmG
90 Ic1RdzgeCfosMF+l/zVRchcLKzenEQA=
91 =ATtX
92 -----END PGP MESSAGE-----")
93
94 (lettmp (z)
95   (letfd ((fd (open z (logior O_WRONLY O_CREAT O_BINARY) #o600)))
96          (display "This is one line\n" (fdopen fd "wb")))
97
98   (for-each-p
99    "Checking opaque ECDSA signatures"
100    (lambda (test)
101      (lettmp (x y)
102        (call-with-output-file
103            x (lambda (p) (display (eval test (current-environment)) p)))
104        (call-check `(,(tool 'gpg) --verify ,x))
105        (call-check `(,(tool 'gpg) --output ,y ,x))
106        (unless (file=? y z) (fail "mismatch"))))
107    '(msg_opaque_signed_256 msg_opaque_signed_384 msg_opaque_signed_521)))
108
109 ;;
110 ;; Import the secret keys so that we now can sign and decrypt.
111 ;;
112 ;; Note that the PGP generated secret keys are not self-signed, thus we
113 ;; need to pass an appropriate option.
114 ;;
115 (info "Importing ECC secret keys")
116 (setenv "PINENTRY_USER_DATA" "ecc" #t)
117 (for-each
118  (lambda (n)
119    (call-check `(,(tool 'gpg) --import
120                  ,@(if (> n 1) '(--allow-non-selfsigned-uid) '())
121                  ,(in-srcdir (string-append
122                               "samplekeys/ecc-sample-"
123                               (number->string n)
124                               "-sec.asc")))))
125  '(1 2 3))
126
127 ;;
128 ;; Check a few sample encrtpted messages.
129 ;;
130 (info "Checking ECC encryption")
131
132 ;; The following block encrypts the text "This is one line\n", 17 bytes,
133 ;; with the subkey 4089AB73.
134 (define msg_encrypted_256 "-----BEGIN PGP MESSAGE-----
135 Version: GnuPG v2.1.0-ecc (GNU/Linux)
136
137 hH4Dd863o0CJq3MSAgMEHdIYZQx+rV1cjy7qitIOEICFFzp4cjsRX4r+rDdMcQUs
138 h7VZmbP1c9C0s9sgCKwubWfkcYUl2ZOju4gy+s4MYTBb4/j8JjnJ9Bqn6LWutTXJ
139 zwsdP13VIJLnhiNqISdR3/6xWQ0ICRYzwb95nUZ1c1DSVgFpjPgUvi4pgYbTpcDB
140 jzILKWBfBDT/jck169XE8vgtbcqVQYZ7lZpaY9CzEbC+4dXZmV1gm5MafpTyFWgH
141 VnyrZB4gad9Lp9e0RKHHcOOE7s/NeLuu
142 =odUZ
143 -----END PGP MESSAGE-----")
144
145 ;; The following block encrypts the text "This is one line\n", 17 bytes,
146 ;; with the subkey 9A201946:
147 (define msg_encrypted_384 "-----BEGIN PGP MESSAGE-----
148 Version: PGP Command Line v10.0.0 (Linux)
149
150 qANQR1DBngOqi5OPmiAZRhIDAwQqIr/00cJyf+QP+VA4QKVkk77KMHdz9OVaR2XK
151 0VYu0F/HPm89vL2orfm2hrAZxY9G2R0PG4Wk5Lg04UjKca/O72uWtjdPYulFidmo
152 uB0QpzXFz22ZZinxeVPLPEr19Pow0EwCc95cg4HAgrD0nV9vRcTJ/+juVfvsJhAO
153 isMKqrFNMvwnK5A1ECeyVXe7oLZl0lUBRhLr59QTtvf85QJjg/m5kaGy8XCJvLv3
154 61pZa6KUmw89PjtPak7ebcjnINL01vwmyeg1PAyW/xjeGGvcO+R4P1b4ewyFnJyR
155 svzIJcP7d4DqYOw7
156 =oiTJ
157 -----END PGP MESSAGE-----")
158
159 ;; The following block encrypts the text "This is one line\n", 17 bytes,
160 ;; with the subkey A81C4838:
161 (define msg_encrypted_521 "-----BEGIN PGP MESSAGE-----
162 Version: PGP Command Line v10.0.0 (Linux)
163
164 qANQR1DBwAIDB+qqSKgcSDgSBCMEAKpzTUxB4c56C7g09ekD9I+ttC5ER/xzDmXU
165 OJmFqU5w3FllhFj4TgGxxdH+8fv4W2Ag0IKoJvIY9V1V7oUCClfqAR01QbN7jGH/
166 I9GFFnH19AYEgMKgFmh14ZwN1BS6/VHh+H4apaYqapbx8/09EL+DV9zWLX4GRLXQ
167 VqCR1N2rXE29MJFzGmDOCueQNkUjcbuenoCSKcNT+6xhO27U9IYVCg4BhRUDGfD6
168 dhfRzBLxL+bKR9JVAe46+K8NLjRVu/bd4Iounx4UF5dBk8ERy+/8k9XantDoQgo6
169 RPqCad4Dg/QqkpbK3y574ds3VFNJmc4dVpsXm7lGV5w0FBxhVNPoWNhhECMlTroX
170 Rg==
171 =5GqW
172 -----END PGP MESSAGE-----")
173
174 (lettmp (z)
175   (letfd ((fd (open z (logior O_WRONLY O_CREAT O_BINARY) #o600)))
176          (display "This is one line\n" (fdopen fd "wb")))
177
178   (for-each-p
179    "Checking ECDSA decryption"
180    (lambda (test)
181      (lettmp (x y)
182        (call-with-output-file
183            x (lambda (p) (display (eval test (current-environment)) p)))
184        (call-check `(,@GPG --yes --output ,y ,x))
185        (unless (file=? y z) (fail "mismatch"))))
186    '(msg_encrypted_256 msg_encrypted_384 msg_encrypted_521)))
187
188 ;;
189 ;; Now check that we can encrypt and decrypt our own messages.
190 ;;
191 ;; Note that we don't need to provide a passphrase because we already
192 ;; preset the passphrase into the gpg-agent.
193 ;;
194 (for-each-p
195  "Checking ECC encryption and decryption"
196  (lambda (source)
197    (for-each-p
198     ""
199     (lambda (keyid)
200       (tr:do
201        (tr:open source)
202        (tr:gpg "" `(--yes --encrypt --recipient ,keyid))
203        (tr:gpg "" '(--yes))
204        (tr:assert-identity source)))
205     mainkeyids))
206  (append plain-files data-files))
207
208 ;;
209 ;; Now check that we can sign and verify our own messages.
210 ;;
211 (for-each-p
212  "Checking ECC signing and verifiction"
213  (lambda (source)
214    (for-each-p
215     ""
216     (lambda (keyid)
217       (tr:do
218        (tr:open source)
219        (tr:gpg "" `(--yes --sign --local-user ,keyid))
220        (tr:gpg "" '(--yes))
221        (tr:assert-identity source)))
222     mainkeyids))
223  (append plain-files data-files))
224
225 ;;
226 ;; Let us also try to import the keys only from a secret keyblock.
227 ;;
228 ;; Because PGP does not sign the UID, it is not very useful to work
229 ;; with this key unless we go into the trouble of adding the
230 ;; self-signature.
231 ;;
232 (info "Importing ECC secret keys directly")
233 (for-each
234  (lambda (keyid)
235    (catch '() (unlink (string-append "private-keys-v1.d/" keyid ".key"))))
236  keygrips)
237 (for-each
238  (lambda (keyid)
239    (call `(,(tool 'gpg) --delete-key --batch --yes ,keyid)))
240  mainkeyids)
241
242 (for-each
243  (lambda (n)
244    (call-check `(,(tool 'gpg) --import
245                  ,@(if (> n 1) '(--allow-non-selfsigned-uid) '())
246                  ,(in-srcdir (string-append
247                               "samplekeys/ecc-sample-"
248                               (number->string n)
249                               "-sec.asc")))))
250  '(1 2 3))