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,
107 #ifndef OPENSSL_NO_EC2M
109 static const unsigned char b233_qcavsx[] = {
110 0x00,0x00,0x01,0x2d,0x9a,0x82,0xfb,0x33,0xc2,0x53,0x10,0x97,
111 0x27,0xd1,0xe4,0xd2,0x15,0xe3,0x9f,0x60,0xee,0x97,0xca,0xbf,
112 0x2a,0x9f,0xb0,0x4a,0xd4,0xcf,0x14,0x73
115 static const unsigned char b233_qcavsy[] = {
116 0x00,0x00,0x01,0xf7,0xd5,0x6e,0xbd,0x86,0x68,0xae,0xec,0x3b,
117 0x2e,0x44,0x99,0x44,0x8d,0x4c,0x14,0x34,0xb8,0x37,0xa1,0x45,
118 0xe7,0xc6,0x04,0xbd,0x7b,0x0b,0xa6,0x41
121 static const unsigned char b233_qiutx[] = {
122 0x01,0x2b,0xfd,0x0c,0x9b,0x46,0x61,0xe9,0x4e,0x1f,0x97,0xa1,
123 0x1d,0x1c,0x9b,0xc4,0xd3,0xfd,0xd0,0xb4,0xc9,0xfc,0xb3,0x34,
124 0xc0,0x1f,0xf8,0x6a,0xec,0x56
127 static const unsigned char b233_qiuty[] = {
128 0x01,0x5f,0xc3,0x77,0x7c,0xd9,0xa6,0xec,0xf3,0x19,0x70,0x96,
129 0xc0,0xeb,0x2c,0xad,0xa3,0xba,0x39,0xb1,0x57,0x8d,0x62,0xdc,
130 0xc7,0x87,0xde,0xa6,0x29,0xe4
133 static const unsigned char b233_qiutd[] = {
134 0xed,0x50,0xf5,0x9c,0xf3,0x36,0xcb,0xa3,0x6d,0xe9,0x95,0x44,
135 0xa2,0x87,0x0d,0xd8,0x3e,0x0e,0x4c,0x25,0x18,0x82,0x92,0xe6,
136 0xca,0x90,0xff,0xa8,0x82
139 static const unsigned char b233_ziut[] = {
140 0x00,0xe1,0x13,0x2d,0x94,0xef,0x03,0x79,0xc0,0xd1,0x5f,0xd9,
141 0x2a,0xd3,0xfe,0x1c,0xfc,0x32,0x7e,0x0a,0x60,0xe6,0x23,0x1e,
142 0xb0,0xfa,0x1b,0x9e,0x5a,0x8d
149 const unsigned char *x1;
151 const unsigned char *y1;
153 const unsigned char *d1;
155 const unsigned char *x2;
157 const unsigned char *y2;
159 const unsigned char *z;
161 } ECDH_SELFTEST_DATA;
163 #define make_ecdh_test(nid, pr) { nid, \
164 pr##_qiutx, sizeof(pr##_qiutx), \
165 pr##_qiuty, sizeof(pr##_qiuty), \
166 pr##_qiutd, sizeof(pr##_qiutd), \
167 pr##_qcavsx, sizeof(pr##_qcavsx), \
168 pr##_qcavsy, sizeof(pr##_qcavsy), \
169 pr##_ziut, sizeof(pr##_ziut) }
171 static ECDH_SELFTEST_DATA test_ecdh_data[] =
173 make_ecdh_test(NID_secp224r1, p224),
174 #ifndef OPENSSL_NO_EC2M
175 make_ecdh_test(NID_sect233r1, b233)
179 int FIPS_selftest_ecdh(void)
181 EC_KEY *ec1 = NULL, *ec2 = NULL;
182 const EC_POINT *ecp = NULL;
183 BIGNUM *x = NULL, *y = NULL, *d = NULL;
184 unsigned char *ztmp = NULL;
188 for (i = 0; i < sizeof(test_ecdh_data)/sizeof(ECDH_SELFTEST_DATA); i++)
190 ECDH_SELFTEST_DATA *ecd = test_ecdh_data + i;
191 if (!fips_post_started(FIPS_TEST_ECDH, ecd->curve, 0))
193 ztmp = OPENSSL_malloc(ecd->zlen);
195 x = BN_bin2bn(ecd->x1, ecd->x1len, x);
196 y = BN_bin2bn(ecd->y1, ecd->y1len, y);
197 d = BN_bin2bn(ecd->d1, ecd->d1len, d);
199 if (!x || !y || !d || !ztmp)
205 ec1 = EC_KEY_new_by_curve_name(ecd->curve);
212 if (!EC_KEY_set_public_key_affine_coordinates(ec1, x, y))
218 if (!EC_KEY_set_private_key(ec1, d))
224 x = BN_bin2bn(ecd->x2, ecd->x2len, x);
225 y = BN_bin2bn(ecd->y2, ecd->y2len, y);
233 ec2 = EC_KEY_new_by_curve_name(ecd->curve);
240 if (!EC_KEY_set_public_key_affine_coordinates(ec2, x, y))
246 ecp = EC_KEY_get0_public_key(ec2);
253 if (!ECDH_compute_key(ztmp, ecd->zlen, ecp, ec1, 0))
259 if (memcmp(ztmp, ecd->z, ecd->zlen))
261 fips_post_failed(FIPS_TEST_ECDH, ecd->curve, 0);
264 else if (!fips_post_success(FIPS_TEST_ECDH, ecd->curve, 0))