xref: /aosp_15_r20/external/tink/cc/config/key_gen_fips_140_2.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
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 //     http://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 ////////////////////////////////////////////////////////////////////////////////
16 
17 #include "tink/config/key_gen_fips_140_2.h"
18 
19 #include "absl/log/check.h"
20 #include "tink/aead/aes_ctr_hmac_aead_key_manager.h"
21 #include "tink/aead/aes_gcm_key_manager.h"
22 #include "tink/internal/fips_utils.h"
23 #include "tink/internal/key_gen_configuration_impl.h"
24 #include "tink/key_gen_configuration.h"
25 #include "tink/mac/hmac_key_manager.h"
26 #include "tink/prf/hmac_prf_key_manager.h"
27 #include "tink/signature/ecdsa_verify_key_manager.h"
28 #include "tink/signature/rsa_ssa_pkcs1_sign_key_manager.h"
29 #include "tink/signature/rsa_ssa_pkcs1_verify_key_manager.h"
30 #include "tink/signature/rsa_ssa_pss_sign_key_manager.h"
31 #include "tink/signature/rsa_ssa_pss_verify_key_manager.h"
32 #include "tink/signature/ecdsa_sign_key_manager.h"
33 
34 namespace crypto {
35 namespace tink {
36 namespace {
37 
AddMac(KeyGenConfiguration & config)38 util::Status AddMac(KeyGenConfiguration& config) {
39   return internal::KeyGenConfigurationImpl::AddKeyTypeManager(
40       absl::make_unique<HmacKeyManager>(), config);
41 }
42 
AddAead(KeyGenConfiguration & config)43 util::Status AddAead(KeyGenConfiguration& config) {
44   util::Status status = internal::KeyGenConfigurationImpl::AddKeyTypeManager(
45       absl::make_unique<AesCtrHmacAeadKeyManager>(), config);
46   if (!status.ok()) {
47     return status;
48   }
49   return internal::KeyGenConfigurationImpl::AddKeyTypeManager(
50       absl::make_unique<AesGcmKeyManager>(), config);
51 }
52 
AddPrf(KeyGenConfiguration & config)53 util::Status AddPrf(KeyGenConfiguration& config) {
54   return internal::KeyGenConfigurationImpl::AddKeyTypeManager(
55       absl::make_unique<HmacPrfKeyManager>(), config);
56 }
57 
AddSignature(KeyGenConfiguration & config)58 util::Status AddSignature(KeyGenConfiguration& config) {
59   util::Status status =
60       internal::KeyGenConfigurationImpl::AddAsymmetricKeyManagers(
61           absl::make_unique<EcdsaSignKeyManager>(),
62           absl::make_unique<EcdsaVerifyKeyManager>(), config);
63   if (!status.ok()) {
64     return status;
65   }
66   status = internal::KeyGenConfigurationImpl::AddAsymmetricKeyManagers(
67       absl::make_unique<RsaSsaPssSignKeyManager>(),
68       absl::make_unique<RsaSsaPssVerifyKeyManager>(), config);
69   if (!status.ok()) {
70     return status;
71   }
72   return internal::KeyGenConfigurationImpl::AddAsymmetricKeyManagers(
73       absl::make_unique<RsaSsaPkcs1SignKeyManager>(),
74       absl::make_unique<RsaSsaPkcs1VerifyKeyManager>(), config);
75 }
76 
77 }  // namespace
78 
KeyGenConfigFips140_2()79 const KeyGenConfiguration& KeyGenConfigFips140_2() {
80   static const KeyGenConfiguration* instance = [] {
81     internal::SetFipsRestricted();
82 
83     static KeyGenConfiguration* config = new KeyGenConfiguration();
84     CHECK_OK(AddMac(*config));
85     CHECK_OK(AddAead(*config));
86     CHECK_OK(AddPrf(*config));
87     CHECK_OK(AddSignature(*config));
88 
89     return config;
90   }();
91   return *instance;
92 }
93 
94 }  // namespace tink
95 }  // namespace crypto
96