Commit 6de4d69f authored by obronchain's avatar obronchain

remove duplicated S-box

parent 9ce6e753
......@@ -22,45 +22,12 @@
*/
#include <string.h>
#include <stdint.h>
#include "primitives.h"
#define CLYDE_128_NS 6 // Number of steps
#define CLYDE_128_NR 2 * CLYDE_128_NS // Number of rounds
// Apply a S-box layer to a Clyde-128 state.
static void sbox_layer(uint32_t* state) {
uint32_t y1 = (state[0] & state[1]) ^ state[2];
uint32_t y0 = (state[3] & state[0]) ^ state[1];
uint32_t y3 = (y1 & state[3]) ^ state[0];
uint32_t y2 = (y0 & y1) ^ state[3];
state[0] = y0;
state[1] = y1;
state[2] = y2;
state[3] = y3;
}
// Apply a L-box to a pair of Clyde-128 rows.
#define ROT32(x,n) ((uint32_t)(((x)>>(n))|((x)<<(32-(n)))))
static void lbox(uint32_t* x, uint32_t* y) {
uint32_t a, b, c, d;
a = *x ^ ROT32(*x, 12);
b = *y ^ ROT32(*y, 12);
a = a ^ ROT32(a, 3);
b = b ^ ROT32(b, 3);
a = a ^ ROT32(*x, 17);
b = b ^ ROT32(*y, 17);
c = a ^ ROT32(a, 31);
d = b ^ ROT32(b, 31);
a = a ^ ROT32(d, 26);
b = b ^ ROT32(c, 25);
a = a ^ ROT32(c, 15);
b = b ^ ROT32(d, 15);
*x = a;
*y = b;
}
#define XORLS(DEST, OP) do { \
(DEST)[0] ^= (OP)[0]; \
(DEST)[1] ^= (OP)[1]; \
......
#include "primitives.h"
//#include INCLUDE_C(PRIMITIVES_TYPE)
//
#define QUOTEME_INNER(M) #M
#define QUOTEME(M) QUOTEME_INNER(M)
#define CONCAT3(X, Y, Z) X ## Y ## Z
#define INCLUDE_C(M) QUOTEME(CONCAT3(M, _, primitives.c))
// Apply a S-box layer to a Clyde-128 state.
void sbox_layer(uint32_t* state) {
uint32_t y1 = (state[0] & state[1]) ^ state[2];
uint32_t y0 = (state[3] & state[0]) ^ state[1];
uint32_t y3 = (y1 & state[3]) ^ state[0];
uint32_t y2 = (y0 & y1) ^ state[3];
state[0] = y0;
state[1] = y1;
state[2] = y2;
state[3] = y3;
}
// Apply a L-box to a pair of Clyde-128 rows.
void lbox(uint32_t* x, uint32_t* y) {
uint32_t a, b, c, d;
a = *x ^ ROT32(*x, 12);
b = *y ^ ROT32(*y, 12);
a = a ^ ROT32(a, 3);
b = b ^ ROT32(b, 3);
a = a ^ ROT32(*x, 17);
b = b ^ ROT32(*y, 17);
c = a ^ ROT32(a, 31);
d = b ^ ROT32(b, 31);
a = a ^ ROT32(d, 26);
b = b ^ ROT32(c, 25);
a = a ^ ROT32(c, 15);
b = b ^ ROT32(d, 15);
*x = a;
*y = b;
}
// Apply a D-box layer to a Shadow state.
void dbox_mls_layer(shadow_state state) {
for (unsigned int row = 0; row < LS_ROWS; row++) {
#if SMALL_PERM
uint32_t x = state[0][row];
uint32_t y = state[1][row];
uint32_t z = state[2][row];
state[0][row] = x ^ y ^ z;
state[1][row] = x ^ z;
state[2][row] = x ^ y;
#else
uint32_t w = state[0][row];
uint32_t x = state[1][row];
uint32_t y = state[2][row];
uint32_t z = state[3][row];
uint32_t u = w ^ x;
uint32_t v = y ^ z;
state[0][row] = x ^ v;
state[1][row] = w ^ v;
state[2][row] = u ^ z;
state[3][row] = u ^ y;
#endif // SMALL_PERM
}
}
......@@ -31,7 +31,7 @@
#define CLYDE_TYPE "clyde_32bit.c"
#define SHADOW_TYPE "shadow_128bit.c"
#define ROT32(x,n) ((uint32_t)(((x)>>(n))|((x)<<(32-(n)))))
typedef __attribute__((aligned(16))) uint32_t clyde128_state[LS_ROWS];
typedef __attribute__((aligned(64))) clyde128_state shadow_state[MLS_BUNDLES];
......@@ -39,6 +39,8 @@ void clyde128_encrypt(clyde128_state state,
const clyde128_state t, const unsigned char* k);
void shadow(shadow_state state);
void sbox_layer(uint32_t* state);
void dbox_mls_layer(shadow_state state);
void lbox(uint32_t* x, uint32_t* y);
#endif //_H_PRIMITIVES_H_
......@@ -23,7 +23,6 @@
#include <string.h>
#include <stdint.h>
#include "primitives.h"
#define CLYDE_128_NS 6 // Number of steps
......@@ -32,63 +31,6 @@
#define SHADOW_NR 2 * SHADOW_NS // Number of rounds
// Apply a S-box layer to a Clyde-128 state.
static void sbox_layer(uint32_t* state) {
uint32_t y1 = (state[0] & state[1]) ^ state[2];
uint32_t y0 = (state[3] & state[0]) ^ state[1];
uint32_t y3 = (y1 & state[3]) ^ state[0];
uint32_t y2 = (y0 & y1) ^ state[3];
state[0] = y0;
state[1] = y1;
state[2] = y2;
state[3] = y3;
}
// Apply a L-box to a pair of Clyde-128 rows.
#define ROT32(x,n) ((uint32_t)(((x)>>(n))|((x)<<(32-(n)))))
static void lbox(uint32_t* x, uint32_t* y) {
uint32_t a, b, c, d;
a = *x ^ ROT32(*x, 12);
b = *y ^ ROT32(*y, 12);
a = a ^ ROT32(a, 3);
b = b ^ ROT32(b, 3);
a = a ^ ROT32(*x, 17);
b = b ^ ROT32(*y, 17);
c = a ^ ROT32(a, 31);
d = b ^ ROT32(b, 31);
a = a ^ ROT32(d, 26);
b = b ^ ROT32(c, 25);
a = a ^ ROT32(c, 15);
b = b ^ ROT32(d, 15);
*x = a;
*y = b;
}
// Apply a D-box layer to a Shadow state.
static void dbox_mls_layer(shadow_state state) {
for (unsigned int row = 0; row < LS_ROWS; row++) {
#if SMALL_PERM
uint32_t x = state[0][row];
uint32_t y = state[1][row];
uint32_t z = state[2][row];
state[0][row] = x ^ y ^ z;
state[1][row] = x ^ z;
state[2][row] = x ^ y;
#else
uint32_t w = state[0][row];
uint32_t x = state[1][row];
uint32_t y = state[2][row];
uint32_t z = state[3][row];
uint32_t u = w ^ x;
uint32_t v = y ^ z;
state[0][row] = x ^ v;
state[1][row] = w ^ v;
state[2][row] = u ^ z;
state[3][row] = u ^ y;
#endif // SMALL_PERM
}
}
#define XORLSS(DEST, OP, SHIFT) do { \
(DEST)[0] ^= ((OP)[0] << (SHIFT)); \
(DEST)[1] ^= ((OP)[1] << (SHIFT)); \
......
......@@ -23,7 +23,7 @@ ifeq "$(SHADOW_TYPE)" "shadow_512bit"
CFLAGS+=-mavx512f
endif
objects := s1p.o genkat_aead.o encrypt.o $(CLYDE_TYPE).o $(SHADOW_TYPE).o
objects := s1p.o genkat_aead.o encrypt.o primitives.o $(CLYDE_TYPE).o $(SHADOW_TYPE).o
all: $(BDIR)/test
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment