diff options
author | lloyd <[email protected]> | 2008-11-08 19:46:52 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-11-08 19:46:52 +0000 |
commit | f1c459725da56fd8ed5766e7779300182fa26bcf (patch) | |
tree | 32295cec92df1155563ae8a535dc695d6800d7f6 /src/block/noekeon | |
parent | 8dba7b5264403e781bbb86ff61850e4377dca7b9 (diff) |
Split ciphers into block and stream ciphers. Move base class headers
Diffstat (limited to 'src/block/noekeon')
-rw-r--r-- | src/block/noekeon/info.txt | 10 | ||||
-rw-r--r-- | src/block/noekeon/noekeon.cpp | 196 | ||||
-rw-r--r-- | src/block/noekeon/noekeon.h | 35 |
3 files changed, 241 insertions, 0 deletions
diff --git a/src/block/noekeon/info.txt b/src/block/noekeon/info.txt new file mode 100644 index 000000000..8025c2073 --- /dev/null +++ b/src/block/noekeon/info.txt @@ -0,0 +1,10 @@ +realname "Noekeon" + +define NOEKEON + +load_on auto + +<add> +noekeon.cpp +noekeon.h +</add> diff --git a/src/block/noekeon/noekeon.cpp b/src/block/noekeon/noekeon.cpp new file mode 100644 index 000000000..437972fe5 --- /dev/null +++ b/src/block/noekeon/noekeon.cpp @@ -0,0 +1,196 @@ +/************************************************* +* Noekeon Source File * +* (C) 1999-2008 Jack Lloyd * +*************************************************/ + +#include <botan/noekeon.h> +#include <botan/loadstor.h> +#include <botan/bit_ops.h> + +namespace Botan { + +namespace { + +/************************************************* +* Noekeon's Theta Operation * +*************************************************/ +inline void theta(u32bit& A0, u32bit& A1, + u32bit& A2, u32bit& A3, + const u32bit EK[4]) + { + u32bit T = A0 ^ A2; + T ^= rotate_left(T, 8) ^ rotate_right(T, 8); + A1 ^= T; + A3 ^= T; + + A0 ^= EK[0]; + A1 ^= EK[1]; + A2 ^= EK[2]; + A3 ^= EK[3]; + + T = A1 ^ A3; + T ^= rotate_left(T, 8) ^ rotate_right(T, 8); + A0 ^= T; + A2 ^= T; + } + +/************************************************* +* Theta With Null Key * +*************************************************/ +inline void theta(u32bit& A0, u32bit& A1, + u32bit& A2, u32bit& A3) + { + u32bit T = A0 ^ A2; + T ^= rotate_left(T, 8) ^ rotate_right(T, 8); + A1 ^= T; + A3 ^= T; + + T = A1 ^ A3; + T ^= rotate_left(T, 8) ^ rotate_right(T, 8); + A0 ^= T; + A2 ^= T; + } + +/************************************************* +* Noekeon's Gamma S-Box Layer * +*************************************************/ +inline void gamma(u32bit& A0, u32bit& A1, u32bit& A2, u32bit& A3) + { + A1 ^= ~A3 & ~A2; + A0 ^= A2 & A1; + + u32bit T = A3; + A3 = A0; + A0 = T; + + A2 ^= A0 ^ A1 ^ A3; + + A1 ^= ~A3 & ~A2; + A0 ^= A2 & A1; + } + +} + +/************************************************* +* Noekeon Round Constants * +*************************************************/ +const byte Noekeon::RC[] = { + 0x80, 0x1B, 0x36, 0x6C, 0xD8, 0xAB, 0x4D, 0x9A, + 0x2F, 0x5E, 0xBC, 0x63, 0xC6, 0x97, 0x35, 0x6A, + 0xD4 }; + +/************************************************* +* Noekeon Encryption * +*************************************************/ +void Noekeon::enc(const byte in[], byte out[]) const + { + u32bit A0 = load_be<u32bit>(in, 0); + u32bit A1 = load_be<u32bit>(in, 1); + u32bit A2 = load_be<u32bit>(in, 2); + u32bit A3 = load_be<u32bit>(in, 3); + + for(u32bit j = 0; j != 16; ++j) + { + A0 ^= RC[j]; + theta(A0, A1, A2, A3, EK); + + A1 = rotate_left(A1, 1); + A2 = rotate_left(A2, 5); + A3 = rotate_left(A3, 2); + + gamma(A0, A1, A2, A3); + + A1 = rotate_right(A1, 1); + A2 = rotate_right(A2, 5); + A3 = rotate_right(A3, 2); + } + + A0 ^= RC[16]; + theta(A0, A1, A2, A3, EK); + + store_be(out, A0, A1, A2, A3); + } + +/************************************************* +* Noekeon Encryption * +*************************************************/ +void Noekeon::dec(const byte in[], byte out[]) const + { + u32bit A0 = load_be<u32bit>(in, 0); + u32bit A1 = load_be<u32bit>(in, 1); + u32bit A2 = load_be<u32bit>(in, 2); + u32bit A3 = load_be<u32bit>(in, 3); + + for(u32bit j = 16; j != 0; --j) + { + theta(A0, A1, A2, A3, DK); + A0 ^= RC[j]; + + A1 = rotate_left(A1, 1); + A2 = rotate_left(A2, 5); + A3 = rotate_left(A3, 2); + + gamma(A0, A1, A2, A3); + + A1 = rotate_right(A1, 1); + A2 = rotate_right(A2, 5); + A3 = rotate_right(A3, 2); + } + + theta(A0, A1, A2, A3, DK); + A0 ^= RC[0]; + + store_be(out, A0, A1, A2, A3); + } + +/************************************************* +* Noekeon Key Schedule * +*************************************************/ +void Noekeon::key(const byte key[], u32bit) + { + u32bit A0 = load_be<u32bit>(key, 0); + u32bit A1 = load_be<u32bit>(key, 1); + u32bit A2 = load_be<u32bit>(key, 2); + u32bit A3 = load_be<u32bit>(key, 3); + + for(u32bit j = 0; j != 16; ++j) + { + A0 ^= RC[j]; + theta(A0, A1, A2, A3); + + A1 = rotate_left(A1, 1); + A2 = rotate_left(A2, 5); + A3 = rotate_left(A3, 2); + + gamma(A0, A1, A2, A3); + + A1 = rotate_right(A1, 1); + A2 = rotate_right(A2, 5); + A3 = rotate_right(A3, 2); + } + + A0 ^= RC[16]; + + DK[0] = A0; + DK[1] = A1; + DK[2] = A2; + DK[3] = A3; + + theta(A0, A1, A2, A3); + + EK[0] = A0; + EK[1] = A1; + EK[2] = A2; + EK[3] = A3; + } + +/************************************************* +* Clear memory of sensitive data * +*************************************************/ +void Noekeon::clear() throw() + { + EK.clear(); + DK.clear(); + } + +} diff --git a/src/block/noekeon/noekeon.h b/src/block/noekeon/noekeon.h new file mode 100644 index 000000000..fb06f8c79 --- /dev/null +++ b/src/block/noekeon/noekeon.h @@ -0,0 +1,35 @@ +/************************************************* +* Noekeon Header File * +* (C) 1999-2008 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_NOEKEON_H__ +#define BOTAN_NOEKEON_H__ + +#include <botan/block_cipher.h> + +namespace Botan { + +/************************************************* +* Noekeon * +*************************************************/ +class BOTAN_DLL Noekeon : public BlockCipher + { + public: + void clear() throw(); + std::string name() const { return "Noekeon"; } + BlockCipher* clone() const { return new Noekeon; } + Noekeon() : BlockCipher(16, 16) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key(const byte[], u32bit); + + static const byte RC[17]; + + SecureBuffer<u32bit, 4> EK, DK; + }; + +} + +#endif |