Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / crypto / kerberosIV / kadmin / random_password.c
1 /*
2  * Copyright (c) 1998 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 /* $FreeBSD: src/crypto/kerberosIV/kadmin/random_password.c,v 1.1.1.2.2.1 2003/02/14 22:37:37 nectar Exp $ */
34 /* $DragonFly: src/crypto/kerberosIV/kadmin/Attic/random_password.c,v 1.2 2003/06/17 04:24:36 dillon Exp $ */
35
36 #include "kadm_locl.h"
37
38 RCSID("$Id: random_password.c,v 1.4 1999/12/02 16:58:36 joda Exp $");
39
40 /* This file defines some a function that generates a random password,
41    that can be used when creating a large amount of principals (such
42    as for a batch of students). Since this is a political matter, you
43    should think about how secure generated passwords has to be.
44    
45    Both methods defined here will give you at least 55 bits of
46    entropy.
47    */
48
49 /* If you want OTP-style passwords, define OTP_STYLE */
50
51 #ifdef OTP_STYLE
52 #include <otp.h>
53 #else
54 static void generate_password(char **pw, int num_classes, ...);
55 #endif
56
57 void
58 random_password(char *pw, size_t len, u_int32_t *low, u_int32_t *high)
59 {
60     des_cblock newkey;
61 #ifdef OTP_STYLE
62     des_random_key(&newkey);
63     otp_print_stddict (newkey, pw, len);
64     strlwr(pw);
65 #else
66     char *pass;
67     generate_password(&pass, 3, 
68                       "abcdefghijklmnopqrstuvwxyz", 7, 
69                       "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 2, 
70                       "@$%&*()-+=:,/<>1234567890", 1);
71     strlcpy(pw, pass, len);
72     memset(pass, 0, strlen(pass));
73     free(pass);
74 #endif
75     des_string_to_key(pw, &newkey);
76     memcpy(low, newkey, 4);
77     memcpy(high, ((char *)newkey) + 4, 4);
78     memset(newkey, 0, sizeof(newkey));
79
80     *low = htonl(*low);
81     *high = htonl(*high);
82 }
83
84 /* some helper functions */
85
86 #ifndef OTP_STYLE
87 /* return a random value in range 0-127 */
88 static int
89 RND(des_cblock *key, int *left)
90 {
91     if(*left == 0){
92         des_random_key(*key);
93         *left = 8;
94     }
95     (*left)--;
96     return ((unsigned char*)key)[*left];
97 }
98
99 /* This a helper function that generates a random password with a
100    number of characters from a set of character classes.
101
102    If there are n classes, and the size of each class is Pi, and the
103    number of characters from each class is Ni, the number of possible
104    passwords are (given that the character classes are disjoint):
105
106      n             n
107    -----        /  ----  \
108    |   |  Ni    |  \     |
109    |   | Pi     |   \  Ni| !
110    |   | ---- * |   /    |
111    |   | Ni!    |  /___  |
112     i=1          \  i=1  /
113    
114     Since it uses the RND function above, neither the size of each
115     class, nor the total length of the generated password should be
116     larger than 127 (without fixing RND).
117    
118    */
119 static void
120 generate_password(char **pw, int num_classes, ...)
121 {
122     struct {
123         const char *str;
124         int len;
125         int freq;
126     } *classes;
127     va_list ap;
128     int len, i;
129     des_cblock rbuf; /* random buffer */
130     int rleft = 0;
131
132     classes = malloc(num_classes * sizeof(*classes));
133     va_start(ap, num_classes);
134     len = 0;
135     for(i = 0; i < num_classes; i++){
136         classes[i].str = va_arg(ap, const char*);
137         classes[i].len = strlen(classes[i].str);
138         classes[i].freq = va_arg(ap, int);
139         len += classes[i].freq;
140     }
141     va_end(ap);
142     *pw = malloc(len + 1);
143     if(*pw == NULL)
144         return;
145     for(i = 0; i < len; i++) {
146         int j;
147         int x = RND(&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, &rleft) % classes[j].len];
152                 classes[j].freq--;
153                 break;
154             }
155             t += classes[j].freq;
156         }
157     }
158     (*pw)[len] = '\0';
159     memset(rbuf, 0, sizeof(rbuf));
160     free(classes);
161 }
162 #endif