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