From 97414f05ca75eddc6d819cfcd6ab30e9e24b3eab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Love=20H=C3=B6rnquist=20=C3=85strand?= Date: Tue, 24 Jul 2007 16:29:05 +0000 Subject: [PATCH] Add Camellia git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@21687 ec53bebd-3082-4978-b11e-865c3cabbd6b --- lib/hcrypto/Makefile.am | 4 ++ lib/hcrypto/evp.c | 99 ++++++++++++++++++++++++++++++++++++++- lib/hcrypto/evp.h | 6 +++ lib/hcrypto/test_cipher.c | 30 +++++++++--- 4 files changed, 131 insertions(+), 8 deletions(-) diff --git a/lib/hcrypto/Makefile.am b/lib/hcrypto/Makefile.am index 11ef3a8c0..0d81a31fc 100644 --- a/lib/hcrypto/Makefile.am +++ b/lib/hcrypto/Makefile.am @@ -89,6 +89,10 @@ libhcrypto_la_SOURCES = \ aes.h \ bn.c \ bn.h \ + camellia.h \ + camellia.c \ + camellia-ntt.c \ + camellia-ntt.h \ des-tables.h \ des.c \ des.h \ diff --git a/lib/hcrypto/evp.c b/lib/hcrypto/evp.c index 34480dbe7..c77cf3b60 100644 --- a/lib/hcrypto/evp.c +++ b/lib/hcrypto/evp.c @@ -9,6 +9,7 @@ #include #include +#include "camellia.h" #include #include #include @@ -768,6 +769,99 @@ EVP_aes_256_cbc(void) return &aes_256_cbc; } +static int +camellia_init(EVP_CIPHER_CTX *ctx, + const unsigned char * key, + const unsigned char * iv, + int encp) +{ + CAMELLIA_KEY *k = ctx->cipher_data; + k->bits = ctx->cipher->key_len * 8; + CAMELLIA_set_key(key, ctx->cipher->key_len * 8, k); + return 1; +} + +static int +camellia_do_cipher(EVP_CIPHER_CTX *ctx, + unsigned char *out, + const unsigned char *in, + unsigned int size) +{ + CAMELLIA_KEY *k = ctx->cipher_data; + CAMELLIA_cbc_encrypt(in, out, size, k, ctx->iv, ctx->encrypt); + return 1; +} + +static int +camellia_cleanup(EVP_CIPHER_CTX *ctx) +{ + memset(ctx->cipher_data, 0, sizeof(CAMELLIA_KEY)); + return 1; +} + +const EVP_CIPHER * +EVP_camellia_128_cbc(void) +{ + static const EVP_CIPHER cipher = { + 0, + 16, + 16, + 16, + EVP_CIPH_CBC_MODE, + camellia_init, + camellia_do_cipher, + camellia_cleanup, + sizeof(CAMELLIA_KEY), + NULL, + NULL, + NULL, + NULL + }; + return &cipher; +} + +const EVP_CIPHER * +EVP_camellia_192_cbc(void) +{ + static const EVP_CIPHER cipher = { + 0, + 16, + 24, + 16, + EVP_CIPH_CBC_MODE, + camellia_init, + camellia_do_cipher, + camellia_cleanup, + sizeof(CAMELLIA_KEY), + NULL, + NULL, + NULL, + NULL + }; + return &cipher; +} + +const EVP_CIPHER * +EVP_camellia_256_cbc(void) +{ + static const EVP_CIPHER cipher = { + 0, + 16, + 32, + 16, + EVP_CIPH_CBC_MODE, + camellia_init, + camellia_do_cipher, + camellia_cleanup, + sizeof(CAMELLIA_KEY), + NULL, + NULL, + NULL, + NULL + }; + return &cipher; +} + /* * */ @@ -779,7 +873,10 @@ static const struct cipher_name { { "des-ede3-cbc", EVP_des_ede3_cbc }, { "aes-128-cbc", EVP_aes_128_cbc }, { "aes-192-cbc", EVP_aes_192_cbc }, - { "aes-256-cbc", EVP_aes_256_cbc } + { "aes-256-cbc", EVP_aes_256_cbc }, + { "camellia-128-cbc", EVP_camellia_128_cbc }, + { "camellia-192-cbc", EVP_camellia_192_cbc }, + { "camellia-256-cbc", EVP_camellia_256_cbc } }; diff --git a/lib/hcrypto/evp.h b/lib/hcrypto/evp.h index 17fd9e930..33cb6a071 100644 --- a/lib/hcrypto/evp.h +++ b/lib/hcrypto/evp.h @@ -83,6 +83,9 @@ #define EVP_rc2_cbc hc_EVP_rc2_cbc #define EVP_rc4 hc_EVP_rc4 #define EVP_rc4_40 hc_EVP_rc4_40 +#define EVP_camellia_128_cbc hc_EVP_camellia_128_cbc +#define EVP_camellia_192_cbc hc_EVP_camellia_192_cbc +#define EVP_camellia_256_cbc hc_EVP_camellia_256_cbc #define EVP_sha hc_EVP_sha #define EVP_sha1 hc_EVP_sha1 #define EVP_sha256 hc_EVP_sha256 @@ -180,6 +183,9 @@ const EVP_CIPHER * EVP_rc2_64_cbc(void); const EVP_CIPHER * EVP_rc2_cbc(void); const EVP_CIPHER * EVP_rc4(void); const EVP_CIPHER * EVP_rc4_40(void); +const EVP_CIPHER * EVP_camellia_128_cbc(void); +const EVP_CIPHER * EVP_camellia_192_cbc(void); +const EVP_CIPHER * EVP_camellia_256_cbc(void); /* * diff --git a/lib/hcrypto/test_cipher.c b/lib/hcrypto/test_cipher.c index 920a68743..c030c63f4 100644 --- a/lib/hcrypto/test_cipher.c +++ b/lib/hcrypto/test_cipher.c @@ -46,6 +46,8 @@ RCSID("$Id$"); #include #include +#include +#include struct tests { void *key; @@ -78,7 +80,7 @@ struct tests rc2_40_tests[] = { } }; -struct tests des_ede3_cbc_tests[] = { +struct tests des_ede3_tests[] = { { "1917ffe6bb772efc297643bc63567e9a002e4d431d5ffd58", 24, "\xbf\x9a\x12\xb7\x26\x69\xfd\x05", @@ -88,6 +90,16 @@ struct tests des_ede3_cbc_tests[] = { } }; +struct tests camellia128_tests[] = { + { "\x19\x17\xff\xe6\xbb\x77\x2e\xfc\x29\x76\x43\xbc\x63\x56\x7e\x9a", + 16, + "\x12\x13\xff\xe3\xbb\x7e\x21\xfc\xe9\x76\x44\xbc\x63\x56\x7e\xff", + 16, + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", + "\xf6\x1b\xa9\xa6\xed\xb2\xd3\x4e\x59\x70\xbc\x50\x4e\x89\xd7\xf5" + } +}; + static int test_cipher(const EVP_CIPHER *c, struct tests *t) @@ -110,16 +122,18 @@ test_cipher(const EVP_CIPHER *c, struct tests *t) return 1; if (memcmp(d, t->outdata, t->datasize) != 0) { - printf("encrypt not the same\n"); - return 1; + char *s; + hex_encode(d, t->datasize, &s); + errx(1, "decrypt not the same: %s\n", s); } if (!EVP_Cipher(&dctx, d, d, t->datasize)) return 1; if (memcmp(d, t->indata, t->datasize) != 0) { - printf("decrypt not the same\n"); - return 1; + char *s; + hex_encode(d, t->datasize, &s); + errx(1, "decrypt not the same: %s\n", s); } EVP_CIPHER_CTX_cleanup(&ectx); @@ -139,8 +153,10 @@ main(int argc, char **argv) ret += test_cipher(EVP_aes_256_cbc(), &aes_tests[i]); for (i = 0; i < sizeof(rc2_40_tests)/sizeof(rc2_40_tests[0]); i++) ret += test_cipher(EVP_rc2_40_cbc(), &rc2_40_tests[i]); - for (i = 0; i < sizeof(rc2_40_tests)/sizeof(rc2_40_tests[0]); i++) - ret += test_cipher(EVP_des_ede3_cbc(), &des_ede3_cbc_tests[i]); + for (i = 0; i < sizeof(des_ede3_tests)/sizeof(des_ede3_tests[0]); i++) + ret += test_cipher(EVP_des_ede3_cbc(), &des_ede3_tests[i]); + for (i = 0; i < sizeof(camellia128_tests)/sizeof(camellia128_tests[0]); i++) + ret += test_cipher(EVP_des_ede3_cbc(), &camellia128_tests[i]); return ret; }