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 #include "anonymous_tokens/cpp/crypto/anonymous_tokens_pb_openssl_converters.h"
16
17 #include <cstdint>
18 #include <string>
19
20 #include "absl/status/status.h"
21 #include "absl/status/statusor.h"
22 #include "anonymous_tokens/cpp/crypto/constants.h"
23 #include "anonymous_tokens/cpp/crypto/crypto_utils.h"
24 #include "anonymous_tokens/proto/anonymous_tokens.pb.h"
25 #include <openssl/base.h>
26 #include <openssl/digest.h>
27 #include <openssl/rand.h>
28
29 namespace anonymous_tokens {
30
GenerateMask(const RSABlindSignaturePublicKey & public_key)31 absl::StatusOr<std::string> GenerateMask(
32 const RSABlindSignaturePublicKey& public_key) {
33 std::string mask;
34 if (public_key.message_mask_type() == AT_MESSAGE_MASK_CONCAT &&
35 static_cast<size_t>(public_key.message_mask_size()) >=
36 kRsaMessageMaskSizeInBytes32) {
37 mask = std::string(public_key.message_mask_size(), '\0');
38 RAND_bytes(reinterpret_cast<uint8_t*>(mask.data()), mask.size());
39 } else if (public_key.message_mask_type() == AT_MESSAGE_MASK_NO_MASK &&
40 public_key.message_mask_size() == 0) {
41 return "";
42 } else {
43 return absl::InvalidArgumentError(
44 "Unsupported message mask type Or invalid message mask size "
45 "requested.");
46 }
47 return mask;
48 }
49
ProtoHashTypeToEVPDigest(const HashType hash_type)50 absl::StatusOr<const EVP_MD*> ProtoHashTypeToEVPDigest(
51 const HashType hash_type) {
52 switch (hash_type) {
53 case AT_HASH_TYPE_SHA256:
54 return EVP_sha256();
55 case AT_HASH_TYPE_SHA384:
56 return EVP_sha384();
57 case AT_HASH_TYPE_UNDEFINED:
58 default:
59 return absl::InvalidArgumentError("Unknown hash type.");
60 }
61 }
62
ProtoMaskGenFunctionToEVPDigest(const MaskGenFunction mgf)63 absl::StatusOr<const EVP_MD*> ProtoMaskGenFunctionToEVPDigest(
64 const MaskGenFunction mgf) {
65 switch (mgf) {
66 case AT_MGF_SHA256:
67 return EVP_sha256();
68 case AT_MGF_SHA384:
69 return EVP_sha384();
70 case AT_MGF_UNDEFINED:
71 default:
72 return absl::InvalidArgumentError(
73 "Unknown hash type for mask generation hash function.");
74 }
75 }
76
AnonymousTokensRSAPrivateKeyToRSA(const RSAPrivateKey & private_key)77 absl::StatusOr<bssl::UniquePtr<RSA>> AnonymousTokensRSAPrivateKeyToRSA(
78 const RSAPrivateKey& private_key) {
79 return CreatePrivateKeyRSA(private_key.n(), private_key.e(), private_key.d(),
80 private_key.p(), private_key.q(), private_key.dp(),
81 private_key.dq(), private_key.crt());
82 }
83
AnonymousTokensRSAPublicKeyToRSA(const RSAPublicKey & public_key)84 absl::StatusOr<bssl::UniquePtr<RSA>> AnonymousTokensRSAPublicKeyToRSA(
85 const RSAPublicKey& public_key) {
86 return CreatePublicKeyRSA(public_key.n(), public_key.e());
87 }
88
89 } // namespace anonymous_tokens
90