This is a port of cyaSSL 2.7.0.

Dependents:   CyaSSL_DTLS_Cellular CyaSSL_DTLS_Ethernet

Committer:
ashleymills
Date:
Thu Sep 05 15:55:50 2013 +0000
Revision:
1:c0ce1562443a
Parent:
0:714293de3836
Nothing;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ashleymills 0:714293de3836 1 /* coding.c
ashleymills 0:714293de3836 2 *
ashleymills 0:714293de3836 3 * Copyright (C) 2006-2013 wolfSSL Inc.
ashleymills 0:714293de3836 4 *
ashleymills 0:714293de3836 5 * This file is part of CyaSSL.
ashleymills 0:714293de3836 6 *
ashleymills 0:714293de3836 7 * CyaSSL is free software; you can redistribute it and/or modify
ashleymills 0:714293de3836 8 * it under the terms of the GNU General Public License as published by
ashleymills 0:714293de3836 9 * the Free Software Foundation; either version 2 of the License, or
ashleymills 0:714293de3836 10 * (at your option) any later version.
ashleymills 0:714293de3836 11 *
ashleymills 0:714293de3836 12 * CyaSSL is distributed in the hope that it will be useful,
ashleymills 0:714293de3836 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ashleymills 0:714293de3836 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ashleymills 0:714293de3836 15 * GNU General Public License for more details.
ashleymills 0:714293de3836 16 *
ashleymills 0:714293de3836 17 * You should have received a copy of the GNU General Public License
ashleymills 0:714293de3836 18 * along with this program; if not, write to the Free Software
ashleymills 0:714293de3836 19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
ashleymills 0:714293de3836 20 */
ashleymills 0:714293de3836 21
ashleymills 0:714293de3836 22 #ifdef HAVE_CONFIG_H
ashleymills 0:714293de3836 23 #include <config.h>
ashleymills 0:714293de3836 24 #endif
ashleymills 0:714293de3836 25
ashleymills 0:714293de3836 26 #include <cyassl/ctaocrypt/settings.h>
ashleymills 0:714293de3836 27
ashleymills 0:714293de3836 28 #ifndef NO_CODING
ashleymills 0:714293de3836 29
ashleymills 0:714293de3836 30 #include <cyassl/ctaocrypt/coding.h>
ashleymills 0:714293de3836 31 #include <cyassl/ctaocrypt/ctaoerror2.h>
ashleymills 0:714293de3836 32 #include <cyassl/ctaocrypt/logging.h>
ashleymills 0:714293de3836 33
ashleymills 0:714293de3836 34
ashleymills 0:714293de3836 35 enum {
ashleymills 0:714293de3836 36 BAD = 0xFF, /* invalid encoding */
ashleymills 0:714293de3836 37 PAD = '=',
ashleymills 0:714293de3836 38 PEM_LINE_SZ = 64
ashleymills 0:714293de3836 39 };
ashleymills 0:714293de3836 40
ashleymills 0:714293de3836 41
ashleymills 0:714293de3836 42 static
ashleymills 0:714293de3836 43 const byte base64Decode[] = { 62, BAD, BAD, BAD, 63, /* + starts at 0x2B */
ashleymills 0:714293de3836 44 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
ashleymills 0:714293de3836 45 BAD, BAD, BAD, BAD, BAD, BAD, BAD,
ashleymills 0:714293de3836 46 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
ashleymills 0:714293de3836 47 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
ashleymills 0:714293de3836 48 20, 21, 22, 23, 24, 25,
ashleymills 0:714293de3836 49 BAD, BAD, BAD, BAD, BAD, BAD,
ashleymills 0:714293de3836 50 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
ashleymills 0:714293de3836 51 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
ashleymills 0:714293de3836 52 46, 47, 48, 49, 50, 51
ashleymills 0:714293de3836 53 };
ashleymills 0:714293de3836 54
ashleymills 0:714293de3836 55
ashleymills 0:714293de3836 56 int Base64_Decode(const byte* in, word32 inLen, byte* out, word32* outLen)
ashleymills 0:714293de3836 57 {
ashleymills 0:714293de3836 58 word32 i = 0;
ashleymills 0:714293de3836 59 word32 j = 0;
ashleymills 0:714293de3836 60 word32 plainSz = inLen - ((inLen + (PEM_LINE_SZ - 1)) / PEM_LINE_SZ );
ashleymills 0:714293de3836 61 const byte maxIdx = (byte)sizeof(base64Decode) + 0x2B - 1;
ashleymills 0:714293de3836 62
ashleymills 0:714293de3836 63 plainSz = (plainSz * 3 + 3) / 4;
ashleymills 0:714293de3836 64 if (plainSz > *outLen) return BAD_FUNC_ARG;
ashleymills 0:714293de3836 65
ashleymills 0:714293de3836 66 while (inLen > 3) {
ashleymills 0:714293de3836 67 byte b1, b2, b3;
ashleymills 0:714293de3836 68 byte e1 = in[j++];
ashleymills 0:714293de3836 69 byte e2 = in[j++];
ashleymills 0:714293de3836 70 byte e3 = in[j++];
ashleymills 0:714293de3836 71 byte e4 = in[j++];
ashleymills 0:714293de3836 72
ashleymills 0:714293de3836 73 int pad3 = 0;
ashleymills 0:714293de3836 74 int pad4 = 0;
ashleymills 0:714293de3836 75
ashleymills 0:714293de3836 76 if (e1 == 0) /* end file 0's */
ashleymills 0:714293de3836 77 break;
ashleymills 0:714293de3836 78 if (e3 == PAD)
ashleymills 0:714293de3836 79 pad3 = 1;
ashleymills 0:714293de3836 80 if (e4 == PAD)
ashleymills 0:714293de3836 81 pad4 = 1;
ashleymills 0:714293de3836 82
ashleymills 0:714293de3836 83 if (e1 < 0x2B || e2 < 0x2B || e3 < 0x2B || e4 < 0x2B) {
ashleymills 0:714293de3836 84 CYASSL_MSG("Bad Base64 Decode data, too small");
ashleymills 0:714293de3836 85 return ASN_INPUT_E;
ashleymills 0:714293de3836 86 }
ashleymills 0:714293de3836 87
ashleymills 0:714293de3836 88 if (e1 > maxIdx || e2 > maxIdx || e3 > maxIdx || e4 > maxIdx) {
ashleymills 0:714293de3836 89 CYASSL_MSG("Bad Base64 Decode data, too big");
ashleymills 0:714293de3836 90 return ASN_INPUT_E;
ashleymills 0:714293de3836 91 }
ashleymills 0:714293de3836 92
ashleymills 0:714293de3836 93 e1 = base64Decode[e1 - 0x2B];
ashleymills 0:714293de3836 94 e2 = base64Decode[e2 - 0x2B];
ashleymills 0:714293de3836 95 e3 = (e3 == PAD) ? 0 : base64Decode[e3 - 0x2B];
ashleymills 0:714293de3836 96 e4 = (e4 == PAD) ? 0 : base64Decode[e4 - 0x2B];
ashleymills 0:714293de3836 97
ashleymills 0:714293de3836 98 b1 = (e1 << 2) | (e2 >> 4);
ashleymills 0:714293de3836 99 b2 = ((e2 & 0xF) << 4) | (e3 >> 2);
ashleymills 0:714293de3836 100 b3 = ((e3 & 0x3) << 6) | e4;
ashleymills 0:714293de3836 101
ashleymills 0:714293de3836 102 out[i++] = b1;
ashleymills 0:714293de3836 103 if (!pad3)
ashleymills 0:714293de3836 104 out[i++] = b2;
ashleymills 0:714293de3836 105 if (!pad4)
ashleymills 0:714293de3836 106 out[i++] = b3;
ashleymills 0:714293de3836 107 else
ashleymills 0:714293de3836 108 break;
ashleymills 0:714293de3836 109
ashleymills 0:714293de3836 110 inLen -= 4;
ashleymills 0:714293de3836 111 if (inLen && (in[j] == ' ' || in[j] == '\r' || in[j] == '\n')) {
ashleymills 0:714293de3836 112 byte endLine = in[j++];
ashleymills 0:714293de3836 113 inLen--;
ashleymills 0:714293de3836 114 while (inLen && endLine == ' ') { /* allow trailing whitespace */
ashleymills 0:714293de3836 115 endLine = in[j++];
ashleymills 0:714293de3836 116 inLen--;
ashleymills 0:714293de3836 117 }
ashleymills 0:714293de3836 118 if (endLine == '\r') {
ashleymills 0:714293de3836 119 if (inLen) {
ashleymills 0:714293de3836 120 endLine = in[j++];
ashleymills 0:714293de3836 121 inLen--;
ashleymills 0:714293de3836 122 }
ashleymills 0:714293de3836 123 }
ashleymills 0:714293de3836 124 if (endLine != '\n') {
ashleymills 0:714293de3836 125 CYASSL_MSG("Bad end of line in Base64 Decode");
ashleymills 0:714293de3836 126 return ASN_INPUT_E;
ashleymills 0:714293de3836 127 }
ashleymills 0:714293de3836 128 }
ashleymills 0:714293de3836 129 }
ashleymills 0:714293de3836 130 *outLen = i;
ashleymills 0:714293de3836 131
ashleymills 0:714293de3836 132 return 0;
ashleymills 0:714293de3836 133 }
ashleymills 0:714293de3836 134
ashleymills 0:714293de3836 135
ashleymills 0:714293de3836 136 #if defined(OPENSSL_EXTRA) || defined (SESSION_CERTS) || defined(CYASSL_KEY_GEN) || defined(CYASSL_CERT_GEN) || defined(HAVE_WEBSERVER)
ashleymills 0:714293de3836 137
ashleymills 0:714293de3836 138 static
ashleymills 0:714293de3836 139 const byte base64Encode[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
ashleymills 0:714293de3836 140 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
ashleymills 0:714293de3836 141 'U', 'V', 'W', 'X', 'Y', 'Z',
ashleymills 0:714293de3836 142 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
ashleymills 0:714293de3836 143 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
ashleymills 0:714293de3836 144 'u', 'v', 'w', 'x', 'y', 'z',
ashleymills 0:714293de3836 145 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
ashleymills 0:714293de3836 146 '+', '/'
ashleymills 0:714293de3836 147 };
ashleymills 0:714293de3836 148
ashleymills 0:714293de3836 149
ashleymills 0:714293de3836 150 /* porting assistance from yaSSL by Raphael HUCK */
ashleymills 0:714293de3836 151 int Base64_Encode(const byte* in, word32 inLen, byte* out, word32* outLen)
ashleymills 0:714293de3836 152 {
ashleymills 0:714293de3836 153 word32 i = 0,
ashleymills 0:714293de3836 154 j = 0,
ashleymills 0:714293de3836 155 n = 0; /* new line counter */
ashleymills 0:714293de3836 156
ashleymills 0:714293de3836 157 word32 outSz = (inLen + 3 - 1) / 3 * 4;
ashleymills 0:714293de3836 158 outSz += (outSz + PEM_LINE_SZ - 1) / PEM_LINE_SZ; /* new lines */
ashleymills 0:714293de3836 159
ashleymills 0:714293de3836 160 if (outSz > *outLen) return BAD_FUNC_ARG;
ashleymills 0:714293de3836 161
ashleymills 0:714293de3836 162 while (inLen > 2) {
ashleymills 0:714293de3836 163 byte b1 = in[j++];
ashleymills 0:714293de3836 164 byte b2 = in[j++];
ashleymills 0:714293de3836 165 byte b3 = in[j++];
ashleymills 0:714293de3836 166
ashleymills 0:714293de3836 167 /* encoded idx */
ashleymills 0:714293de3836 168 byte e1 = b1 >> 2;
ashleymills 0:714293de3836 169 byte e2 = ((b1 & 0x3) << 4) | (b2 >> 4);
ashleymills 0:714293de3836 170 byte e3 = ((b2 & 0xF) << 2) | (b3 >> 6);
ashleymills 0:714293de3836 171 byte e4 = b3 & 0x3F;
ashleymills 0:714293de3836 172
ashleymills 0:714293de3836 173 /* store */
ashleymills 0:714293de3836 174 out[i++] = base64Encode[e1];
ashleymills 0:714293de3836 175 out[i++] = base64Encode[e2];
ashleymills 0:714293de3836 176 out[i++] = base64Encode[e3];
ashleymills 0:714293de3836 177 out[i++] = base64Encode[e4];
ashleymills 0:714293de3836 178
ashleymills 0:714293de3836 179 inLen -= 3;
ashleymills 0:714293de3836 180
ashleymills 0:714293de3836 181 if ((++n % (PEM_LINE_SZ / 4)) == 0 && inLen)
ashleymills 0:714293de3836 182 out[i++] = '\n';
ashleymills 0:714293de3836 183 }
ashleymills 0:714293de3836 184
ashleymills 0:714293de3836 185 /* last integral */
ashleymills 0:714293de3836 186 if (inLen) {
ashleymills 0:714293de3836 187 int twoBytes = (inLen == 2);
ashleymills 0:714293de3836 188
ashleymills 0:714293de3836 189 byte b1 = in[j++];
ashleymills 0:714293de3836 190 byte b2 = (twoBytes) ? in[j++] : 0;
ashleymills 0:714293de3836 191
ashleymills 0:714293de3836 192 byte e1 = b1 >> 2;
ashleymills 0:714293de3836 193 byte e2 = ((b1 & 0x3) << 4) | (b2 >> 4);
ashleymills 0:714293de3836 194 byte e3 = (b2 & 0xF) << 2;
ashleymills 0:714293de3836 195
ashleymills 0:714293de3836 196 out[i++] = base64Encode[e1];
ashleymills 0:714293de3836 197 out[i++] = base64Encode[e2];
ashleymills 0:714293de3836 198 out[i++] = (twoBytes) ? base64Encode[e3] : PAD;
ashleymills 0:714293de3836 199 out[i++] = PAD;
ashleymills 0:714293de3836 200 }
ashleymills 0:714293de3836 201
ashleymills 0:714293de3836 202 out[i++] = '\n';
ashleymills 0:714293de3836 203 if (i != outSz)
ashleymills 0:714293de3836 204 return ASN_INPUT_E;
ashleymills 0:714293de3836 205 *outLen = outSz;
ashleymills 0:714293de3836 206
ashleymills 0:714293de3836 207 return 0;
ashleymills 0:714293de3836 208 }
ashleymills 0:714293de3836 209
ashleymills 0:714293de3836 210
ashleymills 0:714293de3836 211 static
ashleymills 0:714293de3836 212 const byte hexDecode[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
ashleymills 0:714293de3836 213 BAD, BAD, BAD, BAD, BAD, BAD, BAD,
ashleymills 0:714293de3836 214 10, 11, 12, 13, 14, 15
ashleymills 0:714293de3836 215 }; /* A starts at 0x41 not 0x3A */
ashleymills 0:714293de3836 216
ashleymills 0:714293de3836 217 int Base16_Decode(const byte* in, word32 inLen, byte* out, word32* outLen)
ashleymills 0:714293de3836 218 {
ashleymills 0:714293de3836 219 word32 inIdx = 0;
ashleymills 0:714293de3836 220 word32 outIdx = 0;
ashleymills 0:714293de3836 221
ashleymills 0:714293de3836 222 if (inLen == 1 && *outLen && in) {
ashleymills 0:714293de3836 223 byte b = in[inIdx++] - 0x30; /* 0 starts at 0x30 */
ashleymills 0:714293de3836 224
ashleymills 0:714293de3836 225 /* sanity check */
ashleymills 0:714293de3836 226 if (b >= sizeof(hexDecode)/sizeof(hexDecode[0]))
ashleymills 0:714293de3836 227 return ASN_INPUT_E;
ashleymills 0:714293de3836 228
ashleymills 0:714293de3836 229 b = hexDecode[b];
ashleymills 0:714293de3836 230
ashleymills 0:714293de3836 231 if (b == BAD)
ashleymills 0:714293de3836 232 return ASN_INPUT_E;
ashleymills 0:714293de3836 233
ashleymills 0:714293de3836 234 out[outIdx++] = b;
ashleymills 0:714293de3836 235
ashleymills 0:714293de3836 236 *outLen = outIdx;
ashleymills 0:714293de3836 237 return 0;
ashleymills 0:714293de3836 238 }
ashleymills 0:714293de3836 239
ashleymills 0:714293de3836 240 if (inLen % 2)
ashleymills 0:714293de3836 241 return BAD_FUNC_ARG;
ashleymills 0:714293de3836 242
ashleymills 0:714293de3836 243 if (*outLen < (inLen / 2))
ashleymills 0:714293de3836 244 return BAD_FUNC_ARG;
ashleymills 0:714293de3836 245
ashleymills 0:714293de3836 246 while (inLen) {
ashleymills 0:714293de3836 247 byte b = in[inIdx++] - 0x30; /* 0 starts at 0x30 */
ashleymills 0:714293de3836 248 byte b2 = in[inIdx++] - 0x30;
ashleymills 0:714293de3836 249
ashleymills 0:714293de3836 250 /* sanity checks */
ashleymills 0:714293de3836 251 if (b >= sizeof(hexDecode)/sizeof(hexDecode[0]))
ashleymills 0:714293de3836 252 return ASN_INPUT_E;
ashleymills 0:714293de3836 253 if (b2 >= sizeof(hexDecode)/sizeof(hexDecode[0]))
ashleymills 0:714293de3836 254 return ASN_INPUT_E;
ashleymills 0:714293de3836 255
ashleymills 0:714293de3836 256 b = hexDecode[b];
ashleymills 0:714293de3836 257 b2 = hexDecode[b2];
ashleymills 0:714293de3836 258
ashleymills 0:714293de3836 259 if (b == BAD || b2 == BAD)
ashleymills 0:714293de3836 260 return ASN_INPUT_E;
ashleymills 0:714293de3836 261
ashleymills 0:714293de3836 262 out[outIdx++] = (b << 4) | b2;
ashleymills 0:714293de3836 263 inLen -= 2;
ashleymills 0:714293de3836 264 }
ashleymills 0:714293de3836 265
ashleymills 0:714293de3836 266 *outLen = outIdx;
ashleymills 0:714293de3836 267 return 0;
ashleymills 0:714293de3836 268 }
ashleymills 0:714293de3836 269
ashleymills 0:714293de3836 270
ashleymills 0:714293de3836 271 #endif /* defined(OPENSSL_EXTRA) || defined (SESSION_CERTS) || defined(CYASSL_KEY_GEN) || defined(CYASSL_CERT_GEN) || defined(HAVE_WEBSERVER) */
ashleymills 0:714293de3836 272 #endif /* NO_CODING */