encrypt.c 7.39 KB
Newer Older
KNOT team 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300

#include"auxFormat.h"

#define RATE (64 / 8)

#define PR0_ROUNDS 52
#define PR_ROUNDS 28
#define PRF_ROUNDS 32
unsigned char  constant6Format[63] = {
	/*constant6_aead_128v1:*/
0x1,
0x10,
0x2,
0x20,
0x4,
0x41,
0x11,
0x12,
0x22,
0x24,
0x45,
0x50,
0x3,
0x30,
0x6,
0x61,
0x15,
0x53,
0x33,
0x36,
0x67,
0x74,
0x46,
0x60,
0x5,
0x51,
0x13,
0x32,
0x26,
0x65,
0x54,
0x42,
0x21,
0x14,
0x43,
0x31,
0x16,
0x63,
0x35,
0x57,
0x72,
0x27,
0x75,
0x56,
0x62,
0x25,
0x55,
0x52,
0x23,
0x34,
0x47,
0x70,
0x7,
0x71,
0x17,
0x73,
0x37,
0x77,
0x76,
0x66,
0x64,
0x44,
0x40,

};




static void permutation256(unsigned int *in, int rounds, unsigned char *rc) {
	uint32_t w0, w1, w2, w3, w4, w5, w6, w7;
	uint32_t s0, s1, s2;
	uint32_t one = 0x1;
	uint32_t i=0;
	uint32_t ff = 0xff;
	__asm volatile(
		"ldr     w0,     [in]          \n\t"
		"ldr     w4,     [in, #4]      \n\t"
		"ldr     w1,     [in, #8]      \n\t"
		"ldr     w5,     [in, #12]     \n\t"
		"ldr     w2,     [in, #16]     \n\t"
		"ldr     w6,     [in, #20]     \n\t"
		"ldr     w3,     [in, #24]     \n\t"
		"ldr     w7,     [in, #28]     \n\t"	
	"enc_loop:                       \n\t"
    "/*add round const   s0 s1*/           \n\t"
		"ldrb    s0,     [rc]          \n\t"	
		"LSR     s1,     s0, #4       \n\t"	
		"and    s0,     s0, 0xf        \n\t"
	  "eors    w4,     w4, s0        \n\t"
	  "eors    w0,     w0, s1        \n\t"
    "/*sbox first column*/         \n\t"
		"mvns    w0,     w0            \n\t"
		"ands    s0,     w1, w0        \n\t"
		"eors    s0,     w2, s0        \n\t"
		"orrs    w2,     w1, w2        \n\t"
		"eors    w0,     w3, w0        \n\t"
		"eors    w2,     w2, w0        \n\t"
		"eors    s1,     w1, w3        \n\t"
		"eors    w3,     w3, s0        \n\t"
		"ands    w0,     s0, w0        \n\t"
		"eors    w0,     s1, w0        \n\t"
		"ands    w1,     w2, s1        \n\t"
		"eors    w1,     s0, w1        \n\t"
		"/*sbox second column*/        \n\t"
		"mvns    w4,     w4            \n\t"
		"ands    s0,     w5, w4        \n\t"
		"eors    s0,     w6, s0        \n\t"
		"orrs    w6,     w5, w6        \n\t"
		"eors    w4,     w7, w4        \n\t"
		"eors    w6,     w6, w4        \n\t"
		"eors    s1,     w5, w7        \n\t"
		"eors    w7,     w7, s0        \n\t"
		"ands    w4,     s0, w4        \n\t"
		"eors    w4,     s1, w4        \n\t"
		"ands    w5,     w6, s1        \n\t"
		"eors    w5,     s0, w5        \n\t"
    "/*rotate shift left 1 bit*/   \n\t"
		"mov    s0,     w5       \n\t"
		"ROR    w5,     w1, #31        \n\t"
		"mov    w1,     s0       \n\t"
    "/*rotate shift left 8 bits*/  \n\t"
		"ROR    w2,     w2, #28        \n\t"
		"ROR    w6,     w6, #28       \n\t"
    "/*rotate shift left 25 bits*/ \n\t"
		"mov    s0,     w3       \n\t"
		"ROR    w3,     w7, #20       \n\t"
		"ROR    w7,     s0, #19       \n\t"
		"/*loop control*/              \n\t"
 		"adds    rc,     rc, #1        \n\t"
		"subs    rounds, rounds,  #1   \n\t"
		"bne     enc_loop              \n\t"
		"str     w0,     [in]         \n\t"
		"str     w4,     [in, #4]     \n\t"
		"str     w1,     [in, #8]     \n\t"
		"str     w5,     [in, #12]    \n\t"
		"str     w2,     [in, #16]    \n\t"
		"str     w6,     [in, #20]    \n\t"
		"str     w3,     [in, #24]    \n\t"
		"str     w7,     [in, #28]    \n\t"
	);
}


int crypto_aead_encrypt(unsigned char *c, unsigned long long *clen,
	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) {
	unsigned int  i, j;
	u32 s[8] = { 0 };
	u32 dataFormat[2] = { 0 };
	u8 tempData[8];
	u32 s_temp[8] = { 0 };
	u32 t1, t2, t3, t5, t6, t8, t9, t11;
	*clen = mlen + CRYPTO_ABYTES;
	//initialization
	packFormat(s, npub);
	packFormat((s + 2), (npub + 8));
	packFormat((s + 4), k);
	packFormat((s + 6), (k + 8));
	permutation256(s,PR0_ROUNDS,constant6Format);
	// process associated data
	if (adlen) {
		while (adlen >= RATE) {
			packFormat(dataFormat, ad);
			s[0] ^= dataFormat[0];
			s[1] ^= dataFormat[1];
	permutation256(s,PR_ROUNDS,constant6Format);
			adlen -= RATE;
			ad += RATE;
		}
		memset(tempData, 0, sizeof(tempData));
memcpy(tempData, ad, adlen * sizeof(unsigned char));	
tempData[adlen] = 0x01;
		packFormat(dataFormat, tempData);
		s[0] ^= dataFormat[0];
		s[1] ^= dataFormat[1];
	permutation256(s,PR_ROUNDS,constant6Format);
	}
	s[6] ^= 0x80000000;
	if (mlen) {
		while (mlen >= RATE) {
			packFormat(dataFormat, m);
			s[0] ^= dataFormat[0];
			s[1] ^= dataFormat[1];
			unpackFormat(c, s);
	permutation256(s,PR_ROUNDS,constant6Format);
			mlen -= RATE;
			m += RATE;
			c += RATE;
		}
		memset(tempData, 0, sizeof(tempData));
memcpy(tempData, m, mlen * sizeof(unsigned char));
  
tempData[mlen]= 0x01;
		packFormat(dataFormat, tempData);
		s[0] ^= dataFormat[0];
		s[1] ^= dataFormat[1];
		unpackFormat(tempData, s);
		memcpy(c, tempData, mlen * sizeof(unsigned char));
		c +=mlen;
	}
	// finalization
	permutation256(s,PRF_ROUNDS,constant6Format);
	// return tag
	unpackFormat(tempData, s);
		memcpy(c, tempData, sizeof(tempData));
	unpackFormat(tempData,(s + 2));
		memcpy(c+8, tempData, sizeof(tempData));
//	unpackFormat((c), s);
//	unpackFormat((c+8),(s + 2));
	return 0;
}

int crypto_aead_decrypt(unsigned char *m, unsigned long long *mlen,
	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) {
	u8 i, j;
	// initialization
	u32 s[8] = { 0 };
	u32 dataFormat[4] = { 0 };
	u32 dataFormat_1[2] = { 0 };
	u8 tempU8[32] = { 0 };
	u8 tempData[8];
	u32 s_temp[8] = { 0 };
	u32 t1, t2, t3, t5, t6, t8, t9, t11;
		*mlen = clen - CRYPTO_ABYTES;
	if (clen < CRYPTO_ABYTES)
		return -1;
	//initialization
	packFormat(s, npub);
	packFormat((s + 2), (npub + 8));
	packFormat((s + 4), k);
	packFormat((s + 6), (k + 8));
	permutation256(s,PR0_ROUNDS,constant6Format);
	// process associated data
	if (adlen) {
		while (adlen >= RATE) {
			packFormat(dataFormat, ad);
			s[0] ^= dataFormat[0];
			s[1] ^= dataFormat[1];
	permutation256(s,PR_ROUNDS,constant6Format);
			adlen -= RATE;
			ad += RATE;
		}
		memset(tempData, 0, sizeof(tempData));
		memcpy(tempData, ad, adlen * sizeof(unsigned char));
		tempData[adlen] = 0x01;
		packFormat(dataFormat, tempData);
		s[0] ^= dataFormat[0];
		s[1] ^= dataFormat[1];
	permutation256(s,PR_ROUNDS,constant6Format);
	}
	s[6] ^= 0x80000000;
  // process c
	clen = clen - CRYPTO_KEYBYTES;
	if (clen) {
		while (clen >= RATE) {
			packFormat(dataFormat, c);
			dataFormat_1[0] = s[0] ^ dataFormat[0];
			dataFormat_1[1] = s[1] ^ dataFormat[1];
			unpackFormat(m, dataFormat_1);
			s[0] = dataFormat[0];
			s[1] = dataFormat[1];
	permutation256(s,PR_ROUNDS,constant6Format);
			clen -= RATE;
			m += RATE;
			c += RATE;
		}
		unpackFormat(tempU8, s);
		for (i = 0; i < clen; ++i, ++m, ++c)
		{
			*m = tempU8[i]^ *c;
			tempU8[i] = *c;
		}
		tempU8[i] ^= 0x01;
		packFormat(s, tempU8);	
	}
	// finalization
	permutation256(s,PRF_ROUNDS,constant6Format);
	// return tag	
	packFormat(dataFormat, c);
	packFormat((dataFormat + 2), (c +8));
	if (dataFormat[0] != s[0] || dataFormat[1] != s[1] || dataFormat[2] != s[2] || dataFormat[3] != s[3]) {
		return -1;
	}
	return 0;
}