#include "commands.hpp" #include "config.hpp" #include #include #include #include #include #include extern "C" { #include "monocypher.h" } static const size_t NONCE_SIZE = 24; static const size_t KEY_SIZE = 32; static const size_t MAC_SIZE = 16; static std::string toHex(const uint8_t* data, size_t len) { std::ostringstream oss; oss << std::hex; for (size_t i=0;i fromHex(const std::string& hex) { std::vector data; data.reserve(hex.size()/2); for(size_t i=0;i buf=fromHex(hex); if(buf.size()==32){ for(int i=0;i<32;i++) outKey[i]=buf[i]; } } static void handleNickCommand(const std::string &args, AppConfig &config) { std::istringstream iss(args); std::string sub; iss >> sub; if(sub=="set"){ std::string name; std::getline(iss,name); if(!name.empty() && name[0]==' ') name.erase(0,1); config.nickname=name; std::cout<<"[nick] set: "<> plaintext; std::string keyHex; iss >> keyHex; uint8_t localKey[32]; bool useLocal=false; if(!keyHex.empty()){ hexStrToKey(keyHex, localKey); useLocal=true; } if(!config.haveSharedSecret && !useLocal){ FILE* f=fopen("/dev/urandom","rb"); if(!f)return; fread(config.sharedSecret,1,KEY_SIZE,f); fclose(f); config.haveSharedSecret=true; std::cout<<"[makeTea] No key found, random generated: "< nonce(NONCE_SIZE), mac(MAC_SIZE); std::vector ciphertext(plaintext.size()); { FILE* f=fopen("/dev/urandom","rb"); fread(nonce.data(),1,NONCE_SIZE,f); fclose(f); } const uint8_t* usedKey= useLocal? localKey : config.sharedSecret; crypto_aead_lock( ciphertext.data(), mac.data(), usedKey, nonce.data(), nullptr,0, (const uint8_t*)plaintext.data(), plaintext.size() ); std::vector out; out.insert(out.end(),nonce.begin(),nonce.end()); out.insert(out.end(),mac.begin(),mac.end()); out.insert(out.end(),ciphertext.begin(),ciphertext.end()); std::cout<<"[makeTea] keyUsed="<> hexIn; std::string keyHex; iss >> keyHex; uint8_t localKey[32]; bool useLocal=false; if(!keyHex.empty()){ hexStrToKey(keyHex, localKey); useLocal=true; } if(!config.haveSharedSecret && !useLocal) return; auto data=fromHex(hexIn); if(data.size() nonce(data.begin(), data.begin()+NONCE_SIZE); std::vector mac(data.begin()+NONCE_SIZE, data.begin()+NONCE_SIZE+MAC_SIZE); std::vector cipher(data.begin()+NONCE_SIZE+MAC_SIZE, data.end()); std::vector plain(cipher.size()); const uint8_t* usedKey= useLocal? localKey : config.sharedSecret; int rc=crypto_aead_unlock( plain.data(), mac.data(), usedKey, nonce.data(), nullptr,0, cipher.data(), cipher.size() ); if(rc!=0){ std::cerr<<"[drinkTea] MAC error\n"; return; } std::string s((char*)plain.data(),plain.size()); std::cout<<"[drinkTea] keyUsed="<>cmd; if(cmd=="start"){ extern void webServerStart(AppConfig&); webServerStart(config); } else if(cmd=="connect"){ std::string t,ip; iss>>t>>ip; extern void webServerConnect(AppConfig&,const std::string&,const std::string&); webServerConnect(config,t,ip); } else if(cmd=="stop"){ extern void webServerStop(AppConfig&); webServerStop(config); } } static void handleSoundCommand(const std::string &args, AppConfig &config){ std::istringstream iss(args); std::string cmd; iss>>cmd; if(cmd=="find"){ extern void soundFind(AppConfig&); soundFind(config); } else if(cmd=="lose"){ extern void soundLose(AppConfig&); soundLose(config); } }