@@ -57,14 +57,23 @@ std::array<std::byte, POLY1305_KEYLEN> GetPoly1305Key(ChaCha20& c20)
57
57
return polykey;
58
58
}
59
59
60
- void RFC8439Crypt (ChaCha20& c20, 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
- RFC8439Encrypted RFC8439Encrypt (Span<const std::byte> aad, Span<const std::byte> key, const std::array<std::byte, 12 >& nonce, Span<const std::byte> plaintext )
76
+ RFC8439Encrypted RFC8439Encrypt (Span<const std::byte> aad, Span<const std::byte> key, const std::array<std::byte, 12 >& nonce, const std::vector< Span<const std::byte>>& plaintexts )
68
77
{
69
78
assert (key.size () == RFC8439_KEYLEN);
70
79
RFC8439Encrypted ret;
@@ -74,8 +83,13 @@ RFC8439Encrypted RFC8439Encrypt(Span<const std::byte> aad, Span<const std::byte>
74
83
75
84
std::array<std::byte, POLY1305_KEYLEN> polykey{GetPoly1305Key (c20)};
76
85
77
- ret.ciphertext .resize (plaintext.size ());
78
- RFC8439Crypt (c20, plaintext, ret.ciphertext );
86
+ size_t total_bytes = 0 ;
87
+ for (auto plaintext: plaintexts) {
88
+ total_bytes += plaintext.size ();
89
+ }
90
+
91
+ ret.ciphertext .resize (total_bytes);
92
+ RFC8439Crypt (c20, plaintexts, ret.ciphertext );
79
93
ret.tag = ComputeRFC8439Tag (polykey, aad, ret.ciphertext );
80
94
return ret;
81
95
}
@@ -101,6 +115,8 @@ RFC8439Decrypted RFC8439Decrypt(Span<const std::byte> aad, Span<const std::byte>
101
115
102
116
ret.success = true ;
103
117
ret.plaintext .resize (encrypted.ciphertext .size ());
104
- RFC8439Crypt (c20, encrypted.ciphertext , ret.plaintext );
118
+
119
+ std::vector<Span<const std::byte>> ins{encrypted.ciphertext };
120
+ RFC8439Crypt (c20, ins, ret.plaintext );
105
121
return ret;
106
122
}
0 commit comments