From 982775563f27b0fa2b92f8d4df1a950f001949a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=B6ller?= Date: Thu, 10 Oct 2013 22:13:02 +0200 Subject: Reorganized camellia interface Use distinct context structs and functions for camellia128 and camellia256. --- camellia.h | 111 ++++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 80 insertions(+), 31 deletions(-) (limited to 'camellia.h') diff --git a/camellia.h b/camellia.h index c0b45761..dcc43ae7 100644 --- a/camellia.h +++ b/camellia.h @@ -3,7 +3,7 @@ * Copyright (C) 2006,2007 * NTT (Nippon Telegraph and Telephone Corporation). * - * Copyright (C) 2010 Niels Möller + * Copyright (C) 2010, 2013 Niels Möller * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -30,51 +30,100 @@ extern "C" { #endif /* Name mangling */ -#define camellia_set_encrypt_key nettle_camellia_set_encrypt_key -#define camellia_set_decrypt_key nettle_camellia_set_decrypt_key -#define camellia_invert_key nettle_camellia_invert_key -#define camellia_crypt nettle_camellia_crypt -#define camellia_crypt nettle_camellia_crypt +#define camellia128_set_encrypt_key nettle_camellia128_set_encrypt_key +#define camellia128_set_decrypt_key nettle_camellia_set_decrypt_key +#define camellia128_invert_key nettle_camellia128_invert_key +#define camellia128_crypt nettle_camellia128_crypt + +#define camellia192_set_encrypt_key nettle_camellia192_set_encrypt_key +#define camellia192_set_decrypt_key nettle_camellia192_set_decrypt_key + +#define camellia256_set_encrypt_key nettle_camellia256_set_encrypt_key +#define camellia256_set_decrypt_key nettle_camellia256_set_decrypt_key +#define camellia256_invert_key nettle_camellia256_invert_key +#define camellia256_crypt nettle_camellia256_crypt + #define CAMELLIA_BLOCK_SIZE 16 /* Valid key sizes are 128, 192 or 256 bits (16, 24 or 32 bytes) */ -#define CAMELLIA_MIN_KEY_SIZE 16 -#define CAMELLIA_MAX_KEY_SIZE 32 -#define CAMELLIA_KEY_SIZE 32 +#define CAMELLIA128_KEY_SIZE 16 +#define CAMELLIA192_KEY_SIZE 24 +#define CAMELLIA256_KEY_SIZE 32 -struct camellia_ctx +/* For 128-bit keys, there are 18 regular rounds, pre- and + post-whitening, and two FL and FLINV rounds, using a total of 26 + subkeys, each of 64 bit. For 192- and 256-bit keys, there are 6 + additional regular rounds and one additional FL and FLINV, using a + total of 34 subkeys. */ +/* The clever combination of subkeys imply one of the pre- and + post-whitening keys is folded with the round keys, so that subkey + #1 and the last one (#25 or #33) is not used. The result is that we + have only 24 or 32 subkeys at the end of key setup. */ + +#define _CAMELLIA128_NKEYS 24 +#define _CAMELLIA256_NKEYS 32 + +struct camellia128_ctx { - /* Number of subkeys. */ - unsigned nkeys; + uint64_t keys[_CAMELLIA128_NKEYS]; +}; + +void +camellia128_set_encrypt_key(struct camellia128_ctx *ctx, + const uint8_t *key); + +void +camellia128_set_decrypt_key(struct camellia128_ctx *ctx, + const uint8_t *key); + +void +camellia128_invert_key(struct camellia128_ctx *dst, + const struct camellia128_ctx *src); - /* For 128-bit keys, there are 18 regular rounds, pre- and - post-whitening, and two FL and FLINV rounds, using a total of 26 - subkeys, each of 64 bit. For 192- and 256-bit keys, there are 6 - additional regular rounds and one additional FL and FLINV, using - a total of 34 subkeys. */ - /* The clever combination of subkeys imply one of the pre- and - post-whitening keys is folded with the round keys, so that subkey - #1 and the last one (#25 or #33) is not used. The result is that - we have only 24 or 32 subkeys at the end of key setup. */ - uint64_t keys[32]; +void +camellia128_crypt(const struct camellia128_ctx *ctx, + size_t length, uint8_t *dst, + const uint8_t *src); + +struct camellia256_ctx +{ + uint64_t keys[_CAMELLIA256_NKEYS]; }; void -camellia_set_encrypt_key(struct camellia_ctx *ctx, - size_t length, const uint8_t *key); +camellia256_set_encrypt_key(struct camellia256_ctx *ctx, + const uint8_t *key); void -camellia_set_decrypt_key(struct camellia_ctx *ctx, - size_t length, const uint8_t *key); +camellia256_set_decrypt_key(struct camellia256_ctx *ctx, + const uint8_t *key); void -camellia_invert_key(struct camellia_ctx *dst, - const struct camellia_ctx *src); +camellia256_invert_key(struct camellia256_ctx *dst, + const struct camellia256_ctx *src); void -camellia_crypt(const struct camellia_ctx *ctx, - size_t length, uint8_t *dst, - const uint8_t *src); +camellia256_crypt(const struct camellia256_ctx *ctx, + size_t length, uint8_t *dst, + const uint8_t *src); + +/* camellia192 is the same as camellia256, except for the key + schedule. */ +/* Slightly ugly with a #define on a struct tag, since it might cause + surprises if also used as a name of a variable. */ +#define camellia192_ctx camellia256_ctx + +void +camellia192_set_encrypt_key(struct camellia256_ctx *ctx, + const uint8_t *key); + +void +camellia192_set_decrypt_key(struct camellia256_ctx *ctx, + const uint8_t *key); + +#define camellia192_invert_key camellia256_invert_key +#define camellia192_crypt camellia256_crypt + #ifdef __cplusplus } #endif -- cgit v1.2.1