adding a demo with sample input data
This commit is contained in:
142
test/PKCS7_test.c
Normal file
142
test/PKCS7_test.c
Normal file
@@ -0,0 +1,142 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include "./../include/PKCS7.h"
|
||||
#include "PKCS7_test.h"
|
||||
|
||||
void demonstrationPaddingOnTestInput(const uint8_t* const testDataExample, const uint16_t dataBitLength, const uint8_t BLOCK_SIZE);
|
||||
void printDescription(void);
|
||||
|
||||
int main (int argc, char* argv[])
|
||||
{
|
||||
if (argc != 3)
|
||||
{
|
||||
printDescription();
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
testDataSizeSet testDataSizeInput = atoi(argv[1]);
|
||||
paddingBlockSize blockSizeInput = atoi(argv[2]) / 8;
|
||||
|
||||
if (blockSizeInput != BLOCK_SIZE_128_BIT && blockSizeInput != BLOCK_SIZE_256_BIT)
|
||||
{
|
||||
printf(
|
||||
"\n"
|
||||
"\033[1m \033[31m"
|
||||
"\tPadding block size error (check second CLI argument -- arg[2])"
|
||||
"\033[0m"
|
||||
"\n"
|
||||
);
|
||||
printDescription();
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
switch (testDataSizeInput)
|
||||
{
|
||||
case TEST_24_BIT:
|
||||
demonstrationPaddingOnTestInput(testDataExample_24bit, testDataSizeInput, blockSizeInput);
|
||||
break;
|
||||
|
||||
case TEST_32_BIT:
|
||||
demonstrationPaddingOnTestInput(testDataExample_32bit, testDataSizeInput, blockSizeInput);
|
||||
break;
|
||||
|
||||
case TEST_64_BIT:
|
||||
demonstrationPaddingOnTestInput(testDataExample_64bit, testDataSizeInput, blockSizeInput);
|
||||
break;
|
||||
|
||||
case TEST_128_BIT:
|
||||
demonstrationPaddingOnTestInput(testDataExample_128bit, testDataSizeInput, blockSizeInput);
|
||||
break;
|
||||
|
||||
case TEST_192_BIT:
|
||||
demonstrationPaddingOnTestInput(testDataExample_192bit, testDataSizeInput, blockSizeInput);
|
||||
break;
|
||||
|
||||
case TEST_256_BIT:
|
||||
demonstrationPaddingOnTestInput(testDataExample_256bit, testDataSizeInput, blockSizeInput);
|
||||
break;
|
||||
|
||||
case TEST_384_BIT:
|
||||
demonstrationPaddingOnTestInput(testDataExample_384bit, testDataSizeInput, blockSizeInput);
|
||||
break;
|
||||
|
||||
case TEST_408_BIT:
|
||||
demonstrationPaddingOnTestInput(testDataExample_408bit, testDataSizeInput, blockSizeInput);
|
||||
break;
|
||||
|
||||
default:
|
||||
printf(
|
||||
"\n"
|
||||
"\033[1m \033[31m"
|
||||
"\tInput data size error (check first CLI argument -- arg[1])"
|
||||
"\033[0m"
|
||||
"\n"
|
||||
);
|
||||
printDescription();
|
||||
exit(-1);
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void demonstrationPaddingOnTestInput(const uint8_t* const testDataExample, const uint16_t dataBitLength, const uint8_t BLOCK_SIZE)
|
||||
{
|
||||
uint8_t testDataLength = dataBitLength / 8;
|
||||
uint8_t* testData = (uint8_t*) malloc(testDataLength);
|
||||
|
||||
memcpy(testData, testDataExample, testDataLength);
|
||||
|
||||
printf("\n************************************\n");
|
||||
printf("\nORIGINAL DATA (size is %i bytes):\n\n", testDataLength);
|
||||
for (uint8_t i = 0; i < testDataLength; i++)
|
||||
{
|
||||
printf("%x", testData[i]);
|
||||
((i + 1) % 4 == 0) ? printf("\n") : printf("\t");
|
||||
}
|
||||
printf("\n\n************************************\n");
|
||||
|
||||
PKCS7_Padding* structWithPaddingResult = addPadding(testData, testDataLength, BLOCK_SIZE);
|
||||
uint8_t* ptrToPaddingDataResult = structWithPaddingResult->dataWithPadding;
|
||||
|
||||
printf("\nWITH PADDING (now size is %li bytes):\n\n", structWithPaddingResult->dataLengthWithPadding);
|
||||
for (uint16_t i = 0; i < structWithPaddingResult->dataLengthWithPadding; i++)
|
||||
{
|
||||
printf("%x", *ptrToPaddingDataResult);
|
||||
((i + 1) % 4 == 0) ? printf("\n") : printf("\t");
|
||||
ptrToPaddingDataResult++;
|
||||
}
|
||||
printf("\n************************************\n");
|
||||
|
||||
PKCS7_unPadding* structWithUnpaddingResult = removePadding(structWithPaddingResult->dataWithPadding,
|
||||
structWithPaddingResult->dataLengthWithPadding);
|
||||
|
||||
uint8_t* ptrToUnpaddingDataResult = structWithUnpaddingResult->dataWithoutPadding;
|
||||
|
||||
printf("\nREMOVE PADDING (size is %li bytes):\n\n", structWithUnpaddingResult->dataLengthWithoutPadding);
|
||||
for (uint16_t i = 0; i < structWithUnpaddingResult->dataLengthWithoutPadding; i++)
|
||||
{
|
||||
printf("%x", *ptrToUnpaddingDataResult);
|
||||
((i + 1) % 4 == 0) ? printf("\n") : printf("\t");
|
||||
ptrToUnpaddingDataResult++;
|
||||
}
|
||||
printf("\n\n************************************\n\n");
|
||||
}
|
||||
|
||||
void printDescription(void)
|
||||
{
|
||||
printf(
|
||||
"\n"
|
||||
"\033[1m \033[32m"
|
||||
"\tFirst CLI argument (argv[1]) is size (in bits) of test data input:\n"
|
||||
"\t\tUse one of\t{24, 32, 64, 128, 192, 256, 384, 408}\n"
|
||||
"\n"
|
||||
"\tSecond CLI argument (argv[2]) is size (in bits) of padding block size:\n"
|
||||
"\t\tUse one of\t{128, 256}\n"
|
||||
"\033[0m"
|
||||
"\n\tFor example: PKCS7_test.out 24 128 or PKCS7_test.out 408 256\n"
|
||||
"\n"
|
||||
);
|
||||
}
|
||||
94
test/PKCS7_test.h
Normal file
94
test/PKCS7_test.h
Normal file
@@ -0,0 +1,94 @@
|
||||
#ifndef PKCS7_TEST_H
|
||||
#define PKCS7_TEST_H
|
||||
|
||||
typedef enum {
|
||||
TEST_24_BIT = 24,
|
||||
TEST_32_BIT = 32,
|
||||
TEST_64_BIT = 64,
|
||||
TEST_128_BIT = 128,
|
||||
TEST_192_BIT = 192,
|
||||
TEST_256_BIT = 256,
|
||||
TEST_384_BIT = 384,
|
||||
TEST_408_BIT = 408
|
||||
} testDataSizeSet;
|
||||
|
||||
|
||||
const uint8_t testDataExample_24bit[3] = {
|
||||
0xff, 0xfe, 0xfd
|
||||
};
|
||||
|
||||
const uint8_t testDataExample_32bit[4] = {
|
||||
0xff, 0xfe, 0xfd, 0xfc
|
||||
};
|
||||
|
||||
const uint8_t testDataExample_64bit[8] = {
|
||||
0xff, 0xfe, 0xfd, 0xfc,
|
||||
0xfb, 0xfa, 0xf9, 0xf8
|
||||
};
|
||||
|
||||
const uint8_t testDataExample_128bit[16] = {
|
||||
0xff, 0xfe, 0xfd, 0xfc,
|
||||
0xfb, 0xfa, 0xf9, 0xf8,
|
||||
0xf7, 0xf6, 0xf5, 0xf4,
|
||||
0xf3, 0xf2, 0xf1, 0xf0
|
||||
};
|
||||
|
||||
const uint8_t testDataExample_192bit[24] = {
|
||||
0xff, 0xfe, 0xfd, 0xfc,
|
||||
0xfb, 0xfa, 0xf9, 0xf8,
|
||||
0xf7, 0xf6, 0xf5, 0xf4,
|
||||
0xf3, 0xf2, 0xf1, 0xf0,
|
||||
|
||||
0xef, 0xee, 0xed, 0xec,
|
||||
0xeb, 0xea, 0xe9, 0xe8
|
||||
};
|
||||
|
||||
const uint8_t testDataExample_256bit[32] = {
|
||||
0xff, 0xfe, 0xfd, 0xfc,
|
||||
0xfb, 0xfa, 0xf9, 0xf8,
|
||||
0xf7, 0xf6, 0xf5, 0xf4,
|
||||
0xf3, 0xf2, 0xf1, 0xf0,
|
||||
|
||||
0xef, 0xee, 0xed, 0xec,
|
||||
0xeb, 0xea, 0xe9, 0xe8,
|
||||
0xe7, 0xe6, 0xe5, 0xe4,
|
||||
0xe3, 0xe2, 0xe1, 0xe0
|
||||
};
|
||||
|
||||
const uint8_t testDataExample_384bit[48] = {
|
||||
0xff, 0xfe, 0xfd, 0xfc,
|
||||
0xfb, 0xfa, 0xf9, 0xf8,
|
||||
0xf7, 0xf6, 0xf5, 0xf4,
|
||||
0xf3, 0xf2, 0xf1, 0xf0,
|
||||
|
||||
0xef, 0xee, 0xed, 0xec,
|
||||
0xeb, 0xea, 0xe9, 0xe8,
|
||||
0xe7, 0xe6, 0xe5, 0xe4,
|
||||
0xe3, 0xe2, 0xe1, 0xe0,
|
||||
|
||||
0xdf, 0xde, 0xdd, 0xdc,
|
||||
0xdb, 0xda, 0xd9, 0xd8,
|
||||
0xd7, 0xd6, 0xd5, 0xd4,
|
||||
0xd3, 0xd2, 0xd1, 0xd0
|
||||
};
|
||||
|
||||
const uint8_t testDataExample_408bit[51] = {
|
||||
0xff, 0xfe, 0xfd, 0xfc,
|
||||
0xfb, 0xfa, 0xf9, 0xf8,
|
||||
0xf7, 0xf6, 0xf5, 0xf4,
|
||||
0xf3, 0xf2, 0xf1, 0xf0,
|
||||
|
||||
0xef, 0xee, 0xed, 0xec,
|
||||
0xeb, 0xea, 0xe9, 0xe8,
|
||||
0xe7, 0xe6, 0xe5, 0xe4,
|
||||
0xe3, 0xe2, 0xe1, 0xe0,
|
||||
|
||||
0xdf, 0xde, 0xdd, 0xdc,
|
||||
0xdb, 0xda, 0xd9, 0xd8,
|
||||
0xd7, 0xd6, 0xd5, 0xd4,
|
||||
0xd3, 0xd2, 0xd1, 0xd0,
|
||||
|
||||
0xcf, 0xce, 0xcd
|
||||
};
|
||||
|
||||
#endif // PKCS7_TEST_H
|
||||
Reference in New Issue
Block a user