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_TESTING_UTILS_H_ 16 #define ANONYMOUS_TOKENS_CPP_TESTING_UTILS_H_ 17 18 #include <random> 19 #include <string> 20 #include <utility> 21 22 #include "absl/status/statusor.h" 23 #include "absl/strings/string_view.h" 24 #include <openssl/base.h> 25 26 27 namespace anonymous_tokens { 28 29 struct TestRsaPublicKey { 30 std::string n; 31 std::string e; 32 }; 33 34 struct TestRsaPrivateKey { 35 std::string n; 36 std::string e; 37 std::string d; 38 std::string p; 39 std::string q; 40 std::string dp; 41 std::string dq; 42 std::string crt; 43 }; 44 45 struct IetfStandardRsaBlindSignatureTestVector { 46 std::string n; 47 std::string e; 48 std::string d; 49 std::string p; 50 std::string q; 51 std::string message; 52 std::string salt; 53 std::string inv; 54 std::string encoded_message; 55 std::string blinded_message; 56 std::string blinded_signature; 57 std::string signature; 58 }; 59 60 struct IetfRsaBlindSignatureWithPublicMetadataTestVector { 61 std::string n; 62 std::string e; 63 std::string d; 64 std::string p; 65 std::string q; 66 std::string message; 67 std::string public_metadata; 68 std::string message_mask; 69 std::string blinded_message; 70 std::string blinded_signature; 71 std::string signature; 72 }; 73 74 // TestSign can be removed once rsa_blind_signer is moved to 75 // anonympous_tokens/public/cpp/crypto 76 absl::StatusOr<std::string> TestSign(absl::string_view blinded_data, 77 RSA* rsa_key); 78 79 // TestSignWithPublicMetadata can be removed once rsa_blind_signer is moved to 80 // anonympous_tokens/public/cpp/crypto 81 absl::StatusOr<std::string> TestSignWithPublicMetadata( 82 absl::string_view blinded_data, absl::string_view public_metadata, 83 const RSA& rsa_key, bool use_rsa_public_exponent); 84 85 // Returns the IETF test example from 86 // https://datatracker.ietf.org/doc/draft-irtf-cfrg-rsa-blind-signatures/ 87 IetfStandardRsaBlindSignatureTestVector 88 GetIetfStandardRsaBlindSignatureTestVector(); 89 90 // Returns the IETF test with Public Metadata examples from 91 // https://datatracker.ietf.org/doc/draft-amjad-cfrg-partially-blind-rsa/ 92 // 93 // Note that all test vectors use the same RSA key pair. 94 std::vector<IetfRsaBlindSignatureWithPublicMetadataTestVector> 95 GetIetfRsaBlindSignatureWithPublicMetadataTestVectors(); 96 97 // Returns the IETF test with Public Metadata examples that disregard the RSA 98 // public exponent during partially blind RSA signatures protocol execution. 99 // 100 // Note that all test vectors use the same RSA key pair. 101 std::vector<IetfRsaBlindSignatureWithPublicMetadataTestVector> 102 GetIetfPartiallyBlindRSASignatureNoPublicExponentTestVectors(); 103 104 // Method returns fixed 2048-bit strong RSA modulus based key pair for testing. 105 std::pair<TestRsaPublicKey, TestRsaPrivateKey> GetStrongTestRsaKeyPair2048(); 106 107 // Method returns another fixed 2048-bit strong RSA modulus based key pair for 108 // testing. 109 std::pair<TestRsaPublicKey, TestRsaPrivateKey> 110 GetAnotherStrongTestRsaKeyPair2048(); 111 112 // Method returns fixed 3072-bit strong RSA modulus based key pair for testing. 113 std::pair<TestRsaPublicKey, TestRsaPrivateKey> GetStrongTestRsaKeyPair3072(); 114 115 // Method returns fixed 4096-bit strong RSA modulus based key pair for testing. 116 std::pair<TestRsaPublicKey, TestRsaPrivateKey> GetStrongTestRsaKeyPair4096(); 117 118 // Outputs a random string of n characters. 119 std::string RandomString(int n, std::uniform_int_distribution<int>* distr_u8, 120 std::mt19937_64* generator); 121 122 #define ANON_TOKENS_ASSERT_OK_AND_ASSIGN(lhs, rexpr) \ 123 ANON_TOKENS_ASSERT_OK_AND_ASSIGN_IMPL_( \ 124 ANON_TOKENS_STATUS_TESTING_IMPL_CONCAT_(_status_or_value, __LINE__), \ 125 lhs, rexpr) 126 127 #define ANON_TOKENS_ASSERT_OK_AND_ASSIGN_IMPL_(statusor, lhs, rexpr) \ 128 auto statusor = (rexpr); \ 129 ASSERT_THAT(statusor.ok(), ::testing::Eq(true)); \ 130 lhs = std::move(statusor).value() 131 132 #define ANON_TOKENS_STATUS_TESTING_IMPL_CONCAT_INNER_(x, y) x##y 133 #define ANON_TOKENS_STATUS_TESTING_IMPL_CONCAT_(x, y) \ 134 ANON_TOKENS_STATUS_TESTING_IMPL_CONCAT_INNER_(x, y) 135 136 } // namespace anonymous_tokens 137 138 139 #endif // ANONYMOUS_TOKENS_CPP_TESTING_UTILS_H_ 140