Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_crypto.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
27#ifndef _GNU_SOURCE
28#define _GNU_SOURCE
29#define _GNU_SOURCE_WAS_NOT_DEFINED
30#endif
31
32#include "nilorea/n_common.h"
33#include "nilorea/n_base64.h"
34#include "nilorea/n_crypto.h"
36#include <stdio.h>
37#include <string.h>
38#include <ctype.h>
39#ifdef __solaris__
40#include <sys/types.h>
41#include <sys/stat.h>
42#include <fcntl.h>
43#include <unistd.h>
44#include <errno.h>
45#endif
46#if defined(__linux__) || defined(__windows__)
47#include <cpuid.h>
48#endif
49#ifndef __windows__
50#include <sys/sysinfo.h>
51#endif
52#ifdef _GNU_SOURCE_WAS_NOT_DEFINED
53#undef _GNU_SOURCE
54#endif
55
56#ifdef __windows__
57#include "nilorea/n_windows.h"
58#endif
59
61static char* __internal_root_key = "ENOFGLUCUNZJADRDMCZZSACRBKXAGSLTTFCICPALKHMWWGLVUIFYSLQJIHHXSHOZGRABFNBHGVTOMEBTVIPXZJHHEKIYWNVTWQKERROCTXFGMMLYUJSJFWLFCHQQMMUM";
62
68N_STR* n_vigenere_get_rootkey(size_t rootkey_size) {
69 if ((rootkey_size % 2) != 0) {
70 n_log(LOG_ERR, "key size %zu is not a multiple of 2", rootkey_size);
71 return NULL;
72 }
73 time_t t = time(NULL);
74 if (t == -1) {
75 n_log(LOG_ERR, "Error: Unable to get the current time");
76 return NULL;
77 }
78 N_STR* output = new_nstr(rootkey_size + 1);
79 /* NOTE: rand() seeded with time() is not cryptographically secure.
80 * This key is suitable only for obfuscation, not for strong protection. */
81 srand((unsigned int)t);
82 for (size_t it = 0; it < rootkey_size; it++) {
83 output->data[it] = (char)('A' + rand() % 26);
84 }
85 output->written = rootkey_size;
86 return output;
87}
88
94 N_STR* output = NULL;
95
96#ifdef __windows__
97 DWORD dwVolSerial;
98 BOOL bIsRetrieved;
99 bIsRetrieved = GetVolumeInformation(NULL, NULL, 0, &dwVolSerial, NULL, NULL, NULL, 0);
100
101 if (bIsRetrieved) {
102 nstrprintf(output, "%X", (unsigned int)dwVolSerial);
103 } else {
104 n_log(LOG_ERR, "Could not retrieve current directory disk serial id");
105 return NULL;
106 }
107 return output;
108#elif __linux__
109 char* curdir = getcwd(NULL, 0);
110
111 N_STR* cmd = NULL;
112 nstrprintf(cmd, "df -T \"%s\" | awk '/^\\/dev/ {print $1}'", curdir);
113 Free(curdir);
114
115 N_STR* cur_disk = NULL;
116 int ret = -1;
117 n_popen(_nstr(cmd), 1024, (void**)&cur_disk, &ret);
118 free_nstr(&cmd);
119
120 nstrprintf(cmd, "udevadm info --query=all --name=\"%s\" | egrep '(ID_SERIAL=|ID_FS_UUID=)' | awk -F'=' '{print $2}'", _nstr(cur_disk));
121 n_popen(_nstr(cmd), 1024, (void**)&output, &ret);
122 free_nstr(&cmd);
123
124 N_STR* hd_serial = NULL;
125 nstrprintf(hd_serial, "%s=>%s", _nstr(output), _nstr(cur_disk));
126
127 free_nstr(&output);
128 free_nstr(&cur_disk);
129
130 for (unsigned int it = 0; it < hd_serial->written; it++) {
131 if (isspace(hd_serial->data[it])) {
132 hd_serial->data[it] = '-';
133 }
134 }
135 return hd_serial;
136
137#else /* SOLARIS */
138
139 char* curdir = getcwd(NULL, 0);
140 N_STR* hostid = NULL;
141 int ret = -1;
142 n_popen("hostid", 1024, (void**)&hostid, &ret);
143 /* build output before freeing curdir */
144 nstrprintf(output, "%s:%s", _nstr(hostid), _str(curdir));
145 free_nstr(&hostid);
146 Free(curdir);
147 return output;
148
149#endif
150}
151
157 N_STR* cpu_id = NULL;
158
159 /* x86 / x86_64 */
160#if defined(__i386__) || defined(__x86_64__)
161
162 unsigned int eax, ebx, ecx, edx;
163
164 if (__get_cpuid_max(0, NULL) >= 1 &&
165 __get_cpuid(1, &eax, &ebx, &ecx, &edx)) {
166 unsigned int stepping = eax & 0xF;
167 unsigned int model = (eax >> 4) & 0xF;
168 unsigned int family = (eax >> 8) & 0xF;
169 unsigned int processorType = (eax >> 12) & 0x3;
170
171 nstrprintf(cpu_id, "%u%u%u%u-%u%u%u%u-%u%u%u%u-%u%u",
172 (stepping >> 3) & 1,
173 (stepping >> 2) & 1,
174 (stepping >> 1) & 1,
175 stepping & 1,
176
177 (model >> 3) & 1,
178 (model >> 2) & 1,
179 (model >> 1) & 1,
180 model & 1,
181
182 (family >> 3) & 1,
183 (family >> 2) & 1,
184 (family >> 1) & 1,
185 family & 1,
186
187 (processorType >> 1) & 1,
188 processorType & 1);
189
190 return cpu_id;
191 }
192#endif
193
194 /* Solaris fallback */
195#ifdef __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 } regs;
201
202 int d;
203
204 if ((d = open(devname, O_RDONLY)) != -1) {
205 if (pread(d, &regs, sizeof(regs), 0) == sizeof(regs)) {
206 nstrprintf(cpu_id, "%08x", regs.r_eax);
207 close(d);
208 return cpu_id;
209 }
210 close(d);
211 }
212#endif
213
214 /* Linux / Android fallback */
215#if defined(__linux__) || defined(__ANDROID__)
216 FILE* f = fopen("/proc/cpuinfo", "r");
217 if (f) {
218 char line[256];
219
220 while (fgets(line, sizeof(line), f)) {
221 if (strncmp(line, "model name", 10) == 0 ||
222 strncmp(line, "Hardware", 8) == 0) {
223 char* colon = strchr(line, ':');
224 if (colon) {
225 colon += 2; /* skip ": " */
226 colon[strcspn(colon, "\n")] = 0;
227 nstrprintf(cpu_id, "%s", colon);
228 fclose(f);
229 return cpu_id;
230 }
231 }
232 }
233
234 fclose(f);
235 }
236#endif
237
238 /* Ultimate fallback */
239 n_log(LOG_ERR, "unable to determine CPU id");
240 nstrprintf(cpu_id, "unknown");
241 return cpu_id;
242}
243
259
265void* n_vigenere_cypher_thread(void* ptr) {
266 __n_assert(ptr, return NULL);
267 CYPHER_PARAM* params = (CYPHER_PARAM*)ptr;
268 __n_assert(params, return NULL);
269
270 N_STR* input = params->input;
271 N_STR* dest = params->dest;
272 const N_STR* key = params->key;
273
274 __n_assert(input, return NULL);
275 __n_assert(input->data, return NULL);
276 __n_assert(key, return NULL);
277 __n_assert(key->data, return NULL);
278
279 if (key->written == 0) return NULL;
280
281 bool encipher = params->encipher;
282
283 size_t start_pos = params->start_pos;
284 size_t end_pos = params->end_pos;
285
286 bool cIsUpper;
287 size_t keyIndexMod;
288 if (encipher) {
289 for (size_t i = start_pos; i < end_pos; i++) {
290 if ((cIsUpper = n_isupper(input->data[i])) || (n_islower(input->data[i]))) {
291 char offset = cIsUpper ? 'A' : 'a';
292 keyIndexMod = i % key->written;
293 int k = (cIsUpper ? n_toupper(key->data[keyIndexMod]) : n_tolower(key->data[keyIndexMod])) - offset;
294 dest->data[i] = (char)((n_mathmod(((input->data[i] + k) - offset), 26)) + offset);
295 }
296 }
297 } else {
298 for (size_t i = start_pos; i < end_pos; i++) {
299 if ((cIsUpper = n_isupper(input->data[i])) || (n_islower(input->data[i]))) {
300 char offset = cIsUpper ? 'A' : 'a';
301 keyIndexMod = i % key->written;
302 int k = (cIsUpper ? n_toupper(key->data[keyIndexMod]) : n_tolower(key->data[keyIndexMod])) - offset;
303 dest->data[i] = (char)((n_mathmod(((input->data[i] - k) - offset), 26)) + offset);
304 }
305 }
306 }
307 Free(params);
308 return NULL;
309}
310
319N_STR* n_vigenere_cypher(N_STR* input, N_STR* key, bool encipher, bool in_place) {
320 __n_assert(input, return NULL);
321 __n_assert(input->data, return NULL);
322 __n_assert(key, return NULL);
323 __n_assert(key->data, return NULL);
324
325 if (input->written == 0) {
326 n_log(LOG_ERR, "empty input, written=0");
327 return NULL;
328 }
329
330 // cppcheck-suppress nullPointerRedundantCheck -- key validated by __n_assert above
331 if ((key->written % 2) != 0) {
332 n_log(LOG_ERR, "odd number of elements in root key %s, %zu", _nstr(key), key->written);
333 return NULL;
334 }
335
336 for (size_t i = 0; i < key->written; ++i) {
337 if (!isalpha(key->data[i])) {
338 n_log(LOG_ERR, "key contain bad char '%c'", key->data[i]);
339 return NULL; // Error
340 }
341 }
342
343 N_STR* dest = NULL;
344 if (in_place) {
345 dest = input;
346 } else {
347 dest = nstrdup(input);
348 }
349
350#ifdef __windows__
351 SYSTEM_INFO sysinfo;
352 GetSystemInfo(&sysinfo);
353 size_t nb_procs = (size_t)sysinfo.dwNumberOfProcessors;
354#else
355 size_t nb_procs = (size_t)get_nprocs();
356#endif
357
358 if (nb_procs < 2)
359 nb_procs = 2;
360
362 if (!thread_pool) {
363 n_log(LOG_ERR, "Could not create thread pool for vigenere cypher");
364 if (!in_place) free_nstr(&dest);
365 return NULL;
366 }
367
368 size_t bytesteps = input->written / nb_procs;
369 if (bytesteps == 0) bytesteps = input->written;
370
371 size_t end_pos = 0;
372 for (size_t it = 0; it < input->written; it += bytesteps) {
373 end_pos = it + bytesteps;
374
375 if (end_pos >= input->written)
376 end_pos = input->written;
377
378 CYPHER_PARAM* params = NULL;
379 Malloc(params, CYPHER_PARAM, 1);
380 params->input = input;
381 params->dest = dest;
382 params->key = key;
383 params->encipher = encipher;
384 params->start_pos = it;
385 params->end_pos = end_pos;
386
387 if (add_threaded_process(thread_pool, &n_vigenere_cypher_thread, (void*)(intptr_t)params, DIRECT_PROC) == FALSE) {
388 n_log(LOG_ERR, "Error adding n_vigenere_cypher_thread to thread pool");
389 Free(params);
390 }
392 usleep(1);
393 }
394 if (end_pos < input->written) {
395 CYPHER_PARAM* params = NULL;
396 Malloc(params, CYPHER_PARAM, 1);
397 params->input = input;
398 params->dest = dest;
399 params->key = key;
400 params->encipher = encipher;
401 params->start_pos = end_pos;
402 params->end_pos = input->written;
403
404 if (add_threaded_process(thread_pool, &n_vigenere_cypher_thread, (void*)(intptr_t)params, DIRECT_PROC) == FALSE) {
405 n_log(LOG_ERR, "Error adding n_vigenere_cypher_thread to thread pool");
406 Free(params);
407 }
409 usleep(1);
410 }
413
414 /*
415 uint32_t nonAlphaCharCount = 0;
416 for( uint32_t i = 0 ; i < input -> written ; i++ )
417 {
418 char ch = input -> data[ i ];
419 if( isalpha( input -> data[ i ] ) )
420 {
421 bool cIsUpper = isupper( input -> data[ i ] );
422 char offset = cIsUpper ? 'A' : 'a';
423 int keyIndex = ( i - nonAlphaCharCount ) % key -> written ;
424 int k = ( cIsUpper ? toupper( key -> data[ keyIndex % key -> written ] ) : tolower( key -> data[ keyIndex % key -> written ] ) ) - offset ;
425 k = encipher ? k : -k;
426 ch = (char)( ( n_mathmod( ( ( input -> data[ i ] + k ) - offset ) , 26 ) ) + offset );
427 }
428 else
429 {
430 ++nonAlphaCharCount;
431 }
432 dest -> data[ i ] = ch;
433 }
434 */
435
436 return dest;
437}
438
446 return n_vigenere_cypher(string, key, 1, 0);
447}
448
456 return n_vigenere_cypher(string, key, 0, 0);
457}
458
466 return n_vigenere_cypher(string, key, 1, 1);
467}
468
476 return n_vigenere_cypher(string, key, 0, 1);
477}
478
485 N_STR* __internal_root_key_nstr = char_to_nstr(__internal_root_key);
486
487 N_STR* base64_data = n_base64_encode(decoded_data);
488 N_STR* encoded_data = n_vigenere_encode_in_place(base64_data, __internal_root_key_nstr);
489 free_nstr(&__internal_root_key_nstr);
490
491 return encoded_data;
492}
493
500 N_STR* __internal_root_key_nstr = char_to_nstr(__internal_root_key);
501
502 N_STR* decoded_data = n_vigenere_decode(encoded_data, __internal_root_key_nstr);
503 N_STR* base64_data = n_base64_decode(decoded_data);
504
505 free_nstr(&__internal_root_key_nstr);
506 free_nstr(&decoded_data);
507
508 return base64_data;
509}
510
516N_STR* n_vigenere_get_question(size_t question_size) {
517 size_t it = 0;
518
519 if ((question_size % 2) != 0) {
520 n_log(LOG_ERR, "odd number of elements for question size, %zu", question_size);
521 return NULL;
522 }
523
524 N_STR* output = NULL;
526
527 N_STR* cpu_id = n_get_cpu_id();
528 nstrprintf_cat(output, "@%s", _nstr(cpu_id));
529 free_nstr(&cpu_id);
530
531 if (output) {
532 N_STR* tmpoutput = n_base64_encode(output);
533 free_nstr(&output);
534 output = new_nstr(question_size + 1);
535 for (it = 0; it < question_size; it++) {
536 int val = tmpoutput->data[it % tmpoutput->written];
537 while (val < 'A') val += 26;
538 while (val > 'Z') val -= 26;
539 output->data[it] = (char)val;
540 }
541 output->written = question_size;
542 free_nstr(&tmpoutput);
543 } else {
544 // generate random
545 output = new_nstr(question_size + 1);
546 for (it = 0; it < question_size; it++) {
547 int val = rand() % 256;
548 while (val < 'A') val += 26;
549 while (val > 'Z') val -= 26;
550 output->data[it] = (char)val;
551 }
552 }
553
554 N_STR* encoded_data = n_vigenere_quick_encode(output);
555 free_nstr(&output);
556
557 if (!encoded_data) {
558 return NULL;
559 }
560
561 it = 0;
562 while (it < encoded_data->written) {
563 if (encoded_data->data[it] == '=') {
564 encoded_data->data[it] = '\0';
565 }
566 it++;
567 }
568 return encoded_data;
569}
570
577N_STR* n_vigenere_get_answer(N_STR* root_key, N_STR* question) {
578 __n_assert(root_key, return NULL);
579 __n_assert(question, return NULL);
580
581 N_STR* decoded_question = n_vigenere_quick_decode(question);
582
583 if (!decoded_question) {
584 return NULL;
585 }
586
587 if (root_key->written < decoded_question->written) {
588 n_log(LOG_ERR, "root key of size %zu is lower than question key of size %zu", root_key->written, decoded_question->written);
589 free_nstr(&decoded_question);
590 return NULL;
591 }
592 if ((root_key->written % decoded_question->written) != 0) {
593 n_log(LOG_ERR, "question key of size %zu is not a multiple of root key of size %zu", decoded_question->written, root_key->written);
594 free_nstr(&decoded_question);
595 return NULL;
596 }
597
598 N_STR* answer = new_nstr(root_key->length + 1);
599 for (uint32_t it = 0; it < root_key->written; it++) {
600 int32_t val = (root_key->data[it % root_key->written] - 'A') + (decoded_question->data[it % decoded_question->written] - 'A');
601 val = val % 26;
602 val = val + 'A';
603 answer->data[it] = (char)val;
604 }
605 free_nstr(&decoded_question);
606 answer->written = root_key->written;
607 return answer;
608}
609
618 __n_assert(in, return FALSE);
619 __n_assert(out, return FALSE);
620 __n_assert(key, return FALSE);
621
622 if ((key->written % 2) != 0) {
623 n_log(LOG_ERR, "odd number of elements in key %s, %zu", _nstr(key), key->written);
624 return FALSE;
625 }
626
627 N_STR* file = file_to_nstr(_nstr(in));
628 if (!file) {
629 n_log(LOG_ERR, "error loading %s", _nstr(in));
630 return FALSE;
631 }
632
633 N_STR* base64_data = n_base64_encode(file);
634 free_nstr(&file);
635
636 if (!base64_data) {
637 n_log(LOG_ERR, "Could not base64 decode data from file %s", _nstr(in));
638 return FALSE;
639 }
640
641 N_STR* encoded_data = n_vigenere_encode_in_place(base64_data, key);
642 if (!encoded_data) {
643 n_log(LOG_ERR, "Could not decode data from file %s", _nstr(in));
644 free_nstr(&base64_data);
645 return FALSE;
646 }
647
648 int ret = nstr_to_file(encoded_data, _nstr(out));
649 free_nstr(&encoded_data);
650
651 return ret;
652}
653
662 __n_assert(in, return FALSE);
663 __n_assert(out, return FALSE);
664 __n_assert(key, return FALSE);
665
666 if ((key->written % 2) != 0) {
667 n_log(LOG_ERR, "odd number of elements in key %s , %zu", _nstr(key), key->written);
668 return FALSE;
669 }
670
671 N_STR* file = file_to_nstr(_nstr(in));
672 if (!file) {
673 n_log(LOG_ERR, "error loading %s", _nstr(in));
674 return FALSE;
675 }
676
677 N_STR* decoded_data = n_vigenere_decode_in_place(file, key);
678 if (!decoded_data) {
679 n_log(LOG_ERR, "Could not decode data from file %s", _nstr(in));
680 free_nstr(&file);
681 return FALSE;
682 }
683
684 N_STR* base64_data = n_base64_decode(decoded_data);
685 free_nstr(&decoded_data);
686 if (!base64_data) {
687 n_log(LOG_ERR, "Could not base64 decode data from file %s", _nstr(in));
688 return FALSE;
689 }
690
691 int ret = nstr_to_file(base64_data, _nstr(out));
692 free_nstr(&base64_data);
693
694 return ret;
695}
696
704N_STR* n_vigenere_encode_qa(N_STR* input_data, N_STR* question, N_STR* answer) {
705 __n_assert(input_data, return NULL);
706 __n_assert(question, return NULL);
707 __n_assert(answer, return NULL);
708
709 N_STR* b64_encoded = n_base64_encode(input_data);
710 if (!b64_encoded) {
711 n_log(LOG_ERR, "could not base64 decode: input_data %p", input_data);
712 return NULL;
713 }
714
715 N_STR* encoded_with_answer_data = n_vigenere_encode_in_place(b64_encoded, answer);
716 if (!encoded_with_answer_data) {
717 n_log(LOG_ERR, "could not encode: input_data %p", input_data);
718 free_nstr(&b64_encoded);
719 return NULL;
720 }
721
722 N_STR* decoded_question = n_vigenere_quick_decode(question);
723 if (!decoded_question) {
724 n_log(LOG_ERR, "could not decode question");
725 /* encoded_with_answer_data is b64_encoded (in-place), already modified */
726 free_nstr(&encoded_with_answer_data);
727 return NULL;
728 }
729 N_STR* encoded_data = n_vigenere_decode_in_place(encoded_with_answer_data, decoded_question);
730 free_nstr(&decoded_question);
731 if (!encoded_data) {
732 n_log(LOG_ERR, "could not decode: input_data %p", input_data);
733 return NULL;
734 }
735
736 return encoded_data;
737}
738
746N_STR* n_vigenere_decode_qa(N_STR* input_data, N_STR* question, N_STR* answer) {
747 __n_assert(input_data, return NULL);
748 __n_assert(input_data->data, return NULL);
749 __n_assert(question, return NULL);
750 __n_assert(question->data, return NULL);
751 __n_assert(answer, return NULL);
752 __n_assert(answer->data, return NULL);
753
754 N_STR* decoded_question = n_vigenere_quick_decode(question);
755
756 N_STR* encoded_data = n_vigenere_encode(input_data, decoded_question);
757 free_nstr(&decoded_question);
758 if (!encoded_data) {
759 n_log(LOG_ERR, "could not encode input_data %p", input_data);
760 return NULL;
761 }
762 n_log(LOG_DEBUG, "encoded_data: %zu/%zu", encoded_data->written, encoded_data->length);
763
764 N_STR* encoded_with_answer_data = n_vigenere_decode_in_place(encoded_data, answer);
765 if (!encoded_with_answer_data) {
766 n_log(LOG_ERR, "could not decode: input_data %p", input_data);
767 return NULL;
768 }
769 n_log(LOG_DEBUG, "encoded_with_answer_data: %zu/%zu", encoded_with_answer_data->written, encoded_with_answer_data->length);
770
771 N_STR* b64_decoded = n_base64_decode(encoded_with_answer_data);
772 free_nstr(&encoded_with_answer_data);
773 if (!b64_decoded) {
774 n_log(LOG_ERR, "could not base64 decode: input_data %p", input_data);
775 return NULL;
776 }
777 n_log(LOG_DEBUG, "b64_decoded: %zu/%zu", b64_decoded->written, b64_decoded->length);
778 return b64_decoded;
779}
780
789int n_vigenere_decode_file_qa(N_STR* in, N_STR* out, N_STR* question, N_STR* answer) {
790 __n_assert(in, return FALSE);
791 __n_assert(out, return FALSE);
792 __n_assert(question, return FALSE);
793 __n_assert(answer, return FALSE);
794
795 N_STR* input_data = file_to_nstr(_nstr(in));
796 if (!input_data) {
797 n_log(LOG_ERR, "could not load input file %s", _nstr(in));
798 return FALSE;
799 }
800
801 N_STR* decoded = n_vigenere_decode_qa(input_data, question, answer);
802 free_nstr(&input_data);
803
804 int ret = nstr_to_file(decoded, _nstr(out));
805 free_nstr(&decoded);
806
807 return ret;
808}
809
818int n_vigenere_encode_file_qa(N_STR* in, N_STR* out, N_STR* question, N_STR* answer) {
819 __n_assert(in, return FALSE);
820 __n_assert(out, return FALSE);
821 __n_assert(question, return FALSE);
822 __n_assert(answer, return FALSE);
823
824 N_STR* input_data = file_to_nstr(_nstr(in));
825 if (!input_data) {
826 n_log(LOG_ERR, "could not load input file %s", _nstr(in));
827 return FALSE;
828 }
829
830 N_STR* encoded = n_vigenere_encode_qa(input_data, question, answer);
831 free_nstr(&input_data);
832
833 int ret = nstr_to_file(encoded, _nstr(out));
834 free_nstr(&encoded);
835
836 return ret;
837}
THREAD_POOL * thread_pool
Definition ex_fluid.c:77
char * key
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition n_common.h:203
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:278
#define _str(__PTR)
define true
Definition n_common.h:192
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:262
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
Definition n_common.h:198
int n_popen(char *cmd, size_t read_buf_size, void **nstr_output, int *ret)
launch a command and return output and status
Definition n_common.c:179
char n_tolower(char c)
convert char to lower case
Definition n_base64.c:142
N_STR * n_base64_decode(N_STR *bufcoded)
decode a N_STR *string
Definition n_base64.c:187
bool n_isupper(char c)
test if char c is uppercase
Definition n_base64.c:104
char n_toupper(char c)
convert char to upper case
Definition n_base64.c:131
bool n_islower(char c)
test if char c is lowercase
Definition n_base64.c:113
N_STR * n_base64_encode(N_STR *input)
encode a N_STR *string
Definition n_base64.c:269
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:704
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:789
N_STR * n_vigenere_quick_encode(N_STR *decoded_data)
quick encode data
Definition n_crypto.c:484
N_STR * n_vigenere_decode(N_STR *string, N_STR *key)
decode input using vigenere cypher and key
Definition n_crypto.c:455
#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:44
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:577
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:818
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:746
N_STR * n_vigenere_encode(N_STR *string, N_STR *key)
encode input using vigenere cypher and key
Definition n_crypto.c:445
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:617
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:661
N_STR * n_vigenere_get_rootkey(size_t rootkey_size)
get a rootkey randomly generated
Definition n_crypto.c:68
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:516
N_STR * n_vigenere_quick_decode(N_STR *encoded_data)
quick decode data
Definition n_crypto.c:499
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:88
#define LOG_DEBUG
debug-level messages
Definition n_log.h:83
#define LOG_ERR
error conditions
Definition n_log.h:75
size_t written
size of the written data inside the string
Definition n_str.h:66
char * data
the string
Definition n_str.h:62
size_t length
length of string (in case we wanna keep information after the 0 end of string value)
Definition n_str.h:64
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:201
int nstr_to_file(N_STR *str, char *filename)
Write a N_STR content into a file.
Definition n_str.c:416
N_STR * nstrdup(N_STR *str)
Duplicate a N_STR.
Definition n_str.c:708
#define nstrprintf_cat(__nstr_var, __format,...)
Macro to quickly allocate and sprintf and cat to a N_STR.
Definition n_str.h:119
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
Definition n_str.c:254
N_STR * new_nstr(NSTRBYTE size)
create a new N_STR string
Definition n_str.c:206
#define nstrprintf(__nstr_var, __format,...)
Macro to quickly allocate and sprintf to N_STR.
Definition n_str.h:115
N_STR * file_to_nstr(char *filename)
Load a whole file into a N_STR.
Definition n_str.c:286
A box including a string and his lenght.
Definition n_str.h:60
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 wait_for_threaded_pool(THREAD_POOL *thread_pool)
Wait for the thread pool to become idle (no active threads, empty waiting list), blocking without pol...
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
Structure of a thread 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:61
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:319
bool encipher
1 encoding, 0 decoding
Definition n_crypto.c:253
void * n_vigenere_cypher_thread(void *ptr)
encode or decode threaded func using params from ptr
Definition n_crypto.c:265
N_STR * n_vigenere_decode_in_place(N_STR *string, N_STR *key)
decode input using vigenere cypher and key
Definition n_crypto.c:475
N_STR * dest
destination
Definition n_crypto.c:249
N_STR * n_get_current_dir_hd_serial()
get the serial of the current running dir hosting disk
Definition n_crypto.c:93
size_t start_pos
input starting point
Definition n_crypto.c:255
N_STR * input
input data
Definition n_crypto.c:247
N_STR * key
encoding/decoding key
Definition n_crypto.c:251
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:465
size_t end_pos
output starting point
Definition n_crypto.c:257
N_STR * n_get_cpu_id()
get the CPU id
Definition n_crypto.c:156
structure of a n_vigenere_cypher_thread param
Definition n_crypto.c:245
Vigenere encoding and decoding functions using N_STR/files.
Thread pool declaration.