3 * $Id: rijndael-base.c,v 1.2 2004/04/08 01:36:15 mdw Exp $
5 * Low-level stuff for all Rijndael block sizes
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Header files ------------------------------------------------------*/
35 #include <mLib/bits.h>
40 #include "rijndael-base.h"
41 #include "rijndael-tab.h"
43 /*----- Global variables --------------------------------------------------*/
45 const octet rijndael_keysz[] = { KSZ_RANGE, RIJNDAEL_KEYSZ, 4, 32, 4 };
47 /*----- Constant tables ---------------------------------------------------*/
49 const octet rijndael_s[256] = RIJNDAEL_S;
50 const octet rijndael_si[256] = RIJNDAEL_SI;
51 const uint32 rijndael_t[4][256] = RIJNDAEL_T;
52 const uint32 rijndael_ti[4][256] = RIJNDAEL_TI;
53 const uint32 rijndael_u[4][256] = RIJNDAEL_U;
54 const octet rijndael_rcon[] = RIJNDAEL_RCON;
56 /*----- Main code ---------------------------------------------------------*/
58 /* --- @rijndael_setup@ --- *
60 * Arguments: @rijndael_ctx *k@ = pointer to context to initialize
61 * @unsigned nb@ = number of words in the block
62 * @const void *buf@ = pointer to buffer of key material
63 * @size_t sz@ = size of the key material
67 * Use: Low-level key-scheduling.
70 void rijndael_setup(rijndael_ctx *k, unsigned nb, const void *buf, size_t sz)
77 /* --- Sort out the key size --- */
79 KSZ_ASSERT(rijndael, sz);
82 /* --- Select the number of rounds --- */
84 nr = (nk > nb ? nk : nb) + 6;
89 /* --- Fetch the first key words out --- */
92 for (i = 0; i < nk; i++) {
93 k->w[i] = LOAD32_L(p);
97 /* --- Expand this material to fill the rest of the table --- */
102 for (; i < nw; i++) {
103 uint32 w = k->w[i - nk];
106 w ^= SUB(S, ww, ww, ww, ww) ^ *p++;
107 } else if (nk > 6 && i % nk == 4)
108 w ^= SUB(S, ww, ww, ww, ww);
114 /* --- Make the decryption keys --- */
120 k->wi[i] = k->w[j + jj++];
122 for (; i < nw - nb; i += nb) {
124 for (jj = 0; jj < nb; jj++) {
125 uint32 w = k->w[j + jj];
126 k->wi[i + jj] = MIX(U, w, w, w, w);
132 k->wi[i] = k->w[j + jj++];
135 /*----- That's all, folks -------------------------------------------------*/