Merge branch '2.3.x'
authorMark Wooding <mdw@distorted.org.uk>
Tue, 4 Jul 2017 23:51:36 +0000 (00:51 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Tue, 4 Jul 2017 23:51:36 +0000 (00:51 +0100)
* 2.3.x:
  symm/hmac-def.h: Fix the NMAC and SSLMAC classes.

Conflicts:
symm/hmac-def.h

1  2 
symm/hmac-def.h

diff --combined symm/hmac-def.h
   * Use:               Creates implementations for the HMAC and NMAC functions.
   */
  
 -#define HMAC_DEF(PRE, pre)                                            \
 +#define HMAC_DEF(PRE, pre) HMAC_DEFX(PRE, pre, #pre, #pre)
 +#define HMAC_DEFX(PRE, pre, name, fname)                              \
                                                                        \
  /* --- Useful constants --- */                                                \
                                                                        \
 -const octet pre##_hmackeysz[] = { KSZ_ANY, PRE##_STATESZ };           \
 -const octet pre##_sslmackeysz[] = { KSZ_ANY, PRE##_STATESZ };         \
 -const octet pre##_nmackeysz[] = { KSZ_SET, 2 * PRE##_STATESZ, 0 };    \
 +const octet pre##_hmackeysz[] =                                               \
 +  { KSZ_ANY | KSZ_16BIT, PRE##_STATESZ/256, PRE##_STATESZ%256 };      \
 +const octet pre##_sslmackeysz[] =                                     \
 +  { KSZ_ANY | KSZ_16BIT, PRE##_STATESZ/256, PRE##_STATESZ%256 };      \
 +const octet pre##_nmackeysz[] =                                               \
 +  { KSZ_SET | KSZ_16BIT,                                              \
 +    2*PRE##_STATESZ/256, 2*PRE##_STATESZ%256, 0, 0 };                 \
                                                                        \
  /* --- @pre_nmacinit@ --- *                                           \
   *                                                                    \
@@@ -113,28 -108,31 +113,28 @@@ void pre##_hmacinit(pre##_mackey *key, 
    int i;                                                              \
    const octet *kbuf = k;                                              \
    pre##_ctx ctx;                                                      \
 -  octet buf[PRE##_HASHSZ];                                            \
 +  octet hbuf[PRE##_HASHSZ], buf[PRE##_BUFSZ];                         \
                                                                        \
    if (sz > PRE##_BUFSZ)       {                                               \
      pre##_init(&ctx);                                                 \
      pre##_hash(&ctx, k, sz);                                          \
 -    pre##_done(&ctx, buf);                                            \
 -    kbuf = buf;                                                               \
 +    pre##_done(&ctx, hbuf);                                           \
 +    kbuf = hbuf;                                                      \
      sz = PRE##_HASHSZ;                                                        \
    }                                                                   \
                                                                        \
    pre##_init(&ctx);                                                   \
 -  memset(ctx.buf, 0x5c, PRE##_BUFSZ);                                 \
 -  for (i = 0; i < sz; i++)                                            \
 -    ctx.buf[i] ^= kbuf[i];                                            \
 -  pre##_compress(&ctx, ctx.buf);                                      \
 -  pre##_state(&ctx, key->ochain);                                     \
 +  memset(buf, 0x5c, PRE##_BUFSZ);                                     \
 +  for (i = 0; i < sz; i++) buf[i] ^= kbuf[i];                         \
 +  pre##_hash(&ctx, buf, PRE##_BUFSZ);                                 \
 +  key->ocount = pre##_state(&ctx, key->ochain);                               \
                                                                        \
    pre##_init(&ctx);                                                   \
 -  memset(ctx.buf, 0x36, PRE##_BUFSZ);                                 \
 -  for (i = 0; i < sz; i++)                                            \
 -    ctx.buf[i] ^= kbuf[i];                                            \
 -  pre##_compress(&ctx, ctx.buf);                                      \
 -  pre##_state(&ctx, key->ichain);                                     \
 +  memset(buf, 0x36, PRE##_BUFSZ);                                     \
 +  for (i = 0; i < sz; i++) buf[i] ^= kbuf[i];                         \
 +  pre##_hash(&ctx, buf, PRE##_BUFSZ);                                 \
 +  key->icount = pre##_state(&ctx, key->ichain);                               \
                                                                        \
 -  key->ocount = key->icount = PRE##_BUFSZ;                            \
    BURN(ctx);                                                          \
  }                                                                     \
                                                                        \
@@@ -154,28 -152,29 +154,28 @@@ void pre##_sslmacinit(pre##_mackey *key
  {                                                                     \
    const octet *kbuf = k;                                              \
    pre##_ctx ctx;                                                      \
 -  octet buf[PRE##_HASHSZ];                                            \
 +  octet hbuf[PRE##_HASHSZ], buf[PRE##_BUFSZ];                         \
                                                                        \
    if (sz > PRE##_BUFSZ)       {                                               \
      pre##_init(&ctx);                                                 \
      pre##_hash(&ctx, k, sz);                                          \
 -    pre##_done(&ctx, buf);                                            \
 -    kbuf = buf;                                                               \
 +    pre##_done(&ctx, hbuf);                                           \
 +    kbuf = hbuf;                                                      \
      sz = PRE##_HASHSZ;                                                        \
    }                                                                   \
                                                                        \
    pre##_init(&ctx);                                                   \
 -  memcpy(ctx.buf, kbuf, sz);                                          \
 -  memset(ctx.buf + sz, 0x5c, PRE##_BUFSZ - sz);                               \
 -  pre##_compress(&ctx, ctx.buf);                                      \
 -  pre##_state(&ctx, key->ochain);                                     \
 +  memcpy(buf, kbuf, sz);                                              \
 +  memset(buf + sz, 0x5c, PRE##_BUFSZ - sz);                           \
 +  pre##_hash(&ctx, buf, PRE##_BUFSZ);                                 \
 +  key->ocount = pre##_state(&ctx, key->ochain);                               \
                                                                        \
    pre##_init(&ctx);                                                   \
 -  memcpy(ctx.buf, kbuf, sz);                                          \
 -  memset(ctx.buf + sz, 0x36, PRE##_BUFSZ - sz);                               \
 -  pre##_compress(&ctx, ctx.buf);                                      \
 -  pre##_state(&ctx, key->ichain);                                     \
 +  memcpy(buf, kbuf, sz);                                              \
 +  memset(buf + sz, 0x36, PRE##_BUFSZ - sz);                           \
 +  pre##_hash(&ctx, buf, PRE##_BUFSZ);                                 \
 +  key->icount = pre##_state(&ctx, key->ichain);                               \
                                                                        \
 -  key->ocount = key->icount = PRE##_BUFSZ;                            \
    BURN(ctx);                                                          \
  }                                                                     \
                                                                        \
@@@ -232,7 -231,7 +232,7 @@@ void pre##_macdone(pre##_macctx *ctx, v
                                                                        \
  /* --- Generic MAC interface --- */                                   \
                                                                        \
- static const gmac_ops gkops;                                          \
+ static const gmac_ops gkops, gnkops, gsslkops;                                \
  static const ghash_ops gops, gnops, gsslops;                          \
                                                                        \
  typedef struct gkctx {                                                        \
@@@ -270,7 -269,7 +270,7 @@@ static gmac *gnkey(const void *k, size_
    gkctx *gk = S_CREATE(gkctx);                                                \
    const octet *kk = k;                                                        \
    assert(keysz(sz, pre##_nmackeysz) == sz);                           \
-   gk->m.ops = &gkops;                                                 \
+   gk->m.ops = &gnkops;                                                        \
    gk->gops = &gnops;                                                  \
    pre##_nmacinit(&gk->k, kk, kk + PRE##_STATESZ);                     \
    return (&gk->m);                                                    \
  static gmac *gsslkey(const void *k, size_t sz)                                \
  {                                                                     \
    gkctx *gk = S_CREATE(gkctx);                                                \
-   gk->m.ops = &gkops;                                                 \
+   gk->m.ops = &gsslkops;                                              \
    gk->gops = &gsslops;                                                        \
    pre##_sslmacinit(&gk->k, k, sz);                                    \
    return (&gk->m);                                                    \
@@@ -329,27 -328,25 +329,27 @@@ static ghash *ghinit(void)                                              
  }                                                                     \
                                                                        \
  const gcmac pre##_nmac =                                              \
 -  { #pre "-nmac", PRE##_HASHSZ, pre##_nmackeysz, gnkey };             \
 +  { name "-nmac", PRE##_HASHSZ, pre##_nmackeysz, gnkey };             \
  const gcmac pre##_hmac =                                              \
 -  { #pre "-hmac", PRE##_HASHSZ, pre##_hmackeysz, gkey };              \
 +  { name "-hmac", PRE##_HASHSZ, pre##_hmackeysz, gkey };              \
  const gcmac pre##_sslmac =                                            \
 -  { #pre "-sslmac", PRE##_HASHSZ, pre##_sslmackeysz, gsslkey };               \
 +  { name "-sslmac", PRE##_HASHSZ, pre##_sslmackeysz, gsslkey };               \
  static const gmac_ops gkops = { &pre##_hmac, gkinit, gkdestroy };     \
  static const gmac_ops gnkops = { &pre##_nmac, gkinit, gkdestroy };    \
  static const gmac_ops gsslkops = { &pre##_sslmac, gkinit, gkdestroy };        \
 -static const gchash gch = { #pre "-hmac", PRE##_HASHSZ, ghinit };     \
 +static const gchash gch = { name "-hmac", PRE##_HASHSZ, ghinit };     \
  static const ghash_ops gops =                                         \
    { &gch, ghhash, ghdone, ghdestroy, ghcopy };                                \
 -static const gchash gnch = { #pre "-nmac", PRE##_HASHSZ, ghinit };    \
 +static const gchash gnch = { name "-nmac", PRE##_HASHSZ, ghinit };    \
  static const ghash_ops gnops =                                                \
-   { &gch, ghhash, ghdone, ghdestroy, ghcopy };                                \
+   { &gnch, ghhash, ghdone, ghdestroy, ghcopy };                               \
 -static const gchash gsslch = { #pre "-sslmac", PRE##_HASHSZ, ghinit };        \
 +static const gchash gsslch = { name "-sslmac", PRE##_HASHSZ, ghinit };        \
  static const ghash_ops gsslops =                                      \
-   { &gch, ghhash, ghdone, ghdestroy, ghcopy };                                \
+   { &gsslch, ghhash, ghdone, ghdestroy, ghcopy };                     \
                                                                        \
 -HMAC_TEST(PRE, pre)
 +HMAC_TESTX(PRE, pre, name, fname)
 +
 +#define HMAC_TEST(PRE, pre) HMAC_TESTX(PRE, pre, #pre, #pre)
  
  /* --- @HMAC_TEST@ --- *
   *
  #include <mLib/quis.h>
  #include <mLib/testrig.h>
  
 -#define HMAC_TEST(PRE, pre)                                           \
 +#define HMAC_TESTX(PRE, pre, name, fname)                             \
                                                                        \
  static int macverify(dstr *v)                                         \
  {                                                                     \
  }                                                                     \
                                                                        \
  static test_chunk macdefs[] = {                                               \
 -  { #pre "-hmac", macverify,                                          \
 +  { name "-hmac", macverify,                                          \
      { &type_string, &type_hex, &type_hex, 0 } },                      \
    { 0, 0, { 0 } }                                                     \
  };                                                                    \
  int main(int argc, char *argv[])                                      \
  {                                                                     \
    ego(argv[0]);                                                               \
 -  test_run(argc, argv, macdefs, SRCDIR"/t/" #pre);                    \
 +  test_run(argc, argv, macdefs, SRCDIR"/t/" fname);                   \
    return (0);                                                         \
  }
  
  #else
 -#  define HMAC_TEST(PRE, pre)
 +#  define HMAC_TESTX(PRE, pre, name, fname)
  #endif
  
  /*----- That's all, folks -------------------------------------------------*/