3 * $Id: rc5.c,v 1.1 1999/09/03 08:41:12 mdw Exp $
5 * The RC5-32/12 block cipher
7 * (c) 1999 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 --------------------------------------------------*
33 * Revision 1.1 1999/09/03 08:41:12 mdw
38 /*----- Header files ------------------------------------------------------*/
44 #include <mLib/alloc.h>
45 #include <mLib/bits.h>
50 /*----- Internal magical constants ----------------------------------------*/
54 #define T ((RC5_ROUNDS + 1) * 2)
58 /*----- Main code ---------------------------------------------------------*/
60 /* --- @rc5_init@ --- *
62 * Arguments: @rc5_ctx *k@ = pointer to a key block
63 * @const void *sbuf@ = pointer to key material
64 * @size_t sz@ = size of the key material
68 * Use: Initializes an RC5 key block.
71 void rc5_init(rc5_ctx
*k
, const void *sbuf
, size_t sz
)
76 /* --- Set up the @L@ table --- *
78 * This is slightly unfortunately defined.
81 assert(((void)"RC5 does not support zero-length keys", sz
!= 0));
85 const octet
*p
= sbuf
;
87 /* --- Create the buffer --- */
90 l
= xmalloc(w
* sizeof(uint32
));
92 /* --- Extract the key material --- */
94 for (i
= 0; sz
> 3; i
++) {
100 /* --- Fix up the tail end --- */
104 if (sz
> 1) x
|= (U8(*p
++) << 8);
105 if (sz
> 2) x
|= (U8(*p
++) << 16);
110 /* --- Initialize the @S@ table --- */
116 for (i
= 1; i
< T
; i
++)
117 k
->s
[i
] = k
->s
[i
- 1] + Q
;
120 /* --- Mix in the key --- */
123 int m
= 3 * (w
> T ? w
: T
);
127 for (c
= i
= j
= a
= b
= 0; c
< m
; c
++) {
131 k
->s
[i
] = a
= ROL32(x
, 3);
132 i
++; if (i
>= T
) i
= 0;
135 l
[j
] = b
= ROL32(x
, a
+ b
);
136 j
++; if (j
>= w
) j
= 0;
143 /* --- @EROUND@, @DROUND@ --- */
145 #define EROUND(x, y, k) do { \
147 _x = x ^ y; x = ROL32(_x, y) + k[0]; \
148 _x = y ^ x; y = ROL32(_x, x) + k[1]; \
152 #define DROUND(x, y, k) do { \
155 _x = y - k[1]; y = ROR32(_x, x) ^ x; \
156 _x = x - k[0]; x = ROR32(_x, y) ^ y; \
159 /* --- @EBLK@, @DBLK@ --- */
161 #define EBLK(a, b, c, d, k) do { \
163 const uint32 *_k = (k)->s; \
169 EROUND(_l, _r, _k); \
170 EROUND(_l, _r, _k); \
171 EROUND(_l, _r, _k); \
172 EROUND(_l, _r, _k); \
173 EROUND(_l, _r, _k); \
174 EROUND(_l, _r, _k); \
175 EROUND(_l, _r, _k); \
176 EROUND(_l, _r, _k); \
177 EROUND(_l, _r, _k); \
178 EROUND(_l, _r, _k); \
179 EROUND(_l, _r, _k); \
180 EROUND(_l, _r, _k); \
185 #define DBLK(a, b, c, d, k) do { \
187 const uint32 *_k = (k)->s + T; \
192 DROUND(_l, _r, _k); \
193 DROUND(_l, _r, _k); \
194 DROUND(_l, _r, _k); \
195 DROUND(_l, _r, _k); \
196 DROUND(_l, _r, _k); \
197 DROUND(_l, _r, _k); \
198 DROUND(_l, _r, _k); \
199 DROUND(_l, _r, _k); \
200 DROUND(_l, _r, _k); \
201 DROUND(_l, _r, _k); \
202 DROUND(_l, _r, _k); \
203 DROUND(_l, _r, _k); \
210 /* --- @rc5_eblk@, @rc5_dblk@ --- *
212 * Arguments: @const rc5_ctx *k@ = pointer to RC5 context block
213 * @const uint32 s[2]@ = pointer to source block
214 * @uint32 *d[2]@ = pointer to destination block
218 * Use: Low level block encryption and decryption.
221 void rc5_eblk(const rc5_ctx
*k
, const uint32
*s
, uint32
*d
)
223 EBLK(s
[0], s
[1], d
[0], d
[1], k
);
226 void rc5_dblk(const rc5_ctx
*k
, const uint32
*s
, uint32
*d
)
228 DBLK(s
[0], s
[1], d
[0], d
[1], k
);
231 /* --- Test rig --- */
235 /*----- That's all, folks -------------------------------------------------*/