1 /* $NetBSD: test_fx.c,v 1.2 2017/01/28 21:31:49 christos Exp $ */ 2 3 /* 4 * Copyright (c) 2009 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 KTH nor the names of its contributors may be 20 * used to endorse or promote products derived from this software without 21 * specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY 24 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE 27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 34 35 #include "krb5_locl.h" 36 #include <err.h> 37 #include <krb5/getarg.h> 38 39 struct { 40 char *p1; 41 char *pepper1; 42 krb5_enctype e1; 43 char *p2; 44 char *pepper2; 45 krb5_enctype e2; 46 krb5_enctype e3; 47 char *key; 48 size_t len; 49 } cf2[] = { 50 { 51 "key1", "a", ETYPE_AES128_CTS_HMAC_SHA1_96, 52 "key2", "b", ETYPE_AES128_CTS_HMAC_SHA1_96, 53 ETYPE_AES128_CTS_HMAC_SHA1_96, 54 "\x97\xdf\x97\xe4\xb7\x98\xb2\x9e\xb3\x1e\xd7\x28\x02\x87\xa9\x2a", 55 16 56 }, 57 { 58 "key1", "a", ETYPE_AES256_CTS_HMAC_SHA1_96, 59 "key2", "b", ETYPE_AES256_CTS_HMAC_SHA1_96, 60 ETYPE_AES256_CTS_HMAC_SHA1_96, 61 "\x4d\x6c\xa4\xe6\x29\x78\x5c\x1f\x01\xba\xf5\x5e\x2e\x54\x85\x66" 62 "\xb9\x61\x7a\xe3\xa9\x68\x68\xc3\x37\xcb\x93\xb5\xe7\x2b\x1c\x7b", 63 32 64 }, 65 { 66 "key1", "a", ETYPE_AES128_CTS_HMAC_SHA1_96, 67 "key2", "b", ETYPE_AES128_CTS_HMAC_SHA1_96, 68 ETYPE_AES256_CTS_HMAC_SHA1_96, 69 "\x97\xdf\x97\xe4\xb7\x98\xb2\x9e\xb3\x1e\xd7\x28\x2\x87\xa9\x2a" 70 "\x1\x96\xfa\xf2\x44\xf8\x11\x20\xc2\x1c\x51\x17\xb3\xe6\xeb\x98", 71 32 72 }, 73 { 74 "key1", "a", ETYPE_AES256_CTS_HMAC_SHA1_96, 75 "key2", "b", ETYPE_AES256_CTS_HMAC_SHA1_96, 76 ETYPE_AES128_CTS_HMAC_SHA1_96, 77 "\x4d\x6c\xa4\xe6\x29\x78\x5c\x1f\x01\xba\xf5\x5e\x2e\x54\x85\x66", 78 16 79 }, 80 { 81 "key1", "a", ETYPE_AES128_CTS_HMAC_SHA1_96, 82 "key2", "b", ETYPE_AES256_CTS_HMAC_SHA1_96, 83 ETYPE_AES256_CTS_HMAC_SHA1_96, 84 "\x88\xbd\xb2\xa9\xf\x3e\x52\x5a\xb0\x5f\x68\xc5\x43\x9a\x4d\x5e" 85 "\x9c\x2b\xfd\x2b\x02\x24\xde\x39\xb5\x82\xf4\xbb\x05\xfe\x2\x2e", 86 32 87 }, 88 { 89 "key1", "a", ETYPE_ARCFOUR_HMAC_MD5, 90 "key2", "b", ETYPE_ARCFOUR_HMAC_MD5, 91 ETYPE_ARCFOUR_HMAC_MD5, 92 "\x24\xd7\xf6\xb6\xba\xe4\xe5\xc0\x0d\x20\x82\xc5\xeb\xab\x36\x72", 93 16 94 }, 95 /* We don't yet have a PRF for 1DES in Heimdal */ 96 { 97 "key1", "a", ETYPE_DES_CBC_CRC, 98 "key2", "b", ETYPE_DES_CBC_CRC, 99 ETYPE_DES_CBC_CRC, 100 "\x43\xba\xe3\x73\x8c\x94\x67\xe6", 101 8 102 }, 103 { 104 "key1", "a", ETYPE_DES3_CBC_SHA1, 105 "key2", "b", ETYPE_DES3_CBC_SHA1, 106 ETYPE_DES3_CBC_SHA1, 107 "\xe5\x8f\x9e\xb6\x43\x86\x2c\x13\xad\x38\xe5\x29\x31\x34\x62\xa7" 108 "\xf7\x3e\x62\x83\x4f\xe5\x4a\x01", 109 24 110 }, 111 }; 112 113 114 static void 115 test_cf2(krb5_context context) 116 { 117 krb5_error_code ret; 118 krb5_data pw, p1, p2; 119 krb5_salt salt; 120 krb5_keyblock k1, k2, k3; 121 krb5_crypto c1, c2; 122 unsigned int i; 123 unsigned int errors = 0; 124 125 ret = krb5_allow_weak_crypto(context, 1); 126 if (ret) 127 krb5_err(context, 1, ret, "krb5_allow_weak_crypto"); 128 129 for (i = 0; i < sizeof(cf2)/sizeof(cf2[0]); i++) { 130 pw.data = cf2[i].p1; 131 pw.length = strlen(cf2[i].p1); 132 salt.salttype = (krb5_salttype)KRB5_PADATA_PW_SALT; 133 salt.saltvalue.data = cf2[i].p1; 134 salt.saltvalue.length = strlen(cf2[i].p1); 135 136 ret = krb5_string_to_key_data_salt(context, 137 cf2[i].e1, 138 pw, 139 salt, 140 &k1); 141 if (ret) 142 krb5_err(context, 1, ret, "krb5_string_to_key_data_salt"); 143 144 ret = krb5_crypto_init(context, &k1, 0, &c1); 145 if (ret) 146 krb5_err(context, 1, ret, "krb5_crypto_init"); 147 148 pw.data = cf2[i].p2; 149 pw.length = strlen(cf2[i].p2); 150 salt.saltvalue.data = cf2[i].p2; 151 salt.saltvalue.length = strlen(cf2[i].p2); 152 153 ret = krb5_string_to_key_data_salt(context, 154 cf2[i].e2, 155 pw, 156 salt, 157 &k2); 158 if (ret) 159 krb5_err(context, 1, ret, "krb5_string_to_key_data_salt"); 160 161 ret = krb5_crypto_init(context, &k2, 0, &c2); 162 if (ret) 163 krb5_err(context, 1, ret, "krb5_crypto_init"); 164 165 166 p1.data = cf2[i].pepper1; 167 p1.length = strlen(cf2[i].pepper1); 168 169 p2.data = cf2[i].pepper2; 170 p2.length = strlen(cf2[i].pepper2); 171 172 ret = krb5_crypto_fx_cf2(context, c1, c2, &p1, &p2, cf2[i].e3, &k3); 173 if (ret == KRB5_PROG_ETYPE_NOSUPP) { 174 krb5_warn(context, ret, "KRB-FX-CF2 not supported for enctype %d", 175 cf2[i].e1); 176 continue; 177 } else if (ret) { 178 krb5_err(context, 1, ret, "krb5_crypto_fx_cf2"); 179 } 180 181 if (k3.keytype != cf2[i].e3) { 182 errors++; 183 krb5_warnx(context, "length not right for enctype %d", cf2[i].e3); 184 continue; 185 } 186 if (k3.keyvalue.length != cf2[i].len || 187 memcmp(k3.keyvalue.data, cf2[i].key, cf2[i].len) != 0) { 188 errors++; 189 krb5_warnx(context, "key not same for enctypes %d %d %d", 190 cf2[i].e1, cf2[i].e2, cf2[i].e3); 191 continue; 192 } 193 194 krb5_crypto_destroy(context, c1); 195 krb5_crypto_destroy(context, c2); 196 197 krb5_free_keyblock_contents(context, &k1); 198 krb5_free_keyblock_contents(context, &k2); 199 krb5_free_keyblock_contents(context, &k3); 200 } 201 202 if (errors) 203 krb5_errx(context, 1, "%u KRB-FX-CF2 vectors failed", errors); 204 } 205 206 static int version_flag = 0; 207 static int help_flag = 0; 208 209 static struct getargs args[] = { 210 {"version", 0, arg_flag, &version_flag, 211 "print version", NULL }, 212 {"help", 0, arg_flag, &help_flag, 213 NULL, NULL } 214 }; 215 216 static void 217 usage (int ret) 218 { 219 arg_printusage (args, 220 sizeof(args)/sizeof(*args), 221 NULL, 222 ""); 223 exit (ret); 224 } 225 226 int 227 main(int argc, char **argv) 228 { 229 krb5_context context; 230 krb5_error_code ret; 231 int optidx = 0; 232 233 setprogname(argv[0]); 234 235 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx)) 236 usage(1); 237 238 if (help_flag) 239 usage (0); 240 241 if(version_flag){ 242 print_version(NULL); 243 exit(0); 244 } 245 246 ret = krb5_init_context(&context); 247 if (ret) 248 errx (1, "krb5_init_context failed: %d", ret); 249 250 test_cf2(context); 251 252 krb5_free_context(context); 253 254 return 0; 255 } 256