d3ec0d65ef8c8eb7d24ebe4cca95120479bae823
3 * Low-level stuff for all Rijndael block sizes
5 * (c) 2001 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
33 #include <mLib/bits.h>
38 #include "rijndael-base.h"
39 #include "rijndael-tab.h"
41 /*----- Global variables --------------------------------------------------*/
43 const octet rijndael_keysz
[] = { KSZ_RANGE
, RIJNDAEL_KEYSZ
, 4, 32, 4 };
45 /*----- Constant tables ---------------------------------------------------*/
47 const octet rijndael_s
[256] = RIJNDAEL_S
;
48 const octet rijndael_si
[256] = RIJNDAEL_SI
;
49 const uint32 rijndael_t
[4][256] = RIJNDAEL_T
;
50 const uint32 rijndael_ti
[4][256] = RIJNDAEL_TI
;
51 const uint32 rijndael_u
[4][256] = RIJNDAEL_U
;
52 const octet rijndael_rcon
[] = RIJNDAEL_RCON
;
54 /*----- Main code ---------------------------------------------------------*/
56 /* --- @rijndael_setup@ --- *
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
65 * Use: Low-level key-scheduling.
68 void rijndael_setup(rijndael_ctx
*k
, unsigned nb
, const void *buf
, size_t sz
)
75 /* --- Sort out the key size --- */
77 KSZ_ASSERT(rijndael
, sz
);
80 /* --- Select the number of rounds --- */
82 nr
= (nk
> nb ? nk
: nb
) + 6;
87 /* --- Fetch the first key words out --- */
90 for (i
= 0; i
< nk
; i
++) {
91 k
->w
[i
] = LOAD32_B(p
);
95 /* --- Expand this material to fill the rest of the table --- */
100 for (; i
< nw
; i
++) {
101 uint32 w
= k
->w
[i
- nk
];
104 w
^= SUB(S
, ww
, ww
, ww
, ww
) ^ (*p
++ << 24);
105 } else if (nk
> 6 && i
% nk
== 4)
106 w
^= SUB(S
, ww
, ww
, ww
, ww
);
112 /* --- Make the decryption keys --- */
118 k
->wi
[i
] = k
->w
[j
+ jj
++];
120 for (; i
< nw
- nb
; i
+= 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
);
130 k
->wi
[i
] = k
->w
[j
+ jj
++];
133 /*----- That's all, folks -------------------------------------------------*/