summaryrefslogtreecommitdiff
path: root/idea.c
diff options
context:
space:
mode:
Diffstat (limited to 'idea.c')
-rw-r--r--idea.c45
1 files changed, 24 insertions, 21 deletions
diff --git a/idea.c b/idea.c
index 2bc7f9b..bd8d539 100644
--- a/idea.c
+++ b/idea.c
@@ -20,30 +20,32 @@
**
*/
+/* Following constant defines the max number of loops the
+** system will attempt. Keeps things from going off into the
+** weeds. */
+/*#define LOOP_MAX 50000L*/
+#define LOOP_MAX 500000L
+
/*
-** DEFINES
+** Following constant sets the size of the arrays.
+** NOTE: For the IDEA algorithm to work properly, this
+** number MUST be some multiple of 8.
*/
+#define ARRAY_SIZE 4000
+
#define IDEAKEYSIZE 16
#define IDEABLOCKSIZE 8
#define ROUNDS 8
#define KEYLEN (6*ROUNDS+4)
-/*
-** MACROS
-*/
#define low16(x) ((x) & 0x0FFFF)
#define MUL(x,y) (x=mul(low16(x),y))
-
typedef uint16_t IDEAkey[KEYLEN];
-/*
-** PROTOTYPES
-*/
static clock_t DoIDEAIteration(unsigned char *plain1,
unsigned char *crypt1, unsigned char *plain2,
- unsigned long arraysize, unsigned long nloops,
- IDEAkey Z, IDEAkey DK);
+ unsigned long nloops, IDEAkey Z, IDEAkey DK);
static uint16_t mul(register uint16_t a, register uint16_t b);
static uint16_t inv(uint16_t x);
static void en_key_idea(uint16_t userkey[8], IDEAkey Z);
@@ -70,6 +72,7 @@ DoIDEA(void)
uint16_t userkey[8];
int i;
static int is_adjusted = FALSE;
+ static int loops = 100;
randnum(3);
@@ -96,20 +99,20 @@ DoIDEA(void)
** So, plain1 and plain2 should match.
** Also, fill up plain1 with sample text.
*/
- plain1 = malloc(locideastruct->arraysize);
+ plain1 = malloc(ARRAY_SIZE);
if (!plain1) {
fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context);
exit(1);
}
- crypt1 = malloc(locideastruct->arraysize);
+ crypt1 = malloc(ARRAY_SIZE);
if (!crypt1) {
fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context);
free(plain1);
exit(1);
}
- plain2 = malloc(locideastruct->arraysize);
+ plain2 = malloc(ARRAY_SIZE);
if (!plain2) {
fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context);
free(plain1);
@@ -120,7 +123,7 @@ DoIDEA(void)
** Note that we build the "plaintext" by simply loading
** the array up with random numbers.
*/
- for (i = 0;i < locideastruct->arraysize; i++) {
+ for (i = 0;i < ARRAY_SIZE; i++) {
plain1[i] = (unsigned char)(abs_randwc(255) & 0xFF);
}
@@ -134,8 +137,8 @@ DoIDEA(void)
** # of loops and increasing the loop count until we
** get a number of loops that we can use.
*/
- for (locideastruct->loops = 100; locideastruct->loops < MAXIDEALOOPS; locideastruct->loops += 10) {
- if (DoIDEAIteration(plain1, crypt1, plain2, locideastruct->arraysize, locideastruct->loops, Z, DK) > MINIMUM_TICKS) {
+ for (; loops < LOOP_MAX; loops += 10) {
+ if (DoIDEAIteration(plain1, crypt1, plain2, loops, Z, DK) > MINIMUM_TICKS) {
break;
}
}
@@ -146,8 +149,8 @@ DoIDEA(void)
*/
do {
- total_time += DoIDEAIteration(plain1, crypt1, plain2, locideastruct->arraysize, locideastruct->loops, Z, DK);
- iterations += locideastruct->loops;
+ total_time += DoIDEAIteration(plain1, crypt1, plain2, loops, Z, DK);
+ iterations += loops;
} while (total_time < MINIMUM_SECONDS * CLOCKS_PER_SEC);
free(plain1);
@@ -165,7 +168,7 @@ DoIDEA(void)
** decryption.
*/
static clock_t
-DoIDEAIteration(unsigned char *plain1, unsigned char *crypt1, unsigned char *plain2, unsigned long arraysize, unsigned long nloops, IDEAkey Z, IDEAkey DK)
+DoIDEAIteration(unsigned char *plain1, unsigned char *crypt1, unsigned char *plain2, unsigned long nloops, IDEAkey Z, IDEAkey DK)
{
clock_t start, stop;
register unsigned long i;
@@ -174,12 +177,12 @@ DoIDEAIteration(unsigned char *plain1, unsigned char *crypt1, unsigned char *pla
start = clock();
for (i = 0; i < nloops; i++) {
- for (j = 0; j < arraysize; j += sizeof(uint16_t) * 4) {
+ for (j = 0; j < ARRAY_SIZE; j += sizeof(uint16_t) * 4) {
cipher_idea((uint16_t *)(plain1 + j), (uint16_t *)(crypt1 + j), Z); /* Encrypt */
}
}
- for (j = 0; j < arraysize; j += sizeof(uint16_t) * 4) {
+ for (j = 0; j < ARRAY_SIZE; j += sizeof(uint16_t) * 4) {
cipher_idea((uint16_t *)(crypt1 + j), (uint16_t *)(plain2 + j), DK); /* Decrypt */
}