121 lines
2.9 KiB
C
Executable File
121 lines
2.9 KiB
C
Executable File
/*
|
|
* Demo on how to use /dev/crypto device for ciphering.
|
|
*
|
|
* Placed under public domain.
|
|
*
|
|
*/
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
#include <sys/ioctl.h>
|
|
#include "tcrypt.h"
|
|
#include "fh_aes_mpi.h"
|
|
|
|
char plaintext_raw[1024] __attribute__((aligned(16)));
|
|
char ciphertext_raw[1024] __attribute__((aligned(16)));
|
|
|
|
static int test_aes(void)
|
|
{
|
|
FH_UINT16 alignmask;
|
|
struct cipher_test_info *cipher_test;
|
|
const struct cipher_testvec *testvec;
|
|
char *plaintext, *ciphertext;
|
|
unsigned int cipher_len, block_size;
|
|
|
|
unsigned int i , j;
|
|
|
|
for(i = 0; i < ARRAY_SIZE(cipher_test_list); i++) {
|
|
|
|
cipher_test = &cipher_test_list[i];
|
|
testvec = cipher_test->vecs;
|
|
|
|
for(j = 0; j < cipher_test->count; j++) {
|
|
|
|
FH_AES_Init(&alignmask, testvec[j].key, testvec[j].klen, cipher_test->crypt);
|
|
|
|
if (alignmask) {
|
|
plaintext = (char *)(((unsigned long)plaintext_raw + alignmask) & ~alignmask);
|
|
ciphertext = (char *)(((unsigned long)ciphertext_raw + alignmask) & ~alignmask);
|
|
} else {
|
|
plaintext = plaintext_raw;
|
|
ciphertext = ciphertext_raw;
|
|
}
|
|
memset(plaintext, 0 , testvec[j].len);
|
|
memset(ciphertext, 0 , testvec[j].len);
|
|
memcpy(plaintext, testvec[j].ptext, testvec[j].len);
|
|
|
|
block_size = alignmask + 1;
|
|
cipher_len = (testvec[j].len + alignmask) / block_size * block_size ;
|
|
/* Encrypt data.in to data.encrypted */
|
|
FH_AES_Encrypt(testvec[j].iv, plaintext, ciphertext, cipher_len);
|
|
|
|
/* Verify the result */
|
|
if (memcmp(ciphertext, testvec[j].ctext, testvec[j].len) != 0) {
|
|
int k;
|
|
fprintf(stderr,
|
|
"FAIL: Encrypted data are different from the expect cipher data.\n");
|
|
// printf("expect ciphertext:");
|
|
for (k = 0; k < testvec[j].len; k++) {
|
|
printf("%02x ", testvec[j].ctext[k]);
|
|
}
|
|
printf("encode ciphertext:");
|
|
for (k = 0; k < testvec[j].len; k++) {
|
|
//printf("%02x ", ciphertext[k]);
|
|
}
|
|
printf("\n");
|
|
return 1;
|
|
|
|
}
|
|
|
|
memset(plaintext, 0 , testvec[j].len);
|
|
memset(ciphertext, 0 , testvec[j].len);
|
|
memcpy(ciphertext, testvec[j].ctext, testvec[j].len);
|
|
|
|
/* Encrypt data.in to data.encrypted */
|
|
FH_AES_Decrypt(testvec[j].iv, ciphertext, plaintext, cipher_len);
|
|
|
|
/* Verify the result */
|
|
if (memcmp(plaintext, testvec[j].ptext, testvec[j].len) != 0) {
|
|
int k;
|
|
fprintf(stderr,
|
|
"FAIL: Decrypted data are different from the expect plain data.\n");
|
|
printf("expect plaintext:");
|
|
for (k = 0; k < testvec[j].len; k++) {
|
|
// printf("%02x ", testvec[j].ptext[k]);
|
|
}
|
|
//printf("decode plaintext:");
|
|
for (k = 0; k < testvec[j].len; k++) {
|
|
// printf("%02x ", plaintext[k]);
|
|
}
|
|
//printf("\n");
|
|
return 1;
|
|
|
|
}
|
|
|
|
FH_AES_Deinit();
|
|
}
|
|
}
|
|
printf("AES Test passed\n");
|
|
|
|
return 0;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
/* Open the crypto device */
|
|
FH_AES_Open();
|
|
|
|
/* Run the test itself */
|
|
if (test_aes())
|
|
return 1;
|
|
|
|
/* Close the original descriptor */
|
|
FH_AES_Close();
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
|