diff options
author | Jack Lloyd <[email protected]> | 2018-08-10 20:14:36 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-08-10 20:14:36 -0400 |
commit | 4c1129afb9c712f3de01d47992c9f52edfb7eee0 (patch) | |
tree | 9ae9b81f3a61d3ad206cf555943692ea56a0a1ea /src | |
parent | a584ca8d3dd9a7c62dd83a2b772d4645306c0bd1 (diff) |
Optimize computation of CTR input blocks
We don't need to read each block since we know what is there
Improves CTR perf with AES-NI by 5-6%, also helps GCM
GH #969
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/stream/ctr/ctr.cpp | 21 | ||||
-rw-r--r-- | src/tests/data/stream/ctr.vec | 2 |
2 files changed, 14 insertions, 9 deletions
diff --git a/src/lib/stream/ctr/ctr.cpp b/src/lib/stream/ctr/ctr.cpp index 3608eedf9..22cfade9b 100644 --- a/src/lib/stream/ctr/ctr.cpp +++ b/src/lib/stream/ctr/ctr.cpp @@ -142,37 +142,42 @@ void CTR_BE::add_counter(const uint64_t counter) if(ctr_size == 4) { size_t off = (BS - 4); + uint32_t low32 = counter + load_be<uint32_t>(&m_counter[off], 0); + for(size_t i = 0; i != ctr_blocks; ++i) { - uint32_t low32 = load_be<uint32_t>(&m_counter[off], 0); - low32 += counter; store_be(low32, &m_counter[off]); off += BS; + low32 += 1; } } else if(ctr_size == 8) { size_t off = (BS - 8); + uint64_t low64 = counter + load_be<uint64_t>(&m_counter[off], 0); + for(size_t i = 0; i != ctr_blocks; ++i) { - uint64_t low64 = load_be<uint64_t>(&m_counter[off], 0); - low64 += counter; store_be(low64, &m_counter[off]); off += BS; + low64 += 1; } } else if(ctr_size == 16) { size_t off = (BS - 16); + uint64_t b0 = load_be<uint64_t>(&m_counter[off], 0); + uint64_t b1 = load_be<uint64_t>(&m_counter[off], 1); + b1 += counter; + b0 += (b1 < counter) ? 1 : 0; // carry + for(size_t i = 0; i != ctr_blocks; ++i) { - uint64_t b0 = load_be<uint64_t>(&m_counter[off], 0); - uint64_t b1 = load_be<uint64_t>(&m_counter[off], 1); - b1 += counter; - b0 += (b1 < counter) ? 1 : 0; // carry store_be(b0, &m_counter[off]); store_be(b1, &m_counter[off+8]); off += BS; + b1 += 1; + b0 += (b1 == 0); // carry } } else diff --git a/src/tests/data/stream/ctr.vec b/src/tests/data/stream/ctr.vec index 5decae48e..c043ec529 100644 --- a/src/tests/data/stream/ctr.vec +++ b/src/tests/data/stream/ctr.vec @@ -283,7 +283,7 @@ Out = 9D [CTR-BE(AES-128,4)] Key = 2B7E151628AED2A6ABF7158809CF4F3C Nonce = FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF -Out = 8AF2860142F786F409307C1A3F7EAAAC597D5761063D8BAD232CB0136888AABB90B8CF63F44412CEEE802A522AB6566313C5E10652749056AD2F02CE3BBF5BEC +Out = 8AF2860142F786F409307C1A3F7EAAAC597D5761063D8BAD232CB0136888AABB90B8CF63F44412CEEE802A522AB6566313C5E10652749056AD2F02CE3BBF5BEC280D53A3DEB427CBD72262F24668144D91A7FDA59E051F2863443EF463514587B7BE1DBF1D6AA496AD0BDA0B040158A22CABBA7E1C4765DDDE9309E840112D643A6B34DFF3B75112C461F6C2C43C2DB4674750EFFFB2061881C4D6C59B88354D441603C178AAEE8C6E96245201F5F92C36B61565E0AA3C544A51BC5EB792FD15A3468FA2D9493063B3A09F27079EFDD25F04F945A715A9C5FC380EAF1059F7085959EBCE24D5D5FADB38A2D0F1BA26511518214A3A9D5E66031C18AF60DE74458BFF3B11648C195BF3531847B8E21BCD6EAF0D01C8841BDC9DED894A6D2A7AE2313E8FDBFCAE1820D0F89880179947A1A8BB697B44DE07061C1B4BD1851F1D02F6AC7EEDF089349CC9E0277CB599FC68AE1EFFDCCD7DA712423A9C871B6F5514C7AAAC01FCEA368DE3C4D42CB2F32714A0EEDCD1AC197C567AEFE974A74366FEFD2039D7734B4C295038B25FD6BE7CBAB6E3000A4E28ADF21C71EBB40FC0D3CD27BF0BA5302A0274EAD05A8AAE5FECCDBEA92666B9590874E24FA1EEA3AD3AA4EC89AF728D26B476D0BA8517D57ADBCFF5CDEF825EE3981198D538C80ED2529693AF0D2EA0A567AA21FEADD06E10A90169A06388F5EB7619730ACA4A724FFA67 [CTR-BE(AES-128,5)] Key = 2B7E151628AED2A6ABF7158809CF4F3C |