Browse Source

readability code clean-up (#531)

* readability code clean-up

* readability code clean-up
pull/554/head
Logan oos Even 4 years ago
committed by GitHub
parent
commit
d063cf3088
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 20
      include/cc20.h
  2. 32
      src/cc20.c

20
include/cc20.h

@ -20,13 +20,18 @@
#ifndef CC20_H #ifndef CC20_H
#define CC20_H #define CC20_H
#include <stdint.h> #include <stdint.h>
#include "n2n.h" // HAVE_OPENSSL_1_1, traceEvent ... #include "n2n.h" // HAVE_OPENSSL_1_1, traceEvent ...
#define CC20_IV_SIZE 16 #define CC20_IV_SIZE 16
#define CC20_KEY_BYTES (256/8) #define CC20_KEY_BYTES (256/8)
#ifdef HAVE_OPENSSL_1_1 // openSSL 1.1 ----------------------------------------------------
#ifdef HAVE_OPENSSL_1_1 // openSSL 1.1 ----------------------------------------------------------------------------
#include <openssl/evp.h> #include <openssl/evp.h>
#include <openssl/err.h> #include <openssl/err.h>
@ -37,7 +42,9 @@ typedef struct cc20_context_t {
uint8_t key[CC20_KEY_BYTES]; /* the pure key data for payload encryption & decryption */ uint8_t key[CC20_KEY_BYTES]; /* the pure key data for payload encryption & decryption */
} cc20_context_t; } cc20_context_t;
#elif defined (__SSE2__) // SSE ----------------------------------------------------------
#elif defined (__SSE2__) // SSE2 ---------------------------------------------------------------------------------
#include <immintrin.h> #include <immintrin.h>
@ -46,7 +53,9 @@ typedef struct cc20_context {
uint8_t key[CC20_KEY_BYTES]; uint8_t key[CC20_KEY_BYTES];
} cc20_context_t; } cc20_context_t;
#else // plain C --------------------------------------------------------------------------
#else // plain C --------------------------------------------------------------------------------------------------
typedef struct cc20_context { typedef struct cc20_context {
uint32_t keystream32[16]; uint32_t keystream32[16];
@ -54,16 +63,15 @@ typedef struct cc20_context {
uint8_t key[CC20_KEY_BYTES]; uint8_t key[CC20_KEY_BYTES];
} cc20_context_t; } cc20_context_t;
#endif // openSSL 1.1, plain C ------------------------------------------------------------
#endif // openSSL 1.1, plain C ------------------------------------------------------------------------------------
int cc20_crypt (unsigned char *out, const unsigned char *in, size_t in_len, int cc20_crypt (unsigned char *out, const unsigned char *in, size_t in_len,
const unsigned char *iv, cc20_context_t *ctx); const unsigned char *iv, cc20_context_t *ctx);
int cc20_init (const unsigned char *key, cc20_context_t **ctx); int cc20_init (const unsigned char *key, cc20_context_t **ctx);
int cc20_deinit (cc20_context_t *ctx); int cc20_deinit (cc20_context_t *ctx);

32
src/cc20.c

@ -20,12 +20,13 @@
#include "cc20.h" #include "cc20.h"
#if defined (HAVE_OPENSSL_1_1) // openSSL 1.1 --------------------------------------------- #if defined (HAVE_OPENSSL_1_1) // openSSL 1.1 ---------------------------------------------------------------------
/* get any erorr message out of openssl // get any erorr message out of openssl
taken from https://en.wikibooks.org/wiki/OpenSSL/Error_handling */ // taken from https://en.wikibooks.org/wiki/OpenSSL/Error_handling
static char *openssl_err_as_string (void) { static char *openssl_err_as_string (void) {
BIO *bio = BIO_new(BIO_s_mem()); BIO *bio = BIO_new(BIO_s_mem());
ERR_print_errors(bio); ERR_print_errors(bio);
char *buf = NULL; char *buf = NULL;
@ -36,6 +37,7 @@ static char *openssl_err_as_string (void) {
memcpy(ret, buf, len); memcpy(ret, buf, len);
BIO_free(bio); BIO_free(bio);
return ret; return ret;
} }
@ -75,7 +77,7 @@ int cc20_crypt (unsigned char *out, const unsigned char *in, size_t in_len,
} }
#elif defined (__SSE2__) // SSE ---------------------------------------------------------- #elif defined (__SSE2__) // SSE2 ---------------------------------------------------------------------------------
// taken (and heavily modified and enhanced) from // taken (and heavily modified and enhanced) from
@ -93,15 +95,20 @@ int cc20_crypt (unsigned char *out, const unsigned char *in, size_t in_len,
#define TWO _mm_setr_epi32(2, 0, 0, 0) #define TWO _mm_setr_epi32(2, 0, 0, 0)
#if defined (__SSSE3__) // --- SSSE3 #if defined (__SSSE3__) // --- SSSE3
#define L8 _mm_set_epi32(0x0e0d0c0fL, 0x0a09080bL, 0x06050407L, 0x02010003L) #define L8 _mm_set_epi32(0x0e0d0c0fL, 0x0a09080bL, 0x06050407L, 0x02010003L)
#define L16 _mm_set_epi32(0x0d0c0f0eL, 0x09080b0aL, 0x05040706L, 0x01000302L) #define L16 _mm_set_epi32(0x0d0c0f0eL, 0x09080b0aL, 0x05040706L, 0x01000302L)
#define ROL8(X) ( _mm_shuffle_epi8(X, L8)) /* SSSE 3 */ #define ROL8(X) ( _mm_shuffle_epi8(X, L8)) /* SSSE 3 */
#define ROL16(X) ( _mm_shuffle_epi8(X, L16)) /* SSSE 3 */ #define ROL16(X) ( _mm_shuffle_epi8(X, L16)) /* SSSE 3 */
#else // --- regular SSE2 ---------- #else // --- regular SSE2 ----------
#define ROL8(X) ROL(X,8) #define ROL8(X) ROL(X,8)
#define ROL16(X) ROL(X,16) #define ROL16(X) ROL(X,16)
#endif // -------------------------- #endif // --------------------------
#define CC20_PERMUTE_ROWS(A,B,C,D) \ #define CC20_PERMUTE_ROWS(A,B,C,D) \
B = _mm_shuffle_epi32(B, _MM_SHUFFLE(0, 3, 2, 1)); \ B = _mm_shuffle_epi32(B, _MM_SHUFFLE(0, 3, 2, 1)); \
C = _mm_shuffle_epi32(C, _MM_SHUFFLE(1, 0, 3, 2)); \ C = _mm_shuffle_epi32(C, _MM_SHUFFLE(1, 0, 3, 2)); \
@ -133,6 +140,7 @@ int cc20_crypt (unsigned char *out, const unsigned char *in, size_t in_len,
_mm_xor_si128(_mm_loadu_si128((__m128i*)I), X)); \ _mm_xor_si128(_mm_loadu_si128((__m128i*)I), X)); \
I += 16; O += 16 \ I += 16; O += 16 \
int cc20_crypt (unsigned char *out, const unsigned char *in, size_t in_len, int cc20_crypt (unsigned char *out, const unsigned char *in, size_t in_len,
const unsigned char *iv, cc20_context_t *ctx) { const unsigned char *iv, cc20_context_t *ctx) {
@ -148,11 +156,10 @@ int cc20_crypt (unsigned char *out, const unsigned char *in, size_t in_len,
d = _mm_loadu_si128((__m128i*)iv); d = _mm_loadu_si128((__m128i*)iv);
while(in_len >= 128) { while(in_len >= 128) {
k0 = a; k1 = b; k2 = c; k3 = d; k0 = a; k1 = b; k2 = c; k3 = d;
k4 = a; k5 = b; k6 = c; k7 = ADD(d, ONE); k4 = a; k5 = b; k6 = c; k7 = ADD(d, ONE);
// 10 double rounds -- in parallel to make better use of all 8 SSE registers // 10 double rounds -- two in parallel to make better use of all 8 SSE registers
CC20_DOUBLE_ROUND(k0, k1, k2, k3); CC20_DOUBLE_ROUND(k4, k5, k6, k7); CC20_DOUBLE_ROUND(k0, k1, k2, k3); CC20_DOUBLE_ROUND(k4, k5, k6, k7);
CC20_DOUBLE_ROUND(k0, k1, k2, k3); CC20_DOUBLE_ROUND(k4, k5, k6, k7); CC20_DOUBLE_ROUND(k0, k1, k2, k3); CC20_DOUBLE_ROUND(k4, k5, k6, k7);
CC20_DOUBLE_ROUND(k0, k1, k2, k3); CC20_DOUBLE_ROUND(k4, k5, k6, k7); CC20_DOUBLE_ROUND(k0, k1, k2, k3); CC20_DOUBLE_ROUND(k4, k5, k6, k7);
@ -177,7 +184,6 @@ int cc20_crypt (unsigned char *out, const unsigned char *in, size_t in_len,
} }
if(in_len >= 64) { if(in_len >= 64) {
k0 = a; k1 = b; k2 = c; k3 = d; k0 = a; k1 = b; k2 = c; k3 = d;
// 10 double rounds // 10 double rounds
@ -203,7 +209,6 @@ int cc20_crypt (unsigned char *out, const unsigned char *in, size_t in_len,
} }
if(in_len) { if(in_len) {
k0 = a; k1 = b; k2 = c; k3 = d; k0 = a; k1 = b; k2 = c; k3 = d;
// 10 double rounds // 10 double rounds
@ -231,14 +236,13 @@ int cc20_crypt (unsigned char *out, const unsigned char *in, size_t in_len,
in_len--; in_len--;
out[in_len] = in[in_len] ^ keystream8[in_len]; out[in_len] = in[in_len] ^ keystream8[in_len];
} }
} }
return(0); return(0);
} }
#else // plain C -------------------------------------------------------------------------- #else // plain C --------------------------------------------------------------------------------------------------
// taken (and modified) from https://github.com/Ginurx/chacha20-c (public domain) // taken (and modified) from https://github.com/Ginurx/chacha20-c (public domain)
@ -255,11 +259,13 @@ static void cc20_init_block(cc20_context_t *ctx, const uint8_t nonce[]) {
#define ROL32(x,r) (((x)<<(r))|((x)>>(32-(r)))) #define ROL32(x,r) (((x)<<(r))|((x)>>(32-(r))))
#define CC20_QUARTERROUND(x, a, b, c, d) \ #define CC20_QUARTERROUND(x, a, b, c, d) \
x[a] += x[b]; x[d] = ROL32(x[d] ^ x[a], 16); \ x[a] += x[b]; x[d] = ROL32(x[d] ^ x[a], 16); \
x[c] += x[d]; x[b] = ROL32(x[b] ^ x[c], 12); \ x[c] += x[d]; x[b] = ROL32(x[b] ^ x[c], 12); \
x[a] += x[b]; x[d] = ROL32(x[d] ^ x[a], 8); \ x[a] += x[b]; x[d] = ROL32(x[d] ^ x[a], 8); \
x[c] += x[d]; x[b] = ROL32(x[b] ^ x[c], 7) x[c] += x[d]; x[b] = ROL32(x[b] ^ x[c], 7)
#define CC20_DOUBLE_ROUND(s) \ #define CC20_DOUBLE_ROUND(s) \
/* odd round */ \ /* odd round */ \
CC20_QUARTERROUND(s, 0, 4, 8, 12); \ CC20_QUARTERROUND(s, 0, 4, 8, 12); \
@ -272,6 +278,7 @@ static void cc20_init_block(cc20_context_t *ctx, const uint8_t nonce[]) {
CC20_QUARTERROUND(s, 2, 7, 8, 13); \ CC20_QUARTERROUND(s, 2, 7, 8, 13); \
CC20_QUARTERROUND(s, 3, 4, 9, 14) CC20_QUARTERROUND(s, 3, 4, 9, 14)
static void cc20_block_next(cc20_context_t *ctx) { static void cc20_block_next(cc20_context_t *ctx) {
uint32_t *counter = ctx->state + 12; uint32_t *counter = ctx->state + 12;
@ -344,7 +351,6 @@ int cc20_crypt (unsigned char *out, const unsigned char *in, size_t in_len,
cc20_init_context(ctx, iv); cc20_init_context(ctx, iv);
while(in_len >= 64) { while(in_len >= 64) {
cc20_block_next(ctx); cc20_block_next(ctx);
*(uint32_t*)out_p = *(uint32_t*)in_p ^ ctx->keystream32[ 0]; in_p++; out_p++; *(uint32_t*)out_p = *(uint32_t*)in_p ^ ctx->keystream32[ 0]; in_p++; out_p++;
@ -363,11 +369,11 @@ int cc20_crypt (unsigned char *out, const unsigned char *in, size_t in_len,
*(uint32_t*)out_p = *(uint32_t*)in_p ^ ctx->keystream32[13]; in_p++; out_p++; *(uint32_t*)out_p = *(uint32_t*)in_p ^ ctx->keystream32[13]; in_p++; out_p++;
*(uint32_t*)out_p = *(uint32_t*)in_p ^ ctx->keystream32[14]; in_p++; out_p++; *(uint32_t*)out_p = *(uint32_t*)in_p ^ ctx->keystream32[14]; in_p++; out_p++;
*(uint32_t*)out_p = *(uint32_t*)in_p ^ ctx->keystream32[15]; in_p++; out_p++; *(uint32_t*)out_p = *(uint32_t*)in_p ^ ctx->keystream32[15]; in_p++; out_p++;
in_len -= 64; in_len -= 64;
} }
if(in_len > 0) { if(in_len > 0) {
cc20_block_next(ctx); cc20_block_next(ctx);
tmp_len -= in_len; tmp_len -= in_len;
@ -382,7 +388,7 @@ int cc20_crypt (unsigned char *out, const unsigned char *in, size_t in_len,
} }
#endif // openSSL 1.1, plain C ------------------------------------------------------------ #endif // openSSL 1.1, plain C ------------------------------------------------------------------------------------
int cc20_init (const unsigned char *key, cc20_context_t **ctx) { int cc20_init (const unsigned char *key, cc20_context_t **ctx) {

Loading…
Cancel
Save