int crypto_init() { if (-1 == sodium_init()) return 1; randombytes_set_implementation(&randombytes_salsa20_implementation); randombytes_stir(); return 0; }
int sodium_init(void) { if (sodium_crit_enter() != 0) { return -1; } if (initialized != 0) { if (sodium_crit_leave() != 0) { return -1; } return 1; } _sodium_runtime_get_cpu_features(); randombytes_stir(); _sodium_alloc_init(); _crypto_pwhash_argon2i_pick_best_implementation(); _crypto_generichash_blake2b_pick_best_implementation(); _crypto_onetimeauth_poly1305_pick_best_implementation(); _crypto_scalarmult_curve25519_pick_best_implementation(); _crypto_stream_chacha20_pick_best_implementation(); initialized = 1; if (sodium_crit_leave() != 0) { return -1; } return 0; }
CRYPTO_STATUS sidhjs_init () { randombytes_stir(); isogeny = SIDH_curve_allocate(&CurveIsogeny_SIDHp751); return SIDH_curve_initialize( isogeny, sidhjs_randombytes, &CurveIsogeny_SIDHp751 ); }
int crypto_init(const char *password) { if (sodium_init() == -1) { return 1; } randombytes_set_implementation(&randombytes_salsa20_implementation); randombytes_stir(); return crypto_generichash(secret_key, sizeof secret_key, (uint8_t*)password, strlen(password), NULL, 0); }
int sodium_init(void) { if (initialized != 0) { return 1; } _sodium_runtime_get_cpu_features(); randombytes_stir(); _sodium_alloc_init(); _crypto_generichash_blake2b_pick_best_implementation(); _crypto_onetimeauth_poly1305_pick_best_implementation(); initialized = 1; return 0; }
e3x_cipher_t cs3a_init(lob_t options) { e3x_cipher_t ret; // sanity check if(crypto_box_NONCEBYTES != 24 || crypto_box_PUBLICKEYBYTES != 32 || crypto_onetimeauth_BYTES != 16) { return LOG("libsodium version error, defined header sizes don't match expectations"); } ret = malloc(sizeof(struct e3x_cipher_struct)); if(!ret) return NULL; memset(ret,0,sizeof (struct e3x_cipher_struct)); // identifying markers ret->id = CS_3a; ret->csid = 0x3a; memcpy(ret->hex,"3a",3); // which alg's we support ret->alg = "ED25519"; // normal init stuff randombytes_stir(); // configure our callbacks ret->hash = cipher_hash; ret->rand = cipher_rand; ret->err = cipher_err; ret->generate = cipher_generate; // need to cast these to map our struct types to voids ret->local_new = (void *(*)(lob_t, lob_t))local_new; ret->local_free = (void (*)(void *))local_free; ret->local_decrypt = (lob_t (*)(void *, lob_t))local_decrypt; ret->local_sign = (lob_t (*)(void *, lob_t, uint8_t *, size_t))local_sign; ret->remote_new = (void *(*)(lob_t, uint8_t *))remote_new; ret->remote_free = (void (*)(void *))remote_free; ret->remote_verify = (uint8_t (*)(void *, void *, lob_t))remote_verify; ret->remote_encrypt = (lob_t (*)(void *, void *, lob_t))remote_encrypt; ret->remote_validate = (uint8_t (*)(void *, lob_t, lob_t, uint8_t *, size_t))remote_validate; ret->ephemeral_new = (void *(*)(void *, lob_t))ephemeral_new; ret->ephemeral_free = (void (*)(void *))ephemeral_free; ret->ephemeral_encrypt = (lob_t (*)(void *, lob_t))ephemeral_encrypt; ret->ephemeral_decrypt = (lob_t (*)(void *, lob_t))ephemeral_decrypt; return ret; }
static void revoke_privileges(ProxyContext * const proxy_context) { (void) proxy_context; init_locale(); init_tz(); (void) strerror(ENOENT); #ifndef DEBUG randombytes_stir(); # ifndef _WIN32 if (proxy_context->user_dir != NULL) { if (chdir(proxy_context->user_dir) != 0 || chroot(proxy_context->user_dir) != 0 || chdir("/") != 0) { logger(proxy_context, LOG_ERR, "Unable to chroot to [%s]", proxy_context->user_dir); exit(1); } } if (sandboxes_app() != 0) { logger_noformat(proxy_context, LOG_ERR, "Unable to sandbox the main process"); exit(1); } if (proxy_context->user_id != (uid_t) 0) { if (setgid(proxy_context->user_group) != 0 || setegid(proxy_context->user_group) != 0 || setuid(proxy_context->user_id) != 0 || seteuid(proxy_context->user_id) != 0) { logger(proxy_context, LOG_ERR, "Unable to switch to user id [%lu]", (unsigned long) proxy_context->user_id); exit(1); } } # endif #endif }
int wmain(int argc, wchar_t* argv[]) { //Windows XP with SP3 support #if (defined(PLATFORM_WIN32) && !defined(PLATFORM_WIN64)) GetFunctionPointer(FUNCTION_GETTICKCOUNT64); GetFunctionPointer(FUNCTION_INET_NTOP); #endif #elif (defined(PLATFORM_LINUX) || defined(PLATFORM_MACX)) int main(int argc, char *argv[]) { #endif //Get commands. if (argc > 0) { if (!ReadCommand(argc, argv)) return EXIT_SUCCESS; } else { return EXIT_FAILURE; } //Read configuration file and WinPcap or LibPcap initialization. if (!ReadParameter()) { WSACleanup(); return EXIT_FAILURE; } //Mark Local DNS address to PTR Records. std::thread NetworkInformationMonitorThread(NetworkInformationMonitor); NetworkInformationMonitorThread.detach(); //Read IPFilter and Hosts. if (Parameter.OperationMode == LISTEN_MODE_CUSTOM || Parameter.BlacklistCheck || Parameter.LocalRouting) { std::thread IPFilterThread(ReadIPFilter); IPFilterThread.detach(); } std::thread HostsThread(ReadHosts); HostsThread.detach(); //DNSCurve initialization #if defined(ENABLE_LIBSODIUM) if (Parameter.DNSCurve && DNSCurveParameter.IsEncryption) { randombytes_set_implementation(&randombytes_salsa20_implementation); randombytes_stir(); DNSCurveInit(); } #endif #if defined(PLATFORM_WIN) //Service initialization and start service. SERVICE_TABLE_ENTRYW ServiceTable[] = {{DEFAULT_LOCAL_SERVICE_NAME, (LPSERVICE_MAIN_FUNCTIONW)ServiceMain}, {nullptr, nullptr}}; if (!StartServiceCtrlDispatcherW(ServiceTable)) { Parameter.Console = true; wprintf_s(L"System Error: Service start error, error code is %lu.\n", GetLastError()); wprintf_s(L"System Error: Program will continue to run in console mode.\n"); wprintf_s(L"Please ignore those error messages if you want to run in console mode.\n"); //Handle the system signal and start all monitors. SetConsoleCtrlHandler((PHANDLER_ROUTINE)CtrlHandler, TRUE); MonitorInit(); } #elif (defined(PLATFORM_LINUX) || defined(PLATFORM_MACX)) MonitorInit(); #endif WSACleanup(); return EXIT_SUCCESS; } //Read commands from main program #if defined(PLATFORM_WIN) bool __fastcall ReadCommand(int argc, wchar_t* argv[]) #elif (defined(PLATFORM_LINUX) || defined(PLATFORM_MACX)) bool ReadCommand(int argc, char *argv[]) #endif { //Path initialization #if defined(PLATFORM_WIN) if (!FileNameInit(argv[0])) return false; #elif (defined(PLATFORM_LINUX) || defined(PLATFORM_MACX)) std::shared_ptr<char> FileName(new char[PATH_MAX + 1U]()); memset(FileName.get(), 0, PATH_MAX + 1U); if (getcwd(FileName.get(), PATH_MAX) == nullptr) { wprintf(L"Path initialization error.\n"); return false; } if (!FileNameInit(FileName.get())) return false; FileName.reset(); #endif #if defined(PLATFORM_WIN) //Winsock initialization std::shared_ptr<WSAData> WSAInitialization(new WSAData()); if (WSAStartup(MAKEWORD(WINSOCK_VERSION_HIGH, WINSOCK_VERSION_LOW), WSAInitialization.get()) != 0 || LOBYTE(WSAInitialization->wVersion) != WINSOCK_VERSION_LOW || HIBYTE(WSAInitialization->wVersion) != WINSOCK_VERSION_HIGH) { wprintf_s(L"Winsock initialization error, error code is %d.\n", WSAGetLastError()); PrintError(LOG_ERROR_NETWORK, L"Winsock initialization error", WSAGetLastError(), nullptr, 0); WSACleanup(); return false; } //Read commands. std::wstring Commands; #elif (defined(PLATFORM_LINUX) || defined(PLATFORM_MACX)) std::string Commands; #endif for (size_t Index = 1U;(SSIZE_T)Index < argc;++Index) { Commands = argv[Index]; //Flush DNS Cache from user. if (Commands == COMMAND_FLUSH_DNS) { #if defined(PLATFORM_WIN) FlushDNSMailSlotSender(); #elif (defined(PLATFORM_LINUX) || defined(PLATFORM_MACX)) FlushDNSFIFOSender(); #endif WSACleanup(); return false; } //Windows Firewall Test in first start. #if defined(PLATFORM_WIN) else if (Commands == COMMAND_FIREWALL_TEST) { if (!FirewallTest(AF_INET6) && !FirewallTest(AF_INET)) { wprintf_s(L"Windows Firewall Test error.\n"); PrintError(LOG_ERROR_NETWORK, L"Windows Firewall Test error", WSAGetLastError(), nullptr, 0); } WSACleanup(); return false; } #endif //Set system daemon. #if defined(PLATFORM_LINUX) else if (Commands == COMMAND_DISABLE_DAEMON) { Parameter.Daemon = false; } #endif //Print current version. else if (Commands == COMMAND_LONG_PRINT_VERSION || Commands == COMMAND_SHORT_PRINT_VERSION) { wprintf_s(L"Pcap_DNSProxy "); wprintf_s(FULL_VERSION); wprintf_s(L"\n"); WSACleanup(); return false; } //Print help messages. else if (Commands == COMMAND_LONG_HELP || Commands == COMMAND_SHORT_HELP) { wprintf_s(L"Usage: Please see ReadMe... files in Documents folder.\n"); WSACleanup(); return false; } //Set working directory from commands. else if (Commands == COMMAND_LONG_SET_PATH || Commands == COMMAND_SHORT_SET_PATH) { //Commands check if ((SSIZE_T)Index + 1 >= argc) { wprintf_s(L"Commands error.\n"); PrintError(LOG_ERROR_SYSTEM, L"Commands error", 0, nullptr, 0); WSACleanup(); return false; } else { ++Index; Commands = argv[Index]; //Path check. if (Commands.length() > MAX_PATH) { wprintf_s(L"Commands error.\n"); PrintError(LOG_ERROR_SYSTEM, L"Commands error", 0, nullptr, 0); WSACleanup(); return false; } else { if (!FileNameInit(Commands.c_str())) return false; } } } } //Set system daemon. #if defined(PLATFORM_LINUX) if (Parameter.Daemon && daemon(0, 0) == RETURN_ERROR) { PrintError(LOG_ERROR_SYSTEM, L"Set system daemon error", 0, nullptr, 0); return false; } #endif return true; } //Get path of program from the main function parameter and Winsock initialization #if defined(PLATFORM_WIN) bool __fastcall FileNameInit(const wchar_t *OriginalPath) #elif (defined(PLATFORM_LINUX) || defined(PLATFORM_MACX)) bool FileNameInit(const char *OriginalPath) #endif { //Path process #if defined(PLATFORM_WIN) Parameter.Path_Global->clear(); Parameter.Path_Global->push_back(OriginalPath); Parameter.Path_Global->front().erase(Parameter.Path_Global->front().rfind(L"\\") + 1U); for (size_t Index = 0;Index < Parameter.Path_Global->front().length();++Index) { if ((Parameter.Path_Global->front()).at(Index) == L'\\') { Parameter.Path_Global->front().insert(Index, L"\\"); ++Index; } } #elif (defined(PLATFORM_LINUX) || defined(PLATFORM_MACX)) Parameter.sPath_Global->clear(); Parameter.sPath_Global->push_back(OriginalPath); Parameter.sPath_Global->front().append("/"); std::wstring StringTemp; MBSToWCSString(StringTemp, OriginalPath); StringTemp.append(L"/"); Parameter.Path_Global->clear(); Parameter.Path_Global->push_back(StringTemp); StringTemp.clear(); #endif //Get path of error/running status log file and mark start time. Parameter.Path_ErrorLog->clear(); *Parameter.Path_ErrorLog = Parameter.Path_Global->front(); Parameter.Path_ErrorLog->append(L"Error.log"); #if (defined(PLATFORM_LINUX) || defined(PLATFORM_MACX)) Parameter.sPath_ErrorLog->clear(); *Parameter.sPath_ErrorLog = Parameter.sPath_Global->front(); Parameter.sPath_ErrorLog->append("Error.log"); #endif Parameter.PrintError = true; time(&StartTime); return true; }
static int randombytes_tests(void) { unsigned int f = 0U; unsigned int i; uint32_t n; #ifdef __EMSCRIPTEN__ assert(strcmp(randombytes_implementation_name(), "sysrandom")); #else assert(strcmp(randombytes_implementation_name(), "js")); #endif randombytes(x, 1U); do { n = randombytes_random(); f |= ((n >> 24) > 1); f |= ((n >> 16) > 1) << 1; f |= ((n >> 8) > 1) << 2; f |= ((n ) > 1) << 3; f |= (n > 0x7fffffff) << 4; } while (f != 0x1f); randombytes_close(); for (i = 0; i < 256; ++i) { freq[i] = 0; } for (i = 0; i < 65536; ++i) { ++freq[randombytes_uniform(256)]; } for (i = 0; i < 256; ++i) { if (!freq[i]) { printf("randombytes_uniform() test failed\n"); } } assert(randombytes_uniform(1U) == 0U); randombytes_close(); #ifndef __EMSCRIPTEN__ randombytes_set_implementation(&randombytes_salsa20_implementation); assert(strcmp(randombytes_implementation_name(), "salsa20") == 0); #endif randombytes_stir(); for (i = 0; i < 256; ++i) { freq[i] = 0; } for (i = 0; i < 65536; ++i) { ++freq[randombytes_uniform(256)]; } for (i = 0; i < 256; ++i) { if (!freq[i]) { printf("randombytes_uniform() test failed\n"); } } memset(x, 0, sizeof x); randombytes_buf(x, sizeof x); for (i = 0; i < 256; ++i) { freq[i] = 0; } for (i = 0; i < sizeof x; ++i) { ++freq[255 & (int)x[i]]; } for (i = 0; i < 256; ++i) { if (!freq[i]) { printf("randombytes_buf() test failed\n"); } } assert(randombytes_uniform(1U) == 0U); randombytes_close(); randombytes(x, 1U); randombytes_close(); return 0; }
int dnscrypt_proxy_main(int argc, char *argv[]) { ProxyContext proxy_context; setvbuf(stdout, NULL, _IOLBF, BUFSIZ); stack_trace_on_crash(); #ifdef PLUGINS if ((app_context.dcps_context = plugin_support_context_new()) == NULL) { logger_noformat(NULL, LOG_ERR, "Unable to setup plugin support"); exit(2); } #endif if (proxy_context_init(&proxy_context, argc, argv) != 0) { logger_noformat(NULL, LOG_ERR, "Unable to start the proxy"); exit(1); } #ifdef USE_ONLY_PORTABLE_IMPLEMENTATIONS randombytes_stir(); #else logger_noformat(&proxy_context, LOG_INFO, "Initializing libsodium for optimal performance"); if (sodium_init() != 0) { exit(1); } #endif randombytes_set_implementation(&randombytes_salsa20_implementation); #ifdef PLUGINS if (plugin_support_context_load(app_context.dcps_context) != 0) { logger_noformat(NULL, LOG_ERR, "Unable to load plugins"); exit(2); } #endif app_context.proxy_context = &proxy_context; logger_noformat(&proxy_context, LOG_INFO, "Generating a new key pair"); dnscrypt_client_init_with_new_key_pair(&proxy_context.dnscrypt_client); logger_noformat(&proxy_context, LOG_INFO, "Done"); if (cert_updater_init(&proxy_context) != 0 || udp_listener_bind(&proxy_context) != 0 || tcp_listener_bind(&proxy_context) != 0) { exit(1); } #ifdef SIGPIPE signal(SIGPIPE, SIG_IGN); #endif revoke_privileges(&proxy_context); if (cert_updater_start(&proxy_context) != 0) { exit(1); } if (skip_dispatch == 0) { event_base_dispatch(proxy_context.event_loop); } logger_noformat(&proxy_context, LOG_INFO, "Stopping proxy"); cert_updater_free(&proxy_context); udp_listener_stop(&proxy_context); tcp_listener_stop(&proxy_context); event_base_free(proxy_context.event_loop); #ifdef PLUGINS plugin_support_context_free(app_context.dcps_context); #endif proxy_context_free(&proxy_context); app_context.proxy_context = NULL; randombytes_close(); return 0; }
/** * Create a keypair */ void crypto_init (void) { randombytes_stir(); crypto_box_keypair(pk, sk); }