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