summaryrefslogtreecommitdiffstats
path: root/crypto/kdf/kdf_util.c
blob: 8eb6d26b344133ef4514e194b2b8fb210c39dbce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
 * Copyright (c) 2018, Oracle and/or its affiliates.  All rights reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#include <string.h>
#include <stdarg.h>
#include <openssl/kdf.h>
#include <openssl/evp.h>
#include "internal/cryptlib.h"
#include "internal/evp_int.h"
#include "internal/numbers.h"
#include "kdf_local.h"

int call_ctrl(int (*ctrl)(EVP_KDF_IMPL *impl, int cmd, va_list args),
              EVP_KDF_IMPL *impl, int cmd, ...)
{
    int ret;
    va_list args;

    va_start(args, cmd);
    ret = ctrl(impl, cmd, args);
    va_end(args);

    return ret;
}

/* Utility functions to send a string or hex string to a ctrl */

int kdf_str2ctrl(EVP_KDF_IMPL *impl,
                 int (*ctrl)(EVP_KDF_IMPL *impl, int cmd, va_list args),
                 int cmd, const char *str)
{
    return call_ctrl(ctrl, impl, cmd, (const unsigned char *)str, strlen(str));
}

int kdf_hex2ctrl(EVP_KDF_IMPL *impl,
                 int (*ctrl)(EVP_KDF_IMPL *impl, int cmd, va_list args),
                 int cmd, const char *hex)
{
    unsigned char *bin;
    long binlen;
    int ret = -1;

    bin = OPENSSL_hexstr2buf(hex, &binlen);
    if (bin == NULL)
        return 0;

    if (binlen <= INT_MAX)
        ret = call_ctrl(ctrl, impl, cmd, bin, (size_t)binlen);
    OPENSSL_free(bin);
    return ret;
}

/* Pass a message digest to a ctrl */
int kdf_md2ctrl(EVP_KDF_IMPL *impl,
                int (*ctrl)(EVP_KDF_IMPL *impl, int cmd, va_list args),
                int cmd, const char *md_name)
{
    const EVP_MD *md;

    if (md_name == NULL || (md = EVP_get_digestbyname(md_name)) == NULL) {
        KDFerr(KDF_F_KDF_MD2CTRL, KDF_R_INVALID_DIGEST);
        return 0;
    }
    return call_ctrl(ctrl, impl, cmd, md);
}