static int kek_wrap_key(unsigned char *out, size_t *outlen,
                        const unsigned char *in, size_t inlen,
                        EVP_CIPHER_CTX *ctx)
{
    size_t blocklen = EVP_CIPHER_CTX_block_size(ctx);
    size_t olen;
    int dummy;
    olen = (inlen + 4 + blocklen - 1) / blocklen;
    olen *= blocklen;
    if (olen < 2 * blocklen) {
        return 0;
    }
    if (inlen > 0xFF) {
        return 0;
    }
    if (out) {
        out[0] = (unsigned char)inlen;
        out[1] = in[0] ^ 0xFF;
        out[2] = in[1] ^ 0xFF;
        out[3] = in[2] ^ 0xFF;
        memcpy(out + 4, in, inlen);
        if (olen > inlen + 4
            && RAND_bytes(out + 4 + inlen, olen - 4 - inlen) <= 0)
            return 0;
        if (!EVP_EncryptUpdate(ctx, out, &dummy, out, olen)
            || !EVP_EncryptUpdate(ctx, out, &dummy, out, olen))
            return 0;
    }
    *outlen = olen;
    return 1;
}
['int cms_RecipientInfo_pwri_crypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,\n                                 int en_de)\n{\n    CMS_EncryptedContentInfo *ec;\n    CMS_PasswordRecipientInfo *pwri;\n    int r = 0;\n    X509_ALGOR *algtmp, *kekalg = NULL;\n    EVP_CIPHER_CTX *kekctx;\n    const EVP_CIPHER *kekcipher;\n    unsigned char *key = NULL;\n    size_t keylen;\n    ec = cms->d.envelopedData->encryptedContentInfo;\n    pwri = ri->d.pwri;\n    kekctx = EVP_CIPHER_CTX_new();\n    if (!pwri->pass) {\n        CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_NO_PASSWORD);\n        return 0;\n    }\n    algtmp = pwri->keyEncryptionAlgorithm;\n    if (!algtmp || OBJ_obj2nid(algtmp->algorithm) != NID_id_alg_PWRI_KEK) {\n        CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,\n               CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM);\n        return 0;\n    }\n    kekalg = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR),\n                                       algtmp->parameter);\n    if (kekalg == NULL) {\n        CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,\n               CMS_R_INVALID_KEY_ENCRYPTION_PARAMETER);\n        return 0;\n    }\n    kekcipher = EVP_get_cipherbyobj(kekalg->algorithm);\n    if (!kekcipher) {\n        CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_UNKNOWN_CIPHER);\n        goto err;\n    }\n    if (!EVP_CipherInit_ex(kekctx, kekcipher, NULL, NULL, NULL, en_de))\n        goto err;\n    EVP_CIPHER_CTX_set_padding(kekctx, 0);\n    if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) < 0) {\n        CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,\n               CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);\n        goto err;\n    }\n    algtmp = pwri->keyDerivationAlgorithm;\n    if (EVP_PBE_CipherInit(algtmp->algorithm,\n                           (char *)pwri->pass, pwri->passlen,\n                           algtmp->parameter, kekctx, en_de) < 0) {\n        CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, ERR_R_EVP_LIB);\n        goto err;\n    }\n    if (en_de) {\n        if (!kek_wrap_key(NULL, &keylen, ec->key, ec->keylen, kekctx))\n            goto err;\n        key = OPENSSL_malloc(keylen);\n        if (key == NULL)\n            goto err;\n        if (!kek_wrap_key(key, &keylen, ec->key, ec->keylen, kekctx))\n            goto err;\n        pwri->encryptedKey->data = key;\n        pwri->encryptedKey->length = keylen;\n    } else {\n        key = OPENSSL_malloc(pwri->encryptedKey->length);\n        if (key == NULL) {\n            CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, ERR_R_MALLOC_FAILURE);\n            goto err;\n        }\n        if (!kek_unwrap_key(key, &keylen,\n                            pwri->encryptedKey->data,\n                            pwri->encryptedKey->length, kekctx)) {\n            CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_UNWRAP_FAILURE);\n            goto err;\n        }\n        ec->key = key;\n        ec->keylen = keylen;\n    }\n    r = 1;\n err:\n    EVP_CIPHER_CTX_free(kekctx);\n    if (!r)\n        OPENSSL_free(key);\n    X509_ALGOR_free(kekalg);\n    return r;\n}', 'static int kek_wrap_key(unsigned char *out, size_t *outlen,\n                        const unsigned char *in, size_t inlen,\n                        EVP_CIPHER_CTX *ctx)\n{\n    size_t blocklen = EVP_CIPHER_CTX_block_size(ctx);\n    size_t olen;\n    int dummy;\n    olen = (inlen + 4 + blocklen - 1) / blocklen;\n    olen *= blocklen;\n    if (olen < 2 * blocklen) {\n        return 0;\n    }\n    if (inlen > 0xFF) {\n        return 0;\n    }\n    if (out) {\n        out[0] = (unsigned char)inlen;\n        out[1] = in[0] ^ 0xFF;\n        out[2] = in[1] ^ 0xFF;\n        out[3] = in[2] ^ 0xFF;\n        memcpy(out + 4, in, inlen);\n        if (olen > inlen + 4\n            && RAND_bytes(out + 4 + inlen, olen - 4 - inlen) <= 0)\n            return 0;\n        if (!EVP_EncryptUpdate(ctx, out, &dummy, out, olen)\n            || !EVP_EncryptUpdate(ctx, out, &dummy, out, olen))\n            return 0;\n    }\n    *outlen = olen;\n    return 1;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n    void *ret = NULL;\n    if (num <= 0)\n        return NULL;\n    allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n    if (call_malloc_debug) {\n        CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n        ret = malloc(num);\n        CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n    } else {\n        ret = malloc(num);\n    }\n#else\n    (void)file;\n    (void)line;\n    ret = malloc(num);\n#endif\n#ifndef OPENSSL_CPUID_OBJ\n    if (ret && (num > 2048)) {\n        extern unsigned char cleanse_ctr;\n        ((unsigned char *)ret)[0] = cleanse_ctr;\n    }\n#endif\n    return ret;\n}']