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/evp.h>
13 #include "internal/asn1_int.h"
15 #define HMAC_TEST_PRIVATE_KEY_FORMAT
18 * HMAC "ASN1" method. This is just here to indicate the maximum HMAC output
19 * length and to free up an HMAC key.
22 static int hmac_size(const EVP_PKEY *pkey)
24 return EVP_MAX_MD_SIZE;
27 static void hmac_key_free(EVP_PKEY *pkey)
29 ASN1_OCTET_STRING *os = EVP_PKEY_get0(pkey);
32 OPENSSL_cleanse(os->data, os->length);
33 ASN1_OCTET_STRING_free(os);
37 static int hmac_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
40 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
41 *(int *)arg2 = NID_sha256;
49 static int hmac_pkey_public_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
51 return ASN1_OCTET_STRING_cmp(EVP_PKEY_get0(a), EVP_PKEY_get0(b));
54 #ifdef HMAC_TEST_PRIVATE_KEY_FORMAT
56 * A bogus private key format for test purposes. This is simply the HMAC key
57 * with "HMAC PRIVATE KEY" in the headers. When enabled the genpkey utility
58 * can be used to "generate" HMAC keys.
61 static int old_hmac_decode(EVP_PKEY *pkey,
62 const unsigned char **pder, int derlen)
64 ASN1_OCTET_STRING *os;
65 os = ASN1_OCTET_STRING_new();
66 if (os == NULL || !ASN1_OCTET_STRING_set(os, *pder, derlen))
68 if (!EVP_PKEY_assign(pkey, EVP_PKEY_HMAC, os))
73 ASN1_OCTET_STRING_free(os);
77 static int old_hmac_encode(const EVP_PKEY *pkey, unsigned char **pder)
80 ASN1_OCTET_STRING *os = EVP_PKEY_get0(pkey);
83 *pder = OPENSSL_malloc(os->length);
90 memcpy(*pder, os->data, os->length);
101 const EVP_PKEY_ASN1_METHOD hmac_asn1_meth = {
107 "OpenSSL HMAC method",
109 0, 0, hmac_pkey_public_cmp, 0,
119 #ifdef HMAC_TEST_PRIVATE_KEY_FORMAT