| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/evp.h> |
| 16 |
| 17 | #include <string.h> |
| 18 |
| 19 | #include <openssl/aes.h> |
| 20 | #include <openssl/cipher.h> |
| 21 |
| 22 | #include "../../crypto/fipsmodule/cipher/internal.h" |
| 23 | #include "../../crypto/fipsmodule/aes/internal.h" |
| 24 |
| 25 |
| 26 | typedef struct xts128_context { |
| 27 | AES_KEY *key1, *key2; |
| 28 | block128_f block1, block2; |
| 29 | } XTS128_CONTEXT; |
| 30 |
| 31 | static size_t CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx, |
| 32 | const uint8_t iv[16], const uint8_t *inp, |
| 33 | uint8_t *out, size_t len, int enc) { |
| 34 | union { |
| 35 | uint64_t u[2]; |
| 36 | uint32_t d[4]; |
| 37 | uint8_t c[16]; |
| 38 | } tweak, scratch; |
| 39 | unsigned int i; |
| 40 |
| 41 | if (len < 16) { |
| 42 | return 0; |
| 43 | } |
| 44 |
| 45 | OPENSSL_memcpy(tweak.c, iv, 16); |
| 46 |
| 47 | (*ctx->block2)(tweak.c, tweak.c, ctx->key2); |
| 48 |
| 49 | if (!enc && (len % 16)) { |
| 50 | len -= 16; |
| 51 | } |
| 52 |
| 53 | while (len >= 16) { |
| 54 | OPENSSL_memcpy(scratch.c, inp, 16); |
| 55 | scratch.u[0] ^= tweak.u[0]; |
| 56 | scratch.u[1] ^= tweak.u[1]; |
| 57 | (*ctx->block1)(scratch.c, scratch.c, ctx->key1); |
| 58 | scratch.u[0] ^= tweak.u[0]; |
| 59 | scratch.u[1] ^= tweak.u[1]; |
| 60 | OPENSSL_memcpy(out, scratch.c, 16); |
| 61 | inp += 16; |
| 62 | out += 16; |
| 63 | len -= 16; |
| 64 |
| 65 | if (len == 0) { |
| 66 | return 1; |
| 67 | } |
| 68 |
| 69 | unsigned int carry, res; |
| 70 |
| 71 | res = 0x87 & (((int)tweak.d[3]) >> 31); |
| 72 | carry = (unsigned int)(tweak.u[0] >> 63); |
| 73 | tweak.u[0] = (tweak.u[0] << 1) ^ res; |
| 74 | tweak.u[1] = (tweak.u[1] << 1) | carry; |
| 75 | } |
| 76 | if (enc) { |
| 77 | for (i = 0; i < len; ++i) { |
| 78 | uint8_t c = inp[i]; |
| 79 | out[i] = scratch.c[i]; |
| 80 | scratch.c[i] = c; |
| 81 | } |
| 82 | scratch.u[0] ^= tweak.u[0]; |
| 83 | scratch.u[1] ^= tweak.u[1]; |
| 84 | (*ctx->block1)(scratch.c, scratch.c, ctx->key1); |
| 85 | scratch.u[0] ^= tweak.u[0]; |
| 86 | scratch.u[1] ^= tweak.u[1]; |
| 87 | OPENSSL_memcpy(out - 16, scratch.c, 16); |
| 88 | } else { |
| 89 | union { |
| 90 | uint64_t u[2]; |
| 91 | uint8_t c[16]; |
| 92 | } tweak1; |
| 93 |
| 94 | unsigned int carry, res; |
| 95 |
| 96 | res = 0x87 & (((int)tweak.d[3]) >> 31); |
| 97 | carry = (unsigned int)(tweak.u[0] >> 63); |
| 98 | tweak1.u[0] = (tweak.u[0] << 1) ^ res; |
| 99 | tweak1.u[1] = (tweak.u[1] << 1) | carry; |
| 100 | OPENSSL_memcpy(scratch.c, inp, 16); |
| 101 | scratch.u[0] ^= tweak1.u[0]; |
| 102 | scratch.u[1] ^= tweak1.u[1]; |
| 103 | (*ctx->block1)(scratch.c, scratch.c, ctx->key1); |
| 104 | scratch.u[0] ^= tweak1.u[0]; |
| 105 | scratch.u[1] ^= tweak1.u[1]; |
| 106 |
| 107 | for (i = 0; i < len; ++i) { |
| 108 | uint8_t c = inp[16 + i]; |
| 109 | out[16 + i] = scratch.c[i]; |
| 110 | scratch.c[i] = c; |
| 111 | } |
| 112 | scratch.u[0] ^= tweak.u[0]; |
| 113 | scratch.u[1] ^= tweak.u[1]; |
| 114 | (*ctx->block1)(scratch.c, scratch.c, ctx->key1); |
| 115 | scratch.u[0] ^= tweak.u[0]; |
| 116 | scratch.u[1] ^= tweak.u[1]; |
| 117 | OPENSSL_memcpy(out, scratch.c, 16); |
| 118 | } |
| 119 |
| 120 | return 1; |
| 121 | } |
| 122 |
| 123 | typedef struct { |
| 124 | union { |
| 125 | double align; |
| 126 | AES_KEY ks; |
| 127 | } ks1, ks2; // AES key schedules to use |
| 128 | XTS128_CONTEXT xts; |
| 129 | } EVP_AES_XTS_CTX; |
| 130 |
| 131 | static int aes_xts_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key, |
| 132 | const uint8_t *iv, int enc) { |
| 133 | EVP_AES_XTS_CTX *xctx = reinterpret_cast<EVP_AES_XTS_CTX *>(ctx->cipher_data); |
| 134 | if (!iv && !key) { |
| 135 | return 1; |
| 136 | } |
| 137 |
| 138 | if (key) { |
| 139 | // key_len is two AES keys |
| 140 | if (enc) { |
| 141 | AES_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks); |
| 142 | xctx->xts.block1 = AES_encrypt; |
| 143 | } else { |
| 144 | AES_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks); |
| 145 | xctx->xts.block1 = AES_decrypt; |
| 146 | } |
| 147 |
| 148 | AES_set_encrypt_key(key + ctx->key_len / 2, ctx->key_len * 4, |
| 149 | &xctx->ks2.ks); |
| 150 | xctx->xts.block2 = AES_encrypt; |
| 151 | xctx->xts.key1 = &xctx->ks1.ks; |
| 152 | } |
| 153 |
| 154 | if (iv) { |
| 155 | xctx->xts.key2 = &xctx->ks2.ks; |
| 156 | OPENSSL_memcpy(ctx->iv, iv, 16); |
| 157 | } |
| 158 |
| 159 | return 1; |
| 160 | } |
| 161 |
| 162 | static int aes_xts_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in, |
| 163 | size_t len) { |
| 164 | EVP_AES_XTS_CTX *xctx = reinterpret_cast<EVP_AES_XTS_CTX *>(ctx->cipher_data); |
| 165 | if (!xctx->xts.key1 || !xctx->xts.key2 || !out || !in || |
| 166 | len < AES_BLOCK_SIZE || |
| 167 | !CRYPTO_xts128_encrypt(&xctx->xts, ctx->iv, in, out, len, ctx->encrypt)) { |
| 168 | return 0; |
| 169 | } |
| 170 | return 1; |
| 171 | } |
| 172 |
| 173 | static int aes_xts_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) { |
| 174 | EVP_AES_XTS_CTX *xctx = reinterpret_cast<EVP_AES_XTS_CTX *>(c->cipher_data); |
| 175 | if (type == EVP_CTRL_COPY) { |
| 176 | EVP_CIPHER_CTX *out = reinterpret_cast<EVP_CIPHER_CTX *>(ptr); |
| 177 | EVP_AES_XTS_CTX *xctx_out = |
| 178 | reinterpret_cast<EVP_AES_XTS_CTX *>(out->cipher_data); |
| 179 | if (xctx->xts.key1) { |
| 180 | if (xctx->xts.key1 != &xctx->ks1.ks) { |
| 181 | return 0; |
| 182 | } |
| 183 | xctx_out->xts.key1 = &xctx_out->ks1.ks; |
| 184 | } |
| 185 | if (xctx->xts.key2) { |
| 186 | if (xctx->xts.key2 != &xctx->ks2.ks) { |
| 187 | return 0; |
| 188 | } |
| 189 | xctx_out->xts.key2 = &xctx_out->ks2.ks; |
| 190 | } |
| 191 | return 1; |
| 192 | } else if (type != EVP_CTRL_INIT) { |
| 193 | return -1; |
| 194 | } |
| 195 | // key1 and key2 are used as an indicator both key and IV are set |
| 196 | xctx->xts.key1 = NULL; |
| 197 | xctx->xts.key2 = NULL; |
| 198 | return 1; |
| 199 | } |
| 200 |
| 201 | static const EVP_CIPHER aes_256_xts = { |
| 202 | /* nid= */ NID_aes_256_xts, |
| 203 | /* block_size= */ 1, |
| 204 | /* key_len= */ 64 /* 2 AES-256 keys */, |
| 205 | /* iv_len= */ 16, |
| 206 | /* ctx_size= */ sizeof(EVP_AES_XTS_CTX), |
| 207 | /* flags= */ EVP_CIPH_XTS_MODE | EVP_CIPH_CUSTOM_IV | |
| 208 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT | |
| 209 | EVP_CIPH_CUSTOM_COPY, |
| 210 | /* init= */ aes_xts_init_key, |
| 211 | /* cipher= */ aes_xts_cipher, |
| 212 | /* cleanup= */ nullptr, |
| 213 | /* ctrl= */ aes_xts_ctrl, |
| 214 | }; |
| 215 |
| 216 | const EVP_CIPHER *EVP_aes_256_xts(void) { return &aes_256_xts; } |
| 217 |