Browse Source

replace packed parameter struct with offsets into a unsigned char buffer.

Kelvin Sherlock 8 years ago
parent
commit
6b46b447b5
2 changed files with 59 additions and 24 deletions
  1. 29 12
      src/hashes/blake2b.c
  2. 30 12
      src/hashes/blake2s.c

+ 29 - 12
src/hashes/blake2b.c

@@ -32,9 +32,27 @@ enum blake2b_constant {
    BLAKE2B_OUTBYTES = 64,
    BLAKE2B_KEYBYTES = 64,
    BLAKE2B_SALTBYTES = 16,
-   BLAKE2B_PERSONALBYTES = 16
+   BLAKE2B_PERSONALBYTES = 16,
+   BLAKE2B_PARAM_SIZE = 64
 };
 
+/* param offsets */
+enum {
+   O_DIGEST_LENGTH = 0,
+   O_KEY_LENGTH = 1,
+   O_FANOUT = 2,
+   O_DEPTH = 3,
+   O_LEAF_LENGTH = 4,
+   O_NODE_OFFSET = 8,
+   O_XOF_LENGTH = 12,
+   O_NODE_DEPTH = 16,
+   O_INNER_LENGTH = 17,
+   O_RESERVED = 18,
+   O_SALT = 32,
+   O_PERSONAL = 48
+};
+
+/*
 struct blake2b_param {
    unsigned char digest_length;
    unsigned char key_length;
@@ -49,7 +67,7 @@ struct blake2b_param {
    unsigned char salt[BLAKE2B_SALTBYTES];
    unsigned char personal[BLAKE2B_PERSONALBYTES];
 };
-
+*/
 
 const struct ltc_hash_descriptor blake2b_160_desc =
 {
@@ -167,9 +185,8 @@ static void blake2b_init0(hash_state *md)
 }
 
 /* init xors IV with input parameter block */
-static int blake2b_init_param(hash_state *md, const struct blake2b_param *P)
+static int blake2b_init_param(hash_state *md, const unsigned char *P)
 {
-   const unsigned char *p = (const unsigned char *)(P);
    unsigned long i;
 
    blake2b_init0(md);
@@ -177,29 +194,29 @@ static int blake2b_init_param(hash_state *md, const struct blake2b_param *P)
    /* IV XOR ParamBlock */
    for (i = 0; i < 8; ++i) {
       ulong64 tmp;
-      LOAD64L(tmp, p + i * 8);
+      LOAD64L(tmp, P + i * 8);
       md->blake2b.h[i] ^= tmp;
    }
 
-   md->blake2b.outlen = P->digest_length;
+   md->blake2b.outlen = P[O_DIGEST_LENGTH];
    return CRYPT_OK;
 }
 
 int blake2b_init(hash_state *md, unsigned long outlen)
 {
-   struct blake2b_param P;
+   unsigned char P[BLAKE2B_PARAM_SIZE];
 
    LTC_ARGCHK(md != NULL);
 
    if ((!outlen) || (outlen > BLAKE2B_OUTBYTES))
       return CRYPT_INVALID_ARG;
 
-   XMEMSET(&P, 0, sizeof(P));
+   XMEMSET(P, 0, sizeof(P));
 
-   P.digest_length = (unsigned char)outlen;
-   P.fanout = 1;
-   P.depth = 1;
-   return blake2b_init_param(md, &P);
+   P[O_DIGEST_LENGTH] = (unsigned char)outlen;
+   P[O_FANOUT] = 1;
+   P[O_DEPTH] = 1;
+   return blake2b_init_param(md, P);
 }
 
 int blake2b_160_init(hash_state *md) { return blake2b_init(md, 20); }

+ 30 - 12
src/hashes/blake2s.c

@@ -32,21 +32,40 @@ enum blake2s_constant {
    BLAKE2S_OUTBYTES = 32,
    BLAKE2S_KEYBYTES = 32,
    BLAKE2S_SALTBYTES = 8,
-   BLAKE2S_PERSONALBYTES = 8
+   BLAKE2S_PERSONALBYTES = 8,
+   BLAKE2S_PARAM_SIZE = 32
 };
 
+/* param offsets */
+enum {
+   O_DIGEST_LENGTH = 0,
+   O_KEY_LENGTH = 1,
+   O_FANOUT = 2,
+   O_DEPTH = 3,
+   O_LEAF_LENGTH = 4,
+   O_NODE_OFFSET = 8,
+   O_XOF_LENGTH = 12,
+   O_NODE_DEPTH = 14,
+   O_INNER_LENGTH = 15,
+   O_SALT = 16,
+   O_PERSONAL = 24
+};
+
+/*
 struct blake2s_param {
    unsigned char digest_length;
    unsigned char key_length;
    unsigned char fanout;
    unsigned char depth;
    ulong32 leaf_length;
-   unsigned char node_offset[6];
+   ulong32 node_offset;
+   ushort16 xof_length;
    unsigned char node_depth;
    unsigned char inner_length;
    unsigned char salt[BLAKE2S_SALTBYTES];
    unsigned char personal[BLAKE2S_PERSONALBYTES];
 };
+*/
 
 const struct ltc_hash_descriptor blake2s_128_desc =
 {
@@ -160,41 +179,40 @@ static int blake2s_init0(hash_state *md)
 }
 
 /* init2 xors IV with input parameter block */
-static int blake2s_init_param(hash_state *md, const struct blake2s_param *P)
+static int blake2s_init_param(hash_state *md, const unsigned char *P)
 {
    unsigned long i;
-   unsigned char *p = (unsigned char *)(P);
 
    blake2s_init0(md);
 
    /* IV XOR ParamBlock */
    for (i = 0; i < 8; ++i) {
       ulong32 tmp;
-      LOAD32L(tmp, p + i * 4);
+      LOAD32L(tmp, P + i * 4);
       md->blake2s.h[i] ^= tmp;
    }
 
-   md->blake2s.outlen = P->digest_length;
+   md->blake2s.outlen = P[O_DIGEST_LENGTH];
    return CRYPT_OK;
 }
 
 /* Sequential blake2s initialization */
 int blake2s_init(hash_state *md, unsigned long outlen)
 {
-   struct blake2s_param P;
+   unsigned char P[BLAKE2S_PARAM_SIZE];
    LTC_ARGCHK(md != NULL);
 
    if ((!outlen) || (outlen > BLAKE2S_OUTBYTES))
       return CRYPT_INVALID_ARG;
 
-   XMEMSET(&P, 0, sizeof(P));
+   XMEMSET(P, 0, sizeof(P));
 
-   P.digest_length = (unsigned char)outlen;
+   P[O_DIGEST_LENGTH] = (unsigned char)outlen;
 
-   P.fanout = 1;
-   P.depth = 1;
+   P[O_FANOUT] = 1;
+   P[O_DEPTH] = 1;
 
-   return blake2s_init_param(md, &P);
+   return blake2s_init_param(md, P);
 }
 
 int blake2s_128_init(hash_state *md) { return blake2s_init(md, 16); }