1 /*
2  * Copyright (c) 2012 Vincent Hanquez <vincent@snarc.org>
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the author nor the names of his contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #ifndef BLOCK128_H
32 #define BLOCK128_H
33 
34 #include "bitfn.h"
35 
36 typedef union {
37        uint64_t q[2];
38        uint32_t d[4];
39        uint16_t w[8];
40        uint8_t  b[16];
41 } block128;
42 
block128_copy_bytes(block128 * block,uint8_t * src,uint32_t len)43 static inline void block128_copy_bytes(block128 *block, uint8_t *src, uint32_t len)
44 {
45 	int i;
46 	for (i = 0; i < len; i++) block->b[i] = src[i];
47 }
48 
block128_copy(block128 * d,const block128 * s)49 static inline void block128_copy(block128 *d, const block128 *s)
50 {
51 	d->q[0] = s->q[0]; d->q[1] = s->q[1];
52 }
53 
block128_zero(block128 * d)54 static inline void block128_zero(block128 *d)
55 {
56 	d->q[0] = 0; d->q[1] = 0;
57 }
58 
block128_xor(block128 * d,const block128 * s)59 static inline void block128_xor(block128 *d, const block128 *s)
60 {
61 	d->q[0] ^= s->q[0];
62 	d->q[1] ^= s->q[1];
63 }
64 
block128_vxor(block128 * d,const block128 * s1,const block128 * s2)65 static inline void block128_vxor(block128 *d, const block128 *s1, const block128 *s2)
66 {
67 	d->q[0] = s1->q[0] ^ s2->q[0];
68 	d->q[1] = s1->q[1] ^ s2->q[1];
69 }
70 
block128_xor_bytes(block128 * block,uint8_t * src,uint32_t len)71 static inline void block128_xor_bytes(block128 *block, uint8_t *src, uint32_t len)
72 {
73 	int i;
74 	for (i = 0; i < len; i++) block->b[i] ^= src[i];
75 }
76 
block128_inc_be(block128 * b)77 static inline void block128_inc_be(block128 *b)
78 {
79 	uint64_t v = be64_to_cpu(b->q[1]);
80 	if (++v == 0) {
81 		b->q[0] = cpu_to_be64(be64_to_cpu(b->q[0]) + 1);
82 		b->q[1] = 0;
83 	} else
84 		b->q[1] = cpu_to_be64(v);
85 }
86 
87 #ifdef IMPL_DEBUG
88 #include <stdio.h>
block128_print(block128 * b)89 static inline void block128_print(block128 *b)
90 {
91 	int i;
92 	for (i = 0; i < 16; i++) {
93 		printf("%02x ", b->b[i]);
94 	}
95 	printf("\n");
96 }
97 #endif
98 
99 #endif
100