2 * Copyright 2007-2017 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "internal/cryptlib.h"
12 #include <openssl/x509.h>
13 #include <openssl/x509v3.h>
14 #include <openssl/evp.h>
15 #include "internal/siphash.h"
16 #include "siphash_local.h"
17 #include "internal/evp_int.h"
19 /* SIPHASH pkey context structure */
21 typedef struct siphash_pkey_ctx_st {
22 ASN1_OCTET_STRING ktmp; /* Temp storage for key */
26 static int pkey_siphash_init(EVP_PKEY_CTX *ctx)
28 SIPHASH_PKEY_CTX *pctx;
30 pctx = OPENSSL_zalloc(sizeof(*pctx));
33 pctx->ktmp.type = V_ASN1_OCTET_STRING;
35 EVP_PKEY_CTX_set_data(ctx, pctx);
36 EVP_PKEY_CTX_set0_keygen_info(ctx, NULL, 0);
40 static void pkey_siphash_cleanup(EVP_PKEY_CTX *ctx)
42 SIPHASH_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(ctx);
45 OPENSSL_clear_free(pctx->ktmp.data, pctx->ktmp.length);
46 OPENSSL_clear_free(pctx, sizeof(*pctx));
47 EVP_PKEY_CTX_set_data(ctx, NULL);
51 static int pkey_siphash_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
53 SIPHASH_PKEY_CTX *sctx, *dctx;
55 /* allocate memory for dst->data and a new SIPHASH_CTX in dst->data->ctx */
56 if (!pkey_siphash_init(dst))
58 sctx = EVP_PKEY_CTX_get_data(src);
59 dctx = EVP_PKEY_CTX_get_data(dst);
60 if (ASN1_STRING_get0_data(&sctx->ktmp) != NULL &&
61 !ASN1_STRING_copy(&dctx->ktmp, &sctx->ktmp)) {
62 /* cleanup and free the SIPHASH_PKEY_CTX in dst->data */
63 pkey_siphash_cleanup(dst);
66 memcpy(&dctx->ctx, &sctx->ctx, sizeof(SIPHASH));
70 static int pkey_siphash_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
72 ASN1_OCTET_STRING *key;
73 SIPHASH_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(ctx);
75 if (ASN1_STRING_get0_data(&pctx->ktmp) == NULL)
77 key = ASN1_OCTET_STRING_dup(&pctx->ktmp);
80 return EVP_PKEY_assign_SIPHASH(pkey, key);
83 static int int_update(EVP_MD_CTX *ctx, const void *data, size_t count)
85 SIPHASH_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(EVP_MD_CTX_pkey_ctx(ctx));
87 SipHash_Update(&pctx->ctx, data, count);
91 static int siphash_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
93 SIPHASH_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(ctx);
94 const unsigned char* key;
98 key = EVP_PKEY_get0_siphash(EVP_PKEY_CTX_get0_pkey(ctx), &len);
99 if (key == NULL || len != SIPHASH_KEY_SIZE)
101 EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT);
102 EVP_MD_CTX_set_update_fn(mctx, int_update);
103 /* use default rounds (2,4) */
104 hash_size = SipHash_hash_size(&pctx->ctx);
105 return SipHash_Init(&pctx->ctx, key, hash_size, 0, 0);
107 static int siphash_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
110 SIPHASH_PKEY_CTX *pctx = ctx->data;
112 *siglen = SipHash_hash_size(&pctx->ctx);
114 return SipHash_Final(&pctx->ctx, sig, *siglen);
118 static int pkey_siphash_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
120 SIPHASH_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(ctx);
121 const unsigned char *key;
127 case EVP_PKEY_CTRL_MD:
131 case EVP_PKEY_CTRL_SET_DIGEST_SIZE:
132 if (p1 != SIPHASH_MIN_DIGEST_SIZE &&
133 p1 != SIPHASH_MAX_DIGEST_SIZE) {
136 /* use default rounds (2,4) */
137 return SipHash_Init(&pctx->ctx, ASN1_STRING_get0_data(&pctx->ktmp), p1, 0, 0);
139 case EVP_PKEY_CTRL_SET_MAC_KEY:
140 case EVP_PKEY_CTRL_DIGESTINIT:
141 if (type == EVP_PKEY_CTRL_SET_MAC_KEY) {
142 /* user explicitly setting the key */
146 /* user indirectly setting the key via EVP_DigestSignInit */
147 key = EVP_PKEY_get0_siphash(EVP_PKEY_CTX_get0_pkey(ctx), &len);
149 if (key == NULL || len != SIPHASH_KEY_SIZE ||
150 !ASN1_OCTET_STRING_set(&pctx->ktmp, key, len))
152 /* use default rounds (2,4) */
153 hash_size = SipHash_hash_size(&pctx->ctx);
154 return SipHash_Init(&pctx->ctx, ASN1_STRING_get0_data(&pctx->ktmp), hash_size, 0, 0);
163 static int pkey_siphash_ctrl_str(EVP_PKEY_CTX *ctx,
164 const char *type, const char *value)
168 if (strcmp(type, "key") == 0)
169 return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, value);
170 if (strcmp(type, "hexkey") == 0)
171 return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, value);
175 const EVP_PKEY_METHOD siphash_pkey_meth = {
177 EVP_PKEY_FLAG_SIGCTX_CUSTOM, /* we don't deal with a separate MD */
180 pkey_siphash_cleanup,
193 siphash_signctx_init,
205 pkey_siphash_ctrl_str