1 /* $NetBSD: cryptosoft_xform.c,v 1.11 2009/03/18 16:00:24 cegger Exp $ */
2 /* $FreeBSD: src/sys/opencrypto/xform.c,v 1.1.2.1 2002/11/21 23:34:23 sam Exp $ */
3 /* $OpenBSD: xform.c,v 1.19 2002/08/16 22:47:25 dhartmei Exp $ */
6 * The authors of this code are John Ioannidis (ji@tla.org),
7 * Angelos D. Keromytis (kermit@csd.uch.gr) and
8 * Niels Provos (provos@physnet.uni-hamburg.de).
10 * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
13 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
14 * by Angelos D. Keromytis.
16 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
19 * Additional features in 1999 by Angelos D. Keromytis.
21 * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
22 * Angelos D. Keromytis and Niels Provos.
24 * Copyright (C) 2001, Angelos D. Keromytis.
26 * Permission to use, copy, and modify this software with or without fee
27 * is hereby granted, provided that this entire notice is included in
28 * all copies of any software which is or includes a copy or
29 * modification of this software.
30 * You may use this code under the GNU public license if you so wish. Please
31 * contribute changes back to the authors under this freer than GPL license
32 * so that we may further the use of strong encryption without limitations to
35 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
36 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
37 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
38 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(1, "$NetBSD: cryptosoft_xform.c,v 1.11 2009/03/18 16:00:24 cegger Exp $");
45 #include <crypto/blowfish/blowfish.h>
46 #include <crypto/cast128/cast128.h>
47 #include <crypto/des/des.h>
48 #include <crypto/rijndael/rijndael.h>
49 #include <crypto/skipjack/skipjack.h>
51 #include <opencrypto/deflate.h>
54 #include <sys/rmd160.h>
57 struct swcr_auth_hash
{
58 struct auth_hash
*auth_hash
;
60 int (*Update
)(void *, const uint8_t *, uint16_t);
61 void (*Final
)(uint8_t *, void *);
64 struct swcr_enc_xform
{
65 struct enc_xform
*enc_xform
;
66 void (*encrypt
)(void *, uint8_t *);
67 void (*decrypt
)(void *, uint8_t *);
68 int (*setkey
)(uint8_t **, const uint8_t *, int len
);
69 void (*zerokey
)(uint8_t **);
72 struct swcr_comp_algo
{
73 struct comp_algo
*comp_algo
;
74 uint32_t (*compress
)(uint8_t *, uint32_t, uint8_t **);
75 uint32_t (*decompress
)(uint8_t *, uint32_t, uint8_t **);
78 static void null_encrypt(void *, u_int8_t
*);
79 static void null_decrypt(void *, u_int8_t
*);
80 static int null_setkey(u_int8_t
**, const u_int8_t
*, int);
81 static void null_zerokey(u_int8_t
**);
83 static int des1_setkey(u_int8_t
**, const u_int8_t
*, int);
84 static int des3_setkey(u_int8_t
**, const u_int8_t
*, int);
85 static int blf_setkey(u_int8_t
**, const u_int8_t
*, int);
86 static int cast5_setkey(u_int8_t
**, const u_int8_t
*, int);
87 static int skipjack_setkey(u_int8_t
**, const u_int8_t
*, int);
88 static int rijndael128_setkey(u_int8_t
**, const u_int8_t
*, int);
89 static void des1_encrypt(void *, u_int8_t
*);
90 static void des3_encrypt(void *, u_int8_t
*);
91 static void blf_encrypt(void *, u_int8_t
*);
92 static void cast5_encrypt(void *, u_int8_t
*);
93 static void skipjack_encrypt(void *, u_int8_t
*);
94 static void rijndael128_encrypt(void *, u_int8_t
*);
95 static void des1_decrypt(void *, u_int8_t
*);
96 static void des3_decrypt(void *, u_int8_t
*);
97 static void blf_decrypt(void *, u_int8_t
*);
98 static void cast5_decrypt(void *, u_int8_t
*);
99 static void skipjack_decrypt(void *, u_int8_t
*);
100 static void rijndael128_decrypt(void *, u_int8_t
*);
101 static void des1_zerokey(u_int8_t
**);
102 static void des3_zerokey(u_int8_t
**);
103 static void blf_zerokey(u_int8_t
**);
104 static void cast5_zerokey(u_int8_t
**);
105 static void skipjack_zerokey(u_int8_t
**);
106 static void rijndael128_zerokey(u_int8_t
**);
108 static void null_init(void *);
109 static int null_update(void *, const u_int8_t
*, u_int16_t
);
110 static void null_final(u_int8_t
*, void *);
112 static int MD5Update_int(void *, const u_int8_t
*, u_int16_t
);
113 static void SHA1Init_int(void *);
114 static int SHA1Update_int(void *, const u_int8_t
*, u_int16_t
);
115 static void SHA1Final_int(u_int8_t
*, void *);
118 static int RMD160Update_int(void *, const u_int8_t
*, u_int16_t
);
119 static int SHA1Update_int(void *, const u_int8_t
*, u_int16_t
);
120 static void SHA1Final_int(u_int8_t
*, void *);
121 static int RMD160Update_int(void *, const u_int8_t
*, u_int16_t
);
122 static int SHA256Update_int(void *, const u_int8_t
*, u_int16_t
);
123 static int SHA384Update_int(void *, const u_int8_t
*, u_int16_t
);
124 static int SHA512Update_int(void *, const u_int8_t
*, u_int16_t
);
126 static u_int32_t
deflate_compress(u_int8_t
*, u_int32_t
, u_int8_t
**);
127 static u_int32_t
deflate_decompress(u_int8_t
*, u_int32_t
, u_int8_t
**);
128 static u_int32_t
gzip_compress(u_int8_t
*, u_int32_t
, u_int8_t
**);
129 static u_int32_t
gzip_decompress(u_int8_t
*, u_int32_t
, u_int8_t
**);
131 /* Encryption instances */
132 static const struct swcr_enc_xform swcr_enc_xform_null
= {
140 static const struct swcr_enc_xform swcr_enc_xform_des
= {
148 static const struct swcr_enc_xform swcr_enc_xform_3des
= {
156 static const struct swcr_enc_xform swcr_enc_xform_blf
= {
164 static const struct swcr_enc_xform swcr_enc_xform_cast5
= {
172 static const struct swcr_enc_xform swcr_enc_xform_skipjack
= {
180 static const struct swcr_enc_xform swcr_enc_xform_rijndael128
= {
181 &enc_xform_rijndael128
,
188 static const struct swcr_enc_xform swcr_enc_xform_arc4
= {
196 /* Authentication instances */
197 static const struct swcr_auth_hash swcr_auth_hash_null
= {
199 null_init
, null_update
, null_final
202 static const struct swcr_auth_hash swcr_auth_hash_hmac_md5
= {
204 (void (*) (void *)) MD5Init
, MD5Update_int
,
205 (void (*) (u_int8_t
*, void *)) MD5Final
208 static const struct swcr_auth_hash swcr_auth_hash_hmac_sha1
= {
209 &auth_hash_hmac_sha1
,
210 SHA1Init_int
, SHA1Update_int
, SHA1Final_int
213 static const struct swcr_auth_hash swcr_auth_hash_hmac_ripemd_160
= {
214 &auth_hash_hmac_ripemd_160
,
215 (void (*)(void *)) RMD160Init
, RMD160Update_int
,
216 (void (*)(u_int8_t
*, void *)) RMD160Final
218 static const struct swcr_auth_hash swcr_auth_hash_hmac_md5_96
= {
219 &auth_hash_hmac_md5_96
,
220 (void (*) (void *)) MD5Init
, MD5Update_int
,
221 (void (*) (u_int8_t
*, void *)) MD5Final
224 static const struct swcr_auth_hash swcr_auth_hash_hmac_sha1_96
= {
225 &auth_hash_hmac_sha1_96
,
226 SHA1Init_int
, SHA1Update_int
, SHA1Final_int
229 static const struct swcr_auth_hash swcr_auth_hash_hmac_ripemd_160_96
= {
230 &auth_hash_hmac_ripemd_160_96
,
231 (void (*)(void *)) RMD160Init
, RMD160Update_int
,
232 (void (*)(u_int8_t
*, void *)) RMD160Final
235 static const struct swcr_auth_hash swcr_auth_hash_key_md5
= {
237 (void (*)(void *)) MD5Init
, MD5Update_int
,
238 (void (*)(u_int8_t
*, void *)) MD5Final
241 static const struct swcr_auth_hash swcr_auth_hash_key_sha1
= {
243 SHA1Init_int
, SHA1Update_int
, SHA1Final_int
246 static const struct swcr_auth_hash swcr_auth_hash_md5
= {
248 (void (*) (void *)) MD5Init
, MD5Update_int
,
249 (void (*) (u_int8_t
*, void *)) MD5Final
252 static const struct swcr_auth_hash swcr_auth_hash_sha1
= {
254 (void (*)(void *)) SHA1Init
, SHA1Update_int
,
255 (void (*)(u_int8_t
*, void *)) SHA1Final
258 static const struct swcr_auth_hash swcr_auth_hash_hmac_sha2_256
= {
259 &auth_hash_hmac_sha2_256
,
260 (void (*)(void *)) SHA256_Init
, SHA256Update_int
,
261 (void (*)(u_int8_t
*, void *)) SHA256_Final
264 static const struct swcr_auth_hash swcr_auth_hash_hmac_sha2_384
= {
265 &auth_hash_hmac_sha2_384
,
266 (void (*)(void *)) SHA384_Init
, SHA384Update_int
,
267 (void (*)(u_int8_t
*, void *)) SHA384_Final
270 static const struct swcr_auth_hash swcr_auth_hash_hmac_sha2_512
= {
271 &auth_hash_hmac_sha2_384
,
272 (void (*)(void *)) SHA512_Init
, SHA512Update_int
,
273 (void (*)(u_int8_t
*, void *)) SHA512_Final
276 /* Compression instance */
277 static const struct swcr_comp_algo swcr_comp_algo_deflate
= {
283 static const struct swcr_comp_algo swcr_comp_algo_gzip
= {
290 * Encryption wrapper routines.
293 null_encrypt(void *key
, u_int8_t
*blk
)
297 null_decrypt(void *key
, u_int8_t
*blk
)
301 null_setkey(u_int8_t
**sched
, const u_int8_t
*key
, int len
)
307 null_zerokey(u_int8_t
**sched
)
313 des1_encrypt(void *key
, u_int8_t
*blk
)
315 des_cblock
*cb
= (des_cblock
*) blk
;
316 des_key_schedule
*p
= (des_key_schedule
*) key
;
318 des_ecb_encrypt(cb
, cb
, p
[0], DES_ENCRYPT
);
322 des1_decrypt(void *key
, u_int8_t
*blk
)
324 des_cblock
*cb
= (des_cblock
*) blk
;
325 des_key_schedule
*p
= (des_key_schedule
*) key
;
327 des_ecb_encrypt(cb
, cb
, p
[0], DES_DECRYPT
);
331 des1_setkey(u_int8_t
**sched
, const u_int8_t
*key
, int len
)
336 p
= malloc(sizeof (des_key_schedule
),
337 M_CRYPTO_DATA
, M_NOWAIT
|M_ZERO
);
339 des_set_key((des_cblock
*)__UNCONST(key
), p
[0]);
343 *sched
= (u_int8_t
*) p
;
348 des1_zerokey(u_int8_t
**sched
)
350 memset(*sched
, 0, sizeof (des_key_schedule
));
351 free(*sched
, M_CRYPTO_DATA
);
356 des3_encrypt(void *key
, u_int8_t
*blk
)
358 des_cblock
*cb
= (des_cblock
*) blk
;
359 des_key_schedule
*p
= (des_key_schedule
*) key
;
361 des_ecb3_encrypt(cb
, cb
, p
[0], p
[1], p
[2], DES_ENCRYPT
);
365 des3_decrypt(void *key
, u_int8_t
*blk
)
367 des_cblock
*cb
= (des_cblock
*) blk
;
368 des_key_schedule
*p
= (des_key_schedule
*) key
;
370 des_ecb3_encrypt(cb
, cb
, p
[0], p
[1], p
[2], DES_DECRYPT
);
374 des3_setkey(u_int8_t
**sched
, const u_int8_t
*key
, int len
)
379 p
= malloc(3*sizeof (des_key_schedule
),
380 M_CRYPTO_DATA
, M_NOWAIT
|M_ZERO
);
382 des_set_key((des_cblock
*)__UNCONST(key
+ 0), p
[0]);
383 des_set_key((des_cblock
*)__UNCONST(key
+ 8), p
[1]);
384 des_set_key((des_cblock
*)__UNCONST(key
+ 16), p
[2]);
388 *sched
= (u_int8_t
*) p
;
393 des3_zerokey(u_int8_t
**sched
)
395 memset(*sched
, 0, 3*sizeof (des_key_schedule
));
396 free(*sched
, M_CRYPTO_DATA
);
401 blf_encrypt(void *key
, u_int8_t
*blk
)
404 BF_ecb_encrypt(blk
, blk
, (BF_KEY
*)key
, 1);
408 blf_decrypt(void *key
, u_int8_t
*blk
)
411 BF_ecb_encrypt(blk
, blk
, (BF_KEY
*)key
, 0);
415 blf_setkey(u_int8_t
**sched
, const u_int8_t
*key
, int len
)
419 *sched
= malloc(sizeof(BF_KEY
),
420 M_CRYPTO_DATA
, M_NOWAIT
|M_ZERO
);
421 if (*sched
!= NULL
) {
422 BF_set_key((BF_KEY
*) *sched
, len
, key
);
430 blf_zerokey(u_int8_t
**sched
)
432 memset(*sched
, 0, sizeof(BF_KEY
));
433 free(*sched
, M_CRYPTO_DATA
);
438 cast5_encrypt(void *key
, u_int8_t
*blk
)
440 cast128_encrypt((cast128_key
*) key
, blk
, blk
);
444 cast5_decrypt(void *key
, u_int8_t
*blk
)
446 cast128_decrypt((cast128_key
*) key
, blk
, blk
);
450 cast5_setkey(u_int8_t
**sched
, const u_int8_t
*key
, int len
)
454 *sched
= malloc(sizeof(cast128_key
), M_CRYPTO_DATA
,
456 if (*sched
!= NULL
) {
457 cast128_setkey((cast128_key
*)*sched
, key
, len
);
465 cast5_zerokey(u_int8_t
**sched
)
467 memset(*sched
, 0, sizeof(cast128_key
));
468 free(*sched
, M_CRYPTO_DATA
);
473 skipjack_encrypt(void *key
, u_int8_t
*blk
)
475 skipjack_forwards(blk
, blk
, (u_int8_t
**) key
);
479 skipjack_decrypt(void *key
, u_int8_t
*blk
)
481 skipjack_backwards(blk
, blk
, (u_int8_t
**) key
);
485 skipjack_setkey(u_int8_t
**sched
, const u_int8_t
*key
, int len
)
489 /* NB: allocate all the memory that's needed at once */
490 /* XXX assumes bytes are aligned on sizeof(u_char) == 1 boundaries.
491 * Will this break a pdp-10, Cray-1, or GE-645 port?
493 *sched
= malloc(10 * (sizeof(u_int8_t
*) + 0x100),
494 M_CRYPTO_DATA
, M_NOWAIT
|M_ZERO
);
496 if (*sched
!= NULL
) {
498 u_int8_t
** key_tables
= (u_int8_t
**) *sched
;
499 u_int8_t
* table
= (u_int8_t
*) &key_tables
[10];
502 for (k
= 0; k
< 10; k
++) {
503 key_tables
[k
] = table
;
506 subkey_table_gen(key
, (u_int8_t
**) *sched
);
514 skipjack_zerokey(u_int8_t
**sched
)
516 memset(*sched
, 0, 10 * (sizeof(u_int8_t
*) + 0x100));
517 free(*sched
, M_CRYPTO_DATA
);
522 rijndael128_encrypt(void *key
, u_int8_t
*blk
)
524 rijndael_encrypt((rijndael_ctx
*) key
, (u_char
*) blk
, (u_char
*) blk
);
528 rijndael128_decrypt(void *key
, u_int8_t
*blk
)
530 rijndael_decrypt((rijndael_ctx
*) key
, (u_char
*) blk
,
535 rijndael128_setkey(u_int8_t
**sched
, const u_int8_t
*key
, int len
)
539 *sched
= malloc(sizeof(rijndael_ctx
), M_CRYPTO_DATA
,
541 if (*sched
!= NULL
) {
542 rijndael_set_key((rijndael_ctx
*) *sched
, key
, len
* 8);
550 rijndael128_zerokey(u_int8_t
**sched
)
552 memset(*sched
, 0, sizeof(rijndael_ctx
));
553 free(*sched
, M_CRYPTO_DATA
);
567 null_update(void *ctx
, const u_int8_t
*buf
,
574 null_final(u_int8_t
*buf
, void *ctx
)
576 if (buf
!= (u_int8_t
*) 0)
581 RMD160Update_int(void *ctx
, const u_int8_t
*buf
, u_int16_t len
)
583 RMD160Update(ctx
, buf
, len
);
588 MD5Update_int(void *ctx
, const u_int8_t
*buf
, u_int16_t len
)
590 MD5Update(ctx
, buf
, len
);
595 SHA1Init_int(void *ctx
)
601 SHA1Update_int(void *ctx
, const u_int8_t
*buf
, u_int16_t len
)
603 SHA1Update(ctx
, buf
, len
);
608 SHA1Final_int(u_int8_t
*blk
, void *ctx
)
614 SHA256Update_int(void *ctx
, const u_int8_t
*buf
, u_int16_t len
)
616 SHA256_Update(ctx
, buf
, len
);
621 SHA384Update_int(void *ctx
, const u_int8_t
*buf
, u_int16_t len
)
623 SHA384_Update(ctx
, buf
, len
);
628 SHA512Update_int(void *ctx
, const u_int8_t
*buf
, u_int16_t len
)
630 SHA512_Update(ctx
, buf
, len
);
639 deflate_compress(u_int8_t
*data
, u_int32_t size
, u_int8_t
**out
)
641 return deflate_global(data
, size
, 0, out
);
645 deflate_decompress(u_int8_t
*data
, u_int32_t size
, u_int8_t
**out
)
647 return deflate_global(data
, size
, 1, out
);
651 gzip_compress(u_int8_t
*data
, u_int32_t size
, u_int8_t
**out
)
653 return gzip_global(data
, size
, 0, out
);
657 gzip_decompress(u_int8_t
*data
, u_int32_t size
, u_int8_t
**out
)
659 return gzip_global(data
, size
, 1, out
);