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 THIRD_PARTY_ANONYMOUS_TOKENS_CPP_TESTING_UTILS_H_ 16 #define THIRD_PARTY_ANONYMOUS_TOKENS_CPP_TESTING_UTILS_H_ 17 18 #include <stdint.h> 19 20 #include <random> 21 #include <string> 22 #include <utility> 23 24 #include "absl/status/statusor.h" 25 #include "absl/strings/string_view.h" 26 #include "quiche/blind_sign_auth/anonymous_tokens/cpp/crypto/constants.h" 27 #include "quiche/blind_sign_auth/anonymous_tokens/proto/anonymous_tokens.pb.h" 28 #include "openssl/base.h" 29 30 namespace private_membership { 31 namespace anonymous_tokens { 32 33 struct IetfStandardRsaBlindSignatureTestVector { 34 std::string n; 35 std::string e; 36 std::string d; 37 std::string p; 38 std::string q; 39 std::string message; 40 std::string salt; 41 std::string inv; 42 std::string encoded_message; 43 std::string blinded_message; 44 std::string blinded_signature; 45 std::string signature; 46 }; 47 48 struct IetfRsaBlindSignatureWithPublicMetadataTestVector { 49 std::string n; 50 std::string e; 51 std::string d; 52 std::string p; 53 std::string q; 54 std::string message; 55 std::string public_metadata; 56 std::string message_mask; 57 std::string blinded_message; 58 std::string blinded_signature; 59 std::string signature; 60 }; 61 62 // Creates a pair containing a standard RSA Private key and an Anonymous Tokens 63 // RSABlindSignaturePublicKey using RSA_F4 (65537) as the public exponent and 64 // other input parameters. 65 absl::StatusOr<std::pair<bssl::UniquePtr<RSA>, RSABlindSignaturePublicKey>> 66 CreateTestKey(int key_size = 512, HashType sig_hash = AT_HASH_TYPE_SHA384, 67 MaskGenFunction mfg1_hash = AT_MGF_SHA384, int salt_length = 48, 68 MessageMaskType message_mask_type = AT_MESSAGE_MASK_CONCAT, 69 int message_mask_size = kRsaMessageMaskSizeInBytes32); 70 71 // Prepares message for signing by computing its hash and then applying the PSS 72 // padding to the result by executing RSA_padding_add_PKCS1_PSS_mgf1 from the 73 // openssl library, using the input parameters. 74 // 75 // This is a test function and it skips the message blinding part. 76 absl::StatusOr<std::string> EncodeMessageForTests(absl::string_view message, 77 RSAPublicKey public_key, 78 const EVP_MD* sig_hasher, 79 const EVP_MD* mgf1_hasher, 80 int32_t salt_length); 81 82 // TestSign can be removed once rsa_blind_signer is moved to 83 // anonympous_tokens/public/cpp/crypto 84 absl::StatusOr<std::string> TestSign(absl::string_view blinded_data, 85 RSA* rsa_key); 86 87 // TestSignWithPublicMetadata can be removed once rsa_blind_signer is moved to 88 // anonympous_tokens/public/cpp/crypto 89 absl::StatusOr<std::string> TestSignWithPublicMetadata( 90 absl::string_view blinded_data, absl::string_view public_metadata, 91 const RSA& rsa_key); 92 93 // This method returns a newly generated RSA key pair, setting the public 94 // exponent to be the standard RSA_F4 (65537) and the default modulus size to 95 // 512 bytes. 96 absl::StatusOr<std::pair<RSAPublicKey, RSAPrivateKey>> GetStandardRsaKeyPair( 97 int modulus_size_in_bytes = kRsaModulusSizeInBytes512); 98 99 // Method returns fixed 2048-bit strong RSA modulus for testing. 100 absl::StatusOr<std::pair<RSAPublicKey, RSAPrivateKey>> GetStrongRsaKeys2048(); 101 102 // Method returns another fixed 2048-bit strong RSA modulus for testing. 103 absl::StatusOr<std::pair<RSAPublicKey, RSAPrivateKey>> 104 GetAnotherStrongRsaKeys2048(); 105 106 // Method returns fixed 3072-bit strong RSA modulus for testing. 107 absl::StatusOr<std::pair<RSAPublicKey, RSAPrivateKey>> GetStrongRsaKeys3072(); 108 109 // Method returns fixed 4096-bit strong RSA modulus for testing. 110 absl::StatusOr<std::pair<RSAPublicKey, RSAPrivateKey>> GetStrongRsaKeys4096(); 111 112 // Returns the IETF test example from 113 // https://datatracker.ietf.org/doc/draft-irtf-cfrg-rsa-blind-signatures/ 114 IetfStandardRsaBlindSignatureTestVector 115 GetIetfStandardRsaBlindSignatureTestVector(); 116 117 // This method returns a RSA key pair as described in the IETF test example 118 // above. 119 absl::StatusOr<std::pair<RSAPublicKey, RSAPrivateKey>> 120 GetIetfStandardRsaBlindSignatureTestKeys(); 121 122 // Returns the IETF test with Public Metadata examples from 123 // https://datatracker.ietf.org/doc/draft-amjad-cfrg-partially-blind-rsa/ 124 // 125 // Note that all test vectors use the same RSA key pair. 126 std::vector<IetfRsaBlindSignatureWithPublicMetadataTestVector> 127 GetIetfRsaBlindSignatureWithPublicMetadataTestVectors(); 128 129 // This method returns a RSA key pair as described in the IETF test with Public 130 // Metadata example. It can be used for all test vectors returned by 131 // GetIetfRsaBlindSignatureWithPublicMetadataTestVectors. 132 absl::StatusOr<std::pair<RSAPublicKey, RSAPrivateKey>> 133 GetIetfRsaBlindSignatureWithPublicMetadataTestKeys(); 134 135 // Outputs a random string of n characters. 136 std::string RandomString(int n, std::uniform_int_distribution<int>* distr_u8, 137 std::mt19937_64* generator); 138 139 #define ANON_TOKENS_ASSERT_OK_AND_ASSIGN(lhs, rexpr) \ 140 ANON_TOKENS_ASSERT_OK_AND_ASSIGN_IMPL_( \ 141 ANON_TOKENS_STATUS_TESTING_IMPL_CONCAT_(_status_or_value, __LINE__), \ 142 lhs, rexpr) 143 144 #define ANON_TOKENS_ASSERT_OK_AND_ASSIGN_IMPL_(statusor, lhs, rexpr) \ 145 auto statusor = (rexpr); \ 146 ASSERT_THAT(statusor.ok(), ::testing::Eq(true)); \ 147 lhs = std::move(statusor).value() 148 149 #define ANON_TOKENS_STATUS_TESTING_IMPL_CONCAT_INNER_(x, y) x##y 150 #define ANON_TOKENS_STATUS_TESTING_IMPL_CONCAT_(x, y) \ 151 ANON_TOKENS_STATUS_TESTING_IMPL_CONCAT_INNER_(x, y) 152 153 } // namespace anonymous_tokens 154 } // namespace private_membership 155 156 #endif // THIRD_PARTY_ANONYMOUS_TOKENS_CPP_TESTING_UTILS_H_ 157