Files
SHiNE-server/docs/CORE-CONTENT-MINIMAL/M3_REMOTE_VECTOR_CHECK.cpp
T

15 lines
2.2 KiB
C++

#include "shine_core_content.h"
#include <openssl/evp.h>
#include <fstream>
#include <sstream>
#include <iostream>
#include <cassert>
using namespace shine_core;
static Bytes unhex(const std::string& s){Bytes b;for(size_t i=0;i<s.size();i+=2)b.push_back(uint8_t(std::stoul(s.substr(i,2),nullptr,16)));return b;}
static bool sha256(const uint8_t* p,size_t n,uint8_t* out){return EVP_Digest(p,n,out,nullptr,EVP_sha256(),nullptr)==1;}
static bool sha384(const uint8_t* p,size_t n,uint8_t* out){return EVP_Digest(p,n,out,nullptr,EVP_sha384(),nullptr)==1;}
static bool sign(const Bytes& b,const uint8_t* seed,uint8_t* out){auto* key=EVP_PKEY_new_raw_private_key(EVP_PKEY_ED25519,nullptr,seed,32);auto* ctx=EVP_MD_CTX_new();size_t len=64;bool ok=key&&ctx&&EVP_DigestSignInit(ctx,nullptr,nullptr,nullptr,key)==1&&EVP_DigestSign(ctx,out,&len,b.data(),b.size())==1&&len==64;EVP_MD_CTX_free(ctx);EVP_PKEY_free(key);return ok;}
int main(int argc,char** argv){assert(argc==2);std::ifstream in(argv[1]);assert(in);std::string line;std::getline(in,line);uint8_t seed[32];for(int i=0;i<32;i++)seed[i]=i;int count=0;
while(std::getline(in,line)){if(line.empty())continue;std::istringstream ss(line);std::vector<std::string> f;std::string x;while(std::getline(ss,x,'\t'))f.push_back(x);assert(f.size()==10);auto core=unhex(f[2]),content=unhex(f[4]),owner=unhex(f[1]),ci=unhex(f[6]),pi=unhex(f[8]);auto u16=[](const uint8_t* p){return uint16_t(p[0]*256+p[1]);};uint16_t type=u16(content.data()+1),sub=u16(content.data()+3),ver=u16(content.data()+5);uint64_t timestamp=0;for(int i=37;i<45;i++)timestamp=(timestamp<<8)|core[i];Bytes body;if(core.size()==149){if(type!=3&&type!=4)putBE(body,type==0?0:getBE(core.data()+109),4);body.insert(body.end(),core.begin()+113,core.begin()+149);putBE(body,17,4);}body.insert(body.end(),content.begin()+7,content.end());Bytes actual,expected;putBE(expected,ci.size(),4);expected.insert(expected.end(),ci.begin(),ci.end());expected.insert(expected.end(),pi.begin(),pi.end());assert(buildPair(type,sub,ver,body,getBE(core.data()+33),timestamp,core.data()+1,owner.data(),seed,"test5595",sha256,sha384,sign,actual));assert(actual==expected);++count;}
assert(count>0);std::cout<<count<<" portable remote signer fixtures: byte-identical\n";}