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