f9bb3470f6436b4b842ba0c87db14baaba4f016c
[openssl.git] / crypto / dsa / dsa_key.c
1 /*
2  * Copyright 1995-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 <stdio.h>
17 #include <time.h>
18 #include "internal/cryptlib.h"
19 #include <openssl/bn.h>
20 #include <openssl/self_test.h>
21 #include "crypto/dsa.h"
22 #include "dsa_local.h"
23
24 #ifdef FIPS_MODE
25 # define MIN_STRENGTH 112
26 #else
27 # define MIN_STRENGTH 80
28 #endif
29
30 static int dsa_keygen(DSA *dsa, int pairwise_test);
31 static int dsa_keygen_pairwise_test(DSA *dsa, OSSL_CALLBACK *cb, void *cbarg);
32
33 int DSA_generate_key(DSA *dsa)
34 {
35 #ifndef FIPS_MODE
36     if (dsa->meth->dsa_keygen != NULL)
37         return dsa->meth->dsa_keygen(dsa);
38 #endif
39     return dsa_keygen(dsa, 0);
40 }
41
42 int dsa_generate_public_key(BN_CTX *ctx, const DSA *dsa, const BIGNUM *priv_key,
43                             BIGNUM *pub_key)
44 {
45     int ret = 0;
46     BIGNUM *prk = BN_new();
47
48     if (prk == NULL)
49         return 0;
50     BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);
51
52     /* pub_key = g ^ priv_key mod p */
53     if (!BN_mod_exp(pub_key, dsa->params.g, prk, dsa->params.p, ctx))
54         goto err;
55     ret = 1;
56 err:
57     BN_clear_free(prk);
58     return ret;
59 }
60
61 static int dsa_keygen(DSA *dsa, int pairwise_test)
62 {
63     int ok = 0;
64     BN_CTX *ctx = NULL;
65     BIGNUM *pub_key = NULL, *priv_key = NULL;
66
67     if ((ctx = BN_CTX_new_ex(dsa->libctx)) == NULL)
68         goto err;
69
70     if (dsa->priv_key == NULL) {
71         if ((priv_key = BN_secure_new()) == NULL)
72             goto err;
73     } else {
74         priv_key = dsa->priv_key;
75     }
76
77     /*
78      * For FFC FIPS 186-4 keygen
79      * security strength s = 112,
80      * Max Private key size N = len(q)
81      */
82     if (!ffc_generate_private_key(ctx, &dsa->params, BN_num_bits(dsa->params.q),
83                                   MIN_STRENGTH, priv_key))
84         goto err;
85
86     if (dsa->pub_key == NULL) {
87         if ((pub_key = BN_new()) == NULL)
88             goto err;
89     } else {
90         pub_key = dsa->pub_key;
91     }
92
93     if (!dsa_generate_public_key(ctx, dsa, priv_key, pub_key))
94         goto err;
95
96     dsa->priv_key = priv_key;
97     dsa->pub_key = pub_key;
98
99 #ifdef FIPS_MODE
100     pairwise_test = 1;
101 #endif /* FIPS_MODE */
102
103     ok = 1;
104     if (pairwise_test) {
105         OSSL_CALLBACK *cb = NULL;
106         void *cbarg = NULL;
107
108         OSSL_SELF_TEST_get_callback(dsa->libctx, &cb, &cbarg);
109         ok = dsa_keygen_pairwise_test(dsa, cb, cbarg);
110         if (!ok) {
111             BN_free(dsa->pub_key);
112             BN_clear_free(dsa->priv_key);
113             BN_CTX_free(ctx);
114             return ok;
115         }
116     }
117     dsa->dirty_cnt++;
118
119  err:
120     if (pub_key != dsa->pub_key)
121         BN_free(pub_key);
122     if (priv_key != dsa->priv_key)
123         BN_free(priv_key);
124     BN_CTX_free(ctx);
125
126     return ok;
127 }
128
129 /*
130  * FIPS 140-2 IG 9.9 AS09.33
131  * Perform a sign/verify operation.
132  */
133 static int dsa_keygen_pairwise_test(DSA *dsa, OSSL_CALLBACK *cb, void *cbarg)
134 {
135     int ret = 0;
136     unsigned char dgst[16] = {0};
137     unsigned int dgst_len = (unsigned int)sizeof(dgst);
138     DSA_SIG *sig = NULL;
139     OSSL_SELF_TEST *st = NULL;
140
141     st = OSSL_SELF_TEST_new(cb, cbarg);
142     if (st == NULL)
143         goto err;
144
145     OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_PCT,
146                            OSSL_SELF_TEST_DESC_PCT_DSA);
147
148     sig = DSA_do_sign(dgst, (int)dgst_len, dsa);
149     if (sig == NULL)
150         goto err;
151
152     OSSL_SELF_TEST_oncorrupt_byte(st, dgst);
153
154     if (DSA_do_verify(dgst, dgst_len, sig, dsa) != 1)
155         goto err;
156
157     ret = 1;
158 err:
159     OSSL_SELF_TEST_onend(st, ret);
160     OSSL_SELF_TEST_free(st);
161     DSA_SIG_free(sig);
162     return ret;
163 }