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