]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
b0edda11 | 2 | * Copyright 2007-2018 The OpenSSL Project Authors. All Rights Reserved. |
74633553 | 3 | * |
6f888e05 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
d2e9e320 RS |
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 | |
74633553 DSH |
8 | */ |
9 | ||
dbde4726 P |
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 | ||
74633553 | 16 | #include <stdio.h> |
b39fc560 | 17 | #include "internal/cryptlib.h" |
74633553 | 18 | #include <openssl/evp.h> |
25f2138b DMSP |
19 | #include "crypto/asn1.h" |
20 | #include "crypto/evp.h" | |
74633553 | 21 | |
0f113f3e MC |
22 | /* |
23 | * HMAC "ASN1" method. This is just here to indicate the maximum HMAC output | |
24 | * length and to free up an HMAC key. | |
74633553 DSH |
25 | */ |
26 | ||
27 | static int hmac_size(const EVP_PKEY *pkey) | |
0f113f3e MC |
28 | { |
29 | return EVP_MAX_MD_SIZE; | |
30 | } | |
74633553 DSH |
31 | |
32 | static void hmac_key_free(EVP_PKEY *pkey) | |
0f113f3e | 33 | { |
3aeb9348 | 34 | ASN1_OCTET_STRING *os = EVP_PKEY_get0(pkey); |
0f113f3e MC |
35 | if (os) { |
36 | if (os->data) | |
37 | OPENSSL_cleanse(os->data, os->length); | |
38 | ASN1_OCTET_STRING_free(os); | |
39 | } | |
40 | } | |
74633553 | 41 | |
e69adea5 | 42 | static int hmac_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2) |
0f113f3e MC |
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 | } | |
e69adea5 | 53 | |
3b92e518 NM |
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 | ||
e32b52a2 MC |
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 | ||
0d124b0a MC |
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 | ||
0f113f3e MC |
100 | const EVP_PKEY_ASN1_METHOD hmac_asn1_meth = { |
101 | EVP_PKEY_HMAC, | |
102 | EVP_PKEY_HMAC, | |
103 | 0, | |
74633553 | 104 | |
0f113f3e MC |
105 | "HMAC", |
106 | "OpenSSL HMAC method", | |
74633553 | 107 | |
3b92e518 | 108 | 0, 0, hmac_pkey_public_cmp, 0, |
74633553 | 109 | |
0f113f3e | 110 | 0, 0, 0, |
74633553 | 111 | |
0f113f3e MC |
112 | hmac_size, |
113 | 0, 0, | |
114 | 0, 0, 0, 0, 0, 0, 0, | |
74633553 | 115 | |
0f113f3e MC |
116 | hmac_key_free, |
117 | hmac_pkey_ctrl, | |
e32b52a2 MC |
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, | |
0d124b0a MC |
131 | hmac_get_priv_key, |
132 | NULL, | |
0f113f3e | 133 | }; |