-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaes2.c
243 lines (206 loc) · 7.72 KB
/
aes2.c
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <openssl/bn.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/aes.h>
#include <openssl/sha.h>
#include <openssl/rand.h>
#define ERR_EVP_CIPHER_INIT -1
#define ERR_EVP_CIPHER_UPDATE -2
#define ERR_EVP_CIPHER_FINAL -3
#define ERR_EVP_CTX_NEW -4
#define AES_256_KEY_SIZE 32
#define AES_BLOCK_SIZE 16
#define BUFSIZE 1024
typedef struct _cipher_params_t{
unsigned char *key;
unsigned char *iv;
unsigned int encrypt;
const EVP_CIPHER *cipher_type;
}cipher_params_t;
// void cleanup(cipher_params_t *params, FILE *ifp, FILE *ofp, int rc){
// free(params);
// fclose(ifp);
// fclose(ofp);
// exit(rc);
// }
void file_encrypt_decrypt( FILE *ifp, FILE *ofp, unsigned int encrypt,unsigned char *key, unsigned char *iv){
if(encrypt) printf("encrypting...\n");
else printf("decrypting..\n");
/* Allow enough space in output buffer for additional block */
int cipher_block_size = EVP_CIPHER_block_size(EVP_aes_256_cbc());
unsigned char in_buf[BUFSIZE], out_buf[BUFSIZE + cipher_block_size];
int num_bytes_read, out_len;
EVP_CIPHER_CTX *ctx;
ctx = EVP_CIPHER_CTX_new();
if(ctx == NULL){
fprintf(stderr, "ERROR: EVP_CIPHER_CTX_new failed. OpenSSL error: %s\n", ERR_error_string(ERR_get_error(), NULL));
exit(-4);
// cleanup(params, ifp, ofp, ERR_EVP_CTX_NEW);
}
/* Don't set key or IV right away; we want to check lengths */
if(!EVP_CipherInit_ex(ctx, EVP_aes_256_cbc(), NULL, NULL, NULL, encrypt)){
fprintf(stderr, "ERROR: EVP_CipherInit_ex failed. OpenSSL error: %s\n", ERR_error_string(ERR_get_error(), NULL));
exit(-1);
// cleanup(params, ifp, ofp, ERR_EVP_CIPHER_INIT);
}
OPENSSL_assert(EVP_CIPHER_CTX_key_length(ctx) == AES_256_KEY_SIZE);
OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) == AES_BLOCK_SIZE);
/* Now we can set key and IV */
if(!EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, encrypt)){
fprintf(stderr, "ERROR: EVP_CipherInit_ex failed. OpenSSL error: %s\n", ERR_error_string(ERR_get_error(), NULL));
EVP_CIPHER_CTX_cleanup(ctx);
exit(-1);
// cleanup(params, ifp, ofp, ERR_EVP_CIPHER_INIT);
}
while(1){
// Read in data in blocks until EOF. Update the ciphering with each read.
num_bytes_read = fread(in_buf, sizeof(unsigned char), BUFSIZE, ifp);
printf("input buffer size1: %d\n", num_bytes_read);
if (ferror(ifp)){
fprintf(stderr, "ERROR: fread error: %s\n", strerror(errno));
EVP_CIPHER_CTX_cleanup(ctx);
exit(-1);
// cleanup(params, ifp, ofp, errno);
}
if(!EVP_CipherUpdate(ctx, out_buf, &out_len, in_buf, num_bytes_read)){
fprintf(stderr, "ERROR: EVP_CipherUpdate failed. OpenSSL error: %s\n", ERR_error_string(ERR_get_error(), NULL));
EVP_CIPHER_CTX_cleanup(ctx);
exit(-2);
// cleanup(params, ifp, ofp, ERR_EVP_CIPHER_UPDATE);
}
printf("out buff : %s\n", out_buf);
fwrite(out_buf, sizeof(unsigned char), out_len, ofp);
// for(int i=0;i<out_len;i++)
// printf("%c ", out_buf[i]);
if (ferror(ofp)) {
fprintf(stderr, "ERROR: fwrite error: %s\n", strerror(errno));
EVP_CIPHER_CTX_cleanup(ctx);
exit(-1);
// cleanup(params, ifp, ofp, errno);
}
if (num_bytes_read < BUFSIZE) {
/* Reached End of file */
break;
}
}
/* Now cipher the final block and write it out to file */
if(!EVP_CipherFinal_ex(ctx, out_buf, &out_len)){
fprintf(stderr, "ERROR: EVP_CipherFinal_ex failed. OpenSSL error: %s\n", ERR_error_string(ERR_get_error(), NULL));
EVP_CIPHER_CTX_cleanup(ctx);
exit(-3);
// cleanup(params, ifp, ofp, ERR_EVP_CIPHER_FINAL);
}
fwrite(out_buf, sizeof(unsigned char), out_len, ofp);
if (ferror(ofp)) {
fprintf(stderr, "ERROR: fwrite error: %s\n", strerror(errno));
EVP_CIPHER_CTX_cleanup(ctx);
exit(-1);
// cleanup(params, ifp, ofp, errno);
}
EVP_CIPHER_CTX_cleanup(ctx);
}
int main(int argc, char *argv[]) {
FILE *f_input, *f_enc, *f_dec;
/* Make sure user provides the input file */
if (argc != 2) {
printf("Usage: %s /path/to/file\n", argv[0]);
return -1;
}
char *in_file = argv[1];
// char *out_file =
// cipher_params_t *params = (cipher_params_t *)malloc(sizeof(cipher_params_t));
// if (!params) {
// /* Unable to allocate memory on heap*/
// fprintf(stderr, "ERROR: malloc error: %s\n", strerror(errno));
// return errno;
// }
/* Key to use for encrpytion and decryption */
unsigned char key[32];
/* Initialization Vector */
unsigned char iv[16];
/* Generate cryptographically strong pseudo-random bytes for key and IV */
if (!RAND_bytes(key, sizeof(key)) || !RAND_bytes(iv, sizeof(iv))) {
/* OpenSSL reports a failure, act accordingly */
fprintf(stderr, "ERROR: RAND_bytes error: %s\n", strerror(errno));
return errno;
}
printf("size of key: %d\n", sizeof(key));
for(int i=0;i<32;i++){
printf("%d ", key[i] );
}
printf("\n");
// unsigned char* KEY1 = key;
// BN_CTX *ctx = BN_CTX_new();
BIGNUM *Mon= BN_bin2bn(key, sizeof(key), NULL);
BN_print_fp(stdout, Mon);printf("\n");
unsigned char *to = malloc((BN_num_bytes(Mon) +1)*sizeof(char));
BN_bn2bin(Mon, to);
for(int i=0;i<32;i++){
printf("%d ", to[i] );
}
printf("\n");
// printf("\nkey: %s\n", to);
return 0;
// printf("iv: %s\n", iv);
unsigned char *KEY = key;
unsigned char *IV = iv;
BN_CTX *ctx = BN_CTX_new();
BIGNUM *M = BN_new();
BN_dec2bn(&M,KEY);
BN_print(stdin, M);
return 0;
/* Indicate that we want to encrypt */
unsigned int encrypt = 1;
/* Set the cipher type you want for encryption-decryption */
// params->cipher_type = EVP_aes_256_cbc();
/* Open the input file for reading in binary ("rb" mode) */
f_input = fopen(in_file, "rb");
if (!f_input) {
/* Unable to open file for reading */
fprintf(stderr, "ERROR: fopen error: %s\n", strerror(errno));
return errno;
}
// char ch;
// while((ch = fgetc(f_input)) != EOF)
// printf("%c", ch);
/* Open and truncate file to zero length or create ciphertext file for writing */
f_enc = fopen("encrypted_file", "wb");
if (!f_enc) {
/* Unable to open file for writing */
fprintf(stderr, "ERROR: fopen error: %s\n", strerror(errno));
return errno;
}
/* Encrypt the given file */
file_encrypt_decrypt(f_input, f_enc, encrypt, KEY, IV);
/* Encryption done, close the file descriptors */
fclose(f_input);
fclose(f_enc);
/* Decrypt the file */
/* Indicate that we want to decrypt */
encrypt = 0;
/* Open the encrypted file for reading in binary ("rb" mode) */
f_input = fopen("encrypted_file", "rb");
if (!f_input) {
/* Unable to open file for reading */
fprintf(stderr, "ERROR: fopen error: %s\n", strerror(errno));
return errno;
}
/* Open and truncate file to zero length or create decrypted file for writing */
f_dec = fopen("decrypted_file", "wb");
if (!f_dec) {
/* Unable to open file for writing */
fprintf(stderr, "ERROR: fopen error: %s\n", strerror(errno));
return errno;
}
/* Decrypt the given file */
file_encrypt_decrypt(f_input, f_dec, encrypt, KEY, IV);
/* Close the open file descriptors */
fclose(f_input);
fclose(f_dec);
/* Free the memory allocated to our structure */
return 0;
}