Copyright consolidation 07/10
[openssl.git] / crypto / hmac / hm_ameth.c
1 /*
2  * Copyright 2007-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/evp.h>
13 #include "internal/asn1_int.h"
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
22 static int hmac_size(const EVP_PKEY *pkey)
23 {
24     return EVP_MAX_MD_SIZE;
25 }
26
27 static void hmac_key_free(EVP_PKEY *pkey)
28 {
29     ASN1_OCTET_STRING *os = EVP_PKEY_get0(pkey);
30     if (os) {
31         if (os->data)
32             OPENSSL_cleanse(os->data, os->length);
33         ASN1_OCTET_STRING_free(os);
34     }
35 }
36
37 static 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
49 #ifdef HMAC_TEST_PRIVATE_KEY_FORMAT
50 /*
51  * A bogus private key format for test purposes. This is simply the HMAC key
52  * with "HMAC PRIVATE KEY" in the headers. When enabled the genpkey utility
53  * can be used to "generate" HMAC keys.
54  */
55
56 static int old_hmac_decode(EVP_PKEY *pkey,
57                            const unsigned char **pder, int derlen)
58 {
59     ASN1_OCTET_STRING *os;
60     os = ASN1_OCTET_STRING_new();
61     if (os == NULL || !ASN1_OCTET_STRING_set(os, *pder, derlen))
62         goto err;
63     if (!EVP_PKEY_assign(pkey, EVP_PKEY_HMAC, os))
64         goto err;
65     return 1;
66
67  err:
68     ASN1_OCTET_STRING_free(os);
69     return 0;
70 }
71
72 static int old_hmac_encode(const EVP_PKEY *pkey, unsigned char **pder)
73 {
74     int inc;
75     ASN1_OCTET_STRING *os = EVP_PKEY_get0(pkey);
76     if (pder) {
77         if (!*pder) {
78             *pder = OPENSSL_malloc(os->length);
79             if (*pder == NULL)
80                 return -1;
81             inc = 0;
82         } else
83             inc = 1;
84
85         memcpy(*pder, os->data, os->length);
86
87         if (inc)
88             *pder += os->length;
89     }
90
91     return os->length;
92 }
93
94 #endif
95
96 const EVP_PKEY_ASN1_METHOD hmac_asn1_meth = {
97     EVP_PKEY_HMAC,
98     EVP_PKEY_HMAC,
99     0,
100
101     "HMAC",
102     "OpenSSL HMAC method",
103
104     0, 0, 0, 0,
105
106     0, 0, 0,
107
108     hmac_size,
109     0, 0,
110     0, 0, 0, 0, 0, 0, 0,
111
112     hmac_key_free,
113     hmac_pkey_ctrl,
114 #ifdef HMAC_TEST_PRIVATE_KEY_FORMAT
115     old_hmac_decode,
116     old_hmac_encode
117 #else
118     0, 0
119 #endif
120 };