Skip to content

Commit e12b02f

Browse files
alashkincpq
authored andcommitted
Enable SHA1 & Co
PUBLISHED_FROM=10641eaa7b193055f6725bc1b113b1158b3acec7
1 parent d81d545 commit e12b02f

File tree

4 files changed

+37
-33
lines changed

4 files changed

+37
-33
lines changed

fossa.c

+25-25
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ static uint32_t blk0(union char64long16 *block, int i) {
254254
z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \
255255
w = rol(w, 30);
256256

257-
void SHA1Transform(uint32_t state[5], const unsigned char buffer[64]) {
257+
void cs_sha1_transform(uint32_t state[5], const unsigned char buffer[64]) {
258258
uint32_t a, b, c, d, e;
259259
union char64long16 block[1];
260260

@@ -360,7 +360,7 @@ void SHA1Transform(uint32_t state[5], const unsigned char buffer[64]) {
360360
(void) e;
361361
}
362362

363-
void SHA1Init(SHA1_CTX *context) {
363+
void cs_sha1_init(cs_sha1_ctx *context) {
364364
context->state[0] = 0x67452301;
365365
context->state[1] = 0xEFCDAB89;
366366
context->state[2] = 0x98BADCFE;
@@ -369,7 +369,7 @@ void SHA1Init(SHA1_CTX *context) {
369369
context->count[0] = context->count[1] = 0;
370370
}
371371

372-
void SHA1Update(SHA1_CTX *context, const unsigned char *data, uint32_t len) {
372+
void cs_sha1_update(cs_sha1_ctx *context, const unsigned char *data, uint32_t len) {
373373
uint32_t i, j;
374374

375375
j = context->count[0];
@@ -378,17 +378,17 @@ void SHA1Update(SHA1_CTX *context, const unsigned char *data, uint32_t len) {
378378
j = (j >> 3) & 63;
379379
if ((j + len) > 63) {
380380
memcpy(&context->buffer[j], data, (i = 64 - j));
381-
SHA1Transform(context->state, context->buffer);
381+
cs_sha1_transform(context->state, context->buffer);
382382
for (; i + 63 < len; i += 64) {
383-
SHA1Transform(context->state, &data[i]);
383+
cs_sha1_transform(context->state, &data[i]);
384384
}
385385
j = 0;
386386
} else
387387
i = 0;
388388
memcpy(&context->buffer[j], &data[i], len - i);
389389
}
390390

391-
void SHA1Final(unsigned char digest[20], SHA1_CTX *context) {
391+
void cs_sha1_final(unsigned char digest[20], cs_sha1_ctx *context) {
392392
unsigned i;
393393
unsigned char finalcount[8], c;
394394

@@ -398,12 +398,12 @@ void SHA1Final(unsigned char digest[20], SHA1_CTX *context) {
398398
255);
399399
}
400400
c = 0200;
401-
SHA1Update(context, &c, 1);
401+
cs_sha1_update(context, &c, 1);
402402
while ((context->count[0] & 504) != 448) {
403403
c = 0000;
404-
SHA1Update(context, &c, 1);
404+
cs_sha1_update(context, &c, 1);
405405
}
406-
SHA1Update(context, finalcount, 8);
406+
cs_sha1_update(context, finalcount, 8);
407407
for (i = 0; i < 20; i++) {
408408
digest[i] =
409409
(unsigned char) ((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
@@ -415,13 +415,13 @@ void SHA1Final(unsigned char digest[20], SHA1_CTX *context) {
415415
void hmac_sha1(const unsigned char *key, size_t keylen,
416416
const unsigned char *data, size_t datalen,
417417
unsigned char out[20]) {
418-
SHA1_CTX ctx;
418+
cs_sha1_ctx ctx;
419419
unsigned char buf1[64], buf2[64], tmp_key[20], i;
420420

421421
if (keylen > sizeof(buf1)) {
422-
SHA1Init(&ctx);
423-
SHA1Update(&ctx, key, keylen);
424-
SHA1Final(tmp_key, &ctx);
422+
cs_sha1_init(&ctx);
423+
cs_sha1_update(&ctx, key, keylen);
424+
cs_sha1_final(tmp_key, &ctx);
425425
key = tmp_key;
426426
keylen = sizeof(tmp_key);
427427
}
@@ -436,15 +436,15 @@ void hmac_sha1(const unsigned char *key, size_t keylen,
436436
buf2[i] ^= 0x5c;
437437
}
438438

439-
SHA1Init(&ctx);
440-
SHA1Update(&ctx, buf1, sizeof(buf1));
441-
SHA1Update(&ctx, data, datalen);
442-
SHA1Final(out, &ctx);
439+
cs_sha1_init(&ctx);
440+
cs_sha1_update(&ctx, buf1, sizeof(buf1));
441+
cs_sha1_update(&ctx, data, datalen);
442+
cs_sha1_final(out, &ctx);
443443

444-
SHA1Init(&ctx);
445-
SHA1Update(&ctx, buf2, sizeof(buf2));
446-
SHA1Update(&ctx, out, 20);
447-
SHA1Final(out, &ctx);
444+
cs_sha1_init(&ctx);
445+
cs_sha1_update(&ctx, buf2, sizeof(buf2));
446+
cs_sha1_update(&ctx, out, 20);
447+
cs_sha1_final(out, &ctx);
448448
}
449449

450450
#endif /* EXCLUDE_COMMON */
@@ -3592,13 +3592,13 @@ static void websocket_handler(struct ns_connection *nc, int ev, void *ev_data) {
35923592
static void ws_handshake(struct ns_connection *nc, const struct ns_str *key) {
35933593
static const char *magic = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
35943594
char buf[500], sha[20], b64_sha[sizeof(sha) * 2];
3595-
SHA1_CTX sha_ctx;
3595+
cs_sha1_ctx sha_ctx;
35963596

35973597
snprintf(buf, sizeof(buf), "%.*s%s", (int) key->len, key->p, magic);
35983598

3599-
SHA1Init(&sha_ctx);
3600-
SHA1Update(&sha_ctx, (unsigned char *) buf, strlen(buf));
3601-
SHA1Final((unsigned char *) sha, &sha_ctx);
3599+
cs_sha1_init(&sha_ctx);
3600+
cs_sha1_update(&sha_ctx, (unsigned char *) buf, strlen(buf));
3601+
cs_sha1_final((unsigned char *) sha, &sha_ctx);
36023602

36033603
ns_base64_encode((unsigned char *) sha, sizeof(sha), b64_sha);
36043604
ns_printf(nc, "%s%s%s",

fossa.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ struct dirent *readdir(DIR *dir);
193193
#include <lwip/sockets.h>
194194
#include <lwip/netdb.h>
195195
#include <lwip/dns.h>
196+
#include <esp_libc.h>
197+
#define random() os_random()
196198
/* TODO(alashkin): check if zero is OK */
197199
#define SOMAXCONN 0
198200
#include <stdlib.h>
@@ -351,11 +353,11 @@ typedef struct {
351353
uint32_t state[5];
352354
uint32_t count[2];
353355
unsigned char buffer[64];
354-
} SHA1_CTX;
356+
} cs_sha1_ctx;
355357

356-
void SHA1Init(SHA1_CTX *);
357-
void SHA1Update(SHA1_CTX *, const unsigned char *data, uint32_t len);
358-
void SHA1Final(unsigned char digest[20], SHA1_CTX *);
358+
void cs_sha1_init(cs_sha1_ctx *);
359+
void cs_sha1_update(cs_sha1_ctx *, const unsigned char *data, uint32_t len);
360+
void cs_sha1_final(unsigned char digest[20], cs_sha1_ctx *);
359361
void hmac_sha1(const unsigned char *key, size_t key_len,
360362
const unsigned char *text, size_t text_len,
361363
unsigned char out[20]);

src/http.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -523,13 +523,13 @@ static void websocket_handler(struct ns_connection *nc, int ev, void *ev_data) {
523523
static void ws_handshake(struct ns_connection *nc, const struct ns_str *key) {
524524
static const char *magic = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
525525
char buf[500], sha[20], b64_sha[sizeof(sha) * 2];
526-
SHA1_CTX sha_ctx;
526+
cs_sha1_ctx sha_ctx;
527527

528528
snprintf(buf, sizeof(buf), "%.*s%s", (int) key->len, key->p, magic);
529529

530-
SHA1Init(&sha_ctx);
531-
SHA1Update(&sha_ctx, (unsigned char *) buf, strlen(buf));
532-
SHA1Final((unsigned char *) sha, &sha_ctx);
530+
cs_sha1_init(&sha_ctx);
531+
cs_sha1_update(&sha_ctx, (unsigned char *) buf, strlen(buf));
532+
cs_sha1_final((unsigned char *) sha, &sha_ctx);
533533

534534
ns_base64_encode((unsigned char *) sha, sizeof(sha), b64_sha);
535535
ns_printf(nc, "%s%s%s",

test/osdep.h

+2
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ struct dirent *readdir(DIR *dir);
175175
#include <lwip/sockets.h>
176176
#include <lwip/netdb.h>
177177
#include <lwip/dns.h>
178+
#include <esp_libc.h>
179+
#define random() os_random()
178180
/* TODO(alashkin): check if zero is OK */
179181
#define SOMAXCONN 0
180182
#include <stdlib.h>

0 commit comments

Comments
 (0)