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