Deprecate the low level HMAC functions
[openssl.git] / crypto / hmac / hm_ameth.c
1 /*
2  * Copyright 2007-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 /*
11  * HMAC low level APIs are deprecated for public use, but still ok for internal
12  * use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <stdio.h>
17 #include "internal/cryptlib.h"
18 #include <openssl/evp.h>
19 #include "crypto/asn1.h"
20 #include "crypto/evp.h"
21
22 /*
23  * HMAC "ASN1" method. This is just here to indicate the maximum HMAC output
24  * length and to free up an HMAC key.
25  */
26
27 static int hmac_size(const EVP_PKEY *pkey)
28 {
29     return EVP_MAX_MD_SIZE;
30 }
31
32 static void hmac_key_free(EVP_PKEY *pkey)
33 {
34     ASN1_OCTET_STRING *os = EVP_PKEY_get0(pkey);
35     if (os) {
36         if (os->data)
37             OPENSSL_cleanse(os->data, os->length);
38         ASN1_OCTET_STRING_free(os);
39     }
40 }
41
42 static int hmac_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
43 {
44     switch (op) {
45     case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
46         *(int *)arg2 = NID_sha256;
47         return 1;
48
49     default:
50         return -2;
51     }
52 }
53
54 static int hmac_pkey_public_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
55 {
56     return ASN1_OCTET_STRING_cmp(EVP_PKEY_get0(a), EVP_PKEY_get0(b));
57 }
58
59 static int hmac_set_priv_key(EVP_PKEY *pkey, const unsigned char *priv,
60                              size_t len)
61 {
62     ASN1_OCTET_STRING *os;
63
64     if (pkey->pkey.ptr != NULL)
65         return 0;
66
67     os = ASN1_OCTET_STRING_new();
68     if (os == NULL)
69         return 0;
70
71
72     if (!ASN1_OCTET_STRING_set(os, priv, len)) {
73         ASN1_OCTET_STRING_free(os);
74         return 0;
75     }
76
77     pkey->pkey.ptr = os;
78     return 1;
79 }
80
81 static int hmac_get_priv_key(const EVP_PKEY *pkey, unsigned char *priv,
82                              size_t *len)
83 {
84     ASN1_OCTET_STRING *os = (ASN1_OCTET_STRING *)pkey->pkey.ptr;
85
86     if (priv == NULL) {
87         *len = ASN1_STRING_length(os);
88         return 1;
89     }
90
91     if (os == NULL || *len < (size_t)ASN1_STRING_length(os))
92         return 0;
93
94     *len = ASN1_STRING_length(os);
95     memcpy(priv, ASN1_STRING_get0_data(os), *len);
96
97     return 1;
98 }
99
100 const EVP_PKEY_ASN1_METHOD hmac_asn1_meth = {
101     EVP_PKEY_HMAC,
102     EVP_PKEY_HMAC,
103     0,
104
105     "HMAC",
106     "OpenSSL HMAC method",
107
108     0, 0, hmac_pkey_public_cmp, 0,
109
110     0, 0, 0,
111
112     hmac_size,
113     0, 0,
114     0, 0, 0, 0, 0, 0, 0,
115
116     hmac_key_free,
117     hmac_pkey_ctrl,
118     NULL,
119     NULL,
120
121     NULL,
122     NULL,
123     NULL,
124
125     NULL,
126     NULL,
127     NULL,
128
129     hmac_set_priv_key,
130     NULL,
131     hmac_get_priv_key,
132     NULL,
133 };