-
Notifications
You must be signed in to change notification settings - Fork 9
/
rncryptor_c.h
595 lines (546 loc) · 17.4 KB
/
rncryptor_c.h
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
#ifndef RNCRYPTOR_C_H
#define RNCRYPTOR_C_H 1
/**
@mainpage
@section intro Introduction
A C implementation of Rob Napier's Objective-C library RNCryptor data
format specificatoin v 3.
RNCryptor homepage: https://github.com/RNCryptor/RNCryptor
RNCryptor-C homepage: https://github.com/RNCryptor/RNCryptor-C
*/
/**
* @file rncryptor_c.h
* @brief Function Prototypes
*
* This file contains the function prototypes for rncryptorc API functions.
*
* @author Muhammad Muquit http://www.muquit.com/
*/
#undef SUCCESS
#undef FAILURE
#define MCFL __FILE__,__LINE__
#define MJL __LINE__
#define RNCRYPTORC_VERSION_S "1.06"
#define RNCRYPTOR_DATA_FORMAT_VERSION 0x03
#define RNCRYPTOR3_KDF_ITER 10000
#define RNCRYPTOR_URL "https://github.com/RNCryptor/RNCryptor"
#define RNCRYPTORC_URL "https://github.com/RNCryptor/RNCryptor-C"
#define SUCCESS 0x00
#define FAILURE 0x01
#include <stdio.h>
#if STDC_HEADERS || HAVE_STRING_H
#include <string.h> /* ANSI string.h and pre-ANSI memory.h might conflict*/
#if !STDC_HEADERS && HAVE_MEMORY_H
#include <memory.h>
#endif
#else
#if HAVE_STRINGS_H
#include <strings.h>
#endif
#endif
#if HAVE_CTYPE_H
#include <ctype.h>
#endif
#if HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#if UNIX
#include <sys/types.h>
#include <sys/stat.h>
#endif
#ifdef WINNT
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <share.h>
#include <direct.h>
#include <Winsock2.h> /* for timeval */
#define ftruncate chsize
#ifdef getcwd
#undef getcwd
#endif
#define getcwd _getcwd
#ifdef snprintf
#undef snprintf
#endif
#define snprintf _snprintf
#endif
#if HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <stdarg.h>
#if HAVE_FCNTL_H
#ifndef O_RDONLY /* prevent multiple inclusion on lame systems (from vile)*/
#include <fcntl.h>
#endif
#endif
#if HAVE_MALLOC_H
#include <malloc.h>
#endif
#ifdef HAVE_SYS_FILE_H
#include <sys/file.h>
#endif
#ifdef TIME_WITH_SYS_TIME
#include <sys/time.h>
#include <time.h>
#else
#if HAVE_SYS_TIME_H
#include <sys/time.h>
#else
#include <time.h>
#endif
#endif
#ifdef APR_HAVE_LIMITS_H
#include <limits.h>
#else
#if APR_HAVE_SYS_SYSLIMITS_H
#include <sys/syslimits.h>
#endif
#endif
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include <openssl/rand.h>
/* latest documentation is online: https://github.com/RNCryptor/RNCryptor-C */
/*
** Turn on/off debug messages. Default is off
**
** Parameters:
** d Debug value. 1 or 0. To print the debug messages to stdout,
** call the function with 1 before calling any API
**
** Return Values:
** None
**
** Side Effects:
** none
**
** Comments:
** Just a Helper function
**
** Development History:
** muquit@muquit.com May-20-2015 - first cut
*/
void rncryptorc_set_debug(int d);
/*
** Encrypt a file with a password. Encryption salt, HMAC salt and IV are
** auto generaed
**
** Parameters:
** infile_path Path of the input file, can not be empty
** kdf_iter PBKDF2 iterations. Must Pass RNCRYPTOR3_KDF_ITER for RNCryptor
** data format sepc v3
** password Password for encryption, can not be empty
** password_len Length of the password
** outdata_len Returns. Length of the returned encrypted data
** errbuf Buffer to write error to
** errbuf_len Length of errbuf
**
** Return Values:
** Pointer to encrypted data on success, NULL on failure
** In case of failure errbuf will have the error message
**
** Side Effects:
** Memory is allocated for returned data. It is caller's responsibility to free it.
**
** Comments:
** The encryption is done as per RNCryptor data format specification v3.
**
** Development History:
** muquit@muquit.com May-20-2015 - first cut
*/
unsigned char *rncryptorc_encrypt_file_with_password(const char *infile_path,
int kdf_iter,
const char *password,
int password_length,
int *outdata_len,
char *errbuf,
int errbuf_len);
/*
** Encrypt a file with a encryption key. HMAC key is also requried for
** creating the HMAC-SHA256 digest. IV is augo generated.
**
** Parameters:
** infile_path Path of the input file. Required.
** kdf_iter PBKDF2 iterations. Must Pass RNCRYPTOR3_KDF_ITER for RNCryptor
** data format sepc v3
** encryption_key 32 byte long encryption key. Required.
** hmac_key 32 byte long HMAC key. Required.
** outdata_len Returns. Length of the returned encryped data
** errbuf Buffer to write error to
** errbuf_len Length of errbuf
**
** Return Values:
** Pointer to encyrped data on success, NULL on failure
** In case of failure errbuf will have the error message
**
** Side Effects:
** Memory is allocated for returned data. It is caller's responsibility to free it.
**
** Comments:
** The encryption is done as per RNCryptor data format specification v3.
** It is caller's responsibility to pass valid arguments.
**
** Development History:
** muquit@muquit.com May-20-2015 - first cut
*/
unsigned char *rncryptorc_encrypt_file_with_key(const char *infile_path,
int kdf_iter,
const unsigned char *encr_key,
const unsigned char *hmac_key,
int *outdata_len,
char *errbuf,
int errbuf_len);
/*
** Decrypt a file with a password
**
** Parameters:
** infile_path Path of the file to decrypt. Required
** kdf_iter PBKDF2 iterations. Must Pass RNCRYPTOR3_KDF_ITER for RNCryptor
** data format sepc v3
** password Password for decryption. Requied
** password_len Length of the password
** outdata_len Returns. Length of the returned decrypted data
** errbuf Buffer to write error to
** errbuf_len Length of errbuf
**
** Return Values:
** Pointer to decrypted data on success, NULL on failure
** In case of failure, errbuf will have the error message
**
** Side Effects:
** Memory is allocated for returned data. It is caller's responsibility to free it.
**
** Comments:
** The encryption is done as per RNCryptor data format specification v3.
**
** Development History:
** muquit@muquit.com May-20-2015 - first cut
*/
unsigned char *rncryptorc_decrypt_file_with_password(const char *infile_path,
int kdf_iter,
const char *password,
int password_length,
int *outdata_len,
char *errbuf,
int errbuf_len);
/*
** Decrypt a file with a encryption key. HMAC key is also requried for
** verifying the HMAC-SHA256 digest
**
** Parameters:
** infile_path Path of the input file. Required.
** kdf_iter PBKDF2 iterations. Must Pass RNCRYPTOR3_KDF_ITER for RNCryptor
** data format sepc v3
** encryption_key 32 byte long encryption key. Required.
** hmac_key 32 byte long HMAC key. Required.
** outdata_len Returns. Length of the returned encryped data
** errbuf Buffer to write error to
** errbuf_len Length of errbuf
**
** Return Values:
** Pointer to deccyrped data on success, NULL on failure.
** In case of failure, errbuf will have the error message
**
** Side Effects:
** Memory is allocated for returned data. It is caller's
** responsibility to free it.
**
** Comments:
** The encryption is done as per RNCryptor data format specification v3.
**
** Development History:
** muquit@muquit.com May-20-2015 - first cut
*/
unsigned char *rncryptorc_decrypt_file_with_key(const char *infile_path,
int kdf_iter,
const unsigned char *encr_key,
const unsigned char *hmac_key,
int *outdata_len,
char *errbuf,
int errbuf_len);
/*
** Encrypt data with a password. Encryption salt, HMAC salt and IV are
** auto generated.
**
** Parameters:
** indata Pointer to data to encrypt. Required
** indata_len Length of the data in bytes
** kdf_iter PBKDF2 iterations. Must Pass RNCRYPTOR3_KDF_ITER for RNCryptor
** data format sepc v3
** password Password for encryption, can not be empty
** password_len Length of the password
** outdata_len Returns. Length of the returned encryped data
** errbuf Buffer to write error to
** errbuf_len Length of errbuf
**
** Return Values:
** Pointer to encyrped data on success, NULL on failure.
** In case of failure errbuf will have the error message
**
** Side Effects:
** Memory is allocated for returned data. It is caller's responsibility to free it.
**
** Comments:
** The encryption is done as per RNCryptor data format specification v3.
**
** Development History:
** muquit@muquit.com May-20-2015 - first cut
*/
unsigned char *rncryptorc_encrypt_data_with_password(const unsigned char *indata,
int indata_len,
int kdf_iter,
const char *password,
int password_length,
int *out_data_len,
char *errbuf,
int errbuf_len);
/*
** Encrypt data with a password. Caller will pass encryption salt, hmac
** salt and iv
**
** Parameters:
** indata Pointer to data to encrypt. Required
** indata_len Length of the data in bytes
** kdf_iter PBKDF2 iterations. Must Pass RNCRYPTOR3_KDF_ITER for RNCryptor
** data format sepc v3
** password Password for encryption, can not be empty
** password_len Length of the password
** encr_salt_8 8 byte long encryption salt. Required.
** hmac_salt_8 8 byte long hmac salt. Required.
** iv_16 16 byte long iv. Required.
** outdata_len Returns. Length of the returned encryped data
** errbuf Buffer to write error to
** errbuf_len Length of errbuf
**
** Return Values:
** Pointer to encyrped data on success, NULL on failure.
** In case of failure errbuf will have the error message
**
** Side Effects:
** Memory is allocated for returned data. It is caller's responsibility to free it.
**
** Comments:
** The encryption is done as per RNCryptor data format specification v3.
**
** Development History:
** muquit@muquit.com May-30-2015 - first cut
*/
unsigned char *rncryptorc_encrypt_data_with_password_with_salts_and_iv(const unsigned char *indata,
int indata_len,
int kdf_iter,
const char *password,
int password_length,
unsigned char *encr_salt_8,
unsigned char *hmac_salt_8,
unsigned char *iv_16,
int *outdata_len,
char *errbuf,
int errbuf_len);
/*
** Encrypt data with a encryption key. HMAC key is also requried for
** creating the HMAC-SHA256 digest. IV is auto generatd.
**
** Parameters:
** indata Pointer to input data to encrypt. Required.
** indata_len Length of the input data in bytes
** kdf_iter PBKDF2 iterations. Must Pass RNCRYPTOR3_KDF_ITER for
** RNCryptor data format sepc v3
** encryption_key 32 byte long encryption key. Required.
** hmac_key 32 byte long HMAC key. Required.
** outdata_len Returns. Length of the returned encryped data
** errbuf Buffer to write error to
** errbuf_len Length of errbuf
**
** Return Values:
** Pointer to encyrped data on success, NULL on failure
** In case of failure errbuf will have the error message
**
** Side Effects:
** Memory is allocated for returned data. It is caller's responsibility to free it.
**
** Comments:
** The encryption is done as per RNCryptor data format specification v3.
** It is caller's responsibility to pass valid arguments.
**
** Development History:
** muquit@muquit.com May-20-2015 - first cut
*/
unsigned char *rncryptorc_encrypt_data_with_key(const unsigned char *indata,
int indata_len,
int kdf_iter,
const unsigned char *encryption_key,
const unsigned char *hmac_key,
int *out_data_len,
char *errbuf,
int errbuf_len);
/*
** Encrypt data with a encryption key. Caller will pass encryption key,
** hmac key and iv
**
** Parameters:
** indata Pointer to input data to encrypt. Required.
** indata_len Length of the input data in bytes
** kdf_iter PBKDF2 iterations. Must Pass RNCRYPTOR3_KDF_ITER for
** RNCryptor data format sepc v3
** encr_key_32 32 byte long encryption key. Required.
** hmac_key_32 32 byte long HMAC key. Required.
** iv_16 16 byte long IV. Requied.
** outdata_len Returns. Length of the returned encryped data
** errbuf Buffer to write error to
** errbuf_len Length of errbuf
**
** Return Values:
** Pointer to encyrped data on success, NULL on failure
** In case of failure errbuf will have the error message
**
** Side Effects:
** Memory is allocated for returned data. It is caller's
** responsibility to free it.
**
** Comments:
** The encryption is done as per RNCryptor data format specification v3.
**
** Development History:
** muquit@muquit.com May-30-2015 - first cut
*/
unsigned char *rncryptorc_encrypt_data_with_key_iv(const unsigned char *indata,
int indata_len,
int kdf_iter,
const unsigned char *encryption_key_32,
const unsigned char *hmac_key_32,
const unsigned char *iv_16,
int *outdata_len,
char *errbuf,
int errbuf_len);
/*
** Decrypt data with a password
**
** Parameters:
** indata Pointer to input data to encrypt. Required.
** indata_len Length of the input data in bytes
** kdf_iter PBKDF2 iterations. Must Pass RNCRYPTOR3_KDF_ITER for RNCryptor
** data format sepc v3
** password Password for decryption. Requied
** password_len Length of the password
** outdata_len Returns. Length of the returned decrypted data
** errbuf Buffer to write error to
** errbuf_len Length of errbuf
**
** Return Values:
** pointer to decrypted data on success, NULL on failure
** In case of failure, errbuf will have the error message
**
** Side Effects:
** Memory is allocated for returned data. It is caller's responsibility to free it.
**
** Comments:
** The encryption is done as per RNCryptor data format specification v3.
**
** Development History:
** muquit@muquit.com May-20-2015 - first cut
*/
unsigned char *rncryptorc_decrypt_data_with_password(const unsigned char *indata,
int indata_len,
int kdf_iter,
const char *password,
int password_length,
int *out_data_len,
char *errbuf,
int errbuf_len);
/*
** Decrypt a file with a encryption key. HMAC key is also requried for
** verifying the HMAC-SHA256 digest
**
** Parameters:
** indata Pointer to input data to encrypt. Required.
** indata_len Length of the input data in bytes
** kdf_iter PBKDF2 iterations. Must Pass RNCRYPTOR3_KDF_ITER for RNCryptor
** data format sepc v3
** encryption_key 32 byte long encryption key. Required.
** hmac_key 32 byte long HMAC key. Required.
** outdata_len Returns. Length of the returned encryped data
** errbuf Buffer to write error to
** errbuf_len Length of errbuf
**
** Return Values:
** Pointer to deccyrped data on success, NULL on failure.
** In case of failure, errbuf will have the error message.
**
** Side Effects:
** Memory is allocated for returned data. It is caller's responsibility to free it.
**
** Comments:
** The encryption is done as per RNCryptor data format specification v3.
**
** Development History:
** muquit@muquit.com May-20-2015 - first cut
*/
unsigned char *rncryptorc_decrypt_data_with_key(const unsigned char *indata,
int indata_len,
int kdf_iter,
const unsigned char *encr_key,
const unsigned char *hmac_key,
int *outdata_len,
char *errbuf,
int errbuf_len);
/*
** Read and return the content of a file
**
** Parameters:
** path Path of the file to read
** length Length of the data. returns.
**
** Return Values:
** pointer to content of file on success, NULL on failure
**
** Side Effects:
** Memory is allocated for the returned data, the caller is responsible
** to free it
**
** Comments:
** Just a Helper function
**
** Development History:
** muquit@muquit.com May-20-2015 - first cut
*/
unsigned char *rncryptorc_read_file(const char *path,int *length);
/*
** Write data to a file
**
** Parameters:
** outfile_path Path of the output file
** data Pointer to data
** data_len Length of data
**
** Return Values:
** SUCCESS or FAILURE
**
** Side Effects:
** none
**
** Comments:
** Just a Helper function
**
** Development History:
** muquit@muquit.com May-20-2015 - first cut
*/
int rncryptorc_write_file(const char *outfile_path,const unsigned char *data,int data_len);
/* TODO */
typedef void (*rncryptorc_log_func)(const char *fmt,va_list args);
void rncryptorc_log_error(const char *fmt,...);
void rncryptorc_log_info(const char *fmt,...);
void rncryptorc_log_debug(const char *fmt,...);
/*
** show usage for examples
*/
void show_example_usage(const char *prog,const char *arg1,const char *arg2);
void show_example_usage_with_key(const char *prog,const char *arg1,const char *arg2);
/* returns SUCCESS or FAILURE */
int verify_rncryptor_format(unsigned char version,unsigned char options);
#endif /* RNCRYPTOR_C_H */