diff options
Diffstat (limited to 'src/lib/libcrypto/cmac/cm_pmeth.c')
-rw-r--r-- | src/lib/libcrypto/cmac/cm_pmeth.c | 115 |
1 files changed, 60 insertions, 55 deletions
diff --git a/src/lib/libcrypto/cmac/cm_pmeth.c b/src/lib/libcrypto/cmac/cm_pmeth.c index b65c1795ae..3010f91aca 100644 --- a/src/lib/libcrypto/cmac/cm_pmeth.c +++ b/src/lib/libcrypto/cmac/cm_pmeth.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: cm_pmeth.c,v 1.4 2014/06/12 15:49:28 deraadt Exp $ */ | 1 | /* $OpenBSD: cm_pmeth.c,v 1.5 2014/06/21 12:07:02 miod Exp $ */ |
2 | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL | 2 | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL |
3 | * project 2010. | 3 | * project 2010. |
4 | */ | 4 | */ |
@@ -61,135 +61,140 @@ | |||
61 | 61 | ||
62 | /* The context structure and "key" is simply a CMAC_CTX */ | 62 | /* The context structure and "key" is simply a CMAC_CTX */ |
63 | 63 | ||
64 | static int pkey_cmac_init(EVP_PKEY_CTX *ctx) | 64 | static int |
65 | { | 65 | pkey_cmac_init(EVP_PKEY_CTX *ctx) |
66 | { | ||
66 | ctx->data = CMAC_CTX_new(); | 67 | ctx->data = CMAC_CTX_new(); |
67 | if (!ctx->data) | 68 | if (!ctx->data) |
68 | return 0; | 69 | return 0; |
69 | ctx->keygen_info_count = 0; | 70 | ctx->keygen_info_count = 0; |
70 | return 1; | 71 | return 1; |
71 | } | 72 | } |
72 | 73 | ||
73 | static int pkey_cmac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) | 74 | static int |
74 | { | 75 | pkey_cmac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) |
76 | { | ||
75 | if (!pkey_cmac_init(dst)) | 77 | if (!pkey_cmac_init(dst)) |
76 | return 0; | 78 | return 0; |
77 | if (!CMAC_CTX_copy(dst->data, src->data)) | 79 | if (!CMAC_CTX_copy(dst->data, src->data)) |
78 | return 0; | 80 | return 0; |
79 | return 1; | 81 | return 1; |
80 | } | 82 | } |
81 | 83 | ||
82 | static void pkey_cmac_cleanup(EVP_PKEY_CTX *ctx) | 84 | static void |
83 | { | 85 | pkey_cmac_cleanup(EVP_PKEY_CTX *ctx) |
86 | { | ||
84 | CMAC_CTX_free(ctx->data); | 87 | CMAC_CTX_free(ctx->data); |
85 | } | 88 | } |
86 | 89 | ||
87 | static int pkey_cmac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) | 90 | static int |
88 | { | 91 | pkey_cmac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) |
92 | { | ||
89 | CMAC_CTX *cmkey = CMAC_CTX_new(); | 93 | CMAC_CTX *cmkey = CMAC_CTX_new(); |
90 | CMAC_CTX *cmctx = ctx->data; | 94 | CMAC_CTX *cmctx = ctx->data; |
95 | |||
91 | if (!cmkey) | 96 | if (!cmkey) |
92 | return 0; | 97 | return 0; |
93 | if (!CMAC_CTX_copy(cmkey, cmctx)) | 98 | if (!CMAC_CTX_copy(cmkey, cmctx)) { |
94 | { | ||
95 | CMAC_CTX_free(cmkey); | 99 | CMAC_CTX_free(cmkey); |
96 | return 0; | 100 | return 0; |
97 | } | 101 | } |
98 | EVP_PKEY_assign(pkey, EVP_PKEY_CMAC, cmkey); | 102 | EVP_PKEY_assign(pkey, EVP_PKEY_CMAC, cmkey); |
99 | 103 | ||
100 | return 1; | 104 | return 1; |
101 | } | 105 | } |
102 | 106 | ||
103 | static int int_update(EVP_MD_CTX *ctx,const void *data,size_t count) | 107 | static int |
104 | { | 108 | int_update(EVP_MD_CTX *ctx,const void *data,size_t count) |
109 | { | ||
105 | if (!CMAC_Update(ctx->pctx->data, data, count)) | 110 | if (!CMAC_Update(ctx->pctx->data, data, count)) |
106 | return 0; | 111 | return 0; |
107 | return 1; | 112 | return 1; |
108 | } | 113 | } |
109 | 114 | ||
110 | static int cmac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx) | 115 | static int |
111 | { | 116 | cmac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx) |
117 | { | ||
112 | EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT); | 118 | EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT); |
113 | mctx->update = int_update; | 119 | mctx->update = int_update; |
114 | return 1; | 120 | return 1; |
115 | } | 121 | } |
116 | 122 | ||
117 | static int cmac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, | 123 | static int |
118 | EVP_MD_CTX *mctx) | 124 | cmac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, |
119 | { | 125 | EVP_MD_CTX *mctx) |
126 | { | ||
120 | return CMAC_Final(ctx->data, sig, siglen); | 127 | return CMAC_Final(ctx->data, sig, siglen); |
121 | } | 128 | } |
122 | 129 | ||
123 | static int pkey_cmac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) | 130 | static int |
124 | { | 131 | pkey_cmac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) |
132 | { | ||
125 | CMAC_CTX *cmctx = ctx->data; | 133 | CMAC_CTX *cmctx = ctx->data; |
126 | switch (type) | ||
127 | { | ||
128 | 134 | ||
129 | case EVP_PKEY_CTRL_SET_MAC_KEY: | 135 | switch (type) { |
136 | case EVP_PKEY_CTRL_SET_MAC_KEY: | ||
130 | if (!p2 || p1 < 0) | 137 | if (!p2 || p1 < 0) |
131 | return 0; | 138 | return 0; |
132 | if (!CMAC_Init(cmctx, p2, p1, NULL, NULL)) | 139 | if (!CMAC_Init(cmctx, p2, p1, NULL, NULL)) |
133 | return 0; | 140 | return 0; |
134 | break; | 141 | break; |
135 | 142 | ||
136 | case EVP_PKEY_CTRL_CIPHER: | 143 | case EVP_PKEY_CTRL_CIPHER: |
137 | if (!CMAC_Init(cmctx, NULL, 0, p2, ctx->engine)) | 144 | if (!CMAC_Init(cmctx, NULL, 0, p2, ctx->engine)) |
138 | return 0; | 145 | return 0; |
139 | break; | 146 | break; |
140 | 147 | ||
141 | case EVP_PKEY_CTRL_MD: | 148 | case EVP_PKEY_CTRL_MD: |
142 | if (ctx->pkey && !CMAC_CTX_copy(ctx->data, | 149 | if (ctx->pkey && !CMAC_CTX_copy(ctx->data, |
143 | (CMAC_CTX *)ctx->pkey->pkey.ptr)) | 150 | (CMAC_CTX *)ctx->pkey->pkey.ptr)) |
144 | return 0; | 151 | return 0; |
145 | if (!CMAC_Init(cmctx, NULL, 0, NULL, NULL)) | 152 | if (!CMAC_Init(cmctx, NULL, 0, NULL, NULL)) |
146 | return 0; | 153 | return 0; |
147 | break; | 154 | break; |
148 | 155 | ||
149 | default: | 156 | default: |
150 | return -2; | 157 | return -2; |
151 | |||
152 | } | ||
153 | return 1; | ||
154 | } | 158 | } |
159 | return 1; | ||
160 | } | ||
155 | 161 | ||
156 | static int pkey_cmac_ctrl_str(EVP_PKEY_CTX *ctx, | 162 | static int |
157 | const char *type, const char *value) | 163 | pkey_cmac_ctrl_str(EVP_PKEY_CTX *ctx, const char *type, const char *value) |
158 | { | 164 | { |
159 | if (!value) | 165 | if (!value) |
160 | { | ||
161 | return 0; | 166 | return 0; |
162 | } | 167 | if (!strcmp(type, "key")) { |
163 | if (!strcmp(type, "key")) | ||
164 | { | ||
165 | void *p = (void *)value; | 168 | void *p = (void *)value; |
166 | return pkey_cmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, | 169 | return pkey_cmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, |
167 | strlen(p), p); | 170 | strlen(p), p); |
168 | } | 171 | } |
169 | if (!strcmp(type, "cipher")) | 172 | if (!strcmp(type, "cipher")) { |
170 | { | ||
171 | const EVP_CIPHER *c; | 173 | const EVP_CIPHER *c; |
174 | |||
172 | c = EVP_get_cipherbyname(value); | 175 | c = EVP_get_cipherbyname(value); |
173 | if (!c) | 176 | if (!c) |
174 | return 0; | 177 | return 0; |
175 | return pkey_cmac_ctrl(ctx, EVP_PKEY_CTRL_CIPHER, -1, (void *)c); | 178 | return pkey_cmac_ctrl(ctx, EVP_PKEY_CTRL_CIPHER, -1, (void *)c); |
176 | } | 179 | } |
177 | if (!strcmp(type, "hexkey")) | 180 | if (!strcmp(type, "hexkey")) { |
178 | { | ||
179 | unsigned char *key; | 181 | unsigned char *key; |
180 | int r; | 182 | int r; |
181 | long keylen; | 183 | long keylen; |
184 | |||
182 | key = string_to_hex(value, &keylen); | 185 | key = string_to_hex(value, &keylen); |
183 | if (!key) | 186 | if (!key) |
184 | return 0; | 187 | return 0; |
185 | r = pkey_cmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, keylen, key); | 188 | r = pkey_cmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, keylen, key); |
186 | free(key); | 189 | free(key); |
187 | return r; | 190 | return r; |
188 | } | ||
189 | return -2; | ||
190 | } | 191 | } |
191 | 192 | ||
192 | const EVP_PKEY_METHOD cmac_pkey_meth = { | 193 | return -2; |
194 | } | ||
195 | |||
196 | const EVP_PKEY_METHOD | ||
197 | cmac_pkey_meth = { | ||
193 | .pkey_id = EVP_PKEY_CMAC, | 198 | .pkey_id = EVP_PKEY_CMAC, |
194 | .flags = EVP_PKEY_FLAG_SIGCTX_CUSTOM, | 199 | .flags = EVP_PKEY_FLAG_SIGCTX_CUSTOM, |
195 | 200 | ||