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 <openssl/err.h>
16 #include "internal/poly1305.h"
17 #include "poly1305_local.h"
18 #include "internal/evp_int.h"
20 /* POLY1305 pkey context structure */
23 ASN1_OCTET_STRING ktmp; /* Temp storage for key */
27 static int pkey_poly1305_init(EVP_PKEY_CTX *ctx)
29 POLY1305_PKEY_CTX *pctx;
31 if ((pctx = OPENSSL_zalloc(sizeof(*pctx))) == NULL) {
32 CRYPTOerr(CRYPTO_F_PKEY_POLY1305_INIT, ERR_R_MALLOC_FAILURE);
35 pctx->ktmp.type = V_ASN1_OCTET_STRING;
37 EVP_PKEY_CTX_set_data(ctx, pctx);
38 EVP_PKEY_CTX_set0_keygen_info(ctx, NULL, 0);
42 static void pkey_poly1305_cleanup(EVP_PKEY_CTX *ctx)
44 POLY1305_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(ctx);
47 OPENSSL_clear_free(pctx->ktmp.data, pctx->ktmp.length);
48 OPENSSL_clear_free(pctx, sizeof(*pctx));
49 EVP_PKEY_CTX_set_data(ctx, NULL);
53 static int pkey_poly1305_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
55 POLY1305_PKEY_CTX *sctx, *dctx;
57 /* allocate memory for dst->data and a new POLY1305_CTX in dst->data->ctx */
58 if (!pkey_poly1305_init(dst))
60 sctx = EVP_PKEY_CTX_get_data(src);
61 dctx = EVP_PKEY_CTX_get_data(dst);
62 if (ASN1_STRING_get0_data(&sctx->ktmp) != NULL &&
63 !ASN1_STRING_copy(&dctx->ktmp, &sctx->ktmp)) {
64 /* cleanup and free the POLY1305_PKEY_CTX in dst->data */
65 pkey_poly1305_cleanup(dst);
68 memcpy(&dctx->ctx, &sctx->ctx, sizeof(POLY1305));
72 static int pkey_poly1305_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
74 ASN1_OCTET_STRING *key;
75 POLY1305_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(ctx);
77 if (ASN1_STRING_get0_data(&pctx->ktmp) == NULL)
79 key = ASN1_OCTET_STRING_dup(&pctx->ktmp);
82 return EVP_PKEY_assign_POLY1305(pkey, key);
85 static int int_update(EVP_MD_CTX *ctx, const void *data, size_t count)
87 POLY1305_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(EVP_MD_CTX_pkey_ctx(ctx));
89 Poly1305_Update(&pctx->ctx, data, count);
93 static int poly1305_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
95 POLY1305_PKEY_CTX *pctx = ctx->data;
96 ASN1_OCTET_STRING *key = (ASN1_OCTET_STRING *)ctx->pkey->pkey.ptr;
98 if (key->length != POLY1305_KEY_SIZE)
100 EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT);
101 EVP_MD_CTX_set_update_fn(mctx, int_update);
102 Poly1305_Init(&pctx->ctx, key->data);
105 static int poly1305_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
108 POLY1305_PKEY_CTX *pctx = ctx->data;
110 *siglen = POLY1305_DIGEST_SIZE;
112 Poly1305_Final(&pctx->ctx, sig);
116 static int pkey_poly1305_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
118 POLY1305_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(ctx);
119 const unsigned char *key;
124 case EVP_PKEY_CTRL_MD:
128 case EVP_PKEY_CTRL_SET_MAC_KEY:
129 case EVP_PKEY_CTRL_DIGESTINIT:
130 if (type == EVP_PKEY_CTRL_SET_MAC_KEY) {
131 /* user explicitly setting the key */
135 /* user indirectly setting the key via EVP_DigestSignInit */
136 key = EVP_PKEY_get0_poly1305(EVP_PKEY_CTX_get0_pkey(ctx), &len);
138 if (key == NULL || len != POLY1305_KEY_SIZE ||
139 !ASN1_OCTET_STRING_set(&pctx->ktmp, key, len))
141 Poly1305_Init(&pctx->ctx, ASN1_STRING_get0_data(&pctx->ktmp));
151 static int pkey_poly1305_ctrl_str(EVP_PKEY_CTX *ctx,
152 const char *type, const char *value)
156 if (strcmp(type, "key") == 0)
157 return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, value);
158 if (strcmp(type, "hexkey") == 0)
159 return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, value);
163 const EVP_PKEY_METHOD poly1305_pkey_meth = {
165 EVP_PKEY_FLAG_SIGCTX_CUSTOM, /* we don't deal with a separate MD */
168 pkey_poly1305_cleanup,
173 pkey_poly1305_keygen,
181 poly1305_signctx_init,
193 pkey_poly1305_ctrl_str