speed for rsa
[heimdal.git] / kdc / string2key.c
blob316f2278ba5e4bb5593cf32545cf8cae2f39de3a
1 /*
2 * Copyright (c) 1997-2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 #include "headers.h"
35 #include <getarg.h>
37 int version5;
38 int version4;
39 int afs;
40 char *principal;
41 char *cell;
42 char *password;
43 const char *keytype_str = "des3-cbc-sha1";
44 int version;
45 int help;
47 struct getargs args[] = {
48 { "version5", '5', arg_flag, &version5, "Output Kerberos v5 string-to-key" },
49 { "version4", '4', arg_flag, &version4, "Output Kerberos v4 string-to-key" },
50 { "afs", 'a', arg_flag, &afs, "Output AFS string-to-key" },
51 { "cell", 'c', arg_string, &cell, "AFS cell to use", "cell" },
52 { "password", 'w', arg_string, &password, "Password to use", "password" },
53 { "principal",'p', arg_string, &principal, "Kerberos v5 principal to use", "principal" },
54 { "keytype", 'k', arg_string, &keytype_str, "Keytype" },
55 { "version", 0, arg_flag, &version, "print version" },
56 { "help", 0, arg_flag, &help, NULL }
59 int num_args = sizeof(args) / sizeof(args[0]);
61 static void
62 usage(int status)
64 arg_printusage (args, num_args, NULL, "password");
65 exit(status);
68 static void
69 tokey(krb5_context context,
70 krb5_enctype enctype,
71 const char *pw,
72 krb5_salt salt,
73 const char *label)
75 krb5_error_code ret;
76 int i;
77 krb5_keyblock key;
78 char *e;
80 ret = krb5_string_to_key_salt(context, enctype, pw, salt, &key);
81 if (ret)
82 krb5_err(context, 1, ret, "krb5_string_to_key_salt");
83 ret = krb5_enctype_to_string(context, enctype, &e);
84 if (ret)
85 krb5_err(context, 1, ret, "krb5_enctype_to_string");
86 printf(label, e);
87 printf(": ");
88 for(i = 0; i < key.keyvalue.length; i++)
89 printf("%02x", ((unsigned char*)key.keyvalue.data)[i]);
90 printf("\n");
91 krb5_free_keyblock_contents(context, &key);
92 free(e);
95 int
96 main(int argc, char **argv)
98 krb5_context context;
99 krb5_principal princ;
100 krb5_salt salt;
101 int optidx;
102 char buf[1024];
103 krb5_enctype etype;
104 krb5_error_code ret;
106 optidx = krb5_program_setup(&context, argc, argv, args, num_args, NULL);
108 if(help)
109 usage(0);
111 if(version){
112 print_version (NULL);
113 return 0;
116 argc -= optidx;
117 argv += optidx;
119 if (argc > 1)
120 usage(1);
122 if(!version5 && !version4 && !afs)
123 version5 = 1;
125 ret = krb5_string_to_enctype(context, keytype_str, &etype);
126 if(ret)
127 krb5_err(context, 1, ret, "krb5_string_to_enctype");
129 if((etype != ETYPE_DES_CBC_CRC &&
130 etype != ETYPE_DES_CBC_MD4 &&
131 etype != ETYPE_DES_CBC_MD5) &&
132 (afs || version4)) {
133 if(!version5) {
134 etype = ETYPE_DES_CBC_CRC;
135 } else {
136 krb5_errx(context, 1,
137 "DES is the only valid keytype for AFS and Kerberos 4");
141 if(version5 && principal == NULL){
142 printf("Kerberos v5 principal: ");
143 if(fgets(buf, sizeof(buf), stdin) == NULL)
144 return 1;
145 buf[strcspn(buf, "\r\n")] = '\0';
146 principal = estrdup(buf);
148 if(afs && cell == NULL){
149 printf("AFS cell: ");
150 if(fgets(buf, sizeof(buf), stdin) == NULL)
151 return 1;
152 buf[strcspn(buf, "\r\n")] = '\0';
153 cell = estrdup(buf);
155 if(argv[0])
156 password = argv[0];
157 if(password == NULL){
158 if(UI_UTIL_read_pw_string(buf, sizeof(buf), "Password: ", 0))
159 return 1;
160 password = buf;
163 if(version5){
164 krb5_parse_name(context, principal, &princ);
165 krb5_get_pw_salt(context, princ, &salt);
166 tokey(context, etype, password, salt, "Kerberos 5 (%s)");
167 krb5_free_salt(context, salt);
169 if(version4){
170 salt.salttype = KRB5_PW_SALT;
171 salt.saltvalue.length = 0;
172 salt.saltvalue.data = NULL;
173 tokey(context, ETYPE_DES_CBC_MD5, password, salt, "Kerberos 4");
175 if(afs){
176 salt.salttype = KRB5_AFS3_SALT;
177 salt.saltvalue.length = strlen(cell);
178 salt.saltvalue.data = cell;
179 tokey(context, ETYPE_DES_CBC_MD5, password, salt, "AFS");
181 return 0;