]> git.proxmox.com Git - efi-boot-shim.git/blame - Cryptlib/OpenSSL/crypto/hmac/hm_ameth.c
New upstream version 15+1533136590.3beb971
[efi-boot-shim.git] / Cryptlib / OpenSSL / crypto / hmac / hm_ameth.c
CommitLineData
d3819813 1/*
7bf7a6d0 2 * Copyright 2007-2016 The OpenSSL Project Authors. All Rights Reserved.
b2d0e06f 3 *
7bf7a6d0
MTL
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
b2d0e06f
MG
8 */
9
d3819813 10#include <stdio.h>
7bf7a6d0 11#include "internal/cryptlib.h"
d3819813 12#include <openssl/evp.h>
7bf7a6d0 13#include "internal/asn1_int.h"
d3819813
MTL
14
15#define HMAC_TEST_PRIVATE_KEY_FORMAT
16
17/*
18 * HMAC "ASN1" method. This is just here to indicate the maximum HMAC output
19 * length and to free up an HMAC key.
20 */
21
22static int hmac_size(const EVP_PKEY *pkey)
23{
24 return EVP_MAX_MD_SIZE;
b2d0e06f 25}
d3819813
MTL
26
27static void hmac_key_free(EVP_PKEY *pkey)
28{
7bf7a6d0 29 ASN1_OCTET_STRING *os = EVP_PKEY_get0(pkey);
d3819813
MTL
30 if (os) {
31 if (os->data)
32 OPENSSL_cleanse(os->data, os->length);
33 ASN1_OCTET_STRING_free(os);
34 }
35}
36
37static int hmac_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
38{
39 switch (op) {
40 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
41 *(int *)arg2 = NID_sha256;
42 return 1;
43
44 default:
45 return -2;
46 }
47}
48
7bf7a6d0
MTL
49static int hmac_pkey_public_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
50{
51 return ASN1_OCTET_STRING_cmp(EVP_PKEY_get0(a), EVP_PKEY_get0(b));
52}
53
d3819813
MTL
54#ifdef HMAC_TEST_PRIVATE_KEY_FORMAT
55/*
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.
59 */
60
61static int old_hmac_decode(EVP_PKEY *pkey,
62 const unsigned char **pder, int derlen)
63{
64 ASN1_OCTET_STRING *os;
65 os = ASN1_OCTET_STRING_new();
7bf7a6d0 66 if (os == NULL || !ASN1_OCTET_STRING_set(os, *pder, derlen))
62f0afa2
MTL
67 goto err;
68 if (!EVP_PKEY_assign(pkey, EVP_PKEY_HMAC, os))
69 goto err;
d3819813 70 return 1;
62f0afa2
MTL
71
72 err:
73 ASN1_OCTET_STRING_free(os);
74 return 0;
d3819813
MTL
75}
76
77static int old_hmac_encode(const EVP_PKEY *pkey, unsigned char **pder)
78{
79 int inc;
7bf7a6d0 80 ASN1_OCTET_STRING *os = EVP_PKEY_get0(pkey);
d3819813
MTL
81 if (pder) {
82 if (!*pder) {
83 *pder = OPENSSL_malloc(os->length);
7bf7a6d0
MTL
84 if (*pder == NULL)
85 return -1;
d3819813
MTL
86 inc = 0;
87 } else
88 inc = 1;
89
90 memcpy(*pder, os->data, os->length);
91
92 if (inc)
93 *pder += os->length;
94 }
95
96 return os->length;
97}
98
b2d0e06f 99#endif
d3819813
MTL
100
101const EVP_PKEY_ASN1_METHOD hmac_asn1_meth = {
102 EVP_PKEY_HMAC,
103 EVP_PKEY_HMAC,
104 0,
105
106 "HMAC",
107 "OpenSSL HMAC method",
108
7bf7a6d0 109 0, 0, hmac_pkey_public_cmp, 0,
d3819813
MTL
110
111 0, 0, 0,
112
113 hmac_size,
7bf7a6d0 114 0, 0,
d3819813
MTL
115 0, 0, 0, 0, 0, 0, 0,
116
117 hmac_key_free,
118 hmac_pkey_ctrl,
119#ifdef HMAC_TEST_PRIVATE_KEY_FORMAT
120 old_hmac_decode,
121 old_hmac_encode
122#else
123 0, 0
124#endif
125};