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_BLIND_SIGNER_H_ 16 #define ANONYMOUS_TOKENS_CPP_CRYPTO_RSA_BLIND_SIGNER_H_ 17 18 #include <memory> 19 #include <string> 20 21 #include "absl/status/statusor.h" 22 #include "absl/strings/string_view.h" 23 #include "anonymous_tokens/cpp/crypto/blind_signer.h" 24 #include "anonymous_tokens/cpp/crypto/crypto_utils.h" 25 #include "anonymous_tokens/proto/anonymous_tokens.pb.h" 26 27 28 namespace anonymous_tokens { 29 30 // The RSA SSA (Signature Schemes with Appendix) using PSS (Probabilistic 31 // Signature Scheme) encoding is defined at 32 // https://tools.ietf.org/html/rfc8017#section-8.1). This implementation uses 33 // Boring SSL for the underlying cryptographic operations. 34 class RsaBlindSigner : public BlindSigner { 35 public: 36 ~RsaBlindSigner() override = default; 37 RsaBlindSigner(const RsaBlindSigner&) = delete; 38 RsaBlindSigner& operator=(const RsaBlindSigner&) = delete; 39 40 // Passing of public_metadata is optional. If it is set to any value including 41 // an empty string, RsaBlindSigner will assume that partially blind RSA 42 // signature protocol is being executed. 43 // 44 // If public metadata is passed and the boolean "use_rsa_public_exponent" is 45 // set to false, the public exponent in the signing_key is not used in any 46 // computations in the protocol. 47 // 48 // Setting "use_rsa_public_exponent" to true is deprecated. All new users 49 // should set it to false. 50 static absl::StatusOr<std::unique_ptr<RsaBlindSigner>> New( 51 const RSAPrivateKey& signing_key, bool use_rsa_public_exponent, 52 std::optional<absl::string_view> public_metadata = std::nullopt); 53 54 // Computes the signature for 'blinded_data'. 55 absl::StatusOr<std::string> Sign( 56 absl::string_view blinded_data) const override; 57 58 private: 59 // Use New to construct. 60 RsaBlindSigner(std::optional<absl::string_view> public_metadata, 61 bssl::UniquePtr<RSA> rsa_private_key); 62 63 const std::optional<std::string> public_metadata_; 64 65 // In case public metadata is passed to RsaBlindSigner::New, rsa_private_key_ 66 // will be initialized using RSA_new_private_key_large_e method. 67 const bssl::UniquePtr<RSA> rsa_private_key_; 68 }; 69 70 } // namespace anonymous_tokens 71 72 73 #endif // ANONYMOUS_TOKENS_CPP_CRYPTO_RSA_BLIND_SIGNER_H_ 74