1 /* fips/ecdh/fips_ecdh_selftest.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2011 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
55 #define OPENSSL_FIPSAPI
58 #include <openssl/crypto.h>
59 #include <openssl/ec.h>
60 #include <openssl/ecdh.h>
61 #include <openssl/fips.h>
62 #include <openssl/err.h>
63 #include <openssl/evp.h>
64 #include <openssl/bn.h>
68 #include "fips_locl.h"
71 static const unsigned char p224_qcavsx[] = {
72 0x3c,0x81,0x15,0x16,0xab,0xa6,0xad,0xd7,0xe5,0xf3,0xea,0x1f,
73 0x88,0x57,0x43,0x29,0x35,0x6f,0x0a,0xd2,0x38,0xc7,0x11,0x8a,
77 static const unsigned char p224_qcavsy[] = {
78 0x4a,0x87,0x54,0x7b,0x7d,0x69,0xdd,0xb8,0x48,0x73,0xb2,0x1e,
79 0x33,0xfa,0xf6,0x32,0xb4,0x25,0x73,0x55,0x87,0x08,0x16,0xd2,
83 static const unsigned char p224_qiutx[] = {
84 0x23,0xff,0x15,0x91,0x83,0xd6,0xad,0x98,0x93,0x98,0xbd,0x2e,
85 0x01,0xeb,0x5a,0x45,0xe2,0x2a,0xf9,0xc5,0x3b,0x37,0xe1,0x87,
89 static const unsigned char p224_qiuty[] = {
90 0x5e,0x70,0xb7,0x9d,0x9e,0x55,0x2d,0x67,0x4e,0x29,0xa4,0x9d,
91 0x06,0x81,0x11,0xb4,0xb4,0xab,0xe2,0xdf,0xdc,0xe4,0xf1,0x69,
95 static const unsigned char p224_qiutd[] = {
96 0xd7,0xdc,0x9c,0x53,0x04,0x72,0x67,0x59,0x92,0x80,0x9e,0x6f,
97 0xdd,0xe6,0x0b,0x35,0x09,0xe0,0x95,0x45,0xe6,0x13,0x0e,0x22,
101 static const unsigned char p224_ziut[] = {
102 0x84,0x37,0xcf,0x6d,0xfa,0x58,0xbd,0x1f,0x47,0x15,0x45,0x1f,
103 0x2c,0x20,0x53,0x7a,0xf4,0xb0,0xe6,0x19,0xcc,0xa9,0x30,0xc6,
110 const unsigned char *x1;
112 const unsigned char *y1;
114 const unsigned char *d1;
116 const unsigned char *x2;
118 const unsigned char *y2;
120 const unsigned char *z;
122 } ECDH_SELFTEST_DATA;
124 #define make_ecdh_test(nid, pr) { nid, \
125 pr##_qiutx, sizeof(pr##_qiutx), \
126 pr##_qiuty, sizeof(pr##_qiuty), \
127 pr##_qiutd, sizeof(pr##_qiutd), \
128 pr##_qcavsx, sizeof(pr##_qcavsx), \
129 pr##_qcavsy, sizeof(pr##_qcavsy), \
130 pr##_ziut, sizeof(pr##_ziut) }
132 static ECDH_SELFTEST_DATA test_ecdh_data[] =
134 make_ecdh_test(NID_secp224r1, p224),
137 int FIPS_selftest_ecdh(void)
139 EC_KEY *ec1 = NULL, *ec2 = NULL;
140 const EC_POINT *ecp = NULL;
141 BIGNUM *x = NULL, *y = NULL, *d = NULL;
142 unsigned char *ztmp = NULL;
146 for (i = 0; i < sizeof(test_ecdh_data)/sizeof(ECDH_SELFTEST_DATA); i++)
148 ECDH_SELFTEST_DATA *ecd = test_ecdh_data + i;
149 if (!fips_post_started(FIPS_TEST_ECDH, ecd->curve, 0))
151 ztmp = OPENSSL_malloc(ecd->zlen);
153 x = BN_bin2bn(ecd->x1, ecd->x1len, x);
154 y = BN_bin2bn(ecd->y1, ecd->y1len, y);
155 d = BN_bin2bn(ecd->d1, ecd->d1len, d);
157 if (!x || !y || !d || !ztmp)
163 ec1 = EC_KEY_new_by_curve_name(ecd->curve);
169 EC_KEY_set_flags(ec1, EC_FLAG_COFACTOR_ECDH);
171 if (!EC_KEY_set_public_key_affine_coordinates(ec1, x, y))
177 if (!EC_KEY_set_private_key(ec1, d))
183 x = BN_bin2bn(ecd->x2, ecd->x2len, x);
184 y = BN_bin2bn(ecd->y2, ecd->y2len, y);
192 ec2 = EC_KEY_new_by_curve_name(ecd->curve);
198 EC_KEY_set_flags(ec1, EC_FLAG_COFACTOR_ECDH);
200 if (!EC_KEY_set_public_key_affine_coordinates(ec2, x, y))
206 ecp = EC_KEY_get0_public_key(ec2);
213 if (!ECDH_compute_key(ztmp, ecd->zlen, ecp, ec1, 0))
219 if (!fips_post_corrupt(FIPS_TEST_ECDH, ecd->curve, NULL))
222 if (memcmp(ztmp, ecd->z, ecd->zlen))
224 fips_post_failed(FIPS_TEST_ECDH, ecd->curve, 0);
227 else if (!fips_post_success(FIPS_TEST_ECDH, ecd->curve, 0))