Packages
hackney
2.0.0
4.7.2
4.7.1
4.7.0
4.6.1
4.6.0
4.5.2
4.5.1
4.5.0
4.4.5
4.4.3
4.4.2
4.4.1
4.4.0
4.3.0
4.2.3
4.2.2
4.2.1
4.2.0
4.1.0
4.0.3
4.0.2
4.0.1
4.0.0
3.2.1
3.2.0
3.1.2
3.1.1
3.1.0
3.0.3
3.0.2
3.0.1
3.0.0
retired
2.0.1
2.0.0
2.0.0-beta.1
1.25.0
1.24.1
1.24.0
1.23.0
1.22.0
1.21.0
1.20.1
1.20.0
1.19.1
1.19.0
1.18.2
1.18.1
1.18.0
1.17.4
1.17.3
1.17.2
1.17.1
1.17.0
1.16.0
1.15.2
1.15.1
1.15.0
1.14.3
1.14.2
1.14.0
1.13.0
1.12.1
1.12.0
1.11.0
1.10.1
1.10.0
1.9.0
1.8.6
1.8.5
1.8.4
1.8.3
1.8.2
1.8.0
1.7.1
1.7.0
1.6.6
retired
1.6.5
1.6.4
retired
1.6.3
1.6.2
1.6.1
1.6.0
1.5.7
1.5.6
1.5.5
1.5.4
1.5.3
1.5.2
1.5.1
1.5.0
1.4.10
1.4.8
1.4.7
1.4.6
1.4.5
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.2
1.3.1
1.3.0
1.2.0
1.1.0
1.0.6
1.0.5
1.0.2
1.0.1
0.15.2
0.15.0
0.14.3
0.14.2
0.14.1
0.14.0
0.13.1
Simple HTTP client with HTTP/1.1, HTTP/2, and HTTP/3 support
Security advisory:
This version has known vulnerabilities.
View advisories
Current section
Files
Jump to
Current section
Files
c_src/boringssl/decrepit/xts/xts.cc
// Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <openssl/evp.h>
#include <string.h>
#include <openssl/aes.h>
#include <openssl/cipher.h>
#include "../../crypto/fipsmodule/cipher/internal.h"
#include "../../crypto/fipsmodule/aes/internal.h"
typedef struct xts128_context {
AES_KEY *key1, *key2;
block128_f block1, block2;
} XTS128_CONTEXT;
static size_t CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx,
const uint8_t iv[16], const uint8_t *inp,
uint8_t *out, size_t len, int enc) {
union {
uint64_t u[2];
uint32_t d[4];
uint8_t c[16];
} tweak, scratch;
unsigned int i;
if (len < 16) {
return 0;
}
OPENSSL_memcpy(tweak.c, iv, 16);
(*ctx->block2)(tweak.c, tweak.c, ctx->key2);
if (!enc && (len % 16)) {
len -= 16;
}
while (len >= 16) {
OPENSSL_memcpy(scratch.c, inp, 16);
scratch.u[0] ^= tweak.u[0];
scratch.u[1] ^= tweak.u[1];
(*ctx->block1)(scratch.c, scratch.c, ctx->key1);
scratch.u[0] ^= tweak.u[0];
scratch.u[1] ^= tweak.u[1];
OPENSSL_memcpy(out, scratch.c, 16);
inp += 16;
out += 16;
len -= 16;
if (len == 0) {
return 1;
}
unsigned int carry, res;
res = 0x87 & (((int)tweak.d[3]) >> 31);
carry = (unsigned int)(tweak.u[0] >> 63);
tweak.u[0] = (tweak.u[0] << 1) ^ res;
tweak.u[1] = (tweak.u[1] << 1) | carry;
}
if (enc) {
for (i = 0; i < len; ++i) {
uint8_t c = inp[i];
out[i] = scratch.c[i];
scratch.c[i] = c;
}
scratch.u[0] ^= tweak.u[0];
scratch.u[1] ^= tweak.u[1];
(*ctx->block1)(scratch.c, scratch.c, ctx->key1);
scratch.u[0] ^= tweak.u[0];
scratch.u[1] ^= tweak.u[1];
OPENSSL_memcpy(out - 16, scratch.c, 16);
} else {
union {
uint64_t u[2];
uint8_t c[16];
} tweak1;
unsigned int carry, res;
res = 0x87 & (((int)tweak.d[3]) >> 31);
carry = (unsigned int)(tweak.u[0] >> 63);
tweak1.u[0] = (tweak.u[0] << 1) ^ res;
tweak1.u[1] = (tweak.u[1] << 1) | carry;
OPENSSL_memcpy(scratch.c, inp, 16);
scratch.u[0] ^= tweak1.u[0];
scratch.u[1] ^= tweak1.u[1];
(*ctx->block1)(scratch.c, scratch.c, ctx->key1);
scratch.u[0] ^= tweak1.u[0];
scratch.u[1] ^= tweak1.u[1];
for (i = 0; i < len; ++i) {
uint8_t c = inp[16 + i];
out[16 + i] = scratch.c[i];
scratch.c[i] = c;
}
scratch.u[0] ^= tweak.u[0];
scratch.u[1] ^= tweak.u[1];
(*ctx->block1)(scratch.c, scratch.c, ctx->key1);
scratch.u[0] ^= tweak.u[0];
scratch.u[1] ^= tweak.u[1];
OPENSSL_memcpy(out, scratch.c, 16);
}
return 1;
}
typedef struct {
union {
double align;
AES_KEY ks;
} ks1, ks2; // AES key schedules to use
XTS128_CONTEXT xts;
} EVP_AES_XTS_CTX;
static int aes_xts_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
const uint8_t *iv, int enc) {
EVP_AES_XTS_CTX *xctx = reinterpret_cast<EVP_AES_XTS_CTX *>(ctx->cipher_data);
if (!iv && !key) {
return 1;
}
if (key) {
// key_len is two AES keys
if (enc) {
AES_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
xctx->xts.block1 = AES_encrypt;
} else {
AES_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
xctx->xts.block1 = AES_decrypt;
}
AES_set_encrypt_key(key + ctx->key_len / 2, ctx->key_len * 4,
&xctx->ks2.ks);
xctx->xts.block2 = AES_encrypt;
xctx->xts.key1 = &xctx->ks1.ks;
}
if (iv) {
xctx->xts.key2 = &xctx->ks2.ks;
OPENSSL_memcpy(ctx->iv, iv, 16);
}
return 1;
}
static int aes_xts_cipher_update(EVP_CIPHER_CTX *ctx, uint8_t *out,
const uint8_t *in, size_t len) {
EVP_AES_XTS_CTX *xctx = reinterpret_cast<EVP_AES_XTS_CTX *>(ctx->cipher_data);
if (!xctx->xts.key1 || !xctx->xts.key2 || !out || !in ||
len < AES_BLOCK_SIZE ||
!CRYPTO_xts128_encrypt(&xctx->xts, ctx->iv, in, out, len, ctx->encrypt)) {
return 0;
}
return 1;
}
static int aes_xts_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) {
EVP_AES_XTS_CTX *xctx = reinterpret_cast<EVP_AES_XTS_CTX *>(c->cipher_data);
if (type == EVP_CTRL_COPY) {
EVP_CIPHER_CTX *out = reinterpret_cast<EVP_CIPHER_CTX *>(ptr);
EVP_AES_XTS_CTX *xctx_out =
reinterpret_cast<EVP_AES_XTS_CTX *>(out->cipher_data);
if (xctx->xts.key1) {
if (xctx->xts.key1 != &xctx->ks1.ks) {
return 0;
}
xctx_out->xts.key1 = &xctx_out->ks1.ks;
}
if (xctx->xts.key2) {
if (xctx->xts.key2 != &xctx->ks2.ks) {
return 0;
}
xctx_out->xts.key2 = &xctx_out->ks2.ks;
}
return 1;
} else if (type != EVP_CTRL_INIT) {
return -1;
}
// key1 and key2 are used as an indicator both key and IV are set
xctx->xts.key1 = nullptr;
xctx->xts.key2 = nullptr;
return 1;
}
static const EVP_CIPHER aes_256_xts = {
/* nid= */ NID_aes_256_xts,
/* block_size= */ 1,
/* key_len= */ 64 /* 2 AES-256 keys */,
/* iv_len= */ 16,
/* ctx_size= */ sizeof(EVP_AES_XTS_CTX),
/* flags= */ EVP_CIPH_XTS_MODE | EVP_CIPH_CUSTOM_IV |
EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT |
EVP_CIPH_CUSTOM_COPY,
/* init= */ aes_xts_init_key,
/* cipher_update= */ aes_xts_cipher_update,
/* cipher_final= */ nullptr,
/* update_aad= */ nullptr,
/* cleanup= */ nullptr,
/* ctrl= */ aes_xts_ctrl,
};
const EVP_CIPHER *EVP_aes_256_xts(void) { return &aes_256_xts; }