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