1 /* ===================================================================
2 *
3 * Copyright (c) 2015, Legrandin <helderijs@gmail.com>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 * ===================================================================
30 */
31
32 #include "common.h"
33
34 #define NON_STANDARD_START_OPERATION
35 #include "block_base.h"
36
37 FAKE_INIT(raw_arc2)
38
39 #define MODULE_NAME ARC2
40 #define BLOCK_SIZE 8
41 #define KEY_SIZE 0
42
43 struct block_state {
44 unsigned exp_key[64];
45 };
46
47 static int
block_init(struct block_state * self,const uint8_t * key,size_t t,size_t effective_key_bits)48 block_init(struct block_state *self, const uint8_t *key, size_t t /* key_bytes */,
49 size_t effective_key_bits)
50 {
51 uint8_t t8, tm;
52 int i;
53 uint8_t bkey[128];
54
55 static const uint8_t permute[256] = {
56 217,120,249,196, 25,221,181,237, 40,233,253,121, 74,160,216,157,
57 198,126, 55,131, 43,118, 83,142, 98, 76,100,136, 68,139,251,162,
58 23,154, 89,245,135,179, 79, 19, 97, 69,109,141, 9,129,125, 50,
59 189,143, 64,235,134,183,123, 11,240,149, 33, 34, 92,107, 78,130,
60 84,214,101,147,206, 96,178, 28,115, 86,192, 20,167,140,241,220,
61 18,117,202, 31, 59,190,228,209, 66, 61,212, 48,163, 60,182, 38,
62 111,191, 14,218, 70,105, 7, 87, 39,242, 29,155,188,148, 67, 3,
63 248, 17,199,246,144,239, 62,231, 6,195,213, 47,200,102, 30,215,
64 8,232,234,222,128, 82,238,247,132,170,114,172, 53, 77,106, 42,
65 150, 26,210,113, 90, 21, 73,116, 75,159,208, 94, 4, 24,164,236,
66 194,224, 65,110, 15, 81,203,204, 36,145,175, 80,161,244,112, 57,
67 153,124, 58,133, 35,184,180,122,252, 2, 54, 91, 37, 85,151, 49,
68 45, 93,250,152,227,138,146,174, 5,223, 41, 16,103,108,186,201,
69 211, 0,230,207,225,158,168, 44, 99, 22, 1, 63, 88,226,137,169,
70 13, 56, 52, 27,171, 51,255,176,187, 72, 12, 95,185,177,205, 46,
71 197,243,219, 71,229,165,156,119, 10,166, 32,104,254,127,193,173
72 };
73
74 if (NULL == self)
75 return ERR_NULL;
76
77 if ((t < 5) || (t > 128))
78 return ERR_KEY_SIZE;
79
80 if ((effective_key_bits < 40) || (effective_key_bits > 1024))
81 return ERR_KEY_SIZE;
82
83 memcpy(bkey, key, t);
84
85 t8 = (uint8_t)((effective_key_bits + 7) / 8); /** 5..128 **/
86 tm = (uint8_t)((1 << (8 - (t8*8 - (int)effective_key_bits))) - 1);
87
88 for (i=(int)t; i<128; i++)
89 bkey[i] = permute[(bkey[i-1] + bkey[i-(int)t]) % 256];
90
91 bkey[128-t8] = permute[bkey[128-t8] & tm];
92
93 for (i=127-t8; i>=0; i--)
94 bkey[i] = permute[bkey[i+1] ^ bkey[i+t8]];
95
96 for (i=0; i<64; i++)
97 self->exp_key[i] = bkey[2*i] + 256U*bkey[2*i+1];
98
99 return 0;
100 }
101
102 #define ROL16(x, p) ((((x) << (p)) | ((uint16_t)(x) >> (16-(p)))))
103 #define ROR16(x, p) ((((uint16_t)(x) >> (p)) | ((x) << (16-(p)))))
104
mix_round(unsigned * r,const unsigned * k,size_t * j)105 static inline void mix_round(unsigned *r, const unsigned *k, size_t *j)
106 {
107 r[0] += k[(*j)++] + (r[3] & r[2]) + (~r[3] & r[1]);
108 r[0] = ROL16(r[0], 1);
109 r[1] += k[(*j)++] + (r[0] & r[3]) + (~r[0] & r[2]);
110 r[1] = ROL16(r[1], 2);
111 r[2] += k[(*j)++] + (r[1] & r[0]) + (~r[1] & r[3]);
112 r[2] = ROL16(r[2], 3);
113 r[3] += k[(*j)++] + (r[2] & r[1]) + (~r[2] & r[0]);
114 r[3] = ROL16(r[3], 5);
115 }
116
inv_mix_round(unsigned * r,const unsigned * k,size_t * j)117 static inline void inv_mix_round(unsigned *r, const unsigned *k, size_t *j)
118 {
119 r[3] = ROR16(r[3], 5);
120 r[3] -= k[(*j)--] + (r[2] & r[1]) + (~r[2] & r[0]);
121 r[2] = ROR16(r[2], 3);
122 r[2] -= k[(*j)--] + (r[1] & r[0]) + (~r[1] & r[3]);
123 r[1] = ROR16(r[1], 2);
124 r[1] -= k[(*j)--] + (r[0] & r[3]) + (~r[0] & r[2]);
125 r[0] = ROR16(r[0], 1);
126 r[0] -= k[(*j)--] + (r[3] & r[2]) + (~r[3] & r[1]);
127 }
128
mash_round(unsigned * r,const unsigned * k)129 static inline void mash_round(unsigned *r, const unsigned *k)
130 {
131 r[0] += k[r[3] & 63];
132 r[1] += k[r[0] & 63];
133 r[2] += k[r[1] & 63];
134 r[3] += k[r[2] & 63];
135 }
136
inv_mash_round(unsigned * r,const unsigned * k)137 static inline void inv_mash_round(unsigned *r, const unsigned *k)
138 {
139 r[3] -= k[r[2] & 63];
140 r[2] -= k[r[1] & 63];
141 r[1] -= k[r[0] & 63];
142 r[0] -= k[r[3] & 63];
143 }
144
block_encrypt(struct block_state * self,const uint8_t * in,uint8_t * out)145 static void block_encrypt(struct block_state *self, const uint8_t *in, uint8_t *out)
146 {
147 unsigned r[4];
148 const unsigned *k;
149 size_t i, j;
150
151 k = self->exp_key;
152 j = 0;
153
154 for (i=0; i<4; i++) {
155 r[i] = in[2*i] + 256U*in[2*i+1];
156 }
157
158 for (i=0; i<5; i++) mix_round(r, k, &j);
159 mash_round(r, k);
160 for (i=0; i<6; i++) mix_round(r, k, &j);
161 mash_round(r, k);
162 for (i=0; i<5; i++) mix_round(r, k, &j);
163
164 for (i=0; i<4; i++) {
165 out[2*i] = r[i] & 255;
166 out[2*i+1] = (uint8_t)(r[i] >> 8);
167 }
168 }
169
block_decrypt(struct block_state * self,const uint8_t * in,uint8_t * out)170 static void block_decrypt(struct block_state *self, const uint8_t *in, uint8_t *out)
171 {
172 unsigned r[4];
173 const unsigned *k;
174 size_t i, j;
175
176 k = self->exp_key;
177
178 for (i=0; i<4; i++) {
179 r[i] = in[2*i] + 256U*in[2*i+1];
180 }
181
182 j = 63;
183 for (i=0; i<5; i++) inv_mix_round(r, k, &j);
184 inv_mash_round(r, k);
185 for (i=0; i<6; i++) inv_mix_round(r, k, &j);
186 inv_mash_round(r, k);
187 for (i=0; i<5; i++) inv_mix_round(r, k, &j);
188
189 for (i=0; i<4; i++) {
190 out[2*i] = r[i] & 255;
191 out[2*i+1] = (uint8_t)(r[i] >> 8);
192 }
193 }
194
195 static void
block_finalize(struct block_state * self)196 block_finalize(struct block_state* self)
197 {
198 }
199
200 #include "block_common.c"
201
ARC2_start_operation(const uint8_t key[],size_t key_len,size_t effective_key_len,ARC2_State ** pResult)202 EXPORT_SYM int ARC2_start_operation(const uint8_t key[], size_t key_len, size_t effective_key_len, ARC2_State **pResult)
203 {
204 BlockBase *block_base;
205
206 if ((key == NULL) || (pResult == NULL))
207 return ERR_NULL;
208
209 *pResult = calloc(1, sizeof(ARC2_State));
210 if (NULL == *pResult)
211 return ERR_MEMORY;
212
213 block_base = &((*pResult)->base_state);
214 block_base->encrypt = &ARC2_encrypt;
215 block_base->decrypt = &ARC2_decrypt;
216 block_base->destructor = &ARC2_stop_operation;
217 block_base->block_len = BLOCK_SIZE;
218
219 return block_init(&(*pResult)->algo_state, (unsigned char*)key,
220 key_len, effective_key_len);
221 }
222
223