diff --git a/crypto/speck/speck.c b/crypto/speck/speck.c index 1b95eb03..0fcbd9e6 100644 --- a/crypto/speck/speck.c +++ b/crypto/speck/speck.c @@ -49,61 +49,146 @@ #include -#define ROR(x, r) ((x >> r) | (x << ((sizeof(SPECK_TYPE) * 8) - r)))//循环右移 -#define ROL(x, r) ((x << r) | (x >> ((sizeof(SPECK_TYPE) * 8) - r)))//循环左移 - -#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)) -#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 - -void speck_set_encrypt_key(speck_key_t *key, const unsigned char *user_key) +void speck_set_encrypt_key16(SPECK_TYPE16 const K[SPECK_KEY_LEN16], SPECK_TYPE16 S[SPECK_ROUNDS16]) { - int i; - for (i = 0; i < num_word; i++) - { - if (user_key[i] == '\0') - break; - key->rk[i] = user_key[i]; - } - int j = 0; - for (; i < num_word; i++) - { - key->rk[i] = user_key[j++]; - } -} -void speck_expand(SPECK_TYPE const K[ SPECK_KEY_LEN], SPECK_TYPE S[ SPECK_ROUNDS]) -{ - SPECK_TYPE i, b = K[0]; - SPECK_TYPE a[SPECK_KEY_LEN - 1]; - for (i = 0; i < (SPECK_KEY_LEN - 1); i++) + SPECK_TYPE16 i, b = K[0]; + SPECK_TYPE16 a[SPECK_KEY_LEN16 - 1]; + for (i = 0; i < (SPECK_KEY_LEN16 - 1); i++) { a[i] = K[i + 1]; } S[0] = b; - for (i = 0; i < SPECK_ROUNDS - 1; i++) { - R(a[i % (SPECK_KEY_LEN - 1)], b, i); + for (i = 0; i < SPECK_ROUNDS16 - 1; i++) { + R16(a[i % (SPECK_KEY_LEN16 - 1)], b, i); S[i + 1] = b; } } -void speck_encrypt(SPECK_TYPE const pt[ 2], SPECK_TYPE ct[ 2], SPECK_TYPE const K[ SPECK_ROUNDS]) +void speck_set_decrypt_key16(SPECK_TYPE16 const K[SPECK_KEY_LEN16], SPECK_TYPE16 S[SPECK_ROUNDS16]) { - SPECK_TYPE i; + SPECK_TYPE16 i, b = K[0]; + SPECK_TYPE16 a[SPECK_KEY_LEN16 - 1]; + for (i = 0; i < (SPECK_KEY_LEN16 - 1); i++) + { + a[i] = K[i + 1]; + } + S[0] = b; + for (i = 0; i < SPECK_ROUNDS16 - 1; i++) { + R16(a[i % (SPECK_KEY_LEN16 - 1)], b, i); + S[i + 1] = b; + } +} +void speck_encrypt16(SPECK_TYPE16 const pt[2], SPECK_TYPE16 ct[2], SPECK_TYPE16 const K[SPECK_ROUNDS16]) +{ + SPECK_TYPE16 i; ct[0] = pt[0]; ct[1] = pt[1]; - for (i = 0; i < SPECK_ROUNDS; i++){ - R(ct[1], ct[0], K[i]); + for (i = 0; i < SPECK_ROUNDS16; i++){ + R16(ct[1], ct[0], K[i]); } } -void speck_decrypt(SPECK_TYPE const ct[ 2], SPECK_TYPE pt[ 2], SPECK_TYPE const K[ SPECK_ROUNDS]) +void speck_decrypt16(SPECK_TYPE16 const ct[2], SPECK_TYPE16 pt[2], SPECK_TYPE16 const K[SPECK_ROUNDS16]) { - SPECK_TYPE i; + SPECK_TYPE16 i; pt[0] = ct[0]; pt[1] = ct[1]; - for (i = 0; i < SPECK_ROUNDS; i++){ - RR(pt[1], pt[0], K[(SPECK_ROUNDS - 1) - i]); + for (i = 0; i < SPECK_ROUNDS16; i++){ + RR16(pt[1], pt[0], K[(SPECK_ROUNDS16 - 1) - i]); + } +} + + + +void speck_set_encrypt_key32(SPECK_TYPE32 const K[SPECK_KEY_LEN32], SPECK_TYPE32 S[SPECK_ROUNDS32]) +{ + SPECK_TYPE32 i, b = K[0]; + SPECK_TYPE32 a[SPECK_KEY_LEN32 - 1]; + for (i = 0; i < (SPECK_KEY_LEN32 - 1); i++) + { + a[i] = K[i + 1]; + } + S[0] = b; + for (i = 0; i < SPECK_ROUNDS32 - 1; i++) { + R32(a[i % (SPECK_KEY_LEN32 - 1)], b, i); + S[i + 1] = b; + } +} +void speck_set_decrypt_key32(SPECK_TYPE32 const K[SPECK_KEY_LEN32], SPECK_TYPE32 S[SPECK_ROUNDS32]) +{ + SPECK_TYPE32 i, b = K[0]; + SPECK_TYPE32 a[SPECK_KEY_LEN32 - 1]; + for (i = 0; i < (SPECK_KEY_LEN32 - 1); i++) + { + a[i] = K[i + 1]; + } + S[0] = b; + for (i = 0; i < SPECK_ROUNDS32 - 1; i++) { + R32(a[i % (SPECK_KEY_LEN32 - 1)], b, i); + S[i + 1] = b; + } +} +void speck_encrypt32(SPECK_TYPE32 const pt[2], SPECK_TYPE32 ct[2], SPECK_TYPE32 const K[SPECK_ROUNDS32]) +{ + SPECK_TYPE32 i; + ct[0] = pt[0]; ct[1] = pt[1]; + for (i = 0; i < SPECK_ROUNDS32; i++){ + R32(ct[1], ct[0], K[i]); + } +} + +void speck_decrypt32(SPECK_TYPE32 const ct[2], SPECK_TYPE32 pt[2], SPECK_TYPE32 const K[SPECK_ROUNDS32]) +{ + SPECK_TYPE32 i; + pt[0] = ct[0]; pt[1] = ct[1]; + + for (i = 0; i < SPECK_ROUNDS32; i++){ + RR32(pt[1], pt[0], K[(SPECK_ROUNDS32 - 1) - i]); + } +} + + +void speck_set_encrypt_key64(SPECK_TYPE64 const K[SPECK_KEY_LEN64], SPECK_TYPE64 S[SPECK_ROUNDS64]) +{ + SPECK_TYPE64 i, b = K[0]; + SPECK_TYPE64 a[SPECK_KEY_LEN64 - 1]; + for (i = 0; i < (SPECK_KEY_LEN64 - 1); i++) + { + a[i] = K[i + 1]; + } + S[0] = b; + for (i = 0; i < SPECK_ROUNDS64 - 1; i++) { + R64(a[i % (SPECK_KEY_LEN64 - 1)], b, i); + S[i + 1] = b; + } +} +void speck_set_decrypt_key64(SPECK_TYPE64 const K[SPECK_KEY_LEN64], SPECK_TYPE64 S[SPECK_ROUNDS64]) +{ + SPECK_TYPE64 i, b = K[0]; + SPECK_TYPE64 a[SPECK_KEY_LEN64 - 1]; + for (i = 0; i < (SPECK_KEY_LEN64 - 1); i++) + { + a[i] = K[i + 1]; + } + S[0] = b; + for (i = 0; i < SPECK_ROUNDS64 - 1; i++) { + R64(a[i % (SPECK_KEY_LEN64 - 1)], b, i); + S[i + 1] = b; + } +} +void speck_encrypt64(SPECK_TYPE64 const pt[2], SPECK_TYPE64 ct[2], SPECK_TYPE64 const K[SPECK_ROUNDS64]) +{ + SPECK_TYPE64 i; + ct[0] = pt[0]; ct[1] = pt[1]; + for (i = 0; i < SPECK_ROUNDS64; i++){ + R64(ct[1], ct[0], K[i]); + } +} + +void speck_decrypt64(SPECK_TYPE64 const ct[2], SPECK_TYPE64 pt[2], SPECK_TYPE64 const K[SPECK_ROUNDS64]) +{ + SPECK_TYPE64 i; + pt[0] = ct[0]; pt[1] = ct[1]; + + for (i = 0; i < SPECK_ROUNDS64; i++){ + RR64(pt[1], pt[0], K[(SPECK_ROUNDS64 - 1) - i]); } } diff --git a/include/openssl/speck.h b/include/openssl/speck.h index a41d73c6..48d75bf0 100644 --- a/include/openssl/speck.h +++ b/include/openssl/speck.h @@ -49,31 +49,38 @@ #ifndef SPECK_H #define SPECK_H -/* -* define speck type to use -*(one of SPECK_32_64, SPECK_64_128, SPECK_128_256) -*/ -#define SPECK_32_64 +#define SPECK_TYPE16 uint16_t +#define SPECK_ROUNDS16 22 +#define SPECK_KEY_LEN16 4 -#ifdef SPECK_32_64 -#define SPECK_TYPE uint16_t -#define SPECK_ROUNDS 22 -#define SPECK_KEY_LEN 4 -#endif -#ifdef SPECK_64_128 -#define SPECK_TYPE uint32_t -#define SPECK_ROUNDS 27 -#define SPECK_KEY_LEN 4 -#endif +#define SPECK_TYPE32 uint32_t +#define SPECK_ROUNDS32 27 +#define SPECK_KEY_LEN32 4 -#ifdef SPECK_128_256 -#define SPECK_TYPE uint64_t -#define SPECK_ROUNDS 34 -#define SPECK_KEY_LEN 4 -#endif +#define SPECK_TYPE64 uint64_t +#define SPECK_ROUNDS64 34 +#define SPECK_KEY_LEN64 4 + +#define ROR16(x, r) ((x >> r) | (x << ((sizeof(SPECK_TYPE16) * 8) - r)))//循环右移 +#define ROL16(x, r) ((x << r) | (x >> ((sizeof(SPECK_TYPE16) * 8) - r)))//循环左移 + +#define ROR32(x, r) ((x >> r) | (x << ((sizeof(SPECK_TYPE32) * 8) - r)))//循环右移 +#define ROL32(x, r) ((x << r) | (x >> ((sizeof(SPECK_TYPE32) * 8) - r)))//循环左移 + +#define ROR64(x, r) ((x >> r) | (x << ((sizeof(SPECK_TYPE64) * 8) - r)))//循环右移 +#define ROL64(x, r) ((x << r) | (x >> ((sizeof(SPECK_TYPE64) * 8) - r)))//循环左移 + + +#define R16(x, y, k) (x = ROR16(x, 7), x += y, x ^= k, y = ROL16(y, 2), y ^= x) +#define RR16(x, y, k) (y ^= x, y = ROR16(y, 2), x ^= k, x -= y, x = ROL16(x, 7)) + +#define R32(x, y, k) (x = ROR32(x, 8), x += y, x ^= k, y = ROL32(y, 3), y ^= x) +#define RR32(x, y, k) (y ^= x, y = ROR32(y, 3), x ^= k, x -= y, x = ROL32(x, 8)) + +#define R64(x, y, k) (x = ROR64(x, 8), x += y, x ^= k, y = ROL64(y, 3), y ^= x) +#define RR64(x, y, k) (y ^= x, y = ROR64(y, 3), x ^= k, x -= y, x = ROL64(x, 8)) -#define num_word sizeof(SPECK_TYPE) #include #include #include @@ -81,14 +88,23 @@ extern "C" { #endif - typedef struct { - unsigned char rk[num_word]; - } speck_key_t; + void speck_set_encrypt_key16(SPECK_TYPE16 const user[SPECK_KEY_LEN16], SPECK_TYPE16 key[SPECK_ROUNDS16]); + void speck_set_decrypt_key16(SPECK_TYPE16 const user[SPECK_KEY_LEN16], SPECK_TYPE16 key[SPECK_ROUNDS16]); + void speck_expand16(SPECK_TYPE16 const K[SPECK_KEY_LEN16], SPECK_TYPE16 S[SPECK_ROUNDS16]); + void speck_encrypt16(SPECK_TYPE16 const pt[2], SPECK_TYPE16 ct[2], SPECK_TYPE16 const K[SPECK_ROUNDS16]); + void speck_decrypt16(SPECK_TYPE16 const ct[2], SPECK_TYPE16 pt[2], SPECK_TYPE16 const K[SPECK_ROUNDS16]); - void speck_set_encrypt_key(speck_key_t *key, const unsigned char *user_key); - void speck_expand(SPECK_TYPE const K[SPECK_KEY_LEN], SPECK_TYPE S[SPECK_ROUNDS]); - void speck_encrypt(SPECK_TYPE const pt[2], SPECK_TYPE ct[2], SPECK_TYPE const K[SPECK_ROUNDS]); - void speck_decrypt(SPECK_TYPE const ct[2], SPECK_TYPE pt[2], SPECK_TYPE const K[SPECK_ROUNDS]); + void speck_set_encrypt_key32(SPECK_TYPE32 const user[SPECK_KEY_LEN32], SPECK_TYPE32 key[SPECK_ROUNDS32]); + void speck_set_decrypt_key32(SPECK_TYPE32 const user[SPECK_KEY_LEN32], SPECK_TYPE32 key[SPECK_ROUNDS32]); + void speck_expand32(SPECK_TYPE32 const K[SPECK_KEY_LEN32], SPECK_TYPE32 S[SPECK_ROUNDS32]); + void speck_encrypt32(SPECK_TYPE32 const pt[2], SPECK_TYPE32 ct[2], SPECK_TYPE32 const K[SPECK_ROUNDS32]); + void speck_decrypt32(SPECK_TYPE32 const ct[2], SPECK_TYPE32 pt[2], SPECK_TYPE32 const K[SPECK_ROUNDS32]); + + void speck_set_encrypt_key64(SPECK_TYPE64 const user[SPECK_KEY_LEN64], SPECK_TYPE64 key[SPECK_ROUNDS64]); + void speck_set_decrypt_key64(SPECK_TYPE64 const user[SPECK_KEY_LEN64], SPECK_TYPE64 key[SPECK_ROUNDS64]); + void speck_expand64(SPECK_TYPE64 const K[SPECK_KEY_LEN64], SPECK_TYPE64 S[SPECK_ROUNDS64]); + void speck_encrypt64(SPECK_TYPE64 const pt[2], SPECK_TYPE64 ct[2], SPECK_TYPE64 const K[SPECK_ROUNDS64]); + void speck_decrypt64(SPECK_TYPE64 const ct[2], SPECK_TYPE64 pt[2], SPECK_TYPE64 const K[SPECK_ROUNDS64]); #ifdef __cplusplus } diff --git a/test/specktest.c b/test/specktest.c index 0f914ab0..17e14ac6 100644 --- a/test/specktest.c +++ b/test/specktest.c @@ -1,99 +1,68 @@ -/* ==================================================================== - * Copyright (c) 2014 - 2017 The GmSSL Project. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * - * 3. All advertising materials mentioning features or use of this - * software must display the following acknowledgment: - * "This product includes software developed by the GmSSL Project. - * (http://gmssl.org/)" - * - * 4. The name "GmSSL Project" must not be used to endorse or promote - * products derived from this software without prior written - * permission. For written permission, please contact - * guanzhi1980@gmail.com. - * - * 5. Products derived from this software may not be called "GmSSL" - * nor may "GmSSL" appear in their names without prior written - * permission of the GmSSL Project. - * - * 6. Redistributions of any form whatsoever must retain the following - * acknowledgment: - * "This product includes software developed by the GmSSL Project - * (http://gmssl.org/)" - * - * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY - * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR - * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - * ==================================================================== - */ - - #include #include #include - -#include "../e_os.h" - -#ifdef OPENSSL_NO_SPECK -int main(int argc, char **argv) -{ - printf("No Speck support\n"); - return 0; -} -#else - #include int main(int argc, char** argv) { int sum = 0; -#ifdef SPECK_32_64 - uint16_t key[4] = { 0x0100, 0x0908, 0x1110, 0x1918 }; - uint16_t plain[2] = { 0x694c, 0x6574 }; - uint16_t enc[2] = { 0x42f2, 0xa868 }; -#endif + uint16_t key16[4] = { 0x0100, 0x0908, 0x1110, 0x1918 }; + uint16_t plain16[2] = { 0x694c, 0x6574 }; + uint16_t enc16[2] = { 0x42f2, 0xa868 }; -#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_128_256 - uint64_t key[4] = { 0x0706050403020100, 0x0f0e0d0c0b0a0908, 0x1716151413121110, 0x1f1e1d1c1b1a1918 }; - uint64_t plain[2] = { 0x202e72656e6f6f70, 0x65736f6874206e49 }; - uint64_t enc[2] = { 0x4eeeb48d9c188f43, 0x4109010405c0f53e }; -#endif - SPECK_TYPE buffer[2] = { 0 }; - SPECK_TYPE exp[SPECK_ROUNDS]; - speck_set_encrypt_key(key, exp); - speck_encrypt(plain, buffer, exp); - speck_decrypt(enc, buffer, exp); - if (memcmp(buffer, plain, sizeof(enc))) + uint32_t key32[4] = { 0x03020100, 0x0b0a0908, 0x13121110, 0x1b1a1918 }; + uint32_t plain32[2] = { 0x7475432d, 0x3b726574 }; + uint32_t enc32[2] = { 0x454e028b, 0x8c6fa548 }; + + + + uint64_t key64[4] = { 0x0706050403020100, 0x0f0e0d0c0b0a0908, 0x1716151413121110, 0x1f1e1d1c1b1a1918 }; + uint64_t plain64[2] = { 0x202e72656e6f6f70, 0x65736f6874206e49 }; + uint64_t enc64[2] = { 0x4eeeb48d9c188f43, 0x4109010405c0f53e }; + + SPECK_TYPE16 buffer[2] = { 0 }; + SPECK_TYPE16 exp[SPECK_ROUNDS16]; + speck_set_encrypt_key16(key16, exp); + speck_encrypt16(plain16, buffer, exp); + if (memcmp(buffer, enc16, sizeof(enc16))) { sum++; } - + speck_decrypt16(enc16, buffer, exp); + if (memcmp(buffer, plain16, sizeof(enc16))) + { + sum++; + } + + SPECK_TYPE32 exp32[SPECK_ROUNDS32]; + SPECK_TYPE32 buffer32[2] = { 0 }; + speck_set_encrypt_key32(key32, exp32); + speck_encrypt32(plain32, buffer32, exp32); + if (memcmp(buffer, enc32, sizeof(enc32))) + { + sum++; + } + speck_decrypt32(enc32, buffer32, exp32); + if (memcmp(buffer32, plain32, sizeof(enc32))) + { + sum++; + } + + SPECK_TYPE64 exp64[SPECK_ROUNDS64]; + SPECK_TYPE64 buffer64[2] = { 0 }; + speck_set_encrypt_key64(key64, exp64); + speck_encrypt64(plain64, buffer64, exp64); + if (memcmp(buffer64, enc64, sizeof(enc64))) + { + sum++; + system("pause"); + } + speck_decrypt64(enc64, buffer64, exp64); + if (memcmp(buffer64, plain64, sizeof(enc64))) + { + sum++; + system("pause"); + } return sum; } -#endif