encrypt.c 6.4 KB
Newer Older
KNOT team committed
1 2 3
#include"auxFormat.h"

#define aead_RATE (192 / 8)
Wentao Zhang committed
4

KNOT team committed
5 6 7
#define PR0_ROUNDS 76
#define PR_ROUNDS 28
#define PRF_ROUNDS 32
Wentao Zhang committed
8
/*
KNOT team committed
9

Wentao Zhang committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 #define PR0_ROUNDS 76
 #define PR_ROUNDS 40
 #define PRF_ROUNDS 44

 * */
//12*6=72
unsigned char constant7Format[76] = {
/*constant7Format[127]:*/
0x01, 0x08, 0x40, 0x02, 0x10, 0x80, 0x05, 0x09, 0x48, 0x42, 0x12, 0x90, 0x85,
		0x0c, 0x41, 0x0a, 0x50, 0x82, 0x15, 0x89, 0x4d, 0x4b, 0x5a, 0xd2, 0x97,
		0x9c, 0xc4, 0x06, 0x11, 0x88, 0x45, 0x0b, 0x58, 0xc2, 0x17, 0x99, 0xcd,
		0x4e, 0x53, 0x9a, 0xd5, 0x8e, 0x54, 0x83, 0x1d, 0xc9, 0x4f, 0x5b, 0xda,
		0xd7, 0x9e, 0xd4, 0x86, 0x14, 0x81, 0x0d, 0x49, 0x4a, 0x52, 0x92, 0x95,
		0x8c, 0x44, 0x03, 0x18, 0xc0, 0x07, 0x19, 0xc8, 0x47, 0x1b, 0xd8, 0xc7,
		0x1e, 0xd1, 0x8f, };
KNOT team committed
25

KNOT team committed
26
int crypto_aead_encrypt(unsigned char *c, unsigned long long *clen,
Wentao Zhang committed
27 28 29 30
		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) {
KNOT team committed
31 32 33 34 35 36
	u8 i;
	u32 s[12] = { 0 };
	u8 tempData[24] = { 0 };
	u32 dataFormat[6] = { 0 };
	u32 s_temp[12] = { 0 };
	u32 t1, t2, t3, t5, t6, t8, t9, t11;
Wentao Zhang committed
37 38 39 40
	u32 t[3] = { 0 };
	u32 temp0[3] = { 0 };
	u32 temp1[3] = { 0 };
	u32 temp2[3] = { 0 };
Wentao Zhang committed
41
	u8 tempU8[24] = { 0 };
KNOT team committed
42 43 44
	*clen = mlen + CRYPTO_ABYTES;
	// initialization
	packU96FormatToThreePacket(s, npub);
Wentao Zhang committed
45 46
	memcpy(tempData, npub + 12, sizeof(unsigned char) * 4);
	memcpy(tempData + 4, k, sizeof(unsigned char) * 16);
KNOT team committed
47
	packU96FormatToThreePacket((s + 3), tempData);
Wentao Zhang committed
48
	packU96FormatToThreePacket((s + 6), (tempData + 12));
KNOT team committed
49 50 51 52 53 54 55
	s[9] = 0x80000000;
	for (i = 0; i < PR0_ROUNDS; i++) {
		ROUND384(i);
	}
	// process associated data
	if (adlen) {
		while (adlen >= aead_RATE) {
Wentao Zhang committed
56 57
			Processing_Data(ad);

KNOT team committed
58 59 60 61 62 63 64 65 66
			for (i = 0; i < PR_ROUNDS; i++) {
				ROUND384(i);
			}
			adlen -= aead_RATE;
			ad += aead_RATE;
		}
		memset(tempData, 0, sizeof(tempData));
		memcpy(tempData, ad, adlen * sizeof(unsigned char));
		tempData[adlen] = 0x01;
Wentao Zhang committed
67
		Processing_Data(tempData);
KNOT team committed
68 69 70 71 72
		for (i = 0; i < PR_ROUNDS; i++) {
			ROUND384(i);
		}
	}
	s[9] ^= 0x80000000;
Wentao Zhang committed
73
	// process p data
KNOT team committed
74 75
	if (mlen) {
		while (mlen >= aead_RATE) {
Wentao Zhang committed
76
			Processing_Data(m);
KNOT team committed
77
			unpackU96FormatToThreePacket(c, s);
Wentao Zhang committed
78
			unpackU96FormatToThreePacket((c + 12), (s + 3));
KNOT team committed
79 80 81 82 83 84 85 86 87
			for (i = 0; i < PR_ROUNDS; i++) {
				ROUND384(i);
			}
			mlen -= aead_RATE;
			m += aead_RATE;
			c += aead_RATE;
		}
		memset(tempData, 0, sizeof(tempData));
		memcpy(tempData, m, mlen * sizeof(unsigned char));
Wentao Zhang committed
88 89
		tempData[mlen] = 0x01;
		Processing_Data(tempData);
KNOT team committed
90
		unpackU96FormatToThreePacket(tempData, s);
Wentao Zhang committed
91 92
		unpackU96FormatToThreePacket((tempData + 12), (s + 3));
		memcpy(c, tempData, mlen * sizeof(unsigned char));
KNOT team committed
93 94 95 96 97 98
		c += mlen;
	}
	// finalization
	for (i = 0; i < PRF_ROUNDS; i++) {
		ROUND384(i);
	}
Wentao Zhang committed
99 100 101 102
	unpackU96FormatToThreePacket(tempU8, s);
	unpackU96FormatToThreePacket((tempU8 + 12), (s + 3));
	memcpy(c, tempU8, sizeof(unsigned char) * 12);
	memcpy(c + 12, tempU8 + 12, sizeof(unsigned char) * 4);
KNOT team committed
103 104 105 106
	return 0;
}

int crypto_aead_decrypt(unsigned char *m, unsigned long long *mlen,
Wentao Zhang committed
107 108 109
		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) {
KNOT team committed
110

Wentao Zhang committed
111
	u8 i;
KNOT team committed
112 113 114 115 116 117 118
	u32 s[12] = { 0 };
	u32 s_temp[12] = { 0 };
	u32 dataFormat[12] = { 0 };
	u32 dataFormat_1[12] = { 0 };
	u8 tempData[24] = { 0 };
	u8 tempU8[24] = { 0 };
	u32 t1, t2, t3, t5, t6, t8, t9, t11;
Wentao Zhang committed
119 120 121 122
	u32 t[3] = { 0 };
	u32 temp0[3] = { 0 };
	u32 temp1[3] = { 0 };
	u32 temp2[3] = { 0 };
Wentao Zhang committed
123
	*mlen = clen - CRYPTO_ABYTES;
KNOT team committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
	if (clen < CRYPTO_ABYTES)
		return -1;
	// initialization
	packU96FormatToThreePacket(s, npub);
	memcpy(tempData, npub + 12, sizeof(unsigned char) * 4);
	memcpy(tempData + 4, k, sizeof(unsigned char) * 16);
	packU96FormatToThreePacket((s + 3), tempData);
	packU96FormatToThreePacket((s + 6), (tempData + 12));
	s[9] = 0x80000000;
	for (i = 0; i < PR0_ROUNDS; i++) {
		ROUND384(i);
	}
	// process associated data
	if (adlen) {
		while (adlen >= aead_RATE) {
Wentao Zhang committed
139 140
			Processing_Data(ad);

KNOT team committed
141 142 143 144 145 146 147 148 149
			for (i = 0; i < PR_ROUNDS; i++) {
				ROUND384(i);
			}
			adlen -= aead_RATE;
			ad += aead_RATE;
		}
		memset(tempData, 0, sizeof(tempData));
		memcpy(tempData, ad, adlen * sizeof(unsigned char));
		tempData[adlen] = 0x01;
Wentao Zhang committed
150
		Processing_Data(tempData);
KNOT team committed
151 152 153 154 155 156 157 158 159 160 161 162 163
		for (i = 0; i < PR_ROUNDS; i++) {
			ROUND384(i);
		}
	}
	s[9] ^= 0x80000000;
	///////////
	clen -= CRYPTO_ABYTES;
	if (clen) {
		while (clen >= aead_RATE) {
			packU96FormatToThreePacket(dataFormat, c);
			dataFormat_1[0] = s[0] ^ dataFormat[0];
			dataFormat_1[1] = s[1] ^ dataFormat[1];
			dataFormat_1[2] = s[2] ^ dataFormat[2];
Wentao Zhang committed
164
			packU96FormatToThreePacket((dataFormat + 3), (c + 12));
KNOT team committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
			dataFormat_1[3] = s[3] ^ dataFormat[3];
			dataFormat_1[4] = s[4] ^ dataFormat[4];
			dataFormat_1[5] = s[5] ^ dataFormat[5];
			unpackU96FormatToThreePacket(m, dataFormat_1);
			unpackU96FormatToThreePacket((m + 12), (dataFormat_1 + 3));
			s[0] = dataFormat[0];
			s[1] = dataFormat[1];
			s[2] = dataFormat[2];
			s[3] = dataFormat[3];
			s[4] = dataFormat[4];
			s[5] = dataFormat[5];
			for (i = 0; i < PR_ROUNDS; i++) {
				ROUND384(i);
			}
			clen -= aead_RATE;
			m += aead_RATE;
			c += aead_RATE;
		}
		unpackU96FormatToThreePacket(tempU8, s);
Wentao Zhang committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
		unpackU96FormatToThreePacket((tempU8 + 12), (s + 3));
		memset(tempData, 0, sizeof(tempData));
		memcpy(tempData, c, clen * sizeof(unsigned char));
		tempData[clen] = 0x01;
		U32BIG(((u32*)tempU8)[0]) ^= U32BIG(
				((u32* )tempData)[0]);
		U32BIG(((u32*)tempU8)[1]) ^= U32BIG(
				((u32* )tempData)[1]);
		U32BIG(((u32*)tempU8)[2]) ^= U32BIG(
				((u32* )tempData)[2]);
		U32BIG(((u32*)tempU8)[3]) ^= U32BIG(
				((u32* )tempData)[3]);
		U32BIG(((u32*)tempU8)[4]) ^= U32BIG(
				((u32* )tempData)[4]);
		U32BIG(((u32*)tempU8)[5]) ^= U32BIG(
				((u32* )tempData)[5]);
		memcpy(m, tempU8, clen * sizeof(unsigned char));
		memcpy(tempU8, tempData, clen * sizeof(unsigned char));
		c += clen;
KNOT team committed
203 204 205 206 207 208 209 210
		packU96FormatToThreePacket(s, tempU8);
		packU96FormatToThreePacket((s + 3), (tempU8 + 12));
	}
	// finalization		
	for (i = 0; i < PRF_ROUNDS; i++) {
		ROUND384(i);
	}
	unpackU96FormatToThreePacket(tempU8, s);
KNOT team committed
211
	unpackU96FormatToThreePacket((tempU8 + 12), (s + 3));
Wentao Zhang committed
212 213
	if (memcmp((void*) tempU8, (void*) c, CRYPTO_ABYTES)) {
		memset(m, 0, sizeof(unsigned char) * (*mlen));
KNOT team committed
214
		*mlen = 0;
KNOT team committed
215 216 217 218
		return -1;
	}
	return 0;
}