speck.h (n2n-2.8) | : | speck.h (n2n-3.0) | ||
---|---|---|---|---|
// cipher SPECK -- 128 bit block size -- 256 bit key size | /** | |||
* (C) 2007-21 - ntop.org and contributors | ||||
* | ||||
* This program is free software; you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation; either version 3 of the License, or | ||||
* (at your option) any later version. | ||||
* | ||||
* This program is distributed in the hope that it will be useful, | ||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
* GNU General Public License for more details. | ||||
* | ||||
* You should have received a copy of the GNU General Public License | ||||
* along with this program; if not see see <http://www.gnu.org/licenses/> | ||||
* | ||||
*/ | ||||
// cipher SPECK -- 128 bit block size -- 128 and 256 bit key size -- CTR mode | ||||
// taken from (and modified: removed pure crypto-stream generation and seperated key expansion) | // taken from (and modified: removed pure crypto-stream generation and seperated key expansion) | |||
// https://github.com/nsacyber/simon-speck-supercop/blob/master/crypto_stream/sp eck128256ctr/ | // https://github.com/nsacyber/simon-speck-supercop/blob/master/crypto_stream/sp eck128256ctr/ | |||
#ifndef SPECK_H | #ifndef SPECK_H | |||
#define SPECK_H | #define SPECK_H | |||
#include <stdint.h> | #include <stdint.h> | |||
#include <stdlib.h> | ||||
#include "portable_endian.h" | ||||
#define u32 uint32_t | #define u32 uint32_t | |||
#define u64 uint64_t | #define u64 uint64_t | |||
#if defined (__AVX2__) | #define N2N_SPECK_IVEC_SIZE 16 | |||
#define SPECK_KEY_BYTES (256/8) | ||||
#if defined (__AVX512F__) // AVX512 support ------------------------------------ | ||||
----------------------------------- | ||||
#include <immintrin.h> | ||||
#include <string.h> /* memcpy() */ | ||||
#define u512 __m512i | ||||
#define SPECK_ALIGNED_CTX 64 | ||||
typedef struct { | ||||
u512 rk[34]; | ||||
u64 key[34]; | ||||
u32 keysize; | ||||
} speck_context_t; | ||||
#elif defined (__AVX2__) // AVX2 support --------------------------------------- | ||||
----------------------------------- | ||||
#define SPECK_ALIGNED_CTX 32 | ||||
#include <immintrin.h> | #include <immintrin.h> | |||
#define u256 __m256i | #define u256 __m256i | |||
#define SPECK_ALIGNED_CTX 32 | ||||
typedef struct { | typedef struct { | |||
u256 rk[34]; | u256 rk[34]; | |||
u64 key[34]; | u64 key[34]; | |||
u32 keysize; | ||||
} speck_context_t; | } speck_context_t; | |||
#elif defined (__SSE4_2__) | #elif defined (__SSE2__) // SSE support ---------------------------------------- ----------------------------------- | |||
#define SPECK_ALIGNED_CTX 16 | ||||
#define SPECK_CTX_BYVAL 1 | ||||
#include <immintrin.h> | #include <immintrin.h> | |||
#define u128 __m128i | #define u128 __m128i | |||
#define SPECK_ALIGNED_CTX 16 | ||||
#define SPECK_CTX_BYVAL 1 | ||||
typedef struct { | typedef struct { | |||
u128 rk[34]; | u128 rk[34]; | |||
u64 key[34]; | u64 key[34]; | |||
u32 keysize; | ||||
} speck_context_t; | } speck_context_t; | |||
#elif defined (__ARM_NEON) | #elif defined (__ARM_NEON) && defined (SPECK_ARM_NEON) // NEON support ---- ----------------------------------- | |||
#include <arm_neon.h> | #include <arm_neon.h> | |||
#define u128 uint64x2_t | #define u128 uint64x2_t | |||
typedef struct { | typedef struct { | |||
u128 rk[34]; | u128 rk[34]; | |||
u64 key[34]; | u64 key[34]; | |||
u32 keysize; | ||||
} speck_context_t; | } speck_context_t; | |||
#else | #else // plain C --------------------------------------------------------------- ----------------------------------- | |||
typedef struct { | typedef struct { | |||
u64 key[34]; | u64 key[34]; | |||
u32 keysize; | ||||
} speck_context_t; | } speck_context_t; | |||
#endif | #endif // ---------------------------------------------------------------------- ----------------------------------- | |||
int speck_ctr (unsigned char *out, const unsigned char *in, unsigned long long i nlen, | int speck_ctr (unsigned char *out, const unsigned char *in, unsigned long long i nlen, | |||
const unsigned char *n, | const unsigned char *n, | |||
#if defined (SPECK_CTX_BYVAL) | speck_context_t *ctx); | |||
speck_context_t ctx); | ||||
#else | ||||
speck_context_t *ctx); | ||||
#endif | ||||
int speck_expand_key (const unsigned char *k, speck_context_t *ctx); | int speck_init (speck_context_t **ctx, const unsigned char *k, int keysize); | |||
int speck_he (unsigned char *out, const unsigned char *in, unsigned long long in | int speck_deinit (speck_context_t *ctx); | |||
len, | ||||
const unsigned char *n, speck_context_t *ctx); | ||||
int speck_expand_key_he (const unsigned char *k, speck_context_t *ctx); | // ----------------------------------------------------------------------------- | |||
----------------------------------- | ||||
// ----------------------------------------------------------------------------- | ||||
----------------------------------- | ||||
int speck_he_iv_encrypt (unsigned char *inout, speck_context_t *ctx); | // cipher SPECK -- 128 bit block size -- 128 bit key size -- ECB mode | |||
// follows endianess rules as used in official implementation guide and NOT as i | ||||
n original 2013 cipher presentation | ||||
// used for IV in header encryption (one block) and challenge encryption (user/p | ||||
assword) | ||||
// for now: just plain C -- probably no need for AVX, SSE, NEON | ||||
int speck_he_iv_decrypt (unsigned char *inout, speck_context_t *ctx); | int speck_128_decrypt (unsigned char *inout, speck_context_t *ctx); | |||
int speck_expand_key_he_iv (const unsigned char *k, speck_context_t *ctx); | int speck_128_encrypt (unsigned char *inout, speck_context_t *ctx); | |||
#endif | #endif // SPECK_H | |||
End of changes. 27 change blocks. | ||||
29 lines changed or deleted | 85 lines changed or added |