Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_crypto.c
Go to the documentation of this file.
1
9#ifndef _GNU_SOURCE
10#define _GNU_SOURCE
11#define _GNU_SOURCE_WAS_NOT_DEFINED
12#endif
13
14#include "nilorea/n_common.h"
15#include "nilorea/n_base64.h"
16#include "nilorea/n_crypto.h"
18#include <stdio.h>
19#include <string.h>
20#include <ctype.h>
21#ifdef __solaris__
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <fcntl.h>
25#include <unistd.h>
26#include <errno.h>
27#endif
28#if defined(__linux__) || defined(__windows__)
29#include <cpuid.h>
30#endif
31#ifndef __windows__
32#include <sys/sysinfo.h>
33#endif
34#ifdef _GNU_SOURCE_WAS_NOT_DEFINED
35#undef _GNU_SOURCE
36#endif
37
38#ifdef __windows__
39#include <windows.h>
40#endif
41
43static char* __internal_root_key = "ENOFGLUCUNZJADRDMCZZSACRBKXAGSLTTFCICPALKHMWWGLVUIFYSLQJIHHXSHOZGRABFNBHGVTOMEBTVIPXZJHHEKIYWNVTWQKERROCTXFGMMLYUJSJFWLFCHQQMMUM";
44
50N_STR* n_vigenere_get_rootkey(size_t rootkey_size) {
51 if ((rootkey_size % 2) != 0) {
52 n_log(LOG_ERR, "key size %d is not a multiple of 2", rootkey_size);
53 return NULL;
54 }
55 N_STR* output = new_nstr(rootkey_size + 1);
56 time_t t = time(NULL);
57 if (t == -1) {
58 n_log(LOG_ERR, "Error: Unable to get the current time");
59 return NULL;
60 }
61 srand((unsigned int)t);
62 for (size_t it = 0; it < rootkey_size; it++) {
63 output->data[it] = (char)('A' + rand() % 26);
64 }
65 output->written = rootkey_size;
66 return output;
67}
68
74 N_STR* output = NULL;
75
76#ifdef __windows__
77 DWORD dwVolSerial;
78 BOOL bIsRetrieved;
79 bIsRetrieved = GetVolumeInformation(NULL, NULL, 0, &dwVolSerial, NULL, NULL, NULL, 0);
80
81 if (bIsRetrieved) {
82 nstrprintf(output, "%X", (unsigned int)dwVolSerial);
83 } else {
84 n_log(LOG_ERR, "Could not retrieve current directory disk serial id");
85 return NULL;
86 }
87 return output;
88#elif __linux__
89 char* curdir = getcwd(NULL, 0);
90
91 N_STR* cmd = NULL;
92 nstrprintf(cmd, "df -T \"%s\" | awk '/^\\/dev/ {print $1}'", curdir);
93 Free(curdir);
94
95 N_STR* cur_disk = NULL;
96 int ret = -1;
97 n_popen(_nstr(cmd), 1024, (void**)&cur_disk, &ret);
98 free_nstr(&cmd);
99
100 nstrprintf(cmd, "udevadm info --query=all --name=%s | egrep '(ID_SERIAL=|ID_FS_UUID=)' | awk -F'=' '{print $2}'", _nstr(cur_disk));
101 n_popen(_nstr(cmd), 1024, (void**)&output, &ret);
102 free_nstr(&cmd);
103
104 N_STR* hd_serial = NULL;
105 nstrprintf(hd_serial, "%s=>%s", _nstr(output), _nstr(cur_disk));
106
107 free_nstr(&output);
108 free_nstr(&cur_disk);
109
110 for (unsigned int it = 0; it < hd_serial->written; it++) {
111 if (isspace(hd_serial->data[it])) {
112 hd_serial->data[it] = '-';
113 }
114 }
115 return hd_serial;
116
117#else // SOLARIS
118
119 char* curdir = getcwd(NULL, 0);
120 N_STR* hostid = NULL;
121 int ret = -1;
122 n_popen("hostid", 1024, (void**)&hostid, &ret);
123 Free(curdir);
124 nstrprintf(output, "%s:%s", _nstr(hostid), _str(curdir));
125 return output;
126
127#endif
128}
129
135 N_STR* cpu_id = NULL;
136
137#ifndef __sun
138 unsigned int a[4] = {0, 0, 0, 0};
139
140 if (__get_cpuid_max(0, NULL) >= 1) {
141 __get_cpuid(1, &a[0], &a[1], &a[2], &a[3]);
142 } else {
143 n_log(LOG_ERR, "unable to get CPU id");
144 return NULL;
145 }
146
147 int binaryNum[32];
148 memset(&binaryNum, 0, sizeof(binaryNum));
149
150 unsigned int n = a[3];
151 unsigned int i = 0;
152 while (n > 0) {
153 // storing remainder in binary array
154 binaryNum[i] = n % 2;
155 n = n / 2;
156 i++;
157 }
158 n = a[2];
159 i = 0;
160 while (n > 0) {
161 // storing remainder in binary array
162 binaryNum[i] = n % 2;
163 n = n / 2;
164 i++;
165 }
166 n = a[1];
167 i = 0;
168 while (n > 0) {
169 // storing remainder in binary array
170 binaryNum[i] = n % 2;
171 n = n / 2;
172 i++;
173 }
174 n = a[0];
175 i = 0;
176 while (n > 0) {
177 // storing remainder in binary array
178 binaryNum[i] = n % 2;
179 n = n / 2;
180 i++;
181 }
182
183 /* printf("-------Signature(EAX register):-------");
184 printf("\nStepping ID:%d%d%d%d",binaryNum[3],binaryNum[2],binaryNum[1],binaryNum[0]);
185 printf("\nModel:%d%d%d%d",binaryNum[7],binaryNum[6],binaryNum[5],binaryNum[4]);
186 printf("\nFamily ID:%d%d%d%d",binaryNum[11],binaryNum[10],binaryNum[9],binaryNum[8]);
187 printf("\nProcessor Type:%d%d",binaryNum[13],binaryNum[12]);
188 printf("\nExtended Model ID:%d%d%d%d",binaryNum[19],binaryNum[18],binaryNum[17],binaryNum[16]);
189 printf("\nExtended Family ID:%d%d%d%d%d%d%d%d",binaryNum[27],binaryNum[26],binaryNum[25],binaryNum[24],binaryNum[23],binaryNum[22],binaryNum[21],binaryNum[20]);
190 printf("\n"); */
191 nstrprintf(cpu_id, "%d%d%d%d-%d%d%d%d-%d%d%d%d-%d%d", binaryNum[3], binaryNum[2], binaryNum[1], binaryNum[0],
192 binaryNum[7], binaryNum[6], binaryNum[5], binaryNum[4],
193 binaryNum[11], binaryNum[10], binaryNum[9], binaryNum[8],
194 binaryNum[13], binaryNum[12]);
195
196#else // We are on SOLARIS
197 static const char devname[] = "/dev/cpu/self/cpuid";
198
199 struct {
200 uint32_t r_eax, r_ebx, r_ecx, r_edx;
201 } _r, *rp = &_r;
202 int d;
203 char* s = NULL;
204
205 if ((d = open(devname, O_RDONLY)) != -1) {
206 if (pread(d, rp, sizeof(*rp), 0) == sizeof(*rp)) {
207 s = (char*)&rp->r_ebx;
208 }
209 close(d);
210 }
211 nstrprintf(cpu_id, "%s", s);
212#endif
213 return cpu_id;
214}
215
231
237void* n_vigenere_cypher_thread(void* ptr) {
238 __n_assert(ptr, return NULL);
239 CYPHER_PARAM* params = (CYPHER_PARAM*)ptr;
240 __n_assert(params, return NULL);
241
242 N_STR* input = params->input;
243 N_STR* dest = params->dest;
244 N_STR* key = params->key;
245
246 __n_assert(input, return NULL);
247 __n_assert(input->data, return NULL);
248 __n_assert(key, return NULL);
249 __n_assert(key->data, return NULL);
250
251 bool encipher = params->encipher;
252
253 size_t start_pos = params->start_pos;
254 size_t end_pos = params->end_pos;
255
256 bool cIsUpper = 0;
257 size_t keyIndexMod = 0;
258 if (encipher) {
259 for (size_t i = start_pos; i < end_pos; i++) {
260 if ((cIsUpper = n_isupper(input->data[i])) || (n_islower(input->data[i]))) {
261 char offset = cIsUpper ? 'A' : 'a';
262 keyIndexMod = i % key->written;
263 int k = (cIsUpper ? n_toupper(key->data[keyIndexMod]) : n_tolower(key->data[keyIndexMod])) - offset;
264 dest->data[i] = (char)((n_mathmod(((input->data[i] + k) - offset), 26)) + offset);
265 }
266 }
267 } else {
268 for (size_t i = start_pos; i < end_pos; i++) {
269 if ((cIsUpper = n_isupper(input->data[i])) || (n_islower(input->data[i]))) {
270 char offset = cIsUpper ? 'A' : 'a';
271 keyIndexMod = i % key->written;
272 int k = (cIsUpper ? n_toupper(key->data[keyIndexMod]) : n_tolower(key->data[keyIndexMod])) - offset;
273 dest->data[i] = (char)((n_mathmod(((input->data[i] - k) - offset), 26)) + offset);
274 }
275 }
276 }
277 Free(params);
278 return NULL;
279}
280
289N_STR* n_vigenere_cypher(N_STR* input, N_STR* key, bool encipher, bool in_place) {
290 __n_assert(input, return NULL);
291 __n_assert(input->data, return NULL);
292 __n_assert(key, return NULL);
293 __n_assert(key->data, return NULL);
294
295 if (input->written == 0) {
296 n_log(LOG_ERR, "empty input, written=0");
297 return NULL;
298 }
299
300 if ((key->written % 2) != 0) {
301 n_log(LOG_ERR, "odd number of elements in root key %s, %d", _nstr(key), key->written);
302 return NULL;
303 }
304
305 for (size_t i = 0; i < key->written; ++i) {
306 if (!isalpha(key->data[i])) {
307 n_log(LOG_ERR, "key contain bad char '%c'", key->data[i]);
308 return NULL; // Error
309 }
310 }
311
312 N_STR* dest = NULL;
313 if (in_place) {
314 dest = input;
315 } else {
316 dest = nstrdup(input);
317 }
318
319#ifdef __windows__
320 SYSTEM_INFO sysinfo;
321 GetSystemInfo(&sysinfo);
322 size_t nb_procs = (size_t)sysinfo.dwNumberOfProcessors;
323#else
324 size_t nb_procs = (size_t)get_nprocs();
325#endif
326
327 if (nb_procs < 2)
328 nb_procs = 2;
329
331
332 size_t bytesteps = input->written / nb_procs;
333 if (bytesteps == 0) bytesteps = input->written;
334
335 size_t end_pos = 0;
336 for (size_t it = 0; it < input->written; it += bytesteps) {
337 end_pos = it + bytesteps;
338
339 if (end_pos >= input->written)
340 end_pos = input->written;
341
342 CYPHER_PARAM* params = NULL;
343 Malloc(params, CYPHER_PARAM, 1);
344 params->input = input;
345 params->dest = dest;
346 params->key = key;
347 params->encipher = encipher;
348 params->start_pos = it;
349 params->end_pos = end_pos;
350
351 if (add_threaded_process(thread_pool, &n_vigenere_cypher_thread, (void*)(intptr_t)params, DIRECT_PROC) == FALSE) {
352 n_log(LOG_ERR, "Error adding n_vigenere_cypher_thread to thread pool");
353 }
355 usleep(1);
356 }
357 if (end_pos < input->written) {
358 CYPHER_PARAM* params = NULL;
359 Malloc(params, CYPHER_PARAM, 1);
360 params->input = input;
361 params->dest = dest;
362 params->key = key;
363 params->encipher = encipher;
364 params->start_pos = end_pos;
365 params->end_pos = input->written;
366
367 if (add_threaded_process(thread_pool, &n_vigenere_cypher_thread, (void*)(intptr_t)params, DIRECT_PROC) == FALSE) {
368 n_log(LOG_ERR, "Error adding n_vigenere_cypher_thread to thread pool");
369 }
371 usleep(1);
372 }
375
376 /*
377 uint32_t nonAlphaCharCount = 0;
378 for( uint32_t i = 0 ; i < input -> written ; i++ )
379 {
380 char ch = input -> data[ i ];
381 if( isalpha( input -> data[ i ] ) )
382 {
383 bool cIsUpper = isupper( input -> data[ i ] );
384 char offset = cIsUpper ? 'A' : 'a';
385 int keyIndex = ( i - nonAlphaCharCount ) % key -> written ;
386 int k = ( cIsUpper ? toupper( key -> data[ keyIndex % key -> written ] ) : tolower( key -> data[ keyIndex % key -> written ] ) ) - offset ;
387 k = encipher ? k : -k;
388 ch = (char)( ( n_mathmod( ( ( input -> data[ i ] + k ) - offset ) , 26 ) ) + offset );
389 }
390 else
391 {
392 ++nonAlphaCharCount;
393 }
394 dest -> data[ i ] = ch;
395 }
396 */
397
398 return dest;
399}
400
408 return n_vigenere_cypher(string, key, 1, 0);
409}
410
418 return n_vigenere_cypher(string, key, 0, 0);
419}
420
428 return n_vigenere_cypher(string, key, 1, 1);
429}
430
438 return n_vigenere_cypher(string, key, 0, 1);
439}
440
447 N_STR* __internal_root_key_nstr = char_to_nstr(__internal_root_key);
448
449 N_STR* base64_data = n_base64_encode(decoded_data);
450 N_STR* encoded_data = n_vigenere_encode_in_place(base64_data, __internal_root_key_nstr);
451 free_nstr(&__internal_root_key_nstr);
452
453 return encoded_data;
454}
455
462 N_STR* __internal_root_key_nstr = char_to_nstr(__internal_root_key);
463
464 N_STR* decoded_data = n_vigenere_decode(encoded_data, __internal_root_key_nstr);
465 N_STR* base64_data = n_base64_decode(decoded_data);
466
467 free_nstr(&__internal_root_key_nstr);
468 free_nstr(&decoded_data);
469
470 return base64_data;
471}
472
478N_STR* n_vigenere_get_question(size_t question_size) {
479 size_t it = 0;
480
481 if ((question_size % 2) != 0) {
482 n_log(LOG_ERR, "odd number of elements for question size, %d", question_size);
483 return NULL;
484 }
485
486 N_STR* output = NULL;
488
489 N_STR* cpu_id = n_get_cpu_id();
490 nstrprintf_cat(output, "@%s", _nstr(cpu_id));
491 free_nstr(&cpu_id);
492
493 if (output) {
494 N_STR* tmpoutput = n_base64_encode(output);
495 free_nstr(&output);
496 output = new_nstr(question_size + 1);
497 for (it = 0; it < question_size; it++) {
498 int val = tmpoutput->data[it % tmpoutput->written];
499 while (val < 'A') val += 26;
500 while (val > 'Z') val -= 26;
501 output->data[it] = (char)val;
502 }
503 output->written = question_size;
504 free_nstr(&tmpoutput);
505 } else {
506 // generate random
507 output = new_nstr(question_size + 1);
508 for (it = 0; it < question_size; it++) {
509 int val = rand() % 256;
510 while (val < 'A') val += 26;
511 while (val > 'Z') val -= 26;
512 output->data[it] = (char)val;
513 }
514 }
515
516 N_STR* encoded_data = n_vigenere_quick_encode(output);
517 free_nstr(&output);
518
519 it = 0;
520 while (it < encoded_data->written) {
521 if (encoded_data->data[it] == '=') {
522 encoded_data->data[it] = '\0';
523 }
524 it++;
525 }
526 return encoded_data;
527}
528
535N_STR* n_vigenere_get_answer(N_STR* root_key, N_STR* question) {
536 __n_assert(root_key, return NULL);
537 __n_assert(question, return NULL);
538
539 N_STR* decoded_question = n_vigenere_quick_decode(question);
540
541 if (root_key->written < decoded_question->written) {
542 n_log(LOG_ERR, "root key of size %d is lower than question key of size %d", root_key->written, decoded_question->written);
543 free_nstr(&decoded_question);
544 return NULL;
545 }
546 if ((root_key->written % decoded_question->written) != 0) {
547 n_log(LOG_ERR, "question key of size %d is not a multiple of root key of size %d", decoded_question->written, root_key->written);
548 free_nstr(&decoded_question);
549 return NULL;
550 }
551
552 N_STR* answer = new_nstr(root_key->length + 1);
553 for (uint32_t it = 0; it < root_key->written; it++) {
554 int32_t val = (root_key->data[it % root_key->written] - 'A') + (decoded_question->data[it % decoded_question->written] - 'A');
555 val = val % 26;
556 val = val + 'A';
557 answer->data[it] = (char)val;
558 }
559 free_nstr(&decoded_question);
560 answer->written = root_key->written;
561 return answer;
562}
563
572 __n_assert(in, return FALSE);
573 __n_assert(out, return FALSE);
574 __n_assert(key, return FALSE);
575
576 if ((key->written % 2) != 0) {
577 n_log(LOG_ERR, "odd number of elements in key %s, %d", _nstr(key), key->written);
578 return FALSE;
579 }
580
581 N_STR* file = file_to_nstr(_nstr(in));
582 if (!file) {
583 n_log(LOG_ERR, "error loading %s", _nstr(in));
584 return FALSE;
585 }
586
587 N_STR* base64_data = n_base64_encode(file);
588 free_nstr(&file);
589
590 if (!base64_data) {
591 n_log(LOG_ERR, "Could not base64 decode data from file %s", _nstr(in));
592 return FALSE;
593 }
594
595 N_STR* encoded_data = n_vigenere_encode_in_place(base64_data, key);
596 if (!encoded_data) {
597 n_log(LOG_ERR, "Could not decode data from file %s", _nstr(in));
598 return FALSE;
599 }
600
601 int ret = nstr_to_file(encoded_data, _nstr(out));
602 free_nstr(&encoded_data);
603
604 return ret;
605}
606
615 __n_assert(in, return FALSE);
616 __n_assert(out, return FALSE);
617 __n_assert(key, return FALSE);
618
619 if ((key->written % 2) != 0) {
620 n_log(LOG_ERR, "odd number of elements in key %s , %d", _nstr(key), key->written);
621 return FALSE;
622 }
623
624 N_STR* file = file_to_nstr(_nstr(in));
625 if (!file) {
626 n_log(LOG_ERR, "error loading %s", _nstr(in));
627 return FALSE;
628 }
629
630 N_STR* decoded_data = n_vigenere_decode_in_place(file, key);
631 if (!decoded_data) {
632 n_log(LOG_ERR, "Could not decode data from file %s", _nstr(in));
633 return FALSE;
634 }
635
636 N_STR* base64_data = n_base64_decode(decoded_data);
637 free_nstr(&decoded_data);
638 if (!base64_data) {
639 n_log(LOG_ERR, "Could not base64 decode data from file %s", _nstr(in));
640 return FALSE;
641 }
642
643 int ret = nstr_to_file(base64_data, _nstr(out));
644 free_nstr(&base64_data);
645
646 return ret;
647}
648
656N_STR* n_vigenere_encode_qa(N_STR* input_data, N_STR* question, N_STR* answer) {
657 __n_assert(input_data, return NULL);
658 __n_assert(question, return NULL);
659 __n_assert(answer, return NULL);
660
661 N_STR* b64_encoded = n_base64_encode(input_data);
662 if (!b64_encoded) {
663 n_log(LOG_ERR, "could not base64 decode: input_data %p", input_data);
664 return NULL;
665 }
666
667 N_STR* encoded_with_answer_data = n_vigenere_encode_in_place(b64_encoded, answer);
668 if (!encoded_with_answer_data) {
669 n_log(LOG_ERR, "could not encode: input_data %p", input_data);
670 return NULL;
671 }
672
673 N_STR* decoded_question = n_vigenere_quick_decode(question);
674 N_STR* encoded_data = n_vigenere_decode_in_place(encoded_with_answer_data, decoded_question);
675 free_nstr(&decoded_question);
676 if (!encoded_data) {
677 n_log(LOG_ERR, "could not decode: input_data %p", input_data);
678 return NULL;
679 }
680
681 return encoded_data;
682}
683
691N_STR* n_vigenere_decode_qa(N_STR* input_data, N_STR* question, N_STR* answer) {
692 __n_assert(input_data, return NULL);
693 __n_assert(input_data->data, return NULL);
694 __n_assert(question, return NULL);
695 __n_assert(question->data, return NULL);
696 __n_assert(answer, return NULL);
697 __n_assert(answer->data, return NULL);
698
699 N_STR* decoded_question = n_vigenere_quick_decode(question);
700
701 N_STR* encoded_data = n_vigenere_encode(input_data, decoded_question);
702 free_nstr(&decoded_question);
703 if (!encoded_data) {
704 n_log(LOG_ERR, "could not encode input_data %p", input_data);
705 return NULL;
706 }
707 n_log(LOG_DEBUG, "encoded_data: %d/%d", encoded_data->written, encoded_data->length);
708
709 N_STR* encoded_with_answer_data = n_vigenere_decode_in_place(encoded_data, answer);
710 if (!encoded_with_answer_data) {
711 n_log(LOG_ERR, "could not decode: input_data %p", input_data);
712 return NULL;
713 }
714 n_log(LOG_DEBUG, "encoded_with_answer_data: %d/%d", encoded_with_answer_data->written, encoded_with_answer_data->length);
715
716 N_STR* b64_decoded = n_base64_decode(encoded_with_answer_data);
717 free_nstr(&encoded_with_answer_data);
718 if (!b64_decoded) {
719 n_log(LOG_ERR, "could not base64 decode: input_data %p", input_data);
720 return NULL;
721 }
722 n_log(LOG_DEBUG, "b64_decoded: %d/%d", b64_decoded->written, b64_decoded->length);
723 return b64_decoded;
724}
725
734int n_vigenere_decode_file_qa(N_STR* in, N_STR* out, N_STR* question, N_STR* answer) {
735 __n_assert(in, return FALSE);
736 __n_assert(out, return FALSE);
737 __n_assert(question, return FALSE);
738 __n_assert(answer, return FALSE);
739
740 N_STR* input_data = file_to_nstr(_nstr(in));
741 if (!input_data) {
742 n_log(LOG_ERR, "could not load input file %s", _nstr(in));
743 return FALSE;
744 }
745
746 N_STR* decoded = n_vigenere_decode_qa(input_data, question, answer);
747 free_nstr(&input_data);
748
749 int ret = nstr_to_file(decoded, _nstr(out));
750 free_nstr(&decoded);
751
752 return ret;
753}
754
763int n_vigenere_encode_file_qa(N_STR* in, N_STR* out, N_STR* question, N_STR* answer) {
764 __n_assert(in, return FALSE);
765 __n_assert(out, return FALSE);
766 __n_assert(question, return FALSE);
767 __n_assert(answer, return FALSE);
768
769 N_STR* input_data = file_to_nstr(_nstr(in));
770 if (!input_data) {
771 n_log(LOG_ERR, "could not load input file %s", _nstr(in));
772 return FALSE;
773 }
774
775 N_STR* encoded = n_vigenere_encode_qa(input_data, question, answer);
776 free_nstr(&input_data);
777
778 int ret = nstr_to_file(encoded, _nstr(out));
779 free_nstr(&encoded);
780
781 return ret;
782}
THREAD_POOL * thread_pool
Definition ex_fluid.c:59
char * key
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition n_common.h:185
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:256
#define _str(__PTR)
define true
Definition n_common.h:174
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:240
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
Definition n_common.h:180
int n_popen(char *cmd, size_t read_buf_size, void **nstr_output, int *ret)
launch a command abd return output and status
Definition n_common.c:163
char n_tolower(char c)
is_alpha
Definition n_base64.c:124
N_STR * n_base64_decode(N_STR *bufcoded)
decode a N_STR *string
Definition n_base64.c:169
bool n_isupper(char c)
test if char c is uppercase
Definition n_base64.c:86
char n_toupper(char c)
is_alpha
Definition n_base64.c:113
bool n_islower(char c)
test if char c is lowercase
Definition n_base64.c:95
N_STR * n_base64_encode(N_STR *input)
encode a N_STR *string
Definition n_base64.c:251
N_STR * n_vigenere_encode_qa(N_STR *input_data, N_STR *question, N_STR *answer)
directly vigenere encode a file using key
Definition n_crypto.c:656
int n_vigenere_decode_file_qa(N_STR *in, N_STR *out, N_STR *question, N_STR *answer)
directly vigenere decode a file using question and answer
Definition n_crypto.c:734
N_STR * n_vigenere_quick_encode(N_STR *decoded_data)
quick encode data
Definition n_crypto.c:446
N_STR * n_vigenere_decode(N_STR *string, N_STR *key)
decode input using vigenere cypher and key
Definition n_crypto.c:417
#define n_mathmod(__a, __b)
The % operator returns a result that adopts the sign of the dividend, a true mathematical modulus ado...
Definition n_crypto.h:26
N_STR * n_vigenere_get_answer(N_STR *root_key, N_STR *question)
get an answer from a root key and a question
Definition n_crypto.c:535
int n_vigenere_encode_file_qa(N_STR *in, N_STR *out, N_STR *question, N_STR *answer)
directly vigenere encode a file using question and answer
Definition n_crypto.c:763
N_STR * n_vigenere_decode_qa(N_STR *input_data, N_STR *question, N_STR *answer)
directly vigenere decode a file using key
Definition n_crypto.c:691
N_STR * n_vigenere_encode(N_STR *string, N_STR *key)
encode input using vigenere cypher and key
Definition n_crypto.c:407
int n_vigenere_encode_file(N_STR *in, N_STR *out, N_STR *key)
directly vigenere encode a file using key
Definition n_crypto.c:571
int n_vigenere_decode_file(N_STR *in, N_STR *out, N_STR *key)
directly vigenere decode a file using key
Definition n_crypto.c:614
N_STR * n_vigenere_get_rootkey(size_t rootkey_size)
get a rootkey randomly generated
Definition n_crypto.c:50
N_STR * n_vigenere_get_question(size_t question_size)
get a question generated from the current machine hardware (disk&cpu)
Definition n_crypto.c:478
N_STR * n_vigenere_quick_decode(N_STR *encoded_data)
quick decode data
Definition n_crypto.c:461
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:70
#define LOG_DEBUG
debug-level messages
Definition n_log.h:65
#define LOG_ERR
error conditions
Definition n_log.h:57
size_t written
size of the written data inside the string
Definition n_str.h:45
char * data
the string
Definition n_str.h:41
size_t length
length of string (in case we wanna keep information after the 0 end of string value)
Definition n_str.h:43
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:176
int nstr_to_file(N_STR *str, char *filename)
Write a N_STR content into a file.
Definition n_str.c:386
N_STR * nstrdup(N_STR *str)
Duplicate a N_STR.
Definition n_str.c:670
#define nstrprintf_cat(__nstr_var, __format,...)
Macro to quickly allocate and sprintf and cat to a N_STR.
Definition n_str.h:98
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
Definition n_str.c:228
N_STR * new_nstr(NSTRBYTE size)
create a new N_STR string
Definition n_str.c:180
#define nstrprintf(__nstr_var, __format,...)
Macro to quickly allocate and sprintf to N_STR.
Definition n_str.h:94
N_STR * file_to_nstr(char *filename)
Load a whole file into a N_STR.
Definition n_str.c:260
A box including a string and his lenght.
Definition n_str.h:39
THREAD_POOL * new_thread_pool(size_t nbmaxthr, size_t nb_max_waiting)
Create a new pool of nbmaxthr threads.
int add_threaded_process(THREAD_POOL *thread_pool, void *(*func_ptr)(void *param), void *param, int mode)
add a function and params to a thread pool
int refresh_thread_pool(THREAD_POOL *thread_pool)
try to add some waiting DIRECT_PROCs on some free thread slots, else do nothing
int destroy_threaded_pool(THREAD_POOL **pool, unsigned int delay)
delete a thread_pool, exit the threads and free the structs
#define DIRECT_PROC
processing mode for added func, direct start, not queued
int wait_for_threaded_pool(THREAD_POOL *thread_pool, unsigned int delay)
Wait for all the launched process in the thread pool to terminate.
Structure of a trhead pool.
Base64 encoding and decoding functions using N_STR.
Common headers and low-level functions & define.
static char * __internal_root_key
static internal root key, just to obfuscate text so we do not care if is is public
Definition n_crypto.c:43
N_STR * n_vigenere_cypher(N_STR *input, N_STR *key, bool encipher, bool in_place)
encode or decode input using vigenere cypher
Definition n_crypto.c:289
bool encipher
1 encoding, 0 decoding
Definition n_crypto.c:225
void * n_vigenere_cypher_thread(void *ptr)
encode or decode threaded func using params from ptr
Definition n_crypto.c:237
N_STR * n_vigenere_decode_in_place(N_STR *string, N_STR *key)
decode input using vigenere cypher and key
Definition n_crypto.c:437
N_STR * dest
destination
Definition n_crypto.c:221
N_STR * n_get_current_dir_hd_serial()
get the serial of the current running dir hosting disk
Definition n_crypto.c:73
size_t start_pos
input starting point
Definition n_crypto.c:227
N_STR * input
input data
Definition n_crypto.c:219
N_STR * key
encoding/decoding key
Definition n_crypto.c:223
N_STR * n_vigenere_encode_in_place(N_STR *string, N_STR *key)
encode string in place using vigenere cypher and key
Definition n_crypto.c:427
size_t end_pos
output starting point
Definition n_crypto.c:229
N_STR * n_get_cpu_id()
get the CPU id
Definition n_crypto.c:134
structure of a n_vigenere_cypher_thread param
Definition n_crypto.c:217
Vigenere encoding and decoding functions using N_STR/files.
Thread pool declaration.