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