Skip to content

Commit

Permalink
1. correct errors in the AVS test code
Browse files Browse the repository at this point in the history
2  update the AVS test vectors to latest version
  • Loading branch information
BrianGladman committed Sep 27, 2018
1 parent 37b7a4b commit 895de0f
Show file tree
Hide file tree
Showing 136 changed files with 48,905 additions and 15,657 deletions.
93 changes: 42 additions & 51 deletions aes_avs.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,7 @@ char *klen_str[] = { "128", "192", "256" };
typedef enum { L_bad = -1, L_count = 0, L_key, L_iv, L_plaintext, L_ciphertext } line_type;
char *hdr_str[] = { "COUNT = ", "KEY = ", "IV = ", "PLAINTEXT = ", "CIPHERTEXT = " };

char *test_path = "..\\testvals\\fax\\";

int block_in2(unsigned char l[], const char *p)
{ int i = 0;

while(*p && *(p + 1) && isxdigit(*p) && isxdigit(*(p + 1)))
{
l[i++] = (to_hex(*p) << 4) + to_hex(*(p + 1)); p += 2;
}
return i;
}
char *test_path = "..\\testvals\\avs\\";

enum line_type find_line2(char str[], char **p)
{ int i;
Expand All @@ -79,20 +69,20 @@ void do_encrypt(mode mm, const unsigned char key[], unsigned char iv[],
const unsigned char pt[], unsigned char ct[], int key_len, int block_len)
{ aes_encrypt_ctx ctx[1];

f_enc_key(key, key_len, ctx);
f_enc_key(ctx, key, key_len);
switch(mm)
{
case ECB:
f_ecb_enc(pt, ct, block_len, ctx);
f_ecb_enc(ctx, pt, ct, block_len);
break;
case CBC:
f_cbc_enc(pt, ct, block_len, iv, ctx);
f_cbc_enc(ctx, pt, ct, block_len, iv);
break;
case CFB:
f_cfb_enc(pt, ct, block_len, iv, ctx);
f_cfb_enc(ctx, pt, ct, block_len, iv);
break;
case OFB:
f_ofb_cry(pt, ct, block_len, iv, ctx);
f_ofb_cry(ctx, pt, ct, block_len, iv);
break;
}
}
Expand All @@ -104,20 +94,20 @@ void do_decrypt(mode mm, const unsigned char key[], unsigned char iv[],
switch(mm)
{
case ECB:
f_dec_key(key, key_len, ctx);
f_ecb_dec(ct, pt, block_len, ctx);
f_dec_key(ctx, key, key_len);
f_ecb_dec(ctx, ct, pt, block_len);
break;
case CBC:
f_dec_key(key, key_len, ctx);
f_cbc_dec(ct, pt, block_len, iv, ctx);
f_dec_key(ctx, key, key_len);
f_cbc_dec(ctx, ct, pt, block_len, iv);
break;
case CFB:
f_enc_key(key, key_len, (aes_encrypt_ctx*)ctx);
f_cfb_dec(ct, pt, block_len, iv, (aes_encrypt_ctx*)ctx);
f_enc_key((aes_encrypt_ctx*)ctx, key, key_len);
f_cfb_dec((aes_encrypt_ctx*)ctx, ct, pt, block_len, iv);
break;
case OFB:
f_enc_key(key, key_len, (aes_encrypt_ctx*)ctx);
f_ofb_cry(ct, pt, block_len, iv, (aes_encrypt_ctx*)ctx);
f_enc_key((aes_encrypt_ctx*)ctx, key, key_len);
f_ofb_cry((aes_encrypt_ctx*)ctx, ct, pt, block_len, iv);
break;
}
}
Expand All @@ -128,13 +118,13 @@ void do_mct_encrypt(mode mm, const unsigned char key[], unsigned char iv[],
unsigned char tmp[BLOCK_SIZE];
int i;

f_enc_key(key, key_len, ctx);
f_enc_key(ctx, key, key_len);
if(mm == ECB)
{
for(i = 0; i < MCT_REPEAT / 2; ++i)
{
f_ecb_enc(pt, ct, BLOCK_SIZE, ctx);
f_ecb_enc(ct, pt, BLOCK_SIZE, ctx);
f_ecb_enc(ctx, pt, ct, BLOCK_SIZE);
f_ecb_enc(ctx, ct, pt, BLOCK_SIZE);
}
memcpy(ct, pt, BLOCK_SIZE);
}
Expand All @@ -146,13 +136,13 @@ void do_mct_encrypt(mode mm, const unsigned char key[], unsigned char iv[],
switch(mm)
{
case 1:
f_cbc_enc(pt, ct, BLOCK_SIZE, iv, ctx);
f_cbc_enc(ctx, pt, ct, BLOCK_SIZE, iv);
break;
case 2:
f_cfb_enc(pt, ct, BLOCK_SIZE, iv, ctx);
f_cfb_enc(ctx, pt, ct, BLOCK_SIZE, iv);
break;
case 3:
f_ofb_cry(pt, ct, BLOCK_SIZE, iv, ctx);
f_ofb_cry(ctx, pt, ct, BLOCK_SIZE, iv);
break;
}
memcpy(pt, tmp, BLOCK_SIZE);
Expand All @@ -164,38 +154,38 @@ void do_mct_encrypt(mode mm, const unsigned char key[], unsigned char iv[],
void do_mct_decrypt(mode mm, const unsigned char key[], unsigned char iv[],
const unsigned char ct[], unsigned char pt[], int key_len, int block_len)
{ aes_decrypt_ctx ctx[1];
unsigned char tmp[BLOCK_SIZE], tmp2[BLOCK_SIZE];
unsigned char tmp[BLOCK_SIZE];
int i;
if(mm == ECB)
{
f_dec_key(key, key_len, ctx);
f_dec_key(ctx, key, key_len);
memcpy(tmp, ct, BLOCK_SIZE);
for(i = 0; i < MCT_REPEAT / 2; ++i)
{
f_ecb_dec(ct, pt, BLOCK_SIZE, ctx);
f_ecb_dec(pt, ct, BLOCK_SIZE, ctx);
f_ecb_dec(ctx, ct, pt, BLOCK_SIZE);
f_ecb_dec(ctx, pt, ct, BLOCK_SIZE);
}
memcpy(pt, ct, BLOCK_SIZE);
}
else
{
if(mm == CBC)
f_dec_key(key, key_len, ctx);
f_dec_key(ctx, key, key_len);
else
f_enc_key(key, key_len, (aes_encrypt_ctx*)ctx);
f_enc_key((aes_encrypt_ctx*)ctx, key, key_len);
memcpy(tmp, iv, BLOCK_SIZE);
for(i = 0; i < MCT_REPEAT; ++i)
{
switch(mm)
{
case 1:
f_cbc_dec(pt, ct, BLOCK_SIZE, iv, ctx);
f_cbc_dec(ctx, ct, pt, BLOCK_SIZE, iv);
break;
case 2:
f_cfb_dec(pt, ct, BLOCK_SIZE, iv, ctx);
f_cfb_dec(ctx, ct, pt, BLOCK_SIZE, iv);
break;
case 3:
f_ofb_cry(pt, ct, BLOCK_SIZE, iv, ctx);
f_ofb_cry(ctx, ct, pt, BLOCK_SIZE, iv);
break;
}
memcpy(ct, tmp, BLOCK_SIZE);
Expand All @@ -206,7 +196,7 @@ void do_mct_decrypt(mode mm, const unsigned char key[], unsigned char iv[],

void run_aes_avs_test(mode mm, type tt)
{
char path[128], inbuf[1024], *p = inbuf;
char path[128], inbuf[1024], *p = inbuf, *pp;
unsigned char key[2 * BLOCK_SIZE], iv[BLOCK_SIZE], pt[MAX_TEXT_SIZE], ct[MAX_TEXT_SIZE], rt[MAX_TEXT_SIZE];
int i, err, cnt, key_len, iv_len, pt_len, ct_len;
FILE *f;
Expand All @@ -219,58 +209,59 @@ void run_aes_avs_test(mode mm, type tt)
strcat(path, mode_str[mm]);
strcat(path, type_str[tt]);
strcat(path, klen_str[i]);
strcat(path, ".fax");
strcat(path, ".rsp");
if(fopen_s(&f, path, "r"))
{
printf("\nUnable to open %s for reading", path);
return;
}
while(get_line(f, inbuf) == EXIT_SUCCESS)
while(get_line(f, inbuf, 1024) == EXIT_SUCCESS)
{
if((ty = find_line2(inbuf, p)) != L_bad)
if((ty = find_line2(inbuf, &pp)) != L_bad)
{
switch(ty)
{
case L_count:
key_len = iv_len = pt_len = ct_len = 0;
cnt = get_dec(p);
break;
case L_key:
key_len = block_in2(key, p);
key_len = block_in(key, p);
break;
case L_iv:
iv_len = block_in2(iv, p);
iv_len = block_in(iv, p);
break;
case L_plaintext:
pt_len = block_in2(pt, p);
pt_len = block_in(pt, p);
if(pt_len == ct_len)
{
if(tt != MCT)
do_decrypt(mm, key, iv, ct, rt, key_len, pt_len);
else
do_mct_decrypt(mm, key, iv, ct, rt, key_len, pt_len);
if(memcmp(pt, rt, pt_len))
{
if(memcmp(pt, rt, pt_len)){
printf("\nError on file %s, on test %i", path, cnt);
++err;
}
}
break;
case L_ciphertext:
ct_len = block_in2(ct, p);
ct_len = block_in(ct, p);
if(ct_len == pt_len)
{
if(tt == MCT)
do_mct_encrypt(mm, key, iv, pt, rt, key_len, pt_len);
else
do_encrypt(mm, key, iv, pt, rt, key_len, pt_len);
if(memcmp(ct, rt, pt_len))
{
if(memcmp(ct, rt, pt_len)){
printf("\nError on file %s, on test %i", path, cnt);
++err;
}
}
break;
}

}
}
fclose(f);
if(!err)
Expand Down
6 changes: 3 additions & 3 deletions aesaux.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ int to_hex(int ch)
return (ch & 15) + (ch >= '0' && ch <= '9' ? 0 : 9);
}

int get_line(FILE *inf, char s[])
int get_line(FILE *inf, char s[], int max_len)
{
if(feof(inf))
return EXIT_FAILURE;

return fgets(s, 100, inf) != s ? EXIT_FAILURE : EXIT_SUCCESS;
return fgets(s, max_len, inf) != s ? EXIT_FAILURE : EXIT_SUCCESS;
}

char *copy_str(char *d, const char *s)
Expand Down Expand Up @@ -337,7 +337,7 @@ int find_string(const char *s1, const char s2[])
enum line_type find_line(FILE *inf, char str[])
{ int i;

while(get_line(inf, str) == EXIT_SUCCESS) // input a line
while(get_line(inf, str, 128) == EXIT_SUCCESS) // input a line
{
for(i = 1; i < NO_LTYPES; ++i) // compare line header
{
Expand Down
2 changes: 1 addition & 1 deletion aesaux.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ enum line_type { bad_line = 0, block_len, key_len, test_no, iv_val, key_val, pt_
char *file_name(char* buf, size_t len, const unsigned long type, const unsigned long blen, const unsigned long klen);
const char *pos(const char *s);
int to_hex(int ch);
int get_line(FILE *inf, char s[]);
int get_line(FILE *inf, char s[], int max_len);
char *copy_str(char *s, const char *fstr);
const char *df_string(const char *p);
int block_in(unsigned char l[], const char *p);
Expand Down
3 changes: 1 addition & 2 deletions test_avs/test_avs.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,8 @@
<TargetEnvironment>X64</TargetEnvironment>
</Midl>
<ClCompile>
<Optimization>Full</Optimization>
<AdditionalIncludeDirectories>..\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions);DLL_IMPORT;DLL_DYNAMIC_LOAD</PreprocessorDefinitions>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<PrecompiledHeader>
</PrecompiledHeader>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# CAVS 5.0
# Config info for AESSampleVectors
# CAVS 11.1
# Config info for aes_values
# AESVS GFSbox test data for CBC
# State : Encrypt and Decrypt
# Key Length : 128
# Generated on Tue Apr 11 08:47:33 2006
# Generated on Fri Apr 22 15:11:33 2011

[ENCRYPT]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# CAVS 5.0
# Config info for AESSampleVectors
# CAVS 11.1
# Config info for aes_values
# AESVS GFSbox test data for CBC
# State : Encrypt and Decrypt
# Key Length : 192
# Generated on Tue Apr 11 08:47:37 2006
# Generated on Fri Apr 22 15:11:35 2011

[ENCRYPT]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# CAVS 5.0
# Config info for AESSampleVectors
# CAVS 11.1
# Config info for aes_values
# AESVS GFSbox test data for CBC
# State : Encrypt and Decrypt
# Key Length : 256
# Generated on Tue Apr 11 08:47:42 2006
# Generated on Fri Apr 22 15:11:38 2011

[ENCRYPT]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# CAVS 5.0
# Config info for AESSampleVectors
# CAVS 11.1
# Config info for aes_values
# AESVS KeySbox test data for CBC
# State : Encrypt and Decrypt
# Key Length : 128
# Generated on Tue Apr 11 08:47:33 2006
# Generated on Fri Apr 22 15:11:33 2011

[ENCRYPT]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# CAVS 5.0
# Config info for AESSampleVectors
# CAVS 11.1
# Config info for aes_values
# AESVS KeySbox test data for CBC
# State : Encrypt and Decrypt
# Key Length : 192
# Generated on Tue Apr 11 08:47:37 2006
# Generated on Fri Apr 22 15:11:35 2011

[ENCRYPT]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# CAVS 5.0
# Config info for AESSampleVectors
# CAVS 11.1
# Config info for aes_values
# AESVS KeySbox test data for CBC
# State : Encrypt and Decrypt
# Key Length : 256
# Generated on Tue Apr 11 08:47:42 2006
# Generated on Fri Apr 22 15:11:38 2011

[ENCRYPT]

Expand Down
Loading

0 comments on commit 895de0f

Please sign in to comment.