aes_encode.c
3.25 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef unsigned char u8;
#include "aes_api.h"
#define BLOCK_SIZE 128
int asciiToBin(char *str,int bytes,u8 *buf)
{
int i,j;
char t;
for(i=0;i<bytes;i++){
t = str[2*i];
if ((t >= '0') && (t <= '9'))
j = (t - '0') << 4;
else if ((t >= 'a') && (t <= 'f'))
j = (t - 'a' + 10) << 4;
else if ((t >= 'A') && (t <= 'F'))
j = (t - 'A' + 10) << 4;
else return -1;
t = str[2*i+1];
if ((t >= '0') && (t <= '9'))
j ^= (t - '0');
else if ((t >= 'a') && (t <= 'f'))
j ^= (t - 'a' + 10);
else if ((t >= 'A') && (t <= 'F'))
j ^= (t - 'A' + 10);
else return -1;
buf[i]=j;
}
return 0;
}
int
main(int argc, char* argv[]) {
FILE *inptr = stdin;
FILE *outptr = stdout;
AesKeyInstance key;
int keyLen, error;
u8 iv[BLOCK_SIZE/8] ={ 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
u8 keyMaterial[BLOCK_SIZE/8] = {0xab,0xcd,0xef,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67,0x89};
unsigned char buf[BLOCK_SIZE/8], obuf[BLOCK_SIZE/8];
AesCipherInstance cipher;
int decode, print = 0;
const char *prog = argv[1];
decode = argc > 1 && strstr(prog, "aes_decode") != 0;
keyLen = 128;
while(argc > 1 && argv[1][0] == '-') {
switch(argv[1][1]) {
case 'k':
if (argc <= 2) goto usage;
asciiToBin(argv[2],16,keyMaterial);
//strcpy(keyMaterial, argv[2]);
argc--; argv++;
break;
case 'i':
if (argc <= 2) goto usage;
asciiToBin(argv[2],16,iv);
//strcpy(iv, argv[2]);
argc--; argv++;
break;
case 'd':
decode = 1;
break;
case 'D':
decode = 2;
break;
case 'p':
print = 1;
break;
default:
usage:
fprintf(stderr, "Usage: %s [-d|-D] [-p] [-k key] [-i iv] [infile [outfile]]\n", prog);
exit(1);
}
argc--; argv++;
}
if (argc > 1) {
if ((inptr = fopen(argv[1], "rb")) == NULL) {
perror(argv[1]);
exit(1);
}
}
if (argc > 2) {
if ((outptr = fopen(argv[2], "wb")) == NULL) {
perror(argv[2]);
exit(1);
}
}
error = aesMakeKey(&key, decode ? AES_DIR_DECRYPT : AES_DIR_ENCRYPT,
keyLen, keyMaterial);
if (error != AES_TRUE) {
fprintf(stderr, "error in computing key %d\n", error);
exit(1);
}
if (print) {
int x, y, z;
for (x = 0; x < 11; x++)
for(y = 0; y < 4; y++) {
fprintf(stderr, "%08x", key.rk[x*4+y]);
}
fprintf(stderr, "\n");
}
if ((error = aesCipherInit (&cipher, AES_MODE_CBC, iv)) != AES_TRUE) {
fprintf(stderr,"cipherInit error %d \n", error);
exit(1);
}
memset(buf, 0, sizeof buf);
while (fread(buf, 1, (sizeof buf), inptr) > 0) {
if (decode == 1)
error = aesBlockDecrypt(&cipher, &key, buf, sizeof(buf)*8, obuf);
else
error = aesBlockEncrypt(&cipher, &key, buf, sizeof(buf)*8, obuf);
if (error <= 0) {
fprintf(stderr, "cipher encryption error %d\n", error);
exit(1);
} else if (fwrite(obuf, (sizeof obuf), 1, outptr) != 1) {
perror("fwrite");
exit(1);
}
memset(buf, 0, sizeof buf);
}
return 0;
}