etc/services - sync with NetBSD-8
[minix.git] / crypto / external / bsd / heimdal / dist / lib / krb5 / keyblock.c
blob616ae879418aa02d053fe85bae40af02a5577f4e
1 /* $NetBSD: keyblock.c,v 1.1.1.2 2014/04/24 12:45:50 pettai Exp $ */
3 /*
4 * Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
36 #include "krb5_locl.h"
38 /**
39 * Zero out a keyblock
41 * @param keyblock keyblock to zero out
43 * @ingroup krb5_crypto
46 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
47 krb5_keyblock_zero(krb5_keyblock *keyblock)
49 keyblock->keytype = 0;
50 krb5_data_zero(&keyblock->keyvalue);
53 /**
54 * Free a keyblock's content, also zero out the content of the keyblock.
56 * @param context a Kerberos 5 context
57 * @param keyblock keyblock content to free, NULL is valid argument
59 * @ingroup krb5_crypto
62 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
63 krb5_free_keyblock_contents(krb5_context context,
64 krb5_keyblock *keyblock)
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;
74 /**
75 * Free a keyblock, also zero out the content of the keyblock, uses
76 * krb5_free_keyblock_contents() to free the content.
78 * @param context a Kerberos 5 context
79 * @param keyblock keyblock to free, NULL is valid argument
81 * @ingroup krb5_crypto
84 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
85 krb5_free_keyblock(krb5_context context,
86 krb5_keyblock *keyblock)
88 if(keyblock){
89 krb5_free_keyblock_contents(context, keyblock);
90 free(keyblock);
94 /**
95 * Copy a keyblock, free the output keyblock with
96 * krb5_free_keyblock_contents().
98 * @param context a Kerberos 5 context
99 * @param inblock the key to copy
100 * @param to the output key.
102 * @return 0 on success or a Kerberos 5 error code
104 * @ingroup krb5_crypto
107 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
108 krb5_copy_keyblock_contents (krb5_context context,
109 const krb5_keyblock *inblock,
110 krb5_keyblock *to)
112 return copy_EncryptionKey(inblock, to);
116 * Copy a keyblock, free the output keyblock with
117 * krb5_free_keyblock().
119 * @param context a Kerberos 5 context
120 * @param inblock the key to copy
121 * @param to the output key.
123 * @return 0 on success or a Kerberos 5 error code
125 * @ingroup krb5_crypto
129 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
130 krb5_copy_keyblock (krb5_context context,
131 const krb5_keyblock *inblock,
132 krb5_keyblock **to)
134 krb5_error_code ret;
135 krb5_keyblock *k;
137 *to = NULL;
139 k = calloc (1, sizeof(*k));
140 if (k == NULL) {
141 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
142 return ENOMEM;
145 ret = krb5_copy_keyblock_contents (context, inblock, k);
146 if (ret) {
147 free(k);
148 return ret;
150 *to = k;
151 return 0;
155 * Get encryption type of a keyblock.
157 * @ingroup krb5_crypto
160 KRB5_LIB_FUNCTION krb5_enctype KRB5_LIB_CALL
161 krb5_keyblock_get_enctype(const krb5_keyblock *block)
163 return block->keytype;
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().
170 * @return 0 on success or a Kerberos 5 error code
172 * @ingroup krb5_crypto
175 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
176 krb5_keyblock_init(krb5_context context,
177 krb5_enctype type,
178 const void *data,
179 size_t size,
180 krb5_keyblock *key)
182 krb5_error_code ret;
183 size_t len;
185 memset(key, 0, sizeof(*key));
187 ret = krb5_enctype_keysize(context, type, &len);
188 if (ret)
189 return ret;
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;
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;
203 key->keytype = type;
205 return 0;