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/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2020-06-07 14:39:57 +02:00
|
|
|
// cipher SPECK -- 128 bit block size -- 256 bit key size
|
|
|
|
// 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-06-16 19:21:06 +02:00
|
|
|
#include <stdint.h>
|
2020-08-29 17:08:37 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include "portable_endian.h"
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2020-06-04 21:58:57 +02:00
|
|
|
#if defined (__AVX2__)
|
2020-06-06 14:25:54 +02:00
|
|
|
|
|
|
|
#include <immintrin.h>
|
2020-08-29 17:08:37 +02:00
|
|
|
#define SPECK_ALIGNED_CTX 32
|
2020-06-06 14:25:54 +02:00
|
|
|
#define u256 __m256i
|
2020-06-04 21:58:57 +02:00
|
|
|
typedef struct {
|
2020-06-07 14:39:57 +02:00
|
|
|
u256 rk[34];
|
|
|
|
u64 key[34];
|
2020-06-04 21:58:57 +02:00
|
|
|
} speck_context_t;
|
2020-06-06 14:25:54 +02:00
|
|
|
|
2020-10-10 15:09:28 +02:00
|
|
|
#elif defined (__SSE2__)
|
2020-06-06 14:25:54 +02:00
|
|
|
|
2020-08-29 17:08:37 +02:00
|
|
|
#include <immintrin.h>
|
2020-06-06 14:25:54 +02:00
|
|
|
#define SPECK_ALIGNED_CTX 16
|
|
|
|
#define SPECK_CTX_BYVAL 1
|
|
|
|
#define u128 __m128i
|
2020-06-03 18:12:49 +02:00
|
|
|
typedef struct {
|
2020-06-07 14:39:57 +02:00
|
|
|
u128 rk[34];
|
|
|
|
u64 key[34];
|
2020-06-03 18:12:49 +02:00
|
|
|
} speck_context_t;
|
2020-06-06 14:25:54 +02:00
|
|
|
|
2020-06-04 11:07:44 +02:00
|
|
|
#elif defined (__ARM_NEON)
|
2020-06-06 14:25:54 +02:00
|
|
|
|
|
|
|
#include <arm_neon.h>
|
|
|
|
#define u128 uint64x2_t
|
2020-06-04 11:07:44 +02:00
|
|
|
typedef struct {
|
2020-06-07 14:39:57 +02:00
|
|
|
u128 rk[34];
|
|
|
|
u64 key[34];
|
2020-06-04 11:07:44 +02:00
|
|
|
} speck_context_t;
|
2020-06-06 14:25:54 +02:00
|
|
|
|
2020-06-03 18:12:49 +02:00
|
|
|
#else
|
2020-06-06 14:25:54 +02:00
|
|
|
|
2020-06-03 18:25:49 +02:00
|
|
|
typedef struct {
|
2020-06-07 14:39:57 +02:00
|
|
|
u64 key[34];
|
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
|
|
|
#endif
|
|
|
|
|
2020-08-29 17:08:37 +02:00
|
|
|
// -----
|
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
|
|
|
|
2020-08-29 17:08:37 +02:00
|
|
|
int speck_init (const unsigned char *k, speck_context_t **ctx);
|
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-08-29 17:08:37 +02:00
|
|
|
// -----
|
2020-06-15 09:08:37 +02:00
|
|
|
|
|
|
|
int speck_he (unsigned char *out, const unsigned char *in, unsigned long long inlen,
|
|
|
|
const unsigned char *n, speck_context_t *ctx);
|
|
|
|
|
|
|
|
int speck_expand_key_he (const unsigned char *k, speck_context_t *ctx);
|
2020-06-16 09:42:38 +02:00
|
|
|
|
2020-08-29 17:08:37 +02:00
|
|
|
// -----
|
2020-06-16 09:42:38 +02:00
|
|
|
|
2020-06-29 11:28:36 +02:00
|
|
|
int speck_he_iv_encrypt (unsigned char *inout, speck_context_t *ctx);
|
|
|
|
|
|
|
|
int speck_he_iv_decrypt (unsigned char *inout, speck_context_t *ctx);
|
|
|
|
|
|
|
|
int speck_expand_key_he_iv (const unsigned char *k, speck_context_t *ctx);
|
|
|
|
|
|
|
|
|
2020-08-29 17:08:37 +02:00
|
|
|
#endif // SPECK_H
|