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