static int descipher(Espcb *ecb, uchar *p, int n) { uchar tmp[8]; uchar *pp, *tp, *ip, *eip, *ep; DESstate *ds = ecb->espstate; ep = p + n; if(ecb->incoming) { memmove(ds->ivec, p, 8); p += 8; while(p < ep){ memmove(tmp, p, 8); block_cipher(ds->expanded, p, 1); tp = tmp; ip = ds->ivec; for(eip = ip+8; ip < eip; ){ *p++ ^= *ip; *ip++ = *tp++; } } } else { memmove(p, ds->ivec, 8); for(p += 8; p < ep; p += 8){ pp = p; ip = ds->ivec; for(eip = ip+8; ip < eip; ) *pp++ ^= *ip++; block_cipher(ds->expanded, p, 0); memmove(ds->ivec, p, 8); } } return 1; }
void desencrypt(uchar data[8], uchar key[7]) { ulong ekey[32]; key_setup(key, ekey); block_cipher(ekey, data, 0); }
void desECBdecrypt(uint8_t *p, int len, DESstate *s) { int i; uint8_t tmp[8]; for(; len >= 8; len -= 8){ block_cipher(s->expanded, p, 1); p += 8; } if(len > 0){ for (i=0; i<8; i++) tmp[i] = i; block_cipher(s->expanded, tmp, 0); for (i = 0; i < len; i++) p[i] ^= tmp[i]; } }
/* * destructively encrypt the buffer, which * must be at least 8 characters long. */ int encrypt(void *key, void *vbuf, int n) { ulong ekey[32]; uchar *buf; int i, r; if(n < 8) return 0; key_setup(key, ekey); buf = vbuf; n--; r = n % 7; n /= 7; for(i = 0; i < n; i++) { block_cipher(ekey, buf, 0); buf += 7; } if(r) block_cipher(ekey, buf - 7 + r, 0); return 1; }
/* * destructively decrypt the buffer, which * must be at least 8 characters long. */ int decrypt(void *key, void *vbuf, int n) { ulong ekey[128]; uchar *buf; int i, r; if(n < 8) return 0; key_setup(key, ekey); buf = vbuf; n--; r = n % 7; n /= 7; buf += n * 7; if(r) block_cipher(ekey, buf - 7 + r, 1); for(i = 0; i < n; i++) { buf -= 7; block_cipher(ekey, buf, 1); } return 1; }
std::map<std::string, double> algorithm_benchmark(const std::string& name, u32bit milliseconds, Timer& timer, RandomNumberGenerator& rng, Algorithm_Factory& af) { std::vector<std::string> providers = af.providers_of(name); std::map<std::string, double> all_results; if(providers.empty()) // no providers, nothing to do return all_results; const u64bit ns_per_provider = ((u64bit)milliseconds * 1000 * 1000) / providers.size(); std::vector<byte> buf(16 * 1024); rng.randomize(&buf[0], buf.size()); for(u32bit i = 0; i != providers.size(); ++i) { const std::string provider = providers[i]; std::pair<u64bit, u64bit> results(0, 0); if(const BlockCipher* proto = af.prototype_block_cipher(name, provider)) { std::auto_ptr<BlockCipher> block_cipher(proto->clone()); results = bench_block_cipher(block_cipher.get(), timer, ns_per_provider, &buf[0], buf.size()); } else if(const StreamCipher* proto = af.prototype_stream_cipher(name, provider)) { std::auto_ptr<StreamCipher> stream_cipher(proto->clone()); results = bench_stream_cipher(stream_cipher.get(), timer, ns_per_provider, &buf[0], buf.size()); } else if(const HashFunction* proto = af.prototype_hash_function(name, provider)) { std::auto_ptr<HashFunction> hash(proto->clone()); results = bench_hash(hash.get(), timer, ns_per_provider, &buf[0], buf.size()); } else if(const MessageAuthenticationCode* proto = af.prototype_mac(name, provider)) { std::auto_ptr<MessageAuthenticationCode> mac(proto->clone()); results = bench_mac(mac.get(), timer, ns_per_provider, &buf[0], buf.size()); } if(results.first && results.second) { /* 953.67 == 1000 * 1000 * 1000 / 1024 / 1024 - the conversion factor from bytes per nanosecond to mebibytes per second. */ double speed = (953.67 * results.first) / results.second; all_results[provider] = speed; } } return all_results; }