@@ -57,27 +57,41 @@ std::array<std::byte, POLY1305_KEYLEN> GetPoly1305Key(ChaCha20& c20)
57
57
return polykey;
58
58
}
59
59
60
- void RFC8439Crypt (ChaCha20& c20, const Span<const std::byte> in_bytes, Span<std::byte> out_bytes)
60
+ void RFC8439Crypt (ChaCha20& c20, const std::vector< Span<const std::byte>>& in_bytes, Span<std::byte> out_bytes)
61
61
{
62
- assert (in_bytes.size () <= out_bytes.size ());
62
+ size_t total_bytes = 0 ;
63
+ for (auto in: in_bytes) {
64
+ total_bytes += in.size ();
65
+ }
66
+ assert (total_bytes <= out_bytes.size ());
63
67
c20.SeekRFC8439 (1 );
64
- c20.Crypt (reinterpret_cast <const unsigned char *>(in_bytes.data ()), reinterpret_cast <unsigned char *>(out_bytes.data ()), in_bytes.size ());
68
+
69
+ auto write_pos = out_bytes.data ();
70
+ for (auto in: in_bytes) {
71
+ c20.Crypt (reinterpret_cast <const unsigned char *>(in.data ()), reinterpret_cast <unsigned char *>(write_pos), in.size ());
72
+ write_pos += in.size ();
73
+ }
65
74
}
66
75
67
- void RFC8439Encrypt (const Span<const std::byte> aad, const Span<const std::byte> key, const std::array<std::byte, 12 >& nonce, const Span<const std::byte> plaintext , Span<std::byte> output)
76
+ void RFC8439Encrypt (const Span<const std::byte> aad, const Span<const std::byte> key, const std::array<std::byte, 12 >& nonce, const std::vector< Span<const std::byte>>& plaintexts , Span<std::byte> output)
68
77
{
69
78
assert (key.size () == RFC8439_KEYLEN);
70
- assert (output.size () >= plaintext.size () + POLY1305_TAGLEN);
71
79
72
80
ChaCha20 c20{reinterpret_cast <const unsigned char *>(key.data ()), key.size ()};
73
81
c20.SetRFC8439Nonce (nonce);
74
82
75
83
std::array<std::byte, POLY1305_KEYLEN> polykey{GetPoly1305Key (c20)};
76
84
77
- RFC8439Crypt (c20, plaintext, output);
85
+ size_t total_bytes = 0 ;
86
+ for (auto plaintext: plaintexts) {
87
+ total_bytes += plaintext.size ();
88
+ }
89
+
90
+ assert (output.size () >= total_bytes + POLY1305_TAGLEN);
91
+ RFC8439Crypt (c20, plaintexts, output);
78
92
ComputeRFC8439Tag (polykey, aad,
79
- {output.data (), plaintext. size () },
80
- {output.data () + plaintext. size () , POLY1305_TAGLEN});
93
+ {output.data (), total_bytes },
94
+ {output.data () + total_bytes , POLY1305_TAGLEN});
81
95
}
82
96
83
97
bool RFC8439Decrypt (const Span<const std::byte> aad, const Span<const std::byte> key, const std::array<std::byte, 12 >& nonce, const Span<const std::byte> input, Span<std::byte> plaintext)
@@ -98,6 +112,7 @@ bool RFC8439Decrypt(const Span<const std::byte> aad, const Span<const std::byte>
98
112
return false ;
99
113
}
100
114
101
- RFC8439Crypt (c20, {input.data (), input.size () - POLY1305_TAGLEN}, plaintext);
115
+ std::vector<Span<const std::byte>> ins{{input.data (), input.size () - POLY1305_TAGLEN}};
116
+ RFC8439Crypt (c20, ins, plaintext);
102
117
return true ;
103
118
}
0 commit comments