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