Nilorea Library
C utilities for networking, threading, graphics
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{
51 if( ( rootkey_size % 2 ) != 0 )
52 {
53 n_log( LOG_ERR , "key size %d is not a multiple of 2" , rootkey_size );
54 return NULL ;
55 }
56 N_STR *output = new_nstr( rootkey_size + 1 );
57 srand( time( NULL ) );
58 for( size_t it = 0 ; it < rootkey_size ; it ++ )
59 {
60 output -> data[ it ] = 'A' + rand()%26 ;
61 }
62 output -> written = rootkey_size ;
63 return output ;
64}
65
66
67
73{
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 {
83 nstrprintf( output , "%X" , (unsigned int)dwVolSerial );
84 } else {
85 n_log( LOG_ERR , "Could not retrieve current directory disk serial id" );
86 return NULL ;
87 }
88 return output ;
89#elif __linux__
90 char *curdir = getcwd( NULL , 0 );
91
92 N_STR *cmd = NULL ;
93 nstrprintf( cmd , "df -T \"%s\" | awk '/^\\/dev/ {print $1}'" , curdir );
94 Free( curdir );
95
96 N_STR *cur_disk = NULL ;
97 int ret = -1 ;
98 n_popen( _nstr( cmd ) , 1024 , (void **)&cur_disk , &ret );
99 free_nstr( &cmd );
100
101 nstrprintf( cmd , "udevadm info --query=all --name=%s | egrep '(ID_SERIAL=|ID_FS_UUID=)' | awk -F'=' '{print $2}'" , _nstr( cur_disk ) );
102 n_popen( _nstr( cmd ) , 1024 , (void **)&output , &ret );
103 free_nstr( &cmd );
104
105 N_STR *hd_serial = NULL ;
106 nstrprintf( hd_serial , "%s=>%s" , _nstr( output ) , _nstr( cur_disk ) );
107
108 free_nstr( &output );
109 free_nstr( &cur_disk );
110
111 for( unsigned int it = 0 ; it < hd_serial -> written ; it ++ )
112 {
113 if( isspace( hd_serial -> data[ it ] ) )
114 {
115 hd_serial -> data[ it ] = '-' ;
116 }
117 }
118 return hd_serial ;
119
120#else // SOLARIS
121
122 char *curdir = getcwd( NULL , 0 );
123 N_STR *hostid = NULL ;
124 int ret = -1 ;
125 n_popen( "hostid" , 1024 , (void **)&hostid , &ret );
126 Free( curdir );
127 nstrprintf( output , "%s:%s" , _nstr( hostid ) , _str( curdir ) );
128 return output;
129
130#endif
131}
132
133
134
140{
141 N_STR *cpu_id = NULL ;
142
143#ifndef __sun
144
145 unsigned int a[ 4 ] = { 0 , 0 , 0 , 0 };
146
147 if( __get_cpuid_max( 0 , NULL ) >= 1 )
148 {
149 __get_cpuid( 1 , &a[ 0 ] , &a[ 1 ] , &a[ 2 ] , &a[ 3 ] );
150 }
151 else
152 {
153 return char_to_nstr( "1234-1234-1234-12" );
154 }
155
156 int binaryNum[ 30 ];
157 memset( &binaryNum , 0 , sizeof( binaryNum ) );
158
159 int n = a[ 3 ];
160 int i = 0;
161 while (n > 0) {
162
163 // storing remainder in binary array
164 binaryNum[i] = n % 2;
165 n = n / 2;
166 i++;
167 }
168 n = a[ 2 ];
169 i = 0;
170 while (n > 0) {
171
172 // storing remainder in binary array
173 binaryNum[i] = n % 2;
174 n = n / 2;
175 i++;
176 }
177 n = a[ 1 ];
178 i = 0;
179 while (n > 0) {
180
181 // storing remainder in binary array
182 binaryNum[i] = n % 2;
183 n = n / 2;
184 i++;
185 }
186 n = a[ 0 ];
187 i = 0;
188 while (n > 0) {
189
190 // storing remainder in binary array
191 binaryNum[i] = n % 2;
192 n = n / 2;
193 i++;
194 }
195
196 /* printf("-------Signature(EAX register):-------");
197 printf("\nStepping ID:%d%d%d%d",binaryNum[3],binaryNum[2],binaryNum[1],binaryNum[0]);
198 printf("\nModel:%d%d%d%d",binaryNum[7],binaryNum[6],binaryNum[5],binaryNum[4]);
199 printf("\nFamily ID:%d%d%d%d",binaryNum[11],binaryNum[10],binaryNum[9],binaryNum[8]);
200 printf("\nProcessor Type:%d%d",binaryNum[13],binaryNum[12]);
201 printf("\nExtended Model ID:%d%d%d%d",binaryNum[19],binaryNum[18],binaryNum[17],binaryNum[16]);
202 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]);
203 printf("\n"); */
204 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] ,
205 binaryNum[7],binaryNum[6],binaryNum[5],binaryNum[4] ,
206 binaryNum[11],binaryNum[10],binaryNum[9],binaryNum[8] ,
207 binaryNum[13],binaryNum[12] );
208
209#else //We are on SOLARIS
210 static const char devname[] = "/dev/cpu/self/cpuid";
211
212 struct {
213 uint32_t r_eax, r_ebx, r_ecx, r_edx;
214 } _r, *rp = &_r;
215 int d;
216 char *s = NULL ;
217
218 if ((d = open(devname, O_RDONLY)) != -1)
219 {
220 if (pread(d, rp, sizeof (*rp), 0) == sizeof (*rp))
221 {
222 s = (char *)&rp->r_ebx;
223 }
224 close( d );
225 }
226 nstrprintf( cpu_id , "%s", s );
227#endif
228 return cpu_id ;
229}
230
232typedef struct CYPHER_PARAM
233{
241 bool encipher ;
243 size_t start_pos ;
245 size_t end_pos ;
246} CYPHER_PARAM ;
247
248
254void *n_vigenere_cypher_thread( void *ptr )
255{
256 __n_assert( ptr , return NULL );
257 CYPHER_PARAM *params = (CYPHER_PARAM *)ptr ;
258 __n_assert( params , return NULL );
259
260 N_STR *input = params -> input ;
261 N_STR *dest = params -> dest ;
262 N_STR *key = params -> key ;
263
264 __n_assert( input , return NULL );
265 __n_assert( input -> data , return NULL );
266 __n_assert( key , return NULL );
267 __n_assert( key -> data , return NULL );
268
269 bool encipher = params -> encipher ;
270
271 size_t start_pos = params -> start_pos ;
272 size_t end_pos = params -> end_pos ;
273
274 bool cIsUpper = 0 ;
275 uint8_t keyIndexMod = 0;
276 if( encipher )
277 {
278 for( size_t i = start_pos ; i < end_pos ; i++ )
279 {
280 if( ( cIsUpper = n_isupper( input -> data[ i ] ) ) || ( n_islower( input -> data[ i ] ) ) )
281 {
282 char offset = cIsUpper ? 'A' : 'a';
283 keyIndexMod= i % key -> written ;
284 int k = ( cIsUpper ? n_toupper( key -> data[ keyIndexMod ] ) : n_tolower( key -> data[ keyIndexMod ] ) ) - offset ;
285 dest -> data[ i ] = (char)( ( n_mathmod( ( ( input -> data[ i ] + k ) - offset ) , 26 ) ) + offset );
286 }
287 }
288 }
289 else
290 {
291 for( uint32_t i = start_pos ; i < end_pos ; i++ )
292 {
293 if( ( cIsUpper = n_isupper( input -> data[ i ] ) ) || ( n_islower( input -> data[ i ] ) ) )
294 {
295 char offset = cIsUpper ? 'A' : 'a';
296 keyIndexMod= i % key -> written ;
297 int k = ( cIsUpper ? n_toupper( key -> data[ keyIndexMod ] ) : n_tolower( key -> data[ keyIndexMod ] ) ) - offset ;
298 dest -> data[ i ] = (char)( ( n_mathmod( ( ( input -> data[ i ] - k ) - offset ) , 26 ) ) + offset );
299 }
300 }
301 }
302 Free( params );
303 return NULL ;
304}
305
306
307
308
317N_STR *n_vigenere_cypher( N_STR *input , N_STR *key , bool encipher , bool in_place )
318{
319 __n_assert( input , return NULL );
320 __n_assert( input -> data , return NULL );
321 __n_assert( key , return NULL );
322 __n_assert( key -> data , return NULL );
323
324 if( input -> written == 0 )
325 {
326 n_log( LOG_ERR , "empty input, written=0" );
327 return NULL ;
328 }
329
330 if( ( key -> written % 2 ) != 0 )
331 {
332 n_log( LOG_ERR , "odd number of elements in root key %s, %d" , _nstr( key ) , key -> written );
333 return NULL ;
334 }
335
336 for( size_t i = 0 ; i < key -> written ; ++i )
337 {
338 if( !isalpha( key -> data[ i ] ) )
339 {
340 n_log( LOG_ERR , "key contain bad char '%c'" , key -> data[ i ] );
341 return NULL ; // Error
342 }
343 }
344
345 N_STR *dest = NULL ;
346 if( in_place)
347 {
348 dest = input ;
349 }
350 else
351 {
352 dest = nstrdup( input );
353 }
354
355#ifdef __windows__
356 SYSTEM_INFO sysinfo;
357 GetSystemInfo(&sysinfo);
358 int nb_procs = sysinfo.dwNumberOfProcessors;
359#else
360 int nb_procs = get_nprocs();
361#endif
362
363
364 if( nb_procs < 2 )
365 nb_procs = 2 ;
366
367 THREAD_POOL *thread_pool = new_thread_pool( nb_procs , 0 );
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 {
375 end_pos = it + bytesteps ;
376
377 if( end_pos >= input -> written )
378 end_pos = input -> written ;
379
380 CYPHER_PARAM *params = NULL ;
381 Malloc( params , CYPHER_PARAM , 1 );
382 params -> input = input ;
383 params -> dest = dest ;
384 params -> key = key ;
385 params -> encipher = encipher ;
386 params -> start_pos = it ;
387 params -> end_pos = end_pos ;
388
389 if( add_threaded_process( thread_pool, &n_vigenere_cypher_thread, (void *)(intptr_t)params, DIRECT_PROC) == FALSE )
390 {
391 n_log( LOG_ERR, "Error adding n_vigenere_cypher_thread to thread pool" );
392 }
393 refresh_thread_pool( thread_pool );
394 usleep( 1 );
395
396 }
397 if( end_pos < input -> written )
398 {
399 CYPHER_PARAM *params = NULL ;
400 Malloc( params , CYPHER_PARAM , 1 );
401 params -> input = input ;
402 params -> dest = dest ;
403 params -> key = key ;
404 params -> encipher = encipher ;
405 params -> start_pos = end_pos ;
406 params -> end_pos = input -> written ;
407
408 if( add_threaded_process( thread_pool, &n_vigenere_cypher_thread, (void *)(intptr_t)params, DIRECT_PROC) == FALSE )
409 {
410 n_log( LOG_ERR, "Error adding n_vigenere_cypher_thread to thread pool" );
411 }
412 refresh_thread_pool( thread_pool );
413 usleep( 1 );
414 }
415 wait_for_threaded_pool( thread_pool, 100000 );
416 destroy_threaded_pool( &thread_pool, 100000 );
417
418 /*
419 uint32_t nonAlphaCharCount = 0;
420 for( uint32_t i = 0 ; i < input -> written ; i++ )
421 {
422 char ch = input -> data[ i ];
423 if( isalpha( input -> data[ i ] ) )
424 {
425 bool cIsUpper = isupper( input -> data[ i ] );
426 char offset = cIsUpper ? 'A' : 'a';
427 int keyIndex = ( i - nonAlphaCharCount ) % key -> written ;
428 int k = ( cIsUpper ? toupper( key -> data[ keyIndex % key -> written ] ) : tolower( key -> data[ keyIndex % key -> written ] ) ) - offset ;
429 k = encipher ? k : -k;
430 ch = (char)( ( n_mathmod( ( ( input -> data[ i ] + k ) - offset ) , 26 ) ) + offset );
431 }
432 else
433 {
434 ++nonAlphaCharCount;
435 }
436 dest -> data[ i ] = ch;
437 }
438 */
439
440 return dest;
441}
442
443
451{
452 return n_vigenere_cypher( string , key , 1 , 0 );
453}
454
455
456
464{
465 return n_vigenere_cypher( string , key , 0 , 0 );
466}
467
468
469
477{
478 return n_vigenere_cypher( string , key , 1 , 1 );
479}
480
481
482
490{
491 return n_vigenere_cypher( string , key , 0 , 1 );
492}
493
494
495
502{
503 N_STR *__internal_root_key_nstr = char_to_nstr( __internal_root_key );
504
505 N_STR *base64_data = n_base64_encode( decoded_data );
506 N_STR *encoded_data = n_vigenere_encode_in_place( base64_data , __internal_root_key_nstr );
507 free_nstr( &__internal_root_key_nstr );
508
509 return encoded_data ;
510}
511
512
513
520{
521 N_STR *__internal_root_key_nstr = char_to_nstr( __internal_root_key );
522
523 N_STR *decoded_data = n_vigenere_decode( encoded_data , __internal_root_key_nstr );
524 N_STR *base64_data = n_base64_decode( decoded_data );
525
526 free_nstr( &__internal_root_key_nstr );
527 free_nstr( &decoded_data );
528
529 return base64_data ;
530}
531
532
533
539N_STR *n_vigenere_get_question( size_t question_size )
540{
541 size_t it = 0 ;
542
543 if( ( question_size % 2 ) != 0 )
544 {
545 n_log( LOG_ERR , "odd number of elements for question size, %d" , question_size );
546 return NULL ;
547 }
548
549 N_STR *output = NULL ;
551
552 N_STR *cpu_id = n_get_cpu_id();
553 nstrprintf_cat( output , "@%s" , _nstr( cpu_id ) );
554 free_nstr( &cpu_id );
555
556 if( output )
557 {
558 N_STR *tmpoutput = n_base64_encode( output );
559 free_nstr( &output );
560 output = new_nstr( question_size + 1 );
561 for( it = 0 ; it < question_size ; it ++ )
562 {
563 output -> data[ it ] = tmpoutput -> data[ it % tmpoutput -> written ] ;
564 while( output -> data[ it ] < 'A' ) output -> data[ it ] += 26 ;
565 while( output -> data[ it ] > 'Z' ) output -> data[ it ] -= 26 ;
566 }
567 output -> written = question_size ;
568 free_nstr( &tmpoutput );
569 }
570 else
571 {
572 // generate random
573 output = new_nstr( question_size + 1 );
574 for( it = 0 ; it < question_size ; it ++ )
575 {
576 output -> data[ it ] = rand()%255 ;
577 while( output -> data[ it ] < 'A' ) output -> data[ it ] += 26 ;
578 while( output -> data[ it ] > 'Z' ) output -> data[ it ] -= 26 ;
579 }
580 }
581
582 N_STR *encoded_data = n_vigenere_quick_encode( output );
583 free_nstr( &output );
584
585 it = 0 ;
586 while( it < encoded_data -> written )
587 {
588 if( encoded_data -> data[ it ] == '=' )
589 {
590 encoded_data -> data[ it ] = '\0' ;
591 }
592 it ++ ;
593 }
594 return encoded_data ;
595}
596
597
598
605N_STR *n_vigenere_get_answer( N_STR *root_key , N_STR *question )
606{
607 __n_assert( root_key , return NULL );
608 __n_assert( question , return NULL );
609
610 N_STR *decoded_question = n_vigenere_quick_decode( question );
611
612 if( root_key -> written < decoded_question -> written )
613 {
614 n_log( LOG_ERR , "root key of size %d is lower than question key of size %d" , root_key -> written , decoded_question -> written );
615 free_nstr( &decoded_question );
616 return NULL ;
617 }
618 if( (root_key -> written % decoded_question -> written) != 0 )
619 {
620 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 );
621 free_nstr( &decoded_question );
622 return NULL ;
623 }
624
625 N_STR *answer = new_nstr( root_key -> length + 1 );
626 for( uint32_t it = 0 ; it < root_key -> written ; it ++ )
627 {
628 int32_t val = ( root_key -> data[ it % root_key -> written ] - 'A' ) + ( decoded_question -> data[ it % decoded_question -> written ] - 'A' );
629 val = val % 26 ;
630 val = val + 'A' ;
631 answer -> data[ it ] = val ;
632 }
633 free_nstr( &decoded_question );
634 answer -> written = root_key -> written ;
635 return answer ;
636}
637
638
639
640
648int n_vigenere_encode_file( N_STR *in , N_STR *out , N_STR *key )
649{
650 __n_assert( in , return FALSE );
651 __n_assert( out , return FALSE );
652 __n_assert( key , return FALSE );
653
654 if( ( key -> written % 2 ) != 0 )
655 {
656 n_log( LOG_ERR , "odd number of elements in key %s, %d" , _nstr( key ) , key -> written );
657 return FALSE ;
658 }
659
660 N_STR *file = file_to_nstr( _nstr( in ) );
661 if( !file )
662 {
663 n_log( LOG_ERR , "error loading %s" , _nstr( in ) );
664 return FALSE ;
665 }
666
667 N_STR *base64_data = n_base64_encode( file );
668 free_nstr( &file );
669
670 if( !base64_data )
671 {
672 n_log( LOG_ERR , "Could not base64 decode data from file %s" , _nstr( in ) );
673 return FALSE ;
674 }
675
676 N_STR *encoded_data = n_vigenere_encode_in_place( base64_data , key );
677 if( !encoded_data )
678 {
679 n_log( LOG_ERR , "Could not decode data from file %s" , _nstr( in ) );
680 return FALSE ;
681 }
682
683 int ret = nstr_to_file( encoded_data , _nstr( out ) );
684 free_nstr( &encoded_data );
685
686 return ret ;
687}
688
689
690
698int n_vigenere_decode_file( N_STR *in , N_STR *out , N_STR *key )
699{
700 __n_assert( in , return FALSE );
701 __n_assert( out , return FALSE );
702 __n_assert( key , return FALSE );
703
704 if( ( key -> written % 2 ) != 0 )
705 {
706 n_log( LOG_ERR , "odd number of elements in key %s , %d" , _nstr( key ) , key -> written );
707 return FALSE ;
708 }
709
710 N_STR *file = file_to_nstr( _nstr( in ) );
711 if( !file )
712 {
713 n_log( LOG_ERR , "error loading %s" , _nstr( in ) );
714 return FALSE ;
715 }
716
717 N_STR *decoded_data = n_vigenere_decode_in_place( file , key );
718 if( !decoded_data )
719 {
720 n_log( LOG_ERR , "Could not decode data from file %s" , _nstr( in ) );
721 return FALSE ;
722 }
723
724 N_STR *base64_data = n_base64_decode( decoded_data );
725 free_nstr( &decoded_data );
726 if( !base64_data )
727 {
728 n_log( LOG_ERR , "Could not base64 decode data from file %s" , _nstr( in ) );
729 return FALSE ;
730 }
731
732 int ret = nstr_to_file( base64_data , _nstr( out ) );
733 free_nstr( &base64_data );
734
735 return ret ;
736}
737
738
739
747N_STR *n_vigenere_encode_qa( N_STR *input_data , N_STR *question , N_STR *answer )
748{
749 __n_assert( input_data , return NULL );
750 __n_assert( question , return NULL );
751 __n_assert( answer , return NULL );
752
753 N_STR *b64_encoded = n_base64_encode( input_data );
754 if( !b64_encoded )
755 {
756 n_log( LOG_ERR , "could not base64 decode: input_data %p" , input_data );
757 return NULL ;
758 }
759
760 N_STR *encoded_with_answer_data = n_vigenere_encode_in_place( b64_encoded , answer );
761 if( !encoded_with_answer_data )
762 {
763 n_log( LOG_ERR , "could not encode: input_data %p" , input_data );
764 return NULL ;
765 }
766
767 N_STR *decoded_question = n_vigenere_quick_decode( question );
768 N_STR *encoded_data = n_vigenere_decode_in_place( encoded_with_answer_data , decoded_question );
769 free_nstr( &decoded_question );
770 if( !encoded_data )
771 {
772 n_log( LOG_ERR , "could not decode: input_data %p" , input_data );
773 return NULL ;
774 }
775
776 return encoded_data ;
777}
778
779
780
788N_STR *n_vigenere_decode_qa( N_STR *input_data , N_STR *question , N_STR *answer )
789{
790 __n_assert( input_data , return NULL );
791 __n_assert( input_data -> data , return NULL );
792 __n_assert( question , return NULL );
793 __n_assert( question -> data , return NULL );
794 __n_assert( answer , return NULL );
795 __n_assert( answer -> data , return NULL );
796
797 N_STR *decoded_question = n_vigenere_quick_decode( question );
798
799 N_STR *encoded_data = n_vigenere_encode( input_data , decoded_question );
800 free_nstr( &decoded_question );
801 if( !encoded_data )
802 {
803 n_log( LOG_ERR , "could not encode input_data %p" , input_data );
804 return NULL ;
805 }
806 n_log( LOG_DEBUG , "encoded_data: %d/%d" , encoded_data -> written , encoded_data -> length );
807
808 N_STR *encoded_with_answer_data = n_vigenere_decode_in_place( encoded_data , answer );
809 if( !encoded_with_answer_data )
810 {
811 n_log( LOG_ERR , "could not decode: input_data %p" , input_data );
812 return NULL ;
813 }
814 n_log( LOG_DEBUG , "encoded_with_answer_data: %d/%d" , encoded_with_answer_data -> written , encoded_with_answer_data -> length );
815
816 N_STR *b64_decoded = n_base64_decode( encoded_with_answer_data );
817 free_nstr( &encoded_with_answer_data );
818 if( !b64_decoded )
819 {
820 n_log( LOG_ERR , "could not base64 decode: input_data %p" , input_data );
821 return NULL ;
822 }
823 n_log( LOG_DEBUG , "b64_decoded: %d/%d" , b64_decoded -> written , b64_decoded -> length );
824 return b64_decoded ;
825}
826
827
828
837int n_vigenere_decode_file_qa( N_STR *in , N_STR *out , N_STR *question , N_STR *answer )
838{
839 __n_assert( in , return FALSE );
840 __n_assert( out , return FALSE );
841 __n_assert( question , return FALSE );
842 __n_assert( answer , return FALSE );
843
844 N_STR *input_data = file_to_nstr( _nstr( in ) );
845 if( !input_data )
846 {
847 n_log( LOG_ERR , "could not load input file %s" , _nstr( in ) );
848 return FALSE ;
849 }
850
851 N_STR *decoded = n_vigenere_decode_qa( input_data , question , answer );
852 free_nstr( &input_data );
853
854 int ret = nstr_to_file( decoded , _nstr( out ) );
855 free_nstr( &decoded );
856
857 return ret ;
858}
859
860
869int n_vigenere_encode_file_qa( N_STR *in , N_STR *out , N_STR *question , N_STR *answer )
870{
871 __n_assert( in , return FALSE );
872 __n_assert( out , return FALSE );
873 __n_assert( question , return FALSE );
874 __n_assert( answer , return FALSE );
875
876 N_STR *input_data = file_to_nstr( _nstr( in ) );
877 if( !input_data )
878 {
879 n_log( LOG_ERR , "could not load input file %s" , _nstr( in ) );
880 return FALSE ;
881 }
882
883 N_STR *encoded = n_vigenere_encode_qa( input_data , question , answer );
884 free_nstr( &input_data );
885
886 int ret = nstr_to_file( encoded , _nstr( out ) );
887 free_nstr( &encoded );
888
889 return ret ;
890}
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition: n_common.h:183
#define __n_assert(__ptr, __ret)
macro to assert things
Definition: n_common.h:276
#define _str(__PTR)
define true
Definition: n_common.h:172
int n_popen(char *cmd, int read_buf_size, void **nstr_output, int *ret)
launch a command abd return output and status
Definition: n_common.c:185
#define Free(__ptr)
Free Handler to get errors.
Definition: n_common.h:256
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
Definition: n_common.h:178
FORCE_INLINE char n_tolower(char c)
is_alpha
Definition: n_base64.c:140
N_STR * n_base64_decode(N_STR *bufcoded)
decode a N_STR *string
Definition: n_base64.c:174
FORCE_INLINE bool n_isupper(char c)
test if char c is uppercase
Definition: n_base64.c:90
FORCE_INLINE char n_toupper(char c)
is_alpha
Definition: n_base64.c:126
FORCE_INLINE bool n_islower(char c)
test if char c is lowercase
Definition: n_base64.c:102
N_STR * n_base64_encode(N_STR *input)
encode a N_STR *string
Definition: n_base64.c:249
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:747
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:837
N_STR * n_vigenere_quick_encode(N_STR *decoded_data)
quick encode data
Definition: n_crypto.c:501
N_STR * n_vigenere_decode(N_STR *string, N_STR *key)
decode input using vigenere cypher and key
Definition: n_crypto.c:463
#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:605
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:869
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:788
N_STR * n_vigenere_encode(N_STR *string, N_STR *key)
encode input using vigenere cypher and key
Definition: n_crypto.c:450
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:648
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:698
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:539
N_STR * n_vigenere_quick_decode(N_STR *encoded_data)
quick decode data
Definition: n_crypto.c:519
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition: n_log.h:74
#define LOG_DEBUG
debug-level messages
Definition: n_log.h:66
#define LOG_ERR
error conditions
Definition: n_log.h:58
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition: n_str.h:222
#define nstrprintf(__nstr_var,...)
Macro to quickly allocate and sprintf to N_STR *.
Definition: n_str.h:97
int nstr_to_file(N_STR *str, char *filename)
Write a N_STR content into a file.
Definition: n_str.c:459
N_STR * nstrdup(N_STR *str)
Duplicate a N_STR.
Definition: n_str.c:795
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
Definition: n_str.c:273
N_STR * new_nstr(NSTRBYTE size)
create a new N_STR string
Definition: n_str.c:215
#define nstrprintf_cat(__nstr_var,...)
Macro to quickly allocate and sprintf and cat to a 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:332
A box including a string and his lenght.
Definition: n_str.h:173
int destroy_threaded_pool(THREAD_POOL **pool, int delay)
delete a thread_pool, exit the threads and free the structs
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
#define DIRECT_PROC
processing mode for added func, direct start
Definition: n_thread_pool.h:29
int wait_for_threaded_pool(THREAD_POOL *thread_pool, int delay)
Wait for all the launched process in the thread pool to terminate.
THREAD_POOL * new_thread_pool(int nbmaxthr, int nb_max_waiting)
Create a new pool of nbmaxthr threads.
Structure of a trhead pool.
Definition: n_thread_pool.h:81
base64 encoding and decoding functions using N_STR
Common headers and low-level hugly 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:317
bool encipher
1 encoding, 0 decoding
Definition: n_crypto.c:241
void * n_vigenere_cypher_thread(void *ptr)
encode or decode threaded func using params from ptr
Definition: n_crypto.c:254
N_STR * n_vigenere_decode_in_place(N_STR *string, N_STR *key)
decode input using vigenere cypher and key
Definition: n_crypto.c:489
N_STR * dest
destination
Definition: n_crypto.c:237
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:243
N_STR * input
input data
Definition: n_crypto.c:235
N_STR * key
encoding/decoding key
Definition: n_crypto.c:239
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:476
size_t end_pos
output starting point
Definition: n_crypto.c:245
N_STR * n_get_cpu_id()
get the CPU id
Definition: n_crypto.c:139
structure of a n_vigenere_cypher_thread param
Definition: n_crypto.c:233
vigenere encoding and decoding functions using N_STR/files
Thread pool declaration.