xref: /illumos-gate/usr/src/uts/common/gssapi/mechs/krb5/crypto/make_checksum.c (revision 489f6310fe8952e87fc1dce8af87990fcfd90f18)
1 /*
2  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  *
5  * Copyright 2025 RackTop Systems, Inc.
6  */
7 
8 /*
9  * Copyright (C) 1998 by the FundsXpress, INC.
10  *
11  * All rights reserved.
12  *
13  * Export of this software from the United States of America may require
14  * a specific license from the United States Government.  It is the
15  * responsibility of any person or organization contemplating export to
16  * obtain such a license before exporting.
17  *
18  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
19  * distribute this software and its documentation for any purpose and
20  * without fee is hereby granted, provided that the above copyright
21  * notice appear in all copies and that both that copyright notice and
22  * this permission notice appear in supporting documentation, and that
23  * the name of FundsXpress. not be used in advertising or publicity pertaining
24  * to distribution of the software without specific, written prior
25  * permission.  FundsXpress makes no representations about the suitability of
26  * this software for any purpose.  It is provided "as is" without express
27  * or implied warranty.
28  *
29  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
30  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
31  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  */
33 
34 #include "k5-int.h"
35 #include "cksumtypes.h"
36 #include "etypes.h"
37 #include "dk.h"
38 
39 krb5_error_code KRB5_CALLCONV
krb5_c_make_checksum(krb5_context context,krb5_cksumtype cksumtype,const krb5_keyblock * key,krb5_keyusage usage,const krb5_data * input,krb5_checksum * cksum)40 krb5_c_make_checksum(krb5_context context, krb5_cksumtype cksumtype,
41 		     const krb5_keyblock *key, krb5_keyusage usage,
42 		     const krb5_data *input, krb5_checksum *cksum)
43 {
44     int i, e1, e2;
45     krb5_data data;
46     krb5_error_code ret = 0;
47     size_t cksumlen;
48 
49     KRB5_LOG0(KRB5_INFO, "krb5_c_make_checksum() start.");
50 
51     for (i=0; i<krb5_cksumtypes_length; i++) {
52 	if (krb5_cksumtypes_list[i].ctype == cksumtype)
53 	    break;
54     }
55 
56     if (i == krb5_cksumtypes_length)
57 	return(KRB5_BAD_ENCTYPE);
58 
59     if (krb5_cksumtypes_list[i].keyhash)
60 	cksumlen = krb5_cksumtypes_list[i].keyhash->hashsize;
61     else
62 	cksumlen = krb5_cksumtypes_list[i].hash->hashsize;
63 
64 #ifdef _KERNEL
65     context->kef_cksum_mt = krb5_cksumtypes_list[i].kef_cksum_mt;
66 #endif
67     cksum->length = cksumlen;
68 
69     if ((cksum->contents = (krb5_octet *) MALLOC(cksum->length)) == NULL) {
70 	cksum->length = 0;
71 	return(ENOMEM);
72     }
73 
74     data.length = cksum->length;
75     data.data = (char *) cksum->contents;
76 
77     if (krb5_cksumtypes_list[i].keyhash) {
78 	/* check if key is compatible */
79 
80 	if (krb5_cksumtypes_list[i].keyed_etype) {
81 	    for (e1=0; e1<krb5_enctypes_length; e1++)
82 		if (krb5_enctypes_list[e1].etype ==
83 		    krb5_cksumtypes_list[i].keyed_etype)
84 		    break;
85 
86 	    for (e2=0; e2<krb5_enctypes_length; e2++)
87 		if (krb5_enctypes_list[e2].etype == key->enctype)
88 		    break;
89 
90 	    /*
91 	     * Solaris Kerberos: The actual key encryption type could be
92 	     * arbitrary, so the checksum enc type doesn't need to be the same.
93 	     */
94 	    if ((e1 == krb5_enctypes_length) || (e2 == krb5_enctypes_length)) {
95 		ret = KRB5_BAD_ENCTYPE;
96 		goto cleanup;
97 	    }
98 	}
99 #ifdef _KERNEL
100 	context->kef_cipher_mt = krb5_enctypes_list[e1].kef_cipher_mt;
101 	context->kef_hash_mt = krb5_enctypes_list[e1].kef_hash_mt;
102 	if (key->kef_key.ck_data == NULL) {
103 		if ((ret = init_key_kef(context->kef_cipher_mt,
104 				(krb5_keyblock *)key)))
105 			goto cleanup;
106 	}
107 #else
108 	if ((ret = init_key_uef(krb_ctx_hSession(context), (krb5_keyblock *)key)))
109 		goto cleanup;
110 #endif /* _KERNEL */
111 
112 	ret = (*(krb5_cksumtypes_list[i].keyhash->hash))(context, key,
113 						usage, 0, input, &data);
114     } else if (krb5_cksumtypes_list[i].flags & KRB5_CKSUMFLAG_DERIVE) {
115 #ifdef _KERNEL
116     	context->kef_cipher_mt = get_cipher_mech_type(context,
117 					(krb5_keyblock *)key);
118     	context->kef_hash_mt = get_hash_mech_type(context,
119 					(krb5_keyblock *)key);
120 	/*
121 	 * If the hash_mt is invalid, try using the cksum_mt
122 	 * because "hash" and "checksum" are overloaded terms
123 	 * in some places.
124 	 */
125 	if (context->kef_hash_mt == CRYPTO_MECH_INVALID)
126 		context->kef_hash_mt = context->kef_cksum_mt;
127 #else
128 	ret = init_key_uef(krb_ctx_hSession(context), (krb5_keyblock *)key);
129 	if (ret)
130 		goto cleanup;
131 #endif /* _KERNEL */
132 	ret = krb5_dk_make_checksum(context,
133 				krb5_cksumtypes_list[i].hash,
134 				key, usage, input, &data);
135     } else {
136 	    /*
137 	     * No key is used, hash and cksum are synonymous
138 	     * in this case
139 	     */
140 #ifdef _KERNEL
141 	    context->kef_hash_mt = context->kef_cksum_mt;
142 #endif /* _KERNEL */
143 	    ret = (*(krb5_cksumtypes_list[i].hash->hash))(context, 1,
144 							input, &data);
145     }
146 
147     if (!ret) {
148 	cksum->magic = KV5M_CHECKSUM;
149 	cksum->checksum_type = cksumtype;
150 	if (krb5_cksumtypes_list[i].trunc_size) {
151 	    krb5_octet *trunc;
152             size_t old_len = cksum->length;
153 
154             /*
155              * Solaris Kerberos:
156              * The Kernel does not like 'realloc' (which is what
157              * MIT code does here), so we do our own "realloc".
158              */
159             cksum->length = krb5_cksumtypes_list[i].trunc_size;
160             trunc = (krb5_octet *) MALLOC(cksum->length);
161             if (trunc) {
162                 (void) memcpy(trunc, cksum->contents, cksum->length);
163                 FREE(cksum->contents, old_len);
164                 cksum->contents = trunc;
165             } else {
166                 ret = ENOMEM;
167             }
168         }
169     }
170 
171 cleanup:
172     if (ret) {
173 	(void) memset(cksum->contents, 0, cksum->length);
174 	FREE(cksum->contents, cksum->length);
175 	cksum->length = 0;
176 	cksum->contents = NULL;
177     }
178 
179     KRB5_LOG(KRB5_INFO, "krb5_c_make_checksum() end ret = %d\n", ret);
180     return(ret);
181 }
182