1/* $NetBSD: random_password.c,v 1.3 2019/12/15 22:50:46 christos Exp $ */ 2 3/* 4 * Copyright (c) 1998, 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 "kadmin_locl.h" 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 52static void generate_password(char **pw, int num_classes, ...); 53#endif 54 55void 56random_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 len = strlen(pass); 74 memset_s(pass, len, 0, len); 75 free(pass); 76#endif 77} 78 79/* some helper functions */ 80 81#ifndef OTP_STYLE 82/* return a random value in range 0-127 */ 83static int 84RND(unsigned char *key, int keylen, int *left) 85{ 86 if(*left == 0){ 87 krb5_generate_random_block(key, keylen); 88 *left = keylen; 89 } 90 (*left)--; 91 return ((unsigned char*)key)[*left]; 92} 93 94/* This a helper function that generates a random password with a 95 number of characters from a set of character classes. 96 97 If there are n classes, and the size of each class is Pi, and the 98 number of characters from each class is Ni, the number of possible 99 passwords are (given that the character classes are disjoint): 100 101 n n 102 ----- / ---- \ 103 | | Ni | \ | 104 | | Pi | \ Ni| ! 105 | | ---- * | / | 106 | | Ni! | /___ | 107 i=1 \ i=1 / 108 109 Since it uses the RND function above, neither the size of each 110 class, nor the total length of the generated password should be 111 larger than 127 (without fixing RND). 112 113 */ 114static void 115generate_password(char **pw, int num_classes, ...) 116{ 117 struct { 118 const char *str; 119 int len; 120 int freq; 121 } *classes; 122 va_list ap; 123 int len, i; 124 unsigned char rbuf[8]; /* random buffer */ 125 int rleft = 0; 126 127 *pw = NULL; 128 129 classes = malloc(num_classes * sizeof(*classes)); 130 if(classes == NULL) 131 return; 132 va_start(ap, num_classes); 133 len = 0; 134 for(i = 0; i < num_classes; i++){ 135 classes[i].str = va_arg(ap, const char*); 136 classes[i].len = strlen(classes[i].str); 137 classes[i].freq = va_arg(ap, int); 138 len += classes[i].freq; 139 } 140 va_end(ap); 141 *pw = malloc(len + 1); 142 if(*pw == NULL) { 143 free(classes); 144 return; 145 } 146 for(i = 0; i < len; i++) { 147 int j; 148 int x = RND(rbuf, sizeof(rbuf), &rleft) % (len - i); 149 int t = 0; 150 for(j = 0; j < num_classes; j++) { 151 if(x < t + classes[j].freq) { 152 (*pw)[i] = classes[j].str[RND(rbuf, sizeof(rbuf), &rleft) 153 % classes[j].len]; 154 classes[j].freq--; 155 break; 156 } 157 t += classes[j].freq; 158 } 159 } 160 (*pw)[len] = '\0'; 161 memset_s(rbuf, sizeof(rbuf), 0, sizeof(rbuf)); 162 free(classes); 163} 164#endif 165