Major restructuring of the internals of SILC Cipher API
[crypto.git] / lib / silccrypt / rc5.c
1 /*
2  * rc5.c                                RC5-32/16/b
3  *
4  * Copyright (c) 1999 Pekka Riikonen <priikone@poseidon.pspt.fi>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish, dis-
10  * tribute, sublicense, and/or sell copies of the Software, and to permit
11  * persons to whom the Software is furnished to do so, subject to the fol-
12  * lowing conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
19  * ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT
20  * SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABIL-
21  * ITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23  * IN THE SOFTWARE.
24  *
25  * Except as contained in this notice, the name of the authors shall
26  * not be used in advertising or otherwise to promote the sale, use or
27  * other dealings in this Software without prior written authorization from
28  * the authors.
29  *
30  */
31
32 /*
33  * Based on RC5 reference code and on description of Bruce Schneier's
34  * Applied Cryptography.
35  *
36  * This implementation has a word size of 32 bits, a rounds of 16 and
37  * variable key length from 128 and 192 up to 256 bits.
38  *
39  */
40
41 #include "silccrypto.h"
42 #include "rc5_internal.h"
43 #include "rc5.h"
44
45 /*
46  * SILC Crypto API for RC5
47  */
48
49 /* Sets the key for the cipher. */
50
51 SILC_CIPHER_API_SET_KEY(rc5_cbc)
52 {
53   SilcUInt32 k[8];
54
55   SILC_GET_WORD_KEY(key, k, keylen);
56   rc5_set_key((RC5Context *)context, k, keylen);
57
58   return TRUE;
59 }
60
61 /* Sets IV for the cipher. */
62
63 SILC_CIPHER_API_SET_IV(rc5_cbc)
64 {
65
66 }
67
68 /* Initialize */
69
70 SILC_CIPHER_API_INIT(rc5_cbc)
71 {
72   return silc_calloc(1, sizeof(RC5Context));
73 }
74
75 /* Initialize */
76
77 SILC_CIPHER_API_UNINIT(rc5_cbc)
78 {
79   RC5Context *rc5 = context;
80   memset(rc5, 0, sizeof(*rc5));
81   silc_free(rc5);
82 }
83
84 /* Encrypts with the cipher in CBC mode. Source and destination buffers
85    maybe one and same. */
86
87 SILC_CIPHER_API_ENCRYPT(rc5_cbc)
88 {
89   SilcUInt32 tiv[4];
90   int i;
91
92   SILC_CBC_GET_IV(tiv, iv);
93
94   SILC_CBC_ENC_PRE(tiv, src);
95   rc5_encrypt((RC5Context *)context, tiv, tiv);
96   SILC_CBC_ENC_POST(tiv, dst, src);
97
98   for (i = 16; i < len; i += 16) {
99     SILC_CBC_ENC_PRE(tiv, src);
100     rc5_encrypt((RC5Context *)context, tiv, tiv);
101     SILC_CBC_ENC_POST(tiv, dst, src);
102   }
103
104   SILC_CBC_PUT_IV(tiv, iv);
105
106   return TRUE;
107 }
108
109 /* Decrypts with the cipher in CBC mode. Source and destination buffers
110    maybe one and same. */
111
112 SILC_CIPHER_API_DECRYPT(rc5_cbc)
113 {
114   SilcUInt32 tmp[4], tmp2[4], tiv[4];
115   int i;
116
117   SILC_CBC_GET_IV(tiv, iv);
118
119   SILC_CBC_DEC_PRE(tmp, src);
120   rc5_decrypt((RC5Context *)context, tmp, tmp2);
121   SILC_CBC_DEC_POST(tmp2, dst, src, tmp, tiv);
122
123   for (i = 16; i < len; i += 16) {
124     SILC_CBC_DEC_PRE(tmp, src);
125     rc5_decrypt((RC5Context *)context, tmp, tmp2);
126     SILC_CBC_DEC_POST(tmp2, dst, src, tmp, tiv);
127   }
128
129   SILC_CBC_PUT_IV(tiv, iv);
130
131   return TRUE;
132 }
133
134 /* RC5 encryption */
135 #define RC5E(i, A, B)                           \
136                 A = A ^ B;                      \
137                 A = rotl(A, B) + S[i];          \
138                 B = B ^ A;                      \
139                 B = rotl(B, A) + S[i + 1];
140
141 /* RC5 decryption */
142 #define RC5D(i, A, B)                           \
143                 B = B - S[i + 1];               \
144                 B = rotr(B, A) ^ A;             \
145                 A = A - S[i];                   \
146                 A = rotr(A, B) ^ B;
147
148 /* Sets RC5 key */
149
150 int rc5_set_key(RC5Context *ctx, const SilcUInt32 in_key[], int key_len)
151 {
152         u32 i, j, k, A, B, L[c];
153         u32 *out_key = ctx->out_key;
154
155         if (key_len < b || key_len > (2 * b))
156                 return -1;
157
158         /* init L */
159         for (i = 0; i < key_len / w; i++)
160                 L[i] = in_key[i];
161
162         /* init key array (S) */
163         out_key[0] = 0xb7e15163;
164         for (i = 1; i < t; i++)
165                 out_key[i] = out_key[i - 1] + 0x9e3779b9;
166
167         /* mix L and key array (S) */
168         A = B = 0;
169         for (k = i = j = 0; k < (3 * t); k++) {
170                 A = rotl(out_key[i] + (A + B), 3);
171                 B += A;
172                 B = rotl(L[j] + B, B);
173                 out_key[i] = A;
174                 L[j] = B;
175                 i = (i + 1) % t;
176                 j = (j + 1) % c;
177         }
178
179         return 0;
180 }
181
182 /* Encrypts *one* block at a time. */
183
184 int rc5_encrypt(RC5Context *ctx, u32 *in, u32 *out)
185 {
186         u32 A, B;
187         u32 *S = ctx->out_key;
188
189         A = in[0] + S[0];
190         B = in[1] + S[1];
191
192         RC5E(2, A, B); RC5E(4, A, B);
193         RC5E(6, A, B); RC5E(8, A, B);
194         RC5E(10, A, B); RC5E(12, A, B);
195         RC5E(14, A, B); RC5E(16, A, B);
196         RC5E(18, A, B); RC5E(20, A, B);
197         RC5E(22, A, B); RC5E(24, A, B);
198         RC5E(26, A, B); RC5E(28, A, B);
199         RC5E(30, A, B); RC5E(32, A, B);
200
201         out[0] = A;
202         out[1] = B;
203
204         return 0;
205 }
206
207 /* Decrypts *one* block at a time. */
208
209 int rc5_decrypt(RC5Context *ctx, u32 *in, u32 *out)
210 {
211         u32 A, B;
212         u32 *S = ctx->out_key;
213
214         A = in[0];
215         B = in[1];
216
217         RC5D(32, A, B); RC5D(30, A, B);
218         RC5D(28, A, B); RC5D(26, A, B);
219         RC5D(24, A, B); RC5D(22, A, B);
220         RC5D(20, A, B); RC5D(18, A, B);
221         RC5D(16, A, B); RC5D(14, A, B);
222         RC5D(12, A, B); RC5D(10, A, B);
223         RC5D(8, A, B); RC5D(6, A, B);
224         RC5D(4, A, B); RC5D(2, A, B);
225
226         out[0] = A - S[0];
227         out[1] = B - S[1];
228
229         return 0;
230 }