xref: /aosp_15_r20/external/cronet/third_party/anonymous_tokens/src/anonymous_tokens/cpp/crypto/rsa_blinder.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2023 Google LLC
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 //    https://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 #ifndef ANONYMOUS_TOKENS_CPP_CRYPTO_RSA_BLINDER_H_
16 #define ANONYMOUS_TOKENS_CPP_CRYPTO_RSA_BLINDER_H_
17 
18 #include <stdint.h>
19 
20 #include <memory>
21 #include <optional>
22 #include <string>
23 
24 #include "absl/status/status.h"
25 #include "absl/status/statusor.h"
26 #include "absl/strings/string_view.h"
27 #include "anonymous_tokens/cpp/crypto/blinder.h"
28 #include "anonymous_tokens/cpp/crypto/crypto_utils.h"
29 
30 namespace anonymous_tokens {
31 
32 // RsaBlinder `blinds` input messages, and then unblinds them after they are
33 // signed.
34 class RsaBlinder : public Blinder {
35  public:
36   // Passing of public_metadata is optional. If it is set to any value including
37   // an empty string, RsaBlinder will assume that partially blind RSA signature
38   // protocol is being executed.
39   //
40   // If public metadata is passed and the boolean "use_rsa_public_exponent" is
41   // set to false, the rsa_public_exponent is not used in any computations in
42   // the protocol.
43   //
44   // Setting "use_rsa_public_exponent" to true is deprecated. All new users
45   // should set it to false.
46   static absl::StatusOr<std::unique_ptr<RsaBlinder>> New(
47       absl::string_view rsa_modulus, absl::string_view rsa_public_exponent,
48       const EVP_MD* signature_hash_function, const EVP_MD* mgf1_hash_function,
49       int salt_length, bool use_rsa_public_exponent,
50       std::optional<absl::string_view> public_metadata = std::nullopt);
51 
52   // Blind `message` using n and e derived from an RSA public key and the public
53   // metadata if applicable.
54   //
55   // Before blinding, the `message` will first be hashed and then encoded with
56   // the EMSA-PSS operation.
57   absl::StatusOr<std::string> Blind(absl::string_view message) override;
58 
59   // Unblinds `blind_signature`.
60   //
61   // Callers should run Verify on the returned signature before using it /
62   // passing it on.
63   absl::StatusOr<std::string> Unblind(
64       absl::string_view blind_signature) override;
65 
66   // Verifies an `unblinded` signature against the same `message' that was
67   // passed to Blind.
68   absl::Status Verify(absl::string_view signature, absl::string_view message);
69 
70  private:
71   // Use `New` to construct
72   RsaBlinder(int salt_length, std::optional<absl::string_view> public_metadata,
73              const EVP_MD* sig_hash, const EVP_MD* mgf1_hash,
74              bssl::UniquePtr<RSA> rsa_public_key, bssl::UniquePtr<BIGNUM> r,
75              bssl::UniquePtr<BIGNUM> r_inv_mont,
76              bssl::UniquePtr<BN_MONT_CTX> mont_n);
77 
78   const int salt_length_;
79   std::optional<std::string> public_metadata_;
80   const EVP_MD* sig_hash_;   // Owned by BoringSSL.
81   const EVP_MD* mgf1_hash_;  // Owned by BoringSSL.
82 
83   // If public metadata was passed to RsaBlinder::New, rsa_public_key_ will
84   // will be initialized using RSA_new_public_key_large_e method.
85   const bssl::UniquePtr<RSA> rsa_public_key_;
86 
87   const bssl::UniquePtr<BIGNUM> r_;
88   // r^-1 mod n in the Montgomery domain
89   const bssl::UniquePtr<BIGNUM> r_inv_mont_;
90   const bssl::UniquePtr<BN_MONT_CTX> mont_n_;
91 
92   BlinderState blinder_state_;
93 };
94 
95 }  // namespace anonymous_tokens
96 
97 #endif  // ANONYMOUS_TOKENS_CPP_CRYPTO_RSA_BLINDER_H_
98