1 /* $NetBSD: aes-test.c,v 1.1.1.2 2014/04/24 12:45:49 pettai Exp $ */
4 * Copyright (c) 2003 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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.
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.
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. */
35 #include "krb5_locl.h"
41 #include <openssl/evp.h>
44 static int verbose
= 0;
47 hex_dump_data(const void *data
, size_t length
)
51 hex_encode(data
, length
, &p
);
67 "password", "ATHENA.MIT.EDUraeburn", -1,
69 ETYPE_AES128_CTS_HMAC_SHA1_96
, 16,
70 "\xcd\xed\xb5\x28\x1b\xb2\xf8\x01\x56\x5a\x11\x22\xb2\x56\x35\x15",
71 "\x42\x26\x3c\x6e\x89\xf4\xfc\x28\xb8\xdf\x68\xee\x09\x79\x9f\x15"
74 "password", "ATHENA.MIT.EDUraeburn", -1,
76 ETYPE_AES256_CTS_HMAC_SHA1_96
, 32,
77 "\xcd\xed\xb5\x28\x1b\xb2\xf8\x01\x56\x5a\x11\x22\xb2\x56\x35\x15"
78 "\x0a\xd1\xf7\xa0\x4b\xb9\xf3\xa3\x33\xec\xc0\xe2\xe1\xf7\x08\x37",
79 "\xfe\x69\x7b\x52\xbc\x0d\x3c\xe1\x44\x32\xba\x03\x6a\x92\xe6\x5b"
80 "\xbb\x52\x28\x09\x90\xa2\xfa\x27\x88\x39\x98\xd7\x2a\xf3\x01\x61"
83 "password", "ATHENA.MIT.EDUraeburn", -1,
85 ETYPE_AES128_CTS_HMAC_SHA1_96
, 16,
86 "\x01\xdb\xee\x7f\x4a\x9e\x24\x3e\x98\x8b\x62\xc7\x3c\xda\x93\x5d",
87 "\xc6\x51\xbf\x29\xe2\x30\x0a\xc2\x7f\xa4\x69\xd6\x93\xbd\xda\x13"
90 "password", "ATHENA.MIT.EDUraeburn", -1,
92 ETYPE_AES256_CTS_HMAC_SHA1_96
, 32,
93 "\x01\xdb\xee\x7f\x4a\x9e\x24\x3e\x98\x8b\x62\xc7\x3c\xda\x93\x5d"
94 "\xa0\x53\x78\xb9\x32\x44\xec\x8f\x48\xa9\x9e\x61\xad\x79\x9d\x86",
95 "\xa2\xe1\x6d\x16\xb3\x60\x69\xc1\x35\xd5\xe9\xd2\xe2\x5f\x89\x61"
96 "\x02\x68\x56\x18\xb9\x59\x14\xb4\x67\xc6\x76\x22\x22\x58\x24\xff"
99 "password", "ATHENA.MIT.EDUraeburn", -1,
101 ETYPE_AES128_CTS_HMAC_SHA1_96
, 16,
102 "\x5c\x08\xeb\x61\xfd\xf7\x1e\x4e\x4e\xc3\xcf\x6b\xa1\xf5\x51\x2b",
103 "\x4c\x01\xcd\x46\xd6\x32\xd0\x1e\x6d\xbe\x23\x0a\x01\xed\x64\x2a"
106 "password", "ATHENA.MIT.EDUraeburn", -1,
108 ETYPE_AES256_CTS_HMAC_SHA1_96
, 32,
109 "\x5c\x08\xeb\x61\xfd\xf7\x1e\x4e\x4e\xc3\xcf\x6b\xa1\xf5\x51\x2b"
110 "\xa7\xe5\x2d\xdb\xc5\xe5\x14\x2f\x70\x8a\x31\xe2\xe6\x2b\x1e\x13",
111 "\x55\xa6\xac\x74\x0a\xd1\x7b\x48\x46\x94\x10\x51\xe1\xe8\xb0\xa7"
112 "\x54\x8d\x93\xb0\xab\x30\xa8\xbc\x3f\xf1\x62\x80\x38\x2b\x8c\x2a"
115 "password", "\x12\x34\x56\x78\x78\x56\x34\x12", 8,
117 ETYPE_AES128_CTS_HMAC_SHA1_96
, 16,
118 "\xd1\xda\xa7\x86\x15\xf2\x87\xe6\xa1\xc8\xb1\x20\xd7\x06\x2a\x49",
119 "\xe9\xb2\x3d\x52\x27\x37\x47\xdd\x5c\x35\xcb\x55\xbe\x61\x9d\x8e"
122 "password", "\x12\x34\x56\x78\x78\x56\x34\x12", 8,
124 ETYPE_AES256_CTS_HMAC_SHA1_96
, 32,
125 "\xd1\xda\xa7\x86\x15\xf2\x87\xe6\xa1\xc8\xb1\x20\xd7\x06\x2a\x49"
126 "\x3f\x98\xd2\x03\xe6\xbe\x49\xa6\xad\xf4\xfa\x57\x4b\x6e\x64\xee",
127 "\x97\xa4\xe7\x86\xbe\x20\xd8\x1a\x38\x2d\x5e\xbc\x96\xd5\x90\x9c"
128 "\xab\xcd\xad\xc8\x7c\xa4\x8f\x57\x45\x04\x15\x9f\x16\xc3\x6e\x31"
131 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
132 "pass phrase equals block size", -1,
134 ETYPE_AES128_CTS_HMAC_SHA1_96
, 16,
135 "\x13\x9c\x30\xc0\x96\x6b\xc3\x2b\xa5\x5f\xdb\xf2\x12\x53\x0a\xc9",
136 "\x59\xd1\xbb\x78\x9a\x82\x8b\x1a\xa5\x4e\xf9\xc2\x88\x3f\x69\xed"
139 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
140 "pass phrase equals block size", -1,
142 ETYPE_AES256_CTS_HMAC_SHA1_96
, 32,
143 "\x13\x9c\x30\xc0\x96\x6b\xc3\x2b\xa5\x5f\xdb\xf2\x12\x53\x0a\xc9"
144 "\xc5\xec\x59\xf1\xa4\x52\xf5\xcc\x9a\xd9\x40\xfe\xa0\x59\x8e\xd1",
145 "\x89\xad\xee\x36\x08\xdb\x8b\xc7\x1f\x1b\xfb\xfe\x45\x94\x86\xb0"
146 "\x56\x18\xb7\x0c\xba\xe2\x20\x92\x53\x4e\x56\xc5\x53\xba\x4b\x34"
149 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
150 "pass phrase exceeds block size", -1,
152 ETYPE_AES128_CTS_HMAC_SHA1_96
, 16,
153 "\x9c\xca\xd6\xd4\x68\x77\x0c\xd5\x1b\x10\xe6\xa6\x87\x21\xbe\x61",
154 "\xcb\x80\x05\xdc\x5f\x90\x17\x9a\x7f\x02\x10\x4c\x00\x18\x75\x1d"
157 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
158 "pass phrase exceeds block size", -1,
160 ETYPE_AES256_CTS_HMAC_SHA1_96
, 32,
161 "\x9c\xca\xd6\xd4\x68\x77\x0c\xd5\x1b\x10\xe6\xa6\x87\x21\xbe\x61"
162 "\x1a\x8b\x4d\x28\x26\x01\xdb\x3b\x36\xbe\x92\x46\x91\x5e\xc8\x2a",
163 "\xd7\x8c\x5c\x9c\xb8\x72\xa8\xc9\xda\xd4\x69\x7f\x0b\xb5\xb2\xd2"
164 "\x14\x96\xc8\x2b\xeb\x2c\xae\xda\x21\x12\xfc\xee\xa0\x57\x40\x1b"
168 "\xf0\x9d\x84\x9e" /* g-clef */, "EXAMPLE.COMpianist", -1,
170 ETYPE_AES128_CTS_HMAC_SHA1_96
, 16,
171 "\x6b\x9c\xf2\x6d\x45\x45\x5a\x43\xa5\xb8\xbb\x27\x6a\x40\x3b\x39",
172 "\xf1\x49\xc1\xf2\xe1\x54\xa7\x34\x52\xd4\x3e\x7f\xe6\x2a\x56\xe5"
175 "\xf0\x9d\x84\x9e" /* g-clef */, "EXAMPLE.COMpianist", -1,
177 ETYPE_AES256_CTS_HMAC_SHA1_96
, 32,
178 "\x6b\x9c\xf2\x6d\x45\x45\x5a\x43\xa5\xb8\xbb\x27\x6a\x40\x3b\x39"
179 "\xe7\xfe\x37\xa0\xc4\x1e\x02\xc2\x81\xff\x30\x69\xe1\xe9\x4f\x52",
180 "\x4b\x6d\x98\x39\xf8\x44\x06\xdf\x1f\x09\xcc\x16\x6d\xb4\xb8\x3c"
181 "\x57\x18\x48\xb7\x84\xa3\xd6\xbd\xc3\x46\x58\x9a\x3e\x39\x3f\x9e"
186 ETYPE_ARCFOUR_HMAC_MD5
, 16,
188 "\xac\x8e\x65\x7f\x83\xdf\x82\xbe\xea\x5d\x43\xbd\xaf\x78\x00\xcc"
193 ETYPE_ARCFOUR_HMAC_MD5
, 16,
195 "\x0c\xb6\x94\x88\x05\xf7\x97\xbf\x2a\x82\x80\x79\x73\xb8\x95\x37"
200 string_to_key_test(krb5_context context
)
202 krb5_data password
, opaque
;
208 for (i
= 0; i
< sizeof(keys
)/sizeof(keys
[0]); i
++) {
210 password
.data
= keys
[i
].password
;
211 password
.length
= strlen(password
.data
);
213 salt
.salttype
= KRB5_PW_SALT
;
214 salt
.saltvalue
.data
= keys
[i
].salt
;
215 if (keys
[i
].saltlen
== -1)
216 salt
.saltvalue
.length
= strlen(salt
.saltvalue
.data
);
218 salt
.saltvalue
.length
= keys
[i
].saltlen
;
221 opaque
.length
= sizeof(iter
);
222 _krb5_put_int(iter
, keys
[i
].iterations
, 4);
224 if (keys
[i
].pbkdf2
) {
225 unsigned char keyout
[32];
227 if (keys
[i
].keylen
> sizeof(keyout
))
230 PKCS5_PBKDF2_HMAC_SHA1(password
.data
, password
.length
,
231 salt
.saltvalue
.data
, salt
.saltvalue
.length
,
233 keys
[i
].keylen
, keyout
);
235 if (memcmp(keyout
, keys
[i
].pbkdf2
, keys
[i
].keylen
) != 0) {
236 krb5_warnx(context
, "%d: pbkdf2", i
);
243 hex_dump_data(keyout
, keys
[i
].keylen
);
250 ret
= krb5_string_to_key_data_salt_opaque (context
,
257 krb5_warn(context
, ret
, "%d: string_to_key_data_salt_opaque",
263 if (key
.keyvalue
.length
!= keys
[i
].keylen
) {
264 krb5_warnx(context
, "%d: key wrong length (%lu/%lu)",
265 i
, (unsigned long)key
.keyvalue
.length
,
266 (unsigned long)keys
[i
].keylen
);
271 if (memcmp(key
.keyvalue
.data
, keys
[i
].key
, keys
[i
].keylen
) != 0) {
272 krb5_warnx(context
, "%d: key wrong", i
);
279 hex_dump_data(key
.keyvalue
.data
, key
.keyvalue
.length
);
281 krb5_free_keyblock_contents(context
, &key
);
288 krb_enc(krb5_context context
,
297 krb5_data_zero(&decrypt
);
299 ret
= krb5_decrypt(context
,
307 krb5_warn(context
, ret
, "krb5_decrypt");
311 if (decrypt
.length
!= clear
->length
||
312 memcmp(decrypt
.data
, clear
->data
, decrypt
.length
) != 0) {
313 krb5_warnx(context
, "clear text not same");
317 krb5_data_free(&decrypt
);
323 krb_enc_iov2(krb5_context context
,
329 krb5_crypto_iov iov
[4];
338 iov
[0].flags
= KRB5_CRYPTO_TYPE_HEADER
;
339 krb5_crypto_length(context
, crypto
, iov
[0].flags
, &iov
[0].data
.length
);
340 iov
[0].data
.data
= emalloc(iov
[0].data
.length
);
342 iov
[1].flags
= KRB5_CRYPTO_TYPE_DATA
;
343 iov
[1].data
.length
= len
;
344 iov
[1].data
.data
= emalloc(iov
[1].data
.length
);
345 memcpy(iov
[1].data
.data
, p
, iov
[1].data
.length
);
348 iov
[2].flags
= KRB5_CRYPTO_TYPE_PADDING
;
349 krb5_crypto_length(context
, crypto
, KRB5_CRYPTO_TYPE_PADDING
, &iov
[2].data
.length
);
350 iov
[2].data
.data
= emalloc(iov
[2].data
.length
);
352 iov
[3].flags
= KRB5_CRYPTO_TYPE_TRAILER
;
353 krb5_crypto_length(context
, crypto
, iov
[3].flags
, &iov
[3].data
.length
);
354 iov
[3].data
.data
= emalloc(iov
[3].data
.length
);
356 ret
= krb5_encrypt_iov_ivec(context
, crypto
, usage
,
357 iov
, sizeof(iov
)/sizeof(iov
[0]), NULL
);
359 errx(1, "encrypt iov failed: %d", ret
);
362 for (i
= 0, len
= 0; i
< sizeof(iov
)/sizeof(iov
[0]); i
++)
363 len
+= iov
[i
].data
.length
;
364 if (len
!= cipher_len
)
365 errx(1, "cipher len wrong");
371 p
= q
= emalloc(len
);
372 for (i
= 0; i
< sizeof(iov
)/sizeof(iov
[0]); i
++) {
373 memcpy(q
, iov
[i
].data
.data
, iov
[i
].data
.length
);
374 q
+= iov
[i
].data
.length
;
377 ret
= krb5_decrypt(context
, crypto
, usage
, p
, len
, &decrypt
);
379 krb5_err(context
, 1, ret
, "krb5_decrypt");
381 krb5_data_free(&decrypt
);
386 * Now decrypt use iov
389 /* padding turn into data */
390 p
= q
= emalloc(iov
[1].data
.length
+ iov
[2].data
.length
);
392 memcpy(q
, iov
[1].data
.data
, iov
[1].data
.length
);
393 q
+= iov
[1].data
.length
;
394 memcpy(q
, iov
[2].data
.data
, iov
[2].data
.length
);
396 free(iov
[1].data
.data
);
397 free(iov
[2].data
.data
);
399 iov
[1].data
.data
= p
;
400 iov
[1].data
.length
+= iov
[2].data
.length
;
402 iov
[2].flags
= KRB5_CRYPTO_TYPE_EMPTY
;
403 iov
[2].data
.length
= 0;
405 ret
= krb5_decrypt_iov_ivec(context
, crypto
, usage
,
406 iov
, sizeof(iov
)/sizeof(iov
[0]), NULL
);
407 free(iov
[0].data
.data
);
408 free(iov
[3].data
.data
);
411 krb5_err(context
, 1, ret
, "decrypt iov failed: %d", ret
);
413 if (clear
->length
!= iov
[1].data
.length
)
414 errx(1, "length incorrect");
417 if (memcmp(iov
[1].data
.data
, p
, iov
[1].data
.length
) != 0)
418 errx(1, "iov[1] incorrect");
420 free(iov
[1].data
.data
);
427 krb_enc_iov(krb5_context context
,
433 krb5_crypto_iov iov
[3];
439 len
= cipher
->length
;
441 iov
[0].flags
= KRB5_CRYPTO_TYPE_HEADER
;
442 krb5_crypto_length(context
, crypto
, iov
[0].flags
, &iov
[0].data
.length
);
443 iov
[0].data
.data
= emalloc(iov
[0].data
.length
);
444 memcpy(iov
[0].data
.data
, p
, iov
[0].data
.length
);
445 p
+= iov
[0].data
.length
;
446 len
-= iov
[0].data
.length
;
448 iov
[1].flags
= KRB5_CRYPTO_TYPE_TRAILER
;
449 krb5_crypto_length(context
, crypto
, iov
[1].flags
, &iov
[1].data
.length
);
450 iov
[1].data
.data
= emalloc(iov
[1].data
.length
);
451 memcpy(iov
[1].data
.data
, p
+ len
- iov
[1].data
.length
, iov
[1].data
.length
);
452 len
-= iov
[1].data
.length
;
454 iov
[2].flags
= KRB5_CRYPTO_TYPE_DATA
;
455 iov
[2].data
.length
= len
;
456 iov
[2].data
.data
= emalloc(len
);
457 memcpy(iov
[2].data
.data
, p
, len
);
459 ret
= krb5_decrypt_iov_ivec(context
, crypto
, usage
,
460 iov
, sizeof(iov
)/sizeof(iov
[0]), NULL
);
462 krb5_err(context
, 1, ret
, "krb_enc_iov decrypt iov failed: %d", ret
);
464 if (clear
->length
!= iov
[2].data
.length
)
465 errx(1, "length incorrect");
468 if (memcmp(iov
[2].data
.data
, p
, iov
[2].data
.length
) != 0)
469 errx(1, "iov[2] incorrect");
471 free(iov
[0].data
.data
);
472 free(iov
[1].data
.data
);
473 free(iov
[2].data
.data
);
480 krb_checksum_iov(krb5_context context
,
485 krb5_crypto_iov iov
[4];
493 iov
[0].flags
= KRB5_CRYPTO_TYPE_CHECKSUM
;
494 krb5_crypto_length(context
, crypto
, iov
[0].flags
, &iov
[0].data
.length
);
495 iov
[0].data
.data
= emalloc(iov
[0].data
.length
);
497 iov
[1].flags
= KRB5_CRYPTO_TYPE_DATA
;
498 iov
[1].data
.length
= len
;
499 iov
[1].data
.data
= p
;
501 iov
[2].flags
= KRB5_CRYPTO_TYPE_TRAILER
;
502 krb5_crypto_length(context
, crypto
, iov
[0].flags
, &iov
[2].data
.length
);
503 iov
[2].data
.data
= malloc(iov
[2].data
.length
);
505 ret
= krb5_create_checksum_iov(context
, crypto
, usage
,
506 iov
, sizeof(iov
)/sizeof(iov
[0]), NULL
);
508 krb5_err(context
, 1, ret
, "krb5_create_checksum_iov failed");
510 ret
= krb5_verify_checksum_iov(context
, crypto
, usage
, iov
, sizeof(iov
)/sizeof(iov
[0]), NULL
);
512 krb5_err(context
, 1, ret
, "krb5_verify_checksum_iov");
514 free(iov
[0].data
.data
);
515 free(iov
[2].data
.data
);
522 krb_enc_mit(krb5_context context
,
523 krb5_enctype enctype
,
529 #ifndef HEIMDAL_SMALLER
537 e
.ciphertext
= *cipher
;
539 ret
= krb5_c_decrypt(context
, *key
, usage
, NULL
, &e
, &decrypt
);
543 if (decrypt
.length
!= clear
->length
||
544 memcmp(decrypt
.data
, clear
->data
, decrypt
.length
) != 0) {
545 krb5_warnx(context
, "clear text not same");
549 krb5_data_free(&decrypt
);
551 ret
= krb5_c_encrypt_length(context
, enctype
, clear
->length
, &len
);
555 if (len
!= cipher
->length
) {
556 krb5_warnx(context
, "c_encrypt_length wrong %lu != %lu",
557 (unsigned long)len
, (unsigned long)cipher
->length
);
560 #endif /* HEIMDAL_SMALLER */
566 krb5_enctype enctype
;
576 ETYPE_AES256_CTS_HMAC_SHA1_96
,
579 "\x47\x75\x69\x64\x65\x6c\x69\x6e\x65\x73\x20\x74\x6f\x20\x41\x75"
580 "\x74\x68\x6f\x72\x73\x20\x6f\x66\x20\x49\x6e\x74\x65\x72\x6e\x65",
582 "\xcf\x79\x8f\x0d\x76\xf3\xe0\xbe\x8e\x66\x94\x70\xfa\xcc\x9e\x91"
583 "\xa9\xec\x1c\x5c\x21\xfb\x6e\xef\x1a\x7a\xc8\xc1\xcc\x5a\x95\x24"
584 "\x6f\x9f\xf4\xd5\xbe\x5d\x59\x97\x44\xd8\x47\xcd",
586 "\x54\x68\x69\x73\x20\x69\x73\x20\x61\x20\x74\x65\x73\x74\x2e\x0a"
592 krb_enc_test(krb5_context context
)
597 krb5_data cipher
, plain
;
600 for (i
= 0; i
< sizeof(krbencs
)/sizeof(krbencs
[0]); i
++) {
602 kb
.keytype
= krbencs
[i
].enctype
;
603 kb
.keyvalue
.length
= krbencs
[i
].keylen
;
604 kb
.keyvalue
.data
= krbencs
[i
].key
;
606 ret
= krb5_crypto_init(context
, &kb
, krbencs
[i
].enctype
, &crypto
);
608 cipher
.length
= krbencs
[i
].elen
;
609 cipher
.data
= krbencs
[i
].edata
;
610 plain
.length
= krbencs
[i
].plen
;
611 plain
.data
= krbencs
[i
].pdata
;
613 ret
= krb_enc(context
, crypto
, krbencs
[i
].usage
, &cipher
, &plain
);
616 errx(1, "krb_enc failed with %d for test %d", ret
, i
);
618 ret
= krb_enc_iov(context
, crypto
, krbencs
[i
].usage
, &cipher
, &plain
);
620 errx(1, "krb_enc_iov failed with %d for test %d", ret
, i
);
622 ret
= krb_enc_iov2(context
, crypto
, krbencs
[i
].usage
,
623 cipher
.length
, &plain
);
625 errx(1, "krb_enc_iov2 failed with %d for test %d", ret
, i
);
627 ret
= krb_checksum_iov(context
, crypto
, krbencs
[i
].usage
, &plain
);
629 errx(1, "krb_checksum_iov failed with %d for test %d", ret
, i
);
631 krb5_crypto_destroy(context
, crypto
);
633 ret
= krb_enc_mit(context
, krbencs
[i
].enctype
, &kb
,
634 krbencs
[i
].usage
, &cipher
, &plain
);
636 errx(1, "krb_enc_mit failed with %d for test %d", ret
, i
);
643 iov_test(krb5_context context
)
645 krb5_enctype enctype
= ENCTYPE_AES256_CTS_HMAC_SHA1_96
;
649 krb5_data signonly
, in
, in2
;
650 krb5_crypto_iov iov
[6];
652 unsigned char *base
, *p
;
654 ret
= krb5_generate_random_keyblock(context
, enctype
, &key
);
656 krb5_err(context
, 1, ret
, "krb5_generate_random_keyblock");
658 ret
= krb5_crypto_init(context
, &key
, 0, &crypto
);
660 krb5_err(context
, 1, ret
, "krb5_crypto_init");
663 ret
= krb5_crypto_length(context
, crypto
, KRB5_CRYPTO_TYPE_HEADER
, &len
);
665 krb5_err(context
, 1, ret
, "krb5_crypto_length");
667 signonly
.data
= "This should be signed";
668 signonly
.length
= strlen(signonly
.data
);
669 in
.data
= "inputdata";
670 in
.length
= strlen(in
.data
);
672 in2
.data
= "INPUTDATA";
673 in2
.length
= strlen(in2
.data
);
676 memset(iov
, 0, sizeof(iov
));
678 iov
[0].flags
= KRB5_CRYPTO_TYPE_HEADER
;
679 iov
[1].flags
= KRB5_CRYPTO_TYPE_DATA
;
681 iov
[2].flags
= KRB5_CRYPTO_TYPE_SIGN_ONLY
;
682 iov
[2].data
= signonly
;
683 iov
[3].flags
= KRB5_CRYPTO_TYPE_EMPTY
;
684 iov
[4].flags
= KRB5_CRYPTO_TYPE_PADDING
;
685 iov
[5].flags
= KRB5_CRYPTO_TYPE_TRAILER
;
687 ret
= krb5_crypto_length_iov(context
, crypto
, iov
,
688 sizeof(iov
)/sizeof(iov
[0]));
690 krb5_err(context
, 1, ret
, "krb5_crypto_length_iov");
692 for (len
= 0, i
= 0; i
< sizeof(iov
)/sizeof(iov
[0]); i
++) {
693 if (iov
[i
].flags
== KRB5_CRYPTO_TYPE_SIGN_ONLY
)
695 len
+= iov
[i
].data
.length
;
701 * Allocate data for the fields
704 for (p
= base
, i
= 0; i
< sizeof(iov
)/sizeof(iov
[0]); i
++) {
705 if (iov
[i
].flags
== KRB5_CRYPTO_TYPE_SIGN_ONLY
)
707 iov
[i
].data
.data
= p
;
708 p
+= iov
[i
].data
.length
;
710 assert(iov
[1].data
.length
== in
.length
);
711 memcpy(iov
[1].data
.data
, in
.data
, iov
[1].data
.length
);
717 ret
= krb5_encrypt_iov_ivec(context
, crypto
, 7, iov
,
718 sizeof(iov
)/sizeof(iov
[0]), NULL
);
720 krb5_err(context
, 1, ret
, "krb5_encrypt_iov_ivec");
726 ret
= krb5_decrypt_iov_ivec(context
, crypto
, 7,
727 iov
, sizeof(iov
)/sizeof(iov
[0]), NULL
);
729 krb5_err(context
, 1, ret
, "krb5_decrypt_iov_ivec");
735 if (krb5_data_cmp(&iov
[1].data
, &in
) != 0)
736 krb5_errx(context
, 1, "decrypted data not same");
744 /* Set up for second try */
746 iov
[3].flags
= KRB5_CRYPTO_TYPE_DATA
;
749 ret
= krb5_crypto_length_iov(context
, crypto
,
750 iov
, sizeof(iov
)/sizeof(iov
[0]));
752 krb5_err(context
, 1, ret
, "krb5_crypto_length_iov");
754 for (len
= 0, i
= 0; i
< sizeof(iov
)/sizeof(iov
[0]); i
++) {
755 if (iov
[i
].flags
== KRB5_CRYPTO_TYPE_SIGN_ONLY
)
757 len
+= iov
[i
].data
.length
;
763 * Allocate data for the fields
766 for (p
= base
, i
= 0; i
< sizeof(iov
)/sizeof(iov
[0]); i
++) {
767 if (iov
[i
].flags
== KRB5_CRYPTO_TYPE_SIGN_ONLY
)
769 iov
[i
].data
.data
= p
;
770 p
+= iov
[i
].data
.length
;
772 assert(iov
[1].data
.length
== in
.length
);
773 memcpy(iov
[1].data
.data
, in
.data
, iov
[1].data
.length
);
775 assert(iov
[3].data
.length
== in2
.length
);
776 memcpy(iov
[3].data
.data
, in2
.data
, iov
[3].data
.length
);
784 ret
= krb5_encrypt_iov_ivec(context
, crypto
, 7,
785 iov
, sizeof(iov
)/sizeof(iov
[0]), NULL
);
787 krb5_err(context
, 1, ret
, "krb5_encrypt_iov_ivec");
793 ret
= krb5_decrypt_iov_ivec(context
, crypto
, 7,
794 iov
, sizeof(iov
)/sizeof(iov
[0]), NULL
);
796 krb5_err(context
, 1, ret
, "krb5_decrypt_iov_ivec");
802 if (krb5_data_cmp(&iov
[1].data
, &in
) != 0)
803 krb5_errx(context
, 1, "decrypted data 2.1 not same");
805 if (krb5_data_cmp(&iov
[3].data
, &in2
) != 0)
806 krb5_errx(context
, 1, "decrypted data 2.2 not same");
814 krb5_crypto_destroy(context
, crypto
);
816 krb5_free_keyblock_contents(context
, &key
);
824 random_to_key(krb5_context context
)
829 ret
= krb5_random_to_key(context
,
831 "\x21\x39\x04\x58\x6A\xBD\x7F"
832 "\x21\x39\x04\x58\x6A\xBD\x7F"
833 "\x21\x39\x04\x58\x6A\xBD\x7F",
837 krb5_warn(context
, ret
, "random_to_key");
840 if (key
.keyvalue
.length
!= 24)
843 if (memcmp(key
.keyvalue
.data
,
844 "\x20\x38\x04\x58\x6b\xbc\x7f\xc7"
845 "\x20\x38\x04\x58\x6b\xbc\x7f\xc7"
846 "\x20\x38\x04\x58\x6b\xbc\x7f\xc7",
850 krb5_free_keyblock_contents(context
, &key
);
856 main(int argc
, char **argv
)
859 krb5_context context
;
862 ret
= krb5_init_context (&context
);
864 errx (1, "krb5_init_context failed: %d", ret
);
866 val
|= string_to_key_test(context
);
868 val
|= krb_enc_test(context
);
869 val
|= random_to_key(context
);
870 val
|= iov_test(context
);
872 if (verbose
&& val
== 0)
875 printf("tests failed\n");
877 krb5_free_context(context
);