├── third-party └── DONT_DELETE_THIS_FOLDER ├── test ├── main.c ├── testutil.h └── testutil.c ├── .gitignore ├── README.md ├── LICENSE ├── CHANGELOG.md ├── src ├── logger.c ├── rnd.c ├── utility.c └── nakamoto.c ├── include ├── utility.h ├── rnd.h ├── logger.h ├── nakamoto.h └── test │ └── asserts.h ├── main.c └── Makefile /third-party/DONT_DELETE_THIS_FOLDER: -------------------------------------------------------------------------------- 1 | Don't delete this folder 2 | -------------------------------------------------------------------------------- /test/main.c: -------------------------------------------------------------------------------- 1 | #include "testutil.h" 2 | 3 | int main(int argc, char **argv) 4 | { 5 | TITLE_MSG("Initializing tests") 6 | 7 | C_ASSERT_EQUAL_STRING( 8 | VERSION_MAJOR_STR"."VERSION_MINOR_STR"."VERSION_REVISION_STR, 9 | get_version_str(), 10 | CTEST_SETTER( 11 | CTEST_TITLE("Check Nakamoto version ...") 12 | ) 13 | ) 14 | 15 | TEST_check_digest(); 16 | TEST_entropy(); 17 | TEST_generate_random_pass(); 18 | TEST_decryption_aes_256( 19 | TEST_encryption_aes_256() 20 | ); 21 | TEST_password_strength(); 22 | TEST_pbkdf2(); 23 | TEST_argon2id(); 24 | 25 | end_tests(); 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | *.t 39 | nakamoto 40 | 41 | .vscode/ 42 | 43 | #projects 44 | *.nkm 45 | out.txt 46 | 47 | # Debug files 48 | *.dSYM/ 49 | *.su 50 | *.idb 51 | *.pdb 52 | 53 | # Kernel Module Compile Results 54 | *.mod* 55 | *.cmd 56 | .tmp_versions/ 57 | modules.order 58 | Module.symvers 59 | Mkfile.old 60 | dkms.conf 61 | 62 | #folders 63 | third-party/openssl 64 | 65 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Nakamoto 2 | 3 | Nakamoto is a 2 layer encryption tool to protect your data and your cryptocurrency private keys 4 | 5 | ## Usage 6 | 7 | There are 3 types of usage: 8 | 9 | - Generate random passwords 10 | 11 | Example: 12 | 13 | ``` 14 | ./nakamoto g 15 | ``` 16 | 17 | - Encrypt file 18 | 19 | Example: 20 | 21 | Encrypt file _myfile.txt_ 22 | 23 | ``` 24 | ./nakamoto enc myfile.txt 25 | ``` 26 | 27 | - Decrypt file 28 | 29 | Decrypt file _out.nkm_ 30 | 31 | ``` 32 | ./nakamoto dec out.nkm 33 | ``` 34 | 35 | ## Building 36 | 37 | ### Requirements 38 | 39 | - Openssl 3.3 or later 40 | - CMake 4.3 or later 41 | 42 | ### First time installation 43 | 44 | You need to install Open SSL. 45 | 46 | ```sh 47 | make install_ssl 48 | ``` 49 | 50 | **NOTE** You only need to do this step once 51 | 52 | ### Build nakamoto 53 | 54 | ```sh 55 | make 56 | ``` 57 | 58 | ## Cleaning 59 | 60 | ### Cleaning project 61 | 62 | ``` 63 | make clean 64 | ``` 65 | 66 | ### Cleaning Compiled Openssl libs 67 | 68 | ``` 69 | make remove_ssl 70 | ``` 71 | 72 | ### Test project 73 | 74 | ``` 75 | make test 76 | ``` 77 | 78 | Have fun :) 79 | 80 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Fábio Pereira da Silva 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /test/testutil.h: -------------------------------------------------------------------------------- 1 | #ifndef TESTUTIL_H 2 | #define TESTUTIL_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #define VERSION_MAJOR_STR "0" 12 | #define VERSION_MINOR_STR "1" 13 | #define VERSION_REVISION_STR "5" 14 | 15 | #define STR_CONST(str) str, (sizeof(str)-1) 16 | #define VEC_CONST(vec) vec, sizeof(vec) 17 | #define CLEAR_VECTOR(vec) memset(vec, 0, sizeof(vec)); 18 | #define COMPARE_VECTORS(vec1, vec2) is_vec_content_eq(vec1, sizeof(vec1), vec2, sizeof(vec2)) 19 | #define STR_SIZE(str) (sizeof(str)-1) 20 | 21 | struct test_encryption_aes_256_t { 22 | uint8_t iv[16]; 23 | uint8_t priv_key[32]; 24 | uint8_t *encrypted_data; 25 | size_t encrypted_data_size; 26 | const char *message; 27 | size_t message_size; 28 | }; 29 | 30 | void TEST_check_digest(); 31 | void TEST_entropy(); 32 | void TEST_generate_random_pass(); 33 | struct test_encryption_aes_256_t *TEST_encryption_aes_256(); 34 | void TEST_decryption_aes_256(struct test_encryption_aes_256_t *ctx); 35 | void TEST_password_strength(); 36 | void TEST_pbkdf2(); 37 | void TEST_argon2id(); 38 | 39 | #endif 40 | 41 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | _nakamoto_ changelogs 3 | 4 | ## [0.1.5] - 2025-11-23 5 | - Update OpenSSL version 3.4.1 => [3.6.0](https://github.com/openssl/openssl/releases/tag/openssl-3.6.0) ✔️ 6 | - Add reference of system entropy function detector _(verify_system_entropy)_ implementation related to [MIT - 7.3 A Statistical Definition of Entropy](https://web.mit.edu/16.unified/www/FALL/thermodynamics/notes/node56.html) ✔️ 7 | 8 | ## [0.1.4] - 2025-03-27 9 | - Update OpenSSL version 3.4.0 => 3.4.1 ✔️ 10 | 11 | **NOTES** 12 | 13 | OpenSSL 3.4.1 is a security patch release. The most severe CVE fixed in this release is High. This fixed vulnerabilities below does not affect _Nakamoto_ 14 | 15 | [CVE-2024-12797](https://openssl-library.org/news/vulnerabilities/index.html#CVE-2024-12797) 16 | 17 | [CVE-2024-13176](https://openssl-library.org/news/vulnerabilities/index.html#CVE-2024-13176) 18 | 19 | ## [0.1.3] - 2024-11-20 20 | - Update OpenSSL version 3.3.1 => 3.4.0 ✔️ 21 | 22 | ## [0.1.2] - 2024-09-24 23 | - Release 0.1.2 ✔️ 24 | - Add tests to nakamoto ✔️ 25 | 26 | ## [0.1.1] - 2024-08-25 27 | - Release 0.1.1 ✔️ 28 | - Update OpenSSL version to 3.3.1 29 | 30 | ## [0.1.0] - 2024-05-25 31 | - Initial release 0.1.0 ✔️ 32 | 33 | ## [master] - current 34 | 35 | [0.1.0]: https://github.com/devfabiosilva/nakamoto/tree/v0.1.0 36 | [0.1.1]: https://github.com/devfabiosilva/nakamoto/tree/v0.1.1 37 | [0.1.2]: https://github.com/devfabiosilva/nakamoto/tree/v0.1.2 38 | [0.1.3]: https://github.com/devfabiosilva/nakamoto/tree/v0.1.3 39 | [0.1.4]: https://github.com/devfabiosilva/nakamoto/tree/v0.1.4 40 | [0.1.5]: https://github.com/devfabiosilva/nakamoto/tree/v0.1.5 41 | [master]: https://github.com/devfabiosilva/nakamoto/tree/master 42 | 43 | -------------------------------------------------------------------------------- /src/logger.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define PG_ALIGN 16 6 | 7 | void debug_dump(uint8_t *data, size_t data_sz) 8 | { 9 | size_t i; 10 | int page; 11 | 12 | if (!data) { 13 | fprintf(stderr, "debug_dump: NULL data"); 14 | return; 15 | } 16 | 17 | if (!data_sz) { 18 | fprintf(stderr, "debug_dump: Empty data"); 19 | return; 20 | } 21 | 22 | page=0; 23 | 24 | for (i=0;i>1)-1))==0) 29 | fprintf(stdout, " "); 30 | 31 | fprintf(stdout, " %02x", data[i]); 32 | } 33 | 34 | fprintf(stdout, "\n"); 35 | 36 | } 37 | 38 | void debug_dump_ascii(uint8_t *data, size_t data_sz) 39 | { 40 | size_t i; 41 | int page; 42 | uint8_t c; 43 | 44 | if (!data) { 45 | fprintf(stderr, "debug_dump_ascii: NULL data"); 46 | return; 47 | } 48 | 49 | if (!data_sz) { 50 | fprintf(stderr, "debug_dump_ascii: Empty data"); 51 | return; 52 | } 53 | 54 | page=0; 55 | 56 | for (i=0;i>1)-1))==0) 61 | fprintf(stdout, " "); 62 | 63 | if (((c=data[i])<0x21) || (c>0x7E)) 64 | fprintf(stdout, " %02X ", c); 65 | else 66 | fprintf(stdout, " '%c' ", (char)c); 67 | } 68 | 69 | fprintf(stdout, "\n"); 70 | 71 | } 72 | 73 | int is_vec_content_eq( 74 | uint8_t *a, size_t a_sz, 75 | uint8_t *b, size_t b_sz 76 | ) 77 | { 78 | 79 | if (a==b) 80 | return (a_sz==b_sz); 81 | 82 | if (!a) 83 | return 0; 84 | 85 | if (!b) 86 | return 0; 87 | 88 | if (a_sz!=b_sz) 89 | return 0; 90 | 91 | if (!a_sz) 92 | return 1; 93 | 94 | return (memcmp(a, b, a_sz)==0); 95 | } 96 | 97 | #undef PG_ALIGN 98 | 99 | -------------------------------------------------------------------------------- /include/utility.h: -------------------------------------------------------------------------------- 1 | #ifndef UTILITY_H 2 | #define UTILITY_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #define CONST_STR_LEN(s) sizeof(s)-1 9 | #define FILE_BUFFER_SIZE (size_t)1*1024*1024 // 1 MB max 10 | _Static_assert((FILE_BUFFER_SIZE&0x0F)==0, "FILE_BUFFER_SIZE must be 16 byte alingned"); 11 | 12 | uint32_t setU32_LE(uint32_t); 13 | uint64_t setU64_LE(uint64_t); 14 | int writeToFile(const char *, uint8_t *, size_t); 15 | int ssl_hash512(uint8_t *, uint8_t *, size_t, char **); 16 | int pbkdf2( 17 | uint8_t *, size_t, 18 | const char *, int, 19 | uint8_t *, uint32_t, 20 | uint32_t, 21 | char ** 22 | ); 23 | int argon2id( 24 | uint8_t *, size_t, 25 | const char *, int, 26 | uint8_t *, uint32_t, 27 | uint8_t *, uint32_t, //Optional. can be null 28 | uint8_t *, uint32_t, //Optional. can be null 29 | uint32_t, 30 | uint32_t, 31 | uint32_t, 32 | char ** 33 | ); 34 | 35 | int aes_256_cbc_encrypt( 36 | uint8_t *, size_t *, 37 | uint8_t *, size_t, 38 | uint8_t *, uint8_t *, 39 | char ** 40 | ); 41 | 42 | int aes_256_cbc_decrypt( 43 | uint8_t *, size_t *, 44 | uint8_t *, size_t, 45 | uint8_t *, uint8_t *, 46 | char ** 47 | ); 48 | int check_hash512(uint8_t *, uint8_t *, size_t, char **); 49 | 50 | int readFileAlign(uint8_t **, size_t *, size_t *, const char *); 51 | int readFileAlignDecrypt(uint8_t **, size_t *, size_t *, const char *); 52 | void readFileAlignFree(uint8_t **, size_t); 53 | 54 | int writeFileUtil(const char *, uint8_t *, size_t); 55 | 56 | uint8_t *xor_n(uint8_t *, uint8_t *, size_t); 57 | 58 | struct pass_t { 59 | // size_t len; 60 | char passwd[MAX_PASSWD+1]; 61 | char retype_passwd[MAX_PASSWD+1]; 62 | }; 63 | 64 | int set_password(struct pass_t **); 65 | int get_password(struct pass_t **); 66 | void pass_free(struct pass_t **); 67 | 68 | char *get_version_str(); 69 | 70 | #ifdef VISIBLE_FOR_TEST 71 | void *n_malloc(size_t *, size_t); 72 | #endif 73 | 74 | #endif 75 | 76 | -------------------------------------------------------------------------------- /include/rnd.h: -------------------------------------------------------------------------------- 1 | #ifndef RND_H 2 | #define RND_H 3 | 4 | #include 5 | #include 6 | 7 | int gen_rand_no_entropy_util(uint8_t *, size_t); 8 | 9 | int verify_system_entropy( 10 | uint32_t, 11 | uint8_t *, 12 | size_t, 13 | uint64_t 14 | ); 15 | 16 | void open_random(char *); 17 | 18 | void close_random(); 19 | 20 | #define PASS_SZ 38 21 | struct pass_list_vec_t { 22 | char password[PASS_SZ]; 23 | }; 24 | #undef PASS_SZ 25 | struct pass_list_t { 26 | uint16_t n; // Number of vectors; 27 | size_t vec_size; // In bytes 28 | struct pass_list_vec_t *vec; // Vector. MUST be free 29 | }; 30 | 31 | /** 32 | * @def F_ENTROPY_TYPE_PARANOIC 33 | * @brief Type of the very excelent entropy used for verifier. Very slow 34 | */ 35 | #define F_ENTROPY_TYPE_PARANOIC (uint32_t)1477682819 36 | 37 | //#define F_ENTROPY_TYPE_EXCELENT (uint32_t)1475885281 38 | /** 39 | * @def F_ENTROPY_TYPE_EXCELENT 40 | * @brief Type of the excelent entropy used for verifier. Slow 41 | */ 42 | #define F_ENTROPY_TYPE_EXCELENT (uint32_t)1476885281 43 | 44 | //#define F_ENTROPY_TYPE_GOOD (uint32_t)1471531015 45 | /** 46 | * @def F_ENTROPY_TYPE_GOOD 47 | * @brief Type of the good entropy used for verifier. Not so slow 48 | */ 49 | #define F_ENTROPY_TYPE_GOOD (uint32_t)1472531015 50 | 51 | //#define F_ENTROPY_TYPE_NOT_ENOUGH (uint32_t)1470001808 52 | /** 53 | * @def F_ENTROPY_TYPE_NOT_ENOUGH 54 | * @brief Type of the moderate entropy used for verifier. Fast 55 | */ 56 | #define F_ENTROPY_TYPE_NOT_ENOUGH (uint32_t)1471001808 57 | 58 | //#define F_ENTROPY_TYPE_NOT_RECOMENDED (uint32_t)1469703345 59 | /** 60 | * @def F_ENTROPY_TYPE_NOT_RECOMENDED 61 | * @brief Type of the not recommended entropy used for verifier. Very fast 62 | */ 63 | #define F_ENTROPY_TYPE_NOT_RECOMENDED (uint32_t)1470003345 64 | 65 | struct pass_list_t *pass_list_new(uint16_t); 66 | int randomize_and_print_pass_list(struct pass_list_t *, uint32_t, uint64_t); 67 | void pass_list_free(struct pass_list_t **); 68 | 69 | #endif 70 | -------------------------------------------------------------------------------- /include/logger.h: -------------------------------------------------------------------------------- 1 | #ifndef LOGGER_H 2 | #define LOGGER_H 3 | 4 | #include 5 | 6 | #define END_TITLE "\e[0m" 7 | #define ERROR_CODE "\e[31;1m" 8 | #define DEBUG_CODE "\e[1;3m" 9 | #define WARNING_CODE "\e[33;1m" 10 | #define INFO_CODE "\e[34;1m" 11 | 12 | #define N_INFO_TXT "\n"INFO_CODE"INFO:"END_TITLE" " 13 | #define N_WARN_TXT "\n"WARNING_CODE"WARN:"END_TITLE" " 14 | #define N_ERROR_TXT "\n"ERROR_CODE"ERROR:"END_TITLE" " 15 | #define N_DEBUG_TXT "\n"DEBUG_CODE"DEBUG:"END_TITLE" " 16 | 17 | #define N_INFO(msg) \ 18 | fprintf(stdout, N_INFO_TXT msg); 19 | 20 | #define N_INFOF(msg, ...) \ 21 | fprintf(stdout, N_INFO_TXT msg, __VA_ARGS__); 22 | 23 | #define N_WARN(msg) \ 24 | fprintf(stdout, N_WARN_TXT msg); 25 | 26 | #define N_WARNF(msg, ...) \ 27 | fprintf(stdout, N_WARN_TXT msg, __VA_ARGS__); 28 | 29 | #define N_ERROR(msg) \ 30 | fprintf(stderr, N_ERROR_TXT msg); 31 | 32 | #define N_ERRORF(msg, ...) \ 33 | fprintf(stderr, N_ERROR_TXT msg, __VA_ARGS__); 34 | 35 | #ifdef DEBUG 36 | 37 | void debug_dump(uint8_t *, size_t); 38 | void debug_dump_ascii(uint8_t *, size_t); 39 | int is_vec_content_eq( 40 | uint8_t *, size_t, 41 | uint8_t *, size_t 42 | ); 43 | 44 | #define N_DEBUG(msg) \ 45 | fprintf(stdout, N_DEBUG_TXT msg); 46 | 47 | #define N_DEBUGF(msg, ...) \ 48 | fprintf(stdout, N_DEBUG_TXT msg, __VA_ARGS__); 49 | 50 | #define N_DEBUG_DUMP(data, data_sz) \ 51 | debug_dump((uint8_t *)data, (size_t)data_sz); 52 | 53 | #define N_DEBUG_DUMP_A(data, data_sz) \ 54 | debug_dump_ascii((uint8_t *)data, (size_t)data_sz); 55 | 56 | #define N_DEBUG_COMP_VEC(a, a_sz, b, b_sz) \ 57 | N_DEBUGF("Comparing vector \"" #a "\" at (%p) with size %lu with vector \"" #b "\" at (%p) with size %lu", a, a_sz, b, b_sz) \ 58 | if (is_vec_content_eq((uint8_t *)a, (size_t)a_sz, (uint8_t *)b, (size_t)b_sz)) { \ 59 | N_DEBUG(#a " and " #b " are EQUALS") \ 60 | N_DEBUG_DUMP(a, a_sz) \ 61 | } else { \ 62 | N_DEBUG("\n" #a " and " #b " are NOT EQUALS\n\t" #a " value:") \ 63 | N_DEBUG_DUMP(a, a_sz) \ 64 | N_DEBUG("\n\t" #b " value:") \ 65 | N_DEBUG_DUMP(b, b_sz) \ 66 | } 67 | 68 | #else 69 | #define N_DEBUG(msg, ...) 70 | #define N_DEBUGF(msg, ...) 71 | #define N_DEBUG_DUMP(data, data_sz) 72 | #define N_DEBUG_DUMP_A(data, data_sz) 73 | #define N_DEBUG_COMP_VEC(a, a_sz, b, b_sz) 74 | #endif 75 | 76 | #endif 77 | 78 | -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #define GEN_RAND_PWD 1<<0 9 | #define INVALID_ENC 1<<1 10 | #define INVALID_DEC 1<<2 11 | #define ENC 1<<3 12 | #define DEC 1<<4 13 | #define NUM_OF_PASSWORD 50 14 | #define ENTROPY_TIMEOUT 60 15 | 16 | int main(int argc, char **argv) 17 | { 18 | int err, option; 19 | char *v; 20 | struct pass_list_t *pass_list; 21 | struct file_util_t *file_util; 22 | struct pass_t *pass; 23 | 24 | if (argc==1) { 25 | ret_1: 26 | N_INFOF( 27 | "\nVersion: %s" 28 | "\nUsage:\n\thelp - Show this help\n\tg - Generate random passwords\n\tenc - Encrypt file\n\tdec - Decrypt file\n", 29 | get_version_str() 30 | ); 31 | return 0; 32 | } 33 | 34 | if (argc==2) { 35 | if (!strcmp(v=argv[1], "help")) 36 | goto ret_1; 37 | 38 | if (!strcmp(v, "g")) 39 | option=GEN_RAND_PWD; 40 | else if (!strcmp(v, "enc")) 41 | option=INVALID_ENC; 42 | else if (!strcmp(v, "dec")) 43 | option=INVALID_DEC; 44 | else { 45 | ret_2: 46 | N_INFOF("\nInvalid parameter %s\n", v); 47 | return -1; 48 | } 49 | } else if (argc==3) { 50 | if (!strcmp(v=argv[1], "enc")) 51 | option=ENC; 52 | else if (!strcmp(v, "dec")) 53 | option=DEC; 54 | else 55 | goto ret_2; 56 | 57 | v=argv[2]; 58 | 59 | } else { 60 | N_INFO("\n\tToo many arguments\n"); 61 | return -2; 62 | } 63 | 64 | if (option&INVALID_ENC) { 65 | N_INFO("Missing plain text filename\n") 66 | return -3; 67 | } 68 | 69 | if (option&INVALID_DEC) { 70 | N_INFO("Missing encrypted filename\n") 71 | return -4; 72 | } 73 | 74 | open_random(NULL); 75 | 76 | if (option&GEN_RAND_PWD) { 77 | N_INFOF( 78 | "Generating %d random passwords with high entropy. Move your mouse, play a music in your computer, open a browser to increase entropy" 79 | "\n... Wait generating random passwords ...\n", 80 | NUM_OF_PASSWORD 81 | ) 82 | N_INFO("") 83 | if (!(pass_list=pass_list_new(NUM_OF_PASSWORD))) { 84 | err=-5; 85 | N_ERROR("\nError pass_list_new. Try again\n") 86 | goto exit_1; 87 | } 88 | 89 | if ((err=randomize_and_print_pass_list(pass_list, F_ENTROPY_TYPE_PARANOIC, ENTROPY_TIMEOUT))) 90 | N_ERRORF("\nError randomize_and_print_pass_list %d. Try again", err); 91 | 92 | pass_list_free(&pass_list); 93 | 94 | } else if (option&ENC) { 95 | N_INFO( 96 | "Generating random number with high entropy. Move your mouse, play a music in your computer, open a browser to increase entropy" 97 | "\n... Wait generating random numbers ...\n" 98 | ) 99 | N_INFO("") 100 | if (!(file_util=WRITE_begin_header(ENTROPY_TIMEOUT))) { 101 | err=-6; 102 | N_ERROR("\nError begin encrypt write. Try again\n") 103 | goto exit_1; 104 | } 105 | 106 | N_INFOF("Begin encryption ...\n... Wait a little longer to encrypt file %s ...\n", v) 107 | N_INFO("") 108 | 109 | if (!(err=set_password(&pass))) { 110 | if ((err=WRITE_derive_keys(file_util, pass->passwd, v))) 111 | N_ERRORF("WRITE_derive_keys %d\n", err) 112 | 113 | pass_free(&pass); 114 | } 115 | 116 | WRITE_end_header(&file_util); 117 | 118 | } else { 119 | 120 | N_INFOF("Begin decryption \n... Wait a little long to decrypt file %s ...\n", v) 121 | N_INFO("") 122 | 123 | if (!(file_util=READ_begin_header())) { 124 | err=-2; 125 | N_ERROR("\nError begin decrypt read. Try again\n") 126 | goto exit_1; 127 | } 128 | 129 | if (!(err=get_password(&pass))) { 130 | if ((err=READ_extract(file_util, pass->passwd, v))) 131 | N_ERRORF("READ_extract file %s. Try again. Maybe wrong password or corrupted file - %d", v, err) 132 | 133 | pass_free(&pass); 134 | } 135 | 136 | READ_end_header(&file_util); 137 | 138 | } 139 | 140 | exit_1: 141 | 142 | close_random(); 143 | 144 | if (err) 145 | N_ERRORF("Fail with error %d\n", err) 146 | else 147 | N_INFO("SUCCESS\n") 148 | 149 | return err; 150 | } 151 | 152 | -------------------------------------------------------------------------------- /include/nakamoto.h: -------------------------------------------------------------------------------- 1 | #ifndef NAKAMOTO_H 2 | #define NAKAMOTO_H 3 | 4 | #define N_MAX 65 5 | #define MIN_PASSWD 16 6 | #define MAX_PASSWD (N_MAX-1) 7 | 8 | #include 9 | //dom 23 abr 2023 14:50:07 10 | 11 | #define VERSION_MAJOR 0 12 | #define VERSION_MINOR 1 13 | #define VERSION_REVISION 5 14 | #define GET_VER_MAJ(val) (val>>20) 15 | #define GET_VER_MIN(val) ((val>>10)&0x03FF) 16 | #define GET_VER_REV(val) (val&0x03FF) 17 | #define NAKAMOTO_VERSION(major, minor, revision) (uint32_t)((major<<20)|(minor<<10)|(revision)) 18 | #define NAKAMOTO_VERSION_SET NAKAMOTO_VERSION(VERSION_MAJOR, VERSION_MINOR, VERSION_REVISION) 19 | #define INTERACTION 8*1024*1024 20 | #define FILE_BUFFER_SIZE (size_t)1*1024*1024 // 1 MB max 21 | #define FILE_STRUCT_SIZE (sizeof(struct file_struct_t) + sizeof(struct cipher_block_t) + sizeof(struct cipher_block_data_t)) 22 | 23 | #define NAKAMOTO "nakamoto" 24 | 25 | //decrypted by key 1 and key 2 26 | struct cipher_block_data_t { 27 | uint8_t hash_xored_restore[64]; // Salt used to invert logic of hash_xored. if hash_xored_restore^hash_xored = hash512(unencrypted(data)) 28 | uint64_t file_size; // Original file size in little endian 29 | uint8_t pad[8]; //Padding 30 | //Below is data of ALIGN(file_size) encrypted by key1 and key2 31 | //uint8_t *data 32 | } __attribute__ ((packed)); 33 | 34 | _Static_assert((sizeof(struct cipher_block_data_t)&0x0F)==0, "struct cipher_block_data_t MUST have size multiple of 16"); 35 | 36 | //decrypted by key 1 37 | struct cipher_block_t { 38 | // uint32_t memory_cost; 39 | // uint32_t interaction_cost; 40 | // uint32_t parallel_cost; 41 | // uint8_t pad[4+4+4+4]; 42 | uint8_t additional[32]; 43 | uint8_t secret[32]; 44 | uint8_t iv2[16]; // Initial vector from private key 2 45 | uint8_t hash_xored[64]; //Xor'ed or with hash512 of the file. It will be the salt2 of the pbkdf2 step 2 to derive second private key. 46 | } __attribute__ ((packed)); 47 | 48 | _Static_assert((sizeof(struct cipher_block_t)&0x0F)==0, "struct cipher_block_t MUST have size multiple of 16"); 49 | 50 | struct file_struct_t { 51 | char magic[CONST_STR_LEN(NAKAMOTO)]; // Magic 52 | uint32_t version; // Version in Little endian 53 | // uint32_t iteraction; // Interaction for pbkdf2 in little endian 54 | uint8_t pad[4]; 55 | uint8_t iv1[16]; //Initial vector of the private key 1 derived from password 56 | uint8_t salt1[64]; //Initial salt from private key 1 57 | char description[48]; // Brief description in header 58 | uint8_t sha512[64]; //Hash 512 of this header file_struct_t 59 | } __attribute__ ((packed)); 60 | 61 | struct file_util_t { 62 | /* 63 | uint8_t iv1_tmp[16]; //IV1 temp for OpenSSL 64 | uint8_t iv2_tmp[16]; // IV2 temp for OpenSSL 65 | */ 66 | uint8_t priv_key1[32]; // Private key1 for encrypt/decrypt data 67 | uint8_t priv_key2[32]; // Private key2 for encrypt/decrypt data 68 | struct file_struct_t *file_struct; // Structure of the file. Must be free 69 | uint8_t *_file_buffer; //Buffer of file. It must be free 70 | }; 71 | 72 | _Static_assert((sizeof(struct file_struct_t)&0x0F)==0, "struct file_struct_t MUST have size multiple of 16"); 73 | 74 | struct file_util_t *WRITE_begin_header(uint64_t); 75 | void WRITE_end_header(struct file_util_t **); 76 | 77 | int WRITE_derive_keys(struct file_util_t *, const char *, const char *); 78 | 79 | struct file_util_t *READ_begin_header(); 80 | int READ_extract(struct file_util_t *, const char *, const char *); 81 | void READ_end_header(struct file_util_t **); 82 | 83 | #define CIPHER_INFO_SZ sizeof(struct cipher_block_t)+sizeof(struct cipher_block_data_t) 84 | _Static_assert(FILE_BUFFER_SIZE>CIPHER_INFO_SZ, "CIPHER_INFO_SZ is less than FILE_BUFFER_SIZE"); 85 | 86 | #define FILE_STRUCT_DATA_HASH_SZ sizeof(struct file_struct_t)-sizeof(((struct file_struct_t *)0)->sha512) 87 | 88 | /** 89 | * @def PASS_MUST_HAVE_AT_LEAST_NONE 90 | * @brief Password does not need any criteria to pass 91 | */ 92 | #define PASS_MUST_HAVE_AT_LEAST_NONE (int)0 93 | 94 | /** 95 | * @def PASS_MUST_HAVE_AT_LEAST_ONE_NUMBER 96 | * @brief Password must have at least one number 97 | */ 98 | #define PASS_MUST_HAVE_AT_LEAST_ONE_NUMBER (int)1 99 | 100 | /** 101 | * @def PASS_MUST_HAVE_AT_LEAST_ONE_SYMBOL 102 | * @brief Password must have at least one symbol 103 | */ 104 | #define PASS_MUST_HAVE_AT_LEAST_ONE_SYMBOL (int)2 105 | 106 | /** 107 | * @def PASS_MUST_HAVE_AT_LEAST_ONE_UPPER_CASE 108 | * @brief Password must have at least one upper case 109 | */ 110 | #define PASS_MUST_HAVE_AT_LEAST_ONE_UPPER_CASE (int)4 111 | 112 | /** 113 | * @def PASS_MUST_HAVE_AT_LEAST_ONE_LOWER_CASE 114 | * @brief Password must have at least one lower case 115 | */ 116 | #define PASS_MUST_HAVE_AT_LEAST_ONE_LOWER_CASE (int)8 117 | 118 | /** 119 | * @def PASS_IS_TOO_LONG 120 | * @brief Password is too long 121 | */ 122 | #define PASS_IS_TOO_LONG (int)256 123 | 124 | /** 125 | * @def PASS_IS_TOO_SHORT 126 | * @brief Password is too short 127 | */ 128 | #define PASS_IS_TOO_SHORT (int)512 129 | 130 | /** 131 | * @def PASS_IS_OUT_OVF 132 | * @brief Password is overflow and cannot be stored 133 | */ 134 | #define PASS_IS_OUT_OVF (int)1024//768 135 | 136 | /** 137 | * @def PASS_IS_NULL 138 | * @brief Password is NULL 139 | */ 140 | #define PASS_IS_NULL (int)2048//768 141 | 142 | #define MUST_HAVE (PASS_MUST_HAVE_AT_LEAST_ONE_NUMBER|PASS_MUST_HAVE_AT_LEAST_ONE_SYMBOL|PASS_MUST_HAVE_AT_LEAST_ONE_UPPER_CASE| \ 143 | PASS_MUST_HAVE_AT_LEAST_ONE_LOWER_CASE) 144 | 145 | #ifdef VISIBLE_FOR_TEST 146 | uint32_t getArgon2idMemCost(); 147 | uint32_t getArgon2idInteractionCost(); 148 | uint32_t getArgon2idParallelCost(); 149 | int pass_must_have_at_least(size_t *, char *, size_t, size_t, size_t, int); 150 | #endif 151 | 152 | #endif 153 | 154 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | STRIP=strip 3 | CURDIR=$(PWD) 4 | INCLUDEDIR=$(CURDIR)/include 5 | INCLUDEDIR_TEST=$(INCLUDEDIR)/test 6 | SOURCE_DIR=$(CURDIR)/src 7 | TEST_DIR=$(CURDIR)/test 8 | 9 | THIRD_PARTY_DIR=$(CURDIR)/third-party 10 | OPENSSL_GIT=https://github.com/openssl/openssl.git 11 | OPENSSL_BRANCH=openssl-3.6.0 12 | OPENSSL_DIR=$(THIRD_PARTY_DIR)/openssl 13 | OPENSSL_COMPILED_DIR=$(OPENSSL_DIR)/compiled 14 | OPENSSL_COMPILED_DIR_LIB=$(OPENSSL_COMPILED_DIR)/lib64 15 | OPENSSL_COMPILED_DIR_INCLUDE=$(OPENSSL_COMPILED_DIR)/include 16 | 17 | TEST_FILE_NAME=test.t 18 | 19 | FLAG=-Wall -Wno-stringop-truncation -I$(OPENSSL_COMPILED_DIR_INCLUDE) -L$(OPENSSL_COMPILED_DIR_LIB) -lcrypto -lpthread -ldl -DLE 20 | DEBUG_FLAG=-g -fsanitize=address,leak -DDEBUG $(FLAG) 21 | TARG=nakamoto 22 | TARG_DBG=$(TARG)_debug 23 | 24 | NAKAMOTO_INSTALL_MSG="Nakamoto needs Openssl $(OPENSSL_BRANCH) static library installed locally. Please type \"make install_ssl\" before build \"nakamoto\"" 25 | 26 | all: main 27 | 28 | rnd.o: 29 | ifneq ("$(wildcard $(OPENSSL_DIR)/*)","") 30 | @echo "Build random object" 31 | @$(CC) -O2 -c $(SOURCE_DIR)/rnd.c -I$(INCLUDEDIR) -o $(SOURCE_DIR)/rnd.o $(FLAG) 32 | else 33 | @echo $(NAKAMOTO_INSTALL_MSG) 34 | endif 35 | 36 | utility.o: 37 | ifneq ("$(wildcard $(OPENSSL_DIR)/*)","") 38 | @echo "Build utility object" 39 | @$(CC) -O2 -c $(SOURCE_DIR)/utility.c -I$(INCLUDEDIR) -o $(SOURCE_DIR)/utility.o $(FLAG) 40 | else 41 | @echo $(NAKAMOTO_INSTALL_MSG) 42 | endif 43 | 44 | nakamoto.o: 45 | ifneq ("$(wildcard $(OPENSSL_DIR)/*)","") 46 | @echo "Build nakamoto object" 47 | @$(CC) -O2 -c $(SOURCE_DIR)/nakamoto.c -I$(INCLUDEDIR) -o $(SOURCE_DIR)/nakamoto.o $(FLAG) 48 | else 49 | @echo $(NAKAMOTO_INSTALL_MSG) 50 | endif 51 | 52 | rnd_debug.o: 53 | ifneq ("$(wildcard $(OPENSSL_DIR)/*)","") 54 | @echo "Build random (DEBUG) object" 55 | @$(CC) -O2 -c $(SOURCE_DIR)/rnd.c -I$(INCLUDEDIR) -o $(SOURCE_DIR)/rnd_debug.o $(DEBUG_FLAG) 56 | else 57 | @echo $(NAKAMOTO_INSTALL_MSG) 58 | endif 59 | 60 | utility_debug.o: 61 | ifneq ("$(wildcard $(OPENSSL_DIR)/*)","") 62 | @echo "Build utility (DEBUG) object" 63 | @$(CC) -O2 -c $(SOURCE_DIR)/utility.c -I$(INCLUDEDIR) -o $(SOURCE_DIR)/utility_debug.o $(DEBUG_FLAG) 64 | else 65 | @echo $(NAKAMOTO_INSTALL_MSG) 66 | endif 67 | 68 | nakamoto_debug.o: 69 | ifneq ("$(wildcard $(OPENSSL_DIR)/*)","") 70 | @echo "Build nakamoto object" 71 | @$(CC) -O2 -c $(SOURCE_DIR)/nakamoto.c -I$(INCLUDEDIR) -o $(SOURCE_DIR)/nakamoto_debug.o $(DEBUG_FLAG) 72 | else 73 | @echo $(NAKAMOTO_INSTALL_MSG) 74 | endif 75 | 76 | logger_debug.o: 77 | ifneq ("$(wildcard $(OPENSSL_DIR)/*)","") 78 | @echo "Build logger object" 79 | @$(CC) -O2 -c $(SOURCE_DIR)/logger.c -I$(INCLUDEDIR) -o $(SOURCE_DIR)/logger_debug.o $(DEBUG_FLAG) 80 | else 81 | @echo $(NAKAMOTO_INSTALL_MSG) 82 | endif 83 | 84 | rnd_debug_test.o: 85 | ifneq ("$(wildcard $(OPENSSL_DIR)/*)","") 86 | @echo "Build random (DEBUG) object" 87 | @$(CC) -O2 -c $(SOURCE_DIR)/rnd.c -I$(INCLUDEDIR) -o $(SOURCE_DIR)/rnd_debug.o $(DEBUG_FLAG) -DVISIBLE_FOR_TEST 88 | else 89 | @echo $(NAKAMOTO_INSTALL_MSG) 90 | endif 91 | 92 | utility_debug_test.o: 93 | ifneq ("$(wildcard $(OPENSSL_DIR)/*)","") 94 | @echo "Build utility (DEBUG) object" 95 | @$(CC) -O2 -c $(SOURCE_DIR)/utility.c -I$(INCLUDEDIR) -o $(SOURCE_DIR)/utility_debug.o $(DEBUG_FLAG) -DVISIBLE_FOR_TEST 96 | else 97 | @echo $(NAKAMOTO_INSTALL_MSG) 98 | endif 99 | 100 | nakamoto_debug_test.o: 101 | ifneq ("$(wildcard $(OPENSSL_DIR)/*)","") 102 | @echo "Build nakamoto object" 103 | @$(CC) -O2 -c $(SOURCE_DIR)/nakamoto.c -I$(INCLUDEDIR) -o $(SOURCE_DIR)/nakamoto_debug.o $(DEBUG_FLAG) -DVISIBLE_FOR_TEST 104 | else 105 | @echo $(NAKAMOTO_INSTALL_MSG) 106 | endif 107 | 108 | logger_debug_test.o: 109 | ifneq ("$(wildcard $(OPENSSL_DIR)/*)","") 110 | @echo "Build logger object" 111 | @$(CC) -O2 -c $(SOURCE_DIR)/logger.c -I$(INCLUDEDIR) -o $(SOURCE_DIR)/logger_debug.o $(DEBUG_FLAG) -DVISIBLE_FOR_TEST 112 | else 113 | @echo $(NAKAMOTO_INSTALL_MSG) 114 | endif 115 | 116 | main: rnd.o utility.o nakamoto.o 117 | ifneq ("$(wildcard $(OPENSSL_DIR)/*)","") 118 | @echo "Compiling $(TARG)..." 119 | @$(CC) -O2 -o $(TARG) main.c $(SOURCE_DIR)/utility.o $(SOURCE_DIR)/rnd.o $(SOURCE_DIR)/nakamoto.o -I$(INCLUDEDIR) $(FLAG) 120 | @echo "Stripping $(TARG)" 121 | @strip $(TARG) 122 | @echo "Finished" 123 | else 124 | @echo $(NAKAMOTO_INSTALL_MSG) 125 | endif 126 | 127 | .PHONY: 128 | clean: 129 | ifneq ("$(wildcard $(CURDIR)/src/*.o)","") 130 | @echo "Removing objects ..." 131 | rm -v $(CURDIR)/src/*.o 132 | else 133 | @echo "Nothing to do with objects" 134 | endif 135 | 136 | ifneq ("$(wildcard $(CURDIR)/$(TARG_DBG))","") 137 | @echo "Removing main $(TARG_DBG)..." 138 | rm -v $(CURDIR)/$(TARG_DBG) 139 | else 140 | @echo "Nothing to do $(TARG_DBG)" 141 | endif 142 | 143 | ifneq ("$(wildcard $(CURDIR)/$(TARG))","") 144 | @echo "Removing main $(TARG)..." 145 | rm -v $(CURDIR)/$(TARG) 146 | else 147 | @echo "Nothing to do $(TARG)" 148 | endif 149 | 150 | ifneq ("$(wildcard $(TEST_DIR)/$(TEST_FILE_NAME))","") 151 | @echo "Removing $(TEST_FILE_NAME) ..." 152 | rm -v $(TEST_DIR)/$(TEST_FILE_NAME) 153 | else 154 | @echo "Nothing to do $(TEST_FILE_NAME)" 155 | endif 156 | 157 | install_ssl: 158 | ifeq ("$(wildcard $(OPENSSL_DIR)/*)","") 159 | @echo "Cloning OpenSSL $(OPENSSL_BRANCH) at $(THIRD_PARTY_DIR) ..." 160 | pwd; cd $(THIRD_PARTY_DIR); pwd; git clone -b $(OPENSSL_BRANCH) $(OPENSSL_GIT);cd openssl;./Configure --prefix=$(OPENSSL_COMPILED_DIR) --openssldir=$(OPENSSL_COMPILED_DIR)/ssldir;make -j12 && make -j12 test && make -j12 install;rm -rfv $(OPENSSL_COMPILED_DIR_LIB)/*.so* 161 | @echo "OpenSSL $(OPENSSL_BRANCH) installed successfully" 162 | else 163 | @echo "OpenSSL already installed. Skipping ..." 164 | endif 165 | 166 | remove_ssl: 167 | ifneq ("$(wildcard $(OPENSSL_DIR))","") 168 | @echo "Removing $(OPENSSL_DIR)..." 169 | rm -rfv $(OPENSSL_DIR) 170 | @echo "Done" 171 | else 172 | @echo "Nothing to do: Removing SSL" 173 | endif 174 | 175 | 176 | debug: rnd_debug.o utility_debug.o nakamoto_debug.o logger_debug.o 177 | ifneq ("$(wildcard $(OPENSSL_DIR)/*)","") 178 | @echo "Compiling $(TARG_DBG)..." 179 | @$(CC) -O2 -o $(TARG_DBG) main.c $(SOURCE_DIR)/utility_debug.o $(SOURCE_DIR)/rnd_debug.o $(SOURCE_DIR)/nakamoto_debug.o $(SOURCE_DIR)/logger_debug.o -I$(INCLUDEDIR) $(DEBUG_FLAG) 180 | @echo "Finished" 181 | else 182 | @echo $(NAKAMOTO_INSTALL_MSG) 183 | endif 184 | 185 | test: rnd_debug_test.o utility_debug_test.o nakamoto_debug_test.o logger_debug_test.o 186 | ifneq ("$(wildcard $(OPENSSL_DIR)/*)","") 187 | @echo "Build test (TEST) object" 188 | @$(CC) -O2 $(TEST_DIR)/main.c $(TEST_DIR)/testutil.c $(SOURCE_DIR)/utility_debug.o $(SOURCE_DIR)/rnd_debug.o $(SOURCE_DIR)/nakamoto_debug.o $(SOURCE_DIR)/logger_debug.o $(SOURCE_DIR)/test/asserts.c -I$(INCLUDEDIR) -I$(INCLUDEDIR_TEST) -o $(TEST_DIR)/$(TEST_FILE_NAME) $(DEBUG_FLAG) -DVISIBLE_FOR_TEST 189 | cd $(TEST_DIR);./$(TEST_FILE_NAME) 190 | else 191 | @echo $(NAKAMOTO_INSTALL_MSG) 192 | endif 193 | 194 | -------------------------------------------------------------------------------- /src/rnd.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #define _ENTROPY_NUMBER_OF_ELEMENTS 256 11 | #define DISCRETE_LOG_MAX (8*_ENTROPY_NUMBER_OF_ELEMENTS) 12 | #define LOG_DISCRETE_ARRAY (DISCRETE_LOG_MAX+1) 13 | 14 | const uint32_t _log_discrete_array[]={ 15 | 2147483647, 999374, 908521, 855376, 817669, 788421, 764524, 744319, 726817, 711379, 697569, 685077, 673672, 663181, 653467, 644424, 635965, 628019, 620527, 613440, 606717, 600322, 594224, 588398, 582820, 577469, 572328, 567382, 562615, 558015, 553572, 549274, 16 | 545113, 541079, 537166, 533367, 529675, 526083, 522588, 519183, 515865, 512628, 509470, 506386, 503372, 500427, 497546, 494727, 491968, 489265, 486617, 484021, 481476, 478979, 476529, 474124, 471763, 469443, 467163, 464923, 462720, 460553, 458422, 456325, 17 | 454260, 452228, 450227, 448256, 446314, 444401, 442515, 440656, 438822, 437014, 435231, 433472, 431736, 430022, 428331, 426661, 425013, 423384, 421776, 420187, 418618, 417066, 415533, 414018, 412520, 411039, 409575, 408126, 406694, 405277, 403875, 402488, 18 | 401115, 399757, 398413, 397082, 395765, 394460, 393169, 391890, 390624, 389370, 388127, 386897, 385677, 384469, 383272, 382086, 380910, 379745, 378591, 377446, 376311, 375186, 374070, 372964, 371867, 370780, 369701, 368631, 367570, 366517, 365472, 364436, 19 | 363408, 362388, 361376, 360372, 359375, 358386, 357404, 356429, 355462, 354502, 353549, 352602, 351663, 350730, 349803, 348884, 347970, 347063, 346162, 345268, 344379, 343496, 342620, 341749, 340883, 340024, 339170, 338322, 337479, 336641, 335809, 334982, 20 | 334160, 333344, 332532, 331726, 330924, 330127, 329335, 328548, 327765, 326987, 326214, 325445, 324681, 323921, 323166, 322415, 321668, 320925, 320187, 319453, 318722, 317996, 317274, 316556, 315842, 315131, 314424, 313722, 313023, 312327, 311636, 310948, 21 | 310263, 309582, 308905, 308231, 307561, 306893, 306230, 305570, 304913, 304259, 303608, 302961, 302317, 301676, 301038, 300403, 299772, 299143, 298517, 297895, 297275, 296658, 296044, 295433, 294825, 294220, 293617, 293017, 292420, 291826, 291234, 290645, 22 | 290058, 289474, 288893, 288315, 287738, 287165, 286594, 286025, 285459, 284895, 284334, 283775, 283218, 282664, 282112, 281563, 281015, 280470, 279928, 279387, 278849, 278313, 277779, 277247, 276717, 276190, 275665, 275141, 274620, 274101, 273584, 273069, 23 | 272556, 272045, 271536, 271029, 270524, 270021, 269520, 269020, 268523, 268027, 267534, 267042, 266552, 266064, 265577, 265093, 264610, 264129, 263650, 263172, 262696, 262222, 261750, 261279, 260810, 260343, 259878, 259414, 258951, 258490, 258031, 257574, 24 | 257118, 256664, 256211, 255760, 255310, 254862, 254415, 253970, 253527, 253085, 252644, 252205, 251767, 251331, 250896, 250463, 250031, 249601, 249172, 248744, 248318, 247893, 247470, 247047, 246627, 246207, 245789, 245372, 244957, 244543, 244130, 243718, 25 | 243308, 242899, 242492, 242085, 241680, 241276, 240873, 240472, 240072, 239673, 239275, 238878, 238483, 238089, 237696, 237304, 236913, 236524, 236135, 235748, 235362, 234977, 234593, 234211, 233829, 233449, 233069, 232691, 232314, 231938, 231563, 231189, 26 | 230816, 230444, 230073, 229703, 229335, 228967, 228600, 228235, 227870, 227507, 227144, 226782, 226422, 226062, 225704, 225346, 224989, 224634, 224279, 223925, 223572, 223220, 222870, 222520, 222170, 221822, 221475, 221129, 220783, 220439, 220095, 219753, 27 | 219411, 219070, 218730, 218391, 218053, 217715, 217379, 217043, 216708, 216374, 216041, 215709, 215378, 215047, 214717, 214388, 214060, 213733, 213407, 213081, 212756, 212432, 212109, 211786, 211465, 211144, 210824, 210505, 210186, 209868, 209551, 209235, 28 | 208920, 208605, 208291, 207978, 207665, 207354, 207043, 206732, 206423, 206114, 205806, 205499, 205192, 204886, 204581, 204277, 203973, 203670, 203367, 203066, 202765, 202465, 202165, 201866, 201568, 201270, 200973, 200677, 200382, 200087, 199793, 199499, 29 | 199206, 198914, 198622, 198331, 198041, 197751, 197462, 197174, 196886, 196599, 196313, 196027, 195741, 195457, 195173, 194889, 194607, 194324, 194043, 193762, 193482, 193202, 192923, 192644, 192366, 192089, 191812, 191536, 191260, 190985, 190710, 190436, 30 | 190163, 189890, 189618, 189346, 189075, 188805, 188535, 188265, 187997, 187728, 187460, 187193, 186927, 186660, 186395, 186130, 185865, 185601, 185338, 185075, 184812, 184551, 184289, 184028, 183768, 183508, 183249, 182990, 182732, 182474, 182217, 181960, 31 | 181704, 181448, 181193, 180938, 180684, 180430, 180177, 179924, 179672, 179420, 179169, 178918, 178667, 178417, 178168, 177919, 177671, 177423, 177175, 176928, 176681, 176435, 176190, 175944, 175700, 175455, 175211, 174968, 174725, 174483, 174240, 173999, 32 | 173758, 173517, 173277, 173037, 172797, 172558, 172320, 172082, 171844, 171607, 171370, 171134, 170898, 170662, 170427, 170192, 169958, 169724, 169491, 169258, 169025, 168793, 168561, 168330, 168099, 167868, 167638, 167409, 167179, 166950, 166722, 166494, 33 | 166266, 166038, 165811, 165585, 165359, 165133, 164908, 164683, 164458, 164234, 164010, 163786, 163563, 163340, 163118, 162896, 162675, 162453, 162233, 162012, 161792, 161572, 161353, 161134, 160915, 160697, 160479, 160261, 160044, 159827, 159611, 159395, 34 | 159179, 158964, 158749, 158534, 158320, 158106, 157892, 157679, 157466, 157253, 157041, 156829, 156617, 156406, 156195, 155985, 155774, 155565, 155355, 155146, 154937, 154728, 154520, 154312, 154105, 153898, 153691, 153484, 153278, 153072, 152866, 152661, 35 | 152456, 152251, 152047, 151843, 151639, 151436, 151233, 151030, 150828, 150626, 150424, 150222, 150021, 149820, 149620, 149419, 149219, 149020, 148820, 148621, 148423, 148224, 148026, 147828, 147631, 147433, 147237, 147040, 146844, 146647, 146452, 146256, 36 | 146061, 145866, 145671, 145477, 145283, 145089, 144896, 144703, 144510, 144317, 144125, 143933, 143741, 143550, 143358, 143167, 142977, 142786, 142596, 142406, 142217, 142028, 141839, 141650, 141461, 141273, 141085, 140898, 140710, 140523, 140336, 140150, 37 | 139964, 139777, 139592, 139406, 139221, 139036, 138851, 138667, 138482, 138298, 138115, 137931, 137748, 137565, 137383, 137200, 137018, 136836, 136654, 136473, 136292, 136111, 135930, 135750, 135570, 135390, 135210, 135031, 134851, 134672, 134494, 134315, 38 | 134137, 133959, 133781, 133604, 133427, 133250, 133073, 132896, 132720, 132544, 132368, 132193, 132017, 131842, 131667, 131493, 131318, 131144, 130970, 130796, 130623, 130450, 130277, 130104, 129931, 129759, 129587, 129415, 129243, 129072, 128901, 128730, 39 | 128559, 128388, 128218, 128048, 127878, 127708, 127539, 127369, 127200, 127032, 126863, 126695, 126527, 126359, 126191, 126023, 125856, 125689, 125522, 125356, 125189, 125023, 124857, 124691, 124525, 124360, 124195, 124030, 123865, 123701, 123536, 123372, 40 | 123208, 123044, 122881, 122718, 122554, 122391, 122229, 122066, 121904, 121742, 121580, 121418, 121257, 121095, 120934, 120773, 120613, 120452, 120292, 120132, 119972, 119812, 119652, 119493, 119334, 119175, 119016, 118857, 118699, 118541, 118383, 118225, 41 | 118067, 117910, 117753, 117596, 117439, 117282, 117126, 116969, 116813, 116657, 116501, 116346, 116190, 116035, 115880, 115725, 115571, 115416, 115262, 115108, 114954, 114800, 114647, 114493, 114340, 114187, 114034, 113881, 113729, 113577, 113424, 113272, 42 | 113121, 112969, 112818, 112666, 112515, 112364, 112214, 112063, 111913, 111762, 111612, 111462, 111313, 111163, 111014, 110865, 110716, 110567, 110418, 110270, 110121, 109973, 109825, 109677, 109529, 109382, 109235, 109087, 108940, 108794, 108647, 108500, 43 | 108354, 108208, 108062, 107916, 107770, 107625, 107479, 107334, 107189, 107044, 106899, 106755, 106610, 106466, 106322, 106178, 106034, 105890, 105747, 105604, 105460, 105317, 105175, 105032, 104889, 104747, 104605, 104463, 104321, 104179, 104037, 103896, 44 | 103754, 103613, 103472, 103331, 103191, 103050, 102910, 102769, 102629, 102489, 102350, 102210, 102070, 101931, 101792, 101653, 101514, 101375, 101236, 101098, 100960, 100821, 100683, 100545, 100408, 100270, 100133, 99995, 99858, 99721, 99584, 99447, 45 | 99311, 99174, 99038, 98902, 98766, 98630, 98494, 98359, 98223, 98088, 97953, 97818, 97683, 97548, 97413, 97279, 97144, 97010, 96876, 96742, 96608, 96475, 96341, 96208, 96074, 95941, 95808, 95675, 95543, 95410, 95278, 95145, 46 | 95013, 94881, 94749, 94617, 94486, 94354, 94223, 94091, 93960, 93829, 93698, 93568, 93437, 93307, 93176, 93046, 92916, 92786, 92656, 92526, 92397, 92267, 92138, 92009, 91880, 91751, 91622, 91493, 91365, 91236, 91108, 90980, 47 | 90852, 90724, 90596, 90468, 90341, 90213, 90086, 89959, 89832, 89705, 89578, 89451, 89325, 89198, 89072, 88946, 88820, 88694, 88568, 88442, 88316, 88191, 88066, 87940, 87815, 87690, 87565, 87440, 87316, 87191, 87067, 86943, 48 | 86818, 86694, 86570, 86447, 86323, 86199, 86076, 85952, 85829, 85706, 85583, 85460, 85337, 85215, 85092, 84970, 84847, 84725, 84603, 84481, 84359, 84237, 84116, 83994, 83873, 83752, 83630, 83509, 83388, 83267, 83147, 83026, 49 | 82905, 82785, 82665, 82545, 82424, 82305, 82185, 82065, 81945, 81826, 81706, 81587, 81468, 81349, 81230, 81111, 80992, 80873, 80755, 80636, 80518, 80400, 80282, 80164, 80046, 79928, 79810, 79692, 79575, 79458, 79340, 79223, 50 | 79106, 78989, 78872, 78755, 78639, 78522, 78406, 78289, 78173, 78057, 77941, 77825, 77709, 77593, 77478, 77362, 77247, 77132, 77016, 76901, 76786, 76671, 76556, 76442, 76327, 76213, 76098, 75984, 75870, 75755, 75641, 75527, 51 | 75414, 75300, 75186, 75073, 74959, 74846, 74733, 74620, 74507, 74394, 74281, 74168, 74055, 73943, 73830, 73718, 73606, 73494, 73381, 73269, 73158, 73046, 72934, 72823, 72711, 72600, 72488, 72377, 72266, 72155, 72044, 71933, 52 | 71822, 71712, 71601, 71491, 71380, 71270, 71160, 71050, 70940, 70830, 70720, 70610, 70501, 70391, 70282, 70172, 70063, 69954, 69845, 69736, 69627, 69518, 69409, 69301, 69192, 69084, 68975, 68867, 68759, 68651, 68543, 68435, 53 | 68327, 68219, 68112, 68004, 67896, 67789, 67682, 67575, 67467, 67360, 67253, 67147, 67040, 66933, 66826, 66720, 66614, 66507, 66401, 66295, 66189, 66083, 65977, 65871, 65765, 65660, 65554, 65448, 65343, 65238, 65132, 65027, 54 | 64922, 64817, 64712, 64608, 64503, 64398, 64294, 64189, 64085, 63980, 63876, 63772, 63668, 63564, 63460, 63356, 63253, 63149, 63045, 62942, 62838, 62735, 62632, 62529, 62426, 62323, 62220, 62117, 62014, 61911, 61809, 61706, 55 | 61604, 61501, 61399, 61297, 61195, 61093, 60991, 60889, 60787, 60685, 60584, 60482, 60381, 60279, 60178, 60077, 59976, 59874, 59773, 59673, 59572, 59471, 59370, 59270, 59169, 59068, 58968, 58868, 58768, 58667, 58567, 58467, 56 | 58367, 58267, 58168, 58068, 57968, 57869, 57769, 57670, 57571, 57471, 57372, 57273, 57174, 57075, 56976, 56877, 56779, 56680, 56581, 56483, 56384, 56286, 56188, 56089, 55991, 55893, 55795, 55697, 55599, 55502, 55404, 55306, 57 | 55209, 55111, 55014, 54917, 54819, 54722, 54625, 54528, 54431, 54334, 54237, 54140, 54044, 53947, 53851, 53754, 53658, 53561, 53465, 53369, 53273, 53177, 53081, 52985, 52889, 52793, 52697, 52602, 52506, 52411, 52315, 52220, 58 | 52125, 52029, 51934, 51839, 51744, 51649, 51554, 51460, 51365, 51270, 51175, 51081, 50986, 50892, 50798, 50703, 50609, 50515, 50421, 50327, 50233, 50139, 50046, 49952, 49858, 49765, 49671, 49578, 49484, 49391, 49298, 49204, 59 | 49111, 49018, 48925, 48832, 48739, 48647, 48554, 48461, 48369, 48276, 48184, 48091, 47999, 47907, 47814, 47722, 47630, 47538, 47446, 47354, 47263, 47171, 47079, 46988, 46896, 46804, 46713, 46622, 46530, 46439, 46348, 46257, 60 | 46166, 46075, 45984, 45893, 45802, 45711, 45621, 45530, 45440, 45349, 45259, 45168, 45078, 44988, 44898, 44807, 44717, 44627, 44538, 44448, 44358, 44268, 44178, 44089, 43999, 43910, 43820, 43731, 43642, 43552, 43463, 43374, 61 | 43285, 43196, 43107, 43018, 42929, 42840, 42752, 42663, 42575, 42486, 42398, 42309, 42221, 42132, 42044, 41956, 41868, 41780, 41692, 41604, 41516, 41428, 41340, 41253, 41165, 41078, 40990, 40903, 40815, 40728, 40640, 40553, 62 | 40466, 40379, 40292, 40205, 40118, 40031, 39944, 39857, 39771, 39684, 39597, 39511, 39424, 39338, 39252, 39165, 39079, 38993, 38907, 38821, 38735, 38649, 38563, 38477, 38391, 38305, 38220, 38134, 38048, 37963, 37877, 37792, 63 | 37707, 37621, 37536, 37451, 37366, 37281, 37196, 37111, 37026, 36941, 36856, 36771, 36687, 36602, 36517, 36433, 36348, 36264, 36179, 36095, 36011, 35927, 35843, 35758, 35674, 35590, 35506, 35423, 35339, 35255, 35171, 35088, 64 | 35004, 34920, 34837, 34753, 34670, 34587, 34503, 34420, 34337, 34254, 34171, 34088, 34005, 33922, 33839, 33756, 33673, 33591, 33508, 33425, 33343, 33260, 33178, 33095, 33013, 32931, 32848, 32766, 32684, 32602, 32520, 32438, 65 | 32356, 32274, 32192, 32110, 32029, 31947, 31865, 31784, 31702, 31621, 31539, 31458, 31377, 31295, 31214, 31133, 31052, 30971, 30890, 30809, 30728, 30647, 30566, 30485, 30404, 30324, 30243, 30163, 30082, 30002, 29921, 29841, 66 | 29760, 29680, 29600, 29520, 29440, 29359, 29279, 29199, 29119, 29040, 28960, 28880, 28800, 28720, 28641, 28561, 28482, 28402, 28323, 28243, 28164, 28085, 28005, 27926, 27847, 27768, 27689, 27610, 27531, 27452, 27373, 27294, 67 | 27215, 27136, 27058, 26979, 26901, 26822, 26743, 26665, 26587, 26508, 26430, 26352, 26273, 26195, 26117, 26039, 25961, 25883, 25805, 25727, 25649, 25571, 25494, 25416, 25338, 25261, 25183, 25106, 25028, 24951, 24873, 24796, 68 | 24719, 24641, 24564, 24487, 24410, 24333, 24256, 24179, 24102, 24025, 23948, 23871, 23794, 23718, 23641, 23564, 23488, 23411, 23335, 23258, 23182, 23106, 23029, 22953, 22877, 22801, 22724, 22648, 22572, 22496, 22420, 22344, 69 | 22269, 22193, 22117, 22041, 21965, 21890, 21814, 21739, 21663, 21588, 21512, 21437, 21361, 21286, 21211, 21136, 21060, 20985, 20910, 20835, 20760, 20685, 20610, 20535, 20461, 20386, 20311, 20236, 20162, 20087, 20012, 19938, 70 | 19863, 19789, 19715, 19640, 19566, 19492, 19417, 19343, 19269, 19195, 19121, 19047, 18973, 18899, 18825, 18751, 18677, 18603, 18530, 18456, 18382, 18309, 18235, 18162, 18088, 18015, 17941, 17868, 17795, 17721, 17648, 17575, 71 | 17502, 17429, 17356, 17282, 17209, 17137, 17064, 16991, 16918, 16845, 16772, 16700, 16627, 16554, 16482, 16409, 16337, 16264, 16192, 16119, 16047, 15975, 15902, 15830, 15758, 15686, 15614, 15542, 15470, 15398, 15326, 15254, 72 | 15182, 15110, 15038, 14966, 14895, 14823, 14751, 14680, 14608, 14537, 14465, 14394, 14322, 14251, 14180, 14108, 14037, 13966, 13895, 13824, 13752, 13681, 13610, 13539, 13468, 13398, 13327, 13256, 13185, 13114, 13044, 12973, 73 | 12902, 12832, 12761, 12691, 12620, 12550, 12479, 12409, 12338, 12268, 12198, 12128, 12058, 11987, 11917, 11847, 11777, 11707, 11637, 11567, 11497, 11428, 11358, 11288, 11218, 11149, 11079, 11009, 10940, 10870, 10801, 10731, 74 | 10662, 10592, 10523, 10454, 10384, 10315, 10246, 10177, 10107, 10038, 9969, 9900, 9831, 9762, 9693, 9624, 9556, 9487, 9418, 9349, 9280, 9212, 9143, 9075, 9006, 8937, 8869, 8800, 8732, 8664, 8595, 8527, 75 | 8459, 8390, 8322, 8254, 8186, 8118, 8050, 7982, 7914, 7846, 7778, 7710, 7642, 7574, 7506, 7439, 7371, 7303, 7236, 7168, 7100, 7033, 6965, 6898, 6830, 6763, 6696, 6628, 6561, 6494, 6427, 6359, 76 | 6292, 6225, 6158, 6091, 6024, 5957, 5890, 5823, 5756, 5689, 5622, 5556, 5489, 5422, 5355, 5289, 5222, 5156, 5089, 5023, 4956, 4890, 4823, 4757, 4690, 4624, 4558, 4492, 4425, 4359, 4293, 4227, 77 | 4161, 4095, 4029, 3963, 3897, 3831, 3765, 3699, 3633, 3568, 3502, 3436, 3370, 3305, 3239, 3174, 3108, 3043, 2977, 2912, 2846, 2781, 2715, 2650, 2585, 2520, 2454, 2389, 2324, 2259, 2194, 2129, 78 | 2064, 1999, 1934, 1869, 1804, 1739, 1674, 1609, 1545, 1480, 1415, 1350, 1286, 1221, 1157, 1092, 1028, 963, 899, 834, 770, 705, 641, 577, 513, 448, 384, 320, 256, 192, 128, 64, 79 | 0 80 | }; 81 | 82 | _Static_assert((sizeof(_log_discrete_array)/sizeof(_log_discrete_array[0]))==LOG_DISCRETE_ARRAY, "_log_discrete_array size error"); 83 | 84 | #define CLOSED_FILE (int)-10 85 | static int _fd=CLOSED_FILE; 86 | 87 | typedef struct rand_entropy_t { 88 | uint8_t __rand_data[DISCRETE_LOG_MAX]; 89 | uint32_t __entropy_val[_ENTROPY_NUMBER_OF_ELEMENTS]; 90 | } RAND_ENTROPY; 91 | 92 | _Static_assert(sizeof(uint64_t)==sizeof(time_t), "Wrong timestamp adjust"); 93 | 94 | inline 95 | void open_random(char *filename) 96 | { 97 | #define FILE_NAME "/dev/urandom" 98 | 99 | N_DEBUG("Opening random") 100 | _fd=open((filename)?filename:FILE_NAME, O_RDONLY); 101 | N_DEBUGF("File descriptor opened %s %d", filename, _fd) 102 | 103 | #undef FILE_NAME 104 | } 105 | 106 | inline 107 | void close_random() { 108 | if (_fd>-1) { 109 | N_DEBUGF("Closing ramdom descriptor %d", _fd) 110 | if (close(_fd)<0) 111 | N_WARN("Could not close random file\n") 112 | else { 113 | _fd=CLOSED_FILE; 114 | N_DEBUG("Closed ramdom\n") 115 | } 116 | } 117 | } 118 | 119 | int gen_rand_no_entropy_util(uint8_t *output, size_t output_len) 120 | { 121 | if (((size_t)(read(_fd, (void *)output, output_len)))==output_len) 122 | return 0; 123 | 124 | return -2; 125 | } 126 | 127 | static 128 | uint64_t *getCurrentSecond(uint64_t *timestamp) 129 | { 130 | struct timespec now; 131 | 132 | if (!clock_gettime(CLOCK_MONOTONIC_RAW, &now)) { 133 | *timestamp=(uint64_t)now.tv_sec; 134 | return timestamp; 135 | } 136 | 137 | return NULL; 138 | } 139 | 140 | #define rand_entropy_init \ 141 | (RAND_ENTROPY *)malloc(sizeof(RAND_ENTROPY)) 142 | 143 | #define rand_entropy_finish \ 144 | free(memset((void *)rand_entropy, 0, sizeof(RAND_ENTROPY))); 145 | 146 | #define clear_entropy \ 147 | memset((void *)rand_entropy->__entropy_val, 0, sizeof(rand_entropy->__entropy_val)); 148 | 149 | /** 150 | * @fn int verify_system_entropy(uint32_t type, void *rand, size_t rand_size, int timeoutInS) 151 | * @brief Take a random number generator function and returns random value only if randomized data have a desired entropy value 152 | * @param [in] type Entropy type. Entropy type values are: 153 | * - F_ENTROPY_TYPE_PARANOIC Highest level entropy recommended for generate a paranoic entropy. Very slow 154 | * - F_ENTROPY_TYPE_EXCELENT Gives a very excellent entropy. Slow 155 | * - F_ENTROPY_TYPE_GOOD Good entropy type. Normal. 156 | * - F_ENTROPY_TYPE_NOT_ENOUGH Moderate entropy. Usually fast to create a temporary random values. Fast 157 | * - F_ENTROPY_TYPE_NOT_RECOMENDED Fast but not recommended for generating ramdom. 158 | * @param [out] rand Random data with a satisfied type of entropy 159 | * @param [in] rand_size Size of random data output 160 | * @param [in] timeoutInS timeout for random generator 161 | * 162 | * NOTE: This implementation is based on topic in Definition 7.12 in MIT opencourseware (7.3 A Statistical Definition of Entropy - 2005)
Many thanks to Professor Z. S. Spakovszky for this amazing topic 163 | * @retval 0: On Success, otherwise Error 164 | */ 165 | int verify_system_entropy( 166 | uint32_t type, 167 | uint8_t *rand, 168 | size_t rand_size, 169 | uint64_t timeoutInS 170 | ) 171 | { 172 | int err; 173 | size_t i; 174 | uint64_t final, timestamp; 175 | RAND_ENTROPY *rand_entropy; 176 | 177 | if (rand_size>sizeof(rand_entropy->__rand_data)) 178 | return 49; 179 | 180 | if (!(rand_entropy=rand_entropy_init)) 181 | return 50; 182 | 183 | if (!getCurrentSecond(×tamp)) { 184 | err=51; 185 | goto verify_system_entropy_EXIT; 186 | } 187 | 188 | timeoutInS=timeoutInS+timestamp; 189 | 190 | err=0; 191 | 192 | verify_system_entropy_RET: 193 | 194 | if (gen_rand_no_entropy_util(rand_entropy->__rand_data, sizeof(rand_entropy->__rand_data))) { 195 | err=52; 196 | goto verify_system_entropy_EXIT; 197 | } 198 | 199 | final=0; 200 | 201 | clear_entropy 202 | 203 | for (i=0;i__entropy_val[rand_entropy->__rand_data[i]]+=1; 205 | 206 | for (i=0;i<_ENTROPY_NUMBER_OF_ELEMENTS;i++) 207 | final+=rand_entropy->__entropy_val[i]*_log_discrete_array[rand_entropy->__entropy_val[i]]; 208 | 209 | if (!getCurrentSecond(×tamp)) { 210 | err=53; 211 | goto verify_system_entropy_EXIT; 212 | } 213 | 214 | if (timestamp>timeoutInS) { 215 | err=54; 216 | goto verify_system_entropy_EXIT; 217 | } 218 | 219 | if ((uint64_t)type>final) 220 | goto verify_system_entropy_RET; 221 | 222 | memcpy((void *)rand, (const void *)rand_entropy->__rand_data, rand_size); 223 | 224 | verify_system_entropy_EXIT: 225 | rand_entropy_finish 226 | 227 | return err; 228 | } 229 | 230 | char _cauth_rnd[]={ 231 | 'a', 'b', 'c', 'd', 'e', 'f', 'g', '\\', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 232 | '9', 'Y', ':', 'Z', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 233 | 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '^', ';', '<', '=', '>', '?', 234 | '1', 'Q', '2', 'R', '3', 'S', '4', 'T', '5', 'U', '6', 'V', '7', 'W', '8', 'X', 235 | '`', 'p', 'a', 'q', 'b', 'r', 'c', 's', 'd', 't', 'e', 'u', 'f', 'v', 'g', 'w', 236 | '!', 'A', '[', 'B', '#', 'C', '$', 'D', '%', 'E', '&', 'F', ']', 'G', '(', 'H', 237 | 'h', 'x', 'i', 'y', 'j', 'z', 'k', '{', 'l', '|', 'm', '}', 'n', '~', 'o', '_', 238 | ')', 'I', '*', 'J', '+', 'K', ',', 'L', '-', 'M', '.', 'N', '/', 'O', '0', 'P' 239 | }; 240 | 241 | _Static_assert(sizeof(_cauth_rnd)==128, "_cauth_rnd wrong size"); 242 | 243 | struct pass_list_t *pass_list_new(uint16_t numberOfList) 244 | { 245 | size_t sz; 246 | struct pass_list_t *p; 247 | struct pass_list_vec_t *v=(struct pass_list_vec_t *)malloc(sz=(size_t)(numberOfList*sizeof(struct pass_list_vec_t))); 248 | 249 | N_DEBUGF("Init create random password list vector %d. Start vector %p of size %lu", (int)numberOfList, v, sz) 250 | if (!v) 251 | return NULL; 252 | 253 | if ((p=(struct pass_list_t *)malloc(sizeof(struct pass_list_t)))) { 254 | p->n=numberOfList; 255 | p->vec_size=sz; 256 | p->vec=v; 257 | N_DEBUGF("Init create random list. Start %p of size %lu", p, sizeof(struct pass_list_t)) 258 | } else { 259 | free((void *)v); 260 | N_DEBUGF("Could not create pass list :(. Vector v(%p) was free", v) 261 | } 262 | 263 | return p; 264 | } 265 | 266 | inline 267 | int randomize_and_print_pass_list(struct pass_list_t *pass_list, uint32_t type, uint64_t timeoutInS) 268 | { 269 | int n, k=verify_system_entropy(type, (uint8_t *)pass_list->vec, pass_list->vec_size, timeoutInS); 270 | struct pass_list_vec_t *pass_list_vec; 271 | 272 | if (k) 273 | return k; 274 | 275 | n=(int)pass_list->n; 276 | 277 | #define PASS_LEN (int)sizeof(((struct pass_list_vec_t *)0)->password) 278 | 279 | while ((--n)>-1) { 280 | pass_list_vec=&pass_list->vec[(size_t)n]; 281 | for (k=0; kpassword[k]=_cauth_rnd[pass_list_vec->password[k]&0x7F]; 283 | fprintf(stdout, "\nPASSWORD GENERATED %.*s", PASS_LEN, pass_list_vec->password); 284 | } 285 | 286 | #undef PASS_LEN 287 | return 0; 288 | } 289 | 290 | void pass_list_free(struct pass_list_t **pass_list) 291 | { 292 | uint8_t *v; 293 | size_t sz; 294 | 295 | if (*pass_list) { 296 | 297 | v=(uint8_t *)((*pass_list)->vec); 298 | sz=(*pass_list)->vec_size; 299 | 300 | N_DEBUGF("Begin cleaning password in pass_list %p\n\tBefore:", *pass_list) 301 | N_DEBUG_DUMP_A(v, sz) 302 | 303 | memset((void *)v, 0, sz); 304 | N_DEBUG("\tAfter (clear):") 305 | N_DEBUG_DUMP_A(v, sz) 306 | 307 | if (gen_rand_no_entropy_util(v, sz)) 308 | N_WARN("Could not fill random password. Ignore") 309 | 310 | N_DEBUG("\tAfter (dirty):") 311 | N_DEBUG_DUMP_A(v, sz) 312 | 313 | N_DEBUGF("Free pass_list %p", *pass_list) 314 | free((void *)(*pass_list)); 315 | N_DEBUGF("Free vector %p", v) 316 | free((void *)v); 317 | 318 | *pass_list=NULL; 319 | } 320 | } 321 | 322 | -------------------------------------------------------------------------------- /src/utility.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #ifdef BE 15 | uint32_t setU32_LE(uint32_t value) 16 | { 17 | N_DEBUG("setU32_LE : (BIG ENDIAN)") 18 | union val_u { 19 | uint8_t u8[sizeof(uint32_t)]; 20 | uint32_t u32; 21 | } val; 22 | uint8_t u8_tmp; 23 | 24 | val.u32=value; 25 | 26 | u8_tmp=val.u8[0]; 27 | val.u8[0]=val.u8[3]; 28 | val.u8[3]=u8_tmp; 29 | 30 | u8_tmp=val.u8[1]; 31 | val.u8[1]=val.u8[2]; 32 | val.u8[2]=u8_tmp; 33 | 34 | return val.u32; 35 | } 36 | 37 | uint64_t setU64_LE(uint64_t value) 38 | { 39 | N_DEBUG("setU64_LE : (BIG ENDIAN)") 40 | union val_u { 41 | uint8_t u8[sizeof(uint64_t)]; 42 | uint64_t u64; 43 | } val; 44 | uint8_t u8_tmp; 45 | 46 | val.u64=value; 47 | 48 | //0 - 7 49 | //1 - 6 50 | //2 - 5 51 | //3 - 4 52 | 53 | u8_tmp=val.u8[0]; 54 | val.u8[0]=val.u8[7]; 55 | val.u8[7]=u8_tmp; 56 | 57 | u8_tmp=val.u8[1]; 58 | val.u8[1]=val.u8[6]; 59 | val.u8[6]=u8_tmp; 60 | 61 | u8_tmp=val.u8[2]; 62 | val.u8[2]=val.u8[5]; 63 | val.u8[5]=u8_tmp; 64 | 65 | u8_tmp=val.u8[3]; 66 | val.u8[3]=val.u8[4]; 67 | val.u8[4]=u8_tmp; 68 | 69 | return val.u64; 70 | } 71 | 72 | #else 73 | #ifdef LE 74 | inline uint32_t setU32_LE(uint32_t value) 75 | { 76 | N_DEBUG("setU32_LE : (LITTLE ENDIAN)") 77 | return value; 78 | } 79 | 80 | inline uint64_t setU64_LE(uint64_t value) 81 | { 82 | N_DEBUG("setU64_LE : (LITTLE ENDIAN)") 83 | return value; 84 | } 85 | #else 86 | #error "Define LE or BE" 87 | #endif 88 | #endif 89 | 90 | int writeToFile(const char *filename, uint8_t *data, size_t data_sz) 91 | { 92 | int err; 93 | FILE *f; 94 | 95 | if (!(f=fopen(filename, "w"))) 96 | return -2; 97 | 98 | err=(fwrite((void *)data, sizeof(uint8_t), data_sz, f)==data_sz)?0:-3; 99 | 100 | fclose(f); 101 | 102 | return err; 103 | } 104 | 105 | #define SSL_MSG_SET(msg) \ 106 | if (errMsg) \ 107 | *errMsg=msg; 108 | 109 | int ssl_hash512(uint8_t *out, uint8_t *data, size_t data_sz, char **errMsg) 110 | { 111 | OSSL_LIB_CTX *library_context; 112 | int err; 113 | EVP_MD *message_digest; 114 | EVP_MD_CTX *digest_context; 115 | unsigned int digest_length; 116 | 117 | library_context=OSSL_LIB_CTX_new(); 118 | if (library_context == NULL) { 119 | SSL_MSG_SET("OSSL_LIB_CTX_new() returned NULL\n"); 120 | return -20; 121 | } 122 | 123 | /* 124 | * Fetch a message digest by name 125 | * The algorithm name is case insensitive. 126 | * See providers(7) for details about algorithm fetching 127 | */ 128 | message_digest=EVP_MD_fetch(library_context, "SHA512", NULL); 129 | if (message_digest == NULL) { 130 | err=-21; 131 | SSL_MSG_SET("EVP_MD_fetch could not find SHA512.") 132 | goto ssl_hash512_resume1; 133 | } 134 | 135 | /* Determine the length of the fetched digest type */ 136 | digest_length=EVP_MD_get_size(message_digest); 137 | if (digest_length != 64) { 138 | err=-22; 139 | SSL_MSG_SET("EVP_MD_get_size returned invalid size.\n") 140 | goto ssl_hash512_resume2; 141 | } 142 | 143 | /* 144 | * Make a message digest context to hold temporary state 145 | * during digest creation 146 | */ 147 | digest_context = EVP_MD_CTX_new(); 148 | if (digest_context == NULL) { 149 | err=-24; 150 | SSL_MSG_SET("EVP_MD_CTX_new failed.\n") 151 | goto ssl_hash512_resume2; 152 | } 153 | /* 154 | * Initialize the message digest context to use the fetched 155 | * digest provider 156 | */ 157 | if (EVP_DigestInit(digest_context, message_digest) != 1) { 158 | err=-25; 159 | SSL_MSG_SET("EVP_DigestInit failed.\n") 160 | goto ssl_hash512_resume3; 161 | } 162 | if (EVP_DigestUpdate(digest_context, (const void *)data, data_sz) != 1) { 163 | err=-26; 164 | SSL_MSG_SET("EVP_DigestUpdate(data) failed.\n") 165 | goto ssl_hash512_resume3; 166 | } 167 | if (EVP_DigestFinal(digest_context, (unsigned char *)out, &digest_length) != 1) { 168 | err=-27; 169 | SSL_MSG_SET("EVP_DigestFinal() failed.\n") 170 | goto ssl_hash512_resume3; 171 | } 172 | 173 | err=0; 174 | SSL_MSG_SET("Hash 512 success") 175 | if (digest_length != 64) { 176 | err=-28; 177 | SSL_MSG_SET("Hash 512 size error") 178 | } 179 | 180 | ssl_hash512_resume3: 181 | EVP_MD_CTX_free(digest_context); 182 | 183 | ssl_hash512_resume2: 184 | EVP_MD_free(message_digest); 185 | 186 | ssl_hash512_resume1: 187 | OSSL_LIB_CTX_free(library_context); 188 | return err; 189 | } 190 | 191 | int pbkdf2( 192 | uint8_t *out, size_t out_sz, 193 | const char *password, int password_len, 194 | uint8_t *salt, uint32_t salt_sz, 195 | uint32_t iter, 196 | char **errMsg 197 | ) 198 | { 199 | int err;; 200 | EVP_KDF *kdf; 201 | EVP_KDF_CTX *kctx; 202 | // unsigned char out[64]; 203 | OSSL_PARAM params[5], *p; 204 | OSSL_LIB_CTX *library_context; 205 | unsigned int pbkdf2_iteration; 206 | 207 | if (!password) { 208 | SSL_MSG_SET("Null password\n") 209 | return -30; 210 | } 211 | 212 | if (password_len < 0) 213 | password_len=(int)strlen(password); 214 | 215 | if (!password_len) { 216 | SSL_MSG_SET("Empty password\n") 217 | return -31; 218 | } 219 | 220 | library_context=OSSL_LIB_CTX_new(); 221 | if (library_context==NULL) { 222 | SSL_MSG_SET("OSSL_LIB_CTX_new() returned NULL\n") 223 | return -32; 224 | } 225 | 226 | /* Fetch the key derivation function implementation */ 227 | kdf = EVP_KDF_fetch(library_context, "PBKDF2", NULL); 228 | if (kdf == NULL) { 229 | err=-33; 230 | SSL_MSG_SET("EVP_KDF_fetch() returned NULL\n") 231 | goto pbkdf2_exit1; 232 | } 233 | 234 | /* Create a context for the key derivation operation */ 235 | kctx = EVP_KDF_CTX_new(kdf); 236 | if (kctx == NULL) { 237 | err=-32; 238 | SSL_MSG_SET("EVP_KDF_CTX_new() returned NULL\n") 239 | goto pbkdf2_exit2; 240 | } 241 | 242 | pbkdf2_iteration=(unsigned int)iter; 243 | p=params; 244 | 245 | /* Set password */ 246 | *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD, (unsigned char *)password, (unsigned int)password_len); 247 | /* Set salt */ 248 | *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, (unsigned char *)salt, (unsigned int)salt_sz); 249 | /* Set iteration count */ 250 | *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_ITER, &pbkdf2_iteration); 251 | /* Set the underlying hash function used to derive the key */ 252 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, "SHA256", 0); 253 | 254 | *p = OSSL_PARAM_construct_end(); 255 | 256 | err=0; 257 | SSL_MSG_SET("PBKDF2-SHA256 success\n") 258 | /* Derive the key */ 259 | if (EVP_KDF_derive(kctx, (unsigned char *)out, out_sz, params) != 1) { 260 | err=-33; 261 | SSL_MSG_SET("EVP_KDF_derive() failed\n") 262 | } 263 | 264 | // if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) { 265 | 266 | EVP_KDF_CTX_free(kctx); 267 | 268 | pbkdf2_exit2: 269 | EVP_KDF_free(kdf); 270 | 271 | pbkdf2_exit1: 272 | OSSL_LIB_CTX_free(library_context); 273 | 274 | return err; 275 | } 276 | 277 | int argon2id( 278 | uint8_t *out, size_t out_sz, 279 | const char *password, int password_len, 280 | uint8_t *salt, uint32_t salt_sz, 281 | uint8_t *additional, uint32_t additional_sz, //Optional. can be null 282 | uint8_t *secret, uint32_t secret_sz, //Optional. can be null 283 | uint32_t memory_cost, 284 | uint32_t iteration_cost, 285 | uint32_t parallel_cost, 286 | char **errMsg 287 | ) 288 | { 289 | int err; 290 | EVP_KDF *kdf; 291 | EVP_KDF_CTX *kctx; 292 | 293 | OSSL_PARAM params[9], *p = params; 294 | OSSL_LIB_CTX *library_context; 295 | unsigned int threads, m_cost, i_cost, p_cost; 296 | 297 | if (!password) { 298 | SSL_MSG_SET("Null password\n") 299 | return -40; 300 | } 301 | 302 | if (password_len<0) 303 | password_len=(int)strlen(password); 304 | 305 | if (!password_len) { 306 | SSL_MSG_SET("Empty password\n") 307 | return -41; 308 | } 309 | 310 | library_context=OSSL_LIB_CTX_new(); 311 | if (library_context == NULL) { 312 | SSL_MSG_SET("OSSL_LIB_CTX_new() returned NULL\n") 313 | return -41; 314 | } 315 | 316 | /* Fetch the key derivation function implementation */ 317 | kdf = EVP_KDF_fetch(library_context, "argon2id", NULL); 318 | if (kdf == NULL) { 319 | err=-42; 320 | SSL_MSG_SET("EVP_KDF_fetch() returned NULL\n") 321 | goto argon2id_exit1; 322 | } 323 | 324 | /* Create a context for the key derivation operation */ 325 | kctx = EVP_KDF_CTX_new(kdf); 326 | if (kctx == NULL) { 327 | err=-43; 328 | SSL_MSG_SET("EVP_KDF_CTX_new() returned NULL\n") 329 | goto argon2id_exit2; 330 | } 331 | 332 | /* 333 | * Thread support can be turned off; use serialization if we cannot 334 | * set requested number of threads. 335 | */ 336 | threads = (unsigned int)parallel_cost; 337 | if (OSSL_set_max_threads(library_context, (unsigned int)parallel_cost) != 1) { 338 | uint64_t max_threads = OSSL_get_max_threads(library_context); 339 | 340 | if (max_threads == 0) 341 | threads = 1; 342 | else if (max_threads < parallel_cost) 343 | threads = (unsigned int)max_threads; 344 | } 345 | 346 | m_cost=(unsigned int)memory_cost; 347 | i_cost=(unsigned int)iteration_cost; 348 | p_cost=(unsigned int)parallel_cost; 349 | 350 | /* Set password */ 351 | *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD, (unsigned char *)password, (unsigned int)password_len); 352 | /* Set salt */ 353 | *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, (unsigned char *)salt, (unsigned int)salt_sz); 354 | 355 | /* Set optional additional data */ 356 | if (additional) 357 | *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_ARGON2_AD, (unsigned char *)additional, (unsigned int)additional_sz); 358 | 359 | /* Set optional secret */ 360 | if (secret) 361 | *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SECRET, (unsigned char *)secret, (unsigned int)secret_sz); 362 | 363 | /* Set iteration count */ 364 | *p++ = OSSL_PARAM_construct_uint32(OSSL_KDF_PARAM_ITER, &i_cost); 365 | /* Set threads performing derivation (can be decreased) */ 366 | *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_THREADS, &threads); 367 | /* Set parallel cost */ 368 | *p++ = OSSL_PARAM_construct_uint32(OSSL_KDF_PARAM_ARGON2_LANES, &p_cost); 369 | /* Set memory requirement */ 370 | *p++ = OSSL_PARAM_construct_uint32(OSSL_KDF_PARAM_ARGON2_MEMCOST, &m_cost); 371 | *p = OSSL_PARAM_construct_end(); 372 | 373 | err=0; 374 | SSL_MSG_SET("Argon2id success\n") 375 | 376 | /* Derive the key */ 377 | if (EVP_KDF_derive(kctx, (unsigned char *)out, (unsigned int)out_sz, params) != 1) { 378 | err=-44; 379 | SSL_MSG_SET("EVP_KDF_derive() failed\n") 380 | } 381 | 382 | EVP_KDF_CTX_free(kctx); 383 | 384 | argon2id_exit2: 385 | EVP_KDF_free(kdf); 386 | 387 | argon2id_exit1: 388 | OSSL_LIB_CTX_free(library_context); 389 | 390 | return err; 391 | } 392 | 393 | int aes_256_cbc_encrypt( 394 | uint8_t *out, size_t *out_size, 395 | uint8_t *in, size_t in_size, 396 | uint8_t *iv, uint8_t *priv_key, 397 | char **errMsg 398 | ) 399 | { 400 | 401 | int err; 402 | EVP_CIPHER_CTX *ctx; 403 | EVP_CIPHER *cipher; 404 | // 405 | 406 | if (((*out_size)==0)||(in_size==0)) { 407 | SSL_MSG_SET("Invalid output/input data\n") 408 | return -48; 409 | } 410 | 411 | if (in_size&0x0F) { 412 | SSL_MSG_SET("Plain text must be aligned\n") 413 | return -49; 414 | } 415 | 416 | if (in_size>(*out_size)) { 417 | SSL_MSG_SET("No space for encrypt data\n") 418 | return -50; 419 | } 420 | 421 | /* Create a context for the encrypt operation */ 422 | if ((ctx = EVP_CIPHER_CTX_new()) == NULL) { 423 | SSL_MSG_SET("Could not init AES context\n") 424 | return -51; 425 | } 426 | 427 | EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPH_NO_PADDING); 428 | 429 | /* Fetch the cipher implementation */ 430 | if ((cipher = EVP_CIPHER_fetch(NULL, "AES-256-CBC", NULL)) == NULL) { 431 | err=-52; 432 | SSL_MSG_SET("Could not fetch cipher\n") 433 | goto aes_256_cbc_encrypt_exit1; 434 | } 435 | 436 | /* 437 | * Initialise an encrypt operation with the cipher/mode, key and IV. 438 | * We are not setting any custom params so let params be just NULL. 439 | */ 440 | if (!EVP_EncryptInit_ex2(ctx, cipher, (const unsigned char *)priv_key, (const unsigned char *)iv, /* params */ NULL)) { 441 | err=-53; 442 | SSL_MSG_SET("Could not init encrypt\n") 443 | goto aes_256_cbc_encrypt_exit2; 444 | } 445 | 446 | /* Encrypt plaintext */ 447 | if (!EVP_EncryptUpdate(ctx, (unsigned char *)out, (int *)out_size, (unsigned char *)in, (int)in_size)) { 448 | err=-54; 449 | SSL_MSG_SET("Could not update encrypt data\n") 450 | goto aes_256_cbc_encrypt_exit2; 451 | } 452 | 453 | // *out_size=in_size; 454 | /* 455 | //NOT NECESSARY if EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPH_NO_PADDING); 456 | // See https://www.openssl.org/docs/man3.1/man3/EVP_EncryptFinal_ex.html 457 | // Finalise: there can be some additional output from padding / 458 | if (!EVP_EncryptFinal_ex(ctx, (((unsigned char *)out) + *out_size), &tmp_size)) { 459 | err=-55; 460 | SSL_MSG_SET("Could not finalize encrypt data\n") 461 | goto aes_256_cbc_encrypt_exit2; 462 | } 463 | */ 464 | err=0; 465 | SSL_MSG_SET("Encrypt SUCCESS\n") 466 | 467 | aes_256_cbc_encrypt_exit2: 468 | EVP_CIPHER_free(cipher); 469 | 470 | aes_256_cbc_encrypt_exit1: 471 | EVP_CIPHER_CTX_free(ctx); 472 | 473 | return err; 474 | } 475 | 476 | int aes_256_cbc_decrypt( 477 | uint8_t *out, size_t *out_size, 478 | uint8_t *in, size_t in_size, 479 | uint8_t *iv, uint8_t *priv_key, 480 | char **errMsg 481 | ) 482 | { 483 | 484 | int err; 485 | EVP_CIPHER_CTX *ctx; 486 | EVP_CIPHER *cipher; 487 | 488 | if (((*out_size)==0)||(in_size==0)) { 489 | SSL_MSG_SET("Invalid output/input data\n") 490 | return -60; 491 | } 492 | 493 | if (in_size&0x0F) { 494 | SSL_MSG_SET("Encrypted data must be aligned\n") 495 | return -61; 496 | } 497 | 498 | if (in_size>(*out_size)) { 499 | SSL_MSG_SET("No space for decrypt data\n") 500 | return -62; 501 | } 502 | 503 | if ((ctx = EVP_CIPHER_CTX_new()) == NULL) { 504 | SSL_MSG_SET("Could not init AES context\n") 505 | return -63; 506 | } 507 | 508 | EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPH_NO_PADDING); 509 | 510 | if ((cipher = EVP_CIPHER_fetch(NULL, "AES-256-CBC", NULL)) == NULL) { 511 | err=-64; 512 | SSL_MSG_SET("Could not fetch cipher\n") 513 | goto aes_256_cbc_decrypt_exit1; 514 | } 515 | 516 | /* 517 | * Initialise an encrypt operation with the cipher/mode, key and IV. 518 | * We are not setting any custom params so let params be just NULL. 519 | */ 520 | if (!EVP_DecryptInit_ex2(ctx, cipher, (const unsigned char *)priv_key, (const unsigned char *)iv, /* params */ NULL)) { 521 | err=-64; 522 | SSL_MSG_SET("Could not init decrypt\n") 523 | goto aes_256_cbc_decrypt_exit2; 524 | } 525 | 526 | /* Decrypt plaintext */ 527 | if (!EVP_DecryptUpdate(ctx, (unsigned char *)out, (int *)out_size, (unsigned char *)in, (int)in_size)) { 528 | err=-65; 529 | SSL_MSG_SET("Could not update decrypt data\n") 530 | goto aes_256_cbc_decrypt_exit2; 531 | } 532 | 533 | err=0; 534 | SSL_MSG_SET("Decrypt SUCCESS\n") 535 | 536 | /* 537 | //NOT NECESSARY if EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPH_NO_PADDING); 538 | // See https://www.openssl.org/docs/man3.1/man3/EVP_EncryptFinal_ex.html 539 | Finalise: there can be some additional output from padding 540 | if (!EVP_DecryptFinal_ex(ctx, outbuf + outlen, &tmplen)) 541 | goto err; 542 | outlen += tmplen; 543 | */ 544 | 545 | aes_256_cbc_decrypt_exit2: 546 | EVP_CIPHER_free(cipher); 547 | 548 | aes_256_cbc_decrypt_exit1: 549 | EVP_CIPHER_CTX_free(ctx); 550 | 551 | return err; 552 | } 553 | #undef SSL_MSG_SET 554 | 555 | #define N_ALIGNMENT (size_t)(1<<4) 556 | #define N_ALIGNMENT_MASK (size_t)(N_ALIGNMENT-1) 557 | 558 | #define N_ALIGN \ 559 | size_t size_tmp=size; \ 560 | \ 561 | if (size_tmp&(N_ALIGNMENT_MASK)) {\ 562 | size_tmp&=(~N_ALIGNMENT_MASK);\ 563 | size_tmp+=N_ALIGNMENT;\ 564 | } 565 | 566 | #ifndef VISIBLE_FOR_TEST 567 | static 568 | #endif 569 | void *n_malloc(size_t *size_align, size_t size) 570 | { 571 | uint8_t *result; 572 | //size_align >= size 573 | N_ALIGN 574 | 575 | N_DEBUGF("Entering n_malloc. Required size %lu. Alloc'd size: %lu. Offset: %lu", size, size_tmp, size_tmp-size) 576 | 577 | *size_align=size_tmp; 578 | 579 | #ifdef DEBUG 580 | if (size_tmp(((long int)FILE_BUFFER_SIZE)-decrement)) { 645 | err=-4; 646 | goto readFileAlign_exit1; 647 | } 648 | 649 | if (!((*data)=(uint8_t *)n_malloc(data_size_align, (size_t)l))) { 650 | err=-5; 651 | goto readFileAlign_exit1; 652 | } 653 | 654 | N_DEBUGF("Alloc'd data %p", *data) 655 | 656 | (*data_size)=(size_t)l; 657 | 658 | err=0; 659 | 660 | rewind(f); 661 | 662 | N_DEBUGF("Reading file %s", filename) 663 | 664 | if (fread((void *)(*data), sizeof(const char), (*data_size), f)==(*data_size)) 665 | goto readFileAlign_exit1; 666 | 667 | N_DEBUGF("Freeing data %p", *data) 668 | 669 | free((void *)(*data)); 670 | 671 | err=-6; 672 | (*data)=NULL; 673 | (*data_size)=0; 674 | (*data_size_align)=0; 675 | 676 | readFileAlign_exit1: 677 | N_DEBUGF("Closing file %s with err = %d", filename, err) 678 | fclose(f); 679 | 680 | return err; 681 | } 682 | 683 | inline 684 | int readFileAlign(uint8_t **data, size_t *data_size, size_t *data_size_align, const char *filename) 685 | { 686 | return readFileAlignUtil(data, data_size, data_size_align, filename, FILE_STRUCT_SIZE); 687 | } 688 | 689 | inline 690 | int readFileAlignDecrypt(uint8_t **data, size_t *data_size, size_t *data_size_align, const char *filename) 691 | { 692 | return readFileAlignUtil(data, data_size, data_size_align, filename, 0); 693 | } 694 | 695 | void readFileAlignFree(uint8_t **data, size_t data_size) 696 | { 697 | #ifdef DEBUG 698 | int err; 699 | #endif 700 | 701 | if (data) { 702 | memset((void *)*data, 0, data_size); 703 | N_DEBUGF("Cleaning *data(%p) with size = %lu with 0", *data, data_size) 704 | N_DEBUG_DUMP(*data, data_size) 705 | #ifdef DEBUG 706 | err=gen_rand_no_entropy_util(*data, data_size); 707 | N_DEBUGF("Filling *data(%p) with size = %lu with random data. Err = %d", *data, data_size, err) 708 | N_DEBUG_DUMP(*data, data_size) 709 | N_DEBUGF("Freeing *data(%p)\n", *data) 710 | #else 711 | gen_rand_no_entropy_util(*data, data_size); 712 | #endif 713 | 714 | free((void *)*data); 715 | *data=NULL; 716 | } 717 | } 718 | 719 | //filename!=NULL and > 0 data NOT NULL and data_size >0 720 | int writeFileUtil(const char *filename, uint8_t *data, size_t data_size) 721 | { 722 | int err; 723 | FILE *f; 724 | if (!(f=fopen(filename, "w"))) 725 | return -40; 726 | 727 | err=(fwrite((const void *)data, sizeof(const char), data_size, f)==data_size)?0:-41; 728 | 729 | fclose(f); 730 | return err; 731 | } 732 | 733 | uint8_t *xor_n(uint8_t *a, uint8_t *b, size_t n) 734 | { 735 | do { 736 | n--; 737 | a[n]^=b[n]; 738 | } while (n>0); 739 | 740 | return a; 741 | } 742 | //1 for valid, 0 for invalid 743 | int check_hash512(uint8_t *h, uint8_t *data, size_t data_sz, char **errMsg) 744 | { 745 | int err; 746 | uint8_t res[64]; 747 | 748 | if (ssl_hash512(res, data, data_sz, errMsg)) 749 | return 0; 750 | 751 | err=(memcmp(res, h, sizeof(res))==0); 752 | 753 | if (errMsg) 754 | *errMsg=(err)?"Checksum SUCCESS\n":"Wrong checksum\n"; 755 | 756 | return err; 757 | } 758 | 759 | static 760 | int get_console_passwd(char *pass, size_t pass_sz) 761 | { 762 | struct termios oflags, nflags; 763 | int err, i; 764 | 765 | tcgetattr(fileno(stdin), &oflags); 766 | nflags=oflags; 767 | nflags.c_lflag&=~ECHO; 768 | nflags.c_lflag|=ECHONL; 769 | 770 | memset(pass, 0x0A, pass_sz); 771 | 772 | if (tcsetattr(fileno(stdin), TCSADRAIN, &nflags)) return 10; 773 | 774 | if (!fgets(pass, pass_sz, stdin)) { 775 | err=11; 776 | goto PASS_ERR; 777 | } 778 | 779 | err=12; 780 | 781 | for (i=0;ipasswd, (sizeof((*pwd)->passwd)-1)))) { 811 | N_ERRORF("Could not read console password %d\n", err) 812 | goto set_password_fail; 813 | } 814 | 815 | N_DEBUGF("Password typed %s", (*pwd)->passwd) 816 | 817 | fprintf(stdout, "\nRetype your password: "); 818 | 819 | if ((err=get_console_passwd((char *)(*pwd)->retype_passwd, (sizeof((*pwd)->retype_passwd)-1)))) { 820 | N_ERRORF("Could not read console retype password %d\n", err) 821 | goto set_password_fail; 822 | } 823 | 824 | _Static_assert((sizeof(((struct pass_t *)0)->passwd))==(sizeof(((struct pass_t *)0)->retype_passwd)), "Sizeof retype password should be the same"); 825 | 826 | // (*pwd)->len=strlen((*pwd)->passwd); 827 | 828 | N_DEBUGF("Password retyped %s", (*pwd)->retype_passwd) 829 | 830 | if (strcmp((void *)(*pwd)->retype_passwd, (void *)(*pwd)->passwd)) { 831 | err=-121; 832 | N_ERROR("Password does not match\n") 833 | goto set_password_fail; 834 | } 835 | 836 | N_DEBUG("Comparing password vectors") 837 | N_DEBUG_COMP_VEC((*pwd)->passwd, sizeof((*pwd)->passwd), (*pwd)->retype_passwd, sizeof((*pwd)->retype_passwd)) 838 | 839 | return 0; 840 | 841 | set_password_fail: 842 | 843 | memset((void *)(*pwd), 0, sizeof(struct pass_t)); 844 | 845 | if (gen_rand_no_entropy_util((uint8_t *)(*pwd), sizeof(struct pass_t))) 846 | N_WARN("Could not reset random password") 847 | 848 | N_DEBUGF("Filling *pwd(%p) with size = %lu with random data. Err = %d", (*pwd), sizeof(struct pass_t), err) 849 | N_DEBUG_DUMP((*pwd), sizeof(struct pass_t)) 850 | 851 | N_DEBUGF("Could not set password %d\n", err) 852 | 853 | free((void *)(*pwd)); 854 | (*pwd)=NULL; 855 | 856 | return err; 857 | } 858 | 859 | int get_password(struct pass_t **pwd) 860 | { 861 | int err; 862 | 863 | if (!(*pwd=(struct pass_t *)malloc(sizeof(struct pass_t)))) 864 | return -130; 865 | 866 | memset((void *)*pwd, 0, sizeof(struct pass_t)); 867 | 868 | fprintf(stdout, "\nType your password to unlock file: "); 869 | 870 | if ((err=get_console_passwd((char *)(*pwd)->passwd, (sizeof((*pwd)->passwd)-1)))) { 871 | N_ERRORF("Could not read console password (unlock) %d\n", err) 872 | goto get_password_fail; 873 | } 874 | 875 | // (*pwd)->len=strlen((*pwd)->passwd); 876 | 877 | N_DEBUGF("Password typed %s at %p", (*pwd)->passwd, (*pwd)->passwd) 878 | 879 | return 0; 880 | 881 | get_password_fail: 882 | 883 | memset((void *)(*pwd), 0, sizeof(struct pass_t)); 884 | 885 | if (gen_rand_no_entropy_util((uint8_t *)(*pwd), sizeof(struct pass_t))) 886 | N_WARN("Could not reset random password (unlock)") 887 | 888 | N_DEBUGF("Filling (unlock) *pwd(%p) with size = %lu with random data. Err = %d", (*pwd), sizeof(struct pass_t), err) 889 | N_DEBUG_DUMP((*pwd), sizeof(struct pass_t)) 890 | 891 | N_DEBUGF("Could not set password (unlock) %d\n", err) 892 | 893 | free((void *)(*pwd)); 894 | (*pwd)=NULL; 895 | 896 | return err; 897 | } 898 | 899 | void pass_free(struct pass_t **pwd) 900 | { 901 | if (*pwd) { 902 | memset((void *)(*pwd), 0, sizeof(struct pass_t)); 903 | 904 | if (gen_rand_no_entropy_util((uint8_t *)(*pwd), sizeof(struct pass_t))) 905 | N_WARN("Could not reset random password at free") 906 | 907 | N_DEBUGF("Filling *pwd(%p) with size = %lu with random data.", (*pwd), sizeof(struct pass_t)) 908 | N_DEBUG_DUMP((*pwd), sizeof(struct pass_t)) 909 | 910 | free((void *)(*pwd)); 911 | (*pwd)=NULL; 912 | } 913 | } 914 | 915 | inline 916 | char *get_version_str() 917 | { 918 | static char str[16]; 919 | snprintf(str, sizeof(str), "%d.%d.%d", VERSION_MAJOR, VERSION_MINOR, VERSION_REVISION); 920 | 921 | return str; 922 | } 923 | 924 | -------------------------------------------------------------------------------- /src/nakamoto.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define FILE_BUFFER_BLOCK (size_t)2*FILE_BUFFER_SIZE 8 | #define FILE_DESCRIPTION "Nakamoto encrypted file" 9 | #define ARGON2ID_MEM_COST (uint32_t)(32*32*1024) 10 | #define ARGON2ID_INTERACTION_COST (uint32_t)(3+60) 11 | #define ARGON2ID_PARALLEL_COST (uint32_t)(4+80) 12 | #define OUT_FILENAME "out.txt" 13 | #define OUT_FILENAME_ENCRYPTED "out.nkm" 14 | 15 | #ifdef VISIBLE_FOR_TEST 16 | inline uint32_t getArgon2idMemCost() { 17 | return ARGON2ID_MEM_COST; 18 | } 19 | 20 | inline uint32_t getArgon2idInteractionCost() { 21 | return ARGON2ID_INTERACTION_COST; 22 | } 23 | 24 | inline uint32_t getArgon2idParallelCost() { 25 | return ARGON2ID_PARALLEL_COST; 26 | } 27 | #endif 28 | 29 | _Static_assert(sizeof(((struct file_struct_t *)0)->description)>=sizeof(FILE_DESCRIPTION), "Description exceed size"); 30 | 31 | struct file_util_t *WRITE_begin_header(uint64_t timeout) { 32 | int err; 33 | uint8_t 34 | *_file_buffer; // encrypted data by key ; + encrypted data by key 1 = FILE_BUFFER_BLOCK = 2*FILE_BUFFER_SIZE 35 | char *msg; 36 | struct file_struct_t *file_struct; 37 | struct file_util_t *file_util; 38 | #ifdef DEBUG 39 | struct cipher_block_t *cipher_block; 40 | struct cipher_block_data_t *cipher_block_data; 41 | #endif 42 | 43 | N_DEBUGF("WRITE_begin_header with timeout: %lu", timeout) 44 | 45 | if (!(file_struct=(struct file_struct_t *)malloc(FILE_STRUCT_SIZE))) 46 | return NULL; 47 | 48 | N_DEBUGF("WRITE_begin_header alloc'd size %lu bytes at %p", FILE_STRUCT_SIZE, file_struct) 49 | 50 | N_DEBUG("verify_system_entropy. Verify entropy") 51 | 52 | if (verify_system_entropy(F_ENTROPY_TYPE_PARANOIC, (uint8_t *)file_struct, FILE_STRUCT_SIZE, timeout)) 53 | goto WRITE_begin_header_exit1; 54 | 55 | N_DEBUGF("Check iv1 %p of size %lu", file_struct->iv1, sizeof(file_struct->iv1)) 56 | 57 | N_DEBUG_DUMP(file_struct->iv1, sizeof(file_struct->iv1)) 58 | 59 | N_DEBUGF("Check salt1 %p of size %lu", file_struct->salt1, sizeof(file_struct->salt1)) 60 | 61 | N_DEBUG_DUMP(file_struct->salt1, sizeof(file_struct->salt1)) 62 | 63 | N_DEBUG("verify_system_entropy: Success") 64 | 65 | N_DEBUGF("_file_buffer: Alloc size %lu bytes", FILE_BUFFER_BLOCK) 66 | 67 | if (!(_file_buffer=(uint8_t *)malloc(FILE_BUFFER_BLOCK))) 68 | goto WRITE_begin_header_exit1; 69 | 70 | N_DEBUGF("_file_buffer: Alloc'd size %lu bytes at %p success", FILE_BUFFER_BLOCK, _file_buffer) 71 | 72 | N_DEBUGF("file_util: Alloc size %lu bytes", sizeof(struct file_util_t)) 73 | 74 | if ((file_util=(struct file_util_t *)malloc(sizeof(struct file_util_t)))) { 75 | 76 | N_DEBUGF("file_util: Alloc size %lu bytes %p success", sizeof(struct file_util_t), file_util) 77 | 78 | file_util->file_struct=file_struct; 79 | file_util->_file_buffer=_file_buffer; 80 | 81 | N_DEBUG("file_util->magic: set magic") 82 | 83 | memcpy(file_struct->magic, NAKAMOTO, CONST_STR_LEN(NAKAMOTO)); 84 | 85 | file_struct->version=setU32_LE(NAKAMOTO_VERSION_SET); 86 | // file_struct->iteraction=setU32_LE(INTERATION); 87 | 88 | N_DEBUGF("Version: %d", NAKAMOTO_VERSION_SET) 89 | N_DEBUGF("Interaction: %d", INTERACTION) 90 | 91 | N_DEBUG("file_util->description:" FILE_DESCRIPTION) 92 | memcpy(file_struct->description, FILE_DESCRIPTION, sizeof(FILE_DESCRIPTION)); 93 | 94 | //#define FILE_STRUCT_DATA_HASH_SZ sizeof(*file_struct)-sizeof(file_struct->sha512) 95 | if ((err=ssl_hash512(file_struct->sha512, (uint8_t *)file_struct, FILE_STRUCT_DATA_HASH_SZ, &msg))) { 96 | N_ERRORF("ssl_hash512 error with err = %d with message: %s", err, msg) 97 | goto WRITE_begin_header_exit2; 98 | } 99 | 100 | N_DEBUGF("Hash 512 success with message \"%s\" with data size %lu\nWith DUMP:", msg, FILE_STRUCT_DATA_HASH_SZ) 101 | 102 | N_DEBUG_DUMP_A(file_struct, FILE_STRUCT_DATA_HASH_SZ) 103 | 104 | N_DEBUG("With HASH:") 105 | 106 | N_DEBUG_DUMP(file_struct->sha512, sizeof(file_struct->sha512)) 107 | 108 | //#undef FILE_STRUCT_DATA_HASH_SZ 109 | #ifdef DEBUG 110 | cipher_block=(struct cipher_block_t *)&file_struct[1]; 111 | 112 | N_DEBUGF("Setup cipher_block %p with size %lu", cipher_block, sizeof(struct cipher_block_t)) 113 | #endif 114 | // cipher_block->memory_cost=setU32_LE(ARGON2ID_MEM_COST); 115 | // N_DEBUGF("Setup memory cost in Argon2id %u", ARGON2ID_MEM_COST) 116 | 117 | // cipher_block->interaction_cost=setU32_LE(ARGON2ID_INTERACTION_COST); 118 | // N_DEBUGF("Setup interaction cost in Argon2id %u", cipher_block->interaction_cost) 119 | 120 | // cipher_block->parallel_cost=setU32_LE(ARGON2ID_PARALLEL_COST); 121 | // N_DEBUGF("Setup parallel cost in Argon2id %u", cipher_block->parallel_cost) 122 | 123 | N_DEBUGF("Check additional %p of size %lu", cipher_block->additional, sizeof(cipher_block->additional)) 124 | 125 | N_DEBUG_DUMP(cipher_block->additional, sizeof(cipher_block->additional)) 126 | 127 | N_DEBUGF("Check secret %p of size %lu", cipher_block->secret, sizeof(cipher_block->secret)) 128 | 129 | N_DEBUG_DUMP(cipher_block->secret, sizeof(cipher_block->secret)) 130 | 131 | N_DEBUGF("Check iv2 %p of size %lu", cipher_block->iv2, sizeof(cipher_block->iv2)) 132 | 133 | N_DEBUG_DUMP(cipher_block->iv2, sizeof(cipher_block->iv2)) 134 | 135 | #ifdef DEBUG 136 | cipher_block_data=(struct cipher_block_data_t *)&cipher_block[1]; 137 | N_DEBUGF("Check hash_xored_restore %p of size %lu", cipher_block_data->hash_xored_restore, sizeof(cipher_block_data->hash_xored_restore)) 138 | 139 | N_DEBUG_DUMP(cipher_block_data->hash_xored_restore, sizeof(cipher_block_data->hash_xored_restore)) 140 | #endif 141 | 142 | return file_util; 143 | 144 | WRITE_begin_header_exit2: 145 | free((void *)file_util); 146 | 147 | } 148 | 149 | free((void *)_file_buffer); 150 | 151 | WRITE_begin_header_exit1: 152 | free((void *)file_struct); 153 | 154 | return NULL; 155 | } 156 | 157 | void WRITE_end_header(struct file_util_t **file_util) 158 | { 159 | uint8_t *_file_buffer; 160 | struct file_struct_t *file_struct; 161 | 162 | if (*file_util) { 163 | 164 | N_DEBUGF("WRITE_end_header: Begin free %p", *file_util) 165 | 166 | file_struct=(*file_util)->file_struct; 167 | _file_buffer=(*file_util)->_file_buffer; 168 | 169 | N_DEBUGF("WRITE_end_header: Cleaning *file_util(%p) of size %lu", *file_util, sizeof(struct file_util_t)) 170 | memset((*file_util), 0, sizeof(struct file_util_t)); 171 | N_DEBUG_DUMP((*file_util), sizeof(struct file_util_t)) 172 | 173 | N_DEBUGF("WRITE_end_header: Cleaning _file_buffer(%p) of size %lu", _file_buffer, FILE_BUFFER_BLOCK) 174 | memset(_file_buffer, 0, FILE_BUFFER_BLOCK); 175 | //N_DEBUG_DUMP(_file_buffer, FILE_BUFFER_BLOCK) 176 | 177 | N_DEBUGF("WRITE_end_header: Cleaning file_struct(%p) of size %lu", file_struct, FILE_STRUCT_SIZE) 178 | memset(file_struct, 0, FILE_STRUCT_SIZE); 179 | N_DEBUG_DUMP(file_struct, FILE_STRUCT_SIZE) 180 | 181 | if (gen_rand_no_entropy_util((uint8_t *)(*file_util), sizeof(struct file_util_t))) 182 | N_WARN("reset file_util struct") 183 | N_DEBUGF("WRITE_end_header: Randomize *file_util(%p) of size %lu", *file_util, sizeof(struct file_util_t)) 184 | N_DEBUG_DUMP(*file_util, sizeof(struct file_util_t)) 185 | 186 | if (gen_rand_no_entropy_util((uint8_t *)_file_buffer, FILE_BUFFER_BLOCK)) 187 | N_WARN("reset _file_buffer struct\n"); 188 | N_DEBUGF("WRITE_end_header: Randomize _file_buffer(%p) of size %lu", _file_buffer, FILE_BUFFER_BLOCK) 189 | 190 | if (gen_rand_no_entropy_util((uint8_t *)file_struct, FILE_STRUCT_SIZE)) 191 | N_WARN("reset file_struct struct\n"); 192 | N_DEBUGF("WRITE_end_header: Randomize file_struct(%p) of size %lu", file_struct, FILE_STRUCT_SIZE) 193 | N_DEBUG_DUMP(file_struct, FILE_STRUCT_SIZE) 194 | 195 | N_DEBUGF("WRITE_end_header: Freeing *file_util(%p)", (*file_util)) 196 | free((void *)(*file_util)); 197 | 198 | N_DEBUGF("WRITE_end_header: Freeing _file_buffer(%p)", _file_buffer) 199 | free((void *)_file_buffer); 200 | 201 | N_DEBUGF("WRITE_end_header: Freeing file_struct(%p)", file_struct) 202 | free((void *)file_struct); 203 | 204 | *file_util=NULL; 205 | } 206 | } 207 | 208 | // return 0 if sucess 209 | // !!! Assumes always: n > max >= min 210 | #ifndef VISIBLE_FOR_TEST 211 | static 212 | #endif 213 | int pass_must_have_at_least(size_t *passwd_sz, char *password, size_t n, size_t min, size_t max, int must_have) 214 | { 215 | int err; 216 | size_t i; 217 | char c; 218 | 219 | *passwd_sz=0; 220 | 221 | if (!password) 222 | return PASS_IS_NULL; 223 | 224 | if (((*passwd_sz)=strnlen(password, n))==n) 225 | return PASS_IS_OUT_OVF; 226 | 227 | if (min>(*passwd_sz)) 228 | return PASS_IS_TOO_SHORT; 229 | 230 | if ((*passwd_sz)>max) 231 | return PASS_IS_TOO_LONG; 232 | 233 | if ((err=PASS_MUST_HAVE_AT_LEAST_NONE)==must_have) 234 | return err; 235 | 236 | for (i=0;i<(*passwd_sz);i++) { 237 | if (err==must_have) 238 | break; 239 | 240 | c=password[i]; 241 | 242 | if (must_have&PASS_MUST_HAVE_AT_LEAST_ONE_LOWER_CASE) 243 | if ((err&PASS_MUST_HAVE_AT_LEAST_ONE_LOWER_CASE)==0) { 244 | if (c>'z') 245 | goto pass_must_have_at_least_STEP0; 246 | 247 | if (c<'a') 248 | goto pass_must_have_at_least_STEP0; 249 | 250 | err|=PASS_MUST_HAVE_AT_LEAST_ONE_LOWER_CASE; 251 | 252 | continue; 253 | } 254 | 255 | pass_must_have_at_least_STEP0: 256 | 257 | if (must_have&PASS_MUST_HAVE_AT_LEAST_ONE_NUMBER) 258 | if ((err&PASS_MUST_HAVE_AT_LEAST_ONE_NUMBER)==0) { 259 | if (c>'9') 260 | goto pass_must_have_at_least_STEP1; 261 | 262 | if (c<'0') 263 | goto pass_must_have_at_least_STEP1; 264 | 265 | err|=PASS_MUST_HAVE_AT_LEAST_ONE_NUMBER; 266 | 267 | continue; 268 | } 269 | 270 | pass_must_have_at_least_STEP1: 271 | 272 | if (must_have&PASS_MUST_HAVE_AT_LEAST_ONE_UPPER_CASE) 273 | if ((err&PASS_MUST_HAVE_AT_LEAST_ONE_UPPER_CASE)==0) { 274 | 275 | if (c>'Z') 276 | goto pass_must_have_at_least_STEP2; 277 | 278 | if (c<'A') 279 | goto pass_must_have_at_least_STEP2; 280 | 281 | err|=PASS_MUST_HAVE_AT_LEAST_ONE_UPPER_CASE; 282 | 283 | continue; 284 | } 285 | 286 | pass_must_have_at_least_STEP2: 287 | 288 | if (must_have&PASS_MUST_HAVE_AT_LEAST_ONE_SYMBOL) 289 | if ((err&PASS_MUST_HAVE_AT_LEAST_ONE_SYMBOL)==0) { 290 | if (c==0x7F) 291 | continue; 292 | 293 | if (c<'!') 294 | continue; 295 | 296 | if (c>'z') 297 | goto pass_must_have_at_least_EXIT1; 298 | 299 | if (c>'`') 300 | continue; 301 | 302 | if (c>'Z') 303 | goto pass_must_have_at_least_EXIT1; 304 | 305 | if (c>'@') 306 | continue; 307 | 308 | if (c>'9') 309 | goto pass_must_have_at_least_EXIT1; 310 | 311 | if (c>'/') 312 | continue; 313 | 314 | pass_must_have_at_least_EXIT1: 315 | err|=PASS_MUST_HAVE_AT_LEAST_ONE_SYMBOL; 316 | } 317 | 318 | } 319 | 320 | return (err^must_have); 321 | } 322 | 323 | int WRITE_derive_keys(struct file_util_t *file_util, const char *password, const char *filename) 324 | { 325 | int err; 326 | char *errMsg; 327 | size_t password_len; 328 | uint8_t *data, *p, *q; 329 | size_t data_size, data_size_align, size_tmp; 330 | struct cipher_block_t *cipher_block; 331 | struct cipher_block_data_t *cipher_block_data; 332 | 333 | if ((err=pass_must_have_at_least(&password_len, (char *)password, N_MAX, MIN_PASSWD, MAX_PASSWD, MUST_HAVE))) { 334 | if (err&PASS_IS_NULL) 335 | errMsg="Password is NULL\n"; 336 | else if (err&PASS_IS_OUT_OVF) 337 | errMsg="Password buffer overflow\n"; 338 | else if (err&PASS_IS_TOO_SHORT) 339 | errMsg="Password is too short\n"; 340 | else if (err&PASS_IS_TOO_LONG) 341 | errMsg="Password is too long\n"; 342 | else 343 | errMsg="Password must have:\n\tMin. size: 16\n\tMax. size: 64\n\tAt least one symbol\n\tAt least one upper case\n\t\ 344 | At least one number\n\tAt least one lower case\n\t"; 345 | N_ERRORF("Err.: %d\n%s", err, errMsg) 346 | 347 | return err; 348 | } 349 | 350 | N_DEBUGF("Password pass %s with size %lu", password, password_len) 351 | 352 | N_DEBUGF("Begin PBKDF2 with iteration %u", INTERACTION) 353 | 354 | if ((err=pbkdf2( 355 | file_util->priv_key1, sizeof(file_util->priv_key1), 356 | (const char *)password, (int)password_len, 357 | file_util->file_struct->salt1, sizeof(file_util->file_struct->salt1), 358 | INTERACTION, 359 | &errMsg 360 | ))) { 361 | N_ERRORF("Error PBKDF2 %d with message %s\n", err, errMsg) 362 | return err; 363 | } 364 | 365 | N_DEBUGF("End PBKDF2 with out %p of size %lu with err = %d and message %s", file_util->priv_key1, sizeof(file_util->priv_key1), err, errMsg); 366 | N_DEBUG_DUMP(file_util->priv_key1, sizeof(file_util->priv_key1)) 367 | 368 | if ((err=readFileAlign(&data, &data_size, &data_size_align, filename))) { 369 | N_ERRORF("Could not open filename %s. Error: %d\n", filename, err) 370 | return err; 371 | } 372 | 373 | N_DEBUGF("Read file %s with size %lu, aligned %lu with pointer %p", filename, data_size, data_size_align, data) 374 | N_DEBUG_DUMP_A(data, data_size_align) 375 | 376 | cipher_block=(struct cipher_block_t *)&file_util->file_struct[1]; 377 | 378 | N_DEBUGF("BEFORE: Cipher block %p with salt2 = %p of size %lu", cipher_block, cipher_block->hash_xored, sizeof(cipher_block->hash_xored)) 379 | N_DEBUG_DUMP(cipher_block->hash_xored, sizeof(cipher_block->hash_xored)) 380 | 381 | if ((err=ssl_hash512(cipher_block->hash_xored, data, data_size, &errMsg))) { 382 | N_ERRORF("SHA 512 error with message: \"%s\" with err %d\n", errMsg, err) 383 | goto WRITE_derive_keys_exit1; 384 | } 385 | 386 | N_DEBUGF("AFTER (SHA512 step 1): Cipher block %p with salt2 = %p of size %lu", cipher_block, cipher_block->hash_xored, sizeof(cipher_block->hash_xored)) 387 | N_DEBUG_DUMP(cipher_block->hash_xored, sizeof(cipher_block->hash_xored)) 388 | 389 | cipher_block_data=(struct cipher_block_data_t *)&cipher_block[1]; 390 | 391 | cipher_block_data->file_size=setU64_LE(data_size); 392 | 393 | N_DEBUGF("Data size (file): %lu", cipher_block_data->file_size) 394 | N_DEBUG_DUMP(&cipher_block_data->file_size, sizeof(cipher_block_data->file_size)) 395 | 396 | N_DEBUGF( 397 | "Cipher block data %p with hash_xored_restore = %p of size %lu", 398 | cipher_block_data, 399 | cipher_block_data->hash_xored_restore, 400 | sizeof(cipher_block_data->hash_xored_restore) 401 | ) 402 | N_DEBUG_DUMP(cipher_block_data->hash_xored_restore, sizeof(cipher_block_data->hash_xored_restore)) 403 | 404 | _Static_assert(sizeof(cipher_block->hash_xored)==sizeof(cipher_block_data->hash_xored_restore), "Refactor XOR vector size"); 405 | xor_n(cipher_block->hash_xored, cipher_block_data->hash_xored_restore, sizeof(cipher_block->hash_xored)); 406 | 407 | N_DEBUGF("AFTER (SHA512 step 2): Cipher block %p with salt2 = %p of size %lu", cipher_block, cipher_block->hash_xored, sizeof(cipher_block->hash_xored)) 408 | N_DEBUG_DUMP(cipher_block->hash_xored, sizeof(cipher_block->hash_xored)) 409 | 410 | N_DEBUGF( 411 | "\n\tMem. cost: %u\n\tIter. count: %u\n\tParallel cost: %u\n", 412 | ARGON2ID_MEM_COST, 413 | ARGON2ID_INTERACTION_COST, 414 | ARGON2ID_PARALLEL_COST 415 | ) 416 | 417 | if ((err=argon2id( 418 | file_util->priv_key2, sizeof(file_util->priv_key2), 419 | (const char *)password, (int)password_len, 420 | cipher_block->hash_xored, sizeof(cipher_block->hash_xored), 421 | cipher_block->additional, sizeof(cipher_block->additional), 422 | cipher_block->secret, sizeof(cipher_block->secret), 423 | ARGON2ID_MEM_COST, 424 | ARGON2ID_INTERACTION_COST, 425 | ARGON2ID_PARALLEL_COST, 426 | &errMsg 427 | ))) { 428 | N_ERRORF("Error Argon2id %d with message %s\n", err, errMsg) 429 | goto WRITE_derive_keys_exit1; 430 | } 431 | 432 | N_DEBUGF("End Argon2id with out %p of size %lu with err = %d and message %s", file_util->priv_key2, sizeof(file_util->priv_key2), err, errMsg); 433 | N_DEBUG_DUMP(file_util->priv_key2, sizeof(file_util->priv_key2)) 434 | 435 | N_DEBUGF( 436 | "Copy file struct from file_util->file_struct %p to file_util->_file_buffer %p of size %lu", 437 | file_util->file_struct, file_util->_file_buffer, FILE_STRUCT_SIZE 438 | ) 439 | memcpy((void *)file_util->_file_buffer, (void *)file_util->file_struct, FILE_STRUCT_SIZE); 440 | N_DEBUG_COMP_VEC(file_util->_file_buffer, FILE_STRUCT_SIZE, file_util->file_struct, FILE_STRUCT_SIZE) 441 | 442 | p=&file_util->_file_buffer[FILE_STRUCT_SIZE]; 443 | 444 | N_DEBUGF("Copy data file from data %p to &file_util->_file_buffer[FILE_STRUCT_SIZE] %p of size %lu", data, p, data_size_align) 445 | 446 | memcpy((void *)p, (void *)data, data_size_align); 447 | N_DEBUG_DUMP_A(file_util->_file_buffer, FILE_STRUCT_SIZE+data_size_align) 448 | 449 | N_DEBUG("Begin encrypt (layer 2) with private key 2"); 450 | 451 | //p=(uint8_t *)&((struct cipher_block_t *)(&((struct file_struct_t *)file_util->_file_buffer)[1]))[1]; 452 | p=&file_util->_file_buffer[FILE_STRUCT_SIZE-sizeof(struct cipher_block_data_t)]; 453 | size_tmp=FILE_BUFFER_SIZE; 454 | 455 | q=&file_util->_file_buffer[FILE_BUFFER_SIZE]; 456 | 457 | if ((err=aes_256_cbc_encrypt( 458 | q, &size_tmp, 459 | p, sizeof(struct cipher_block_data_t)+data_size_align, 460 | cipher_block->iv2, file_util->priv_key2, 461 | &errMsg 462 | ))) { 463 | N_ERRORF("aes_256_cbc_encrypt(key 2) error with message \"%s\" with err %d", errMsg, err) 464 | goto WRITE_derive_keys_exit1; 465 | } 466 | 467 | #ifdef DEBUG 468 | if (size_tmp==(sizeof(struct cipher_block_data_t)+data_size_align)) 469 | N_INFO("Pass size_tmp") 470 | else { 471 | N_ERRORF("size_tmp = %lu differs from correct size %lu", size_tmp, sizeof(struct cipher_block_data_t)+data_size_align) 472 | goto WRITE_derive_keys_exit1; 473 | } 474 | #endif 475 | 476 | N_DEBUGF("Block encrypted at %p (key 2) with private key 2 %p", q, file_util->priv_key2) 477 | N_DEBUGF("Private KEY 2 %p", file_util->priv_key2) 478 | N_DEBUG_DUMP(file_util->priv_key2, sizeof(file_util->priv_key2)) 479 | N_DEBUGF("IV 2 %p", cipher_block->iv2) 480 | N_DEBUG_DUMP(cipher_block->iv2, sizeof(cipher_block->iv2)) 481 | 482 | N_DEBUGF("\nEncrypted data 1 %p of size %lu", q, size_tmp) 483 | N_DEBUG_DUMP(q, size_tmp) 484 | 485 | N_DEBUGF("Copy back 1 from q(%p) to p(%p) of size %lu", q, p, size_tmp) 486 | memcpy((void *)p, (void *)q, size_tmp); 487 | N_DEBUG_COMP_VEC(p, size_tmp, q, size_tmp) 488 | 489 | size_tmp=FILE_BUFFER_SIZE; 490 | 491 | p=&file_util->_file_buffer[FILE_STRUCT_SIZE-(sizeof(struct cipher_block_t) + sizeof(struct cipher_block_data_t))]; 492 | 493 | N_DEBUG("Begin encrypt (layer 1) with private key 1"); 494 | N_DEBUGF("Private KEY 1 %p", file_util->priv_key1) 495 | N_DEBUG_DUMP(file_util->priv_key1, sizeof(file_util->priv_key1)) 496 | N_DEBUGF("IV 1 %p", file_util->file_struct->iv1) 497 | N_DEBUG_DUMP(file_util->file_struct->iv1, sizeof(file_util->file_struct->iv1)) 498 | 499 | if ((err=aes_256_cbc_encrypt( 500 | q, &size_tmp, 501 | p, sizeof(struct cipher_block_t) + sizeof(struct cipher_block_data_t) + data_size_align, 502 | file_util->file_struct->iv1, file_util->priv_key1, 503 | &errMsg 504 | ))) { 505 | N_ERRORF("aes_256_cbc_encrypt(key 1) error with message \"%s\" with err %d", errMsg, err) 506 | goto WRITE_derive_keys_exit1; 507 | } 508 | 509 | #ifdef DEBUG 510 | if (size_tmp==(sizeof(struct cipher_block_t) + sizeof(struct cipher_block_data_t) + data_size_align)) 511 | N_INFO("Pass size_tmp") 512 | else { 513 | N_ERRORF("size_tmp = %lu differs from correct size %lu", size_tmp, sizeof(struct cipher_block_t) + sizeof(struct cipher_block_data_t) + data_size_align) 514 | goto WRITE_derive_keys_exit1; 515 | } 516 | #endif 517 | 518 | N_DEBUGF("\nEncrypted data 2 %p of size %lu", q, size_tmp) 519 | N_DEBUG_DUMP(q, size_tmp) 520 | 521 | N_DEBUGF("Copy back 2 from q(%p) to p(%p) of size %lu", q, p, size_tmp) 522 | memcpy((void *)p, (void *)q, size_tmp); 523 | N_DEBUG_COMP_VEC(p, size_tmp, q, size_tmp) 524 | 525 | if ((err=writeFileUtil(OUT_FILENAME_ENCRYPTED, file_util->_file_buffer, sizeof(struct file_struct_t) + size_tmp))) 526 | N_ERRORF("Could not write to file %d", err) 527 | 528 | WRITE_derive_keys_exit1: 529 | readFileAlignFree(&data, data_size_align); 530 | 531 | return err; 532 | } 533 | 534 | struct file_util_t *READ_begin_header() 535 | { 536 | struct file_util_t *file_util=(struct file_util_t *)malloc(sizeof(struct file_util_t)); 537 | 538 | if (file_util) 539 | if (!(file_util->_file_buffer=(uint8_t *)malloc(FILE_BUFFER_BLOCK))) { 540 | free((void *)file_util); 541 | file_util=NULL; 542 | } 543 | 544 | N_DEBUGF("READ begin header %p", file_util) 545 | 546 | return file_util; 547 | } 548 | 549 | int READ_extract(struct file_util_t *file_util, const char *password, const char *filename) 550 | { 551 | int err; 552 | uint8_t *data, *p; 553 | uint32_t u32; 554 | size_t 555 | data_size, 556 | data_size_align; 557 | char *msg; 558 | struct cipher_block_t *cipher_block; 559 | struct cipher_block_data_t *cipher_block_data; 560 | 561 | if ((err=readFileAlignDecrypt(&data, &data_size, &data_size_align, filename))) { 562 | N_ERRORF("Could not open file %s. Error %d", filename, err) 563 | return err; 564 | } 565 | 566 | N_DEBUGF("READ. File %s opened %p with size %lu aligned %lu", filename, data, data_size, data_size_align) 567 | N_DEBUG_DUMP_A(data, data_size_align) 568 | 569 | if (data_size_align!=data_size) { 570 | N_ERRORF("Not aligned data size: %lu data size align %lu", data_size, data_size_align) 571 | err=80; 572 | goto READ_extract_exit1; 573 | } 574 | 575 | if (FILE_STRUCT_SIZE>=data_size_align) { 576 | N_ERRORF("Wrong file structure in %s", filename) 577 | err=81; 578 | goto READ_extract_exit1; 579 | } 580 | 581 | if (memcmp((file_util->file_struct=(struct file_struct_t *)data)->magic, NAKAMOTO, CONST_STR_LEN(NAKAMOTO))) { 582 | N_ERRORF("Wrong magic in %s", filename) 583 | err=82; 584 | goto READ_extract_exit1; 585 | } 586 | 587 | u32=setU32_LE(file_util->file_struct->version); 588 | 589 | N_DEBUGF("Check version in %s\n\tVersion: %d.%d.%d", filename, GET_VER_MAJ(u32), GET_VER_MIN(u32), GET_VER_REV(u32)) 590 | 591 | if ((err=((GET_VER_MAJ(u32)!=VERSION_MAJOR)||(GET_VER_MIN(u32)!=VERSION_MINOR)))) { 592 | err=83; 593 | N_ERRORF( 594 | "Unexpected version in %s. Expected %d.%d. But found %d.%d", 595 | filename, 596 | VERSION_MAJOR, VERSION_MINOR, 597 | GET_VER_MAJ(u32), GET_VER_MIN(u32) 598 | ) 599 | goto READ_extract_exit1; 600 | } 601 | 602 | if (!check_hash512(file_util->file_struct->sha512, (uint8_t *)file_util->file_struct, FILE_STRUCT_DATA_HASH_SZ, &msg)) { 603 | err=84; 604 | N_ERRORF("Wrong checksum in %s with message %s", filename, msg) 605 | goto READ_extract_exit1; 606 | } 607 | 608 | N_DEBUGF("Checksum in filename %s pass with message %s", filename, msg) 609 | 610 | N_DEBUG("Extracting private key") 611 | 612 | if ((err=pbkdf2( 613 | file_util->priv_key1, sizeof(file_util->priv_key1), 614 | password, -1, 615 | file_util->file_struct->salt1, sizeof(file_util->file_struct->salt1), 616 | INTERACTION, 617 | &msg 618 | ))) { 619 | N_ERRORF("Error extracting private key 1. %s. Err.: %d", msg, err) 620 | goto READ_extract_exit1; 621 | } 622 | 623 | N_DEBUGF("Extracted private key 1 at %p with size %lu", file_util->priv_key1, sizeof(file_util->priv_key1)) 624 | N_DEBUG_DUMP(file_util->priv_key1, sizeof(file_util->priv_key1)) 625 | 626 | p=(uint8_t *)&file_util->file_struct[1]; 627 | 628 | N_DEBUGF("Begin decrypt data (1st step) from %p to %p of size %lu", p, file_util->_file_buffer, data_size_align-sizeof(struct file_struct_t)) 629 | 630 | data_size=FILE_BUFFER_SIZE; 631 | if ((err=aes_256_cbc_decrypt( 632 | file_util->_file_buffer, &data_size, 633 | p, data_size_align-sizeof(struct file_struct_t), 634 | file_util->file_struct->iv1, file_util->priv_key1, 635 | &msg 636 | ))) { 637 | N_ERRORF("Error decrypt data %s. Err %d", msg, err) 638 | goto READ_extract_exit1; 639 | } 640 | 641 | N_DEBUGF("Cleaning private key 1 (BEFORE) at %p with size %lu", file_util->priv_key1, sizeof(file_util->priv_key1)) 642 | N_DEBUG_DUMP(file_util->priv_key1, sizeof(file_util->priv_key1)) 643 | memset(file_util->priv_key1, 0, sizeof(file_util->priv_key1)); 644 | N_DEBUGF("Cleaning private key 1 (AFTER) at %p with size %lu", file_util->priv_key1, sizeof(file_util->priv_key1)) 645 | N_DEBUG_DUMP(file_util->priv_key1, sizeof(file_util->priv_key1)) 646 | 647 | if ((data_size_align-data_size)!=sizeof(struct file_struct_t)) { 648 | err=85; 649 | N_ERROR("Decryption error. Fatal. 85") 650 | goto READ_extract_exit1; 651 | } 652 | 653 | cipher_block=(struct cipher_block_t *)file_util->_file_buffer; 654 | 655 | N_DEBUGF("Begin extract private key2 %p with size %lu", file_util->priv_key2, sizeof(file_util->priv_key2)) 656 | 657 | if ((err=argon2id( 658 | file_util->priv_key2, sizeof(file_util->priv_key2), 659 | password, -1, 660 | cipher_block->hash_xored, sizeof(cipher_block->hash_xored), 661 | cipher_block->additional, sizeof(cipher_block->additional), 662 | cipher_block->secret, sizeof(cipher_block->secret), 663 | ARGON2ID_MEM_COST, 664 | ARGON2ID_INTERACTION_COST, 665 | ARGON2ID_PARALLEL_COST, 666 | &msg 667 | ))) { 668 | N_ERRORF("Error extracting private key 2. %s. Err.: %d", msg, err) 669 | goto READ_extract_exit1; 670 | } 671 | 672 | N_DEBUGF("Extracted private key 2 at %p with size %lu", file_util->priv_key2, sizeof(file_util->priv_key2)) 673 | N_DEBUG_DUMP(file_util->priv_key2, sizeof(file_util->priv_key2)) 674 | 675 | p=(uint8_t *)&cipher_block[1]; 676 | 677 | N_DEBUGF("Begin decrypt data (2st step) from %p to %p of size %lu", p, &file_util->_file_buffer[FILE_BUFFER_SIZE], 678 | data_size_align-sizeof(struct file_struct_t)-sizeof(struct cipher_block_t)) 679 | 680 | cipher_block_data=(struct cipher_block_data_t *)&file_util->_file_buffer[FILE_BUFFER_SIZE]; 681 | data_size=FILE_BUFFER_SIZE; 682 | if ((err=aes_256_cbc_decrypt( 683 | (uint8_t *)cipher_block_data, &data_size, 684 | p, data_size_align-sizeof(struct file_struct_t)-sizeof(struct cipher_block_t), 685 | cipher_block->iv2, file_util->priv_key2, 686 | &msg 687 | ))) { 688 | N_ERRORF("Error decrypt data(2) %s. Err %d", msg, err) 689 | goto READ_extract_exit1; 690 | } 691 | 692 | N_DEBUGF("Cleaning private key 2 (BEFORE) at %p with size %lu", file_util->priv_key2, sizeof(file_util->priv_key2)) 693 | N_DEBUG_DUMP(file_util->priv_key2, sizeof(file_util->priv_key2)) 694 | memset(file_util->priv_key2, 0, sizeof(file_util->priv_key2)); 695 | N_DEBUGF("Cleaning private key 2 (AFTER) at %p with size %lu", file_util->priv_key2, sizeof(file_util->priv_key2)) 696 | N_DEBUG_DUMP(file_util->priv_key2, sizeof(file_util->priv_key2)) 697 | 698 | if ((data_size_align-data_size)!=(sizeof(struct file_struct_t)+sizeof(struct cipher_block_t))) { 699 | err=86; 700 | N_ERROR("Decryption error. Fatal. 86") 701 | goto READ_extract_exit1; 702 | } 703 | 704 | data_size=data_size_align-FILE_STRUCT_SIZE; 705 | N_DEBUGF("\n\tData size (align): %lu\n\tFile struct Size %lu\n\tData size %lu\n", data_size_align, FILE_STRUCT_SIZE, data_size) 706 | if ((cipher_block_data->file_size>(uint64_t)data_size)||(cipher_block_data->file_size==0)) 707 | N_WARNF("Wrong block file size %lu realign to %lu. Maybe wrong password of file corrupted", cipher_block_data->file_size, data_size) 708 | else 709 | data_size=(size_t)cipher_block_data->file_size; 710 | 711 | p=(uint8_t *)&cipher_block_data[1]; 712 | 713 | N_DEBUGF("Revert checksum (BEFORE) at %p with size %lu", cipher_block->hash_xored, sizeof(cipher_block->hash_xored)) 714 | N_DEBUG_DUMP(cipher_block->hash_xored, sizeof(cipher_block->hash_xored)) 715 | 716 | xor_n(cipher_block->hash_xored, cipher_block_data->hash_xored_restore, sizeof(cipher_block->hash_xored)); 717 | 718 | N_DEBUGF("Revert checksum (AFTER) at %p with size %lu", cipher_block->hash_xored, sizeof(cipher_block->hash_xored)) 719 | N_DEBUG_DUMP(cipher_block->hash_xored, sizeof(cipher_block->hash_xored)) 720 | 721 | if (!check_hash512(cipher_block->hash_xored, p, data_size, &msg)) { 722 | err=87; 723 | N_ERRORF("Wrong password or corrupted file with: %s ", msg) 724 | goto READ_extract_exit1; 725 | } 726 | 727 | if ((err=writeToFile(OUT_FILENAME, p, data_size))) 728 | N_ERRORF("Write to file error %d. Could not write \""OUT_FILENAME"\"\n", err) 729 | else 730 | N_INFO("Success. \""OUT_FILENAME"\"\n") 731 | 732 | READ_extract_exit1: 733 | readFileAlignFree(&data, data_size_align); 734 | 735 | return err; 736 | } 737 | 738 | void READ_end_header(struct file_util_t **file_util) 739 | { 740 | uint8_t *_file_buffer; 741 | 742 | if (*file_util) { 743 | 744 | _file_buffer=(*file_util)->_file_buffer; 745 | 746 | N_DEBUGF("READ_end_header: Begin free %p", *file_util) 747 | 748 | N_DEBUGF("READ_end_header: Cleaning *file_util(%p) of size %lu", *file_util, sizeof(struct file_util_t)) 749 | memset((*file_util), 0, sizeof(struct file_util_t)); 750 | N_DEBUG_DUMP((*file_util), sizeof(struct file_util_t)) 751 | 752 | N_DEBUGF("READ_end_header: Cleaning _file_buffer(%p) of size %lu", _file_buffer, FILE_BUFFER_BLOCK) 753 | memset(_file_buffer, 0, FILE_BUFFER_BLOCK); 754 | 755 | if (gen_rand_no_entropy_util((uint8_t *)(*file_util), sizeof(struct file_util_t))) 756 | N_WARN("READ reset file_util struct") 757 | N_DEBUGF("READ_end_header: Randomize *file_util(%p) of size %lu", *file_util, sizeof(struct file_util_t)) 758 | N_DEBUG_DUMP(*file_util, sizeof(struct file_util_t)) 759 | 760 | if (gen_rand_no_entropy_util((uint8_t *)_file_buffer, FILE_BUFFER_BLOCK)) 761 | N_WARN("READ reset _file_buffer struct\n"); 762 | N_DEBUGF("READ_end_header: Randomize _file_buffer(%p) of size %lu", _file_buffer, FILE_BUFFER_BLOCK) 763 | 764 | N_DEBUGF("READ_end_header: Freeing *file_util(%p)", (*file_util)) 765 | free((void *)(*file_util)); 766 | 767 | N_DEBUGF("READ_end_header: Freeing _file_buffer(%p)", _file_buffer) 768 | free((void *)_file_buffer); 769 | 770 | *file_util=NULL; 771 | } 772 | } 773 | 774 | -------------------------------------------------------------------------------- /include/test/asserts.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | /** 9 | * @file 10 | * @brief A simplest lightweight compatible C test suite for low level C and C++ applications 11 | * @mainpage Overview 12 | * 13 | * ## Usage 14 | * 15 | * Just add _asserts.h_ and _asserts.c_ source code into your project 16 | * 17 | * ```sh 18 | * make 19 | * ``` 20 | * 21 | * to test [autoexplaining example](https://github.com/devfabiosilva/ctest/tree/master/example) 22 | * 23 | * To clean test: 24 | * ```sh 25 | * make clean 26 | * ``` 27 | * 28 | * ## Projects using _ctest_ 29 | * 30 | * - [myNanoEmbedded](https://github.com/devfabiosilva/myNanoEmbedded) 31 | * - [JNI tutorial](https://github.com/devfabiosilva/jni_tutorial) 32 | * 33 | * ## Credits 34 | * 35 | * @author Fábio Pereira da Silva 36 | * @date Jun 10 2021 37 | * @version 0.0.1 38 | * @copyright License MIT see here 39 | * 40 | * ## Contact 41 | * 42 | * mailto:fabioegel@gmail.com 43 | */ 44 | 45 | /** 46 | * @def C_TEST_TRUE 47 | * _TRUE_ value for CTEST 48 | */ 49 | #define C_TEST_TRUE (int)(1==1) 50 | 51 | /** 52 | * @def C_TEST_FALSE 53 | * _FALSE_ value for CTEST 54 | */ 55 | #define C_TEST_FALSE (int)(1!=1) 56 | 57 | #ifndef CTEST_DOC_SKIP 58 | void _c_test_ignore(); 59 | void _c_test_ignore_end(); 60 | #endif 61 | 62 | /** 63 | * @def C_TEST_BEGIN_IGNORE 64 | * Begin ignoring tests 65 | */ 66 | #define C_TEST_BEGIN_IGNORE _c_test_ignore(); 67 | 68 | /** 69 | * @def C_TEST_END_IGNORE 70 | * End ignoring tests 71 | */ 72 | #define C_TEST_END_IGNORE _c_test_ignore_end(); 73 | 74 | #ifndef CTEST_DOC_SKIP 75 | #define END_TITLE "\e[0m" 76 | #define INITIAL_TITLE "\e[1;3m" 77 | #define ERROR_CODE "\e[31;1m" 78 | #define SUCCESS_CODE "\e[32;1m" 79 | #define WARNING_CODE "\e[33;1m" 80 | #define INFO_CODE "\e[34;1m" 81 | 82 | void write_title(const char *, const char *); 83 | void write_title_fmt(const char *, const char *, ...); 84 | #endif 85 | 86 | /** 87 | * @def TITLE_MSG 88 | * Add title text message 89 | * 90 | * ## Example: 91 | * 92 | * ```c 93 | * ... 94 | * TITLE_MSG("This is a text title message") 95 | * ... 96 | * ``` 97 | * 98 | * @see TITLE_MSG_FMT 99 | */ 100 | #define TITLE_MSG(msg) write_title(msg, INITIAL_TITLE); 101 | 102 | /** 103 | * @def ERROR_MSG 104 | * Add error text message 105 | * 106 | * ## Example: 107 | * 108 | * ```c 109 | * ... 110 | * ERROR_MSG("This is a text error message") 111 | * ... 112 | * ``` 113 | * 114 | * @see ERROR_MSG_FMT 115 | */ 116 | #define ERROR_MSG(msg) write_title(msg, ERROR_CODE); 117 | 118 | /** 119 | * @def SUCCESS_MSG 120 | * Add success text message 121 | * 122 | * ## Example: 123 | * 124 | * ```c 125 | * ... 126 | * SUCCESS_MSG("This is a text success message") 127 | * ... 128 | * ``` 129 | * 130 | * @see SUCCESS_MSG_FMT 131 | */ 132 | #define SUCCESS_MSG(msg) write_title(msg, SUCCESS_CODE); 133 | 134 | /** 135 | * @def WARN_MSG 136 | * Add warning text message 137 | * 138 | * ## Example: 139 | * 140 | * ```c 141 | * ... 142 | * WARN_MSG("This is a text warning message") 143 | * ... 144 | * ``` 145 | * 146 | * @see WARN_MSG_FMT 147 | */ 148 | #define WARN_MSG(msg) write_title(msg, WARNING_CODE); 149 | 150 | /** 151 | * @def INFO_MSG 152 | * Add info text message 153 | * 154 | * ## Example: 155 | * 156 | * ```c 157 | * ... 158 | * WARN_MSG("This is a text info message") 159 | * ... 160 | * ``` 161 | * 162 | * @see INFO_MSG_FMT 163 | */ 164 | #define INFO_MSG(msg) write_title(msg, INFO_CODE); 165 | 166 | /** 167 | * @def TITLE_MSG_FMT 168 | * Add title text message with formatted string 169 | * 170 | * ## Example: 171 | * 172 | * ```c 173 | * 174 | * int a=5, b=10; 175 | * 176 | * ... 177 | * TITLE_MSG_FMT("This is a %s and a + b = %d", "text", a+b) 178 | * ... 179 | * ``` 180 | * 181 | * @see TITLE_MSG 182 | */ 183 | #define TITLE_MSG_FMT(...) write_title_fmt(INITIAL_TITLE, __VA_ARGS__); 184 | 185 | /** 186 | * @def ERROR_MSG_FMT 187 | * Add error text message with formatted string 188 | * 189 | * ## Example: 190 | * 191 | * ```c 192 | * 193 | * int a=5, b=10; 194 | * 195 | * ... 196 | * ERROR_MSG_FMT("This is a %s and a + b = %d", "text", a+b) 197 | * ... 198 | * ``` 199 | * 200 | * @see ERROR_MSG 201 | */ 202 | #define ERROR_MSG_FMT(...) write_title_fmt(ERROR_CODE, __VA_ARGS__); 203 | 204 | /** 205 | * @def WARN_MSG_FMT 206 | * Add warning text message with formatted string 207 | * 208 | * ## Example: 209 | * 210 | * ```c 211 | * 212 | * int a=5, b=10; 213 | * 214 | * ... 215 | * WARN_MSG_FMT("This is a %s and a + b = %d", "text", a+b) 216 | * ... 217 | * ``` 218 | * 219 | * @see WARN_MSG 220 | */ 221 | #define WARN_MSG_FMT(...) write_title_fmt(WARNING_CODE, __VA_ARGS__); 222 | 223 | /** 224 | * @def INFO_MSG_FMT 225 | * Add info text message with formatted string 226 | * 227 | * ## Example: 228 | * 229 | * ```c 230 | * 231 | * int a=5, b=10; 232 | * 233 | * ... 234 | * INFO_MSG_FMT("This is a %s and a + b = %d", "text", a+b) 235 | * ... 236 | * ``` 237 | * 238 | * @see INFO_MSG 239 | */ 240 | #define INFO_MSG_FMT(...) write_title_fmt(INFO_CODE, __VA_ARGS__); 241 | 242 | /** 243 | * @def SUCCESS_MSG_FMT 244 | * Add success text message with formatted string 245 | * 246 | * ## Example: 247 | * 248 | * ```c 249 | * 250 | * int a=5, b=10; 251 | * 252 | * ... 253 | * SUCCESS_MSG_FMT("This is a %s and a + b = %d", "text", a+b) 254 | * ... 255 | * ``` 256 | * 257 | * @see SUCCESS_MSG 258 | */ 259 | #define SUCCESS_MSG_FMT(...) write_title_fmt(SUCCESS_CODE, __VA_ARGS__); 260 | 261 | #ifndef CTEST_DOC_SKIP 262 | uint64_t *get_va_end_signature(); 263 | uint64_t *get_vas_end_signature(); 264 | 265 | #define VA_END_SIGNATURE get_va_end_signature() 266 | #define VAS_END_SIGNATURE get_vas_end_signature() 267 | 268 | typedef void (*header_on_cb)(void *); 269 | typedef void (*cb_fn)(void *); 270 | 271 | void *set_varg_callback(uint32_t, cb_fn, ...); 272 | 273 | void assert_true(int, ...); 274 | void assert_false(int, ...); 275 | void assert_equal_int(int, int, ...); 276 | void assert_not_equal_int(int, int, ...); 277 | void assert_equal_longint(long long int, long long int, ...); 278 | void assert_not_equal_longint(long long int, long long int, ...); 279 | void assert_equal_unsigned_longint(unsigned long long int, unsigned long long int, ...); 280 | void assert_not_equal_unsigned_longint(unsigned long long int, unsigned long long int, ...); 281 | void assert_equal_double(double, double, double, ...); 282 | void assert_not_equal_double(double, double, double, ...); 283 | void assert_equal_byte( 284 | void *, 285 | void *, 286 | size_t, 287 | ... 288 | ); 289 | void assert_not_equal_byte( 290 | void *, 291 | void *, 292 | size_t, 293 | ... 294 | ); 295 | void assert_equal_string(const char *, const char *, ...); 296 | void assert_not_equal_string(const char *, const char *, ...); 297 | void assert_equal_string_ignore_case(const char *, const char *, ...); 298 | void assert_not_equal_string_ignore_case(const char *, const char *, ...); 299 | void assert_null(void *, ...); 300 | void assert_not_null(void *, ...); 301 | void assert_equal_u8(uint8_t, uint8_t, ...); 302 | void assert_not_equal_u8(uint8_t, uint8_t, ...); 303 | void assert_equal_s8(int8_t, int8_t, ...); 304 | void assert_not_equal_s8(int8_t, int8_t, ...); 305 | void assert_equal_u16(uint16_t, uint16_t, ...); 306 | void assert_not_equal_u16(uint16_t, uint16_t, ...); 307 | void assert_equal_s16(int16_t, int16_t, ...); 308 | void assert_not_equal_s16(int16_t, int16_t, ...); 309 | void assert_equal_u32(uint32_t, uint32_t, ...); 310 | void assert_not_equal_u32(uint32_t, uint32_t, ...); 311 | void assert_equal_s32(int32_t, int32_t, ...); 312 | void assert_not_equal_s32(int32_t, int32_t, ...); 313 | void assert_equal_u64(uint64_t, uint64_t, ...); 314 | void assert_not_equal_u64(uint64_t, uint64_t, ...); 315 | void assert_equal_s64(int64_t, int64_t, ...); 316 | void assert_not_equal_s64(int64_t, int64_t, ...); 317 | void assert_fail(void *, ...); 318 | #endif 319 | 320 | /** 321 | * @fn void on_add_test(header_on_cb callback) 322 | * @brief Call function _callback_ on adding test event 323 | * 324 | * - Example: 325 | * 326 | * ```c 327 | * #include 328 | * #include 329 | * #include 330 | * 331 | * void on_add_test_fn(void *context) 332 | * { 333 | * // context is not used. 334 | * printf("\nOn add test event ...\n\n"); 335 | * } 336 | * 337 | * int main(int argc, char **argv) 338 | * { 339 | * uint8_t vec1[] = "This is a simple text"; 340 | * uint8_t *vec2 = malloc(sizeof(vec1)); 341 | * 342 | * memcpy(vec2, vec1, sizeof(vec1)); 343 | * on_add_test(on_add_test_fn); // It will be called every add test event 344 | * 345 | * C_ASSERT_NOT_EQUAL_BYTE(vec1, vec2, sizeof(vec1), 346 | * CTEST_SETTER( 347 | * CTEST_INFO("Testing if \"vec1\" (%p) is different from \"vec2\" (%p) of size %u", vec1, vec2, sizeof(vec1)), 348 | * CTEST_WARN("Warning: This should be different") 349 | * ) 350 | * ) 351 | * 352 | * end_tests(); 353 | * 354 | * return 0; 355 | * } 356 | * ``` 357 | * @see rm_on_add_test 358 | */ 359 | void on_add_test(header_on_cb); 360 | 361 | /** 362 | * @fn void rm_on_add_test() 363 | * @brief Removes callback pointer from global _rm_on_add_test_ context 364 | * 365 | * @see on_add_test 366 | */ 367 | void rm_on_add_test(); 368 | 369 | /** 370 | * @fn void on_begin_test(header_on_cb callback) 371 | * @brief Call function _callback_ on each beginning test event 372 | * 373 | * - Example: 374 | * 375 | * ```c 376 | * #include 377 | * #include 378 | * #include 379 | * 380 | * void on_begin_test_fn(void *context) 381 | * { 382 | * // context is not used. 383 | * printf("\nOn begin test event ...\n\n"); 384 | * } 385 | * 386 | * int main(int argc, char **argv) 387 | * { 388 | * uint8_t vec1[] = "This is a simple text"; 389 | * uint8_t *vec2 = malloc(sizeof(vec1)); 390 | * 391 | * memcpy(vec2, vec1, sizeof(vec1)); 392 | * on_begin_test(on_begin_test_fn); // It will be called every begin test event 393 | * 394 | * C_ASSERT_NOT_EQUAL_BYTE(vec1, vec2, sizeof(vec1), 395 | * CTEST_SETTER( 396 | * CTEST_INFO("Testing if \"vec1\" (%p) is different from \"vec2\" (%p) of size %u", vec1, vec2, sizeof(vec1)), 397 | * CTEST_WARN("Warning: This should be different") 398 | * ) 399 | * ) 400 | * 401 | * end_tests(); 402 | * 403 | * return 0; 404 | * } 405 | * ``` 406 | * @see rm_begin_test 407 | */ 408 | void on_begin_test(header_on_cb); 409 | 410 | /** 411 | * @fn void rm_begin_test() 412 | * @brief Removes callback pointer from global _on_begin_test_ context 413 | * 414 | * @see on_test 415 | */ 416 | void rm_begin_test(); 417 | 418 | /** 419 | * @fn void on_test(header_on_cb callback) 420 | * @brief Call function _callback_ on each test event 421 | * 422 | * - Example: 423 | * 424 | * ```c 425 | * #include 426 | * #include 427 | * #include 428 | * 429 | * void on_test_fn(void *context) 430 | * { 431 | * // context is not used. 432 | * printf("\nOn test event ...\n\n"); 433 | * } 434 | * 435 | * int main(int argc, char **argv) 436 | * { 437 | * uint8_t vec1[] = "This is a simple text"; 438 | * uint8_t *vec2 = malloc(sizeof(vec1)); 439 | * 440 | * memcpy(vec2, vec1, sizeof(vec1)); 441 | * on_test(on_test_fn); // It will be called every test event 442 | * 443 | * C_ASSERT_NOT_EQUAL_BYTE(vec1, vec2, sizeof(vec1), 444 | * CTEST_SETTER( 445 | * CTEST_INFO("Testing if \"vec1\" (%p) is different from \"vec2\" (%p) of size %u", vec1, vec2, sizeof(vec1)), 446 | * CTEST_WARN("Warning: This should be different") 447 | * ) 448 | * ) 449 | * 450 | * end_tests(); 451 | * 452 | * return 0; 453 | * } 454 | * ``` 455 | * @see rm_on_test 456 | */ 457 | void on_test(header_on_cb); 458 | 459 | /** 460 | * @fn void rm_on_test() 461 | * @brief Removes callback pointer from global _on_test_ context 462 | * 463 | * @see on_test 464 | */ 465 | void rm_on_test(); 466 | 467 | /** 468 | * @fn void on_end_test(header_on_cb callback) 469 | * @brief Call function _callback_ if at the end of all tests (if success) 470 | * 471 | * - Example: 472 | * 473 | * ```c 474 | * #include 475 | * #include 476 | * #include 477 | * 478 | * void on_end_test_fn(void *context) 479 | * { 480 | * // context is not used. 481 | * printf("\nEnding tests ...\nExtra summary goes here ...\n\n"); 482 | * } 483 | * 484 | * int main(int argc, char **argv) 485 | * { 486 | * uint8_t vec1[] = "This is a simple text"; 487 | * uint8_t *vec2 = malloc(sizeof(vec1)); 488 | * 489 | * memcpy(vec2, vec1, sizeof(vec1)); 490 | * on_end_test(on_end_test_fn); // It will be called if all tests finishes successfully 491 | * 492 | * C_ASSERT_NOT_EQUAL_BYTE(vec1, vec2, sizeof(vec1), 493 | * CTEST_SETTER( 494 | * CTEST_INFO("Testing if \"vec1\" (%p) is different from \"vec2\" (%p) of size %u", vec1, vec2, sizeof(vec1)), 495 | * CTEST_WARN("Warning: This should be different") 496 | * ) 497 | * ) 498 | * 499 | * end_tests(); // Function on_end_test_fn will be called here 500 | * 501 | * return 0; 502 | * } 503 | * ``` 504 | * @see rm_on_end_test 505 | */ 506 | void on_end_test(header_on_cb); 507 | 508 | /** 509 | * @fn void rm_on_end_test() 510 | * @brief Removes callback pointer from global _rm_on_end_test_ context 511 | * 512 | * @see on_end_test 513 | */ 514 | void rm_on_end_test(); 515 | 516 | /** 517 | * @fn void on_abort(header_on_cb callback) 518 | * @brief Call function _callback_ if any test fails 519 | * 520 | * - Example: 521 | * 522 | * ```c 523 | * #include 524 | * #include 525 | * #include 526 | * 527 | * uint8_t *vec2; 528 | * 529 | * void on_abort_fn(void *context) 530 | * { 531 | * // context is not used. 532 | * if (vec2) 533 | * free(vec2); 534 | * } 535 | * 536 | * int main(int argc, char **argv) 537 | * { 538 | * uint8_t vec1[] = "This is a simple text"; 539 | * vec2 = malloc(sizeof(vec1)); 540 | * 541 | * memcpy(vec2, vec1, sizeof(vec1)); 542 | * on_abort(on_abort_fn); // If error occurs, on abort will call on_abort_fn() function for any assert function 543 | * 544 | * C_ASSERT_NOT_EQUAL_BYTE(vec1, vec2, sizeof(vec1), 545 | * CTEST_SETTER( 546 | * CTEST_INFO("Testing if \"vec1\" (%p) is different from \"vec2\" (%p) of size %u", vec1, vec2, sizeof(vec1)), 547 | * CTEST_WARN("Warning: This should be different") 548 | * ) 549 | * ) 550 | * 551 | * rm_on_abort(); // Release on_abort_fn() from lines below 552 | * end_tests(); 553 | * 554 | * return 0; 555 | * } 556 | * ``` 557 | * @see rm_on_abort 558 | */ 559 | void on_abort(header_on_cb); 560 | 561 | /** 562 | * @fn void rm_on_abort() 563 | * @brief Removes callback pointer from global _on_abort_ context 564 | * 565 | * @see on_abort 566 | */ 567 | void rm_on_abort(); 568 | 569 | /** 570 | * @fn void end_tests() 571 | * @brief This function is called in every tests. It shows statistics of the tests 572 | */ 573 | void end_tests(); 574 | 575 | #ifndef CTEST_DOC_SKIP 576 | void *vargs_setter(int, ...); 577 | void *set_varg(uint32_t, const char *, ...); 578 | 579 | #define C_TEST_TYPE_VARGS_MSG (uint32_t)(0x10000000) 580 | #define C_TEST_TYPE_VARGS_CALLBACK (uint32_t)(0x20000000|C_TEST_TYPE_VARGS_MSG) 581 | 582 | #define C_TEST_VARGS_TITLE (uint32_t)(C_TEST_TYPE_VARGS_MSG|0x002E4992) 583 | #define C_TEST_VARGS_INFO (uint32_t)(C_TEST_TYPE_VARGS_MSG|0x012E4992) 584 | #define C_TEST_VARGS_WARNING (uint32_t)(C_TEST_TYPE_VARGS_MSG|0x022E4992) 585 | #define C_TEST_VARGS_ERROR (uint32_t)(C_TEST_TYPE_VARGS_MSG|0x032E4992) 586 | #define C_TEST_VARGS_SUCCESS (uint32_t)(C_TEST_TYPE_VARGS_MSG|0x042E4992) 587 | 588 | #define C_TEST_VARGS_ON_SUCCESS_CALLBACK (uint32_t)(C_TEST_TYPE_VARGS_CALLBACK|0x052E4992) 589 | #define C_TEST_VARGS_ON_ERROR_CALLBACK (uint32_t)(C_TEST_TYPE_VARGS_CALLBACK|0x062E4992) 590 | #endif 591 | 592 | /** 593 | * @def CTEST_SETTER(...) 594 | * @brief Setter for CTEST. This setter allows callback function such _on_error_ and _on_success_ and add custom _message_, _warn_, _error_, _info_ and _title_ 595 | * 596 | * # Example: 597 | * 598 | *```c 599 | * const char *message = "This is a text"; 600 | * ... 601 | * C_ASSERT_TRUE(a > b, 602 | * CTEST_SETTER( 603 | * CTEST_TITLE("This is a title with message %s", message), 604 | * CTEST_INFO("This is an INFO title"), 605 | * CTEST_WARN("This is a WARN message"), 606 | * CTEST_ON_ERROR("This is a message when error occurs"), 607 | * CTEST_ON_SUCCESS("This is a message when SUCCESS occurs"), 608 | * CTEST_ON_ERROR_CB(cb_func_on_error, "This function is called on error"), 609 | * CTEST_ON_SUCCESS_CB(cb_func_on_success, "This function is callend on success") 610 | * )) 611 | * ... 612 | *``` 613 | */ 614 | #define CTEST_SETTER(...) vargs_setter(-1, __VA_ARGS__, NULL, VA_END_SIGNATURE) 615 | 616 | /** 617 | * @def CTEST_TITLE(...) 618 | * @brief Set a title message to test. It is only used with _CTEST_SETTER_ macro 619 | * 620 | * # Example: 621 | * 622 | *```c 623 | * 624 | * ... 625 | * C_ASSERT_EQUAL_BYTE(vec1, vec3, sizeof(vec1), 626 | * CTEST_SETTER( 627 | * CTEST_TITLE("Checking if vec1 at (%p) has equal bytes with vec2 at (%p)", vec1, vec2, 628 | * ) 629 | * ) 630 | * ... 631 | *``` 632 | */ 633 | #define CTEST_TITLE(...) set_varg(C_TEST_VARGS_TITLE, __VA_ARGS__) 634 | 635 | /** 636 | * @def CTEST_INFO(...) 637 | * @brief Set a info message to test. It is only used with _CTEST_SETTER_ macro 638 | * 639 | * # Example: 640 | * 641 | *```c 642 | * 643 | * ... 644 | * C_ASSERT_EQUAL_BYTE(vec1, vec3, sizeof(vec1), 645 | * CTEST_SETTER( 646 | * CTEST_INFO("Checking if vec1 at (%p) has equal bytes with vec2 at (%p)", vec1, vec2, 647 | * ) 648 | * ) 649 | * ... 650 | *``` 651 | */ 652 | #define CTEST_INFO(...) set_varg(C_TEST_VARGS_INFO, __VA_ARGS__) 653 | 654 | /** 655 | * @def CTEST_WARN(...) 656 | * @brief Set a warn message to test. It is only used with _CTEST_SETTER_ macro 657 | * 658 | * # Example: 659 | * 660 | *```c 661 | * 662 | * ... 663 | * C_ASSERT_EQUAL_BYTE(vec1, vec3, sizeof(vec1), 664 | * CTEST_SETTER( 665 | * CTEST_WARN("Checking if vec1 at (%p) has equal bytes with vec2 at (%p)", vec1, vec2, 666 | * ) 667 | * ) 668 | * ... 669 | *``` 670 | */ 671 | #define CTEST_WARN(...) set_varg(C_TEST_VARGS_WARNING, __VA_ARGS__) 672 | #define CTEST_ON_ERROR(...) set_varg(C_TEST_VARGS_ERROR, __VA_ARGS__) 673 | #define CTEST_ON_SUCCESS(...) set_varg(C_TEST_VARGS_SUCCESS, __VA_ARGS__) 674 | #define CTEST_ON_SUCCESS_CB(...) set_varg_callback(C_TEST_VARGS_ON_SUCCESS_CALLBACK, __VA_ARGS__, NULL, VAS_END_SIGNATURE) 675 | #define CTEST_ON_ERROR_CB(...) set_varg_callback(C_TEST_VARGS_ON_ERROR_CALLBACK, __VA_ARGS__, NULL, VAS_END_SIGNATURE) 676 | 677 | /** 678 | * @def C_ASSERT_FALSE(result, ...) 679 | * @brief Checks if result is _FALSE_ 680 | * @param result Result value 681 | * @param ... Optional. See CTEST_SETTER() for details 682 | * 683 | * @see C_ASSERT_TRUE 684 | */ 685 | #define C_ASSERT_FALSE(...) assert_false(__VA_ARGS__, VAS_END_SIGNATURE); 686 | 687 | /** 688 | * @def C_ASSERT_TRUE(result, ...) 689 | * @brief Checks if result is _TRUE_ 690 | * @param result Result value 691 | * @param ... Optional. See CTEST_SETTER() for details 692 | * 693 | * @see C_ASSERT_FALSE 694 | */ 695 | #define C_ASSERT_TRUE(...) assert_true(__VA_ARGS__, VAS_END_SIGNATURE); 696 | 697 | /** 698 | * @def C_ASSERT_EQUAL_INT(expected, result, ...) 699 | * @brief Checks if expected and result value are equal 700 | * @param expected Expected value 701 | * @param result Result value 702 | * @param ... Optional. See CTEST_SETTER() for details 703 | * 704 | * @see C_ASSERT_NOT_EQUAL_INT 705 | */ 706 | #define C_ASSERT_EQUAL_INT(expected, ...) assert_equal_int(expected, __VA_ARGS__, VAS_END_SIGNATURE); 707 | 708 | /** 709 | * @def C_ASSERT_NOT_EQUAL_INT(unexpected, result, ...) 710 | * @brief Checks if expected and result value are NOT equal 711 | * @param unexpected Unexpected value 712 | * @param result Result value 713 | * @param ... Optional. See CTEST_SETTER() for details 714 | * 715 | * @see C_ASSERT_EQUAL_INT 716 | */ 717 | #define C_ASSERT_NOT_EQUAL_INT(unexpected, ...) assert_not_equal_int(unexpected, __VA_ARGS__, VAS_END_SIGNATURE); 718 | 719 | /** 720 | * @def C_ASSERT_EQUAL_LONG_INT(expected, result, ...) 721 | * @brief Checks if expected and result value are equal 722 | * @param expected Expected value 723 | * @param result Result value 724 | * @param ... Optional. See CTEST_SETTER() for details 725 | * 726 | * @see C_ASSERT_NOT_EQUAL_LONG_INT 727 | */ 728 | #define C_ASSERT_EQUAL_LONG_INT(expected, ...) assert_equal_longint(expected, __VA_ARGS__, VAS_END_SIGNATURE); 729 | 730 | /** 731 | * @def C_ASSERT_NOT_EQUAL_LONG_INT(unexpected, result, ...) 732 | * @brief Checks if expected and result value are NOT equal 733 | * @param unexpected Unexpected value 734 | * @param result Result value 735 | * @param ... Optional. See CTEST_SETTER() for details 736 | * 737 | * @see C_ASSERT_EQUAL_LONG_INT 738 | */ 739 | #define C_ASSERT_NOT_EQUAL_LONG_INT(expected, ...) assert_not_equal_longint(expected, __VA_ARGS__, VAS_END_SIGNATURE); 740 | 741 | /** 742 | * @def C_ASSERT_EQUAL_UNSIGNED_LONG_INT(expected, result, ...) 743 | * @brief Checks if expected and result value are equal 744 | * @param expected Expected value 745 | * @param result Result value 746 | * @param ... Optional. See CTEST_SETTER() for details 747 | * 748 | * @see C_ASSERT_NOT_EQUAL_UNSIGNED_LONG_INT 749 | */ 750 | #define C_ASSERT_EQUAL_UNSIGNED_LONG_INT(expected, ...) assert_equal_unsigned_longint(expected, __VA_ARGS__, VAS_END_SIGNATURE); 751 | 752 | /** 753 | * @def C_ASSERT_NOT_EQUAL_UNSIGNED_LONG_INT(unexpected, result, ...) 754 | * @brief Checks if expected and result value are NOT equal 755 | * @param unexpected Unexpected value 756 | * @param result Result value 757 | * @param ... Optional. See CTEST_SETTER() for details 758 | * 759 | * @see C_ASSERT_EQUAL_UNSIGNED_LONG_INT 760 | */ 761 | #define C_ASSERT_NOT_EQUAL_UNSIGNED_LONG_INT(expected, ...) assert_not_equal_unsigned_longint(expected, __VA_ARGS__, VAS_END_SIGNATURE); 762 | 763 | /** 764 | * @def C_ASSERT_EQUAL_DOUBLE(expected, result, delta, ...) 765 | * @brief Checks if expected and result value are equal 766 | * @param expected Unexpected value 767 | * @param result Result value 768 | * @param delta Delta double value 769 | * @param ... Optional. See CTEST_SETTER() for details 770 | * 771 | * @see C_ASSERT_NOT_EQUAL_DOUBLE 772 | */ 773 | #define C_ASSERT_EQUAL_DOUBLE(expected, result, ...) assert_equal_double(expected, result, __VA_ARGS__, VAS_END_SIGNATURE); 774 | 775 | /** 776 | * @def C_ASSERT_NOT_EQUAL_DOUBLE(unexpected, result, delta, ...) 777 | * @brief Checks if expected and result value are NOT equal 778 | * @param unexpected Expected value 779 | * @param result Result value 780 | * @param delta Delta double value 781 | * @param ... Optional. See CTEST_SETTER() for details 782 | * 783 | * @see C_ASSERT_EQUAL_DOUBLE 784 | */ 785 | #define C_ASSERT_NOT_EQUAL_DOUBLE(expected, result, ...) assert_not_equal_double(expected, result, __VA_ARGS__, VAS_END_SIGNATURE); 786 | 787 | /** 788 | * @def C_ASSERT_EQUAL_BYTE(expected, result, size, ...) 789 | * @brief Checks two memory pointers with same size are equals 790 | * @param expected Expected value 791 | * @param result Result value 792 | * @param size Size of _expected_ and _result_ pointers 793 | * @param ... Optional. See CTEST_SETTER() for details 794 | * 795 | * @see C_ASSERT_NOT_EQUAL_BYTE 796 | */ 797 | #define C_ASSERT_EQUAL_BYTE(expected, result, ...) assert_equal_byte(expected, result, __VA_ARGS__, VAS_END_SIGNATURE); 798 | 799 | /** 800 | * @def C_ASSERT_NOT_EQUAL_BYTE(unexpected, result, size, ...) 801 | * @brief Checks two memory pointers with same size are not equals 802 | * @param unexpected Unexpected value 803 | * @param result Result value 804 | * @param size Size of _unexpected_ and _result_ pointers 805 | * @param ... Optional. See CTEST_SETTER() for details 806 | * 807 | * @see C_ASSERT_EQUAL_BYTE 808 | */ 809 | #define C_ASSERT_NOT_EQUAL_BYTE(unexpected, result, ...) assert_not_equal_byte(unexpected, result, __VA_ARGS__, VAS_END_SIGNATURE); 810 | 811 | /** 812 | * @def C_ASSERT_NULL(result, ...) 813 | * @brief Checks if pointer is _NULL_ 814 | * @param result Result pointer 815 | * @param ... Optional. See CTEST_SETTER() for details 816 | * 817 | * @see C_ASSERT_NOT_NULL 818 | */ 819 | #define C_ASSERT_NULL(...) assert_null(__VA_ARGS__, VAS_END_SIGNATURE); 820 | 821 | /** 822 | * @def C_ASSERT_NOT_NULL(result, ...) 823 | * @brief Checks if pointer is not _NULL_ 824 | * @param result Result pointer 825 | * @param ... Optional. See CTEST_SETTER() for details 826 | * 827 | * @see C_ASSERT_NULL 828 | */ 829 | #define C_ASSERT_NOT_NULL(...) assert_not_null(__VA_ARGS__, VAS_END_SIGNATURE); 830 | 831 | /** 832 | * @def C_ASSERT_EQUAL_STRING(expected, result, ...) 833 | * @brief Checks if two strings are equal 834 | * @param expected Expected value 835 | * @param result Result value 836 | * @param ... Optional. See CTEST_SETTER() for details 837 | * 838 | * @see C_ASSERT_NOT_EQUAL_STRING 839 | */ 840 | #define C_ASSERT_EQUAL_STRING(expected, ...) assert_equal_string(expected, __VA_ARGS__, VAS_END_SIGNATURE); 841 | 842 | /** 843 | * @def C_ASSERT_NOT_EQUAL_STRING(unexpected, result, ...) 844 | * @brief Checks if two strings are not equal 845 | * @param unexpected Unexpected value 846 | * @param result Result value 847 | * @param ... Optional. See CTEST_SETTER() for details 848 | * 849 | * @see C_ASSERT_EQUAL_STRING 850 | */ 851 | #define C_ASSERT_NOT_EQUAL_STRING(expected, ...) assert_not_equal_string(expected, __VA_ARGS__, VAS_END_SIGNATURE); 852 | 853 | /** 854 | * @def C_ASSERT_EQUAL_STRING_IGNORE_CASE(expected, result, ...) 855 | * @brief Checks if two strings are equal ignoring case 856 | * @param Expected Expected value 857 | * @param result Result value 858 | * @param ... Optional. See CTEST_SETTER() for details 859 | * 860 | * @see C_ASSERT_NOT_EQUAL_STRING_IGNORE_CASE 861 | */ 862 | #define C_ASSERT_EQUAL_STRING_IGNORE_CASE(expected, ...) assert_equal_string_ignore_case(expected, __VA_ARGS__, VAS_END_SIGNATURE); 863 | 864 | /** 865 | * @def C_ASSERT_NOT_EQUAL_STRING_IGNORE_CASE(unexpected, result, ...) 866 | * @brief Checks if two strings are NOT equal ignoring case 867 | * @param unexpected Unexpected value 868 | * @param result Result value 869 | * @param ... Optional. See CTEST_SETTER() for details 870 | * 871 | * @see C_ASSERT_EQUAL_STRING_IGNORE_CASE 872 | */ 873 | #define C_ASSERT_NOT_EQUAL_STRING_IGNORE_CASE(expected, ...) assert_not_equal_string_ignore_case(expected, __VA_ARGS__, VAS_END_SIGNATURE); 874 | 875 | /** 876 | * @def C_ASSERT_EQUAL_U8(expected, result, ...) 877 | * @brief Checks if expected and result value are equal 878 | * @param expected Expected value 879 | * @param result Result value 880 | * @param ... Optional. See CTEST_SETTER() for details 881 | * 882 | * @see C_ASSERT_NOT_EQUAL_U8 883 | */ 884 | #define C_ASSERT_EQUAL_U8(expected, ...) assert_equal_u8(expected, __VA_ARGS__, VAS_END_SIGNATURE); 885 | 886 | /** 887 | * @def C_ASSERT_NOT_EQUAL_U8(unexpected, result, ...) 888 | * @brief Checks if expected and result value are NOT equal 889 | * @param unexpected Unexpected value 890 | * @param result Result value 891 | * @param ... Optional. See CTEST_SETTER() for details 892 | * 893 | * @see C_ASSERT_EQUAL_U8 894 | */ 895 | #define C_ASSERT_NOT_EQUAL_U8(expected, ...) assert_not_equal_u8(expected, __VA_ARGS__, VAS_END_SIGNATURE); 896 | 897 | /** 898 | * @def C_ASSERT_EQUAL_S8(expected, result, ...) 899 | * @brief Checks if expected and result value are equal 900 | * @param expected Expected value 901 | * @param result Result value 902 | * @param ... Optional. See CTEST_SETTER() for details 903 | * 904 | * @see C_ASSERT_NOT_EQUAL_S8 905 | */ 906 | #define C_ASSERT_EQUAL_S8(expected, ...) assert_equal_s8(expected, __VA_ARGS__, VAS_END_SIGNATURE); 907 | 908 | /** 909 | * @def C_ASSERT_NOT_EQUAL_S8(unexpected, result, ...) 910 | * @brief Checks if expected and result value are NOT equal 911 | * @param unexpected Unexpected value 912 | * @param result Result value 913 | * @param ... Optional. See CTEST_SETTER() for details 914 | * 915 | * @see C_ASSERT_EQUAL_S8 916 | */ 917 | #define C_ASSERT_NOT_EQUAL_S8(expected, ...) assert_not_equal_s8(expected, __VA_ARGS__, VAS_END_SIGNATURE); 918 | 919 | /** 920 | * @def C_ASSERT_EQUAL_U16(expected, result, ...) 921 | * @brief Checks if expected and result value are equal 922 | * @param expected Expected value 923 | * @param result Result value 924 | * @param ... Optional. See CTEST_SETTER() for details 925 | * 926 | * @see C_ASSERT_NOT_EQUAL_U16 927 | */ 928 | #define C_ASSERT_EQUAL_U16(expected, ...) assert_equal_u16(expected, __VA_ARGS__, VAS_END_SIGNATURE); 929 | 930 | /** 931 | * @def C_ASSERT_NOT_EQUAL_U16(unexpected, result, ...) 932 | * @brief Checks if expected and result value are NOT equal 933 | * @param unexpected Unexpected value 934 | * @param result Result value 935 | * @param ... Optional. See CTEST_SETTER() for details 936 | * 937 | * @see C_ASSERT_EQUAL_U16 938 | */ 939 | #define C_ASSERT_NOT_EQUAL_U16(expected, ...) assert_not_equal_u16(expected, __VA_ARGS__, VAS_END_SIGNATURE); 940 | 941 | /** 942 | * @def C_ASSERT_EQUAL_S16(expected, result, ...) 943 | * @brief Checks if expected and result value are equal 944 | * @param expected Expected value 945 | * @param result Result value 946 | * @param ... Optional. See CTEST_SETTER() for details 947 | * 948 | * @see C_ASSERT_NOT_EQUAL_S16 949 | */ 950 | #define C_ASSERT_EQUAL_S16(expected, ...) assert_equal_s16(expected, __VA_ARGS__, VAS_END_SIGNATURE); 951 | 952 | /** 953 | * @def C_ASSERT_NOT_EQUAL_S16(unexpected, result, ...) 954 | * @brief Checks if expected and result value are NOT equal 955 | * @param unexpected Unexpected value 956 | * @param result Result value 957 | * @param ... Optional. See CTEST_SETTER() for details 958 | * 959 | * @see C_ASSERT_EQUAL_S16 960 | */ 961 | #define C_ASSERT_NOT_EQUAL_S16(expected, ...) assert_not_equal_s16(expected, __VA_ARGS__, VAS_END_SIGNATURE); 962 | 963 | /** 964 | * @def C_ASSERT_EQUAL_U32(expected, result, ...) 965 | * @brief Checks if expected and result value are equal 966 | * @param expected Expected value 967 | * @param result Result value 968 | * @param ... Optional. See CTEST_SETTER() for details 969 | * 970 | * @see C_ASSERT_NOT_EQUAL_U32 971 | */ 972 | #define C_ASSERT_EQUAL_U32(expected, ...) assert_equal_u32(expected, __VA_ARGS__, VAS_END_SIGNATURE); 973 | 974 | /** 975 | * @def C_ASSERT_NOT_EQUAL_U32(unexpected, result, ...) 976 | * @brief Checks if expected and result value are NOT equal 977 | * @param unexpected Unexpected value 978 | * @param result Result value 979 | * @param ... Optional. See CTEST_SETTER() for details 980 | * 981 | * @see C_ASSERT_EQUAL_U32 982 | */ 983 | #define C_ASSERT_NOT_EQUAL_U32(expected, ...) assert_not_equal_u32(expected, __VA_ARGS__, VAS_END_SIGNATURE); 984 | 985 | /** 986 | * @def C_ASSERT_EQUAL_S32(expected, result, ...) 987 | * @brief Checks if expected and result value are equal 988 | * @param expected Expected value 989 | * @param result Result value 990 | * @param ... Optional. See CTEST_SETTER() for details 991 | * 992 | * @see C_ASSERT_NOT_EQUAL_S32 993 | */ 994 | #define C_ASSERT_EQUAL_S32(expected, ...) assert_equal_s32(expected, __VA_ARGS__, VAS_END_SIGNATURE); 995 | 996 | /** 997 | * @def C_ASSERT_NOT_EQUAL_S32(unexpected, result, ...) 998 | * @brief Checks if expected and result value are NOT equal 999 | * @param unexpected Unexpected value 1000 | * @param result Result value 1001 | * @param ... Optional. See CTEST_SETTER() for details 1002 | * 1003 | * @see C_ASSERT_EQUAL_S32 1004 | */ 1005 | #define C_ASSERT_NOT_EQUAL_S32(expected, ...) assert_not_equal_s32(expected, __VA_ARGS__, VAS_END_SIGNATURE); 1006 | 1007 | /** 1008 | * @def C_ASSERT_EQUAL_U64(expected, result, ...) 1009 | * @brief Checks if expected and result value are equal 1010 | * @param expected Expected value 1011 | * @param result Result value 1012 | * @param ... Optional. See CTEST_SETTER() for details 1013 | * 1014 | * @see C_ASSERT_NOT_EQUAL_U64 1015 | */ 1016 | #define C_ASSERT_EQUAL_U64(expected, ...) assert_equal_u64(expected, __VA_ARGS__, VAS_END_SIGNATURE); 1017 | 1018 | /** 1019 | * @def C_ASSERT_NOT_EQUAL_U64(unexpected, result, ...) 1020 | * @brief Checks if expected and result value are NOT equal 1021 | * @param unexpected Unexpected value 1022 | * @param result Result value 1023 | * @param ... Optional. See CTEST_SETTER() for details 1024 | * 1025 | * @see C_ASSERT_EQUAL_U64 1026 | */ 1027 | #define C_ASSERT_NOT_EQUAL_U64(expected, ...) assert_not_equal_u64(expected, __VA_ARGS__, VAS_END_SIGNATURE); 1028 | 1029 | /** 1030 | * @def C_ASSERT_EQUAL_S64(expected, result, ...) 1031 | * @brief Checks if expected and result value are equal 1032 | * @param expected Expected value 1033 | * @param result Result value 1034 | * @param ... Optional. See CTEST_SETTER() for details 1035 | * 1036 | * @see C_ASSERT_NOT_EQUAL_S64 1037 | */ 1038 | #define C_ASSERT_EQUAL_S64(expected, ...) assert_equal_s64(expected, __VA_ARGS__, VAS_END_SIGNATURE); 1039 | 1040 | /** 1041 | * @def C_ASSERT_NOT_EQUAL_S64(unexpected, result, ...) 1042 | * @brief Checks if expected and result value are NOT equal 1043 | * @param unexpected Unexpected value 1044 | * @param result Result value 1045 | * @param ... Optional. See CTEST_SETTER() for details 1046 | * 1047 | * @see C_ASSERT_EQUAL_S64 1048 | */ 1049 | #define C_ASSERT_NOT_EQUAL_S64(expected, ...) assert_not_equal_s64(expected, __VA_ARGS__, VAS_END_SIGNATURE); 1050 | 1051 | /** 1052 | * @def C_ASSERT_FAIL(...) 1053 | * @brief Fails on execution 1054 | * @param ... Optional. See CTEST_SETTER() for details 1055 | * 1056 | * - Example: 1057 | * 1058 | * ```c 1059 | * #include 1060 | * #include 1061 | * #include 1062 | * 1063 | * int main(int argc, char **argv) 1064 | * { 1065 | * C_ASSERT_FAIL(NULL, 1066 | * CTEST_SETTER( 1067 | * CTEST_INFO("Information fail"), 1068 | * CTEST_WARN("Warning: This should fail") 1069 | * ) 1070 | * ) 1071 | * printf("Never reaches here"); 1072 | * 1073 | * end_tests(); 1074 | * 1075 | * return 0; 1076 | * } 1077 | * ``` 1078 | */ 1079 | #define C_ASSERT_FAIL(...) assert_fail(__VA_ARGS__, VAS_END_SIGNATURE); 1080 | 1081 | #ifndef CTEST_DOC_SKIP 1082 | #ifdef DEBUG_TEST 1083 | // TEMPORARY FOR TESTS 1084 | 1085 | int load_test_vargs_for_test(void **, ...); 1086 | int load_test_vargs_for_test_v2(void **, ...); 1087 | int free_vargs_for_test(void *); 1088 | char *ctest_setter_has_title(void *); 1089 | char *ctest_setter_has_info(void *); 1090 | char *ctest_setter_has_warn(void *); 1091 | char *ctest_setter_has_onerror(void *); 1092 | char *ctest_setter_has_onsuccess(void *); 1093 | void show_message_text(); 1094 | #endif 1095 | #endif 1096 | 1097 | #ifdef __cplusplus 1098 | } 1099 | #endif 1100 | 1101 | 1102 | -------------------------------------------------------------------------------- /test/testutil.c: -------------------------------------------------------------------------------- 1 | #include "testutil.h" 2 | 3 | static int is_vec_null(uint8_t *vec, ssize_t vec_sz) 4 | { 5 | while (vec_sz > 0) 6 | if (vec[--vec_sz]) 7 | return 0; 8 | 9 | return 1; 10 | } 11 | 12 | void TEST_check_digest() { 13 | int err; 14 | uint8_t sha512[64]; 15 | char *errMsg; 16 | #define DIGEST_MESSAGE "test pointer\x0A" 17 | //sha512sum_check = echo "test pointer" | sha512sum 18 | uint8_t sha512sum_check[] = { 19 | 0xda, 0x28, 0xeb, 0x1a, 0xc5, 0xe0, 0x56, 0xbd, 20 | 0x2d, 0x74, 0x90, 0x90, 0x9b, 0xe5, 0xe2, 0x02, 21 | 0xe7, 0xea, 0x1d, 0x91, 0xb5, 0xd4, 0x31, 0x20, 22 | 0xe9, 0x3f, 0x38, 0xe0, 0xaf, 0xf1, 0xe9, 0x7b, 23 | 0xf2, 0x3d, 0xc5, 0xa4, 0x0b, 0xc5, 0x43, 0xa6, 24 | 0x7e, 0x89, 0x96, 0x61, 0x0c, 0x35, 0xa1, 0x15, 25 | 0x10, 0x64, 0x70, 0x93, 0xd6, 0x5f, 0xa8, 0x0b, 26 | 0xce, 0x8f, 0x07, 0x52, 0x41, 0x1f, 0xbe, 0xdf 27 | }; 28 | 29 | CLEAR_VECTOR(sha512) 30 | 31 | err=ssl_hash512(sha512, (uint8_t *)STR_CONST(DIGEST_MESSAGE), &errMsg); 32 | 33 | C_ASSERT_EQUAL_INT(0, err, CTEST_SETTER( 34 | CTEST_TITLE("Testing ssl_hash512() FIRST TEST"), 35 | CTEST_INFO("Return value SHOULD be 0"), 36 | CTEST_ON_ERROR("Was expected value equal 0. Error message: %s", errMsg), 37 | CTEST_ON_SUCCESS("Success with message: %s", errMsg) 38 | )) 39 | 40 | TITLE_MSG("Testing CHECK SHA 512 digest below is correct ...") 41 | debug_dump(sha512, sizeof(sha512)); 42 | 43 | C_ASSERT_TRUE(COMPARE_VECTORS(sha512sum_check, sha512), CTEST_SETTER( 44 | CTEST_TITLE("Testing ssl_hash512("DIGEST_MESSAGE") and compare to sha512sum_check vector ..."), 45 | CTEST_INFO("Return value SHOULD be TRUE"), 46 | CTEST_ON_ERROR("Was expected value equal."), 47 | CTEST_ON_SUCCESS("Success. Vectors are equals") 48 | )) 49 | 50 | err=check_hash512(sha512sum_check, (uint8_t *)STR_CONST(DIGEST_MESSAGE), &errMsg); 51 | 52 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 53 | CTEST_TITLE("Testing %p for success HASH message", errMsg), 54 | CTEST_INFO("Pointer string value SHOULD be not NULL") 55 | )) 56 | 57 | C_ASSERT_EQUAL_STRING("Checksum SUCCESS\n", errMsg, CTEST_SETTER( 58 | CTEST_TITLE("Testing successful hash message") 59 | )) 60 | 61 | C_ASSERT_TRUE(err != 0, CTEST_SETTER( 62 | CTEST_TITLE("Testing check_hash512 with correct digest message..."), 63 | CTEST_INFO("Return value SHOULD be NOT EQUAL ZERO (%d)", err), 64 | CTEST_ON_ERROR("Was unexpected: value equal zero"), 65 | CTEST_ON_SUCCESS("Success. Return = %d \n%s", err, errMsg) 66 | )) 67 | 68 | err=check_hash512(sha512sum_check, (uint8_t *)STR_CONST(DIGEST_MESSAGE"_ABC"), &errMsg); 69 | #undef DIGEST_MESSAGE 70 | 71 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 72 | CTEST_TITLE("Testing %p for not success HASH message", errMsg), 73 | CTEST_INFO("Pointer string value SHOULD be not NULL") 74 | )) 75 | 76 | C_ASSERT_EQUAL_STRING("Wrong checksum\n", errMsg, CTEST_SETTER( 77 | CTEST_TITLE("Testing NOT successful hash message") 78 | )) 79 | 80 | C_ASSERT_TRUE(err == 0, CTEST_SETTER( 81 | CTEST_TITLE("Testing check_hash512 with incorrect digest message..."), 82 | CTEST_INFO("Return value SHOULD be EQUAL ZERO (%d)", err), 83 | CTEST_ON_ERROR("Was unexpected: value not equal zero"), 84 | CTEST_ON_SUCCESS("Success. Return = %d\n%s", err, errMsg) 85 | )) 86 | } 87 | 88 | #define TEST_TYPE_NAME(type) {#type, type} 89 | struct test_entropy_t { 90 | const char *name; 91 | uint32_t type; 92 | } TEST_ENTROPY_TYPE[] = { 93 | TEST_TYPE_NAME(F_ENTROPY_TYPE_NOT_RECOMENDED), 94 | TEST_TYPE_NAME(F_ENTROPY_TYPE_GOOD), 95 | TEST_TYPE_NAME(F_ENTROPY_TYPE_EXCELENT), 96 | TEST_TYPE_NAME(F_ENTROPY_TYPE_PARANOIC), 97 | {NULL} 98 | }; 99 | #undef TEST_TYPE_NAME 100 | 101 | #define MAX_TIMEOUT_IN_SECOND 16 102 | 103 | void TEST_entropy_destroy(void *ctx) 104 | { 105 | free(ctx); 106 | close_random(); 107 | } 108 | 109 | void TEST_entropy() 110 | { 111 | uint8_t *random_values; 112 | size_t random_values_size_aligned; 113 | uint64_t wait_time; 114 | int err; 115 | 116 | struct test_entropy_t *tep = TEST_ENTROPY_TYPE; 117 | 118 | TITLE_MSG("Begin entropy test") 119 | INFO_MSG("Opening random ...") 120 | 121 | open_random(NULL); 122 | 123 | #define FIRST_VALUE_SIZE 56 124 | random_values=(uint8_t *)n_malloc(&random_values_size_aligned, FIRST_VALUE_SIZE); 125 | 126 | C_ASSERT_NOT_NULL(random_values, CTEST_SETTER( 127 | CTEST_TITLE("Testing n_malloc to alloc pointer %p with size %lu bytes", random_values, random_values_size_aligned), 128 | CTEST_INFO("Return value SHOULD be not NULL") 129 | )) 130 | 131 | C_ASSERT_TRUE(random_values_size_aligned > FIRST_VALUE_SIZE, CTEST_SETTER( 132 | CTEST_TITLE("Check if memory is aligned"), 133 | CTEST_INFO("Value %lu bytes with aligned size %lu bytes", FIRST_VALUE_SIZE, random_values_size_aligned), 134 | CTEST_ON_ERROR_CB(TEST_entropy_destroy, (void *)random_values) 135 | )) 136 | 137 | free(random_values); 138 | #undef FIRST_VALUE_SIZE 139 | 140 | #define SECOND_VALUE_SIZE 1024 141 | random_values=(uint8_t *)n_malloc(&random_values_size_aligned, SECOND_VALUE_SIZE); 142 | 143 | C_ASSERT_NOT_NULL(random_values, CTEST_SETTER( 144 | CTEST_TITLE("Testing n_malloc to second alloc pointer %p with size %lu bytes", random_values, random_values_size_aligned), 145 | CTEST_INFO("Return value SHOULD be not NULL") 146 | )) 147 | 148 | C_ASSERT_TRUE(random_values_size_aligned == SECOND_VALUE_SIZE, CTEST_SETTER( 149 | CTEST_TITLE("Check if memory is aligned (SECOND TEST)"), 150 | CTEST_INFO("Value %lu bytes with aligned size %lu bytes (SECOND TEST)", SECOND_VALUE_SIZE, random_values_size_aligned), 151 | CTEST_ON_ERROR_CB(TEST_entropy_destroy, (void *)random_values) 152 | )) 153 | #undef SECOND_VALUE_SIZE 154 | 155 | INFO_MSG_FMT("Cleaning vector random_values at (%p) with size %lu bytes", random_values, random_values_size_aligned) 156 | memset(random_values, 0, random_values_size_aligned); 157 | debug_dump(random_values, random_values_size_aligned); 158 | 159 | while (tep->name) { 160 | wait_time = 1; 161 | 162 | system_entropy_ret: 163 | 164 | INFO_MSG_FMT("Testing entropy %s with random number generator with timeout %lu s ...", tep->name, wait_time) 165 | 166 | err=verify_system_entropy(tep->type, random_values, random_values_size_aligned, wait_time); 167 | 168 | if ((err != 0) && (wait_time < MAX_TIMEOUT_IN_SECOND)) { 169 | WARN_MSG_FMT("verify_system_entropy %s error %d. Trying new timeout %lu", tep->name, err, ++wait_time) 170 | goto system_entropy_ret; 171 | } 172 | 173 | C_ASSERT_TRUE(err == 0, CTEST_SETTER( 174 | CTEST_TITLE("Check if entropy %s has SUCCESS", tep->name), 175 | CTEST_ON_ERROR("Check entropy %s. Return error : %d MAX_TIMEOUT_IN_SECOND = %lu s reached", tep->name, err, MAX_TIMEOUT_IN_SECOND), 176 | CTEST_ON_SUCCESS("Entropy %s success", tep->name), 177 | CTEST_ON_ERROR_CB(TEST_entropy_destroy, (void *)random_values) 178 | )) 179 | 180 | INFO_MSG_FMT("Vector random_values at (%p) with size %lu bytes with random data:", random_values, random_values_size_aligned) 181 | debug_dump(random_values, random_values_size_aligned); 182 | tep++; 183 | } 184 | 185 | TEST_entropy_destroy((void *)random_values); 186 | } 187 | 188 | void TEST_generate_random_pass_destroy(void *ctx) 189 | { 190 | pass_list_free((struct pass_list_t **)ctx); 191 | close_random(); 192 | } 193 | 194 | void TEST_generate_random_pass() 195 | { 196 | int err; 197 | uint8_t wait_time; 198 | struct test_entropy_t *tep = TEST_ENTROPY_TYPE; 199 | 200 | #define PASS_LIST_NUM 50 201 | struct pass_list_t *pass_list = pass_list_new(PASS_LIST_NUM); 202 | 203 | TITLE_MSG("Begin generate random password ...") 204 | 205 | open_random(NULL); 206 | 207 | C_ASSERT_NOT_NULL(pass_list, CTEST_SETTER( 208 | CTEST_TITLE("Testing pass_list is not NULL"), 209 | CTEST_INFO("Return value SHOULD be not NULL") 210 | )) 211 | #undef PASS_LIST_NUM 212 | 213 | while (tep->name) { 214 | wait_time=1; 215 | 216 | INFO_MSG_FMT("Testing pass list %s with random number generator with timeout %lu s ...", tep->name, wait_time) 217 | 218 | generate_random_pass_ret: 219 | err = randomize_and_print_pass_list(pass_list, tep->type, wait_time); 220 | 221 | printf("\n\n"); 222 | 223 | if ((err != 0) && (wait_time < MAX_TIMEOUT_IN_SECOND)) { 224 | WARN_MSG_FMT("Pass list %s error %d. Trying new timeout %lu", tep->name, err, ++wait_time) 225 | goto generate_random_pass_ret; 226 | } 227 | 228 | C_ASSERT_TRUE(err == 0, CTEST_SETTER( 229 | CTEST_TITLE("Check if pass list with %s has SUCCESS", tep->name), 230 | CTEST_ON_ERROR("Check pass list %s fail. Return error : %d MAX_TIMEOUT_IN_SECOND = %lu s reached", tep->name, err, MAX_TIMEOUT_IN_SECOND), 231 | CTEST_ON_SUCCESS("Pass list %s success", tep->name), 232 | CTEST_ON_ERROR_CB(TEST_generate_random_pass_destroy, (void *)&pass_list) 233 | )) 234 | 235 | tep++; 236 | } 237 | 238 | INFO_MSG_FMT("Destroying pass list (%p)", pass_list) 239 | pass_list_free(&pass_list); 240 | 241 | C_ASSERT_NULL(pass_list, CTEST_SETTER( 242 | CTEST_TITLE("Check if pass_list is NULL") 243 | )) 244 | 245 | WARN_MSG("Testing \"double free\" guard is safe") 246 | pass_list_free(&pass_list); 247 | 248 | close_random(); 249 | } 250 | 251 | #undef MAX_TIMEOUT_IN_SECOND 252 | 253 | #define PLAIN_TEXT "This Message has more than 16 bytes" 254 | #define IV_TEST "ThisIVHas16Bytes" 255 | #define PRIVATE_KEY_TEST "~This Private Key Has 32 B long~" 256 | 257 | _Static_assert((sizeof(PLAIN_TEXT)-1)&0x0F, "PLAIN_TEXT LENGHT MUST BE NOT MULTIPLE OF 16 FOR THIS TEST"); 258 | _Static_assert(sizeof(IV_TEST)-1 == 16, "IV_TEST STRING MUST HAVE LENGTH = 16"); 259 | _Static_assert(sizeof(PRIVATE_KEY_TEST)-1 == 32, "PRIVATE_KEY_TEST STRING MUST HAVE LENGTH = 32"); 260 | 261 | static void TEST_encryption_aes_256_destroy(void *context) 262 | { 263 | struct test_encryption_aes_256_t **ctx = (struct test_encryption_aes_256_t **)context; 264 | if (*ctx) { 265 | free((void *)(*ctx)->encrypted_data); 266 | free((void *)(*ctx)); 267 | (*ctx) = NULL; 268 | } 269 | } 270 | 271 | struct test_encryption_aes_256_t *TEST_encryption_aes_256() 272 | { 273 | int err, testNumber = 0; 274 | char *errMsg; 275 | uint8_t *buffer; 276 | uint8_t encryptedData[64]; 277 | size_t encryptedData_size = 0, buffer_size; 278 | struct test_encryption_aes_256_t *res; 279 | 280 | TITLE_MSG("Begin encryption test AES 256 ...") 281 | 282 | CLEAR_VECTOR(encryptedData); 283 | 284 | err = aes_256_cbc_encrypt(encryptedData, &encryptedData_size, (uint8_t *)STR_CONST(PLAIN_TEXT), (uint8_t *)IV_TEST, (uint8_t *)PRIVATE_KEY_TEST, &errMsg); 285 | 286 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 287 | CTEST_TITLE("Check (%d) errMsg is NOT NULL %p", ++testNumber, errMsg) 288 | )) 289 | 290 | C_ASSERT_TRUE(err == -48, CTEST_SETTER( 291 | CTEST_TITLE("Check if HAS ERROR = -48. Output pointer encryptedData (%p) with size = 0", encryptedData), 292 | CTEST_ON_ERROR("Was expected error = -48, but found error = %d", err), 293 | CTEST_ON_SUCCESS("Error = -48 success") 294 | )) 295 | 296 | C_ASSERT_EQUAL_STRING( 297 | "Invalid output/input data\n", 298 | errMsg, 299 | CTEST_SETTER( 300 | CTEST_TITLE("Check errMsg error description") 301 | ) 302 | ) 303 | 304 | encryptedData_size = sizeof(encryptedData); 305 | err = aes_256_cbc_encrypt(encryptedData, &encryptedData_size, (uint8_t *)PLAIN_TEXT, 0, (uint8_t *)IV_TEST, (uint8_t *)PRIVATE_KEY_TEST, &errMsg); 306 | 307 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 308 | CTEST_TITLE("Check (%d) errMsg is NOT NULL %p", ++testNumber, errMsg) 309 | )) 310 | 311 | C_ASSERT_TRUE(err == -48, CTEST_SETTER( 312 | CTEST_TITLE("Check if HAS ERROR = -48. Input pointer plain text (%p) with size = 0", PLAIN_TEXT), 313 | CTEST_ON_ERROR("Was expected error = -48, but found error = %d", err), 314 | CTEST_ON_SUCCESS("Error = -48 success") 315 | )) 316 | 317 | C_ASSERT_EQUAL_STRING( 318 | "Invalid output/input data\n", 319 | errMsg, 320 | CTEST_SETTER( 321 | CTEST_TITLE("Check errMsg error description") 322 | ) 323 | ) 324 | 325 | err = aes_256_cbc_encrypt(encryptedData, &encryptedData_size, (uint8_t *)STR_CONST(PLAIN_TEXT), (uint8_t *)IV_TEST, (uint8_t *)PRIVATE_KEY_TEST, &errMsg); 326 | 327 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 328 | CTEST_TITLE("Check (%d) errMsg is NOT NULL %p", ++testNumber, errMsg) 329 | )) 330 | 331 | C_ASSERT_TRUE(err == -49, CTEST_SETTER( 332 | CTEST_TITLE("Check if HAS ERROR = -49. Check if plain text is aligned (%p)", PLAIN_TEXT), 333 | CTEST_ON_ERROR("Was expected error = -49 (INPUT NOT ALIGNED IN MEMORY), but found error = %d", err), 334 | CTEST_ON_SUCCESS("Error = -49 success") 335 | )) 336 | 337 | C_ASSERT_EQUAL_STRING( 338 | "Plain text must be aligned\n", 339 | errMsg, 340 | CTEST_SETTER( 341 | CTEST_TITLE("Check errMsg error description must contain: NOT ALIGNED INPUT DATA DESCRIPTION") 342 | ) 343 | ) 344 | 345 | open_random(NULL); 346 | buffer = (uint8_t *)n_malloc(&buffer_size, sizeof(PLAIN_TEXT)); 347 | close_random(); 348 | 349 | C_ASSERT_NOT_NULL(buffer, CTEST_SETTER( 350 | CTEST_TITLE("Check (%d) buffer (%p) with size is %lu is NOT NULL %p", ++testNumber, buffer) 351 | )) 352 | 353 | strcpy((char *)buffer, PLAIN_TEXT); 354 | debug_dump_ascii(buffer, buffer_size); 355 | 356 | encryptedData_size = 10; 357 | 358 | err = aes_256_cbc_encrypt(encryptedData, &encryptedData_size, buffer, buffer_size, (uint8_t *)IV_TEST, (uint8_t *)PRIVATE_KEY_TEST, &errMsg); 359 | 360 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 361 | CTEST_TITLE("Check (%d) errMsg is NOT NULL %p", ++testNumber, errMsg), 362 | CTEST_ON_ERROR_CB(free, (void *)buffer) 363 | )) 364 | 365 | C_ASSERT_TRUE(err == -50, CTEST_SETTER( 366 | CTEST_TITLE("Check if HAS ERROR = -50. Check if buffer is greater or equal text plain aligned data size"), 367 | CTEST_ON_ERROR("Was expected error = -50 (BUFFER HAS NO SIZE TO STORE ENCRYPTED DATA), but found error = %d", err), 368 | CTEST_ON_SUCCESS("Error = -50 success"), 369 | CTEST_ON_ERROR_CB(free, (void *)buffer) 370 | )) 371 | 372 | C_ASSERT_EQUAL_STRING( 373 | "No space for encrypt data\n", 374 | errMsg, 375 | CTEST_SETTER( 376 | CTEST_TITLE("Check errMsg error description must contain: BUFFER HAS NO SIZE TO STORE ENCRYPTED DATA"), 377 | CTEST_ON_ERROR_CB(free, (void *)buffer) 378 | ) 379 | ) 380 | 381 | encryptedData_size = sizeof(encryptedData); 382 | err = aes_256_cbc_encrypt(encryptedData, &encryptedData_size, buffer, buffer_size, (uint8_t *)IV_TEST, (uint8_t *)PRIVATE_KEY_TEST, &errMsg); 383 | 384 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 385 | CTEST_TITLE("Check (%d) errMsg is NOT NULL %p", ++testNumber, errMsg), 386 | CTEST_ON_ERROR_CB(free, (void *)buffer) 387 | )) 388 | 389 | C_ASSERT_TRUE(err == 0, CTEST_SETTER( 390 | CTEST_TITLE("Check ENCRYPTED SUCCESS"), 391 | CTEST_ON_ERROR("Was expected error = 0 (ENCRYPT SUCCESS), but found error = %d", err), 392 | CTEST_ON_SUCCESS("Error = 0 success"), 393 | CTEST_ON_ERROR_CB(free, (void *)buffer) 394 | )) 395 | 396 | C_ASSERT_EQUAL_STRING( 397 | "Encrypt SUCCESS\n", 398 | errMsg, 399 | CTEST_SETTER( 400 | CTEST_TITLE("Check errMsg error description must contain: ENCRYPT SUCCESS"), 401 | CTEST_ON_ERROR_CB(free, (void *)buffer) 402 | ) 403 | ) 404 | 405 | C_ASSERT_TRUE(buffer_size == encryptedData_size, CTEST_SETTER( 406 | CTEST_TITLE("Check (%d) if encryptedData (%p) size = %lu has same size of plain text buffer (%p) size = %lu", 407 | ++testNumber, encryptedData, encryptedData_size, buffer, buffer_size), 408 | CTEST_ON_ERROR("encryptedData_size = %lu and buffer_size = %lu are not equals", encryptedData_size, buffer_size), 409 | CTEST_ON_SUCCESS("encryptedData_size (%lu) == buffer_size (%lu) success", encryptedData_size, buffer_size), 410 | CTEST_ON_ERROR_CB(free, (void *)buffer) 411 | )) 412 | 413 | C_ASSERT_TRUE(sizeof(encryptedData) >= buffer_size, CTEST_SETTER( 414 | CTEST_TITLE("Check (%d) if encryptedData (%p) sizeof(encryptedData) = %lu is greater or equal to encryptedData (%p) size = %lu", 415 | ++testNumber, encryptedData, sizeof(encryptedData), encryptedData, encryptedData_size), 416 | CTEST_ON_ERROR("sizeof(encryptedData) = %lu IS LESS THAN encryptedData = %lu size", sizeof(encryptedData), encryptedData_size), 417 | CTEST_ON_SUCCESS("encryptedData_size (%lu) IS GREATER OR EQUAL buffer_size (%lu) success", sizeof(encryptedData), buffer_size), 418 | CTEST_ON_ERROR_CB(free, (void *)buffer) 419 | )) 420 | 421 | INFO_MSG_FMT("ENCRYPTED PLAIN \"%s\". One page", PLAIN_TEXT) 422 | debug_dump_ascii(encryptedData, encryptedData_size); 423 | 424 | INFO_MSG_FMT("ENCRYPTED PLAIN \"%s\". Full page", PLAIN_TEXT) 425 | debug_dump_ascii(encryptedData, sizeof(encryptedData)); 426 | 427 | free((void *)buffer); 428 | 429 | if ((res = (struct test_encryption_aes_256_t *)malloc(sizeof(struct test_encryption_aes_256_t)))) { 430 | if ((res->encrypted_data = (uint8_t *)malloc(encryptedData_size))) { 431 | memcpy(res->iv, IV_TEST, sizeof(res->iv)); 432 | memcpy(res->priv_key, PRIVATE_KEY_TEST, sizeof(res->priv_key)); 433 | memcpy(res->encrypted_data, encryptedData, encryptedData_size); 434 | res->encrypted_data_size = encryptedData_size; 435 | res->message = PLAIN_TEXT; 436 | res-> message_size = sizeof(PLAIN_TEXT); 437 | 438 | return res; 439 | } 440 | 441 | free(res); 442 | } 443 | 444 | return NULL; 445 | } 446 | 447 | #undef PRIVATE_KEY_TEST 448 | #undef IV_TEST 449 | #undef PLAIN_TEXT 450 | //TEST_encryption_aes_256_destroy 451 | void TEST_decryption_aes_256(struct test_encryption_aes_256_t *ctx) 452 | { 453 | int err, testNumber = 0; 454 | uint8_t decryptedData[64]; 455 | size_t decryptedData_size = 0; 456 | char *errMsg; 457 | void *context = (void *)ctx; 458 | 459 | TITLE_MSG("Begin decryption test AES 256 ...") 460 | 461 | C_ASSERT_NOT_NULL(ctx, CTEST_SETTER( 462 | CTEST_TITLE("Check (%d) encryption context of last test is NOT NULL %p", ++testNumber, ctx), 463 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 464 | )) 465 | 466 | CLEAR_VECTOR(decryptedData); 467 | 468 | err = aes_256_cbc_decrypt( 469 | decryptedData, &decryptedData_size, ctx->encrypted_data, ctx->encrypted_data_size, ctx->iv, ctx->priv_key, &errMsg); 470 | 471 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 472 | CTEST_TITLE("Check (%d) errMsg is NOT NULL %p", ++testNumber, errMsg), 473 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 474 | )) 475 | 476 | C_ASSERT_TRUE(err == -60, CTEST_SETTER( 477 | CTEST_TITLE("Check if HAS ERROR = -60. Output pointer decryptedData (%p) with size = 0", decryptedData), 478 | CTEST_ON_ERROR("Was expected error = -60, but found error = %d", err), 479 | CTEST_ON_SUCCESS("Error = -60 success"), 480 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 481 | )) 482 | 483 | C_ASSERT_EQUAL_STRING( 484 | "Invalid output/input data\n", 485 | errMsg, 486 | CTEST_SETTER( 487 | CTEST_TITLE("Check errMsg error description"), 488 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 489 | ) 490 | ) 491 | 492 | decryptedData_size = sizeof(decryptedData); 493 | err = aes_256_cbc_decrypt( 494 | decryptedData, &decryptedData_size, ctx->encrypted_data, 0, ctx->iv, ctx->priv_key, &errMsg); 495 | 496 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 497 | CTEST_TITLE("Check (%d) errMsg is NOT NULL %p", ++testNumber, errMsg), 498 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 499 | )) 500 | 501 | C_ASSERT_TRUE(err == -60, CTEST_SETTER( 502 | CTEST_TITLE("Check if HAS ERROR = -60. Input pointer with encrypted data (%p) with size = 0", ctx->encrypted_data), 503 | CTEST_ON_ERROR("Was expected error = -60, but found error = %d", err), 504 | CTEST_ON_SUCCESS("Error = -60 success"), 505 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 506 | )) 507 | 508 | C_ASSERT_EQUAL_STRING( 509 | "Invalid output/input data\n", 510 | errMsg, 511 | CTEST_SETTER( 512 | CTEST_TITLE("Check errMsg error description"), 513 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 514 | ) 515 | ) 516 | 517 | err = aes_256_cbc_decrypt( 518 | decryptedData, &decryptedData_size, ctx->encrypted_data, 1, ctx->iv, ctx->priv_key, &errMsg); 519 | 520 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 521 | CTEST_TITLE("Check (%d) errMsg is NOT NULL %p", ++testNumber, errMsg), 522 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 523 | )) 524 | 525 | C_ASSERT_TRUE(err == -61, CTEST_SETTER( 526 | CTEST_TITLE("Check if HAS ERROR = -61. Check if encrypted data is aligned (%p)", ctx->encrypted_data), 527 | CTEST_ON_ERROR("Was expected error = -61 (DECRYPTION INPUT NOT ALIGNED IN MEMORY), but found error = %d", err), 528 | CTEST_ON_SUCCESS("Error = -61 success"), 529 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 530 | )) 531 | 532 | C_ASSERT_EQUAL_STRING( 533 | "Encrypted data must be aligned\n", 534 | errMsg, 535 | CTEST_SETTER( 536 | CTEST_TITLE("Check errMsg error description must contain: DECRYPTION INPUT NOT ALIGNED IN MEMORY"), 537 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 538 | ) 539 | ) 540 | 541 | decryptedData_size = 1; 542 | err = aes_256_cbc_decrypt( 543 | decryptedData, &decryptedData_size, ctx->encrypted_data, ctx->encrypted_data_size, ctx->iv, ctx->priv_key, &errMsg); 544 | 545 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 546 | CTEST_TITLE("Check (%d) errMsg is NOT NULL %p", ++testNumber, errMsg), 547 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 548 | )) 549 | 550 | C_ASSERT_TRUE(err == -62, CTEST_SETTER( 551 | CTEST_TITLE("Check if HAS ERROR = -62. Check if buffer size is greater or equal encrypted aligned data size"), 552 | CTEST_ON_ERROR("Was expected error = -62 (BUFFER HAS NO SIZE TO STORE DECRYPTED DATA), but found error = %d", err), 553 | CTEST_ON_SUCCESS("Error = -62 success"), 554 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 555 | )) 556 | 557 | C_ASSERT_EQUAL_STRING( 558 | "No space for decrypt data\n", 559 | errMsg, 560 | CTEST_SETTER( 561 | CTEST_TITLE("Check errMsg error description must contain: BUFFER HAS NO SIZE TO STORE DECRYPTED DATA"), 562 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 563 | ) 564 | ) 565 | 566 | INFO_MSG("DECRYPTED SCENARIO (STEP 1: Different salt).") 567 | (*ctx->iv)++; 568 | 569 | decryptedData_size = sizeof(decryptedData); 570 | err = aes_256_cbc_decrypt( 571 | decryptedData, &decryptedData_size, ctx->encrypted_data, ctx->encrypted_data_size, ctx->iv, ctx->priv_key, &errMsg); 572 | 573 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 574 | CTEST_TITLE("Check (%d) errMsg is NOT NULL %p", ++testNumber, errMsg), 575 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 576 | )) 577 | 578 | C_ASSERT_TRUE(err == 0, CTEST_SETTER( 579 | CTEST_TITLE("Check DECRYPTED SUCCESS"), 580 | CTEST_ON_ERROR("Was expected error = 0 (DECRYPTED SUCCESS), but found error = %d", err), 581 | CTEST_ON_SUCCESS("Error = 0 success"), 582 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 583 | )) 584 | 585 | C_ASSERT_EQUAL_STRING( 586 | "Decrypt SUCCESS\n", 587 | errMsg, 588 | CTEST_SETTER( 589 | CTEST_TITLE("Check errMsg error description must contain: DECRYPTED SUCCESS"), 590 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 591 | ) 592 | ) 593 | 594 | C_ASSERT_TRUE(sizeof(decryptedData) >= decryptedData_size, CTEST_SETTER( 595 | CTEST_TITLE("Check (%d) if decryptedData (%p) size = %lu is less than decrypetedData maximum buffer size = %lu", 596 | ++testNumber, decryptedData, decryptedData_size, sizeof(decryptedData)), 597 | CTEST_ON_ERROR("decryptedData_size = %lu is NOT less or equal to decrypetedData maximum buffer size %lu", decryptedData_size, sizeof(decryptedData)), 598 | CTEST_ON_SUCCESS("decryptedData_size (%lu) is less or equal to decrypetedData maximum buffer size %lu success", decryptedData_size, sizeof(decryptedData)), 599 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 600 | )) 601 | 602 | INFO_MSG("DECRYPTED PLAIN (STEP 1: Incorrect iv).") 603 | debug_dump_ascii(decryptedData, decryptedData_size); 604 | 605 | INFO_MSG("DECRYPTED PLAIN (STEP 1: Incorrect iv). Full buffer") 606 | debug_dump_ascii(decryptedData, sizeof(decryptedData)); 607 | 608 | C_ASSERT_TRUE(decryptedData_size >= ctx->message_size, CTEST_SETTER( 609 | CTEST_TITLE("Check (%d) if decryptedData (%p) size = %lu is greater or equal than original message size = %lu at pointer (%p)", 610 | ++testNumber, decryptedData, decryptedData_size, ctx->message_size, ctx->message), 611 | CTEST_ON_ERROR("decryptedData_size = %lu is NOT greater or equal than original message size = %lu", decryptedData_size, ctx->message_size), 612 | CTEST_ON_SUCCESS("decryptedData_size (%lu) is greater or equal to original message size %lu success", decryptedData_size, ctx->message_size), 613 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 614 | )) 615 | 616 | C_ASSERT_TRUE(!is_vec_content_eq((uint8_t *)ctx->message, ctx->message_size, decryptedData, ctx->message_size), CTEST_SETTER( 617 | CTEST_TITLE("Check (%d) if decryptedData (%p) is NOT equal to original message (%p)", ++testNumber, decryptedData, ctx->message), 618 | CTEST_ON_ERROR("decryptedData equal than original message size with wrong iv"), 619 | CTEST_ON_SUCCESS("Original message NOT equal to decrypted data with wrong iv. Success"), 620 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 621 | )) 622 | 623 | INFO_MSG("DECRYPTED SCENARIO (STEP 2: Different salt and wrong private key).") 624 | (*ctx->priv_key)++; 625 | 626 | decryptedData_size = sizeof(decryptedData); 627 | err = aes_256_cbc_decrypt( 628 | decryptedData, &decryptedData_size, ctx->encrypted_data, ctx->encrypted_data_size, ctx->iv, ctx->priv_key, &errMsg); 629 | 630 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 631 | CTEST_TITLE("Check (%d) errMsg is NOT NULL %p", ++testNumber, errMsg), 632 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 633 | )) 634 | 635 | C_ASSERT_TRUE(err == 0, CTEST_SETTER( 636 | CTEST_TITLE("Check DECRYPTED SUCCESS"), 637 | CTEST_ON_ERROR("Was expected error = 0 (DECRYPTED SUCCESS), but found error = %d", err), 638 | CTEST_ON_SUCCESS("Error = 0 success"), 639 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 640 | )) 641 | 642 | C_ASSERT_EQUAL_STRING( 643 | "Decrypt SUCCESS\n", 644 | errMsg, 645 | CTEST_SETTER( 646 | CTEST_TITLE("Check errMsg error description must contain: DECRYPTED SUCCESS"), 647 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 648 | ) 649 | ) 650 | 651 | C_ASSERT_TRUE(sizeof(decryptedData) >= decryptedData_size, CTEST_SETTER( 652 | CTEST_TITLE("Check (%d) if decryptedData (%p) size = %lu is less than decrypetedData maximum buffer size = %lu", 653 | ++testNumber, decryptedData, decryptedData_size, sizeof(decryptedData)), 654 | CTEST_ON_ERROR("decryptedData_size = %lu is NOT less or equal to decrypetedData maximum buffer size %lu", decryptedData_size, sizeof(decryptedData)), 655 | CTEST_ON_SUCCESS("decryptedData_size (%lu) is less or equal to decrypetedData maximum buffer size %lu success", decryptedData_size, sizeof(decryptedData)), 656 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 657 | )) 658 | 659 | INFO_MSG("DECRYPTED PLAIN (STEP 2: Incorrect iv and private key).") 660 | debug_dump_ascii(decryptedData, decryptedData_size); 661 | 662 | INFO_MSG("DECRYPTED PLAIN (STEP 2: Incorrect iv and private key). Full buffer") 663 | debug_dump_ascii(decryptedData, sizeof(decryptedData)); 664 | 665 | C_ASSERT_TRUE(decryptedData_size >= ctx->message_size, CTEST_SETTER( 666 | CTEST_TITLE("Check (%d) if decryptedData (%p) size = %lu is greater or equal than original message size = %lu at pointer (%p)", 667 | ++testNumber, decryptedData, decryptedData_size, ctx->message_size, ctx->message), 668 | CTEST_ON_ERROR("decryptedData_size = %lu is NOT greater or equal than original message size = %lu", decryptedData_size, ctx->message_size), 669 | CTEST_ON_SUCCESS("decryptedData_size (%lu) is greater or equal to original message size %lu success", decryptedData_size, ctx->message_size), 670 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 671 | )) 672 | 673 | C_ASSERT_TRUE(!is_vec_content_eq((uint8_t *)ctx->message, ctx->message_size, decryptedData, ctx->message_size), CTEST_SETTER( 674 | CTEST_TITLE("Check (%d) if decryptedData (%p) is NOT equal to original message (%p)", ++testNumber, decryptedData, ctx->message), 675 | CTEST_ON_ERROR("decryptedData equal than original message size with wrong iv and private key"), 676 | CTEST_ON_SUCCESS("Original message NOT equal to decrypted data with wrong iv and private key. Success"), 677 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 678 | )) 679 | 680 | INFO_MSG("DECRYPTED SCENARIO (STEP 3: Correct salt and private key).") 681 | (*ctx->iv)--; 682 | (*ctx->priv_key)--; 683 | 684 | decryptedData_size = sizeof(decryptedData); 685 | err = aes_256_cbc_decrypt( 686 | decryptedData, &decryptedData_size, ctx->encrypted_data, ctx->encrypted_data_size, ctx->iv, ctx->priv_key, &errMsg); 687 | 688 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 689 | CTEST_TITLE("Check (%d) errMsg is NOT NULL %p", ++testNumber, errMsg), 690 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 691 | )) 692 | 693 | C_ASSERT_TRUE(err == 0, CTEST_SETTER( 694 | CTEST_TITLE("Check DECRYPTED SUCCESS"), 695 | CTEST_ON_ERROR("Was expected error = 0 (DECRYPTED SUCCESS), but found error = %d", err), 696 | CTEST_ON_SUCCESS("Error = 0 success"), 697 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 698 | )) 699 | 700 | C_ASSERT_EQUAL_STRING( 701 | "Decrypt SUCCESS\n", 702 | errMsg, 703 | CTEST_SETTER( 704 | CTEST_TITLE("Check errMsg error description must contain: DECRYPTED SUCCESS"), 705 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 706 | ) 707 | ) 708 | 709 | C_ASSERT_TRUE(sizeof(decryptedData) >= decryptedData_size, CTEST_SETTER( 710 | CTEST_TITLE("Check (%d) if decryptedData (%p) size = %lu is less than decrypetedData maximum buffer size = %lu", 711 | ++testNumber, decryptedData, decryptedData_size, sizeof(decryptedData)), 712 | CTEST_ON_ERROR("decryptedData_size = %lu is NOT less or equal to decrypetedData maximum buffer size %lu", decryptedData_size, sizeof(decryptedData)), 713 | CTEST_ON_SUCCESS("decryptedData_size (%lu) is less or equal to decrypetedData maximum buffer size %lu success", decryptedData_size, sizeof(decryptedData)), 714 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 715 | )) 716 | 717 | INFO_MSG("DECRYPTED PLAIN (STEP 3: Correct iv and private key).") 718 | debug_dump_ascii(decryptedData, decryptedData_size); 719 | 720 | INFO_MSG("DECRYPTED PLAIN (STEP 3: Correct iv and private key). Full buffer") 721 | debug_dump_ascii(decryptedData, sizeof(decryptedData)); 722 | 723 | C_ASSERT_TRUE(decryptedData_size >= ctx->message_size, CTEST_SETTER( 724 | CTEST_TITLE("Check (%d) if decryptedData (%p) size = %lu is greater or equal than original message size = %lu at pointer (%p)", 725 | ++testNumber, decryptedData, decryptedData_size, ctx->message_size, ctx->message), 726 | CTEST_ON_ERROR("decryptedData_size = %lu is NOT greater or equal than original message size = %lu", decryptedData_size, ctx->message_size), 727 | CTEST_ON_SUCCESS("decryptedData_size (%lu) is greater or equal to original message size %lu success", decryptedData_size, ctx->message_size), 728 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 729 | )) 730 | 731 | C_ASSERT_TRUE(is_vec_content_eq((uint8_t *)ctx->message, ctx->message_size, decryptedData, ctx->message_size), CTEST_SETTER( 732 | CTEST_TITLE("Check (%d) if decryptedData (%p) contains original message (%p)", ++testNumber, decryptedData, ctx->message), 733 | CTEST_ON_ERROR("decryptedData does NOT contain original message"), 734 | CTEST_ON_SUCCESS("Original message \"%s\" at pointer %p is contained in decrypted data \"%s\" at pointer %p are equals. Success", 735 | ctx->message, ctx->message, (char *)decryptedData, decryptedData), 736 | CTEST_ON_ERROR_CB(TEST_encryption_aes_256_destroy, (void *)&context) 737 | )) 738 | 739 | TEST_encryption_aes_256_destroy((void *)&context); 740 | } 741 | 742 | void TEST_password_strength() 743 | { 744 | #define MIN_PASS 8 745 | #define MAX_PASS 20 746 | #define N_PASS (MAX_PASS + 2) 747 | int err, must_have = PASS_MUST_HAVE_AT_LEAST_NONE; 748 | 749 | char *password; 750 | size_t password_sz; 751 | 752 | TITLE_MSG("Begin Test of password strength") 753 | 754 | err = pass_must_have_at_least(&password_sz, NULL, N_PASS, MIN_PASS, MAX_PASS, must_have); 755 | 756 | C_ASSERT_TRUE(err == PASS_IS_NULL, CTEST_SETTER( 757 | CTEST_TITLE("Check if password is valid (NOT NULL)"), 758 | CTEST_ON_ERROR("Was expected error = PASS_IS_NULL (%d), but found error = %d", PASS_IS_NULL, err), 759 | CTEST_ON_SUCCESS("Success error = PASS_IS_NULL (%d) for password NULL", PASS_IS_NULL) 760 | )) 761 | 762 | err = pass_must_have_at_least(&password_sz, "weak", N_PASS, MIN_PASS, MAX_PASS, must_have); 763 | 764 | C_ASSERT_TRUE(err == PASS_IS_TOO_SHORT, CTEST_SETTER( 765 | CTEST_TITLE("Check weak password with length < MIN_PASS = %d", MIN_PASS), 766 | CTEST_ON_ERROR("Was expected error = PASS_IS_TOO_SHORT (%d), but found error = %d", PASS_IS_TOO_SHORT, err), 767 | CTEST_ON_SUCCESS("Success error = PASS_IS_TOO_SHORT (%d) for password length = %lu < MIN_PASS = %d", PASS_IS_TOO_SHORT, password_sz, MIN_PASS) 768 | )) 769 | 770 | err = pass_must_have_at_least(&password_sz, "This password is too long, thus error = PASS_IS_OUT_OVF will occur", N_PASS, MIN_PASS, MAX_PASS, must_have); 771 | 772 | C_ASSERT_TRUE(err == PASS_IS_OUT_OVF, CTEST_SETTER( 773 | CTEST_TITLE("Check password with length >= N_PASS = %d", N_PASS), 774 | CTEST_ON_ERROR("Was expected error = PASS_IS_OUT_OVF (%d), but found error = %d", PASS_IS_OUT_OVF, err), 775 | CTEST_ON_SUCCESS("Success error = PASS_IS_OUT_OVF (%d) for password length", PASS_IS_OUT_OVF) 776 | )) 777 | 778 | err = pass_must_have_at_least(&password_sz, "This password is ok??", N_PASS, MIN_PASS, MAX_PASS, must_have); 779 | 780 | C_ASSERT_TRUE(err == PASS_IS_TOO_LONG, CTEST_SETTER( 781 | CTEST_TITLE("Check password with length (%lu) > MAX_PASS = %d", password_sz, MAX_PASS), 782 | CTEST_ON_ERROR("Was expected error = PASS_IS_TOO_LONG (%d), but found error = %d", PASS_IS_TOO_LONG, err), 783 | CTEST_ON_SUCCESS("Success error = PASS_IS_TOO_LONG (%d) for password length = %lu > MAX_PASS = %d ", PASS_IS_TOO_LONG, password_sz, MAX_PASS) 784 | )) 785 | 786 | err = pass_must_have_at_least(&password_sz, "this password is ok.", N_PASS, MIN_PASS, MAX_PASS, must_have); 787 | 788 | C_ASSERT_TRUE(err == 0, CTEST_SETTER( 789 | CTEST_TITLE("Check password with length (%lu) is OK", password_sz), 790 | CTEST_ON_ERROR("Was expected password OK (error = 0), but found error = %d", err), 791 | CTEST_ON_SUCCESS("Success error = 0. Password is ok for PASS_MUST_HAVE_AT_LEAST_NONE = %d", PASS_MUST_HAVE_AT_LEAST_NONE) 792 | )) 793 | 794 | password = "THIS PASSWORD IS OK."; 795 | err = pass_must_have_at_least(&password_sz, password, N_PASS, MIN_PASS, MAX_PASS, PASS_MUST_HAVE_AT_LEAST_ONE_LOWER_CASE); 796 | 797 | C_ASSERT_TRUE(err == PASS_MUST_HAVE_AT_LEAST_ONE_LOWER_CASE, CTEST_SETTER( 798 | CTEST_TITLE("Check password (\"%s\") with length (%lu) PASS_MUST_HAVE_AT_LEAST_ONE_LOWER_CASE (%d)", 799 | password, password_sz, PASS_MUST_HAVE_AT_LEAST_ONE_LOWER_CASE), 800 | CTEST_ON_ERROR("Was expected password OK (error = PASS_MUST_HAVE_AT_LEAST_ONE_LOWER_CASE (%d)), but found error = %d", 801 | PASS_MUST_HAVE_AT_LEAST_ONE_LOWER_CASE, err), 802 | CTEST_ON_SUCCESS("Success error = PASS_MUST_HAVE_AT_LEAST_ONE_LOWER_CASE (%d). Password NOT ok for PASS_MUST_HAVE_AT_LEAST_ONE_LOWER_CASE", 803 | PASS_MUST_HAVE_AT_LEAST_ONE_LOWER_CASE) 804 | )) 805 | 806 | password = "THIS PASSWORD IS Ok."; 807 | err = pass_must_have_at_least(&password_sz, password, N_PASS, MIN_PASS, MAX_PASS, PASS_MUST_HAVE_AT_LEAST_ONE_LOWER_CASE); 808 | 809 | C_ASSERT_TRUE(err == 0, CTEST_SETTER( 810 | CTEST_TITLE("Check password (\"%s\") with length (%lu) PASS_MUST_HAVE_AT_LEAST_ONE_LOWER_CASE (%d) must pass", 811 | password, password_sz, PASS_MUST_HAVE_AT_LEAST_ONE_LOWER_CASE), 812 | CTEST_ON_ERROR("Was expected password OK (error = 0), but found error = %d", err), 813 | CTEST_ON_SUCCESS("Success error = 0. Password (\"%s\") contains at least one lower case ", password) 814 | )) 815 | 816 | password = "this password is ok."; 817 | err = pass_must_have_at_least(&password_sz, password, N_PASS, MIN_PASS, MAX_PASS, PASS_MUST_HAVE_AT_LEAST_ONE_UPPER_CASE); 818 | 819 | C_ASSERT_TRUE(err == PASS_MUST_HAVE_AT_LEAST_ONE_UPPER_CASE, CTEST_SETTER( 820 | CTEST_TITLE("Check password (\"%s\") with length (%lu) PASS_MUST_HAVE_AT_LEAST_ONE_UPPER_CASE (%d) must NOT pass", 821 | password, password_sz, PASS_MUST_HAVE_AT_LEAST_ONE_UPPER_CASE), 822 | CTEST_ON_ERROR("Was expected password (error = PASS_MUST_HAVE_AT_LEAST_ONE_UPPER_CASE (%d)), but found error = %d", 823 | PASS_MUST_HAVE_AT_LEAST_ONE_UPPER_CASE, err), 824 | CTEST_ON_SUCCESS("Success error = PASS_MUST_HAVE_AT_LEAST_ONE_UPPER_CASE (%d). Password (\"%s\") NOT contains at least one upper case ", 825 | PASS_MUST_HAVE_AT_LEAST_ONE_UPPER_CASE, password) 826 | )) 827 | 828 | password = "This password is ok."; 829 | err = pass_must_have_at_least(&password_sz, password, N_PASS, MIN_PASS, MAX_PASS, PASS_MUST_HAVE_AT_LEAST_ONE_UPPER_CASE); 830 | 831 | C_ASSERT_TRUE(err == 0, CTEST_SETTER( 832 | CTEST_TITLE("Check password (\"%s\") with length (%lu) PASS_MUST_HAVE_AT_LEAST_ONE_UPPER_CASE (%d) must pass", 833 | password, password_sz, PASS_MUST_HAVE_AT_LEAST_ONE_UPPER_CASE), 834 | CTEST_ON_ERROR("Was expected password OK (error = 0), but found error = %d", err), 835 | CTEST_ON_SUCCESS("Success error = 0. Password (\"%s\")contains at least one upper case ", password) 836 | )) 837 | 838 | password = "This password is ok."; 839 | err = pass_must_have_at_least(&password_sz, password, N_PASS, MIN_PASS, MAX_PASS, PASS_MUST_HAVE_AT_LEAST_ONE_NUMBER); 840 | 841 | C_ASSERT_TRUE(err == PASS_MUST_HAVE_AT_LEAST_ONE_NUMBER, CTEST_SETTER( 842 | CTEST_TITLE("Check password (\"%s\") with length (%lu) PASS_MUST_HAVE_AT_LEAST_ONE_NUMBER (%d) must NOT pass", 843 | password, password_sz, PASS_MUST_HAVE_AT_LEAST_ONE_NUMBER), 844 | CTEST_ON_ERROR("Was expected password (error = PASS_MUST_HAVE_AT_LEAST_ONE_NUMBER (%d)), but found error = %d", PASS_MUST_HAVE_AT_LEAST_ONE_NUMBER, err), 845 | CTEST_ON_SUCCESS("Success error = PASS_MUST_HAVE_AT_LEAST_ONE_NUMBER (%d). Password (\"%s\") NOT contains at least one number", 846 | PASS_MUST_HAVE_AT_LEAST_ONE_NUMBER, password) 847 | )) 848 | 849 | password = "This password is ok9"; 850 | err = pass_must_have_at_least(&password_sz, password, N_PASS, MIN_PASS, MAX_PASS, PASS_MUST_HAVE_AT_LEAST_ONE_NUMBER); 851 | 852 | C_ASSERT_TRUE(err == 0, CTEST_SETTER( 853 | CTEST_TITLE("Check password (\"%s\") with length (%lu) PASS_MUST_HAVE_AT_LEAST_ONE_NUMBER (%d) must pass", 854 | password, password_sz, PASS_MUST_HAVE_AT_LEAST_ONE_NUMBER), 855 | CTEST_ON_ERROR("Was expected password OK (error = 0), but found error = %d", err), 856 | CTEST_ON_SUCCESS("Success error = 0. Password (\"%s\") contains at least one number", password) 857 | )) 858 | 859 | password = "This password is ok"; 860 | err = pass_must_have_at_least(&password_sz, password, N_PASS, MIN_PASS, MAX_PASS, PASS_MUST_HAVE_AT_LEAST_ONE_SYMBOL); 861 | 862 | C_ASSERT_TRUE(err == PASS_MUST_HAVE_AT_LEAST_ONE_SYMBOL, CTEST_SETTER( 863 | CTEST_TITLE("Check password (\"%s\") with length (%lu) PASS_MUST_HAVE_AT_LEAST_ONE_SYMBOL (%d) must NOT pass", 864 | password, password_sz, PASS_MUST_HAVE_AT_LEAST_ONE_SYMBOL), 865 | CTEST_ON_ERROR("Was expected password (error = PASS_MUST_HAVE_AT_LEAST_ONE_SYMBOL (%d)), but found error = %d", PASS_MUST_HAVE_AT_LEAST_ONE_SYMBOL, err), 866 | CTEST_ON_SUCCESS("Success error = PASS_MUST_HAVE_AT_LEAST_ONE_SYMBOL (%d). Password (\"%s\") contains at least one symbol", 867 | PASS_MUST_HAVE_AT_LEAST_ONE_SYMBOL, password) 868 | )) 869 | 870 | password = "This password is ok#"; 871 | err = pass_must_have_at_least(&password_sz, password, N_PASS, MIN_PASS, MAX_PASS, PASS_MUST_HAVE_AT_LEAST_ONE_SYMBOL); 872 | 873 | C_ASSERT_TRUE(err == 0, CTEST_SETTER( 874 | CTEST_TITLE("Check password (\"%s\") with length (%lu) PASS_MUST_HAVE_AT_LEAST_ONE_SYMBOL (%d) must pass", 875 | password, password_sz, PASS_MUST_HAVE_AT_LEAST_ONE_SYMBOL), 876 | CTEST_ON_ERROR("Was expected password OK (error = 0), but found error = %d", err), 877 | CTEST_ON_SUCCESS("Success error = 0. Password (\"%s\") contains at least one symbol", password) 878 | )) 879 | 880 | password = "this password is ok"; 881 | must_have = PASS_MUST_HAVE_AT_LEAST_ONE_SYMBOL|PASS_MUST_HAVE_AT_LEAST_ONE_NUMBER|PASS_MUST_HAVE_AT_LEAST_ONE_UPPER_CASE|PASS_MUST_HAVE_AT_LEAST_ONE_LOWER_CASE; 882 | err = pass_must_have_at_least(&password_sz, password, N_PASS, MIN_PASS, MAX_PASS, must_have); 883 | 884 | #define MISSING_ONES (PASS_MUST_HAVE_AT_LEAST_ONE_SYMBOL|PASS_MUST_HAVE_AT_LEAST_ONE_NUMBER|PASS_MUST_HAVE_AT_LEAST_ONE_UPPER_CASE) 885 | C_ASSERT_TRUE(err == MISSING_ONES, CTEST_SETTER( 886 | CTEST_TITLE("Check password (\"%s\") with length (%lu) has missing |upper case|number|symbol| (%d) must NOT pass", 887 | password, password_sz, MISSING_ONES), 888 | CTEST_ON_ERROR("Was expected password (error = %d) missing |upper case|number|symbol|, but found error = %d", MISSING_ONES, err), 889 | CTEST_ON_SUCCESS("Success error = %d. Password (\"%s\") NOT contains at least one missing |upper case|number| symbol|", MISSING_ONES, password) 890 | )) 891 | #undef MISSING_ONES 892 | 893 | password = "THIS PASSWORD IS OK"; 894 | must_have = PASS_MUST_HAVE_AT_LEAST_ONE_SYMBOL|PASS_MUST_HAVE_AT_LEAST_ONE_NUMBER|PASS_MUST_HAVE_AT_LEAST_ONE_UPPER_CASE|PASS_MUST_HAVE_AT_LEAST_ONE_LOWER_CASE; 895 | err = pass_must_have_at_least(&password_sz, password, N_PASS, MIN_PASS, MAX_PASS, must_have); 896 | 897 | #define MISSING_ONES (PASS_MUST_HAVE_AT_LEAST_ONE_SYMBOL|PASS_MUST_HAVE_AT_LEAST_ONE_NUMBER|PASS_MUST_HAVE_AT_LEAST_ONE_LOWER_CASE) 898 | C_ASSERT_TRUE(err == MISSING_ONES, CTEST_SETTER( 899 | CTEST_TITLE("Check password (\"%s\") with length (%lu) has missing |lower case|number|symbol| (%d) must NOT pass", 900 | password, password_sz, MISSING_ONES), 901 | CTEST_ON_ERROR("Was expected password (error = %d) missing |lower case|number|symbol|, but found error = %d", MISSING_ONES, err), 902 | CTEST_ON_SUCCESS("Success error = %d. Password (\"%s\") NOT contains at least one missing |lower case|number| symbol|", MISSING_ONES, password) 903 | )) 904 | #undef MISSING_ONES 905 | 906 | password = "THIS pass Is Ok #1"; 907 | must_have = PASS_MUST_HAVE_AT_LEAST_ONE_SYMBOL|PASS_MUST_HAVE_AT_LEAST_ONE_NUMBER|PASS_MUST_HAVE_AT_LEAST_ONE_UPPER_CASE|PASS_MUST_HAVE_AT_LEAST_ONE_LOWER_CASE; 908 | err = pass_must_have_at_least(&password_sz, password, N_PASS, MIN_PASS, MAX_PASS, must_have); 909 | 910 | C_ASSERT_TRUE(err == 0, CTEST_SETTER( 911 | CTEST_TITLE("Check password (\"%s\") with length (%lu) has at least one |upper case|lower case|number|symbol| must pass", password, password_sz), 912 | CTEST_ON_ERROR("Was expected password (error = 0), but found error = %d", err), 913 | CTEST_ON_SUCCESS("Success error = 0. Password (\"%s\") contains at least one |upper case|lower case|number|symbol|", password) 914 | )) 915 | 916 | #undef N_PASS 917 | #undef MAX_PASS 918 | #undef MIN_PASS 919 | } 920 | 921 | void TEST_pbkdf2() 922 | { 923 | int err, testNumber = 0; 924 | uint8_t 925 | out[32], 926 | salt[32], 927 | out2[32], 928 | salt2[32]; 929 | char *errMsg; 930 | 931 | #define PBKDF2_ITER INTERACTION 932 | #define PBKDF2_PASS "Password" 933 | TITLE_MSG_FMT("Begin Test of PBKDF2 algorithm with interaction %lu ...", PBKDF2_ITER) 934 | 935 | CLEAR_VECTOR(out) 936 | CLEAR_VECTOR(salt) 937 | CLEAR_VECTOR(out2) 938 | CLEAR_VECTOR(salt2) 939 | 940 | err = pbkdf2(VEC_CONST(out), NULL, 0, VEC_CONST(salt), PBKDF2_ITER, &errMsg); 941 | 942 | C_ASSERT_TRUE(err == -30, CTEST_SETTER( 943 | CTEST_TITLE("Check (%d) PBKDF2 function to return error given NULL password", ++testNumber), 944 | CTEST_ON_ERROR("Was expected password (error = -30), but found error = %d", err), 945 | CTEST_ON_SUCCESS("Success error = -30") 946 | )) 947 | 948 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 949 | CTEST_TITLE("Check (%d) errMsg is NOT NULL %p", ++testNumber, errMsg), 950 | CTEST_ON_ERROR("Was expected errMsg pointer NOT NULL, but is NULL"), 951 | CTEST_ON_SUCCESS("Success errMsg (%p) NOT NULL", errMsg) 952 | )) 953 | 954 | C_ASSERT_EQUAL_STRING( 955 | "Null password\n", 956 | errMsg, 957 | CTEST_SETTER( 958 | CTEST_TITLE("Check errMsg has correct description"), 959 | CTEST_ON_ERROR("Wrong correct message errMsg = \"%s\"", errMsg), 960 | CTEST_ON_SUCCESS("Success errMsg = \"%s\"", errMsg) 961 | ) 962 | ) 963 | 964 | err = pbkdf2(VEC_CONST(out), STR_CONST(""), VEC_CONST(salt), PBKDF2_ITER, &errMsg); 965 | 966 | C_ASSERT_TRUE(err == -31, CTEST_SETTER( 967 | CTEST_TITLE("Check (%d) PBKDF2 function to return error given empty password", ++testNumber), 968 | CTEST_ON_ERROR("Was expected empty password (error = -31), but found error = %d", err), 969 | CTEST_ON_SUCCESS("Success error = -31") 970 | )) 971 | 972 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 973 | CTEST_TITLE("Check (%d) errMsg is NOT NULL %p", ++testNumber, errMsg), 974 | CTEST_ON_ERROR("Was expected errMsg pointer NOT NULL, but is NULL"), 975 | CTEST_ON_SUCCESS("Success errMsg (%p) NOT NULL", errMsg) 976 | )) 977 | 978 | C_ASSERT_EQUAL_STRING( 979 | "Empty password\n", 980 | errMsg, 981 | CTEST_SETTER( 982 | CTEST_TITLE("Check errMsg has correct description"), 983 | CTEST_ON_ERROR("Wrong correct message errMsg = \"%s\"", errMsg), 984 | CTEST_ON_SUCCESS("Success errMsg = \"%s\"", errMsg) 985 | ) 986 | ) 987 | 988 | err = pbkdf2(VEC_CONST(out), STR_CONST(PBKDF2_PASS), VEC_CONST(salt), PBKDF2_ITER, &errMsg); 989 | 990 | C_ASSERT_TRUE(err == 0, CTEST_SETTER( 991 | CTEST_TITLE("Check (%d) PBKDF2 function to return error = 0 given password \""PBKDF2_PASS"\"", ++testNumber), 992 | CTEST_ON_ERROR("Was expected password success (error = 0), but found error = %d", err), 993 | CTEST_ON_SUCCESS("Success error = 0") 994 | )) 995 | 996 | C_ASSERT_FALSE(is_vec_null(VEC_CONST(out)), CTEST_SETTER( 997 | CTEST_TITLE("Check (%d) out vector (%p) with size %lu is VECTOR NULL", ++testNumber, VEC_CONST(out)), 998 | CTEST_ON_ERROR("Was expected VECTOR NOT NULL"), 999 | CTEST_ON_SUCCESS("Success VECTOR at (%p) with size %lu is NOT NULL", VEC_CONST(out)) 1000 | )) 1001 | 1002 | debug_dump(VEC_CONST(out)); 1003 | 1004 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 1005 | CTEST_TITLE("Check (%d) errMsg is NOT NULL %p", ++testNumber, errMsg), 1006 | CTEST_ON_ERROR("Was expected errMsg pointer NOT NULL, but is NULL"), 1007 | CTEST_ON_SUCCESS("Success errMsg (%p) NOT NULL", errMsg) 1008 | )) 1009 | 1010 | C_ASSERT_EQUAL_STRING( 1011 | "PBKDF2-SHA256 success\n", 1012 | errMsg, 1013 | CTEST_SETTER( 1014 | CTEST_TITLE("Check errMsg has correct description"), 1015 | CTEST_ON_ERROR("Wrong correct message errMsg = \"%s\"", errMsg), 1016 | CTEST_ON_SUCCESS("Success errMsg = \"%s\"", errMsg) 1017 | ) 1018 | ) 1019 | 1020 | salt2[0] = 1; 1021 | err = pbkdf2(VEC_CONST(out2), STR_CONST(PBKDF2_PASS), VEC_CONST(salt2), PBKDF2_ITER, &errMsg); 1022 | 1023 | C_ASSERT_TRUE(err == 0, CTEST_SETTER( 1024 | CTEST_TITLE("Check (%d) PBKDF2 function to return error = 0 given password \""PBKDF2_PASS"\" with SALT NON NULL", ++testNumber), 1025 | CTEST_ON_ERROR("Was expected password success (error = 0), but found error = %d", err), 1026 | CTEST_ON_SUCCESS("Success error = 0") 1027 | )) 1028 | 1029 | C_ASSERT_FALSE(is_vec_null(VEC_CONST(out2)), CTEST_SETTER( 1030 | CTEST_TITLE("Check (%d) out2 vector (%p) with size %lu is VECTOR NULL", ++testNumber, VEC_CONST(out2)), 1031 | CTEST_ON_ERROR("Was expected VECTOR NOT NULL"), 1032 | CTEST_ON_SUCCESS("Success VECTOR at (%p) with size %lu is NOT NULL", VEC_CONST(out2)) 1033 | )) 1034 | 1035 | debug_dump(VEC_CONST(out2)); 1036 | 1037 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 1038 | CTEST_TITLE("Check (%d) errMsg is NOT NULL %p", ++testNumber, errMsg), 1039 | CTEST_ON_ERROR("Was expected errMsg pointer NOT NULL, but is NULL"), 1040 | CTEST_ON_SUCCESS("Success errMsg (%p) NOT NULL", errMsg) 1041 | )) 1042 | 1043 | C_ASSERT_EQUAL_STRING( 1044 | "PBKDF2-SHA256 success\n", 1045 | errMsg, 1046 | CTEST_SETTER( 1047 | CTEST_TITLE("Check errMsg has correct description"), 1048 | CTEST_ON_ERROR("Wrong correct message errMsg = \"%s\"", errMsg), 1049 | CTEST_ON_SUCCESS("Success errMsg = \"%s\"", errMsg) 1050 | ) 1051 | ) 1052 | 1053 | C_ASSERT_FALSE(is_vec_content_eq(VEC_CONST(out2), VEC_CONST(out)), CTEST_SETTER( 1054 | CTEST_TITLE("Check (%d) out2 vector (%p) with size %lu is NOT EQUAL TO out (%p) vector with size %lu", ++testNumber, VEC_CONST(out2), VEC_CONST(out)), 1055 | CTEST_ON_ERROR("Was expected vector out (%p) %lu with out2 (%p) %lu to BE NOT EQUAL", VEC_CONST(out2), VEC_CONST(out)), 1056 | CTEST_ON_SUCCESS("Expected vector out (%p) %lu with out2 (%p) %lu to IS NOT EQUAL", VEC_CONST(out2), VEC_CONST(out)) 1057 | )) 1058 | 1059 | INFO_MSG_FMT("out2 (%p) vector with size %lu", VEC_CONST(out2)) 1060 | debug_dump(VEC_CONST(out2)); 1061 | 1062 | INFO_MSG_FMT("out (%p) vector with size %lu", VEC_CONST(out)) 1063 | debug_dump(VEC_CONST(out)); 1064 | 1065 | C_ASSERT_TRUE(is_vec_null(VEC_CONST(salt)), CTEST_SETTER( 1066 | CTEST_TITLE("Check (%d) salt vector (%p) with size %lu is VECTOR NULL", ++testNumber, VEC_CONST(salt)), 1067 | CTEST_ON_ERROR("Was expected salt VECTOR NOT NULL"), 1068 | CTEST_ON_SUCCESS("Success salt VECTOR at (%p) with size %lu is NULL", VEC_CONST(salt)) 1069 | )) 1070 | 1071 | err = pbkdf2(VEC_CONST(out2), STR_CONST(PBKDF2_PASS"ABC"), VEC_CONST(salt), PBKDF2_ITER, &errMsg); 1072 | 1073 | C_ASSERT_TRUE(err == 0, CTEST_SETTER( 1074 | CTEST_TITLE("Check (%d) PBKDF2 function to return error = 0 given password \""PBKDF2_PASS"ABC\" with SALT NULL", ++testNumber), 1075 | CTEST_ON_ERROR("Was expected password success (error = 0), but found error = %d", err), 1076 | CTEST_ON_SUCCESS("Success error = 0") 1077 | )) 1078 | 1079 | C_ASSERT_FALSE(is_vec_null(VEC_CONST(out2)), CTEST_SETTER( 1080 | CTEST_TITLE("Check (%d) out2 vector (%p) with size %lu is VECTOR NULL", ++testNumber, VEC_CONST(out2)), 1081 | CTEST_ON_ERROR("Was expected VECTOR NOT NULL"), 1082 | CTEST_ON_SUCCESS("Success VECTOR at (%p) with size %lu is NOT NULL", VEC_CONST(out2)) 1083 | )) 1084 | 1085 | debug_dump(VEC_CONST(out2)); 1086 | 1087 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 1088 | CTEST_TITLE("Check (%d) errMsg is NOT NULL %p", ++testNumber, errMsg), 1089 | CTEST_ON_ERROR("Was expected errMsg pointer NOT NULL, but is NULL"), 1090 | CTEST_ON_SUCCESS("Success errMsg (%p) NOT NULL", errMsg) 1091 | )) 1092 | 1093 | C_ASSERT_EQUAL_STRING( 1094 | "PBKDF2-SHA256 success\n", 1095 | errMsg, 1096 | CTEST_SETTER( 1097 | CTEST_TITLE("Check errMsg has correct description"), 1098 | CTEST_ON_ERROR("Wrong correct message errMsg = \"%s\"", errMsg), 1099 | CTEST_ON_SUCCESS("Success errMsg = \"%s\"", errMsg) 1100 | ) 1101 | ) 1102 | 1103 | C_ASSERT_FALSE(is_vec_content_eq(VEC_CONST(out2), VEC_CONST(out)), CTEST_SETTER( 1104 | CTEST_TITLE("Check (%d) out2 vector (%p) with size %lu is NOT EQUAL TO out (%p) vector with size %lu for different passwords", 1105 | ++testNumber, VEC_CONST(out2), VEC_CONST(out)), 1106 | CTEST_ON_ERROR("Was expected vector out (%p) %lu with out2 (%p) %lu to BE NOT EQUAL", VEC_CONST(out2), VEC_CONST(out)), 1107 | CTEST_ON_SUCCESS("Expected vector out (%p) %lu with out2 (%p) %lu to IS NOT EQUAL", VEC_CONST(out2), VEC_CONST(out)) 1108 | )) 1109 | 1110 | INFO_MSG_FMT("out2 (%p) vector with size %lu for PASSWORD=\""PBKDF2_PASS"ABC\"", VEC_CONST(out2)) 1111 | debug_dump(VEC_CONST(out2)); 1112 | 1113 | INFO_MSG_FMT("with salt (%p) vector with size %lu", VEC_CONST(salt)) 1114 | debug_dump(VEC_CONST(salt)); 1115 | 1116 | INFO_MSG_FMT("out (%p) vector with size %lu for PASSWORD=\""PBKDF2_PASS"\"", VEC_CONST(out)) 1117 | debug_dump(VEC_CONST(out)); 1118 | 1119 | INFO_MSG_FMT("with salt (%p) vector with size %lu", VEC_CONST(salt)) 1120 | debug_dump(VEC_CONST(salt)); 1121 | 1122 | err = pbkdf2(VEC_CONST(out2), STR_CONST(PBKDF2_PASS), VEC_CONST(salt), (PBKDF2_ITER - 1), &errMsg); 1123 | 1124 | C_ASSERT_TRUE(err == 0, CTEST_SETTER( 1125 | CTEST_TITLE("Check (%d) PBKDF2 function to return error = 0 given password \""PBKDF2_PASS"\" with SALT NON NULL and PBKDF2_ITER1 != PBKDF2_ITER2", 1126 | ++testNumber), 1127 | CTEST_ON_ERROR("Was expected password success (error = 0), but found error = %d", err), 1128 | CTEST_ON_SUCCESS("Success error = 0") 1129 | )) 1130 | 1131 | C_ASSERT_FALSE(is_vec_null(VEC_CONST(out2)), CTEST_SETTER( 1132 | CTEST_TITLE("Check (%d) out2 vector (%p) with size %lu is VECTOR NULL", ++testNumber, VEC_CONST(out2)), 1133 | CTEST_ON_ERROR("Was expected VECTOR NOT NULL"), 1134 | CTEST_ON_SUCCESS("Success VECTOR at (%p) with size %lu is NOT NULL", VEC_CONST(out2)) 1135 | )) 1136 | 1137 | debug_dump(VEC_CONST(out2)); 1138 | 1139 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 1140 | CTEST_TITLE("Check (%d) errMsg is NOT NULL %p", ++testNumber, errMsg), 1141 | CTEST_ON_ERROR("Was expected errMsg pointer NOT NULL, but is NULL"), 1142 | CTEST_ON_SUCCESS("Success errMsg (%p) NOT NULL", errMsg) 1143 | )) 1144 | 1145 | C_ASSERT_EQUAL_STRING( 1146 | "PBKDF2-SHA256 success\n", 1147 | errMsg, 1148 | CTEST_SETTER( 1149 | CTEST_TITLE("Check errMsg has correct description"), 1150 | CTEST_ON_ERROR("Wrong correct message errMsg = \"%s\"", errMsg), 1151 | CTEST_ON_SUCCESS("Success errMsg = \"%s\"", errMsg) 1152 | ) 1153 | ) 1154 | 1155 | C_ASSERT_FALSE(is_vec_content_eq(VEC_CONST(out2), VEC_CONST(out)), CTEST_SETTER( 1156 | CTEST_TITLE("Check (%d) out2 vector (%p) with size %lu is NOT EQUAL TO out (%p) vector with size %lu for different passwords", 1157 | ++testNumber, VEC_CONST(out2), VEC_CONST(out)), 1158 | CTEST_ON_ERROR("Was expected vector out (%p) %lu with out2 (%p) %lu to BE NOT EQUAL", VEC_CONST(out2), VEC_CONST(out)), 1159 | CTEST_ON_SUCCESS("Expected vector out (%p) %lu with out2 (%p) %lu to IS NOT EQUAL", VEC_CONST(out2), VEC_CONST(out)) 1160 | )) 1161 | 1162 | INFO_MSG_FMT("out2 (%p) vector with size %lu for PASSWORD=\""PBKDF2_PASS"\" with PBKDF2_ITER1 != PBKDF2_ITER2", VEC_CONST(out2)) 1163 | debug_dump(VEC_CONST(out2)); 1164 | 1165 | INFO_MSG_FMT("with salt (%p) vector with size %lu", VEC_CONST(salt)) 1166 | debug_dump(VEC_CONST(salt)); 1167 | 1168 | INFO_MSG_FMT("out (%p) vector with size %lu for PASSWORD=\""PBKDF2_PASS"\" with PBKDF2_ITER1 != PBKDF2_ITER2", VEC_CONST(out)) 1169 | debug_dump(VEC_CONST(out)); 1170 | 1171 | INFO_MSG_FMT("with salt (%p) vector with size %lu", VEC_CONST(salt)) 1172 | debug_dump(VEC_CONST(salt)); 1173 | 1174 | #undef PBKDF2_PASS 1175 | #undef PBKDF2_ITER 1176 | } 1177 | 1178 | void TEST_argon2id() 1179 | { 1180 | int err, testNumber = 0; 1181 | uint8_t 1182 | out[32], 1183 | salt[32], 1184 | out2[32], 1185 | salt2[32]; 1186 | char *errMsg; 1187 | 1188 | #define TEST_MEM_COST getArgon2idMemCost() 1189 | #define TEST_INTERACTION_COST getArgon2idInteractionCost() 1190 | #define TEST_PARALLEL_COST getArgon2idParallelCost() 1191 | #define TEST_ARGON2ID_PASS "Argon2idPassword" 1192 | #define TEST_INTERACTION_COST2 TEST_INTERACTION_COST-1 1193 | #define TEST_MEM_COST2 TEST_MEM_COST-1 1194 | #define TEST_PARALLEL_COST2 TEST_PARALLEL_COST-1 1195 | #define ADDITIONAL_DATA "Additional data to increase complexity" 1196 | #define ADDITIONAL_SECRET "Additional Secret To Increase Complexity" 1197 | 1198 | TITLE_MSG_FMT("Begin Test of ARGON2ID algorithm with:\n\n\tMem. Cost: %u\n\tInteraction Cost: %u\n\tParallel cost: %d\n", 1199 | TEST_MEM_COST, TEST_INTERACTION_COST, TEST_PARALLEL_COST) 1200 | 1201 | CLEAR_VECTOR(out) 1202 | CLEAR_VECTOR(salt) 1203 | CLEAR_VECTOR(out2) 1204 | CLEAR_VECTOR(salt2) 1205 | 1206 | err = argon2id(VEC_CONST(out), NULL, 0, VEC_CONST(salt), NULL, 0, NULL, 0, TEST_MEM_COST, TEST_INTERACTION_COST, TEST_PARALLEL_COST, &errMsg); 1207 | 1208 | C_ASSERT_TRUE(err == -40, CTEST_SETTER( 1209 | CTEST_TITLE("Check (%d) ARGON2ID function to return error given NULL password", ++testNumber), 1210 | CTEST_ON_ERROR("Was expected password (error = -40), but found error = %d", err), 1211 | CTEST_ON_SUCCESS("Success error = -40") 1212 | )) 1213 | 1214 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 1215 | CTEST_TITLE("Check (%d) errMsg is NOT NULL %p", ++testNumber, errMsg), 1216 | CTEST_ON_ERROR("Was expected errMsg pointer NOT NULL, but is NULL"), 1217 | CTEST_ON_SUCCESS("Success errMsg (%p) NOT NULL", errMsg) 1218 | )) 1219 | 1220 | C_ASSERT_EQUAL_STRING( 1221 | "Null password\n", 1222 | errMsg, 1223 | CTEST_SETTER( 1224 | CTEST_TITLE("Check errMsg has correct description"), 1225 | CTEST_ON_ERROR("Wrong correct message errMsg = \"%s\"", errMsg), 1226 | CTEST_ON_SUCCESS("Success errMsg = \"%s\"", errMsg) 1227 | ) 1228 | ) 1229 | 1230 | err = argon2id(VEC_CONST(out), "", 0, VEC_CONST(salt), NULL, 0, NULL, 0, TEST_MEM_COST, TEST_INTERACTION_COST, TEST_PARALLEL_COST, &errMsg); 1231 | 1232 | C_ASSERT_TRUE(err == -41, CTEST_SETTER( 1233 | CTEST_TITLE("Check (%d) ARGON2ID function to return error given EMPTY password", ++testNumber), 1234 | CTEST_ON_ERROR("Was expected password (error = -41), but found error = %d", err), 1235 | CTEST_ON_SUCCESS("Success error = -41") 1236 | )) 1237 | 1238 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 1239 | CTEST_TITLE("Check (%d) errMsg is NOT NULL %p", ++testNumber, errMsg), 1240 | CTEST_ON_ERROR("Was expected errMsg pointer NOT NULL, but is NULL"), 1241 | CTEST_ON_SUCCESS("Success errMsg (%p) NOT NULL", errMsg) 1242 | )) 1243 | 1244 | C_ASSERT_EQUAL_STRING( 1245 | "Empty password\n", 1246 | errMsg, 1247 | CTEST_SETTER( 1248 | CTEST_TITLE("Check errMsg has correct description"), 1249 | CTEST_ON_ERROR("Wrong correct message errMsg = \"%s\"", errMsg), 1250 | CTEST_ON_SUCCESS("Success errMsg = \"%s\"", errMsg) 1251 | ) 1252 | ) 1253 | 1254 | INFO_MSG_FMT("Testing of ARGON2ID algorithm with:\n\n\tMem. Cost: %u\n\tInteraction Cost: %u\n\tParallel cost: %d\n", 1255 | TEST_MEM_COST, TEST_INTERACTION_COST, TEST_PARALLEL_COST) 1256 | 1257 | err = argon2id(VEC_CONST(out), STR_CONST(TEST_ARGON2ID_PASS), VEC_CONST(salt), NULL, 0, NULL, 0, 1258 | TEST_MEM_COST, TEST_INTERACTION_COST, TEST_PARALLEL_COST, &errMsg); 1259 | 1260 | C_ASSERT_TRUE(err == 0, CTEST_SETTER( 1261 | CTEST_TITLE("Check (%d) ARGON2ID function to return error given password = \""TEST_ARGON2ID_PASS"\"", ++testNumber), 1262 | CTEST_ON_ERROR("Was expected password (error = 0), but found error = %d", err), 1263 | CTEST_ON_SUCCESS("Success error = 0") 1264 | )) 1265 | 1266 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 1267 | CTEST_TITLE("Check (%d) errMsg is NOT NULL %p", ++testNumber, errMsg), 1268 | CTEST_ON_ERROR("Was expected errMsg pointer NOT NULL, but is NULL"), 1269 | CTEST_ON_SUCCESS("Success errMsg (%p) NOT NULL", errMsg) 1270 | )) 1271 | 1272 | C_ASSERT_EQUAL_STRING( 1273 | "Argon2id success\n", 1274 | errMsg, 1275 | CTEST_SETTER( 1276 | CTEST_TITLE("Check errMsg has correct description"), 1277 | CTEST_ON_ERROR("Wrong correct message errMsg = \"%s\"", errMsg), 1278 | CTEST_ON_SUCCESS("Success errMsg = \"%s\"", errMsg) 1279 | ) 1280 | ) 1281 | 1282 | salt2[0] = 1; 1283 | 1284 | err = argon2id(VEC_CONST(out2), STR_CONST(TEST_ARGON2ID_PASS), VEC_CONST(salt2), NULL, 0, NULL, 0, 1285 | TEST_MEM_COST, TEST_INTERACTION_COST, TEST_PARALLEL_COST, &errMsg); 1286 | 1287 | C_ASSERT_TRUE(err == 0, CTEST_SETTER( 1288 | CTEST_TITLE("Check (%d) ARGON2ID function to return error given password = \""TEST_ARGON2ID_PASS"\" with different salt", ++testNumber), 1289 | CTEST_ON_ERROR("Was expected password (error = 0), but found error = %d", err), 1290 | CTEST_ON_SUCCESS("Success error = 0") 1291 | )) 1292 | 1293 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 1294 | CTEST_TITLE("Check (%d) errMsg is NOT NULL %p", ++testNumber, errMsg), 1295 | CTEST_ON_ERROR("Was expected errMsg pointer NOT NULL, but is NULL"), 1296 | CTEST_ON_SUCCESS("Success errMsg (%p) NOT NULL", errMsg) 1297 | )) 1298 | 1299 | C_ASSERT_EQUAL_STRING( 1300 | "Argon2id success\n", 1301 | errMsg, 1302 | CTEST_SETTER( 1303 | CTEST_TITLE("Check errMsg has correct description"), 1304 | CTEST_ON_ERROR("Wrong correct message errMsg = \"%s\"", errMsg), 1305 | CTEST_ON_SUCCESS("Success errMsg = \"%s\"", errMsg) 1306 | ) 1307 | ) 1308 | 1309 | INFO_MSG("out2 vector content") 1310 | debug_dump(VEC_CONST(out2)); 1311 | 1312 | INFO_MSG("out1 vector content") 1313 | debug_dump(VEC_CONST(out)); 1314 | 1315 | C_ASSERT_FALSE(is_vec_content_eq(VEC_CONST(out2), VEC_CONST(out)), CTEST_SETTER( 1316 | CTEST_TITLE("Check (%d) if vector out2 (%p) is NOT equal to vector out1 (%p) with same password and different salts", ++testNumber, out2, out), 1317 | CTEST_ON_ERROR("Was expected different vector for same password and different salt, but they are equals"), 1318 | CTEST_ON_SUCCESS("Vector out2 and out1 are different for same password and different salts") 1319 | )) 1320 | 1321 | CLEAR_VECTOR(out2) 1322 | 1323 | err = argon2id(VEC_CONST(out2), STR_CONST(TEST_ARGON2ID_PASS"ABC"), VEC_CONST(salt), NULL, 0, NULL, 0, 1324 | TEST_MEM_COST, TEST_INTERACTION_COST, TEST_PARALLEL_COST, &errMsg); 1325 | 1326 | C_ASSERT_TRUE(err == 0, CTEST_SETTER( 1327 | CTEST_TITLE("Check (%d) ARGON2ID function to return error given password = \""TEST_ARGON2ID_PASS"ABC\" with same salt", ++testNumber), 1328 | CTEST_ON_ERROR("Was expected password (error = 0), but found error = %d", err), 1329 | CTEST_ON_SUCCESS("Success error = 0") 1330 | )) 1331 | 1332 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 1333 | CTEST_TITLE("Check (%d) errMsg is NOT NULL %p", ++testNumber, errMsg), 1334 | CTEST_ON_ERROR("Was expected errMsg pointer NOT NULL, but is NULL"), 1335 | CTEST_ON_SUCCESS("Success errMsg (%p) NOT NULL", errMsg) 1336 | )) 1337 | 1338 | C_ASSERT_EQUAL_STRING( 1339 | "Argon2id success\n", 1340 | errMsg, 1341 | CTEST_SETTER( 1342 | CTEST_TITLE("Check errMsg has correct description"), 1343 | CTEST_ON_ERROR("Wrong correct message errMsg = \"%s\"", errMsg), 1344 | CTEST_ON_SUCCESS("Success errMsg = \"%s\"", errMsg) 1345 | ) 1346 | ) 1347 | 1348 | INFO_MSG("out2 vector content") 1349 | debug_dump(VEC_CONST(out2)); 1350 | 1351 | INFO_MSG("out1 vector content") 1352 | debug_dump(VEC_CONST(out)); 1353 | 1354 | C_ASSERT_FALSE(is_vec_content_eq(VEC_CONST(out2), VEC_CONST(out)), CTEST_SETTER( 1355 | CTEST_TITLE("Check (%d) if vector out2 (%p) is NOT equal to vector out1 (%p) with different passwords and same salt", ++testNumber, out2, out), 1356 | CTEST_ON_ERROR("Was expected different vector for different passwords and same salts, but they are equals"), 1357 | CTEST_ON_SUCCESS("Vector out2 and out1 are different for different passwords and same salts") 1358 | )) 1359 | 1360 | CLEAR_VECTOR(out2) 1361 | 1362 | err = argon2id(VEC_CONST(out2), STR_CONST(TEST_ARGON2ID_PASS), VEC_CONST(salt), NULL, 0, NULL, 0, 1363 | TEST_MEM_COST, TEST_INTERACTION_COST2, TEST_PARALLEL_COST, &errMsg); 1364 | 1365 | C_ASSERT_TRUE(err == 0, CTEST_SETTER( 1366 | CTEST_TITLE("Check (%d) ARGON2ID function to return error given password = \""TEST_ARGON2ID_PASS"\" with different interaction cost %u", 1367 | ++testNumber, TEST_INTERACTION_COST2), 1368 | CTEST_ON_ERROR("Was expected password (error = 0), but found error = %d", err), 1369 | CTEST_ON_SUCCESS("Success error = 0") 1370 | )) 1371 | 1372 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 1373 | CTEST_TITLE("Check (%d) errMsg is NOT NULL %p", ++testNumber, errMsg), 1374 | CTEST_ON_ERROR("Was expected errMsg pointer NOT NULL, but is NULL"), 1375 | CTEST_ON_SUCCESS("Success errMsg (%p) NOT NULL", errMsg) 1376 | )) 1377 | 1378 | C_ASSERT_EQUAL_STRING( 1379 | "Argon2id success\n", 1380 | errMsg, 1381 | CTEST_SETTER( 1382 | CTEST_TITLE("Check errMsg has correct description"), 1383 | CTEST_ON_ERROR("Wrong correct message errMsg = \"%s\"", errMsg), 1384 | CTEST_ON_SUCCESS("Success errMsg = \"%s\"", errMsg) 1385 | ) 1386 | ) 1387 | 1388 | INFO_MSG_FMT("out2 vector content with interaction cost %u", TEST_INTERACTION_COST2) 1389 | debug_dump(VEC_CONST(out2)); 1390 | 1391 | INFO_MSG_FMT("out1 vector content with interaction cost %u", TEST_INTERACTION_COST) 1392 | debug_dump(VEC_CONST(out)); 1393 | 1394 | C_ASSERT_FALSE(is_vec_content_eq(VEC_CONST(out2), VEC_CONST(out)), CTEST_SETTER( 1395 | CTEST_TITLE("Check (%d) if vector out2 (%p) is NOT equal to vector out1 (%p) with different interaction cost", ++testNumber, out2, out), 1396 | CTEST_ON_ERROR("Was expected different vector for different interaction cost, but they are equals"), 1397 | CTEST_ON_SUCCESS("Vector out2 and out1 are different for different interaction cost") 1398 | )) 1399 | 1400 | CLEAR_VECTOR(out2) 1401 | 1402 | err = argon2id(VEC_CONST(out2), STR_CONST(TEST_ARGON2ID_PASS), VEC_CONST(salt), NULL, 0, NULL, 0, 1403 | TEST_MEM_COST2, TEST_INTERACTION_COST, TEST_PARALLEL_COST, &errMsg); 1404 | 1405 | C_ASSERT_TRUE(err == 0, CTEST_SETTER( 1406 | CTEST_TITLE("Check (%d) ARGON2ID function to return error given password = \""TEST_ARGON2ID_PASS"\" with different memory cost %u", 1407 | ++testNumber, TEST_MEM_COST2), 1408 | CTEST_ON_ERROR("Was expected password (error = 0), but found error = %d", err), 1409 | CTEST_ON_SUCCESS("Success error = 0") 1410 | )) 1411 | 1412 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 1413 | CTEST_TITLE("Check (%d) errMsg is NOT NULL %p", ++testNumber, errMsg), 1414 | CTEST_ON_ERROR("Was expected errMsg pointer NOT NULL, but is NULL"), 1415 | CTEST_ON_SUCCESS("Success errMsg (%p) NOT NULL", errMsg) 1416 | )) 1417 | 1418 | C_ASSERT_EQUAL_STRING( 1419 | "Argon2id success\n", 1420 | errMsg, 1421 | CTEST_SETTER( 1422 | CTEST_TITLE("Check errMsg has correct description"), 1423 | CTEST_ON_ERROR("Wrong correct message errMsg = \"%s\"", errMsg), 1424 | CTEST_ON_SUCCESS("Success errMsg = \"%s\"", errMsg) 1425 | ) 1426 | ) 1427 | 1428 | INFO_MSG_FMT("out2 vector content with memory cost %u", TEST_MEM_COST2) 1429 | debug_dump(VEC_CONST(out2)); 1430 | 1431 | INFO_MSG_FMT("out1 vector content with memory cost %u", TEST_MEM_COST) 1432 | debug_dump(VEC_CONST(out)); 1433 | 1434 | C_ASSERT_FALSE(is_vec_content_eq(VEC_CONST(out2), VEC_CONST(out)), CTEST_SETTER( 1435 | CTEST_TITLE("Check (%d) if vector out2 (%p) is NOT equal to vector out1 (%p) with different memory cost", ++testNumber, out2, out), 1436 | CTEST_ON_ERROR("Was expected different vector for different memory cost, but they are equals"), 1437 | CTEST_ON_SUCCESS("Vector out2 and out1 are different for memory cost") 1438 | )) 1439 | 1440 | CLEAR_VECTOR(out2) 1441 | 1442 | err = argon2id(VEC_CONST(out2), STR_CONST(TEST_ARGON2ID_PASS), VEC_CONST(salt), NULL, 0, NULL, 0, 1443 | TEST_MEM_COST, TEST_INTERACTION_COST, TEST_PARALLEL_COST2, &errMsg); 1444 | 1445 | C_ASSERT_TRUE(err == 0, CTEST_SETTER( 1446 | CTEST_TITLE("Check (%d) ARGON2ID function to return error given password = \""TEST_ARGON2ID_PASS"\" with different parallel cost %u", 1447 | ++testNumber, TEST_MEM_COST2), 1448 | CTEST_ON_ERROR("Was expected password (error = 0), but found error = %d", err), 1449 | CTEST_ON_SUCCESS("Success error = 0") 1450 | )) 1451 | 1452 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 1453 | CTEST_TITLE("Check (%d) errMsg is NOT NULL %p", ++testNumber, errMsg), 1454 | CTEST_ON_ERROR("Was expected errMsg pointer NOT NULL, but is NULL"), 1455 | CTEST_ON_SUCCESS("Success errMsg (%p) NOT NULL", errMsg) 1456 | )) 1457 | 1458 | C_ASSERT_EQUAL_STRING( 1459 | "Argon2id success\n", 1460 | errMsg, 1461 | CTEST_SETTER( 1462 | CTEST_TITLE("Check errMsg has correct description"), 1463 | CTEST_ON_ERROR("Wrong correct message errMsg = \"%s\"", errMsg), 1464 | CTEST_ON_SUCCESS("Success errMsg = \"%s\"", errMsg) 1465 | ) 1466 | ) 1467 | 1468 | INFO_MSG_FMT("out2 vector content with parallel cost %u", TEST_PARALLEL_COST2) 1469 | debug_dump(VEC_CONST(out2)); 1470 | 1471 | INFO_MSG_FMT("out1 vector content with parallel cost %u", TEST_PARALLEL_COST) 1472 | debug_dump(VEC_CONST(out)); 1473 | 1474 | C_ASSERT_FALSE(is_vec_content_eq(VEC_CONST(out2), VEC_CONST(out)), CTEST_SETTER( 1475 | CTEST_TITLE("Check (%d) if vector out2 (%p) is NOT equal to vector out1 (%p) with different parallel cost", ++testNumber, out2, out), 1476 | CTEST_ON_ERROR("Was expected different vector for different parallel cost, but they are equals"), 1477 | CTEST_ON_SUCCESS("Vector out2 and out1 are different for parallel cost") 1478 | )) 1479 | 1480 | CLEAR_VECTOR(out2) 1481 | 1482 | err = argon2id(VEC_CONST(out2), STR_CONST(TEST_ARGON2ID_PASS), VEC_CONST(salt), (uint8_t *)STR_CONST(ADDITIONAL_DATA), NULL, 0, 1483 | TEST_MEM_COST, TEST_INTERACTION_COST, TEST_PARALLEL_COST2, &errMsg); 1484 | 1485 | C_ASSERT_TRUE(err == 0, CTEST_SETTER( 1486 | CTEST_TITLE("Check (%d) ARGON2ID function to return error given additional data = \""ADDITIONAL_DATA"\"", ++testNumber), 1487 | CTEST_ON_ERROR("Was expected password (error = 0), but found error = %d", err), 1488 | CTEST_ON_SUCCESS("Success error = 0") 1489 | )) 1490 | 1491 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 1492 | CTEST_TITLE("Check (%d) errMsg is NOT NULL %p", ++testNumber, errMsg), 1493 | CTEST_ON_ERROR("Was expected errMsg pointer NOT NULL, but is NULL"), 1494 | CTEST_ON_SUCCESS("Success errMsg (%p) NOT NULL", errMsg) 1495 | )) 1496 | 1497 | C_ASSERT_EQUAL_STRING( 1498 | "Argon2id success\n", 1499 | errMsg, 1500 | CTEST_SETTER( 1501 | CTEST_TITLE("Check errMsg has correct description"), 1502 | CTEST_ON_ERROR("Wrong correct message errMsg = \"%s\"", errMsg), 1503 | CTEST_ON_SUCCESS("Success errMsg = \"%s\"", errMsg) 1504 | ) 1505 | ) 1506 | 1507 | INFO_MSG("out2 vector content with additional data = \""ADDITIONAL_DATA"\"") 1508 | debug_dump(VEC_CONST(out2)); 1509 | 1510 | INFO_MSG("out1 vector content with no additional data") 1511 | debug_dump(VEC_CONST(out)); 1512 | 1513 | C_ASSERT_FALSE(is_vec_content_eq(VEC_CONST(out2), VEC_CONST(out)), CTEST_SETTER( 1514 | CTEST_TITLE("Check (%d) if vector out2 (%p) is NOT equal to vector out1 (%p) with additional data", ++testNumber, out2, out), 1515 | CTEST_ON_ERROR("Was expected different vector for additional data, but they are equals"), 1516 | CTEST_ON_SUCCESS("Vector out2 and out1 for additional data") 1517 | )) 1518 | 1519 | CLEAR_VECTOR(out2) 1520 | 1521 | err = argon2id(VEC_CONST(out2), STR_CONST(TEST_ARGON2ID_PASS), VEC_CONST(salt), NULL, 0, (uint8_t *)STR_CONST(ADDITIONAL_SECRET), 1522 | TEST_MEM_COST, TEST_INTERACTION_COST, TEST_PARALLEL_COST2, &errMsg); 1523 | 1524 | C_ASSERT_TRUE(err == 0, CTEST_SETTER( 1525 | CTEST_TITLE("Check (%d) ARGON2ID function to return error given additional secret = \""ADDITIONAL_SECRET"\"", ++testNumber), 1526 | CTEST_ON_ERROR("Was expected password (error = 0), but found error = %d", err), 1527 | CTEST_ON_SUCCESS("Success error = 0") 1528 | )) 1529 | 1530 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 1531 | CTEST_TITLE("Check (%d) errMsg is NOT NULL %p", ++testNumber, errMsg), 1532 | CTEST_ON_ERROR("Was expected errMsg pointer NOT NULL, but is NULL"), 1533 | CTEST_ON_SUCCESS("Success errMsg (%p) NOT NULL", errMsg) 1534 | )) 1535 | 1536 | C_ASSERT_EQUAL_STRING( 1537 | "Argon2id success\n", 1538 | errMsg, 1539 | CTEST_SETTER( 1540 | CTEST_TITLE("Check errMsg has correct description"), 1541 | CTEST_ON_ERROR("Wrong correct message errMsg = \"%s\"", errMsg), 1542 | CTEST_ON_SUCCESS("Success errMsg = \"%s\"", errMsg) 1543 | ) 1544 | ) 1545 | 1546 | INFO_MSG("out2 vector content with additional secret = \""ADDITIONAL_SECRET"\"") 1547 | debug_dump(VEC_CONST(out2)); 1548 | 1549 | INFO_MSG("out1 vector content with no additional secret") 1550 | debug_dump(VEC_CONST(out)); 1551 | 1552 | C_ASSERT_FALSE(is_vec_content_eq(VEC_CONST(out2), VEC_CONST(out)), CTEST_SETTER( 1553 | CTEST_TITLE("Check (%d) if vector out2 (%p) is NOT equal to vector out1 (%p) with additional secret", ++testNumber, out2, out), 1554 | CTEST_ON_ERROR("Was expected different vector for additional secret, but they are equals"), 1555 | CTEST_ON_SUCCESS("Vector out2 and out1 for additional secret") 1556 | )) 1557 | 1558 | CLEAR_VECTOR(out2) 1559 | 1560 | err = argon2id(VEC_CONST(out2), STR_CONST(TEST_ARGON2ID_PASS), VEC_CONST(salt), (uint8_t *)STR_CONST(ADDITIONAL_DATA), (uint8_t *)STR_CONST(ADDITIONAL_SECRET), 1561 | TEST_MEM_COST, TEST_INTERACTION_COST, TEST_PARALLEL_COST2, &errMsg); 1562 | 1563 | C_ASSERT_TRUE(err == 0, CTEST_SETTER( 1564 | CTEST_TITLE("Check (%d) ARGON2ID function to return error given additional secret = \""ADDITIONAL_SECRET"\" and additional data = \""ADDITIONAL_DATA"\"", 1565 | ++testNumber), 1566 | CTEST_ON_ERROR("Was expected password (error = 0), but found error = %d", err), 1567 | CTEST_ON_SUCCESS("Success error = 0") 1568 | )) 1569 | 1570 | C_ASSERT_NOT_NULL(errMsg, CTEST_SETTER( 1571 | CTEST_TITLE("Check (%d) errMsg is NOT NULL %p", ++testNumber, errMsg), 1572 | CTEST_ON_ERROR("Was expected errMsg pointer NOT NULL, but is NULL"), 1573 | CTEST_ON_SUCCESS("Success errMsg (%p) NOT NULL", errMsg) 1574 | )) 1575 | 1576 | C_ASSERT_EQUAL_STRING( 1577 | "Argon2id success\n", 1578 | errMsg, 1579 | CTEST_SETTER( 1580 | CTEST_TITLE("Check errMsg has correct description"), 1581 | CTEST_ON_ERROR("Wrong correct message errMsg = \"%s\"", errMsg), 1582 | CTEST_ON_SUCCESS("Success errMsg = \"%s\"", errMsg) 1583 | ) 1584 | ) 1585 | 1586 | INFO_MSG("out2 vector content with additional secret = \""ADDITIONAL_SECRET"\" and additional data = \""ADDITIONAL_DATA"\"") 1587 | debug_dump(VEC_CONST(out2)); 1588 | 1589 | INFO_MSG("out1 vector content with no additional secret and additional data") 1590 | debug_dump(VEC_CONST(out)); 1591 | 1592 | C_ASSERT_FALSE(is_vec_content_eq(VEC_CONST(out2), VEC_CONST(out)), CTEST_SETTER( 1593 | CTEST_TITLE("Check (%d) if vector out2 (%p) is NOT equal to vector out1 (%p) with additional secret and additional data", ++testNumber, out2, out), 1594 | CTEST_ON_ERROR("Was expected different vector for additional secret and data, but they are equals"), 1595 | CTEST_ON_SUCCESS("Vector out2 and out1 for additional secret and data") 1596 | )) 1597 | 1598 | #undef ADDITIONAL_SECRET 1599 | #undef ADDITIONAL_DATA 1600 | #undef TEST_PARALLEL_COST 1601 | #undef TEST_MEM_COST2 1602 | #undef TEST_INTERACTION_COST2 1603 | #undef TEST_ARGON2ID_PASS 1604 | #undef TEST_PARALLEL_COST 1605 | #undef TEST_INTERACTION_COST 1606 | #undef TEST_MEM_COST 1607 | } 1608 | 1609 | --------------------------------------------------------------------------------