xref: /aosp_15_r20/external/cronet/third_party/anonymous_tokens/src/anonymous_tokens/cpp/testing/proto_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_PROTO_UTILS_H_
16 #define ANONYMOUS_TOKENS_CPP_TESTING_PROTO_UTILS_H_
17 
18 #include <cstdint>
19 #include <string>
20 #include <utility>
21 
22 #include "absl/status/statusor.h"
23 #include "absl/strings/string_view.h"
24 #include "anonymous_tokens/cpp/crypto/constants.h"
25 #include "anonymous_tokens/proto/anonymous_tokens.pb.h"
26 #include <openssl/base.h>
27 
28 namespace anonymous_tokens {
29 
30 // Creates a pair containing a standard RSA Private key and an Anonymous Tokens
31 // RSABlindSignaturePublicKey using RSA_F4 (65537) as the public exponent and
32 // other input parameters.
33 //
34 // The standard key pair produced by this method should only be used to test
35 // standard RSA Blind Signatures. For testing RSA Blind Signatures with Public
36 // Metadata please use RSA keys with strong RSA moduli.
37 absl::StatusOr<std::pair<bssl::UniquePtr<RSA>, RSABlindSignaturePublicKey>>
38 CreateTestKey(int key_size = 512, HashType sig_hash = AT_HASH_TYPE_SHA384,
39               MaskGenFunction mfg1_hash = AT_MGF_SHA384, int salt_length = 48,
40               MessageMaskType message_mask_type = AT_MESSAGE_MASK_CONCAT,
41               int message_mask_size = kRsaMessageMaskSizeInBytes32);
42 
43 // Prepares message for signing by computing its hash and then applying the PSS
44 // padding to the result by executing RSA_padding_add_PKCS1_PSS_mgf1 from the
45 // openssl library, using the input parameters.
46 //
47 // This is a test function and it skips the message blinding part.
48 absl::StatusOr<std::string> EncodeMessageForTests(absl::string_view message,
49                                                   RSAPublicKey public_key,
50                                                   const EVP_MD* sig_hasher,
51                                                   const EVP_MD* mgf1_hasher,
52                                                   int32_t salt_length);
53 
54 // This method returns a newly generated RSA key pair, setting the public
55 // exponent to be the standard RSA_F4 (65537) and the default modulus size to
56 // 512 bytes.
57 absl::StatusOr<std::pair<RSAPublicKey, RSAPrivateKey>> GetStandardRsaKeyPair(
58     int modulus_size_in_bytes = kRsaModulusSizeInBytes512);
59 
60 // Method returns fixed 2048-bit strong RSA modulus for testing.
61 absl::StatusOr<std::pair<RSAPublicKey, RSAPrivateKey>> GetStrongRsaKeys2048();
62 
63 // Method returns another fixed 2048-bit strong RSA modulus for testing.
64 absl::StatusOr<std::pair<RSAPublicKey, RSAPrivateKey>>
65 GetAnotherStrongRsaKeys2048();
66 
67 // Method returns fixed 3072-bit strong RSA modulus for testing.
68 absl::StatusOr<std::pair<RSAPublicKey, RSAPrivateKey>> GetStrongRsaKeys3072();
69 
70 // Method returns fixed 4096-bit strong RSA modulus for testing.
71 absl::StatusOr<std::pair<RSAPublicKey, RSAPrivateKey>> GetStrongRsaKeys4096();
72 
73 // This method returns a RSA key pair as described in the IETF test example
74 // above.
75 absl::StatusOr<std::pair<RSAPublicKey, RSAPrivateKey>>
76 GetIetfStandardRsaBlindSignatureTestKeys();
77 
78 // This method returns a RSA key pair as described in the IETF test with Public
79 // Metadata example. It can be used for all test vectors returned by
80 // GetIetfRsaBlindSignatureWithPublicMetadataTestVectors.
81 absl::StatusOr<std::pair<RSAPublicKey, RSAPrivateKey>>
82 GetIetfRsaBlindSignatureWithPublicMetadataTestKeys();
83 
84 }  // namespace anonymous_tokens
85 
86 #endif  // ANONYMOUS_TOKENS_CPP_TESTING_PROTO_UTILS_H_
87