xref: /minix3/crypto/external/bsd/heimdal/dist/lib/krb5/keyblock.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1 /*	$NetBSD: keyblock.c,v 1.1.1.2 2014/04/24 12:45:50 pettai Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
5  * (Royal Institute of Technology, Stockholm, Sweden).
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the Institute nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include "krb5_locl.h"
37 
38 /**
39  * Zero out a keyblock
40  *
41  * @param keyblock keyblock to zero out
42  *
43  * @ingroup krb5_crypto
44  */
45 
46 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_keyblock_zero(krb5_keyblock * keyblock)47 krb5_keyblock_zero(krb5_keyblock *keyblock)
48 {
49     keyblock->keytype = 0;
50     krb5_data_zero(&keyblock->keyvalue);
51 }
52 
53 /**
54  * Free a keyblock's content, also zero out the content of the keyblock.
55  *
56  * @param context a Kerberos 5 context
57  * @param keyblock keyblock content to free, NULL is valid argument
58  *
59  * @ingroup krb5_crypto
60  */
61 
62 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_free_keyblock_contents(krb5_context context,krb5_keyblock * keyblock)63 krb5_free_keyblock_contents(krb5_context context,
64 			    krb5_keyblock *keyblock)
65 {
66     if(keyblock) {
67 	if (keyblock->keyvalue.data != NULL)
68 	    memset(keyblock->keyvalue.data, 0, keyblock->keyvalue.length);
69 	krb5_data_free (&keyblock->keyvalue);
70 	keyblock->keytype = ENCTYPE_NULL;
71     }
72 }
73 
74 /**
75  * Free a keyblock, also zero out the content of the keyblock, uses
76  * krb5_free_keyblock_contents() to free the content.
77  *
78  * @param context a Kerberos 5 context
79  * @param keyblock keyblock to free, NULL is valid argument
80  *
81  * @ingroup krb5_crypto
82  */
83 
84 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_free_keyblock(krb5_context context,krb5_keyblock * keyblock)85 krb5_free_keyblock(krb5_context context,
86 		   krb5_keyblock *keyblock)
87 {
88     if(keyblock){
89 	krb5_free_keyblock_contents(context, keyblock);
90 	free(keyblock);
91     }
92 }
93 
94 /**
95  * Copy a keyblock, free the output keyblock with
96  * krb5_free_keyblock_contents().
97  *
98  * @param context a Kerberos 5 context
99  * @param inblock the key to copy
100  * @param to the output key.
101  *
102  * @return 0 on success or a Kerberos 5 error code
103  *
104  * @ingroup krb5_crypto
105  */
106 
107 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_copy_keyblock_contents(krb5_context context,const krb5_keyblock * inblock,krb5_keyblock * to)108 krb5_copy_keyblock_contents (krb5_context context,
109 			     const krb5_keyblock *inblock,
110 			     krb5_keyblock *to)
111 {
112     return copy_EncryptionKey(inblock, to);
113 }
114 
115 /**
116  * Copy a keyblock, free the output keyblock with
117  * krb5_free_keyblock().
118  *
119  * @param context a Kerberos 5 context
120  * @param inblock the key to copy
121  * @param to the output key.
122  *
123  * @return 0 on success or a Kerberos 5 error code
124  *
125  * @ingroup krb5_crypto
126  */
127 
128 
129 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_copy_keyblock(krb5_context context,const krb5_keyblock * inblock,krb5_keyblock ** to)130 krb5_copy_keyblock (krb5_context context,
131 		    const krb5_keyblock *inblock,
132 		    krb5_keyblock **to)
133 {
134     krb5_error_code ret;
135     krb5_keyblock *k;
136 
137     *to = NULL;
138 
139     k = calloc (1, sizeof(*k));
140     if (k == NULL) {
141 	krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
142 	return ENOMEM;
143     }
144 
145     ret = krb5_copy_keyblock_contents (context, inblock, k);
146     if (ret) {
147       free(k);
148       return ret;
149     }
150     *to = k;
151     return 0;
152 }
153 
154 /**
155  * Get encryption type of a keyblock.
156  *
157  * @ingroup krb5_crypto
158  */
159 
160 KRB5_LIB_FUNCTION krb5_enctype KRB5_LIB_CALL
krb5_keyblock_get_enctype(const krb5_keyblock * block)161 krb5_keyblock_get_enctype(const krb5_keyblock *block)
162 {
163     return block->keytype;
164 }
165 
166 /**
167  * Fill in `key' with key data of type `enctype' from `data' of length
168  * `size'. Key should be freed using krb5_free_keyblock_contents().
169  *
170  * @return 0 on success or a Kerberos 5 error code
171  *
172  * @ingroup krb5_crypto
173  */
174 
175 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)176 krb5_keyblock_init(krb5_context context,
177 		   krb5_enctype type,
178 		   const void *data,
179 		   size_t size,
180 		   krb5_keyblock *key)
181 {
182     krb5_error_code ret;
183     size_t len;
184 
185     memset(key, 0, sizeof(*key));
186 
187     ret = krb5_enctype_keysize(context, type, &len);
188     if (ret)
189 	return ret;
190 
191     if (len != size) {
192 	krb5_set_error_message(context, KRB5_PROG_ETYPE_NOSUPP,
193 			       "Encryption key %d is %lu bytes "
194 			       "long, %lu was passed in",
195 			       type, (unsigned long)len, (unsigned long)size);
196 	return KRB5_PROG_ETYPE_NOSUPP;
197     }
198     ret = krb5_data_copy(&key->keyvalue, data, len);
199     if(ret) {
200 	krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
201 	return ret;
202     }
203     key->keytype = type;
204 
205     return 0;
206 }
207