-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathmain.c
More file actions
144 lines (120 loc) · 4.13 KB
/
main.c
File metadata and controls
144 lines (120 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* KeeLoq Cipher Implementation
* Author: H. Hadipour
*
* Usage:
* ./keeloq - Demo encryption/decryption
* ./keeloq speed - Run encryption speed benchmark
* ./keeloq polygen - Generate polynomial equations
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "keeloq.h"
#include "speed.h"
#include "attacks/polygen.h"
#define DEFAULT_ROUNDS 528
#define DEFAULT_KEY 0x5CEC6701B79FD949ULL
void print_usage(const char *prog) {
printf("KeeLoq Cipher Implementation\n\n");
printf("Usage:\n");
printf(" %s Demo encryption/decryption\n", prog);
printf(" %s speed Run encryption speed benchmark\n", prog);
printf(" %s polygen Generate polynomial equations (to mqkeeloq.txt)\n", prog);
printf("\n");
}
void demo_encrypt_decrypt(void) {
uint64_t key = DEFAULT_KEY;
uint32_t plaintext = 0xF741E2DB;
uint32_t ciphertext;
int rounds = DEFAULT_ROUNDS;
printf("KeeLoq Encryption/Decryption Demo\n");
printf("==================================\n");
printf("Key: 0x%016llX\n", (unsigned long long)key);
printf("Rounds: %d\n", rounds);
printf("Plaintext: 0x%08X\n\n", plaintext);
keeloq_encrypt(&key, &plaintext, &ciphertext, rounds);
printf("After encryption: 0x%08X\n", ciphertext);
uint32_t decrypted;
keeloq_decrypt(&key, &decrypted, &ciphertext, rounds);
printf("After decryption: 0x%08X\n", decrypted);
if (decrypted == plaintext) {
printf("\nVerification: PASSED\n");
} else {
printf("\nVerification: FAILED\n");
}
}
void run_speed_benchmark(void) {
int rounds = DEFAULT_ROUNDS;
double rate;
printf("KeeLoq Speed Benchmark\n");
printf("======================\n");
printf("Rounds: %d\n", rounds);
printf("Running benchmark...\n\n");
rate = speed(rounds);
printf("Encryption speed: %.2f MB/s\n", rate / 1000000.0);
}
void generate_equations(void) {
uint64_t key = DEFAULT_KEY;
int rounds = 32; /* Default for equation generation */
int num_pairs = 1;
printf("KeeLoq Polynomial Equation Generator\n");
printf("====================================\n");
printf("Enter number of rounds: ");
scanf("%d", &rounds);
printf("Enter number of P/C pairs: ");
scanf("%d", &num_pairs);
if (num_pairs < 1) num_pairs = 1;
if (rounds < 1) rounds = 1;
/* Allocate arrays */
uint32_t *plaintexts = malloc(num_pairs * sizeof(uint32_t));
uint32_t *ciphertexts = malloc(num_pairs * sizeof(uint32_t));
/* Generate random plaintexts and encrypt */
srand((unsigned)time(NULL));
for (int i = 0; i < num_pairs; i++) {
plaintexts[i] = (uint32_t)rand();
keeloq_encrypt(&key, &plaintexts[i], &ciphertexts[i], rounds);
}
/* Generate equations */
uint64_t num_eqs = calculate_num_of_equations(rounds, num_pairs);
polynomial *equations = malloc(num_eqs * sizeof(polynomial));
polynomials(plaintexts, ciphertexts, equations, rounds, num_pairs);
/* Write to file */
FILE *fp = fopen("mqkeeloq.txt", "w");
if (!fp) {
fprintf(stderr, "Error: Cannot open mqkeeloq.txt for writing\n");
free(plaintexts);
free(ciphertexts);
free(equations);
return;
}
for (uint64_t i = 0; i < num_eqs; i++) {
fprintf(fp, "%s\n", equations[i].poly);
}
fclose(fp);
printf("\nGenerated %llu equations for %d rounds, %d P/C pairs\n",
(unsigned long long)num_eqs, rounds, num_pairs);
printf("Output written to: mqkeeloq.txt\n");
free(plaintexts);
free(ciphertexts);
free(equations);
}
int main(int argc, char *argv[]) {
if (argc < 2) {
demo_encrypt_decrypt();
return 0;
}
if (strcmp(argv[1], "speed") == 0) {
run_speed_benchmark();
} else if (strcmp(argv[1], "polygen") == 0) {
generate_equations();
} else if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
print_usage(argv[0]);
} else {
fprintf(stderr, "Unknown command: %s\n\n", argv[1]);
print_usage(argv[0]);
return 1;
}
return 0;
}