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