diff options
author | Ben Wiederhake <BenWiederhake.GitHub@gmx.de> | 2020-07-25 23:40:55 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-07-28 19:10:10 +0200 |
commit | a51cbc29781d4e711bd090dd9c89de39c9807556 (patch) | |
tree | 84a5c5561e9c5c726341e28772aeb2ac7e275e22 /Libraries | |
parent | ef4ce54b026a4115cf096531d11a0da0cff52d9e (diff) | |
download | serenity-a51cbc29781d4e711bd090dd9c89de39c9807556.zip |
LibCrypto: Fix broken CTR mode, implement RFC 3686 Test Vectors
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibCrypto/Cipher/Mode/CTR.h | 24 |
1 files changed, 12 insertions, 12 deletions
diff --git a/Libraries/LibCrypto/Cipher/Mode/CTR.h b/Libraries/LibCrypto/Cipher/Mode/CTR.h index 8a6ceb38d5..07e4e1a0f4 100644 --- a/Libraries/LibCrypto/Cipher/Mode/CTR.h +++ b/Libraries/LibCrypto/Cipher/Mode/CTR.h @@ -82,8 +82,9 @@ namespace Cipher { * * Due to this plethora of mutually-incompatible counters, * the method of counting should be a template parameter. - * This currently implements BIGINT_MIXEDENDIAN_INCR_0, which is not used - * anywhere else. + * This currently implements BIGINT_INCR_0, which means perfect + * interoperability with openssl. The test vectors from RFC 3686 just need to be + * incremented by 1. * TODO: Implement other counters? */ @@ -112,6 +113,8 @@ public: virtual Optional<ByteBuffer> encrypt(const ByteBuffer& in, ByteBuffer& out, Optional<ByteBuffer> ivec = {}) override { + // Our interpretation of "ivec" is what AES-CTR + // would define as nonce + IV + 4 zero bytes. return this->encrypt_or_stream(&in, out, ivec); } @@ -129,20 +132,17 @@ public: } private: - static ByteBuffer increment(const ByteBuffer& in) + static void increment_inplace(ByteBuffer& in) { - ByteBuffer new_buffer(in); - size_t* num_view = (size_t*)new_buffer.data(); - - for (size_t i = 0; i < in.size() / sizeof(size_t); ++i) { - if (num_view[i] == (size_t)-1) { - num_view[i] = 0; + for (size_t i = in.size(); i > 0;) { + --i; + if (in[i] == (u8)-1) { + in[i] = 0; } else { - num_view[i]++; + in[i]++; break; } } - return new_buffer; } Optional<ByteBuffer> encrypt_or_stream(const ByteBuffer* in, ByteBuffer& out, Optional<ByteBuffer> ivec) @@ -178,7 +178,7 @@ private: auto write_size = min(block_size, length); out.overwrite(offset, block.get().data(), write_size); - iv = increment(iv); + increment_inplace(iv); length -= write_size; offset += write_size; } |