Add support for setting raw private SIPHASH keys
[openssl.git] / crypto / siphash / siphash_pmeth.c
1 /*
2  * Copyright 2007-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <stdio.h>
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"
18
19 /* SIPHASH pkey context structure */
20
21 typedef struct siphash_pkey_ctx_st {
22     ASN1_OCTET_STRING ktmp;     /* Temp storage for key */
23     SIPHASH ctx;
24 } SIPHASH_PKEY_CTX;
25
26 static int pkey_siphash_init(EVP_PKEY_CTX *ctx)
27 {
28     SIPHASH_PKEY_CTX *pctx;
29
30     pctx = OPENSSL_zalloc(sizeof(*pctx));
31     if (pctx == NULL)
32         return 0;
33     pctx->ktmp.type = V_ASN1_OCTET_STRING;
34
35     EVP_PKEY_CTX_set_data(ctx, pctx);
36     EVP_PKEY_CTX_set0_keygen_info(ctx, NULL, 0);
37     return 1;
38 }
39
40 static void pkey_siphash_cleanup(EVP_PKEY_CTX *ctx)
41 {
42     SIPHASH_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(ctx);
43
44     if (pctx != NULL) {
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);
48     }
49 }
50
51 static int pkey_siphash_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
52 {
53     SIPHASH_PKEY_CTX *sctx, *dctx;
54
55     /* allocate memory for dst->data and a new SIPHASH_CTX in dst->data->ctx */
56     if (!pkey_siphash_init(dst))
57         return 0;
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);
64         return 0;
65     }
66     memcpy(&dctx->ctx, &sctx->ctx, sizeof(SIPHASH));
67     return 1;
68 }
69
70 static int pkey_siphash_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
71 {
72     ASN1_OCTET_STRING *key;
73     SIPHASH_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(ctx);
74
75     if (ASN1_STRING_get0_data(&pctx->ktmp) == NULL)
76         return 0;
77     key = ASN1_OCTET_STRING_dup(&pctx->ktmp);
78     if (key == NULL)
79         return 0;
80     return EVP_PKEY_assign_SIPHASH(pkey, key);
81 }
82
83 static int int_update(EVP_MD_CTX *ctx, const void *data, size_t count)
84 {
85     SIPHASH_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(EVP_MD_CTX_pkey_ctx(ctx));
86
87     SipHash_Update(&pctx->ctx, data, count);
88     return 1;
89 }
90
91 static int siphash_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
92 {
93     SIPHASH_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(ctx);
94     const unsigned char* key;
95     size_t len;
96     int hash_size;
97
98     key = EVP_PKEY_get0_siphash(EVP_PKEY_CTX_get0_pkey(ctx), &len);
99     if (key == NULL || len != SIPHASH_KEY_SIZE)
100         return 0;
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);
106 }
107 static int siphash_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
108                             EVP_MD_CTX *mctx)
109 {
110     SIPHASH_PKEY_CTX *pctx = ctx->data;
111
112     *siglen = SipHash_hash_size(&pctx->ctx);
113     if (sig != NULL)
114         return SipHash_Final(&pctx->ctx, sig, *siglen);
115     return 1;
116 }
117
118 static int pkey_siphash_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
119 {
120     SIPHASH_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(ctx);
121     const unsigned char *key;
122     size_t len;
123     int hash_size;
124
125     switch (type) {
126
127     case EVP_PKEY_CTRL_MD:
128         /* ignore */
129         break;
130
131     case EVP_PKEY_CTRL_SET_DIGEST_SIZE:
132         if (p1 != SIPHASH_MIN_DIGEST_SIZE &&
133             p1 != SIPHASH_MAX_DIGEST_SIZE) {
134             return 0;
135         }
136         /* use default rounds (2,4) */
137         return SipHash_Init(&pctx->ctx, ASN1_STRING_get0_data(&pctx->ktmp), p1, 0, 0);
138
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 */
143             key = p2;
144             len = p1;
145         } else {
146             /* user indirectly setting the key via EVP_DigestSignInit */
147             key = EVP_PKEY_get0_siphash(EVP_PKEY_CTX_get0_pkey(ctx), &len);
148         }
149         if (key == NULL || len != SIPHASH_KEY_SIZE ||
150             !ASN1_OCTET_STRING_set(&pctx->ktmp, key, len))
151             return 0;
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);
155
156     default:
157         return -2;
158
159     }
160     return 1;
161 }
162
163 static int pkey_siphash_ctrl_str(EVP_PKEY_CTX *ctx,
164                                   const char *type, const char *value)
165 {
166     if (value == NULL)
167         return 0;
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);
172     return -2;
173 }
174
175 const EVP_PKEY_METHOD siphash_pkey_meth = {
176     EVP_PKEY_SIPHASH,
177     EVP_PKEY_FLAG_SIGCTX_CUSTOM, /* we don't deal with a separate MD */
178     pkey_siphash_init,
179     pkey_siphash_copy,
180     pkey_siphash_cleanup,
181
182     0, 0,
183
184     0,
185     pkey_siphash_keygen,
186
187     0, 0,
188
189     0, 0,
190
191     0, 0,
192
193     siphash_signctx_init,
194     siphash_signctx,
195
196     0, 0,
197
198     0, 0,
199
200     0, 0,
201
202     0, 0,
203
204     pkey_siphash_ctrl,
205     pkey_siphash_ctrl_str
206 };