diff --git a/speck.c b/speck.c index ade5942..a7e61d2 100644 --- a/speck.c +++ b/speck.c @@ -15,15 +15,32 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include "speck.h" -#define ROR(x, r) ((x >> r) | (x << ((sizeof(SPECK_TYPE) * 8) - r))) -#define ROL(x, r) ((x << r) | (x >> ((sizeof(SPECK_TYPE) * 8) - r))) +#if WORDSIZE == 24 + // rotates for word size n=24 bits + #define ROR(x, r) ((x >> r) | (x << (24 - r))&MASK24)&MASK24 + #define ROL(x, r) ((x << r) | (x >> (24 - r))&MASK24)&MASK24 +#elif WORDSIZE == 48 + #define ROR(x, r) ((x >> r) | (x << (48 - r))&MASK48)&MASK48 + #define ROL(x, r) ((x << r) | (x >> (48 - r))&MASK48)&MASK48 +#else + #define ROR(x, r) ((x >> r) | (x << ((sizeof(SPECK_TYPE) * 8) - r))) + #define ROL(x, r) ((x << r) | (x >> ((sizeof(SPECK_TYPE) * 8) - r))) +#endif #ifdef SPECK_32_64 -#define R(x, y, k) (x = ROR(x, 7), x += y, x ^= k, y = ROL(y, 2), y ^= x) -#define RR(x, y, k) (y ^= x, y = ROR(y, 2), x ^= k, x -= y, x = ROL(x, 7)) + #define R(x, y, k) (x = ROR(x, 7), x += y, x ^= k, y = ROL(y, 2), y ^= x) + #define RR(x, y, k) (y ^= x, y = ROR(y, 2), x ^= k, x -= y, x = ROL(x, 7)) #else -#define R(x, y, k) (x = ROR(x, 8), x += y, x ^= k, y = ROL(y, 3), y ^= x) -#define RR(x, y, k) (y ^= x, y = ROR(y, 3), x ^= k, x -= y, x = ROL(x, 8)) + #if WORDSIZE == 24 + #define R(x, y, k) (x = ROR(x, 8), x = (x + y)&MASK24, x ^= k, y = ROL(y, 3), y ^= x) + #define RR(x, y, k) (y ^= x, y = ROR(y, 3), x ^= k, x = (x - y)&MASK24, x = ROL(x, 8)) + #elif WORDSIZE == 48 + #define R(x, y, k) (x = ROR(x, 8), x = (x + y)&MASK48, x ^= k, y = ROL(y, 3), y ^= x) + #define RR(x, y, k) (y ^= x, y = ROR(y, 3), x ^= k, x = (x - y)&MASK48, x = ROL(x, 8)) + #else + #define R(x, y, k) (x = ROR(x, 8), x += y, x ^= k, y = ROL(y, 3), y ^= x) + #define RR(x, y, k) (y ^= x, y = ROR(y, 3), x ^= k, x -= y, x = ROL(x, 8)) + #endif #endif void speck_expand(SPECK_TYPE const K[static SPECK_KEY_LEN], SPECK_TYPE S[static SPECK_ROUNDS]) @@ -116,12 +133,36 @@ int main(int argc, char** argv) uint16_t enc[2] = {0x42f2, 0xa868}; #endif +#ifdef SPECK_48_72 + uint32_t key[3] = {0x020100, 0x0a0908, 0x121110}; + uint32_t plain[2] = {0x6c6172, 0x20796c}; + uint32_t enc[2] = {0x385adc, 0xc049a5}; +#endif + +#ifdef SPECK_48_96 + uint32_t key[4] = {0x020100, 0x0a0908, 0x121110, 0x1a1918}; + uint32_t plain[2] = {0x696874, 0x6d2073}; + uint32_t enc[2] = {0xb6445d, 0x735e10}; +#endif + #ifdef SPECK_64_128 uint32_t key[4] = {0x03020100, 0x0b0a0908, 0x13121110, 0x1b1a1918}; uint32_t plain[2] = {0x7475432d, 0x3b726574}; uint32_t enc[2] = {0x454e028b, 0x8c6fa548}; #endif +#ifdef SPECK_96_96 + uint64_t key[2] = {0x050403020100, 0x0d0c0b0a0908}; + uint64_t plain[2] = {0x656761737520, 0x65776f68202c}; + uint64_t enc[2] = {0x62bdde8f79aa, 0x9e4d09ab7178}; +#endif + +#ifdef SPECK_96_144 + uint64_t key[3] = {0x050403020100, 0x0d0c0b0a0908, 0x151413121110}; + uint64_t plain[2] = {0x69202c726576, 0x656d6974206e}; + uint64_t enc[2] = {0x7ae440252ee6, 0x2bf31072228a}; +#endif + #ifdef SPECK_128_256 uint64_t key[4] = {0x0706050403020100, 0x0f0e0d0c0b0a0908, 0x1716151413121110, 0x1f1e1d1c1b1a1918}; uint64_t plain[2] = {0x202e72656e6f6f70, 0x65736f6874206e49}; @@ -155,4 +196,4 @@ int main(int argc, char** argv) return 0; } -#endif \ No newline at end of file +#endif diff --git a/speck.h b/speck.h index af01060..46cabc6 100644 --- a/speck.h +++ b/speck.h @@ -22,6 +22,9 @@ extern "C" { #include +#define MASK24 0xFFFFFF +#define MASK48 0xFFFFFFFFFFFF + /* * define speck type to use (one of SPECK_32_64, SPECK_64_128, SPECK_128_256) */ @@ -33,12 +36,40 @@ extern "C" { #define SPECK_KEY_LEN 4 #endif +#ifdef SPECK_48_72 +#define SPECK_TYPE uint32_t +#define SPECK_ROUNDS 22 +#define SPECK_KEY_LEN 3 +#define WORDSIZE 24 +#endif + +#ifdef SPECK_48_96 +#define SPECK_TYPE uint32_t +#define SPECK_ROUNDS 23 +#define SPECK_KEY_LEN 4 +#define WORDSIZE 24 +#endif + #ifdef SPECK_64_128 #define SPECK_TYPE uint32_t #define SPECK_ROUNDS 27 #define SPECK_KEY_LEN 4 #endif +#ifdef SPECK_96_96 +#define SPECK_TYPE uint64_t +#define SPECK_ROUNDS 28 +#define SPECK_KEY_LEN 2 +#define WORDSIZE 48 +#endif + +#ifdef SPECK_96_144 +#define SPECK_TYPE uint64_t +#define SPECK_ROUNDS 29 +#define SPECK_KEY_LEN 3 +#define WORDSIZE 48 +#endif + #ifdef SPECK_128_256 #define SPECK_TYPE uint64_t #define SPECK_ROUNDS 34 @@ -56,4 +87,4 @@ void speck_decrypt_combined(SPECK_TYPE const ct[static 2], SPECK_TYPE pt[static } #endif -#endif \ No newline at end of file +#endif