cyassl re-port with cellular comms, PSK test

Dependencies:   VodafoneUSBModem_bleedingedge2 mbed-rtos mbed-src

Committer:
ashleymills
Date:
Fri Apr 26 16:59:36 2013 +0000
Revision:
1:b211d97b0068
Parent:
0:e979170e02e7
nothing

Who changed what in which revision?

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