You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@teaclave.apache.org by iv...@apache.org on 2026/10/22 01:07:40 UTC

(teaclave-trustzone-sdk) 01/02: optee-utee: expose PBKDF2 key derivation from OP-TEE 4.11

This is an automated email from the ASF dual-hosted git repository.

ivila pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/teaclave-trustzone-sdk.git

commit ee688845c6af121ac0246895db08dfd719ee1b26
Author: ivila <iv...@apache.org>
AuthorDate: Wed Oct 21 17:08:13 2026 +0800

    optee-utee: expose PBKDF2 key derivation from OP-TEE 4.11
---
 crates/optee-utee-sys/src/tee_api_defines.rs     | 16 ++++
 crates/optee-utee-systest/build.rs               |  1 +
 crates/optee-utee/src/crypto_op.rs               | 98 +++++++++++++++++++++++-
 crates/optee-utee/src/object/attribute.rs        |  9 +++
 crates/optee-utee/src/object/transient_object.rs |  3 +
 5 files changed, 124 insertions(+), 3 deletions(-)

diff --git a/crates/optee-utee-sys/src/tee_api_defines.rs b/crates/optee-utee-sys/src/tee_api_defines.rs
index 149b82b..b66a48d 100644
--- a/crates/optee-utee-sys/src/tee_api_defines.rs
+++ b/crates/optee-utee-sys/src/tee_api_defines.rs
@@ -197,6 +197,14 @@ pub const TEE_ALG_DSA_SHA224: u32 = 0x70003131;
 pub const TEE_ALG_DSA_SHA256: u32 = 0x70004131;
 pub const TEE_ALG_SM2_DSA_SM3: u32 = 0x70006045;
 pub const TEE_ALG_DH_DERIVE_SHARED_SECRET: u32 = 0x80000032;
+// PKCS #5 v2.0 PBKDF2 (RFC 2898), OP-TEE extensions, see
+// tee_api_defines_extensions.h in OP-TEE.
+pub const TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY: u32 = 0x800020C2;
+pub const TEE_ALG_PBKDF2_HMAC_SHA224_DERIVE_KEY: u32 = 0x800030C2;
+pub const TEE_ALG_PBKDF2_HMAC_SHA256_DERIVE_KEY: u32 = 0x800040C2;
+pub const TEE_ALG_PBKDF2_HMAC_SHA384_DERIVE_KEY: u32 = 0x800050C2;
+pub const TEE_ALG_PBKDF2_HMAC_SHA512_DERIVE_KEY: u32 = 0x800060C2;
+pub const TEE_ALG_PBKDF2_HMAC_SM3_DERIVE_KEY: u32 = 0x800070C2;
 pub const TEE_ALG_SM2_KEP: u32 = 0x60000045;
 pub const TEE_ALG_MD5: u32 = 0x50000001;
 pub const TEE_ALG_SHA1: u32 = 0x50000002;
@@ -256,6 +264,8 @@ pub const TEE_TYPE_SM2_PKE_KEYPAIR: u32 = 0xA1000047;
 pub const TEE_TYPE_GENERIC_SECRET: u32 = 0xA0000000;
 pub const TEE_TYPE_CORRUPTED_OBJECT: u32 = 0xA00000BE;
 pub const TEE_TYPE_DATA: u32 = 0xA00000BF;
+// OP-TEE extension, see tee_api_defines_extensions.h in OP-TEE.
+pub const TEE_TYPE_PBKDF2_PASSWORD: u32 = 0xA10000C2;
 
 // List of Object or Operation Attributes
 pub const TEE_ATTR_SECRET_VALUE: u32 = 0xC0000000;
@@ -291,6 +301,12 @@ pub const TEE_ATTR_SM2_KEP_CONFIRMATION_IN: u32 = 0xD0000746;
 pub const TEE_ATTR_SM2_KEP_CONFIRMATION_OUT: u32 = 0xD0000846;
 pub const TEE_ATTR_ECC_EPHEMERAL_PUBLIC_VALUE_X: u32 = 0xD0000946;
 pub const TEE_ATTR_ECC_EPHEMERAL_PUBLIC_VALUE_Y: u32 = 0xD0000A46;
+// PKCS #5 v2.0 PBKDF2 (RFC 2898), OP-TEE extensions, see
+// tee_api_defines_extensions.h in OP-TEE.
+pub const TEE_ATTR_PBKDF2_PASSWORD: u32 = 0xC00001C2;
+pub const TEE_ATTR_PBKDF2_SALT: u32 = 0xD00002C2;
+pub const TEE_ATTR_PBKDF2_ITERATION_COUNT: u32 = 0xF00003C2;
+pub const TEE_ATTR_PBKDF2_DKM_LENGTH: u32 = 0xF00004C2;
 pub const TEE_ATTR_FLAG_PUBLIC: u32 = 1 << 28;
 pub const TEE_ATTR_FLAG_VALUE: u32 = 1 << 29;
 // Deprecated, but kept for backwards compatibility
diff --git a/crates/optee-utee-systest/build.rs b/crates/optee-utee-systest/build.rs
index 5422fb2..3cbcfc4 100644
--- a/crates/optee-utee-systest/build.rs
+++ b/crates/optee-utee-systest/build.rs
@@ -56,6 +56,7 @@ fn generate_cfg(ta_include_path: PathBuf) -> ctest::TestGenerator {
         .header("tee_udpsocket.h")
         .header("tee_internal_api.h")
         .header("tee_internal_api_extensions.h")
+        .header("tee_api_defines_extensions.h")
         .header("__tee_tcpsocket_defines_extensions.h")
         .include(ta_include_path.display().to_string())
         .rename_type(|s| match s {
diff --git a/crates/optee-utee/src/crypto_op.rs b/crates/optee-utee/src/crypto_op.rs
index bda53d7..2eb90f3 100644
--- a/crates/optee-utee/src/crypto_op.rs
+++ b/crates/optee-utee/src/crypto_op.rs
@@ -1471,14 +1471,19 @@ impl OpHandle for Asymmetric {
 pub struct DeriveKey(OperationHandle);
 
 impl DeriveKey {
-    /// Take one of the Asymmetric Derivation Operation Algorithm that supports this operation as
+    /// Take one of the key derivation algorithms that supports this operation as
     /// defined in [AlgorithmId](AlgorithmId), and output a key object.
     ///
     /// # Parameters
     ///
     /// 1) `params`: For algorithm [DhDeriveSharedSecret][AlgorithmId::DhDeriveSharedSecret],
     ///    [DhPublicValue](../object/enum.AttributeId.html#variant.DhPublicValue) is required as
-    ///    the passed in attribute.
+    ///    the passed in attribute. For the PBKDF2-HMAC algorithms
+    ///    ([Pbkdf2HmacSha1][AlgorithmId::Pbkdf2HmacSha1] and similar), pass the
+    ///    salt, the iteration count and the derived key material length as
+    ///    attributes, and use a
+    ///    [Pbkdf2Password](crate::TransientObjectType::Pbkdf2Password) object
+    ///    as the operation key.
     /// 2) `object`: An uninitialized transient object to be filled with the derived key.
     ///
     /// # Example
@@ -1556,7 +1561,9 @@ impl DeriveKey {
     }
 
     /// Function usage is similar to [Digest::allocate](Digest::allocate).
-    /// Currently only supports [DhDeriveSharedSecret][AlgorithmId::DhDeriveSharedSecret] as
+    /// Currently supports [DhDeriveSharedSecret][AlgorithmId::DhDeriveSharedSecret],
+    /// [EcDhDeriveSharedSecret][AlgorithmId::EcDhDeriveSharedSecret],
+    /// [X25519][AlgorithmId::X25519] and the PBKDF2-HMAC algorithms as
     /// `algo`.
     pub fn allocate(algo: AlgorithmId, max_key_size: usize) -> Result<Self> {
         match OperationHandle::allocate(algo, OperationMode::Derive, max_key_size) {
@@ -1631,6 +1638,7 @@ impl Random {
 }
 
 /// Algorithms that can be allocated as an crypto operation.
+#[derive(Clone, Copy, strum::Display)]
 #[repr(u32)]
 pub enum AlgorithmId {
     /// [Cipher](Cipher) supported algorithm.
@@ -1748,6 +1756,18 @@ pub enum AlgorithmId {
     DhDeriveSharedSecret = 0x80000032,
     /// [DeriveKey](DeriveKey) supported algorithm.
     EcDhDeriveSharedSecret = 0x80000042,
+    /// [DeriveKey](DeriveKey) supported algorithm, PBKDF2 with HMAC-SHA1.
+    Pbkdf2HmacSha1 = 0x800020C2,
+    /// [DeriveKey](DeriveKey) supported algorithm, PBKDF2 with HMAC-SHA224.
+    Pbkdf2HmacSha224 = 0x800030C2,
+    /// [DeriveKey](DeriveKey) supported algorithm, PBKDF2 with HMAC-SHA256.
+    Pbkdf2HmacSha256 = 0x800040C2,
+    /// [DeriveKey](DeriveKey) supported algorithm, PBKDF2 with HMAC-SHA384.
+    Pbkdf2HmacSha384 = 0x800050C2,
+    /// [DeriveKey](DeriveKey) supported algorithm, PBKDF2 with HMAC-SHA512.
+    Pbkdf2HmacSha512 = 0x800060C2,
+    /// [DeriveKey](DeriveKey) supported algorithm, PBKDF2 with HMAC-SM3.
+    Pbkdf2HmacSm3 = 0x800070C2,
     /// [Asymmetric](Asymmetric) supported algorithm, can be applied with
     /// [Sign](OperationMode::Sign) or [Verify](OperationMode::Verify) mode.
     EcDsaSha1 = 0x70001042,
@@ -1817,3 +1837,75 @@ pub enum ElementId {
     /// Source: `IETF`, Generic: `N`, Size: 256 bits
     EccCurve25519 = 0x00000300,
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use crate::{AttributeId, TransientObjectType};
+
+    #[test]
+    // Guarantee: every high-level PBKDF2 `AlgorithmId` variant maps 1:1
+    // to the OP-TEE raw constant from `tee_api_defines_extensions.h`,
+    // so `DeriveKey::allocate` passes the correct algorithm ID to
+    // `TEE_AllocateOperation`.
+    fn pbkdf2_algorithm_ids_match_raw_constants() {
+        let cases = [
+            (
+                AlgorithmId::Pbkdf2HmacSha1,
+                raw::TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY,
+            ),
+            (
+                AlgorithmId::Pbkdf2HmacSha224,
+                raw::TEE_ALG_PBKDF2_HMAC_SHA224_DERIVE_KEY,
+            ),
+            (
+                AlgorithmId::Pbkdf2HmacSha256,
+                raw::TEE_ALG_PBKDF2_HMAC_SHA256_DERIVE_KEY,
+            ),
+            (
+                AlgorithmId::Pbkdf2HmacSha384,
+                raw::TEE_ALG_PBKDF2_HMAC_SHA384_DERIVE_KEY,
+            ),
+            (
+                AlgorithmId::Pbkdf2HmacSha512,
+                raw::TEE_ALG_PBKDF2_HMAC_SHA512_DERIVE_KEY,
+            ),
+            (
+                AlgorithmId::Pbkdf2HmacSm3,
+                raw::TEE_ALG_PBKDF2_HMAC_SM3_DERIVE_KEY,
+            ),
+        ];
+        for (algo, expected) in cases {
+            assert_eq!(algo as u32, expected, "algorithm ID mismatch for {algo}");
+        }
+    }
+
+    #[test]
+    // Guarantee: the high-level PBKDF2 object type and attribute IDs map
+    // 1:1 to the OP-TEE raw constants, so password objects and PBKDF2
+    // operation parameters are built with the correct IDs.
+    fn pbkdf2_object_and_attribute_ids_match_raw_constants() {
+        let object_cases = [(
+            TransientObjectType::Pbkdf2Password,
+            raw::TEE_TYPE_PBKDF2_PASSWORD,
+        )];
+        for (obj_type, expected) in object_cases {
+            assert_eq!(obj_type as u32, expected, "ID mismatch for {obj_type}");
+        }
+        let attr_cases = [
+            (AttributeId::Pbkdf2Password, raw::TEE_ATTR_PBKDF2_PASSWORD),
+            (AttributeId::Pbkdf2Salt, raw::TEE_ATTR_PBKDF2_SALT),
+            (
+                AttributeId::Pbkdf2IterationCount,
+                raw::TEE_ATTR_PBKDF2_ITERATION_COUNT,
+            ),
+            (
+                AttributeId::Pbkdf2DkmLength,
+                raw::TEE_ATTR_PBKDF2_DKM_LENGTH,
+            ),
+        ];
+        for (attr, expected) in attr_cases {
+            assert_eq!(attr as u32, expected, "ID mismatch for {attr}");
+        }
+    }
+}
diff --git a/crates/optee-utee/src/object/attribute.rs b/crates/optee-utee/src/object/attribute.rs
index 91ed92c..1292c5c 100644
--- a/crates/optee-utee/src/object/attribute.rs
+++ b/crates/optee-utee/src/object/attribute.rs
@@ -149,6 +149,7 @@ impl AttributeValue {
     }
 }
 
+#[derive(Clone, Copy, strum::Display)]
 #[repr(u32)]
 pub enum AttributeId {
     /// Used for all secret keys for symmetric ciphers, MACs, and HMACs
@@ -191,6 +192,14 @@ pub enum AttributeId {
     DhPublicValue = 0xD0000132,
     /// Diffie-Hellman public value: `x`
     DhPrivateValue = 0xC0000232,
+    /// PBKDF2 password, bytes of the password
+    Pbkdf2Password = 0xC00001C2,
+    /// PBKDF2 salt, bytes of the salt
+    Pbkdf2Salt = 0xD00002C2,
+    /// PBKDF2 iteration count
+    Pbkdf2IterationCount = 0xF00003C2,
+    /// PBKDF2 derived key material length in bytes
+    Pbkdf2DkmLength = 0xF00004C2,
     RsaOaepLabel = 0xD0000930,
     RsaOaepMgf1Hash = 0xD0000931,
     RsaPssSaltLength = 0xF0000A30,
diff --git a/crates/optee-utee/src/object/transient_object.rs b/crates/optee-utee/src/object/transient_object.rs
index 71d655e..0fcde59 100644
--- a/crates/optee-utee/src/object/transient_object.rs
+++ b/crates/optee-utee/src/object/transient_object.rs
@@ -24,6 +24,7 @@ use crate::{Result, tee_check};
 
 /// Define types of [TransientObject](crate::TransientObject) with
 /// predefined maximum sizes.
+#[derive(Clone, Copy, strum::Display)]
 #[repr(u32)]
 pub enum TransientObjectType {
     /// 128, 192, or 256 bits
@@ -65,6 +66,8 @@ pub enum TransientObjectType {
     DsaKeypair = 0xA1000031,
     /// From 256 to 2048 bits, multiple of 8 bits.
     DhKeypair = 0xA1000032,
+    /// Password for PBKDF2 key derivation. Size in bits, multiple of 8 bits.
+    Pbkdf2Password = 0xA10000C2,
     /// Between 160 and 521 bits. Conditional: Available only if at least
     /// one of the ECC the curves defined in Table 6-14 with "generic"
     /// equal to "Y" is supported.


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@teaclave.apache.org
For additional commands, e-mail: commits-help@teaclave.apache.org