encrypt.c 6.23 KB
Newer Older
KNOT team committed
1 2 3

#include"auxFormat.h"

Zhao Xuefeng committed
4
void Initialize(u32 *s, const unsigned char *npub, const unsigned char *k) {
KNOT team committed
5 6
	u8 tempData[24] = { 0 };
	packU96FormatToThreePacket(s, npub);
Zhao Xuefeng committed
7 8 9 10
	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);
KNOT team committed
11
	s[9] = 0x80000000;
Zhao Xuefeng committed
12 13 14 15 16
	P384(s, constant7Format, PR0_ROUNDS);
}
void ProcessAssocData(u32 *s, const u8* ad, unsigned long long adlen) {
	u32 dataFormat[6] = { 0 };
	u8 tempData[24] = { 0 };
KNOT team committed
17 18 19 20 21 22
	if (adlen) {
		while (adlen >= aead_RATE) {
			packU96FormatToThreePacket(dataFormat, ad);
			s[0] ^= dataFormat[0];
			s[1] ^= dataFormat[1];
			s[2] ^= dataFormat[2];
Zhao Xuefeng committed
23
			packU96FormatToThreePacket(dataFormat + 3, ad + 12);
KNOT team committed
24 25 26
			s[3] ^= dataFormat[3];
			s[4] ^= dataFormat[4];
			s[5] ^= dataFormat[5];
Zhao Xuefeng committed
27
			P384(s, constant7Format, PR_ROUNDS);
KNOT team committed
28 29 30 31 32 33 34 35 36 37
			adlen -= aead_RATE;
			ad += aead_RATE;
		}
		memset(tempData, 0, sizeof(tempData));
		memcpy(tempData, ad, adlen * sizeof(unsigned char));
		tempData[adlen] = 0x01;
		packU96FormatToThreePacket(dataFormat, tempData);
		s[0] ^= dataFormat[0];
		s[1] ^= dataFormat[1];
		s[2] ^= dataFormat[2];
Zhao Xuefeng committed
38
		packU96FormatToThreePacket(dataFormat + 3, tempData + 12);
KNOT team committed
39 40 41
		s[3] ^= dataFormat[3];
		s[4] ^= dataFormat[4];
		s[5] ^= dataFormat[5];
Zhao Xuefeng committed
42 43

		P384(s, constant7Format, PR_ROUNDS);
KNOT team committed
44 45
	}
	s[9] ^= 0x80000000;
Zhao Xuefeng committed
46 47 48 49 50
}

void ProcessPlaintext(u32 *s, const u8* m, unsigned long long mlen, unsigned char *c) {
	u32 dataFormat[6] = { 0 };
	u8 tempData[24] = { 0 };
KNOT team committed
51 52 53 54 55 56
	if (mlen) {
		while (mlen >= aead_RATE) {
			packU96FormatToThreePacket(dataFormat, m);
			s[0] ^= dataFormat[0];
			s[1] ^= dataFormat[1];
			s[2] ^= dataFormat[2];
Zhao Xuefeng committed
57
			packU96FormatToThreePacket(dataFormat + 3, m + 12);
KNOT team committed
58 59 60 61
			s[3] ^= dataFormat[3];
			s[4] ^= dataFormat[4];
			s[5] ^= dataFormat[5];
			unpackU96FormatToThreePacket(c, s);
Zhao Xuefeng committed
62 63 64
			unpackU96FormatToThreePacket(c + 12, s + 3);

			P384(s, constant7Format, PR_ROUNDS);
KNOT team committed
65 66 67 68 69 70
			mlen -= aead_RATE;
			m += aead_RATE;
			c += aead_RATE;
		}
		memset(tempData, 0, sizeof(tempData));
		memcpy(tempData, m, mlen * sizeof(unsigned char));
Zhao Xuefeng committed
71
		tempData[mlen] = 0x01;
KNOT team committed
72 73 74 75
		packU96FormatToThreePacket(dataFormat, tempData);
		s[0] ^= dataFormat[0];
		s[1] ^= dataFormat[1];
		s[2] ^= dataFormat[2];
Zhao Xuefeng committed
76
		packU96FormatToThreePacket(dataFormat + 3, tempData + 12);
KNOT team committed
77 78 79
		s[3] ^= dataFormat[3];
		s[4] ^= dataFormat[4];
		s[5] ^= dataFormat[5];
Zhao Xuefeng committed
80
		//*c = EXT_BYTE(x0, i);
KNOT team committed
81
		unpackU96FormatToThreePacket(tempData, s);
Zhao Xuefeng committed
82 83 84
		unpackU96FormatToThreePacket(tempData + 12, s + 3);
		memcpy(c, tempData, mlen * sizeof(unsigned char));
		//c += mlen;
KNOT team committed
85
	}
Zhao Xuefeng committed
86 87 88 89
}
void Finalize_GenerateTag(u32 *s, unsigned char *c) {
	u8 tempData[12] = { 0 };
	P384(s, constant7Format, PRF_ROUNDS);
KNOT team committed
90
	// return tag
Zhao Xuefeng committed
91 92 93
	unpackU96FormatToThreePacket(c , s);
	unpackU96FormatToThreePacket(tempData, s + 3);
	memcpy(c + 12 , tempData, sizeof(unsigned char) * 4);
KNOT team committed
94
}
Zhao Xuefeng committed
95 96
void ProcessCiphertext(u32 *s, unsigned char *m, const unsigned char *c, unsigned long long clen)
{
KNOT team committed
97 98
	u32 dataFormat[12] = { 0 };
	u32 dataFormat_1[12] = { 0 };
Wentao Zhang committed
99
	u8 tempU8[24] = { 0 },tempData[24] = { 0 };
KNOT team committed
100 101 102 103 104 105
	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];
Zhao Xuefeng committed
106
			packU96FormatToThreePacket(dataFormat + 3, c + 12);
KNOT team committed
107 108 109 110
			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);
Zhao Xuefeng committed
111
			unpackU96FormatToThreePacket(m + 12, dataFormat_1 + 3);
KNOT team committed
112 113 114 115 116 117
			s[0] = dataFormat[0];
			s[1] = dataFormat[1];
			s[2] = dataFormat[2];
			s[3] = dataFormat[3];
			s[4] = dataFormat[4];
			s[5] = dataFormat[5];
Zhao Xuefeng committed
118 119

			P384(s, constant7Format, PR_ROUNDS);
KNOT team committed
120 121 122 123 124
			clen -= aead_RATE;
			m += aead_RATE;
			c += aead_RATE;
		}
		unpackU96FormatToThreePacket(tempU8, s);
Zhao Xuefeng committed
125
		unpackU96FormatToThreePacket(tempU8 + 12, s + 3);
Wentao Zhang committed
126 127 128 129 130 131 132 133 134 135 136 137
		  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
138
		packU96FormatToThreePacket(s, tempU8);
Zhao Xuefeng committed
139
		packU96FormatToThreePacket(s + 3, tempU8 + 12);
KNOT team committed
140
	}
Zhao Xuefeng committed
141 142 143 144
}
int Finalize_VerifyTag(u32 *s, const unsigned char *c, unsigned char *m, unsigned long long *mlen) {
	u8 tempU8[24] = { 0 };
	P384(s, constant7Format, PRF_ROUNDS);
KNOT team committed
145 146
	// return tag	
	unpackU96FormatToThreePacket(tempU8, s);
Zhao Xuefeng committed
147 148 149 150
	unpackU96FormatToThreePacket(tempU8 + 12, s + 3);
	if (memcmp((void*)tempU8, (void*)(c), CRYPTO_ABYTES)) {
		memset(m, 0, sizeof(unsigned char) * (*mlen));
		*mlen = 0;
KNOT team committed
151 152 153 154
		return -1;
	}
	return 0;
}
Zhao Xuefeng committed
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
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) {
	u32 s[12] = { 0 };
	*clen = mlen + CRYPTO_ABYTES;
	// initialization
	Initialize(s,npub,k);
	// process associated data
	ProcessAssocData(s, ad, adlen);
	ProcessPlaintext(s, m, mlen,c);
	// finalization
	Finalize_GenerateTag(s, c + mlen);
	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) {
	u32 s[12] = { 0 };
		*mlen = clen - CRYPTO_ABYTES;
	if (clen < CRYPTO_ABYTES)
		return -1;
	// initialization
	Initialize(s, npub, k);
	// process associated data
	ProcessAssocData(s, ad, adlen);
	ProcessCiphertext(s,m,  c, clen - CRYPTO_ABYTES);
	// finalization		
	return Finalize_VerifyTag(s, c + clen - CRYPTO_KEYBYTES, m, mlen);
}