xref: /minix3/crypto/external/bsd/heimdal/dist/lib/gssapi/krb5/prf.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: prf.c,v 1.1.1.2 2014/04/24 12:45:29 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 2007 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc  * are met:
11ebfedea0SLionel Sambuc  *
12ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc  *
15ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc  *
19ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc  *    without specific prior written permission.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc  * SUCH DAMAGE.
34ebfedea0SLionel Sambuc  */
35ebfedea0SLionel Sambuc 
36ebfedea0SLionel Sambuc #include "gsskrb5_locl.h"
37ebfedea0SLionel Sambuc 
38ebfedea0SLionel Sambuc OM_uint32 GSSAPI_CALLCONV
_gsskrb5_pseudo_random(OM_uint32 * minor_status,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)39ebfedea0SLionel Sambuc _gsskrb5_pseudo_random(OM_uint32 *minor_status,
40ebfedea0SLionel Sambuc 		       gss_ctx_id_t context_handle,
41ebfedea0SLionel Sambuc 		       int prf_key,
42ebfedea0SLionel Sambuc 		       const gss_buffer_t prf_in,
43ebfedea0SLionel Sambuc 		       ssize_t desired_output_len,
44ebfedea0SLionel Sambuc 		       gss_buffer_t prf_out)
45ebfedea0SLionel Sambuc {
46ebfedea0SLionel Sambuc     gsskrb5_ctx ctx = (gsskrb5_ctx)context_handle;
47ebfedea0SLionel Sambuc     krb5_context context;
48ebfedea0SLionel Sambuc     krb5_error_code ret;
49ebfedea0SLionel Sambuc     krb5_crypto crypto;
50ebfedea0SLionel Sambuc     krb5_data input, output;
51ebfedea0SLionel Sambuc     uint32_t num;
52*0a6a1f1dSLionel Sambuc     OM_uint32 junk;
53ebfedea0SLionel Sambuc     unsigned char *p;
54ebfedea0SLionel Sambuc     krb5_keyblock *key = NULL;
55*0a6a1f1dSLionel Sambuc     size_t dol;
56ebfedea0SLionel Sambuc 
57ebfedea0SLionel Sambuc     if (ctx == NULL) {
58ebfedea0SLionel Sambuc 	*minor_status = 0;
59ebfedea0SLionel Sambuc 	return GSS_S_NO_CONTEXT;
60ebfedea0SLionel Sambuc     }
61ebfedea0SLionel Sambuc 
62*0a6a1f1dSLionel Sambuc     if (desired_output_len <= 0 || prf_in->length + 4 < prf_in->length) {
63ebfedea0SLionel Sambuc 	*minor_status = 0;
64ebfedea0SLionel Sambuc 	return GSS_S_FAILURE;
65ebfedea0SLionel Sambuc     }
66*0a6a1f1dSLionel Sambuc     dol = desired_output_len;
67ebfedea0SLionel Sambuc 
68ebfedea0SLionel Sambuc     GSSAPI_KRB5_INIT (&context);
69ebfedea0SLionel Sambuc 
70ebfedea0SLionel Sambuc     switch(prf_key) {
71ebfedea0SLionel Sambuc     case GSS_C_PRF_KEY_FULL:
72ebfedea0SLionel Sambuc 	_gsskrb5i_get_acceptor_subkey(ctx, context, &key);
73ebfedea0SLionel Sambuc 	break;
74ebfedea0SLionel Sambuc     case GSS_C_PRF_KEY_PARTIAL:
75ebfedea0SLionel Sambuc 	_gsskrb5i_get_initiator_subkey(ctx, context, &key);
76ebfedea0SLionel Sambuc 	break;
77ebfedea0SLionel Sambuc     default:
78ebfedea0SLionel Sambuc 	_gsskrb5_set_status(EINVAL, "unknown kerberos prf_key");
79ebfedea0SLionel Sambuc 	*minor_status = EINVAL;
80ebfedea0SLionel Sambuc 	return GSS_S_FAILURE;
81ebfedea0SLionel Sambuc     }
82ebfedea0SLionel Sambuc 
83ebfedea0SLionel Sambuc     if (key == NULL) {
84ebfedea0SLionel Sambuc 	_gsskrb5_set_status(EINVAL, "no prf_key found");
85ebfedea0SLionel Sambuc 	*minor_status = EINVAL;
86ebfedea0SLionel Sambuc 	return GSS_S_FAILURE;
87ebfedea0SLionel Sambuc     }
88ebfedea0SLionel Sambuc 
89ebfedea0SLionel Sambuc     ret = krb5_crypto_init(context, key, 0, &crypto);
90ebfedea0SLionel Sambuc     krb5_free_keyblock (context, key);
91ebfedea0SLionel Sambuc     if (ret) {
92ebfedea0SLionel Sambuc 	*minor_status = ret;
93ebfedea0SLionel Sambuc 	return GSS_S_FAILURE;
94ebfedea0SLionel Sambuc     }
95ebfedea0SLionel Sambuc 
96*0a6a1f1dSLionel Sambuc     prf_out->value = malloc(dol);
97ebfedea0SLionel Sambuc     if (prf_out->value == NULL) {
98ebfedea0SLionel Sambuc 	_gsskrb5_set_status(GSS_KRB5_S_KG_INPUT_TOO_LONG, "Out of memory");
99ebfedea0SLionel Sambuc 	*minor_status = GSS_KRB5_S_KG_INPUT_TOO_LONG;
100ebfedea0SLionel Sambuc 	krb5_crypto_destroy(context, crypto);
101ebfedea0SLionel Sambuc 	return GSS_S_FAILURE;
102ebfedea0SLionel Sambuc     }
103*0a6a1f1dSLionel Sambuc     prf_out->length = dol;
104ebfedea0SLionel Sambuc 
105ebfedea0SLionel Sambuc     HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);
106ebfedea0SLionel Sambuc 
107ebfedea0SLionel Sambuc     input.length = prf_in->length + 4;
108ebfedea0SLionel Sambuc     input.data = malloc(prf_in->length + 4);
109ebfedea0SLionel Sambuc     if (input.data == NULL) {
110ebfedea0SLionel Sambuc 	_gsskrb5_set_status(GSS_KRB5_S_KG_INPUT_TOO_LONG, "Out of memory");
111ebfedea0SLionel Sambuc 	*minor_status = GSS_KRB5_S_KG_INPUT_TOO_LONG;
112ebfedea0SLionel Sambuc 	gss_release_buffer(&junk, prf_out);
113ebfedea0SLionel Sambuc 	krb5_crypto_destroy(context, crypto);
114ebfedea0SLionel Sambuc 	HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
115ebfedea0SLionel Sambuc 	return GSS_S_FAILURE;
116ebfedea0SLionel Sambuc     }
117*0a6a1f1dSLionel Sambuc     memcpy(((uint8_t *)input.data) + 4, prf_in->value, prf_in->length);
118ebfedea0SLionel Sambuc 
119ebfedea0SLionel Sambuc     num = 0;
120ebfedea0SLionel Sambuc     p = prf_out->value;
121*0a6a1f1dSLionel Sambuc     while(dol > 0) {
122*0a6a1f1dSLionel Sambuc 	size_t tsize;
123*0a6a1f1dSLionel Sambuc 
124*0a6a1f1dSLionel Sambuc 	_gsskrb5_encode_be_om_uint32(num, input.data);
125*0a6a1f1dSLionel Sambuc 
126ebfedea0SLionel Sambuc 	ret = krb5_crypto_prf(context, crypto, &input, &output);
127ebfedea0SLionel Sambuc 	if (ret) {
128ebfedea0SLionel Sambuc 	    *minor_status = ret;
129ebfedea0SLionel Sambuc 	    free(input.data);
130ebfedea0SLionel Sambuc 	    gss_release_buffer(&junk, prf_out);
131ebfedea0SLionel Sambuc 	    krb5_crypto_destroy(context, crypto);
132ebfedea0SLionel Sambuc 	    HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
133ebfedea0SLionel Sambuc 	    return GSS_S_FAILURE;
134ebfedea0SLionel Sambuc 	}
135*0a6a1f1dSLionel Sambuc 
136*0a6a1f1dSLionel Sambuc 	tsize = min(dol, output.length);
137*0a6a1f1dSLionel Sambuc 	memcpy(p, output.data, tsize);
138*0a6a1f1dSLionel Sambuc 	p += tsize;
139*0a6a1f1dSLionel Sambuc 	dol -= tsize;
140ebfedea0SLionel Sambuc 	krb5_data_free(&output);
141ebfedea0SLionel Sambuc 	num++;
142ebfedea0SLionel Sambuc     }
143ebfedea0SLionel Sambuc     free(input.data);
144ebfedea0SLionel Sambuc 
145ebfedea0SLionel Sambuc     krb5_crypto_destroy(context, crypto);
146ebfedea0SLionel Sambuc 
147ebfedea0SLionel Sambuc     HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
148ebfedea0SLionel Sambuc 
149ebfedea0SLionel Sambuc     return GSS_S_COMPLETE;
150ebfedea0SLionel Sambuc }
151