1
0
mirror of https://github.com/biergaizi/codecrypt synced 2024-06-28 18:03:36 +00:00

symkey: simplify padding

This commit is contained in:
Mirek Kratochvil 2014-04-19 22:09:44 +02:00
parent 07012cb297
commit 68909b74ad

@ -94,11 +94,9 @@ bool symkey::encrypt (std::istream&in, std::ostream&out, prng&rng)
* *
* - one-time key part, key.size() bytes * - one-time key part, key.size() bytes
* (repeat: * (repeat:
* - 4B blocksize little-endian
* - blocksize encrypted bytes * - blocksize encrypted bytes
* - sum(hashes's size) blocksize marker+bytes of block hashes * - sum(hashes's size) blocksize marker+bytes of block hashes
* ) * )
* - 4B less than blocksize (may be zero!)
* - possibly incomplete last block (may be empty) * - possibly incomplete last block (may be empty)
* - hashes of last blocksize+block * - hashes of last blocksize+block
* - eof * - eof
@ -158,11 +156,11 @@ bool symkey::encrypt (std::istream&in, std::ostream&out, prng&rng)
*/ */
std::vector<byte>buf, cipbuf; std::vector<byte>buf, cipbuf;
buf.resize (4 + blocksize + hashes_size); buf.resize (blocksize + hashes_size);
cipbuf.resize (buf.size() ); cipbuf.resize (buf.size() );
for (;;) { for (;;) {
in.read ( (char*) & (buf[4]), blocksize); in.read ( (char*) & (buf[0]), blocksize);
uint bytes_read = in.gcount(); uint bytes_read = in.gcount();
if (!in && !in.eof() ) { if (!in && !in.eof() ) {
@ -170,25 +168,19 @@ bool symkey::encrypt (std::istream&in, std::ostream&out, prng&rng)
return false; return false;
} }
//now we got bytes_read of key stuff ready in buf.
uint blksizeid = bytes_read;
for (uint i = 0; i < 4; ++i) {
buf[i] = blksizeid & 0xff;
blksizeid >>= 8;
}
//hashup! //hashup!
uint hashpos = 4 + bytes_read; uint hashpos = bytes_read;
for (hashes_t::iterator i = hs.begin(), e = hs.end(); for (hashes_t::iterator i = hs.begin(), e = hs.end();
i != e; ++i) { i != e; ++i) {
hash_proc&hp = **i; hash_proc&hp = **i;
hp.init(); hp.init();
hp.eat (& (buf[0]), & (buf[4 + bytes_read]) ); hp.eat (& (buf[0]), & (buf[bytes_read]) );
hp.eat (key); hp.eat (key);
hp.eat (otkey); hp.eat (otkey);
std::vector<byte> res = hp.finish(); std::vector<byte> res = hp.finish();
for (uint j = 0; j < res.size(); ++j, ++hashpos) for (uint j = 0; j < res.size(); ++j, ++hashpos)
buf[hashpos] = res[j]; buf[hashpos] = res[j];
//hashpos gets to the end of block with hashes
} }
//encrypt! //encrypt!
@ -275,14 +267,14 @@ int symkey::decrypt (std::istream&in, std::ostream&out)
*/ */
std::vector<byte> buf, cipbuf; std::vector<byte> buf, cipbuf;
buf.resize (4 + blocksize + hashes_size); buf.resize (blocksize + hashes_size);
cipbuf.resize (buf.size() ); cipbuf.resize (buf.size() );
for (;;) { for (;;) {
in.read ( (char*) & (buf[0]), buf.size() ); in.read ( (char*) & (buf[0]), buf.size() );
uint bytes_read = in.gcount(); uint bytes_read = in.gcount();
if ( (!in && !in.eof() ) || bytes_read < 4 + hashes_size) { if ( (!in && !in.eof() ) || bytes_read < hashes_size) {
err ("symkey: failed reading input"); err ("symkey: failed reading input");
return 1; return 1;
} }
@ -296,25 +288,15 @@ int symkey::decrypt (std::istream&in, std::ostream&out)
buf[j] = buf[j] ^ cipbuf[j]; buf[j] = buf[j] ^ cipbuf[j];
} }
//verify the size bytes_read -= hashes_size;
bytes_read -= (4 + hashes_size);
uint blksizeid = bytes_read;
for (uint i = 0; i < 4; ++i) {
if (buf[i] != (blksizeid & 0xff) ) {
err ("symkey: mangled input");
return 3;
}
blksizeid >>= 8;
}
//verify the hashes //verify the hashes
uint hashpos = 4 + bytes_read; uint hashpos = bytes_read;
for (hashes_t::iterator i = hs.begin(), e = hs.end(); for (hashes_t::iterator i = hs.begin(), e = hs.end();
i != e; ++i) { i != e; ++i) {
hash_proc&hp = **i; hash_proc&hp = **i;
hp.init(); hp.init();
hp.eat (& (buf[0]), & (buf[4 + bytes_read]) ); hp.eat (& (buf[0]), & (buf[bytes_read]) );
hp.eat (key); hp.eat (key);
hp.eat (otkey); hp.eat (otkey);
std::vector<byte> res = hp.finish(); std::vector<byte> res = hp.finish();
@ -326,7 +308,7 @@ int symkey::decrypt (std::istream&in, std::ostream&out)
} }
//now that all is OK, output! //now that all is OK, output!
out.write ( (char*) & (buf[4]), bytes_read); out.write ( (char*) & (buf[0]), bytes_read);
//last one //last one
if (bytes_read < blocksize) break; if (bytes_read < blocksize) break;