Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utils: Replace padded_str macro with function #137

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions pkcs11/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ pub mod verify;
use crate::{
backend::events::{fetch_slots_state, EventsManager},
data::{self, DEVICE, EVENTS_MANAGER, THREADS_ALLOWED, TOKENS_STATE},
defs, padded_str,
defs,
utils::padded_str,
};
use cryptoki_sys::{CK_INFO, CK_INFO_PTR, CK_RV, CK_VOID_PTR};
use log::{debug, trace};
Expand Down Expand Up @@ -99,9 +100,9 @@ pub extern "C" fn C_GetInfo(pInfo: CK_INFO_PTR) -> CK_RV {

let infos = CK_INFO {
cryptokiVersion: defs::CRYPTOKI_VERSION,
manufacturerID: padded_str!(defs::LIB_MANUFACTURER, 32),
manufacturerID: padded_str(defs::LIB_MANUFACTURER),
flags: 0,
libraryDescription: padded_str!(defs::LIB_DESCRIPTION, 32),
libraryDescription: padded_str(defs::LIB_DESCRIPTION),
libraryVersion: defs::LIB_VERSION,
};

Expand Down
16 changes: 8 additions & 8 deletions pkcs11/src/api/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use crate::{
},
data::{DEVICE, EVENTS_MANAGER},
defs::{DEFAULT_FIRMWARE_VERSION, DEFAULT_HARDWARE_VERSION, MECHANISM_LIST},
lock_session, padded_str,
utils::version_struct_from_str,
lock_session,
utils::{padded_str, version_struct_from_str},
};

pub extern "C" fn C_GetSlotList(
Expand Down Expand Up @@ -129,8 +129,8 @@ pub extern "C" fn C_GetSlotInfo(
}

let info: CK_SLOT_INFO = CK_SLOT_INFO {
slotDescription: padded_str!(&info.product, 64),
manufacturerID: padded_str!(info.vendor, 32),
slotDescription: padded_str(&info.product),
manufacturerID: padded_str(&info.vendor),
flags,
hardwareVersion: DEFAULT_HARDWARE_VERSION,
firmwareVersion: DEFAULT_FIRMWARE_VERSION,
Expand Down Expand Up @@ -209,10 +209,10 @@ pub extern "C" fn C_GetTokenInfo(
}

let token_info = CK_TOKEN_INFO {
label: padded_str!(slot.label, 32),
manufacturerID: padded_str!(info.entity.vendor, 32),
model: padded_str!(info.entity.product, 16),
serialNumber: padded_str!(serial_number, 16),
label: padded_str(&slot.label),
manufacturerID: padded_str(&info.entity.vendor),
model: padded_str(&info.entity.product),
serialNumber: padded_str(&serial_number),
flags,
hardwareVersion: hardware_version,
firmwareVersion: firmware_version,
Expand Down
13 changes: 5 additions & 8 deletions pkcs11/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,9 @@ macro_rules! read_session {
// Modified from the ACM project : https://github.com/aws/aws-nitro-enclaves-acm/blob/main/src/vtok_p11/src/util/mod.rs
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
#[macro_export]
macro_rules! padded_str {
($src:expr, $len: expr) => {{
let mut ret = [b' '; $len];
let count = std::cmp::min($src.len(), $len);
ret[..count].copy_from_slice(&$src.as_bytes()[..count]);
ret
}};
pub fn padded_str<const N: usize>(s: &str) -> [u8; N] {
let mut ret = [b' '; N];
let count = std::cmp::min(s.len(), N);
ret[..count].copy_from_slice(&s.as_bytes()[..count]);
ret
}
Loading