ECDH POST selftest failure inducing support.
[openssl.git] / fips / ecdh / fips_ecdh_selftest.c
1 /* fips/ecdh/fips_ecdh_selftest.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project 2011.
4  */
5 /* ====================================================================
6  * Copyright (c) 2011 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
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
18  *    distribution.
19  *
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/)"
24  *
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.
29  *
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.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
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  * ====================================================================
52  *
53  */
54
55 #define OPENSSL_FIPSAPI
56
57 #include <string.h>
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>
65
66 #ifdef OPENSSL_FIPS
67
68 #include "fips_locl.h"
69
70 __fips_constseg
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,
74         0x90,0xd1,0x46,0x63
75 };
76 __fips_constseg
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,
80         0xdd,0xa6,0x77,0xcf
81 };
82 __fips_constseg
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,
86         0x32,0xa5,0x16,0x5f
87 };
88 __fips_constseg
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,
92         0x55,0x54,0xe3,0x37
93 };
94 __fips_constseg
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,
98         0x43,0x6a,0x63,0xef
99 };
100 __fips_constseg
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,
104         0x5c,0x1a,0xf2,0xdd
105 };
106
107 typedef struct 
108         {
109         int curve;
110         const unsigned char *x1;
111         size_t x1len;
112         const unsigned char *y1;
113         size_t y1len;
114         const unsigned char *d1;
115         size_t d1len;
116         const unsigned char *x2;
117         size_t x2len;
118         const unsigned char *y2;
119         size_t y2len;
120         const unsigned char *z;
121         size_t zlen;
122         } ECDH_SELFTEST_DATA;
123
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) }
131
132 static ECDH_SELFTEST_DATA test_ecdh_data[] = 
133         {
134         make_ecdh_test(NID_secp224r1, p224),
135         };
136
137 int FIPS_selftest_ecdh(void)
138         {
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;
143         int rv = 1;
144         size_t i;
145
146         for (i = 0; i < sizeof(test_ecdh_data)/sizeof(ECDH_SELFTEST_DATA); i++)
147                 {
148                 ECDH_SELFTEST_DATA *ecd = test_ecdh_data + i;
149                 if (!fips_post_started(FIPS_TEST_ECDH, ecd->curve, 0))
150                         continue;
151                 ztmp = OPENSSL_malloc(ecd->zlen);
152
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);
156
157                 if (!x || !y || !d || !ztmp)
158                         {
159                         rv = -1;
160                         goto err;
161                         }
162
163                 ec1 = EC_KEY_new_by_curve_name(ecd->curve);
164                 if (!ec1)
165                         {
166                         rv = -1;
167                         goto err;
168                         }
169
170                 if (!EC_KEY_set_public_key_affine_coordinates(ec1, x, y))
171                         {
172                         rv = -1;
173                         goto err;
174                         }
175
176                 if (!EC_KEY_set_private_key(ec1, d))
177                         {
178                         rv = -1;
179                         goto err;
180                         }
181
182                 x = BN_bin2bn(ecd->x2, ecd->x2len, x);
183                 y = BN_bin2bn(ecd->y2, ecd->y2len, y);
184
185                 if (!x || !y)
186                         {
187                         rv = -1;
188                         goto err;
189                         }
190
191                 ec2 = EC_KEY_new_by_curve_name(ecd->curve);
192                 if (!ec2)
193                         {
194                         rv = -1;
195                         goto err;
196                         }
197
198                 if (!EC_KEY_set_public_key_affine_coordinates(ec2, x, y))
199                         {
200                         rv = -1;
201                         goto err;
202                         }
203
204                 ecp = EC_KEY_get0_public_key(ec2);
205                 if (!ecp)
206                         {
207                         rv = -1;
208                         goto err;
209                         }
210
211                 if (!ECDH_compute_key(ztmp, ecd->zlen, ecp, ec1, 0))
212                         {
213                         rv = -1;
214                         goto err;
215                         }
216
217                 if (!fips_post_corrupt(FIPS_TEST_ECDH, ecd->curve, NULL))
218                         ztmp[0] ^= 0x1;
219
220                 if (memcmp(ztmp, ecd->z, ecd->zlen))
221                         {
222                         fips_post_failed(FIPS_TEST_ECDH, ecd->curve, 0);
223                         rv = 0;
224                         }
225                 else if (!fips_post_success(FIPS_TEST_ECDH, ecd->curve, 0))
226                         goto err;
227
228                 EC_KEY_free(ec1);
229                 ec1 = NULL;
230                 EC_KEY_free(ec2);
231                 ec2 = NULL;
232                 OPENSSL_free(ztmp);
233                 ztmp = NULL;
234                 }
235
236         err:
237
238         if (x)
239                 BN_clear_free(x);
240         if (y)
241                 BN_clear_free(y);
242         if (d)
243                 BN_clear_free(d);
244         if (ec1)
245                 EC_KEY_free(ec1);
246         if (ec2)
247                 EC_KEY_free(ec2);
248         if (ztmp)
249                 OPENSSL_free(ztmp);
250
251         return rv;
252
253         }
254
255 #endif