SSL/TLS Library
CyaSSL is SSL/TLS library for embedded systems.
ctaocrypt/src/dsa.c@0:9d17e4342598, 2014-04-20 (annotated)
- Committer:
- wolfSSL
- Date:
- Sun Apr 20 12:40:57 2014 +0000
- Revision:
- 0:9d17e4342598
CyaSSL SSL/TLS Library 2.9.4;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wolfSSL | 0:9d17e4342598 | 1 | /* dsa.c |
wolfSSL | 0:9d17e4342598 | 2 | * |
wolfSSL | 0:9d17e4342598 | 3 | * Copyright (C) 2006-2013 wolfSSL Inc. |
wolfSSL | 0:9d17e4342598 | 4 | * |
wolfSSL | 0:9d17e4342598 | 5 | * This file is part of CyaSSL. |
wolfSSL | 0:9d17e4342598 | 6 | * |
wolfSSL | 0:9d17e4342598 | 7 | * CyaSSL is free software; you can redistribute it and/or modify |
wolfSSL | 0:9d17e4342598 | 8 | * it under the terms of the GNU General Public License as published by |
wolfSSL | 0:9d17e4342598 | 9 | * the Free Software Foundation; either version 2 of the License, or |
wolfSSL | 0:9d17e4342598 | 10 | * (at your option) any later version. |
wolfSSL | 0:9d17e4342598 | 11 | * |
wolfSSL | 0:9d17e4342598 | 12 | * CyaSSL is distributed in the hope that it will be useful, |
wolfSSL | 0:9d17e4342598 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
wolfSSL | 0:9d17e4342598 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wolfSSL | 0:9d17e4342598 | 15 | * GNU General Public License for more details. |
wolfSSL | 0:9d17e4342598 | 16 | * |
wolfSSL | 0:9d17e4342598 | 17 | * You should have received a copy of the GNU General Public License |
wolfSSL | 0:9d17e4342598 | 18 | * along with this program; if not, write to the Free Software |
wolfSSL | 0:9d17e4342598 | 19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
wolfSSL | 0:9d17e4342598 | 20 | */ |
wolfSSL | 0:9d17e4342598 | 21 | |
wolfSSL | 0:9d17e4342598 | 22 | #ifdef HAVE_CONFIG_H |
wolfSSL | 0:9d17e4342598 | 23 | #include <config.h> |
wolfSSL | 0:9d17e4342598 | 24 | #endif |
wolfSSL | 0:9d17e4342598 | 25 | |
wolfSSL | 0:9d17e4342598 | 26 | #include <cyassl/ctaocrypt/settings.h> |
wolfSSL | 0:9d17e4342598 | 27 | |
wolfSSL | 0:9d17e4342598 | 28 | #ifndef NO_DSA |
wolfSSL | 0:9d17e4342598 | 29 | |
wolfSSL | 0:9d17e4342598 | 30 | #include <cyassl/ctaocrypt/dsa.h> |
wolfSSL | 0:9d17e4342598 | 31 | #include <cyassl/ctaocrypt/sha.h> |
wolfSSL | 0:9d17e4342598 | 32 | #include <cyassl/ctaocrypt/random.h> |
wolfSSL | 0:9d17e4342598 | 33 | #include <cyassl/ctaocrypt/error-crypt.h> |
wolfSSL | 0:9d17e4342598 | 34 | |
wolfSSL | 0:9d17e4342598 | 35 | |
wolfSSL | 0:9d17e4342598 | 36 | enum { |
wolfSSL | 0:9d17e4342598 | 37 | DSA_HALF_SIZE = 20, /* r and s size */ |
wolfSSL | 0:9d17e4342598 | 38 | DSA_SIG_SIZE = 40 /* signature size */ |
wolfSSL | 0:9d17e4342598 | 39 | }; |
wolfSSL | 0:9d17e4342598 | 40 | |
wolfSSL | 0:9d17e4342598 | 41 | |
wolfSSL | 0:9d17e4342598 | 42 | #ifndef min |
wolfSSL | 0:9d17e4342598 | 43 | |
wolfSSL | 0:9d17e4342598 | 44 | static INLINE word32 min(word32 a, word32 b) |
wolfSSL | 0:9d17e4342598 | 45 | { |
wolfSSL | 0:9d17e4342598 | 46 | return a > b ? b : a; |
wolfSSL | 0:9d17e4342598 | 47 | } |
wolfSSL | 0:9d17e4342598 | 48 | |
wolfSSL | 0:9d17e4342598 | 49 | #endif /* min */ |
wolfSSL | 0:9d17e4342598 | 50 | |
wolfSSL | 0:9d17e4342598 | 51 | |
wolfSSL | 0:9d17e4342598 | 52 | void InitDsaKey(DsaKey* key) |
wolfSSL | 0:9d17e4342598 | 53 | { |
wolfSSL | 0:9d17e4342598 | 54 | key->type = -1; /* haven't decided yet */ |
wolfSSL | 0:9d17e4342598 | 55 | |
wolfSSL | 0:9d17e4342598 | 56 | /* TomsFastMath doesn't use memory allocation */ |
wolfSSL | 0:9d17e4342598 | 57 | #ifndef USE_FAST_MATH |
wolfSSL | 0:9d17e4342598 | 58 | key->p.dp = 0; /* public alloc parts */ |
wolfSSL | 0:9d17e4342598 | 59 | key->q.dp = 0; |
wolfSSL | 0:9d17e4342598 | 60 | key->g.dp = 0; |
wolfSSL | 0:9d17e4342598 | 61 | key->y.dp = 0; |
wolfSSL | 0:9d17e4342598 | 62 | |
wolfSSL | 0:9d17e4342598 | 63 | key->x.dp = 0; /* private alloc parts */ |
wolfSSL | 0:9d17e4342598 | 64 | #endif |
wolfSSL | 0:9d17e4342598 | 65 | } |
wolfSSL | 0:9d17e4342598 | 66 | |
wolfSSL | 0:9d17e4342598 | 67 | |
wolfSSL | 0:9d17e4342598 | 68 | void FreeDsaKey(DsaKey* key) |
wolfSSL | 0:9d17e4342598 | 69 | { |
wolfSSL | 0:9d17e4342598 | 70 | (void)key; |
wolfSSL | 0:9d17e4342598 | 71 | /* TomsFastMath doesn't use memory allocation */ |
wolfSSL | 0:9d17e4342598 | 72 | #ifndef USE_FAST_MATH |
wolfSSL | 0:9d17e4342598 | 73 | if (key->type == DSA_PRIVATE) |
wolfSSL | 0:9d17e4342598 | 74 | mp_clear(&key->x); |
wolfSSL | 0:9d17e4342598 | 75 | mp_clear(&key->y); |
wolfSSL | 0:9d17e4342598 | 76 | mp_clear(&key->g); |
wolfSSL | 0:9d17e4342598 | 77 | mp_clear(&key->q); |
wolfSSL | 0:9d17e4342598 | 78 | mp_clear(&key->p); |
wolfSSL | 0:9d17e4342598 | 79 | #endif |
wolfSSL | 0:9d17e4342598 | 80 | } |
wolfSSL | 0:9d17e4342598 | 81 | |
wolfSSL | 0:9d17e4342598 | 82 | |
wolfSSL | 0:9d17e4342598 | 83 | int DsaSign(const byte* digest, byte* out, DsaKey* key, RNG* rng) |
wolfSSL | 0:9d17e4342598 | 84 | { |
wolfSSL | 0:9d17e4342598 | 85 | mp_int k, kInv, r, s, H; |
wolfSSL | 0:9d17e4342598 | 86 | int ret = 0, sz; |
wolfSSL | 0:9d17e4342598 | 87 | byte buffer[DSA_HALF_SIZE]; |
wolfSSL | 0:9d17e4342598 | 88 | |
wolfSSL | 0:9d17e4342598 | 89 | if (mp_init_multi(&k, &kInv, &r, &s, &H, 0) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 90 | return MP_INIT_E; |
wolfSSL | 0:9d17e4342598 | 91 | |
wolfSSL | 0:9d17e4342598 | 92 | sz = min(sizeof(buffer), mp_unsigned_bin_size(&key->q)); |
wolfSSL | 0:9d17e4342598 | 93 | |
wolfSSL | 0:9d17e4342598 | 94 | /* generate k */ |
wolfSSL | 0:9d17e4342598 | 95 | RNG_GenerateBlock(rng, buffer, sz); |
wolfSSL | 0:9d17e4342598 | 96 | buffer[0] |= 0x0C; |
wolfSSL | 0:9d17e4342598 | 97 | |
wolfSSL | 0:9d17e4342598 | 98 | if (mp_read_unsigned_bin(&k, buffer, sz) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 99 | ret = MP_READ_E; |
wolfSSL | 0:9d17e4342598 | 100 | |
wolfSSL | 0:9d17e4342598 | 101 | if (mp_cmp_d(&k, 1) != MP_GT) |
wolfSSL | 0:9d17e4342598 | 102 | ret = MP_CMP_E; |
wolfSSL | 0:9d17e4342598 | 103 | |
wolfSSL | 0:9d17e4342598 | 104 | /* inverse k mod q */ |
wolfSSL | 0:9d17e4342598 | 105 | if (ret == 0 && mp_invmod(&k, &key->q, &kInv) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 106 | ret = MP_INVMOD_E; |
wolfSSL | 0:9d17e4342598 | 107 | |
wolfSSL | 0:9d17e4342598 | 108 | /* generate r, r = (g exp k mod p) mod q */ |
wolfSSL | 0:9d17e4342598 | 109 | if (ret == 0 && mp_exptmod(&key->g, &k, &key->p, &r) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 110 | ret = MP_EXPTMOD_E; |
wolfSSL | 0:9d17e4342598 | 111 | |
wolfSSL | 0:9d17e4342598 | 112 | if (ret == 0 && mp_mod(&r, &key->q, &r) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 113 | ret = MP_MOD_E; |
wolfSSL | 0:9d17e4342598 | 114 | |
wolfSSL | 0:9d17e4342598 | 115 | /* generate H from sha digest */ |
wolfSSL | 0:9d17e4342598 | 116 | if (ret == 0 && mp_read_unsigned_bin(&H, digest,SHA_DIGEST_SIZE) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 117 | ret = MP_READ_E; |
wolfSSL | 0:9d17e4342598 | 118 | |
wolfSSL | 0:9d17e4342598 | 119 | /* generate s, s = (kInv * (H + x*r)) % q */ |
wolfSSL | 0:9d17e4342598 | 120 | if (ret == 0 && mp_mul(&key->x, &r, &s) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 121 | ret = MP_MUL_E; |
wolfSSL | 0:9d17e4342598 | 122 | |
wolfSSL | 0:9d17e4342598 | 123 | if (ret == 0 && mp_add(&s, &H, &s) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 124 | ret = MP_ADD_E; |
wolfSSL | 0:9d17e4342598 | 125 | |
wolfSSL | 0:9d17e4342598 | 126 | if (ret == 0 && mp_mulmod(&s, &kInv, &key->q, &s) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 127 | ret = MP_MULMOD_E; |
wolfSSL | 0:9d17e4342598 | 128 | |
wolfSSL | 0:9d17e4342598 | 129 | /* write out */ |
wolfSSL | 0:9d17e4342598 | 130 | if (ret == 0) { |
wolfSSL | 0:9d17e4342598 | 131 | int rSz = mp_unsigned_bin_size(&r); |
wolfSSL | 0:9d17e4342598 | 132 | int sSz = mp_unsigned_bin_size(&s); |
wolfSSL | 0:9d17e4342598 | 133 | |
wolfSSL | 0:9d17e4342598 | 134 | if (rSz == DSA_HALF_SIZE - 1) { |
wolfSSL | 0:9d17e4342598 | 135 | out[0] = 0; |
wolfSSL | 0:9d17e4342598 | 136 | out++; |
wolfSSL | 0:9d17e4342598 | 137 | } |
wolfSSL | 0:9d17e4342598 | 138 | |
wolfSSL | 0:9d17e4342598 | 139 | if (mp_to_unsigned_bin(&r, out) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 140 | ret = MP_TO_E; |
wolfSSL | 0:9d17e4342598 | 141 | else { |
wolfSSL | 0:9d17e4342598 | 142 | if (sSz == DSA_HALF_SIZE - 1) { |
wolfSSL | 0:9d17e4342598 | 143 | out[rSz] = 0; |
wolfSSL | 0:9d17e4342598 | 144 | out++; |
wolfSSL | 0:9d17e4342598 | 145 | } |
wolfSSL | 0:9d17e4342598 | 146 | ret = mp_to_unsigned_bin(&s, out + rSz); |
wolfSSL | 0:9d17e4342598 | 147 | } |
wolfSSL | 0:9d17e4342598 | 148 | } |
wolfSSL | 0:9d17e4342598 | 149 | |
wolfSSL | 0:9d17e4342598 | 150 | mp_clear(&H); |
wolfSSL | 0:9d17e4342598 | 151 | mp_clear(&s); |
wolfSSL | 0:9d17e4342598 | 152 | mp_clear(&r); |
wolfSSL | 0:9d17e4342598 | 153 | mp_clear(&kInv); |
wolfSSL | 0:9d17e4342598 | 154 | mp_clear(&k); |
wolfSSL | 0:9d17e4342598 | 155 | |
wolfSSL | 0:9d17e4342598 | 156 | return ret; |
wolfSSL | 0:9d17e4342598 | 157 | } |
wolfSSL | 0:9d17e4342598 | 158 | |
wolfSSL | 0:9d17e4342598 | 159 | |
wolfSSL | 0:9d17e4342598 | 160 | int DsaVerify(const byte* digest, const byte* sig, DsaKey* key, int* answer) |
wolfSSL | 0:9d17e4342598 | 161 | { |
wolfSSL | 0:9d17e4342598 | 162 | mp_int w, u1, u2, v, r, s; |
wolfSSL | 0:9d17e4342598 | 163 | int ret = 0; |
wolfSSL | 0:9d17e4342598 | 164 | |
wolfSSL | 0:9d17e4342598 | 165 | if (mp_init_multi(&w, &u1, &u2, &v, &r, &s) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 166 | return MP_INIT_E; |
wolfSSL | 0:9d17e4342598 | 167 | |
wolfSSL | 0:9d17e4342598 | 168 | /* set r and s from signature */ |
wolfSSL | 0:9d17e4342598 | 169 | if (mp_read_unsigned_bin(&r, sig, DSA_HALF_SIZE) != MP_OKAY || |
wolfSSL | 0:9d17e4342598 | 170 | mp_read_unsigned_bin(&s, sig + DSA_HALF_SIZE, DSA_HALF_SIZE) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 171 | ret = MP_READ_E; |
wolfSSL | 0:9d17e4342598 | 172 | |
wolfSSL | 0:9d17e4342598 | 173 | /* sanity checks */ |
wolfSSL | 0:9d17e4342598 | 174 | |
wolfSSL | 0:9d17e4342598 | 175 | |
wolfSSL | 0:9d17e4342598 | 176 | /* put H into u1 from sha digest */ |
wolfSSL | 0:9d17e4342598 | 177 | if (ret == 0 && mp_read_unsigned_bin(&u1,digest,SHA_DIGEST_SIZE) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 178 | ret = MP_READ_E; |
wolfSSL | 0:9d17e4342598 | 179 | |
wolfSSL | 0:9d17e4342598 | 180 | /* w = s invmod q */ |
wolfSSL | 0:9d17e4342598 | 181 | if (ret == 0 && mp_invmod(&s, &key->q, &w) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 182 | ret = MP_INVMOD_E; |
wolfSSL | 0:9d17e4342598 | 183 | |
wolfSSL | 0:9d17e4342598 | 184 | /* u1 = (H * w) % q */ |
wolfSSL | 0:9d17e4342598 | 185 | if (ret == 0 && mp_mulmod(&u1, &w, &key->q, &u1) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 186 | ret = MP_MULMOD_E; |
wolfSSL | 0:9d17e4342598 | 187 | |
wolfSSL | 0:9d17e4342598 | 188 | /* u2 = (r * w) % q */ |
wolfSSL | 0:9d17e4342598 | 189 | if (ret == 0 && mp_mulmod(&r, &w, &key->q, &u2) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 190 | ret = MP_MULMOD_E; |
wolfSSL | 0:9d17e4342598 | 191 | |
wolfSSL | 0:9d17e4342598 | 192 | /* verify v = ((g^u1 * y^u2) mod p) mod q */ |
wolfSSL | 0:9d17e4342598 | 193 | if (ret == 0 && mp_exptmod(&key->g, &u1, &key->p, &u1) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 194 | ret = MP_EXPTMOD_E; |
wolfSSL | 0:9d17e4342598 | 195 | |
wolfSSL | 0:9d17e4342598 | 196 | if (ret == 0 && mp_exptmod(&key->y, &u2, &key->p, &u2) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 197 | ret = MP_EXPTMOD_E; |
wolfSSL | 0:9d17e4342598 | 198 | |
wolfSSL | 0:9d17e4342598 | 199 | if (ret == 0 && mp_mulmod(&u1, &u2, &key->p, &v) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 200 | ret = MP_MULMOD_E; |
wolfSSL | 0:9d17e4342598 | 201 | |
wolfSSL | 0:9d17e4342598 | 202 | if (ret == 0 && mp_mod(&v, &key->q, &v) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 203 | ret = MP_MULMOD_E; |
wolfSSL | 0:9d17e4342598 | 204 | |
wolfSSL | 0:9d17e4342598 | 205 | /* do they match */ |
wolfSSL | 0:9d17e4342598 | 206 | if (ret == 0 && mp_cmp(&r, &v) == MP_EQ) |
wolfSSL | 0:9d17e4342598 | 207 | *answer = 1; |
wolfSSL | 0:9d17e4342598 | 208 | else |
wolfSSL | 0:9d17e4342598 | 209 | *answer = 0; |
wolfSSL | 0:9d17e4342598 | 210 | |
wolfSSL | 0:9d17e4342598 | 211 | mp_clear(&s); |
wolfSSL | 0:9d17e4342598 | 212 | mp_clear(&r); |
wolfSSL | 0:9d17e4342598 | 213 | mp_clear(&u1); |
wolfSSL | 0:9d17e4342598 | 214 | mp_clear(&u2); |
wolfSSL | 0:9d17e4342598 | 215 | mp_clear(&w); |
wolfSSL | 0:9d17e4342598 | 216 | mp_clear(&v); |
wolfSSL | 0:9d17e4342598 | 217 | |
wolfSSL | 0:9d17e4342598 | 218 | return ret; |
wolfSSL | 0:9d17e4342598 | 219 | } |
wolfSSL | 0:9d17e4342598 | 220 | |
wolfSSL | 0:9d17e4342598 | 221 | |
wolfSSL | 0:9d17e4342598 | 222 | #endif /* NO_DSA */ |
wolfSSL | 0:9d17e4342598 | 223 |