Skip to content

Commit

Permalink
Switch key id type from int to int32_t.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 653631453
Change-Id: I83fcb029b3edc8a85871136ef2a1027d93b3f201
  • Loading branch information
willinois authored and copybara-github committed Jul 18, 2024
1 parent deac528 commit a2d15d5
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 16 deletions.
9 changes: 5 additions & 4 deletions tink/core/keyset_handle_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "tink/keyset_handle_builder.h"

#include <cstdint>
#include <iostream>
#include <memory>
#include <set>
Expand Down Expand Up @@ -49,7 +50,7 @@ namespace {
using ::google::crypto::tink::Keyset;

void SetBuilderEntryAttributes(KeyStatus status, bool is_primary,
absl::optional<int> id,
absl::optional<int32_t> id,
KeysetHandleBuilder::Entry* entry) {
entry->SetStatus(status);
if (is_primary) {
Expand Down Expand Up @@ -95,8 +96,8 @@ KeysetHandleBuilder::Entry KeysetHandleBuilder::Entry::CreateFromParams(
return entry;
}

util::StatusOr<int> KeysetHandleBuilder::NextIdFromKeyIdStrategy(
internal::KeyIdStrategy strategy, const std::set<int>& ids_so_far) {
util::StatusOr<int32_t> KeysetHandleBuilder::NextIdFromKeyIdStrategy(
internal::KeyIdStrategy strategy, const std::set<int32_t>& ids_so_far) {
if (strategy.strategy == internal::KeyIdStrategyEnum::kFixedId) {
if (!strategy.id_requirement.has_value()) {
return util::Status(absl::StatusCode::kInvalidArgument,
Expand Down Expand Up @@ -177,7 +178,7 @@ util::StatusOr<KeysetHandle> KeysetHandleBuilder::Build() {
util::Status assigned_ids_status = CheckIdAssignments();
if (!assigned_ids_status.ok()) return assigned_ids_status;

std::set<int> ids_so_far;
std::set<int32_t> ids_so_far;
for (KeysetHandleBuilder::Entry& entry : entries_) {
util::StatusOr<int> id =
NextIdFromKeyIdStrategy(entry.GetKeyIdStrategy(), ids_so_far);
Expand Down
5 changes: 3 additions & 2 deletions tink/internal/keyset_handle_builder_entry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "tink/internal/keyset_handle_builder_entry.h"

#include <cstdint>
#include <memory>
#include <string>

Expand Down Expand Up @@ -166,7 +167,7 @@ void KeysetHandleBuilderEntry::SetRandomId() {
strategy_.id_requirement = absl::nullopt;
}

util::StatusOr<SecretProto<Keyset::Key>> KeyEntry::CreateKeysetKey(int id) {
util::StatusOr<SecretProto<Keyset::Key>> KeyEntry::CreateKeysetKey(int32_t id) {
util::StatusOr<KeyStatusType> key_status = ToKeyStatusType(key_status_);
if (!key_status.ok()) return key_status.status();

Expand All @@ -192,7 +193,7 @@ util::StatusOr<SecretProto<Keyset::Key>> KeyEntry::CreateKeysetKey(int id) {
}

util::StatusOr<SecretProto<Keyset::Key>> ParametersEntry::CreateKeysetKey(
int id) {
int32_t id) {
util::StatusOr<KeyStatusType> key_status = ToKeyStatusType(key_status_);
if (!key_status.ok()) return key_status.status();

Expand Down
7 changes: 4 additions & 3 deletions tink/internal/keyset_handle_builder_entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifndef TINK_INTERNAL_KEYSET_HANDLE_BUILDER_ENTRY_H_
#define TINK_INTERNAL_KEYSET_HANDLE_BUILDER_ENTRY_H_

#include <cstdint>
#include <memory>
#include <utility>

Expand Down Expand Up @@ -79,7 +80,7 @@ class KeysetHandleBuilderEntry {
// `Key` object or a `Parameters` object.
virtual crypto::tink::util::StatusOr<
crypto::tink::util::SecretProto<google::crypto::tink::Keyset::Key>>
CreateKeysetKey(int id) = 0;
CreateKeysetKey(int32_t id) = 0;

protected:
KeyStatus key_status_ = KeyStatus::kDisabled;
Expand All @@ -103,7 +104,7 @@ class KeyEntry : public KeysetHandleBuilderEntry {

crypto::tink::util::StatusOr<
crypto::tink::util::SecretProto<google::crypto::tink::Keyset::Key>>
CreateKeysetKey(int id) override;
CreateKeysetKey(int32_t id) override;

private:
std::shared_ptr<const Key> key_;
Expand All @@ -123,7 +124,7 @@ class ParametersEntry : public KeysetHandleBuilderEntry {

crypto::tink::util::StatusOr<
crypto::tink::util::SecretProto<google::crypto::tink::Keyset::Key>>
CreateKeysetKey(int id) override;
CreateKeysetKey(int32_t id) override;

private:
std::shared_ptr<const Parameters> parameters_;
Expand Down
6 changes: 3 additions & 3 deletions tink/keyset_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class KeysetHandle {

// ID should be unique (though currently Tink still accepts keysets with
// repeated IDs).
int GetId() const { return id_; }
int32_t GetId() const { return id_; }

// Should return true for exactly one entry (though currently Tink still
// accepts keysets which have no entry marked as primary).
Expand All @@ -86,7 +86,7 @@ class KeysetHandle {
friend class KeysetHandle;
friend class KeysetHandleBuilder;

Entry(std::shared_ptr<const Key> key, KeyStatus status, int id,
Entry(std::shared_ptr<const Key> key, KeyStatus status, int32_t id,
bool is_primary)
: key_(std::move(key)),
status_(status),
Expand All @@ -95,7 +95,7 @@ class KeysetHandle {

std::shared_ptr<const Key> key_;
KeyStatus status_;
int id_;
int32_t id_;
bool is_primary_;
};

Expand Down
9 changes: 5 additions & 4 deletions tink/keyset_handle_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifndef TINK_KEYSET_HANDLE_BUILDER_H_
#define TINK_KEYSET_HANDLE_BUILDER_H_

#include <cstdint>
#include <memory>
#include <set>
#include <string>
Expand Down Expand Up @@ -95,7 +96,7 @@ class KeysetHandleBuilder {
KeyStatus GetStatus() const { return entry_->GetStatus(); }

// Assigns a fixed id when this keyset is built.
void SetFixedId(int id) { entry_->SetFixedId(id); }
void SetFixedId(int32_t id) { entry_->SetFixedId(id); }
// Assigns an unused random id when this keyset is built.
void SetRandomId() { entry_->SetRandomId(); }

Expand Down Expand Up @@ -124,7 +125,7 @@ class KeysetHandleBuilder {

crypto::tink::util::StatusOr<
crypto::tink::util::SecretProto<google::crypto::tink::Keyset::Key>>
CreateKeysetKey(int id) {
CreateKeysetKey(int32_t id) {
return entry_->CreateKeysetKey(id);
}

Expand Down Expand Up @@ -162,8 +163,8 @@ class KeysetHandleBuilder {

private:
// Select the next key id based on the given strategy.
crypto::tink::util::StatusOr<int> NextIdFromKeyIdStrategy(
internal::KeyIdStrategy strategy, const std::set<int>& ids_so_far);
crypto::tink::util::StatusOr<int32_t> NextIdFromKeyIdStrategy(
internal::KeyIdStrategy strategy, const std::set<int32_t>& ids_so_far);

// Unset primary flag on all entries.
void ClearPrimary();
Expand Down

0 comments on commit a2d15d5

Please sign in to comment.