| 1 |
/* |
| 2 |
* Copyright 2018-2025 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 |
/* clang-format off */ |
| 10 |
{- |
| 11 |
use OpenSSL::paramnames qw(produce_param_decoder); |
| 12 |
-} |
| 13 |
/* clang-format on */ |
| 14 |
|
| 15 |
/* |
| 16 |
* DES low level APIs are deprecated for public use, but still ok for internal |
| 17 |
* use. We access the DES_set_odd_parity(3) function here. |
| 18 |
*/ |
| 19 |
#include "internal/deprecated.h" |
| 20 |
|
| 21 |
#include <stdlib.h> |
| 22 |
#include <stdarg.h> |
| 23 |
#include <string.h> |
| 24 |
|
| 25 |
#include <openssl/core_names.h> |
| 26 |
#include <openssl/des.h> |
| 27 |
#include <openssl/evp.h> |
| 28 |
#include <openssl/kdf.h> |
| 29 |
#include <openssl/proverr.h> |
| 30 |
|
| 31 |
#include "internal/cryptlib.h" |
| 32 |
#include "crypto/evp.h" |
| 33 |
#include "internal/numbers.h" |
| 34 |
#include "prov/implementations.h" |
| 35 |
#include "prov/provider_ctx.h" |
| 36 |
#include "prov/provider_util.h" |
| 37 |
#include "prov/providercommon.h" |
| 38 |
|
| 39 |
/* KRB5 KDF defined in RFC 3961, Section 5.1 */ |
| 40 |
|
| 41 |
static OSSL_FUNC_kdf_newctx_fn krb5kdf_new; |
| 42 |
static OSSL_FUNC_kdf_dupctx_fn krb5kdf_dup; |
| 43 |
static OSSL_FUNC_kdf_freectx_fn krb5kdf_free; |
| 44 |
static OSSL_FUNC_kdf_reset_fn krb5kdf_reset; |
| 45 |
static OSSL_FUNC_kdf_derive_fn krb5kdf_derive; |
| 46 |
static OSSL_FUNC_kdf_settable_ctx_params_fn krb5kdf_settable_ctx_params; |
| 47 |
static OSSL_FUNC_kdf_set_ctx_params_fn krb5kdf_set_ctx_params; |
| 48 |
static OSSL_FUNC_kdf_gettable_ctx_params_fn krb5kdf_gettable_ctx_params; |
| 49 |
static OSSL_FUNC_kdf_get_ctx_params_fn krb5kdf_get_ctx_params; |
| 50 |
|
| 51 |
static int KRB5KDF(const EVP_CIPHER *cipher, ENGINE *engine, |
| 52 |
const unsigned char *key, size_t key_len, |
| 53 |
const unsigned char *constant, size_t constant_len, |
| 54 |
unsigned char *okey, size_t okey_len); |
| 55 |
|
| 56 |
typedef struct { |
| 57 |
void *provctx; |
| 58 |
PROV_CIPHER cipher; |
| 59 |
unsigned char *key; |
| 60 |
size_t key_len; |
| 61 |
unsigned char *constant; |
| 62 |
size_t constant_len; |
| 63 |
} KRB5KDF_CTX; |
| 64 |
|
| 65 |
static void *krb5kdf_new(void *provctx) |
| 66 |
{ |
| 67 |
KRB5KDF_CTX *ctx; |
| 68 |
|
| 69 |
if (!ossl_prov_is_running()) |
| 70 |
return NULL; |
| 71 |
|
| 72 |
if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) |
| 73 |
return NULL; |
| 74 |
ctx->provctx = provctx; |
| 75 |
return ctx; |
| 76 |
} |
| 77 |
|
| 78 |
static void krb5kdf_free(void *vctx) |
| 79 |
{ |
| 80 |
KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx; |
| 81 |
|
| 82 |
if (ctx != NULL) { |
| 83 |
krb5kdf_reset(ctx); |
| 84 |
OPENSSL_free(ctx); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
static void krb5kdf_reset(void *vctx) |
| 89 |
{ |
| 90 |
KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx; |
| 91 |
void *provctx = ctx->provctx; |
| 92 |
|
| 93 |
ossl_prov_cipher_reset(&ctx->cipher); |
| 94 |
OPENSSL_clear_free(ctx->key, ctx->key_len); |
| 95 |
OPENSSL_clear_free(ctx->constant, ctx->constant_len); |
| 96 |
memset(ctx, 0, sizeof(*ctx)); |
| 97 |
ctx->provctx = provctx; |
| 98 |
} |
| 99 |
|
| 100 |
static int krb5kdf_set_membuf(unsigned char **dst, size_t *dst_len, |
| 101 |
const OSSL_PARAM *p) |
| 102 |
{ |
| 103 |
OPENSSL_clear_free(*dst, *dst_len); |
| 104 |
*dst = NULL; |
| 105 |
*dst_len = 0; |
| 106 |
return OSSL_PARAM_get_octet_string(p, (void **)dst, 0, dst_len); |
| 107 |
} |
| 108 |
|
| 109 |
static void *krb5kdf_dup(void *vctx) |
| 110 |
{ |
| 111 |
const KRB5KDF_CTX *src = (const KRB5KDF_CTX *)vctx; |
| 112 |
KRB5KDF_CTX *dest; |
| 113 |
|
| 114 |
dest = krb5kdf_new(src->provctx); |
| 115 |
if (dest != NULL) { |
| 116 |
if (!ossl_prov_memdup(src->key, src->key_len, |
| 117 |
&dest->key, &dest->key_len) |
| 118 |
|| !ossl_prov_memdup(src->constant, src->constant_len, |
| 119 |
&dest->constant, &dest->constant_len) |
| 120 |
|| !ossl_prov_cipher_copy(&dest->cipher, &src->cipher)) |
| 121 |
goto err; |
| 122 |
} |
| 123 |
return dest; |
| 124 |
|
| 125 |
err: |
| 126 |
krb5kdf_free(dest); |
| 127 |
return NULL; |
| 128 |
} |
| 129 |
|
| 130 |
static int krb5kdf_derive(void *vctx, unsigned char *key, size_t keylen, |
| 131 |
const OSSL_PARAM params[]) |
| 132 |
{ |
| 133 |
KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx; |
| 134 |
const EVP_CIPHER *cipher; |
| 135 |
ENGINE *engine; |
| 136 |
|
| 137 |
if (!ossl_prov_is_running() || !krb5kdf_set_ctx_params(ctx, params)) |
| 138 |
return 0; |
| 139 |
|
| 140 |
cipher = ossl_prov_cipher_cipher(&ctx->cipher); |
| 141 |
if (cipher == NULL) { |
| 142 |
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CIPHER); |
| 143 |
return 0; |
| 144 |
} |
| 145 |
if (ctx->key == NULL) { |
| 146 |
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY); |
| 147 |
return 0; |
| 148 |
} |
| 149 |
if (ctx->constant == NULL) { |
| 150 |
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CONSTANT); |
| 151 |
return 0; |
| 152 |
} |
| 153 |
engine = ossl_prov_cipher_engine(&ctx->cipher); |
| 154 |
return KRB5KDF(cipher, engine, ctx->key, ctx->key_len, |
| 155 |
ctx->constant, ctx->constant_len, |
| 156 |
key, keylen); |
| 157 |
} |
| 158 |
|
| 159 |
/* clang-format off */ |
| 160 |
{- produce_param_decoder('krb5kdf_set_ctx_params', |
| 161 |
(['OSSL_KDF_PARAM_PROPERTIES', 'propq', 'utf8_string'], |
| 162 |
['OSSL_ALG_PARAM_ENGINE', 'engine', 'utf8_string', 'hidden'], |
| 163 |
['OSSL_KDF_PARAM_CIPHER', 'cipher', 'utf8_string'], |
| 164 |
['OSSL_KDF_PARAM_KEY', 'key', 'octet_string'], |
| 165 |
['OSSL_KDF_PARAM_CONSTANT', 'cnst', 'octet_string'], |
| 166 |
)); -} |
| 167 |
/* clang-format on */ |
| 168 |
|
| 169 |
static int krb5kdf_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
| 170 |
{ |
| 171 |
struct krb5kdf_set_ctx_params_st p; |
| 172 |
KRB5KDF_CTX *ctx = vctx; |
| 173 |
OSSL_LIB_CTX *provctx; |
| 174 |
|
| 175 |
if (ctx == NULL || !krb5kdf_set_ctx_params_decoder(params, &p)) |
| 176 |
return 0; |
| 177 |
|
| 178 |
provctx = PROV_LIBCTX_OF(ctx->provctx); |
| 179 |
|
| 180 |
if (!ossl_prov_cipher_load(&ctx->cipher, p.cipher, p.propq, p.engine, provctx)) |
| 181 |
return 0; |
| 182 |
|
| 183 |
if (p.key != NULL && !krb5kdf_set_membuf(&ctx->key, &ctx->key_len, p.key)) |
| 184 |
return 0; |
| 185 |
|
| 186 |
if (p.cnst != NULL |
| 187 |
&& !krb5kdf_set_membuf(&ctx->constant, &ctx->constant_len, p.cnst)) |
| 188 |
return 0; |
| 189 |
|
| 190 |
return 1; |
| 191 |
} |
| 192 |
|
| 193 |
static const OSSL_PARAM *krb5kdf_settable_ctx_params(ossl_unused void *ctx, |
| 194 |
ossl_unused void *provctx) |
| 195 |
{ |
| 196 |
return krb5kdf_set_ctx_params_list; |
| 197 |
} |
| 198 |
|
| 199 |
/* clang-format off */ |
| 200 |
{- produce_param_decoder('krb5kdf_get_ctx_params', |
| 201 |
(['OSSL_KDF_PARAM_SIZE', 'size', 'size_t'], |
| 202 |
)); -} |
| 203 |
/* clang-format on */ |
| 204 |
|
| 205 |
static int krb5kdf_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
| 206 |
{ |
| 207 |
struct krb5kdf_get_ctx_params_st p; |
| 208 |
KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx; |
| 209 |
|
| 210 |
if (ctx == NULL || !krb5kdf_get_ctx_params_decoder(params, &p)) |
| 211 |
return 0; |
| 212 |
|
| 213 |
if (p.size != NULL) { |
| 214 |
const EVP_CIPHER *cipher = ossl_prov_cipher_cipher(&ctx->cipher); |
| 215 |
size_t len; |
| 216 |
|
| 217 |
if (cipher != NULL) |
| 218 |
len = EVP_CIPHER_get_key_length(cipher); |
| 219 |
else |
| 220 |
len = EVP_MAX_KEY_LENGTH; |
| 221 |
|
| 222 |
if (!OSSL_PARAM_set_size_t(p.size, len)) |
| 223 |
return 0; |
| 224 |
} |
| 225 |
return 1; |
| 226 |
} |
| 227 |
|
| 228 |
static const OSSL_PARAM *krb5kdf_gettable_ctx_params(ossl_unused void *ctx, |
| 229 |
ossl_unused void *provctx) |
| 230 |
{ |
| 231 |
return krb5kdf_get_ctx_params_list; |
| 232 |
} |
| 233 |
|
| 234 |
const OSSL_DISPATCH ossl_kdf_krb5kdf_functions[] = { |
| 235 |
{ OSSL_FUNC_KDF_NEWCTX, (void (*)(void))krb5kdf_new }, |
| 236 |
{ OSSL_FUNC_KDF_DUPCTX, (void (*)(void))krb5kdf_dup }, |
| 237 |
{ OSSL_FUNC_KDF_FREECTX, (void (*)(void))krb5kdf_free }, |
| 238 |
{ OSSL_FUNC_KDF_RESET, (void (*)(void))krb5kdf_reset }, |
| 239 |
{ OSSL_FUNC_KDF_DERIVE, (void (*)(void))krb5kdf_derive }, |
| 240 |
{ OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, |
| 241 |
(void (*)(void))krb5kdf_settable_ctx_params }, |
| 242 |
{ OSSL_FUNC_KDF_SET_CTX_PARAMS, |
| 243 |
(void (*)(void))krb5kdf_set_ctx_params }, |
| 244 |
{ OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, |
| 245 |
(void (*)(void))krb5kdf_gettable_ctx_params }, |
| 246 |
{ OSSL_FUNC_KDF_GET_CTX_PARAMS, |
| 247 |
(void (*)(void))krb5kdf_get_ctx_params }, |
| 248 |
OSSL_DISPATCH_END |
| 249 |
}; |
| 250 |
|
| 251 |
#ifndef OPENSSL_NO_DES |
| 252 |
/* |
| 253 |
* DES3 is a special case, it requires a random-to-key function and its |
| 254 |
* input truncated to 21 bytes of the 24 produced by the cipher. |
| 255 |
* See RFC3961 6.3.1 |
| 256 |
*/ |
| 257 |
static int fixup_des3_key(unsigned char *key) |
| 258 |
{ |
| 259 |
unsigned char *cblock; |
| 260 |
int i, j; |
| 261 |
|
| 262 |
for (i = 2; i >= 0; i--) { |
| 263 |
cblock = &key[i * 8]; |
| 264 |
memmove(cblock, &key[i * 7], 7); |
| 265 |
cblock[7] = 0; |
| 266 |
for (j = 0; j < 7; j++) |
| 267 |
cblock[7] |= (cblock[j] & 1) << (j + 1); |
| 268 |
DES_set_odd_parity((DES_cblock *)cblock); |
| 269 |
} |
| 270 |
|
| 271 |
/* fail if keys are such that triple des degrades to single des */ |
| 272 |
if (CRYPTO_memcmp(&key[0], &key[8], 8) == 0 || CRYPTO_memcmp(&key[8], &key[16], 8) == 0) { |
| 273 |
return 0; |
| 274 |
} |
| 275 |
|
| 276 |
return 1; |
| 277 |
} |
| 278 |
#endif |
| 279 |
|
| 280 |
/* |
| 281 |
* N-fold(K) where blocksize is N, and constant_len is K |
| 282 |
* Note: Here |= denotes concatenation |
| 283 |
* |
| 284 |
* L = lcm(N,K) |
| 285 |
* R = L/K |
| 286 |
* |
| 287 |
* for r: 1 -> R |
| 288 |
* s |= constant rot 13*(r-1)) |
| 289 |
* |
| 290 |
* block = 0 |
| 291 |
* for k: 1 -> K |
| 292 |
* block += s[N(k-1)..(N-1)k] (ones'-complement addition) |
| 293 |
* |
| 294 |
* Optimizing for space we compute: |
| 295 |
* for each l in L-1 -> 0: |
| 296 |
* s[l] = (constant rot 13*(l/K))[l%k] |
| 297 |
* block[l % N] += s[l] (with carry) |
| 298 |
* finally add carry if any |
| 299 |
*/ |
| 300 |
static void n_fold(unsigned char *block, unsigned int blocksize, |
| 301 |
const unsigned char *constant, unsigned int constant_len) |
| 302 |
{ |
| 303 |
unsigned int tmp, gcd, remainder, lcm, carry; |
| 304 |
int b, l; |
| 305 |
|
| 306 |
if (constant_len == blocksize) { |
| 307 |
memcpy(block, constant, constant_len); |
| 308 |
return; |
| 309 |
} |
| 310 |
|
| 311 |
/* Least Common Multiple of lengths: LCM(a,b)*/ |
| 312 |
gcd = blocksize; |
| 313 |
remainder = constant_len; |
| 314 |
/* Calculate Great Common Divisor first GCD(a,b) */ |
| 315 |
while (remainder != 0) { |
| 316 |
tmp = gcd % remainder; |
| 317 |
gcd = remainder; |
| 318 |
remainder = tmp; |
| 319 |
} |
| 320 |
/* resulting a is the GCD, LCM(a,b) = |a*b|/GCD(a,b) */ |
| 321 |
lcm = blocksize * constant_len / gcd; |
| 322 |
|
| 323 |
/* now spread out the bits */ |
| 324 |
memset(block, 0, blocksize); |
| 325 |
|
| 326 |
/* last to first to be able to bring carry forward */ |
| 327 |
carry = 0; |
| 328 |
for (l = lcm - 1; l >= 0; l--) { |
| 329 |
unsigned int rotbits, rshift, rbyte; |
| 330 |
|
| 331 |
/* destination byte in block is l % N */ |
| 332 |
b = l % blocksize; |
| 333 |
/* Our virtual s buffer is R = L/K long (K = constant_len) */ |
| 334 |
/* So we rotate backwards from R-1 to 0 (none) rotations */ |
| 335 |
rotbits = 13 * (l / constant_len); |
| 336 |
/* find the byte on s where rotbits falls onto */ |
| 337 |
rbyte = l - (rotbits / 8); |
| 338 |
/* calculate how much shift on that byte */ |
| 339 |
rshift = rotbits & 0x07; |
| 340 |
/* rbyte % constant_len gives us the unrotated byte in the |
| 341 |
* constant buffer, get also the previous byte then |
| 342 |
* appropriately shift them to get the rotated byte we need */ |
| 343 |
tmp = (constant[(rbyte - 1) % constant_len] << (8 - rshift) |
| 344 |
| constant[rbyte % constant_len] >> rshift) |
| 345 |
& 0xff; |
| 346 |
/* add with carry to any value placed by previous passes */ |
| 347 |
tmp += carry + block[b]; |
| 348 |
block[b] = tmp & 0xff; |
| 349 |
/* save any carry that may be left */ |
| 350 |
carry = tmp >> 8; |
| 351 |
} |
| 352 |
|
| 353 |
/* if any carry is left at the end, add it through the number */ |
| 354 |
for (b = blocksize - 1; b >= 0 && carry != 0; b--) { |
| 355 |
carry += block[b]; |
| 356 |
block[b] = carry & 0xff; |
| 357 |
carry >>= 8; |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
static int cipher_init(EVP_CIPHER_CTX *ctx, |
| 362 |
const EVP_CIPHER *cipher, ENGINE *engine, |
| 363 |
const unsigned char *key, size_t key_len) |
| 364 |
{ |
| 365 |
int klen, ret; |
| 366 |
|
| 367 |
ret = EVP_EncryptInit_ex(ctx, cipher, engine, NULL, NULL); |
| 368 |
if (!ret) |
| 369 |
goto out; |
| 370 |
/* set the key len for the odd variable key len cipher */ |
| 371 |
klen = EVP_CIPHER_CTX_get_key_length(ctx); |
| 372 |
if (key_len != (size_t)klen) { |
| 373 |
ret = EVP_CIPHER_CTX_set_key_length(ctx, (int)key_len); |
| 374 |
if (ret <= 0) { |
| 375 |
ret = 0; |
| 376 |
goto out; |
| 377 |
} |
| 378 |
} |
| 379 |
ret = EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL); |
| 380 |
if (!ret) |
| 381 |
goto out; |
| 382 |
/* we never want padding, either the length requested is a multiple of |
| 383 |
* the cipher block size or we are passed a cipher that can cope with |
| 384 |
* partial blocks via techniques like cipher text stealing */ |
| 385 |
ret = EVP_CIPHER_CTX_set_padding(ctx, 0); |
| 386 |
if (!ret) |
| 387 |
goto out; |
| 388 |
|
| 389 |
out: |
| 390 |
return ret; |
| 391 |
} |
| 392 |
|
| 393 |
static int KRB5KDF(const EVP_CIPHER *cipher, ENGINE *engine, |
| 394 |
const unsigned char *key, size_t key_len, |
| 395 |
const unsigned char *constant, size_t constant_len, |
| 396 |
unsigned char *okey, size_t okey_len) |
| 397 |
{ |
| 398 |
EVP_CIPHER_CTX *ctx = NULL; |
| 399 |
unsigned char block[EVP_MAX_BLOCK_LENGTH * 2]; |
| 400 |
unsigned char *plainblock, *cipherblock; |
| 401 |
size_t blocksize; |
| 402 |
size_t cipherlen; |
| 403 |
size_t osize; |
| 404 |
#ifndef OPENSSL_NO_DES |
| 405 |
int des3_no_fixup = 0; |
| 406 |
#endif |
| 407 |
int ret; |
| 408 |
|
| 409 |
if (key_len != okey_len) { |
| 410 |
#ifndef OPENSSL_NO_DES |
| 411 |
/* special case for 3des, where the caller may be requesting |
| 412 |
* the random raw key, instead of the fixed up key */ |
| 413 |
if (EVP_CIPHER_get_nid(cipher) == NID_des_ede3_cbc && key_len == 24 && okey_len == 21) { |
| 414 |
des3_no_fixup = 1; |
| 415 |
} else { |
| 416 |
#endif |
| 417 |
ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_OUTPUT_BUFFER_SIZE); |
| 418 |
return 0; |
| 419 |
#ifndef OPENSSL_NO_DES |
| 420 |
} |
| 421 |
#endif |
| 422 |
} |
| 423 |
|
| 424 |
ctx = EVP_CIPHER_CTX_new(); |
| 425 |
if (ctx == NULL) |
| 426 |
return 0; |
| 427 |
|
| 428 |
ret = cipher_init(ctx, cipher, engine, key, key_len); |
| 429 |
if (!ret) |
| 430 |
goto out; |
| 431 |
|
| 432 |
/* Initialize input block */ |
| 433 |
blocksize = EVP_CIPHER_CTX_get_block_size(ctx); |
| 434 |
|
| 435 |
if (blocksize == 0) { |
| 436 |
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CIPHER); |
| 437 |
ret = 0; |
| 438 |
goto out; |
| 439 |
} |
| 440 |
|
| 441 |
if (constant_len > blocksize) { |
| 442 |
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CONSTANT_LENGTH); |
| 443 |
ret = 0; |
| 444 |
goto out; |
| 445 |
} |
| 446 |
|
| 447 |
n_fold(block, (unsigned int)blocksize, constant, (unsigned int)constant_len); |
| 448 |
plainblock = block; |
| 449 |
cipherblock = block + EVP_MAX_BLOCK_LENGTH; |
| 450 |
|
| 451 |
for (osize = 0; osize < okey_len; osize += cipherlen) { |
| 452 |
int olen; |
| 453 |
|
| 454 |
ret = EVP_EncryptUpdate(ctx, cipherblock, &olen, |
| 455 |
plainblock, (int)blocksize); |
| 456 |
if (!ret) |
| 457 |
goto out; |
| 458 |
cipherlen = olen; |
| 459 |
ret = EVP_EncryptFinal_ex(ctx, cipherblock, &olen); |
| 460 |
if (!ret) |
| 461 |
goto out; |
| 462 |
if (olen != 0) { |
| 463 |
ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_FINAL_BLOCK_LENGTH); |
| 464 |
ret = 0; |
| 465 |
goto out; |
| 466 |
} |
| 467 |
|
| 468 |
/* write cipherblock out */ |
| 469 |
if (cipherlen > okey_len - osize) |
| 470 |
cipherlen = okey_len - osize; |
| 471 |
memcpy(okey + osize, cipherblock, cipherlen); |
| 472 |
|
| 473 |
if (okey_len > osize + cipherlen) { |
| 474 |
/* we need to reinitialize cipher context per spec */ |
| 475 |
ret = EVP_CIPHER_CTX_reset(ctx); |
| 476 |
if (!ret) |
| 477 |
goto out; |
| 478 |
ret = cipher_init(ctx, cipher, engine, key, key_len); |
| 479 |
if (!ret) |
| 480 |
goto out; |
| 481 |
|
| 482 |
/* also swap block offsets so last ciphertext becomes new |
| 483 |
* plaintext */ |
| 484 |
plainblock = cipherblock; |
| 485 |
if (cipherblock == block) { |
| 486 |
cipherblock += EVP_MAX_BLOCK_LENGTH; |
| 487 |
} else { |
| 488 |
cipherblock = block; |
| 489 |
} |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
#ifndef OPENSSL_NO_DES |
| 494 |
if (EVP_CIPHER_get_nid(cipher) == NID_des_ede3_cbc && !des3_no_fixup) { |
| 495 |
ret = fixup_des3_key(okey); |
| 496 |
if (!ret) { |
| 497 |
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY); |
| 498 |
goto out; |
| 499 |
} |
| 500 |
} |
| 501 |
#endif |
| 502 |
|
| 503 |
ret = 1; |
| 504 |
|
| 505 |
out: |
| 506 |
EVP_CIPHER_CTX_free(ctx); |
| 507 |
OPENSSL_cleanse(block, EVP_MAX_BLOCK_LENGTH * 2); |
| 508 |
return ret; |
| 509 |
} |