xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/hcrypto/rnd_keys.c (revision d3273b5b76f5afaafe308cead5511dbb8df8c5e9)
1 /*	$NetBSD: rnd_keys.c,v 1.2 2017/01/28 21:31:47 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1995, 1996, 1997, 1999 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 <config.h>
37 #include <krb5/roken.h>
38 
39 #define HC_DEPRECATED
40 
41 #ifdef KRB5
42 #include <krb5/krb5-types.h>
43 #endif
44 
45 #include <des.h>
46 #include <rand.h>
47 
48 #undef __attribute__
49 #define __attribute__(X)
50 
51 void HC_DEPRECATED
DES_rand_data(void * outdata,int size)52 DES_rand_data(void *outdata, int size)
53 {
54     RAND_bytes(outdata, size);
55 }
56 
57 void HC_DEPRECATED
DES_generate_random_block(DES_cblock * block)58 DES_generate_random_block(DES_cblock *block)
59 {
60     RAND_bytes(block, sizeof(*block));
61 }
62 
63 #define DES_rand_data_key hc_DES_rand_data_key
64 
65 void HC_DEPRECATED
66 DES_rand_data_key(DES_cblock *key);
67 
68 /*
69  * Generate a random DES key.
70  */
71 
72 void HC_DEPRECATED
DES_rand_data_key(DES_cblock * key)73 DES_rand_data_key(DES_cblock *key)
74 {
75     DES_new_random_key(key);
76 }
77 
78 void HC_DEPRECATED
DES_set_sequence_number(void * ll)79 DES_set_sequence_number(void *ll)
80 {
81 }
82 
83 void HC_DEPRECATED
DES_set_random_generator_seed(DES_cblock * seed)84 DES_set_random_generator_seed(DES_cblock *seed)
85 {
86     RAND_seed(seed, sizeof(*seed));
87 }
88 
89 /**
90  * Generate a random des key using a random block, fixup parity and
91  * skip weak keys.
92  *
93  * @param key is set to a random key.
94  *
95  * @return 0 on success, non zero on random number generator failure.
96  *
97  * @ingroup hcrypto_des
98  */
99 
100 int HC_DEPRECATED
DES_new_random_key(DES_cblock * key)101 DES_new_random_key(DES_cblock *key)
102 {
103     do {
104 	if (RAND_bytes(key, sizeof(*key)) != 1)
105 	    return 1;
106 	DES_set_odd_parity(key);
107     } while(DES_is_weak_key(key));
108 
109     return(0);
110 }
111 
112 /**
113  * Seed the random number generator. Deprecated, use @ref page_rand
114  *
115  * @param seed a seed to seed that random number generate with.
116  *
117  * @ingroup hcrypto_des
118  */
119 
120 void HC_DEPRECATED
DES_init_random_number_generator(DES_cblock * seed)121 DES_init_random_number_generator(DES_cblock *seed)
122 {
123     RAND_seed(seed, sizeof(*seed));
124 }
125 
126 /**
127  * Generate a random key, deprecated since it doesn't return an error
128  * code, use DES_new_random_key().
129  *
130  * @param key is set to a random key.
131  *
132  * @ingroup hcrypto_des
133  */
134 
135 void HC_DEPRECATED
DES_random_key(DES_cblock * key)136 DES_random_key(DES_cblock *key)
137 {
138     if (DES_new_random_key(key))
139 	abort();
140 }
141