/src/openssl111/crypto/modes/xts128.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2011-2020 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include <openssl/crypto.h> |
11 | | #include "modes_local.h" |
12 | | #include <string.h> |
13 | | |
14 | | #ifndef STRICT_ALIGNMENT |
15 | | # ifdef __GNUC__ |
16 | | typedef u64 u64_a1 __attribute((__aligned__(1))); |
17 | | # else |
18 | | typedef u64 u64_a1; |
19 | | # endif |
20 | | #endif |
21 | | |
22 | | int CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx, |
23 | | const unsigned char iv[16], |
24 | | const unsigned char *inp, unsigned char *out, |
25 | | size_t len, int enc) |
26 | 0 | { |
27 | 0 | const union { |
28 | 0 | long one; |
29 | 0 | char little; |
30 | 0 | } is_endian = { |
31 | 0 | 1 |
32 | 0 | }; |
33 | 0 | union { |
34 | 0 | u64 u[2]; |
35 | 0 | u32 d[4]; |
36 | 0 | u8 c[16]; |
37 | 0 | } tweak, scratch; |
38 | 0 | unsigned int i; |
39 | |
|
40 | 0 | if (len < 16) |
41 | 0 | return -1; |
42 | | |
43 | 0 | memcpy(tweak.c, iv, 16); |
44 | |
|
45 | 0 | (*ctx->block2) (tweak.c, tweak.c, ctx->key2); |
46 | |
|
47 | 0 | if (!enc && (len % 16)) |
48 | 0 | len -= 16; |
49 | |
|
50 | 0 | while (len >= 16) { |
51 | 0 | #if defined(STRICT_ALIGNMENT) |
52 | 0 | memcpy(scratch.c, inp, 16); |
53 | 0 | scratch.u[0] ^= tweak.u[0]; |
54 | 0 | scratch.u[1] ^= tweak.u[1]; |
55 | | #else |
56 | | scratch.u[0] = ((u64_a1 *)inp)[0] ^ tweak.u[0]; |
57 | | scratch.u[1] = ((u64_a1 *)inp)[1] ^ tweak.u[1]; |
58 | | #endif |
59 | 0 | (*ctx->block1) (scratch.c, scratch.c, ctx->key1); |
60 | 0 | #if defined(STRICT_ALIGNMENT) |
61 | 0 | scratch.u[0] ^= tweak.u[0]; |
62 | 0 | scratch.u[1] ^= tweak.u[1]; |
63 | 0 | memcpy(out, scratch.c, 16); |
64 | | #else |
65 | | ((u64_a1 *)out)[0] = scratch.u[0] ^= tweak.u[0]; |
66 | | ((u64_a1 *)out)[1] = scratch.u[1] ^= tweak.u[1]; |
67 | | #endif |
68 | 0 | inp += 16; |
69 | 0 | out += 16; |
70 | 0 | len -= 16; |
71 | |
|
72 | 0 | if (len == 0) |
73 | 0 | return 0; |
74 | | |
75 | 0 | if (is_endian.little) { |
76 | 0 | unsigned int carry, res; |
77 | |
|
78 | 0 | res = 0x87 & (((int)tweak.d[3]) >> 31); |
79 | 0 | carry = (unsigned int)(tweak.u[0] >> 63); |
80 | 0 | tweak.u[0] = (tweak.u[0] << 1) ^ res; |
81 | 0 | tweak.u[1] = (tweak.u[1] << 1) | carry; |
82 | 0 | } else { |
83 | 0 | size_t c; |
84 | |
|
85 | 0 | for (c = 0, i = 0; i < 16; ++i) { |
86 | | /* |
87 | | * + substitutes for |, because c is 1 bit |
88 | | */ |
89 | 0 | c += ((size_t)tweak.c[i]) << 1; |
90 | 0 | tweak.c[i] = (u8)c; |
91 | 0 | c = c >> 8; |
92 | 0 | } |
93 | 0 | tweak.c[0] ^= (u8)(0x87 & (0 - c)); |
94 | 0 | } |
95 | 0 | } |
96 | 0 | if (enc) { |
97 | 0 | for (i = 0; i < len; ++i) { |
98 | 0 | u8 c = inp[i]; |
99 | 0 | out[i] = scratch.c[i]; |
100 | 0 | scratch.c[i] = c; |
101 | 0 | } |
102 | 0 | scratch.u[0] ^= tweak.u[0]; |
103 | 0 | scratch.u[1] ^= tweak.u[1]; |
104 | 0 | (*ctx->block1) (scratch.c, scratch.c, ctx->key1); |
105 | 0 | scratch.u[0] ^= tweak.u[0]; |
106 | 0 | scratch.u[1] ^= tweak.u[1]; |
107 | 0 | memcpy(out - 16, scratch.c, 16); |
108 | 0 | } else { |
109 | 0 | union { |
110 | 0 | u64 u[2]; |
111 | 0 | u8 c[16]; |
112 | 0 | } tweak1; |
113 | |
|
114 | 0 | if (is_endian.little) { |
115 | 0 | unsigned int carry, res; |
116 | |
|
117 | 0 | res = 0x87 & (((int)tweak.d[3]) >> 31); |
118 | 0 | carry = (unsigned int)(tweak.u[0] >> 63); |
119 | 0 | tweak1.u[0] = (tweak.u[0] << 1) ^ res; |
120 | 0 | tweak1.u[1] = (tweak.u[1] << 1) | carry; |
121 | 0 | } else { |
122 | 0 | size_t c; |
123 | |
|
124 | 0 | for (c = 0, i = 0; i < 16; ++i) { |
125 | | /* |
126 | | * + substitutes for |, because c is 1 bit |
127 | | */ |
128 | 0 | c += ((size_t)tweak.c[i]) << 1; |
129 | 0 | tweak1.c[i] = (u8)c; |
130 | 0 | c = c >> 8; |
131 | 0 | } |
132 | 0 | tweak1.c[0] ^= (u8)(0x87 & (0 - c)); |
133 | 0 | } |
134 | 0 | #if defined(STRICT_ALIGNMENT) |
135 | 0 | memcpy(scratch.c, inp, 16); |
136 | 0 | scratch.u[0] ^= tweak1.u[0]; |
137 | 0 | scratch.u[1] ^= tweak1.u[1]; |
138 | | #else |
139 | | scratch.u[0] = ((u64_a1 *)inp)[0] ^ tweak1.u[0]; |
140 | | scratch.u[1] = ((u64_a1 *)inp)[1] ^ tweak1.u[1]; |
141 | | #endif |
142 | 0 | (*ctx->block1) (scratch.c, scratch.c, ctx->key1); |
143 | 0 | scratch.u[0] ^= tweak1.u[0]; |
144 | 0 | scratch.u[1] ^= tweak1.u[1]; |
145 | |
|
146 | 0 | for (i = 0; i < len; ++i) { |
147 | 0 | u8 c = inp[16 + i]; |
148 | 0 | out[16 + i] = scratch.c[i]; |
149 | 0 | scratch.c[i] = c; |
150 | 0 | } |
151 | 0 | scratch.u[0] ^= tweak.u[0]; |
152 | 0 | scratch.u[1] ^= tweak.u[1]; |
153 | 0 | (*ctx->block1) (scratch.c, scratch.c, ctx->key1); |
154 | 0 | #if defined(STRICT_ALIGNMENT) |
155 | 0 | scratch.u[0] ^= tweak.u[0]; |
156 | 0 | scratch.u[1] ^= tweak.u[1]; |
157 | 0 | memcpy(out, scratch.c, 16); |
158 | | #else |
159 | | ((u64_a1 *)out)[0] = scratch.u[0] ^ tweak.u[0]; |
160 | | ((u64_a1 *)out)[1] = scratch.u[1] ^ tweak.u[1]; |
161 | | #endif |
162 | 0 | } |
163 | |
|
164 | 0 | return 0; |
165 | 0 | } |