SHA256
Новый формат блоков CORE/CONTENT test5595
This commit is contained in:
+83
@@ -0,0 +1,83 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
|
||||
// Remote AddBlock body is the existing type/subtype/version + semantic body.
|
||||
// This helper has no network/state dependencies; hashes and Ed25519 are supplied
|
||||
// by the existing firmware crypto libraries (or the offline vector test).
|
||||
namespace shine_core {
|
||||
using Bytes = std::vector<uint8_t>;
|
||||
using Hash = bool (*)(const uint8_t*, size_t, uint8_t*);
|
||||
using Sign = bool (*)(const Bytes&, const uint8_t*, uint8_t*);
|
||||
inline void putBE(Bytes& out, uint64_t n, unsigned count) {
|
||||
for (unsigned i=count; i>0; --i) out.push_back(uint8_t(n >> ((i-1)*8)));
|
||||
}
|
||||
inline void putLE(Bytes& out, uint64_t n, unsigned count) {
|
||||
for (unsigned i=0; i<count; ++i) out.push_back(uint8_t(n >> (i*8)));
|
||||
}
|
||||
inline uint32_t getBE(const uint8_t* p) {
|
||||
return (uint32_t(p[0])<<24)|(uint32_t(p[1])<<16)|(uint32_t(p[2])<<8)|p[3];
|
||||
}
|
||||
inline Bytes text(const std::string& s) { return Bytes(s.begin(),s.end()); }
|
||||
inline void avroLong(Bytes& out, size_t n) {
|
||||
uint64_t x=uint64_t(n)*2;
|
||||
while (x>=128) { out.push_back(uint8_t(x)|0x80); x>>=7; }
|
||||
out.push_back(uint8_t(x));
|
||||
}
|
||||
inline void avroString(Bytes& out,const std::string& s) {
|
||||
avroLong(out,s.size());out.insert(out.end(),s.begin(),s.end());
|
||||
}
|
||||
inline bool deepBlob(const Bytes& data,Hash sha384,uint8_t* out) {
|
||||
Bytes tag=text("blob"+std::to_string(data.size()));uint8_t pair[96];
|
||||
return sha384(tag.data(),tag.size(),pair)&&sha384(data.data(),data.size(),pair+48)&&sha384(pair,96,out);
|
||||
}
|
||||
inline bool makeItem(const Bytes& data,const Bytes& tags,size_t count,const uint8_t* owner,const uint8_t* secret,
|
||||
Hash sha384,Sign sign,Bytes& out) {
|
||||
const Bytes children[]={text("dataitem"),text("1"),text("2"),Bytes(owner,owner+32),Bytes(),Bytes(),tags,data};
|
||||
const Bytes list=text("list8");uint8_t acc[48],pair[96],sig[64];
|
||||
if(!sha384(list.data(),list.size(),acc))return false;
|
||||
for(const Bytes& child:children){
|
||||
std::copy(acc,acc+48,pair);
|
||||
if(!deepBlob(child,sha384,pair+48)||!sha384(pair,96,acc))return false;
|
||||
}
|
||||
if(!sign(Bytes(acc,acc+48),secret,sig))return false;
|
||||
out.clear();putLE(out,2,2);out.insert(out.end(),sig,sig+64);out.insert(out.end(),owner,owner+32);
|
||||
out.push_back(0);out.push_back(0);putLE(out,count,8);putLE(out,tags.size(),8);
|
||||
out.insert(out.end(),tags.begin(),tags.end());out.insert(out.end(),data.begin(),data.end());return true;
|
||||
}
|
||||
inline bool buildPair(uint16_t type,uint16_t subtype,uint16_t version,const Bytes& body,
|
||||
uint32_t number,uint64_t timestamp,const uint8_t* prev,const uint8_t* owner,const uint8_t* secret,
|
||||
const std::string& app,Hash sha256,Hash sha384,Sign sign,Bytes& out) {
|
||||
constexpr uint32_t MAX_NUMBER=2147482623,PROFILE=2147483645,CONNECTION=2147483646,TECH=2147483647;
|
||||
const bool header=type==0&&subtype==0,tech=type==0&&subtype==1,fixed=type==3||type==4;
|
||||
const bool channel=type==1&&(subtype==10||subtype==11||subtype==50||subtype==90||subtype==92||subtype==100||subtype==110||subtype==120||subtype==130);
|
||||
if(version!=1||number>MAX_NUMBER||header!=(number==0)||(type==0&&!header&&!tech)||body.size()>8*1024*1024-124)return false;
|
||||
if(header&&std::any_of(prev,prev+32,[](uint8_t b){return b!=0;}))return false;
|
||||
const size_t prefix=fixed?40:(tech||channel?44:0),offset=fixed?0:4;
|
||||
if(body.size()<prefix)return false;
|
||||
uint32_t code=0,previous=0;
|
||||
if(prefix){
|
||||
if(tech&&getBE(body.data())!=0)return false;
|
||||
code=type==3?CONNECTION:type==4?PROFILE:tech?TECH:getBE(body.data());
|
||||
previous=getBE(body.data()+offset);
|
||||
if(previous>=number||(channel&&(code>MAX_NUMBER||(code>0&&(code>=number||previous<code)))))return false;
|
||||
}
|
||||
Bytes content{1};putBE(content,type,2);putBE(content,subtype,2);putBE(content,version,2);
|
||||
content.insert(content.end(),body.begin()+prefix,body.end());
|
||||
Bytes contentItem,coreItem;uint8_t contentHash[32],contentId[32];
|
||||
if(!makeItem(content,Bytes{0},0,owner,secret,sha384,sign,contentItem)||
|
||||
!sha256(content.data(),content.size(),contentHash)||!sha256(contentItem.data()+2,64,contentId))return false;
|
||||
Bytes core{1};core.insert(core.end(),prev,prev+32);putBE(core,number,4);putBE(core,timestamp,8);
|
||||
core.insert(core.end(),contentHash,contentHash+32);core.insert(core.end(),contentId,contentId+32);
|
||||
if(prefix){putBE(core,code,4);putBE(core,previous,4);core.insert(core.end(),body.begin()+offset+4,body.begin()+offset+36);}
|
||||
Bytes tags;const size_t count=prefix?3:2;avroLong(tags,count);avroString(tags,"App");avroString(tags,app);
|
||||
avroString(tags,"n");avroString(tags,std::to_string(number));
|
||||
if(prefix){avroString(tags,"l");avroString(tags,code==PROFILE?"P":code==CONNECTION?"C":code==TECH?"T":std::to_string(code));}
|
||||
tags.push_back(0);
|
||||
if(!makeItem(core,tags,count,owner,secret,sha384,sign,coreItem))return false;
|
||||
out.clear();putBE(out,coreItem.size(),4);out.insert(out.end(),coreItem.begin(),coreItem.end());out.insert(out.end(),contentItem.begin(),contentItem.end());return true;
|
||||
}
|
||||
} // namespace shine_core
|
||||
+48
-23
@@ -9,6 +9,8 @@
|
||||
#include <TouchDrvCSTXXX.hpp>
|
||||
#include <mbedtls/gcm.h>
|
||||
#include <mbedtls/sha256.h>
|
||||
#include <mbedtls/sha512.h>
|
||||
#include "shine_core_content.h"
|
||||
#include <mbedtls/base64.h>
|
||||
#include <Ed25519.h>
|
||||
#include <sodium.h>
|
||||
@@ -4581,19 +4583,6 @@ static void processPendingRemoteAddBlockRequests() {
|
||||
continue;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> preimage;
|
||||
preimage.reserve(2 + 32 + 4 + 4 + 8 + 2 + 2 + 2 + bodyBytes.size());
|
||||
appendUint16BE(preimage, 0);
|
||||
preimage.insert(preimage.end(), prevHash32, prevHash32 + 32);
|
||||
int32_t blockSize = (int32_t)(2 + 32 + 4 + 4 + 8 + 2 + 2 + 2 + bodyBytes.size());
|
||||
appendInt32BE(preimage, blockSize);
|
||||
appendInt32BE(preimage, nextBlockNumber);
|
||||
appendInt64BE(preimage, (int64_t)(shineNowMs() / 1000ULL));
|
||||
appendUint16BE(preimage, msgType);
|
||||
appendUint16BE(preimage, msgSubType);
|
||||
appendUint16BE(preimage, msgVersion);
|
||||
preimage.insert(preimage.end(), bodyBytes.begin(), bodyBytes.end());
|
||||
|
||||
uint8_t blockchainSeed[32] = {};
|
||||
uint8_t blockchainPub[32] = {};
|
||||
uint8_t blockchainSec[64] = {};
|
||||
@@ -4604,26 +4593,41 @@ static void processPendingRemoteAddBlockRequests() {
|
||||
continue;
|
||||
}
|
||||
|
||||
uint8_t hash32[32] = {};
|
||||
sha256calc(preimage.data(), preimage.size(), hash32);
|
||||
uint8_t signature[64] = {};
|
||||
if (!signMessageEd25519(std::vector<uint8_t>(hash32, hash32 + 32), blockchainSec, signature)) {
|
||||
responseData = String("{\"ok\":false,\"error\":\"sign_failed\",\"errorMessage\":\"Failed to sign AddBlock hash\",\"requestId\":\"")
|
||||
std::vector<uint8_t> fullBlock;
|
||||
const auto hash256 = +[](const uint8_t* data, size_t len, uint8_t* out) -> bool {
|
||||
return mbedtls_sha256(data, len, out, 0) == 0;
|
||||
};
|
||||
const auto hash384 = +[](const uint8_t* data, size_t len, uint8_t* out) -> bool {
|
||||
uint8_t buffer[64] = {};
|
||||
if (mbedtls_sha512(data, len, buffer, 1) != 0) return false;
|
||||
memcpy(out, buffer, 48);
|
||||
return true;
|
||||
};
|
||||
const auto sign = +[](const std::vector<uint8_t>& data, const uint8_t* key, uint8_t* sig) -> bool {
|
||||
return signMessageEd25519(data, key, sig);
|
||||
};
|
||||
// Same semantic remote body and signal contract; only the signed wire bytes change.
|
||||
if (resolvedBlockchainName != blockchainName || nextBlockNumber < 0 ||
|
||||
!shine_core::buildPair(msgType, msgSubType, msgVersion, bodyBytes, (uint32_t)nextBlockNumber,
|
||||
shineNowMs()/1000ULL, prevHash32, blockchainPub, blockchainSec,
|
||||
"test5595", hash256, hash384, sign, fullBlock)) {
|
||||
responseData = String("{\"ok\":false,\"error\":\"sign_failed\",\"errorMessage\":\"Failed to sign CORE/CONTENT pair\",\"requestId\":\"")
|
||||
+ jsonEscape(item.signalRequestId) + "\"}";
|
||||
sendSignalResponse(item.fromLogin, item.fromSessionId, "remote_addblock_result", item.signalRequestId, responseData);
|
||||
continue;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> fullBlock = preimage;
|
||||
fullBlock.push_back(0x01);
|
||||
fullBlock.push_back(0x00);
|
||||
fullBlock.insert(fullBlock.end(), signature, signature + 64);
|
||||
String addBlockReq = String("{\"blockchainName\":\"") + jsonEscape(resolvedBlockchainName)
|
||||
+ "\",\"blockNumber\":" + String((long long)nextBlockNumber)
|
||||
+ ",\"prevBlockHash\":\"" + jsonEscape(cleanPrevHash)
|
||||
+ "\",\"blockBytesB64\":\"" + jsonEscape(bytesToBase64String(fullBlock.data(), fullBlock.size())) + "\"}";
|
||||
String addBlockResp;
|
||||
bool addBlockOk = shineWsRequest(gShineWs, "AddBlock", addBlockReq, addBlockResp, SHINE_RPC_TIMEOUT_MS);
|
||||
const bool uncertain = !addBlockOk;
|
||||
if (uncertain) {
|
||||
// Retry the identical container, not the semantic request or a new timestamp.
|
||||
addBlockResp = "";
|
||||
addBlockOk = shineWsRequest(gShineWs, "AddBlock", addBlockReq, addBlockResp, SHINE_RPC_TIMEOUT_MS);
|
||||
}
|
||||
uint64_t statusCode = 0;
|
||||
jsonInt64Field(addBlockResp, "status", statusCode);
|
||||
String serverLastHash;
|
||||
@@ -4635,6 +4639,27 @@ static void processPendingRemoteAddBlockRequests() {
|
||||
jsonStringField(addBlockResp, "message", errorMessage);
|
||||
jsonInt64Field(addBlockResp, "serverLastGlobalNumber", serverLastNumber);
|
||||
|
||||
if (uncertain && (!addBlockOk || statusCode != 200)) {
|
||||
const String readReq = String("{\"blockchainName\":\"") + jsonEscape(resolvedBlockchainName)
|
||||
+ "\",\"blockNumber\":" + String((long long)nextBlockNumber) + "}";
|
||||
String readResp, readBytes;
|
||||
uint64_t readStatus = 0;
|
||||
if (shineWsRequest(gShineWs, "GetBlockchainBlock", readReq, readResp, SHINE_RPC_TIMEOUT_MS)) {
|
||||
jsonInt64Field(readResp, "status", readStatus);
|
||||
jsonStringField(readResp, "blockBytesB64", readBytes);
|
||||
if (readStatus == 200 && readBytes == bytesToBase64String(fullBlock.data(), fullBlock.size())) {
|
||||
jsonStringField(readResp, "blockHash", serverLastHash);
|
||||
serverLastNumber = (uint64_t)nextBlockNumber;
|
||||
addBlockOk = true;
|
||||
statusCode = 200;
|
||||
}
|
||||
}
|
||||
if (!addBlockOk || statusCode != 200) {
|
||||
errorCode = "addblock_outcome_unknown";
|
||||
errorMessage = "AddBlock outcome unknown; refresh history before retrying";
|
||||
}
|
||||
}
|
||||
|
||||
if (!addBlockOk || statusCode != 200) {
|
||||
if (errorCode.isEmpty()) {
|
||||
errorCode = addBlockOk ? "addblock_rejected" : "addblock_request_failed";
|
||||
|
||||
Reference in New Issue
Block a user