A simple library to support serving https.

Dependents:   oldheating gps motorhome heating

Revision:
7:94ef5824c3c0
Parent:
6:819c17738dc2
Child:
14:03a0b8fd6ddc
--- a/sha/sha1.c	Sun Sep 01 18:15:12 2019 +0000
+++ b/sha/sha1.c	Thu Sep 05 12:58:41 2019 +0000
@@ -13,7 +13,7 @@
 {
     return (val >> 31) | (val << 1);
 }
-static void sha1_transform(struct sha1_ctx *ctx)
+static void sha1_transform(struct Sha1State *ctx)
 {
     uint32_t W[80];
     register uint32_t A, B, C, D, E;
@@ -92,7 +92,7 @@
     ctx->state[3] += D;
     ctx->state[4] += E;
 }
-void sha1_update(struct sha1_ctx *ctx, const uint8_t *data, uint32_t len)
+void Sha1Add(struct Sha1State *ctx, const uint8_t *data, int len)
 {
     int i = ctx->count % sizeof(ctx->buf);
     const uint8_t *p = (const uint8_t *)data;
@@ -113,22 +113,21 @@
         }
     }
 }
-uint8_t *sha1_final(struct sha1_ctx *ctx)
+void Sha1Finish(struct Sha1State *ctx, uint8_t *out)
 {
     uint32_t cnt = ctx->count * 8;
     int i;
-    sha1_update(ctx, (uint8_t *)"\x80", 1);
+    Sha1Add(ctx, (uint8_t *)"\x80", 1);
     while ((ctx->count % sizeof(ctx->buf)) != (sizeof(ctx->buf) - 8))
-        sha1_update(ctx, (uint8_t *)"\0", 1);
+        Sha1Add(ctx, (uint8_t *)"\0", 1);
     for (i = 0; i < 8; ++i) {
         uint8_t tmp = cnt >> ((7 - i) * 8);
-        sha1_update(ctx, &tmp, 1);
+        Sha1Add(ctx, &tmp, 1);
     }
-    for (i = 0; i < 5; i++)
-        ctx->buf.w[i] = __builtin_bswap32(ctx->state[i]);
-    return ctx->buf.b;
+    for (i = 0; i <  5; i++) ctx->buf.w[i] = __builtin_bswap32(ctx->state[i]);
+    for (i = 0; i < 20; i++) out[i] = ctx->buf.b[i];
 }
-void sha1_init(struct sha1_ctx *ctx)
+void Sha1Start(struct Sha1State *ctx)
 {
     ctx->state[0] = 0x67452301;
     ctx->state[1] = 0xEFCDAB89;
@@ -136,4 +135,12 @@
     ctx->state[3] = 0x10325476;
     ctx->state[4] = 0xC3D2E1F0;
     ctx->count = 0;
+}
+
+void Sha1(const uint8_t* in, int inlen, uint8_t* hash)
+{
+    struct Sha1State md;
+    Sha1Start (&md);
+    Sha1Add   (&md, in, inlen);
+    Sha1Finish(&md, hash);
 }
\ No newline at end of file