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