encrypt.c 6.98 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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
/**
 * encrypt.c
 *
 * @version 1.0 (March 2019)
 *
 * Reference ANSI C code for the Qameleon memory encryption cipher
 *
 * @author Roberto Avanzi <roberto.avanzi@gmail.com>, <roberto.avanzi@arm.com>
 * @author Subhadeep Banik <subhadeep.banik@epfl.ch>
 * @author Francesco Regazzoni <regazzoni@alari.ch>
 *
 * This code is hereby placed in the public domain.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "api.h"
#include "crypto_aead.h"
#include <stdio.h>
#include <stdint.h>
#include <string.h>

#include "qarma128_11.h"

#define MSB_AD 			      (0x2<<4)
#define MSB_AD_LAST		      (0x3<<4)
#define MSB_M			      (0x0<<4)
#define MSB_MB			      (0x8<<4)
#define MSB_NE       		  (0xF<<4)
#define MSB_LAST              (0x1<<4)
#define MSB_LASTB             (0x9<<4)

#define MSB_CHKSUM            (0x4<<4)
#define MSB_CHKSUMB           (0xC<<4)
#define ROUNDS                11
#define TAGLENGTH             8

void 
set_nonce_in_tweak(u8 * tweak, const u8 * nonce)
{
	tweak[0] = (tweak[0] & 0xf0) ^ (nonce[0] >> 4);
	tweak[1] = (nonce[0] & 0xf) << 4 ^ (nonce[1] >> 4);
	tweak[2] = (nonce[1] & 0xf) << 4 ^ (nonce[2] >> 4);
	tweak[3] = (nonce[2] & 0xf) << 4 ^ (nonce[3] >> 4);
	tweak[4] = (nonce[3] & 0xf) << 4 ^ (nonce[4] >> 4);
	tweak[5] = (nonce[4] & 0xf) << 4 ^ (nonce[5] >> 4);
	tweak[6] = (nonce[5] & 0xf) << 4 ^ (nonce[6] >> 4);
	tweak[7] = (nonce[6] & 0xf) << 4 ^ (nonce[7] >> 4);
	tweak[8] = (nonce[7] & 0xf) << 4;
}

/*
 * * Modifiy the block number in the tweak value
 */
void 
set_block_number_in_tweak(u8 * tweak, const u64 block_no)
{
	tweak[8] = (tweak[8] & 0xf0) ^ ((block_no >> 56ULL) & 0xf);
	tweak[9] = ((block_no >> 48ULL) & 0xff);
	tweak[10] = ((block_no >> 40ULL) & 0xff);
	tweak[11] = ((block_no >> 32ULL) & 0xff);
	tweak[12] = ((block_no >> 24ULL) & 0xff);
	tweak[13] = ((block_no >> 16ULL) & 0xff);
	tweak[14] = ((block_no >> 8ULL) & 0xff);
	tweak[15] = ((block_no >> 0ULL) & 0xff);
}

/*
 * * Modifiy the stage value in the tweak value
 */
void 
set_stage_in_tweak(u8 * tweak, const u8 value)
{
	tweak[0] = (tweak[0] & 0xf) ^ value;
}

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 long long int i;
	unsigned long long int j;

	unsigned char	tweak[16], k0[16], w0[16], k1[16], k1_M[16], w1[16];

	unsigned char	last_block[16];
	unsigned char	Checksum[16];
	unsigned char	Final[16];

	unsigned char	Pad[16];

	(void)nsec;
	(void)ad;
	if (adlen != 0)
		return -1;

	for (i = 0; i < 16; i++) {

		w0[i] = k[i];
		k0[i] = k[16 + i];

	}

	for (i = 0; i < mlen + TAGLENGTH; i++) {
		c[i] = 0;
	}
	*clen = 0;
	for (j = 0; j < 16; j++)
		Checksum[j] = 0;
	KeySpecialisation(k0, w0, k1, k1_M, w1);

	for (j = 0; j < 16; j++)
		tweak[j] = 0;

	if (mlen) {
		/* Message */

		for (j = 0; j < 16; j++)
			tweak[j] = 0;
		set_nonce_in_tweak(tweak, npub);

		set_stage_in_tweak(tweak, MSB_M);

		i = 0;
		while (16 * (i + 1) < mlen) {

			for (j = 0; j < 16; j++)
				Checksum[j] ^= m[16 * i + j];
			set_block_number_in_tweak(tweak, i);

			qarma128(m + 16 * i, w0, w1, k0, k1, tweak, ROUNDS, c + 16 * i);

			i++;
		}

		/* Process last block */

		for (j = 0; j < 16; j++)
			last_block[j] = 0;

		for (j = 0; j < mlen - 16 * i; j++)
			last_block[j] = *(m + 16 * i + j);

		//xor_values(Checksum, last_block);
		for (j = 0; j < 16; j++)
			Checksum[j] ^= last_block[j];

		/* Encrypt it */

		set_stage_in_tweak(tweak, MSB_LAST);

		set_block_number_in_tweak(tweak, mlen);

		qarma128(last_block, w0, w1, k0, k1, tweak, ROUNDS, Pad);

		/* Write the ciphertext block */
		for (j = 0; j < 16; j++) {

			c[16 * i + j] = Pad[j];
		}
		*clen = 16 * i + 16;
	}			/* if message length>0 */
	set_stage_in_tweak(tweak, MSB_CHKSUM);

	set_nonce_in_tweak(tweak, npub);
	set_block_number_in_tweak(tweak, 0);

	qarma128(Checksum, w0, w1, k0, k1, tweak, ROUNDS, Final);

	for (i = 0; i < TAGLENGTH; i++) {
		c[*clen + i] = Final[i];

	}
	*clen = *clen + TAGLENGTH;
	return 0;
}

int 
crypto_aead_decrypt(
		    unsigned char *m, unsigned long long *outputmlen,
		    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 char	tweak[16], k0[16], w0[16], k1[16], k1_M[16], w1[16],
			w0d           [16], k0d[16], w1d[16];

	unsigned long long int i;
	unsigned long long int j;

	unsigned char	Checksum[16];
	unsigned char	Final[16];

	unsigned char	Pad[16];
	unsigned char	Tag[TAGLENGTH], Tagc[TAGLENGTH];

	(void)nsec;
	(void)ad;
	if (adlen != 0)
		return -1;
	if (clen % 64 != TAGLENGTH)
		return -1;

	for (i = 0; i < 16; i++) {

		w0[i] = k[i];
		k0[i] = k[16 + i];

	}

	KeySpecialisation_dec(k0, w0, k1, k1_M, w1, w0d, w1d, k0d);

	/* Get the tag from the last 16 bytes of the ciphertext */
	memcpy(Tag, c + clen - TAGLENGTH, TAGLENGTH);

	/*
	 * Update c_len to the actual size of the ciphertext (i.e., without
	 * the tag)
	 */
	*outputmlen = clen - TAGLENGTH;
	clen -= TAGLENGTH;

	/* Fill the tweak from nonce */
	memset(tweak, 0, 16);

	for (j = 0; j < 16; j++)
		Checksum[j] = 0;
	i = 0;

	if (clen) {
		/* Message */

		for (j = 0; j < 16; j++)
			tweak[j] = 0;
		set_nonce_in_tweak(tweak, npub);

		set_stage_in_tweak(tweak, MSB_M);

		i = 0;

		while (16 * (i + 1) < clen) {

			set_block_number_in_tweak(tweak, i);

			qarma128(c + 16 * i, w0d, w1d, k0d, k1_M, tweak, ROUNDS, m + 16 * i);

			for (j = 0; j < 16; j++)
				Checksum[j] ^= m[16 * i + j];

			i++;
		}

		/* Process last block */

		/* Encrypt it */

		set_stage_in_tweak(tweak, MSB_LAST);

		set_block_number_in_tweak(tweak, *outputmlen);

		qarma128(c + 16 * i, w0d, w1d, k0d, k1_M, tweak, ROUNDS, Pad);

		for (j = 0; j < 16; j++)
			Checksum[j] ^= Pad[j];

		/* Write the ciphertext block */
		for (j = 0; j < *outputmlen - 16 * i; j++) {

			m[16 * i + j] = Pad[j];
		}

	}
	/* if message length>0 */

	set_stage_in_tweak(tweak, MSB_CHKSUM);

	set_nonce_in_tweak(tweak, npub);
	set_block_number_in_tweak(tweak, 0);

	qarma128(Checksum, w0, w1, k0, k1, tweak, ROUNDS, Final);
	for (j = 0; j < TAGLENGTH; j++)
		Tagc[j] = Final[j];

	if (0 != memcmp(Tagc, Tag, TAGLENGTH)) {
		memset(m, 0, clen);
		return -1;

	}
	return 0;

}