n2n/include/speck.h

143 lines
3.6 KiB
C
Raw Normal View History

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/>
*
*/
// 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
Basic C Code lint checker and shell checker (#859) * Factor build packages out into a more maintainable list * Create a location for scripts to live * Provide a make target to return the source dir as close as reasonable to the original distributed state * Add a code lint step, checking the coding style * Change test harness as recommended by shellcheck * Ensure we actually have the linter tool installed * Use the correct directory for cmake to run the tests * Adjust for the older uncrustify in the current github ubuntu-latest * Make one file pass the linter * Integrate the lint with the existing test workflow * Add files with minimal changes needed to the linter * Add more files with minimal changes needed to the linter * Dont build binaries if we fail the lint test * Update the phony targets with the lint steps * Ensure the flake8 package is installed in the new lint workflow job * Use the makefile to drive the packages needed to install for linting * No need to add dependancies on lint, just rely on the workflow status to show failure * Update the scripts dir README to reflect current assumptions * Rename and briefly document the indent.sh script * Fix the ignore to ignore the right Makefile * Rename the test_harness script to make it clear it is a shell script * Provide a master lint make target and add a shell script lint tool * Elminate stray tabs * Drop include/auth.h from linter - there are inconsistant results with function definitions when using the current uncrustify rules
2021-10-23 21:36:18 +02:00
#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
Basic C Code lint checker and shell checker (#859) * Factor build packages out into a more maintainable list * Create a location for scripts to live * Provide a make target to return the source dir as close as reasonable to the original distributed state * Add a code lint step, checking the coding style * Change test harness as recommended by shellcheck * Ensure we actually have the linter tool installed * Use the correct directory for cmake to run the tests * Adjust for the older uncrustify in the current github ubuntu-latest * Make one file pass the linter * Integrate the lint with the existing test workflow * Add files with minimal changes needed to the linter * Add more files with minimal changes needed to the linter * Dont build binaries if we fail the lint test * Update the phony targets with the lint steps * Ensure the flake8 package is installed in the new lint workflow job * Use the makefile to drive the packages needed to install for linting * No need to add dependancies on lint, just rely on the workflow status to show failure * Update the scripts dir README to reflect current assumptions * Rename and briefly document the indent.sh script * Fix the ignore to ignore the right Makefile * Rename the test_harness script to make it clear it is a shell script * Provide a master lint make target and add a shell script lint tool * Elminate stray tabs * Drop include/auth.h from linter - there are inconsistant results with function definitions when using the current uncrustify rules
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];
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
Basic C Code lint checker and shell checker (#859) * Factor build packages out into a more maintainable list * Create a location for scripts to live * Provide a make target to return the source dir as close as reasonable to the original distributed state * Add a code lint step, checking the coding style * Change test harness as recommended by shellcheck * Ensure we actually have the linter tool installed * Use the correct directory for cmake to run the tests * Adjust for the older uncrustify in the current github ubuntu-latest * Make one file pass the linter * Integrate the lint with the existing test workflow * Add files with minimal changes needed to the linter * Add more files with minimal changes needed to the linter * Dont build binaries if we fail the lint test * Update the phony targets with the lint steps * Ensure the flake8 package is installed in the new lint workflow job * Use the makefile to drive the packages needed to install for linting * No need to add dependancies on lint, just rely on the workflow status to show failure * Update the scripts dir README to reflect current assumptions * Rename and briefly document the indent.sh script * Fix the ignore to ignore the right Makefile * Rename the test_harness script to make it clear it is a shell script * Provide a master lint make target and add a shell script lint tool * Elminate stray tabs * Drop include/auth.h from linter - there are inconsistant results with function definitions when using the current uncrustify rules
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];
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,
Basic C Code lint checker and shell checker (#859) * Factor build packages out into a more maintainable list * Create a location for scripts to live * Provide a make target to return the source dir as close as reasonable to the original distributed state * Add a code lint step, checking the coding style * Change test harness as recommended by shellcheck * Ensure we actually have the linter tool installed * Use the correct directory for cmake to run the tests * Adjust for the older uncrustify in the current github ubuntu-latest * Make one file pass the linter * Integrate the lint with the existing test workflow * Add files with minimal changes needed to the linter * Add more files with minimal changes needed to the linter * Dont build binaries if we fail the lint test * Update the phony targets with the lint steps * Ensure the flake8 package is installed in the new lint workflow job * Use the makefile to drive the packages needed to install for linting * No need to add dependancies on lint, just rely on the workflow status to show failure * Update the scripts dir README to reflect current assumptions * Rename and briefly document the indent.sh script * Fix the ignore to ignore the right Makefile * Rename the test_harness script to make it clear it is a shell script * Provide a master lint make target and add a shell script lint tool * Elminate stray tabs * Drop include/auth.h from linter - there are inconsistant results with function definitions when using the current uncrustify rules
2021-10-23 21:36:18 +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) 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
int speck_128_decrypt (unsigned char *inout, speck_context_t *ctx);
int speck_128_encrypt (unsigned char *inout, speck_context_t *ctx);
2020-08-29 17:08:37 +02:00
#endif // SPECK_H