xref: /aosp_15_r20/external/tink/cc/streamingaead/decrypting_input_stream.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2019 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 ///////////////////////////////////////////////////////////////////////////////
16 
17 #include "tink/streamingaead/decrypting_input_stream.h"
18 
19 #include <algorithm>
20 #include <cstring>
21 #include <memory>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 
26 #include "absl/memory/memory.h"
27 #include "absl/status/status.h"
28 #include "tink/input_stream.h"
29 #include "tink/primitive_set.h"
30 #include "tink/streaming_aead.h"
31 #include "tink/streamingaead/buffered_input_stream.h"
32 #include "tink/streamingaead/shared_input_stream.h"
33 #include "tink/util/errors.h"
34 #include "tink/util/status.h"
35 #include "tink/util/statusor.h"
36 
37 namespace crypto {
38 namespace tink {
39 namespace streamingaead {
40 
41 using crypto::tink::PrimitiveSet;
42 using crypto::tink::StreamingAead;
43 using util::Status;
44 using util::StatusOr;
45 
46 using StreamingAeadEntry = PrimitiveSet<StreamingAead>::Entry<StreamingAead>;
47 
48 // static
New(std::shared_ptr<PrimitiveSet<StreamingAead>> primitives,std::unique_ptr<crypto::tink::InputStream> ciphertext_source,absl::string_view associated_data)49 StatusOr<std::unique_ptr<InputStream>> DecryptingInputStream::New(
50     std::shared_ptr<PrimitiveSet<StreamingAead>> primitives,
51     std::unique_ptr<crypto::tink::InputStream> ciphertext_source,
52     absl::string_view associated_data) {
53   std::unique_ptr<DecryptingInputStream> dec_stream(
54       new DecryptingInputStream());
55   dec_stream->primitives_ = primitives;
56   dec_stream->buffered_ct_source_ =
57       std::make_shared<BufferedInputStream>(std::move(ciphertext_source));
58   dec_stream->associated_data_ = std::string(associated_data);
59   dec_stream->attempted_matching_ = false;
60   dec_stream->matching_stream_ = nullptr;
61   return {std::move(dec_stream)};
62 }
63 
Next(const void ** data)64 util::StatusOr<int> DecryptingInputStream::Next(const void** data) {
65   if (matching_stream_ != nullptr) {
66     return matching_stream_->Next(data);
67   }
68   if (attempted_matching_) {
69     return Status(absl::StatusCode::kInvalidArgument,
70                   "Could not find a decrypter matching the ciphertext stream.");
71   }
72   // Matching has not been attempted yet, so try it now.
73   attempted_matching_ = true;
74   std::vector<StreamingAeadEntry*> all_primitives = primitives_->get_all();
75 
76   for (const StreamingAeadEntry* entry : all_primitives) {
77     StreamingAead& streaming_aead = entry->get_primitive();
78     auto shared_ct = absl::make_unique<SharedInputStream>(
79         buffered_ct_source_.get());
80     auto decrypting_stream_result = streaming_aead.NewDecryptingStream(
81         std::move(shared_ct), associated_data_);
82     if (decrypting_stream_result.ok()) {
83       auto next_result = decrypting_stream_result.value()->Next(data);
84       if (next_result.status().code() == absl::StatusCode::kOutOfRange ||
85           next_result.ok()) {  // Found a match.
86         buffered_ct_source_->DisableRewinding();
87         matching_stream_ = std::move(decrypting_stream_result.value());
88         return next_result;
89       }
90     }
91     // Not a match, rewind and try the next primitive.
92     Status s = buffered_ct_source_->Rewind();
93     if (!s.ok()) {
94       return s;
95     }
96   }
97   return Status(absl::StatusCode::kInvalidArgument,
98                 "Could not find a decrypter matching the ciphertext stream.");
99 }
100 
BackUp(int count)101 void DecryptingInputStream::BackUp(int count) {
102   if (matching_stream_ != nullptr) {
103     matching_stream_->BackUp(count);
104   }
105 }
106 
107 
Position() const108 int64_t DecryptingInputStream::Position() const {
109   if (matching_stream_ != nullptr) {
110     return matching_stream_->Position();
111   }
112   return 0;
113 }
114 
115 }  // namespace streamingaead
116 }  // namespace tink
117 }  // namespace crypto
118