xref: /freebsd/crypto/krb5/src/plugins/preauth/spake/openssl.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* plugins/preauth/spake/openssl.c - SPAKE implementations using OpenSSL */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert  * Copyright (C) 2015 by the Massachusetts Institute of Technology.
5*7f2fe78bSCy Schubert  * All rights reserved.
6*7f2fe78bSCy Schubert  *
7*7f2fe78bSCy Schubert  * Redistribution and use in source and binary forms, with or without
8*7f2fe78bSCy Schubert  * modification, are permitted provided that the following conditions
9*7f2fe78bSCy Schubert  * are met:
10*7f2fe78bSCy Schubert  *
11*7f2fe78bSCy Schubert  * * Redistributions of source code must retain the above copyright
12*7f2fe78bSCy Schubert  *   notice, this list of conditions and the following disclaimer.
13*7f2fe78bSCy Schubert  *
14*7f2fe78bSCy Schubert  * * Redistributions in binary form must reproduce the above copyright
15*7f2fe78bSCy Schubert  *   notice, this list of conditions and the following disclaimer in
16*7f2fe78bSCy Schubert  *   the documentation and/or other materials provided with the
17*7f2fe78bSCy Schubert  *   distribution.
18*7f2fe78bSCy Schubert  *
19*7f2fe78bSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20*7f2fe78bSCy Schubert  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21*7f2fe78bSCy Schubert  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22*7f2fe78bSCy Schubert  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23*7f2fe78bSCy Schubert  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24*7f2fe78bSCy Schubert  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25*7f2fe78bSCy Schubert  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26*7f2fe78bSCy Schubert  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*7f2fe78bSCy Schubert  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28*7f2fe78bSCy Schubert  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29*7f2fe78bSCy Schubert  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30*7f2fe78bSCy Schubert  * OF THE POSSIBILITY OF SUCH DAMAGE.
31*7f2fe78bSCy Schubert  */
32*7f2fe78bSCy Schubert 
33*7f2fe78bSCy Schubert #include "k5-int.h"
34*7f2fe78bSCy Schubert 
35*7f2fe78bSCy Schubert #include "groups.h"
36*7f2fe78bSCy Schubert #include "iana.h"
37*7f2fe78bSCy Schubert 
38*7f2fe78bSCy Schubert #ifdef SPAKE_OPENSSL
39*7f2fe78bSCy Schubert #include <openssl/bn.h>
40*7f2fe78bSCy Schubert #include <openssl/ec.h>
41*7f2fe78bSCy Schubert #include <openssl/obj_mac.h>
42*7f2fe78bSCy Schubert #include <openssl/evp.h>
43*7f2fe78bSCy Schubert 
44*7f2fe78bSCy Schubert /* OpenSSL 1.1 standardizes constructor and destructor names, renaming
45*7f2fe78bSCy Schubert  * EVP_MD_CTX_create and EVP_MD_CTX_destroy. */
46*7f2fe78bSCy Schubert #if OPENSSL_VERSION_NUMBER < 0x10100000L
47*7f2fe78bSCy Schubert #define EVP_MD_CTX_new EVP_MD_CTX_create
48*7f2fe78bSCy Schubert #define EVP_MD_CTX_free EVP_MD_CTX_destroy
49*7f2fe78bSCy Schubert #endif
50*7f2fe78bSCy Schubert 
51*7f2fe78bSCy Schubert struct groupdata_st {
52*7f2fe78bSCy Schubert     const groupdef *gdef;
53*7f2fe78bSCy Schubert     EC_GROUP *group;
54*7f2fe78bSCy Schubert     BIGNUM *order;
55*7f2fe78bSCy Schubert     BN_CTX *ctx;
56*7f2fe78bSCy Schubert     EC_POINT *M;
57*7f2fe78bSCy Schubert     EC_POINT *N;
58*7f2fe78bSCy Schubert     const EVP_MD *md;
59*7f2fe78bSCy Schubert };
60*7f2fe78bSCy Schubert 
61*7f2fe78bSCy Schubert static void
ossl_fini(groupdata * gd)62*7f2fe78bSCy Schubert ossl_fini(groupdata *gd)
63*7f2fe78bSCy Schubert {
64*7f2fe78bSCy Schubert     if (gd == NULL)
65*7f2fe78bSCy Schubert         return;
66*7f2fe78bSCy Schubert 
67*7f2fe78bSCy Schubert     EC_GROUP_free(gd->group);
68*7f2fe78bSCy Schubert     EC_POINT_free(gd->M);
69*7f2fe78bSCy Schubert     EC_POINT_free(gd->N);
70*7f2fe78bSCy Schubert     BN_CTX_free(gd->ctx);
71*7f2fe78bSCy Schubert     BN_free(gd->order);
72*7f2fe78bSCy Schubert     free(gd);
73*7f2fe78bSCy Schubert }
74*7f2fe78bSCy Schubert 
75*7f2fe78bSCy Schubert static krb5_error_code
ossl_init(krb5_context context,const groupdef * gdef,groupdata ** gdata_out)76*7f2fe78bSCy Schubert ossl_init(krb5_context context, const groupdef *gdef, groupdata **gdata_out)
77*7f2fe78bSCy Schubert {
78*7f2fe78bSCy Schubert     const spake_iana *reg = gdef->reg;
79*7f2fe78bSCy Schubert     const EVP_MD *md;
80*7f2fe78bSCy Schubert     groupdata *gd;
81*7f2fe78bSCy Schubert     int nid;
82*7f2fe78bSCy Schubert 
83*7f2fe78bSCy Schubert     switch (reg->id) {
84*7f2fe78bSCy Schubert     case SPAKE_GROUP_P256:
85*7f2fe78bSCy Schubert         nid = NID_X9_62_prime256v1;
86*7f2fe78bSCy Schubert         md = EVP_sha256();
87*7f2fe78bSCy Schubert         break;
88*7f2fe78bSCy Schubert     case SPAKE_GROUP_P384:
89*7f2fe78bSCy Schubert         nid = NID_secp384r1;
90*7f2fe78bSCy Schubert         md = EVP_sha384();
91*7f2fe78bSCy Schubert         break;
92*7f2fe78bSCy Schubert     case SPAKE_GROUP_P521:
93*7f2fe78bSCy Schubert         nid = NID_secp521r1;
94*7f2fe78bSCy Schubert         md = EVP_sha512();
95*7f2fe78bSCy Schubert         break;
96*7f2fe78bSCy Schubert     default:
97*7f2fe78bSCy Schubert         return EINVAL;
98*7f2fe78bSCy Schubert     };
99*7f2fe78bSCy Schubert 
100*7f2fe78bSCy Schubert     gd = calloc(1, sizeof(*gd));
101*7f2fe78bSCy Schubert     if (gd == NULL)
102*7f2fe78bSCy Schubert         return ENOMEM;
103*7f2fe78bSCy Schubert     gd->gdef = gdef;
104*7f2fe78bSCy Schubert 
105*7f2fe78bSCy Schubert     gd->group = EC_GROUP_new_by_curve_name(nid);
106*7f2fe78bSCy Schubert     if (gd->group == NULL)
107*7f2fe78bSCy Schubert         goto error;
108*7f2fe78bSCy Schubert 
109*7f2fe78bSCy Schubert     gd->ctx = BN_CTX_new();
110*7f2fe78bSCy Schubert     if (gd->ctx == NULL)
111*7f2fe78bSCy Schubert         goto error;
112*7f2fe78bSCy Schubert 
113*7f2fe78bSCy Schubert     gd->order = BN_new();
114*7f2fe78bSCy Schubert     if (gd->order == NULL)
115*7f2fe78bSCy Schubert         goto error;
116*7f2fe78bSCy Schubert     if (!EC_GROUP_get_order(gd->group, gd->order, gd->ctx))
117*7f2fe78bSCy Schubert         goto error;
118*7f2fe78bSCy Schubert 
119*7f2fe78bSCy Schubert     gd->M = EC_POINT_new(gd->group);
120*7f2fe78bSCy Schubert     if (gd->M == NULL)
121*7f2fe78bSCy Schubert         goto error;
122*7f2fe78bSCy Schubert     if (!EC_POINT_oct2point(gd->group, gd->M, reg->m, reg->elem_len, gd->ctx))
123*7f2fe78bSCy Schubert         goto error;
124*7f2fe78bSCy Schubert 
125*7f2fe78bSCy Schubert     gd->N = EC_POINT_new(gd->group);
126*7f2fe78bSCy Schubert     if (gd->N == NULL)
127*7f2fe78bSCy Schubert         goto error;
128*7f2fe78bSCy Schubert     if (!EC_POINT_oct2point(gd->group, gd->N, reg->n, reg->elem_len, gd->ctx))
129*7f2fe78bSCy Schubert         goto error;
130*7f2fe78bSCy Schubert 
131*7f2fe78bSCy Schubert     gd->md = md;
132*7f2fe78bSCy Schubert 
133*7f2fe78bSCy Schubert     *gdata_out = gd;
134*7f2fe78bSCy Schubert     return 0;
135*7f2fe78bSCy Schubert 
136*7f2fe78bSCy Schubert error:
137*7f2fe78bSCy Schubert     ossl_fini(gd);
138*7f2fe78bSCy Schubert     return ENOMEM;
139*7f2fe78bSCy Schubert }
140*7f2fe78bSCy Schubert 
141*7f2fe78bSCy Schubert /* Convert pseudo-random bytes into a scalar value in constant time.
142*7f2fe78bSCy Schubert  * Return NULL on failure. */
143*7f2fe78bSCy Schubert static BIGNUM *
unmarshal_w(const groupdata * gdata,const uint8_t * wbytes)144*7f2fe78bSCy Schubert unmarshal_w(const groupdata *gdata, const uint8_t *wbytes)
145*7f2fe78bSCy Schubert {
146*7f2fe78bSCy Schubert     const spake_iana *reg = gdata->gdef->reg;
147*7f2fe78bSCy Schubert     BIGNUM *w = NULL;
148*7f2fe78bSCy Schubert 
149*7f2fe78bSCy Schubert     w = BN_new();
150*7f2fe78bSCy Schubert     if (w == NULL)
151*7f2fe78bSCy Schubert         return NULL;
152*7f2fe78bSCy Schubert 
153*7f2fe78bSCy Schubert     BN_set_flags(w, BN_FLG_CONSTTIME);
154*7f2fe78bSCy Schubert 
155*7f2fe78bSCy Schubert     if (BN_bin2bn(wbytes, reg->mult_len, w) &&
156*7f2fe78bSCy Schubert         BN_div(NULL, w, w, gdata->order, gdata->ctx))
157*7f2fe78bSCy Schubert         return w;
158*7f2fe78bSCy Schubert 
159*7f2fe78bSCy Schubert     BN_free(w);
160*7f2fe78bSCy Schubert     return NULL;
161*7f2fe78bSCy Schubert }
162*7f2fe78bSCy Schubert 
163*7f2fe78bSCy Schubert static krb5_error_code
ossl_keygen(krb5_context context,groupdata * gdata,const uint8_t * wbytes,krb5_boolean use_m,uint8_t * priv_out,uint8_t * pub_out)164*7f2fe78bSCy Schubert ossl_keygen(krb5_context context, groupdata *gdata, const uint8_t *wbytes,
165*7f2fe78bSCy Schubert             krb5_boolean use_m, uint8_t *priv_out, uint8_t *pub_out)
166*7f2fe78bSCy Schubert {
167*7f2fe78bSCy Schubert     const spake_iana *reg = gdata->gdef->reg;
168*7f2fe78bSCy Schubert     const EC_POINT *constant = use_m ? gdata->M : gdata->N;
169*7f2fe78bSCy Schubert     krb5_boolean success = FALSE;
170*7f2fe78bSCy Schubert     EC_POINT *pub = NULL;
171*7f2fe78bSCy Schubert     BIGNUM *priv = NULL, *w = NULL;
172*7f2fe78bSCy Schubert     size_t len;
173*7f2fe78bSCy Schubert 
174*7f2fe78bSCy Schubert     w = unmarshal_w(gdata, wbytes);
175*7f2fe78bSCy Schubert     if (w == NULL)
176*7f2fe78bSCy Schubert         goto cleanup;
177*7f2fe78bSCy Schubert 
178*7f2fe78bSCy Schubert     pub = EC_POINT_new(gdata->group);
179*7f2fe78bSCy Schubert     if (pub == NULL)
180*7f2fe78bSCy Schubert         goto cleanup;
181*7f2fe78bSCy Schubert 
182*7f2fe78bSCy Schubert     priv = BN_new();
183*7f2fe78bSCy Schubert     if (priv == NULL)
184*7f2fe78bSCy Schubert         goto cleanup;
185*7f2fe78bSCy Schubert 
186*7f2fe78bSCy Schubert     if (!BN_rand_range(priv, gdata->order))
187*7f2fe78bSCy Schubert         goto cleanup;
188*7f2fe78bSCy Schubert 
189*7f2fe78bSCy Schubert     /* Compute priv*G + w*constant; EC_POINT_mul() does this in one call. */
190*7f2fe78bSCy Schubert     if (!EC_POINT_mul(gdata->group, pub, priv, constant, w, gdata->ctx))
191*7f2fe78bSCy Schubert         goto cleanup;
192*7f2fe78bSCy Schubert 
193*7f2fe78bSCy Schubert     /* Marshal priv into priv_out. */
194*7f2fe78bSCy Schubert     memset(priv_out, 0, reg->mult_len);
195*7f2fe78bSCy Schubert     BN_bn2bin(priv, &priv_out[reg->mult_len - BN_num_bytes(priv)]);
196*7f2fe78bSCy Schubert 
197*7f2fe78bSCy Schubert     /* Marshal pub into pub_out. */
198*7f2fe78bSCy Schubert     len = EC_POINT_point2oct(gdata->group, pub, POINT_CONVERSION_COMPRESSED,
199*7f2fe78bSCy Schubert                              pub_out, reg->elem_len, gdata->ctx);
200*7f2fe78bSCy Schubert     if (len != reg->elem_len)
201*7f2fe78bSCy Schubert         goto cleanup;
202*7f2fe78bSCy Schubert 
203*7f2fe78bSCy Schubert     success = TRUE;
204*7f2fe78bSCy Schubert 
205*7f2fe78bSCy Schubert cleanup:
206*7f2fe78bSCy Schubert     EC_POINT_free(pub);
207*7f2fe78bSCy Schubert     BN_clear_free(priv);
208*7f2fe78bSCy Schubert     BN_clear_free(w);
209*7f2fe78bSCy Schubert     return success ? 0 : ENOMEM;
210*7f2fe78bSCy Schubert }
211*7f2fe78bSCy Schubert 
212*7f2fe78bSCy Schubert static krb5_error_code
ossl_result(krb5_context context,groupdata * gdata,const uint8_t * wbytes,const uint8_t * ourpriv,const uint8_t * theirpub,krb5_boolean use_m,uint8_t * elem_out)213*7f2fe78bSCy Schubert ossl_result(krb5_context context, groupdata *gdata, const uint8_t *wbytes,
214*7f2fe78bSCy Schubert             const uint8_t *ourpriv, const uint8_t *theirpub,
215*7f2fe78bSCy Schubert             krb5_boolean use_m, uint8_t *elem_out)
216*7f2fe78bSCy Schubert {
217*7f2fe78bSCy Schubert     const spake_iana *reg = gdata->gdef->reg;
218*7f2fe78bSCy Schubert     const EC_POINT *constant = use_m ? gdata->M : gdata->N;
219*7f2fe78bSCy Schubert     krb5_boolean success = FALSE, invalid = FALSE;
220*7f2fe78bSCy Schubert     EC_POINT *result = NULL, *pub = NULL;
221*7f2fe78bSCy Schubert     BIGNUM *priv = NULL, *w = NULL;
222*7f2fe78bSCy Schubert     size_t len;
223*7f2fe78bSCy Schubert 
224*7f2fe78bSCy Schubert     w = unmarshal_w(gdata, wbytes);
225*7f2fe78bSCy Schubert     if (w == NULL)
226*7f2fe78bSCy Schubert         goto cleanup;
227*7f2fe78bSCy Schubert 
228*7f2fe78bSCy Schubert     priv = BN_bin2bn(ourpriv, reg->mult_len, NULL);
229*7f2fe78bSCy Schubert     if (priv == NULL)
230*7f2fe78bSCy Schubert         goto cleanup;
231*7f2fe78bSCy Schubert 
232*7f2fe78bSCy Schubert     pub = EC_POINT_new(gdata->group);
233*7f2fe78bSCy Schubert     if (pub == NULL)
234*7f2fe78bSCy Schubert         goto cleanup;
235*7f2fe78bSCy Schubert     if (!EC_POINT_oct2point(gdata->group, pub, theirpub, reg->elem_len,
236*7f2fe78bSCy Schubert                             gdata->ctx)) {
237*7f2fe78bSCy Schubert         invalid = TRUE;
238*7f2fe78bSCy Schubert         goto cleanup;
239*7f2fe78bSCy Schubert     }
240*7f2fe78bSCy Schubert 
241*7f2fe78bSCy Schubert     /* Compute result = priv*(pub - w*constant), using result to hold the
242*7f2fe78bSCy Schubert      * intermediate steps. */
243*7f2fe78bSCy Schubert     result = EC_POINT_new(gdata->group);
244*7f2fe78bSCy Schubert     if (result == NULL)
245*7f2fe78bSCy Schubert         goto cleanup;
246*7f2fe78bSCy Schubert     if (!EC_POINT_mul(gdata->group, result, NULL, constant, w, gdata->ctx))
247*7f2fe78bSCy Schubert         goto cleanup;
248*7f2fe78bSCy Schubert     if (!EC_POINT_invert(gdata->group, result, gdata->ctx))
249*7f2fe78bSCy Schubert         goto cleanup;
250*7f2fe78bSCy Schubert     if (!EC_POINT_add(gdata->group, result, pub, result, gdata->ctx))
251*7f2fe78bSCy Schubert         goto cleanup;
252*7f2fe78bSCy Schubert     if (!EC_POINT_mul(gdata->group, result, NULL, result, priv, gdata->ctx))
253*7f2fe78bSCy Schubert         goto cleanup;
254*7f2fe78bSCy Schubert 
255*7f2fe78bSCy Schubert     /* Marshal result into elem_out. */
256*7f2fe78bSCy Schubert     len = EC_POINT_point2oct(gdata->group, result, POINT_CONVERSION_COMPRESSED,
257*7f2fe78bSCy Schubert                              elem_out, reg->elem_len, gdata->ctx);
258*7f2fe78bSCy Schubert     if (len != reg->elem_len)
259*7f2fe78bSCy Schubert         goto cleanup;
260*7f2fe78bSCy Schubert 
261*7f2fe78bSCy Schubert     success = TRUE;
262*7f2fe78bSCy Schubert 
263*7f2fe78bSCy Schubert cleanup:
264*7f2fe78bSCy Schubert     BN_clear_free(priv);
265*7f2fe78bSCy Schubert     BN_clear_free(w);
266*7f2fe78bSCy Schubert     EC_POINT_free(pub);
267*7f2fe78bSCy Schubert     EC_POINT_clear_free(result);
268*7f2fe78bSCy Schubert     return invalid ? EINVAL : (success ? 0 : ENOMEM);
269*7f2fe78bSCy Schubert }
270*7f2fe78bSCy Schubert 
271*7f2fe78bSCy Schubert static krb5_error_code
ossl_hash(krb5_context context,groupdata * gdata,const krb5_data * dlist,size_t ndata,uint8_t * result_out)272*7f2fe78bSCy Schubert ossl_hash(krb5_context context, groupdata *gdata, const krb5_data *dlist,
273*7f2fe78bSCy Schubert           size_t ndata, uint8_t *result_out)
274*7f2fe78bSCy Schubert {
275*7f2fe78bSCy Schubert     EVP_MD_CTX *ctx;
276*7f2fe78bSCy Schubert     size_t i;
277*7f2fe78bSCy Schubert     int ok;
278*7f2fe78bSCy Schubert 
279*7f2fe78bSCy Schubert     ctx = EVP_MD_CTX_new();
280*7f2fe78bSCy Schubert     if (ctx == NULL)
281*7f2fe78bSCy Schubert         return ENOMEM;
282*7f2fe78bSCy Schubert     ok = EVP_DigestInit_ex(ctx, gdata->md, NULL);
283*7f2fe78bSCy Schubert     for (i = 0; i < ndata; i++)
284*7f2fe78bSCy Schubert         ok = ok && EVP_DigestUpdate(ctx, dlist[i].data, dlist[i].length);
285*7f2fe78bSCy Schubert     ok = ok && EVP_DigestFinal_ex(ctx, result_out, NULL);
286*7f2fe78bSCy Schubert     EVP_MD_CTX_free(ctx);
287*7f2fe78bSCy Schubert     return ok ? 0 : ENOMEM;
288*7f2fe78bSCy Schubert }
289*7f2fe78bSCy Schubert 
290*7f2fe78bSCy Schubert groupdef ossl_P256 = {
291*7f2fe78bSCy Schubert     .reg = &spake_iana_p256,
292*7f2fe78bSCy Schubert     .init = ossl_init,
293*7f2fe78bSCy Schubert     .fini = ossl_fini,
294*7f2fe78bSCy Schubert     .keygen = ossl_keygen,
295*7f2fe78bSCy Schubert     .result = ossl_result,
296*7f2fe78bSCy Schubert     .hash = ossl_hash,
297*7f2fe78bSCy Schubert };
298*7f2fe78bSCy Schubert 
299*7f2fe78bSCy Schubert groupdef ossl_P384 = {
300*7f2fe78bSCy Schubert     .reg = &spake_iana_p384,
301*7f2fe78bSCy Schubert     .init = ossl_init,
302*7f2fe78bSCy Schubert     .fini = ossl_fini,
303*7f2fe78bSCy Schubert     .keygen = ossl_keygen,
304*7f2fe78bSCy Schubert     .result = ossl_result,
305*7f2fe78bSCy Schubert     .hash = ossl_hash,
306*7f2fe78bSCy Schubert };
307*7f2fe78bSCy Schubert 
308*7f2fe78bSCy Schubert groupdef ossl_P521 = {
309*7f2fe78bSCy Schubert     .reg = &spake_iana_p521,
310*7f2fe78bSCy Schubert     .init = ossl_init,
311*7f2fe78bSCy Schubert     .fini = ossl_fini,
312*7f2fe78bSCy Schubert     .keygen = ossl_keygen,
313*7f2fe78bSCy Schubert     .result = ossl_result,
314*7f2fe78bSCy Schubert     .hash = ossl_hash,
315*7f2fe78bSCy Schubert };
316*7f2fe78bSCy Schubert #endif /* SPAKE_OPENSSL */
317