1280297Sjkim/*
2280297Sjkim * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
3280297Sjkim * 2007.
4238384Sjkim */
5238384Sjkim/* ====================================================================
6238384Sjkim * Copyright (c) 2007 The OpenSSL Project.  All rights reserved.
7238384Sjkim *
8238384Sjkim * Redistribution and use in source and binary forms, with or without
9238384Sjkim * modification, are permitted provided that the following conditions
10238384Sjkim * are met:
11238384Sjkim *
12238384Sjkim * 1. Redistributions of source code must retain the above copyright
13280297Sjkim *    notice, this list of conditions and the following disclaimer.
14238384Sjkim *
15238384Sjkim * 2. Redistributions in binary form must reproduce the above copyright
16238384Sjkim *    notice, this list of conditions and the following disclaimer in
17238384Sjkim *    the documentation and/or other materials provided with the
18238384Sjkim *    distribution.
19238384Sjkim *
20238384Sjkim * 3. All advertising materials mentioning features or use of this
21238384Sjkim *    software must display the following acknowledgment:
22238384Sjkim *    "This product includes software developed by the OpenSSL Project
23238384Sjkim *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24238384Sjkim *
25238384Sjkim * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26238384Sjkim *    endorse or promote products derived from this software without
27238384Sjkim *    prior written permission. For written permission, please contact
28238384Sjkim *    licensing@OpenSSL.org.
29238384Sjkim *
30238384Sjkim * 5. Products derived from this software may not be called "OpenSSL"
31238384Sjkim *    nor may "OpenSSL" appear in their names without prior written
32238384Sjkim *    permission of the OpenSSL Project.
33238384Sjkim *
34238384Sjkim * 6. Redistributions of any form whatsoever must retain the following
35238384Sjkim *    acknowledgment:
36238384Sjkim *    "This product includes software developed by the OpenSSL Project
37238384Sjkim *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38238384Sjkim *
39238384Sjkim * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40238384Sjkim * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41238384Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42238384Sjkim * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43238384Sjkim * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44238384Sjkim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45238384Sjkim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46238384Sjkim * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47238384Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48238384Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49238384Sjkim * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50238384Sjkim * OF THE POSSIBILITY OF SUCH DAMAGE.
51238384Sjkim * ====================================================================
52238384Sjkim *
53238384Sjkim * This product includes cryptographic software written by Eric Young
54238384Sjkim * (eay@cryptsoft.com).  This product includes software written by Tim
55238384Sjkim * Hudson (tjh@cryptsoft.com).
56238384Sjkim *
57238384Sjkim */
58238384Sjkim
59238384Sjkim#include <stdio.h>
60238384Sjkim#include "cryptlib.h"
61238384Sjkim#include <openssl/evp.h>
62238384Sjkim#include "asn1_locl.h"
63238384Sjkim
64238384Sjkim#define HMAC_TEST_PRIVATE_KEY_FORMAT
65238384Sjkim
66280297Sjkim/*
67280297Sjkim * HMAC "ASN1" method. This is just here to indicate the maximum HMAC output
68280297Sjkim * length and to free up an HMAC key.
69238384Sjkim */
70238384Sjkim
71238384Sjkimstatic int hmac_size(const EVP_PKEY *pkey)
72280297Sjkim{
73280297Sjkim    return EVP_MAX_MD_SIZE;
74280297Sjkim}
75238384Sjkim
76238384Sjkimstatic void hmac_key_free(EVP_PKEY *pkey)
77280297Sjkim{
78280297Sjkim    ASN1_OCTET_STRING *os = (ASN1_OCTET_STRING *)pkey->pkey.ptr;
79280297Sjkim    if (os) {
80280297Sjkim        if (os->data)
81280297Sjkim            OPENSSL_cleanse(os->data, os->length);
82280297Sjkim        ASN1_OCTET_STRING_free(os);
83280297Sjkim    }
84280297Sjkim}
85238384Sjkim
86238384Sjkimstatic int hmac_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
87280297Sjkim{
88280297Sjkim    switch (op) {
89280297Sjkim    case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
90290207Sjkim        *(int *)arg2 = NID_sha256;
91280297Sjkim        return 1;
92238384Sjkim
93280297Sjkim    default:
94280297Sjkim        return -2;
95280297Sjkim    }
96280297Sjkim}
97238384Sjkim
98238384Sjkim#ifdef HMAC_TEST_PRIVATE_KEY_FORMAT
99280297Sjkim/*
100280297Sjkim * A bogus private key format for test purposes. This is simply the HMAC key
101280297Sjkim * with "HMAC PRIVATE KEY" in the headers. When enabled the genpkey utility
102280297Sjkim * can be used to "generate" HMAC keys.
103238384Sjkim */
104238384Sjkim
105238384Sjkimstatic int old_hmac_decode(EVP_PKEY *pkey,
106280297Sjkim                           const unsigned char **pder, int derlen)
107280297Sjkim{
108280297Sjkim    ASN1_OCTET_STRING *os;
109280297Sjkim    os = ASN1_OCTET_STRING_new();
110280297Sjkim    if (!os || !ASN1_OCTET_STRING_set(os, *pder, derlen))
111291719Sjkim        goto err;
112291719Sjkim    if (!EVP_PKEY_assign(pkey, EVP_PKEY_HMAC, os))
113291719Sjkim        goto err;
114280297Sjkim    return 1;
115291719Sjkim
116291719Sjkim err:
117291719Sjkim    ASN1_OCTET_STRING_free(os);
118291719Sjkim    return 0;
119280297Sjkim}
120238384Sjkim
121238384Sjkimstatic int old_hmac_encode(const EVP_PKEY *pkey, unsigned char **pder)
122280297Sjkim{
123280297Sjkim    int inc;
124280297Sjkim    ASN1_OCTET_STRING *os = (ASN1_OCTET_STRING *)pkey->pkey.ptr;
125280297Sjkim    if (pder) {
126280297Sjkim        if (!*pder) {
127280297Sjkim            *pder = OPENSSL_malloc(os->length);
128280297Sjkim            inc = 0;
129280297Sjkim        } else
130280297Sjkim            inc = 1;
131238384Sjkim
132280297Sjkim        memcpy(*pder, os->data, os->length);
133238384Sjkim
134280297Sjkim        if (inc)
135280297Sjkim            *pder += os->length;
136280297Sjkim    }
137238384Sjkim
138280297Sjkim    return os->length;
139280297Sjkim}
140280297Sjkim
141238384Sjkim#endif
142238384Sjkim
143280297Sjkimconst EVP_PKEY_ASN1_METHOD hmac_asn1_meth = {
144280297Sjkim    EVP_PKEY_HMAC,
145280297Sjkim    EVP_PKEY_HMAC,
146280297Sjkim    0,
147238384Sjkim
148280297Sjkim    "HMAC",
149280297Sjkim    "OpenSSL HMAC method",
150238384Sjkim
151280297Sjkim    0, 0, 0, 0,
152238384Sjkim
153280297Sjkim    0, 0, 0,
154238384Sjkim
155280297Sjkim    hmac_size,
156280297Sjkim    0,
157280297Sjkim    0, 0, 0, 0, 0, 0, 0,
158238384Sjkim
159280297Sjkim    hmac_key_free,
160280297Sjkim    hmac_pkey_ctrl,
161238384Sjkim#ifdef HMAC_TEST_PRIVATE_KEY_FORMAT
162280297Sjkim    old_hmac_decode,
163280297Sjkim    old_hmac_encode
164238384Sjkim#else
165280297Sjkim    0, 0
166238384Sjkim#endif
167280297Sjkim};
168