n2n/include/speck.h

141 lines
3.5 KiB
C
Raw Normal View History

2020-08-29 17:08:37 +02:00
/**
* (C) 2007-20 - 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
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
#include <stdint.h>
2020-08-29 17:08:37 +02:00
#include <stdlib.h>
2020-12-19 12:26:32 +01:00
2020-08-29 17:08:37 +02:00
#include "portable_endian.h"
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)
#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 --------------------------------------------------------------------------
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
#define SPECK_ALIGNED_CTX 32
2020-06-04 21:58:57 +02:00
typedef struct {
2020-12-19 12:26:32 +01:00
u256 rk[34];
u64 key[34];
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
2020-06-06 14:25:54 +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];
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
#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
typedef struct {
u128 rk[34];
u64 key[34];
u32 keysize;
} 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];
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,
2020-08-29 17:08:37 +02:00
speck_context_t *ctx);
2020-06-06 14:25:54 +02: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
// ----------------------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------------------
// 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
// used for IV in header encryption (one block); encrytion via speck_ctr with null_block as data
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
int speck_128_decrypt (unsigned char *inout, speck_context_t *ctx);
2020-08-29 17:08:37 +02:00
#endif // SPECK_H