1221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 2221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * project 2007. 3221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom */ 4221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom/* ==================================================================== 5221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * Copyright (c) 2007 The OpenSSL Project. All rights reserved. 6221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 7221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * Redistribution and use in source and binary forms, with or without 8221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * modification, are permitted provided that the following conditions 9221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * are met: 10221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 11221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 1. Redistributions of source code must retain the above copyright 12221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * notice, this list of conditions and the following disclaimer. 13221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 14221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 2. Redistributions in binary form must reproduce the above copyright 15221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * notice, this list of conditions and the following disclaimer in 16221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * the documentation and/or other materials provided with the 17221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * distribution. 18221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 19221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 3. All advertising materials mentioning features or use of this 20221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * software must display the following acknowledgment: 21221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * "This product includes software developed by the OpenSSL Project 22221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 23221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 24221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 25221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * endorse or promote products derived from this software without 26221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * prior written permission. For written permission, please contact 27221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * licensing@OpenSSL.org. 28221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 29221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 5. Products derived from this software may not be called "OpenSSL" 30221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * nor may "OpenSSL" appear in their names without prior written 31221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * permission of the OpenSSL Project. 32221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 33221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 6. Redistributions of any form whatsoever must retain the following 34221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * acknowledgment: 35221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * "This product includes software developed by the OpenSSL Project 36221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 37221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 38221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 39221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 40221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 41221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 42221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 43221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 44221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 45221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 46221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 47221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 48221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 49221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * OF THE POSSIBILITY OF SUCH DAMAGE. 50221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * ==================================================================== 51221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 52221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * This product includes cryptographic software written by Eric Young 53221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * (eay@cryptsoft.com). This product includes software written by Tim 54221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * Hudson (tjh@cryptsoft.com). 55221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 56221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom */ 57221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 58221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include <stdio.h> 59221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include "cryptlib.h" 60221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include <openssl/x509.h> 61221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include <openssl/x509v3.h> 62221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include <openssl/evp.h> 63221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include <openssl/hmac.h> 64221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include "evp_locl.h" 65221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 66221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom/* HMAC pkey context structure */ 67221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 68221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromtypedef struct 69221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom { 70221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom const EVP_MD *md; /* MD for HMAC use */ 71221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom ASN1_OCTET_STRING ktmp; /* Temp storage for key */ 72221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom HMAC_CTX ctx; 73221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom } HMAC_PKEY_CTX; 74221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 75221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int pkey_hmac_init(EVP_PKEY_CTX *ctx) 76221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom { 77221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom HMAC_PKEY_CTX *hctx; 78221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom hctx = OPENSSL_malloc(sizeof(HMAC_PKEY_CTX)); 79221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom if (!hctx) 80221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom return 0; 81221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom hctx->md = NULL; 82221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom hctx->ktmp.data = NULL; 83221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom hctx->ktmp.length = 0; 84221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom hctx->ktmp.flags = 0; 85221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom hctx->ktmp.type = V_ASN1_OCTET_STRING; 86221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom HMAC_CTX_init(&hctx->ctx); 87221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 88221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom ctx->data = hctx; 89221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom ctx->keygen_info_count = 0; 90221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 91221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom return 1; 92221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom } 93221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 94221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int pkey_hmac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) 95221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom { 96221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom HMAC_PKEY_CTX *sctx, *dctx; 97221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom if (!pkey_hmac_init(dst)) 98221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom return 0; 99221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom sctx = src->data; 100221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom dctx = dst->data; 101221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom dctx->md = sctx->md; 102221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom HMAC_CTX_init(&dctx->ctx); 103392aa7cc7d2b122614c5393c3e357da07fd07af3Brian Carlstrom if (!HMAC_CTX_copy(&dctx->ctx, &sctx->ctx)) 104392aa7cc7d2b122614c5393c3e357da07fd07af3Brian Carlstrom return 0; 105221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom if (sctx->ktmp.data) 106221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom { 107221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom if (!ASN1_OCTET_STRING_set(&dctx->ktmp, 108221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom sctx->ktmp.data, sctx->ktmp.length)) 109221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom return 0; 110221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom } 111221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom return 1; 112221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom } 113221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 114221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic void pkey_hmac_cleanup(EVP_PKEY_CTX *ctx) 115221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom { 116221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom HMAC_PKEY_CTX *hctx = ctx->data; 117221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom HMAC_CTX_cleanup(&hctx->ctx); 118221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom if (hctx->ktmp.data) 119221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom { 120221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom if (hctx->ktmp.length) 121221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom OPENSSL_cleanse(hctx->ktmp.data, hctx->ktmp.length); 122221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom OPENSSL_free(hctx->ktmp.data); 123221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom hctx->ktmp.data = NULL; 124221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom } 125221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom OPENSSL_free(hctx); 126221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom } 127221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 128221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int pkey_hmac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) 129221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom { 130221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom ASN1_OCTET_STRING *hkey = NULL; 131221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom HMAC_PKEY_CTX *hctx = ctx->data; 132221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom if (!hctx->ktmp.data) 133221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom return 0; 134221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom hkey = ASN1_OCTET_STRING_dup(&hctx->ktmp); 135221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom if (!hkey) 136221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom return 0; 137221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom EVP_PKEY_assign(pkey, EVP_PKEY_HMAC, hkey); 138221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 139221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom return 1; 140221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom } 141221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 142221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int int_update(EVP_MD_CTX *ctx,const void *data,size_t count) 143221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom { 144221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom HMAC_PKEY_CTX *hctx = ctx->pctx->data; 145392aa7cc7d2b122614c5393c3e357da07fd07af3Brian Carlstrom if (!HMAC_Update(&hctx->ctx, data, count)) 146392aa7cc7d2b122614c5393c3e357da07fd07af3Brian Carlstrom return 0; 147221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom return 1; 148221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom } 149221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 150221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int hmac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx) 151221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom { 152ee7afb3c942c4eefef6ed06201eafaf8ec58e2e3Brian Carlstrom HMAC_PKEY_CTX *hctx = ctx->data; 153ee7afb3c942c4eefef6ed06201eafaf8ec58e2e3Brian Carlstrom HMAC_CTX_set_flags(&hctx->ctx, mctx->flags & ~EVP_MD_CTX_FLAG_NO_INIT); 154221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT); 155221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom mctx->update = int_update; 156221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom return 1; 157221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom } 158221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 159221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int hmac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, 160221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom EVP_MD_CTX *mctx) 161221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom { 162221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom unsigned int hlen; 163221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom HMAC_PKEY_CTX *hctx = ctx->data; 164221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom int l = EVP_MD_CTX_size(mctx); 165221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 166221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom if (l < 0) 167221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom return 0; 168221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *siglen = l; 169221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom if (!sig) 170221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom return 1; 171221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 172392aa7cc7d2b122614c5393c3e357da07fd07af3Brian Carlstrom if (!HMAC_Final(&hctx->ctx, sig, &hlen)) 173392aa7cc7d2b122614c5393c3e357da07fd07af3Brian Carlstrom return 0; 174221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *siglen = (size_t)hlen; 175221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom return 1; 176221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom } 177221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 178221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int pkey_hmac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) 179221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom { 180221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom HMAC_PKEY_CTX *hctx = ctx->data; 181221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom ASN1_OCTET_STRING *key; 182221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom switch (type) 183221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom { 184221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 185221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom case EVP_PKEY_CTRL_SET_MAC_KEY: 186221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom if ((!p2 && p1 > 0) || (p1 < -1)) 187221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom return 0; 188221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom if (!ASN1_OCTET_STRING_set(&hctx->ktmp, p2, p1)) 189221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom return 0; 190221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom break; 191221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 192221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom case EVP_PKEY_CTRL_MD: 193221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom hctx->md = p2; 194221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom break; 195221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 196221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom case EVP_PKEY_CTRL_DIGESTINIT: 197221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom key = (ASN1_OCTET_STRING *)ctx->pkey->pkey.ptr; 198392aa7cc7d2b122614c5393c3e357da07fd07af3Brian Carlstrom if (!HMAC_Init_ex(&hctx->ctx, key->data, key->length, hctx->md, 199392aa7cc7d2b122614c5393c3e357da07fd07af3Brian Carlstrom ctx->engine)) 200392aa7cc7d2b122614c5393c3e357da07fd07af3Brian Carlstrom return 0; 201221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom break; 202221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 203221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom default: 204221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom return -2; 205221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 206221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom } 207221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom return 1; 208221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom } 209221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 210221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int pkey_hmac_ctrl_str(EVP_PKEY_CTX *ctx, 211221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom const char *type, const char *value) 212221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom { 213221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom if (!value) 214221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom { 215221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom return 0; 216221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom } 217221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom if (!strcmp(type, "key")) 218221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom { 219221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom void *p = (void *)value; 220221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom return pkey_hmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, 221221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom -1, p); 222221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom } 223221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom if (!strcmp(type, "hexkey")) 224221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom { 225221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom unsigned char *key; 226221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom int r; 227221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom long keylen; 228221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom key = string_to_hex(value, &keylen); 229221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom if (!key) 230221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom return 0; 231221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom r = pkey_hmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, keylen, key); 232221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom OPENSSL_free(key); 233221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom return r; 234221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom } 235221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom return -2; 236221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom } 237221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 238221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromconst EVP_PKEY_METHOD hmac_pkey_meth = 239221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom { 240221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom EVP_PKEY_HMAC, 241221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 0, 242221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom pkey_hmac_init, 243221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom pkey_hmac_copy, 244221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom pkey_hmac_cleanup, 245221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 246221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 0, 0, 247221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 248221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 0, 249221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom pkey_hmac_keygen, 250221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 251221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 0, 0, 252221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 253221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 0, 0, 254221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 255221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 0,0, 256221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 257221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom hmac_signctx_init, 258221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom hmac_signctx, 259221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 260221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 0,0, 261221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 262221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 0,0, 263221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 264221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 0,0, 265221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 266221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 0,0, 267221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 268221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom pkey_hmac_ctrl, 269221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom pkey_hmac_ctrl_str 270221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom 271221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom }; 272