Rearrange the file tree.
[u/mdw/catacomb] / symm / rijndael-base.c
CommitLineData
8b3d7f30 1/* -*-c-*-
2 *
8b3d7f30 3 * Low-level stuff for all Rijndael block sizes
4 *
5 * (c) 2001 Straylight/Edgeware
6 */
7
45c0fd36 8/*----- Licensing notice --------------------------------------------------*
8b3d7f30 9 *
10 * This file is part of Catacomb.
11 *
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
45c0fd36 16 *
8b3d7f30 17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
45c0fd36 21 *
8b3d7f30 22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
8b3d7f30 28/*----- Header files ------------------------------------------------------*/
29
30#include <assert.h>
31#include <stdio.h>
32
33#include <mLib/bits.h>
34
35#include "blkc.h"
36#include "gcipher.h"
37#include "rijndael.h"
38#include "rijndael-base.h"
39#include "rijndael-tab.h"
40
41/*----- Global variables --------------------------------------------------*/
42
43const octet rijndael_keysz[] = { KSZ_RANGE, RIJNDAEL_KEYSZ, 4, 32, 4 };
44
45/*----- Constant tables ---------------------------------------------------*/
46
47const octet rijndael_s[256] = RIJNDAEL_S;
48const octet rijndael_si[256] = RIJNDAEL_SI;
49const uint32 rijndael_t[4][256] = RIJNDAEL_T;
50const uint32 rijndael_ti[4][256] = RIJNDAEL_TI;
51const uint32 rijndael_u[4][256] = RIJNDAEL_U;
52const octet rijndael_rcon[] = RIJNDAEL_RCON;
53
54/*----- Main code ---------------------------------------------------------*/
55
56/* --- @rijndael_setup@ --- *
57 *
58 * Arguments: @rijndael_ctx *k@ = pointer to context to initialize
59 * @unsigned nb@ = number of words in the block
60 * @const void *buf@ = pointer to buffer of key material
61 * @size_t sz@ = size of the key material
62 *
63 * Returns: ---
64 *
65 * Use: Low-level key-scheduling.
66 */
67
68void rijndael_setup(rijndael_ctx *k, unsigned nb, const void *buf, size_t sz)
69{
70 unsigned nk, nr, nw;
71 unsigned i, j, jj;
72 const octet *p;
73 uint32 ww;
74
75 /* --- Sort out the key size --- */
76
77 KSZ_ASSERT(rijndael, sz);
78 nk = sz / 4;
79
80 /* --- Select the number of rounds --- */
81
82 nr = (nk > nb ? nk : nb) + 6;
83 if (nr < 10)
84 nr = 10;
85 k->nr = nr;
86
87 /* --- Fetch the first key words out --- */
88
89 p = buf;
90 for (i = 0; i < nk; i++) {
38333dc2 91 k->w[i] = LOAD32_B(p);
8b3d7f30 92 p += 4;
93 }
94
95 /* --- Expand this material to fill the rest of the table --- */
96
97 nw = (nr + 1) * nb;
98 ww = k->w[i - 1];
99 p = RCON;
100 for (; i < nw; i++) {
101 uint32 w = k->w[i - nk];
102 if (i % nk == 0) {
38333dc2
MW
103 ww = ROL32(ww, 8);
104 w ^= SUB(S, ww, ww, ww, ww) ^ (*p++ << 24);
8b3d7f30 105 } else if (nk > 6 && i % nk == 4)
106 w ^= SUB(S, ww, ww, ww, ww);
107 else
108 w ^= ww;
109 k->w[i] = ww = w;
110 }
111
112 /* --- Make the decryption keys --- */
113
114 j = nw; i = 0;
115
116 j -= nb; jj = 0;
117 for (; i < nb; i++)
118 k->wi[i] = k->w[j + jj++];
119
120 for (; i < nw - nb; i += nb) {
121 j -= nb;
122 for (jj = 0; jj < nb; jj++) {
123 uint32 w = k->w[j + jj];
124 k->wi[i + jj] = MIX(U, w, w, w, w);
125 }
126 }
127
128 j -= nb; jj = 0;
129 for (; i < nw; i++)
130 k->wi[i] = k->w[j + jj++];
131}
132
133/*----- That's all, folks -------------------------------------------------*/