xref: /openbsd-src/usr.bin/ssh/cipher-aesctr.c (revision 909269cf8ff51125b580e2b6d8a8793eda219563)
1*909269cfSmarkus /* $OpenBSD: cipher-aesctr.c,v 1.2 2015/01/14 10:24:42 markus Exp $ */
250ee004fSmarkus /*
3*909269cfSmarkus  * Copyright (c) 2003 Markus Friedl.  All rights reserved.
450ee004fSmarkus  *
550ee004fSmarkus  * Permission to use, copy, modify, and distribute this software for any
650ee004fSmarkus  * purpose with or without fee is hereby granted, provided that the above
750ee004fSmarkus  * copyright notice and this permission notice appear in all copies.
850ee004fSmarkus  *
950ee004fSmarkus  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1050ee004fSmarkus  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1150ee004fSmarkus  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1250ee004fSmarkus  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1350ee004fSmarkus  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1450ee004fSmarkus  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1550ee004fSmarkus  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1650ee004fSmarkus  */
1750ee004fSmarkus 
1850ee004fSmarkus #include <sys/types.h>
1950ee004fSmarkus #include <string.h>
2050ee004fSmarkus 
2150ee004fSmarkus #include "cipher-aesctr.h"
2250ee004fSmarkus 
2350ee004fSmarkus /*
2450ee004fSmarkus  * increment counter 'ctr',
2550ee004fSmarkus  * the counter is of size 'len' bytes and stored in network-byte-order.
2650ee004fSmarkus  * (LSB at ctr[len-1], MSB at ctr[0])
2750ee004fSmarkus  */
2850ee004fSmarkus static __inline__ void
aesctr_inc(u8 * ctr,u32 len)2950ee004fSmarkus aesctr_inc(u8 *ctr, u32 len)
3050ee004fSmarkus {
3150ee004fSmarkus 	ssize_t i;
3250ee004fSmarkus 
3350ee004fSmarkus #ifndef CONSTANT_TIME_INCREMENT
3450ee004fSmarkus 	for (i = len - 1; i >= 0; i--)
3550ee004fSmarkus 		if (++ctr[i])	/* continue on overflow */
3650ee004fSmarkus 			return;
3750ee004fSmarkus #else
3850ee004fSmarkus 	u8 x, add = 1;
3950ee004fSmarkus 
4050ee004fSmarkus 	for (i = len - 1; i >= 0; i--) {
4150ee004fSmarkus 		ctr[i] += add;
4250ee004fSmarkus 		/* constant time for: x = ctr[i] ? 1 : 0 */
4350ee004fSmarkus 		x = ctr[i];
4450ee004fSmarkus 		x = (x | (x >> 4)) & 0xf;
4550ee004fSmarkus 		x = (x | (x >> 2)) & 0x3;
4650ee004fSmarkus 		x = (x | (x >> 1)) & 0x1;
4750ee004fSmarkus 		add *= (x^1);
4850ee004fSmarkus 	}
4950ee004fSmarkus #endif
5050ee004fSmarkus }
5150ee004fSmarkus 
5250ee004fSmarkus void
aesctr_keysetup(aesctr_ctx * x,const u8 * k,u32 kbits,u32 ivbits)5350ee004fSmarkus aesctr_keysetup(aesctr_ctx *x,const u8 *k,u32 kbits,u32 ivbits)
5450ee004fSmarkus {
5550ee004fSmarkus 	x->rounds = rijndaelKeySetupEnc(x->ek, k, kbits);
5650ee004fSmarkus }
5750ee004fSmarkus 
5850ee004fSmarkus void
aesctr_ivsetup(aesctr_ctx * x,const u8 * iv)5950ee004fSmarkus aesctr_ivsetup(aesctr_ctx *x,const u8 *iv)
6050ee004fSmarkus {
6150ee004fSmarkus 	memcpy(x->ctr, iv, AES_BLOCK_SIZE);
6250ee004fSmarkus }
6350ee004fSmarkus 
6450ee004fSmarkus void
aesctr_encrypt_bytes(aesctr_ctx * x,const u8 * m,u8 * c,u32 bytes)6550ee004fSmarkus aesctr_encrypt_bytes(aesctr_ctx *x,const u8 *m,u8 *c,u32 bytes)
6650ee004fSmarkus {
6750ee004fSmarkus 	u32 n = 0;
6850ee004fSmarkus 	u8 buf[AES_BLOCK_SIZE];
6950ee004fSmarkus 
7050ee004fSmarkus 	while ((bytes--) > 0) {
7150ee004fSmarkus 		if (n == 0) {
7250ee004fSmarkus 			rijndaelEncrypt(x->ek, x->rounds, x->ctr, buf);
7350ee004fSmarkus 			aesctr_inc(x->ctr, AES_BLOCK_SIZE);
7450ee004fSmarkus 		}
7550ee004fSmarkus 		*(c++) = *(m++) ^ buf[n];
7650ee004fSmarkus 		n = (n + 1) % AES_BLOCK_SIZE;
7750ee004fSmarkus 	}
7850ee004fSmarkus }
79