|
Packit |
fd8b60 |
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
|
|
Packit |
fd8b60 |
/* lib/crypto/crypto_tests/aes-test.c */
|
|
Packit |
fd8b60 |
/*
|
|
Packit |
fd8b60 |
* Copyright (C) 2002 by the Massachusetts Institute of Technology.
|
|
Packit |
fd8b60 |
* All Rights Reserved.
|
|
Packit |
fd8b60 |
*
|
|
Packit |
fd8b60 |
* Export of this software from the United States of America may
|
|
Packit |
fd8b60 |
* require a specific license from the United States Government.
|
|
Packit |
fd8b60 |
* It is the responsibility of any person or organization contemplating
|
|
Packit |
fd8b60 |
* export to obtain such a license before exporting.
|
|
Packit |
fd8b60 |
*
|
|
Packit |
fd8b60 |
* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
|
|
Packit |
fd8b60 |
* distribute this software and its documentation for any purpose and
|
|
Packit |
fd8b60 |
* without fee is hereby granted, provided that the above copyright
|
|
Packit |
fd8b60 |
* notice appear in all copies and that both that copyright notice and
|
|
Packit |
fd8b60 |
* this permission notice appear in supporting documentation, and that
|
|
Packit |
fd8b60 |
* the name of M.I.T. not be used in advertising or publicity pertaining
|
|
Packit |
fd8b60 |
* to distribution of the software without specific, written prior
|
|
Packit |
fd8b60 |
* permission. Furthermore if you modify this software you must label
|
|
Packit |
fd8b60 |
* your software as modified software and not distribute it in such a
|
|
Packit |
fd8b60 |
* fashion that it might be confused with the original M.I.T. software.
|
|
Packit |
fd8b60 |
* M.I.T. makes no representations about the suitability of
|
|
Packit |
fd8b60 |
* this software for any purpose. It is provided "as is" without express
|
|
Packit |
fd8b60 |
* or implied warranty.
|
|
Packit |
fd8b60 |
*/
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
/*
|
|
Packit |
fd8b60 |
* Subset of NIST tests for AES; specifically, the variable-key and
|
|
Packit |
fd8b60 |
* variable-text tests for 128- and 256-bit keys.
|
|
Packit |
fd8b60 |
*/
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
#include <stdio.h>
|
|
Packit |
fd8b60 |
#include "crypto_int.h"
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
static char key[32];
|
|
Packit |
fd8b60 |
static char plain[16], cipher[16], zero[16];
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
static krb5_keyblock enc_key;
|
|
Packit |
fd8b60 |
static krb5_data ivec;
|
|
Packit |
fd8b60 |
static void init()
|
|
Packit |
fd8b60 |
{
|
|
Packit |
fd8b60 |
enc_key.contents = (krb5_octet *)key;
|
|
Packit |
fd8b60 |
enc_key.length = 16;
|
|
Packit |
fd8b60 |
ivec.data = zero;
|
|
Packit |
fd8b60 |
ivec.length = 16;
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
static void enc()
|
|
Packit |
fd8b60 |
{
|
|
Packit |
fd8b60 |
krb5_key k;
|
|
Packit |
fd8b60 |
krb5_crypto_iov iov;
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
memcpy(cipher, plain, 16);
|
|
Packit |
fd8b60 |
iov.flags = KRB5_CRYPTO_TYPE_DATA;
|
|
Packit |
fd8b60 |
iov.data = make_data(cipher, 16);
|
|
Packit |
fd8b60 |
krb5_k_create_key(NULL, &enc_key, &k);
|
|
Packit |
fd8b60 |
krb5int_aes_encrypt(k, &ivec, &iov, 1);
|
|
Packit |
fd8b60 |
krb5_k_free_key(NULL, k);
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
static void hexdump(const char *label, const char *cp, int len)
|
|
Packit |
fd8b60 |
{
|
|
Packit |
fd8b60 |
printf("%s=", label);
|
|
Packit |
fd8b60 |
while (len--) printf("%02X", 0xff & *cp++);
|
|
Packit |
fd8b60 |
printf("\n");
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
static void set_bit(char *ptr, int bitnum)
|
|
Packit |
fd8b60 |
{
|
|
Packit |
fd8b60 |
int bytenum;
|
|
Packit |
fd8b60 |
bytenum = bitnum / 8;
|
|
Packit |
fd8b60 |
bitnum %= 8;
|
|
Packit |
fd8b60 |
/* First bit is the high bit! */
|
|
Packit |
fd8b60 |
ptr[bytenum] = 1 << (7 - bitnum);
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
/* Variable-Key tests */
|
|
Packit |
fd8b60 |
static void vk_test_1(int len, krb5_enctype etype)
|
|
Packit |
fd8b60 |
{
|
|
Packit |
fd8b60 |
int i;
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
enc_key.length = len;
|
|
Packit |
fd8b60 |
enc_key.enctype = etype;
|
|
Packit |
fd8b60 |
printf("\nKEYSIZE=%d\n\n", len * 8);
|
|
Packit |
fd8b60 |
memset(plain, 0, sizeof(plain));
|
|
Packit |
fd8b60 |
hexdump("PT", plain, 16);
|
|
Packit |
fd8b60 |
for (i = 0; i < len * 8; i++) {
|
|
Packit |
fd8b60 |
memset(key, 0, len);
|
|
Packit |
fd8b60 |
set_bit(key, i);
|
|
Packit |
fd8b60 |
printf("\nI=%d\n", i+1);
|
|
Packit |
fd8b60 |
hexdump("KEY", key, len);
|
|
Packit |
fd8b60 |
enc();
|
|
Packit |
fd8b60 |
hexdump("CT", cipher, 16);
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
printf("\n==========\n");
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
static void vk_test()
|
|
Packit |
fd8b60 |
{
|
|
Packit |
fd8b60 |
vk_test_1(16, ENCTYPE_AES128_CTS_HMAC_SHA1_96);
|
|
Packit |
fd8b60 |
vk_test_1(32, ENCTYPE_AES256_CTS_HMAC_SHA1_96);
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
/* Variable-Text tests */
|
|
Packit |
fd8b60 |
static void vt_test_1(int len, krb5_enctype etype)
|
|
Packit |
fd8b60 |
{
|
|
Packit |
fd8b60 |
int i;
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
enc_key.length = len;
|
|
Packit |
fd8b60 |
enc_key.enctype = etype;
|
|
Packit |
fd8b60 |
printf("\nKEYSIZE=%d\n\n", len * 8);
|
|
Packit |
fd8b60 |
memset(key, 0, len);
|
|
Packit |
fd8b60 |
hexdump("KEY", key, len);
|
|
Packit |
fd8b60 |
for (i = 0; i < 16 * 8; i++) {
|
|
Packit |
fd8b60 |
memset(plain, 0, sizeof(plain));
|
|
Packit |
fd8b60 |
set_bit(plain, i);
|
|
Packit |
fd8b60 |
printf("\nI=%d\n", i+1);
|
|
Packit |
fd8b60 |
hexdump("PT", plain, 16);
|
|
Packit |
fd8b60 |
enc();
|
|
Packit |
fd8b60 |
hexdump("CT", cipher, 16);
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
printf("\n==========\n");
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
static void vt_test()
|
|
Packit |
fd8b60 |
{
|
|
Packit |
fd8b60 |
vt_test_1(16, ENCTYPE_AES128_CTS_HMAC_SHA1_96);
|
|
Packit |
fd8b60 |
vt_test_1(32, ENCTYPE_AES256_CTS_HMAC_SHA1_96);
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
int main (int argc, char *argv[])
|
|
Packit |
fd8b60 |
{
|
|
Packit |
fd8b60 |
if (argc > 2 || (argc == 2 && strcmp(argv[1], "-k"))) {
|
|
Packit |
fd8b60 |
fprintf(stderr,
|
|
Packit |
fd8b60 |
"usage:\t%s -k\tfor variable-key tests\n"
|
|
Packit |
fd8b60 |
" or:\t%s \tfor variable-plaintext tests\n",
|
|
Packit |
fd8b60 |
argv[0], argv[0]);
|
|
Packit |
fd8b60 |
return 1;
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
init();
|
|
Packit |
fd8b60 |
if (argc == 2)
|
|
Packit |
fd8b60 |
vk_test();
|
|
Packit |
fd8b60 |
else
|
|
Packit |
fd8b60 |
vt_test();
|
|
Packit |
fd8b60 |
return 0;
|
|
Packit |
fd8b60 |
}
|