This is Webservice SDK for mbed. LPCXpresso1769/LPC1768/FRDM-K64F/LPC4088

Fork of libMiMic by Ryo Iizuka

Committer:
furutani
Date:
Fri Feb 24 04:43:41 2017 +0000
Revision:
115:fa79286d8ea4
Parent:
57:bc4330dfa62f
Delete missing include line.; Add parameter "timeout" to TCPSocket::connect(), precv().; Fix to send ARP request to default gateway when connecting to IP address of different segment.;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nyatla 57:bc4330dfa62f 1 /*
nyatla 57:bc4330dfa62f 2 SHA-1 in C
nyatla 57:bc4330dfa62f 3 By Steve Reid <steve@edmweb.com>
nyatla 57:bc4330dfa62f 4 100% Public Domain
nyatla 57:bc4330dfa62f 5
nyatla 57:bc4330dfa62f 6 Test Vectors (from FIPS PUB 180-1)
nyatla 57:bc4330dfa62f 7 "abc"
nyatla 57:bc4330dfa62f 8 A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
nyatla 57:bc4330dfa62f 9 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
nyatla 57:bc4330dfa62f 10 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
nyatla 57:bc4330dfa62f 11 A million repetitions of "a"
nyatla 57:bc4330dfa62f 12 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
nyatla 57:bc4330dfa62f 13 */
nyatla 57:bc4330dfa62f 14 /**
nyatla 57:bc4330dfa62f 15 * modified by nyatla
nyatla 57:bc4330dfa62f 16 * Still public domain.
nyatla 57:bc4330dfa62f 17 */
nyatla 57:bc4330dfa62f 18
nyatla 57:bc4330dfa62f 19 /* #define LITTLE_ENDIAN * This should be #define'd if true. */
nyatla 57:bc4330dfa62f 20 /* #define SHA1HANDSOFF * Copies data before messing with it. */
nyatla 57:bc4330dfa62f 21
nyatla 57:bc4330dfa62f 22 #include <stdio.h>
nyatla 57:bc4330dfa62f 23 #include <string.h>
nyatla 57:bc4330dfa62f 24 #include "sha1.h"
nyatla 57:bc4330dfa62f 25 #include "NyLPC_config.h"
nyatla 57:bc4330dfa62f 26
nyatla 57:bc4330dfa62f 27
nyatla 57:bc4330dfa62f 28 # if UIP_BYTE_ORDER == NyLPC_ENDIAN_BIG
nyatla 57:bc4330dfa62f 29 # else
nyatla 57:bc4330dfa62f 30 # define LITTLE_ENDIAN 1
nyatla 57:bc4330dfa62f 31 #endif
nyatla 57:bc4330dfa62f 32
nyatla 57:bc4330dfa62f 33
nyatla 57:bc4330dfa62f 34
nyatla 57:bc4330dfa62f 35
nyatla 57:bc4330dfa62f 36 /*
nyatla 57:bc4330dfa62f 37 void SHA1Transform(unsigned long state[5], unsigned char buffer[64]);
nyatla 57:bc4330dfa62f 38 void SHA1Init(SHA1_CTX* context);
nyatla 57:bc4330dfa62f 39 void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int len);
nyatla 57:bc4330dfa62f 40 void SHA1Final(unsigned char digest[20], SHA1_CTX* context);
nyatla 57:bc4330dfa62f 41 */
nyatla 57:bc4330dfa62f 42 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
nyatla 57:bc4330dfa62f 43
nyatla 57:bc4330dfa62f 44 /* blk0() and blk() perform the initial expand. */
nyatla 57:bc4330dfa62f 45 /* I got the idea of expanding during the round function from SSLeay */
nyatla 57:bc4330dfa62f 46 #ifdef LITTLE_ENDIAN
nyatla 57:bc4330dfa62f 47 #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
nyatla 57:bc4330dfa62f 48 |(rol(block->l[i],8)&0x00FF00FF))
nyatla 57:bc4330dfa62f 49 #else
nyatla 57:bc4330dfa62f 50 #define blk0(i) block->l[i]
nyatla 57:bc4330dfa62f 51 #endif
nyatla 57:bc4330dfa62f 52 #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
nyatla 57:bc4330dfa62f 53 ^block->l[(i+2)&15]^block->l[i&15],1))
nyatla 57:bc4330dfa62f 54
nyatla 57:bc4330dfa62f 55 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
nyatla 57:bc4330dfa62f 56 #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
nyatla 57:bc4330dfa62f 57 #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
nyatla 57:bc4330dfa62f 58 #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
nyatla 57:bc4330dfa62f 59 #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
nyatla 57:bc4330dfa62f 60 #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
nyatla 57:bc4330dfa62f 61
nyatla 57:bc4330dfa62f 62
nyatla 57:bc4330dfa62f 63
nyatla 57:bc4330dfa62f 64 /* Hash a single 512-bit block. This is the core of the algorithm. */
nyatla 57:bc4330dfa62f 65 #define SHA1HANDSOFF 1
nyatla 57:bc4330dfa62f 66 void SHA1Transform(unsigned long state[5],const unsigned char buffer[64])
nyatla 57:bc4330dfa62f 67 {
nyatla 57:bc4330dfa62f 68 unsigned long a, b, c, d, e;
nyatla 57:bc4330dfa62f 69 typedef union {
nyatla 57:bc4330dfa62f 70 unsigned char c[64];
nyatla 57:bc4330dfa62f 71 unsigned long l[16];
nyatla 57:bc4330dfa62f 72 } CHAR64LONG16;
nyatla 57:bc4330dfa62f 73 CHAR64LONG16* block;
nyatla 57:bc4330dfa62f 74 #ifdef SHA1HANDSOFF
nyatla 57:bc4330dfa62f 75 static unsigned char workspace[64];
nyatla 57:bc4330dfa62f 76 block = (CHAR64LONG16*)workspace;
nyatla 57:bc4330dfa62f 77 memcpy(block, buffer, 64);
nyatla 57:bc4330dfa62f 78 #else
nyatla 57:bc4330dfa62f 79 block = (CHAR64LONG16*)buffer;
nyatla 57:bc4330dfa62f 80 #endif
nyatla 57:bc4330dfa62f 81 /* Copy context->state[] to working vars */
nyatla 57:bc4330dfa62f 82 a = state[0];
nyatla 57:bc4330dfa62f 83 b = state[1];
nyatla 57:bc4330dfa62f 84 c = state[2];
nyatla 57:bc4330dfa62f 85 d = state[3];
nyatla 57:bc4330dfa62f 86 e = state[4];
nyatla 57:bc4330dfa62f 87 /* 4 rounds of 20 operations each. Loop unrolled. */
nyatla 57:bc4330dfa62f 88 R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
nyatla 57:bc4330dfa62f 89 R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
nyatla 57:bc4330dfa62f 90 R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
nyatla 57:bc4330dfa62f 91 R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
nyatla 57:bc4330dfa62f 92 R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
nyatla 57:bc4330dfa62f 93 R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
nyatla 57:bc4330dfa62f 94 R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
nyatla 57:bc4330dfa62f 95 R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
nyatla 57:bc4330dfa62f 96 R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
nyatla 57:bc4330dfa62f 97 R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
nyatla 57:bc4330dfa62f 98 R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
nyatla 57:bc4330dfa62f 99 R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
nyatla 57:bc4330dfa62f 100 R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
nyatla 57:bc4330dfa62f 101 R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
nyatla 57:bc4330dfa62f 102 R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
nyatla 57:bc4330dfa62f 103 R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
nyatla 57:bc4330dfa62f 104 R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
nyatla 57:bc4330dfa62f 105 R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
nyatla 57:bc4330dfa62f 106 R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
nyatla 57:bc4330dfa62f 107 R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
nyatla 57:bc4330dfa62f 108 /* Add the working vars back into context.state[] */
nyatla 57:bc4330dfa62f 109 state[0] += a;
nyatla 57:bc4330dfa62f 110 state[1] += b;
nyatla 57:bc4330dfa62f 111 state[2] += c;
nyatla 57:bc4330dfa62f 112 state[3] += d;
nyatla 57:bc4330dfa62f 113 state[4] += e;
nyatla 57:bc4330dfa62f 114 /* Wipe variables */
nyatla 57:bc4330dfa62f 115 a = b = c = d = e = 0;
nyatla 57:bc4330dfa62f 116 }
nyatla 57:bc4330dfa62f 117
nyatla 57:bc4330dfa62f 118
nyatla 57:bc4330dfa62f 119 /* SHA1Init - Initialize new context */
nyatla 57:bc4330dfa62f 120
nyatla 57:bc4330dfa62f 121 void SHA1Init(SHA1_CTX* context)
nyatla 57:bc4330dfa62f 122 {
nyatla 57:bc4330dfa62f 123 /* SHA1 initialization constants */
nyatla 57:bc4330dfa62f 124 context->state[0] = 0x67452301;
nyatla 57:bc4330dfa62f 125 context->state[1] = 0xEFCDAB89;
nyatla 57:bc4330dfa62f 126 context->state[2] = 0x98BADCFE;
nyatla 57:bc4330dfa62f 127 context->state[3] = 0x10325476;
nyatla 57:bc4330dfa62f 128 context->state[4] = 0xC3D2E1F0;
nyatla 57:bc4330dfa62f 129 context->count[0] = context->count[1] = 0;
nyatla 57:bc4330dfa62f 130 }
nyatla 57:bc4330dfa62f 131
nyatla 57:bc4330dfa62f 132
nyatla 57:bc4330dfa62f 133 /* Run your data through this. */
nyatla 57:bc4330dfa62f 134
nyatla 57:bc4330dfa62f 135 void SHA1Update(SHA1_CTX* context,const void* data, unsigned int len)
nyatla 57:bc4330dfa62f 136 {
nyatla 57:bc4330dfa62f 137 unsigned int i, j;
nyatla 57:bc4330dfa62f 138
nyatla 57:bc4330dfa62f 139 j = (context->count[0] >> 3) & 63;
nyatla 57:bc4330dfa62f 140 if ((context->count[0] += len << 3) < (len << 3)) context->count[1]++;
nyatla 57:bc4330dfa62f 141 context->count[1] += (len >> 29);
nyatla 57:bc4330dfa62f 142 if ((j + len) > 63) {
nyatla 57:bc4330dfa62f 143 memcpy(&context->buffer[j], data, (i = 64-j));
nyatla 57:bc4330dfa62f 144 SHA1Transform(context->state, context->buffer);
nyatla 57:bc4330dfa62f 145 for ( ; i + 63 < len; i += 64) {
nyatla 57:bc4330dfa62f 146 SHA1Transform(context->state, ((const unsigned char*)data)+i);
nyatla 57:bc4330dfa62f 147 }
nyatla 57:bc4330dfa62f 148 j = 0;
nyatla 57:bc4330dfa62f 149 }
nyatla 57:bc4330dfa62f 150 else i = 0;
nyatla 57:bc4330dfa62f 151 memcpy(&context->buffer[j], ((const unsigned char*)data)+i, len - i);
nyatla 57:bc4330dfa62f 152 }
nyatla 57:bc4330dfa62f 153
nyatla 57:bc4330dfa62f 154
nyatla 57:bc4330dfa62f 155 /* Add padding and return the message digest. */
nyatla 57:bc4330dfa62f 156
nyatla 57:bc4330dfa62f 157 void SHA1Final(unsigned char digest[20], SHA1_CTX* context)
nyatla 57:bc4330dfa62f 158 {
nyatla 57:bc4330dfa62f 159 unsigned long i, j;
nyatla 57:bc4330dfa62f 160 unsigned char finalcount[8];
nyatla 57:bc4330dfa62f 161
nyatla 57:bc4330dfa62f 162 for (i = 0; i < 8; i++) {
nyatla 57:bc4330dfa62f 163 finalcount[i] = (unsigned char)((context->count[((i >= 4) ? 0 : 1)]
nyatla 57:bc4330dfa62f 164 >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
nyatla 57:bc4330dfa62f 165 }
nyatla 57:bc4330dfa62f 166 SHA1Update(context, (unsigned char *)"\200", 1);
nyatla 57:bc4330dfa62f 167 while ((context->count[0] & 504) != 448) {
nyatla 57:bc4330dfa62f 168 SHA1Update(context, (unsigned char *)"\0", 1);
nyatla 57:bc4330dfa62f 169 }
nyatla 57:bc4330dfa62f 170 SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
nyatla 57:bc4330dfa62f 171 for (i = 0; i < 20; i++) {
nyatla 57:bc4330dfa62f 172 digest[i] = (unsigned char)
nyatla 57:bc4330dfa62f 173 ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
nyatla 57:bc4330dfa62f 174 }
nyatla 57:bc4330dfa62f 175 /* Wipe variables */
nyatla 57:bc4330dfa62f 176 i = j = 0;
nyatla 57:bc4330dfa62f 177 memset(context->buffer, 0, 64);
nyatla 57:bc4330dfa62f 178 memset(context->state, 0, 20);
nyatla 57:bc4330dfa62f 179 memset(context->count, 0, 8);
nyatla 57:bc4330dfa62f 180 memset(&finalcount, 0, 8);
nyatla 57:bc4330dfa62f 181 #ifdef SHA1HANDSOFF /* make SHA1Transform overwrite it's own static vars */
nyatla 57:bc4330dfa62f 182 SHA1Transform(context->state, context->buffer);
nyatla 57:bc4330dfa62f 183 #endif
nyatla 57:bc4330dfa62f 184 }
nyatla 57:bc4330dfa62f 185
nyatla 57:bc4330dfa62f 186
nyatla 57:bc4330dfa62f 187 /*************************************************************/
nyatla 57:bc4330dfa62f 188 //#define TEST_SHA1
nyatla 57:bc4330dfa62f 189 #ifdef TEST_SHA1
nyatla 57:bc4330dfa62f 190
nyatla 57:bc4330dfa62f 191 int main(int argc, char** argv)
nyatla 57:bc4330dfa62f 192 {
nyatla 57:bc4330dfa62f 193 int i, j;
nyatla 57:bc4330dfa62f 194 SHA1_CTX context;
nyatla 57:bc4330dfa62f 195 unsigned char digest[20], buffer[16384];
nyatla 57:bc4330dfa62f 196 FILE* file;
nyatla 57:bc4330dfa62f 197
nyatla 57:bc4330dfa62f 198 if (argc > 2) {
nyatla 57:bc4330dfa62f 199 puts("Public domain SHA-1 implementation - by Steve Reid <steve@edmweb.com>");
nyatla 57:bc4330dfa62f 200 puts("Produces the SHA-1 hash of a file, or stdin if no file is specified.");
nyatla 57:bc4330dfa62f 201 exit(0);
nyatla 57:bc4330dfa62f 202 }
nyatla 57:bc4330dfa62f 203 if (argc < 2) {
nyatla 57:bc4330dfa62f 204 file = stdin;
nyatla 57:bc4330dfa62f 205 }
nyatla 57:bc4330dfa62f 206 else {
nyatla 57:bc4330dfa62f 207 if (!(file = fopen(argv[1], "rb"))) {
nyatla 57:bc4330dfa62f 208 fputs("Unable to open file.", stderr);
nyatla 57:bc4330dfa62f 209 exit(-1);
nyatla 57:bc4330dfa62f 210 }
nyatla 57:bc4330dfa62f 211 }
nyatla 57:bc4330dfa62f 212 SHA1Init(&context);
nyatla 57:bc4330dfa62f 213 while (!feof(file)) { /* note: what if ferror(file) */
nyatla 57:bc4330dfa62f 214 i = fread(buffer, 1, 16384, file);
nyatla 57:bc4330dfa62f 215 SHA1Update(&context, buffer, i);
nyatla 57:bc4330dfa62f 216 }
nyatla 57:bc4330dfa62f 217 SHA1Final(digest, &context);
nyatla 57:bc4330dfa62f 218 fclose(file);
nyatla 57:bc4330dfa62f 219 for (i = 0; i < 5; i++) {
nyatla 57:bc4330dfa62f 220 for (j = 0; j < 4; j++) {
nyatla 57:bc4330dfa62f 221 printf("%02X", digest[i*4+j]);
nyatla 57:bc4330dfa62f 222 }
nyatla 57:bc4330dfa62f 223 putchar(' ');
nyatla 57:bc4330dfa62f 224 }
nyatla 57:bc4330dfa62f 225 putchar('\n');
nyatla 57:bc4330dfa62f 226 exit(0);
nyatla 57:bc4330dfa62f 227 }
nyatla 57:bc4330dfa62f 228 #endif
nyatla 57:bc4330dfa62f 229