1280304Sjkim/* 2280304Sjkim * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project 3280304Sjkim * 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 13280304Sjkim * 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 66280304Sjkim/* 67280304Sjkim * HMAC "ASN1" method. This is just here to indicate the maximum HMAC output 68280304Sjkim * length and to free up an HMAC key. 69238384Sjkim */ 70238384Sjkim 71238384Sjkimstatic int hmac_size(const EVP_PKEY *pkey) 72280304Sjkim{ 73280304Sjkim return EVP_MAX_MD_SIZE; 74280304Sjkim} 75238384Sjkim 76238384Sjkimstatic void hmac_key_free(EVP_PKEY *pkey) 77280304Sjkim{ 78280304Sjkim ASN1_OCTET_STRING *os = (ASN1_OCTET_STRING *)pkey->pkey.ptr; 79280304Sjkim if (os) { 80280304Sjkim if (os->data) 81280304Sjkim OPENSSL_cleanse(os->data, os->length); 82280304Sjkim ASN1_OCTET_STRING_free(os); 83280304Sjkim } 84280304Sjkim} 85238384Sjkim 86238384Sjkimstatic int hmac_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2) 87280304Sjkim{ 88280304Sjkim switch (op) { 89280304Sjkim case ASN1_PKEY_CTRL_DEFAULT_MD_NID: 90280304Sjkim *(int *)arg2 = NID_sha1; 91280304Sjkim return 1; 92238384Sjkim 93280304Sjkim default: 94280304Sjkim return -2; 95280304Sjkim } 96280304Sjkim} 97238384Sjkim 98238384Sjkim#ifdef HMAC_TEST_PRIVATE_KEY_FORMAT 99280304Sjkim/* 100280304Sjkim * A bogus private key format for test purposes. This is simply the HMAC key 101280304Sjkim * with "HMAC PRIVATE KEY" in the headers. When enabled the genpkey utility 102280304Sjkim * can be used to "generate" HMAC keys. 103238384Sjkim */ 104238384Sjkim 105238384Sjkimstatic int old_hmac_decode(EVP_PKEY *pkey, 106280304Sjkim const unsigned char **pder, int derlen) 107280304Sjkim{ 108280304Sjkim ASN1_OCTET_STRING *os; 109280304Sjkim os = ASN1_OCTET_STRING_new(); 110280304Sjkim if (!os || !ASN1_OCTET_STRING_set(os, *pder, derlen)) 111291721Sjkim goto err; 112291721Sjkim if (!EVP_PKEY_assign(pkey, EVP_PKEY_HMAC, os)) 113291721Sjkim goto err; 114280304Sjkim return 1; 115291721Sjkim 116291721Sjkim err: 117291721Sjkim ASN1_OCTET_STRING_free(os); 118291721Sjkim return 0; 119280304Sjkim} 120238384Sjkim 121238384Sjkimstatic int old_hmac_encode(const EVP_PKEY *pkey, unsigned char **pder) 122280304Sjkim{ 123280304Sjkim int inc; 124280304Sjkim ASN1_OCTET_STRING *os = (ASN1_OCTET_STRING *)pkey->pkey.ptr; 125280304Sjkim if (pder) { 126280304Sjkim if (!*pder) { 127280304Sjkim *pder = OPENSSL_malloc(os->length); 128280304Sjkim inc = 0; 129280304Sjkim } else 130280304Sjkim inc = 1; 131238384Sjkim 132280304Sjkim memcpy(*pder, os->data, os->length); 133238384Sjkim 134280304Sjkim if (inc) 135280304Sjkim *pder += os->length; 136280304Sjkim } 137238384Sjkim 138280304Sjkim return os->length; 139280304Sjkim} 140280304Sjkim 141238384Sjkim#endif 142238384Sjkim 143280304Sjkimconst EVP_PKEY_ASN1_METHOD hmac_asn1_meth = { 144280304Sjkim EVP_PKEY_HMAC, 145280304Sjkim EVP_PKEY_HMAC, 146280304Sjkim 0, 147238384Sjkim 148280304Sjkim "HMAC", 149280304Sjkim "OpenSSL HMAC method", 150238384Sjkim 151280304Sjkim 0, 0, 0, 0, 152238384Sjkim 153280304Sjkim 0, 0, 0, 154238384Sjkim 155280304Sjkim hmac_size, 156280304Sjkim 0, 157280304Sjkim 0, 0, 0, 0, 0, 0, 0, 158238384Sjkim 159280304Sjkim hmac_key_free, 160280304Sjkim hmac_pkey_ctrl, 161238384Sjkim#ifdef HMAC_TEST_PRIVATE_KEY_FORMAT 162280304Sjkim old_hmac_decode, 163280304Sjkim old_hmac_encode 164238384Sjkim#else 165280304Sjkim 0, 0 166238384Sjkim#endif 167280304Sjkim}; 168