]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
SHA512: Fixing bug in SHA512 & SHA384
authorPavel Tvrdík <pawel.tvrdik@gmail.cz>
Tue, 12 May 2015 10:38:12 +0000 (12:38 +0200)
committerPavel Tvrdík <pawel.tvrdik@gmail.cz>
Tue, 12 May 2015 10:46:26 +0000 (12:46 +0200)
SHA512/SHA384 are using the SHA256 update and differs in the transform function.
This adds a pointer to transform function into a SHA context.

lib/sha256.c
lib/sha256.h
lib/sha512.c

index 8bf1739529da1de82cfb1035174011774eff04fa..84fcc737e8fb968b4288c8f57aa926722e5ba882 100644 (file)
@@ -33,6 +33,7 @@ sha256_init(sha256_context *ctx)
   ctx->nblocks_high = 0;
   ctx->count = 0;
   ctx->blocksize = 64;
+  ctx->transform = sha256_transform;
 }
 
 void
@@ -51,6 +52,7 @@ sha224_init(sha224_context *ctx)
   ctx->nblocks_high = 0;
   ctx->count = 0;
   ctx->blocksize = 64;
+  ctx->transform = sha256_transform;
 }
 
 /* (4.2) same as SHA-1's F1.  */
@@ -244,7 +246,7 @@ sha256_update(sha256_context *ctx, const byte *in_buf, size_t in_len)
 
   if (ctx->count == blocksize)  /* Flush the buffer. */
   {
-    sha256_transform(ctx, ctx->buf, 1);
+    ctx->transform(ctx, ctx->buf, 1);
     ctx->count = 0;
     if (!++ctx->nblocks)
       ctx->nblocks_high++;
@@ -264,7 +266,7 @@ sha256_update(sha256_context *ctx, const byte *in_buf, size_t in_len)
   if (in_len >= blocksize)
   {
     inblocks = in_len / blocksize;
-    sha256_transform(ctx, in_buf, inblocks);
+    ctx->transform(ctx, in_buf, inblocks);
     ctx->count = 0;
     ctx->nblocks_high += (ctx->nblocks + inblocks < inblocks);
     ctx->nblocks += inblocks;
index bdbc980d235bef4f6701b0c0a94035474ad3640e..682cab13f8df45f9572b99ea05b563a1d900572b 100644 (file)
@@ -22,6 +22,8 @@
 #define SHA256_HEX_SIZE                65
 #define SHA256_BLOCK_SIZE      64
 
+typedef unsigned int sha_transform_fn (void *c, const unsigned char *blks, size_t nblks);
+
 typedef struct {
   u32  h0,h1,h2,h3,h4,h5,h6,h7;
   byte buf[128];                       /* 128 is for SHA384 and SHA512 support, otherwise for SHA224 and SHA256 is 64 enough */
@@ -29,6 +31,7 @@ typedef struct {
   u32 nblocks_high;
   int count;
   u32 blocksize;
+  sha_transform_fn *transform;
 } sha256_context;
 typedef sha256_context sha224_context;
 
@@ -47,6 +50,8 @@ byte* sha224_final(sha224_context *ctx)
   return sha256_final(ctx);
 }
 
+static unsigned int sha256_transform(void *ctx, const unsigned char *data, size_t nblks);
+
 /**
  *     HMAC-SHA256, HMAC-SHA224
  */
index 249edccd2880c2c1acfe86021faea24e5e63f956..69bcd09965fb5f6af24f0986368c03e33480db46 100644 (file)
@@ -39,6 +39,7 @@ sha512_init(sha512_context *ctx)
   ctx->bctx.nblocks_high = 0;
   ctx->bctx.count = 0;
   ctx->bctx.blocksize = 128;
+  ctx->bctx.transform = sha512_transform;
 }
 
 void
@@ -59,6 +60,7 @@ sha384_init(sha384_context *ctx)
   ctx->bctx.nblocks_high = 0;
   ctx->bctx.count = 0;
   ctx->bctx.blocksize = 128;
+  ctx->bctx.transform = sha512_transform;
 }
 
 void sha512_update(sha512_context *ctx, const byte *in_buf, size_t in_len)