[crypto/ec] don't assume points are of order group->order
[openssl.git] / test / drbg_cavs_test.c
1 /*
2  * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 <string.h>
11 #include "internal/nelem.h"
12 #include <openssl/crypto.h>
13 #include <openssl/err.h>
14 #include <openssl/rand.h>
15 #include <openssl/obj_mac.h>
16 #include <openssl/evp.h>
17 #include <openssl/aes.h>
18 #include "../crypto/rand/rand_lcl.h"
19
20 #include "testutil.h"
21 #include "drbg_cavs_data.h"
22
23 static int app_data_index;
24
25 typedef struct test_ctx_st {
26     const unsigned char *entropy;
27     size_t entropylen;
28     int entropycnt;
29     const unsigned char *nonce;
30     size_t noncelen;
31     int noncecnt;
32 } TEST_CTX;
33
34 static size_t kat_entropy(RAND_DRBG *drbg, unsigned char **pout,
35                           int entropy, size_t min_len, size_t max_len,
36                           int prediction_resistance)
37 {
38     TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
39
40     t->entropycnt++;
41     *pout = (unsigned char *)t->entropy;
42     return t->entropylen;
43 }
44
45 static size_t kat_nonce(RAND_DRBG *drbg, unsigned char **pout,
46                         int entropy, size_t min_len, size_t max_len)
47 {
48     TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
49
50     t->noncecnt++;
51     *pout = (unsigned char *)t->nonce;
52     return t->noncelen;
53 }
54
55 /*
56  * Do a single NO_RESEED KAT:
57  *
58  * Instantiate
59  * Generate Random Bits (pr=false)
60  * Generate Random Bits (pr=false)
61  * Uninstantiate
62  *
63  * Return 0 on failure.
64  */
65 static int single_kat_no_reseed(const struct drbg_kat *td)
66 {
67     struct drbg_kat_no_reseed *data = (struct drbg_kat_no_reseed *)td->t;
68     RAND_DRBG *drbg = NULL;
69     unsigned char *buff = NULL;
70     unsigned int flags = 0;
71     int failures = 0;
72     TEST_CTX t;
73
74     if (td->df != USE_DF)
75         flags |= RAND_DRBG_FLAG_CTR_NO_DF;
76
77     if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, flags, NULL)))
78         return 0;
79
80     if (!TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
81                                            kat_nonce, NULL))) {
82         failures++;
83         goto err;
84     }
85     memset(&t, 0, sizeof(t));
86     t.entropy = data->entropyin;
87     t.entropylen = td->entropyinlen;
88     t.nonce = data->nonce;
89     t.noncelen = td->noncelen;
90     RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
91
92     buff = OPENSSL_malloc(td->retbyteslen);
93     if (buff == NULL)
94         goto err;
95
96     if (!TEST_true(RAND_DRBG_instantiate(drbg, data->persstr, td->persstrlen))
97         || !TEST_true(RAND_DRBG_generate(drbg, buff, td->retbyteslen, 0,
98                                          data->addin1, td->addinlen))
99         || !TEST_true(RAND_DRBG_generate(drbg, buff, td->retbyteslen, 0,
100                                          data->addin2, td->addinlen))
101         || !TEST_true(RAND_DRBG_uninstantiate(drbg))
102         || !TEST_mem_eq(data->retbytes, td->retbyteslen, buff,
103                         td->retbyteslen))
104         failures++;
105
106 err:
107     if (buff != NULL)
108         OPENSSL_free(buff);
109     if (drbg != NULL) {
110         RAND_DRBG_uninstantiate(drbg);
111         RAND_DRBG_free(drbg);
112     }
113     return failures == 0;
114 }
115
116 /*-
117  * Do a single PR_FALSE KAT:
118  *
119  * Instantiate
120  * Reseed
121  * Generate Random Bits (pr=false)
122  * Generate Random Bits (pr=false)
123  * Uninstantiate
124  *
125  * Return 0 on failure.
126  */
127 static int single_kat_pr_false(const struct drbg_kat *td)
128 {
129     struct drbg_kat_pr_false *data = (struct drbg_kat_pr_false *)td->t;
130     RAND_DRBG *drbg = NULL;
131     unsigned char *buff = NULL;
132     unsigned int flags = 0;
133     int failures = 0;
134     TEST_CTX t;
135
136     if (td->df != USE_DF)
137         flags |= RAND_DRBG_FLAG_CTR_NO_DF;
138
139     if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, flags, NULL)))
140         return 0;
141
142     if (!TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
143                                            kat_nonce, NULL))) {
144         failures++;
145         goto err;
146     }
147     memset(&t, 0, sizeof(t));
148     t.entropy = data->entropyin;
149     t.entropylen = td->entropyinlen;
150     t.nonce = data->nonce;
151     t.noncelen = td->noncelen;
152     RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
153
154     buff = OPENSSL_malloc(td->retbyteslen);
155     if (buff == NULL)
156         goto err;
157
158     if (!TEST_true(RAND_DRBG_instantiate(drbg, data->persstr, td->persstrlen)))
159         failures++;
160
161     t.entropy = data->entropyinreseed;
162     t.entropylen = td->entropyinlen;
163
164     if (!TEST_true(RAND_DRBG_reseed(drbg, data->addinreseed, td->addinlen, 0))
165         || !TEST_true(RAND_DRBG_generate(drbg, buff, td->retbyteslen, 0,
166                                          data->addin1, td->addinlen))
167         || !TEST_true(RAND_DRBG_generate(drbg, buff, td->retbyteslen, 0,
168                                          data->addin2, td->addinlen))
169         || !TEST_true(RAND_DRBG_uninstantiate(drbg))
170         || !TEST_mem_eq(data->retbytes, td->retbyteslen, buff,
171                         td->retbyteslen))
172         failures++;
173
174 err:
175     if (buff != NULL)
176         OPENSSL_free(buff);
177     if (drbg != NULL) {
178         RAND_DRBG_uninstantiate(drbg);
179         RAND_DRBG_free(drbg);
180     }
181     return failures == 0;
182 }
183
184 /*-
185  * Do a single PR_TRUE KAT:
186  *
187  * Instantiate
188  * Generate Random Bits (pr=true)
189  * Generate Random Bits (pr=true)
190  * Uninstantiate
191  *
192  * Return 0 on failure.
193  */
194 static int single_kat_pr_true(const struct drbg_kat *td)
195 {
196     struct drbg_kat_pr_true *data = (struct drbg_kat_pr_true *)td->t;
197     RAND_DRBG *drbg = NULL;
198     unsigned char *buff = NULL;
199     unsigned int flags = 0;
200     int failures = 0;
201     TEST_CTX t;
202
203     if (td->df != USE_DF)
204         flags |= RAND_DRBG_FLAG_CTR_NO_DF;
205
206     if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, flags, NULL)))
207         return 0;
208
209     if (!TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
210                                            kat_nonce, NULL))) {
211         failures++;
212         goto err;
213     }
214     memset(&t, 0, sizeof(t));
215     t.nonce = data->nonce;
216     t.noncelen = td->noncelen;
217     t.entropy = data->entropyin;
218     t.entropylen = td->entropyinlen;
219     RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
220
221     buff = OPENSSL_malloc(td->retbyteslen);
222     if (buff == NULL)
223         goto err;
224
225     if (!TEST_true(RAND_DRBG_instantiate(drbg, data->persstr, td->persstrlen)))
226         failures++;
227
228     t.entropy = data->entropyinpr1;
229     t.entropylen = td->entropyinlen;
230
231     if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->retbyteslen, 1,
232                                       data->addin1, td->addinlen)))
233         failures++;
234
235     t.entropy = data->entropyinpr2;
236     t.entropylen = td->entropyinlen;
237
238     if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->retbyteslen, 1,
239                                       data->addin2, td->addinlen))
240         || !TEST_true(RAND_DRBG_uninstantiate(drbg))
241         || !TEST_mem_eq(data->retbytes, td->retbyteslen, buff,
242                         td->retbyteslen))
243         failures++;
244
245 err:
246     if (buff != NULL)
247         OPENSSL_free(buff);
248     if (drbg != NULL) {
249         RAND_DRBG_uninstantiate(drbg);
250         RAND_DRBG_free(drbg);
251     }
252     return failures == 0;
253 }
254
255 static int test_cavs_kats(int i)
256 {
257     const struct drbg_kat *td = drbg_test[i];
258     int rv = 0;
259
260     switch (td->type) {
261     case NO_RESEED:
262         if (!single_kat_no_reseed(td))
263             goto err;
264         break;
265     case PR_FALSE:
266         if (!single_kat_pr_false(td))
267             goto err;
268         break;
269     case PR_TRUE:
270         if (!single_kat_pr_true(td))
271             goto err;
272         break;
273     default:    /* cant happen */
274         goto err;
275     }
276     rv = 1;
277 err:
278     return rv;
279 }
280
281 int setup_tests(void)
282 {
283     app_data_index = RAND_DRBG_get_ex_new_index(0L, NULL, NULL, NULL, NULL);
284
285     ADD_ALL_TESTS(test_cavs_kats, drbg_test_nelem);
286     return 1;
287 }