3 * Copyright (C) 2006-2015 wolfSSL Inc.
5 * This file is part of wolfSSL. (formerly known as CyaSSL)
7 * wolfSSL is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * wolfSSL is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26 #include <wolfssl/wolfcrypt/settings.h>
30 #include <wolfssl/wolfcrypt/dsa.h>
31 #include <wolfssl/wolfcrypt/sha.h>
32 #include <wolfssl/wolfcrypt/random.h>
33 #include <wolfssl/wolfcrypt/error-crypt.h>
37 DSA_HALF_SIZE = 20, /* r and s size */
38 DSA_SIG_SIZE = 40 /* signature size */
42 #ifndef WOLFSSL_HAVE_MIN
43 #define WOLFSSL_HAVE_MIN
45 static INLINE word32 min(word32 a, word32 b)
50 #endif /* WOLFSSL_HAVE_MIN */
53 void wc_InitDsaKey(DsaKey* key)
55 key->type = -1; /* haven't decided yet */
57 /* TomsFastMath doesn't use memory allocation */
59 key->p.dp = 0; /* public alloc parts */
64 key->x.dp = 0; /* private alloc parts */
69 void wc_FreeDsaKey(DsaKey* key)
72 /* TomsFastMath doesn't use memory allocation */
74 if (key->type == DSA_PRIVATE)
84 int wc_DsaSign(const byte* digest, byte* out, DsaKey* key, RNG* rng)
86 mp_int k, kInv, r, s, H;
88 byte buffer[DSA_HALF_SIZE];
90 sz = min(sizeof(buffer), mp_unsigned_bin_size(&key->q));
93 ret = wc_RNG_GenerateBlock(rng, buffer, sz);
99 if (mp_init_multi(&k, &kInv, &r, &s, &H, 0) != MP_OKAY)
102 if (mp_read_unsigned_bin(&k, buffer, sz) != MP_OKAY)
105 if (ret == 0 && mp_cmp_d(&k, 1) != MP_GT)
108 /* inverse k mod q */
109 if (ret == 0 && mp_invmod(&k, &key->q, &kInv) != MP_OKAY)
112 /* generate r, r = (g exp k mod p) mod q */
113 if (ret == 0 && mp_exptmod(&key->g, &k, &key->p, &r) != MP_OKAY)
116 if (ret == 0 && mp_mod(&r, &key->q, &r) != MP_OKAY)
119 /* generate H from sha digest */
120 if (ret == 0 && mp_read_unsigned_bin(&H, digest,SHA_DIGEST_SIZE) != MP_OKAY)
123 /* generate s, s = (kInv * (H + x*r)) % q */
124 if (ret == 0 && mp_mul(&key->x, &r, &s) != MP_OKAY)
127 if (ret == 0 && mp_add(&s, &H, &s) != MP_OKAY)
130 if (ret == 0 && mp_mulmod(&s, &kInv, &key->q, &s) != MP_OKAY)
135 int rSz = mp_unsigned_bin_size(&r);
136 int sSz = mp_unsigned_bin_size(&s);
138 if (rSz == DSA_HALF_SIZE - 1) {
143 if (mp_to_unsigned_bin(&r, out) != MP_OKAY)
146 if (sSz == DSA_HALF_SIZE - 1) {
150 ret = mp_to_unsigned_bin(&s, out + rSz);
164 int wc_DsaVerify(const byte* digest, const byte* sig, DsaKey* key, int* answer)
166 mp_int w, u1, u2, v, r, s;
169 if (mp_init_multi(&w, &u1, &u2, &v, &r, &s) != MP_OKAY)
172 /* set r and s from signature */
173 if (mp_read_unsigned_bin(&r, sig, DSA_HALF_SIZE) != MP_OKAY ||
174 mp_read_unsigned_bin(&s, sig + DSA_HALF_SIZE, DSA_HALF_SIZE) != MP_OKAY)
179 if (mp_iszero(&r) == MP_YES || mp_iszero(&s) == MP_YES ||
180 mp_cmp(&r, &key->q) != MP_LT || mp_cmp(&s, &key->q) != MP_LT) {
185 /* put H into u1 from sha digest */
186 if (ret == 0 && mp_read_unsigned_bin(&u1,digest,SHA_DIGEST_SIZE) != MP_OKAY)
190 if (ret == 0 && mp_invmod(&s, &key->q, &w) != MP_OKAY)
193 /* u1 = (H * w) % q */
194 if (ret == 0 && mp_mulmod(&u1, &w, &key->q, &u1) != MP_OKAY)
197 /* u2 = (r * w) % q */
198 if (ret == 0 && mp_mulmod(&r, &w, &key->q, &u2) != MP_OKAY)
201 /* verify v = ((g^u1 * y^u2) mod p) mod q */
202 if (ret == 0 && mp_exptmod(&key->g, &u1, &key->p, &u1) != MP_OKAY)
205 if (ret == 0 && mp_exptmod(&key->y, &u2, &key->p, &u2) != MP_OKAY)
208 if (ret == 0 && mp_mulmod(&u1, &u2, &key->p, &v) != MP_OKAY)
211 if (ret == 0 && mp_mod(&v, &key->q, &v) != MP_OKAY)
215 if (ret == 0 && mp_cmp(&r, &v) == MP_EQ)