-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathexample.c
More file actions
124 lines (97 loc) · 2.95 KB
/
example.c
File metadata and controls
124 lines (97 loc) · 2.95 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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <fpe.h>
void hex2chars(unsigned char hex[], unsigned char result[])
{
int len = strlen(hex);
unsigned char temp[3];
temp[2] = 0x00;
int j = 0;
for (int i = 0; i < len; i += 2) {
temp[0] = hex[i];
temp[1] = hex[i + 1];
result[j] = (char)strtol(temp, NULL, 16);
++j;
}
}
void map_chars(unsigned char str[], unsigned int result[])
{
int len = strlen(str);
for (int i = 0; i < len; ++i)
if (str[i] >= 'a')
result[i] = str[i] - 'a' + 10;
else
result[i] = str[i] - '0';
}
void inverse_map_chars(unsigned result[], unsigned char str[], int len)
{
for (int i = 0; i < len; ++i)
if (result[i] < 10)
str[i] = result[i] + '0';
else
str[i] = result[i] - 10 + 'a';
str[len] = 0x00;
}
int main(int argc, char *argv[])
{
if (argc != 5) {
printf("Usage: %s <key> <tweak> <radix> <plaintext>\n", argv[0]);
return 0;
}
unsigned char k[100],
t[100],
result[100];
int xlen = strlen(argv[4]),
klen = strlen(argv[1]) / 2,
tlen = strlen(argv[2]) / 2,
radix = atoi(argv[3]);
unsigned int x[100],
y[xlen];
unsigned int tmp;
hex2chars(argv[1], k);
hex2chars(argv[2], t);
map_chars(argv[4], x);
for (int i = 0; i < xlen; ++i)
assert(x[i] < radix);
FPE_KEY ff1, ff3;
printf("key:");
for (int i = 0; i < klen; ++i) printf(" %02x", k[i]);
puts("");
if (tlen) printf("tweak:");
for (int i = 0; i < tlen; ++i) printf(" %02x", t[i]);
if (tlen) puts("");
FPE_set_ff1_key(k, klen * 8, t, tlen, radix, &ff1);
FPE_set_ff3_key(k, klen * 8, t, radix, &ff3);
printf("after map: ");
for (int i = 0; i < xlen; ++i) printf(" %d", x[i]);
printf("\n\n");
printf("========== FF1 ==========\n");
FPE_ff1_encrypt(x, y, xlen, &ff1, FPE_ENCRYPT);
printf("ciphertext(numeral string):");
for (int i = 0; i < xlen; ++i) printf(" %d", y[i]);
printf("\n");
inverse_map_chars(y, result, xlen);
printf("ciphertext: %s\n\n", result);
memset(x, 0, sizeof(x));
FPE_ff1_encrypt(y, x, xlen, &ff1, FPE_DECRYPT);
printf("plaintext:");
for (int i = 0; i < xlen; ++i) printf(" %d", x[i]);
printf("\n\n");
printf("========== FF3 ==========\n");
FPE_ff3_encrypt(x, y, xlen, &ff3, FPE_ENCRYPT);
printf("ciphertext(numeral string):");
for (int i = 0; i < xlen; ++i) printf(" %d", y[i]);
printf("\n");
inverse_map_chars(y, result, xlen);
printf("ciphertext: %s\n\n", result);
memset(x, 0, sizeof(x));
FPE_ff3_encrypt(y, x, xlen, &ff3, FPE_DECRYPT);
printf("plaintext:");
for (int i = 0; i < xlen; ++i) printf(" %d", x[i]);
printf("\n");
FPE_unset_ff1_key(&ff1);
FPE_unset_ff3_key(&ff3);
return 0;
}