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 <cryptonite_bitfn.h>
35 #include <cryptonite_align.h>
36
37 typedef union {
38 uint64_t q[2];
39 uint32_t d[4];
40 uint16_t w[8];
41 uint8_t b[16];
42 } block128;
43
block128_copy_bytes(block128 * block,const uint8_t * src,uint32_t len)44 static inline void block128_copy_bytes(block128 *block, const uint8_t *src, uint32_t len)
45 {
46 int i;
47 for (i = 0; i < len; i++) block->b[i] = src[i];
48 }
49
block128_copy_aligned(block128 * d,const block128 * s)50 static inline void block128_copy_aligned(block128 *d, const block128 *s)
51 {
52 d->q[0] = s->q[0]; d->q[1] = s->q[1];
53 }
54
block128_copy(block128 * d,const block128 * s)55 static inline void block128_copy(block128 *d, const block128 *s)
56 {
57 if (need_alignment(d, 8) || need_alignment(s, 8)) {
58 block128_copy_bytes(d, (const uint8_t *) s, 16);
59 } else {
60 block128_copy_aligned(d, s);
61 }
62 }
63
block128_zero(block128 * d)64 static inline void block128_zero(block128 *d)
65 {
66 d->q[0] = 0; d->q[1] = 0;
67 }
68
block128_xor_bytes(block128 * block,const uint8_t * src,uint32_t len)69 static inline void block128_xor_bytes(block128 *block, const uint8_t *src, uint32_t len)
70 {
71 int i;
72 for (i = 0; i < len; i++) block->b[i] ^= src[i];
73 }
74
block128_xor_aligned(block128 * d,const block128 * s)75 static inline void block128_xor_aligned(block128 *d, const block128 *s)
76 {
77 d->q[0] ^= s->q[0];
78 d->q[1] ^= s->q[1];
79 }
80
block128_xor(block128 * d,const block128 * s)81 static inline void block128_xor(block128 *d, const block128 *s)
82 {
83 if (need_alignment(d, 8) || need_alignment(s, 8)) {
84 block128_xor_bytes(d, (const uint8_t *) s, 16);
85 } else {
86 block128_xor_aligned(d, s);
87 }
88 }
89
block128_vxor_bytes(block128 * block,const uint8_t * src1,const uint8_t * src2,uint32_t len)90 static inline void block128_vxor_bytes(block128 *block, const uint8_t *src1, const uint8_t *src2, uint32_t len)
91 {
92 int i;
93 for (i = 0; i < len; i++) block->b[i] = src1[i] ^ src2[i];
94 }
95
block128_vxor_aligned(block128 * d,const block128 * s1,const block128 * s2)96 static inline void block128_vxor_aligned(block128 *d, const block128 *s1, const block128 *s2)
97 {
98 d->q[0] = s1->q[0] ^ s2->q[0];
99 d->q[1] = s1->q[1] ^ s2->q[1];
100 }
101
block128_vxor(block128 * d,const block128 * s1,const block128 * s2)102 static inline void block128_vxor(block128 *d, const block128 *s1, const block128 *s2)
103 {
104 if (need_alignment(d, 8) || need_alignment(s1, 8) || need_alignment(s2, 8)) {
105 block128_vxor_bytes(d, (const uint8_t *) s1, (const uint8_t *) s2, 16);
106 } else {
107 block128_vxor_aligned(d, s1, s2);
108 }
109 }
110
block128_byte_reverse(block128 * a)111 static inline void block128_byte_reverse(block128 *a)
112 {
113 uint64_t s0 = a->q[0], s1 = a->q[1];
114 a->q[0] = bitfn_swap64(s1);
115 a->q[1] = bitfn_swap64(s0);
116 }
117
block128_inc_be(block128 * b)118 static inline void block128_inc_be(block128 *b)
119 {
120 uint64_t v = be64_to_cpu(b->q[1]);
121 if (++v == 0) {
122 b->q[0] = cpu_to_be64(be64_to_cpu(b->q[0]) + 1);
123 b->q[1] = 0;
124 } else
125 b->q[1] = cpu_to_be64(v);
126 }
127
block128_inc32_be(block128 * b)128 static inline void block128_inc32_be(block128 *b)
129 {
130 b->d[3] = cpu_to_be32(be32_to_cpu(b->d[3]) + 1);
131 }
132
block128_inc32_le(block128 * b)133 static inline void block128_inc32_le(block128 *b)
134 {
135 b->d[0] = cpu_to_le32(le32_to_cpu(b->d[0]) + 1);
136 }
137
138 #ifdef IMPL_DEBUG
139 #include <stdio.h>
block128_print(block128 * b)140 static inline void block128_print(block128 *b)
141 {
142 int i;
143 for (i = 0; i < 16; i++) {
144 printf("%02x ", b->b[i]);
145 }
146 printf("\n");
147 }
148 #endif
149
150 #endif
151