/src/openssl30/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 | 0 | { | 
| 28 | 0 |     DECLARE_IS_ENDIAN; | 
| 29 | 0 |     union { | 
| 30 | 0 |         u64 u[2]; | 
| 31 | 0 |         u32 d[4]; | 
| 32 | 0 |         u8 c[16]; | 
| 33 | 0 |     } tweak, scratch; | 
| 34 | 0 |     unsigned int i; | 
| 35 |  | 
 | 
| 36 | 0 |     if (len < 16) | 
| 37 | 0 |         return -1; | 
| 38 |  |  | 
| 39 | 0 |     memcpy(tweak.c, iv, 16); | 
| 40 |  | 
 | 
| 41 | 0 |     (*ctx->block2) (tweak.c, tweak.c, ctx->key2); | 
| 42 |  | 
 | 
| 43 | 0 |     if (!enc && (len % 16)) | 
| 44 | 0 |         len -= 16; | 
| 45 |  | 
 | 
| 46 | 0 |     while (len >= 16) { | 
| 47 | 0 | #if defined(STRICT_ALIGNMENT) | 
| 48 | 0 |         memcpy(scratch.c, inp, 16); | 
| 49 | 0 |         scratch.u[0] ^= tweak.u[0]; | 
| 50 | 0 |         scratch.u[1] ^= tweak.u[1]; | 
| 51 |  | #else | 
| 52 |  |         scratch.u[0] = ((u64_a1 *)inp)[0] ^ tweak.u[0]; | 
| 53 |  |         scratch.u[1] = ((u64_a1 *)inp)[1] ^ tweak.u[1]; | 
| 54 |  | #endif | 
| 55 | 0 |         (*ctx->block1) (scratch.c, scratch.c, ctx->key1); | 
| 56 | 0 | #if defined(STRICT_ALIGNMENT) | 
| 57 | 0 |         scratch.u[0] ^= tweak.u[0]; | 
| 58 | 0 |         scratch.u[1] ^= tweak.u[1]; | 
| 59 | 0 |         memcpy(out, scratch.c, 16); | 
| 60 |  | #else | 
| 61 |  |         ((u64_a1 *)out)[0] = scratch.u[0] ^= tweak.u[0]; | 
| 62 |  |         ((u64_a1 *)out)[1] = scratch.u[1] ^= tweak.u[1]; | 
| 63 |  | #endif | 
| 64 | 0 |         inp += 16; | 
| 65 | 0 |         out += 16; | 
| 66 | 0 |         len -= 16; | 
| 67 |  | 
 | 
| 68 | 0 |         if (len == 0) | 
| 69 | 0 |             return 0; | 
| 70 |  |  | 
| 71 | 0 |         if (IS_LITTLE_ENDIAN) { | 
| 72 | 0 |             unsigned int carry, res; | 
| 73 |  | 
 | 
| 74 | 0 |             res = 0x87 & (((int)tweak.d[3]) >> 31); | 
| 75 | 0 |             carry = (unsigned int)(tweak.u[0] >> 63); | 
| 76 | 0 |             tweak.u[0] = (tweak.u[0] << 1) ^ res; | 
| 77 | 0 |             tweak.u[1] = (tweak.u[1] << 1) | carry; | 
| 78 | 0 |         } 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 | 0 |     } | 
| 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 | 0 | #if defined(STRICT_ALIGNMENT) | 
| 131 | 0 |         memcpy(scratch.c, inp, 16); | 
| 132 | 0 |         scratch.u[0] ^= tweak1.u[0]; | 
| 133 | 0 |         scratch.u[1] ^= tweak1.u[1]; | 
| 134 |  | #else | 
| 135 |  |         scratch.u[0] = ((u64_a1 *)inp)[0] ^ tweak1.u[0]; | 
| 136 |  |         scratch.u[1] = ((u64_a1 *)inp)[1] ^ tweak1.u[1]; | 
| 137 |  | #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 | 0 | #if defined(STRICT_ALIGNMENT) | 
| 151 | 0 |         scratch.u[0] ^= tweak.u[0]; | 
| 152 | 0 |         scratch.u[1] ^= tweak.u[1]; | 
| 153 | 0 |         memcpy(out, scratch.c, 16); | 
| 154 |  | #else | 
| 155 |  |         ((u64_a1 *)out)[0] = scratch.u[0] ^ tweak.u[0]; | 
| 156 |  |         ((u64_a1 *)out)[1] = scratch.u[1] ^ tweak.u[1]; | 
| 157 |  | #endif | 
| 158 | 0 |     } | 
| 159 |  | 
 | 
| 160 | 0 |     return 0; | 
| 161 | 0 | } |