Repository: randombit/botan
Pull request #3187 (2023-01-17 19:38:00)

[TLS 1.3] further usage of std::span.

codecov-commenter: # [Codecov](https://codecov.io/gh/randombit/botan/pull/3187?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jack+Lloyd) Report
Base: **88.08**% // Head: **88.08**% // Increases project coverage by **`+0.00%`** :tada:
> Coverage data is based on head [(`925488c`)](https://codecov.io/gh/randombit/botan/pull/3187?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jack+Lloyd) compared to base [(`8c69a25`)](https://codecov.io/gh/randombit/botan/commit/8c69a2513af0868356833d7d9f8806eabb126082?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jack+Lloyd).
> Patch coverage: 100.00% of modified lines in pull request are covered.

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##           master    #3187   +/-   ##
=======================================
  Coverage   88.08%   88.08%           
=======================================
  Files         604      604           
  Lines       67326    67331    +5     
  Branches     6733     6736    +3     
=======================================
+ Hits        59301    59309    +8     
+ Misses       5203     5202    -1     
+ Partials     2822     2820    -2     
```


| [Impacted Files](https://codecov.io/gh/randombit/botan/pull/3187?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jack+Lloyd) | Coverage Δ | |
|---|---|---|
| [src/lib/tls/tls13/tls\_handshake\_layer\_13.cpp](https://codecov.io/gh/randombit/botan/pull/3187?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jack+Lloyd#diff-c3JjL2xpYi90bHMvdGxzMTMvdGxzX2hhbmRzaGFrZV9sYXllcl8xMy5jcHA=) | `97.05% <100.00%> (ø)` | |
| [src/lib/tls/tls13/tls\_transcript\_hash\_13.cpp](https://codecov.io/gh/randombit/botan/pull/3187?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jack+Lloyd#diff-c3JjL2xpYi90bHMvdGxzMTMvdGxzX3RyYW5zY3JpcHRfaGFzaF8xMy5jcHA=) | `94.18% <100.00%> (+0.13%)` | :arrow_up: |
| [src/tests/test\_tls\_transcript\_hash\_13.cpp](https://codecov.io/gh/randombit/botan/pull/3187?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jack+Lloyd#diff-c3JjL3Rlc3RzL3Rlc3RfdGxzX3RyYW5zY3JpcHRfaGFzaF8xMy5jcHA=) | `100.00% <100.00%> (ø)` | |
| [src/lib/pubkey/dl\_group/dl\_group.cpp](https://codecov.io/gh/randombit/botan/pull/3187?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jack+Lloyd#diff-c3JjL2xpYi9wdWJrZXkvZGxfZ3JvdXAvZGxfZ3JvdXAuY3Bw) | `84.42% <0.00%> (-0.68%)` | :arrow_down: |
| [src/cli/tls\_proxy.cpp](https://codecov.io/gh/randombit/botan/pull/3187?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jack+Lloyd#diff-c3JjL2NsaS90bHNfcHJveHkuY3Bw) | `81.81% <0.00%> (-0.54%)` | :arrow_down: |
| [src/tests/test\_bigint.cpp](https://codecov.io/gh/randombit/botan/pull/3187?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jack+Lloyd#diff-c3JjL3Rlc3RzL3Rlc3RfYmlnaW50LmNwcA==) | `90.81% <0.00%> (ø)` | |
| [src/lib/asn1/der\_enc.cpp](https://codecov.io/gh/randombit/botan/pull/3187?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jack+Lloyd#diff-c3JjL2xpYi9hc24xL2Rlcl9lbmMuY3Bw) | `78.94% <0.00%> (+1.31%)` | :arrow_up: |
| [src/fuzzer/invert.cpp](https://codecov.io/gh/randombit/botan/pull/3187?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jack+Lloyd#diff-c3JjL2Z1enplci9pbnZlcnQuY3Bw) | `75.67% <0.00%> (+1.99%)` | :arrow_up: |
| [src/fuzzer/mode\_padding.cpp](https://codecov.io/gh/randombit/botan/pull/3187?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jack+Lloyd#diff-c3JjL2Z1enplci9tb2RlX3BhZGRpbmcuY3Bw) | `96.49% <0.00%> (+5.75%)` | :arrow_up: |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jack+Lloyd). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jack+Lloyd)

</details>

[:umbrella: View full report at Codecov](https://codecov.io/gh/randombit/botan/pull/3187?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jack+Lloyd).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jack+Lloyd).


Patch:
diff --git a/src/lib/tls/tls13/tls_handshake_layer_13.cpp b/src/lib/tls/tls13/tls_handshake_layer_13.cpp
index 7f7db1917..fcb1a38f6 100644
--- a/src/lib/tls/tls13/tls_handshake_layer_13.cpp
+++ b/src/lib/tls/tls13/tls_handshake_layer_13.cpp
@@ -130,7 +130,7 @@ std::optional<Handshake_Message_13> Handshake_Layer::next_message(const Policy&
    if(msg.has_value())
       {
       BOTAN_ASSERT_NOMSG(m_read_buffer.size() >= reader.read_so_far());
-      transcript_hash.update(m_read_buffer.data(), reader.read_so_far());
+      transcript_hash.update(std::span{m_read_buffer.data(), reader.read_so_far()});
       m_read_buffer.erase(m_read_buffer.cbegin(), m_read_buffer.cbegin() + reader.read_so_far());
       }
 
diff --git a/src/lib/tls/tls13/tls_transcript_hash_13.cpp b/src/lib/tls/tls13/tls_transcript_hash_13.cpp
index 677055a76..980666851 100644
--- a/src/lib/tls/tls13/tls_transcript_hash_13.cpp
+++ b/src/lib/tls/tls13/tls_transcript_hash_13.cpp
@@ -156,8 +156,10 @@ std::vector<uint8_t> read_hash_state(std::unique_ptr<HashFunction>& hash)
 
 }  // namespace
 
-void Transcript_Hash_State::update(const uint8_t* serialized_message, const size_t serialized_message_length)
+void Transcript_Hash_State::update(std::span<const uint8_t> serialized_message_s)
    {
+   auto serialized_message = serialized_message_s.data();
+   auto serialized_message_length = serialized_message_s.end() - serialized_message_s.begin();
    if(m_hash != nullptr)
       {
       auto truncation_mark = serialized_message_length;
diff --git a/src/lib/tls/tls13/tls_transcript_hash_13.h b/src/lib/tls/tls13/tls_transcript_hash_13.h
index db8cc5eb4..34662dffe 100644
--- a/src/lib/tls/tls13/tls_transcript_hash_13.h
+++ b/src/lib/tls/tls13/tls_transcript_hash_13.h
@@ -12,6 +12,7 @@
 #include <memory>
 #include <string>
 #include <vector>
+#include <span>
 
 #include <botan/hash.h>
 #include <botan/tls_magic.h>
@@ -49,13 +50,7 @@ class BOTAN_TEST_API Transcript_Hash_State
       Transcript_Hash_State(Transcript_Hash_State&&) = default;
       Transcript_Hash_State& operator=(Transcript_Hash_State&&) = default;
 
-      void update(const std::vector<uint8_t>& serialized_message)
-         {
-         update(serialized_message.data(), serialized_message.size());
-         }
-
-      // TODO: C++20 replace this C-style API with std::span
-      void update(const uint8_t* serialized_message, const size_t serialized_message_length);
+      void update(std::span<const uint8_t> serialized_message_s);
 
       /**
        * returns the latest transcript hash


Test patch:
diff --git a/src/tests/test_tls_transcript_hash_13.cpp b/src/tests/test_tls_transcript_hash_13.cpp
index e142a0607..9aef7bc6f 100644
--- a/src/tests/test_tls_transcript_hash_13.cpp
+++ b/src/tests/test_tls_transcript_hash_13.cpp
@@ -73,7 +73,7 @@ std::vector<Test::Result> transcript_hash()
       Botan_Tests::CHECK("update without an algorithm", [](Test::Result& result)
          {
          Transcript_Hash_State h;
-         result.test_no_throw("update is successful", [&] { h.update({0xba, 0xad, 0xbe, 0xef}); });
+         result.test_no_throw("update is successful", [&] { h.update(Botan::hex_decode("baadbeef")); });
          result.test_throws<Botan::Invalid_State>("previous throws invalid state exception",
                [&] { h.previous(); });
          result.test_throws<Botan::Invalid_State>("current throws invalid state exception",
@@ -98,12 +98,12 @@ std::vector<Test::Result> transcript_hash()
          {
          Transcript_Hash_State h("SHA-256");
 
-         h.update({0xba, 0xad, 0xbe, 0xef});
+         h.update(Botan::hex_decode("baadbeef"));
          result.test_throws<Botan::Invalid_State>("previous throws invalid state exception",
                [&] { h.previous(); });
          result.test_eq("c = SHA-256(baadbeef)", h.current(), sha256("baadbeef"));
 
-         h.update({0x60, 0x0d, 0xf0, 0x0d});
+         h.update(Botan::hex_decode("600df00d"));
          result.test_eq("p = SHA-256(baadbeef)", h.previous(), sha256("baadbeef"));
          result.test_eq("c = SHA-256(deadbeef | goodfood)", h.current(), sha256("baadbeef600df00d"));
          }),
@@ -112,7 +112,7 @@ std::vector<Test::Result> transcript_hash()
          {
          Transcript_Hash_State h;
 
-         h.update({0xba, 0xad, 0xbe, 0xef});
+         h.update(Botan::hex_decode("baadbeef"));
          h.set_algorithm("SHA-256");
 
          result.test_throws<Botan::Invalid_State>("previous throws invalid state exception",
@@ -124,8 +124,8 @@ std::vector<Test::Result> transcript_hash()
          {
          Transcript_Hash_State h;
 
-         h.update({0xba, 0xad, 0xbe, 0xef});
-         h.update({0x60, 0x0d, 0xf0, 0x0d});
+	 h.update(Botan::hex_decode("baadbeef"));
+	 h.update(Botan::hex_decode("600df00d"));
          h.set_algorithm("SHA-256");
 
          result.test_eq("c = SHA-256(baadbeef | goodfood)", h.current(), sha256("baadbeef600df00d"));
@@ -136,14 +136,14 @@ std::vector<Test::Result> transcript_hash()
          Transcript_Hash_State h;
 
          std::array<uint8_t, 2> baad{0xba, 0xad};
-         h.update(baad.data(), baad.size());
-         h.update({0xbe, 0xef});
+         h.update(baad);
+         h.update(std::array<uint8_t, 2>{0xbe, 0xef});
 
          h.set_algorithm("SHA-256");
 
          std::array<uint8_t, 2> food{0xf0, 0x0d};
-         h.update({0x60, 0x0d});
-         h.update(food.data(), food.size());
+         h.update(std::array<uint8_t, 2>{0x60, 0x0d});
+         h.update(food);
 
          result.test_eq("c = SHA-256(baadbeef | goodfood)", h.current(), sha256("baadbeef600df00d"));
          }),
@@ -152,14 +152,14 @@ std::vector<Test::Result> transcript_hash()
          {
          Transcript_Hash_State h1("SHA-256");
 
-         h1.update({0xba, 0xad, 0xbe, 0xef});
-         h1.update({0x60, 0x0d, 0xf0, 0x0d});
+         h1.update(std::array<uint8_t, 4>{0xba, 0xad, 0xbe, 0xef});
+         h1.update(std::array<uint8_t, 4>{0x60, 0x0d, 0xf0, 0x0d});
 
          auto h2 = h1.clone();
          result.test_eq("c1 = SHA-256(baadbeef | goodfood)", h1.current(), sha256("baadbeef600df00d"));
          result.test_eq("c2 = SHA-256(baadbeef | goodfood)", h2.current(), sha256("baadbeef600df00d"));
 
-         h1.update({0xca, 0xfe, 0xd0, 0x0d});
+         h1.update(std::array<uint8_t, 4>{0xca, 0xfe, 0xd0, 0x0d});
          result.test_eq("c1 = SHA-256(baadbeef | goodfood | cafedude)", h1.current(), sha256("baadbeef600df00dcafed00d"));
          result.test_eq("c2 = SHA-256(baadbeef | goodfood)", h2.current(), sha256("baadbeef600df00d"));
          }),
@@ -168,8 +168,8 @@ std::vector<Test::Result> transcript_hash()
          {
          Transcript_Hash_State h1;
 
-         h1.update({0xc0, 0xca, 0xc0, 0x1a} /* client hello 1 */);
-         h1.update({0xc0, 0x01, 0xf0, 0x0d} /* hello retry request */);
+         h1.update(std::array<uint8_t, 4>{0xc0, 0xca, 0xc0, 0x1a} /* client hello 1 */);
+         h1.update(std::array<uint8_t, 4>{0xc0, 0x01, 0xf0, 0x0d} /* hello retry request */);
 
          auto h2 = Transcript_Hash_State::recreate_after_hello_retry_request("SHA-256", h1);
 
@@ -194,7 +194,7 @@ std::vector<Test::Result> transcript_hash()
          result.test_eq("current hash", h1.current(), sha256_full_ch);
 
          // truncated hash is cleared as soon as new messages are read
-         h1.update({0xc0, 0xca, 0xc0, 0x1a} /* server hello */);
+         h1.update(std::array<uint8_t, 4>{0xc0, 0xca, 0xc0, 0x1a} /* server hello */);
          result.test_throws("truncated hash is cleared", [&] { h1.truncated(); });
          }),
       };