encrypt.c 5.59 KB
Newer Older
Wentao Zhang 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
#include"auxFormat.h"

#define aead_RATE 16
#define PR0_ROUNDS 100
#define PR_ROUNDS 52
#define PRF_ROUNDS 56
/*
 #define PR0_ROUNDS 100
 #define PR_ROUNDS 52
 #define PRF_ROUNDS 56
 * */
unsigned char constant7Format_aead[100] = { 0x01, 0x04, 0x10, 0x40, 0x02, 0x08,
		0x21, 0x05, 0x14, 0x50, 0x42, 0x0a, 0x29, 0x24, 0x11, 0x44, 0x12, 0x48,
		0x23, 0x0d, 0x35, 0x55, 0x56, 0x5a, 0x6b, 0x2e, 0x38, 0x60, 0x03, 0x0c,
		0x31, 0x45, 0x16, 0x58, 0x63, 0x0f, 0x3d, 0x74, 0x53, 0x4e, 0x3b, 0x6c,
		0x32, 0x49, 0x27, 0x1d, 0x75, 0x57, 0x5e, 0x7b, 0x6e, 0x3a, 0x68, 0x22,
		0x09, 0x25, 0x15, 0x54, 0x52, 0x4a, 0x2b, 0x2c, 0x30, 0x41, 0x06, 0x18,
		0x61, 0x07, 0x1c, 0x71, 0x47, 0x1e, 0x79, 0x66, 0x1b, 0x6d, 0x36, 0x59,
		0x67, 0x1f, 0x7d, 0x76, 0x5b, 0x6f, 0x3e, 0x78, 0x62, 0x0b, 0x2d, 0x34,
		0x51, 0x46, 0x1a, 0x69, 0x26, 0x19, 0x65, 0x17, 0x5c, 0x73, };

#define Processing_Data(data) \
do { \
	packU128FormatToFourPacket(dataFormat, data);   \
	s[0] ^= dataFormat[0];   \
	s[1] ^= dataFormat[1];   \
	s[2] ^= dataFormat[2];   \
	s[3] ^= dataFormat[3];   \
} while (0)

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 i;
	u32 s_temp[16] = { 0 };
	u32 t1, t2, t3, t5, t6, t8, t9, t11;
	u32 s[16] = { 0 };
	u32 t[4] = { 0 };
	u32 dataFormat[4] = { 0 };
	u8 tempData[16] = { 0 };
	u8 tempU8[32] = { 0 };
	*clen = mlen + CRYPTO_ABYTES;
	//initialization
	packU128FormatToFourPacket(s, npub);
	packU128FormatToFourPacket((s + 4), (npub + 16));
	packU128FormatToFourPacket((s + 8), k);
	packU128FormatToFourPacket((s + 12), (k + 16));
	for (i = 0; i < PR0_ROUNDS; i++) {
		ROUND512(i);
	}
	// process associated data
	//PAD(adlen, ad);
	if (adlen) {
		while (adlen >= aead_RATE) {
			Processing_Data(ad);
			for (i = 0; i < PR_ROUNDS; i++) {
				ROUND512(i);
			}
			adlen -= aead_RATE;
			ad += aead_RATE;
		}
		memset(tempData, 0, aead_RATE);
		memcpy(tempData, ad, adlen  );
		tempData[adlen] = 0x01;
		Processing_Data(tempData);
		for (i = 0; i < PR_ROUNDS; i++) {
			ROUND512(i);
		}
	}
	s[15] ^= 0x80000000;
	// process p data
	if (mlen) {
		while (mlen >= aead_RATE) {
			Processing_Data(m);
			unpackU128FormatToFourPacket(c, s);
			for (i = 0; i < PR_ROUNDS; i++) {
				ROUND512(i);
			}
			mlen -= aead_RATE;
			m += aead_RATE;
			c += aead_RATE;
		}
		memset(tempData, 0, aead_RATE);
		memcpy(tempData, m, mlen  );
		tempData[mlen] = 0x01;
		Processing_Data(tempData);
		unpackU128FormatToFourPacket(tempData, s);
		memcpy(c, tempData, mlen  );
		c += mlen;
	}
	// finalization
	for (i = 0; i < PRF_ROUNDS; i++) {
		ROUND512(i);
	}
	unpackU128FormatToFourPacket(tempU8, s);
	unpackU128FormatToFourPacket((tempU8 + 16), (s + 4));
	memcpy(c, tempU8, CRYPTO_ABYTES );
	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_temp[16] = { 0 };
	u32 t1, t2, t3, t5, t6, t8, t9, t11;
	u8 i;
	// initialization
	u32 s[16] = { 0 };
	u32 dataFormat_1[4] = { 0 };
	u32 dataFormat[4] = { 0 };
	u8 tempData[16] = { 0 };
	u8 tempU8[64] = { 0 };

	u32 t[4] = { 0 };
	if (clen < CRYPTO_ABYTES)
		return -1;
	*mlen = clen - CRYPTO_ABYTES;
	//initialization
	packU128FormatToFourPacket(s, npub);
	packU128FormatToFourPacket((s + 4), (npub + 16));
	packU128FormatToFourPacket((s + 8), k);
	packU128FormatToFourPacket((s + 12), (k + 16));
	for (i = 0; i < PR0_ROUNDS; i++) {
		ROUND512(i);
	}
	// process associated data
	if (adlen) {
		while (adlen >= aead_RATE) {
			Processing_Data(ad);
			for (i = 0; i < PR_ROUNDS; i++) {
				ROUND512(i);
			}
			adlen -= aead_RATE;
			ad += aead_RATE;
		}
		memset(tempData, 0, aead_RATE);
		memcpy(tempData, ad, adlen  );
		tempData[adlen] = 0x01;
		Processing_Data(tempData);
		for (i = 0; i < PR_ROUNDS; i++) {
			ROUND512(i);
		}
	}
	s[15] ^= 0x80000000;
	// process c data
	clen = clen - CRYPTO_KEYBYTES;
	if (clen) {
		while (clen >= aead_RATE) {
			packU128FormatToFourPacket(dataFormat, c);
			dataFormat_1[0] = s[0] ^ dataFormat[0];
			dataFormat_1[1] = s[1] ^ dataFormat[1];
			dataFormat_1[2] = s[2] ^ dataFormat[2];
			dataFormat_1[3] = s[3] ^ dataFormat[3];
			unpackU128FormatToFourPacket(m, dataFormat_1);
			s[0] = dataFormat[0];
			s[1] = dataFormat[1];
			s[2] = dataFormat[2];
			s[3] = dataFormat[3];
			for (i = 0; i < PR_ROUNDS; i++) {
				ROUND512(i);
			}
			clen -= aead_RATE;
			m += aead_RATE;
			c += aead_RATE;
		}
		unpackU128FormatToFourPacket(tempU8, s);
		memset(tempData, 0, aead_RATE);
		memcpy(tempData, c, clen  );
		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]);
		memcpy(m, tempU8, clen  );
		memcpy(tempU8, tempData, clen  );
		c += clen;
		tempU8[i] ^= 0x01;
		packU128FormatToFourPacket(s, tempU8);
	}
	// finalization
	for (i = 0; i < PRF_ROUNDS; i++) {
		ROUND512(i);

	}
	unpackU128FormatToFourPacket(tempU8, s);
	unpackU128FormatToFourPacket((tempU8 + 16), (s + 4));
	if (memcmp((void*) tempU8, (void*) c, CRYPTO_ABYTES)) {
		memset(m, 0,   (*mlen));
		*mlen = 0;
		return -1;
	}
	return 0;
}