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