From 410071311d2fba966660aec0df908ad5bb4d4a5d Mon Sep 17 00:00:00 2001 From: "papizh.konstantin" <papizh.konstantin@demlabs.net> Date: Sat, 31 Oct 2020 11:17:47 +0000 Subject: [PATCH] ... cherry-pick SimpleFIPS202 --- dap-sdk/crypto/include/dap_sign.h | 8 +- .../lib/high/Keccak/FIPS202/SimpleFIPS202.h | 2 +- .../low/KeccakP-200/Compact/KeccakP-200-SnP.h | 34 --- .../KeccakP-200/Compact/KeccakP-200-compact.c | 185 -------------- .../low/KeccakP-800/Compact/KeccakP-800-SnP.h | 35 --- .../KeccakP-800/Compact/KeccakP-800-compact.c | 241 ------------------ dap-sdk/crypto/src/dap_cert.c | 4 +- dap-sdk/crypto/src/dap_enc_key.c | 2 +- 8 files changed, 8 insertions(+), 503 deletions(-) delete mode 100755 dap-sdk/crypto/src/XKCP/lib/low/KeccakP-200/Compact/KeccakP-200-SnP.h delete mode 100755 dap-sdk/crypto/src/XKCP/lib/low/KeccakP-200/Compact/KeccakP-200-compact.c delete mode 100755 dap-sdk/crypto/src/XKCP/lib/low/KeccakP-800/Compact/KeccakP-800-SnP.h delete mode 100755 dap-sdk/crypto/src/XKCP/lib/low/KeccakP-800/Compact/KeccakP-800-compact.c diff --git a/dap-sdk/crypto/include/dap_sign.h b/dap-sdk/crypto/include/dap_sign.h index 9aa8084154..9182a7e10b 100755 --- a/dap-sdk/crypto/include/dap_sign.h +++ b/dap-sdk/crypto/include/dap_sign.h @@ -43,13 +43,13 @@ typedef enum { } dap_sign_type_enum_t; typedef union dap_sign_type { - dap_sign_type_enum_t type: 16; - uint16_t raw; -} dap_sign_type_t; + dap_sign_type_enum_t type: 32; + uint32_t raw; +} DAP_ALIGN_PACKED dap_sign_type_t; typedef struct dap_sign_hdr { dap_sign_type_t type; /// Signature type - uint8_t padding[2]; /// Padding for better aligmnent + uint16_t padding; uint32_t sign_size; /// Signature size uint32_t sign_pkey_size; /// Signature serialized public key size } DAP_ALIGN_PACKED dap_sign_hdr_t; diff --git a/dap-sdk/crypto/src/XKCP/lib/high/Keccak/FIPS202/SimpleFIPS202.h b/dap-sdk/crypto/src/XKCP/lib/high/Keccak/FIPS202/SimpleFIPS202.h index 1713a46e71..d1b7ef3920 100644 --- a/dap-sdk/crypto/src/XKCP/lib/high/Keccak/FIPS202/SimpleFIPS202.h +++ b/dap-sdk/crypto/src/XKCP/lib/high/Keccak/FIPS202/SimpleFIPS202.h @@ -84,4 +84,4 @@ int SHA3_512(unsigned char *output, const unsigned char *input, size_t inputByte } #endif -#endif +#endif \ No newline at end of file diff --git a/dap-sdk/crypto/src/XKCP/lib/low/KeccakP-200/Compact/KeccakP-200-SnP.h b/dap-sdk/crypto/src/XKCP/lib/low/KeccakP-200/Compact/KeccakP-200-SnP.h deleted file mode 100755 index 1ad16097cd..0000000000 --- a/dap-sdk/crypto/src/XKCP/lib/low/KeccakP-200/Compact/KeccakP-200-SnP.h +++ /dev/null @@ -1,34 +0,0 @@ -/* -Implementation by Ronny Van Keer, hereby denoted as "the implementer". - -For more information, feedback or questions, please refer to our website: -https://keccak.team/ - -To the extent possible under law, the implementer has waived all copyright -and related or neighboring rights to the source code in this file. -http://creativecommons.org/publicdomain/zero/1.0/ - ---- - -Please refer to SnP-documentation.h for more details. -*/ - -#ifndef _KeccakP_200_SnP_h_ -#define _KeccakP_200_SnP_h_ - -#define KeccakP200_implementation "8-bit compact implementation" -#define KeccakP200_stateSizeInBytes 25 -#define KeccakP200_stateAlignment 1 - -#define KeccakP200_StaticInitialize() -void KeccakP200_Initialize(void *state); -void KeccakP200_AddByte(void *state, unsigned char data, unsigned int offset); -void KeccakP200_AddBytes(void *state, const unsigned char *data, unsigned int offset, unsigned int length); -void KeccakP200_OverwriteBytes(void *state, const unsigned char *data, unsigned int offset, unsigned int length); -void KeccakP200_OverwriteWithZeroes(void *state, unsigned int byteCount); -void KeccakP200_Permute_Nrounds(void *state, unsigned int nrounds); -void KeccakP200_Permute_18rounds(void *state); -void KeccakP200_ExtractBytes(const void *state, unsigned char *data, unsigned int offset, unsigned int length); -void KeccakP200_ExtractAndAddBytes(const void *state, const unsigned char *input, unsigned char *output, unsigned int offset, unsigned int length); - -#endif diff --git a/dap-sdk/crypto/src/XKCP/lib/low/KeccakP-200/Compact/KeccakP-200-compact.c b/dap-sdk/crypto/src/XKCP/lib/low/KeccakP-200/Compact/KeccakP-200-compact.c deleted file mode 100755 index ea47b55257..0000000000 --- a/dap-sdk/crypto/src/XKCP/lib/low/KeccakP-200/Compact/KeccakP-200-compact.c +++ /dev/null @@ -1,185 +0,0 @@ -/* -Implementation by Ronny Van Keer, hereby denoted as "the implementer". - -For more information, feedback or questions, please refer to our website: -https://keccak.team/ - -To the extent possible under law, the implementer has waived all copyright -and related or neighboring rights to the source code in this file. -http://creativecommons.org/publicdomain/zero/1.0/ - ---- - -This file implements Keccak-p[200] in a SnP-compatible way. -Please refer to SnP-documentation.h for more details. - -This implementation comes with KeccakP-200-SnP.h in the same folder. -Please refer to LowLevel.build for the exact list of other files it must be combined with. -*/ - -#include <string.h> -#include <stdlib.h> -#include "KeccakP-200-SnP.h" - -/* #define DIVISION_INSTRUCTION /* comment if no division instruction or more compact when not using division */ -#define UNROLL_CHILOOP /* comment if more compact using for loop */ - -typedef unsigned char UINT8; -typedef unsigned int tSmallUInt; /*INFO It could be more optimized to use "unsigned char" on an 8-bit CPU */ -typedef UINT8 tKeccakLane; - -#define ROL8(a, offset) (UINT8)((((UINT8)a) << (offset&7)) ^ (((UINT8)a) >> (8-(offset&7)))) - -const UINT8 KeccakP200_RotationConstants[25] = -{ - 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14, 27, 41, 56, 8, 25, 43, 62, 18, 39, 61, 20, 44 -}; - -const UINT8 KeccakP200_PiLane[25] = -{ - 10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4, 15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1 -}; - -#if defined(DIVISION_INSTRUCTION) -#define MOD5(argValue) ((argValue) % 5) -#else -const UINT8 KeccakP200_Mod5[10] = -{ - 0, 1, 2, 3, 4, 0, 1, 2, 3, 4 -}; -#define MOD5(argValue) KeccakP200_Mod5[argValue] -#endif - -const UINT8 KeccakF200_RoundConstants[] = -{ - 0x01, 0x82, 0x8a, 0x00, 0x8b, 0x01, 0x81, 0x09, 0x8a, 0x88, 0x09, 0x0a, 0x8b, 0x8b, 0x89, 0x03, 0x02, 0x80 -}; - -/* ---------------------------------------------------------------- */ - -void KeccakP200_Initialize(void *argState) -{ - memset( argState, 0, 25 * sizeof(tKeccakLane) ); -} - -/* ---------------------------------------------------------------- */ - -void KeccakP200_AddByte(void *argState, unsigned char byte, unsigned int offset) -{ - ((tKeccakLane*)argState)[offset] ^= byte; -} - -/* ---------------------------------------------------------------- */ - -void KeccakP200_AddBytes(void *argState, const unsigned char *data, unsigned int offset, unsigned int length) -{ - tSmallUInt i; - tKeccakLane * state = (tKeccakLane*)argState + offset; - for(i=0; i<length; i++) - state[i] ^= data[i]; -} - -/* ---------------------------------------------------------------- */ - -void KeccakP200_OverwriteBytes(void *state, const unsigned char *data, unsigned int offset, unsigned int length) -{ - memcpy((unsigned char*)state+offset, data, length); -} - -/* ---------------------------------------------------------------- */ - -void KeccakP200_OverwriteWithZeroes(void *state, unsigned int byteCount) -{ - memset(state, 0, byteCount); -} - -/* ---------------------------------------------------------------- */ - -void KeccakP200_Permute_Nrounds(void *argState, unsigned int nr) -{ - tSmallUInt x, y; - tKeccakLane temp; - tKeccakLane BC[5]; - tKeccakLane *state; - const tKeccakLane *rc; - - state = (tKeccakLane*)argState; - rc = KeccakF200_RoundConstants + 18 - nr; - do - { - /* Theta */ - for ( x = 0; x < 5; ++x ) - { - BC[x] = state[x] ^ state[5 + x] ^ state[10 + x] ^ state[15 + x] ^ state[20 + x]; - } - for ( x = 0; x < 5; ++x ) - { - temp = BC[MOD5(x+4)] ^ ROL8(BC[MOD5(x+1)], 1); - for ( y = 0; y < 25; y += 5 ) - { - state[y + x] ^= temp; - } - } - - /* Rho Pi */ - temp = state[1]; - for ( x = 0; x < 24; ++x ) - { - BC[0] = state[KeccakP200_PiLane[x]]; - state[KeccakP200_PiLane[x]] = ROL8( temp, KeccakP200_RotationConstants[x] ); - temp = BC[0]; - } - - /* Chi */ - for ( y = 0; y < 25; y += 5 ) - { -#if defined(UNROLL_CHILOOP) - BC[0] = state[y + 0]; - BC[1] = state[y + 1]; - BC[2] = state[y + 2]; - BC[3] = state[y + 3]; - BC[4] = state[y + 4]; -#else - for ( x = 0; x < 5; ++x ) - { - BC[x] = state[y + x]; - } -#endif - for ( x = 0; x < 5; ++x ) - { - state[y + x] = BC[x] ^((~BC[MOD5(x+1)]) & BC[MOD5(x+2)]); - } - } - - /* Iota */ - temp = *(rc++); - state[0] ^= temp; - } - while( temp != 0x80 ); -} - -/* ---------------------------------------------------------------- */ - -void KeccakP200_Permute_18rounds(void *argState) -{ - KeccakP200_Permute_Nrounds(argState, 18); -} - -/* ---------------------------------------------------------------- */ - -void KeccakP200_ExtractBytes(const void *state, unsigned char *data, unsigned int offset, unsigned int length) -{ - memcpy(data, (UINT8*)state+offset, length); -} - -/* ---------------------------------------------------------------- */ - -void KeccakP200_ExtractAndAddBytes(const void *argState, const unsigned char *input, unsigned char *output, unsigned int offset, unsigned int length) -{ - unsigned int i; - tKeccakLane * state = (tKeccakLane*)argState + offset; - for(i=0; i<length; i++) - output[i] = input[i] ^ state[i]; -} - -/* ---------------------------------------------------------------- */ diff --git a/dap-sdk/crypto/src/XKCP/lib/low/KeccakP-800/Compact/KeccakP-800-SnP.h b/dap-sdk/crypto/src/XKCP/lib/low/KeccakP-800/Compact/KeccakP-800-SnP.h deleted file mode 100755 index 599a8b94ca..0000000000 --- a/dap-sdk/crypto/src/XKCP/lib/low/KeccakP-800/Compact/KeccakP-800-SnP.h +++ /dev/null @@ -1,35 +0,0 @@ -/* -Implementation by Ronny Van Keer, hereby denoted as "the implementer". - -For more information, feedback or questions, please refer to our website: -https://keccak.team/ - -To the extent possible under law, the implementer has waived all copyright -and related or neighboring rights to the source code in this file. -http://creativecommons.org/publicdomain/zero/1.0/ - ---- - -Please refer to SnP-documentation.h for more details. -*/ - -#ifndef _KeccakP_800_SnP_h_ -#define _KeccakP_800_SnP_h_ - -#define KeccakP800_implementation "32-bit compact implementation" -#define KeccakP800_stateSizeInBytes 100 -#define KeccakP800_stateAlignment 4 - -#define KeccakP800_StaticInitialize() -void KeccakP800_Initialize(void *state); -void KeccakP800_AddByte(void *state, unsigned char data, unsigned int offset); -void KeccakP800_AddBytes(void *state, const unsigned char *data, unsigned int offset, unsigned int length); -void KeccakP800_OverwriteBytes(void *state, const unsigned char *data, unsigned int offset, unsigned int length); -void KeccakP800_OverwriteWithZeroes(void *state, unsigned int byteCount); -void KeccakP800_Permute_Nrounds(void *state, unsigned int nrounds); -void KeccakP800_Permute_12rounds(void *state); -void KeccakP800_Permute_22rounds(void *state); -void KeccakP800_ExtractBytes(const void *state, unsigned char *data, unsigned int offset, unsigned int length); -void KeccakP800_ExtractAndAddBytes(const void *state, const unsigned char *input, unsigned char *output, unsigned int offset, unsigned int length); - -#endif diff --git a/dap-sdk/crypto/src/XKCP/lib/low/KeccakP-800/Compact/KeccakP-800-compact.c b/dap-sdk/crypto/src/XKCP/lib/low/KeccakP-800/Compact/KeccakP-800-compact.c deleted file mode 100755 index 0d9bfa3aac..0000000000 --- a/dap-sdk/crypto/src/XKCP/lib/low/KeccakP-800/Compact/KeccakP-800-compact.c +++ /dev/null @@ -1,241 +0,0 @@ -/* -Implementation by Ronny Van Keer, hereby denoted as "the implementer". - -For more information, feedback or questions, please refer to our website: -https://keccak.team/ - -To the extent possible under law, the implementer has waived all copyright -and related or neighboring rights to the source code in this file. -http://creativecommons.org/publicdomain/zero/1.0/ - ---- - -This file implements Keccak-p[800] in a SnP-compatible way. -Please refer to SnP-documentation.h for more details. - -This implementation comes with KeccakP-800-SnP.h in the same folder. -Please refer to LowLevel.build for the exact list of other files it must be combined with. -*/ - -#include <stdint.h> -#include <string.h> -#include <stdlib.h> -#include "brg_endian.h" -#include "KeccakP-800-SnP.h" - -#if (PLATFORM_BYTE_ORDER != IS_LITTLE_ENDIAN) -#error Not yet implemented -#endif - -#define USE_MEMSET -/* #define DIVISION_INSTRUCTION /* comment if no division instruction or more compact when not using division */ -#define UNROLL_CHILOOP /* comment more compact using for loop */ - -typedef unsigned char UINT8; -typedef uint32_t UINT32; -typedef unsigned int tSmallUInt; /*INFO It could be more optimized to use "unsigned char" on an 8-bit CPU */ -typedef UINT32 tKeccakLane; - -#if defined (__arm__) && !defined(__GNUC__) -#define ROL32(a, offset) __ror(a, 32-(offset)) -#elif defined(_MSC_VER) -#define ROL32(a, offset) _rotl(a, offset) -#else -#define ROL32(a, offset) ((((UINT32)a) << offset) ^ (((UINT32)a) >> (32-offset))) -#endif - -#define cKeccakNumberOfRounds 22 - -const UINT8 KeccakP800_RotationConstants[25] = -{ - 1, 3, 6, 10, 15, 21, 28, 4, 13, 23, 2, 14, 27, 9, 24, 8, 25, 11, 30, 18, 7, 29, 20, 12 -}; - -const UINT8 KeccakP800_PiLane[25] = -{ - 10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4, 15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1 -}; - -#if defined(DIVISION_INSTRUCTION) -#define MOD5(argValue) ((argValue) % 5) -#else -const UINT8 KeccakP800_Mod5[10] = -{ - 0, 1, 2, 3, 4, 0, 1, 2, 3, 4 -}; -#define MOD5(argValue) KeccakP800_Mod5[argValue] -#endif - -/* ---------------------------------------------------------------- */ - -void KeccakP800_Initialize(void *argState) -{ - #if defined(USE_MEMSET) - memset( argState, 0, 25 * sizeof(tKeccakLane) ); - #else - tSmallUInt i; - tKeccakLane *state; - - state = (tKeccakLane*)argState; - i = 25; - do - { - *(state++) = 0; - } - while ( --i != 0 ); - #endif -} - -/* ---------------------------------------------------------------- */ - -void KeccakP800_AddByte(void *state, unsigned char data, unsigned int offset) -{ - ((unsigned char *)state)[offset] ^= data; -} - -/* ---------------------------------------------------------------- */ - -void KeccakP800_AddBytes(void *argState, const unsigned char *data, unsigned int offset, unsigned int length) -{ - tSmallUInt i; - unsigned char * state = (unsigned char*)argState + offset; - - for(i=0; i<length; i++) - ((unsigned char *)state)[i] ^= data[i]; -} - -/* ---------------------------------------------------------------- */ - -void KeccakP800_OverwriteBytes(void *state, const unsigned char *data, unsigned int offset, unsigned int length) -{ - memcpy((unsigned char*)state+offset, data, length); -} - -/* ---------------------------------------------------------------- */ - -void KeccakP800_OverwriteWithZeroes(void *state, unsigned int byteCount) -{ - memset(state, 0, byteCount); -} - -/* ---------------------------------------------------------------- */ - -void KeccakP800_Permute_Nrounds(void *argState, unsigned int nr) -{ - tSmallUInt x, y; - tKeccakLane temp; - tKeccakLane BC[5]; - tKeccakLane *state; - UINT8 LFSRstate; - - state = (tKeccakLane*)argState; - LFSRstate = 0x01; - for ( y = (tSmallUInt)(cKeccakNumberOfRounds - nr); y != 0; --y ) - { - for( x = 1; x < 128; x <<= 1 ) - { - if ((LFSRstate & 0x80) != 0) - /* Primitive polynomial over GF(2): x^8+x^6+x^5+x^4+1 */ - LFSRstate = (LFSRstate << 1) ^ 0x71; - else - LFSRstate <<= 1; - } - } - - do - { - /* Theta */ - for ( x = 0; x < 5; ++x ) - { - BC[x] = state[x] ^ state[5 + x] ^ state[10 + x] ^ state[15 + x] ^ state[20 + x]; - } - for ( x = 0; x < 5; ++x ) - { - temp = BC[MOD5(x+4)] ^ ROL32(BC[MOD5(x+1)], 1); - for ( y = 0; y < 25; y += 5 ) - { - state[y + x] ^= temp; - } - } - - /* Rho Pi */ - temp = state[1]; - for ( x = 0; x < 24; ++x ) - { - BC[0] = state[KeccakP800_PiLane[x]]; - state[KeccakP800_PiLane[x]] = ROL32( temp, KeccakP800_RotationConstants[x] ); - temp = BC[0]; - } - - /* Chi */ - for ( y = 0; y < 25; y += 5 ) - { -#if defined(UNROLL_CHILOOP) - BC[0] = state[y + 0]; - BC[1] = state[y + 1]; - BC[2] = state[y + 2]; - BC[3] = state[y + 3]; - BC[4] = state[y + 4]; -#else - for ( x = 0; x < 5; ++x ) - { - BC[x] = state[y + x]; - } -#endif - for ( x = 0; x < 5; ++x ) - { - state[y + x] = BC[x] ^((~BC[MOD5(x+1)]) & BC[MOD5(x+2)]); - } - } - - /* Iota */ - temp = 0; - for( x = 1; x < 128; x <<= 1 ) - { - if ( x <= (sizeof(tKeccakLane)*8) ) - temp ^= (tKeccakLane)(LFSRstate & 1) << (x - 1); - - if ((LFSRstate & 0x80) != 0) - /* Primitive polynomial over GF(2): x^8+x^6+x^5+x^4+1 */ - LFSRstate = (LFSRstate << 1) ^ 0x71; - else - LFSRstate <<= 1; - } - state[0] ^= temp; - } - while( --nr != 0 ); -} - -/* ---------------------------------------------------------------- */ - -void KeccakP800_Permute_12rounds(void *argState) -{ - KeccakP800_Permute_Nrounds(argState, 12); -} - -/* ---------------------------------------------------------------- */ - -void KeccakP800_Permute_22rounds(void *argState) -{ - KeccakP800_Permute_Nrounds(argState, 22); -} - -/* ---------------------------------------------------------------- */ - -void KeccakP800_ExtractBytes(const void *state, unsigned char *data, unsigned int offset, unsigned int length) -{ - memcpy(data, (const UINT8*)state+offset, length); -} - -/* ---------------------------------------------------------------- */ - -void KeccakP800_ExtractAndAddBytes(const void *argState, const unsigned char *input, unsigned char *output, unsigned int offset, unsigned int length) -{ - tSmallUInt i; - const unsigned char * state = (const unsigned char*)argState + offset; - - for(i=0; i<length; i++) - output[i] = input[i] ^ state[i]; -} - -/* ---------------------------------------------------------------- */ diff --git a/dap-sdk/crypto/src/dap_cert.c b/dap-sdk/crypto/src/dap_cert.c index b9bc426886..67ce81d47a 100755 --- a/dap-sdk/crypto/src/dap_cert.c +++ b/dap-sdk/crypto/src/dap_cert.c @@ -169,8 +169,8 @@ size_t dap_cert_sign_output_size(dap_cert_t * a_cert, size_t a_size_wished) dap_sign_t * dap_cert_sign(dap_cert_t * a_cert, const void * a_data , size_t a_data_size, size_t a_output_size_wished ) { - dap_enc_key_t * l_key = a_cert->enc_key; - dap_sign_t *l_ret = dap_sign_create(l_key, a_data, a_data_size, a_output_size_wished); + dap_sign_t *l_ret = dap_sign_create(a_cert->enc_key, a_data, a_data_size, a_output_size_wished); + log_it(L_INFO, "Sign sizes: %d %d", l_ret->header.sign_size, l_ret->header.sign_pkey_size); return l_ret; } diff --git a/dap-sdk/crypto/src/dap_enc_key.c b/dap-sdk/crypto/src/dap_enc_key.c index d105cb5c14..4c82fd3a69 100755 --- a/dap-sdk/crypto/src/dap_enc_key.c +++ b/dap-sdk/crypto/src/dap_enc_key.c @@ -643,7 +643,7 @@ dap_enc_key_t* dap_enc_key_dup(dap_enc_key_t * a_key) return NULL; } dap_enc_key_t *l_ret = dap_enc_key_new(a_key->type); - if (l_ret->priv_key_data_size) { + if (a_key->priv_key_data_size) { l_ret->priv_key_data = DAP_NEW_Z_SIZE(byte_t, a_key->priv_key_data_size); l_ret->priv_key_data_size = a_key->priv_key_data_size; memcpy(l_ret->priv_key_data, a_key->priv_key_data, a_key->priv_key_data_size); -- GitLab