xref: /openssh-portable/cipher-aesctr.c (revision a47ead7c)
160c2c4eaSmarkus@openbsd.org /* $OpenBSD: cipher-aesctr.c,v 1.2 2015/01/14 10:24:42 markus Exp $ */
21f0311c7SDamien Miller /*
360c2c4eaSmarkus@openbsd.org  * Copyright (c) 2003 Markus Friedl.  All rights reserved.
41f0311c7SDamien Miller  *
51f0311c7SDamien Miller  * Permission to use, copy, modify, and distribute this software for any
61f0311c7SDamien Miller  * purpose with or without fee is hereby granted, provided that the above
71f0311c7SDamien Miller  * copyright notice and this permission notice appear in all copies.
81f0311c7SDamien Miller  *
91f0311c7SDamien Miller  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
101f0311c7SDamien Miller  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
111f0311c7SDamien Miller  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
121f0311c7SDamien Miller  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
131f0311c7SDamien Miller  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
141f0311c7SDamien Miller  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
151f0311c7SDamien Miller  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
161f0311c7SDamien Miller  */
171f0311c7SDamien Miller 
18*a47ead7cSDarren Tucker #include "includes.h"
19*a47ead7cSDarren Tucker 
201f0311c7SDamien Miller #include <sys/types.h>
211f0311c7SDamien Miller #include <string.h>
221f0311c7SDamien Miller 
2372ef7c14SDamien Miller #ifndef WITH_OPENSSL
2472ef7c14SDamien Miller 
251f0311c7SDamien Miller #include "cipher-aesctr.h"
261f0311c7SDamien Miller 
271f0311c7SDamien Miller /*
281f0311c7SDamien Miller  * increment counter 'ctr',
291f0311c7SDamien Miller  * the counter is of size 'len' bytes and stored in network-byte-order.
301f0311c7SDamien Miller  * (LSB at ctr[len-1], MSB at ctr[0])
311f0311c7SDamien Miller  */
321221b220STim Rice static inline void
aesctr_inc(u8 * ctr,u32 len)331f0311c7SDamien Miller aesctr_inc(u8 *ctr, u32 len)
341f0311c7SDamien Miller {
351f0311c7SDamien Miller 	ssize_t i;
361f0311c7SDamien Miller 
371f0311c7SDamien Miller #ifndef CONSTANT_TIME_INCREMENT
381f0311c7SDamien Miller 	for (i = len - 1; i >= 0; i--)
391f0311c7SDamien Miller 		if (++ctr[i])	/* continue on overflow */
401f0311c7SDamien Miller 			return;
411f0311c7SDamien Miller #else
421f0311c7SDamien Miller 	u8 x, add = 1;
431f0311c7SDamien Miller 
441f0311c7SDamien Miller 	for (i = len - 1; i >= 0; i--) {
451f0311c7SDamien Miller 		ctr[i] += add;
461f0311c7SDamien Miller 		/* constant time for: x = ctr[i] ? 1 : 0 */
471f0311c7SDamien Miller 		x = ctr[i];
481f0311c7SDamien Miller 		x = (x | (x >> 4)) & 0xf;
491f0311c7SDamien Miller 		x = (x | (x >> 2)) & 0x3;
501f0311c7SDamien Miller 		x = (x | (x >> 1)) & 0x1;
511f0311c7SDamien Miller 		add *= (x^1);
521f0311c7SDamien Miller 	}
531f0311c7SDamien Miller #endif
541f0311c7SDamien Miller }
551f0311c7SDamien Miller 
561f0311c7SDamien Miller void
aesctr_keysetup(aesctr_ctx * x,const u8 * k,u32 kbits,u32 ivbits)571f0311c7SDamien Miller aesctr_keysetup(aesctr_ctx *x,const u8 *k,u32 kbits,u32 ivbits)
581f0311c7SDamien Miller {
591f0311c7SDamien Miller 	x->rounds = rijndaelKeySetupEnc(x->ek, k, kbits);
601f0311c7SDamien Miller }
611f0311c7SDamien Miller 
621f0311c7SDamien Miller void
aesctr_ivsetup(aesctr_ctx * x,const u8 * iv)631f0311c7SDamien Miller aesctr_ivsetup(aesctr_ctx *x,const u8 *iv)
641f0311c7SDamien Miller {
651f0311c7SDamien Miller 	memcpy(x->ctr, iv, AES_BLOCK_SIZE);
661f0311c7SDamien Miller }
671f0311c7SDamien Miller 
681f0311c7SDamien Miller void
aesctr_encrypt_bytes(aesctr_ctx * x,const u8 * m,u8 * c,u32 bytes)691f0311c7SDamien Miller aesctr_encrypt_bytes(aesctr_ctx *x,const u8 *m,u8 *c,u32 bytes)
701f0311c7SDamien Miller {
711f0311c7SDamien Miller 	u32 n = 0;
721f0311c7SDamien Miller 	u8 buf[AES_BLOCK_SIZE];
731f0311c7SDamien Miller 
741f0311c7SDamien Miller 	while ((bytes--) > 0) {
751f0311c7SDamien Miller 		if (n == 0) {
761f0311c7SDamien Miller 			rijndaelEncrypt(x->ek, x->rounds, x->ctr, buf);
771f0311c7SDamien Miller 			aesctr_inc(x->ctr, AES_BLOCK_SIZE);
781f0311c7SDamien Miller 		}
791f0311c7SDamien Miller 		*(c++) = *(m++) ^ buf[n];
801f0311c7SDamien Miller 		n = (n + 1) % AES_BLOCK_SIZE;
811f0311c7SDamien Miller 	}
821f0311c7SDamien Miller }
8372ef7c14SDamien Miller #endif /* !WITH_OPENSSL */
84