xref: /aosp_15_r20/external/tink/cc/subtle/rsa_ssa_pkcs1_verify_boringssl.h (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2018 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 #ifndef TINK_SUBTLE_RSA_SSA_PKCS1_VERIFY_BORINGSSL_H_
18 #define TINK_SUBTLE_RSA_SSA_PKCS1_VERIFY_BORINGSSL_H_
19 
20 #include <memory>
21 #include <utility>
22 
23 #include "absl/strings/string_view.h"
24 #include "openssl/evp.h"
25 #include "openssl/rsa.h"
26 #include "tink/internal/fips_utils.h"
27 #include "tink/internal/rsa_util.h"
28 #include "tink/internal/ssl_unique_ptr.h"
29 #include "tink/public_key_verify.h"
30 #include "tink/subtle/common_enums.h"
31 #include "tink/util/status.h"
32 
33 namespace crypto {
34 namespace tink {
35 namespace subtle {
36 
37 // RSA SSA (Signature Schemes with Appendix) using PKCS1 (Public-Key
38 // Cryptography Standards) encoding is defined at
39 // https://tools.ietf.org/html/rfc8017#section-8.2). This implemention uses
40 // BoringSSL for the underlying cryptographic operations.
41 class RsaSsaPkcs1VerifyBoringSsl : public PublicKeyVerify {
42  public:
43   static crypto::tink::util::StatusOr<
44       std::unique_ptr<RsaSsaPkcs1VerifyBoringSsl>>
45   New(const internal::RsaPublicKey& pub_key,
46       const internal::RsaSsaPkcs1Params& params);
47 
48   // Verifies that 'signature' is a digital signature for 'data'.
49   crypto::tink::util::Status Verify(absl::string_view signature,
50                                     absl::string_view data) const override;
51 
52   ~RsaSsaPkcs1VerifyBoringSsl() override = default;
53 
54   static constexpr crypto::tink::internal::FipsCompatibility kFipsStatus =
55       crypto::tink::internal::FipsCompatibility::kRequiresBoringCrypto;
56 
57  private:
58   // To reach 128-bit security strength, RSA's modulus must be at least 3072-bit
59   // while 2048-bit RSA key only has 112-bit security. Nevertheless, a 2048-bit
60   // RSA key is considered safe by NIST until 2030 (see
61   // https://www.keylength.com/en/4/).
62   static constexpr size_t kMinModulusSizeInBits = 2048;
63 
RsaSsaPkcs1VerifyBoringSsl(internal::SslUniquePtr<RSA> rsa,const EVP_MD * sig_hash)64   RsaSsaPkcs1VerifyBoringSsl(internal::SslUniquePtr<RSA> rsa,
65                              const EVP_MD* sig_hash)
66       : rsa_(std::move(rsa)), sig_hash_(sig_hash) {}
67 
68   const internal::SslUniquePtr<RSA> rsa_;
69   const EVP_MD* const sig_hash_;  // Owned by BoringSSL.
70 };
71 
72 }  // namespace subtle
73 }  // namespace tink
74 }  // namespace crypto
75 
76 #endif  // TINK_SUBTLE_RSA_SSA_PKCS1_VERIFY_BORINGSSL_H_
77