This is a port of cyaSSL 2.7.0.
Dependents: CyaSSL_DTLS_Cellular CyaSSL_DTLS_Ethernet
ctaocrypt/src/dh.c@1:c0ce1562443a, 2013-09-05 (annotated)
- Committer:
- ashleymills
- Date:
- Thu Sep 05 15:55:50 2013 +0000
- Revision:
- 1:c0ce1562443a
- Parent:
- 0:714293de3836
Nothing;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ashleymills | 0:714293de3836 | 1 | /* dh.c |
ashleymills | 0:714293de3836 | 2 | * |
ashleymills | 0:714293de3836 | 3 | * Copyright (C) 2006-2013 wolfSSL Inc. |
ashleymills | 0:714293de3836 | 4 | * |
ashleymills | 0:714293de3836 | 5 | * This file is part of CyaSSL. |
ashleymills | 0:714293de3836 | 6 | * |
ashleymills | 0:714293de3836 | 7 | * CyaSSL is free software; you can redistribute it and/or modify |
ashleymills | 0:714293de3836 | 8 | * it under the terms of the GNU General Public License as published by |
ashleymills | 0:714293de3836 | 9 | * the Free Software Foundation; either version 2 of the License, or |
ashleymills | 0:714293de3836 | 10 | * (at your option) any later version. |
ashleymills | 0:714293de3836 | 11 | * |
ashleymills | 0:714293de3836 | 12 | * CyaSSL is distributed in the hope that it will be useful, |
ashleymills | 0:714293de3836 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
ashleymills | 0:714293de3836 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ashleymills | 0:714293de3836 | 15 | * GNU General Public License for more details. |
ashleymills | 0:714293de3836 | 16 | * |
ashleymills | 0:714293de3836 | 17 | * You should have received a copy of the GNU General Public License |
ashleymills | 0:714293de3836 | 18 | * along with this program; if not, write to the Free Software |
ashleymills | 0:714293de3836 | 19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
ashleymills | 0:714293de3836 | 20 | */ |
ashleymills | 0:714293de3836 | 21 | |
ashleymills | 0:714293de3836 | 22 | #ifdef HAVE_CONFIG_H |
ashleymills | 0:714293de3836 | 23 | #include <config.h> |
ashleymills | 0:714293de3836 | 24 | #endif |
ashleymills | 0:714293de3836 | 25 | |
ashleymills | 0:714293de3836 | 26 | #include <cyassl/ctaocrypt/settings.h> |
ashleymills | 0:714293de3836 | 27 | |
ashleymills | 0:714293de3836 | 28 | #ifndef NO_DH |
ashleymills | 0:714293de3836 | 29 | |
ashleymills | 0:714293de3836 | 30 | #include <cyassl/ctaocrypt/dh.h> |
ashleymills | 0:714293de3836 | 31 | #include <cyassl/ctaocrypt/ctaoerror2.h> |
ashleymills | 0:714293de3836 | 32 | |
ashleymills | 0:714293de3836 | 33 | #ifndef USER_MATH_LIB |
ashleymills | 0:714293de3836 | 34 | #include <math.h> |
ashleymills | 0:714293de3836 | 35 | #define XPOW(x,y) pow((x),(y)) |
ashleymills | 0:714293de3836 | 36 | #define XLOG(x) log((x)) |
ashleymills | 0:714293de3836 | 37 | #else |
ashleymills | 0:714293de3836 | 38 | /* user's own math lib */ |
ashleymills | 0:714293de3836 | 39 | #endif |
ashleymills | 0:714293de3836 | 40 | |
ashleymills | 0:714293de3836 | 41 | |
ashleymills | 0:714293de3836 | 42 | #ifndef min |
ashleymills | 0:714293de3836 | 43 | |
ashleymills | 0:714293de3836 | 44 | static INLINE word32 min(word32 a, word32 b) |
ashleymills | 0:714293de3836 | 45 | { |
ashleymills | 0:714293de3836 | 46 | return a > b ? b : a; |
ashleymills | 0:714293de3836 | 47 | } |
ashleymills | 0:714293de3836 | 48 | |
ashleymills | 0:714293de3836 | 49 | #endif /* min */ |
ashleymills | 0:714293de3836 | 50 | |
ashleymills | 0:714293de3836 | 51 | |
ashleymills | 0:714293de3836 | 52 | void InitDhKey(DhKey* key) |
ashleymills | 0:714293de3836 | 53 | { |
ashleymills | 0:714293de3836 | 54 | (void)key; |
ashleymills | 0:714293de3836 | 55 | /* TomsFastMath doesn't use memory allocation */ |
ashleymills | 0:714293de3836 | 56 | #ifndef USE_FAST_MATH |
ashleymills | 0:714293de3836 | 57 | key->p.dp = 0; |
ashleymills | 0:714293de3836 | 58 | key->g.dp = 0; |
ashleymills | 0:714293de3836 | 59 | #endif |
ashleymills | 0:714293de3836 | 60 | } |
ashleymills | 0:714293de3836 | 61 | |
ashleymills | 0:714293de3836 | 62 | |
ashleymills | 0:714293de3836 | 63 | void FreeDhKey(DhKey* key) |
ashleymills | 0:714293de3836 | 64 | { |
ashleymills | 0:714293de3836 | 65 | (void)key; |
ashleymills | 0:714293de3836 | 66 | /* TomsFastMath doesn't use memory allocation */ |
ashleymills | 0:714293de3836 | 67 | #ifndef USE_FAST_MATH |
ashleymills | 0:714293de3836 | 68 | mp_clear(&key->p); |
ashleymills | 0:714293de3836 | 69 | mp_clear(&key->g); |
ashleymills | 0:714293de3836 | 70 | #endif |
ashleymills | 0:714293de3836 | 71 | } |
ashleymills | 0:714293de3836 | 72 | |
ashleymills | 0:714293de3836 | 73 | |
ashleymills | 0:714293de3836 | 74 | static word32 DiscreteLogWorkFactor(word32 n) |
ashleymills | 0:714293de3836 | 75 | { |
ashleymills | 0:714293de3836 | 76 | /* assuming discrete log takes about the same time as factoring */ |
ashleymills | 0:714293de3836 | 77 | if (n<5) |
ashleymills | 0:714293de3836 | 78 | return 0; |
ashleymills | 0:714293de3836 | 79 | else |
ashleymills | 0:714293de3836 | 80 | return (word32)(2.4 * XPOW((double)n, 1.0/3.0) * |
ashleymills | 0:714293de3836 | 81 | XPOW(XLOG((double)n), 2.0/3.0) - 5); |
ashleymills | 0:714293de3836 | 82 | } |
ashleymills | 0:714293de3836 | 83 | |
ashleymills | 0:714293de3836 | 84 | |
ashleymills | 0:714293de3836 | 85 | static void GeneratePrivate(DhKey* key, RNG* rng, byte* priv, word32* privSz) |
ashleymills | 0:714293de3836 | 86 | { |
ashleymills | 0:714293de3836 | 87 | word32 sz = mp_unsigned_bin_size(&key->p); |
ashleymills | 0:714293de3836 | 88 | sz = min(sz, 2 * DiscreteLogWorkFactor(sz * CYASSL_BIT_SIZE) / |
ashleymills | 0:714293de3836 | 89 | CYASSL_BIT_SIZE + 1); |
ashleymills | 0:714293de3836 | 90 | RNG_GenerateBlock(rng, priv, sz); |
ashleymills | 0:714293de3836 | 91 | priv[0] |= 0x0C; |
ashleymills | 0:714293de3836 | 92 | |
ashleymills | 0:714293de3836 | 93 | *privSz = sz; |
ashleymills | 0:714293de3836 | 94 | } |
ashleymills | 0:714293de3836 | 95 | |
ashleymills | 0:714293de3836 | 96 | |
ashleymills | 0:714293de3836 | 97 | static int GeneratePublic(DhKey* key, const byte* priv, word32 privSz, |
ashleymills | 0:714293de3836 | 98 | byte* pub, word32* pubSz) |
ashleymills | 0:714293de3836 | 99 | { |
ashleymills | 0:714293de3836 | 100 | int ret = 0; |
ashleymills | 0:714293de3836 | 101 | |
ashleymills | 0:714293de3836 | 102 | mp_int x; |
ashleymills | 0:714293de3836 | 103 | mp_int y; |
ashleymills | 0:714293de3836 | 104 | |
ashleymills | 0:714293de3836 | 105 | if (mp_init_multi(&x, &y, 0, 0, 0, 0) != MP_OKAY) |
ashleymills | 0:714293de3836 | 106 | return MP_INIT_E; |
ashleymills | 0:714293de3836 | 107 | |
ashleymills | 0:714293de3836 | 108 | if (mp_read_unsigned_bin(&x, priv, privSz) != MP_OKAY) |
ashleymills | 0:714293de3836 | 109 | ret = MP_READ_E; |
ashleymills | 0:714293de3836 | 110 | |
ashleymills | 0:714293de3836 | 111 | if (ret == 0 && mp_exptmod(&key->g, &x, &key->p, &y) != MP_OKAY) |
ashleymills | 0:714293de3836 | 112 | ret = MP_EXPTMOD_E; |
ashleymills | 0:714293de3836 | 113 | |
ashleymills | 0:714293de3836 | 114 | if (ret == 0 && mp_to_unsigned_bin(&y, pub) != MP_OKAY) |
ashleymills | 0:714293de3836 | 115 | ret = MP_TO_E; |
ashleymills | 0:714293de3836 | 116 | |
ashleymills | 0:714293de3836 | 117 | if (ret == 0) |
ashleymills | 0:714293de3836 | 118 | *pubSz = mp_unsigned_bin_size(&y); |
ashleymills | 0:714293de3836 | 119 | |
ashleymills | 0:714293de3836 | 120 | mp_clear(&y); |
ashleymills | 0:714293de3836 | 121 | mp_clear(&x); |
ashleymills | 0:714293de3836 | 122 | |
ashleymills | 0:714293de3836 | 123 | return ret; |
ashleymills | 0:714293de3836 | 124 | } |
ashleymills | 0:714293de3836 | 125 | |
ashleymills | 0:714293de3836 | 126 | |
ashleymills | 0:714293de3836 | 127 | int DhGenerateKeyPair(DhKey* key, RNG* rng, byte* priv, word32* privSz, |
ashleymills | 0:714293de3836 | 128 | byte* pub, word32* pubSz) |
ashleymills | 0:714293de3836 | 129 | { |
ashleymills | 0:714293de3836 | 130 | GeneratePrivate(key, rng, priv, privSz); |
ashleymills | 0:714293de3836 | 131 | return GeneratePublic(key, priv, *privSz, pub, pubSz); |
ashleymills | 0:714293de3836 | 132 | |
ashleymills | 0:714293de3836 | 133 | } |
ashleymills | 0:714293de3836 | 134 | |
ashleymills | 0:714293de3836 | 135 | int DhAgree(DhKey* key, byte* agree, word32* agreeSz, const byte* priv, |
ashleymills | 0:714293de3836 | 136 | word32 privSz, const byte* otherPub, word32 pubSz) |
ashleymills | 0:714293de3836 | 137 | { |
ashleymills | 0:714293de3836 | 138 | int ret = 0; |
ashleymills | 0:714293de3836 | 139 | |
ashleymills | 0:714293de3836 | 140 | mp_int x; |
ashleymills | 0:714293de3836 | 141 | mp_int y; |
ashleymills | 0:714293de3836 | 142 | mp_int z; |
ashleymills | 0:714293de3836 | 143 | |
ashleymills | 0:714293de3836 | 144 | if (mp_init_multi(&x, &y, &z, 0, 0, 0) != MP_OKAY) |
ashleymills | 0:714293de3836 | 145 | return MP_INIT_E; |
ashleymills | 0:714293de3836 | 146 | |
ashleymills | 0:714293de3836 | 147 | if (mp_read_unsigned_bin(&x, priv, privSz) != MP_OKAY) |
ashleymills | 0:714293de3836 | 148 | ret = MP_READ_E; |
ashleymills | 0:714293de3836 | 149 | |
ashleymills | 0:714293de3836 | 150 | if (ret == 0 && mp_read_unsigned_bin(&y, otherPub, pubSz) != MP_OKAY) |
ashleymills | 0:714293de3836 | 151 | ret = MP_READ_E; |
ashleymills | 0:714293de3836 | 152 | |
ashleymills | 0:714293de3836 | 153 | if (ret == 0 && mp_exptmod(&y, &x, &key->p, &z) != MP_OKAY) |
ashleymills | 0:714293de3836 | 154 | ret = MP_EXPTMOD_E; |
ashleymills | 0:714293de3836 | 155 | |
ashleymills | 0:714293de3836 | 156 | if (ret == 0 && mp_to_unsigned_bin(&z, agree) != MP_OKAY) |
ashleymills | 0:714293de3836 | 157 | ret = MP_TO_E; |
ashleymills | 0:714293de3836 | 158 | |
ashleymills | 0:714293de3836 | 159 | if (ret == 0) |
ashleymills | 0:714293de3836 | 160 | *agreeSz = mp_unsigned_bin_size(&z); |
ashleymills | 0:714293de3836 | 161 | |
ashleymills | 0:714293de3836 | 162 | mp_clear(&z); |
ashleymills | 0:714293de3836 | 163 | mp_clear(&y); |
ashleymills | 0:714293de3836 | 164 | mp_clear(&x); |
ashleymills | 0:714293de3836 | 165 | |
ashleymills | 0:714293de3836 | 166 | return ret; |
ashleymills | 0:714293de3836 | 167 | } |
ashleymills | 0:714293de3836 | 168 | |
ashleymills | 0:714293de3836 | 169 | |
ashleymills | 0:714293de3836 | 170 | #endif /* NO_DH */ |
ashleymills | 0:714293de3836 | 171 |