aboutsummaryrefslogtreecommitdiff
path: root/cc/subtle/nonce_based_streaming_aead.cc
blob: 4a346d33f160eb00cac527389757a6cc88c10bb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2019 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///////////////////////////////////////////////////////////////////////////////

#include "tink/subtle/nonce_based_streaming_aead.h"

#include <memory>
#include <utility>

#include "absl/strings/string_view.h"
#include "tink/input_stream.h"
#include "tink/output_stream.h"
#include "tink/random_access_stream.h"
#include "tink/streaming_aead.h"
#include "tink/subtle/decrypting_random_access_stream.h"
#include "tink/subtle/stream_segment_decrypter.h"
#include "tink/subtle/stream_segment_encrypter.h"
#include "tink/subtle/streaming_aead_decrypting_stream.h"
#include "tink/subtle/streaming_aead_encrypting_stream.h"
#include "tink/util/statusor.h"

namespace crypto {
namespace tink {
namespace subtle {

crypto::tink::util::StatusOr<std::unique_ptr<crypto::tink::OutputStream>>
    NonceBasedStreamingAead::NewEncryptingStream(
        std::unique_ptr<crypto::tink::OutputStream> ciphertext_destination,
        absl::string_view associated_data) const {
  auto segment_encrypter_result = NewSegmentEncrypter(associated_data);
  if (!segment_encrypter_result.ok()) return segment_encrypter_result.status();
  return StreamingAeadEncryptingStream::New(
      std::move(segment_encrypter_result.value()),
      std::move(ciphertext_destination));
}

crypto::tink::util::StatusOr<std::unique_ptr<crypto::tink::InputStream>>
    NonceBasedStreamingAead::NewDecryptingStream(
        std::unique_ptr<crypto::tink::InputStream> ciphertext_source,
        absl::string_view associated_data) const {
  auto segment_decrypter_result = NewSegmentDecrypter(associated_data);
  if (!segment_decrypter_result.ok()) return segment_decrypter_result.status();
  return StreamingAeadDecryptingStream::New(
      std::move(segment_decrypter_result.value()),
      std::move(ciphertext_source));
}

crypto::tink::util::StatusOr<std::unique_ptr<crypto::tink::RandomAccessStream>>
    NonceBasedStreamingAead::NewDecryptingRandomAccessStream(
        std::unique_ptr<crypto::tink::RandomAccessStream> ciphertext_source,
        absl::string_view associated_data) const {
  auto segment_decrypter_result = NewSegmentDecrypter(associated_data);
  if (!segment_decrypter_result.ok()) return segment_decrypter_result.status();
  return DecryptingRandomAccessStream::New(
      std::move(segment_decrypter_result.value()),
      std::move(ciphertext_source));
}

}  // namespace subtle
}  // namespace tink
}  // namespace crypto