encrypt.c 6.58 KB
Newer Older
lwc-tester committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include"api.h"
typedef unsigned char u8;
typedef unsigned long long u64;
typedef long long i64;
typedef long long i64;
typedef unsigned int u32;

#define sbox(a, b, c, d, e, f, g, h)                                                                            \
{                                                                                                                             \
	t1 = ~a; t2 = b & t1;t3 = c ^ t2; h = d ^ t3; t5 = b | c; t6 = d ^ t1; g = t5 ^ t6; t8 = b ^ d; t9 = t3 & t6; e = t8 ^ t9; t11 = g & t8; f = t3 ^ t11; \
}

#define ARR_SIZE(a) (sizeof((a))/sizeof((a[0])))
#define ROTR961(a,b,n) (((a)<<(n))|((b)>>(64-n)))
#define ROTR962(a,b,n) (((b)<<(n))|((a)>>(32-n)))

#define ROTR96MORE321(a,b,n) ((b<<(n-32))>>32)
#define ROTR96MORE322(a,b,n) (b<<n|(u64)a<<(n-32)|b>>(96-n))
u8 constant7[127] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x41, 0x03, 0x06,
		0x0c, 0x18, 0x30, 0x61, 0x42, 0x05, 0x0a, 0x14, 0x28, 0x51, 0x23, 0x47,
		0x0f, 0x1e, 0x3c, 0x79, 0x72, 0x64, 0x48, 0x11, 0x22, 0x45, 0x0b, 0x16,
		0x2c, 0x59, 0x33, 0x67, 0x4e, 0x1d, 0x3a, 0x75, 0x6a, 0x54, 0x29, 0x53,
		0x27, 0x4f, 0x1f, 0x3e, 0x7d, 0x7a, 0x74, 0x68, 0x50, 0x21, 0x43, 0x07,
		0x0e, 0x1c, 0x38, 0x71, 0x62, 0x44, 0x09, 0x12, 0x24, 0x49, 0x13, 0x26,
		0x4d, 0x1b, 0x36, 0x6d, 0x5a, 0x35, 0x6b, 0x56, 0x2d, 0x5b, 0x37, 0x6f,
		0x5e, 0x3d, 0x7b, 0x76, 0x6c, 0x58, 0x31, 0x63, 0x46, 0x0d, 0x1a, 0x34,
		0x69, 0x52, 0x25, 0x4b, 0x17, 0x2e, 0x5d, 0x3b, 0x77, 0x6e, 0x5c, 0x39,
		0x73, 0x66, 0x4c, 0x19, 0x32, 0x65, 0x4a, 0x15, 0x2a, 0x55, 0x2b, 0x57,
		0x2f, 0x5f, 0x3f, 0x7f, 0x7e, 0x7c, 0x78, 0x70, 0x60, 0x40 };

lwc-tester committed
31
void load32(u32* x, u8* S) {
lwc-tester committed
32 33
	int i;
	*x = 0;
lwc-tester committed
34 35
	for (i = 0; i < 4; ++i)
		*x |= ((u32)S[i]) << i * 8;
lwc-tester committed
36 37
}

lwc-tester committed
38
void store32(u8* S, u32 x) {
lwc-tester committed
39
	int i;
lwc-tester committed
40 41
	for (i = 0; i < 4; ++i)
		S[i] = (u8)(x >> i * 8);
lwc-tester committed
42 43
}

lwc-tester committed
44
void load64(u64* x, u8* S) {
lwc-tester committed
45 46
	int i;
	*x = 0;
lwc-tester committed
47 48
	for (i = 0; i < 8; ++i)
		*x |= ((u64)S[i]) << i * 8;
lwc-tester committed
49 50
}

lwc-tester committed
51
void store64(u8* S, u64 x) {
lwc-tester committed
52
	int i;
lwc-tester committed
53 54
	for (i = 0; i < 8; ++i)
		S[i] = (u8)(x >> i * 8);
lwc-tester committed
55 56 57
}
void permutation384(u8* S, int rounds, u8 *c) {

lwc-tester committed
58 59 60 61 62
	int i;
	u64 x00, x10, x20, x30;
	u32 x01, x11, x21, x31;
	u64 x40, x50, x60, x70;
	u32 x41, x51, x61, x71;
lwc-tester committed
63

lwc-tester committed
64 65 66 67
	load64(&x00, S + 0);
	load32(&x01, S + 8);
	load64(&x10, S + 12);
	load32(&x11, S + 20);
lwc-tester committed
68

lwc-tester committed
69 70
	load64(&x20, S + 24);
	load32(&x21, S + 32);
lwc-tester committed
71

lwc-tester committed
72 73
	load64(&x30, S + 36);
	load32(&x31, S + 44);
lwc-tester committed
74 75 76 77

	u64 t1, t2, t3, t5, t6, t8, t9, t11;
	for (i = 0; i < rounds; ++i) {
		// addition of round constant
lwc-tester committed
78 79
		x00 ^= c[i];
		//substitution layer 
lwc-tester committed
80 81 82
		sbox(x00, x10, x20, x30, x40, x50, x60, x70);
		sbox(x01, x11, x21, x31, x41, x51, x61, x71);
		// linear diffusion layer
lwc-tester committed
83

lwc-tester committed
84 85
		x00 = x40;
		x01 = x41;
lwc-tester committed
86 87 88 89 90 91
		x11 = ROTR961(x51, x50, 1);
		x10 = ROTR962(x51, x50, 1);
		x21 = ROTR961(x61, x60, 8);
		x20 = ROTR962(x61, x60, 8);
		x31 = ROTR96MORE321(x71, x70, 55);
		x30 = ROTR96MORE322(x71, x70, 55);
lwc-tester committed
92
	}
lwc-tester committed
93 94 95 96 97 98 99 100 101
	store64(S, x00);
	store32(S + 8, x01);
	store64(S + 12, x10);
	store32(S + 20, x11);
	store64(S + 24, x20);
	store32(S + 32, x21);
	store64(S + 36, x30);
	store32(S + 44, x31);

lwc-tester committed
102 103
}
int crypto_aead_encrypt(unsigned char *c, unsigned long long *clen,
lwc-tester committed
104 105 106 107
	const unsigned char *m, unsigned long long mlen,
	const unsigned char *ad, unsigned long long adlen,
	const unsigned char *nsec, const unsigned char *npub,
	const unsigned char *k) {
lwc-tester committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

	int nr0 = 76;
	int nr = 28;
	int nrf = 32;

	int b = 384, r = 192;
	u32 size = b / 8; //32
	u32 rate = r / 8; //8
	u32 klen = CRYPTO_KEYBYTES;
	u32 nlen = CRYPTO_NPUBBYTES;
	u32 taglen = CRYPTO_ABYTES;
	u32 u = adlen / rate + 1;
	u32 v = mlen / rate + 1;
	u32 vl = mlen % rate;
	u32 i, j;
	u8 A[u * rate];
	u8 M[v * rate];
	u8 S[size];
	//pad associated data
	for (i = 0; i < adlen; i++) {
		A[i] = ad[i];
	}
lwc-tester committed
130
	A[adlen] = 0x01;
lwc-tester committed
131 132 133 134 135 136 137
	for (i = adlen + 1; i < u * rate; i++) {
		A[i] = 0;
	}
	//pad plaintext data
	for (i = 0; i < mlen; i++) {
		M[i] = m[i];
	}
lwc-tester committed
138
	M[mlen] = 0x01;
lwc-tester committed
139 140 141 142
	for (i = mlen + 1; i < v * rate; i++) {
		M[i] = 0;
	}
	//initalization
lwc-tester committed
143

lwc-tester committed
144 145 146 147 148 149
	for (i = 0; i < nlen; i++) {
		S[i] = npub[i];
	}
	for (i = 0; i < klen; i++) {
		S[i + nlen] = k[i];
	}
lwc-tester committed
150
	for (i = nlen + klen; i < size; i++) {
lwc-tester committed
151 152
		S[i] = 0;
	}
lwc-tester committed
153
	S[size - 1] ^= 0x80;
lwc-tester committed
154 155 156 157 158 159 160 161 162 163
	permutation384(S, nr0, constant7);
	//processiong associated data
	if (adlen != 0) {
		for (i = 0; i < u; i++) {
			for (j = 0; j < rate; j++) {
				S[j] ^= A[i * rate + j];
			}
			permutation384(S, nr, constant7);
		}
	}
lwc-tester committed
164
	S[size - 1] ^= 0x80;
lwc-tester committed
165 166 167 168
	// Encryption processiong plaintext data
	if (mlen != 0) {
		for (i = 0; i < v - 1; i++) {
			for (j = 0; j < rate; j++) {
lwc-tester committed
169
				S[j] ^= M[i * rate + j];
lwc-tester committed
170 171 172 173 174
				c[i * rate + j] = S[j];
			}
			permutation384(S, nr, constant7);
		}
		for (j = 0; j <= vl; j++) {
lwc-tester committed
175 176
			S[j] ^= M[(v - 1) * rate + j];
			c[(v - 1) * rate + j] = S[j];
lwc-tester committed
177 178 179 180 181 182 183 184 185 186 187 188
		}
	}
	//finalization
	permutation384(S, nrf, constant7);
	//return tag
	for (i = 0; i < taglen; i++) {
		c[mlen + i] = S[i];
	}
	*clen = mlen + taglen;
	return 0;
}
int crypto_aead_decrypt(unsigned char *m, unsigned long long *mlen,
lwc-tester committed
189 190 191
	unsigned char *nsec, const unsigned char *c, unsigned long long clen,
	const unsigned char *ad, unsigned long long adlen,
	const unsigned char *npub, const unsigned char *k) {
lwc-tester committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

	*mlen = 0;
	if (clen < CRYPTO_KEYBYTES)
		return -1;

	int nr0 = 76;
	int nr = 28;
	int nrf = 32;

	int b = 384, r = 192;
	u32 size = b / 8; //32
	u32 rate = r / 8; //8
	u32 klen = CRYPTO_KEYBYTES;
	u32 nlen = CRYPTO_NPUBBYTES;
	u32 taglen = CRYPTO_ABYTES;
	u32 u = adlen / rate + 1;
	u32 v = (clen - taglen) / rate + 1;
	u32 vl = (clen - taglen) % rate;
	u32 i, j;
	u8 A[u * rate];
	u8 M[v * rate];
	u8 S[size];
	//pad associated data
	for (i = 0; i < adlen; i++) {
		A[i] = ad[i];
	}
lwc-tester committed
218
	A[adlen] = 0x01;
lwc-tester committed
219 220 221 222 223 224 225 226 227 228
	for (i = adlen + 1; i < u * rate; i++) {
		A[i] = 0;
	}
	//initalization
	for (i = 0; i < nlen; i++) {
		S[i] = npub[i];
	}
	for (i = 0; i < klen; i++) {
		S[i + nlen] = k[i];
	}
lwc-tester committed
229
	for (i = nlen + klen; i < size; i++) {
lwc-tester committed
230 231
		S[i] = 0;
	}
lwc-tester committed
232
	S[size - 1] ^= 0x80;
lwc-tester committed
233 234 235 236 237
	permutation384(S, nr0, constant7);
	//processiong associated data
	if (adlen != 0) {
		for (i = 0; i < u; i++) {
			for (j = 0; j < rate; j++) {
lwc-tester committed
238
				S[j] ^= A[i * rate + j];
lwc-tester committed
239 240 241 242
			}
			permutation384(S, nr, constant7);
		}
	}
lwc-tester committed
243
	S[size - 1] ^= 0x80;
lwc-tester committed
244 245 246 247
	// Encryption processiong ciphertext data
	if (clen - taglen > 0) {
		for (i = 0; i < v - 1; i++) {
			for (j = 0; j < rate; j++) {
lwc-tester committed
248 249
				M[i * rate + j] = S[j] ^ c[i * rate + j];
				S[j] = c[i * rate + j];
lwc-tester committed
250 251 252 253
			}
			permutation384(S, nr, constant7);
		}
		for (j = 0; j < vl; j++) {
lwc-tester committed
254 255
			M[i * rate + j] = S[j] ^ c[i * rate + j];
			S[j] = c[i * rate + j];
lwc-tester committed
256
		}
lwc-tester committed
257
		S[j] ^= 0x01;
lwc-tester committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
	}
	//finalization
	permutation384(S, nrf, constant7);
	// return -1 if verification fails
	for (i = 0; i < klen; i++) {
		if (c[clen - klen + i] != S[i]) {
			return -1;
		}
	}
	*mlen = clen - taglen;
	for (i = 0; i < clen - taglen; i++) {
		m[i] = M[i];
	}
	return 0;
}