wolfSSL SSL/TLS library, support up to TLS1.3

Dependents:   CyaSSL-Twitter-OAuth4Tw Example-client-tls-cert TwitterReader TweetTest ... more

Committer:
wolfSSL
Date:
Tue Aug 22 10:48:22 2017 +0000
Revision:
13:f67a6c6013ca
wolfSSL3.12.0 with TLS1.3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 13:f67a6c6013ca 1 /* dsa.c
wolfSSL 13:f67a6c6013ca 2 *
wolfSSL 13:f67a6c6013ca 3 * Copyright (C) 2006-2016 wolfSSL Inc.
wolfSSL 13:f67a6c6013ca 4 *
wolfSSL 13:f67a6c6013ca 5 * This file is part of wolfSSL.
wolfSSL 13:f67a6c6013ca 6 *
wolfSSL 13:f67a6c6013ca 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 13:f67a6c6013ca 8 * it under the terms of the GNU General Public License as published by
wolfSSL 13:f67a6c6013ca 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 13:f67a6c6013ca 10 * (at your option) any later version.
wolfSSL 13:f67a6c6013ca 11 *
wolfSSL 13:f67a6c6013ca 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 13:f67a6c6013ca 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 13:f67a6c6013ca 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 13:f67a6c6013ca 15 * GNU General Public License for more details.
wolfSSL 13:f67a6c6013ca 16 *
wolfSSL 13:f67a6c6013ca 17 * You should have received a copy of the GNU General Public License
wolfSSL 13:f67a6c6013ca 18 * along with this program; if not, write to the Free Software
wolfSSL 13:f67a6c6013ca 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
wolfSSL 13:f67a6c6013ca 20 */
wolfSSL 13:f67a6c6013ca 21
wolfSSL 13:f67a6c6013ca 22
wolfSSL 13:f67a6c6013ca 23 #ifdef HAVE_CONFIG_H
wolfSSL 13:f67a6c6013ca 24 #include <config.h>
wolfSSL 13:f67a6c6013ca 25 #endif
wolfSSL 13:f67a6c6013ca 26
wolfSSL 13:f67a6c6013ca 27 #include <wolfssl/wolfcrypt/settings.h>
wolfSSL 13:f67a6c6013ca 28
wolfSSL 13:f67a6c6013ca 29 #ifndef NO_DSA
wolfSSL 13:f67a6c6013ca 30
wolfSSL 13:f67a6c6013ca 31 #include <wolfssl/wolfcrypt/random.h>
wolfSSL 13:f67a6c6013ca 32 #include <wolfssl/wolfcrypt/integer.h>
wolfSSL 13:f67a6c6013ca 33 #include <wolfssl/wolfcrypt/error-crypt.h>
wolfSSL 13:f67a6c6013ca 34 #include <wolfssl/wolfcrypt/logging.h>
wolfSSL 13:f67a6c6013ca 35 #include <wolfssl/wolfcrypt/sha.h>
wolfSSL 13:f67a6c6013ca 36 #include <wolfssl/wolfcrypt/dsa.h>
wolfSSL 13:f67a6c6013ca 37
wolfSSL 13:f67a6c6013ca 38 #ifdef NO_INLINE
wolfSSL 13:f67a6c6013ca 39 #include <wolfssl/wolfcrypt/misc.h>
wolfSSL 13:f67a6c6013ca 40 #else
wolfSSL 13:f67a6c6013ca 41 #define WOLFSSL_MISC_INCLUDED
wolfSSL 13:f67a6c6013ca 42 #include <wolfcrypt/src/misc.c>
wolfSSL 13:f67a6c6013ca 43 #endif
wolfSSL 13:f67a6c6013ca 44
wolfSSL 13:f67a6c6013ca 45
wolfSSL 13:f67a6c6013ca 46 enum {
wolfSSL 13:f67a6c6013ca 47 DSA_HALF_SIZE = 20, /* r and s size */
wolfSSL 13:f67a6c6013ca 48 DSA_SIG_SIZE = 40 /* signature size */
wolfSSL 13:f67a6c6013ca 49 };
wolfSSL 13:f67a6c6013ca 50
wolfSSL 13:f67a6c6013ca 51
wolfSSL 13:f67a6c6013ca 52
wolfSSL 13:f67a6c6013ca 53 int wc_InitDsaKey(DsaKey* key)
wolfSSL 13:f67a6c6013ca 54 {
wolfSSL 13:f67a6c6013ca 55 if (key == NULL)
wolfSSL 13:f67a6c6013ca 56 return BAD_FUNC_ARG;
wolfSSL 13:f67a6c6013ca 57
wolfSSL 13:f67a6c6013ca 58 key->type = -1; /* haven't decided yet */
wolfSSL 13:f67a6c6013ca 59 key->heap = NULL;
wolfSSL 13:f67a6c6013ca 60
wolfSSL 13:f67a6c6013ca 61 return mp_init_multi(
wolfSSL 13:f67a6c6013ca 62 /* public alloc parts */
wolfSSL 13:f67a6c6013ca 63 &key->p,
wolfSSL 13:f67a6c6013ca 64 &key->q,
wolfSSL 13:f67a6c6013ca 65 &key->g,
wolfSSL 13:f67a6c6013ca 66 &key->y,
wolfSSL 13:f67a6c6013ca 67
wolfSSL 13:f67a6c6013ca 68 /* private alloc parts */
wolfSSL 13:f67a6c6013ca 69 &key->x,
wolfSSL 13:f67a6c6013ca 70 NULL
wolfSSL 13:f67a6c6013ca 71 );
wolfSSL 13:f67a6c6013ca 72 }
wolfSSL 13:f67a6c6013ca 73
wolfSSL 13:f67a6c6013ca 74
wolfSSL 13:f67a6c6013ca 75 int wc_InitDsaKey_h(DsaKey* key, void* h)
wolfSSL 13:f67a6c6013ca 76 {
wolfSSL 13:f67a6c6013ca 77 int ret = wc_InitDsaKey(key);
wolfSSL 13:f67a6c6013ca 78 if (ret == 0)
wolfSSL 13:f67a6c6013ca 79 key->heap = h;
wolfSSL 13:f67a6c6013ca 80
wolfSSL 13:f67a6c6013ca 81 return ret;
wolfSSL 13:f67a6c6013ca 82 }
wolfSSL 13:f67a6c6013ca 83
wolfSSL 13:f67a6c6013ca 84
wolfSSL 13:f67a6c6013ca 85 void wc_FreeDsaKey(DsaKey* key)
wolfSSL 13:f67a6c6013ca 86 {
wolfSSL 13:f67a6c6013ca 87 if (key == NULL)
wolfSSL 13:f67a6c6013ca 88 return;
wolfSSL 13:f67a6c6013ca 89
wolfSSL 13:f67a6c6013ca 90 if (key->type == DSA_PRIVATE)
wolfSSL 13:f67a6c6013ca 91 mp_forcezero(&key->x);
wolfSSL 13:f67a6c6013ca 92
wolfSSL 13:f67a6c6013ca 93 mp_clear(&key->x);
wolfSSL 13:f67a6c6013ca 94 mp_clear(&key->y);
wolfSSL 13:f67a6c6013ca 95 mp_clear(&key->g);
wolfSSL 13:f67a6c6013ca 96 mp_clear(&key->q);
wolfSSL 13:f67a6c6013ca 97 mp_clear(&key->p);
wolfSSL 13:f67a6c6013ca 98 }
wolfSSL 13:f67a6c6013ca 99
wolfSSL 13:f67a6c6013ca 100 #ifdef WOLFSSL_KEY_GEN
wolfSSL 13:f67a6c6013ca 101
wolfSSL 13:f67a6c6013ca 102 int wc_MakeDsaKey(WC_RNG *rng, DsaKey *dsa)
wolfSSL 13:f67a6c6013ca 103 {
wolfSSL 13:f67a6c6013ca 104 unsigned char *buf;
wolfSSL 13:f67a6c6013ca 105 int qsize, err;
wolfSSL 13:f67a6c6013ca 106
wolfSSL 13:f67a6c6013ca 107 if (rng == NULL || dsa == NULL)
wolfSSL 13:f67a6c6013ca 108 return BAD_FUNC_ARG;
wolfSSL 13:f67a6c6013ca 109
wolfSSL 13:f67a6c6013ca 110 qsize = mp_unsigned_bin_size(&dsa->q);
wolfSSL 13:f67a6c6013ca 111 if (qsize == 0)
wolfSSL 13:f67a6c6013ca 112 return BAD_FUNC_ARG;
wolfSSL 13:f67a6c6013ca 113
wolfSSL 13:f67a6c6013ca 114 /* allocate ram */
wolfSSL 13:f67a6c6013ca 115 buf = (unsigned char *)XMALLOC(qsize, dsa->heap,
wolfSSL 13:f67a6c6013ca 116 DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 13:f67a6c6013ca 117 if (buf == NULL)
wolfSSL 13:f67a6c6013ca 118 return MEMORY_E;
wolfSSL 13:f67a6c6013ca 119
wolfSSL 13:f67a6c6013ca 120 if (mp_init(&dsa->x) != MP_OKAY) {
wolfSSL 13:f67a6c6013ca 121 XFREE(buf, dsa->heap, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 13:f67a6c6013ca 122 return MP_INIT_E;
wolfSSL 13:f67a6c6013ca 123 }
wolfSSL 13:f67a6c6013ca 124
wolfSSL 13:f67a6c6013ca 125 do {
wolfSSL 13:f67a6c6013ca 126 /* make a random exponent mod q */
wolfSSL 13:f67a6c6013ca 127 err = wc_RNG_GenerateBlock(rng, buf, qsize);
wolfSSL 13:f67a6c6013ca 128 if (err != MP_OKAY) {
wolfSSL 13:f67a6c6013ca 129 mp_clear(&dsa->x);
wolfSSL 13:f67a6c6013ca 130 XFREE(buf, dsa->heap, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 13:f67a6c6013ca 131 return err;
wolfSSL 13:f67a6c6013ca 132 }
wolfSSL 13:f67a6c6013ca 133
wolfSSL 13:f67a6c6013ca 134 err = mp_read_unsigned_bin(&dsa->x, buf, qsize);
wolfSSL 13:f67a6c6013ca 135 if (err != MP_OKAY) {
wolfSSL 13:f67a6c6013ca 136 mp_clear(&dsa->x);
wolfSSL 13:f67a6c6013ca 137 XFREE(buf, dsa->heap, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 13:f67a6c6013ca 138 return err;
wolfSSL 13:f67a6c6013ca 139 }
wolfSSL 13:f67a6c6013ca 140 } while (mp_cmp_d(&dsa->x, 1) != MP_GT);
wolfSSL 13:f67a6c6013ca 141
wolfSSL 13:f67a6c6013ca 142 XFREE(buf, dsa->heap, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 13:f67a6c6013ca 143
wolfSSL 13:f67a6c6013ca 144 if (mp_init(&dsa->y) != MP_OKAY) {
wolfSSL 13:f67a6c6013ca 145 mp_clear(&dsa->x);
wolfSSL 13:f67a6c6013ca 146 return MP_INIT_E;
wolfSSL 13:f67a6c6013ca 147 }
wolfSSL 13:f67a6c6013ca 148
wolfSSL 13:f67a6c6013ca 149 /* public key : y = g^x mod p */
wolfSSL 13:f67a6c6013ca 150 err = mp_exptmod(&dsa->g, &dsa->x, &dsa->p, &dsa->y);
wolfSSL 13:f67a6c6013ca 151 if (err != MP_OKAY) {
wolfSSL 13:f67a6c6013ca 152 mp_clear(&dsa->x);
wolfSSL 13:f67a6c6013ca 153 mp_clear(&dsa->y);
wolfSSL 13:f67a6c6013ca 154 return err;
wolfSSL 13:f67a6c6013ca 155 }
wolfSSL 13:f67a6c6013ca 156
wolfSSL 13:f67a6c6013ca 157 dsa->type = DSA_PRIVATE;
wolfSSL 13:f67a6c6013ca 158
wolfSSL 13:f67a6c6013ca 159 return MP_OKAY;
wolfSSL 13:f67a6c6013ca 160 }
wolfSSL 13:f67a6c6013ca 161
wolfSSL 13:f67a6c6013ca 162 /* modulus_size in bits */
wolfSSL 13:f67a6c6013ca 163 int wc_MakeDsaParameters(WC_RNG *rng, int modulus_size, DsaKey *dsa)
wolfSSL 13:f67a6c6013ca 164 {
wolfSSL 13:f67a6c6013ca 165 mp_int tmp, tmp2;
wolfSSL 13:f67a6c6013ca 166 int err, msize, qsize,
wolfSSL 13:f67a6c6013ca 167 loop_check_prime = 0,
wolfSSL 13:f67a6c6013ca 168 check_prime = MP_NO;
wolfSSL 13:f67a6c6013ca 169 unsigned char *buf;
wolfSSL 13:f67a6c6013ca 170
wolfSSL 13:f67a6c6013ca 171 if (rng == NULL || dsa == NULL)
wolfSSL 13:f67a6c6013ca 172 return BAD_FUNC_ARG;
wolfSSL 13:f67a6c6013ca 173
wolfSSL 13:f67a6c6013ca 174 /* set group size in bytes from modulus size
wolfSSL 13:f67a6c6013ca 175 * FIPS 186-4 defines valid values (1024, 160) (2048, 256) (3072, 256)
wolfSSL 13:f67a6c6013ca 176 */
wolfSSL 13:f67a6c6013ca 177 switch (modulus_size) {
wolfSSL 13:f67a6c6013ca 178 case 1024:
wolfSSL 13:f67a6c6013ca 179 qsize = 20;
wolfSSL 13:f67a6c6013ca 180 break;
wolfSSL 13:f67a6c6013ca 181 case 2048:
wolfSSL 13:f67a6c6013ca 182 case 3072:
wolfSSL 13:f67a6c6013ca 183 qsize = 32;
wolfSSL 13:f67a6c6013ca 184 break;
wolfSSL 13:f67a6c6013ca 185 default:
wolfSSL 13:f67a6c6013ca 186 return BAD_FUNC_ARG;
wolfSSL 13:f67a6c6013ca 187 break;
wolfSSL 13:f67a6c6013ca 188 }
wolfSSL 13:f67a6c6013ca 189
wolfSSL 13:f67a6c6013ca 190 /* modulus size in bytes */
wolfSSL 13:f67a6c6013ca 191 msize = modulus_size / 8;
wolfSSL 13:f67a6c6013ca 192
wolfSSL 13:f67a6c6013ca 193 /* allocate ram */
wolfSSL 13:f67a6c6013ca 194 buf = (unsigned char *)XMALLOC(msize - qsize,
wolfSSL 13:f67a6c6013ca 195 dsa->heap, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 13:f67a6c6013ca 196 if (buf == NULL) {
wolfSSL 13:f67a6c6013ca 197 return MEMORY_E;
wolfSSL 13:f67a6c6013ca 198 }
wolfSSL 13:f67a6c6013ca 199
wolfSSL 13:f67a6c6013ca 200 /* make a random string that will be multplied against q */
wolfSSL 13:f67a6c6013ca 201 err = wc_RNG_GenerateBlock(rng, buf, msize - qsize);
wolfSSL 13:f67a6c6013ca 202 if (err != MP_OKAY) {
wolfSSL 13:f67a6c6013ca 203 XFREE(buf, dsa->heap, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 13:f67a6c6013ca 204 return err;
wolfSSL 13:f67a6c6013ca 205 }
wolfSSL 13:f67a6c6013ca 206
wolfSSL 13:f67a6c6013ca 207 /* force magnitude */
wolfSSL 13:f67a6c6013ca 208 buf[0] |= 0xC0;
wolfSSL 13:f67a6c6013ca 209
wolfSSL 13:f67a6c6013ca 210 /* force even */
wolfSSL 13:f67a6c6013ca 211 buf[msize - qsize - 1] &= ~1;
wolfSSL 13:f67a6c6013ca 212
wolfSSL 13:f67a6c6013ca 213 if (mp_init_multi(&tmp2, &dsa->p, &dsa->q, 0, 0, 0) != MP_OKAY) {
wolfSSL 13:f67a6c6013ca 214 mp_clear(&dsa->q);
wolfSSL 13:f67a6c6013ca 215 XFREE(buf, dsa->heap, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 13:f67a6c6013ca 216 return MP_INIT_E;
wolfSSL 13:f67a6c6013ca 217 }
wolfSSL 13:f67a6c6013ca 218
wolfSSL 13:f67a6c6013ca 219 err = mp_read_unsigned_bin(&tmp2, buf, msize - qsize);
wolfSSL 13:f67a6c6013ca 220 if (err != MP_OKAY) {
wolfSSL 13:f67a6c6013ca 221 mp_clear(&dsa->q);
wolfSSL 13:f67a6c6013ca 222 mp_clear(&dsa->p);
wolfSSL 13:f67a6c6013ca 223 mp_clear(&tmp2);
wolfSSL 13:f67a6c6013ca 224 XFREE(buf, dsa->heap, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 13:f67a6c6013ca 225 return err;
wolfSSL 13:f67a6c6013ca 226 }
wolfSSL 13:f67a6c6013ca 227 XFREE(buf, dsa->heap, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 13:f67a6c6013ca 228
wolfSSL 13:f67a6c6013ca 229 /* make our prime q */
wolfSSL 13:f67a6c6013ca 230 err = mp_rand_prime(&dsa->q, qsize, rng, NULL);
wolfSSL 13:f67a6c6013ca 231 if (err != MP_OKAY) {
wolfSSL 13:f67a6c6013ca 232 mp_clear(&dsa->q);
wolfSSL 13:f67a6c6013ca 233 mp_clear(&dsa->p);
wolfSSL 13:f67a6c6013ca 234 mp_clear(&tmp2);
wolfSSL 13:f67a6c6013ca 235 return err;
wolfSSL 13:f67a6c6013ca 236 }
wolfSSL 13:f67a6c6013ca 237
wolfSSL 13:f67a6c6013ca 238 /* p = random * q */
wolfSSL 13:f67a6c6013ca 239 err = mp_mul(&dsa->q, &tmp2, &dsa->p);
wolfSSL 13:f67a6c6013ca 240 if (err != MP_OKAY) {
wolfSSL 13:f67a6c6013ca 241 mp_clear(&dsa->q);
wolfSSL 13:f67a6c6013ca 242 mp_clear(&dsa->p);
wolfSSL 13:f67a6c6013ca 243 mp_clear(&tmp2);
wolfSSL 13:f67a6c6013ca 244 return err;
wolfSSL 13:f67a6c6013ca 245 }
wolfSSL 13:f67a6c6013ca 246
wolfSSL 13:f67a6c6013ca 247 /* p = random * q + 1, so q is a prime divisor of p-1 */
wolfSSL 13:f67a6c6013ca 248 err = mp_add_d(&dsa->p, 1, &dsa->p);
wolfSSL 13:f67a6c6013ca 249 if (err != MP_OKAY) {
wolfSSL 13:f67a6c6013ca 250 mp_clear(&dsa->q);
wolfSSL 13:f67a6c6013ca 251 mp_clear(&dsa->p);
wolfSSL 13:f67a6c6013ca 252 mp_clear(&tmp2);
wolfSSL 13:f67a6c6013ca 253 return err;
wolfSSL 13:f67a6c6013ca 254 }
wolfSSL 13:f67a6c6013ca 255
wolfSSL 13:f67a6c6013ca 256 if (mp_init(&tmp) != MP_OKAY) {
wolfSSL 13:f67a6c6013ca 257 mp_clear(&dsa->q);
wolfSSL 13:f67a6c6013ca 258 mp_clear(&dsa->p);
wolfSSL 13:f67a6c6013ca 259 mp_clear(&tmp2);
wolfSSL 13:f67a6c6013ca 260 return MP_INIT_E;
wolfSSL 13:f67a6c6013ca 261 }
wolfSSL 13:f67a6c6013ca 262
wolfSSL 13:f67a6c6013ca 263 /* tmp = 2q */
wolfSSL 13:f67a6c6013ca 264 err = mp_add(&dsa->q, &dsa->q, &tmp);
wolfSSL 13:f67a6c6013ca 265 if (err != MP_OKAY) {
wolfSSL 13:f67a6c6013ca 266 mp_clear(&dsa->q);
wolfSSL 13:f67a6c6013ca 267 mp_clear(&dsa->p);
wolfSSL 13:f67a6c6013ca 268 mp_clear(&tmp);
wolfSSL 13:f67a6c6013ca 269 mp_clear(&tmp2);
wolfSSL 13:f67a6c6013ca 270 return err;
wolfSSL 13:f67a6c6013ca 271 }
wolfSSL 13:f67a6c6013ca 272
wolfSSL 13:f67a6c6013ca 273 /* loop until p is prime */
wolfSSL 13:f67a6c6013ca 274 while (check_prime == MP_NO) {
wolfSSL 13:f67a6c6013ca 275 err = mp_prime_is_prime(&dsa->p, 8, &check_prime);
wolfSSL 13:f67a6c6013ca 276 if (err != MP_OKAY) {
wolfSSL 13:f67a6c6013ca 277 mp_clear(&dsa->q);
wolfSSL 13:f67a6c6013ca 278 mp_clear(&dsa->p);
wolfSSL 13:f67a6c6013ca 279 mp_clear(&tmp);
wolfSSL 13:f67a6c6013ca 280 mp_clear(&tmp2);
wolfSSL 13:f67a6c6013ca 281 return err;
wolfSSL 13:f67a6c6013ca 282 }
wolfSSL 13:f67a6c6013ca 283
wolfSSL 13:f67a6c6013ca 284 if (check_prime != MP_YES) {
wolfSSL 13:f67a6c6013ca 285 /* p += 2q */
wolfSSL 13:f67a6c6013ca 286 err = mp_add(&tmp, &dsa->p, &dsa->p);
wolfSSL 13:f67a6c6013ca 287 if (err != MP_OKAY) {
wolfSSL 13:f67a6c6013ca 288 mp_clear(&dsa->q);
wolfSSL 13:f67a6c6013ca 289 mp_clear(&dsa->p);
wolfSSL 13:f67a6c6013ca 290 mp_clear(&tmp);
wolfSSL 13:f67a6c6013ca 291 mp_clear(&tmp2);
wolfSSL 13:f67a6c6013ca 292 return err;
wolfSSL 13:f67a6c6013ca 293 }
wolfSSL 13:f67a6c6013ca 294
wolfSSL 13:f67a6c6013ca 295 loop_check_prime++;
wolfSSL 13:f67a6c6013ca 296 }
wolfSSL 13:f67a6c6013ca 297 }
wolfSSL 13:f67a6c6013ca 298
wolfSSL 13:f67a6c6013ca 299 /* tmp2 += (2*loop_check_prime)
wolfSSL 13:f67a6c6013ca 300 * to have p = (q * tmp2) + 1 prime
wolfSSL 13:f67a6c6013ca 301 */
wolfSSL 13:f67a6c6013ca 302 if (loop_check_prime) {
wolfSSL 13:f67a6c6013ca 303 err = mp_add_d(&tmp2, 2*loop_check_prime, &tmp2);
wolfSSL 13:f67a6c6013ca 304 if (err != MP_OKAY) {
wolfSSL 13:f67a6c6013ca 305 mp_clear(&dsa->q);
wolfSSL 13:f67a6c6013ca 306 mp_clear(&dsa->p);
wolfSSL 13:f67a6c6013ca 307 mp_clear(&tmp);
wolfSSL 13:f67a6c6013ca 308 mp_clear(&tmp2);
wolfSSL 13:f67a6c6013ca 309 return err;
wolfSSL 13:f67a6c6013ca 310 }
wolfSSL 13:f67a6c6013ca 311 }
wolfSSL 13:f67a6c6013ca 312
wolfSSL 13:f67a6c6013ca 313 if (mp_init(&dsa->g) != MP_OKAY) {
wolfSSL 13:f67a6c6013ca 314 mp_clear(&dsa->q);
wolfSSL 13:f67a6c6013ca 315 mp_clear(&dsa->p);
wolfSSL 13:f67a6c6013ca 316 mp_clear(&tmp);
wolfSSL 13:f67a6c6013ca 317 mp_clear(&tmp2);
wolfSSL 13:f67a6c6013ca 318 return MP_INIT_E;
wolfSSL 13:f67a6c6013ca 319 }
wolfSSL 13:f67a6c6013ca 320
wolfSSL 13:f67a6c6013ca 321 /* find a value g for which g^tmp2 != 1 */
wolfSSL 13:f67a6c6013ca 322 if (mp_set(&dsa->g, 1) != MP_OKAY) {
wolfSSL 13:f67a6c6013ca 323 mp_clear(&dsa->q);
wolfSSL 13:f67a6c6013ca 324 mp_clear(&dsa->p);
wolfSSL 13:f67a6c6013ca 325 mp_clear(&tmp);
wolfSSL 13:f67a6c6013ca 326 mp_clear(&tmp2);
wolfSSL 13:f67a6c6013ca 327 return MP_INIT_E;
wolfSSL 13:f67a6c6013ca 328 }
wolfSSL 13:f67a6c6013ca 329
wolfSSL 13:f67a6c6013ca 330 do {
wolfSSL 13:f67a6c6013ca 331 err = mp_add_d(&dsa->g, 1, &dsa->g);
wolfSSL 13:f67a6c6013ca 332 if (err != MP_OKAY) {
wolfSSL 13:f67a6c6013ca 333 mp_clear(&dsa->q);
wolfSSL 13:f67a6c6013ca 334 mp_clear(&dsa->p);
wolfSSL 13:f67a6c6013ca 335 mp_clear(&dsa->g);
wolfSSL 13:f67a6c6013ca 336 mp_clear(&tmp);
wolfSSL 13:f67a6c6013ca 337 mp_clear(&tmp2);
wolfSSL 13:f67a6c6013ca 338 return err;
wolfSSL 13:f67a6c6013ca 339 }
wolfSSL 13:f67a6c6013ca 340
wolfSSL 13:f67a6c6013ca 341 err = mp_exptmod(&dsa->g, &tmp2, &dsa->p, &tmp);
wolfSSL 13:f67a6c6013ca 342 if (err != MP_OKAY) {
wolfSSL 13:f67a6c6013ca 343 mp_clear(&dsa->q);
wolfSSL 13:f67a6c6013ca 344 mp_clear(&dsa->p);
wolfSSL 13:f67a6c6013ca 345 mp_clear(&dsa->g);
wolfSSL 13:f67a6c6013ca 346 mp_clear(&tmp);
wolfSSL 13:f67a6c6013ca 347 mp_clear(&tmp2);
wolfSSL 13:f67a6c6013ca 348 return err;
wolfSSL 13:f67a6c6013ca 349 }
wolfSSL 13:f67a6c6013ca 350
wolfSSL 13:f67a6c6013ca 351 } while (mp_cmp_d(&tmp, 1) == MP_EQ);
wolfSSL 13:f67a6c6013ca 352
wolfSSL 13:f67a6c6013ca 353 /* at this point tmp generates a group of order q mod p */
wolfSSL 13:f67a6c6013ca 354 mp_exch(&tmp, &dsa->g);
wolfSSL 13:f67a6c6013ca 355
wolfSSL 13:f67a6c6013ca 356 mp_clear(&tmp);
wolfSSL 13:f67a6c6013ca 357 mp_clear(&tmp2);
wolfSSL 13:f67a6c6013ca 358
wolfSSL 13:f67a6c6013ca 359 return MP_OKAY;
wolfSSL 13:f67a6c6013ca 360 }
wolfSSL 13:f67a6c6013ca 361 #endif /* WOLFSSL_KEY_GEN */
wolfSSL 13:f67a6c6013ca 362
wolfSSL 13:f67a6c6013ca 363
wolfSSL 13:f67a6c6013ca 364 int wc_DsaSign(const byte* digest, byte* out, DsaKey* key, WC_RNG* rng)
wolfSSL 13:f67a6c6013ca 365 {
wolfSSL 13:f67a6c6013ca 366 mp_int k, kInv, r, s, H;
wolfSSL 13:f67a6c6013ca 367 int ret, sz;
wolfSSL 13:f67a6c6013ca 368 byte buffer[DSA_HALF_SIZE];
wolfSSL 13:f67a6c6013ca 369 byte* tmp = out; /* initial output pointer */
wolfSSL 13:f67a6c6013ca 370
wolfSSL 13:f67a6c6013ca 371 sz = min((int)sizeof(buffer), mp_unsigned_bin_size(&key->q));
wolfSSL 13:f67a6c6013ca 372
wolfSSL 13:f67a6c6013ca 373 if (mp_init_multi(&k, &kInv, &r, &s, &H, 0) != MP_OKAY)
wolfSSL 13:f67a6c6013ca 374 return MP_INIT_E;
wolfSSL 13:f67a6c6013ca 375
wolfSSL 13:f67a6c6013ca 376 do {
wolfSSL 13:f67a6c6013ca 377 /* generate k */
wolfSSL 13:f67a6c6013ca 378 ret = wc_RNG_GenerateBlock(rng, buffer, sz);
wolfSSL 13:f67a6c6013ca 379 if (ret != 0)
wolfSSL 13:f67a6c6013ca 380 return ret;
wolfSSL 13:f67a6c6013ca 381
wolfSSL 13:f67a6c6013ca 382 buffer[0] |= 0x0C;
wolfSSL 13:f67a6c6013ca 383
wolfSSL 13:f67a6c6013ca 384 if (mp_read_unsigned_bin(&k, buffer, sz) != MP_OKAY)
wolfSSL 13:f67a6c6013ca 385 ret = MP_READ_E;
wolfSSL 13:f67a6c6013ca 386
wolfSSL 13:f67a6c6013ca 387 /* k is a random numnber and it should be less than q
wolfSSL 13:f67a6c6013ca 388 * if k greater than repeat
wolfSSL 13:f67a6c6013ca 389 */
wolfSSL 13:f67a6c6013ca 390 } while (mp_cmp(&k, &key->q) != MP_LT);
wolfSSL 13:f67a6c6013ca 391
wolfSSL 13:f67a6c6013ca 392 if (ret == 0 && mp_cmp_d(&k, 1) != MP_GT)
wolfSSL 13:f67a6c6013ca 393 ret = MP_CMP_E;
wolfSSL 13:f67a6c6013ca 394
wolfSSL 13:f67a6c6013ca 395 /* inverse k mod q */
wolfSSL 13:f67a6c6013ca 396 if (ret == 0 && mp_invmod(&k, &key->q, &kInv) != MP_OKAY)
wolfSSL 13:f67a6c6013ca 397 ret = MP_INVMOD_E;
wolfSSL 13:f67a6c6013ca 398
wolfSSL 13:f67a6c6013ca 399 /* generate r, r = (g exp k mod p) mod q */
wolfSSL 13:f67a6c6013ca 400 if (ret == 0 && mp_exptmod(&key->g, &k, &key->p, &r) != MP_OKAY)
wolfSSL 13:f67a6c6013ca 401 ret = MP_EXPTMOD_E;
wolfSSL 13:f67a6c6013ca 402
wolfSSL 13:f67a6c6013ca 403 if (ret == 0 && mp_mod(&r, &key->q, &r) != MP_OKAY)
wolfSSL 13:f67a6c6013ca 404 ret = MP_MOD_E;
wolfSSL 13:f67a6c6013ca 405
wolfSSL 13:f67a6c6013ca 406 /* generate H from sha digest */
wolfSSL 13:f67a6c6013ca 407 if (ret == 0 && mp_read_unsigned_bin(&H, digest,SHA_DIGEST_SIZE) != MP_OKAY)
wolfSSL 13:f67a6c6013ca 408 ret = MP_READ_E;
wolfSSL 13:f67a6c6013ca 409
wolfSSL 13:f67a6c6013ca 410 /* generate s, s = (kInv * (H + x*r)) % q */
wolfSSL 13:f67a6c6013ca 411 if (ret == 0 && mp_mul(&key->x, &r, &s) != MP_OKAY)
wolfSSL 13:f67a6c6013ca 412 ret = MP_MUL_E;
wolfSSL 13:f67a6c6013ca 413
wolfSSL 13:f67a6c6013ca 414 if (ret == 0 && mp_add(&s, &H, &s) != MP_OKAY)
wolfSSL 13:f67a6c6013ca 415 ret = MP_ADD_E;
wolfSSL 13:f67a6c6013ca 416
wolfSSL 13:f67a6c6013ca 417 if (ret == 0 && mp_mulmod(&s, &kInv, &key->q, &s) != MP_OKAY)
wolfSSL 13:f67a6c6013ca 418 ret = MP_MULMOD_E;
wolfSSL 13:f67a6c6013ca 419
wolfSSL 13:f67a6c6013ca 420 /* detect zero r or s */
wolfSSL 13:f67a6c6013ca 421 if (ret == 0 && (mp_iszero(&r) == MP_YES || mp_iszero(&s) == MP_YES))
wolfSSL 13:f67a6c6013ca 422 ret = MP_ZERO_E;
wolfSSL 13:f67a6c6013ca 423
wolfSSL 13:f67a6c6013ca 424 /* write out */
wolfSSL 13:f67a6c6013ca 425 if (ret == 0) {
wolfSSL 13:f67a6c6013ca 426 int rSz = mp_unsigned_bin_size(&r);
wolfSSL 13:f67a6c6013ca 427 int sSz = mp_unsigned_bin_size(&s);
wolfSSL 13:f67a6c6013ca 428
wolfSSL 13:f67a6c6013ca 429 while (rSz++ < DSA_HALF_SIZE) {
wolfSSL 13:f67a6c6013ca 430 *out++ = 0x00; /* pad front with zeros */
wolfSSL 13:f67a6c6013ca 431 }
wolfSSL 13:f67a6c6013ca 432
wolfSSL 13:f67a6c6013ca 433 if (mp_to_unsigned_bin(&r, out) != MP_OKAY)
wolfSSL 13:f67a6c6013ca 434 ret = MP_TO_E;
wolfSSL 13:f67a6c6013ca 435 else {
wolfSSL 13:f67a6c6013ca 436 out = tmp + DSA_HALF_SIZE; /* advance to s in output */
wolfSSL 13:f67a6c6013ca 437 while (sSz++ < DSA_HALF_SIZE) {
wolfSSL 13:f67a6c6013ca 438 *out++ = 0x00; /* pad front with zeros */
wolfSSL 13:f67a6c6013ca 439 }
wolfSSL 13:f67a6c6013ca 440 ret = mp_to_unsigned_bin(&s, out);
wolfSSL 13:f67a6c6013ca 441 }
wolfSSL 13:f67a6c6013ca 442 }
wolfSSL 13:f67a6c6013ca 443
wolfSSL 13:f67a6c6013ca 444 mp_clear(&H);
wolfSSL 13:f67a6c6013ca 445 mp_clear(&s);
wolfSSL 13:f67a6c6013ca 446 mp_clear(&r);
wolfSSL 13:f67a6c6013ca 447 mp_clear(&kInv);
wolfSSL 13:f67a6c6013ca 448 mp_clear(&k);
wolfSSL 13:f67a6c6013ca 449
wolfSSL 13:f67a6c6013ca 450 return ret;
wolfSSL 13:f67a6c6013ca 451 }
wolfSSL 13:f67a6c6013ca 452
wolfSSL 13:f67a6c6013ca 453
wolfSSL 13:f67a6c6013ca 454 int wc_DsaVerify(const byte* digest, const byte* sig, DsaKey* key, int* answer)
wolfSSL 13:f67a6c6013ca 455 {
wolfSSL 13:f67a6c6013ca 456 mp_int w, u1, u2, v, r, s;
wolfSSL 13:f67a6c6013ca 457 int ret = 0;
wolfSSL 13:f67a6c6013ca 458
wolfSSL 13:f67a6c6013ca 459 if (mp_init_multi(&w, &u1, &u2, &v, &r, &s) != MP_OKAY)
wolfSSL 13:f67a6c6013ca 460 return MP_INIT_E;
wolfSSL 13:f67a6c6013ca 461
wolfSSL 13:f67a6c6013ca 462 /* set r and s from signature */
wolfSSL 13:f67a6c6013ca 463 if (mp_read_unsigned_bin(&r, sig, DSA_HALF_SIZE) != MP_OKAY ||
wolfSSL 13:f67a6c6013ca 464 mp_read_unsigned_bin(&s, sig + DSA_HALF_SIZE, DSA_HALF_SIZE) != MP_OKAY)
wolfSSL 13:f67a6c6013ca 465 ret = MP_READ_E;
wolfSSL 13:f67a6c6013ca 466
wolfSSL 13:f67a6c6013ca 467 /* sanity checks */
wolfSSL 13:f67a6c6013ca 468 if (ret == 0) {
wolfSSL 13:f67a6c6013ca 469 if (mp_iszero(&r) == MP_YES || mp_iszero(&s) == MP_YES ||
wolfSSL 13:f67a6c6013ca 470 mp_cmp(&r, &key->q) != MP_LT || mp_cmp(&s, &key->q) != MP_LT) {
wolfSSL 13:f67a6c6013ca 471 ret = MP_ZERO_E;
wolfSSL 13:f67a6c6013ca 472 }
wolfSSL 13:f67a6c6013ca 473 }
wolfSSL 13:f67a6c6013ca 474
wolfSSL 13:f67a6c6013ca 475 /* put H into u1 from sha digest */
wolfSSL 13:f67a6c6013ca 476 if (ret == 0 && mp_read_unsigned_bin(&u1,digest,SHA_DIGEST_SIZE) != MP_OKAY)
wolfSSL 13:f67a6c6013ca 477 ret = MP_READ_E;
wolfSSL 13:f67a6c6013ca 478
wolfSSL 13:f67a6c6013ca 479 /* w = s invmod q */
wolfSSL 13:f67a6c6013ca 480 if (ret == 0 && mp_invmod(&s, &key->q, &w) != MP_OKAY)
wolfSSL 13:f67a6c6013ca 481 ret = MP_INVMOD_E;
wolfSSL 13:f67a6c6013ca 482
wolfSSL 13:f67a6c6013ca 483 /* u1 = (H * w) % q */
wolfSSL 13:f67a6c6013ca 484 if (ret == 0 && mp_mulmod(&u1, &w, &key->q, &u1) != MP_OKAY)
wolfSSL 13:f67a6c6013ca 485 ret = MP_MULMOD_E;
wolfSSL 13:f67a6c6013ca 486
wolfSSL 13:f67a6c6013ca 487 /* u2 = (r * w) % q */
wolfSSL 13:f67a6c6013ca 488 if (ret == 0 && mp_mulmod(&r, &w, &key->q, &u2) != MP_OKAY)
wolfSSL 13:f67a6c6013ca 489 ret = MP_MULMOD_E;
wolfSSL 13:f67a6c6013ca 490
wolfSSL 13:f67a6c6013ca 491 /* verify v = ((g^u1 * y^u2) mod p) mod q */
wolfSSL 13:f67a6c6013ca 492 if (ret == 0 && mp_exptmod(&key->g, &u1, &key->p, &u1) != MP_OKAY)
wolfSSL 13:f67a6c6013ca 493 ret = MP_EXPTMOD_E;
wolfSSL 13:f67a6c6013ca 494
wolfSSL 13:f67a6c6013ca 495 if (ret == 0 && mp_exptmod(&key->y, &u2, &key->p, &u2) != MP_OKAY)
wolfSSL 13:f67a6c6013ca 496 ret = MP_EXPTMOD_E;
wolfSSL 13:f67a6c6013ca 497
wolfSSL 13:f67a6c6013ca 498 if (ret == 0 && mp_mulmod(&u1, &u2, &key->p, &v) != MP_OKAY)
wolfSSL 13:f67a6c6013ca 499 ret = MP_MULMOD_E;
wolfSSL 13:f67a6c6013ca 500
wolfSSL 13:f67a6c6013ca 501 if (ret == 0 && mp_mod(&v, &key->q, &v) != MP_OKAY)
wolfSSL 13:f67a6c6013ca 502 ret = MP_MULMOD_E;
wolfSSL 13:f67a6c6013ca 503
wolfSSL 13:f67a6c6013ca 504 /* do they match */
wolfSSL 13:f67a6c6013ca 505 if (ret == 0 && mp_cmp(&r, &v) == MP_EQ)
wolfSSL 13:f67a6c6013ca 506 *answer = 1;
wolfSSL 13:f67a6c6013ca 507 else
wolfSSL 13:f67a6c6013ca 508 *answer = 0;
wolfSSL 13:f67a6c6013ca 509
wolfSSL 13:f67a6c6013ca 510 mp_clear(&s);
wolfSSL 13:f67a6c6013ca 511 mp_clear(&r);
wolfSSL 13:f67a6c6013ca 512 mp_clear(&u1);
wolfSSL 13:f67a6c6013ca 513 mp_clear(&u2);
wolfSSL 13:f67a6c6013ca 514 mp_clear(&w);
wolfSSL 13:f67a6c6013ca 515 mp_clear(&v);
wolfSSL 13:f67a6c6013ca 516
wolfSSL 13:f67a6c6013ca 517 return ret;
wolfSSL 13:f67a6c6013ca 518 }
wolfSSL 13:f67a6c6013ca 519
wolfSSL 13:f67a6c6013ca 520
wolfSSL 13:f67a6c6013ca 521 #endif /* NO_DSA */
wolfSSL 13:f67a6c6013ca 522
wolfSSL 13:f67a6c6013ca 523