wolfSSL 3.11.1 for TLS1.3 beta

Fork of wolfSSL by wolf SSL

Committer:
wolfSSL
Date:
Tue May 02 08:44:47 2017 +0000
Revision:
7:481bce714567
wolfSSL3.10.2

Who changed what in which revision?

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