summaryrefslogtreecommitdiffstats
path: root/third_party/heimdal/lib/gssapi/sanon/crypto.c
blob: 0c7a67f7077188aedc6847ae1d0526861a24afaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
 * Copyright (c) 2019-2020, AuriStor, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include "sanon_locl.h"

OM_uint32 GSSAPI_CALLCONV
_gss_sanon_wrap(OM_uint32 *minor,
		gss_const_ctx_id_t context_handle,
		int conf_req_flag,
		gss_qop_t qop_req,
		const gss_buffer_t input_message_buffer,
		int *conf_state,
		gss_buffer_t output_message_buffer)
{
    const sanon_ctx sc = (const sanon_ctx)context_handle;

    if (sc->rfc4121 == GSS_C_NO_CONTEXT) {
	*minor = GSS_KRB5_S_KG_CTX_INCOMPLETE;
	return GSS_S_NO_CONTEXT;
    }

    return gss_wrap(minor, sc->rfc4121,
		    conf_req_flag, qop_req,
		    input_message_buffer, conf_state,
		    output_message_buffer);
}

OM_uint32 GSSAPI_CALLCONV
_gss_sanon_wrap_size_limit(OM_uint32 *minor,
			   gss_const_ctx_id_t context_handle,
			   int conf_req_flag,
			   gss_qop_t qop_req,
			   OM_uint32 req_output_size,
			   OM_uint32 *max_input_size)
{
    const sanon_ctx sc = (const sanon_ctx)context_handle;

    if (sc->rfc4121 == GSS_C_NO_CONTEXT) {
	*minor = GSS_KRB5_S_KG_CTX_INCOMPLETE;
	return GSS_S_NO_CONTEXT;
    }

    return gss_wrap_size_limit(minor, sc->rfc4121,
			       conf_req_flag, qop_req,
			       req_output_size, max_input_size);
}

OM_uint32 GSSAPI_CALLCONV
_gss_sanon_wrap_iov(OM_uint32 *minor,
		    gss_ctx_id_t context_handle,
		    int conf_req_flag,
		    gss_qop_t qop_req,
		    int *conf_state,
		    gss_iov_buffer_desc *iov,
		    int iov_count)
{
    const sanon_ctx sc = (const sanon_ctx)context_handle;

    if (sc->rfc4121 == GSS_C_NO_CONTEXT) {
	*minor = GSS_KRB5_S_KG_CTX_INCOMPLETE;
	return GSS_S_NO_CONTEXT;
    }

    return gss_wrap_iov(minor, sc->rfc4121,
			conf_req_flag, qop_req,
			conf_state, iov, iov_count);
}

OM_uint32 GSSAPI_CALLCONV
_gss_sanon_wrap_iov_length(OM_uint32 *minor,
			   gss_ctx_id_t context_handle,
			   int conf_req_flag,
			   gss_qop_t qop_req,
			   int *conf_state,
			   gss_iov_buffer_desc *iov,
			   int iov_count)
{
    const sanon_ctx sc = (const sanon_ctx)context_handle;

    if (sc->rfc4121 == GSS_C_NO_CONTEXT) {
	*minor = GSS_KRB5_S_KG_CTX_INCOMPLETE;
	return GSS_S_NO_CONTEXT;
    }

    return gss_wrap_iov_length(minor, sc->rfc4121,
			       conf_req_flag, qop_req,
			       conf_state, iov, iov_count);
}

OM_uint32 GSSAPI_CALLCONV
_gss_sanon_unwrap(OM_uint32 *minor,
		  gss_const_ctx_id_t context_handle,
		  const gss_buffer_t input_message_buffer,
		  gss_buffer_t output_message_buffer,
		  int *conf_state,
		  gss_qop_t * qop_state)
{
    const sanon_ctx sc = (const sanon_ctx)context_handle;

    if (sc->rfc4121 == GSS_C_NO_CONTEXT) {
	*minor = GSS_KRB5_S_KG_CTX_INCOMPLETE;
	return GSS_S_NO_CONTEXT;
    }

    return gss_unwrap(minor, sc->rfc4121,
		      input_message_buffer, output_message_buffer,
		      conf_state, qop_state);
}

OM_uint32 GSSAPI_CALLCONV
_gss_sanon_unwrap_iov(OM_uint32 *minor,
		      gss_ctx_id_t context_handle,
		      int *conf_state,
		      gss_qop_t *qop_state,
		      gss_iov_buffer_desc *iov,
		      int iov_count)
{
    const sanon_ctx sc = (const sanon_ctx)context_handle;

    if (sc->rfc4121 == GSS_C_NO_CONTEXT) {
	*minor = GSS_KRB5_S_KG_CTX_INCOMPLETE;
	return GSS_S_NO_CONTEXT;
    }

    return gss_unwrap_iov(minor, sc->rfc4121,
			  conf_state, qop_state,
			  iov, iov_count);
}

OM_uint32 GSSAPI_CALLCONV
_gss_sanon_get_mic(OM_uint32 *minor,
		   gss_const_ctx_id_t context_handle,
		   gss_qop_t qop_req,
		   const gss_buffer_t message_buffer,
		   gss_buffer_t message_token)
{
    const sanon_ctx sc = (const sanon_ctx)context_handle;

    if (sc->rfc4121 == GSS_C_NO_CONTEXT) {
	*minor = GSS_KRB5_S_KG_CTX_INCOMPLETE;
	return GSS_S_NO_CONTEXT;
    }

    return gss_get_mic(minor, sc->rfc4121,
		       qop_req, message_buffer,
		       message_token);
}

OM_uint32 GSSAPI_CALLCONV
_gss_sanon_verify_mic(OM_uint32 *minor,
		      gss_const_ctx_id_t context_handle,
		      const gss_buffer_t message_buffer,
		      const gss_buffer_t token_buffer,
		      gss_qop_t *qop_state)
{
    const sanon_ctx sc = (const sanon_ctx)context_handle;

    if (sc->rfc4121 == GSS_C_NO_CONTEXT) {
	*minor = GSS_KRB5_S_KG_CTX_INCOMPLETE;
	return GSS_S_NO_CONTEXT;
    }

    return gss_verify_mic(minor, sc->rfc4121,
			  message_buffer, token_buffer,
			  qop_state);
}

OM_uint32 GSSAPI_CALLCONV
_gss_sanon_pseudo_random(OM_uint32 *minor,
			 gss_ctx_id_t context_handle,
			 int prf_key,
			 const gss_buffer_t prf_in,
			 ssize_t desired_output_len,
			 gss_buffer_t prf_out)
{
    const sanon_ctx sc = (const sanon_ctx)context_handle;

    if (sc->rfc4121 == GSS_C_NO_CONTEXT) {
	*minor = GSS_KRB5_S_KG_CTX_INCOMPLETE;
	return GSS_S_NO_CONTEXT;
    }

    return gss_pseudo_random(minor, sc->rfc4121,
			     prf_key, prf_in, desired_output_len,
			     prf_out);
}

/*
 * Generate a curve25519 secret and public key
 */

OM_uint32
_gss_sanon_curve25519_base(OM_uint32 *minor, sanon_ctx sc)
{
    krb5_generate_random_block(sc->sk, crypto_scalarmult_curve25519_BYTES);

    if (crypto_scalarmult_curve25519_base(sc->pk, sc->sk) != 0) {
	*minor = EINVAL;
	return GSS_S_FAILURE;
    }

    return GSS_S_COMPLETE;
}

/*
 * Derive the context session key using SP800-108 KDF in HMAC mode
 * and the public keys and channel binding data.
 */

OM_uint32
_gss_sanon_curve25519(OM_uint32 *minor,
		      sanon_ctx sc,
		      gss_buffer_t pk,
		      OM_uint32 gss_flags,
		      const gss_channel_bindings_t input_chan_bindings,
		      gss_buffer_t session_key)
{
    uint8_t shared[crypto_scalarmult_curve25519_BYTES], *p;
    krb5_error_code ret;
    krb5_context context;
    krb5_data kdf_K1, kdf_label, kdf_context, keydata;

    _mg_buffer_zero(session_key);

    if (pk == GSS_C_NO_BUFFER || pk->length != crypto_scalarmult_curve25519_BYTES)
	return GSS_S_DEFECTIVE_TOKEN;

    if (crypto_scalarmult_curve25519(shared, sc->sk, pk->value) != 0)
	return GSS_S_FAILURE;

    ret = krb5_init_context(&context);
    if (ret != 0) {
	*minor = ret;
	return GSS_S_FAILURE;
    }

    kdf_K1.data = shared;
    kdf_K1.length = sizeof(shared);

    kdf_label.data = "sanon-x25519";
    kdf_label.length = sizeof("sanon-x25519") - 1;

    ret = krb5_data_alloc(&kdf_context,
			  2 * crypto_scalarmult_curve25519_BYTES + 8 +
			  (input_chan_bindings ? input_chan_bindings->application_data.length : 0));
    if (ret != 0) {
	krb5_free_context(context);
	*minor = ret;
	return GSS_S_FAILURE;
    }

    p = kdf_context.data;

    if (sc->is_initiator) {
	memcpy(p, sc->pk, sizeof(sc->pk));
	memcpy(&p[pk->length], pk->value, pk->length);
    } else {
	memcpy(p, pk->value, pk->length);
	memcpy(&p[sizeof(sc->pk)], sc->pk, sizeof(sc->pk));
    }
    p += 2 * crypto_scalarmult_curve25519_BYTES;
    _gss_mg_encode_be_uint32(0, p); /* upper 32 bits presently unused */
    p += 4;
    _gss_mg_encode_be_uint32(gss_flags, p);
    p += 4;

    if (input_chan_bindings != GSS_C_NO_CHANNEL_BINDINGS &&
	input_chan_bindings->application_data.value != NULL) {
	memcpy(p, input_chan_bindings->application_data.value,
	       input_chan_bindings->application_data.length);
    }

    ret = krb5_data_alloc(&keydata, 16);
    if (ret == 0) {
	ret = _krb5_SP800_108_HMAC_KDF(context, &kdf_K1, &kdf_label,
				       &kdf_context, EVP_sha256(), &keydata);

	session_key->length = keydata.length;
	session_key->value = keydata.data;
    } else {
	krb5_data_free(&keydata);
    }

    memset_s(kdf_context.data, kdf_context.length, 0, kdf_context.length);
    krb5_data_free(&kdf_context);

    memset_s(shared, sizeof(shared), 0, sizeof(shared));

    krb5_free_context(context);

    *minor = ret;
    return ret != 0 ? GSS_S_FAILURE : GSS_S_COMPLETE;
}

OM_uint32
_gss_sanon_import_rfc4121_context(OM_uint32 *minor,
				  sanon_ctx sc,
				  OM_uint32 gss_flags,
				  gss_const_buffer_t session_key)
{
    return _gss_mg_import_rfc4121_context(minor, sc->is_initiator, gss_flags,
                                          KRB5_ENCTYPE_AES128_CTS_HMAC_SHA256_128,
                                          session_key, &sc->rfc4121);
}