From baf8543def01a90f84de19d5822509a3bca829b7 Mon Sep 17 00:00:00 2001 From: gardners Date: Tue, 26 Jun 2012 21:21:46 +0930 Subject: [PATCH] added cache for positive and negative rhizome manifest signature verifications to avoid repeating expensive lookups. not perfect, and not tested. --- rhizome_crypto.c | 76 ++++++++++++++++++++++++++++++++++++++++++++---- rhizome_fetch.c | 4 +++ 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/rhizome_crypto.c b/rhizome_crypto.c index 0d88a269..22ec7f36 100644 --- a/rhizome_crypto.c +++ b/rhizome_crypto.c @@ -133,7 +133,6 @@ int rhizome_bk_xor(const unsigned char *authorSid, // binary int rhizome_extract_privatekey(rhizome_manifest *m, const unsigned char *authorSid) { IN(); - char desc[1024]; char *bk = rhizome_manifest_get(m, "BK", NULL, 0); if (!bk) { RETURN(WHY("missing BK field")); } unsigned char bkBytes[RHIZOME_BUNDLE_KEY_BYTES]; @@ -270,12 +269,71 @@ rhizome_signature *rhizome_sign_hash(rhizome_manifest *m, const unsigned char *a RETURN(out); } +typedef struct manifest_signature_block_cache { + unsigned char manifest_hash[crypto_hash_sha512_BYTES]; + unsigned char signature_bytes[256]; + int signature_length; + int signature_valid; +} manifest_signature_block_cache; + +#define SIG_CACHE_SIZE 1024 +manifest_signature_block_cache sig_cache[SIG_CACHE_SIZE]; + +int rhizome_manifest_lookup_signature_validity(unsigned char *hash,unsigned char *sig,int sig_len) +{ + IN(); + unsigned int slot=0; + int i; + + for(i=0;i=m->manifest_all_bytes) { RETURN(0); } @@ -290,11 +348,18 @@ int rhizome_manifest_extract_signature(rhizome_manifest *m,int *ofs) /* Each signature type is required to have a different length to detect it. At present only crypto_sign_edwards25519sha512batch() signatures are supported. */ + int r; if (m->sig_countmanifesthash,&m->manifestdata[(*ofs)+1],96); +#ifdef DEPRECATED + unsigned char sigBuf[256]; + unsigned char verifyBuf[256]; + unsigned char publicKey[256]; bcopy(&m->manifestdata[(*ofs)+1],&sigBuf[0],32); bcopy(&m->manifesthash[0],&sigBuf[32],crypto_hash_sha512_BYTES); bcopy(&m->manifestdata[(*ofs)+1+32],&sigBuf[96],32); @@ -305,6 +370,7 @@ int rhizome_manifest_extract_signature(rhizome_manifest *m,int *ofs) int r=crypto_sign_edwards25519sha512batch_open(verifyBuf,&mlen,&sigBuf[0],128, publicKey); fflush(stdout); fflush(stderr); +#endif if (r) { (*ofs)+=len; m->errors++; @@ -318,7 +384,7 @@ int rhizome_manifest_extract_signature(rhizome_manifest *m,int *ofs) (*ofs)+=len; RETURN(WHY("malloc() failed when reading signature block")); } - bcopy(&publicKey[0],m->signatories[m->sig_count], + bcopy(&m->manifestdata[(*ofs)+1+64],m->signatories[m->sig_count], crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES); m->sig_count++; if (debug&DEBUG_RHIZOME) DEBUG("Signature passed."); diff --git a/rhizome_fetch.c b/rhizome_fetch.c index 7de1243a..1d67946f 100644 --- a/rhizome_fetch.c +++ b/rhizome_fetch.c @@ -438,6 +438,8 @@ int rhizome_suggest_queue_manifest_import(rhizome_manifest *m, /* replace listed version with this newer version */ if (rhizome_manifest_verify(m)) { WHY("Error verifying manifest when considering queuing for import"); + /* Don't waste time looking at this manifest again for a while */ + rhizome_queue_ignore_manifest(m,peerip,60000); rhizome_manifest_free(m); RETURN(-1); } @@ -466,6 +468,8 @@ int rhizome_suggest_queue_manifest_import(rhizome_manifest *m, if (rhizome_manifest_verify(m)) { WHY("Error verifying manifest when considering queuing for import"); + /* Don't waste time looking at this manifest again for a while */ + rhizome_queue_ignore_manifest(m,peerip,60000); rhizome_manifest_free(m); RETURN(-1); }