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