Skip to content

Commit c13b173

Browse files
committed
feat(startauthsession): add full support for algo/bits/mode combinations
Instead of only supporting the two constants (AES_128_CFB and AES_256_CFB) pre-defined in the upstream tss-esapi and the manually defined XOR, parse a structured "algo-bits-mode" format that covers all symmetric algorithms available in tss-esapi: - AES with 128/192/256 key bits and CFB/CBC/ECB/OFB/CTR modes - SM4 with 128 key bits and all modes - Camellia with 128/192/256 key bits and all modes - XOR with any supported hash algorithm (xor-sha256, etc.) - null Legacy shorthand forms ("aes128cfb", "aes256cfb" and "xor") remain accepted for backwards compatibility. Signed-off-by: Takuma IMAMURA <209989118+hyperfinitism@users.noreply.github.com>
1 parent 67986ed commit c13b173

2 files changed

Lines changed: 112 additions & 11 deletions

File tree

src/cmd/startauthsession.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub struct StartAuthSessionCmd {
4242
#[arg(long = "audit-session", conflicts_with_all = ["policy_session", "hmac_session"])]
4343
pub audit_session: bool,
4444

45-
/// Symmetric algorithm for session encryption (aes128cfb, aes256cfb, xor, null)
45+
/// Symmetric algorithm for session encryption (e.g. aes-128-cfb, sm4-128-cfb, camellia-256-cbc, xor-sha256, null)
4646
#[arg(long = "symmetric", default_value = "aes128cfb", value_parser = parse::parse_symmetric_definition)]
4747
pub symmetric: SymmetricDefinition,
4848

src/parse.rs

Lines changed: 111 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use tss_esapi::attributes::NvIndexAttributesBuilder;
1616
use tss_esapi::handles::AuthHandle;
1717
use tss_esapi::interface_types::algorithm::{HashingAlgorithm, SymmetricMode};
1818
use tss_esapi::interface_types::ecc::EccCurve;
19+
use tss_esapi::interface_types::key_bits::{AesKeyBits, CamelliaKeyBits, Sm4KeyBits};
1920
use tss_esapi::interface_types::reserved_handles::{Hierarchy, HierarchyAuth, Provision};
2021
use tss_esapi::structures::{
2122
Auth, Data, HashScheme, PcrSelectionList, PcrSelectionListBuilder, PcrSlot, SensitiveData,
@@ -484,21 +485,121 @@ pub fn parse_ecc_curve(s: &str) -> Result<EccCurve, String> {
484485
// Symmetric definition (for sessions)
485486
// ---------------------------------------------------------------------------
486487

487-
/// Parse a symmetric algorithm definition for session encryption.
488+
/// Parse a symmetric algorithm definition.
488489
///
489-
/// Accepted values: `aes128cfb`, `aes256cfb`, `xor`, `null`.
490+
/// Accepted formats:
491+
/// - `aes-{128,192,256}-{cfb,cbc,ecb,ofb,ctr}` — AES with key size and mode
492+
/// - `sm4-128-{cfb,cbc,ecb,ofb,ctr}` — SM4 with key size and mode
493+
/// - `camellia-{128,192,256}-{cfb,cbc,ecb,ofb,ctr}` — Camellia with key size and mode
494+
/// - `xor-{sha1,sha256,...}` — XOR with a hashing algorithm
495+
/// - `null` — no symmetric algorithm
496+
///
497+
/// Legacy shorthand forms `aes128cfb` and `aes256cfb` are also accepted.
490498
///
491499
/// Intended for use as a clap `value_parser`.
492500
pub fn parse_symmetric_definition(s: &str) -> Result<SymmetricDefinition, String> {
493-
match s.to_lowercase().as_str() {
494-
"aes128cfb" | "aes-128-cfb" => Ok(SymmetricDefinition::AES_128_CFB),
495-
"aes256cfb" | "aes-256-cfb" => Ok(SymmetricDefinition::AES_256_CFB),
496-
"xor" => Ok(SymmetricDefinition::Xor {
497-
hashing_algorithm: HashingAlgorithm::Sha256,
498-
}),
499-
"null" => Ok(SymmetricDefinition::Null),
501+
let lower = s.to_lowercase();
502+
503+
// Handle "null" first.
504+
if lower == "null" {
505+
return Ok(SymmetricDefinition::Null);
506+
}
507+
508+
// Legacy shorthand aliases for backwards compatibility.
509+
match lower.as_str() {
510+
"aes128cfb" => {
511+
return Ok(SymmetricDefinition::Aes {
512+
key_bits: AesKeyBits::Aes128,
513+
mode: SymmetricMode::Cfb,
514+
});
515+
}
516+
"aes256cfb" => {
517+
return Ok(SymmetricDefinition::Aes {
518+
key_bits: AesKeyBits::Aes256,
519+
mode: SymmetricMode::Cfb,
520+
});
521+
}
522+
"xor" => {
523+
return Ok(SymmetricDefinition::Xor {
524+
hashing_algorithm: HashingAlgorithm::Sha256,
525+
});
526+
}
527+
_ => {}
528+
}
529+
530+
let parts: Vec<&str> = lower.split('-').collect();
531+
532+
match parts[0] {
533+
"aes" => {
534+
if parts.len() != 3 {
535+
return Err(format!(
536+
"expected 'aes-<bits>-<mode>' (e.g. aes-128-cfb), got: '{s}'"
537+
));
538+
}
539+
let key_bits = match parts[1] {
540+
"128" => AesKeyBits::Aes128,
541+
"192" => AesKeyBits::Aes192,
542+
"256" => AesKeyBits::Aes256,
543+
_ => {
544+
return Err(format!(
545+
"unsupported AES key size: {} (expected 128, 192, or 256)",
546+
parts[1]
547+
));
548+
}
549+
};
550+
let mode = parse_symmetric_mode(parts[2])?;
551+
Ok(SymmetricDefinition::Aes { key_bits, mode })
552+
}
553+
"sm4" => {
554+
if parts.len() != 3 {
555+
return Err(format!(
556+
"expected 'sm4-128-<mode>' (e.g. sm4-128-cfb), got: '{s}'"
557+
));
558+
}
559+
let key_bits = match parts[1] {
560+
"128" => Sm4KeyBits::Sm4_128,
561+
_ => {
562+
return Err(format!(
563+
"unsupported SM4 key size: {} (expected 128)",
564+
parts[1]
565+
));
566+
}
567+
};
568+
let mode = parse_symmetric_mode(parts[2])?;
569+
Ok(SymmetricDefinition::Sm4 { key_bits, mode })
570+
}
571+
"camellia" => {
572+
if parts.len() != 3 {
573+
return Err(format!(
574+
"expected 'camellia-<bits>-<mode>' (e.g. camellia-128-cfb), got: '{s}'"
575+
));
576+
}
577+
let key_bits = match parts[1] {
578+
"128" => CamelliaKeyBits::Camellia128,
579+
"192" => CamelliaKeyBits::Camellia192,
580+
"256" => CamelliaKeyBits::Camellia256,
581+
_ => {
582+
return Err(format!(
583+
"unsupported Camellia key size: {} (expected 128, 192, or 256)",
584+
parts[1]
585+
));
586+
}
587+
};
588+
let mode = parse_symmetric_mode(parts[2])?;
589+
Ok(SymmetricDefinition::Camellia { key_bits, mode })
590+
}
591+
"xor" => {
592+
if parts.len() != 2 {
593+
return Err(format!(
594+
"expected 'xor-<hash>' (e.g. xor-sha256), got: '{s}'"
595+
));
596+
}
597+
let hashing_algorithm = parse_hashing_algorithm(parts[1])?;
598+
Ok(SymmetricDefinition::Xor { hashing_algorithm })
599+
}
500600
_ => Err(format!(
501-
"unsupported symmetric definition: {s} (supported: aes128cfb, aes256cfb, xor, null)"
601+
"unsupported symmetric algorithm: '{}'; expected aes, sm4, camellia, xor, or null",
602+
parts[0]
502603
)),
503604
}
504605
}

0 commit comments

Comments
 (0)