SSL/TLS Library
CyaSSL is SSL/TLS library for embedded systems.
ctaocrypt/src/rsa.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 | /* rsa.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 | |
wolfSSL | 0:9d17e4342598 | 23 | #ifdef HAVE_CONFIG_H |
wolfSSL | 0:9d17e4342598 | 24 | #include <config.h> |
wolfSSL | 0:9d17e4342598 | 25 | #endif |
wolfSSL | 0:9d17e4342598 | 26 | |
wolfSSL | 0:9d17e4342598 | 27 | #include <cyassl/ctaocrypt/settings.h> |
wolfSSL | 0:9d17e4342598 | 28 | |
wolfSSL | 0:9d17e4342598 | 29 | #ifndef NO_RSA |
wolfSSL | 0:9d17e4342598 | 30 | |
wolfSSL | 0:9d17e4342598 | 31 | #ifdef HAVE_FIPS |
wolfSSL | 0:9d17e4342598 | 32 | /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */ |
wolfSSL | 0:9d17e4342598 | 33 | #define FIPS_NO_WRAPPERS |
wolfSSL | 0:9d17e4342598 | 34 | #endif |
wolfSSL | 0:9d17e4342598 | 35 | |
wolfSSL | 0:9d17e4342598 | 36 | #include <cyassl/ctaocrypt/rsa.h> |
wolfSSL | 0:9d17e4342598 | 37 | #include <cyassl/ctaocrypt/random.h> |
wolfSSL | 0:9d17e4342598 | 38 | #include <cyassl/ctaocrypt/error-crypt.h> |
wolfSSL | 0:9d17e4342598 | 39 | #include <cyassl/ctaocrypt/logging.h> |
wolfSSL | 0:9d17e4342598 | 40 | |
wolfSSL | 0:9d17e4342598 | 41 | #ifdef SHOW_GEN |
wolfSSL | 0:9d17e4342598 | 42 | #ifdef FREESCALE_MQX |
wolfSSL | 0:9d17e4342598 | 43 | #include <fio.h> |
wolfSSL | 0:9d17e4342598 | 44 | #else |
wolfSSL | 0:9d17e4342598 | 45 | #include <stdio.h> |
wolfSSL | 0:9d17e4342598 | 46 | #endif |
wolfSSL | 0:9d17e4342598 | 47 | #endif |
wolfSSL | 0:9d17e4342598 | 48 | |
wolfSSL | 0:9d17e4342598 | 49 | #ifdef HAVE_CAVIUM |
wolfSSL | 0:9d17e4342598 | 50 | static int InitCaviumRsaKey(RsaKey* key, void* heap); |
wolfSSL | 0:9d17e4342598 | 51 | static int FreeCaviumRsaKey(RsaKey* key); |
wolfSSL | 0:9d17e4342598 | 52 | static int CaviumRsaPublicEncrypt(const byte* in, word32 inLen, byte* out, |
wolfSSL | 0:9d17e4342598 | 53 | word32 outLen, RsaKey* key); |
wolfSSL | 0:9d17e4342598 | 54 | static int CaviumRsaPrivateDecrypt(const byte* in, word32 inLen, byte* out, |
wolfSSL | 0:9d17e4342598 | 55 | word32 outLen, RsaKey* key); |
wolfSSL | 0:9d17e4342598 | 56 | static int CaviumRsaSSL_Sign(const byte* in, word32 inLen, byte* out, |
wolfSSL | 0:9d17e4342598 | 57 | word32 outLen, RsaKey* key); |
wolfSSL | 0:9d17e4342598 | 58 | static int CaviumRsaSSL_Verify(const byte* in, word32 inLen, byte* out, |
wolfSSL | 0:9d17e4342598 | 59 | word32 outLen, RsaKey* key); |
wolfSSL | 0:9d17e4342598 | 60 | #endif |
wolfSSL | 0:9d17e4342598 | 61 | |
wolfSSL | 0:9d17e4342598 | 62 | enum { |
wolfSSL | 0:9d17e4342598 | 63 | RSA_PUBLIC_ENCRYPT = 0, |
wolfSSL | 0:9d17e4342598 | 64 | RSA_PUBLIC_DECRYPT = 1, |
wolfSSL | 0:9d17e4342598 | 65 | RSA_PRIVATE_ENCRYPT = 2, |
wolfSSL | 0:9d17e4342598 | 66 | RSA_PRIVATE_DECRYPT = 3, |
wolfSSL | 0:9d17e4342598 | 67 | |
wolfSSL | 0:9d17e4342598 | 68 | RSA_BLOCK_TYPE_1 = 1, |
wolfSSL | 0:9d17e4342598 | 69 | RSA_BLOCK_TYPE_2 = 2, |
wolfSSL | 0:9d17e4342598 | 70 | |
wolfSSL | 0:9d17e4342598 | 71 | RSA_MIN_SIZE = 512, |
wolfSSL | 0:9d17e4342598 | 72 | RSA_MAX_SIZE = 4096, |
wolfSSL | 0:9d17e4342598 | 73 | |
wolfSSL | 0:9d17e4342598 | 74 | RSA_MIN_PAD_SZ = 11 /* seperator + 0 + pad value + 8 pads */ |
wolfSSL | 0:9d17e4342598 | 75 | }; |
wolfSSL | 0:9d17e4342598 | 76 | |
wolfSSL | 0:9d17e4342598 | 77 | |
wolfSSL | 0:9d17e4342598 | 78 | int InitRsaKey(RsaKey* key, void* heap) |
wolfSSL | 0:9d17e4342598 | 79 | { |
wolfSSL | 0:9d17e4342598 | 80 | #ifdef HAVE_CAVIUM |
wolfSSL | 0:9d17e4342598 | 81 | if (key->magic == CYASSL_RSA_CAVIUM_MAGIC) |
wolfSSL | 0:9d17e4342598 | 82 | return InitCaviumRsaKey(key, heap); |
wolfSSL | 0:9d17e4342598 | 83 | #endif |
wolfSSL | 0:9d17e4342598 | 84 | |
wolfSSL | 0:9d17e4342598 | 85 | key->type = -1; /* haven't decided yet */ |
wolfSSL | 0:9d17e4342598 | 86 | key->heap = heap; |
wolfSSL | 0:9d17e4342598 | 87 | |
wolfSSL | 0:9d17e4342598 | 88 | /* TomsFastMath doesn't use memory allocation */ |
wolfSSL | 0:9d17e4342598 | 89 | #ifndef USE_FAST_MATH |
wolfSSL | 0:9d17e4342598 | 90 | key->n.dp = key->e.dp = 0; /* public alloc parts */ |
wolfSSL | 0:9d17e4342598 | 91 | |
wolfSSL | 0:9d17e4342598 | 92 | key->d.dp = key->p.dp = 0; /* private alloc parts */ |
wolfSSL | 0:9d17e4342598 | 93 | key->q.dp = key->dP.dp = 0; |
wolfSSL | 0:9d17e4342598 | 94 | key->u.dp = key->dQ.dp = 0; |
wolfSSL | 0:9d17e4342598 | 95 | #endif |
wolfSSL | 0:9d17e4342598 | 96 | |
wolfSSL | 0:9d17e4342598 | 97 | return 0; |
wolfSSL | 0:9d17e4342598 | 98 | } |
wolfSSL | 0:9d17e4342598 | 99 | |
wolfSSL | 0:9d17e4342598 | 100 | |
wolfSSL | 0:9d17e4342598 | 101 | int FreeRsaKey(RsaKey* key) |
wolfSSL | 0:9d17e4342598 | 102 | { |
wolfSSL | 0:9d17e4342598 | 103 | (void)key; |
wolfSSL | 0:9d17e4342598 | 104 | |
wolfSSL | 0:9d17e4342598 | 105 | #ifdef HAVE_CAVIUM |
wolfSSL | 0:9d17e4342598 | 106 | if (key->magic == CYASSL_RSA_CAVIUM_MAGIC) |
wolfSSL | 0:9d17e4342598 | 107 | return FreeCaviumRsaKey(key); |
wolfSSL | 0:9d17e4342598 | 108 | #endif |
wolfSSL | 0:9d17e4342598 | 109 | |
wolfSSL | 0:9d17e4342598 | 110 | /* TomsFastMath doesn't use memory allocation */ |
wolfSSL | 0:9d17e4342598 | 111 | #ifndef USE_FAST_MATH |
wolfSSL | 0:9d17e4342598 | 112 | if (key->type == RSA_PRIVATE) { |
wolfSSL | 0:9d17e4342598 | 113 | mp_clear(&key->u); |
wolfSSL | 0:9d17e4342598 | 114 | mp_clear(&key->dQ); |
wolfSSL | 0:9d17e4342598 | 115 | mp_clear(&key->dP); |
wolfSSL | 0:9d17e4342598 | 116 | mp_clear(&key->q); |
wolfSSL | 0:9d17e4342598 | 117 | mp_clear(&key->p); |
wolfSSL | 0:9d17e4342598 | 118 | mp_clear(&key->d); |
wolfSSL | 0:9d17e4342598 | 119 | } |
wolfSSL | 0:9d17e4342598 | 120 | mp_clear(&key->e); |
wolfSSL | 0:9d17e4342598 | 121 | mp_clear(&key->n); |
wolfSSL | 0:9d17e4342598 | 122 | #endif |
wolfSSL | 0:9d17e4342598 | 123 | |
wolfSSL | 0:9d17e4342598 | 124 | return 0; |
wolfSSL | 0:9d17e4342598 | 125 | } |
wolfSSL | 0:9d17e4342598 | 126 | |
wolfSSL | 0:9d17e4342598 | 127 | static void RsaPad(const byte* input, word32 inputLen, byte* pkcsBlock, |
wolfSSL | 0:9d17e4342598 | 128 | word32 pkcsBlockLen, byte padValue, RNG* rng) |
wolfSSL | 0:9d17e4342598 | 129 | { |
wolfSSL | 0:9d17e4342598 | 130 | if (inputLen == 0) return; |
wolfSSL | 0:9d17e4342598 | 131 | |
wolfSSL | 0:9d17e4342598 | 132 | pkcsBlock[0] = 0x0; /* set first byte to zero and advance */ |
wolfSSL | 0:9d17e4342598 | 133 | pkcsBlock++; pkcsBlockLen--; |
wolfSSL | 0:9d17e4342598 | 134 | pkcsBlock[0] = padValue; /* insert padValue */ |
wolfSSL | 0:9d17e4342598 | 135 | |
wolfSSL | 0:9d17e4342598 | 136 | if (padValue == RSA_BLOCK_TYPE_1) |
wolfSSL | 0:9d17e4342598 | 137 | /* pad with 0xff bytes */ |
wolfSSL | 0:9d17e4342598 | 138 | XMEMSET(&pkcsBlock[1], 0xFF, pkcsBlockLen - inputLen - 2); |
wolfSSL | 0:9d17e4342598 | 139 | else { |
wolfSSL | 0:9d17e4342598 | 140 | /* pad with non-zero random bytes */ |
wolfSSL | 0:9d17e4342598 | 141 | word32 padLen = pkcsBlockLen - inputLen - 1, i; |
wolfSSL | 0:9d17e4342598 | 142 | RNG_GenerateBlock(rng, &pkcsBlock[1], padLen); |
wolfSSL | 0:9d17e4342598 | 143 | |
wolfSSL | 0:9d17e4342598 | 144 | /* remove zeros */ |
wolfSSL | 0:9d17e4342598 | 145 | for (i = 1; i < padLen; i++) |
wolfSSL | 0:9d17e4342598 | 146 | if (pkcsBlock[i] == 0) pkcsBlock[i] = 0x01; |
wolfSSL | 0:9d17e4342598 | 147 | } |
wolfSSL | 0:9d17e4342598 | 148 | |
wolfSSL | 0:9d17e4342598 | 149 | pkcsBlock[pkcsBlockLen-inputLen-1] = 0; /* separator */ |
wolfSSL | 0:9d17e4342598 | 150 | XMEMCPY(pkcsBlock+pkcsBlockLen-inputLen, input, inputLen); |
wolfSSL | 0:9d17e4342598 | 151 | } |
wolfSSL | 0:9d17e4342598 | 152 | |
wolfSSL | 0:9d17e4342598 | 153 | |
wolfSSL | 0:9d17e4342598 | 154 | static word32 RsaUnPad(const byte *pkcsBlock, unsigned int pkcsBlockLen, |
wolfSSL | 0:9d17e4342598 | 155 | byte **output, byte padValue) |
wolfSSL | 0:9d17e4342598 | 156 | { |
wolfSSL | 0:9d17e4342598 | 157 | word32 maxOutputLen = (pkcsBlockLen > 10) ? (pkcsBlockLen - 10) : 0, |
wolfSSL | 0:9d17e4342598 | 158 | invalid = 0, |
wolfSSL | 0:9d17e4342598 | 159 | i = 1, |
wolfSSL | 0:9d17e4342598 | 160 | outputLen; |
wolfSSL | 0:9d17e4342598 | 161 | |
wolfSSL | 0:9d17e4342598 | 162 | if (pkcsBlock[0] != 0x0) /* skip past zero */ |
wolfSSL | 0:9d17e4342598 | 163 | invalid = 1; |
wolfSSL | 0:9d17e4342598 | 164 | pkcsBlock++; pkcsBlockLen--; |
wolfSSL | 0:9d17e4342598 | 165 | |
wolfSSL | 0:9d17e4342598 | 166 | /* Require block type padValue */ |
wolfSSL | 0:9d17e4342598 | 167 | invalid = (pkcsBlock[0] != padValue) || invalid; |
wolfSSL | 0:9d17e4342598 | 168 | |
wolfSSL | 0:9d17e4342598 | 169 | /* skip past the padding until we find the separator */ |
wolfSSL | 0:9d17e4342598 | 170 | while (i<pkcsBlockLen && pkcsBlock[i++]) { /* null body */ |
wolfSSL | 0:9d17e4342598 | 171 | } |
wolfSSL | 0:9d17e4342598 | 172 | if(!(i==pkcsBlockLen || pkcsBlock[i-1]==0)) { |
wolfSSL | 0:9d17e4342598 | 173 | CYASSL_MSG("RsaUnPad error, bad formatting"); |
wolfSSL | 0:9d17e4342598 | 174 | return 0; |
wolfSSL | 0:9d17e4342598 | 175 | } |
wolfSSL | 0:9d17e4342598 | 176 | |
wolfSSL | 0:9d17e4342598 | 177 | outputLen = pkcsBlockLen - i; |
wolfSSL | 0:9d17e4342598 | 178 | invalid = (outputLen > maxOutputLen) || invalid; |
wolfSSL | 0:9d17e4342598 | 179 | |
wolfSSL | 0:9d17e4342598 | 180 | if (invalid) { |
wolfSSL | 0:9d17e4342598 | 181 | CYASSL_MSG("RsaUnPad error, bad formatting"); |
wolfSSL | 0:9d17e4342598 | 182 | return 0; |
wolfSSL | 0:9d17e4342598 | 183 | } |
wolfSSL | 0:9d17e4342598 | 184 | |
wolfSSL | 0:9d17e4342598 | 185 | *output = (byte *)(pkcsBlock + i); |
wolfSSL | 0:9d17e4342598 | 186 | return outputLen; |
wolfSSL | 0:9d17e4342598 | 187 | } |
wolfSSL | 0:9d17e4342598 | 188 | |
wolfSSL | 0:9d17e4342598 | 189 | |
wolfSSL | 0:9d17e4342598 | 190 | static int RsaFunction(const byte* in, word32 inLen, byte* out, word32* outLen, |
wolfSSL | 0:9d17e4342598 | 191 | int type, RsaKey* key) |
wolfSSL | 0:9d17e4342598 | 192 | { |
wolfSSL | 0:9d17e4342598 | 193 | #define ERROR_OUT(x) { ret = x; goto done;} |
wolfSSL | 0:9d17e4342598 | 194 | |
wolfSSL | 0:9d17e4342598 | 195 | mp_int tmp; |
wolfSSL | 0:9d17e4342598 | 196 | int ret = 0; |
wolfSSL | 0:9d17e4342598 | 197 | word32 keyLen, len; |
wolfSSL | 0:9d17e4342598 | 198 | |
wolfSSL | 0:9d17e4342598 | 199 | if (mp_init(&tmp) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 200 | return MP_INIT_E; |
wolfSSL | 0:9d17e4342598 | 201 | |
wolfSSL | 0:9d17e4342598 | 202 | if (mp_read_unsigned_bin(&tmp, (byte*)in, inLen) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 203 | ERROR_OUT(MP_READ_E); |
wolfSSL | 0:9d17e4342598 | 204 | |
wolfSSL | 0:9d17e4342598 | 205 | if (type == RSA_PRIVATE_DECRYPT || type == RSA_PRIVATE_ENCRYPT) { |
wolfSSL | 0:9d17e4342598 | 206 | #ifdef RSA_LOW_MEM /* half as much memory but twice as slow */ |
wolfSSL | 0:9d17e4342598 | 207 | if (mp_exptmod(&tmp, &key->d, &key->n, &tmp) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 208 | ERROR_OUT(MP_EXPTMOD_E); |
wolfSSL | 0:9d17e4342598 | 209 | #else |
wolfSSL | 0:9d17e4342598 | 210 | #define INNER_ERROR_OUT(x) { ret = x; goto inner_done; } |
wolfSSL | 0:9d17e4342598 | 211 | |
wolfSSL | 0:9d17e4342598 | 212 | mp_int tmpa, tmpb; |
wolfSSL | 0:9d17e4342598 | 213 | |
wolfSSL | 0:9d17e4342598 | 214 | if (mp_init(&tmpa) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 215 | ERROR_OUT(MP_INIT_E); |
wolfSSL | 0:9d17e4342598 | 216 | |
wolfSSL | 0:9d17e4342598 | 217 | if (mp_init(&tmpb) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 218 | mp_clear(&tmpa); |
wolfSSL | 0:9d17e4342598 | 219 | ERROR_OUT(MP_INIT_E); |
wolfSSL | 0:9d17e4342598 | 220 | } |
wolfSSL | 0:9d17e4342598 | 221 | |
wolfSSL | 0:9d17e4342598 | 222 | /* tmpa = tmp^dP mod p */ |
wolfSSL | 0:9d17e4342598 | 223 | if (mp_exptmod(&tmp, &key->dP, &key->p, &tmpa) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 224 | INNER_ERROR_OUT(MP_EXPTMOD_E); |
wolfSSL | 0:9d17e4342598 | 225 | |
wolfSSL | 0:9d17e4342598 | 226 | /* tmpb = tmp^dQ mod q */ |
wolfSSL | 0:9d17e4342598 | 227 | if (mp_exptmod(&tmp, &key->dQ, &key->q, &tmpb) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 228 | INNER_ERROR_OUT(MP_EXPTMOD_E); |
wolfSSL | 0:9d17e4342598 | 229 | |
wolfSSL | 0:9d17e4342598 | 230 | /* tmp = (tmpa - tmpb) * qInv (mod p) */ |
wolfSSL | 0:9d17e4342598 | 231 | if (mp_sub(&tmpa, &tmpb, &tmp) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 232 | INNER_ERROR_OUT(MP_SUB_E); |
wolfSSL | 0:9d17e4342598 | 233 | |
wolfSSL | 0:9d17e4342598 | 234 | if (mp_mulmod(&tmp, &key->u, &key->p, &tmp) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 235 | INNER_ERROR_OUT(MP_MULMOD_E); |
wolfSSL | 0:9d17e4342598 | 236 | |
wolfSSL | 0:9d17e4342598 | 237 | /* tmp = tmpb + q * tmp */ |
wolfSSL | 0:9d17e4342598 | 238 | if (mp_mul(&tmp, &key->q, &tmp) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 239 | INNER_ERROR_OUT(MP_MUL_E); |
wolfSSL | 0:9d17e4342598 | 240 | |
wolfSSL | 0:9d17e4342598 | 241 | if (mp_add(&tmp, &tmpb, &tmp) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 242 | INNER_ERROR_OUT(MP_ADD_E); |
wolfSSL | 0:9d17e4342598 | 243 | |
wolfSSL | 0:9d17e4342598 | 244 | inner_done: |
wolfSSL | 0:9d17e4342598 | 245 | mp_clear(&tmpa); |
wolfSSL | 0:9d17e4342598 | 246 | mp_clear(&tmpb); |
wolfSSL | 0:9d17e4342598 | 247 | |
wolfSSL | 0:9d17e4342598 | 248 | if (ret != 0) return ret; |
wolfSSL | 0:9d17e4342598 | 249 | |
wolfSSL | 0:9d17e4342598 | 250 | #endif /* RSA_LOW_MEM */ |
wolfSSL | 0:9d17e4342598 | 251 | } |
wolfSSL | 0:9d17e4342598 | 252 | else if (type == RSA_PUBLIC_ENCRYPT || type == RSA_PUBLIC_DECRYPT) { |
wolfSSL | 0:9d17e4342598 | 253 | if (mp_exptmod(&tmp, &key->e, &key->n, &tmp) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 254 | ERROR_OUT(MP_EXPTMOD_E); |
wolfSSL | 0:9d17e4342598 | 255 | } |
wolfSSL | 0:9d17e4342598 | 256 | else |
wolfSSL | 0:9d17e4342598 | 257 | ERROR_OUT(RSA_WRONG_TYPE_E); |
wolfSSL | 0:9d17e4342598 | 258 | |
wolfSSL | 0:9d17e4342598 | 259 | keyLen = mp_unsigned_bin_size(&key->n); |
wolfSSL | 0:9d17e4342598 | 260 | if (keyLen > *outLen) |
wolfSSL | 0:9d17e4342598 | 261 | ERROR_OUT(RSA_BUFFER_E); |
wolfSSL | 0:9d17e4342598 | 262 | |
wolfSSL | 0:9d17e4342598 | 263 | len = mp_unsigned_bin_size(&tmp); |
wolfSSL | 0:9d17e4342598 | 264 | |
wolfSSL | 0:9d17e4342598 | 265 | /* pad front w/ zeros to match key length */ |
wolfSSL | 0:9d17e4342598 | 266 | while (len < keyLen) { |
wolfSSL | 0:9d17e4342598 | 267 | *out++ = 0x00; |
wolfSSL | 0:9d17e4342598 | 268 | len++; |
wolfSSL | 0:9d17e4342598 | 269 | } |
wolfSSL | 0:9d17e4342598 | 270 | |
wolfSSL | 0:9d17e4342598 | 271 | *outLen = keyLen; |
wolfSSL | 0:9d17e4342598 | 272 | |
wolfSSL | 0:9d17e4342598 | 273 | /* convert */ |
wolfSSL | 0:9d17e4342598 | 274 | if (mp_to_unsigned_bin(&tmp, out) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 275 | ERROR_OUT(MP_TO_E); |
wolfSSL | 0:9d17e4342598 | 276 | |
wolfSSL | 0:9d17e4342598 | 277 | done: |
wolfSSL | 0:9d17e4342598 | 278 | mp_clear(&tmp); |
wolfSSL | 0:9d17e4342598 | 279 | return ret; |
wolfSSL | 0:9d17e4342598 | 280 | } |
wolfSSL | 0:9d17e4342598 | 281 | |
wolfSSL | 0:9d17e4342598 | 282 | |
wolfSSL | 0:9d17e4342598 | 283 | int RsaPublicEncrypt(const byte* in, word32 inLen, byte* out, word32 outLen, |
wolfSSL | 0:9d17e4342598 | 284 | RsaKey* key, RNG* rng) |
wolfSSL | 0:9d17e4342598 | 285 | { |
wolfSSL | 0:9d17e4342598 | 286 | int sz, ret; |
wolfSSL | 0:9d17e4342598 | 287 | |
wolfSSL | 0:9d17e4342598 | 288 | #ifdef HAVE_CAVIUM |
wolfSSL | 0:9d17e4342598 | 289 | if (key->magic == CYASSL_RSA_CAVIUM_MAGIC) |
wolfSSL | 0:9d17e4342598 | 290 | return CaviumRsaPublicEncrypt(in, inLen, out, outLen, key); |
wolfSSL | 0:9d17e4342598 | 291 | #endif |
wolfSSL | 0:9d17e4342598 | 292 | |
wolfSSL | 0:9d17e4342598 | 293 | sz = mp_unsigned_bin_size(&key->n); |
wolfSSL | 0:9d17e4342598 | 294 | if (sz > (int)outLen) |
wolfSSL | 0:9d17e4342598 | 295 | return RSA_BUFFER_E; |
wolfSSL | 0:9d17e4342598 | 296 | |
wolfSSL | 0:9d17e4342598 | 297 | if (inLen > (word32)(sz - RSA_MIN_PAD_SZ)) |
wolfSSL | 0:9d17e4342598 | 298 | return RSA_BUFFER_E; |
wolfSSL | 0:9d17e4342598 | 299 | |
wolfSSL | 0:9d17e4342598 | 300 | RsaPad(in, inLen, out, sz, RSA_BLOCK_TYPE_2, rng); |
wolfSSL | 0:9d17e4342598 | 301 | |
wolfSSL | 0:9d17e4342598 | 302 | if ((ret = RsaFunction(out, sz, out, &outLen, RSA_PUBLIC_ENCRYPT, key)) < 0) |
wolfSSL | 0:9d17e4342598 | 303 | sz = ret; |
wolfSSL | 0:9d17e4342598 | 304 | |
wolfSSL | 0:9d17e4342598 | 305 | return sz; |
wolfSSL | 0:9d17e4342598 | 306 | } |
wolfSSL | 0:9d17e4342598 | 307 | |
wolfSSL | 0:9d17e4342598 | 308 | |
wolfSSL | 0:9d17e4342598 | 309 | int RsaPrivateDecryptInline(byte* in, word32 inLen, byte** out, RsaKey* key) |
wolfSSL | 0:9d17e4342598 | 310 | { |
wolfSSL | 0:9d17e4342598 | 311 | int plainLen, ret; |
wolfSSL | 0:9d17e4342598 | 312 | |
wolfSSL | 0:9d17e4342598 | 313 | #ifdef HAVE_CAVIUM |
wolfSSL | 0:9d17e4342598 | 314 | if (key->magic == CYASSL_RSA_CAVIUM_MAGIC) { |
wolfSSL | 0:9d17e4342598 | 315 | ret = CaviumRsaPrivateDecrypt(in, inLen, in, inLen, key); |
wolfSSL | 0:9d17e4342598 | 316 | if (ret > 0) |
wolfSSL | 0:9d17e4342598 | 317 | *out = in; |
wolfSSL | 0:9d17e4342598 | 318 | return ret; |
wolfSSL | 0:9d17e4342598 | 319 | } |
wolfSSL | 0:9d17e4342598 | 320 | #endif |
wolfSSL | 0:9d17e4342598 | 321 | |
wolfSSL | 0:9d17e4342598 | 322 | if ((ret = RsaFunction(in, inLen, in, &inLen, RSA_PRIVATE_DECRYPT, key)) |
wolfSSL | 0:9d17e4342598 | 323 | < 0) { |
wolfSSL | 0:9d17e4342598 | 324 | return ret; |
wolfSSL | 0:9d17e4342598 | 325 | } |
wolfSSL | 0:9d17e4342598 | 326 | |
wolfSSL | 0:9d17e4342598 | 327 | plainLen = RsaUnPad(in, inLen, out, RSA_BLOCK_TYPE_2); |
wolfSSL | 0:9d17e4342598 | 328 | |
wolfSSL | 0:9d17e4342598 | 329 | return plainLen; |
wolfSSL | 0:9d17e4342598 | 330 | } |
wolfSSL | 0:9d17e4342598 | 331 | |
wolfSSL | 0:9d17e4342598 | 332 | |
wolfSSL | 0:9d17e4342598 | 333 | int RsaPrivateDecrypt(const byte* in, word32 inLen, byte* out, word32 outLen, |
wolfSSL | 0:9d17e4342598 | 334 | RsaKey* key) |
wolfSSL | 0:9d17e4342598 | 335 | { |
wolfSSL | 0:9d17e4342598 | 336 | int plainLen, ret; |
wolfSSL | 0:9d17e4342598 | 337 | byte* tmp; |
wolfSSL | 0:9d17e4342598 | 338 | byte* pad = 0; |
wolfSSL | 0:9d17e4342598 | 339 | |
wolfSSL | 0:9d17e4342598 | 340 | #ifdef HAVE_CAVIUM |
wolfSSL | 0:9d17e4342598 | 341 | if (key->magic == CYASSL_RSA_CAVIUM_MAGIC) |
wolfSSL | 0:9d17e4342598 | 342 | return CaviumRsaPrivateDecrypt(in, inLen, out, outLen, key); |
wolfSSL | 0:9d17e4342598 | 343 | #endif |
wolfSSL | 0:9d17e4342598 | 344 | |
wolfSSL | 0:9d17e4342598 | 345 | tmp = (byte*)XMALLOC(inLen, key->heap, DYNAMIC_TYPE_RSA); |
wolfSSL | 0:9d17e4342598 | 346 | if (tmp == NULL) { |
wolfSSL | 0:9d17e4342598 | 347 | return MEMORY_E; |
wolfSSL | 0:9d17e4342598 | 348 | } |
wolfSSL | 0:9d17e4342598 | 349 | |
wolfSSL | 0:9d17e4342598 | 350 | XMEMCPY(tmp, in, inLen); |
wolfSSL | 0:9d17e4342598 | 351 | |
wolfSSL | 0:9d17e4342598 | 352 | if ((ret = plainLen = RsaPrivateDecryptInline(tmp, inLen, &pad, key)) |
wolfSSL | 0:9d17e4342598 | 353 | < 0) { |
wolfSSL | 0:9d17e4342598 | 354 | XFREE(tmp, key->heap, DYNAMIC_TYPE_RSA); |
wolfSSL | 0:9d17e4342598 | 355 | return ret; |
wolfSSL | 0:9d17e4342598 | 356 | } |
wolfSSL | 0:9d17e4342598 | 357 | if (plainLen > (int)outLen) |
wolfSSL | 0:9d17e4342598 | 358 | plainLen = BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 359 | else |
wolfSSL | 0:9d17e4342598 | 360 | XMEMCPY(out, pad, plainLen); |
wolfSSL | 0:9d17e4342598 | 361 | XMEMSET(tmp, 0x00, inLen); |
wolfSSL | 0:9d17e4342598 | 362 | |
wolfSSL | 0:9d17e4342598 | 363 | XFREE(tmp, key->heap, DYNAMIC_TYPE_RSA); |
wolfSSL | 0:9d17e4342598 | 364 | return plainLen; |
wolfSSL | 0:9d17e4342598 | 365 | } |
wolfSSL | 0:9d17e4342598 | 366 | |
wolfSSL | 0:9d17e4342598 | 367 | |
wolfSSL | 0:9d17e4342598 | 368 | /* for Rsa Verify */ |
wolfSSL | 0:9d17e4342598 | 369 | int RsaSSL_VerifyInline(byte* in, word32 inLen, byte** out, RsaKey* key) |
wolfSSL | 0:9d17e4342598 | 370 | { |
wolfSSL | 0:9d17e4342598 | 371 | int plainLen, ret; |
wolfSSL | 0:9d17e4342598 | 372 | |
wolfSSL | 0:9d17e4342598 | 373 | #ifdef HAVE_CAVIUM |
wolfSSL | 0:9d17e4342598 | 374 | if (key->magic == CYASSL_RSA_CAVIUM_MAGIC) { |
wolfSSL | 0:9d17e4342598 | 375 | ret = CaviumRsaSSL_Verify(in, inLen, in, inLen, key); |
wolfSSL | 0:9d17e4342598 | 376 | if (ret > 0) |
wolfSSL | 0:9d17e4342598 | 377 | *out = in; |
wolfSSL | 0:9d17e4342598 | 378 | return ret; |
wolfSSL | 0:9d17e4342598 | 379 | } |
wolfSSL | 0:9d17e4342598 | 380 | #endif |
wolfSSL | 0:9d17e4342598 | 381 | |
wolfSSL | 0:9d17e4342598 | 382 | if ((ret = RsaFunction(in, inLen, in, &inLen, RSA_PUBLIC_DECRYPT, key)) |
wolfSSL | 0:9d17e4342598 | 383 | < 0) { |
wolfSSL | 0:9d17e4342598 | 384 | return ret; |
wolfSSL | 0:9d17e4342598 | 385 | } |
wolfSSL | 0:9d17e4342598 | 386 | |
wolfSSL | 0:9d17e4342598 | 387 | plainLen = RsaUnPad(in, inLen, out, RSA_BLOCK_TYPE_1); |
wolfSSL | 0:9d17e4342598 | 388 | |
wolfSSL | 0:9d17e4342598 | 389 | return plainLen; |
wolfSSL | 0:9d17e4342598 | 390 | } |
wolfSSL | 0:9d17e4342598 | 391 | |
wolfSSL | 0:9d17e4342598 | 392 | |
wolfSSL | 0:9d17e4342598 | 393 | int RsaSSL_Verify(const byte* in, word32 inLen, byte* out, word32 outLen, |
wolfSSL | 0:9d17e4342598 | 394 | RsaKey* key) |
wolfSSL | 0:9d17e4342598 | 395 | { |
wolfSSL | 0:9d17e4342598 | 396 | int plainLen, ret; |
wolfSSL | 0:9d17e4342598 | 397 | byte* tmp; |
wolfSSL | 0:9d17e4342598 | 398 | byte* pad = 0; |
wolfSSL | 0:9d17e4342598 | 399 | |
wolfSSL | 0:9d17e4342598 | 400 | #ifdef HAVE_CAVIUM |
wolfSSL | 0:9d17e4342598 | 401 | if (key->magic == CYASSL_RSA_CAVIUM_MAGIC) |
wolfSSL | 0:9d17e4342598 | 402 | return CaviumRsaSSL_Verify(in, inLen, out, outLen, key); |
wolfSSL | 0:9d17e4342598 | 403 | #endif |
wolfSSL | 0:9d17e4342598 | 404 | |
wolfSSL | 0:9d17e4342598 | 405 | tmp = (byte*)XMALLOC(inLen, key->heap, DYNAMIC_TYPE_RSA); |
wolfSSL | 0:9d17e4342598 | 406 | if (tmp == NULL) { |
wolfSSL | 0:9d17e4342598 | 407 | return MEMORY_E; |
wolfSSL | 0:9d17e4342598 | 408 | } |
wolfSSL | 0:9d17e4342598 | 409 | |
wolfSSL | 0:9d17e4342598 | 410 | XMEMCPY(tmp, in, inLen); |
wolfSSL | 0:9d17e4342598 | 411 | |
wolfSSL | 0:9d17e4342598 | 412 | if ((ret = plainLen = RsaSSL_VerifyInline(tmp, inLen, &pad, key)) |
wolfSSL | 0:9d17e4342598 | 413 | < 0) { |
wolfSSL | 0:9d17e4342598 | 414 | XFREE(tmp, key->heap, DYNAMIC_TYPE_RSA); |
wolfSSL | 0:9d17e4342598 | 415 | return ret; |
wolfSSL | 0:9d17e4342598 | 416 | } |
wolfSSL | 0:9d17e4342598 | 417 | |
wolfSSL | 0:9d17e4342598 | 418 | if (plainLen > (int)outLen) |
wolfSSL | 0:9d17e4342598 | 419 | plainLen = BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 420 | else |
wolfSSL | 0:9d17e4342598 | 421 | XMEMCPY(out, pad, plainLen); |
wolfSSL | 0:9d17e4342598 | 422 | XMEMSET(tmp, 0x00, inLen); |
wolfSSL | 0:9d17e4342598 | 423 | |
wolfSSL | 0:9d17e4342598 | 424 | XFREE(tmp, key->heap, DYNAMIC_TYPE_RSA); |
wolfSSL | 0:9d17e4342598 | 425 | return plainLen; |
wolfSSL | 0:9d17e4342598 | 426 | } |
wolfSSL | 0:9d17e4342598 | 427 | |
wolfSSL | 0:9d17e4342598 | 428 | |
wolfSSL | 0:9d17e4342598 | 429 | /* for Rsa Sign */ |
wolfSSL | 0:9d17e4342598 | 430 | int RsaSSL_Sign(const byte* in, word32 inLen, byte* out, word32 outLen, |
wolfSSL | 0:9d17e4342598 | 431 | RsaKey* key, RNG* rng) |
wolfSSL | 0:9d17e4342598 | 432 | { |
wolfSSL | 0:9d17e4342598 | 433 | int sz, ret; |
wolfSSL | 0:9d17e4342598 | 434 | |
wolfSSL | 0:9d17e4342598 | 435 | #ifdef HAVE_CAVIUM |
wolfSSL | 0:9d17e4342598 | 436 | if (key->magic == CYASSL_RSA_CAVIUM_MAGIC) |
wolfSSL | 0:9d17e4342598 | 437 | return CaviumRsaSSL_Sign(in, inLen, out, outLen, key); |
wolfSSL | 0:9d17e4342598 | 438 | #endif |
wolfSSL | 0:9d17e4342598 | 439 | |
wolfSSL | 0:9d17e4342598 | 440 | sz = mp_unsigned_bin_size(&key->n); |
wolfSSL | 0:9d17e4342598 | 441 | if (sz > (int)outLen) |
wolfSSL | 0:9d17e4342598 | 442 | return RSA_BUFFER_E; |
wolfSSL | 0:9d17e4342598 | 443 | |
wolfSSL | 0:9d17e4342598 | 444 | if (inLen > (word32)(sz - RSA_MIN_PAD_SZ)) |
wolfSSL | 0:9d17e4342598 | 445 | return RSA_BUFFER_E; |
wolfSSL | 0:9d17e4342598 | 446 | |
wolfSSL | 0:9d17e4342598 | 447 | RsaPad(in, inLen, out, sz, RSA_BLOCK_TYPE_1, rng); |
wolfSSL | 0:9d17e4342598 | 448 | |
wolfSSL | 0:9d17e4342598 | 449 | if ((ret = RsaFunction(out, sz, out, &outLen, RSA_PRIVATE_ENCRYPT,key)) < 0) |
wolfSSL | 0:9d17e4342598 | 450 | sz = ret; |
wolfSSL | 0:9d17e4342598 | 451 | |
wolfSSL | 0:9d17e4342598 | 452 | return sz; |
wolfSSL | 0:9d17e4342598 | 453 | } |
wolfSSL | 0:9d17e4342598 | 454 | |
wolfSSL | 0:9d17e4342598 | 455 | |
wolfSSL | 0:9d17e4342598 | 456 | int RsaEncryptSize(RsaKey* key) |
wolfSSL | 0:9d17e4342598 | 457 | { |
wolfSSL | 0:9d17e4342598 | 458 | #ifdef HAVE_CAVIUM |
wolfSSL | 0:9d17e4342598 | 459 | if (key->magic == CYASSL_RSA_CAVIUM_MAGIC) |
wolfSSL | 0:9d17e4342598 | 460 | return key->c_nSz; |
wolfSSL | 0:9d17e4342598 | 461 | #endif |
wolfSSL | 0:9d17e4342598 | 462 | return mp_unsigned_bin_size(&key->n); |
wolfSSL | 0:9d17e4342598 | 463 | } |
wolfSSL | 0:9d17e4342598 | 464 | |
wolfSSL | 0:9d17e4342598 | 465 | |
wolfSSL | 0:9d17e4342598 | 466 | #ifdef CYASSL_KEY_GEN |
wolfSSL | 0:9d17e4342598 | 467 | |
wolfSSL | 0:9d17e4342598 | 468 | static const int USE_BBS = 1; |
wolfSSL | 0:9d17e4342598 | 469 | |
wolfSSL | 0:9d17e4342598 | 470 | static int rand_prime(mp_int* N, int len, RNG* rng, void* heap) |
wolfSSL | 0:9d17e4342598 | 471 | { |
wolfSSL | 0:9d17e4342598 | 472 | int err, res, type; |
wolfSSL | 0:9d17e4342598 | 473 | byte* buf; |
wolfSSL | 0:9d17e4342598 | 474 | |
wolfSSL | 0:9d17e4342598 | 475 | (void)heap; |
wolfSSL | 0:9d17e4342598 | 476 | if (N == NULL || rng == NULL) |
wolfSSL | 0:9d17e4342598 | 477 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 478 | |
wolfSSL | 0:9d17e4342598 | 479 | /* get type */ |
wolfSSL | 0:9d17e4342598 | 480 | if (len < 0) { |
wolfSSL | 0:9d17e4342598 | 481 | type = USE_BBS; |
wolfSSL | 0:9d17e4342598 | 482 | len = -len; |
wolfSSL | 0:9d17e4342598 | 483 | } else { |
wolfSSL | 0:9d17e4342598 | 484 | type = 0; |
wolfSSL | 0:9d17e4342598 | 485 | } |
wolfSSL | 0:9d17e4342598 | 486 | |
wolfSSL | 0:9d17e4342598 | 487 | /* allow sizes between 2 and 512 bytes for a prime size */ |
wolfSSL | 0:9d17e4342598 | 488 | if (len < 2 || len > 512) { |
wolfSSL | 0:9d17e4342598 | 489 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 490 | } |
wolfSSL | 0:9d17e4342598 | 491 | |
wolfSSL | 0:9d17e4342598 | 492 | /* allocate buffer to work with */ |
wolfSSL | 0:9d17e4342598 | 493 | buf = (byte*)XMALLOC(len, heap, DYNAMIC_TYPE_RSA); |
wolfSSL | 0:9d17e4342598 | 494 | if (buf == NULL) { |
wolfSSL | 0:9d17e4342598 | 495 | return MEMORY_E; |
wolfSSL | 0:9d17e4342598 | 496 | } |
wolfSSL | 0:9d17e4342598 | 497 | XMEMSET(buf, 0, len); |
wolfSSL | 0:9d17e4342598 | 498 | |
wolfSSL | 0:9d17e4342598 | 499 | do { |
wolfSSL | 0:9d17e4342598 | 500 | #ifdef SHOW_GEN |
wolfSSL | 0:9d17e4342598 | 501 | printf("."); |
wolfSSL | 0:9d17e4342598 | 502 | fflush(stdout); |
wolfSSL | 0:9d17e4342598 | 503 | #endif |
wolfSSL | 0:9d17e4342598 | 504 | /* generate value */ |
wolfSSL | 0:9d17e4342598 | 505 | RNG_GenerateBlock(rng, buf, len); |
wolfSSL | 0:9d17e4342598 | 506 | |
wolfSSL | 0:9d17e4342598 | 507 | /* munge bits */ |
wolfSSL | 0:9d17e4342598 | 508 | buf[0] |= 0x80 | 0x40; |
wolfSSL | 0:9d17e4342598 | 509 | buf[len-1] |= 0x01 | ((type & USE_BBS) ? 0x02 : 0x00); |
wolfSSL | 0:9d17e4342598 | 510 | |
wolfSSL | 0:9d17e4342598 | 511 | /* load value */ |
wolfSSL | 0:9d17e4342598 | 512 | if ((err = mp_read_unsigned_bin(N, buf, len)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 513 | XFREE(buf, heap, DYNAMIC_TYPE_RSA); |
wolfSSL | 0:9d17e4342598 | 514 | return err; |
wolfSSL | 0:9d17e4342598 | 515 | } |
wolfSSL | 0:9d17e4342598 | 516 | |
wolfSSL | 0:9d17e4342598 | 517 | /* test */ |
wolfSSL | 0:9d17e4342598 | 518 | if ((err = mp_prime_is_prime(N, 8, &res)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 519 | XFREE(buf, heap, DYNAMIC_TYPE_RSA); |
wolfSSL | 0:9d17e4342598 | 520 | return err; |
wolfSSL | 0:9d17e4342598 | 521 | } |
wolfSSL | 0:9d17e4342598 | 522 | } while (res == MP_NO); |
wolfSSL | 0:9d17e4342598 | 523 | |
wolfSSL | 0:9d17e4342598 | 524 | #ifdef LTC_CLEAN_STACK |
wolfSSL | 0:9d17e4342598 | 525 | XMEMSET(buf, 0, len); |
wolfSSL | 0:9d17e4342598 | 526 | #endif |
wolfSSL | 0:9d17e4342598 | 527 | |
wolfSSL | 0:9d17e4342598 | 528 | XFREE(buf, heap, DYNAMIC_TYPE_RSA); |
wolfSSL | 0:9d17e4342598 | 529 | return 0; |
wolfSSL | 0:9d17e4342598 | 530 | } |
wolfSSL | 0:9d17e4342598 | 531 | |
wolfSSL | 0:9d17e4342598 | 532 | |
wolfSSL | 0:9d17e4342598 | 533 | /* Make an RSA key for size bits, with e specified, 65537 is a good e */ |
wolfSSL | 0:9d17e4342598 | 534 | int MakeRsaKey(RsaKey* key, int size, long e, RNG* rng) |
wolfSSL | 0:9d17e4342598 | 535 | { |
wolfSSL | 0:9d17e4342598 | 536 | mp_int p, q, tmp1, tmp2, tmp3; |
wolfSSL | 0:9d17e4342598 | 537 | int err; |
wolfSSL | 0:9d17e4342598 | 538 | |
wolfSSL | 0:9d17e4342598 | 539 | if (key == NULL || rng == NULL) |
wolfSSL | 0:9d17e4342598 | 540 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 541 | |
wolfSSL | 0:9d17e4342598 | 542 | if (size < RSA_MIN_SIZE || size > RSA_MAX_SIZE) |
wolfSSL | 0:9d17e4342598 | 543 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 544 | |
wolfSSL | 0:9d17e4342598 | 545 | if (e < 3 || (e & 1) == 0) |
wolfSSL | 0:9d17e4342598 | 546 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 547 | |
wolfSSL | 0:9d17e4342598 | 548 | if ((err = mp_init_multi(&p, &q, &tmp1, &tmp2, &tmp3, NULL)) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 549 | return err; |
wolfSSL | 0:9d17e4342598 | 550 | |
wolfSSL | 0:9d17e4342598 | 551 | err = mp_set_int(&tmp3, e); |
wolfSSL | 0:9d17e4342598 | 552 | |
wolfSSL | 0:9d17e4342598 | 553 | /* make p */ |
wolfSSL | 0:9d17e4342598 | 554 | if (err == MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 555 | do { |
wolfSSL | 0:9d17e4342598 | 556 | err = rand_prime(&p, size/16, rng, key->heap); /* size in bytes/2 */ |
wolfSSL | 0:9d17e4342598 | 557 | |
wolfSSL | 0:9d17e4342598 | 558 | if (err == MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 559 | err = mp_sub_d(&p, 1, &tmp1); /* tmp1 = p-1 */ |
wolfSSL | 0:9d17e4342598 | 560 | |
wolfSSL | 0:9d17e4342598 | 561 | if (err == MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 562 | err = mp_gcd(&tmp1, &tmp3, &tmp2); /* tmp2 = gcd(p-1, e) */ |
wolfSSL | 0:9d17e4342598 | 563 | } while (err == MP_OKAY && mp_cmp_d(&tmp2, 1) != 0); /* e divdes p-1 */ |
wolfSSL | 0:9d17e4342598 | 564 | } |
wolfSSL | 0:9d17e4342598 | 565 | |
wolfSSL | 0:9d17e4342598 | 566 | /* make q */ |
wolfSSL | 0:9d17e4342598 | 567 | if (err == MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 568 | do { |
wolfSSL | 0:9d17e4342598 | 569 | err = rand_prime(&q, size/16, rng, key->heap); /* size in bytes/2 */ |
wolfSSL | 0:9d17e4342598 | 570 | |
wolfSSL | 0:9d17e4342598 | 571 | if (err == MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 572 | err = mp_sub_d(&q, 1, &tmp1); /* tmp1 = q-1 */ |
wolfSSL | 0:9d17e4342598 | 573 | |
wolfSSL | 0:9d17e4342598 | 574 | if (err == MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 575 | err = mp_gcd(&tmp1, &tmp3, &tmp2); /* tmp2 = gcd(q-1, e) */ |
wolfSSL | 0:9d17e4342598 | 576 | } while (err == MP_OKAY && mp_cmp_d(&tmp2, 1) != 0); /* e divdes q-1 */ |
wolfSSL | 0:9d17e4342598 | 577 | } |
wolfSSL | 0:9d17e4342598 | 578 | |
wolfSSL | 0:9d17e4342598 | 579 | if (err == MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 580 | err = mp_init_multi(&key->n, &key->e, &key->d, &key->p, &key->q, NULL); |
wolfSSL | 0:9d17e4342598 | 581 | |
wolfSSL | 0:9d17e4342598 | 582 | if (err == MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 583 | err = mp_init_multi(&key->dP, &key->dQ, &key->u, NULL, NULL, NULL); |
wolfSSL | 0:9d17e4342598 | 584 | |
wolfSSL | 0:9d17e4342598 | 585 | if (err == MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 586 | err = mp_sub_d(&p, 1, &tmp2); /* tmp2 = p-1 */ |
wolfSSL | 0:9d17e4342598 | 587 | |
wolfSSL | 0:9d17e4342598 | 588 | if (err == MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 589 | err = mp_lcm(&tmp1, &tmp2, &tmp1); /* tmp1 = lcm(p-1, q-1),last loop */ |
wolfSSL | 0:9d17e4342598 | 590 | |
wolfSSL | 0:9d17e4342598 | 591 | /* make key */ |
wolfSSL | 0:9d17e4342598 | 592 | if (err == MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 593 | err = mp_set_int(&key->e, e); /* key->e = e */ |
wolfSSL | 0:9d17e4342598 | 594 | |
wolfSSL | 0:9d17e4342598 | 595 | if (err == MP_OKAY) /* key->d = 1/e mod lcm(p-1, q-1) */ |
wolfSSL | 0:9d17e4342598 | 596 | err = mp_invmod(&key->e, &tmp1, &key->d); |
wolfSSL | 0:9d17e4342598 | 597 | |
wolfSSL | 0:9d17e4342598 | 598 | if (err == MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 599 | err = mp_mul(&p, &q, &key->n); /* key->n = pq */ |
wolfSSL | 0:9d17e4342598 | 600 | |
wolfSSL | 0:9d17e4342598 | 601 | if (err == MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 602 | err = mp_sub_d(&p, 1, &tmp1); |
wolfSSL | 0:9d17e4342598 | 603 | |
wolfSSL | 0:9d17e4342598 | 604 | if (err == MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 605 | err = mp_sub_d(&q, 1, &tmp2); |
wolfSSL | 0:9d17e4342598 | 606 | |
wolfSSL | 0:9d17e4342598 | 607 | if (err == MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 608 | err = mp_mod(&key->d, &tmp1, &key->dP); |
wolfSSL | 0:9d17e4342598 | 609 | |
wolfSSL | 0:9d17e4342598 | 610 | if (err == MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 611 | err = mp_mod(&key->d, &tmp2, &key->dQ); |
wolfSSL | 0:9d17e4342598 | 612 | |
wolfSSL | 0:9d17e4342598 | 613 | if (err == MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 614 | err = mp_invmod(&q, &p, &key->u); |
wolfSSL | 0:9d17e4342598 | 615 | |
wolfSSL | 0:9d17e4342598 | 616 | if (err == MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 617 | err = mp_copy(&p, &key->p); |
wolfSSL | 0:9d17e4342598 | 618 | |
wolfSSL | 0:9d17e4342598 | 619 | if (err == MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 620 | err = mp_copy(&q, &key->q); |
wolfSSL | 0:9d17e4342598 | 621 | |
wolfSSL | 0:9d17e4342598 | 622 | if (err == MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 623 | key->type = RSA_PRIVATE; |
wolfSSL | 0:9d17e4342598 | 624 | |
wolfSSL | 0:9d17e4342598 | 625 | mp_clear(&tmp3); |
wolfSSL | 0:9d17e4342598 | 626 | mp_clear(&tmp2); |
wolfSSL | 0:9d17e4342598 | 627 | mp_clear(&tmp1); |
wolfSSL | 0:9d17e4342598 | 628 | mp_clear(&q); |
wolfSSL | 0:9d17e4342598 | 629 | mp_clear(&p); |
wolfSSL | 0:9d17e4342598 | 630 | |
wolfSSL | 0:9d17e4342598 | 631 | if (err != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 632 | FreeRsaKey(key); |
wolfSSL | 0:9d17e4342598 | 633 | return err; |
wolfSSL | 0:9d17e4342598 | 634 | } |
wolfSSL | 0:9d17e4342598 | 635 | |
wolfSSL | 0:9d17e4342598 | 636 | return 0; |
wolfSSL | 0:9d17e4342598 | 637 | } |
wolfSSL | 0:9d17e4342598 | 638 | |
wolfSSL | 0:9d17e4342598 | 639 | |
wolfSSL | 0:9d17e4342598 | 640 | #endif /* CYASSL_KEY_GEN */ |
wolfSSL | 0:9d17e4342598 | 641 | |
wolfSSL | 0:9d17e4342598 | 642 | |
wolfSSL | 0:9d17e4342598 | 643 | #ifdef HAVE_CAVIUM |
wolfSSL | 0:9d17e4342598 | 644 | |
wolfSSL | 0:9d17e4342598 | 645 | #include <cyassl/ctaocrypt/logging.h> |
wolfSSL | 0:9d17e4342598 | 646 | #include "cavium_common.h" |
wolfSSL | 0:9d17e4342598 | 647 | |
wolfSSL | 0:9d17e4342598 | 648 | /* Initiliaze RSA for use with Nitrox device */ |
wolfSSL | 0:9d17e4342598 | 649 | int RsaInitCavium(RsaKey* rsa, int devId) |
wolfSSL | 0:9d17e4342598 | 650 | { |
wolfSSL | 0:9d17e4342598 | 651 | if (rsa == NULL) |
wolfSSL | 0:9d17e4342598 | 652 | return -1; |
wolfSSL | 0:9d17e4342598 | 653 | |
wolfSSL | 0:9d17e4342598 | 654 | if (CspAllocContext(CONTEXT_SSL, &rsa->contextHandle, devId) != 0) |
wolfSSL | 0:9d17e4342598 | 655 | return -1; |
wolfSSL | 0:9d17e4342598 | 656 | |
wolfSSL | 0:9d17e4342598 | 657 | rsa->devId = devId; |
wolfSSL | 0:9d17e4342598 | 658 | rsa->magic = CYASSL_RSA_CAVIUM_MAGIC; |
wolfSSL | 0:9d17e4342598 | 659 | |
wolfSSL | 0:9d17e4342598 | 660 | return 0; |
wolfSSL | 0:9d17e4342598 | 661 | } |
wolfSSL | 0:9d17e4342598 | 662 | |
wolfSSL | 0:9d17e4342598 | 663 | |
wolfSSL | 0:9d17e4342598 | 664 | /* Free RSA from use with Nitrox device */ |
wolfSSL | 0:9d17e4342598 | 665 | void RsaFreeCavium(RsaKey* rsa) |
wolfSSL | 0:9d17e4342598 | 666 | { |
wolfSSL | 0:9d17e4342598 | 667 | if (rsa == NULL) |
wolfSSL | 0:9d17e4342598 | 668 | return; |
wolfSSL | 0:9d17e4342598 | 669 | |
wolfSSL | 0:9d17e4342598 | 670 | CspFreeContext(CONTEXT_SSL, rsa->contextHandle, rsa->devId); |
wolfSSL | 0:9d17e4342598 | 671 | rsa->magic = 0; |
wolfSSL | 0:9d17e4342598 | 672 | } |
wolfSSL | 0:9d17e4342598 | 673 | |
wolfSSL | 0:9d17e4342598 | 674 | |
wolfSSL | 0:9d17e4342598 | 675 | /* Initialize cavium RSA key */ |
wolfSSL | 0:9d17e4342598 | 676 | static int InitCaviumRsaKey(RsaKey* key, void* heap) |
wolfSSL | 0:9d17e4342598 | 677 | { |
wolfSSL | 0:9d17e4342598 | 678 | if (key == NULL) |
wolfSSL | 0:9d17e4342598 | 679 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 680 | |
wolfSSL | 0:9d17e4342598 | 681 | key->heap = heap; |
wolfSSL | 0:9d17e4342598 | 682 | key->type = -1; /* don't know yet */ |
wolfSSL | 0:9d17e4342598 | 683 | |
wolfSSL | 0:9d17e4342598 | 684 | key->c_n = NULL; |
wolfSSL | 0:9d17e4342598 | 685 | key->c_e = NULL; |
wolfSSL | 0:9d17e4342598 | 686 | key->c_d = NULL; |
wolfSSL | 0:9d17e4342598 | 687 | key->c_p = NULL; |
wolfSSL | 0:9d17e4342598 | 688 | key->c_q = NULL; |
wolfSSL | 0:9d17e4342598 | 689 | key->c_dP = NULL; |
wolfSSL | 0:9d17e4342598 | 690 | key->c_dQ = NULL; |
wolfSSL | 0:9d17e4342598 | 691 | key->c_u = NULL; |
wolfSSL | 0:9d17e4342598 | 692 | |
wolfSSL | 0:9d17e4342598 | 693 | key->c_nSz = 0; |
wolfSSL | 0:9d17e4342598 | 694 | key->c_eSz = 0; |
wolfSSL | 0:9d17e4342598 | 695 | key->c_dSz = 0; |
wolfSSL | 0:9d17e4342598 | 696 | key->c_pSz = 0; |
wolfSSL | 0:9d17e4342598 | 697 | key->c_qSz = 0; |
wolfSSL | 0:9d17e4342598 | 698 | key->c_dP_Sz = 0; |
wolfSSL | 0:9d17e4342598 | 699 | key->c_dQ_Sz = 0; |
wolfSSL | 0:9d17e4342598 | 700 | key->c_uSz = 0; |
wolfSSL | 0:9d17e4342598 | 701 | |
wolfSSL | 0:9d17e4342598 | 702 | return 0; |
wolfSSL | 0:9d17e4342598 | 703 | } |
wolfSSL | 0:9d17e4342598 | 704 | |
wolfSSL | 0:9d17e4342598 | 705 | |
wolfSSL | 0:9d17e4342598 | 706 | /* Free cavium RSA key */ |
wolfSSL | 0:9d17e4342598 | 707 | static int FreeCaviumRsaKey(RsaKey* key) |
wolfSSL | 0:9d17e4342598 | 708 | { |
wolfSSL | 0:9d17e4342598 | 709 | if (key == NULL) |
wolfSSL | 0:9d17e4342598 | 710 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 711 | |
wolfSSL | 0:9d17e4342598 | 712 | XFREE(key->c_n, key->heap, DYNAMIC_TYPE_CAVIUM_TMP); |
wolfSSL | 0:9d17e4342598 | 713 | XFREE(key->c_e, key->heap, DYNAMIC_TYPE_CAVIUM_TMP); |
wolfSSL | 0:9d17e4342598 | 714 | XFREE(key->c_d, key->heap, DYNAMIC_TYPE_CAVIUM_TMP); |
wolfSSL | 0:9d17e4342598 | 715 | XFREE(key->c_p, key->heap, DYNAMIC_TYPE_CAVIUM_TMP); |
wolfSSL | 0:9d17e4342598 | 716 | XFREE(key->c_q, key->heap, DYNAMIC_TYPE_CAVIUM_TMP); |
wolfSSL | 0:9d17e4342598 | 717 | XFREE(key->c_dP, key->heap, DYNAMIC_TYPE_CAVIUM_TMP); |
wolfSSL | 0:9d17e4342598 | 718 | XFREE(key->c_dQ, key->heap, DYNAMIC_TYPE_CAVIUM_TMP); |
wolfSSL | 0:9d17e4342598 | 719 | XFREE(key->c_u, key->heap, DYNAMIC_TYPE_CAVIUM_TMP); |
wolfSSL | 0:9d17e4342598 | 720 | |
wolfSSL | 0:9d17e4342598 | 721 | return InitCaviumRsaKey(key, key->heap); /* reset pointers */ |
wolfSSL | 0:9d17e4342598 | 722 | } |
wolfSSL | 0:9d17e4342598 | 723 | |
wolfSSL | 0:9d17e4342598 | 724 | |
wolfSSL | 0:9d17e4342598 | 725 | static int CaviumRsaPublicEncrypt(const byte* in, word32 inLen, byte* out, |
wolfSSL | 0:9d17e4342598 | 726 | word32 outLen, RsaKey* key) |
wolfSSL | 0:9d17e4342598 | 727 | { |
wolfSSL | 0:9d17e4342598 | 728 | word32 requestId; |
wolfSSL | 0:9d17e4342598 | 729 | word32 ret; |
wolfSSL | 0:9d17e4342598 | 730 | |
wolfSSL | 0:9d17e4342598 | 731 | if (key == NULL || in == NULL || out == NULL || outLen < (word32)key->c_nSz) |
wolfSSL | 0:9d17e4342598 | 732 | return -1; |
wolfSSL | 0:9d17e4342598 | 733 | |
wolfSSL | 0:9d17e4342598 | 734 | ret = CspPkcs1v15Enc(CAVIUM_BLOCKING, BT2, key->c_nSz, key->c_eSz, |
wolfSSL | 0:9d17e4342598 | 735 | (word16)inLen, key->c_n, key->c_e, (byte*)in, out, |
wolfSSL | 0:9d17e4342598 | 736 | &requestId, key->devId); |
wolfSSL | 0:9d17e4342598 | 737 | if (ret != 0) { |
wolfSSL | 0:9d17e4342598 | 738 | CYASSL_MSG("Cavium Enc BT2 failed"); |
wolfSSL | 0:9d17e4342598 | 739 | return -1; |
wolfSSL | 0:9d17e4342598 | 740 | } |
wolfSSL | 0:9d17e4342598 | 741 | return key->c_nSz; |
wolfSSL | 0:9d17e4342598 | 742 | } |
wolfSSL | 0:9d17e4342598 | 743 | |
wolfSSL | 0:9d17e4342598 | 744 | |
wolfSSL | 0:9d17e4342598 | 745 | static INLINE void ato16(const byte* c, word16* u16) |
wolfSSL | 0:9d17e4342598 | 746 | { |
wolfSSL | 0:9d17e4342598 | 747 | *u16 = (c[0] << 8) | (c[1]); |
wolfSSL | 0:9d17e4342598 | 748 | } |
wolfSSL | 0:9d17e4342598 | 749 | |
wolfSSL | 0:9d17e4342598 | 750 | |
wolfSSL | 0:9d17e4342598 | 751 | static int CaviumRsaPrivateDecrypt(const byte* in, word32 inLen, byte* out, |
wolfSSL | 0:9d17e4342598 | 752 | word32 outLen, RsaKey* key) |
wolfSSL | 0:9d17e4342598 | 753 | { |
wolfSSL | 0:9d17e4342598 | 754 | word32 requestId; |
wolfSSL | 0:9d17e4342598 | 755 | word32 ret; |
wolfSSL | 0:9d17e4342598 | 756 | word16 outSz = (word16)outLen; |
wolfSSL | 0:9d17e4342598 | 757 | |
wolfSSL | 0:9d17e4342598 | 758 | if (key == NULL || in == NULL || out == NULL || inLen != (word32)key->c_nSz) |
wolfSSL | 0:9d17e4342598 | 759 | return -1; |
wolfSSL | 0:9d17e4342598 | 760 | |
wolfSSL | 0:9d17e4342598 | 761 | ret = CspPkcs1v15CrtDec(CAVIUM_BLOCKING, BT2, key->c_nSz, key->c_q, |
wolfSSL | 0:9d17e4342598 | 762 | key->c_dQ, key->c_p, key->c_dP, key->c_u, |
wolfSSL | 0:9d17e4342598 | 763 | (byte*)in, &outSz, out, &requestId, key->devId); |
wolfSSL | 0:9d17e4342598 | 764 | if (ret != 0) { |
wolfSSL | 0:9d17e4342598 | 765 | CYASSL_MSG("Cavium CRT Dec BT2 failed"); |
wolfSSL | 0:9d17e4342598 | 766 | return -1; |
wolfSSL | 0:9d17e4342598 | 767 | } |
wolfSSL | 0:9d17e4342598 | 768 | ato16((const byte*)&outSz, &outSz); |
wolfSSL | 0:9d17e4342598 | 769 | |
wolfSSL | 0:9d17e4342598 | 770 | return outSz; |
wolfSSL | 0:9d17e4342598 | 771 | } |
wolfSSL | 0:9d17e4342598 | 772 | |
wolfSSL | 0:9d17e4342598 | 773 | |
wolfSSL | 0:9d17e4342598 | 774 | static int CaviumRsaSSL_Sign(const byte* in, word32 inLen, byte* out, |
wolfSSL | 0:9d17e4342598 | 775 | word32 outLen, RsaKey* key) |
wolfSSL | 0:9d17e4342598 | 776 | { |
wolfSSL | 0:9d17e4342598 | 777 | word32 requestId; |
wolfSSL | 0:9d17e4342598 | 778 | word32 ret; |
wolfSSL | 0:9d17e4342598 | 779 | |
wolfSSL | 0:9d17e4342598 | 780 | if (key == NULL || in == NULL || out == NULL || inLen == 0 || outLen < |
wolfSSL | 0:9d17e4342598 | 781 | (word32)key->c_nSz) |
wolfSSL | 0:9d17e4342598 | 782 | return -1; |
wolfSSL | 0:9d17e4342598 | 783 | |
wolfSSL | 0:9d17e4342598 | 784 | ret = CspPkcs1v15CrtEnc(CAVIUM_BLOCKING, BT1, key->c_nSz, (word16)inLen, |
wolfSSL | 0:9d17e4342598 | 785 | key->c_q, key->c_dQ, key->c_p, key->c_dP, key->c_u, |
wolfSSL | 0:9d17e4342598 | 786 | (byte*)in, out, &requestId, key->devId); |
wolfSSL | 0:9d17e4342598 | 787 | if (ret != 0) { |
wolfSSL | 0:9d17e4342598 | 788 | CYASSL_MSG("Cavium CRT Enc BT1 failed"); |
wolfSSL | 0:9d17e4342598 | 789 | return -1; |
wolfSSL | 0:9d17e4342598 | 790 | } |
wolfSSL | 0:9d17e4342598 | 791 | return key->c_nSz; |
wolfSSL | 0:9d17e4342598 | 792 | } |
wolfSSL | 0:9d17e4342598 | 793 | |
wolfSSL | 0:9d17e4342598 | 794 | |
wolfSSL | 0:9d17e4342598 | 795 | static int CaviumRsaSSL_Verify(const byte* in, word32 inLen, byte* out, |
wolfSSL | 0:9d17e4342598 | 796 | word32 outLen, RsaKey* key) |
wolfSSL | 0:9d17e4342598 | 797 | { |
wolfSSL | 0:9d17e4342598 | 798 | word32 requestId; |
wolfSSL | 0:9d17e4342598 | 799 | word32 ret; |
wolfSSL | 0:9d17e4342598 | 800 | word16 outSz = (word16)outLen; |
wolfSSL | 0:9d17e4342598 | 801 | |
wolfSSL | 0:9d17e4342598 | 802 | if (key == NULL || in == NULL || out == NULL || inLen != (word32)key->c_nSz) |
wolfSSL | 0:9d17e4342598 | 803 | return -1; |
wolfSSL | 0:9d17e4342598 | 804 | |
wolfSSL | 0:9d17e4342598 | 805 | ret = CspPkcs1v15Dec(CAVIUM_BLOCKING, BT1, key->c_nSz, key->c_eSz, |
wolfSSL | 0:9d17e4342598 | 806 | key->c_n, key->c_e, (byte*)in, &outSz, out, |
wolfSSL | 0:9d17e4342598 | 807 | &requestId, key->devId); |
wolfSSL | 0:9d17e4342598 | 808 | if (ret != 0) { |
wolfSSL | 0:9d17e4342598 | 809 | CYASSL_MSG("Cavium Dec BT1 failed"); |
wolfSSL | 0:9d17e4342598 | 810 | return -1; |
wolfSSL | 0:9d17e4342598 | 811 | } |
wolfSSL | 0:9d17e4342598 | 812 | outSz = ntohs(outSz); |
wolfSSL | 0:9d17e4342598 | 813 | |
wolfSSL | 0:9d17e4342598 | 814 | return outSz; |
wolfSSL | 0:9d17e4342598 | 815 | } |
wolfSSL | 0:9d17e4342598 | 816 | |
wolfSSL | 0:9d17e4342598 | 817 | |
wolfSSL | 0:9d17e4342598 | 818 | #endif /* HAVE_CAVIUM */ |
wolfSSL | 0:9d17e4342598 | 819 | |
wolfSSL | 0:9d17e4342598 | 820 | #endif /* NO_RSA */ |