/src/openssl/crypto/siphash/siphash_pmeth.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2007-2018 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 <openssl/err.h> |
16 | | #include "internal/siphash.h" |
17 | | #include "siphash_local.h" |
18 | | #include "internal/evp_int.h" |
19 | | |
20 | | /* SIPHASH pkey context structure */ |
21 | | |
22 | | typedef struct siphash_pkey_ctx_st { |
23 | | ASN1_OCTET_STRING ktmp; /* Temp storage for key */ |
24 | | SIPHASH ctx; |
25 | | } SIPHASH_PKEY_CTX; |
26 | | |
27 | | static int pkey_siphash_init(EVP_PKEY_CTX *ctx) |
28 | 0 | { |
29 | 0 | SIPHASH_PKEY_CTX *pctx; |
30 | 0 |
|
31 | 0 | if ((pctx = OPENSSL_zalloc(sizeof(*pctx))) == NULL) { |
32 | 0 | CRYPTOerr(CRYPTO_F_PKEY_SIPHASH_INIT, ERR_R_MALLOC_FAILURE); |
33 | 0 | return 0; |
34 | 0 | } |
35 | 0 | pctx->ktmp.type = V_ASN1_OCTET_STRING; |
36 | 0 |
|
37 | 0 | EVP_PKEY_CTX_set_data(ctx, pctx); |
38 | 0 | EVP_PKEY_CTX_set0_keygen_info(ctx, NULL, 0); |
39 | 0 | return 1; |
40 | 0 | } |
41 | | |
42 | | static void pkey_siphash_cleanup(EVP_PKEY_CTX *ctx) |
43 | 0 | { |
44 | 0 | SIPHASH_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(ctx); |
45 | 0 |
|
46 | 0 | if (pctx != NULL) { |
47 | 0 | OPENSSL_clear_free(pctx->ktmp.data, pctx->ktmp.length); |
48 | 0 | OPENSSL_clear_free(pctx, sizeof(*pctx)); |
49 | 0 | EVP_PKEY_CTX_set_data(ctx, NULL); |
50 | 0 | } |
51 | 0 | } |
52 | | |
53 | | static int pkey_siphash_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) |
54 | 0 | { |
55 | 0 | SIPHASH_PKEY_CTX *sctx, *dctx; |
56 | 0 |
|
57 | 0 | /* allocate memory for dst->data and a new SIPHASH_CTX in dst->data->ctx */ |
58 | 0 | if (!pkey_siphash_init(dst)) |
59 | 0 | return 0; |
60 | 0 | sctx = EVP_PKEY_CTX_get_data(src); |
61 | 0 | dctx = EVP_PKEY_CTX_get_data(dst); |
62 | 0 | if (ASN1_STRING_get0_data(&sctx->ktmp) != NULL && |
63 | 0 | !ASN1_STRING_copy(&dctx->ktmp, &sctx->ktmp)) { |
64 | 0 | /* cleanup and free the SIPHASH_PKEY_CTX in dst->data */ |
65 | 0 | pkey_siphash_cleanup(dst); |
66 | 0 | return 0; |
67 | 0 | } |
68 | 0 | memcpy(&dctx->ctx, &sctx->ctx, sizeof(SIPHASH)); |
69 | 0 | return 1; |
70 | 0 | } |
71 | | |
72 | | static int pkey_siphash_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) |
73 | 0 | { |
74 | 0 | ASN1_OCTET_STRING *key; |
75 | 0 | SIPHASH_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(ctx); |
76 | 0 |
|
77 | 0 | if (ASN1_STRING_get0_data(&pctx->ktmp) == NULL) |
78 | 0 | return 0; |
79 | 0 | key = ASN1_OCTET_STRING_dup(&pctx->ktmp); |
80 | 0 | if (key == NULL) |
81 | 0 | return 0; |
82 | 0 | return EVP_PKEY_assign_SIPHASH(pkey, key); |
83 | 0 | } |
84 | | |
85 | | static int int_update(EVP_MD_CTX *ctx, const void *data, size_t count) |
86 | 0 | { |
87 | 0 | SIPHASH_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(EVP_MD_CTX_pkey_ctx(ctx)); |
88 | 0 |
|
89 | 0 | SipHash_Update(&pctx->ctx, data, count); |
90 | 0 | return 1; |
91 | 0 | } |
92 | | |
93 | | static int siphash_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx) |
94 | 0 | { |
95 | 0 | SIPHASH_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(ctx); |
96 | 0 | const unsigned char* key; |
97 | 0 | size_t len; |
98 | 0 | int hash_size; |
99 | 0 |
|
100 | 0 | key = EVP_PKEY_get0_siphash(EVP_PKEY_CTX_get0_pkey(ctx), &len); |
101 | 0 | if (key == NULL || len != SIPHASH_KEY_SIZE) |
102 | 0 | return 0; |
103 | 0 | EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT); |
104 | 0 | EVP_MD_CTX_set_update_fn(mctx, int_update); |
105 | 0 | /* use default rounds (2,4) */ |
106 | 0 | hash_size = SipHash_hash_size(&pctx->ctx); |
107 | 0 | return SipHash_Init(&pctx->ctx, key, hash_size, 0, 0); |
108 | 0 | } |
109 | | static int siphash_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, |
110 | | EVP_MD_CTX *mctx) |
111 | 0 | { |
112 | 0 | SIPHASH_PKEY_CTX *pctx = ctx->data; |
113 | 0 |
|
114 | 0 | *siglen = SipHash_hash_size(&pctx->ctx); |
115 | 0 | if (sig != NULL) |
116 | 0 | return SipHash_Final(&pctx->ctx, sig, *siglen); |
117 | 0 | return 1; |
118 | 0 | } |
119 | | |
120 | | static int pkey_siphash_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) |
121 | 0 | { |
122 | 0 | SIPHASH_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(ctx); |
123 | 0 | const unsigned char *key; |
124 | 0 | size_t len; |
125 | 0 | int hash_size; |
126 | 0 |
|
127 | 0 | switch (type) { |
128 | 0 |
|
129 | 0 | case EVP_PKEY_CTRL_MD: |
130 | 0 | /* ignore */ |
131 | 0 | break; |
132 | 0 |
|
133 | 0 | case EVP_PKEY_CTRL_SET_DIGEST_SIZE: |
134 | 0 | if (p1 != SIPHASH_MIN_DIGEST_SIZE && |
135 | 0 | p1 != SIPHASH_MAX_DIGEST_SIZE) { |
136 | 0 | return 0; |
137 | 0 | } |
138 | 0 | /* use default rounds (2,4) */ |
139 | 0 | return SipHash_Init(&pctx->ctx, ASN1_STRING_get0_data(&pctx->ktmp), p1, 0, 0); |
140 | 0 |
|
141 | 0 | case EVP_PKEY_CTRL_SET_MAC_KEY: |
142 | 0 | case EVP_PKEY_CTRL_DIGESTINIT: |
143 | 0 | if (type == EVP_PKEY_CTRL_SET_MAC_KEY) { |
144 | 0 | /* user explicitly setting the key */ |
145 | 0 | key = p2; |
146 | 0 | len = p1; |
147 | 0 | } else { |
148 | 0 | /* user indirectly setting the key via EVP_DigestSignInit */ |
149 | 0 | key = EVP_PKEY_get0_siphash(EVP_PKEY_CTX_get0_pkey(ctx), &len); |
150 | 0 | } |
151 | 0 | if (key == NULL || len != SIPHASH_KEY_SIZE || |
152 | 0 | !ASN1_OCTET_STRING_set(&pctx->ktmp, key, len)) |
153 | 0 | return 0; |
154 | 0 | /* use default rounds (2,4) */ |
155 | 0 | hash_size = SipHash_hash_size(&pctx->ctx); |
156 | 0 | return SipHash_Init(&pctx->ctx, ASN1_STRING_get0_data(&pctx->ktmp), hash_size, 0, 0); |
157 | 0 |
|
158 | 0 | default: |
159 | 0 | return -2; |
160 | 0 | |
161 | 0 | } |
162 | 0 | return 1; |
163 | 0 | } |
164 | | |
165 | | static int pkey_siphash_ctrl_str(EVP_PKEY_CTX *ctx, |
166 | | const char *type, const char *value) |
167 | | { |
168 | | if (value == NULL) |
169 | | return 0; |
170 | | if (strcmp(type, "key") == 0) |
171 | | return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, value); |
172 | | if (strcmp(type, "hexkey") == 0) |
173 | | return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, value); |
174 | | return -2; |
175 | | } |
176 | | |
177 | | const EVP_PKEY_METHOD siphash_pkey_meth = { |
178 | | EVP_PKEY_SIPHASH, |
179 | | EVP_PKEY_FLAG_SIGCTX_CUSTOM, /* we don't deal with a separate MD */ |
180 | | pkey_siphash_init, |
181 | | pkey_siphash_copy, |
182 | | pkey_siphash_cleanup, |
183 | | |
184 | | 0, 0, |
185 | | |
186 | | 0, |
187 | | pkey_siphash_keygen, |
188 | | |
189 | | 0, 0, |
190 | | |
191 | | 0, 0, |
192 | | |
193 | | 0, 0, |
194 | | |
195 | | siphash_signctx_init, |
196 | | siphash_signctx, |
197 | | |
198 | | 0, 0, |
199 | | |
200 | | 0, 0, |
201 | | |
202 | | 0, 0, |
203 | | |
204 | | 0, 0, |
205 | | |
206 | | pkey_siphash_ctrl, |
207 | | pkey_siphash_ctrl_str |
208 | | }; |