Skip to content

Commit

Permalink
Reduce code size by swapping utf-8 checker for ascii checker
Browse files Browse the repository at this point in the history
Checking that byte slice is utf-8 requires a significant amount of code
space. By enforcing ascii this space can be saved at the cost of using
subject alt names that contain non-ascii utf-8 characters.
  • Loading branch information
clundin25 authored and jhand2 committed Jan 31, 2025
1 parent fa6b1a9 commit f17528a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
3 changes: 0 additions & 3 deletions runtime/src/certify_key_extended.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ Abstract:
--*/

use core::str::from_utf8;

use arrayvec::ArrayVec;
use bitflags::bitflags;
use caliptra_common::mailbox_api::{
CertifyKeyExtendedFlags, CertifyKeyExtendedReq, CertifyKeyExtendedResp, MailboxResp,
Expand Down
26 changes: 17 additions & 9 deletions runtime/src/subject_alt_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ Abstract:
--*/

use core::str::from_utf8;

use arrayvec::ArrayVec;
use caliptra_common::mailbox_api::{AddSubjectAltNameReq, MailboxResp};
use caliptra_error::{CaliptraError, CaliptraResult};
Expand Down Expand Up @@ -52,14 +50,24 @@ impl AddSubjectAltNameCmd {
}
}

/// Verifies that `dmtf_device_info` only contains ascii characters and contains exactly 2 ':'
/// characters.
///
/// dmtf_device_info_utf8 must match ^[^:]*:[^:]*:[^:]*$
fn validate_dmtf_device_info(dmtf_device_info: &[u8]) -> CaliptraResult<()> {
let dmtf_device_info_utf8 = from_utf8(dmtf_device_info)
.map_err(|_| CaliptraError::RUNTIME_DMTF_DEVICE_INFO_VALIDATION_FAILED)?;
// dmtf_device_info_utf8 must match ^[^:]*:[^:]*:[^:]*$
if dmtf_device_info_utf8.chars().filter(|c| *c == ':').count() != 2 {
Err(CaliptraError::RUNTIME_DMTF_DEVICE_INFO_VALIDATION_FAILED)
} else {
Ok(())
let mut colon_count = 0;
for c in dmtf_device_info.iter() {
if !c.is_ascii() {
Err(CaliptraError::RUNTIME_DMTF_DEVICE_INFO_VALIDATION_FAILED)?
}

if *c == b':' {
colon_count += 1;
}
}
if colon_count != 2 {
Err(CaliptraError::RUNTIME_DMTF_DEVICE_INFO_VALIDATION_FAILED)?
}
Ok(())
}
}

0 comments on commit f17528a

Please sign in to comment.