1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
/* $OpenBSD: crypto.c,v 1.16 2003/08/28 14:43:35 markus Exp $ */
/* $EOM: crypto.c,v 1.32 2000/03/07 20:08:51 niklas Exp $ */
/*
* Copyright (c) 1998 Niels Provos. All rights reserved.
* Copyright (c) 1999, 2000 Niklas Hallqvist. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This code was written under funding by Ericsson Radio Systems.
*/
#include <sys/param.h>
#include <stdlib.h>
#include <string.h>
#include "sysdep.h"
#include "crypto.h"
#include "log.h"
enum cryptoerr evp_init (struct keystate *, u_int8_t *, u_int16_t,
const EVP_CIPHER *);
enum cryptoerr des1_init (struct keystate *, u_int8_t *, u_int16_t);
enum cryptoerr des3_init (struct keystate *, u_int8_t *, u_int16_t);
enum cryptoerr blf_init (struct keystate *, u_int8_t *, u_int16_t);
enum cryptoerr cast_init (struct keystate *, u_int8_t *, u_int16_t);
enum cryptoerr aes_init (struct keystate *, u_int8_t *, u_int16_t);
void evp_encrypt (struct keystate *, u_int8_t *, u_int16_t);
void evp_decrypt (struct keystate *, u_int8_t *, u_int16_t);
struct crypto_xf transforms[] = {
#ifdef USE_DES
{
DES_CBC, "Data Encryption Standard (CBC-Mode)", 8, 8, BLOCKSIZE, 0,
des1_init,
evp_encrypt, evp_decrypt
},
#endif
#ifdef USE_TRIPLEDES
{
TRIPLEDES_CBC, "Triple-DES (CBC-Mode)", 24, 24, BLOCKSIZE, 0,
des3_init,
evp_encrypt, evp_decrypt
},
#endif
#ifdef USE_BLOWFISH
{
BLOWFISH_CBC, "Blowfish (CBC-Mode)", 12, 56, BLOCKSIZE, 0,
blf_init,
evp_encrypt, evp_decrypt
},
#endif
#ifdef USE_CAST
{
CAST_CBC, "CAST (CBC-Mode)", 12, 16, BLOCKSIZE, 0,
cast_init,
evp_encrypt, evp_decrypt
},
#endif
#ifdef USE_AES
{
AES_CBC, "AES (CBC-Mode)", 16, 32, 2*BLOCKSIZE, 0,
aes_init,
evp_encrypt, evp_decrypt
},
#endif
};
#ifdef USE_DES
enum cryptoerr
des1_init (struct keystate *ks, u_int8_t *key, u_int16_t len)
{
const EVP_CIPHER *evp;
evp = EVP_des_cbc();
return evp_init (ks, key, len, evp);
}
#endif
#ifdef USE_TRIPLEDES
enum cryptoerr
des3_init (struct keystate *ks, u_int8_t *key, u_int16_t len)
{
const EVP_CIPHER *evp;
evp = EVP_des_ede3_cbc();
return evp_init (ks, key, len, evp);
}
#endif
#ifdef USE_BLOWFISH
enum cryptoerr
blf_init (struct keystate *ks, u_int8_t *key, u_int16_t len)
{
const EVP_CIPHER *evp;
evp = EVP_bf_cbc();
return evp_init (ks, key, len, evp);
}
#endif
#ifdef USE_CAST
enum cryptoerr
cast_init (struct keystate *ks, u_int8_t *key, u_int16_t len)
{
const EVP_CIPHER *evp;
evp = EVP_cast5_cbc();
return evp_init (ks, key, len, evp);
}
#endif
#ifdef USE_AES
enum cryptoerr
aes_init (struct keystate *ks, u_int8_t *key, u_int16_t len)
{
const EVP_CIPHER *evp;
switch (8 * len)
{
case 128:
evp = EVP_aes_128_cbc();
break;
case 192:
evp = EVP_aes_192_cbc();
break;
case 256:
evp = EVP_aes_256_cbc();
break;
default:
return EKEYLEN;
}
return evp_init (ks, key, len, evp);
}
#endif
enum cryptoerr
evp_init (struct keystate *ks, u_int8_t *key, u_int16_t len, const EVP_CIPHER *evp)
{
EVP_CIPHER_CTX_init(&ks->ks_evpenc);
EVP_CIPHER_CTX_init(&ks->ks_evpdec);
if (EVP_CIPHER_key_length(evp) != len
&& !(EVP_CIPHER_flags(evp) & EVP_CIPH_VARIABLE_LENGTH))
return EKEYLEN;
if (EVP_CipherInit(&ks->ks_evpenc, evp, key, NULL, 1) <= 0)
return EKEYLEN;
if (EVP_CipherInit(&ks->ks_evpdec, evp, key, NULL, 0) <= 0)
return EKEYLEN;
return EOKAY;
}
void
evp_encrypt (struct keystate *ks, u_int8_t *data, u_int16_t len)
{
(void) EVP_CipherInit(&ks->ks_evpenc, NULL, NULL, ks->riv, -1);
EVP_Cipher(&ks->ks_evpenc, data, data, len);
}
void
evp_decrypt (struct keystate *ks, u_int8_t *data, u_int16_t len)
{
(void) EVP_CipherInit(&ks->ks_evpdec, NULL, NULL, ks->riv, -1);
EVP_Cipher(&ks->ks_evpdec, data, data, len);
}
struct crypto_xf *
crypto_get (enum transform id)
{
int i;
for (i = 0; i < sizeof transforms / sizeof transforms[0]; i++)
if (id == transforms[i].id)
return &transforms[i];
return 0;
}
struct keystate *
crypto_init (struct crypto_xf *xf, u_int8_t *key, u_int16_t len,
enum cryptoerr *err)
{
struct keystate *ks;
if (len < xf->keymin || len > xf->keymax)
{
LOG_DBG ((LOG_CRYPTO, 10, "crypto_init: invalid key length %d", len));
*err = EKEYLEN;
return 0;
}
ks = calloc (1, sizeof *ks);
if (!ks)
{
log_error ("crypto_init: calloc (1, %lu) failed",
(unsigned long)sizeof *ks);
*err = ENOCRYPTO;
return 0;
}
ks->xf = xf;
/* Setup the IV. */
ks->riv = ks->iv;
ks->liv = ks->iv2;
LOG_DBG_BUF ((LOG_CRYPTO, 40, "crypto_init: key", key, len));
*err = xf->init (ks, key, len);
if (*err != EOKAY)
{
LOG_DBG ((LOG_CRYPTO, 30, "crypto_init: weak key found for %s",
xf->name));
free (ks);
return 0;
}
return ks;
}
void
crypto_update_iv (struct keystate *ks)
{
u_int8_t *tmp;
tmp = ks->riv;
ks->riv = ks->liv;
ks->liv = tmp;
LOG_DBG_BUF ((LOG_CRYPTO, 50, "crypto_update_iv: updated IV", ks->riv,
ks->xf->blocksize));
}
void
crypto_init_iv (struct keystate *ks, u_int8_t *buf, size_t len)
{
memcpy (ks->riv, buf, len);
LOG_DBG_BUF ((LOG_CRYPTO, 50, "crypto_update_iv: initialized IV", ks->riv,
len));
}
void
crypto_encrypt (struct keystate *ks, u_int8_t *buf, u_int16_t len)
{
LOG_DBG_BUF ((LOG_CRYPTO, 10, "crypto_encrypt: before encryption", buf,
len));
ks->xf->encrypt (ks, buf, len);
memcpy (ks->liv, buf + len - ks->xf->blocksize, ks->xf->blocksize);
LOG_DBG_BUF ((LOG_CRYPTO, 30, "crypto_encrypt: after encryption", buf,
len));
}
void
crypto_decrypt (struct keystate *ks, u_int8_t *buf, u_int16_t len)
{
LOG_DBG_BUF ((LOG_CRYPTO, 10, "crypto_decrypt: before decryption", buf,
len));
/*
* XXX There is controversy about the correctness of updating the IV
* like this.
*/
memcpy (ks->liv, buf + len - ks->xf->blocksize, ks->xf->blocksize);
ks->xf->decrypt (ks, buf, len);
LOG_DBG_BUF ((LOG_CRYPTO, 30, "crypto_decrypt: after decryption", buf,
len));
}
/* Make a copy of the keystate pointed to by OKS. */
struct keystate *
crypto_clone_keystate (struct keystate *oks)
{
struct keystate *ks;
ks = malloc (sizeof *ks);
if (!ks)
{
log_error ("crypto_clone_keystate: malloc (%lu) failed",
(unsigned long)sizeof *ks);
return 0;
}
memcpy (ks, oks, sizeof *ks);
if (oks->riv == oks->iv)
{
ks->riv = ks->iv;
ks->liv = ks->iv2;
}
else
{
ks->riv = ks->iv2;
ks->liv = ks->iv;
}
return ks;
}
|