2 * Copyright 2007-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
11 #include "internal/cryptlib.h"
12 #include <openssl/x509.h>
13 #include <openssl/x509v3.h>
14 #include <openssl/evp.h>
15 #include <openssl/hmac.h>
16 #include <openssl/err.h>
17 #include "internal/evp_int.h"
19 /* HMAC pkey context structure */
22 const EVP_MD *md; /* MD for HMAC use */
23 ASN1_OCTET_STRING ktmp; /* Temp storage for key */
27 static int pkey_hmac_init(EVP_PKEY_CTX *ctx)
31 if ((hctx = OPENSSL_zalloc(sizeof(*hctx))) == NULL) {
32 CRYPTOerr(CRYPTO_F_PKEY_HMAC_INIT, ERR_R_MALLOC_FAILURE);
35 hctx->ktmp.type = V_ASN1_OCTET_STRING;
36 hctx->ctx = HMAC_CTX_new();
37 if (hctx->ctx == NULL) {
43 ctx->keygen_info_count = 0;
48 static void pkey_hmac_cleanup(EVP_PKEY_CTX *ctx);
50 static int pkey_hmac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
52 HMAC_PKEY_CTX *sctx, *dctx;
54 /* allocate memory for dst->data and a new HMAC_CTX in dst->data->ctx */
55 if (!pkey_hmac_init(dst))
57 sctx = EVP_PKEY_CTX_get_data(src);
58 dctx = EVP_PKEY_CTX_get_data(dst);
60 if (!HMAC_CTX_copy(dctx->ctx, sctx->ctx))
62 if (sctx->ktmp.data) {
63 if (!ASN1_OCTET_STRING_set(&dctx->ktmp,
64 sctx->ktmp.data, sctx->ktmp.length))
69 /* release HMAC_CTX in dst->data->ctx and memory allocated for dst->data */
70 pkey_hmac_cleanup (dst);
74 static void pkey_hmac_cleanup(EVP_PKEY_CTX *ctx)
76 HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
79 HMAC_CTX_free(hctx->ctx);
80 OPENSSL_clear_free(hctx->ktmp.data, hctx->ktmp.length);
82 EVP_PKEY_CTX_set_data(ctx, NULL);
86 static int pkey_hmac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
88 ASN1_OCTET_STRING *hkey = NULL;
89 HMAC_PKEY_CTX *hctx = ctx->data;
92 hkey = ASN1_OCTET_STRING_dup(&hctx->ktmp);
95 EVP_PKEY_assign(pkey, EVP_PKEY_HMAC, hkey);
100 static int int_update(EVP_MD_CTX *ctx, const void *data, size_t count)
102 HMAC_PKEY_CTX *hctx = EVP_MD_CTX_pkey_ctx(ctx)->data;
103 if (!HMAC_Update(hctx->ctx, data, count))
108 static int hmac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
110 HMAC_PKEY_CTX *hctx = ctx->data;
111 HMAC_CTX_set_flags(hctx->ctx,
112 EVP_MD_CTX_test_flags(mctx, ~EVP_MD_CTX_FLAG_NO_INIT));
113 EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT);
114 EVP_MD_CTX_set_update_fn(mctx, int_update);
118 static int hmac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
122 HMAC_PKEY_CTX *hctx = ctx->data;
123 int l = EVP_MD_CTX_size(mctx);
131 if (!HMAC_Final(hctx->ctx, sig, &hlen))
133 *siglen = (size_t)hlen;
137 static int pkey_hmac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
139 HMAC_PKEY_CTX *hctx = ctx->data;
140 ASN1_OCTET_STRING *key;
143 case EVP_PKEY_CTRL_SET_MAC_KEY:
144 if ((!p2 && p1 > 0) || (p1 < -1))
146 if (!ASN1_OCTET_STRING_set(&hctx->ktmp, p2, p1))
150 case EVP_PKEY_CTRL_MD:
154 case EVP_PKEY_CTRL_DIGESTINIT:
155 key = (ASN1_OCTET_STRING *)ctx->pkey->pkey.ptr;
156 if (!HMAC_Init_ex(hctx->ctx, key->data, key->length, hctx->md,
168 static int pkey_hmac_ctrl_str(EVP_PKEY_CTX *ctx,
169 const char *type, const char *value)
174 if (strcmp(type, "key") == 0)
175 return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, value);
176 if (strcmp(type, "hexkey") == 0)
177 return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, value);
181 const EVP_PKEY_METHOD hmac_pkey_meth = {