Added CTR mode to AES. Simplified cipher implementation API more.
[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 "silc.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 /* Returns the size of the cipher context. */
62
63 SILC_CIPHER_API_CONTEXT_LEN(rc5_cbc)
64 {
65   return sizeof(RC5Context);
66 }
67
68 /* Encrypts with the cipher in CBC mode. Source and destination buffers
69    maybe one and same. */
70
71 SILC_CIPHER_API_ENCRYPT(rc5_cbc)
72 {
73   SilcUInt32 tiv[4];
74   int i;
75
76   SILC_CBC_GET_IV(tiv, iv);
77
78   SILC_CBC_ENC_PRE(tiv, src);
79   rc5_encrypt((RC5Context *)context, tiv, tiv);
80   SILC_CBC_ENC_POST(tiv, dst, src);
81
82   for (i = 16; i < len; i += 16) {
83     SILC_CBC_ENC_PRE(tiv, src);
84     rc5_encrypt((RC5Context *)context, tiv, tiv);
85     SILC_CBC_ENC_POST(tiv, dst, src);
86   }
87
88   SILC_CBC_PUT_IV(tiv, iv);
89
90   return TRUE;
91 }
92
93 /* Decrypts with the cipher in CBC mode. Source and destination buffers
94    maybe one and same. */
95
96 SILC_CIPHER_API_DECRYPT(rc5_cbc)
97 {
98   SilcUInt32 tmp[4], tmp2[4], tiv[4];
99   int i;
100
101   SILC_CBC_GET_IV(tiv, iv);
102
103   SILC_CBC_DEC_PRE(tmp, src);
104   rc5_decrypt((RC5Context *)context, tmp, tmp2);
105   SILC_CBC_DEC_POST(tmp2, dst, src, tmp, tiv);
106
107   for (i = 16; i < len; i += 16) {
108     SILC_CBC_DEC_PRE(tmp, src);
109     rc5_decrypt((RC5Context *)context, tmp, tmp2);
110     SILC_CBC_DEC_POST(tmp2, dst, src, tmp, tiv);
111   }
112
113   SILC_CBC_PUT_IV(tiv, iv);
114
115   return TRUE;
116 }
117
118 /* RC5 encryption */
119 #define RC5E(i, A, B)                           \
120                 A = A ^ B;                      \
121                 A = rotl(A, B) + S[i];          \
122                 B = B ^ A;                      \
123                 B = rotl(B, A) + S[i + 1];
124
125 /* RC5 decryption */
126 #define RC5D(i, A, B)                           \
127                 B = B - S[i + 1];               \
128                 B = rotr(B, A) ^ A;             \
129                 A = A - S[i];                   \
130                 A = rotr(A, B) ^ B;
131
132 /* Sets RC5 key */
133
134 int rc5_set_key(RC5Context *ctx, const SilcUInt32 in_key[], int key_len)
135 {
136         u32 i, j, k, A, B, L[c];
137         u32 *out_key = ctx->out_key;
138
139         if (key_len < b || key_len > (2 * b))
140                 return -1;
141
142         /* init L */
143         for (i = 0; i < key_len / w; i++)
144                 L[i] = in_key[i];
145
146         /* init key array (S) */
147         out_key[0] = 0xb7e15163;
148         for (i = 1; i < t; i++)
149                 out_key[i] = out_key[i - 1] + 0x9e3779b9;
150
151         /* mix L and key array (S) */
152         A = B = 0;
153         for (k = i = j = 0; k < (3 * t); k++) {
154                 A = rotl(out_key[i] + (A + B), 3);
155                 B += A;
156                 B = rotl(L[j] + B, B);
157                 out_key[i] = A;
158                 L[j] = B;
159                 i = (i + 1) % t;
160                 j = (j + 1) % c;
161         }
162
163         return 0;
164 }
165
166 /* Encrypts *one* block at a time. */
167
168 int rc5_encrypt(RC5Context *ctx, u32 *in, u32 *out)
169 {
170         u32 A, B;
171         u32 *S = ctx->out_key;
172
173         A = in[0] + S[0];
174         B = in[1] + S[1];
175
176         RC5E(2, A, B); RC5E(4, A, B);
177         RC5E(6, A, B); RC5E(8, A, B);
178         RC5E(10, A, B); RC5E(12, A, B);
179         RC5E(14, A, B); RC5E(16, A, B);
180         RC5E(18, A, B); RC5E(20, A, B);
181         RC5E(22, A, B); RC5E(24, A, B);
182         RC5E(26, A, B); RC5E(28, A, B);
183         RC5E(30, A, B); RC5E(32, A, B);
184
185         out[0] = A;
186         out[1] = B;
187
188         return 0;
189 }
190
191 /* Decrypts *one* block at a time. */
192
193 int rc5_decrypt(RC5Context *ctx, u32 *in, u32 *out)
194 {
195         u32 A, B;
196         u32 *S = ctx->out_key;
197
198         A = in[0];
199         B = in[1];
200
201         RC5D(32, A, B); RC5D(30, A, B);
202         RC5D(28, A, B); RC5D(26, A, B);
203         RC5D(24, A, B); RC5D(22, A, B);
204         RC5D(20, A, B); RC5D(18, A, B);
205         RC5D(16, A, B); RC5D(14, A, B);
206         RC5D(12, A, B); RC5D(10, A, B);
207         RC5D(8, A, B); RC5D(6, A, B);
208         RC5D(4, A, B); RC5D(2, A, B);
209
210         out[0] = A - S[0];
211         out[1] = B - S[1];
212
213         return 0;
214 }