mac_lib.c 4.6 KB
Newer Older
R
Richard Levitte 已提交
1
/*
M
Matt Caswell 已提交
2
 * Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved.
R
Richard Levitte 已提交
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
R
Richard Levitte 已提交
5 6 7 8 9 10 11 12 13
 * 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/evp.h>
#include <openssl/err.h>
14 15
#include <openssl/core.h>
#include <openssl/core_names.h>
16
#include <openssl/types.h>
R
Richard Levitte 已提交
17
#include "internal/nelem.h"
18
#include "crypto/evp.h"
19
#include "internal/provider.h"
20
#include "evp_local.h"
R
Richard Levitte 已提交
21

22
EVP_MAC_CTX *EVP_MAC_CTX_new(EVP_MAC *mac)
R
Richard Levitte 已提交
23 24 25
{
    EVP_MAC_CTX *ctx = OPENSSL_zalloc(sizeof(EVP_MAC_CTX));

26 27 28
    if (ctx == NULL
        || (ctx->data = mac->newctx(ossl_provider_ctx(mac->prov))) == NULL
        || !EVP_MAC_up_ref(mac)) {
29
        ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
30 31
        if (ctx != NULL)
            mac->freectx(ctx->data);
R
Richard Levitte 已提交
32 33 34 35 36 37 38 39
        OPENSSL_free(ctx);
        ctx = NULL;
    } else {
        ctx->meth = mac;
    }
    return ctx;
}

40
void EVP_MAC_CTX_free(EVP_MAC_CTX *ctx)
R
Richard Levitte 已提交
41
{
42 43 44 45 46 47
    if (ctx == NULL)
        return;
    ctx->meth->freectx(ctx->data);
    ctx->data = NULL;
    /* refcnt-- */
    EVP_MAC_free(ctx->meth);
R
Richard Levitte 已提交
48 49 50
    OPENSSL_free(ctx);
}

51
EVP_MAC_CTX *EVP_MAC_CTX_dup(const EVP_MAC_CTX *src)
R
Richard Levitte 已提交
52
{
53
    EVP_MAC_CTX *dst;
R
Richard Levitte 已提交
54

55
    if (src->data == NULL)
56 57 58 59
        return NULL;

    dst = OPENSSL_malloc(sizeof(*dst));
    if (dst == NULL) {
60
        ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
61 62
        return NULL;
    }
R
Richard Levitte 已提交
63 64

    *dst = *src;
65
    if (!EVP_MAC_up_ref(dst->meth)) {
66
        ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
67 68 69
        OPENSSL_free(dst);
        return NULL;
    }
R
Richard Levitte 已提交
70

71
    dst->data = src->meth->dupctx(src->data);
72
    if (dst->data == NULL) {
73
        EVP_MAC_CTX_free(dst);
74 75 76 77
        return NULL;
    }

    return dst;
R
Richard Levitte 已提交
78 79
}

80
EVP_MAC *EVP_MAC_CTX_mac(EVP_MAC_CTX *ctx)
R
Richard Levitte 已提交
81 82 83 84
{
    return ctx->meth;
}

85
size_t EVP_MAC_CTX_get_mac_size(EVP_MAC_CTX *ctx)
R
Richard Levitte 已提交
86
{
87 88 89 90 91
    size_t sz = 0;

    if (ctx->data != NULL) {
        OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };

92
        params[0] = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, &sz);
93 94
        if (ctx->meth->get_ctx_params != NULL) {
            if (ctx->meth->get_ctx_params(ctx->data, params))
95 96 97 98 99 100 101 102 103 104
                return sz;
        } else if (ctx->meth->get_params != NULL) {
            if (ctx->meth->get_params(params))
                return sz;
        }
    }
    /*
     * If the MAC hasn't been initialized yet, or there is no size to get,
     * we return zero
     */
R
Richard Levitte 已提交
105 106 107
    return 0;
}

108 109
int EVP_MAC_init(EVP_MAC_CTX *ctx, const unsigned char *key, size_t keylen,
                 const OSSL_PARAM params[])
R
Richard Levitte 已提交
110
{
111
    return ctx->meth->init(ctx->data, key, keylen, params);
R
Richard Levitte 已提交
112 113 114 115 116 117 118
}

int EVP_MAC_update(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen)
{
    return ctx->meth->update(ctx->data, data, datalen);
}

119 120
int EVP_MAC_final(EVP_MAC_CTX *ctx,
                  unsigned char *out, size_t *outl, size_t outsize)
R
Richard Levitte 已提交
121
{
122
    size_t l;
123
    int res = 1;
R
Richard Levitte 已提交
124

125 126
    if (out != NULL)
        res = ctx->meth->final(ctx->data, out, &l, outsize);
127
    else
128
        l = EVP_MAC_CTX_get_mac_size(ctx);
129 130
    if (outl != NULL)
        *outl = l;
131
    return res;
R
Richard Levitte 已提交
132 133
}

134 135 136 137 138 139 140
/*
 * The {get,set}_params functions return 1 if there is no corresponding
 * function in the implementation.  This is the same as if there was one,
 * but it didn't recognise any of the given params, i.e. nothing in the
 * bag of parameters was useful.
 */
int EVP_MAC_get_params(EVP_MAC *mac, OSSL_PARAM params[])
R
Richard Levitte 已提交
141
{
142 143 144
    if (mac->get_params != NULL)
        return mac->get_params(params);
    return 1;
R
Richard Levitte 已提交
145 146
}

147
int EVP_MAC_CTX_get_params(EVP_MAC_CTX *ctx, OSSL_PARAM params[])
R
Richard Levitte 已提交
148
{
149 150
    if (ctx->meth->get_ctx_params != NULL)
        return ctx->meth->get_ctx_params(ctx->data, params);
151
    return 1;
R
Richard Levitte 已提交
152 153
}

154
int EVP_MAC_CTX_set_params(EVP_MAC_CTX *ctx, const OSSL_PARAM params[])
R
Richard Levitte 已提交
155
{
156 157
    if (ctx->meth->set_ctx_params != NULL)
        return ctx->meth->set_ctx_params(ctx->data, params);
158
    return 1;
R
Richard Levitte 已提交
159
}
160

161 162 163 164 165
int EVP_MAC_number(const EVP_MAC *mac)
{
    return mac->name_id;
}

166 167
const char *EVP_MAC_name(const EVP_MAC *mac)
{
168
    return mac->type_name;
169 170
}

171 172 173 174 175
const char *EVP_MAC_description(const EVP_MAC *mac)
{
    return mac->description;
}

176 177
int EVP_MAC_is_a(const EVP_MAC *mac, const char *name)
{
178
    return evp_is_a(mac->prov, mac->name_id, NULL, name);
179
}
180

181 182 183
int EVP_MAC_names_do_all(const EVP_MAC *mac,
                         void (*fn)(const char *name, void *data),
                         void *data)
184 185
{
    if (mac->prov != NULL)
186 187 188
        return evp_names_do_all(mac->prov, mac->name_id, fn, data);

    return 1;
189
}