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