2003-11-19 02:01:39 +03:00
|
|
|
/* $NetBSD: xform.c,v 1.13 2003/11/18 23:01:39 jonathan Exp $ */
|
2003-07-26 01:12:39 +04:00
|
|
|
/* $FreeBSD: src/sys/opencrypto/xform.c,v 1.1.2.1 2002/11/21 23:34:23 sam Exp $ */
|
|
|
|
/* $OpenBSD: xform.c,v 1.19 2002/08/16 22:47:25 dhartmei Exp $ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The authors of this code are John Ioannidis (ji@tla.org),
|
|
|
|
* Angelos D. Keromytis (kermit@csd.uch.gr) and
|
|
|
|
* Niels Provos (provos@physnet.uni-hamburg.de).
|
|
|
|
*
|
|
|
|
* This code was written by John Ioannidis for BSD/OS in Athens, Greece,
|
|
|
|
* in November 1995.
|
|
|
|
*
|
|
|
|
* Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
|
|
|
|
* by Angelos D. Keromytis.
|
|
|
|
*
|
|
|
|
* Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
|
|
|
|
* and Niels Provos.
|
|
|
|
*
|
|
|
|
* Additional features in 1999 by Angelos D. Keromytis.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
|
|
|
|
* Angelos D. Keromytis and Niels Provos.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2001, Angelos D. Keromytis.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, and modify this software with or without fee
|
|
|
|
* is hereby granted, provided that this entire notice is included in
|
|
|
|
* all copies of any software which is or includes a copy or
|
|
|
|
* modification of this software.
|
|
|
|
* You may use this code under the GNU public license if you so wish. Please
|
|
|
|
* contribute changes back to the authors under this freer than GPL license
|
|
|
|
* so that we may further the use of strong encryption without limitations to
|
|
|
|
* all.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
|
|
|
|
* REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
|
|
|
|
* MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
|
|
|
|
* PURPOSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2003-11-19 02:01:39 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: xform.c,v 1.13 2003/11/18 23:01:39 jonathan Exp $");
|
2003-07-26 01:12:39 +04:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <machine/cpu.h>
|
|
|
|
|
|
|
|
#include <crypto/blowfish/blowfish.h>
|
2003-08-26 20:37:36 +04:00
|
|
|
#include <crypto/cast128/cast128.h>
|
2003-07-26 01:12:39 +04:00
|
|
|
#include <crypto/des/des.h>
|
2003-08-26 18:24:05 +04:00
|
|
|
#include <crypto/rijndael/rijndael.h>
|
2003-08-26 18:24:35 +04:00
|
|
|
#include <crypto/ripemd160/rmd160.h>
|
2003-11-16 15:07:50 +03:00
|
|
|
#include <crypto/skipjack/skipjack.h>
|
2003-07-26 01:12:39 +04:00
|
|
|
|
|
|
|
#include <opencrypto/deflate.h>
|
|
|
|
|
2003-07-28 23:37:03 +04:00
|
|
|
#include <sys/md5.h>
|
2003-07-26 01:12:39 +04:00
|
|
|
#include <sys/sha1.h>
|
|
|
|
|
|
|
|
#include <opencrypto/cryptodev.h>
|
|
|
|
#include <opencrypto/xform.h>
|
|
|
|
|
|
|
|
static void null_encrypt(caddr_t, u_int8_t *);
|
|
|
|
static void null_decrypt(caddr_t, u_int8_t *);
|
2003-08-27 04:20:56 +04:00
|
|
|
static int null_setkey(u_int8_t **, const u_int8_t *, int);
|
2003-07-26 01:12:39 +04:00
|
|
|
static void null_zerokey(u_int8_t **);
|
|
|
|
|
2003-08-27 04:20:56 +04:00
|
|
|
static int des1_setkey(u_int8_t **, const u_int8_t *, int);
|
|
|
|
static int des3_setkey(u_int8_t **, const u_int8_t *, int);
|
|
|
|
static int blf_setkey(u_int8_t **, const u_int8_t *, int);
|
|
|
|
static int cast5_setkey(u_int8_t **, const u_int8_t *, int);
|
|
|
|
static int skipjack_setkey(u_int8_t **, const u_int8_t *, int);
|
|
|
|
static int rijndael128_setkey(u_int8_t **, const u_int8_t *, int);
|
2003-07-26 01:12:39 +04:00
|
|
|
static void des1_encrypt(caddr_t, u_int8_t *);
|
|
|
|
static void des3_encrypt(caddr_t, u_int8_t *);
|
|
|
|
static void blf_encrypt(caddr_t, u_int8_t *);
|
|
|
|
static void cast5_encrypt(caddr_t, u_int8_t *);
|
|
|
|
static void skipjack_encrypt(caddr_t, u_int8_t *);
|
|
|
|
static void rijndael128_encrypt(caddr_t, u_int8_t *);
|
|
|
|
static void des1_decrypt(caddr_t, u_int8_t *);
|
|
|
|
static void des3_decrypt(caddr_t, u_int8_t *);
|
|
|
|
static void blf_decrypt(caddr_t, u_int8_t *);
|
|
|
|
static void cast5_decrypt(caddr_t, u_int8_t *);
|
|
|
|
static void skipjack_decrypt(caddr_t, u_int8_t *);
|
|
|
|
static void rijndael128_decrypt(caddr_t, u_int8_t *);
|
|
|
|
static void des1_zerokey(u_int8_t **);
|
|
|
|
static void des3_zerokey(u_int8_t **);
|
|
|
|
static void blf_zerokey(u_int8_t **);
|
|
|
|
static void cast5_zerokey(u_int8_t **);
|
|
|
|
static void skipjack_zerokey(u_int8_t **);
|
|
|
|
static void rijndael128_zerokey(u_int8_t **);
|
|
|
|
|
|
|
|
static void null_init(void *);
|
2003-08-27 04:20:56 +04:00
|
|
|
static int null_update(void *, const u_int8_t *, u_int16_t);
|
2003-07-26 01:12:39 +04:00
|
|
|
static void null_final(u_int8_t *, void *);
|
|
|
|
|
2003-08-27 04:20:56 +04:00
|
|
|
static int MD5Update_int(void *, const u_int8_t *, u_int16_t);
|
2003-07-26 01:12:39 +04:00
|
|
|
static void SHA1Init_int(void *);
|
2003-08-27 04:20:56 +04:00
|
|
|
static int SHA1Update_int(void *, const u_int8_t *, u_int16_t);
|
2003-07-26 01:12:39 +04:00
|
|
|
static void SHA1Final_int(u_int8_t *, void *);
|
|
|
|
|
|
|
|
|
2003-08-27 04:20:56 +04:00
|
|
|
static int RMD160Update_int(void *, const u_int8_t *, u_int16_t);
|
|
|
|
static int SHA1Update_int(void *, const u_int8_t *, u_int16_t);
|
2003-07-26 01:12:39 +04:00
|
|
|
static void SHA1Final_int(u_int8_t *, void *);
|
2003-08-27 04:20:56 +04:00
|
|
|
static int RMD160Update_int(void *, const u_int8_t *, u_int16_t);
|
|
|
|
static int SHA256Update_int(void *, const u_int8_t *, u_int16_t);
|
|
|
|
static int SHA384Update_int(void *, const u_int8_t *, u_int16_t);
|
|
|
|
static int SHA512Update_int(void *, const u_int8_t *, u_int16_t);
|
2003-07-26 01:12:39 +04:00
|
|
|
|
|
|
|
static u_int32_t deflate_compress(u_int8_t *, u_int32_t, u_int8_t **);
|
|
|
|
static u_int32_t deflate_decompress(u_int8_t *, u_int32_t, u_int8_t **);
|
|
|
|
|
|
|
|
MALLOC_DEFINE(M_XDATA, "xform", "xform data buffers");
|
|
|
|
|
|
|
|
/* Encryption instances */
|
|
|
|
struct enc_xform enc_xform_null = {
|
|
|
|
CRYPTO_NULL_CBC, "NULL",
|
|
|
|
/* NB: blocksize of 4 is to generate a properly aligned ESP header */
|
|
|
|
4, 0, 256, /* 2048 bits, max key */
|
|
|
|
null_encrypt,
|
|
|
|
null_decrypt,
|
|
|
|
null_setkey,
|
|
|
|
null_zerokey,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct enc_xform enc_xform_des = {
|
|
|
|
CRYPTO_DES_CBC, "DES",
|
|
|
|
8, 8, 8,
|
|
|
|
des1_encrypt,
|
|
|
|
des1_decrypt,
|
|
|
|
des1_setkey,
|
|
|
|
des1_zerokey,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct enc_xform enc_xform_3des = {
|
|
|
|
CRYPTO_3DES_CBC, "3DES",
|
|
|
|
8, 24, 24,
|
|
|
|
des3_encrypt,
|
|
|
|
des3_decrypt,
|
|
|
|
des3_setkey,
|
|
|
|
des3_zerokey
|
|
|
|
};
|
|
|
|
|
|
|
|
struct enc_xform enc_xform_blf = {
|
|
|
|
CRYPTO_BLF_CBC, "Blowfish",
|
|
|
|
8, 5, 56 /* 448 bits, max key */,
|
|
|
|
blf_encrypt,
|
|
|
|
blf_decrypt,
|
|
|
|
blf_setkey,
|
|
|
|
blf_zerokey
|
|
|
|
};
|
|
|
|
|
|
|
|
struct enc_xform enc_xform_cast5 = {
|
|
|
|
CRYPTO_CAST_CBC, "CAST-128",
|
|
|
|
8, 5, 16,
|
|
|
|
cast5_encrypt,
|
|
|
|
cast5_decrypt,
|
|
|
|
cast5_setkey,
|
|
|
|
cast5_zerokey
|
|
|
|
};
|
|
|
|
|
|
|
|
struct enc_xform enc_xform_skipjack = {
|
|
|
|
CRYPTO_SKIPJACK_CBC, "Skipjack",
|
|
|
|
8, 10, 10,
|
|
|
|
skipjack_encrypt,
|
|
|
|
skipjack_decrypt,
|
|
|
|
skipjack_setkey,
|
|
|
|
skipjack_zerokey
|
|
|
|
};
|
|
|
|
|
|
|
|
struct enc_xform enc_xform_rijndael128 = {
|
|
|
|
CRYPTO_RIJNDAEL128_CBC, "Rijndael-128/AES",
|
|
|
|
16, 8, 32,
|
|
|
|
rijndael128_encrypt,
|
|
|
|
rijndael128_decrypt,
|
|
|
|
rijndael128_setkey,
|
|
|
|
rijndael128_zerokey,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct enc_xform enc_xform_arc4 = {
|
|
|
|
CRYPTO_ARC4, "ARC4",
|
|
|
|
1, 1, 32,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Authentication instances */
|
|
|
|
struct auth_hash auth_hash_null = {
|
|
|
|
CRYPTO_NULL_HMAC, "NULL-HMAC",
|
|
|
|
0, 0, 12, sizeof(int), /* NB: context isn't used */
|
|
|
|
null_init, null_update, null_final
|
|
|
|
};
|
|
|
|
|
|
|
|
struct auth_hash auth_hash_hmac_md5_96 = {
|
|
|
|
CRYPTO_MD5_HMAC, "HMAC-MD5",
|
|
|
|
16, 16, 12, sizeof(MD5_CTX),
|
|
|
|
(void (*) (void *)) MD5Init, MD5Update_int,
|
|
|
|
(void (*) (u_int8_t *, void *)) MD5Final
|
|
|
|
};
|
|
|
|
|
|
|
|
struct auth_hash auth_hash_hmac_sha1_96 = {
|
|
|
|
CRYPTO_SHA1_HMAC, "HMAC-SHA1",
|
|
|
|
20, 20, 12, sizeof(SHA1_CTX),
|
|
|
|
SHA1Init_int, SHA1Update_int, SHA1Final_int
|
|
|
|
};
|
|
|
|
|
|
|
|
struct auth_hash auth_hash_hmac_ripemd_160_96 = {
|
|
|
|
CRYPTO_RIPEMD160_HMAC, "HMAC-RIPEMD-160",
|
|
|
|
20, 20, 12, sizeof(RMD160_CTX),
|
|
|
|
(void (*)(void *)) RMD160Init, RMD160Update_int,
|
|
|
|
(void (*)(u_int8_t *, void *)) RMD160Final
|
|
|
|
};
|
|
|
|
|
|
|
|
struct auth_hash auth_hash_key_md5 = {
|
|
|
|
CRYPTO_MD5_KPDK, "Keyed MD5",
|
2003-08-01 05:47:45 +04:00
|
|
|
0, 16, 16, sizeof(MD5_CTX),
|
2003-07-26 01:12:39 +04:00
|
|
|
(void (*)(void *)) MD5Init, MD5Update_int,
|
|
|
|
(void (*)(u_int8_t *, void *)) MD5Final
|
|
|
|
};
|
|
|
|
|
|
|
|
struct auth_hash auth_hash_key_sha1 = {
|
|
|
|
CRYPTO_SHA1_KPDK, "Keyed SHA1",
|
2003-08-01 05:47:45 +04:00
|
|
|
0, 20, 20, sizeof(SHA1_CTX),
|
2003-07-26 01:12:39 +04:00
|
|
|
SHA1Init_int, SHA1Update_int, SHA1Final_int
|
|
|
|
};
|
|
|
|
|
|
|
|
struct auth_hash auth_hash_md5 = {
|
|
|
|
CRYPTO_MD5, "MD5",
|
|
|
|
0, 16, 16, sizeof(MD5_CTX),
|
|
|
|
(void (*) (void *)) MD5Init, MD5Update_int,
|
|
|
|
(void (*) (u_int8_t *, void *)) MD5Final
|
|
|
|
};
|
|
|
|
|
|
|
|
struct auth_hash auth_hash_sha1 = {
|
|
|
|
CRYPTO_SHA1, "SHA1",
|
|
|
|
0, 20, 20, sizeof(SHA1_CTX),
|
|
|
|
(void (*)(void *)) SHA1Init, SHA1Update_int,
|
|
|
|
(void (*)(u_int8_t *, void *)) SHA1Final
|
|
|
|
};
|
|
|
|
|
|
|
|
struct auth_hash auth_hash_hmac_sha2_256 = {
|
|
|
|
CRYPTO_SHA2_HMAC, "HMAC-SHA2",
|
|
|
|
32, 32, 12, sizeof(SHA256_CTX),
|
|
|
|
(void (*)(void *)) SHA256_Init, SHA256Update_int,
|
|
|
|
(void (*)(u_int8_t *, void *)) SHA256_Final
|
|
|
|
};
|
|
|
|
|
|
|
|
struct auth_hash auth_hash_hmac_sha2_384 = {
|
|
|
|
CRYPTO_SHA2_HMAC, "HMAC-SHA2-384",
|
|
|
|
48, 48, 12, sizeof(SHA384_CTX),
|
|
|
|
(void (*)(void *)) SHA384_Init, SHA384Update_int,
|
|
|
|
(void (*)(u_int8_t *, void *)) SHA384_Final
|
|
|
|
};
|
|
|
|
|
|
|
|
struct auth_hash auth_hash_hmac_sha2_512 = {
|
|
|
|
CRYPTO_SHA2_HMAC, "HMAC-SHA2-512",
|
|
|
|
64, 64, 12, sizeof(SHA512_CTX),
|
|
|
|
(void (*)(void *)) SHA512_Init, SHA512Update_int,
|
|
|
|
(void (*)(u_int8_t *, void *)) SHA512_Final
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Compression instance */
|
|
|
|
struct comp_algo comp_algo_deflate = {
|
|
|
|
CRYPTO_DEFLATE_COMP, "Deflate",
|
|
|
|
90, deflate_compress,
|
|
|
|
deflate_decompress
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Encryption wrapper routines.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
null_encrypt(caddr_t key, u_int8_t *blk)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
static void
|
|
|
|
null_decrypt(caddr_t key, u_int8_t *blk)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
static int
|
2003-08-27 04:20:56 +04:00
|
|
|
null_setkey(u_int8_t **sched, const u_int8_t *key, int len)
|
2003-07-26 01:12:39 +04:00
|
|
|
{
|
|
|
|
*sched = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
static void
|
|
|
|
null_zerokey(u_int8_t **sched)
|
|
|
|
{
|
|
|
|
*sched = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
des1_encrypt(caddr_t key, u_int8_t *blk)
|
|
|
|
{
|
|
|
|
des_cblock *cb = (des_cblock *) blk;
|
|
|
|
des_key_schedule *p = (des_key_schedule *) key;
|
|
|
|
|
|
|
|
des_ecb_encrypt(cb, cb, p[0], DES_ENCRYPT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
des1_decrypt(caddr_t key, u_int8_t *blk)
|
|
|
|
{
|
|
|
|
des_cblock *cb = (des_cblock *) blk;
|
|
|
|
des_key_schedule *p = (des_key_schedule *) key;
|
|
|
|
|
|
|
|
des_ecb_encrypt(cb, cb, p[0], DES_DECRYPT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2003-08-27 04:20:56 +04:00
|
|
|
des1_setkey(u_int8_t **sched, const u_int8_t *key, int len)
|
2003-07-26 01:12:39 +04:00
|
|
|
{
|
|
|
|
des_key_schedule *p;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
MALLOC(p, des_key_schedule *, sizeof (des_key_schedule),
|
|
|
|
M_CRYPTO_DATA, M_NOWAIT);
|
|
|
|
if (p != NULL) {
|
|
|
|
bzero(p, sizeof(des_key_schedule));
|
|
|
|
des_set_key((des_cblock *) key, p[0]);
|
|
|
|
err = 0;
|
|
|
|
} else
|
|
|
|
err = ENOMEM;
|
|
|
|
*sched = (u_int8_t *) p;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
des1_zerokey(u_int8_t **sched)
|
|
|
|
{
|
|
|
|
bzero(*sched, sizeof (des_key_schedule));
|
|
|
|
FREE(*sched, M_CRYPTO_DATA);
|
|
|
|
*sched = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
des3_encrypt(caddr_t key, u_int8_t *blk)
|
|
|
|
{
|
|
|
|
des_cblock *cb = (des_cblock *) blk;
|
|
|
|
des_key_schedule *p = (des_key_schedule *) key;
|
|
|
|
|
|
|
|
des_ecb3_encrypt(cb, cb, p[0], p[1], p[2], DES_ENCRYPT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
des3_decrypt(caddr_t key, u_int8_t *blk)
|
|
|
|
{
|
|
|
|
des_cblock *cb = (des_cblock *) blk;
|
|
|
|
des_key_schedule *p = (des_key_schedule *) key;
|
|
|
|
|
|
|
|
des_ecb3_encrypt(cb, cb, p[0], p[1], p[2], DES_DECRYPT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2003-08-27 04:20:56 +04:00
|
|
|
des3_setkey(u_int8_t **sched, const u_int8_t *key, int len)
|
2003-07-26 01:12:39 +04:00
|
|
|
{
|
|
|
|
des_key_schedule *p;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
MALLOC(p, des_key_schedule *, 3*sizeof (des_key_schedule),
|
|
|
|
M_CRYPTO_DATA, M_NOWAIT);
|
|
|
|
if (p != NULL) {
|
|
|
|
bzero(p, 3*sizeof(des_key_schedule));
|
|
|
|
des_set_key((des_cblock *)(key + 0), p[0]);
|
|
|
|
des_set_key((des_cblock *)(key + 8), p[1]);
|
|
|
|
des_set_key((des_cblock *)(key + 16), p[2]);
|
|
|
|
err = 0;
|
|
|
|
} else
|
|
|
|
err = ENOMEM;
|
|
|
|
*sched = (u_int8_t *) p;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
des3_zerokey(u_int8_t **sched)
|
|
|
|
{
|
|
|
|
bzero(*sched, 3*sizeof (des_key_schedule));
|
|
|
|
FREE(*sched, M_CRYPTO_DATA);
|
|
|
|
*sched = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
blf_encrypt(caddr_t key, u_int8_t *blk)
|
|
|
|
{
|
2003-08-27 04:05:26 +04:00
|
|
|
|
|
|
|
#if defined(__NetBSD__)
|
|
|
|
BF_ecb_encrypt(blk, blk, (BF_KEY *)key, 1);
|
|
|
|
#else
|
2003-07-26 01:12:39 +04:00
|
|
|
blf_ecb_encrypt((blf_ctx *) key, blk, 8);
|
2003-08-27 04:05:26 +04:00
|
|
|
#endif
|
2003-07-26 01:12:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
blf_decrypt(caddr_t key, u_int8_t *blk)
|
|
|
|
{
|
2003-08-27 04:05:26 +04:00
|
|
|
|
|
|
|
#if defined(__NetBSD__)
|
|
|
|
BF_ecb_encrypt(blk, blk, (BF_KEY *)key, 0);
|
|
|
|
#else
|
2003-07-26 01:12:39 +04:00
|
|
|
blf_ecb_decrypt((blf_ctx *) key, blk, 8);
|
2003-08-27 04:05:26 +04:00
|
|
|
#endif
|
2003-07-26 01:12:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2003-08-27 04:20:56 +04:00
|
|
|
blf_setkey(u_int8_t **sched, const u_int8_t *key, int len)
|
2003-07-26 01:12:39 +04:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2003-08-27 04:05:26 +04:00
|
|
|
#if defined(__FreeBSD__) || defined(__NetBSD__)
|
|
|
|
#define BLF_SIZ sizeof(BF_KEY)
|
2003-07-26 01:12:39 +04:00
|
|
|
#else
|
2003-08-27 04:05:26 +04:00
|
|
|
#define BLF_SIZ sizeof(blf_ctx)
|
2003-07-26 01:12:39 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
MALLOC(*sched, u_int8_t *, BLF_SIZ,
|
|
|
|
M_CRYPTO_DATA, M_NOWAIT);
|
|
|
|
if (*sched != NULL) {
|
|
|
|
bzero(*sched, BLF_SIZ);
|
2003-08-27 04:05:26 +04:00
|
|
|
#if defined(__FreeBSD__) || defined(__NetBSD__)
|
2003-07-26 01:12:39 +04:00
|
|
|
BF_set_key((BF_KEY *) *sched, len, key);
|
|
|
|
#else
|
|
|
|
blf_key((blf_ctx *)*sched, key, len);
|
|
|
|
#endif
|
|
|
|
err = 0;
|
|
|
|
} else
|
|
|
|
err = ENOMEM;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
blf_zerokey(u_int8_t **sched)
|
|
|
|
{
|
|
|
|
bzero(*sched, BLF_SIZ);
|
|
|
|
FREE(*sched, M_CRYPTO_DATA);
|
|
|
|
*sched = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cast5_encrypt(caddr_t key, u_int8_t *blk)
|
|
|
|
{
|
2003-08-26 20:37:36 +04:00
|
|
|
cast128_encrypt((cast128_key *) key, blk, blk);
|
2003-07-26 01:12:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cast5_decrypt(caddr_t key, u_int8_t *blk)
|
|
|
|
{
|
2003-08-26 20:37:36 +04:00
|
|
|
cast128_decrypt((cast128_key *) key, blk, blk);
|
2003-07-26 01:12:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2003-08-27 04:20:56 +04:00
|
|
|
cast5_setkey(u_int8_t **sched, const u_int8_t *key, int len)
|
2003-07-26 01:12:39 +04:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2003-08-26 20:37:36 +04:00
|
|
|
MALLOC(*sched, u_int8_t *, sizeof(cast128_key), M_CRYPTO_DATA,
|
2003-07-26 01:12:39 +04:00
|
|
|
M_NOWAIT);
|
|
|
|
if (*sched != NULL) {
|
2003-08-26 20:37:36 +04:00
|
|
|
bzero(*sched, sizeof(cast128_key));
|
|
|
|
cast128_setkey((cast128_key *)*sched, key, len);
|
2003-07-26 01:12:39 +04:00
|
|
|
err = 0;
|
|
|
|
} else
|
|
|
|
err = ENOMEM;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cast5_zerokey(u_int8_t **sched)
|
|
|
|
{
|
2003-08-26 20:37:36 +04:00
|
|
|
bzero(*sched, sizeof(cast128_key));
|
2003-07-26 01:12:39 +04:00
|
|
|
FREE(*sched, M_CRYPTO_DATA);
|
|
|
|
*sched = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
skipjack_encrypt(caddr_t key, u_int8_t *blk)
|
|
|
|
{
|
|
|
|
skipjack_forwards(blk, blk, (u_int8_t **) key);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
skipjack_decrypt(caddr_t key, u_int8_t *blk)
|
|
|
|
{
|
|
|
|
skipjack_backwards(blk, blk, (u_int8_t **) key);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2003-08-27 04:20:56 +04:00
|
|
|
skipjack_setkey(u_int8_t **sched, const u_int8_t *key, int len)
|
2003-07-26 01:12:39 +04:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
/* NB: allocate all the memory that's needed at once */
|
|
|
|
/* XXX assumes bytes are aligned on sizeof(u_char) == 1 boundaries.
|
|
|
|
* Will this break a pdp-10, Cray-1, or GE-645 port?
|
|
|
|
*/
|
|
|
|
MALLOC(*sched, u_int8_t *, 10 * (sizeof(u_int8_t *) + 0x100),
|
|
|
|
M_CRYPTO_DATA, M_NOWAIT);
|
|
|
|
|
|
|
|
if (*sched != NULL) {
|
|
|
|
|
|
|
|
u_int8_t** key_tables = (u_int8_t**) *sched;
|
|
|
|
u_int8_t* table = (u_int8_t*) &key_tables[10];
|
|
|
|
int k;
|
|
|
|
|
|
|
|
bzero(*sched, 10 * sizeof(u_int8_t *)+0x100);
|
|
|
|
|
|
|
|
for (k = 0; k < 10; k++) {
|
|
|
|
key_tables[k] = table;
|
|
|
|
table += 0x100;
|
|
|
|
}
|
|
|
|
subkey_table_gen(key, (u_int8_t **) *sched);
|
|
|
|
err = 0;
|
|
|
|
} else
|
|
|
|
err = ENOMEM;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
skipjack_zerokey(u_int8_t **sched)
|
|
|
|
{
|
|
|
|
bzero(*sched, 10 * (sizeof(u_int8_t *) + 0x100));
|
|
|
|
FREE(*sched, M_CRYPTO_DATA);
|
|
|
|
*sched = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rijndael128_encrypt(caddr_t key, u_int8_t *blk)
|
|
|
|
{
|
|
|
|
rijndael_encrypt((rijndael_ctx *) key, (u_char *) blk, (u_char *) blk);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rijndael128_decrypt(caddr_t key, u_int8_t *blk)
|
|
|
|
{
|
2003-08-27 18:55:36 +04:00
|
|
|
rijndael_decrypt((rijndael_ctx *) key, (u_char *) blk,
|
2003-07-26 01:12:39 +04:00
|
|
|
(u_char *) blk);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2003-08-27 04:20:56 +04:00
|
|
|
rijndael128_setkey(u_int8_t **sched, const u_int8_t *key, int len)
|
2003-07-26 01:12:39 +04:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2003-08-27 18:23:25 +04:00
|
|
|
MALLOC(*sched, u_int8_t *, sizeof(rijndael_ctx), M_CRYPTO_DATA,
|
2003-07-26 01:12:39 +04:00
|
|
|
M_WAITOK);
|
|
|
|
if (*sched != NULL) {
|
2003-08-27 18:55:36 +04:00
|
|
|
bzero(*sched, sizeof(rijndael_ctx));
|
|
|
|
rijndael_set_key((rijndael_ctx *) *sched, key, len * 8);
|
2003-07-26 01:12:39 +04:00
|
|
|
err = 0;
|
|
|
|
} else
|
|
|
|
err = ENOMEM;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rijndael128_zerokey(u_int8_t **sched)
|
|
|
|
{
|
2003-11-19 02:01:39 +03:00
|
|
|
bzero(*sched, sizeof(rijndael_ctx));
|
2003-07-26 01:12:39 +04:00
|
|
|
FREE(*sched, M_CRYPTO_DATA);
|
|
|
|
*sched = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* And now for auth.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
null_init(void *ctx)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2003-08-27 04:20:56 +04:00
|
|
|
null_update(void *ctx, const u_int8_t *buf, u_int16_t len)
|
2003-07-26 01:12:39 +04:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
null_final(u_int8_t *buf, void *ctx)
|
|
|
|
{
|
|
|
|
if (buf != (u_int8_t *) 0)
|
|
|
|
bzero(buf, 12);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2003-08-27 04:20:56 +04:00
|
|
|
RMD160Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
|
2003-07-26 01:12:39 +04:00
|
|
|
{
|
|
|
|
RMD160Update(ctx, buf, len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2003-08-27 04:20:56 +04:00
|
|
|
MD5Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
|
2003-07-26 01:12:39 +04:00
|
|
|
{
|
|
|
|
MD5Update(ctx, buf, len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
SHA1Init_int(void *ctx)
|
|
|
|
{
|
|
|
|
SHA1Init(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2003-08-27 04:20:56 +04:00
|
|
|
SHA1Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
|
2003-07-26 01:12:39 +04:00
|
|
|
{
|
|
|
|
SHA1Update(ctx, buf, len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
SHA1Final_int(u_int8_t *blk, void *ctx)
|
|
|
|
{
|
|
|
|
SHA1Final(blk, ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2003-08-27 04:20:56 +04:00
|
|
|
SHA256Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
|
2003-07-26 01:12:39 +04:00
|
|
|
{
|
|
|
|
SHA256_Update(ctx, buf, len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2003-08-27 04:20:56 +04:00
|
|
|
SHA384Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
|
2003-07-26 01:12:39 +04:00
|
|
|
{
|
|
|
|
SHA384_Update(ctx, buf, len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2003-08-27 04:20:56 +04:00
|
|
|
SHA512Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
|
2003-07-26 01:12:39 +04:00
|
|
|
{
|
|
|
|
SHA512_Update(ctx, buf, len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* And compression
|
|
|
|
*/
|
|
|
|
|
|
|
|
static u_int32_t
|
|
|
|
deflate_compress(data, size, out)
|
|
|
|
u_int8_t *data;
|
|
|
|
u_int32_t size;
|
|
|
|
u_int8_t **out;
|
|
|
|
{
|
|
|
|
return deflate_global(data, size, 0, out);
|
|
|
|
}
|
|
|
|
|
|
|
|
static u_int32_t
|
|
|
|
deflate_decompress(data, size, out)
|
|
|
|
u_int8_t *data;
|
|
|
|
u_int32_t size;
|
|
|
|
u_int8_t **out;
|
|
|
|
{
|
|
|
|
return deflate_global(data, size, 1, out);
|
|
|
|
}
|