xref: /freebsd/crypto/krb5/src/plugins/preauth/spake/util.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* plugins/preauth/spake/util.c - Utility functions for SPAKE preauth module */
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 #include "trace.h"
35*7f2fe78bSCy Schubert #include "util.h"
36*7f2fe78bSCy Schubert #include "groups.h"
37*7f2fe78bSCy Schubert 
38*7f2fe78bSCy Schubert /* Use data to construct a single-element pa-data list of type
39*7f2fe78bSCy Schubert  * KRB5_PADATA_SPAKE.  Claim data's memory on success or failure. */
40*7f2fe78bSCy Schubert krb5_error_code
convert_to_padata(krb5_data * data,krb5_pa_data *** pa_out)41*7f2fe78bSCy Schubert convert_to_padata(krb5_data *data, krb5_pa_data ***pa_out)
42*7f2fe78bSCy Schubert {
43*7f2fe78bSCy Schubert     krb5_pa_data *pa = NULL, **list = NULL;
44*7f2fe78bSCy Schubert 
45*7f2fe78bSCy Schubert     list = calloc(2, sizeof(*list));
46*7f2fe78bSCy Schubert     if (list == NULL)
47*7f2fe78bSCy Schubert         goto fail;
48*7f2fe78bSCy Schubert     pa = calloc(1, sizeof(*pa));
49*7f2fe78bSCy Schubert     if (pa == NULL)
50*7f2fe78bSCy Schubert         goto fail;
51*7f2fe78bSCy Schubert     pa->magic = KV5M_PA_DATA;
52*7f2fe78bSCy Schubert     pa->pa_type = KRB5_PADATA_SPAKE;
53*7f2fe78bSCy Schubert     pa->length = data->length;
54*7f2fe78bSCy Schubert     pa->contents = (uint8_t *)data->data;
55*7f2fe78bSCy Schubert     list[0] = pa;
56*7f2fe78bSCy Schubert     list[1] = NULL;
57*7f2fe78bSCy Schubert     *pa_out = list;
58*7f2fe78bSCy Schubert     free(data);
59*7f2fe78bSCy Schubert     return 0;
60*7f2fe78bSCy Schubert 
61*7f2fe78bSCy Schubert fail:
62*7f2fe78bSCy Schubert     free(list);
63*7f2fe78bSCy Schubert     free(pa);
64*7f2fe78bSCy Schubert     free(data->data);
65*7f2fe78bSCy Schubert     free(data);
66*7f2fe78bSCy Schubert     return ENOMEM;
67*7f2fe78bSCy Schubert }
68*7f2fe78bSCy Schubert 
69*7f2fe78bSCy Schubert /*
70*7f2fe78bSCy Schubert  * Update the transcript hash thash with its current value and the
71*7f2fe78bSCy Schubert  * concatenation of data1 and data2, using the hash function for group.  Either
72*7f2fe78bSCy Schubert  * data1 or data2 may be NULL to omit it.  Allocate thash if it is empty.
73*7f2fe78bSCy Schubert  */
74*7f2fe78bSCy Schubert krb5_error_code
update_thash(krb5_context context,groupstate * gstate,int32_t group,krb5_data * thash,const krb5_data * data1,const krb5_data * data2)75*7f2fe78bSCy Schubert update_thash(krb5_context context, groupstate *gstate, int32_t group,
76*7f2fe78bSCy Schubert              krb5_data *thash, const krb5_data *data1, const krb5_data *data2)
77*7f2fe78bSCy Schubert {
78*7f2fe78bSCy Schubert     krb5_error_code ret;
79*7f2fe78bSCy Schubert     size_t hashlen;
80*7f2fe78bSCy Schubert     krb5_data dlist[3];
81*7f2fe78bSCy Schubert     const krb5_data empty = empty_data();
82*7f2fe78bSCy Schubert 
83*7f2fe78bSCy Schubert     if (thash->length == 0) {
84*7f2fe78bSCy Schubert         /* Initialize the transcript hash to all zeros. */
85*7f2fe78bSCy Schubert         ret = group_hash_len(group, &hashlen);
86*7f2fe78bSCy Schubert         if (ret)
87*7f2fe78bSCy Schubert             return ret;
88*7f2fe78bSCy Schubert         ret = alloc_data(thash, hashlen);
89*7f2fe78bSCy Schubert         if (ret)
90*7f2fe78bSCy Schubert             return ret;
91*7f2fe78bSCy Schubert     }
92*7f2fe78bSCy Schubert 
93*7f2fe78bSCy Schubert     /* Set up the data array and hash it with the group's hash function. */
94*7f2fe78bSCy Schubert     dlist[0] = *thash;
95*7f2fe78bSCy Schubert     dlist[1] = (data1 != NULL) ? *data1 : empty;
96*7f2fe78bSCy Schubert     dlist[2] = (data2 != NULL) ? *data2 : empty;
97*7f2fe78bSCy Schubert     return group_hash(context, gstate, group, dlist, 3,
98*7f2fe78bSCy Schubert                       (uint8_t *)thash->data);
99*7f2fe78bSCy Schubert }
100*7f2fe78bSCy Schubert 
101*7f2fe78bSCy Schubert /* Derive a byte vector for the SPAKE w multiplier input from ikey.  Place
102*7f2fe78bSCy Schubert  * result in allocated storage in *wbytes_out. */
103*7f2fe78bSCy Schubert krb5_error_code
derive_wbytes(krb5_context context,int32_t group,const krb5_keyblock * ikey,krb5_data * wbytes_out)104*7f2fe78bSCy Schubert derive_wbytes(krb5_context context, int32_t group, const krb5_keyblock *ikey,
105*7f2fe78bSCy Schubert               krb5_data *wbytes_out)
106*7f2fe78bSCy Schubert {
107*7f2fe78bSCy Schubert     krb5_error_code ret;
108*7f2fe78bSCy Schubert     const char prefix[] = "SPAKEsecret";
109*7f2fe78bSCy Schubert     size_t mult_len, prefix_len = sizeof(prefix) - 1;
110*7f2fe78bSCy Schubert     krb5_data prf_input = empty_data(), wbytes = empty_data();
111*7f2fe78bSCy Schubert 
112*7f2fe78bSCy Schubert     *wbytes_out = empty_data();
113*7f2fe78bSCy Schubert 
114*7f2fe78bSCy Schubert     /* Allocate space for a multiplier. */
115*7f2fe78bSCy Schubert     ret = group_mult_len(group, &mult_len);
116*7f2fe78bSCy Schubert     if (ret)
117*7f2fe78bSCy Schubert         goto cleanup;
118*7f2fe78bSCy Schubert     ret = alloc_data(&wbytes, mult_len);
119*7f2fe78bSCy Schubert     if (ret)
120*7f2fe78bSCy Schubert         goto cleanup;
121*7f2fe78bSCy Schubert 
122*7f2fe78bSCy Schubert     /* Compose the PRF input string. */
123*7f2fe78bSCy Schubert     ret = alloc_data(&prf_input, prefix_len + 4);
124*7f2fe78bSCy Schubert     if (ret)
125*7f2fe78bSCy Schubert         goto cleanup;
126*7f2fe78bSCy Schubert     memcpy(prf_input.data, prefix, prefix_len);
127*7f2fe78bSCy Schubert     store_32_be(group, prf_input.data + prefix_len);
128*7f2fe78bSCy Schubert 
129*7f2fe78bSCy Schubert     /* Derive the SPAKE input from the initial reply key with PRF+. */
130*7f2fe78bSCy Schubert     ret = krb5_c_prfplus(context, ikey, &prf_input, &wbytes);
131*7f2fe78bSCy Schubert     if (ret)
132*7f2fe78bSCy Schubert         goto cleanup;
133*7f2fe78bSCy Schubert 
134*7f2fe78bSCy Schubert     *wbytes_out = wbytes;
135*7f2fe78bSCy Schubert     wbytes = empty_data();
136*7f2fe78bSCy Schubert 
137*7f2fe78bSCy Schubert cleanup:
138*7f2fe78bSCy Schubert     free(prf_input.data);
139*7f2fe78bSCy Schubert     zapfree(wbytes.data, wbytes.length);
140*7f2fe78bSCy Schubert     return ret;
141*7f2fe78bSCy Schubert }
142*7f2fe78bSCy Schubert 
143*7f2fe78bSCy Schubert /*
144*7f2fe78bSCy Schubert  * Derive K'[n] from the group number, the initial key enctype, the initial
145*7f2fe78bSCy Schubert  * multiplier, the SPAKE result, the transcript hash, and the encoded
146*7f2fe78bSCy Schubert  * KDC-REQ-BODY.  Place the result in allocated storage in *out.
147*7f2fe78bSCy Schubert  */
148*7f2fe78bSCy Schubert krb5_error_code
derive_key(krb5_context context,groupstate * gstate,int32_t group,const krb5_keyblock * ikey,const krb5_data * wbytes,const krb5_data * spakeresult,const krb5_data * thash,const krb5_data * der_req,uint32_t n,krb5_keyblock ** out)149*7f2fe78bSCy Schubert derive_key(krb5_context context, groupstate *gstate, int32_t group,
150*7f2fe78bSCy Schubert            const krb5_keyblock *ikey, const krb5_data *wbytes,
151*7f2fe78bSCy Schubert            const krb5_data *spakeresult, const krb5_data *thash,
152*7f2fe78bSCy Schubert            const krb5_data *der_req, uint32_t n, krb5_keyblock **out)
153*7f2fe78bSCy Schubert {
154*7f2fe78bSCy Schubert     krb5_error_code ret;
155*7f2fe78bSCy Schubert     krb5_data dlist[9], seed = empty_data(), d;
156*7f2fe78bSCy Schubert     uint8_t groupnbuf[4], etypenbuf[4], nbuf[4], bcount;
157*7f2fe78bSCy Schubert     size_t hashlen, seedlen, keylen, nblocks, i;
158*7f2fe78bSCy Schubert     size_t ndata = sizeof(dlist) / sizeof(*dlist);
159*7f2fe78bSCy Schubert     krb5_keyblock *hkey = NULL;
160*7f2fe78bSCy Schubert 
161*7f2fe78bSCy Schubert     *out = NULL;
162*7f2fe78bSCy Schubert 
163*7f2fe78bSCy Schubert     store_32_be(group, groupnbuf);
164*7f2fe78bSCy Schubert     store_32_be(n, nbuf);
165*7f2fe78bSCy Schubert     store_32_be(ikey->enctype, etypenbuf);
166*7f2fe78bSCy Schubert     dlist[0] = string2data("SPAKEkey");
167*7f2fe78bSCy Schubert     dlist[1] = make_data(groupnbuf, sizeof(groupnbuf));
168*7f2fe78bSCy Schubert     dlist[2] = make_data(etypenbuf, sizeof(etypenbuf));
169*7f2fe78bSCy Schubert     dlist[3] = *wbytes;
170*7f2fe78bSCy Schubert     dlist[4] = *spakeresult;
171*7f2fe78bSCy Schubert     dlist[5] = *thash;
172*7f2fe78bSCy Schubert     dlist[6] = *der_req;
173*7f2fe78bSCy Schubert     dlist[7] = make_data(nbuf, sizeof(nbuf));
174*7f2fe78bSCy Schubert     dlist[8] = make_data(&bcount, 1);
175*7f2fe78bSCy Schubert 
176*7f2fe78bSCy Schubert     /* Count the number of hash blocks required (should be 1 for all current
177*7f2fe78bSCy Schubert      * scenarios) and allocate space. */
178*7f2fe78bSCy Schubert     ret = group_hash_len(group, &hashlen);
179*7f2fe78bSCy Schubert     if (ret)
180*7f2fe78bSCy Schubert         goto cleanup;
181*7f2fe78bSCy Schubert     ret = krb5_c_keylengths(context, ikey->enctype, &seedlen, &keylen);
182*7f2fe78bSCy Schubert     if (ret)
183*7f2fe78bSCy Schubert         goto cleanup;
184*7f2fe78bSCy Schubert     nblocks = (seedlen + hashlen - 1) / hashlen;
185*7f2fe78bSCy Schubert     ret = alloc_data(&seed, nblocks * hashlen);
186*7f2fe78bSCy Schubert     if (ret)
187*7f2fe78bSCy Schubert         goto cleanup;
188*7f2fe78bSCy Schubert 
189*7f2fe78bSCy Schubert     /* Compute and concatenate hash blocks to fill the seed buffer. */
190*7f2fe78bSCy Schubert     for (i = 0; i < nblocks; i++) {
191*7f2fe78bSCy Schubert         bcount = i + 1;
192*7f2fe78bSCy Schubert         ret = group_hash(context, gstate, group, dlist, ndata,
193*7f2fe78bSCy Schubert                          (uint8_t *)seed.data + i * hashlen);
194*7f2fe78bSCy Schubert         if (ret)
195*7f2fe78bSCy Schubert             goto cleanup;
196*7f2fe78bSCy Schubert     }
197*7f2fe78bSCy Schubert 
198*7f2fe78bSCy Schubert     ret = krb5_init_keyblock(context, ikey->enctype, keylen, &hkey);
199*7f2fe78bSCy Schubert     if (ret)
200*7f2fe78bSCy Schubert         goto cleanup;
201*7f2fe78bSCy Schubert     d = make_data(seed.data, seedlen);
202*7f2fe78bSCy Schubert     ret = krb5_c_random_to_key(context, ikey->enctype, &d, hkey);
203*7f2fe78bSCy Schubert     if (ret)
204*7f2fe78bSCy Schubert         goto cleanup;
205*7f2fe78bSCy Schubert 
206*7f2fe78bSCy Schubert     ret = krb5_c_fx_cf2_simple(context, ikey, "SPAKE", hkey, "keyderiv", out);
207*7f2fe78bSCy Schubert 
208*7f2fe78bSCy Schubert cleanup:
209*7f2fe78bSCy Schubert     zapfree(seed.data, seed.length);
210*7f2fe78bSCy Schubert     krb5_free_keyblock(context, hkey);
211*7f2fe78bSCy Schubert     return ret;
212*7f2fe78bSCy Schubert }
213