-
Notifications
You must be signed in to change notification settings - Fork 0
/
myCrypto.c
265 lines (219 loc) · 7.86 KB
/
myCrypto.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*----------------------------------------------------------------------------
Final-Project: Needham-Scroeder Protocol
FILE: myCrypto.c
Written By:
1- Adam Slattum
Submitted on: 12/3/17
----------------------------------------------------------------------------*/
#include "myCrypto.h"
#define CIPHER_LEN_MAX 1024
#define PLAINTEXT_LEN_MAX (CIPHER_LEN_MAX-16)
void handleErrors( char *msg)
{
fprintf( stderr , "%s\n" , msg ) ;
ERR_print_errors_fp(stderr);
abort();
}
//-----------------------------------------------------------------------------
int BN_write_fd(const BIGNUM *bn, int fd_out)
{
int bnSize = BN_num_bytes(bn);
if (write(fd_out, &bnSize, sizeof(int)) < 0) { //Send #of bytes
return 0;
}
unsigned char *bytes = malloc((bnSize+1) * sizeof(char));
bytes[bnSize] = '\0';
BN_bn2bin(bn, bytes);
if (write(fd_out, bytes, bnSize) < 0) { //Send bytes of BIGNUM
return 0;
}
return 1; //Success
}
//-----------------------------------------------------------------------------
BIGNUM * BN_read_fd(int fd_in)
{
int bnSize;
if (read(fd_in, &bnSize, sizeof(int)) < 0) {
return NULL;
}
unsigned char *bytes = malloc((bnSize+1) * sizeof(char));
bytes[bnSize] = '\0';
if (read(fd_in, bytes, bnSize) < 0) {
return NULL;
}
BIGNUM *bn;
bn = BN_bin2bn(bytes, bnSize, NULL);
return bn;
}
//-----------------------------------------------------------------------------
BIGNUM * BN_myRandom(const BIGNUM *p)
{
BIGNUM *bn = BN_new();
BIGNUM *two = BN_new();
BIGNUM *four = BN_new();
BIGNUM *result = BN_new();
BIGNUM *p_minus_four = BN_new();
BN_dec2bn(&two, "2");
BN_dec2bn(&four, "4");
BN_sub(p_minus_four, p, four); //p-4
BN_rand_range(bn, p_minus_four); //in range [0, p-4]
BN_add(result, bn, two); //add two to put in range [2, p-2]
return result;
}
//-----------------------------------------------------------------------------
int encrypt( unsigned char *plaintext, int plaintext_len, unsigned char *key,
unsigned char *iv, unsigned char *ciphertext )
{
EVP_CIPHER_CTX *ctx;
int len;
int ciphertext_len;
/* Create and initialise the context */
if( !(ctx = EVP_CIPHER_CTX_new()) )
handleErrors("Error");
/* Initialise the encryption operation. IMPORTANT - ensure you use a key
* and IV size appropriate for your cipher
* In this example we are using 256 bit AES (i.e. a 256 bit key). The
* IV size for *most* modes is the same as the block size. For AES this
* is 128 bits */
if( 1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv) )
handleErrors("Error");
/* Provide the message to be encrypted, and obtain the encrypted output.
* EVP_EncryptUpdate can be called multiple times if necessary
*/
if( 1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len) )
handleErrors("Error");
ciphertext_len = len;
//printf("Before EncryptFinal:\n");
//BIO_dump_fp (stdout, (const char *)ciphertext, ciphertext_len);
/* Finalise the encryption. Further ciphertext bytes may be written at
* this stage.
*/
if( 1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len) )
handleErrors("Error");
ciphertext_len += len;
//printf("After EncryptFinal:\n");
//BIO_dump_fp (stdout, (const char *)ciphertext, ciphertext_len);
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return ciphertext_len;
}
//-----------------------------------------------------------------------------
int decrypt( unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
unsigned char *iv, unsigned char *plaintext)
{
EVP_CIPHER_CTX *ctx;
int len;
int plaintext_len;
/* Create and initialise the context */
if( !(ctx = EVP_CIPHER_CTX_new()) )
handleErrors("Error");
/* Initialise the decryption operation. IMPORTANT - ensure you use a key
* and IV size appropriate for your cipher
* In this example we are using 256 bit AES (i.e. a 256 bit key). The
* IV size for *most* modes is the same as the block size. For AES this
* is 128 bits */
if( 1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv) )
handleErrors("Error");
/* Provide the message to be decrypted, and obtain the plaintext output.
* EVP_DecryptUpdate can be called multiple times if necessary
*/
if( 1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len) )
handleErrors("Error");
plaintext_len = len;
/* Finalise the decryption. Further plaintext bytes may be written at
* this stage.
*/
if( 1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len) )
handleErrors("Error");
plaintext_len += len;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return plaintext_len;
}
//-----------------------------------------------------------------------------
int encryptFile( int fd_in, int fd_out, unsigned char *key, unsigned char *iv )
{
EVP_CIPHER_CTX *ctx;
int len, ciphertext_len;
char plaintext[PLAINTEXT_LEN_MAX];
char ciphertext[CIPHER_LEN_MAX];
/* Create and initialise the context */
if( !(ctx = EVP_CIPHER_CTX_new()) )
handleErrors("Error");
/* Initialise the encryption operation. IMPORTANT - ensure you use a key
* and IV size appropriate for your cipher
* In this example we are using 256 bit AES (i.e. a 256 bit key). The
* IV size for *most* modes is the same as the block size. For AES this
* is 128 bits */
if( 1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv) )
handleErrors("Error");
/* Continuously loop until no more plaintext can be read,
encrypting along the way
*/
while ( 1 )
{
int nbytes = read( fd_in, plaintext, PLAINTEXT_LEN_MAX ) ;
if ( nbytes <= 0 )
break ;
/* Provide the message to be encrypted, and obtain the encrypted output.*/
if( 1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, nbytes ) )
handleErrors("Error");
ciphertext_len += len;
write( fd_out, (const char *) ciphertext, len );
memset( ciphertext, 0, sizeof(ciphertext) );
memset( plaintext, 0, sizeof(plaintext) );
}
/* Finalise the encryption. Further ciphertext bytes may be written at
* this stage.
*/
if( 1 != EVP_EncryptFinal_ex(ctx, ciphertext, &len) )
handleErrors("Error");
write( fd_out, (const char*) ciphertext, len );
ciphertext_len += len;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return ciphertext_len;
}
//-----------------------------------------------------------------------------
int decryptFile( int fd_in, int fd_out, unsigned char *key, unsigned char *iv )
{
EVP_CIPHER_CTX *ctx;
int len, plaintext_len;
char plaintext[PLAINTEXT_LEN_MAX];
char ciphertext[CIPHER_LEN_MAX];
/* Create and initialise the context */
if( !(ctx = EVP_CIPHER_CTX_new()) )
handleErrors("Error");
/* Initialise the decryption operation. IMPORTANT - ensure you use a key
* and IV size appropriate for your cipher
* In this example we are using 256 bit AES (i.e. a 256 bit key). The
* IV size for *most* modes is the same as the block size. For AES this
* is 128 bits */
if( 1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv) )
handleErrors("Error");
/* Provide the message to be decrypted, and obtain the plaintext output.
* EVP_DecryptUpdate can be called multiple times if necessary
*/
while ( 1 )
{
int nbytes = read( fd_in, ciphertext, CIPHER_LEN_MAX) ;
if ( nbytes <= 0 )
break ;
if ( 1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, nbytes ) )
handleErrors("Error");
plaintext_len += len;
write( fd_out, plaintext, len ) ;
memset( ciphertext, 0, sizeof(ciphertext) ) ;
memset( plaintext, 0, sizeof(plaintext) ) ;
}
/* Finalise the decryption. Further plaintext bytes may be written at
* this stage.
*/
if( 1 != EVP_DecryptFinal_ex(ctx, plaintext, &len) )
handleErrors("Error");
write( fd_out, plaintext, len ) ;
plaintext_len += len;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return plaintext_len;
}