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/public_key_sign.h"
18
19 #include <string>
20 #include <utility>
21
22 #include "pybind11/pybind11.h"
23 #include "tink/public_key_sign.h"
24 #include "tink/util/statusor.h"
25 #include "tink/cc/pybind/tink_exception.h"
26
27 namespace crypto {
28 namespace tink {
29
30 using pybind11::google_tink::TinkException;
31
PybindRegisterPublicKeySign(pybind11::module * module)32 void PybindRegisterPublicKeySign(pybind11::module* module) {
33 namespace py = pybind11;
34 py::module& m = *module;
35
36 // TODO(b/146492561): Reduce the number of complicated lambdas.
37 py::class_<PublicKeySign>(
38 m, "PublicKeySign",
39 "Interface for public key signing. "
40 "Digital Signatures provide functionality of signing data and "
41 "verification of the signatures. They are represented by a pair of "
42 "primitives (interfaces) 'PublicKeySign' for signing of data, and "
43 "'PublicKeyVerify' for verification of signatures. Implementations of "
44 "these interfaces are secure against adaptive chosen-message attacks. "
45 "Signing data ensures the authenticity and the integrity of that data, "
46 "but not its secrecy.")
47
48 .def(
49 "sign",
50 [](const PublicKeySign& self,
51 const py::bytes& data) -> py::bytes {
52 // TODO(b/145925674)
53 util::StatusOr<std::string> result = self.Sign(std::string(data));
54 if (!result.ok()) {
55 throw TinkException(result.status());
56 }
57 return *std::move(result);
58 },
59 py::arg("data"), "Computes the signature for 'data'.");
60 }
61
62 } // namespace tink
63 } // namespace crypto
64