1 // Copyright 2021 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/experimental/pqcrypto/signature/subtle/dilithium_avx2_sign.h"
18
19 #include <algorithm>
20 #include <cstddef>
21 #include <cstdint>
22 #include <iterator>
23 #include <memory>
24 #include <string>
25 #include <utility>
26
27 #include "absl/memory/memory.h"
28 #include "absl/status/status.h"
29 #include "absl/strings/str_format.h"
30 #include "absl/strings/string_view.h"
31 #include "tink/experimental/pqcrypto/signature/subtle/dilithium_key.h"
32 #include "tink/public_key_sign.h"
33 #include "tink/util/statusor.h"
34
35 extern "C" {
36 #include "third_party/pqclean/crypto_sign/dilithium2/api.h"
37 #include "third_party/pqclean/crypto_sign/dilithium2aes/api.h"
38 #include "third_party/pqclean/crypto_sign/dilithium3/api.h"
39 #include "third_party/pqclean/crypto_sign/dilithium3aes/api.h"
40 #include "third_party/pqclean/crypto_sign/dilithium5/api.h"
41 #include "third_party/pqclean/crypto_sign/dilithium5aes/api.h"
42 }
43
44 namespace crypto {
45 namespace tink {
46 namespace subtle {
47
48 // static
New(DilithiumPrivateKeyPqclean private_key)49 util::StatusOr<std::unique_ptr<PublicKeySign>> DilithiumAvx2Sign::New(
50 DilithiumPrivateKeyPqclean private_key) {
51 auto status = internal::CheckFipsCompatibility<DilithiumAvx2Sign>();
52 if (!status.ok()) return status;
53
54 int32_t key_size = private_key.GetKeyData().size();
55
56 if (key_size != PQCLEAN_DILITHIUM2_CRYPTO_SECRETKEYBYTES &&
57 key_size != PQCLEAN_DILITHIUM3_CRYPTO_SECRETKEYBYTES &&
58 key_size != PQCLEAN_DILITHIUM5_CRYPTO_SECRETKEYBYTES) {
59 return util::Status(
60 absl::StatusCode::kInvalidArgument,
61 absl::StrFormat("Invalid private key size (%d). "
62 "The only valid sizes are %d, %d, %d.",
63 private_key.GetKeyData().size(),
64 PQCLEAN_DILITHIUM2_CRYPTO_SECRETKEYBYTES,
65 PQCLEAN_DILITHIUM3_CRYPTO_SECRETKEYBYTES,
66 PQCLEAN_DILITHIUM5_CRYPTO_SECRETKEYBYTES));
67 }
68
69 return {absl::WrapUnique(new DilithiumAvx2Sign(std::move(private_key)))};
70 }
71
Sign(absl::string_view data) const72 util::StatusOr<std::string> DilithiumAvx2Sign::Sign(
73 absl::string_view data) const {
74 size_t sig_length;
75 int32_t key_size = private_key_.GetKeyData().size();
76 std::string signature;
77 int result = 1;
78
79 switch (key_size) {
80 case PQCLEAN_DILITHIUM2_CRYPTO_SECRETKEYBYTES: {
81 switch (private_key_.GetSeedExpansion()) {
82 case DilithiumSeedExpansion::SEED_EXPANSION_AES: {
83 signature.resize(PQCLEAN_DILITHIUM2AES_CRYPTO_BYTES, '0');
84 result = PQCLEAN_DILITHIUM2AES_crypto_sign_signature(
85 reinterpret_cast<uint8_t *>(signature.data()), &sig_length,
86 reinterpret_cast<const uint8_t *>(data.data()), data.size(),
87 reinterpret_cast<const uint8_t *>(
88 private_key_.GetKeyData().data()));
89 break;
90 }
91 case DilithiumSeedExpansion::SEED_EXPANSION_SHAKE: {
92 signature.resize(PQCLEAN_DILITHIUM2_CRYPTO_BYTES, '0');
93 result = PQCLEAN_DILITHIUM2_crypto_sign_signature(
94 reinterpret_cast<uint8_t *>(signature.data()), &sig_length,
95 reinterpret_cast<const uint8_t *>(data.data()), data.size(),
96 reinterpret_cast<const uint8_t *>(
97 private_key_.GetKeyData().data()));
98
99 break;
100 }
101 default: {
102 return util::Status(absl::StatusCode::kInternal,
103 "Invalid seed expansion.");
104 }
105 }
106 break;
107 }
108 case PQCLEAN_DILITHIUM3_CRYPTO_SECRETKEYBYTES: {
109 switch (private_key_.GetSeedExpansion()) {
110 case DilithiumSeedExpansion::SEED_EXPANSION_AES: {
111 signature.resize(PQCLEAN_DILITHIUM3AES_CRYPTO_BYTES, '0');
112 result = PQCLEAN_DILITHIUM3AES_crypto_sign_signature(
113 reinterpret_cast<uint8_t *>(signature.data()), &sig_length,
114 reinterpret_cast<const uint8_t *>(data.data()), data.size(),
115 reinterpret_cast<const uint8_t *>(
116 private_key_.GetKeyData().data()));
117 break;
118 }
119 case DilithiumSeedExpansion::SEED_EXPANSION_SHAKE: {
120 signature.resize(PQCLEAN_DILITHIUM3_CRYPTO_BYTES, '0');
121 result = PQCLEAN_DILITHIUM3_crypto_sign_signature(
122 reinterpret_cast<uint8_t *>(signature.data()), &sig_length,
123 reinterpret_cast<const uint8_t *>(data.data()), data.size(),
124 reinterpret_cast<const uint8_t *>(
125 private_key_.GetKeyData().data()));
126 break;
127 }
128 default: {
129 return util::Status(absl::StatusCode::kInternal,
130 "Invalid seed expansion.");
131 }
132 }
133 break;
134 }
135 case PQCLEAN_DILITHIUM5_CRYPTO_SECRETKEYBYTES: {
136 switch (private_key_.GetSeedExpansion()) {
137 case DilithiumSeedExpansion::SEED_EXPANSION_AES: {
138 signature.resize(PQCLEAN_DILITHIUM5AES_CRYPTO_BYTES, '0');
139 result = PQCLEAN_DILITHIUM5AES_crypto_sign_signature(
140 reinterpret_cast<uint8_t *>(signature.data()), &sig_length,
141 reinterpret_cast<const uint8_t *>(data.data()), data.size(),
142 reinterpret_cast<const uint8_t *>(
143 private_key_.GetKeyData().data()));
144 break;
145 }
146 case DilithiumSeedExpansion::SEED_EXPANSION_SHAKE: {
147 signature.resize(PQCLEAN_DILITHIUM5_CRYPTO_BYTES, '0');
148 result = PQCLEAN_DILITHIUM5_crypto_sign_signature(
149 reinterpret_cast<uint8_t *>(signature.data()), &sig_length,
150 reinterpret_cast<const uint8_t *>(data.data()), data.size(),
151 reinterpret_cast<const uint8_t *>(
152 private_key_.GetKeyData().data()));
153 break;
154 }
155 default: {
156 return util::Status(absl::StatusCode::kInternal,
157 "Invalid seed expansion.");
158 }
159 }
160 break;
161 }
162 default:
163 return util::Status(absl::StatusCode::kInternal, "Invalid keysize.");
164 }
165
166 if (result != 0) {
167 return util::Status(absl::StatusCode::kInternal, "Signing failed.");
168 }
169
170 return signature;
171 }
172
173 } // namespace subtle
174 } // namespace tink
175 } // namespace crypto
176