SSL/TLS Library
CyaSSL is SSL/TLS library for embedded systems.
ctaocrypt/src/pwdbased.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 | /* pwdbased.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_PWDBASED |
wolfSSL | 0:9d17e4342598 | 29 | |
wolfSSL | 0:9d17e4342598 | 30 | #ifdef CYASSL_PIC32MZ_HASH |
wolfSSL | 0:9d17e4342598 | 31 | |
wolfSSL | 0:9d17e4342598 | 32 | #define InitMd5 InitMd5_sw |
wolfSSL | 0:9d17e4342598 | 33 | #define Md5Update Md5Update_sw |
wolfSSL | 0:9d17e4342598 | 34 | #define Md5Final Md5Final_sw |
wolfSSL | 0:9d17e4342598 | 35 | |
wolfSSL | 0:9d17e4342598 | 36 | #define InitSha InitSha_sw |
wolfSSL | 0:9d17e4342598 | 37 | #define ShaUpdate ShaUpdate_sw |
wolfSSL | 0:9d17e4342598 | 38 | #define ShaFinal ShaFinal_sw |
wolfSSL | 0:9d17e4342598 | 39 | |
wolfSSL | 0:9d17e4342598 | 40 | #define InitSha256 InitSha256_sw |
wolfSSL | 0:9d17e4342598 | 41 | #define Sha256Update Sha256Update_sw |
wolfSSL | 0:9d17e4342598 | 42 | #define Sha256Final Sha256Final_sw |
wolfSSL | 0:9d17e4342598 | 43 | |
wolfSSL | 0:9d17e4342598 | 44 | #endif |
wolfSSL | 0:9d17e4342598 | 45 | |
wolfSSL | 0:9d17e4342598 | 46 | #include <cyassl/ctaocrypt/pwdbased.h> |
wolfSSL | 0:9d17e4342598 | 47 | #include <cyassl/ctaocrypt/hmac.h> |
wolfSSL | 0:9d17e4342598 | 48 | #include <cyassl/ctaocrypt/integer.h> |
wolfSSL | 0:9d17e4342598 | 49 | #include <cyassl/ctaocrypt/error-crypt.h> |
wolfSSL | 0:9d17e4342598 | 50 | #if defined(CYASSL_SHA512) || defined(CYASSL_SHA384) |
wolfSSL | 0:9d17e4342598 | 51 | #include <cyassl/ctaocrypt/sha512.h> |
wolfSSL | 0:9d17e4342598 | 52 | #endif |
wolfSSL | 0:9d17e4342598 | 53 | |
wolfSSL | 0:9d17e4342598 | 54 | #ifdef NO_INLINE |
wolfSSL | 0:9d17e4342598 | 55 | #include <cyassl/ctaocrypt/misc.h> |
wolfSSL | 0:9d17e4342598 | 56 | #else |
wolfSSL | 0:9d17e4342598 | 57 | #include <ctaocrypt/src/misc.c> |
wolfSSL | 0:9d17e4342598 | 58 | #endif |
wolfSSL | 0:9d17e4342598 | 59 | |
wolfSSL | 0:9d17e4342598 | 60 | |
wolfSSL | 0:9d17e4342598 | 61 | #ifndef min |
wolfSSL | 0:9d17e4342598 | 62 | |
wolfSSL | 0:9d17e4342598 | 63 | static INLINE word32 min(word32 a, word32 b) |
wolfSSL | 0:9d17e4342598 | 64 | { |
wolfSSL | 0:9d17e4342598 | 65 | return a > b ? b : a; |
wolfSSL | 0:9d17e4342598 | 66 | } |
wolfSSL | 0:9d17e4342598 | 67 | |
wolfSSL | 0:9d17e4342598 | 68 | #endif /* min */ |
wolfSSL | 0:9d17e4342598 | 69 | |
wolfSSL | 0:9d17e4342598 | 70 | |
wolfSSL | 0:9d17e4342598 | 71 | int PBKDF1(byte* output, const byte* passwd, int pLen, const byte* salt, |
wolfSSL | 0:9d17e4342598 | 72 | int sLen, int iterations, int kLen, int hashType) |
wolfSSL | 0:9d17e4342598 | 73 | { |
wolfSSL | 0:9d17e4342598 | 74 | Md5 md5; |
wolfSSL | 0:9d17e4342598 | 75 | Sha sha; |
wolfSSL | 0:9d17e4342598 | 76 | int hLen = (hashType == MD5) ? (int)MD5_DIGEST_SIZE : (int)SHA_DIGEST_SIZE; |
wolfSSL | 0:9d17e4342598 | 77 | int i, ret = 0; |
wolfSSL | 0:9d17e4342598 | 78 | byte buffer[SHA_DIGEST_SIZE]; /* max size */ |
wolfSSL | 0:9d17e4342598 | 79 | |
wolfSSL | 0:9d17e4342598 | 80 | if (hashType != MD5 && hashType != SHA) |
wolfSSL | 0:9d17e4342598 | 81 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 82 | |
wolfSSL | 0:9d17e4342598 | 83 | if (kLen > hLen) |
wolfSSL | 0:9d17e4342598 | 84 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 85 | |
wolfSSL | 0:9d17e4342598 | 86 | if (iterations < 1) |
wolfSSL | 0:9d17e4342598 | 87 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 88 | |
wolfSSL | 0:9d17e4342598 | 89 | if (hashType == MD5) { |
wolfSSL | 0:9d17e4342598 | 90 | InitMd5(&md5); |
wolfSSL | 0:9d17e4342598 | 91 | Md5Update(&md5, passwd, pLen); |
wolfSSL | 0:9d17e4342598 | 92 | Md5Update(&md5, salt, sLen); |
wolfSSL | 0:9d17e4342598 | 93 | Md5Final(&md5, buffer); |
wolfSSL | 0:9d17e4342598 | 94 | } |
wolfSSL | 0:9d17e4342598 | 95 | else { |
wolfSSL | 0:9d17e4342598 | 96 | ret = InitSha(&sha); |
wolfSSL | 0:9d17e4342598 | 97 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 98 | return ret; |
wolfSSL | 0:9d17e4342598 | 99 | ShaUpdate(&sha, passwd, pLen); |
wolfSSL | 0:9d17e4342598 | 100 | ShaUpdate(&sha, salt, sLen); |
wolfSSL | 0:9d17e4342598 | 101 | ShaFinal(&sha, buffer); |
wolfSSL | 0:9d17e4342598 | 102 | } |
wolfSSL | 0:9d17e4342598 | 103 | |
wolfSSL | 0:9d17e4342598 | 104 | for (i = 1; i < iterations; i++) { |
wolfSSL | 0:9d17e4342598 | 105 | if (hashType == MD5) { |
wolfSSL | 0:9d17e4342598 | 106 | Md5Update(&md5, buffer, hLen); |
wolfSSL | 0:9d17e4342598 | 107 | Md5Final(&md5, buffer); |
wolfSSL | 0:9d17e4342598 | 108 | } |
wolfSSL | 0:9d17e4342598 | 109 | else { |
wolfSSL | 0:9d17e4342598 | 110 | ShaUpdate(&sha, buffer, hLen); |
wolfSSL | 0:9d17e4342598 | 111 | ShaFinal(&sha, buffer); |
wolfSSL | 0:9d17e4342598 | 112 | } |
wolfSSL | 0:9d17e4342598 | 113 | } |
wolfSSL | 0:9d17e4342598 | 114 | XMEMCPY(output, buffer, kLen); |
wolfSSL | 0:9d17e4342598 | 115 | |
wolfSSL | 0:9d17e4342598 | 116 | return 0; |
wolfSSL | 0:9d17e4342598 | 117 | } |
wolfSSL | 0:9d17e4342598 | 118 | |
wolfSSL | 0:9d17e4342598 | 119 | |
wolfSSL | 0:9d17e4342598 | 120 | int PBKDF2(byte* output, const byte* passwd, int pLen, const byte* salt, |
wolfSSL | 0:9d17e4342598 | 121 | int sLen, int iterations, int kLen, int hashType) |
wolfSSL | 0:9d17e4342598 | 122 | { |
wolfSSL | 0:9d17e4342598 | 123 | word32 i = 1; |
wolfSSL | 0:9d17e4342598 | 124 | int hLen; |
wolfSSL | 0:9d17e4342598 | 125 | int j, ret; |
wolfSSL | 0:9d17e4342598 | 126 | Hmac hmac; |
wolfSSL | 0:9d17e4342598 | 127 | byte buffer[MAX_DIGEST_SIZE]; |
wolfSSL | 0:9d17e4342598 | 128 | |
wolfSSL | 0:9d17e4342598 | 129 | if (hashType == MD5) { |
wolfSSL | 0:9d17e4342598 | 130 | hLen = MD5_DIGEST_SIZE; |
wolfSSL | 0:9d17e4342598 | 131 | } |
wolfSSL | 0:9d17e4342598 | 132 | else if (hashType == SHA) { |
wolfSSL | 0:9d17e4342598 | 133 | hLen = SHA_DIGEST_SIZE; |
wolfSSL | 0:9d17e4342598 | 134 | } |
wolfSSL | 0:9d17e4342598 | 135 | #ifndef NO_SHA256 |
wolfSSL | 0:9d17e4342598 | 136 | else if (hashType == SHA256) { |
wolfSSL | 0:9d17e4342598 | 137 | hLen = SHA256_DIGEST_SIZE; |
wolfSSL | 0:9d17e4342598 | 138 | } |
wolfSSL | 0:9d17e4342598 | 139 | #endif |
wolfSSL | 0:9d17e4342598 | 140 | #ifdef CYASSL_SHA512 |
wolfSSL | 0:9d17e4342598 | 141 | else if (hashType == SHA512) { |
wolfSSL | 0:9d17e4342598 | 142 | hLen = SHA512_DIGEST_SIZE; |
wolfSSL | 0:9d17e4342598 | 143 | } |
wolfSSL | 0:9d17e4342598 | 144 | #endif |
wolfSSL | 0:9d17e4342598 | 145 | else |
wolfSSL | 0:9d17e4342598 | 146 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 147 | |
wolfSSL | 0:9d17e4342598 | 148 | ret = HmacSetKey(&hmac, hashType, passwd, pLen); |
wolfSSL | 0:9d17e4342598 | 149 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 150 | return ret; |
wolfSSL | 0:9d17e4342598 | 151 | |
wolfSSL | 0:9d17e4342598 | 152 | while (kLen) { |
wolfSSL | 0:9d17e4342598 | 153 | int currentLen; |
wolfSSL | 0:9d17e4342598 | 154 | HmacUpdate(&hmac, salt, sLen); |
wolfSSL | 0:9d17e4342598 | 155 | |
wolfSSL | 0:9d17e4342598 | 156 | /* encode i */ |
wolfSSL | 0:9d17e4342598 | 157 | for (j = 0; j < 4; j++) { |
wolfSSL | 0:9d17e4342598 | 158 | byte b = (byte)(i >> ((3-j) * 8)); |
wolfSSL | 0:9d17e4342598 | 159 | HmacUpdate(&hmac, &b, 1); |
wolfSSL | 0:9d17e4342598 | 160 | } |
wolfSSL | 0:9d17e4342598 | 161 | HmacFinal(&hmac, buffer); |
wolfSSL | 0:9d17e4342598 | 162 | |
wolfSSL | 0:9d17e4342598 | 163 | currentLen = min(kLen, hLen); |
wolfSSL | 0:9d17e4342598 | 164 | XMEMCPY(output, buffer, currentLen); |
wolfSSL | 0:9d17e4342598 | 165 | |
wolfSSL | 0:9d17e4342598 | 166 | for (j = 1; j < iterations; j++) { |
wolfSSL | 0:9d17e4342598 | 167 | HmacUpdate(&hmac, buffer, hLen); |
wolfSSL | 0:9d17e4342598 | 168 | HmacFinal(&hmac, buffer); |
wolfSSL | 0:9d17e4342598 | 169 | xorbuf(output, buffer, currentLen); |
wolfSSL | 0:9d17e4342598 | 170 | } |
wolfSSL | 0:9d17e4342598 | 171 | |
wolfSSL | 0:9d17e4342598 | 172 | output += currentLen; |
wolfSSL | 0:9d17e4342598 | 173 | kLen -= currentLen; |
wolfSSL | 0:9d17e4342598 | 174 | i++; |
wolfSSL | 0:9d17e4342598 | 175 | } |
wolfSSL | 0:9d17e4342598 | 176 | |
wolfSSL | 0:9d17e4342598 | 177 | return 0; |
wolfSSL | 0:9d17e4342598 | 178 | } |
wolfSSL | 0:9d17e4342598 | 179 | |
wolfSSL | 0:9d17e4342598 | 180 | |
wolfSSL | 0:9d17e4342598 | 181 | int PKCS12_PBKDF(byte* output, const byte* passwd, int passLen,const byte* salt, |
wolfSSL | 0:9d17e4342598 | 182 | int saltLen, int iterations, int kLen, int hashType, int id) |
wolfSSL | 0:9d17e4342598 | 183 | { |
wolfSSL | 0:9d17e4342598 | 184 | /* all in bytes instead of bits */ |
wolfSSL | 0:9d17e4342598 | 185 | word32 u, v, dLen, pLen, iLen, sLen, totalLen; |
wolfSSL | 0:9d17e4342598 | 186 | int dynamic = 0; |
wolfSSL | 0:9d17e4342598 | 187 | int ret = 0; |
wolfSSL | 0:9d17e4342598 | 188 | int i; |
wolfSSL | 0:9d17e4342598 | 189 | byte *D, *S, *P, *I; |
wolfSSL | 0:9d17e4342598 | 190 | byte staticBuffer[1024]; |
wolfSSL | 0:9d17e4342598 | 191 | byte* buffer = staticBuffer; |
wolfSSL | 0:9d17e4342598 | 192 | #ifdef CYASSL_SHA512 |
wolfSSL | 0:9d17e4342598 | 193 | byte Ai[SHA512_DIGEST_SIZE]; |
wolfSSL | 0:9d17e4342598 | 194 | byte B[SHA512_BLOCK_SIZE]; |
wolfSSL | 0:9d17e4342598 | 195 | #elif !defined(NO_SHA256) |
wolfSSL | 0:9d17e4342598 | 196 | byte Ai[SHA256_DIGEST_SIZE]; |
wolfSSL | 0:9d17e4342598 | 197 | byte B[SHA256_BLOCK_SIZE]; |
wolfSSL | 0:9d17e4342598 | 198 | #else |
wolfSSL | 0:9d17e4342598 | 199 | byte Ai[SHA_DIGEST_SIZE]; |
wolfSSL | 0:9d17e4342598 | 200 | byte B[SHA_BLOCK_SIZE]; |
wolfSSL | 0:9d17e4342598 | 201 | #endif |
wolfSSL | 0:9d17e4342598 | 202 | |
wolfSSL | 0:9d17e4342598 | 203 | if (!iterations) |
wolfSSL | 0:9d17e4342598 | 204 | iterations = 1; |
wolfSSL | 0:9d17e4342598 | 205 | |
wolfSSL | 0:9d17e4342598 | 206 | if (hashType == MD5) { |
wolfSSL | 0:9d17e4342598 | 207 | v = MD5_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 208 | u = MD5_DIGEST_SIZE; |
wolfSSL | 0:9d17e4342598 | 209 | } |
wolfSSL | 0:9d17e4342598 | 210 | else if (hashType == SHA) { |
wolfSSL | 0:9d17e4342598 | 211 | v = SHA_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 212 | u = SHA_DIGEST_SIZE; |
wolfSSL | 0:9d17e4342598 | 213 | } |
wolfSSL | 0:9d17e4342598 | 214 | #ifndef NO_SHA256 |
wolfSSL | 0:9d17e4342598 | 215 | else if (hashType == SHA256) { |
wolfSSL | 0:9d17e4342598 | 216 | v = SHA256_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 217 | u = SHA256_DIGEST_SIZE; |
wolfSSL | 0:9d17e4342598 | 218 | } |
wolfSSL | 0:9d17e4342598 | 219 | #endif |
wolfSSL | 0:9d17e4342598 | 220 | #ifdef CYASSL_SHA512 |
wolfSSL | 0:9d17e4342598 | 221 | else if (hashType == SHA512) { |
wolfSSL | 0:9d17e4342598 | 222 | v = SHA512_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 223 | u = SHA512_DIGEST_SIZE; |
wolfSSL | 0:9d17e4342598 | 224 | } |
wolfSSL | 0:9d17e4342598 | 225 | #endif |
wolfSSL | 0:9d17e4342598 | 226 | else |
wolfSSL | 0:9d17e4342598 | 227 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 228 | |
wolfSSL | 0:9d17e4342598 | 229 | dLen = v; |
wolfSSL | 0:9d17e4342598 | 230 | sLen = v * ((saltLen + v - 1) / v); |
wolfSSL | 0:9d17e4342598 | 231 | if (passLen) |
wolfSSL | 0:9d17e4342598 | 232 | pLen = v * ((passLen + v - 1) / v); |
wolfSSL | 0:9d17e4342598 | 233 | else |
wolfSSL | 0:9d17e4342598 | 234 | pLen = 0; |
wolfSSL | 0:9d17e4342598 | 235 | iLen = sLen + pLen; |
wolfSSL | 0:9d17e4342598 | 236 | |
wolfSSL | 0:9d17e4342598 | 237 | totalLen = dLen + sLen + pLen; |
wolfSSL | 0:9d17e4342598 | 238 | |
wolfSSL | 0:9d17e4342598 | 239 | if (totalLen > sizeof(staticBuffer)) { |
wolfSSL | 0:9d17e4342598 | 240 | buffer = (byte*)XMALLOC(totalLen, 0, DYNAMIC_TYPE_KEY); |
wolfSSL | 0:9d17e4342598 | 241 | if (buffer == NULL) return MEMORY_E; |
wolfSSL | 0:9d17e4342598 | 242 | dynamic = 1; |
wolfSSL | 0:9d17e4342598 | 243 | } |
wolfSSL | 0:9d17e4342598 | 244 | |
wolfSSL | 0:9d17e4342598 | 245 | D = buffer; |
wolfSSL | 0:9d17e4342598 | 246 | S = D + dLen; |
wolfSSL | 0:9d17e4342598 | 247 | P = S + sLen; |
wolfSSL | 0:9d17e4342598 | 248 | I = S; |
wolfSSL | 0:9d17e4342598 | 249 | |
wolfSSL | 0:9d17e4342598 | 250 | XMEMSET(D, id, dLen); |
wolfSSL | 0:9d17e4342598 | 251 | |
wolfSSL | 0:9d17e4342598 | 252 | for (i = 0; i < (int)sLen; i++) |
wolfSSL | 0:9d17e4342598 | 253 | S[i] = salt[i % saltLen]; |
wolfSSL | 0:9d17e4342598 | 254 | for (i = 0; i < (int)pLen; i++) |
wolfSSL | 0:9d17e4342598 | 255 | P[i] = passwd[i % passLen]; |
wolfSSL | 0:9d17e4342598 | 256 | |
wolfSSL | 0:9d17e4342598 | 257 | while (kLen > 0) { |
wolfSSL | 0:9d17e4342598 | 258 | word32 currentLen; |
wolfSSL | 0:9d17e4342598 | 259 | mp_int B1; |
wolfSSL | 0:9d17e4342598 | 260 | |
wolfSSL | 0:9d17e4342598 | 261 | if (hashType == MD5) { |
wolfSSL | 0:9d17e4342598 | 262 | Md5 md5; |
wolfSSL | 0:9d17e4342598 | 263 | |
wolfSSL | 0:9d17e4342598 | 264 | InitMd5(&md5); |
wolfSSL | 0:9d17e4342598 | 265 | Md5Update(&md5, buffer, totalLen); |
wolfSSL | 0:9d17e4342598 | 266 | Md5Final(&md5, Ai); |
wolfSSL | 0:9d17e4342598 | 267 | |
wolfSSL | 0:9d17e4342598 | 268 | for (i = 1; i < iterations; i++) { |
wolfSSL | 0:9d17e4342598 | 269 | Md5Update(&md5, Ai, u); |
wolfSSL | 0:9d17e4342598 | 270 | Md5Final(&md5, Ai); |
wolfSSL | 0:9d17e4342598 | 271 | } |
wolfSSL | 0:9d17e4342598 | 272 | } |
wolfSSL | 0:9d17e4342598 | 273 | else if (hashType == SHA) { |
wolfSSL | 0:9d17e4342598 | 274 | Sha sha; |
wolfSSL | 0:9d17e4342598 | 275 | |
wolfSSL | 0:9d17e4342598 | 276 | ret = InitSha(&sha); |
wolfSSL | 0:9d17e4342598 | 277 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 278 | break; |
wolfSSL | 0:9d17e4342598 | 279 | ShaUpdate(&sha, buffer, totalLen); |
wolfSSL | 0:9d17e4342598 | 280 | ShaFinal(&sha, Ai); |
wolfSSL | 0:9d17e4342598 | 281 | |
wolfSSL | 0:9d17e4342598 | 282 | for (i = 1; i < iterations; i++) { |
wolfSSL | 0:9d17e4342598 | 283 | ShaUpdate(&sha, Ai, u); |
wolfSSL | 0:9d17e4342598 | 284 | ShaFinal(&sha, Ai); |
wolfSSL | 0:9d17e4342598 | 285 | } |
wolfSSL | 0:9d17e4342598 | 286 | } |
wolfSSL | 0:9d17e4342598 | 287 | #ifndef NO_SHA256 |
wolfSSL | 0:9d17e4342598 | 288 | else if (hashType == SHA256) { |
wolfSSL | 0:9d17e4342598 | 289 | Sha256 sha256; |
wolfSSL | 0:9d17e4342598 | 290 | |
wolfSSL | 0:9d17e4342598 | 291 | ret = InitSha256(&sha256); |
wolfSSL | 0:9d17e4342598 | 292 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 293 | break; |
wolfSSL | 0:9d17e4342598 | 294 | Sha256Update(&sha256, buffer, totalLen); |
wolfSSL | 0:9d17e4342598 | 295 | Sha256Final(&sha256, Ai); |
wolfSSL | 0:9d17e4342598 | 296 | |
wolfSSL | 0:9d17e4342598 | 297 | for (i = 1; i < iterations; i++) { |
wolfSSL | 0:9d17e4342598 | 298 | Sha256Update(&sha256, Ai, u); |
wolfSSL | 0:9d17e4342598 | 299 | Sha256Final(&sha256, Ai); |
wolfSSL | 0:9d17e4342598 | 300 | } |
wolfSSL | 0:9d17e4342598 | 301 | } |
wolfSSL | 0:9d17e4342598 | 302 | #endif |
wolfSSL | 0:9d17e4342598 | 303 | #ifdef CYASSL_SHA512 |
wolfSSL | 0:9d17e4342598 | 304 | else if (hashType == SHA512) { |
wolfSSL | 0:9d17e4342598 | 305 | Sha512 sha512; |
wolfSSL | 0:9d17e4342598 | 306 | |
wolfSSL | 0:9d17e4342598 | 307 | ret = InitSha512(&sha512); |
wolfSSL | 0:9d17e4342598 | 308 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 309 | break; |
wolfSSL | 0:9d17e4342598 | 310 | Sha512Update(&sha512, buffer, totalLen); |
wolfSSL | 0:9d17e4342598 | 311 | Sha512Final(&sha512, Ai); |
wolfSSL | 0:9d17e4342598 | 312 | |
wolfSSL | 0:9d17e4342598 | 313 | for (i = 1; i < iterations; i++) { |
wolfSSL | 0:9d17e4342598 | 314 | Sha512Update(&sha512, Ai, u); |
wolfSSL | 0:9d17e4342598 | 315 | Sha512Final(&sha512, Ai); |
wolfSSL | 0:9d17e4342598 | 316 | } |
wolfSSL | 0:9d17e4342598 | 317 | } |
wolfSSL | 0:9d17e4342598 | 318 | #endif |
wolfSSL | 0:9d17e4342598 | 319 | |
wolfSSL | 0:9d17e4342598 | 320 | for (i = 0; i < (int)v; i++) |
wolfSSL | 0:9d17e4342598 | 321 | B[i] = Ai[i % u]; |
wolfSSL | 0:9d17e4342598 | 322 | |
wolfSSL | 0:9d17e4342598 | 323 | if (mp_init(&B1) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 324 | ret = MP_INIT_E; |
wolfSSL | 0:9d17e4342598 | 325 | else if (mp_read_unsigned_bin(&B1, B, v) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 326 | ret = MP_READ_E; |
wolfSSL | 0:9d17e4342598 | 327 | else if (mp_add_d(&B1, (mp_digit)1, &B1) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 328 | ret = MP_ADD_E; |
wolfSSL | 0:9d17e4342598 | 329 | |
wolfSSL | 0:9d17e4342598 | 330 | if (ret != 0) { |
wolfSSL | 0:9d17e4342598 | 331 | mp_clear(&B1); |
wolfSSL | 0:9d17e4342598 | 332 | break; |
wolfSSL | 0:9d17e4342598 | 333 | } |
wolfSSL | 0:9d17e4342598 | 334 | |
wolfSSL | 0:9d17e4342598 | 335 | for (i = 0; i < (int)iLen; i += v) { |
wolfSSL | 0:9d17e4342598 | 336 | int outSz; |
wolfSSL | 0:9d17e4342598 | 337 | mp_int i1; |
wolfSSL | 0:9d17e4342598 | 338 | mp_int res; |
wolfSSL | 0:9d17e4342598 | 339 | |
wolfSSL | 0:9d17e4342598 | 340 | if (mp_init_multi(&i1, &res, NULL, NULL, NULL, NULL) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 341 | ret = MP_INIT_E; |
wolfSSL | 0:9d17e4342598 | 342 | break; |
wolfSSL | 0:9d17e4342598 | 343 | } |
wolfSSL | 0:9d17e4342598 | 344 | if (mp_read_unsigned_bin(&i1, I + i, v) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 345 | ret = MP_READ_E; |
wolfSSL | 0:9d17e4342598 | 346 | else if (mp_add(&i1, &B1, &res) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 347 | ret = MP_ADD_E; |
wolfSSL | 0:9d17e4342598 | 348 | else if ( (outSz = mp_unsigned_bin_size(&res)) < 0) |
wolfSSL | 0:9d17e4342598 | 349 | ret = MP_TO_E; |
wolfSSL | 0:9d17e4342598 | 350 | else { |
wolfSSL | 0:9d17e4342598 | 351 | if (outSz > (int)v) { |
wolfSSL | 0:9d17e4342598 | 352 | /* take off MSB */ |
wolfSSL | 0:9d17e4342598 | 353 | byte tmp[129]; |
wolfSSL | 0:9d17e4342598 | 354 | ret = mp_to_unsigned_bin(&res, tmp); |
wolfSSL | 0:9d17e4342598 | 355 | XMEMCPY(I + i, tmp + 1, v); |
wolfSSL | 0:9d17e4342598 | 356 | } |
wolfSSL | 0:9d17e4342598 | 357 | else if (outSz < (int)v) { |
wolfSSL | 0:9d17e4342598 | 358 | XMEMSET(I + i, 0, v - outSz); |
wolfSSL | 0:9d17e4342598 | 359 | ret = mp_to_unsigned_bin(&res, I + i + v - outSz); |
wolfSSL | 0:9d17e4342598 | 360 | } |
wolfSSL | 0:9d17e4342598 | 361 | else |
wolfSSL | 0:9d17e4342598 | 362 | ret = mp_to_unsigned_bin(&res, I + i); |
wolfSSL | 0:9d17e4342598 | 363 | } |
wolfSSL | 0:9d17e4342598 | 364 | |
wolfSSL | 0:9d17e4342598 | 365 | mp_clear(&i1); |
wolfSSL | 0:9d17e4342598 | 366 | mp_clear(&res); |
wolfSSL | 0:9d17e4342598 | 367 | if (ret < 0) break; |
wolfSSL | 0:9d17e4342598 | 368 | } |
wolfSSL | 0:9d17e4342598 | 369 | |
wolfSSL | 0:9d17e4342598 | 370 | currentLen = min(kLen, (int)u); |
wolfSSL | 0:9d17e4342598 | 371 | XMEMCPY(output, Ai, currentLen); |
wolfSSL | 0:9d17e4342598 | 372 | output += currentLen; |
wolfSSL | 0:9d17e4342598 | 373 | kLen -= currentLen; |
wolfSSL | 0:9d17e4342598 | 374 | mp_clear(&B1); |
wolfSSL | 0:9d17e4342598 | 375 | } |
wolfSSL | 0:9d17e4342598 | 376 | |
wolfSSL | 0:9d17e4342598 | 377 | if (dynamic) XFREE(buffer, 0, DYNAMIC_TYPE_KEY); |
wolfSSL | 0:9d17e4342598 | 378 | return ret; |
wolfSSL | 0:9d17e4342598 | 379 | } |
wolfSSL | 0:9d17e4342598 | 380 | |
wolfSSL | 0:9d17e4342598 | 381 | #endif /* NO_PWDBASED */ |
wolfSSL | 0:9d17e4342598 | 382 |