encrypt.c 5.23 KB
Newer Older
lwc-tester 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
#include <string.h>
#include <stdio.h>
#include "crypto_aead.h"

//Constant
const unsigned int CONSTANT[20] =
{ 0x14f1c272,0x3279c419,0x4b8ea41d,0x0cc80863,
	0xd28062e1,0xe71d3dda,0xe3c4d158,0xa7f067ac,
	0x94935056,0x8ee5c63d,0xf5a0cec3,0xd33da5a7,
	0x7de892ac,0xe8fd9b12,0xfb625a84,0xf15a5323,
	0xd93d3995,0x9a485a71,0xdab8ecd1,0x9d9b3e2e };

//256-bit State
unsigned char S[256];
unsigned char a, b;

//Two Sboxes
#define SB (S[30]&S[29])^(S[96]&S[88])^(S[159]&S[132])^(S[207]&S[206])^1
#define SB1 (S[30]&S[132])^(S[29]&S[206])^(S[96]&S[207])^(S[159]&S[88])

#define ADDa { S[63]^=a; S[191]^=a; }
#define ADDb { S[127]^=b; S[255]^=b; }

//Core step
void CoreStep()
{
	a = SB;
	b = (SB1 ^ S[32]) & 1;

	unsigned char f[4];

	f[0] = S[0] ^ S[31] ^ S[32] ^ S[77];
	f[1] = S[64] ^ S[92] ^ S[94] ^ S[129];
	f[2] = S[128] ^ S[150] ^ S[155] ^ S[218];
	f[3] = S[192] ^ S[200] ^ S[211] ^ S[31];

	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 63; j++)
		{
			S[64 * i + j] = S[64 * i + j + 1];
		}
	}

	S[63] = f[0];
	S[127] = f[1];
	S[191] = f[2];
	S[255] = f[3];
}

//Initinalization and finilation process by bit
void InitBit(unsigned char ivbit)
{
	CoreStep();

	a ^= ivbit;

	ADDa;
	ADDb;
}

//Initinalization and finilation process by byte
void InitByte(unsigned char ivbyte)
{
	unsigned char ivbit;
	for (int i = 0; i < 8; i++)
	{
		ivbit = (ivbyte >> i) & 1;
		InitBit(ivbit);
	}
}

//Encrypt by bit
void EncryptBit(unsigned char plaintextbit, unsigned char *ciphertextbit)
{
	CoreStep();

	*ciphertextbit = b ^ plaintextbit;
	a ^= plaintextbit;

	ADDa;
}

//Encrypt by byte
void EncryptByte(unsigned char plaintextbyte, unsigned char *ciphertextbyte)
{
	*ciphertextbyte = 0;
	unsigned char plaintextbit, ciphertextbit;
	for (int i = 0; i < 8; i++)
	{
		plaintextbit = (plaintextbyte >> i) & 1;
		EncryptBit(plaintextbit, &ciphertextbit);
		*ciphertextbyte |= (ciphertextbit << i);
	}
}

//Decrypt by bit
void DecryptBit(unsigned char *plaintextbit, unsigned char ciphertextbit)
{
	CoreStep();

	*plaintextbit = b ^ ciphertextbit;
	a ^= *plaintextbit;

	ADDa;
}

//Decrypt by byte
void DecryptByte(unsigned char *plaintextbyte, unsigned char ciphertextbyte)
{
	*plaintextbyte = 0;
	unsigned char plaintextbit, ciphertextbit;
	for (int i = 0; i < 8; i++)
	{
		ciphertextbit = (ciphertextbyte >> i) & 1;
		DecryptBit(&plaintextbit, ciphertextbit);
		*plaintextbyte |= (plaintextbit << i);
	}
}

//Load constants and key to the state
void Load(const unsigned char *key)
{
	for (int rc = 0; rc < 4; rc++)
	{
		for (int i = 0; i < 8; i++)
		{
			unsigned char cbit = (CONSTANT[rc] >> (31 - i)) & 1;
			S[i + rc * 64] = cbit;
		}
		for (int i = 0; i < 3; i++)
		{
			for (int j = 0; j < 8; j++)
			{
				unsigned char cbit = (CONSTANT[rc] >> (23 - 8 * i - j)) & 1;
				S[i * 8 + j + 40 + rc * 64] = cbit;
			}
		}
	}

	for (int i = 0; i < 16; i++)
	{
		for (unsigned char j = 0; j < 8; j++)
		{
			unsigned char keybit = (key[i] >> (7 - j)) & 1;
			S[8 * (i % 4) + j + (i / 4) * 64 + 8] = keybit;
		}
	}
}

//Encrypt a message
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
)
{
	//Load
	Load(k);

	//Process IV
	for (int i = 0; i < 16; i++)
	{
		InitByte(npub[i]);
	}

	//Process associated data
	for (unsigned long long i = 0; i < adlen; i++)
	{
		InitByte(ad[i]);
	}

	//512 steps 0-stream
	for (int i = 0; i < 512 / 8; i++)
	{
		InitByte(0);
	}

	//Process message
	for (unsigned long long i = 0; i < mlen; i++)
	{
		EncryptByte(m[i], &c[i]);
	}

	//512 steps 0-stream
	for (int i = 0; i < 512 / 8; i++)
	{
		InitByte(0);
	}

	unsigned char tag[16];
	//Tag generation
	for (int i = 0; i < 128 / 8; i++)
	{
		EncryptByte(0, &tag[i]);
	}

	//Tag is appended to ciphertext
	*clen = mlen + 16;
	memcpy(c + mlen, tag, 16);

	return 0;
}

//Decrypt a ciphertext
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
)
{
	//Ciphertext length is less than 128-bit
	if (clen < 16)
	{
		return -1;
	}

	//Load
	Load(k);

	//Process IV
	for (int i = 0; i < 16; i++)
	{
		InitByte(npub[i]);
	}

	//Processage associated data
	for (unsigned long long i = 0; i < adlen; i++)
	{
		InitByte(ad[i]);
	}

	//512 steps 0-stream
	for (int i = 0; i < 512 / 8; i++)
	{
		InitByte(0);
	}

	//Process ciphertext
	*mlen = clen - 16;
	for (unsigned long long i = 0; i < *mlen; i++)
	{
		DecryptByte(&m[i], c[i]);
	}

	//512 steps 0-stream
	for (int i = 0; i < 512 / 8; i++)
	{
		InitByte(0);
	}

	unsigned char tag[16];
	//Tag generation
	for (int i = 0; i < 128 / 8; i++)
	{
		EncryptByte(0, &tag[i]);
	}

	unsigned char check = 0;
	//Check authentication
	for (int i = 0; i < 16; i++)
	{
		check |= tag[i] ^ c[clen - 16 + i];
	}

	if (check == 0)
	{
		return 0;
	}
	else
	{
		return -1;
	}
}