99f37d2b6bfd103d5ea081ca5a63af8d3c015eca
[openssl.git] / test / ecdhtest.c
1 /*
2  * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /* ====================================================================
11  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
12  *
13  * The Elliptic Curve Public-Key Crypto Library (ECC Code) included
14  * herein is developed by SUN MICROSYSTEMS, INC., and is contributed
15  * to the OpenSSL project.
16  *
17  * The ECC Code is licensed pursuant to the OpenSSL open source
18  * license provided below.
19  *
20  * The ECDH software is originally written by Douglas Stebila of
21  * Sun Microsystems Laboratories.
22  *
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "../e_os.h"
30
31 #include <openssl/opensslconf.h> /* for OPENSSL_NO_EC */
32 #include <openssl/crypto.h>
33 #include <openssl/bio.h>
34 #include <openssl/bn.h>
35 #include <openssl/objects.h>
36 #include <openssl/rand.h>
37 #include <openssl/sha.h>
38 #include <openssl/err.h>
39
40 #ifdef OPENSSL_NO_EC
41 int main(int argc, char *argv[])
42 {
43     printf("No ECDH support\n");
44     return (0);
45 }
46 #else
47 # include <openssl/ec.h>
48
49 static const char rnd_seed[] =
50     "string to make the random number generator think it has entropy";
51
52 typedef struct {
53     const int nid;
54     const char *da;
55     const char *db;
56     const char *Z;
57 } ecdh_kat_t;
58
59 static const ecdh_kat_t ecdh_kats[] = {
60     /* Keys and shared secrets from RFC 5114 */
61     { NID_X9_62_prime192v1,
62     "323FA3169D8E9C6593F59476BC142000AB5BE0E249C43426",
63     "631F95BB4A67632C9C476EEE9AB695AB240A0499307FCF62",
64     "AD420182633F8526BFE954ACDA376F05E5FF4F837F54FEBE" },
65     { NID_secp224r1,
66     "B558EB6C288DA707BBB4F8FBAE2AB9E9CB62E3BC5C7573E22E26D37F",
67     "AC3B1ADD3D9770E6F6A708EE9F3B8E0AB3B480E9F27F85C88B5E6D18",
68     "52272F50F46F4EDC9151569092F46DF2D96ECC3B6DC1714A4EA949FA" },
69     { NID_X9_62_prime256v1,
70     "814264145F2F56F2E96A8E337A1284993FAF432A5ABCE59E867B7291D507A3AF",
71     "2CE1788EC197E096DB95A200CC0AB26A19CE6BCCAD562B8EEE1B593761CF7F41",
72     "DD0F5396219D1EA393310412D19A08F1F5811E9DC8EC8EEA7F80D21C820C2788" },
73     { NID_secp384r1,
74     "D27335EA71664AF244DD14E9FD1260715DFD8A7965571C48D709EE7A7962A156"
75     "D706A90CBCB5DF2986F05FEADB9376F1",
76     "52D1791FDB4B70F89C0F00D456C2F7023B6125262C36A7DF1F80231121CCE3D3"
77     "9BE52E00C194A4132C4A6C768BCD94D2",
78     "5EA1FC4AF7256D2055981B110575E0A8CAE53160137D904C59D926EB1B8456E4"
79     "27AA8A4540884C37DE159A58028ABC0E" },
80     { NID_secp521r1,
81     "0113F82DA825735E3D97276683B2B74277BAD27335EA71664AF2430CC4F33459"
82     "B9669EE78B3FFB9B8683015D344DCBFEF6FB9AF4C6C470BE254516CD3C1A1FB4"
83     "7362",
84     "00CEE3480D8645A17D249F2776D28BAE616952D1791FDB4B70F7C3378732AA1B"
85     "22928448BCD1DC2496D435B01048066EBE4F72903C361B1A9DC1193DC2C9D089"
86     "1B96",
87     "00CDEA89621CFA46B132F9E4CFE2261CDE2D4368EB5656634C7CC98C7A00CDE5"
88     "4ED1866A0DD3E6126C9D2F845DAFF82CEB1DA08F5D87521BB0EBECA77911169C"
89     "20CC" },
90     /* Keys and shared secrets from RFC 5903 */
91     { NID_X9_62_prime256v1,
92     "C88F01F510D9AC3F70A292DAA2316DE544E9AAB8AFE84049C62A9C57862D1433",
93     "C6EF9C5D78AE012A011164ACB397CE2088685D8F06BF9BE0B283AB46476BEE53",
94     "D6840F6B42F6EDAFD13116E0E12565202FEF8E9ECE7DCE03812464D04B9442DE" },
95     { NID_secp384r1,
96     "099F3C7034D4A2C699884D73A375A67F7624EF7C6B3C0F160647B67414DCE655"
97     "E35B538041E649EE3FAEF896783AB194",
98     "41CB0779B4BDB85D47846725FBEC3C9430FAB46CC8DC5060855CC9BDA0AA2942"
99     "E0308312916B8ED2960E4BD55A7448FC",
100     "11187331C279962D93D604243FD592CB9D0A926F422E47187521287E7156C5C4"
101     "D603135569B9E9D09CF5D4A270F59746" },
102     { NID_secp521r1,
103     "0037ADE9319A89F4DABDB3EF411AACCCA5123C61ACAB57B5393DCE47608172A0"
104     "95AA85A30FE1C2952C6771D937BA9777F5957B2639BAB072462F68C27A57382D"
105     "4A52",
106     "0145BA99A847AF43793FDD0E872E7CDFA16BE30FDC780F97BCCC3F078380201E"
107     "9C677D600B343757A3BDBF2A3163E4C2F869CCA7458AA4A4EFFC311F5CB15168"
108     "5EB9",
109     "01144C7D79AE6956BC8EDB8E7C787C4521CB086FA64407F97894E5E6B2D79B04"
110     "D1427E73CA4BAA240A34786859810C06B3C715A3A8CC3151F2BEE417996D19F3"
111     "DDEA" },
112     /* Keys and shared secrets from RFC 7027 */
113     { NID_brainpoolP256r1,
114     "81DB1EE100150FF2EA338D708271BE38300CB54241D79950F77B063039804F1D",
115     "55E40BC41E37E3E2AD25C3C6654511FFA8474A91A0032087593852D3E7D76BD3",
116     "89AFC39D41D3B327814B80940B042590F96556EC91E6AE7939BCE31F3A18BF2B" },
117     { NID_brainpoolP384r1,
118     "1E20F5E048A5886F1F157C74E91BDE2B98C8B52D58E5003D57053FC4B0BD65D6"
119     "F15EB5D1EE1610DF870795143627D042",
120     "032640BC6003C59260F7250C3DB58CE647F98E1260ACCE4ACDA3DD869F74E01F"
121     "8BA5E0324309DB6A9831497ABAC96670",
122     "0BD9D3A7EA0B3D519D09D8E48D0785FB744A6B355E6304BC51C229FBBCE239BB"
123     "ADF6403715C35D4FB2A5444F575D4F42" },
124     { NID_brainpoolP512r1,
125     "16302FF0DBBB5A8D733DAB7141C1B45ACBC8715939677F6A56850A38BD87BD59"
126     "B09E80279609FF333EB9D4C061231FB26F92EEB04982A5F1D1764CAD57665422",
127     "230E18E1BCC88A362FA54E4EA3902009292F7F8033624FD471B5D8ACE49D12CF"
128     "ABBC19963DAB8E2F1EBA00BFFB29E4D72D13F2224562F405CB80503666B25429",
129     "A7927098655F1F9976FA50A9D566865DC530331846381C87256BAF3226244B76"
130     "D36403C024D7BBF0AA0803EAFF405D3D24F11A9B5C0BEF679FE1454B21C4CD1F" }
131 };
132
133 /* Given private value and NID, create EC_KEY structure */
134
135 static EC_KEY *mk_eckey(int nid, const char *str)
136 {
137     int ok = 0;
138     EC_KEY *k = NULL;
139     BIGNUM *priv = NULL;
140     EC_POINT *pub = NULL;
141     const EC_GROUP *grp;
142     k = EC_KEY_new_by_curve_name(nid);
143     if (!k)
144         goto err;
145     if(!BN_hex2bn(&priv, str))
146         goto err;
147     if (!priv)
148         goto err;
149     if (!EC_KEY_set_private_key(k, priv))
150         goto err;
151     grp = EC_KEY_get0_group(k);
152     pub = EC_POINT_new(grp);
153     if (!pub)
154         goto err;
155     if (!EC_POINT_mul(grp, pub, priv, NULL, NULL, NULL))
156         goto err;
157     if (!EC_KEY_set_public_key(k, pub))
158         goto err;
159     ok = 1;
160  err:
161     BN_clear_free(priv);
162     EC_POINT_free(pub);
163     if (ok)
164         return k;
165     EC_KEY_free(k);
166     return NULL;
167 }
168
169 /*
170  * Known answer test: compute shared secret and check it matches expected
171  * value.
172  */
173
174 static int ecdh_kat(BIO *out, const ecdh_kat_t *kat)
175 {
176     int rv = 0;
177     EC_KEY *key1 = NULL, *key2 = NULL;
178     BIGNUM *bnz = NULL;
179     unsigned char *Ztmp = NULL, *Z = NULL;
180     size_t Ztmplen, Zlen;
181     BIO_puts(out, "Testing ECDH shared secret with ");
182     BIO_puts(out, OBJ_nid2sn(kat->nid));
183     if(!BN_hex2bn(&bnz, kat->Z))
184         goto err;
185     key1 = mk_eckey(kat->nid, kat->da);
186     key2 = mk_eckey(kat->nid, kat->db);
187     if (!key1 || !key2)
188         goto err;
189     Ztmplen = (EC_GROUP_get_degree(EC_KEY_get0_group(key1)) + 7) / 8;
190     Zlen = BN_num_bytes(bnz);
191     if (Zlen > Ztmplen)
192         goto err;
193     if((Ztmp = OPENSSL_zalloc(Ztmplen)) == NULL)
194         goto err;
195     if((Z = OPENSSL_zalloc(Ztmplen)) == NULL)
196         goto err;
197     if(!BN_bn2binpad(bnz, Z, Ztmplen))
198         goto err;
199     if (!ECDH_compute_key(Ztmp, Ztmplen,
200                           EC_KEY_get0_public_key(key2), key1, 0))
201         goto err;
202     if (memcmp(Ztmp, Z, Ztmplen))
203         goto err;
204     memset(Ztmp, 0, Ztmplen);
205     if (!ECDH_compute_key(Ztmp, Ztmplen,
206                           EC_KEY_get0_public_key(key1), key2, 0))
207         goto err;
208     if (memcmp(Ztmp, Z, Ztmplen))
209         goto err;
210     rv = 1;
211  err:
212     EC_KEY_free(key1);
213     EC_KEY_free(key2);
214     OPENSSL_free(Ztmp);
215     OPENSSL_free(Z);
216     BN_free(bnz);
217     if (rv)
218         BIO_puts(out, " ok\n");
219     else {
220         fprintf(stderr, "Error in ECDH routines\n");
221         ERR_print_errors_fp(stderr);
222     }
223     return rv;
224 }
225
226 #include "ecdhtest_cavs.h"
227
228 /*
229  * NIST SP800-56A co-factor ECDH tests.
230  * KATs taken from NIST documents with parameters:
231  *
232  * - (QCAVSx,QCAVSy) is the public key for CAVS.
233  * - dIUT is the private key for IUT.
234  * - (QIUTx,QIUTy) is the public key for IUT.
235  * - ZIUT is the shared secret KAT.
236  *
237  * CAVS: Cryptographic Algorithm Validation System
238  * IUT: Implementation Under Test
239  *
240  * This function tests two things:
241  *
242  * 1. dIUT * G = (QIUTx,QIUTy)
243  *    i.e. public key for IUT computes correctly.
244  * 2. x-coord of cofactor * dIUT * (QCAVSx,QCAVSy) = ZIUT
245  *    i.e. co-factor ECDH key computes correctly.
246  *
247  * returns zero on failure or unsupported curve. One otherwise.
248  */
249 static int ecdh_cavs_kat(BIO *out, const ecdh_cavs_kat_t *kat)
250 {
251     int rv = 0, is_char_two = 0;
252     EC_KEY *key1 = NULL;
253     EC_POINT *pub = NULL;
254     const EC_GROUP *group = NULL;
255     BIGNUM *bnz = NULL, *x = NULL, *y = NULL;
256     unsigned char *Ztmp = NULL, *Z = NULL;
257     size_t Ztmplen, Zlen;
258     BIO_puts(out, "Testing ECC CDH Primitive SP800-56A with ");
259     BIO_puts(out, OBJ_nid2sn(kat->nid));
260
261     /* dIUT is IUT's private key */
262     if ((key1 = mk_eckey(kat->nid, kat->dIUT)) == NULL)
263         goto err;
264     /* these are cofactor ECDH KATs */
265     EC_KEY_set_flags(key1, EC_FLAG_COFACTOR_ECDH);
266
267     if ((group = EC_KEY_get0_group(key1)) == NULL)
268         goto err;
269     if ((pub = EC_POINT_new(group)) == NULL)
270         goto err;
271
272     if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_characteristic_two_field)
273         is_char_two = 1;
274
275     /* (QIUTx, QIUTy) is IUT's public key */
276     if(!BN_hex2bn(&x, kat->QIUTx))
277         goto err;
278     if(!BN_hex2bn(&y, kat->QIUTy))
279         goto err;
280     if (is_char_two) {
281 #ifdef OPENSSL_NO_EC2M
282         goto err;
283 #else
284         if (!EC_POINT_set_affine_coordinates_GF2m(group, pub, x, y, NULL))
285             goto err;
286 #endif
287     }
288     else {
289         if (!EC_POINT_set_affine_coordinates_GFp(group, pub, x, y, NULL))
290             goto err;
291     }
292     /* dIUT * G = (QIUTx, QIUTy) should hold */
293     if (EC_POINT_cmp(group, EC_KEY_get0_public_key(key1), pub, NULL))
294         goto err;
295
296     /* (QCAVSx, QCAVSy) is CAVS's public key */
297     if(!BN_hex2bn(&x, kat->QCAVSx))
298         goto err;
299     if(!BN_hex2bn(&y, kat->QCAVSy))
300         goto err;
301     if (is_char_two) {
302 #ifdef OPENSSL_NO_EC2M
303         goto err;
304 #else
305         if (!EC_POINT_set_affine_coordinates_GF2m(group, pub, x, y, NULL))
306             goto err;
307 #endif
308     }
309     else {
310         if (!EC_POINT_set_affine_coordinates_GFp(group, pub, x, y, NULL))
311             goto err;
312     }
313
314     /* ZIUT is the shared secret */
315     if(!BN_hex2bn(&bnz, kat->ZIUT))
316         goto err;
317     Ztmplen = (EC_GROUP_get_degree(EC_KEY_get0_group(key1)) + 7) / 8;
318     Zlen = BN_num_bytes(bnz);
319     if (Zlen > Ztmplen)
320         goto err;
321     if((Ztmp = OPENSSL_zalloc(Ztmplen)) == NULL)
322         goto err;
323     if((Z = OPENSSL_zalloc(Ztmplen)) == NULL)
324         goto err;
325     if(!BN_bn2binpad(bnz, Z, Ztmplen))
326         goto err;
327     if (!ECDH_compute_key(Ztmp, Ztmplen, pub, key1, 0))
328         goto err;
329     /* shared secrets should be identical */
330     if (memcmp(Ztmp, Z, Ztmplen))
331         goto err;
332     rv = 1;
333  err:
334     EC_KEY_free(key1);
335     EC_POINT_free(pub);
336     BN_free(bnz);
337     BN_free(x);
338     BN_free(y);
339     OPENSSL_free(Ztmp);
340     OPENSSL_free(Z);
341     if (rv) {
342         BIO_puts(out, " ok\n");
343     }
344     else {
345         fprintf(stderr, "Error in ECC CDH routines\n");
346         ERR_print_errors_fp(stderr);
347     }
348     return rv;
349 }
350
351 int main(int argc, char *argv[])
352 {
353     BN_CTX *ctx = NULL;
354     int ret = 1;
355     EC_builtin_curve *curves = NULL;
356     size_t crv_len = 0, n = 0;
357     BIO *out;
358
359     CRYPTO_set_mem_debug(1);
360     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
361
362     RAND_seed(rnd_seed, sizeof rnd_seed);
363
364     out = BIO_new(BIO_s_file());
365     if (out == NULL)
366         EXIT(1);
367     BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
368
369     if ((ctx = BN_CTX_new()) == NULL)
370         goto err;
371
372     /* get a list of all internal curves */
373     crv_len = EC_get_builtin_curves(NULL, 0);
374     curves = OPENSSL_malloc(sizeof(*curves) * crv_len);
375     if (curves == NULL) goto err;
376
377     if (!EC_get_builtin_curves(curves, crv_len)) goto err;
378
379     /* NAMED CURVES TESTS: moved to evptests.txt */
380
381     /* KATs */
382     for (n = 0; n < (sizeof(ecdh_kats)/sizeof(ecdh_kat_t)); n++) {
383         if (!ecdh_kat(out, &ecdh_kats[n]))
384             goto err;
385     }
386
387     /* NIST SP800-56A co-factor ECDH KATs */
388     for (n = 0; n < (sizeof(ecdh_cavs_kats)/sizeof(ecdh_cavs_kat_t)); n++) {
389         if (!ecdh_cavs_kat(out, &ecdh_cavs_kats[n]))
390             goto err;
391     }
392
393     ret = 0;
394
395  err:
396     ERR_print_errors_fp(stderr);
397     OPENSSL_free(curves);
398     BN_CTX_free(ctx);
399     BIO_free(out);
400
401 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
402     if (CRYPTO_mem_leaks_fp(stderr) <= 0)
403         ret = 1;
404 #endif
405     EXIT(ret);
406 }
407 #endif