• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/cc/pybind/cc_streaming_aead_wrappers.h"
18 
19 #include <string>
20 #include <utility>
21 
22 #include "pybind11/pybind11.h"
23 #include "tink/cc/cc_streaming_aead_wrappers.h"
24 #include "tink/cc/pybind/import_helper.h"
25 #include "tink/cc/pybind/tink_exception.h"
26 
27 namespace crypto {
28 namespace tink {
29 
30 using pybind11::google_tink::TinkException;
31 
PybindRegisterCcStreamingAeadWrappers(pybind11::module * module)32 void PybindRegisterCcStreamingAeadWrappers(pybind11::module* module) {
33   namespace py = pybind11;
34   py::module& m = *module;
35 
36   // TODO(b/146492561): Reduce the number of complicated lambdas.
37   m.def(
38       "new_cc_encrypting_stream",
39       // TODO(b/145925674)
40       [](StreamingAead* streaming_aead, const py::bytes& aad,
41          std::shared_ptr<PythonFileObjectAdapter> ciphertext_destination)
42           -> std::unique_ptr<OutputStreamAdapter> {
43         util::StatusOr<std::unique_ptr<OutputStreamAdapter>> result_stream =
44             NewCcEncryptingStream(streaming_aead, std::string(aad),
45                                   ciphertext_destination);
46         if (!result_stream.ok()) {
47           throw TinkException(result_stream.status());
48         }
49         return *std::move(result_stream);
50       },
51       py::arg("primitive"), py::arg("aad"), py::arg("destination"),
52       // Keep destination alive at least as long as OutputStreamAdapter.
53       py::keep_alive<0, 3>());
54 
55   m.def(
56       "new_cc_decrypting_stream",
57       // TODO(b/145925674)
58       [](StreamingAead* streaming_aead, const py::bytes& aad,
59          std::shared_ptr<PythonFileObjectAdapter> ciphertext_source)
60           -> std::unique_ptr<InputStreamAdapter> {
61         util::StatusOr<std::unique_ptr<InputStreamAdapter>> result_stream =
62             NewCcDecryptingStream(streaming_aead, std::string(aad),
63                                   ciphertext_source);
64         if (!result_stream.ok()) {
65           throw TinkException(result_stream.status());
66         }
67         return *std::move(result_stream);
68       },
69       py::arg("primitive"), py::arg("aad"), py::arg("source"),
70       // Keep source alive at least as long as InputStreamAdapter.
71       py::keep_alive<0, 3>());
72 }
73 
74 }  // namespace tink
75 }  // namespace crypto
76