From 7cee294a99758676a598cf0b3b914b0dea42d506 Mon Sep 17 00:00:00 2001 From: mdw Date: Mon, 7 May 2001 17:31:53 +0000 Subject: [PATCH] Separate out key scheduling. --- rijndael.c | 88 +++++--------------------------------------------------------- rijndael.h | 24 ++++++++++++++--- 2 files changed, 27 insertions(+), 85 deletions(-) diff --git a/rijndael.c b/rijndael.c index c2bb8f3..981e79c 100644 --- a/rijndael.c +++ b/rijndael.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: rijndael.c,v 1.3 2001/05/07 15:44:46 mdw Exp $ + * $Id: rijndael.c,v 1.4 2001/05/07 17:31:53 mdw Exp $ * * The Rijndael block cipher * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: rijndael.c,v $ + * Revision 1.4 2001/05/07 17:31:53 mdw + * Separate out key scheduling. + * * Revision 1.3 2001/05/07 15:44:46 mdw * Fix unusual numbers of rounds. Simplify implementation. * @@ -51,29 +54,10 @@ #include "blkc.h" #include "gcipher.h" #include "rijndael.h" -#include "rijndael-tab.h" - -/*----- Global variables --------------------------------------------------*/ - -const octet rijndael_keysz[] = { KSZ_RANGE, RIJNDAEL_KEYSZ, 4, 32, 4 }; - -/*----- Constant tables ---------------------------------------------------*/ - -static const octet S[256] = RIJNDAEL_S, SI[256] = RIJNDAEL_SI; -static const uint32 T[4][256] = RIJNDAEL_T, TI[4][256] = RIJNDAEL_TI; -static const uint32 U[4][256] = RIJNDAEL_U; -static const octet rcon[] = RIJNDAEL_RCON; +#include "rijndael-base.h" /*----- Main code ---------------------------------------------------------*/ -#define SUB(s, a, b, c, d) \ - (s[U8((a) >> 0)] << 0 | s[U8((b) >> 8)] << 8 | \ - s[U8((c) >> 16)] << 16 | s[U8((d) >> 24)] << 24) - -#define MIX(t, a, b, c, d) \ - (t[0][U8((a) >> 0)] ^ t[1][U8((b) >> 8)] ^ \ - t[2][U8((c) >> 16)] ^ t[3][U8((d) >> 24)]) - /* --- @rijndael_init@ --- * * * Arguments: @rijndael_ctx *k@ = pointer to context to initialize @@ -90,67 +74,7 @@ static const octet rcon[] = RIJNDAEL_RCON; void rijndael_init(rijndael_ctx *k, const void *buf, size_t sz) { - unsigned nk, nr, nw; - unsigned i, j, jj; - const octet *p; - uint32 ww; - - /* --- Sort out the key size --- */ - - KSZ_ASSERT(rijndael, sz); - nk = sz / 4; - - /* --- Select the number of rounds --- */ - - nr = nk + 6; - if (nr < 10) - nr = 10; - k->nr = nr; - - /* --- Fetch the first key words out --- */ - - p = buf; - for (i = 0; i < nk; i++) { - k->w[i] = LOAD32_L(p); - p += 4; - } - - /* --- Expand this material to fill the rest of the table --- */ - - nw = (nr + 1) * (RIJNDAEL_BLKSZ / 4); - ww = k->w[i - 1]; - p = rcon; - for (; i < nw; i++) { - uint32 w = k->w[i - nk]; - if (i % nk == 0) { - ww = ROR32(ww, 8); - w ^= SUB(S, ww, ww, ww, ww) ^ *p++; - } else if (nk > 6 && i % nk == 4) - w ^= SUB(S, ww, ww, ww, ww); - else - w ^= ww; - k->w[i] = ww = w; - } - - /* --- Make the decryption keys --- */ - - j = nw; - - j -= RIJNDAEL_BLKSZ / 4; jj = 0; - for (i = 0; i < RIJNDAEL_BLKSZ / 4; i++) - k->wi[i] = k->w[j + jj++]; - - for (; i < nw - RIJNDAEL_BLKSZ / 4; i += RIJNDAEL_BLKSZ / 4) { - j -= RIJNDAEL_BLKSZ / 4; - for (jj = 0; jj < RIJNDAEL_BLKSZ / 4; jj++) { - uint32 w = k->w[j + jj]; - k->wi[i + jj] = MIX(U, w, w, w, w); - } - } - - j -= RIJNDAEL_BLKSZ / 4; jj = 0; - for (; i < nw; i++) - k->wi[i] = k->w[j + jj++]; + rijndael_setup(k, RIJNDAEL_BLKSZ / 4, buf, sz); } /* --- @rijndael_eblk@, @rijndael_dblk@ --- * diff --git a/rijndael.h b/rijndael.h index 88463b7..282cef8 100644 --- a/rijndael.h +++ b/rijndael.h @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: rijndael.h,v 1.2 2000/10/08 15:48:58 mdw Exp $ + * $Id: rijndael.h,v 1.3 2001/05/07 17:31:53 mdw Exp $ * * The Rijndael block cipher * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: rijndael.h,v $ + * Revision 1.3 2001/05/07 17:31:53 mdw + * Separate out key scheduling. + * * Revision 1.2 2000/10/08 15:48:58 mdw * Update comments now that AES has been chosen. * @@ -71,8 +74,8 @@ extern const octet rijndael_keysz[]; /*----- Data structures ---------------------------------------------------*/ -#define RIJNDAEL_MAXROUNDS 32 -#define RIJNDAEL_KWORDS ((RIJNDAEL_MAXROUNDS + 1) * (RIJNDAEL_BLKSZ / 4)) +#define RIJNDAEL_MAXROUNDS 16 +#define RIJNDAEL_KWORDS ((RIJNDAEL_MAXROUNDS + 1) * 8) typedef struct rijndael_ctx { unsigned nr; @@ -82,6 +85,21 @@ typedef struct rijndael_ctx { /*----- Functions provided ------------------------------------------------*/ +/* --- @rijndael_setup@ --- * + * + * Arguments: @rijndael_ctx *k@ = pointer to context to initialize + * @unsigned nb@ = number of words in the block + * @const void *buf@ = pointer to buffer of key material + * @size_t sz@ = size of the key material + * + * Returns: --- + * + * Use: Low-level key-scheduling. Don't call this directly. + */ + +extern void rijndael_setup(rijndael_ctx */*k*/, unsigned /*nb*/, + const void */*buf*/, size_t /*sz*/); + /* --- @rijndael_init@ --- * * * Arguments: @rijndael_ctx *k@ = pointer to context to initialize -- 2.11.0