// tosv-sftp - standalone SSH + SFTP vault server. Single file plus vault.inc. // Profile: mlkem768x25519-sha256 (PQ hybrid) or curve25519-sha256 kex (strict required), ssh-ed25519 host, // chacha20-poly1305@openssh.com, pubkey auth, subsystem sftp only. #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define MLK_CONFIG_PARAMETER_SET 768 extern "C" { #include "mlkem_native.h" } extern "C" int tosv_mlkem_rng(uint8_t*o,size_t n){randombytes_buf(o,n);return 0;} #include "vault.inc" #ifndef TOSV_SFTP_VERSION #define TOSV_SFTP_VERSION "dev" #endif static KeyCache KC; static std::string HKF(){ return BASE + "/sftp_host_ed25519"; } static std::string GKF(){ return BASE + "/sftp_gateway_keys"; } // ---- byte helpers: big-endian u32/u64, SSH strings, mpint ---- static uint32_t uget(const unsigned char*p){return ((uint32_t)p[0]<<24)|((uint32_t)p[1]<<16)|((uint32_t)p[2]<<8)|p[3];} static void uput(std::vector&o,uint32_t v){o.push_back((v>>24)&255);o.push_back((v>>16)&255);o.push_back((v>>8)&255);o.push_back(v&255);} static void sput(std::vector&o,const std::string&s){uput(o,(uint32_t)s.size());o.insert(o.end(),s.begin(),s.end());} static void sputb(std::vector&o,const unsigned char*d,size_t n){uput(o,(uint32_t)n);o.insert(o.end(),d,d+n);} static void mput(std::vector&o,const unsigned char*d,size_t n){size_t i=0;while(in){ok=false;return false;}return true;} unsigned char u8(){if(!need(1))return 0;return p[o++];} uint32_t u32(){if(!need(4))return 0;uint32_t v=uget(p+o);o+=4;return v;} uint64_t u64(){if(!need(8))return 0;uint64_t v=0;for(int i=0;i<8;i++)v=(v<<8)|p[o+i];o+=8;return v;} std::string str(){uint32_t k=u32();if(!ok)return "";if(!need(k))return "";std::string s((const char*)p+o,k);o+=k;return s;} std::vector raw(){uint32_t k=u32();if(!ok)return {};if(!need(k))return {};std::vector v(p+o,p+o+k);o+=k;return v;} }; // base64 encode (for .pub companion) and decode (gateway keys) static std::string b64e(const unsigned char*d,size_t n){static const char*A="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";std::string o;for(size_t i=0;i>((3-j)*6))&63];else o+='=';}}return o;} static std::vector b64dx(std::string s){static int T[256];static bool I=false;if(!I){memset(T,-1,sizeof T);const char*A="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";for(int i=0;A[i];i++)T[(unsigned char)A[i]]=i;I=true;}std::vector o;int v=0,nb=0;for(char c:s){if(c=='=')break;int x=T[(unsigned char)c];if(x<0)continue;v=(v<<6)|x;nb+=6;if(nb>=8){nb-=8;o.push_back((unsigned char)((v>>nb)&255));}}return o;} // ---- host key: 32B seed in HKF, 0600 ---- struct HostKey{unsigned char pk[32];unsigned char sk[64];bool ok=false;}; static HostKey HK; static bool hostkey_load(){std::ifstream f(HKF(),std::ios::binary);unsigned char seed[32];f.read((char*)seed,32);if(f.gcount()!=32)return false;if(crypto_sign_seed_keypair(HK.pk,HK.sk,seed)!=0)return false;wipe_mem(seed,32);HK.ok=true;return true;} static bool hostkey_gen(){unsigned char seed[32];randombytes_buf(seed,32);{int hf=open(HKF().c_str(),O_WRONLY|O_CREAT|O_TRUNC,0600);if(hf<0)return false;fchmod(hf,0600);size_t wo=0;while(wo<32){ssize_t w=write(hf,(char*)seed+wo,32-wo);if(w<=0)break;wo+=(size_t)w;}close(hf);if(wo!=32)return false;}bool r=hostkey_load();wipe_mem(seed,32);{std::vector blob;std::string alg="ssh-ed25519";sput(blob,alg);sputb(blob,HK.pk,32);std::ofstream pf(HKF()+".pub");pf<<"ssh-ed25519 "< hostkey_blob(){std::vector b;std::string alg="ssh-ed25519";sput(b,alg);sputb(b,HK.pk,32);return b;} // ---- gateway keys: ssh-ed25519