03eadda3d7420addda019c0ca7bb9939b527c746
[openssl.git] / fips / ecdsa / fips_ecdsa_selftest.c
1 /* fips/ecdsa/fips_ecdsa_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/ecdsa.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 static const char P_224_name[] = "ECDSA P-224";
69
70 static const unsigned char P_224_d[] = {
71         0x98,0x1f,0xb5,0xf1,0xfc,0x87,0x1d,0x7d,0xde,0x1e,0x01,0x64,
72         0x09,0x9b,0xe7,0x1b,0x9f,0xad,0x63,0xdd,0x33,0x01,0xd1,0x50,
73         0x80,0x93,0x50,0x30
74 };
75 static const unsigned char P_224_qx[] = {
76         0x95,0x47,0x99,0x44,0x29,0x8f,0x51,0x39,0xe2,0x53,0xec,0x79,
77         0xb0,0x4d,0xde,0x87,0x1a,0x76,0x54,0xd5,0x96,0xb8,0x7a,0x6d,
78         0xf4,0x1c,0x2c,0x87
79 };
80 static const unsigned char P_224_qy[] = {
81         0x91,0x5f,0xd5,0x31,0xdd,0x24,0xe5,0x78,0xd9,0x08,0x24,0x8a,
82         0x49,0x99,0xec,0x55,0xf2,0x82,0xb3,0xc4,0xb7,0x33,0x68,0xe4,
83         0x24,0xa9,0x12,0x82
84 };
85
86 #ifndef OPENSSL_NO_EC2M
87
88 static const char K_233_name[] = "ECDSA K-233";
89
90 static const unsigned char K_233_d[] = {
91         0x10,0x0a,0xe0,0xae,0xcf,0x1b,0xa4,0x55,0x1a,0xd4,0xc8,0x3f,
92         0xc3,0x7e,0xdc,0x97,0x40,0x2c,0x6a,0xc8,0xe2,0x50,0x09,0xf8,
93         0x1c,0x70,0x23,0xcb,0xde
94 };
95 static const unsigned char K_233_qx[] = {
96         0x01,0xa6,0xbf,0x38,0x32,0xe2,0xd7,0x15,0x4a,0xc8,0xaa,0x1f,
97         0x9d,0xdb,0xb8,0x8f,0x9a,0x9b,0xc0,0xb4,0xc1,0xb6,0xa5,0x5c,
98         0x93,0xb9,0x8a,0x83,0x65,0xe9
99 };
100 static const unsigned char K_233_qy[] = {
101         0x01,0x81,0x3d,0xfe,0x38,0x56,0x8f,0x3c,0x23,0x29,0xc6,0x59,
102         0xcb,0xa5,0x90,0x86,0xd1,0x8c,0xd8,0xb0,0xf2,0xd4,0x35,0x2b,
103         0x11,0x40,0x33,0x9a,0x88,0x10
104 };
105
106 #endif
107
108 typedef struct 
109         {
110         int curve;
111         const char *name;
112         const unsigned char *x;
113         size_t xlen;
114         const unsigned char *y;
115         size_t ylen;
116         const unsigned char *d;
117         size_t dlen;
118         } EC_SELFTEST_DATA;
119
120 #define make_ecdsa_test(nid, pr) { nid, pr##_name, \
121                                 pr##_qx, sizeof(pr##_qx), \
122                                 pr##_qy, sizeof(pr##_qy), \
123                                 pr##_d, sizeof(pr##_d)}
124
125 static EC_SELFTEST_DATA test_ec_data[] = 
126         {
127         make_ecdsa_test(NID_secp224r1, P_224),
128 #ifndef OPENSSL_NO_EC2M
129         make_ecdsa_test(NID_sect233k1, K_233)
130 #endif
131         };
132
133 int FIPS_selftest_ecdsa()
134         {
135         EC_KEY *ec = NULL;
136         BIGNUM *x = NULL, *y = NULL, *d = NULL;
137         EVP_PKEY pk;
138         int rv = 0;
139         size_t i;
140
141         for (i = 0; i < sizeof(test_ec_data)/sizeof(EC_SELFTEST_DATA); i++)
142                 {
143                 EC_SELFTEST_DATA *ecd = test_ec_data + i;
144
145                 x = BN_bin2bn(ecd->x, ecd->xlen, x);
146                 y = BN_bin2bn(ecd->y, ecd->ylen, y);
147                 d = BN_bin2bn(ecd->d, ecd->dlen, d);
148
149                 if (!x || !y || !d)
150                         goto err;
151
152                 ec = EC_KEY_new_by_curve_name(ecd->curve);
153                 if (!ec)
154                         goto err;
155
156                 if (!EC_KEY_set_public_key_affine_coordinates(ec, x, y))
157                         goto err;
158
159                 if (!EC_KEY_set_private_key(ec, d))
160                         goto err;
161
162                 pk.type = EVP_PKEY_EC;
163                 pk.pkey.ec = ec;
164
165                 if (!fips_pkey_signature_test(FIPS_TEST_SIGNATURE, &pk, NULL, 0,
166                                                 NULL, 0, EVP_sha512(), 0,
167                                                 ecd->name))
168                         goto err;
169                 EC_KEY_free(ec);
170                 ec = NULL;
171                 }
172
173         rv = 1;
174
175         err:
176
177         if (x)
178                 BN_clear_free(x);
179         if (y)
180                 BN_clear_free(y);
181         if (d)
182                 BN_clear_free(d);
183         if (ec)
184                 EC_KEY_free(ec);
185
186         return rv;
187
188         }
189
190 #endif