cipher-aesctr.c revision 294332
1193323Sed/* $OpenBSD: cipher-aesctr.c,v 1.2 2015/01/14 10:24:42 markus Exp $ */
2193323Sed/*
3193323Sed * Copyright (c) 2003 Markus Friedl.  All rights reserved.
4193323Sed *
5193323Sed * Permission to use, copy, modify, and distribute this software for any
6193323Sed * purpose with or without fee is hereby granted, provided that the above
7193323Sed * copyright notice and this permission notice appear in all copies.
8193323Sed *
9193323Sed * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10193323Sed * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11193323Sed * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12193323Sed * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13193323Sed * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14193323Sed * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15193323Sed * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16193323Sed */
17193323Sed
18193323Sed#include "includes.h"
19193323Sed
20199481Srdivacky#include <sys/types.h>
21198090Srdivacky#include <string.h>
22193323Sed
23193323Sed#ifndef WITH_OPENSSL
24198090Srdivacky
25218893Sdim#include "cipher-aesctr.h"
26218893Sdim
27218893Sdim/*
28198090Srdivacky * increment counter 'ctr',
29198090Srdivacky * the counter is of size 'len' bytes and stored in network-byte-order.
30198090Srdivacky * (LSB at ctr[len-1], MSB at ctr[0])
31198090Srdivacky */
32198090Srdivackystatic inline void
33198090Srdivackyaesctr_inc(u8 *ctr, u32 len)
34198090Srdivacky{
35193323Sed	ssize_t i;
36193323Sed
37193323Sed#ifndef CONSTANT_TIME_INCREMENT
38193323Sed	for (i = len - 1; i >= 0; i--)
39193323Sed		if (++ctr[i])	/* continue on overflow */
40193323Sed			return;
41193323Sed#else
42199989Srdivacky	u8 x, add = 1;
43193323Sed
44193323Sed	for (i = len - 1; i >= 0; i--) {
45193323Sed		ctr[i] += add;
46193323Sed		/* constant time for: x = ctr[i] ? 1 : 0 */
47226633Sdim		x = ctr[i];
48193323Sed		x = (x | (x >> 4)) & 0xf;
49193323Sed		x = (x | (x >> 2)) & 0x3;
50193323Sed		x = (x | (x >> 1)) & 0x1;
51193323Sed		add *= (x^1);
52193323Sed	}
53193323Sed#endif
54193323Sed}
55193323Sed
56193323Sedvoid
57193323Sedaesctr_keysetup(aesctr_ctx *x,const u8 *k,u32 kbits,u32 ivbits)
58199989Srdivacky{
59193323Sed	x->rounds = rijndaelKeySetupEnc(x->ek, k, kbits);
60234353Sdim}
61234353Sdim
62234353Sdimvoid
63193323Sedaesctr_ivsetup(aesctr_ctx *x,const u8 *iv)
64193323Sed{
65193323Sed	memcpy(x->ctr, iv, AES_BLOCK_SIZE);
66193323Sed}
67193323Sed
68193323Sedvoid
69226633Sdimaesctr_encrypt_bytes(aesctr_ctx *x,const u8 *m,u8 *c,u32 bytes)
70193323Sed{
71193323Sed	u32 n = 0;
72193323Sed	u8 buf[AES_BLOCK_SIZE];
73193323Sed
74193323Sed	while ((bytes--) > 0) {
75193323Sed		if (n == 0) {
76193323Sed			rijndaelEncrypt(x->ek, x->rounds, x->ctr, buf);
77193323Sed			aesctr_inc(x->ctr, AES_BLOCK_SIZE);
78193323Sed		}
79193323Sed		*(c++) = *(m++) ^ buf[n];
80193323Sed		n = (n + 1) % AES_BLOCK_SIZE;
81193323Sed	}
82193323Sed}
83193323Sed#endif /* !WITH_OPENSSL */
84193323Sed