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