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

Zhao Xuefeng committed
3
void ProcessAssocData(unsigned int *s, const u8* ad, unsigned long long adlen) {
KNOT team committed
4 5 6 7 8 9 10
	u32 dataFormat[2] = { 0 };
	u8 tempData[8];
	if (adlen) {
		while (adlen >= RATE) {
			packFormat(dataFormat, ad);
			s[0] ^= dataFormat[0];
			s[1] ^= dataFormat[1];
Zhao Xuefeng committed
11
			P256(s, constant6Format, PR_ROUNDS);
KNOT team committed
12 13 14 15
			adlen -= RATE;
			ad += RATE;
		}
		memset(tempData, 0, sizeof(tempData));
Zhao Xuefeng committed
16 17
		memcpy(tempData, ad, adlen * sizeof(unsigned char));
		tempData[adlen] = 0x01;
KNOT team committed
18 19 20
		packFormat(dataFormat, tempData);
		s[0] ^= dataFormat[0];
		s[1] ^= dataFormat[1];
Zhao Xuefeng committed
21
		P256(s, constant6Format, PR_ROUNDS);
KNOT team committed
22 23
	}
	s[6] ^= 0x80000000;
Zhao Xuefeng committed
24 25 26 27 28
}
void ProcessPlaintext(unsigned int *s, const u8* m, unsigned long long mlen,
		unsigned char *c) {
	u32 dataFormat[2] = { 0 };
	u8 tempData[8] = { 0 };
KNOT team committed
29 30 31 32 33 34
	if (mlen) {
		while (mlen >= RATE) {
			packFormat(dataFormat, m);
			s[0] ^= dataFormat[0];
			s[1] ^= dataFormat[1];
			unpackFormat(c, s);
Zhao Xuefeng committed
35
			P256(s, constant6Format, PR_ROUNDS);
KNOT team committed
36 37 38 39 40
			mlen -= RATE;
			m += RATE;
			c += RATE;
		}
		memset(tempData, 0, sizeof(tempData));
Zhao Xuefeng committed
41 42
		memcpy(tempData, m, mlen * sizeof(unsigned char));
		tempData[mlen] = 0x01;
KNOT team committed
43 44 45 46 47
		packFormat(dataFormat, tempData);
		s[0] ^= dataFormat[0];
		s[1] ^= dataFormat[1];
		unpackFormat(tempData, s);
		memcpy(c, tempData, mlen * sizeof(unsigned char));
Zhao Xuefeng committed
48
		//c+=mlen;
KNOT team committed
49
	}
Zhao Xuefeng committed
50 51 52
}
void Finalize_GenerateTag(unsigned int *s, unsigned char *c) {
	P256(s, constant6Format, PRF_ROUNDS);
KNOT team committed
53
	// return tag
Zhao Xuefeng committed
54 55
	unpackFormat(c, s);
	unpackFormat((c + 8), (s + 2));
KNOT team committed
56
}
Zhao Xuefeng committed
57
void Initialize(unsigned int *s, const unsigned char *npub, const unsigned char *k) {
KNOT team committed
58
	packFormat(s, npub);
Zhao Xuefeng committed
59 60 61 62 63 64 65 66 67 68
	packFormat(s + 2, npub + 8);
	packFormat(s + 4, k);
	packFormat(s + 6, k + 8);
	P256(s, constant6Format, PR0_ROUNDS);
}
void ProcessCiphertext(unsigned int *s, unsigned char *m, const unsigned char *c,
		unsigned long long clen) {
	u8 tempU8[32] = { 0 }, i;
	u32 dataFormat[2] = { 0 };
	u32 dataFormat_1[2] = { 0 };
KNOT team committed
69 70 71 72 73 74 75 76
	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];
Zhao Xuefeng committed
77
			P256(s, constant6Format, PR_ROUNDS);
KNOT team committed
78 79 80 81 82
			clen -= RATE;
			m += RATE;
			c += RATE;
		}
		unpackFormat(tempU8, s);
Zhao Xuefeng committed
83 84
		for (i = 0; i < clen; ++i, ++m, ++c) {
			*m = tempU8[i] ^ *c;
KNOT team committed
85 86 87
			tempU8[i] = *c;
		}
		tempU8[i] ^= 0x01;
Zhao Xuefeng committed
88
		packFormat(s, tempU8);
KNOT team committed
89
	}
Zhao Xuefeng committed
90 91 92 93 94
}
int Finalize_VerifyTag(unsigned int *s, const unsigned char *c, unsigned char *m,
		unsigned long long *mlen) {
	u8 tempU8[16] = { 0 };
	P256(s, constant6Format, PRF_ROUNDS);
KNOT team committed
95
	// return tag	
Zhao Xuefeng committed
96 97 98 99 100
	unpackFormat(tempU8, s);
	unpackFormat((tempU8 + 8), (s + 2));
	if (memcmp((void*) tempU8, (void*) (c), CRYPTO_ABYTES)) {
		memset(m, 0, sizeof(unsigned char) * (*mlen));
		*mlen = 0;
KNOT team committed
101 102 103 104
		return -1;
	}
	return 0;
}
Zhao Xuefeng committed
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

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  s[8] = { 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) {
	unsigned int s[8] = { 0 };
	*mlen = clen - CRYPTO_ABYTES;
	if (clen < CRYPTO_ABYTES)
		return -1;
	//initialization
	Initialize(s, npub, k);
	// process associated data
	ProcessAssocData(s, ad, adlen);
	// process cipher
	ProcessCiphertext(s, m, c, clen - CRYPTO_KEYBYTES);
	// finalization
	return Finalize_VerifyTag(s, c + clen - CRYPTO_KEYBYTES, m, mlen);
}