- Timestamp:
- Jun 22, 2021, 9:00:19 PM (3 years ago)
- Location:
- azure_iot_hub_f767zi/trunk/wolfssl-4.7.0
- Files:
-
- 1 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
azure_iot_hub_f767zi/trunk/wolfssl-4.7.0/wolfcrypt/src/coding.c
r457 r464 32 32 #include <wolfssl/wolfcrypt/error-crypt.h> 33 33 #include <wolfssl/wolfcrypt/logging.h> 34 34 #ifndef NO_ASN 35 #include <wolfssl/wolfcrypt/asn.h> /* For PEM_LINE_SZ */ 36 #endif 37 #ifdef NO_INLINE 38 #include <wolfssl/wolfcrypt/misc.h> 39 #else 40 #define WOLFSSL_MISC_INCLUDED 41 #include <wolfcrypt/src/misc.c> 42 #endif 35 43 36 44 enum { 37 45 BAD = 0xFF, /* invalid encoding */ 38 46 PAD = '=', 39 PEM_LINE_SZ = 64,40 47 BASE64_MIN = 0x2B, 41 48 BASE16_MIN = 0x30, … … 43 50 44 51 52 #ifndef BASE64_LINE_SZ 53 #ifdef NO_ASN 54 #define BASE64_LINE_SZ 64 55 #else 56 #define BASE64_LINE_SZ PEM_LINE_SZ 57 #endif 58 #endif 59 45 60 #ifdef WOLFSSL_BASE64_DECODE 46 61 62 #ifdef BASE64_NO_TABLE 63 static WC_INLINE byte Base64_Char2Val(byte c) 64 { 65 word16 v = 0x0000; 66 67 v |= 0xff3E & ctMask16Eq(c, 0x2b); 68 v |= 0xff3F & ctMask16Eq(c, 0x2f); 69 v |= (c + 0xff04) & ctMask16GTE(c, 0x30) & ctMask16LTE(c, 0x39); 70 v |= (0xff00 + c - 0x41) & ctMask16GTE(c, 0x41) & ctMask16LTE(c, 0x5a); 71 v |= (0xff00 + c - 0x47) & ctMask16GTE(c, 0x61) & ctMask16LTE(c, 0x7a); 72 v |= ~(v >> 8); 73 74 return (byte)v; 75 } 76 #else 47 77 static 48 const byte base64Decode[] = { 62, BAD, BAD, BAD, 63, /* + starts at 0x2B */ 49 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 50 BAD, BAD, BAD, BAD, BAD, BAD, BAD, 51 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 52 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 53 20, 21, 22, 23, 24, 25, 54 BAD, BAD, BAD, BAD, BAD, BAD, 55 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 56 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 57 46, 47, 48, 49, 50, 51 78 ALIGN64 const byte base64Decode[] = { /* + starts at 0x2B */ 79 /* 0x28: + , - . / */ 62, BAD, BAD, BAD, 63, 80 /* 0x30: 0 1 2 3 4 5 6 7 */ 52, 53, 54, 55, 56, 57, 58, 59, 81 /* 0x38: 8 9 : ; < = > ? */ 60, 61, BAD, BAD, BAD, BAD, BAD, BAD, 82 /* 0x40: @ A B C D E F G */ BAD, 0, 1, 2, 3, 4, 5, 6, 83 /* 0x48: H I J K L M N O */ 7, 8, 9, 10, 11, 12, 13, 14, 84 /* 0x50: P Q R S T U V W */ 15, 16, 17, 18, 19, 20, 21, 22, 85 /* 0x58: X Y Z [ \ ] ^ _ */ 23, 24, 25, BAD, BAD, BAD, BAD, BAD, 86 /* 0x60: ` a b c d e f g */ BAD, 26, 27, 28, 29, 30, 31, 32, 87 /* 0x68: h i j k l m n o */ 33, 34, 35, 36, 37, 38, 39, 40, 88 /* 0x70: p q r s t u v w */ 41, 42, 43, 44, 45, 46, 47, 48, 89 /* 0x78: x y z */ 49, 50, 51 58 90 }; 91 #define BASE64DECODE_SZ (byte)(sizeof(base64Decode)) 92 93 static WC_INLINE byte Base64_Char2Val(byte c) 94 { 95 #ifndef WC_NO_CACHE_RESISTANT 96 /* 80 characters in table. 97 * 64 bytes in a cache line - first line has 64, second has 16 98 */ 99 byte v; 100 byte mask; 101 102 c -= BASE64_MIN; 103 mask = (((byte)(0x3f - c)) >> 7) - 1; 104 /* Load a value from the first cache line and use when mask set. */ 105 v = base64Decode[ c & 0x3f ] & mask ; 106 /* Load a value from the second cache line and use when mask not set. */ 107 v |= base64Decode[(c & 0x0f) | 0x40] & (~mask); 108 109 return v; 110 #else 111 return base64Decode[c - BASE64_MIN]; 112 #endif 113 } 114 #endif 59 115 60 116 static WC_INLINE int Base64_SkipNewline(const byte* in, word32 *inLen, word32 *outJ) … … 92 148 word32 i = 0; 93 149 word32 j = 0; 94 word32 plainSz = inLen - ((inLen + ( PEM_LINE_SZ - 1)) / PEM_LINE_SZ );150 word32 plainSz = inLen - ((inLen + (BASE64_LINE_SZ - 1)) / BASE64_LINE_SZ ); 95 151 int ret; 96 const byte maxIdx = (byte)sizeof(base64Decode) + BASE64_MIN - 1; 152 #ifndef BASE64_NO_TABLE 153 const byte maxIdx = BASE64DECODE_SZ + BASE64_MIN - 1; 154 #endif 97 155 98 156 plainSz = (plainSz * 3 + 3) / 4; … … 102 160 int pad3 = 0; 103 161 int pad4 = 0; 104 105 162 byte b1, b2, b3; 106 163 byte e1, e2, e3, e4; 164 107 165 if ((ret = Base64_SkipNewline(in, &inLen, &j)) != 0) { 108 166 if (ret == BUFFER_E) { … … 133 191 inLen--; 134 192 135 if (e1 == 0) /* end file 0's */136 break;137 193 if (e3 == PAD) 138 194 pad3 = 1; … … 140 196 pad4 = 1; 141 197 142 if (e1 < BASE64_MIN || e2 < BASE64_MIN || e3 < BASE64_MIN || e4 < BASE64_MIN) { 198 if (pad3 && !pad4) 199 return ASN_INPUT_E; 200 201 #ifndef BASE64_NO_TABLE 202 if (e1 < BASE64_MIN || e2 < BASE64_MIN || e3 < BASE64_MIN || 203 e4 < BASE64_MIN) { 143 204 WOLFSSL_MSG("Bad Base64 Decode data, too small"); 144 205 return ASN_INPUT_E; … … 149 210 return ASN_INPUT_E; 150 211 } 212 #endif 151 213 152 214 if (i + 1 + !pad3 + !pad4 > *outLen) { … … 155 217 } 156 218 157 e1 = base64Decode[e1 - BASE64_MIN]; 158 e2 = base64Decode[e2 - BASE64_MIN]; 159 e3 = (e3 == PAD) ? 0 : base64Decode[e3 - BASE64_MIN]; 160 e4 = (e4 == PAD) ? 0 : base64Decode[e4 - BASE64_MIN]; 219 e1 = Base64_Char2Val(e1); 220 e2 = Base64_Char2Val(e2); 221 e3 = (e3 == PAD) ? 0 : Base64_Char2Val(e3); 222 e4 = (e4 == PAD) ? 0 : Base64_Char2Val(e4); 223 224 if (e1 == BAD || e2 == BAD || e3 == BAD || e4 == BAD) { 225 WOLFSSL_MSG("Bad Base64 Decode bad character"); 226 return ASN_INPUT_E; 227 } 161 228 162 229 b1 = (byte)((e1 << 2) | (e2 >> 4)); … … 199 266 /* make sure *i (idx) won't exceed max, store and possibly escape to out, 200 267 * raw means use e w/o decode, 0 on success */ 201 static int CEscape(int escaped, byte e, byte* out, word32* i, word32 max ,268 static int CEscape(int escaped, byte e, byte* out, word32* i, word32 maxSz, 202 269 int raw, int getSzOnly) 203 270 { … … 241 308 242 309 /* check size */ 243 if ( (idx+needed) > max && !getSzOnly) {310 if ( (idx+needed) > maxSz && !getSzOnly) { 244 311 WOLFSSL_MSG("Escape buffer max too small"); 245 312 return BUFFER_E; … … 292 359 293 360 word32 outSz = (inLen + 3 - 1) / 3 * 4; 294 word32 addSz = (outSz + PEM_LINE_SZ - 1) / PEM_LINE_SZ; /* new lines */361 word32 addSz = (outSz + BASE64_LINE_SZ - 1) / BASE64_LINE_SZ; /* new lines */ 295 362 296 363 if (escaped == WC_ESC_NL_ENC) … … 329 396 inLen -= 3; 330 397 331 /* Insert newline after PEM_LINE_SZ, unless no \n requested */332 if (escaped != WC_NO_NL_ENC && (++n % ( PEM_LINE_SZ/4)) == 0 && inLen) {398 /* Insert newline after BASE64_LINE_SZ, unless no \n requested */ 399 if (escaped != WC_NO_NL_ENC && (++n % (BASE64_LINE_SZ/4)) == 0 && inLen) { 333 400 ret = CEscape(escaped, '\n', out, &i, *outLen, 1, getSzOnly); 334 401 if (ret != 0) break;
Note:
See TracChangeset
for help on using the changeset viewer.