2 * Copyright 2007-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 * HMAC low level APIs are deprecated for public use, but still ok for internal
14 #include "internal/deprecated.h"
17 #include "internal/cryptlib.h"
18 #include <openssl/evp.h>
19 #include "crypto/asn1.h"
20 #include "crypto/evp.h"
23 * HMAC "ASN1" method. This is just here to indicate the maximum HMAC output
24 * length and to free up an HMAC key.
27 static int hmac_size(const EVP_PKEY *pkey)
29 return EVP_MAX_MD_SIZE;
32 static void hmac_key_free(EVP_PKEY *pkey)
34 ASN1_OCTET_STRING *os = EVP_PKEY_get0(pkey);
37 OPENSSL_cleanse(os->data, os->length);
38 ASN1_OCTET_STRING_free(os);
42 static int hmac_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
45 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
46 *(int *)arg2 = NID_sha256;
54 static int hmac_pkey_public_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
56 return ASN1_OCTET_STRING_cmp(EVP_PKEY_get0(a), EVP_PKEY_get0(b));
59 static int hmac_set_priv_key(EVP_PKEY *pkey, const unsigned char *priv,
62 ASN1_OCTET_STRING *os;
64 if (pkey->pkey.ptr != NULL)
67 os = ASN1_OCTET_STRING_new();
72 if (!ASN1_OCTET_STRING_set(os, priv, len)) {
73 ASN1_OCTET_STRING_free(os);
81 static int hmac_get_priv_key(const EVP_PKEY *pkey, unsigned char *priv,
84 ASN1_OCTET_STRING *os = (ASN1_OCTET_STRING *)pkey->pkey.ptr;
87 *len = ASN1_STRING_length(os);
91 if (os == NULL || *len < (size_t)ASN1_STRING_length(os))
94 *len = ASN1_STRING_length(os);
95 memcpy(priv, ASN1_STRING_get0_data(os), *len);
100 const EVP_PKEY_ASN1_METHOD hmac_asn1_meth = {
106 "OpenSSL HMAC method",
108 0, 0, hmac_pkey_public_cmp, 0,