1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/gssapi/krb5/prf.c */
4 * Copyright 2009 by the Massachusetts Institute of Technology.
7 * Export of this software from the United States of America may
8 * require a specific license from the United States Government.
9 * It is the responsibility of any person or organization contemplating
10 * export to obtain such a license before exporting.
12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13 * distribute this software and its documentation for any purpose and
14 * without fee is hereby granted, provided that the above copyright
15 * notice appear in all copies and that both that copyright notice and
16 * this permission notice appear in supporting documentation, and that
17 * the name of M.I.T. not be used in advertising or publicity pertaining
18 * to distribution of the software without specific, written prior
19 * permission. Furthermore if you modify this software you must label
20 * your software as modified software and not distribute it in such a
21 * fashion that it might be confused with the original M.I.T. software.
22 * M.I.T. makes no representations about the suitability of
23 * this software for any purpose. It is provided "as is" without express
24 * or implied warranty.
28 #include "gssapiP_krb5.h"
30 #ifndef MIN /* Usually found in <sys/param.h>. */
31 #define MIN(_a,_b) ((_a)<(_b)?(_a):(_b))
34 OM_uint32 KRB5_CALLCONV
35 krb5_gss_pseudo_random(OM_uint32 *minor_status,
38 const gss_buffer_t prf_in,
39 ssize_t desired_output_len,
44 krb5_gss_ctx_id_t ctx;
52 prf_out->value = NULL;
60 ctx = (krb5_gss_ctx_id_t)context;
61 if (ctx->terminated || !ctx->established) {
62 *minor_status = KG_CTX_INCOMPLETE;
63 return GSS_S_NO_CONTEXT;
67 case GSS_C_PRF_KEY_FULL:
68 if (ctx->have_acceptor_subkey) {
69 key = ctx->acceptor_subkey;
73 case GSS_C_PRF_KEY_PARTIAL:
86 if (desired_output_len == 0)
87 return GSS_S_COMPLETE;
89 prf_out->value = gssalloc_malloc(desired_output_len);
90 if (prf_out->value == NULL) {
91 code = KG_INPUT_TOO_LONG;
94 prf_out->length = desired_output_len;
96 code = krb5_c_prf_length(ctx->k5_context,
97 krb5_k_key_enctype(ctx->k5_context, key),
102 ns.length = 4 + prf_in->length;
103 ns.data = k5alloc(ns.length, &code);
104 if (ns.data == NULL) {
105 code = KG_INPUT_TOO_LONG;
110 t.data = k5alloc(t.length, &code);
114 memcpy(ns.data + 4, prf_in->value, prf_in->length);
116 p = (unsigned char *)prf_out->value;
117 while (desired_output_len > 0) {
118 store_32_be(i, ns.data);
120 code = krb5_k_prf(ctx->k5_context, key, &ns, &t);
124 memcpy(p, t.data, MIN(t.length, desired_output_len));
127 desired_output_len -= t.length;
133 gss_release_buffer(&minor, prf_out);
134 krb5_free_data_contents(ctx->k5_context, &ns);
135 krb5_free_data_contents(ctx->k5_context, &t);
137 *minor_status = (OM_uint32)code;
138 return (code == 0) ? GSS_S_COMPLETE : GSS_S_FAILURE;