xref: /minix3/crypto/external/bsd/heimdal/dist/lib/krb5/keyblock.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: keyblock.c,v 1.1.1.2 2014/04/24 12:45:50 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 1997 - 2001 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 "krb5_locl.h"
37ebfedea0SLionel Sambuc 
38ebfedea0SLionel Sambuc /**
39ebfedea0SLionel Sambuc  * Zero out a keyblock
40ebfedea0SLionel Sambuc  *
41ebfedea0SLionel Sambuc  * @param keyblock keyblock to zero out
42ebfedea0SLionel Sambuc  *
43ebfedea0SLionel Sambuc  * @ingroup krb5_crypto
44ebfedea0SLionel Sambuc  */
45ebfedea0SLionel Sambuc 
46ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_keyblock_zero(krb5_keyblock * keyblock)47ebfedea0SLionel Sambuc krb5_keyblock_zero(krb5_keyblock *keyblock)
48ebfedea0SLionel Sambuc {
49ebfedea0SLionel Sambuc     keyblock->keytype = 0;
50ebfedea0SLionel Sambuc     krb5_data_zero(&keyblock->keyvalue);
51ebfedea0SLionel Sambuc }
52ebfedea0SLionel Sambuc 
53ebfedea0SLionel Sambuc /**
54ebfedea0SLionel Sambuc  * Free a keyblock's content, also zero out the content of the keyblock.
55ebfedea0SLionel Sambuc  *
56ebfedea0SLionel Sambuc  * @param context a Kerberos 5 context
57ebfedea0SLionel Sambuc  * @param keyblock keyblock content to free, NULL is valid argument
58ebfedea0SLionel Sambuc  *
59ebfedea0SLionel Sambuc  * @ingroup krb5_crypto
60ebfedea0SLionel Sambuc  */
61ebfedea0SLionel Sambuc 
62ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_free_keyblock_contents(krb5_context context,krb5_keyblock * keyblock)63ebfedea0SLionel Sambuc krb5_free_keyblock_contents(krb5_context context,
64ebfedea0SLionel Sambuc 			    krb5_keyblock *keyblock)
65ebfedea0SLionel Sambuc {
66ebfedea0SLionel Sambuc     if(keyblock) {
67ebfedea0SLionel Sambuc 	if (keyblock->keyvalue.data != NULL)
68ebfedea0SLionel Sambuc 	    memset(keyblock->keyvalue.data, 0, keyblock->keyvalue.length);
69ebfedea0SLionel Sambuc 	krb5_data_free (&keyblock->keyvalue);
70ebfedea0SLionel Sambuc 	keyblock->keytype = ENCTYPE_NULL;
71ebfedea0SLionel Sambuc     }
72ebfedea0SLionel Sambuc }
73ebfedea0SLionel Sambuc 
74ebfedea0SLionel Sambuc /**
75ebfedea0SLionel Sambuc  * Free a keyblock, also zero out the content of the keyblock, uses
76ebfedea0SLionel Sambuc  * krb5_free_keyblock_contents() to free the content.
77ebfedea0SLionel Sambuc  *
78ebfedea0SLionel Sambuc  * @param context a Kerberos 5 context
79ebfedea0SLionel Sambuc  * @param keyblock keyblock to free, NULL is valid argument
80ebfedea0SLionel Sambuc  *
81ebfedea0SLionel Sambuc  * @ingroup krb5_crypto
82ebfedea0SLionel Sambuc  */
83ebfedea0SLionel Sambuc 
84ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_free_keyblock(krb5_context context,krb5_keyblock * keyblock)85ebfedea0SLionel Sambuc krb5_free_keyblock(krb5_context context,
86ebfedea0SLionel Sambuc 		   krb5_keyblock *keyblock)
87ebfedea0SLionel Sambuc {
88ebfedea0SLionel Sambuc     if(keyblock){
89ebfedea0SLionel Sambuc 	krb5_free_keyblock_contents(context, keyblock);
90ebfedea0SLionel Sambuc 	free(keyblock);
91ebfedea0SLionel Sambuc     }
92ebfedea0SLionel Sambuc }
93ebfedea0SLionel Sambuc 
94ebfedea0SLionel Sambuc /**
95ebfedea0SLionel Sambuc  * Copy a keyblock, free the output keyblock with
96ebfedea0SLionel Sambuc  * krb5_free_keyblock_contents().
97ebfedea0SLionel Sambuc  *
98ebfedea0SLionel Sambuc  * @param context a Kerberos 5 context
99ebfedea0SLionel Sambuc  * @param inblock the key to copy
100ebfedea0SLionel Sambuc  * @param to the output key.
101ebfedea0SLionel Sambuc  *
102ebfedea0SLionel Sambuc  * @return 0 on success or a Kerberos 5 error code
103ebfedea0SLionel Sambuc  *
104ebfedea0SLionel Sambuc  * @ingroup krb5_crypto
105ebfedea0SLionel Sambuc  */
106ebfedea0SLionel Sambuc 
107ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_copy_keyblock_contents(krb5_context context,const krb5_keyblock * inblock,krb5_keyblock * to)108ebfedea0SLionel Sambuc krb5_copy_keyblock_contents (krb5_context context,
109ebfedea0SLionel Sambuc 			     const krb5_keyblock *inblock,
110ebfedea0SLionel Sambuc 			     krb5_keyblock *to)
111ebfedea0SLionel Sambuc {
112ebfedea0SLionel Sambuc     return copy_EncryptionKey(inblock, to);
113ebfedea0SLionel Sambuc }
114ebfedea0SLionel Sambuc 
115ebfedea0SLionel Sambuc /**
116ebfedea0SLionel Sambuc  * Copy a keyblock, free the output keyblock with
117ebfedea0SLionel Sambuc  * krb5_free_keyblock().
118ebfedea0SLionel Sambuc  *
119ebfedea0SLionel Sambuc  * @param context a Kerberos 5 context
120ebfedea0SLionel Sambuc  * @param inblock the key to copy
121ebfedea0SLionel Sambuc  * @param to the output key.
122ebfedea0SLionel Sambuc  *
123ebfedea0SLionel Sambuc  * @return 0 on success or a Kerberos 5 error code
124ebfedea0SLionel Sambuc  *
125ebfedea0SLionel Sambuc  * @ingroup krb5_crypto
126ebfedea0SLionel Sambuc  */
127ebfedea0SLionel Sambuc 
128ebfedea0SLionel Sambuc 
129ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_copy_keyblock(krb5_context context,const krb5_keyblock * inblock,krb5_keyblock ** to)130ebfedea0SLionel Sambuc krb5_copy_keyblock (krb5_context context,
131ebfedea0SLionel Sambuc 		    const krb5_keyblock *inblock,
132ebfedea0SLionel Sambuc 		    krb5_keyblock **to)
133ebfedea0SLionel Sambuc {
134ebfedea0SLionel Sambuc     krb5_error_code ret;
135ebfedea0SLionel Sambuc     krb5_keyblock *k;
136ebfedea0SLionel Sambuc 
137ebfedea0SLionel Sambuc     *to = NULL;
138ebfedea0SLionel Sambuc 
139ebfedea0SLionel Sambuc     k = calloc (1, sizeof(*k));
140ebfedea0SLionel Sambuc     if (k == NULL) {
141ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
142ebfedea0SLionel Sambuc 	return ENOMEM;
143ebfedea0SLionel Sambuc     }
144ebfedea0SLionel Sambuc 
145ebfedea0SLionel Sambuc     ret = krb5_copy_keyblock_contents (context, inblock, k);
146ebfedea0SLionel Sambuc     if (ret) {
147ebfedea0SLionel Sambuc       free(k);
148ebfedea0SLionel Sambuc       return ret;
149ebfedea0SLionel Sambuc     }
150ebfedea0SLionel Sambuc     *to = k;
151ebfedea0SLionel Sambuc     return 0;
152ebfedea0SLionel Sambuc }
153ebfedea0SLionel Sambuc 
154ebfedea0SLionel Sambuc /**
155ebfedea0SLionel Sambuc  * Get encryption type of a keyblock.
156ebfedea0SLionel Sambuc  *
157ebfedea0SLionel Sambuc  * @ingroup krb5_crypto
158ebfedea0SLionel Sambuc  */
159ebfedea0SLionel Sambuc 
160ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_enctype KRB5_LIB_CALL
krb5_keyblock_get_enctype(const krb5_keyblock * block)161ebfedea0SLionel Sambuc krb5_keyblock_get_enctype(const krb5_keyblock *block)
162ebfedea0SLionel Sambuc {
163ebfedea0SLionel Sambuc     return block->keytype;
164ebfedea0SLionel Sambuc }
165ebfedea0SLionel Sambuc 
166ebfedea0SLionel Sambuc /**
167ebfedea0SLionel Sambuc  * Fill in `key' with key data of type `enctype' from `data' of length
168ebfedea0SLionel Sambuc  * `size'. Key should be freed using krb5_free_keyblock_contents().
169ebfedea0SLionel Sambuc  *
170ebfedea0SLionel Sambuc  * @return 0 on success or a Kerberos 5 error code
171ebfedea0SLionel Sambuc  *
172ebfedea0SLionel Sambuc  * @ingroup krb5_crypto
173ebfedea0SLionel Sambuc  */
174ebfedea0SLionel Sambuc 
175ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_keyblock_init(krb5_context context,krb5_enctype type,const void * data,size_t size,krb5_keyblock * key)176ebfedea0SLionel Sambuc krb5_keyblock_init(krb5_context context,
177ebfedea0SLionel Sambuc 		   krb5_enctype type,
178ebfedea0SLionel Sambuc 		   const void *data,
179ebfedea0SLionel Sambuc 		   size_t size,
180ebfedea0SLionel Sambuc 		   krb5_keyblock *key)
181ebfedea0SLionel Sambuc {
182ebfedea0SLionel Sambuc     krb5_error_code ret;
183ebfedea0SLionel Sambuc     size_t len;
184ebfedea0SLionel Sambuc 
185ebfedea0SLionel Sambuc     memset(key, 0, sizeof(*key));
186ebfedea0SLionel Sambuc 
187ebfedea0SLionel Sambuc     ret = krb5_enctype_keysize(context, type, &len);
188ebfedea0SLionel Sambuc     if (ret)
189ebfedea0SLionel Sambuc 	return ret;
190ebfedea0SLionel Sambuc 
191ebfedea0SLionel Sambuc     if (len != size) {
192ebfedea0SLionel Sambuc 	krb5_set_error_message(context, KRB5_PROG_ETYPE_NOSUPP,
193ebfedea0SLionel Sambuc 			       "Encryption key %d is %lu bytes "
194ebfedea0SLionel Sambuc 			       "long, %lu was passed in",
195ebfedea0SLionel Sambuc 			       type, (unsigned long)len, (unsigned long)size);
196ebfedea0SLionel Sambuc 	return KRB5_PROG_ETYPE_NOSUPP;
197ebfedea0SLionel Sambuc     }
198ebfedea0SLionel Sambuc     ret = krb5_data_copy(&key->keyvalue, data, len);
199ebfedea0SLionel Sambuc     if(ret) {
200ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
201ebfedea0SLionel Sambuc 	return ret;
202ebfedea0SLionel Sambuc     }
203ebfedea0SLionel Sambuc     key->keytype = type;
204ebfedea0SLionel Sambuc 
205ebfedea0SLionel Sambuc     return 0;
206ebfedea0SLionel Sambuc }
207