xref: /freebsd/crypto/heimdal/kadmin/random_password.c (revision 2f6a179eb910129fb812c1ad1bdc300da1203dc0)
1 /*
2  * Copyright (c) 1998, 1999 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include "kadmin_locl.h"
35 
36 RCSID("$Id: random_password.c 21745 2007-07-31 16:11:25Z lha $");
37 
38 /* This file defines some a function that generates a random password,
39    that can be used when creating a large amount of principals (such
40    as for a batch of students). Since this is a political matter, you
41    should think about how secure generated passwords has to be.
42 
43    Both methods defined here will give you at least 55 bits of
44    entropy.
45    */
46 
47 /* If you want OTP-style passwords, define OTP_STYLE */
48 
49 #ifdef OTP_STYLE
50 #include <otp.h>
51 #else
52 static void generate_password(char **pw, int num_classes, ...);
53 #endif
54 
55 void
56 random_password(char *pw, size_t len)
57 {
58 #ifdef OTP_STYLE
59     {
60 	OtpKey newkey;
61 
62 	krb5_generate_random_block(&newkey, sizeof(newkey));
63 	otp_print_stddict (newkey, pw, len);
64 	strlwr(pw);
65     }
66 #else
67     char *pass;
68     generate_password(&pass, 3,
69 		      "abcdefghijklmnopqrstuvwxyz", 7,
70 		      "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 2,
71 		      "@$%&*()-+=:,/<>1234567890", 1);
72     strlcpy(pw, pass, len);
73     memset(pass, 0, strlen(pass));
74     free(pass);
75 #endif
76 }
77 
78 /* some helper functions */
79 
80 #ifndef OTP_STYLE
81 /* return a random value in range 0-127 */
82 static int
83 RND(unsigned char *key, int keylen, int *left)
84 {
85     if(*left == 0){
86 	krb5_generate_random_block(key, keylen);
87 	*left = keylen;
88     }
89     (*left)--;
90     return ((unsigned char*)key)[*left];
91 }
92 
93 /* This a helper function that generates a random password with a
94    number of characters from a set of character classes.
95 
96    If there are n classes, and the size of each class is Pi, and the
97    number of characters from each class is Ni, the number of possible
98    passwords are (given that the character classes are disjoint):
99 
100      n             n
101    -----        /  ----  \
102    |   |  Ni    |  \     |
103    |   | Pi     |   \  Ni| !
104    |   | ---- * |   /    |
105    |   | Ni!    |  /___  |
106     i=1          \  i=1  /
107 
108     Since it uses the RND function above, neither the size of each
109     class, nor the total length of the generated password should be
110     larger than 127 (without fixing RND).
111 
112    */
113 static void
114 generate_password(char **pw, int num_classes, ...)
115 {
116     struct {
117 	const char *str;
118 	int len;
119 	int freq;
120     } *classes;
121     va_list ap;
122     int len, i;
123     unsigned char rbuf[8]; /* random buffer */
124     int rleft = 0;
125 
126     *pw = NULL;
127 
128     classes = malloc(num_classes * sizeof(*classes));
129     if(classes == NULL)
130 	return;
131     va_start(ap, num_classes);
132     len = 0;
133     for(i = 0; i < num_classes; i++){
134 	classes[i].str = va_arg(ap, const char*);
135 	classes[i].len = strlen(classes[i].str);
136 	classes[i].freq = va_arg(ap, int);
137 	len += classes[i].freq;
138     }
139     va_end(ap);
140     *pw = malloc(len + 1);
141     if(*pw == NULL) {
142 	free(classes);
143 	return;
144     }
145     for(i = 0; i < len; i++) {
146 	int j;
147 	int x = RND(rbuf, sizeof(rbuf), &rleft) % (len - i);
148 	int t = 0;
149 	for(j = 0; j < num_classes; j++) {
150 	    if(x < t + classes[j].freq) {
151 		(*pw)[i] = classes[j].str[RND(rbuf, sizeof(rbuf), &rleft)
152 					 % classes[j].len];
153 		classes[j].freq--;
154 		break;
155 	    }
156 	    t += classes[j].freq;
157 	}
158     }
159     (*pw)[len] = '\0';
160     memset(rbuf, 0, sizeof(rbuf));
161     free(classes);
162 }
163 #endif
164