| // Copyright 2026 Google LLC |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // https://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| // |
| |
| #ifndef THIRD_PARTY_CREDENTIO_CRYPTO_ALGORITHMS_H_ |
| #define THIRD_PARTY_CREDENTIO_CRYPTO_ALGORITHMS_H_ |
| |
| #include <cstdint> |
| |
| #include "absl/log/log.h" |
| #include "absl/status/statusor.h" |
| #include "absl/strings/str_cat.h" |
| #include "absl/strings/string_view.h" |
| |
| namespace credentio { |
| |
| // Hashing algorithms allowed by the C2PA spec, see |
| // https://c2pa.org/specifications/specifications/2.1/specs/C2PA_Specification.html#_hashing |
| // All values must correspond to an OpenSSL NID. |
| enum class HashAlgorithm { |
| kSha256 = 672, // NID_sha256 |
| kSha384 = 673, // NID_sha384 |
| kSha512 = 674, // NID_sha512 |
| }; |
| |
| inline uint64_t HashSizeBytes(HashAlgorithm algorithm) { |
| switch (algorithm) { |
| case credentio::HashAlgorithm::kSha256: |
| return 32; |
| case credentio::HashAlgorithm::kSha512: |
| return 64; |
| case credentio::HashAlgorithm::kSha384: |
| return 48; |
| } |
| LOG(DFATAL) << "unknown HashAlgorithm enum: " << static_cast<int>(algorithm); |
| return 0; |
| } |
| |
| absl::StatusOr<HashAlgorithm> NidToHashAlgorithm(int nid); |
| absl::StatusOr<HashAlgorithm> ParseHashAlgorithm(absl::string_view txt); |
| |
| template <typename Sink> |
| void AbslStringify(Sink& sink, HashAlgorithm algorithm) { |
| switch (algorithm) { |
| case HashAlgorithm::kSha256: |
| sink.Append("sha256"); |
| return; |
| case HashAlgorithm::kSha384: |
| sink.Append("sha384"); |
| return; |
| case HashAlgorithm::kSha512: |
| sink.Append("sha512"); |
| return; |
| } |
| LOG(DFATAL) << "unknown HashAlgorithm enum: " << static_cast<int>(algorithm); |
| sink.Append( |
| absl::StrCat("UNKNOWN_HASH_ALGORITHM_", static_cast<int>(algorithm))); |
| } |
| |
| // IANA registry: https://www.iana.org/assignments/cose/cose.xhtml#algorithms |
| // C2PA accepted list: |
| // https://spec.c2pa.org/specifications/specifications/2.4/specs/C2PA_Specification.html#_signature_algorithms |
| enum class SigningAlgorithm { |
| kEs256, // ECDSA with SHA-256 |
| kEs384, // ECDSA with SHA-384 |
| kEs512, // ECDSA with SHA-512 |
| kPs256, // RSASSA-PSS using SHA-256 and MGF1 with SHA-256 |
| kPs384, // RSASSA-PSS using SHA-384 and MGF1 with SHA-384 |
| kPs512, // RSASSA-PSS using SHA-512 and MGF1 with SHA-512 |
| kEdDsa, // Edwards-Curve DSA |
| }; |
| |
| template <typename Sink> |
| inline void AbslStringify(Sink& sink, const SigningAlgorithm& algorithm) { |
| switch (algorithm) { |
| case SigningAlgorithm::kEs256: |
| sink.Append("ES256"); |
| return; |
| case SigningAlgorithm::kEs384: |
| sink.Append("ES384"); |
| return; |
| case SigningAlgorithm::kEs512: |
| sink.Append("ES512"); |
| return; |
| case SigningAlgorithm::kPs256: |
| sink.Append("PS256"); |
| return; |
| case SigningAlgorithm::kPs384: |
| sink.Append("PS384"); |
| return; |
| case SigningAlgorithm::kPs512: |
| sink.Append("PS512"); |
| return; |
| case SigningAlgorithm::kEdDsa: |
| sink.Append("EdDSA"); |
| return; |
| } |
| LOG(DFATAL) << "unknown SigningAlgorithm enum: " |
| << static_cast<int>(algorithm); |
| sink.Append( |
| absl::StrCat("UNKNOWN_SIGNING_ALGORITHM_", static_cast<int>(algorithm))); |
| } |
| |
| } // namespace credentio |
| |
| #endif // THIRD_PARTY_CREDENTIO_CRYPTO_ALGORITHMS_H_ |