diff options
author | lloyd <[email protected]> | 2012-05-18 13:58:23 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2012-05-18 13:58:23 +0000 |
commit | 2c3dc93d6466a9215d585613fb55f5222ce70d10 (patch) | |
tree | 9d44c738fdbb79103bed2d34c8fb171f8eaa00a2 /src | |
parent | c2d0d2982e96ab7ce15e90b8d73c9774e2650d86 (diff) |
First step towards replacing the existing containers with std::vector
with a custom allocator; remove the 3 argument version of
MemoryRegion::copy, replacing with freestanding buffer_insert
function.
Diffstat (limited to 'src')
-rw-r--r-- | src/alloc/secmem.h | 36 | ||||
-rw-r--r-- | src/filters/codec_filt/b64_filt.cpp | 2 | ||||
-rw-r--r-- | src/filters/codec_filt/hex_filt.cpp | 2 | ||||
-rw-r--r-- | src/filters/modes/cfb/cfb.cpp | 12 | ||||
-rw-r--r-- | src/filters/modes/cts/cts.cpp | 8 | ||||
-rw-r--r-- | src/filters/modes/eax/eax_dec.cpp | 2 | ||||
-rw-r--r-- | src/filters/modes/xts/xts.cpp | 16 | ||||
-rw-r--r-- | src/hash/gost_3411/gost_3411.cpp | 4 | ||||
-rw-r--r-- | src/hash/md2/md2.cpp | 4 | ||||
-rw-r--r-- | src/hash/mdx_hash/mdx_hash.cpp | 4 | ||||
-rw-r--r-- | src/hash/skein/skein_512.cpp | 4 | ||||
-rw-r--r-- | src/mac/cmac/cmac.cpp | 2 | ||||
-rw-r--r-- | src/pbkdf/pgps2k/pgp_s2k.cpp | 2 | ||||
-rw-r--r-- | src/pk_pad/eme1/eme1.cpp | 6 | ||||
-rw-r--r-- | src/pk_pad/eme_pkcs/eme_pkcs.cpp | 2 | ||||
-rw-r--r-- | src/pk_pad/emsa2/emsa2.cpp | 2 | ||||
-rw-r--r-- | src/pk_pad/emsa3/emsa3.cpp | 4 | ||||
-rw-r--r-- | src/pk_pad/emsa4/emsa4.cpp | 6 | ||||
-rw-r--r-- | src/pubkey/dlies/dlies.cpp | 2 | ||||
-rw-r--r-- | src/stream/ctr/ctr.cpp | 4 | ||||
-rw-r--r-- | src/stream/ofb/ofb.cpp | 2 |
21 files changed, 73 insertions, 53 deletions
diff --git a/src/alloc/secmem.h b/src/alloc/secmem.h index 884f2ebc0..e6b2b71ec 100644 --- a/src/alloc/secmem.h +++ b/src/alloc/secmem.h @@ -117,23 +117,16 @@ class MemoryRegion * @param in the array to copy the contents from * @param n the length of in */ + void copy(const T in[], size_t n) { copy_mem(buf, in, std::min(n, size())); } - /** - * Copy the contents of an array of objects of type T into this buffer. - * The former contents of *this are discarded. - * The length of *this must be at least n, otherwise memory errors occur. - * @param off the offset position inside this buffer to start inserting - * the copied bytes - * @param in the array to copy the contents from - * @param n the length of in - */ - void copy(size_t off, const T in[], size_t n) + void assign(const T* start, const T* end) { - copy_mem(buf + off, in, std::min(n, size() - off)); + resize(end - start); + copy_mem(buf, start, (end - start)); } /** @@ -370,6 +363,27 @@ class SecureVector : public MemoryRegion<T> }; template<typename T> +size_t buffer_insert(MemoryRegion<T>& buf, + size_t buf_offset, + const T input[], + size_t input_length) + { + const size_t to_copy = std::min(input_length, buf.size() - buf_offset); + copy_mem(&buf[buf_offset], input, to_copy); + return to_copy; + } + +template<typename T> +size_t buffer_insert(MemoryRegion<T>& buf, + size_t buf_offset, + const MemoryRegion<T>& input) + { + const size_t to_copy = std::min(input.size(), buf.size() - buf_offset); + copy_mem(&buf[buf_offset], &input[0], to_copy); + return to_copy; + } + +template<typename T> MemoryRegion<T>& operator+=(MemoryRegion<T>& out, const MemoryRegion<T>& in) { diff --git a/src/filters/codec_filt/b64_filt.cpp b/src/filters/codec_filt/b64_filt.cpp index 9341571d4..55eaac965 100644 --- a/src/filters/codec_filt/b64_filt.cpp +++ b/src/filters/codec_filt/b64_filt.cpp @@ -79,7 +79,7 @@ void Base64_Encoder::do_output(const byte input[], size_t length) */ void Base64_Encoder::write(const byte input[], size_t length) { - in.copy(position, input, length); + buffer_insert(in, position, input, length); if(position + length >= in.size()) { encode_and_send(&in[0], in.size()); diff --git a/src/filters/codec_filt/hex_filt.cpp b/src/filters/codec_filt/hex_filt.cpp index 3d56beec4..47995f928 100644 --- a/src/filters/codec_filt/hex_filt.cpp +++ b/src/filters/codec_filt/hex_filt.cpp @@ -75,7 +75,7 @@ void Hex_Encoder::encode_and_send(const byte block[], size_t length) */ void Hex_Encoder::write(const byte input[], size_t length) { - in.copy(position, input, length); + buffer_insert(in, position, input, length); if(position + length >= in.size()) { encode_and_send(&in[0], in.size()); diff --git a/src/filters/modes/cfb/cfb.cpp b/src/filters/modes/cfb/cfb.cpp index 22d4a9c2e..9aa6f6159 100644 --- a/src/filters/modes/cfb/cfb.cpp +++ b/src/filters/modes/cfb/cfb.cpp @@ -82,7 +82,10 @@ void CFB_Encryption::write(const byte input[], size_t length) { for(size_t j = 0; j != cipher->block_size() - feedback; ++j) state[j] = state[j + feedback]; - state.copy(cipher->block_size() - feedback, buffer, feedback); + + buffer_insert(state, cipher->block_size() - feedback, + &buffer[0], feedback); + cipher->encrypt(state, buffer); position = 0; } @@ -151,7 +154,7 @@ void CFB_Decryption::write(const byte input[], size_t length) size_t xored = std::min(feedback - position, length); xor_buf(&buffer[position], input, xored); send(&buffer[position], xored); - buffer.copy(position, input, xored); + buffer_insert(buffer, position, input, xored); input += xored; length -= xored; position += xored; @@ -159,7 +162,10 @@ void CFB_Decryption::write(const byte input[], size_t length) { for(size_t j = 0; j != cipher->block_size() - feedback; ++j) state[j] = state[j + feedback]; - state.copy(cipher->block_size() - feedback, buffer, feedback); + + buffer_insert(state, cipher->block_size() - feedback, + &buffer[0], feedback); + cipher->encrypt(state, buffer); position = 0; } diff --git a/src/filters/modes/cts/cts.cpp b/src/filters/modes/cts/cts.cpp index c654c8719..bc9c99d73 100644 --- a/src/filters/modes/cts/cts.cpp +++ b/src/filters/modes/cts/cts.cpp @@ -67,7 +67,7 @@ void CTS_Encryption::encrypt(const byte block[]) void CTS_Encryption::write(const byte input[], size_t length) { size_t copied = std::min<size_t>(buffer.size() - position, length); - buffer.copy(position, input, copied); + buffer_insert(buffer, position, input, copied); length -= copied; input += copied; position += copied; @@ -91,7 +91,7 @@ void CTS_Encryption::write(const byte input[], size_t length) copy_mem(&buffer[0], &buffer[cipher->block_size()], cipher->block_size()); position = cipher->block_size(); } - buffer.copy(position, input, length); + buffer_insert(buffer, position, input, length); position += length; } @@ -170,7 +170,7 @@ void CTS_Decryption::decrypt(const byte block[]) void CTS_Decryption::write(const byte input[], size_t length) { size_t copied = std::min<size_t>(buffer.size() - position, length); - buffer.copy(position, input, copied); + buffer_insert(buffer, position, input, copied); length -= copied; input += copied; position += copied; @@ -194,7 +194,7 @@ void CTS_Decryption::write(const byte input[], size_t length) copy_mem(&buffer[0], &buffer[cipher->block_size()], cipher->block_size()); position = cipher->block_size(); } - buffer.copy(position, input, length); + buffer_insert(buffer, position, input, length); position += length; } diff --git a/src/filters/modes/eax/eax_dec.cpp b/src/filters/modes/eax/eax_dec.cpp index 72e2249ac..3cf29a532 100644 --- a/src/filters/modes/eax/eax_dec.cpp +++ b/src/filters/modes/eax/eax_dec.cpp @@ -47,7 +47,7 @@ void EAX_Decryption::write(const byte input[], size_t length) { const size_t copied = std::min<size_t>(length, queue.size() - queue_end); - queue.copy(queue_end, input, copied); + buffer_insert(queue, queue_end, input, copied); input += copied; length -= copied; queue_end += copied; diff --git a/src/filters/modes/xts/xts.cpp b/src/filters/modes/xts/xts.cpp index 2d2957088..317edf1f8 100644 --- a/src/filters/modes/xts/xts.cpp +++ b/src/filters/modes/xts/xts.cpp @@ -97,9 +97,9 @@ void XTS_Encryption::set_iv(const InitializationVector& iv) for(size_t i = 1; i < blocks_in_tweak; ++i) { - tweak.copy(i*cipher->block_size(), - &tweak[(i-1)*cipher->block_size()], - cipher->block_size()); + buffer_insert(tweak, i*cipher->block_size(), + &tweak[(i-1)*cipher->block_size()], + cipher->block_size()); poly_double(&tweak[i*cipher->block_size()], cipher->block_size()); } @@ -157,7 +157,7 @@ void XTS_Encryption::buffered_block(const byte input[], size_t length) for(size_t i = 1; i < blocks_in_tweak; ++i) { - tweak.copy(i*cipher->block_size(), + buffer_insert(tweak, i*cipher->block_size(), &tweak[(i-1)*cipher->block_size()], cipher->block_size()); @@ -269,7 +269,7 @@ void XTS_Decryption::set_iv(const InitializationVector& iv) for(size_t i = 1; i < blocks_in_tweak; ++i) { - tweak.copy(i*cipher->block_size(), + buffer_insert(tweak, i*cipher->block_size(), &tweak[(i-1)*cipher->block_size()], cipher->block_size()); @@ -330,9 +330,9 @@ void XTS_Decryption::buffered_block(const byte input[], size_t input_length) for(size_t i = 1; i < blocks_in_tweak; ++i) { - tweak.copy(i*cipher->block_size(), - &tweak[(i-1)*cipher->block_size()], - cipher->block_size()); + buffer_insert(tweak, i*cipher->block_size(), + &tweak[(i-1)*cipher->block_size()], + cipher->block_size()); poly_double(&tweak[i*cipher->block_size()], cipher->block_size()); } diff --git a/src/hash/gost_3411/gost_3411.cpp b/src/hash/gost_3411/gost_3411.cpp index c0f39da47..6996e45e6 100644 --- a/src/hash/gost_3411/gost_3411.cpp +++ b/src/hash/gost_3411/gost_3411.cpp @@ -43,7 +43,7 @@ void GOST_34_11::add_data(const byte input[], size_t length) if(position) { - buffer.copy(position, input, length); + buffer_insert(buffer, position, input, length); if(position + length >= hash_block_size()) { @@ -60,7 +60,7 @@ void GOST_34_11::add_data(const byte input[], size_t length) if(full_blocks) compress_n(input, full_blocks); - buffer.copy(position, input + full_blocks * hash_block_size(), remaining); + buffer_insert(buffer, position, input + full_blocks * hash_block_size(), remaining); position += remaining; } diff --git a/src/hash/md2/md2.cpp b/src/hash/md2/md2.cpp index 761528dc6..f44053a1c 100644 --- a/src/hash/md2/md2.cpp +++ b/src/hash/md2/md2.cpp @@ -39,7 +39,7 @@ void MD2::hash(const byte input[]) 0x31, 0x44, 0x50, 0xB4, 0x8F, 0xED, 0x1F, 0x1A, 0xDB, 0x99, 0x8D, 0x33, 0x9F, 0x11, 0x83, 0x14 }; - X.copy(16, input, hash_block_size()); + buffer_insert(X, 16, input, hash_block_size()); xor_buf(&X[32], &X[0], &X[16], hash_block_size()); byte T = 0; @@ -66,7 +66,7 @@ void MD2::hash(const byte input[]) */ void MD2::add_data(const byte input[], size_t length) { - buffer.copy(position, input, length); + buffer_insert(buffer, position, input, length); if(position + length >= hash_block_size()) { diff --git a/src/hash/mdx_hash/mdx_hash.cpp b/src/hash/mdx_hash/mdx_hash.cpp index 7bfcf6592..81042c1fa 100644 --- a/src/hash/mdx_hash/mdx_hash.cpp +++ b/src/hash/mdx_hash/mdx_hash.cpp @@ -44,7 +44,7 @@ void MDx_HashFunction::add_data(const byte input[], size_t length) if(position) { - buffer.copy(position, input, length); + buffer_insert(buffer, position, input, length); if(position + length >= buffer.size()) { @@ -61,7 +61,7 @@ void MDx_HashFunction::add_data(const byte input[], size_t length) if(full_blocks) compress_n(input, full_blocks); - buffer.copy(position, input + full_blocks * buffer.size(), remaining); + buffer_insert(buffer, position, input + full_blocks * buffer.size(), remaining); position += remaining; } diff --git a/src/hash/skein/skein_512.cpp b/src/hash/skein/skein_512.cpp index 47bee5274..f6541fba7 100644 --- a/src/hash/skein/skein_512.cpp +++ b/src/hash/skein/skein_512.cpp @@ -210,7 +210,7 @@ void Skein_512::add_data(const byte input[], size_t length) if(buf_pos) { - buffer.copy(buf_pos, input, length); + buffer_insert(buffer, buf_pos, input, length); if(buf_pos + length > 64) { ubi_512(H, T, &buffer[0], buffer.size()); @@ -228,7 +228,7 @@ void Skein_512::add_data(const byte input[], size_t length) length -= full_blocks * 64; - buffer.copy(buf_pos, input + full_blocks * 64, length); + buffer_insert(buffer, buf_pos, input + full_blocks * 64, length); buf_pos += length; } diff --git a/src/mac/cmac/cmac.cpp b/src/mac/cmac/cmac.cpp index baf22f4e8..adc08852c 100644 --- a/src/mac/cmac/cmac.cpp +++ b/src/mac/cmac/cmac.cpp @@ -38,7 +38,7 @@ SecureVector<byte> CMAC::poly_double(const MemoryRegion<byte>& in, */ void CMAC::add_data(const byte input[], size_t length) { - buffer.copy(position, input, length); + buffer_insert(buffer, position, input, length); if(position + length > output_length()) { xor_buf(state, buffer, output_length()); diff --git a/src/pbkdf/pgps2k/pgp_s2k.cpp b/src/pbkdf/pgps2k/pgp_s2k.cpp index 9cec7304c..4ee4c6bd9 100644 --- a/src/pbkdf/pgps2k/pgp_s2k.cpp +++ b/src/pbkdf/pgps2k/pgp_s2k.cpp @@ -46,7 +46,7 @@ OctetString OpenPGP_S2K::derive_key(size_t key_len, } hash_buf = hash->final(); - key.copy(generated, &hash_buf[0], hash->output_length()); + buffer_insert(key, generated, &hash_buf[0], hash->output_length()); generated += hash->output_length(); ++pass; } diff --git a/src/pk_pad/eme1/eme1.cpp b/src/pk_pad/eme1/eme1.cpp index 1cc0c332d..69251605f 100644 --- a/src/pk_pad/eme1/eme1.cpp +++ b/src/pk_pad/eme1/eme1.cpp @@ -28,9 +28,9 @@ SecureVector<byte> EME1::pad(const byte in[], size_t in_length, rng.randomize(&out[0], Phash.size()); - out.copy(Phash.size(), &Phash[0], Phash.size()); + buffer_insert(out, Phash.size(), &Phash[0], Phash.size()); out[out.size() - in_length - 1] = 0x01; - out.copy(out.size() - in_length, in, in_length); + buffer_insert(out, out.size() - in_length, in, in_length); mgf->mask(&out[0], Phash.size(), &out[Phash.size()], out.size() - Phash.size()); @@ -66,7 +66,7 @@ SecureVector<byte> EME1::unpad(const byte in[], size_t in_length, in_length = 0; SecureVector<byte> input(key_length); - input.copy(key_length - in_length, in, in_length); + buffer_insert(input, key_length - in_length, in, in_length); mgf->mask(&input[Phash.size()], input.size() - Phash.size(), &input[0], Phash.size()); diff --git a/src/pk_pad/eme_pkcs/eme_pkcs.cpp b/src/pk_pad/eme_pkcs/eme_pkcs.cpp index c4d6838b1..a217d6d03 100644 --- a/src/pk_pad/eme_pkcs/eme_pkcs.cpp +++ b/src/pk_pad/eme_pkcs/eme_pkcs.cpp @@ -29,7 +29,7 @@ SecureVector<byte> EME_PKCS1v15::pad(const byte in[], size_t inlen, for(size_t j = 1; j != olen - inlen - 1; ++j) while(out[j] == 0) out[j] = rng.next_byte(); - out.copy(olen - inlen, in, inlen); + buffer_insert(out, olen - inlen, in, inlen); return out; } diff --git a/src/pk_pad/emsa2/emsa2.cpp b/src/pk_pad/emsa2/emsa2.cpp index 96ac8e908..50ea7dbe3 100644 --- a/src/pk_pad/emsa2/emsa2.cpp +++ b/src/pk_pad/emsa2/emsa2.cpp @@ -39,7 +39,7 @@ SecureVector<byte> emsa2_encoding(const MemoryRegion<byte>& msg, output[0] = (empty ? 0x4B : 0x6B); output[output_length - 3 - HASH_SIZE] = 0xBA; set_mem(&output[1], output_length - 4 - HASH_SIZE, 0xBB); - output.copy(output_length - (HASH_SIZE + 2), &msg[0], msg.size()); + buffer_insert(output, output_length - (HASH_SIZE + 2), &msg[0], msg.size()); output[output_length-2] = hash_id; output[output_length-1] = 0xCC; diff --git a/src/pk_pad/emsa3/emsa3.cpp b/src/pk_pad/emsa3/emsa3.cpp index a381a82f6..6532bafd4 100644 --- a/src/pk_pad/emsa3/emsa3.cpp +++ b/src/pk_pad/emsa3/emsa3.cpp @@ -30,8 +30,8 @@ SecureVector<byte> emsa3_encoding(const MemoryRegion<byte>& msg, T[0] = 0x01; set_mem(&T[1], P_LENGTH, 0xFF); T[P_LENGTH+1] = 0x00; - T.copy(P_LENGTH+2, hash_id, hash_id_length); - T.copy(output_length-msg.size(), &msg[0], msg.size()); + buffer_insert(T, P_LENGTH+2, hash_id, hash_id_length); + buffer_insert(T, output_length-msg.size(), &msg[0], msg.size()); return T; } diff --git a/src/pk_pad/emsa4/emsa4.cpp b/src/pk_pad/emsa4/emsa4.cpp index ef88e1953..65078de5e 100644 --- a/src/pk_pad/emsa4/emsa4.cpp +++ b/src/pk_pad/emsa4/emsa4.cpp @@ -54,10 +54,10 @@ SecureVector<byte> EMSA4::encoding_of(const MemoryRegion<byte>& msg, SecureVector<byte> EM(output_length); EM[output_length - HASH_SIZE - SALT_SIZE - 2] = 0x01; - EM.copy(output_length - 1 - HASH_SIZE - SALT_SIZE, salt, SALT_SIZE); + buffer_insert(EM, output_length - 1 - HASH_SIZE - SALT_SIZE, salt); mgf->mask(H, HASH_SIZE, EM, output_length - HASH_SIZE - 1); EM[0] &= 0xFF >> (8 * ((output_bits + 7) / 8) - output_bits); - EM.copy(output_length - 1 - HASH_SIZE, H, HASH_SIZE); + buffer_insert(EM, output_length - 1 - HASH_SIZE, H); EM[output_length-1] = 0xBC; return EM; @@ -85,7 +85,7 @@ bool EMSA4::verify(const MemoryRegion<byte>& const_coded, if(coded.size() < KEY_BYTES) { SecureVector<byte> temp(KEY_BYTES); - temp.copy(KEY_BYTES - coded.size(), coded, coded.size()); + buffer_insert(temp, KEY_BYTES - coded.size(), coded); coded = temp; } diff --git a/src/pubkey/dlies/dlies.cpp b/src/pubkey/dlies/dlies.cpp index 2b3f65d06..129cc46d9 100644 --- a/src/pubkey/dlies/dlies.cpp +++ b/src/pubkey/dlies/dlies.cpp @@ -44,7 +44,7 @@ SecureVector<byte> DLIES_Encryptor::enc(const byte in[], size_t length, SecureVector<byte> out(my_key.size() + length + mac->output_length()); out.copy(&my_key[0], my_key.size()); - out.copy(my_key.size(), in, length); + buffer_insert(out, my_key.size(), in, length); SecureVector<byte> vz = my_key; vz += ka.derive_key(0, other_key).bits_of(); diff --git a/src/stream/ctr/ctr.cpp b/src/stream/ctr/ctr.cpp index 3a370eca3..8ceac858a 100644 --- a/src/stream/ctr/ctr.cpp +++ b/src/stream/ctr/ctr.cpp @@ -89,14 +89,14 @@ void CTR_BE::set_iv(const byte iv[], size_t iv_len) zeroise(counter); - counter.copy(0, iv, iv_len); + buffer_insert(counter, 0, iv, iv_len); /* * Set counter blocks to IV, IV + 1, ... IV + 255 */ for(size_t i = 1; i != 256; ++i) { - counter.copy(i*bs, &counter[(i-1)*bs], bs); + buffer_insert(counter, i*bs, &counter[(i-1)*bs], bs); for(size_t j = 0; j != bs; ++j) if(++counter[i*bs + (bs - 1 - j)]) diff --git a/src/stream/ofb/ofb.cpp b/src/stream/ofb/ofb.cpp index 382a2b4dd..02521581b 100644 --- a/src/stream/ofb/ofb.cpp +++ b/src/stream/ofb/ofb.cpp @@ -84,7 +84,7 @@ void OFB::set_iv(const byte iv[], size_t iv_len) throw Invalid_IV_Length(name(), iv_len); zeroise(buffer); - buffer.copy(0, iv, iv_len); + buffer_insert(buffer, 0, iv, iv_len); permutation->encrypt(buffer); position = 0; |