Generate precomputed tables as sources in `precomps/'.
[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"
8b3d7f30 39
40/*----- Global variables --------------------------------------------------*/
41
42const octet rijndael_keysz[] = { KSZ_RANGE, RIJNDAEL_KEYSZ, 4, 32, 4 };
43
8b3d7f30 44/*----- Main code ---------------------------------------------------------*/
45
46/* --- @rijndael_setup@ --- *
47 *
48 * Arguments: @rijndael_ctx *k@ = pointer to context to initialize
49 * @unsigned nb@ = number of words in the block
50 * @const void *buf@ = pointer to buffer of key material
51 * @size_t sz@ = size of the key material
52 *
53 * Returns: ---
54 *
55 * Use: Low-level key-scheduling.
56 */
57
58void rijndael_setup(rijndael_ctx *k, unsigned nb, const void *buf, size_t sz)
59{
60 unsigned nk, nr, nw;
61 unsigned i, j, jj;
62 const octet *p;
63 uint32 ww;
64
65 /* --- Sort out the key size --- */
66
67 KSZ_ASSERT(rijndael, sz);
68 nk = sz / 4;
69
70 /* --- Select the number of rounds --- */
71
72 nr = (nk > nb ? nk : nb) + 6;
73 if (nr < 10)
74 nr = 10;
75 k->nr = nr;
76
77 /* --- Fetch the first key words out --- */
78
79 p = buf;
80 for (i = 0; i < nk; i++) {
38333dc2 81 k->w[i] = LOAD32_B(p);
8b3d7f30 82 p += 4;
83 }
84
85 /* --- Expand this material to fill the rest of the table --- */
86
87 nw = (nr + 1) * nb;
88 ww = k->w[i - 1];
89 p = RCON;
90 for (; i < nw; i++) {
91 uint32 w = k->w[i - nk];
92 if (i % nk == 0) {
38333dc2
MW
93 ww = ROL32(ww, 8);
94 w ^= SUB(S, ww, ww, ww, ww) ^ (*p++ << 24);
8b3d7f30 95 } else if (nk > 6 && i % nk == 4)
96 w ^= SUB(S, ww, ww, ww, ww);
97 else
98 w ^= ww;
99 k->w[i] = ww = w;
100 }
101
102 /* --- Make the decryption keys --- */
103
104 j = nw; i = 0;
105
106 j -= nb; jj = 0;
107 for (; i < nb; i++)
108 k->wi[i] = k->w[j + jj++];
109
110 for (; i < nw - nb; i += nb) {
111 j -= nb;
112 for (jj = 0; jj < nb; jj++) {
113 uint32 w = k->w[j + jj];
114 k->wi[i + jj] = MIX(U, w, w, w, w);
115 }
116 }
117
118 j -= nb; jj = 0;
119 for (; i < nw; i++)
120 k->wi[i] = k->w[j + jj++];
121}
122
123/*----- That's all, folks -------------------------------------------------*/