gss_pseudo_random implementation
[moonshot.git] / mech_eap / pseudo_random.c
1 /*
2  * Copyright (c) 2010, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 /*
33  * lib/gssapi/krb5/prf.c
34  *
35  * Copyright 2009 by the Massachusetts Institute of Technology.
36  * All Rights Reserved.
37  *
38  * Export of this software from the United States of America may
39  *   require a specific license from the United States Government.
40  *   It is the responsibility of any person or organization contemplating
41  *   export to obtain such a license before exporting.
42  *
43  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
44  * distribute this software and its documentation for any purpose and
45  * without fee is hereby granted, provided that the above copyright
46  * notice appear in all copies and that both that copyright notice and
47  * this permission notice appear in supporting documentation, and that
48  * the name of M.I.T. not be used in advertising or publicity pertaining
49  * to distribution of the software without specific, written prior
50  * permission.  Furthermore if you modify this software you must label
51  * your software as modified software and not distribute it in such a
52  * fashion that it might be confused with the original M.I.T. software.
53  * M.I.T. makes no representations about the suitability of
54  * this software for any purpose.  It is provided "as is" without express
55  * or implied warranty.
56  *
57  *
58  */
59
60 #include "gssapiP_eap.h"
61
62 #ifndef MIN             /* Usually found in <sys/param.h>. */
63 #define MIN(_a,_b)  ((_a)<(_b)?(_a):(_b))
64 #endif
65
66 OM_uint32
67 gss_pseudo_random(OM_uint32 *minor,
68                   gss_ctx_id_t ctx,
69                   int prf_key,
70                   const gss_buffer_t prf_in,
71                   ssize_t desired_output_len,
72                   gss_buffer_t prf_out)
73 {
74     krb5_error_code code;
75     int i;
76     OM_uint32 tmpMinor;
77     size_t prflen;
78     krb5_data t, ns;
79     unsigned char *p;
80
81     prf_out->length = 0;
82     prf_out->value = NULL;
83
84     if (!CTX_IS_ESTABLISHED(ctx))
85         return GSS_S_NO_CONTEXT;
86
87     t.length = 0;
88     t.data = NULL;
89
90     ns.length = 0;
91     ns.data = NULL;
92
93     if (prf_key != GSS_C_PRF_KEY_FULL &&
94         prf_key != GSS_C_PRF_KEY_FULL) {
95         code = EINVAL;
96         goto cleanup;
97     }
98
99     prf_out->value = GSSEAP_MALLOC(desired_output_len);
100     if (prf_out->value == NULL) {
101         code = ENOMEM;
102         goto cleanup;
103     }
104     prf_out->length = desired_output_len;
105
106     code = krb5_c_prf_length(ctx->kerberosCtx,
107                              ctx->encryptionType,
108                              &prflen);
109     if (code != 0)
110         goto cleanup;
111
112     ns.length = 4 + prf_in->length;
113     ns.data = GSSEAP_MALLOC(ns.length);
114     if (ns.data == NULL) {
115         code = ENOMEM;
116         goto cleanup;
117     }
118
119     t.length = prflen;
120     t.data = GSSEAP_MALLOC(t.length);
121     if (t.data == NULL) {
122         code = ENOMEM;
123         goto cleanup;
124     }
125
126     memcpy(ns.data + 4, prf_in->value, prf_in->length);
127     i = 0;
128     p = (unsigned char *)prf_out->value;
129     while (desired_output_len > 0) {
130         store_uint32_be(i, ns.data);
131
132         code = krb5_c_prf(ctx->kerberosCtx, ctx->encryptionKey, &ns, &t);
133         if (code != 0)
134             goto cleanup;
135
136         memcpy(p, t.data, MIN(t.length, desired_output_len));
137
138         p += t.length;
139         desired_output_len -= t.length;
140         i++;
141     }
142
143 cleanup:
144     if (code != 0)
145         gss_release_buffer(&tmpMinor, prf_out);
146     krb5_free_data_contents(ctx->kerberosCtx, &ns);
147     krb5_free_data_contents(ctx->kerberosCtx, &t);
148
149     *minor = code;
150     return (code == 0) ? GSS_S_COMPLETE : GSS_S_FAILURE;
151 }
152