xref: /dflybsd-src/crypto/openssh/cipher-aesctr.c (revision ba1276acd1c8c22d225b1bcf370a14c878644f44)
1*ba1276acSMatthew Dillon /* $OpenBSD: cipher-aesctr.c,v 1.2 2015/01/14 10:24:42 markus Exp $ */
2*ba1276acSMatthew Dillon /*
3*ba1276acSMatthew Dillon  * Copyright (c) 2003 Markus Friedl.  All rights reserved.
4*ba1276acSMatthew Dillon  *
5*ba1276acSMatthew Dillon  * Permission to use, copy, modify, and distribute this software for any
6*ba1276acSMatthew Dillon  * purpose with or without fee is hereby granted, provided that the above
7*ba1276acSMatthew Dillon  * copyright notice and this permission notice appear in all copies.
8*ba1276acSMatthew Dillon  *
9*ba1276acSMatthew Dillon  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10*ba1276acSMatthew Dillon  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*ba1276acSMatthew Dillon  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12*ba1276acSMatthew Dillon  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*ba1276acSMatthew Dillon  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*ba1276acSMatthew Dillon  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15*ba1276acSMatthew Dillon  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16*ba1276acSMatthew Dillon  */
17*ba1276acSMatthew Dillon 
18*ba1276acSMatthew Dillon #include "includes.h"
19*ba1276acSMatthew Dillon 
20*ba1276acSMatthew Dillon #include <sys/types.h>
21*ba1276acSMatthew Dillon #include <string.h>
22*ba1276acSMatthew Dillon 
23*ba1276acSMatthew Dillon #ifndef WITH_OPENSSL
24*ba1276acSMatthew Dillon 
25*ba1276acSMatthew Dillon #include "cipher-aesctr.h"
26*ba1276acSMatthew Dillon 
27*ba1276acSMatthew Dillon /*
28*ba1276acSMatthew Dillon  * increment counter 'ctr',
29*ba1276acSMatthew Dillon  * the counter is of size 'len' bytes and stored in network-byte-order.
30*ba1276acSMatthew Dillon  * (LSB at ctr[len-1], MSB at ctr[0])
31*ba1276acSMatthew Dillon  */
32*ba1276acSMatthew Dillon static inline void
aesctr_inc(u8 * ctr,u32 len)33*ba1276acSMatthew Dillon aesctr_inc(u8 *ctr, u32 len)
34*ba1276acSMatthew Dillon {
35*ba1276acSMatthew Dillon 	ssize_t i;
36*ba1276acSMatthew Dillon 
37*ba1276acSMatthew Dillon #ifndef CONSTANT_TIME_INCREMENT
38*ba1276acSMatthew Dillon 	for (i = len - 1; i >= 0; i--)
39*ba1276acSMatthew Dillon 		if (++ctr[i])	/* continue on overflow */
40*ba1276acSMatthew Dillon 			return;
41*ba1276acSMatthew Dillon #else
42*ba1276acSMatthew Dillon 	u8 x, add = 1;
43*ba1276acSMatthew Dillon 
44*ba1276acSMatthew Dillon 	for (i = len - 1; i >= 0; i--) {
45*ba1276acSMatthew Dillon 		ctr[i] += add;
46*ba1276acSMatthew Dillon 		/* constant time for: x = ctr[i] ? 1 : 0 */
47*ba1276acSMatthew Dillon 		x = ctr[i];
48*ba1276acSMatthew Dillon 		x = (x | (x >> 4)) & 0xf;
49*ba1276acSMatthew Dillon 		x = (x | (x >> 2)) & 0x3;
50*ba1276acSMatthew Dillon 		x = (x | (x >> 1)) & 0x1;
51*ba1276acSMatthew Dillon 		add *= (x^1);
52*ba1276acSMatthew Dillon 	}
53*ba1276acSMatthew Dillon #endif
54*ba1276acSMatthew Dillon }
55*ba1276acSMatthew Dillon 
56*ba1276acSMatthew Dillon void
aesctr_keysetup(aesctr_ctx * x,const u8 * k,u32 kbits,u32 ivbits)57*ba1276acSMatthew Dillon aesctr_keysetup(aesctr_ctx *x,const u8 *k,u32 kbits,u32 ivbits)
58*ba1276acSMatthew Dillon {
59*ba1276acSMatthew Dillon 	x->rounds = rijndaelKeySetupEnc(x->ek, k, kbits);
60*ba1276acSMatthew Dillon }
61*ba1276acSMatthew Dillon 
62*ba1276acSMatthew Dillon void
aesctr_ivsetup(aesctr_ctx * x,const u8 * iv)63*ba1276acSMatthew Dillon aesctr_ivsetup(aesctr_ctx *x,const u8 *iv)
64*ba1276acSMatthew Dillon {
65*ba1276acSMatthew Dillon 	memcpy(x->ctr, iv, AES_BLOCK_SIZE);
66*ba1276acSMatthew Dillon }
67*ba1276acSMatthew Dillon 
68*ba1276acSMatthew Dillon void
aesctr_encrypt_bytes(aesctr_ctx * x,const u8 * m,u8 * c,u32 bytes)69*ba1276acSMatthew Dillon aesctr_encrypt_bytes(aesctr_ctx *x,const u8 *m,u8 *c,u32 bytes)
70*ba1276acSMatthew Dillon {
71*ba1276acSMatthew Dillon 	u32 n = 0;
72*ba1276acSMatthew Dillon 	u8 buf[AES_BLOCK_SIZE];
73*ba1276acSMatthew Dillon 
74*ba1276acSMatthew Dillon 	while ((bytes--) > 0) {
75*ba1276acSMatthew Dillon 		if (n == 0) {
76*ba1276acSMatthew Dillon 			rijndaelEncrypt(x->ek, x->rounds, x->ctr, buf);
77*ba1276acSMatthew Dillon 			aesctr_inc(x->ctr, AES_BLOCK_SIZE);
78*ba1276acSMatthew Dillon 		}
79*ba1276acSMatthew Dillon 		*(c++) = *(m++) ^ buf[n];
80*ba1276acSMatthew Dillon 		n = (n + 1) % AES_BLOCK_SIZE;
81*ba1276acSMatthew Dillon 	}
82*ba1276acSMatthew Dillon }
83*ba1276acSMatthew Dillon #endif /* !WITH_OPENSSL */
84