]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/p5_crpt.c
Convert all {NAME}err() in crypto/ to their corresponding ERR_raise() call
[thirdparty/openssl.git] / crypto / evp / p5_crpt.c
CommitLineData
0f113f3e 1/*
33388b44 2 * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
ef8335d9 3 *
4a8b0c55 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
62867571
RS
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
ef8335d9
DSH
8 */
9
10#include <stdio.h>
11#include <stdlib.h>
b39fc560 12#include "internal/cryptlib.h"
ef8335d9
DSH
13#include <openssl/x509.h>
14#include <openssl/evp.h>
ef8335d9 15
0f113f3e
MC
16/*
17 * Doesn't do anything now: Builtin PBE algorithms in static table.
ef8335d9
DSH
18 */
19
20void PKCS5_PBE_add(void)
21{
ef8335d9
DSH
22}
23
2bd83ca1 24int PKCS5_PBE_keyivgen(EVP_CIPHER_CTX *cctx, const char *pass, int passlen,
0f113f3e
MC
25 ASN1_TYPE *param, const EVP_CIPHER *cipher,
26 const EVP_MD *md, int en_de)
ef8335d9 27{
77a01145 28 EVP_MD_CTX *ctx;
0f113f3e
MC
29 unsigned char md_tmp[EVP_MAX_MD_SIZE];
30 unsigned char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH];
a05bf83c 31 int i, ivl, kl;
0f113f3e
MC
32 PBEPARAM *pbe;
33 int saltlen, iter;
34 unsigned char *salt;
0f113f3e
MC
35 int mdsize;
36 int rv = 0;
69cbf468 37
0f113f3e
MC
38 /* Extract useful info from parameter */
39 if (param == NULL || param->type != V_ASN1_SEQUENCE ||
40 param->value.sequence == NULL) {
9311d0c4 41 ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
0f113f3e
MC
42 return 0;
43 }
c755c5fd 44
e93c8748
DSH
45 pbe = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBEPARAM), param);
46 if (pbe == NULL) {
9311d0c4 47 ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
0f113f3e
MC
48 return 0;
49 }
69cbf468 50
a05bf83c
P
51 ivl = EVP_CIPHER_iv_length(cipher);
52 if (ivl < 0 || ivl > 16) {
9311d0c4 53 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_IV_LENGTH);
adc9086b 54 PBEPARAM_free(pbe);
a05bf83c
P
55 return 0;
56 }
57 kl = EVP_CIPHER_key_length(cipher);
58 if (kl < 0 || kl > (int)sizeof(md_tmp)) {
9311d0c4 59 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
adc9086b 60 PBEPARAM_free(pbe);
a05bf83c
P
61 return 0;
62 }
63
12a765a5 64 if (pbe->iter == NULL)
0f113f3e
MC
65 iter = 1;
66 else
67 iter = ASN1_INTEGER_get(pbe->iter);
68 salt = pbe->salt->data;
69 saltlen = pbe->salt->length;
69cbf468 70
12a765a5 71 if (pass == NULL)
0f113f3e
MC
72 passlen = 0;
73 else if (passlen == -1)
74 passlen = strlen(pass);
a331a305 75
bfb0641f 76 ctx = EVP_MD_CTX_new();
77a01145 77 if (ctx == NULL) {
9311d0c4 78 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
0f113f3e 79 goto err;
77a01145
RL
80 }
81
82 if (!EVP_DigestInit_ex(ctx, md, NULL))
83 goto err;
84 if (!EVP_DigestUpdate(ctx, pass, passlen))
0f113f3e 85 goto err;
77a01145 86 if (!EVP_DigestUpdate(ctx, salt, saltlen))
0f113f3e
MC
87 goto err;
88 PBEPARAM_free(pbe);
adc9086b 89 pbe = NULL;
77a01145 90 if (!EVP_DigestFinal_ex(ctx, md_tmp, NULL))
0f113f3e
MC
91 goto err;
92 mdsize = EVP_MD_size(md);
93 if (mdsize < 0)
30f3b4e1 94 goto err;
0f113f3e 95 for (i = 1; i < iter; i++) {
77a01145 96 if (!EVP_DigestInit_ex(ctx, md, NULL))
0f113f3e 97 goto err;
77a01145 98 if (!EVP_DigestUpdate(ctx, md_tmp, mdsize))
0f113f3e 99 goto err;
77a01145 100 if (!EVP_DigestFinal_ex(ctx, md_tmp, NULL))
0f113f3e
MC
101 goto err;
102 }
a05bf83c
P
103 memcpy(key, md_tmp, kl);
104 memcpy(iv, md_tmp + (16 - ivl), ivl);
0f113f3e
MC
105 if (!EVP_CipherInit_ex(cctx, cipher, NULL, key, iv, en_de))
106 goto err;
107 OPENSSL_cleanse(md_tmp, EVP_MAX_MD_SIZE);
108 OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH);
109 OPENSSL_cleanse(iv, EVP_MAX_IV_LENGTH);
110 rv = 1;
111 err:
adc9086b 112 PBEPARAM_free(pbe);
bfb0641f 113 EVP_MD_CTX_free(ctx);
0f113f3e 114 return rv;
ef8335d9 115}