2 * Copyright 2007-2016 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 "internal/evp_int.h"
18 /* HMAC pkey context structure */
21 const EVP_MD *md; /* MD for HMAC use */
22 ASN1_OCTET_STRING ktmp; /* Temp storage for key */
26 static int pkey_hmac_init(EVP_PKEY_CTX *ctx)
30 hctx = OPENSSL_zalloc(sizeof(*hctx));
33 hctx->ktmp.type = V_ASN1_OCTET_STRING;
34 hctx->ctx = HMAC_CTX_new();
35 if (hctx->ctx == NULL) {
41 ctx->keygen_info_count = 0;
46 static void pkey_hmac_cleanup(EVP_PKEY_CTX *ctx);
48 static int pkey_hmac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
50 HMAC_PKEY_CTX *sctx, *dctx;
52 /* allocate memory for dst->data and a new HMAC_CTX in dst->data->ctx */
53 if (!pkey_hmac_init(dst))
55 sctx = EVP_PKEY_CTX_get_data(src);
56 dctx = EVP_PKEY_CTX_get_data(dst);
58 if (!HMAC_CTX_copy(dctx->ctx, sctx->ctx))
60 if (sctx->ktmp.data) {
61 if (!ASN1_OCTET_STRING_set(&dctx->ktmp,
62 sctx->ktmp.data, sctx->ktmp.length))
67 /* release HMAC_CTX in dst->data->ctx and memory allocated for dst->data */
68 pkey_hmac_cleanup (dst);
72 static void pkey_hmac_cleanup(EVP_PKEY_CTX *ctx)
74 HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
77 HMAC_CTX_free(hctx->ctx);
78 OPENSSL_clear_free(hctx->ktmp.data, hctx->ktmp.length);
80 EVP_PKEY_CTX_set_data(ctx, NULL);
84 static int pkey_hmac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
86 ASN1_OCTET_STRING *hkey = NULL;
87 HMAC_PKEY_CTX *hctx = ctx->data;
90 hkey = ASN1_OCTET_STRING_dup(&hctx->ktmp);
93 EVP_PKEY_assign(pkey, EVP_PKEY_HMAC, hkey);
98 static int int_update(EVP_MD_CTX *ctx, const void *data, size_t count)
100 HMAC_PKEY_CTX *hctx = EVP_MD_CTX_pkey_ctx(ctx)->data;
101 if (!HMAC_Update(hctx->ctx, data, count))
106 static int hmac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
108 HMAC_PKEY_CTX *hctx = ctx->data;
109 HMAC_CTX_set_flags(hctx->ctx,
110 EVP_MD_CTX_test_flags(mctx, ~EVP_MD_CTX_FLAG_NO_INIT));
111 EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT);
112 EVP_MD_CTX_set_update_fn(mctx, int_update);
116 static int hmac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
120 HMAC_PKEY_CTX *hctx = ctx->data;
121 int l = EVP_MD_CTX_size(mctx);
129 if (!HMAC_Final(hctx->ctx, sig, &hlen))
131 *siglen = (size_t)hlen;
135 static int pkey_hmac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
137 HMAC_PKEY_CTX *hctx = ctx->data;
138 ASN1_OCTET_STRING *key;
141 case EVP_PKEY_CTRL_SET_MAC_KEY:
142 if ((!p2 && p1 > 0) || (p1 < -1))
144 if (!ASN1_OCTET_STRING_set(&hctx->ktmp, p2, p1))
148 case EVP_PKEY_CTRL_MD:
152 case EVP_PKEY_CTRL_DIGESTINIT:
153 key = (ASN1_OCTET_STRING *)ctx->pkey->pkey.ptr;
154 if (!HMAC_Init_ex(hctx->ctx, key->data, key->length, hctx->md,
166 static int pkey_hmac_ctrl_str(EVP_PKEY_CTX *ctx,
167 const char *type, const char *value)
172 if (strcmp(type, "key") == 0)
173 return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, value);
174 if (strcmp(type, "hexkey") == 0)
175 return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, value);
179 const EVP_PKEY_METHOD hmac_pkey_meth = {