xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/krb5/fast.c (revision d3273b5b76f5afaafe308cead5511dbb8df8c5e9)
1*d3273b5bSchristos /*	$NetBSD: fast.c,v 1.2 2017/01/28 21:31:49 christos Exp $	*/
2b9d004c6Schristos 
3b9d004c6Schristos /*
4b9d004c6Schristos  * Copyright (c) 2011 Kungliga Tekniska Högskolan
5b9d004c6Schristos  * (Royal Institute of Technology, Stockholm, Sweden).
6b9d004c6Schristos  * All rights reserved.
7b9d004c6Schristos  *
8b9d004c6Schristos  * Redistribution and use in source and binary forms, with or without
9b9d004c6Schristos  * modification, are permitted provided that the following conditions
10b9d004c6Schristos  * are met:
11b9d004c6Schristos  *
12b9d004c6Schristos  * 1. Redistributions of source code must retain the above copyright
13b9d004c6Schristos  *    notice, this list of conditions and the following disclaimer.
14b9d004c6Schristos  *
15b9d004c6Schristos  * 2. Redistributions in binary form must reproduce the above copyright
16b9d004c6Schristos  *    notice, this list of conditions and the following disclaimer in the
17b9d004c6Schristos  *    documentation and/or other materials provided with the distribution.
18b9d004c6Schristos  *
19b9d004c6Schristos  * 3. Neither the name of the Institute nor the names of its contributors
20b9d004c6Schristos  *    may be used to endorse or promote products derived from this software
21b9d004c6Schristos  *    without specific prior written permission.
22b9d004c6Schristos  *
23b9d004c6Schristos  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24b9d004c6Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25b9d004c6Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26b9d004c6Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27b9d004c6Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28b9d004c6Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29b9d004c6Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30b9d004c6Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31b9d004c6Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32b9d004c6Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33b9d004c6Schristos  * SUCH DAMAGE.
34b9d004c6Schristos  */
35b9d004c6Schristos 
36b9d004c6Schristos #include "krb5_locl.h"
37b9d004c6Schristos 
38b9d004c6Schristos 
39b9d004c6Schristos KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
_krb5_fast_cf2(krb5_context context,krb5_keyblock * key1,const char * pepper1,krb5_keyblock * key2,const char * pepper2,krb5_keyblock * armorkey,krb5_crypto * armor_crypto)40b9d004c6Schristos _krb5_fast_cf2(krb5_context context,
41b9d004c6Schristos 	       krb5_keyblock *key1,
42b9d004c6Schristos 	       const char *pepper1,
43b9d004c6Schristos 	       krb5_keyblock *key2,
44b9d004c6Schristos 	       const char *pepper2,
45b9d004c6Schristos 	       krb5_keyblock *armorkey,
46b9d004c6Schristos 	       krb5_crypto *armor_crypto)
47b9d004c6Schristos {
48b9d004c6Schristos     krb5_crypto crypto1, crypto2;
49b9d004c6Schristos     krb5_data pa1, pa2;
50b9d004c6Schristos     krb5_error_code ret;
51b9d004c6Schristos 
52b9d004c6Schristos     ret = krb5_crypto_init(context, key1, 0, &crypto1);
53b9d004c6Schristos     if (ret)
54b9d004c6Schristos 	return ret;
55b9d004c6Schristos 
56b9d004c6Schristos     ret = krb5_crypto_init(context, key2, 0, &crypto2);
57b9d004c6Schristos     if (ret) {
58b9d004c6Schristos 	krb5_crypto_destroy(context, crypto1);
59b9d004c6Schristos 	return ret;
60b9d004c6Schristos     }
61b9d004c6Schristos 
62b9d004c6Schristos     pa1.data = rk_UNCONST(pepper1);
63b9d004c6Schristos     pa1.length = strlen(pepper1);
64b9d004c6Schristos     pa2.data = rk_UNCONST(pepper2);
65b9d004c6Schristos     pa2.length = strlen(pepper2);
66b9d004c6Schristos 
67b9d004c6Schristos     ret = krb5_crypto_fx_cf2(context, crypto1, crypto2, &pa1, &pa2,
68b9d004c6Schristos 			     key1->keytype, armorkey);
69b9d004c6Schristos     krb5_crypto_destroy(context, crypto1);
70b9d004c6Schristos     krb5_crypto_destroy(context, crypto2);
71b9d004c6Schristos     if (ret)
72b9d004c6Schristos 	return ret;
73b9d004c6Schristos 
74b9d004c6Schristos     if (armor_crypto) {
75b9d004c6Schristos 	ret = krb5_crypto_init(context, armorkey, 0, armor_crypto);
76b9d004c6Schristos 	if (ret)
77b9d004c6Schristos 	    krb5_free_keyblock_contents(context, armorkey);
78b9d004c6Schristos     }
79b9d004c6Schristos 
80b9d004c6Schristos     return ret;
81b9d004c6Schristos }
82b9d004c6Schristos 
83b9d004c6Schristos KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
_krb5_fast_armor_key(krb5_context context,krb5_keyblock * subkey,krb5_keyblock * sessionkey,krb5_keyblock * armorkey,krb5_crypto * armor_crypto)84b9d004c6Schristos _krb5_fast_armor_key(krb5_context context,
85b9d004c6Schristos 		     krb5_keyblock *subkey,
86b9d004c6Schristos 		     krb5_keyblock *sessionkey,
87b9d004c6Schristos 		     krb5_keyblock *armorkey,
88b9d004c6Schristos 		     krb5_crypto *armor_crypto)
89b9d004c6Schristos {
90b9d004c6Schristos     return _krb5_fast_cf2(context,
91b9d004c6Schristos 			  subkey,
92b9d004c6Schristos 			  "subkeyarmor",
93b9d004c6Schristos 			  sessionkey,
94b9d004c6Schristos 			  "ticketarmor",
95b9d004c6Schristos 			  armorkey,
96b9d004c6Schristos 			  armor_crypto);
97b9d004c6Schristos }
98