blob: 7ce218b53849a8805e525c5c055c8eae79d619c7 [file] [edit]
// 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_UTILS_CBOR_PADDING_H_
#define THIRD_PARTY_CREDENTIO_UTILS_CBOR_PADDING_H_
#include <cstddef>
#include <cstdint>
#include "absl/status/status.h"
#include "cose/sig_structure.h"
#include "proto/boxes_hash_assertion.pb.h"
#include "proto/data_hash_assertion.pb.h"
#include "proto/multi_asset_hash_assertion.pb.h"
namespace credentio {
// Adds padding to DataHashAssertion to account for the variable encoding sizes
// of integers when serializing to CBOR. Given any two DataHashAssertion(s) that
// are identical except for the specific lengths/offsets in each exclusion,
// their CBOR-serialized sizes after applying this padding will be the same.
void PadDataHashAssertion(DataHashAssertion& data_hash);
// Set an empty `pad` field for compatibility with other third-party validators.
void PadBoxesHashAssertion(BoxesHashAssertion& boxes_hash);
// Adds padding to MultiAssetHashAssertion to account for the variable encoding
// sizes of integers when serializing to CBOR. Given any two
// MultiAssetHashAssertion(s) that are identical except for the specific
// lengths/offsets in each part location, their CBOR-serialized sizes after
// applying this padding will be the same.
void PadMultiAssetHashAssertion(MultiAssetHashAssertion& multi_asset_hash);
// Adds padding to CoseSign1TaggedStructure to account for the variable
// encoding sizes of signature and timestamp bstr. It ensures that the
// encoded size is a function only of `max_signature_size`,
// `max_timestamp_size`, and the contents of `cose_sign1` other than the
// signature, timestamp, and padding.
//
// Returns an error if there is more than one timestamp or the signature or
// timestamp is too big.
absl::Status PadCoseSign1TaggedStructure(CoseSign1TaggedStructure& cose_sign1,
int64_t max_signature_size,
int64_t max_timestamp_size);
// Pads the structure to an overall target size by adjusting the pad and pad2
// fields. This handles cases where pad crosses the length prefix threshold
// (e.g. 23->24, 255->256, 65535->65536, etc.)
//
// Returns an error if the target size is smaller than the current size of the
// encoded structure.
absl::Status PadCoseSign1TaggedStructure(CoseSign1TaggedStructure& cose_sign1,
size_t target_size);
} // namespace credentio
#endif // THIRD_PARTY_CREDENTIO_UTILS_CBOR_PADDING_H_