2020-08-29 17:08:37 +02:00
|
|
|
/**
|
2022-03-12 10:22:42 +01:00
|
|
|
* (C) 2007-22 - ntop.org and contributors
|
2020-08-29 17:08:37 +02:00
|
|
|
*
|
|
|
|
* 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/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2021-01-07 11:14:44 +01:00
|
|
|
// cipher SPECK -- 128 bit block size -- 128 and 256 bit key size -- CTR mode
|
2020-06-07 14:39:57 +02:00
|
|
|
// taken from (and modified: removed pure crypto-stream generation and seperated key expansion)
|
|
|
|
// https://github.com/nsacyber/simon-speck-supercop/blob/master/crypto_stream/speck128256ctr/
|
|
|
|
|
2020-06-16 09:42:38 +02:00
|
|
|
|
|
|
|
#ifndef SPECK_H
|
|
|
|
#define SPECK_H
|
|
|
|
|
2020-12-19 12:26:32 +01:00
|
|
|
|
2023-06-14 21:30:43 +02:00
|
|
|
#include <stdint.h> // for uint64_t, uint32_t
|
2020-08-29 17:08:37 +02:00
|
|
|
|
2020-12-19 12:26:32 +01:00
|
|
|
|
2020-06-07 15:51:58 +02:00
|
|
|
#define u32 uint32_t
|
2020-05-27 23:32:21 +02:00
|
|
|
#define u64 uint64_t
|
|
|
|
|
2020-08-29 17:08:37 +02:00
|
|
|
#define N2N_SPECK_IVEC_SIZE 16
|
|
|
|
#define SPECK_KEY_BYTES (256/8)
|
|
|
|
|
|
|
|
|
2021-02-08 10:42:29 +01:00
|
|
|
#if defined (__AVX512F__) // AVX512 support -----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
#include <immintrin.h>
|
|
|
|
#include <string.h> /* memcpy() */
|
|
|
|
|
|
|
|
#define u512 __m512i
|
|
|
|
|
2021-10-23 21:36:18 +02:00
|
|
|
#define SPECK_ALIGNED_CTX 64
|
2021-02-08 10:42:29 +01:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
u512 rk[34];
|
|
|
|
u64 key[34];
|
|
|
|
u32 keysize;
|
|
|
|
} speck_context_t;
|
|
|
|
|
|
|
|
|
|
|
|
#elif defined (__AVX2__) // AVX2 support --------------------------------------------------------------------------
|
2020-12-19 12:26:32 +01:00
|
|
|
|
2020-06-06 14:25:54 +02:00
|
|
|
|
|
|
|
#include <immintrin.h>
|
2020-12-19 12:26:32 +01:00
|
|
|
|
2020-06-06 14:25:54 +02:00
|
|
|
#define u256 __m256i
|
2020-12-19 12:26:32 +01:00
|
|
|
|
2021-10-23 21:36:18 +02:00
|
|
|
#define SPECK_ALIGNED_CTX 32
|
2020-12-19 12:26:32 +01:00
|
|
|
|
2020-06-04 21:58:57 +02:00
|
|
|
typedef struct {
|
2020-12-19 12:26:32 +01:00
|
|
|
u256 rk[34];
|
|
|
|
u64 key[34];
|
2021-01-07 11:14:44 +01:00
|
|
|
u32 keysize;
|
2020-06-04 21:58:57 +02:00
|
|
|
} speck_context_t;
|
2020-06-06 14:25:54 +02:00
|
|
|
|
2020-12-19 12:26:32 +01:00
|
|
|
|
|
|
|
#elif defined (__SSE2__) // SSE support ---------------------------------------------------------------------------
|
|
|
|
|
2020-06-06 14:25:54 +02:00
|
|
|
|
2020-08-29 17:08:37 +02:00
|
|
|
#include <immintrin.h>
|
2020-12-19 12:26:32 +01:00
|
|
|
|
|
|
|
#define u128 __m128i
|
|
|
|
|
2021-10-23 21:36:18 +02:00
|
|
|
#define SPECK_ALIGNED_CTX 16
|
|
|
|
#define SPECK_CTX_BYVAL 1
|
2020-12-19 12:26:32 +01:00
|
|
|
|
2020-06-03 18:12:49 +02:00
|
|
|
typedef struct {
|
2020-12-19 12:26:32 +01:00
|
|
|
u128 rk[34];
|
|
|
|
u64 key[34];
|
2021-01-07 11:14:44 +01:00
|
|
|
u32 keysize;
|
2020-06-03 18:12:49 +02:00
|
|
|
} speck_context_t;
|
2020-06-06 14:25:54 +02:00
|
|
|
|
2020-12-19 12:26:32 +01:00
|
|
|
|
2021-01-26 16:38:08 +01:00
|
|
|
#elif defined (__ARM_NEON) && defined (SPECK_ARM_NEON) // NEON support ---------------------------------------
|
2020-12-19 12:26:32 +01:00
|
|
|
|
2020-06-06 14:25:54 +02:00
|
|
|
|
|
|
|
#include <arm_neon.h>
|
2020-12-19 12:26:32 +01:00
|
|
|
|
2020-06-06 14:25:54 +02:00
|
|
|
#define u128 uint64x2_t
|
2020-12-19 12:26:32 +01:00
|
|
|
|
2020-06-04 11:07:44 +02:00
|
|
|
typedef struct {
|
2021-01-07 11:14:44 +01:00
|
|
|
u128 rk[34];
|
|
|
|
u64 key[34];
|
|
|
|
u32 keysize;
|
2020-06-04 11:07:44 +02:00
|
|
|
} speck_context_t;
|
2020-06-06 14:25:54 +02:00
|
|
|
|
2020-12-19 12:26:32 +01:00
|
|
|
|
|
|
|
#else // plain C --------------------------------------------------------------------------------------------------
|
|
|
|
|
2020-06-06 14:25:54 +02:00
|
|
|
|
2020-06-03 18:25:49 +02:00
|
|
|
typedef struct {
|
2020-12-19 12:26:32 +01:00
|
|
|
u64 key[34];
|
2021-01-07 11:14:44 +01:00
|
|
|
u32 keysize;
|
2020-06-03 18:25:49 +02:00
|
|
|
} speck_context_t;
|
2020-06-06 14:25:54 +02:00
|
|
|
|
2020-06-03 18:12:49 +02:00
|
|
|
|
2020-12-19 12:26:32 +01:00
|
|
|
#endif // ---------------------------------------------------------------------------------------------------------
|
|
|
|
|
2020-06-06 14:25:54 +02:00
|
|
|
|
|
|
|
int speck_ctr (unsigned char *out, const unsigned char *in, unsigned long long inlen,
|
2020-05-27 23:32:21 +02:00
|
|
|
const unsigned char *n,
|
2021-10-23 21:36:18 +02:00
|
|
|
speck_context_t *ctx);
|
2020-06-06 14:25:54 +02:00
|
|
|
|
2021-01-07 11:14:44 +01:00
|
|
|
int speck_init (speck_context_t **ctx, const unsigned char *k, int keysize);
|
2020-05-27 23:32:21 +02:00
|
|
|
|
2020-08-29 17:08:37 +02:00
|
|
|
int speck_deinit (speck_context_t *ctx);
|
2020-06-15 09:08:37 +02:00
|
|
|
|
2020-12-19 12:26:32 +01:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------------------------------------
|
|
|
|
// ----------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2021-01-17 13:52:17 +01:00
|
|
|
// cipher SPECK -- 128 bit block size -- 128 bit key size -- ECB mode
|
2020-12-19 12:26:32 +01:00
|
|
|
// follows endianess rules as used in official implementation guide and NOT as in original 2013 cipher presentation
|
2021-06-03 23:26:22 +02:00
|
|
|
// used for IV in header encryption (one block) and challenge encryption (user/password)
|
2020-12-19 12:26:32 +01:00
|
|
|
// for now: just plain C -- probably no need for AVX, SSE, NEON
|
|
|
|
|
2020-06-16 09:42:38 +02:00
|
|
|
|
2021-01-17 13:52:17 +01:00
|
|
|
int speck_128_decrypt (unsigned char *inout, speck_context_t *ctx);
|
2020-06-29 11:28:36 +02:00
|
|
|
|
2021-06-03 23:26:22 +02:00
|
|
|
int speck_128_encrypt (unsigned char *inout, speck_context_t *ctx);
|
|
|
|
|
2020-06-29 11:28:36 +02:00
|
|
|
|
2020-08-29 17:08:37 +02:00
|
|
|
#endif // SPECK_H
|