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