3 * $Id: rijndael-base.c,v 1.1 2001/05/07 17:31:37 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 /*----- Revision history --------------------------------------------------*
32 * $Log: rijndael-base.c,v $
33 * Revision 1.1 2001/05/07 17:31:37 mdw
34 * Centralize Rijndael tables and key scheduling.
38 /*----- Header files ------------------------------------------------------*/
43 #include <mLib/bits.h>
48 #include "rijndael-base.h"
49 #include "rijndael-tab.h"
51 /*----- Global variables --------------------------------------------------*/
53 const octet rijndael_keysz[] = { KSZ_RANGE, RIJNDAEL_KEYSZ, 4, 32, 4 };
55 /*----- Constant tables ---------------------------------------------------*/
57 const octet rijndael_s[256] = RIJNDAEL_S;
58 const octet rijndael_si[256] = RIJNDAEL_SI;
59 const uint32 rijndael_t[4][256] = RIJNDAEL_T;
60 const uint32 rijndael_ti[4][256] = RIJNDAEL_TI;
61 const uint32 rijndael_u[4][256] = RIJNDAEL_U;
62 const octet rijndael_rcon[] = RIJNDAEL_RCON;
64 /*----- Main code ---------------------------------------------------------*/
66 /* --- @rijndael_setup@ --- *
68 * Arguments: @rijndael_ctx *k@ = pointer to context to initialize
69 * @unsigned nb@ = number of words in the block
70 * @const void *buf@ = pointer to buffer of key material
71 * @size_t sz@ = size of the key material
75 * Use: Low-level key-scheduling.
78 void rijndael_setup(rijndael_ctx *k, unsigned nb, const void *buf, size_t sz)
85 /* --- Sort out the key size --- */
87 KSZ_ASSERT(rijndael, sz);
90 /* --- Select the number of rounds --- */
92 nr = (nk > nb ? nk : nb) + 6;
97 /* --- Fetch the first key words out --- */
100 for (i = 0; i < nk; i++) {
101 k->w[i] = LOAD32_L(p);
105 /* --- Expand this material to fill the rest of the table --- */
110 for (; i < nw; i++) {
111 uint32 w = k->w[i - nk];
114 w ^= SUB(S, ww, ww, ww, ww) ^ *p++;
115 } else if (nk > 6 && i % nk == 4)
116 w ^= SUB(S, ww, ww, ww, ww);
122 /* --- Make the decryption keys --- */
128 k->wi[i] = k->w[j + jj++];
130 for (; i < nw - nb; i += nb) {
132 for (jj = 0; jj < nb; jj++) {
133 uint32 w = k->w[j + jj];
134 k->wi[i + jj] = MIX(U, w, w, w, w);
140 k->wi[i] = k->w[j + jj++];
143 /*----- That's all, folks -------------------------------------------------*/