test/recipes/02-test_ordinals.t: Take '?' and '?+' into account
[openssl.git] / test / dsa_no_digest_size_test.c
1 /*
2  * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include "testutil.h"
14
15 #include <openssl/evp.h>
16 #include <openssl/err.h>
17 #include <openssl/rand.h>
18
19 #ifndef OPENSSL_NO_DSA
20 #include <openssl/dsa.h>
21
22 static DSA *dsakey;
23
24 /*
25  * These parameters are from test/recipes/04-test_pem_data/dsaparam.pem,
26  * converted using dsaparam -C
27  */
28 static DSA *load_dsa_params(void)
29 {
30     static unsigned char dsap_2048[] = {
31         0xAE, 0x35, 0x7D, 0x4E, 0x1D, 0x96, 0xE2, 0x9F, 0x00, 0x96,
32         0x60, 0x5A, 0x6E, 0x4D, 0x07, 0x8D, 0xA5, 0x7C, 0xBC, 0xF9,
33         0xAD, 0xD7, 0x9F, 0xD5, 0xE9, 0xEE, 0xA6, 0x33, 0x51, 0xDE,
34         0x7B, 0x72, 0xD2, 0x75, 0xAA, 0x71, 0x77, 0xF1, 0x63, 0xFB,
35         0xB6, 0xEC, 0x5A, 0xBA, 0x0D, 0x72, 0xA2, 0x1A, 0x1C, 0x64,
36         0xB8, 0xE5, 0x89, 0x09, 0x6D, 0xC9, 0x6F, 0x0B, 0x7F, 0xD2,
37         0xCE, 0x9F, 0xEF, 0x87, 0x5A, 0xB6, 0x67, 0x2F, 0xEF, 0xEE,
38         0xEB, 0x59, 0xF5, 0x5E, 0xFF, 0xA8, 0x28, 0x84, 0x9E, 0x5B,
39         0x37, 0x09, 0x11, 0x80, 0x7C, 0x08, 0x5C, 0xD5, 0xE1, 0x48,
40         0x4B, 0xD2, 0x68, 0xFB, 0x3F, 0x9F, 0x2B, 0x6B, 0x6C, 0x0D,
41         0x48, 0x1B, 0x1A, 0x80, 0xC2, 0xEB, 0x11, 0x1B, 0x37, 0x79,
42         0xD6, 0x8C, 0x8B, 0x72, 0x3E, 0x67, 0xA5, 0x05, 0x0E, 0x41,
43         0x8A, 0x9E, 0x35, 0x50, 0xB4, 0xD2, 0x40, 0x27, 0x6B, 0xFD,
44         0xE0, 0x64, 0x6B, 0x5B, 0x38, 0x42, 0x94, 0xB5, 0x49, 0xDA,
45         0xEF, 0x6E, 0x78, 0x37, 0xCD, 0x30, 0x89, 0xC3, 0x45, 0x50,
46         0x7B, 0x9C, 0x8C, 0xE7, 0x1C, 0x98, 0x70, 0x71, 0x5D, 0x79,
47         0x5F, 0xEF, 0xE8, 0x94, 0x85, 0x53, 0x3E, 0xEF, 0xA3, 0x2C,
48         0xCE, 0x1A, 0xAB, 0x7D, 0xD6, 0x5E, 0x14, 0xCD, 0x51, 0x54,
49         0x89, 0x9D, 0x77, 0xE4, 0xF8, 0x22, 0xF0, 0x35, 0x10, 0x75,
50         0x05, 0x71, 0x51, 0x4F, 0x8C, 0x4C, 0x5C, 0x0D, 0x2C, 0x2C,
51         0xBE, 0x6C, 0x34, 0xEE, 0x12, 0x82, 0x87, 0x03, 0x19, 0x06,
52         0x12, 0xA8, 0xAA, 0xF4, 0x0D, 0x3C, 0x49, 0xCC, 0x70, 0x5A,
53         0xD8, 0x32, 0xEE, 0x32, 0x50, 0x85, 0x70, 0xE8, 0x18, 0xFD,
54         0x74, 0x80, 0x53, 0x32, 0x57, 0xEE, 0x50, 0xC9, 0xAE, 0xEB,
55         0xAE, 0xB6, 0x22, 0x32, 0x16, 0x6B, 0x8C, 0x59, 0xDA, 0xEE,
56         0x1D, 0x33, 0xDF, 0x4C, 0xA2, 0x3D
57     };
58     static unsigned char dsaq_2048[] = {
59         0xAD, 0x2D, 0x6E, 0x17, 0xB0, 0xF3, 0xEB, 0xC7, 0xB8, 0xEE,
60         0x95, 0x78, 0xF2, 0x17, 0xF5, 0x33, 0x01, 0x67, 0xBC, 0xDE,
61         0x93, 0xFF, 0xEE, 0x40, 0xE8, 0x7F, 0xF1, 0x93, 0x6D, 0x4B,
62         0x87, 0x13
63     };
64     static unsigned char dsag_2048[] = {
65         0x66, 0x6F, 0xDA, 0x63, 0xA5, 0x8E, 0xD2, 0x4C, 0xD5, 0x45,
66         0x2D, 0x76, 0x5D, 0x5F, 0xCD, 0x4A, 0xB4, 0x1A, 0x42, 0x35,
67         0x86, 0x3A, 0x6F, 0xA9, 0xFA, 0x27, 0xAB, 0xDE, 0x03, 0x21,
68         0x36, 0x0A, 0x07, 0x29, 0xC9, 0x2F, 0x6D, 0x49, 0xA8, 0xF7,
69         0xC6, 0xF4, 0x92, 0xD7, 0x73, 0xC1, 0xD8, 0x76, 0x0E, 0x61,
70         0xA7, 0x0B, 0x6E, 0x96, 0xB8, 0xC8, 0xCB, 0x38, 0x35, 0x12,
71         0x20, 0x79, 0xA5, 0x08, 0x28, 0x35, 0x5C, 0xBC, 0x52, 0x16,
72         0xAF, 0x52, 0xBA, 0x0F, 0xC3, 0xB1, 0x63, 0x12, 0x27, 0x0B,
73         0x74, 0xA4, 0x47, 0x43, 0xD6, 0x30, 0xB8, 0x9C, 0x2E, 0x40,
74         0x14, 0xCD, 0x99, 0x7F, 0xE8, 0x8E, 0x37, 0xB0, 0xA9, 0x3F,
75         0x54, 0xE9, 0x66, 0x22, 0x61, 0x4C, 0xF8, 0x49, 0x03, 0x57,
76         0x14, 0x32, 0x1D, 0x37, 0x3D, 0xE2, 0x92, 0xF8, 0x8E, 0xA0,
77         0x6A, 0x66, 0x63, 0xF0, 0xB0, 0x6E, 0x07, 0x2B, 0x3D, 0xBF,
78         0xD0, 0x84, 0x6A, 0xAA, 0x1F, 0x30, 0x77, 0x65, 0xE5, 0xFC,
79         0xF5, 0xEC, 0x55, 0xCE, 0x73, 0xDB, 0xBE, 0xA7, 0x8D, 0x3A,
80         0x9F, 0x7A, 0xED, 0x4F, 0xAF, 0xA2, 0x80, 0x4C, 0x30, 0x9E,
81         0x28, 0x49, 0x65, 0x40, 0xF0, 0x03, 0x45, 0x56, 0x99, 0xA2,
82         0x93, 0x1B, 0x9C, 0x46, 0xDE, 0xBD, 0xA8, 0xAB, 0x5F, 0x90,
83         0x3F, 0xB7, 0x3F, 0xD4, 0x6F, 0x8D, 0x5A, 0x30, 0xE1, 0xD4,
84         0x63, 0x3A, 0x6A, 0x7C, 0x8F, 0x24, 0xFC, 0xD9, 0x14, 0x28,
85         0x09, 0xE4, 0x84, 0x4E, 0x17, 0x43, 0x56, 0xB8, 0xD4, 0x4B,
86         0xA2, 0x29, 0x45, 0xD3, 0x13, 0xF0, 0xC2, 0x76, 0x9B, 0x01,
87         0xA0, 0x80, 0x6E, 0x93, 0x63, 0x5E, 0x87, 0x24, 0x20, 0x2A,
88         0xFF, 0xBB, 0x9F, 0xA8, 0x99, 0x6C, 0xA7, 0x9A, 0x00, 0xB9,
89         0x7D, 0xDA, 0x66, 0xC9, 0xC0, 0x72, 0x72, 0x22, 0x0F, 0x1A,
90         0xCC, 0x23, 0xD9, 0xB7, 0x5F, 0x1B
91     };
92     DSA *dsa = DSA_new();
93     BIGNUM *p, *q, *g;
94
95     if (dsa == NULL)
96         return NULL;
97     if (!DSA_set0_pqg(dsa, p = BN_bin2bn(dsap_2048, sizeof(dsap_2048), NULL),
98                            q = BN_bin2bn(dsaq_2048, sizeof(dsaq_2048), NULL),
99                            g = BN_bin2bn(dsag_2048, sizeof(dsag_2048), NULL))) {
100         DSA_free(dsa);
101         BN_free(p);
102         BN_free(q);
103         BN_free(g);
104         return NULL;
105     }
106     return dsa;
107 }
108
109 static int genkeys(void)
110 {
111     if (!TEST_ptr(dsakey = load_dsa_params()))
112         return 0;
113
114     if (!TEST_int_eq(DSA_generate_key(dsakey), 1))
115         return 0;
116
117     return 1;
118 }
119
120 static int sign_and_verify(int len)
121 {
122     /*
123      * Per FIPS 186-4, the hash is recommended to be the same length as q.
124      * If the hash is longer than q, the leftmost N bits are used; if the hash
125      * is shorter, then we left-pad (see appendix C.2.1).
126      */
127     size_t sigLength;
128     int digestlen = BN_num_bytes(DSA_get0_q(dsakey));
129     int ok = 0;
130
131     unsigned char *dataToSign = OPENSSL_malloc(len);
132     unsigned char *paddedData = OPENSSL_malloc(digestlen);
133     unsigned char *signature = NULL;
134     EVP_PKEY_CTX *ctx = NULL;
135     EVP_PKEY *pkey = NULL;
136
137     if (!TEST_ptr(dataToSign) ||
138         !TEST_ptr(paddedData) ||
139         !TEST_int_eq(RAND_bytes(dataToSign, len), 1))
140         goto end;
141
142     memset(paddedData, 0, digestlen);
143     if (len > digestlen)
144         memcpy(paddedData, dataToSign, digestlen);
145     else
146         memcpy(paddedData + digestlen - len, dataToSign, len);
147
148     if (!TEST_ptr(pkey = EVP_PKEY_new()))
149         goto end;
150     EVP_PKEY_set1_DSA(pkey, dsakey);
151
152     if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, NULL)))
153         goto end;
154     if (!TEST_int_eq(EVP_PKEY_sign_init(ctx), 1))
155         goto end;
156
157     if (EVP_PKEY_sign(ctx, NULL, &sigLength, dataToSign, len) != 1) {
158         TEST_error("Failed to get signature length, len=%d", len);
159         goto end;
160     }
161
162     if (!TEST_ptr(signature = OPENSSL_malloc(sigLength)))
163         goto end;
164
165     if (EVP_PKEY_sign(ctx, signature, &sigLength, dataToSign, len) != 1) {
166         TEST_error("Failed to sign, len=%d", len);
167         goto end;
168     }
169
170     /* Check that the signature is okay via the EVP interface */
171     if (!TEST_int_eq(EVP_PKEY_verify_init(ctx), 1))
172         goto end;
173
174     /* ... using the same data we just signed */
175     if (EVP_PKEY_verify(ctx, signature, sigLength, dataToSign, len) != 1) {
176         TEST_error("EVP verify with unpadded length %d failed\n", len);
177         goto end;
178     }
179
180     /* ... padding/truncating the data to the appropriate digest size */
181     if (EVP_PKEY_verify(ctx, signature, sigLength, paddedData, digestlen) != 1) {
182         TEST_error("EVP verify with length %d failed\n", len);
183         goto end;
184     }
185
186     /* Verify again using the raw DSA interface */
187     if (DSA_verify(0, dataToSign, len, signature, sigLength, dsakey) != 1) {
188         TEST_error("Verification with unpadded data failed, len=%d", len);
189         goto end;
190     }
191
192     if (DSA_verify(0, paddedData, digestlen, signature, sigLength, dsakey) != 1) {
193         TEST_error("verify with length %d failed\n", len);
194         goto end;
195     }
196
197     ok = 1;
198 end:
199     EVP_PKEY_CTX_free(ctx);
200     EVP_PKEY_free(pkey);
201
202     OPENSSL_free(signature);
203     OPENSSL_free(paddedData);
204     OPENSSL_free(dataToSign);
205
206     return ok;
207 }
208
209 static int dsa_exact_size_test(void) {
210     /*
211      * For a 2048-bit p, q should be either 224 or 256 bits per the table in
212      * FIPS 186-4 4.2.
213      */
214
215     return sign_and_verify(224 / 8) && sign_and_verify(256 / 8);
216 }
217
218 static int dsa_small_digest_test(void) {
219     return sign_and_verify(16) && sign_and_verify(1);
220 }
221
222 static int dsa_large_digest_test(void) {
223     return sign_and_verify(33) && sign_and_verify(64);
224 }
225
226 void cleanup_tests(void)
227 {
228     DSA_free(dsakey);
229 }
230
231 #endif /* OPENSSL_NO_DSA */
232
233 int setup_tests(void)
234 {
235 #ifndef OPENSSL_NO_DSA
236     if (!genkeys())
237         return 0;
238
239     ADD_TEST(dsa_exact_size_test);
240     ADD_TEST(dsa_small_digest_test);
241     ADD_TEST(dsa_large_digest_test);
242 #endif
243     return 1;
244 }
245