Copyright consolidation 07/10
[openssl.git] / crypto / hmac / hm_pmeth.c
1 /*
2  * Copyright 2007-2016 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/hmac.h>
16 #include "internal/evp_int.h"
17
18 /* HMAC pkey context structure */
19
20 typedef struct {
21     const EVP_MD *md;           /* MD for HMAC use */
22     ASN1_OCTET_STRING ktmp;     /* Temp storage for key */
23     HMAC_CTX *ctx;
24 } HMAC_PKEY_CTX;
25
26 static int pkey_hmac_init(EVP_PKEY_CTX *ctx)
27 {
28     HMAC_PKEY_CTX *hctx;
29
30     hctx = OPENSSL_zalloc(sizeof(*hctx));
31     if (hctx == NULL)
32         return 0;
33     hctx->ktmp.type = V_ASN1_OCTET_STRING;
34     hctx->ctx = HMAC_CTX_new();
35
36     ctx->data = hctx;
37     ctx->keygen_info_count = 0;
38
39     return 1;
40 }
41
42 static int pkey_hmac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
43 {
44     HMAC_PKEY_CTX *sctx, *dctx;
45     if (!pkey_hmac_init(dst))
46         return 0;
47     sctx = src->data;
48     dctx = dst->data;
49     dctx->md = sctx->md;
50     if (!HMAC_CTX_copy(dctx->ctx, sctx->ctx))
51         return 0;
52     if (sctx->ktmp.data) {
53         if (!ASN1_OCTET_STRING_set(&dctx->ktmp,
54                                    sctx->ktmp.data, sctx->ktmp.length))
55             return 0;
56     }
57     return 1;
58 }
59
60 static void pkey_hmac_cleanup(EVP_PKEY_CTX *ctx)
61 {
62     HMAC_PKEY_CTX *hctx = ctx->data;
63
64     if (hctx != NULL) {
65         HMAC_CTX_free(hctx->ctx);
66         OPENSSL_clear_free(hctx->ktmp.data, hctx->ktmp.length);
67         OPENSSL_free(hctx);
68         ctx->data = NULL;
69     }
70 }
71
72 static int pkey_hmac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
73 {
74     ASN1_OCTET_STRING *hkey = NULL;
75     HMAC_PKEY_CTX *hctx = ctx->data;
76     if (!hctx->ktmp.data)
77         return 0;
78     hkey = ASN1_OCTET_STRING_dup(&hctx->ktmp);
79     if (!hkey)
80         return 0;
81     EVP_PKEY_assign(pkey, EVP_PKEY_HMAC, hkey);
82
83     return 1;
84 }
85
86 static int int_update(EVP_MD_CTX *ctx, const void *data, size_t count)
87 {
88     HMAC_PKEY_CTX *hctx = EVP_MD_CTX_pkey_ctx(ctx)->data;
89     if (!HMAC_Update(hctx->ctx, data, count))
90         return 0;
91     return 1;
92 }
93
94 static int hmac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
95 {
96     HMAC_PKEY_CTX *hctx = ctx->data;
97     HMAC_CTX_set_flags(hctx->ctx,
98                        EVP_MD_CTX_test_flags(mctx, ~EVP_MD_CTX_FLAG_NO_INIT));
99     EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT);
100     EVP_MD_CTX_set_update_fn(mctx, int_update);
101     return 1;
102 }
103
104 static int hmac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
105                         EVP_MD_CTX *mctx)
106 {
107     unsigned int hlen;
108     HMAC_PKEY_CTX *hctx = ctx->data;
109     int l = EVP_MD_CTX_size(mctx);
110
111     if (l < 0)
112         return 0;
113     *siglen = l;
114     if (!sig)
115         return 1;
116
117     if (!HMAC_Final(hctx->ctx, sig, &hlen))
118         return 0;
119     *siglen = (size_t)hlen;
120     return 1;
121 }
122
123 static int pkey_hmac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
124 {
125     HMAC_PKEY_CTX *hctx = ctx->data;
126     ASN1_OCTET_STRING *key;
127     switch (type) {
128
129     case EVP_PKEY_CTRL_SET_MAC_KEY:
130         if ((!p2 && p1 > 0) || (p1 < -1))
131             return 0;
132         if (!ASN1_OCTET_STRING_set(&hctx->ktmp, p2, p1))
133             return 0;
134         break;
135
136     case EVP_PKEY_CTRL_MD:
137         hctx->md = p2;
138         break;
139
140     case EVP_PKEY_CTRL_DIGESTINIT:
141         key = (ASN1_OCTET_STRING *)ctx->pkey->pkey.ptr;
142         if (!HMAC_Init_ex(hctx->ctx, key->data, key->length, hctx->md,
143                           ctx->engine))
144             return 0;
145         break;
146
147     default:
148         return -2;
149
150     }
151     return 1;
152 }
153
154 static int pkey_hmac_ctrl_str(EVP_PKEY_CTX *ctx,
155                               const char *type, const char *value)
156 {
157     if (!value) {
158         return 0;
159     }
160     if (strcmp(type, "key") == 0)
161         return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, value);
162     if (strcmp(type, "hexkey") == 0)
163         return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, value);
164     return -2;
165 }
166
167 const EVP_PKEY_METHOD hmac_pkey_meth = {
168     EVP_PKEY_HMAC,
169     0,
170     pkey_hmac_init,
171     pkey_hmac_copy,
172     pkey_hmac_cleanup,
173
174     0, 0,
175
176     0,
177     pkey_hmac_keygen,
178
179     0, 0,
180
181     0, 0,
182
183     0, 0,
184
185     hmac_signctx_init,
186     hmac_signctx,
187
188     0, 0,
189
190     0, 0,
191
192     0, 0,
193
194     0, 0,
195
196     pkey_hmac_ctrl,
197     pkey_hmac_ctrl_str
198 };