/* -*-c-*-
*
- * $Id: rijndael.c,v 1.3 2001/05/07 15:44:46 mdw Exp $
+ * $Id: rijndael.c,v 1.5 2004/04/08 01:36:15 mdw Exp $
*
* The Rijndael block cipher
*
* (c) 2000 Straylight/Edgeware
*/
-/*----- Licensing notice --------------------------------------------------*
+/*----- Licensing notice --------------------------------------------------*
*
* This file is part of Catacomb.
*
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
- *
+ *
* Catacomb is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
- *
+ *
* You should have received a copy of the GNU Library General Public
* License along with Catacomb; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA.
*/
-/*----- Revision history --------------------------------------------------*
- *
- * $Log: rijndael.c,v $
- * Revision 1.3 2001/05/07 15:44:46 mdw
- * Fix unusual numbers of rounds. Simplify implementation.
- *
- * Revision 1.2 2000/12/06 20:32:59 mdw
- * Fix round count for weird key sizes.
- *
- * Revision 1.1 2000/06/17 11:56:07 mdw
- * New cipher.
- *
- */
-
/*----- Header files ------------------------------------------------------*/
#include <assert.h>
#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
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@ --- *
{
uint32 a = s[0], b = s[1], c = s[2], d = s[3];
uint32 aa, bb, cc, dd;
- uint32 *w = k->w;
+ const uint32 *w = k->w;
a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++;
aa = a; bb = b; cc = c; dd = d;
{
uint32 a = s[0], b = s[1], c = s[2], d = s[3];
uint32 aa, bb, cc, dd;
- uint32 *w = k->wi;
+ const uint32 *w = k->wi;
a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++;
aa = a; bb = b; cc = c; dd = d;