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