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 /* dh.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_DH
ashleymills 0:e979170e02e7 27
ashleymills 0:e979170e02e7 28 #include <cyassl/ctaocrypt/dh.h>
ashleymills 0:e979170e02e7 29 #include <cyassl/ctaocrypt/error.h>
ashleymills 0:e979170e02e7 30
ashleymills 0:e979170e02e7 31 #ifndef USER_MATH_LIB
ashleymills 0:e979170e02e7 32 #include <math.h>
ashleymills 0:e979170e02e7 33 #define XPOW(x,y) pow((x),(y))
ashleymills 0:e979170e02e7 34 #define XLOG(x) log((x))
ashleymills 0:e979170e02e7 35 #else
ashleymills 0:e979170e02e7 36 /* user's own math lib */
ashleymills 0:e979170e02e7 37 #endif
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 InitDhKey(DhKey* key)
ashleymills 0:e979170e02e7 51 {
ashleymills 0:e979170e02e7 52 (void)key;
ashleymills 0:e979170e02e7 53 /* TomsFastMath doesn't use memory allocation */
ashleymills 0:e979170e02e7 54 #ifndef USE_FAST_MATH
ashleymills 0:e979170e02e7 55 key->p.dp = 0;
ashleymills 0:e979170e02e7 56 key->g.dp = 0;
ashleymills 0:e979170e02e7 57 #endif
ashleymills 0:e979170e02e7 58 }
ashleymills 0:e979170e02e7 59
ashleymills 0:e979170e02e7 60
ashleymills 0:e979170e02e7 61 void FreeDhKey(DhKey* key)
ashleymills 0:e979170e02e7 62 {
ashleymills 0:e979170e02e7 63 (void)key;
ashleymills 0:e979170e02e7 64 /* TomsFastMath doesn't use memory allocation */
ashleymills 0:e979170e02e7 65 #ifndef USE_FAST_MATH
ashleymills 0:e979170e02e7 66 mp_clear(&key->p);
ashleymills 0:e979170e02e7 67 mp_clear(&key->g);
ashleymills 0:e979170e02e7 68 #endif
ashleymills 0:e979170e02e7 69 }
ashleymills 0:e979170e02e7 70
ashleymills 0:e979170e02e7 71
ashleymills 0:e979170e02e7 72 static word32 DiscreteLogWorkFactor(word32 n)
ashleymills 0:e979170e02e7 73 {
ashleymills 0:e979170e02e7 74 /* assuming discrete log takes about the same time as factoring */
ashleymills 0:e979170e02e7 75 if (n<5)
ashleymills 0:e979170e02e7 76 return 0;
ashleymills 0:e979170e02e7 77 else
ashleymills 0:e979170e02e7 78 return (word32)(2.4 * XPOW((double)n, 1.0/3.0) *
ashleymills 0:e979170e02e7 79 XPOW(XLOG((double)n), 2.0/3.0) - 5);
ashleymills 0:e979170e02e7 80 }
ashleymills 0:e979170e02e7 81
ashleymills 0:e979170e02e7 82
ashleymills 0:e979170e02e7 83 static void GeneratePrivate(DhKey* key, RNG* rng, byte* priv, word32* privSz)
ashleymills 0:e979170e02e7 84 {
ashleymills 0:e979170e02e7 85 word32 sz = mp_unsigned_bin_size(&key->p);
ashleymills 0:e979170e02e7 86 sz = min(sz, 2 * DiscreteLogWorkFactor(sz * BIT_SIZE) / BIT_SIZE + 1);
ashleymills 0:e979170e02e7 87
ashleymills 0:e979170e02e7 88 RNG_GenerateBlock(rng, priv, sz);
ashleymills 0:e979170e02e7 89 priv[0] |= 0x0C;
ashleymills 0:e979170e02e7 90
ashleymills 0:e979170e02e7 91 *privSz = sz;
ashleymills 0:e979170e02e7 92 }
ashleymills 0:e979170e02e7 93
ashleymills 0:e979170e02e7 94
ashleymills 0:e979170e02e7 95 static int GeneratePublic(DhKey* key, const byte* priv, word32 privSz,
ashleymills 0:e979170e02e7 96 byte* pub, word32* pubSz)
ashleymills 0:e979170e02e7 97 {
ashleymills 0:e979170e02e7 98 int ret = 0;
ashleymills 0:e979170e02e7 99
ashleymills 0:e979170e02e7 100 mp_int x;
ashleymills 0:e979170e02e7 101 mp_int y;
ashleymills 0:e979170e02e7 102
ashleymills 0:e979170e02e7 103 if (mp_init_multi(&x, &y, 0, 0, 0, 0) != MP_OKAY)
ashleymills 0:e979170e02e7 104 return MP_INIT_E;
ashleymills 0:e979170e02e7 105
ashleymills 0:e979170e02e7 106 if (mp_read_unsigned_bin(&x, priv, privSz) != MP_OKAY)
ashleymills 0:e979170e02e7 107 ret = MP_READ_E;
ashleymills 0:e979170e02e7 108
ashleymills 0:e979170e02e7 109 if (ret == 0 && mp_exptmod(&key->g, &x, &key->p, &y) != MP_OKAY)
ashleymills 0:e979170e02e7 110 ret = MP_EXPTMOD_E;
ashleymills 0:e979170e02e7 111
ashleymills 0:e979170e02e7 112 if (ret == 0 && mp_to_unsigned_bin(&y, pub) != MP_OKAY)
ashleymills 0:e979170e02e7 113 ret = MP_TO_E;
ashleymills 0:e979170e02e7 114
ashleymills 0:e979170e02e7 115 if (ret == 0)
ashleymills 0:e979170e02e7 116 *pubSz = mp_unsigned_bin_size(&y);
ashleymills 0:e979170e02e7 117
ashleymills 0:e979170e02e7 118 mp_clear(&y);
ashleymills 0:e979170e02e7 119 mp_clear(&x);
ashleymills 0:e979170e02e7 120
ashleymills 0:e979170e02e7 121 return ret;
ashleymills 0:e979170e02e7 122 }
ashleymills 0:e979170e02e7 123
ashleymills 0:e979170e02e7 124
ashleymills 0:e979170e02e7 125 int DhGenerateKeyPair(DhKey* key, RNG* rng, byte* priv, word32* privSz,
ashleymills 0:e979170e02e7 126 byte* pub, word32* pubSz)
ashleymills 0:e979170e02e7 127 {
ashleymills 0:e979170e02e7 128 GeneratePrivate(key, rng, priv, privSz);
ashleymills 0:e979170e02e7 129 return GeneratePublic(key, priv, *privSz, pub, pubSz);
ashleymills 0:e979170e02e7 130
ashleymills 0:e979170e02e7 131 }
ashleymills 0:e979170e02e7 132
ashleymills 0:e979170e02e7 133 int DhAgree(DhKey* key, byte* agree, word32* agreeSz, const byte* priv,
ashleymills 0:e979170e02e7 134 word32 privSz, const byte* otherPub, word32 pubSz)
ashleymills 0:e979170e02e7 135 {
ashleymills 0:e979170e02e7 136 int ret = 0;
ashleymills 0:e979170e02e7 137
ashleymills 0:e979170e02e7 138 mp_int x;
ashleymills 0:e979170e02e7 139 mp_int y;
ashleymills 0:e979170e02e7 140 mp_int z;
ashleymills 0:e979170e02e7 141
ashleymills 0:e979170e02e7 142 if (mp_init_multi(&x, &y, &z, 0, 0, 0) != MP_OKAY)
ashleymills 0:e979170e02e7 143 return MP_INIT_E;
ashleymills 0:e979170e02e7 144
ashleymills 0:e979170e02e7 145 if (mp_read_unsigned_bin(&x, priv, privSz) != MP_OKAY)
ashleymills 0:e979170e02e7 146 ret = MP_READ_E;
ashleymills 0:e979170e02e7 147
ashleymills 0:e979170e02e7 148 if (ret == 0 && mp_read_unsigned_bin(&y, otherPub, pubSz) != MP_OKAY)
ashleymills 0:e979170e02e7 149 ret = MP_READ_E;
ashleymills 0:e979170e02e7 150
ashleymills 0:e979170e02e7 151 if (ret == 0 && mp_exptmod(&y, &x, &key->p, &z) != MP_OKAY)
ashleymills 0:e979170e02e7 152 ret = MP_EXPTMOD_E;
ashleymills 0:e979170e02e7 153
ashleymills 0:e979170e02e7 154 if (ret == 0 && mp_to_unsigned_bin(&z, agree) != MP_OKAY)
ashleymills 0:e979170e02e7 155 ret = MP_TO_E;
ashleymills 0:e979170e02e7 156
ashleymills 0:e979170e02e7 157 if (ret == 0)
ashleymills 0:e979170e02e7 158 *agreeSz = mp_unsigned_bin_size(&z);
ashleymills 0:e979170e02e7 159
ashleymills 0:e979170e02e7 160 mp_clear(&z);
ashleymills 0:e979170e02e7 161 mp_clear(&y);
ashleymills 0:e979170e02e7 162 mp_clear(&x);
ashleymills 0:e979170e02e7 163
ashleymills 0:e979170e02e7 164 return ret;
ashleymills 0:e979170e02e7 165 }
ashleymills 0:e979170e02e7 166
ashleymills 0:e979170e02e7 167
ashleymills 0:e979170e02e7 168 #endif /* NO_DH */
ashleymills 0:e979170e02e7 169