/src/openssl/crypto/modes/xts128.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2011-2016 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_lcl.h" |
12 | | #include <string.h> |
13 | | |
14 | | int CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx, |
15 | | const unsigned char iv[16], |
16 | | const unsigned char *inp, unsigned char *out, |
17 | | size_t len, int enc) |
18 | 0 | { |
19 | 0 | const union { |
20 | 0 | long one; |
21 | 0 | char little; |
22 | 0 | } is_endian = { |
23 | 0 | 1 |
24 | 0 | }; |
25 | 0 | union { |
26 | 0 | u64 u[2]; |
27 | 0 | u32 d[4]; |
28 | 0 | u8 c[16]; |
29 | 0 | } tweak, scratch; |
30 | 0 | unsigned int i; |
31 | 0 |
|
32 | 0 | if (len < 16) |
33 | 0 | return -1; |
34 | 0 | |
35 | 0 | memcpy(tweak.c, iv, 16); |
36 | 0 |
|
37 | 0 | (*ctx->block2) (tweak.c, tweak.c, ctx->key2); |
38 | 0 |
|
39 | 0 | if (!enc && (len % 16)) |
40 | 0 | len -= 16; |
41 | 0 |
|
42 | 0 | while (len >= 16) { |
43 | | #if defined(STRICT_ALIGNMENT) |
44 | | memcpy(scratch.c, inp, 16); |
45 | | scratch.u[0] ^= tweak.u[0]; |
46 | | scratch.u[1] ^= tweak.u[1]; |
47 | | #else |
48 | | scratch.u[0] = ((u64 *)inp)[0] ^ tweak.u[0]; |
49 | 0 | scratch.u[1] = ((u64 *)inp)[1] ^ tweak.u[1]; |
50 | 0 | #endif |
51 | 0 | (*ctx->block1) (scratch.c, scratch.c, ctx->key1); |
52 | | #if defined(STRICT_ALIGNMENT) |
53 | | scratch.u[0] ^= tweak.u[0]; |
54 | | scratch.u[1] ^= tweak.u[1]; |
55 | | memcpy(out, scratch.c, 16); |
56 | | #else |
57 | | ((u64 *)out)[0] = scratch.u[0] ^= tweak.u[0]; |
58 | 0 | ((u64 *)out)[1] = scratch.u[1] ^= tweak.u[1]; |
59 | 0 | #endif |
60 | 0 | inp += 16; |
61 | 0 | out += 16; |
62 | 0 | len -= 16; |
63 | 0 |
|
64 | 0 | if (len == 0) |
65 | 0 | return 0; |
66 | 0 | |
67 | 0 | if (is_endian.little) { |
68 | 0 | unsigned int carry, res; |
69 | 0 |
|
70 | 0 | res = 0x87 & (((int)tweak.d[3]) >> 31); |
71 | 0 | carry = (unsigned int)(tweak.u[0] >> 63); |
72 | 0 | tweak.u[0] = (tweak.u[0] << 1) ^ res; |
73 | 0 | tweak.u[1] = (tweak.u[1] << 1) | carry; |
74 | 0 | } else { |
75 | 0 | size_t c; |
76 | 0 |
|
77 | 0 | for (c = 0, i = 0; i < 16; ++i) { |
78 | 0 | /* |
79 | 0 | * + substitutes for |, because c is 1 bit |
80 | 0 | */ |
81 | 0 | c += ((size_t)tweak.c[i]) << 1; |
82 | 0 | tweak.c[i] = (u8)c; |
83 | 0 | c = c >> 8; |
84 | 0 | } |
85 | 0 | tweak.c[0] ^= (u8)(0x87 & (0 - c)); |
86 | 0 | } |
87 | 0 | } |
88 | 0 | if (enc) { |
89 | 0 | for (i = 0; i < len; ++i) { |
90 | 0 | u8 c = inp[i]; |
91 | 0 | out[i] = scratch.c[i]; |
92 | 0 | scratch.c[i] = c; |
93 | 0 | } |
94 | 0 | scratch.u[0] ^= tweak.u[0]; |
95 | 0 | scratch.u[1] ^= tweak.u[1]; |
96 | 0 | (*ctx->block1) (scratch.c, scratch.c, ctx->key1); |
97 | 0 | scratch.u[0] ^= tweak.u[0]; |
98 | 0 | scratch.u[1] ^= tweak.u[1]; |
99 | 0 | memcpy(out - 16, scratch.c, 16); |
100 | 0 | } else { |
101 | 0 | union { |
102 | 0 | u64 u[2]; |
103 | 0 | u8 c[16]; |
104 | 0 | } tweak1; |
105 | 0 |
|
106 | 0 | if (is_endian.little) { |
107 | 0 | unsigned int carry, res; |
108 | 0 |
|
109 | 0 | res = 0x87 & (((int)tweak.d[3]) >> 31); |
110 | 0 | carry = (unsigned int)(tweak.u[0] >> 63); |
111 | 0 | tweak1.u[0] = (tweak.u[0] << 1) ^ res; |
112 | 0 | tweak1.u[1] = (tweak.u[1] << 1) | carry; |
113 | 0 | } else { |
114 | 0 | size_t c; |
115 | 0 |
|
116 | 0 | for (c = 0, i = 0; i < 16; ++i) { |
117 | 0 | /* |
118 | 0 | * + substitutes for |, because c is 1 bit |
119 | 0 | */ |
120 | 0 | c += ((size_t)tweak.c[i]) << 1; |
121 | 0 | tweak1.c[i] = (u8)c; |
122 | 0 | c = c >> 8; |
123 | 0 | } |
124 | 0 | tweak1.c[0] ^= (u8)(0x87 & (0 - c)); |
125 | 0 | } |
126 | | #if defined(STRICT_ALIGNMENT) |
127 | | memcpy(scratch.c, inp, 16); |
128 | | scratch.u[0] ^= tweak1.u[0]; |
129 | | scratch.u[1] ^= tweak1.u[1]; |
130 | | #else |
131 | | scratch.u[0] = ((u64 *)inp)[0] ^ tweak1.u[0]; |
132 | 0 | scratch.u[1] = ((u64 *)inp)[1] ^ tweak1.u[1]; |
133 | 0 | #endif |
134 | 0 | (*ctx->block1) (scratch.c, scratch.c, ctx->key1); |
135 | 0 | scratch.u[0] ^= tweak1.u[0]; |
136 | 0 | scratch.u[1] ^= tweak1.u[1]; |
137 | 0 |
|
138 | 0 | for (i = 0; i < len; ++i) { |
139 | 0 | u8 c = inp[16 + i]; |
140 | 0 | out[16 + i] = scratch.c[i]; |
141 | 0 | scratch.c[i] = c; |
142 | 0 | } |
143 | 0 | scratch.u[0] ^= tweak.u[0]; |
144 | 0 | scratch.u[1] ^= tweak.u[1]; |
145 | 0 | (*ctx->block1) (scratch.c, scratch.c, ctx->key1); |
146 | | #if defined(STRICT_ALIGNMENT) |
147 | | scratch.u[0] ^= tweak.u[0]; |
148 | | scratch.u[1] ^= tweak.u[1]; |
149 | | memcpy(out, scratch.c, 16); |
150 | | #else |
151 | | ((u64 *)out)[0] = scratch.u[0] ^ tweak.u[0]; |
152 | 0 | ((u64 *)out)[1] = scratch.u[1] ^ tweak.u[1]; |
153 | 0 | #endif |
154 | 0 | } |
155 | 0 |
|
156 | 0 | return 0; |
157 | 0 | } |