cyassl re-port with cellular comms, PSK test
Dependencies: VodafoneUSBModem_bleedingedge2 mbed-rtos mbed-src
cyassllib/ctaocrypt/src/sha256.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 | /* sha256.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 | |
ashleymills | 0:e979170e02e7 | 23 | /* code submitted by raphael.huck@efixo.com */ |
ashleymills | 0:e979170e02e7 | 24 | |
ashleymills | 0:e979170e02e7 | 25 | #ifdef HAVE_CONFIG_H |
ashleymills | 0:e979170e02e7 | 26 | #include <config.h> |
ashleymills | 0:e979170e02e7 | 27 | #endif |
ashleymills | 0:e979170e02e7 | 28 | |
ashleymills | 0:e979170e02e7 | 29 | #ifndef NO_SHA256 |
ashleymills | 0:e979170e02e7 | 30 | |
ashleymills | 0:e979170e02e7 | 31 | #include <cyassl/ctaocrypt/sha256.h> |
ashleymills | 0:e979170e02e7 | 32 | #ifdef NO_INLINE |
ashleymills | 0:e979170e02e7 | 33 | #include <cyassl/ctaocrypt/misc.h> |
ashleymills | 0:e979170e02e7 | 34 | #else |
ashleymills | 0:e979170e02e7 | 35 | #include <ctaocrypt/src/misc.c> |
ashleymills | 0:e979170e02e7 | 36 | #endif |
ashleymills | 0:e979170e02e7 | 37 | |
ashleymills | 0:e979170e02e7 | 38 | |
ashleymills | 0:e979170e02e7 | 39 | #ifndef min |
ashleymills | 0:e979170e02e7 | 40 | |
ashleymills | 0:e979170e02e7 | 41 | static INLINE word32 min(word32 a, word32 b) |
ashleymills | 0:e979170e02e7 | 42 | { |
ashleymills | 0:e979170e02e7 | 43 | return a > b ? b : a; |
ashleymills | 0:e979170e02e7 | 44 | } |
ashleymills | 0:e979170e02e7 | 45 | |
ashleymills | 0:e979170e02e7 | 46 | #endif /* min */ |
ashleymills | 0:e979170e02e7 | 47 | |
ashleymills | 0:e979170e02e7 | 48 | |
ashleymills | 0:e979170e02e7 | 49 | void InitSha256(Sha256* sha256) |
ashleymills | 0:e979170e02e7 | 50 | { |
ashleymills | 0:e979170e02e7 | 51 | sha256->digest[0] = 0x6A09E667L; |
ashleymills | 0:e979170e02e7 | 52 | sha256->digest[1] = 0xBB67AE85L; |
ashleymills | 0:e979170e02e7 | 53 | sha256->digest[2] = 0x3C6EF372L; |
ashleymills | 0:e979170e02e7 | 54 | sha256->digest[3] = 0xA54FF53AL; |
ashleymills | 0:e979170e02e7 | 55 | sha256->digest[4] = 0x510E527FL; |
ashleymills | 0:e979170e02e7 | 56 | sha256->digest[5] = 0x9B05688CL; |
ashleymills | 0:e979170e02e7 | 57 | sha256->digest[6] = 0x1F83D9ABL; |
ashleymills | 0:e979170e02e7 | 58 | sha256->digest[7] = 0x5BE0CD19L; |
ashleymills | 0:e979170e02e7 | 59 | |
ashleymills | 0:e979170e02e7 | 60 | sha256->buffLen = 0; |
ashleymills | 0:e979170e02e7 | 61 | sha256->loLen = 0; |
ashleymills | 0:e979170e02e7 | 62 | sha256->hiLen = 0; |
ashleymills | 0:e979170e02e7 | 63 | } |
ashleymills | 0:e979170e02e7 | 64 | |
ashleymills | 0:e979170e02e7 | 65 | static const word32 K[64] = { |
ashleymills | 0:e979170e02e7 | 66 | 0x428A2F98L, 0x71374491L, 0xB5C0FBCFL, 0xE9B5DBA5L, 0x3956C25BL, |
ashleymills | 0:e979170e02e7 | 67 | 0x59F111F1L, 0x923F82A4L, 0xAB1C5ED5L, 0xD807AA98L, 0x12835B01L, |
ashleymills | 0:e979170e02e7 | 68 | 0x243185BEL, 0x550C7DC3L, 0x72BE5D74L, 0x80DEB1FEL, 0x9BDC06A7L, |
ashleymills | 0:e979170e02e7 | 69 | 0xC19BF174L, 0xE49B69C1L, 0xEFBE4786L, 0x0FC19DC6L, 0x240CA1CCL, |
ashleymills | 0:e979170e02e7 | 70 | 0x2DE92C6FL, 0x4A7484AAL, 0x5CB0A9DCL, 0x76F988DAL, 0x983E5152L, |
ashleymills | 0:e979170e02e7 | 71 | 0xA831C66DL, 0xB00327C8L, 0xBF597FC7L, 0xC6E00BF3L, 0xD5A79147L, |
ashleymills | 0:e979170e02e7 | 72 | 0x06CA6351L, 0x14292967L, 0x27B70A85L, 0x2E1B2138L, 0x4D2C6DFCL, |
ashleymills | 0:e979170e02e7 | 73 | 0x53380D13L, 0x650A7354L, 0x766A0ABBL, 0x81C2C92EL, 0x92722C85L, |
ashleymills | 0:e979170e02e7 | 74 | 0xA2BFE8A1L, 0xA81A664BL, 0xC24B8B70L, 0xC76C51A3L, 0xD192E819L, |
ashleymills | 0:e979170e02e7 | 75 | 0xD6990624L, 0xF40E3585L, 0x106AA070L, 0x19A4C116L, 0x1E376C08L, |
ashleymills | 0:e979170e02e7 | 76 | 0x2748774CL, 0x34B0BCB5L, 0x391C0CB3L, 0x4ED8AA4AL, 0x5B9CCA4FL, |
ashleymills | 0:e979170e02e7 | 77 | 0x682E6FF3L, 0x748F82EEL, 0x78A5636FL, 0x84C87814L, 0x8CC70208L, |
ashleymills | 0:e979170e02e7 | 78 | 0x90BEFFFAL, 0xA4506CEBL, 0xBEF9A3F7L, 0xC67178F2L |
ashleymills | 0:e979170e02e7 | 79 | }; |
ashleymills | 0:e979170e02e7 | 80 | |
ashleymills | 0:e979170e02e7 | 81 | #define Ch(x,y,z) (z ^ (x & (y ^ z))) |
ashleymills | 0:e979170e02e7 | 82 | #define Maj(x,y,z) (((x | y) & z) | (x & y)) |
ashleymills | 0:e979170e02e7 | 83 | #define S(x, n) rotrFixed(x, n) |
ashleymills | 0:e979170e02e7 | 84 | #define R(x, n) (((x)&0xFFFFFFFFU)>>(n)) |
ashleymills | 0:e979170e02e7 | 85 | #define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22)) |
ashleymills | 0:e979170e02e7 | 86 | #define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25)) |
ashleymills | 0:e979170e02e7 | 87 | #define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3)) |
ashleymills | 0:e979170e02e7 | 88 | #define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10)) |
ashleymills | 0:e979170e02e7 | 89 | |
ashleymills | 0:e979170e02e7 | 90 | #define RND(a,b,c,d,e,f,g,h,i) \ |
ashleymills | 0:e979170e02e7 | 91 | t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \ |
ashleymills | 0:e979170e02e7 | 92 | t1 = Sigma0(a) + Maj(a, b, c); \ |
ashleymills | 0:e979170e02e7 | 93 | d += t0; \ |
ashleymills | 0:e979170e02e7 | 94 | h = t0 + t1; |
ashleymills | 0:e979170e02e7 | 95 | |
ashleymills | 0:e979170e02e7 | 96 | |
ashleymills | 0:e979170e02e7 | 97 | static void Transform(Sha256* sha256) |
ashleymills | 0:e979170e02e7 | 98 | { |
ashleymills | 0:e979170e02e7 | 99 | word32 S[8], W[64], t0, t1; |
ashleymills | 0:e979170e02e7 | 100 | int i; |
ashleymills | 0:e979170e02e7 | 101 | |
ashleymills | 0:e979170e02e7 | 102 | /* Copy context->state[] to working vars */ |
ashleymills | 0:e979170e02e7 | 103 | for (i = 0; i < 8; i++) |
ashleymills | 0:e979170e02e7 | 104 | S[i] = sha256->digest[i]; |
ashleymills | 0:e979170e02e7 | 105 | |
ashleymills | 0:e979170e02e7 | 106 | for (i = 0; i < 16; i++) |
ashleymills | 0:e979170e02e7 | 107 | W[i] = sha256->buffer[i]; |
ashleymills | 0:e979170e02e7 | 108 | |
ashleymills | 0:e979170e02e7 | 109 | for (i = 16; i < 64; i++) |
ashleymills | 0:e979170e02e7 | 110 | W[i] = Gamma1(W[i-2]) + W[i-7] + Gamma0(W[i-15]) + W[i-16]; |
ashleymills | 0:e979170e02e7 | 111 | |
ashleymills | 0:e979170e02e7 | 112 | for (i = 0; i < 64; i += 8) { |
ashleymills | 0:e979170e02e7 | 113 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i+0); |
ashleymills | 0:e979170e02e7 | 114 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],i+1); |
ashleymills | 0:e979170e02e7 | 115 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],i+2); |
ashleymills | 0:e979170e02e7 | 116 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],i+3); |
ashleymills | 0:e979170e02e7 | 117 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],i+4); |
ashleymills | 0:e979170e02e7 | 118 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],i+5); |
ashleymills | 0:e979170e02e7 | 119 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],i+6); |
ashleymills | 0:e979170e02e7 | 120 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],i+7); |
ashleymills | 0:e979170e02e7 | 121 | } |
ashleymills | 0:e979170e02e7 | 122 | |
ashleymills | 0:e979170e02e7 | 123 | /* Add the working vars back into digest state[] */ |
ashleymills | 0:e979170e02e7 | 124 | for (i = 0; i < 8; i++) { |
ashleymills | 0:e979170e02e7 | 125 | sha256->digest[i] += S[i]; |
ashleymills | 0:e979170e02e7 | 126 | } |
ashleymills | 0:e979170e02e7 | 127 | } |
ashleymills | 0:e979170e02e7 | 128 | |
ashleymills | 0:e979170e02e7 | 129 | |
ashleymills | 0:e979170e02e7 | 130 | static INLINE void AddLength(Sha256* sha256, word32 len) |
ashleymills | 0:e979170e02e7 | 131 | { |
ashleymills | 0:e979170e02e7 | 132 | word32 tmp = sha256->loLen; |
ashleymills | 0:e979170e02e7 | 133 | if ( (sha256->loLen += len) < tmp) |
ashleymills | 0:e979170e02e7 | 134 | sha256->hiLen++; /* carry low to high */ |
ashleymills | 0:e979170e02e7 | 135 | } |
ashleymills | 0:e979170e02e7 | 136 | |
ashleymills | 0:e979170e02e7 | 137 | |
ashleymills | 0:e979170e02e7 | 138 | void Sha256Update(Sha256* sha256, const byte* data, word32 len) |
ashleymills | 0:e979170e02e7 | 139 | { |
ashleymills | 0:e979170e02e7 | 140 | /* do block size increments */ |
ashleymills | 0:e979170e02e7 | 141 | byte* local = (byte*)sha256->buffer; |
ashleymills | 0:e979170e02e7 | 142 | |
ashleymills | 0:e979170e02e7 | 143 | while (len) { |
ashleymills | 0:e979170e02e7 | 144 | word32 add = min(len, SHA256_BLOCK_SIZE - sha256->buffLen); |
ashleymills | 0:e979170e02e7 | 145 | XMEMCPY(&local[sha256->buffLen], data, add); |
ashleymills | 0:e979170e02e7 | 146 | |
ashleymills | 0:e979170e02e7 | 147 | sha256->buffLen += add; |
ashleymills | 0:e979170e02e7 | 148 | data += add; |
ashleymills | 0:e979170e02e7 | 149 | len -= add; |
ashleymills | 0:e979170e02e7 | 150 | |
ashleymills | 0:e979170e02e7 | 151 | if (sha256->buffLen == SHA256_BLOCK_SIZE) { |
ashleymills | 0:e979170e02e7 | 152 | #ifdef LITTLE_ENDIAN_ORDER |
ashleymills | 0:e979170e02e7 | 153 | ByteReverseBytes(local, local, SHA256_BLOCK_SIZE); |
ashleymills | 0:e979170e02e7 | 154 | #endif |
ashleymills | 0:e979170e02e7 | 155 | Transform(sha256); |
ashleymills | 0:e979170e02e7 | 156 | AddLength(sha256, SHA256_BLOCK_SIZE); |
ashleymills | 0:e979170e02e7 | 157 | sha256->buffLen = 0; |
ashleymills | 0:e979170e02e7 | 158 | } |
ashleymills | 0:e979170e02e7 | 159 | } |
ashleymills | 0:e979170e02e7 | 160 | } |
ashleymills | 0:e979170e02e7 | 161 | |
ashleymills | 0:e979170e02e7 | 162 | |
ashleymills | 0:e979170e02e7 | 163 | void Sha256Final(Sha256* sha256, byte* hash) |
ashleymills | 0:e979170e02e7 | 164 | { |
ashleymills | 0:e979170e02e7 | 165 | byte* local = (byte*)sha256->buffer; |
ashleymills | 0:e979170e02e7 | 166 | |
ashleymills | 0:e979170e02e7 | 167 | AddLength(sha256, sha256->buffLen); /* before adding pads */ |
ashleymills | 0:e979170e02e7 | 168 | |
ashleymills | 0:e979170e02e7 | 169 | local[sha256->buffLen++] = 0x80; /* add 1 */ |
ashleymills | 0:e979170e02e7 | 170 | |
ashleymills | 0:e979170e02e7 | 171 | /* pad with zeros */ |
ashleymills | 0:e979170e02e7 | 172 | if (sha256->buffLen > SHA256_PAD_SIZE) { |
ashleymills | 0:e979170e02e7 | 173 | XMEMSET(&local[sha256->buffLen], 0, SHA256_BLOCK_SIZE - sha256->buffLen); |
ashleymills | 0:e979170e02e7 | 174 | sha256->buffLen += SHA256_BLOCK_SIZE - sha256->buffLen; |
ashleymills | 0:e979170e02e7 | 175 | |
ashleymills | 0:e979170e02e7 | 176 | #ifdef LITTLE_ENDIAN_ORDER |
ashleymills | 0:e979170e02e7 | 177 | ByteReverseBytes(local, local, SHA256_BLOCK_SIZE); |
ashleymills | 0:e979170e02e7 | 178 | #endif |
ashleymills | 0:e979170e02e7 | 179 | Transform(sha256); |
ashleymills | 0:e979170e02e7 | 180 | sha256->buffLen = 0; |
ashleymills | 0:e979170e02e7 | 181 | } |
ashleymills | 0:e979170e02e7 | 182 | XMEMSET(&local[sha256->buffLen], 0, SHA256_PAD_SIZE - sha256->buffLen); |
ashleymills | 0:e979170e02e7 | 183 | |
ashleymills | 0:e979170e02e7 | 184 | /* put lengths in bits */ |
ashleymills | 0:e979170e02e7 | 185 | sha256->hiLen = (sha256->loLen >> (8*sizeof(sha256->loLen) - 3)) + |
ashleymills | 0:e979170e02e7 | 186 | (sha256->hiLen << 3); |
ashleymills | 0:e979170e02e7 | 187 | sha256->loLen = sha256->loLen << 3; |
ashleymills | 0:e979170e02e7 | 188 | |
ashleymills | 0:e979170e02e7 | 189 | /* store lengths */ |
ashleymills | 0:e979170e02e7 | 190 | #ifdef LITTLE_ENDIAN_ORDER |
ashleymills | 0:e979170e02e7 | 191 | ByteReverseBytes(local, local, SHA256_BLOCK_SIZE); |
ashleymills | 0:e979170e02e7 | 192 | #endif |
ashleymills | 0:e979170e02e7 | 193 | /* ! length ordering dependent on digest endian type ! */ |
ashleymills | 0:e979170e02e7 | 194 | XMEMCPY(&local[SHA256_PAD_SIZE], &sha256->hiLen, sizeof(word32)); |
ashleymills | 0:e979170e02e7 | 195 | XMEMCPY(&local[SHA256_PAD_SIZE + sizeof(word32)], &sha256->loLen, |
ashleymills | 0:e979170e02e7 | 196 | sizeof(word32)); |
ashleymills | 0:e979170e02e7 | 197 | |
ashleymills | 0:e979170e02e7 | 198 | Transform(sha256); |
ashleymills | 0:e979170e02e7 | 199 | #ifdef LITTLE_ENDIAN_ORDER |
ashleymills | 0:e979170e02e7 | 200 | ByteReverseWords(sha256->digest, sha256->digest, SHA256_DIGEST_SIZE); |
ashleymills | 0:e979170e02e7 | 201 | #endif |
ashleymills | 0:e979170e02e7 | 202 | XMEMCPY(hash, sha256->digest, SHA256_DIGEST_SIZE); |
ashleymills | 0:e979170e02e7 | 203 | |
ashleymills | 0:e979170e02e7 | 204 | InitSha256(sha256); /* reset state */ |
ashleymills | 0:e979170e02e7 | 205 | } |
ashleymills | 0:e979170e02e7 | 206 | |
ashleymills | 0:e979170e02e7 | 207 | |
ashleymills | 0:e979170e02e7 | 208 | #endif /* NO_SHA256 */ |
ashleymills | 0:e979170e02e7 | 209 |