]> pd.if.org Git - zpackage/blob - libtomcrypt/src/ciphers/safer/saferp.c
116590ff68a98c5c60fa34a5f26ca8708cb9fcba
[zpackage] / libtomcrypt / src / ciphers / safer / saferp.c
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2  *
3  * LibTomCrypt is a library that provides various cryptographic
4  * algorithms in a highly modular and flexible manner.
5  *
6  * The library is free for all purposes without any express
7  * guarantee it works.
8  */
9
10 /**
11    @file saferp.c
12    LTC_SAFER+ Implementation by Tom St Denis
13 */
14 #include "tomcrypt.h"
15
16 #ifdef LTC_SAFERP
17
18 #define __LTC_SAFER_TAB_C__
19 #include "safer_tab.c"
20
21 const struct ltc_cipher_descriptor saferp_desc =
22 {
23     "safer+",
24     4,
25     16, 32, 16, 8,
26     &saferp_setup,
27     &saferp_ecb_encrypt,
28     &saferp_ecb_decrypt,
29     &saferp_test,
30     &saferp_done,
31     &saferp_keysize,
32     NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
33 };
34
35 /* ROUND(b,i)
36  *
37  * This is one forward key application.  Note the basic form is
38  * key addition, substitution, key addition.  The safer_ebox and safer_lbox
39  * are the exponentiation box and logarithm boxes respectively.
40  * The value of 'i' is the current round number which allows this
41  * function to be unrolled massively.  Most of LTC_SAFER+'s speed
42  * comes from not having to compute indirect accesses into the
43  * array of 16 bytes b[0..15] which is the block of data
44 */
45
46 #define ROUND(b, i) do {                                                                         \
47     b[0]  = (safer_ebox[(b[0] ^ skey->saferp.K[i][0]) & 255] + skey->saferp.K[i+1][0]) & 255;    \
48     b[1]  = safer_lbox[(b[1] + skey->saferp.K[i][1]) & 255] ^ skey->saferp.K[i+1][1];            \
49     b[2]  = safer_lbox[(b[2] + skey->saferp.K[i][2]) & 255] ^ skey->saferp.K[i+1][2];            \
50     b[3]  = (safer_ebox[(b[3] ^ skey->saferp.K[i][3]) & 255] + skey->saferp.K[i+1][3]) & 255;    \
51     b[4]  = (safer_ebox[(b[4] ^ skey->saferp.K[i][4]) & 255] + skey->saferp.K[i+1][4]) & 255;    \
52     b[5]  = safer_lbox[(b[5] + skey->saferp.K[i][5]) & 255] ^ skey->saferp.K[i+1][5];            \
53     b[6]  = safer_lbox[(b[6] + skey->saferp.K[i][6]) & 255] ^ skey->saferp.K[i+1][6];            \
54     b[7]  = (safer_ebox[(b[7] ^ skey->saferp.K[i][7]) & 255] + skey->saferp.K[i+1][7]) & 255;    \
55     b[8]  = (safer_ebox[(b[8] ^ skey->saferp.K[i][8]) & 255] + skey->saferp.K[i+1][8]) & 255;    \
56     b[9]  = safer_lbox[(b[9] + skey->saferp.K[i][9]) & 255] ^ skey->saferp.K[i+1][9];            \
57     b[10] = safer_lbox[(b[10] + skey->saferp.K[i][10]) & 255] ^ skey->saferp.K[i+1][10];         \
58     b[11] = (safer_ebox[(b[11] ^ skey->saferp.K[i][11]) & 255] + skey->saferp.K[i+1][11]) & 255; \
59     b[12] = (safer_ebox[(b[12] ^ skey->saferp.K[i][12]) & 255] + skey->saferp.K[i+1][12]) & 255; \
60     b[13] = safer_lbox[(b[13] + skey->saferp.K[i][13]) & 255] ^ skey->saferp.K[i+1][13];         \
61     b[14] = safer_lbox[(b[14] + skey->saferp.K[i][14]) & 255] ^ skey->saferp.K[i+1][14];         \
62     b[15] = (safer_ebox[(b[15] ^ skey->saferp.K[i][15]) & 255] + skey->saferp.K[i+1][15]) & 255; \
63 } while (0)
64
65 /* This is one inverse key application */
66 #define iROUND(b, i) do {                                                                        \
67     b[0]  = safer_lbox[(b[0] - skey->saferp.K[i+1][0]) & 255] ^ skey->saferp.K[i][0];            \
68     b[1]  = (safer_ebox[(b[1] ^ skey->saferp.K[i+1][1]) & 255] - skey->saferp.K[i][1]) & 255;    \
69     b[2]  = (safer_ebox[(b[2] ^ skey->saferp.K[i+1][2]) & 255] - skey->saferp.K[i][2]) & 255;    \
70     b[3]  = safer_lbox[(b[3] - skey->saferp.K[i+1][3]) & 255] ^ skey->saferp.K[i][3];            \
71     b[4]  = safer_lbox[(b[4] - skey->saferp.K[i+1][4]) & 255] ^ skey->saferp.K[i][4];            \
72     b[5]  = (safer_ebox[(b[5] ^ skey->saferp.K[i+1][5]) & 255] - skey->saferp.K[i][5]) & 255;    \
73     b[6]  = (safer_ebox[(b[6] ^ skey->saferp.K[i+1][6]) & 255] - skey->saferp.K[i][6]) & 255;    \
74     b[7]  = safer_lbox[(b[7] - skey->saferp.K[i+1][7]) & 255] ^ skey->saferp.K[i][7];            \
75     b[8]  = safer_lbox[(b[8] - skey->saferp.K[i+1][8]) & 255] ^ skey->saferp.K[i][8];            \
76     b[9]  = (safer_ebox[(b[9] ^ skey->saferp.K[i+1][9]) & 255] - skey->saferp.K[i][9]) & 255;    \
77     b[10] = (safer_ebox[(b[10] ^ skey->saferp.K[i+1][10]) & 255] - skey->saferp.K[i][10]) & 255; \
78     b[11] = safer_lbox[(b[11] - skey->saferp.K[i+1][11]) & 255] ^ skey->saferp.K[i][11];         \
79     b[12] = safer_lbox[(b[12] - skey->saferp.K[i+1][12]) & 255] ^ skey->saferp.K[i][12];         \
80     b[13] = (safer_ebox[(b[13] ^ skey->saferp.K[i+1][13]) & 255] - skey->saferp.K[i][13]) & 255; \
81     b[14] = (safer_ebox[(b[14] ^ skey->saferp.K[i+1][14]) & 255] - skey->saferp.K[i][14]) & 255; \
82     b[15] = safer_lbox[(b[15] - skey->saferp.K[i+1][15]) & 255] ^ skey->saferp.K[i][15];         \
83 } while (0)
84
85 /* This is a forward single layer PHT transform.  */
86 #define PHT(b) do {                                          \
87     b[0]  = (b[0] + (b[1] = (b[0] + b[1]) & 255)) & 255;     \
88     b[2]  = (b[2] + (b[3] = (b[3] + b[2]) & 255)) & 255;     \
89     b[4]  = (b[4] + (b[5] = (b[5] + b[4]) & 255)) & 255;     \
90     b[6]  = (b[6] + (b[7] = (b[7] + b[6]) & 255)) & 255;     \
91     b[8]  = (b[8] + (b[9] = (b[9] + b[8]) & 255)) & 255;     \
92     b[10] = (b[10] + (b[11] = (b[11] + b[10]) & 255)) & 255; \
93     b[12] = (b[12] + (b[13] = (b[13] + b[12]) & 255)) & 255; \
94     b[14] = (b[14] + (b[15] = (b[15] + b[14]) & 255)) & 255; \
95 } while (0)
96
97 /* This is an inverse single layer PHT transform */
98 #define iPHT(b) do {                                          \
99     b[15] = (b[15] - (b[14] = (b[14] - b[15]) & 255)) & 255;  \
100     b[13] = (b[13] - (b[12] = (b[12] - b[13]) & 255)) & 255;  \
101     b[11] = (b[11] - (b[10] = (b[10] - b[11]) & 255)) & 255;  \
102     b[9]  = (b[9] - (b[8] = (b[8] - b[9]) & 255)) & 255;      \
103     b[7]  = (b[7] - (b[6] = (b[6] - b[7]) & 255)) & 255;      \
104     b[5]  = (b[5] - (b[4] = (b[4] - b[5]) & 255)) & 255;      \
105     b[3]  = (b[3] - (b[2] = (b[2] - b[3]) & 255)) & 255;      \
106     b[1]  = (b[1] - (b[0] = (b[0] - b[1]) & 255)) & 255;      \
107  } while (0)
108
109 /* This is the "Armenian" Shuffle.  It takes the input from b and stores it in b2 */
110 #define SHUF(b, b2) do {                                         \
111     b2[0] = b[8]; b2[1] = b[11]; b2[2] = b[12]; b2[3] = b[15];   \
112     b2[4] = b[2]; b2[5] = b[1]; b2[6] = b[6]; b2[7] = b[5];      \
113     b2[8] = b[10]; b2[9] = b[9]; b2[10] = b[14]; b2[11] = b[13]; \
114     b2[12] = b[0]; b2[13] = b[7]; b2[14] = b[4]; b2[15] = b[3];  \
115 } while (0)
116
117 /* This is the inverse shuffle.  It takes from b and gives to b2 */
118 #define iSHUF(b, b2) do {                                          \
119     b2[0] = b[12]; b2[1] = b[5]; b2[2] = b[4]; b2[3] = b[15];      \
120     b2[4] = b[14]; b2[5] = b[7]; b2[6] = b[6]; b2[7] = b[13];      \
121     b2[8] = b[0]; b2[9] = b[9]; b2[10] = b[8]; b2[11] = b[1];      \
122     b2[12] = b[2]; b2[13] = b[11]; b2[14] = b[10]; b2[15] = b[3];  \
123 } while (0)
124
125 /* The complete forward Linear Transform layer.
126  * Note that alternating usage of b and b2.
127  * Each round of LT starts in 'b' and ends in 'b2'.
128  */
129 #define LT(b, b2) do {        \
130     PHT(b);  SHUF(b, b2);     \
131     PHT(b2); SHUF(b2, b);     \
132     PHT(b);  SHUF(b, b2);     \
133     PHT(b2);                  \
134 } while (0)
135
136 /* This is the inverse linear transform layer.  */
137 #define iLT(b, b2) do {       \
138     iPHT(b);                  \
139     iSHUF(b, b2); iPHT(b2);   \
140     iSHUF(b2, b); iPHT(b);    \
141     iSHUF(b, b2); iPHT(b2);   \
142 } while (0)
143
144 #ifdef LTC_SMALL_CODE
145
146 static void _round(unsigned char *b, int i, symmetric_key *skey)
147 {
148    ROUND(b, i);
149 }
150
151 static void _iround(unsigned char *b, int i, symmetric_key *skey)
152 {
153    iROUND(b, i);
154 }
155
156 static void _lt(unsigned char *b, unsigned char *b2)
157 {
158    LT(b, b2);
159 }
160
161 static void _ilt(unsigned char *b, unsigned char *b2)
162 {
163    iLT(b, b2);
164 }
165
166 #undef ROUND
167 #define ROUND(b, i) _round(b, i, skey)
168
169 #undef iROUND
170 #define iROUND(b, i) _iround(b, i, skey)
171
172 #undef LT
173 #define LT(b, b2) _lt(b, b2)
174
175 #undef iLT
176 #define iLT(b, b2) _ilt(b, b2)
177
178 #endif
179
180 /* These are the 33, 128-bit bias words for the key schedule */
181 static const unsigned char safer_bias[33][16] = {
182 {  70, 151, 177, 186, 163, 183,  16,  10, 197,  55, 179, 201,  90,  40, 172, 100},
183 { 236, 171, 170, 198, 103, 149,  88,  13, 248, 154, 246, 110, 102, 220,   5,  61},
184 { 138, 195, 216, 137, 106, 233,  54,  73,  67, 191, 235, 212, 150, 155, 104, 160},
185 {  93,  87, 146,  31, 213, 113,  92, 187,  34, 193, 190, 123, 188, 153,  99, 148},
186 {  42,  97, 184,  52,  50,  25, 253, 251,  23,  64, 230,  81,  29,  65,  68, 143},
187 { 221,   4, 128, 222, 231,  49, 214, 127,   1, 162, 247,  57, 218, 111,  35, 202},
188 {  58, 208,  28, 209,  48,  62,  18, 161, 205,  15, 224, 168, 175, 130,  89,  44},
189 { 125, 173, 178, 239, 194, 135, 206, 117,   6,  19,   2, 144,  79,  46, 114,  51},
190 { 192, 141, 207, 169, 129, 226, 196,  39,  47, 108, 122, 159,  82, 225,  21,  56},
191 { 252,  32,  66, 199,   8, 228,   9,  85,  94, 140,  20, 118,  96, 255, 223, 215},
192 { 250,  11,  33,   0,  26, 249, 166, 185, 232, 158,  98,  76, 217, 145,  80, 210},
193 {  24, 180,   7, 132, 234,  91, 164, 200,  14, 203,  72, 105,  75,  78, 156,  53},
194 {  69,  77,  84, 229,  37,  60,  12,  74, 139,  63, 204, 167, 219, 107, 174, 244},
195 {  45, 243, 124, 109, 157, 181,  38, 116, 242, 147,  83, 176, 240,  17, 237, 131},
196 { 182,   3,  22, 115,  59,  30, 142, 112, 189, 134,  27,  71, 126,  36,  86, 241},
197 { 136,  70, 151, 177, 186, 163, 183,  16,  10, 197,  55, 179, 201,  90,  40, 172},
198 { 220, 134, 119, 215, 166,  17, 251, 244, 186, 146, 145, 100, 131, 241,  51, 239},
199 {  44, 181, 178,  43, 136, 209, 153, 203, 140, 132,  29,  20, 129, 151, 113, 202},
200 { 163, 139,  87,  60, 130, 196,  82,  92,  28, 232, 160,   4, 180, 133,  74, 246},
201 {  84, 182, 223,  12,  26, 142, 222, 224,  57, 252,  32, 155,  36,  78, 169, 152},
202 { 171, 242,  96, 208, 108, 234, 250, 199, 217,   0, 212,  31, 110,  67, 188, 236},
203 { 137, 254, 122,  93,  73, 201,  50, 194, 249, 154, 248, 109,  22, 219,  89, 150},
204 { 233, 205, 230,  70,  66, 143,  10, 193, 204, 185, 101, 176, 210, 198, 172,  30},
205 {  98,  41,  46,  14, 116,  80,   2,  90, 195,  37, 123, 138,  42,  91, 240,   6},
206 {  71, 111, 112, 157, 126,  16, 206,  18,  39, 213,  76,  79, 214, 121,  48, 104},
207 { 117, 125, 228, 237, 128, 106, 144,  55, 162,  94, 118, 170, 197, 127,  61, 175},
208 { 229,  25,  97, 253,  77, 124, 183,  11, 238, 173,  75,  34, 245, 231, 115,  35},
209 { 200,   5, 225, 102, 221, 179,  88, 105,  99,  86,  15, 161,  49, 149,  23,   7},
210 {  40,   1,  45, 226, 147, 190,  69,  21, 174, 120,   3, 135, 164, 184,  56, 207},
211 {   8, 103,   9, 148, 235,  38, 168, 107, 189,  24,  52,  27, 187, 191, 114, 247},
212 {  53,  72, 156,  81,  47,  59,  85, 227, 192, 159, 216, 211, 243, 141, 177, 255},
213 {  62, 220, 134, 119, 215, 166,  17, 251, 244, 186, 146, 145, 100, 131, 241,  51}};
214
215  /**
216     Initialize the LTC_SAFER+ block cipher
217     @param key The symmetric key you wish to pass
218     @param keylen The key length in bytes
219     @param num_rounds The number of rounds desired (0 for default)
220     @param skey The key in as scheduled by this function.
221     @return CRYPT_OK if successful
222  */
223 int saferp_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
224 {
225    unsigned x, y, z;
226    unsigned char t[33];
227    static const int rounds[3] = { 8, 12, 16 };
228
229    LTC_ARGCHK(key  != NULL);
230    LTC_ARGCHK(skey != NULL);
231
232    /* check arguments */
233    if (keylen != 16 && keylen != 24 && keylen != 32) {
234       return CRYPT_INVALID_KEYSIZE;
235    }
236
237    /* Is the number of rounds valid?  Either use zero for default or
238     * 8,12,16 rounds for 16,24,32 byte keys
239     */
240    if (num_rounds != 0 && num_rounds != rounds[(keylen/8)-2]) {
241       return CRYPT_INVALID_ROUNDS;
242    }
243
244    /* 128 bit key version */
245    if (keylen == 16) {
246        /* copy key into t */
247        for (x = y = 0; x < 16; x++) {
248            t[x] = key[x];
249            y ^= key[x];
250        }
251        t[16] = y;
252
253        /* make round keys */
254        for (x = 0; x < 16; x++) {
255            skey->saferp.K[0][x] = t[x];
256        }
257
258        /* make the 16 other keys as a transformation of the first key */
259        for (x = 1; x < 17; x++) {
260            /* rotate 3 bits each */
261            for (y = 0; y < 17; y++) {
262                t[y] = ((t[y]<<3)|(t[y]>>5)) & 255;
263            }
264
265            /* select and add */
266            z = x;
267            for (y = 0; y < 16; y++) {
268                skey->saferp.K[x][y] = (t[z] + safer_bias[x-1][y]) & 255;
269                if (++z == 17) { z = 0; }
270            }
271        }
272        skey->saferp.rounds = 8;
273    } else if (keylen == 24) {
274        /* copy key into t */
275        for (x = y = 0; x < 24; x++) {
276            t[x] = key[x];
277            y ^= key[x];
278        }
279        t[24] = y;
280
281        /* make round keys */
282        for (x = 0; x < 16; x++) {
283            skey->saferp.K[0][x] = t[x];
284        }
285
286        for (x = 1; x < 25; x++) {
287            /* rotate 3 bits each */
288            for (y = 0; y < 25; y++) {
289                t[y] = ((t[y]<<3)|(t[y]>>5)) & 255;
290            }
291
292            /* select and add */
293            z = x;
294            for (y = 0; y < 16; y++) {
295                skey->saferp.K[x][y] = (t[z] + safer_bias[x-1][y]) & 255;
296                if (++z == 25) { z = 0; }
297            }
298        }
299        skey->saferp.rounds = 12;
300    } else {
301        /* copy key into t */
302        for (x = y = 0; x < 32; x++) {
303            t[x] = key[x];
304            y ^= key[x];
305        }
306        t[32] = y;
307
308        /* make round keys */
309        for (x = 0; x < 16; x++) {
310            skey->saferp.K[0][x] = t[x];
311        }
312
313        for (x = 1; x < 33; x++) {
314            /* rotate 3 bits each */
315            for (y = 0; y < 33; y++) {
316                t[y] = ((t[y]<<3)|(t[y]>>5)) & 255;
317            }
318
319            /* select and add */
320            z = x;
321            for (y = 0; y < 16; y++) {
322                skey->saferp.K[x][y] = (t[z] + safer_bias[x-1][y]) & 255;
323                if (++z == 33) { z = 0; }
324            }
325        }
326        skey->saferp.rounds = 16;
327    }
328 #ifdef LTC_CLEAN_STACK
329    zeromem(t, sizeof(t));
330 #endif
331    return CRYPT_OK;
332 }
333
334 /**
335   Encrypts a block of text with LTC_SAFER+
336   @param pt The input plaintext (16 bytes)
337   @param ct The output ciphertext (16 bytes)
338   @param skey The key as scheduled
339   @return CRYPT_OK if successful
340 */
341 int saferp_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
342 {
343    unsigned char b[16];
344    int x;
345
346    LTC_ARGCHK(pt   != NULL);
347    LTC_ARGCHK(ct   != NULL);
348    LTC_ARGCHK(skey != NULL);
349
350    /* do eight rounds */
351    for (x = 0; x < 16; x++) {
352        b[x] = pt[x];
353    }
354    ROUND(b,  0);  LT(b, ct);
355    ROUND(ct, 2);  LT(ct, b);
356    ROUND(b,  4);  LT(b, ct);
357    ROUND(ct, 6);  LT(ct, b);
358    ROUND(b,  8);  LT(b, ct);
359    ROUND(ct, 10); LT(ct, b);
360    ROUND(b,  12); LT(b, ct);
361    ROUND(ct, 14); LT(ct, b);
362    /* 192-bit key? */
363    if (skey->saferp.rounds > 8) {
364       ROUND(b, 16);  LT(b, ct);
365       ROUND(ct, 18); LT(ct, b);
366       ROUND(b, 20);  LT(b, ct);
367       ROUND(ct, 22); LT(ct, b);
368    }
369    /* 256-bit key? */
370    if (skey->saferp.rounds > 12) {
371       ROUND(b, 24);  LT(b, ct);
372       ROUND(ct, 26); LT(ct, b);
373       ROUND(b, 28);  LT(b, ct);
374       ROUND(ct, 30); LT(ct, b);
375    }
376    ct[0] = b[0] ^ skey->saferp.K[skey->saferp.rounds*2][0];
377    ct[1] = (b[1] + skey->saferp.K[skey->saferp.rounds*2][1]) & 255;
378    ct[2] = (b[2] + skey->saferp.K[skey->saferp.rounds*2][2]) & 255;
379    ct[3] = b[3] ^ skey->saferp.K[skey->saferp.rounds*2][3];
380    ct[4] = b[4] ^ skey->saferp.K[skey->saferp.rounds*2][4];
381    ct[5] = (b[5] + skey->saferp.K[skey->saferp.rounds*2][5]) & 255;
382    ct[6] = (b[6] + skey->saferp.K[skey->saferp.rounds*2][6]) & 255;
383    ct[7] = b[7] ^ skey->saferp.K[skey->saferp.rounds*2][7];
384    ct[8] = b[8] ^ skey->saferp.K[skey->saferp.rounds*2][8];
385    ct[9] = (b[9] + skey->saferp.K[skey->saferp.rounds*2][9]) & 255;
386    ct[10] = (b[10] + skey->saferp.K[skey->saferp.rounds*2][10]) & 255;
387    ct[11] = b[11] ^ skey->saferp.K[skey->saferp.rounds*2][11];
388    ct[12] = b[12] ^ skey->saferp.K[skey->saferp.rounds*2][12];
389    ct[13] = (b[13] + skey->saferp.K[skey->saferp.rounds*2][13]) & 255;
390    ct[14] = (b[14] + skey->saferp.K[skey->saferp.rounds*2][14]) & 255;
391    ct[15] = b[15] ^ skey->saferp.K[skey->saferp.rounds*2][15];
392 #ifdef LTC_CLEAN_STACK
393    zeromem(b, sizeof(b));
394 #endif
395    return CRYPT_OK;
396 }
397
398 /**
399   Decrypts a block of text with LTC_SAFER+
400   @param ct The input ciphertext (16 bytes)
401   @param pt The output plaintext (16 bytes)
402   @param skey The key as scheduled
403   @return CRYPT_OK if successful
404 */
405 int saferp_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
406 {
407    unsigned char b[16];
408    int x;
409
410    LTC_ARGCHK(pt   != NULL);
411    LTC_ARGCHK(ct   != NULL);
412    LTC_ARGCHK(skey != NULL);
413
414    /* do eight rounds */
415    b[0] = ct[0] ^ skey->saferp.K[skey->saferp.rounds*2][0];
416    b[1] = (ct[1] - skey->saferp.K[skey->saferp.rounds*2][1]) & 255;
417    b[2] = (ct[2] - skey->saferp.K[skey->saferp.rounds*2][2]) & 255;
418    b[3] = ct[3] ^ skey->saferp.K[skey->saferp.rounds*2][3];
419    b[4] = ct[4] ^ skey->saferp.K[skey->saferp.rounds*2][4];
420    b[5] = (ct[5] - skey->saferp.K[skey->saferp.rounds*2][5]) & 255;
421    b[6] = (ct[6] - skey->saferp.K[skey->saferp.rounds*2][6]) & 255;
422    b[7] = ct[7] ^ skey->saferp.K[skey->saferp.rounds*2][7];
423    b[8] = ct[8] ^ skey->saferp.K[skey->saferp.rounds*2][8];
424    b[9] = (ct[9] - skey->saferp.K[skey->saferp.rounds*2][9]) & 255;
425    b[10] = (ct[10] - skey->saferp.K[skey->saferp.rounds*2][10]) & 255;
426    b[11] = ct[11] ^ skey->saferp.K[skey->saferp.rounds*2][11];
427    b[12] = ct[12] ^ skey->saferp.K[skey->saferp.rounds*2][12];
428    b[13] = (ct[13] - skey->saferp.K[skey->saferp.rounds*2][13]) & 255;
429    b[14] = (ct[14] - skey->saferp.K[skey->saferp.rounds*2][14]) & 255;
430    b[15] = ct[15] ^ skey->saferp.K[skey->saferp.rounds*2][15];
431    /* 256-bit key? */
432    if (skey->saferp.rounds > 12) {
433       iLT(b, pt); iROUND(pt, 30);
434       iLT(pt, b); iROUND(b, 28);
435       iLT(b, pt); iROUND(pt, 26);
436       iLT(pt, b); iROUND(b, 24);
437    }
438    /* 192-bit key? */
439    if (skey->saferp.rounds > 8) {
440       iLT(b, pt); iROUND(pt, 22);
441       iLT(pt, b); iROUND(b, 20);
442       iLT(b, pt); iROUND(pt, 18);
443       iLT(pt, b); iROUND(b, 16);
444    }
445    iLT(b, pt); iROUND(pt, 14);
446    iLT(pt, b); iROUND(b, 12);
447    iLT(b, pt); iROUND(pt,10);
448    iLT(pt, b); iROUND(b, 8);
449    iLT(b, pt); iROUND(pt,6);
450    iLT(pt, b); iROUND(b, 4);
451    iLT(b, pt); iROUND(pt,2);
452    iLT(pt, b); iROUND(b, 0);
453    for (x = 0; x < 16; x++) {
454        pt[x] = b[x];
455    }
456 #ifdef LTC_CLEAN_STACK
457    zeromem(b, sizeof(b));
458 #endif
459    return CRYPT_OK;
460 }
461
462 /**
463   Performs a self-test of the LTC_SAFER+ block cipher
464   @return CRYPT_OK if functional, CRYPT_NOP if self-test has been disabled
465 */
466 int saferp_test(void)
467 {
468  #ifndef LTC_TEST
469     return CRYPT_NOP;
470  #else
471    static const struct {
472        int keylen;
473        unsigned char key[32], pt[16], ct[16];
474    } tests[] = {
475        {
476            16,
477            { 41, 35, 190, 132, 225, 108, 214, 174,
478              82, 144, 73, 241, 241, 187, 233, 235 },
479            { 179, 166, 219, 60, 135, 12, 62, 153,
480              36, 94, 13, 28, 6, 183, 71, 222 },
481            { 224, 31, 182, 10, 12, 255, 84, 70,
482              127, 13, 89, 249, 9, 57, 165, 220 }
483        }, {
484            24,
485            { 72, 211, 143, 117, 230, 217, 29, 42,
486              229, 192, 247, 43, 120, 129, 135, 68,
487              14, 95, 80, 0, 212, 97, 141, 190 },
488            { 123, 5, 21, 7, 59, 51, 130, 31,
489              24, 112, 146, 218, 100, 84, 206, 177 },
490            { 92, 136, 4, 63, 57, 95, 100, 0,
491              150, 130, 130, 16, 193, 111, 219, 133 }
492        }, {
493            32,
494            { 243, 168, 141, 254, 190, 242, 235, 113,
495              255, 160, 208, 59, 117, 6, 140, 126,
496              135, 120, 115, 77, 208, 190, 130, 190,
497              219, 194, 70, 65, 43, 140, 250, 48 },
498            { 127, 112, 240, 167, 84, 134, 50, 149,
499              170, 91, 104, 19, 11, 230, 252, 245 },
500            { 88, 11, 25, 36, 172, 229, 202, 213,
501              170, 65, 105, 153, 220, 104, 153, 138 }
502        }
503     };
504
505    unsigned char tmp[2][16];
506    symmetric_key skey;
507    int err, i, y;
508
509    for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
510       if ((err = saferp_setup(tests[i].key, tests[i].keylen, 0, &skey)) != CRYPT_OK)  {
511          return err;
512       }
513       saferp_ecb_encrypt(tests[i].pt, tmp[0], &skey);
514       saferp_ecb_decrypt(tmp[0], tmp[1], &skey);
515
516       /* compare */
517       if (compare_testvector(tmp[0], 16, tests[i].ct, 16, "Safer+ Encrypt", i) ||
518             compare_testvector(tmp[1], 16, tests[i].pt, 16, "Safer+ Decrypt", i)) {
519          return CRYPT_FAIL_TESTVECTOR;
520       }
521
522       /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
523       for (y = 0; y < 16; y++) tmp[0][y] = 0;
524       for (y = 0; y < 1000; y++) saferp_ecb_encrypt(tmp[0], tmp[0], &skey);
525       for (y = 0; y < 1000; y++) saferp_ecb_decrypt(tmp[0], tmp[0], &skey);
526       for (y = 0; y < 16; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR;
527    }
528
529    return CRYPT_OK;
530  #endif
531 }
532
533 /** Terminate the context
534    @param skey    The scheduled key
535 */
536 void saferp_done(symmetric_key *skey)
537 {
538   LTC_UNUSED_PARAM(skey);
539 }
540
541 /**
542   Gets suitable key size
543   @param keysize [in/out] The length of the recommended key (in bytes).  This function will store the suitable size back in this variable.
544   @return CRYPT_OK if the input key size is acceptable.
545 */
546 int saferp_keysize(int *keysize)
547 {
548    LTC_ARGCHK(keysize != NULL);
549
550    if (*keysize < 16)
551       return CRYPT_INVALID_KEYSIZE;
552    if (*keysize < 24) {
553       *keysize = 16;
554    } else if (*keysize < 32) {
555       *keysize = 24;
556    } else {
557       *keysize = 32;
558    }
559    return CRYPT_OK;
560 }
561
562 #endif
563
564
565
566 /* ref:         $Format:%D$ */
567 /* git commit:  $Format:%H$ */
568 /* commit time: $Format:%ai$ */