xref: /netbsd-src/crypto/external/bsd/openssh/dist/cipher-aesctr.c (revision ffae97bbe8f2584baf41881dffd63be2de01b43c)
1*ffae97bbSchristos /*	$NetBSD: cipher-aesctr.c,v 1.2 2018/04/06 18:59:00 christos Exp $	*/
2e161120fSchristos /* $OpenBSD: cipher-aesctr.c,v 1.2 2015/01/14 10:24:42 markus Exp $ */
35484a5efSchristos /*
4e161120fSchristos  * Copyright (c) 2003 Markus Friedl.  All rights reserved.
55484a5efSchristos  *
65484a5efSchristos  * Permission to use, copy, modify, and distribute this software for any
75484a5efSchristos  * purpose with or without fee is hereby granted, provided that the above
85484a5efSchristos  * copyright notice and this permission notice appear in all copies.
95484a5efSchristos  *
105484a5efSchristos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
115484a5efSchristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
125484a5efSchristos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
135484a5efSchristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
145484a5efSchristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
155484a5efSchristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
165484a5efSchristos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
175484a5efSchristos  */
18*ffae97bbSchristos #include "includes.h"
19*ffae97bbSchristos __RCSID("$NetBSD: cipher-aesctr.c,v 1.2 2018/04/06 18:59:00 christos Exp $");
205484a5efSchristos 
215484a5efSchristos #include <sys/types.h>
225484a5efSchristos #include <string.h>
235484a5efSchristos 
245484a5efSchristos #include "cipher-aesctr.h"
255484a5efSchristos 
265484a5efSchristos /*
275484a5efSchristos  * increment counter 'ctr',
285484a5efSchristos  * the counter is of size 'len' bytes and stored in network-byte-order.
295484a5efSchristos  * (LSB at ctr[len-1], MSB at ctr[0])
305484a5efSchristos  */
315484a5efSchristos static __inline__ void
aesctr_inc(u8 * ctr,u32 len)325484a5efSchristos aesctr_inc(u8 *ctr, u32 len)
335484a5efSchristos {
345484a5efSchristos 	ssize_t i;
355484a5efSchristos 
365484a5efSchristos #ifndef CONSTANT_TIME_INCREMENT
375484a5efSchristos 	for (i = len - 1; i >= 0; i--)
385484a5efSchristos 		if (++ctr[i])	/* continue on overflow */
395484a5efSchristos 			return;
405484a5efSchristos #else
415484a5efSchristos 	u8 x, add = 1;
425484a5efSchristos 
435484a5efSchristos 	for (i = len - 1; i >= 0; i--) {
445484a5efSchristos 		ctr[i] += add;
455484a5efSchristos 		/* constant time for: x = ctr[i] ? 1 : 0 */
465484a5efSchristos 		x = ctr[i];
475484a5efSchristos 		x = (x | (x >> 4)) & 0xf;
485484a5efSchristos 		x = (x | (x >> 2)) & 0x3;
495484a5efSchristos 		x = (x | (x >> 1)) & 0x1;
505484a5efSchristos 		add *= (x^1);
515484a5efSchristos 	}
525484a5efSchristos #endif
535484a5efSchristos }
545484a5efSchristos 
555484a5efSchristos void
aesctr_keysetup(aesctr_ctx * x,const u8 * k,u32 kbits,u32 ivbits)565484a5efSchristos aesctr_keysetup(aesctr_ctx *x,const u8 *k,u32 kbits,u32 ivbits)
575484a5efSchristos {
585484a5efSchristos 	x->rounds = rijndaelKeySetupEnc(x->ek, k, kbits);
595484a5efSchristos }
605484a5efSchristos 
615484a5efSchristos void
aesctr_ivsetup(aesctr_ctx * x,const u8 * iv)625484a5efSchristos aesctr_ivsetup(aesctr_ctx *x,const u8 *iv)
635484a5efSchristos {
645484a5efSchristos 	memcpy(x->ctr, iv, AES_BLOCK_SIZE);
655484a5efSchristos }
665484a5efSchristos 
675484a5efSchristos void
aesctr_encrypt_bytes(aesctr_ctx * x,const u8 * m,u8 * c,u32 bytes)685484a5efSchristos aesctr_encrypt_bytes(aesctr_ctx *x,const u8 *m,u8 *c,u32 bytes)
695484a5efSchristos {
705484a5efSchristos 	u32 n = 0;
715484a5efSchristos 	u8 buf[AES_BLOCK_SIZE];
725484a5efSchristos 
735484a5efSchristos 	while ((bytes--) > 0) {
745484a5efSchristos 		if (n == 0) {
755484a5efSchristos 			rijndaelEncrypt(x->ek, x->rounds, x->ctr, buf);
765484a5efSchristos 			aesctr_inc(x->ctr, AES_BLOCK_SIZE);
775484a5efSchristos 		}
785484a5efSchristos 		*(c++) = *(m++) ^ buf[n];
795484a5efSchristos 		n = (n + 1) % AES_BLOCK_SIZE;
805484a5efSchristos 	}
815484a5efSchristos }
82