Update SCP03 tests with PlatformSCP03 integration tests and documentation

- Add PlatformSCP03 integration test cases (test_scp03_platform_integration, test_scp03_platform_key_file)
- Update test helpers with mock session creation
- Update README with PlatformSCP03 configuration guide
- Add references to NXP AN12413 and AN12436
- Fix test assertions to work with opaque session type
This commit is contained in:
km
2026-03-26 07:27:23 +09:00
commit c29a189b9a
13 changed files with 3192 additions and 0 deletions
+129
View File
@@ -0,0 +1,129 @@
/**
* @file se050_crypto_utils.h
* @brief Cryptographic utility functions
*
* Constant-time memory operations for security-critical code.
*
* License: MIT (Clean-room implementation)
*/
#ifndef SE050_CRYPTO_UTILS_H
#define SE050_CRYPTO_UTILS_H
#include <stdint.h>
#include <stddef.h>
#ifdef __linux__
/* Linux kernel-style definitions */
#include <linux/types.h>
/* Constant-time memory comparison */
static inline int crypto_memneq(const void *a, const void *b, size_t len)
{
const volatile uint8_t *pa = (const volatile uint8_t *)a;
const volatile uint8_t *pb = (const volatile uint8_t *)b;
volatile uint8_t result = 0;
size_t i;
for (i = 0; i < len; i++) {
result |= pa[i] ^ pb[i];
}
return result;
}
/* Constant-time memory zeroing */
static inline void memzero_explicit(void *s, size_t count)
{
volatile uint8_t *p = (volatile uint8_t *)s;
while (count--) {
*p++ = 0;
}
/* Prevent compiler from optimizing out the zeroing */
__asm__ __volatile__("":::"memory");
}
#else
/* Non-Linux implementations */
/**
* @brief Constant-time memory comparison
* @param a First memory area
* @param b Second memory area
* @param len Number of bytes to compare
* @return 0 if equal, non-zero otherwise
*
* This function runs in constant time to prevent timing attacks.
*/
static inline int crypto_memneq(const void *a, const void *b, size_t len)
{
const volatile uint8_t *pa = (const volatile uint8_t *)a;
const volatile uint8_t *pb = (const volatile uint8_t *)b;
volatile uint8_t result = 0;
size_t i;
for (i = 0; i < len; i++) {
result |= pa[i] ^ pb[i];
}
return result;
}
/**
* @brief Constant-time memory zeroing
* @param s Memory area to zero
* @param count Number of bytes to zero
*
* This function uses volatile to prevent compiler optimization.
*/
static inline void memzero_explicit(void *s, size_t count)
{
volatile uint8_t *p = (volatile uint8_t *)s;
while (count--) {
*p++ = 0;
}
}
/* Memory barrier for non-Linux platforms */
#ifndef barrier
#define barrier() do { __asm__ __volatile__("": : :"memory"); } while(0)
#endif
/* ssize_t definition for non-Linux platforms */
#ifndef _SSIZE_T_DEFINED
#define _SSIZE_T_DEFINED
#include <sys/types.h>
#endif
#endif /* __linux__ */
/* Additional security utilities */
/**
* @brief Securely compare two buffers
* @param a First buffer
* @param b Second buffer
* @param len Length of buffers
* @return 0 if equal, -1 if different
*/
static inline int secure_memcmp(const void *a, const void *b, size_t len)
{
return crypto_memneq(a, b, len) ? -1 : 0;
}
/**
* @brief Securely copy memory with zeroing of destination first
* @param dst Destination buffer
* @param src Source buffer
* @param len Number of bytes to copy
*/
static inline void secure_memcpy(void *dst, const void *src, size_t len)
{
memzero_explicit(dst, len);
__builtin_memcpy(dst, src, len);
}
#endif /* SE050_CRYPTO_UTILS_H */
+79
View File
@@ -0,0 +1,79 @@
/**
* @file se050_keystore_internal.h
* @brief SE050 Key Store Internal Definitions
*
* Internal definitions for key store implementation.
*
* License: MIT (Clean-room implementation)
*/
#ifndef SE050_KEYSTORE_INTERNAL_H
#define SE050_KEYSTORE_INTERNAL_H
#include "se050_wireguard.h"
#include <stdint.h>
#include <stddef.h>
/* Key object types */
typedef enum {
KEY_PART_PRIVATE = 0,
KEY_PART_PUBLIC,
KEY_PART_PAIR,
} key_part_t;
/* Key cipher types */
typedef enum {
CIPHER_TYPE_NONE = 0,
CIPHER_TYPE_EC_MONTGOMERY, /* X25519 */
CIPHER_TYPE_EC_NIST_P256,
CIPHER_TYPE_AES,
} cipher_type_t;
/* Key object flags */
#define KEY_FLAG_PERSISTENT (1 << 0)
#define KEY_FLAG_TRANSIENT (1 << 1)
#define KEY_FLAG_GENERATED (1 << 2)
#define KEY_FLAG_EXPORTED (1 << 3)
/**
* @brief Key object structure
*/
typedef struct {
uint32_t key_id; /**< Key identifier */
key_part_t key_part; /**< Key part (private/public/pair) */
cipher_type_t cipher_type; /**< Cipher type */
size_t key_size; /**< Key size in bytes */
uint8_t flags; /**< Key flags */
uint8_t private_key[32]; /**< Private key data (secure) */
uint8_t public_key[32]; /**< Public key data */
} key_object_t;
/**
* @brief Key store context structure
*/
struct se050_keystore_ctx {
se050_session_ctx_t *session; /**< Associated session */
key_object_t *objects; /**< Key objects array */
size_t num_objects; /**< Number of key objects */
size_t max_objects; /**< Maximum key objects */
};
/* Internal functions */
key_object_t *find_key_object(se050_keystore_ctx_t *keystore, uint32_t key_id);
key_object_t *allocate_key_object(se050_keystore_ctx_t *keystore);
se050_status_t se050_keystore_generate_key(se050_keystore_ctx_t *keystore,
uint32_t key_id,
cipher_type_t cipher_type,
size_t key_size,
uint8_t *private_key,
uint8_t *public_key);
se050_status_t se050_keystore_get_public_key(se050_keystore_ctx_t *keystore,
uint32_t key_id,
uint8_t *public_key,
size_t *key_size);
se050_status_t se050_keystore_get_private_key(se050_keystore_ctx_t *keystore,
uint32_t key_id,
uint8_t *private_key,
size_t *key_size);
#endif /* SE050_KEYSTORE_INTERNAL_H */
+37
View File
@@ -0,0 +1,37 @@
/**
* @file se050_session_internal.h
* @brief SE050 Session Internal Definitions
*
* Internal definitions for session implementation.
*
* License: MIT (Clean-room implementation)
*/
#ifndef SE050_SESSION_INTERNAL_H
#define SE050_SESSION_INTERNAL_H
#include "se050_wireguard.h"
#include <stdint.h>
/* Session states */
typedef enum {
SESSION_STATE_CREATED = 0,
SESSION_STATE_OPENED,
SESSION_STATE_CLOSED,
} session_state_t;
/**
* @brief Session context structure
*/
struct se050_session_ctx {
se050_i2c_hal_t *hal; /**< I2C HAL interface */
session_state_t state; /**< Current session state */
uint32_t session_id; /**< Unique session identifier */
uint8_t session_key[32]; /**< Session encryption key */
size_t session_key_len; /**< Session key length */
uint32_t cmd_counter; /**< Command counter for SCP03 */
uint32_t resp_counter; /**< Response counter for SCP03 */
se050_rng_ctx_t *rng; /**< RNG context */
};
#endif /* SE050_SESSION_INTERNAL_H */
+377
View File
@@ -0,0 +1,377 @@
/**
* @file se050_wireguard.h
* @brief SE050 WireGuard Minimum Interface
*
* Clean-room interface for WireGuard cryptographic operations using SE050.
*
* This header defines the API for:
* - X25519 ECDH key exchange
* - True Random Number Generation (TRNG)
* - Platform SCP03 secure channel
*
* License: MIT (Clean-room implementation)
*/
#ifndef SE050_WIREGUARD_H
#define SE050_WIREGUARD_H
#include <stdint.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
/* ============================================================================
* Error Codes
* ============================================================================ */
typedef enum {
SE050_OK = 0x00,
SE050_ERR_FAIL = 0x01,
SE050_ERR_INVALID_ARG = 0x02,
SE050_ERR_SESSION = 0x03,
SE050_ERR_KEY_STORE = 0x04,
SE050_ERR_RNG = 0x05,
SE050_ERR_ECDH = 0x06,
SE050_ERR_SCP03 = 0x07,
SE050_ERR_I2C = 0x08,
SE050_ERR_NOT_INIT = 0x09,
} se050_status_t;
/* ============================================================================
* Constants
* ============================================================================ */
/** WireGuard key size (X25519 uses 32-byte keys) */
#define SE050_WG_KEY_SIZE 32
/** WireGuard public key size */
#define SE050_WG_PUBKEY_SIZE 32
/** SCP03 key size */
#define SE050_SCP03_KEY_SIZE 16
/** SCP03 IV size */
#define SE050_SCP03_IV_SIZE 16
/** SCP03 CMAC size */
#define SE050_SCP03_CMAC_SIZE 8
/** Maximum buffer size for SCP03 */
#define SE050_SCP03_MAX_BUF 1024
/* ============================================================================
* Type Definitions
* ============================================================================ */
/** Session context for SE050 communication */
typedef struct se050_session_ctx se050_session_ctx_t;
/** Key store context */
typedef struct se050_keystore_ctx se050_keystore_ctx_t;
/** RNG context */
typedef struct se050_rng_ctx se050_rng_ctx_t;
/** SCP03 session context */
typedef struct se050_scp03_ctx se050_scp03_ctx_t;
/** X25519 key pair */
typedef struct {
uint8_t private_key[SE050_WG_KEY_SIZE]; /**< Private key (32 bytes) */
uint8_t public_key[SE050_WG_PUBKEY_SIZE]; /**< Public key (32 bytes) */
} se050_x25519_keypair_t;
/** I2C HAL interface */
typedef struct {
void *handle; /**< I2C device handle */
uint8_t slave_addr; /**< I2C slave address (default: 0x90) */
const char *dev_path; /**< Device path (e.g., "/dev/i2c-1") */
} se050_i2c_hal_t;
/* ============================================================================
* I2C HAL Layer
* ============================================================================ */
/**
* @brief Initialize I2C HAL
* @param hal I2C HAL structure
* @param dev_path Device path (e.g., "/dev/i2c-1")
* @param slave_addr I2C slave address (default: 0x90)
* @return SE050_OK on success
*/
se050_status_t se050_i2c_init(se050_i2c_hal_t *hal, const char *dev_path, uint8_t slave_addr);
/**
* @brief Close I2C HAL
* @param hal I2C HAL structure
*/
void se050_i2c_close(se050_i2c_hal_t *hal);
/**
* @brief Read from SE050 via I2C
* @param hal I2C HAL structure
* @param buffer Read buffer
* @param length Number of bytes to read
* @return Number of bytes read, or negative on error
*/
int se050_i2c_read(se050_i2c_hal_t *hal, uint8_t *buffer, int length);
/**
* @brief Write to SE050 via I2C
* @param hal I2C HAL structure
* @param buffer Write buffer
* @param length Number of bytes to write
* @return Number of bytes written, or negative on error
*/
int se050_i2c_write(se050_i2c_hal_t *hal, const uint8_t *buffer, int length);
/**
* @brief Wake up SE050 (if needed)
* @param hal I2C HAL structure
* @return SE050_OK on success
*/
se050_status_t se050_i2c_wakeup(se050_i2c_hal_t *hal);
/* ============================================================================
* Session Management
* ============================================================================ */
/**
* @brief Create SE050 session
* @param ctx Output session context
* @param hal I2C HAL interface
* @return SE050_OK on success
*/
se050_status_t se050_session_create(se050_session_ctx_t **ctx, se050_i2c_hal_t *hal);
/**
* @brief Open SE050 session
* @param ctx Session context
* @return SE050_OK on success
*/
se050_status_t se050_session_open(se050_session_ctx_t *ctx);
/**
* @brief Close SE050 session
* @param ctx Session context
*/
void se050_session_close(se050_session_ctx_t *ctx);
/**
* @brief Delete SE050 session
* @param ctx Session context
*/
void se050_session_delete(se050_session_ctx_t *ctx);
/* ============================================================================
* Key Store
* ============================================================================ */
/**
* @brief Initialize key store
* @param ctx Output key store context
* @param session SE050 session
* @return SE050_OK on success
*/
se050_status_t se050_keystore_init(se050_keystore_ctx_t **ctx, se050_session_ctx_t *session);
/**
* @brief Free key store
* @param ctx Key store context
*/
void se050_keystore_free(se050_keystore_ctx_t *ctx);
/* ============================================================================
* Random Number Generation (TRNG)
* ============================================================================ */
/**
* @brief Initialize RNG context
* @param ctx Output RNG context
* @param session SE050 session
* @return SE050_OK on success
*/
se050_status_t se050_rng_init(se050_rng_ctx_t **ctx, se050_session_ctx_t *session);
/**
* @brief Generate random bytes using SE050 TRNG
* @param ctx RNG context
* @param output Output buffer
* @param length Number of bytes to generate
* @return SE050_OK on success
*/
se050_status_t se050_rng_generate(se050_rng_ctx_t *ctx, uint8_t *output, size_t length);
/**
* @brief Free RNG context
* @param ctx RNG context
*/
void se050_rng_free(se050_rng_ctx_t *ctx);
/* ============================================================================
* X25519 ECDH
* ============================================================================ */
/**
* @brief Generate X25519 key pair
* @param keystore Key store context
* @param keypair Output key pair
* @param key_id Unique key identifier
* @return SE050_OK on success
*
* Note: Private key is generated using SE050 TRNG and stored securely.
* The private key never leaves the SE050.
*/
se050_status_t se050_x25519_generate_keypair(se050_keystore_ctx_t *keystore,
se050_x25519_keypair_t *keypair,
uint32_t key_id);
/**
* @brief Compute X25519 ECDH shared secret
* @param keystore Key store context
* @param private_key_id Local private key ID
* @param peer_public Peer's public key (32 bytes)
* @param shared_secret Output shared secret (32 bytes)
* @return SE050_OK on success
*
* Note: ECDH computation is performed inside SE050.
*/
se050_status_t se050_x25519_compute_shared_secret(se050_keystore_ctx_t *keystore,
uint32_t private_key_id,
const uint8_t *peer_public,
uint8_t *shared_secret);
/**
* @brief Export X25519 public key from SE050
* @param keystore Key store context
* @param key_id Key identifier
* @param public_key Output public key (32 bytes)
* @return SE050_OK on success
*/
se050_status_t se050_x25519_export_public_key(se050_keystore_ctx_t *keystore,
uint32_t key_id,
uint8_t *public_key);
/* ============================================================================
* Platform SCP03 Secure Channel
* ============================================================================ */
/**
* @brief Initialize SCP03 context
* @param ctx Output SCP03 context
* @param session SE050 session
* @return SE050_OK on success
*/
se050_status_t se050_scp03_init(se050_scp03_ctx_t **ctx, se050_session_ctx_t *session);
/**
* @brief Set SCP03 keys (ENC, MAC, and DEK)
* @param ctx SCP03 context
* @param enc_key Encryption key (16 bytes)
* @param mac_key MAC key (16 bytes)
* @param dek_key Data Encryption Key (16 bytes)
* @return SE050_OK on success
*/
se050_status_t se050_scp03_set_keys(se050_scp03_ctx_t *ctx,
const uint8_t *enc_key,
const uint8_t *mac_key,
const uint8_t *dek_key);
/**
* @brief Load SCP03 keys from file
* @param ctx SCP03 context
* @param file_path Path to key file (ENC[16] + MAC[16] + DEK[16] = 48 bytes)
* @return SE050_OK on success
*/
se050_status_t se050_scp03_load_keys_from_file(se050_scp03_ctx_t *ctx, const char *file_path);
/**
* @brief Encrypt command APDU
* @param ctx SCP03 context
* @param cmd Command buffer (in-place encryption)
* @param cmd_len Command length (updated after padding)
* @return SE050_OK on success
*/
se050_status_t se050_scp03_encrypt_command(se050_scp03_ctx_t *ctx,
uint8_t *cmd,
size_t *cmd_len);
/**
* @brief Decrypt response APDU
* @param ctx SCP03 context
* @param cmd_len Original command length (for ICV calculation)
* @param rsp Response buffer (in-place decryption)
* @param rsp_len Response length (updated after decryption)
* @return SW status code (e.g., 0x9000 for success)
*/
uint16_t se050_scp03_decrypt_response(se050_scp03_ctx_t *ctx,
size_t cmd_len,
uint8_t *rsp,
size_t *rsp_len);
/**
* @brief Free SCP03 context
* @param ctx SCP03 context
*/
void se050_scp03_free(se050_scp03_ctx_t *ctx);
/* ============================================================================
* High-Level WireGuard API
* ============================================================================ */
/**
* @brief Initialize WireGuard SE050 subsystem
* @param hal I2C HAL interface
* @param session Output session context
* @param keystore Output key store context
* @param rng Output RNG context
* @return SE050_OK on success
*/
se050_status_t se050_wireguard_init(se050_i2c_hal_t *hal,
se050_session_ctx_t **session,
se050_keystore_ctx_t **keystore,
se050_rng_ctx_t **rng);
/**
* @brief Generate WireGuard key pair
* @param keystore Key store context
* @param rng RNG context
* @param keypair Output key pair
* @param key_id Key identifier
* @return SE050_OK on success
*/
se050_status_t se050_wireguard_generate_key(se050_keystore_ctx_t *keystore,
se050_rng_ctx_t *rng,
se050_x25519_keypair_t *keypair,
uint32_t key_id);
/**
* @brief Compute WireGuard shared secret
* @param keystore Key store context
* @param key_id Local private key ID
* @param peer_public Peer's public key
* @param shared_secret Output shared secret
* @return SE050_OK on success
*/
se050_status_t se050_wireguard_compute_shared(se050_keystore_ctx_t *keystore,
uint32_t key_id,
const uint8_t *peer_public,
uint8_t *shared_secret);
/**
* @brief Cleanup WireGuard SE050 subsystem
* @param session Session context
* @param keystore Key store context
* @param rng RNG context
*/
void se050_wireguard_cleanup(se050_session_ctx_t *session,
se050_keystore_ctx_t *keystore,
se050_rng_ctx_t *rng);
#ifdef __cplusplus
}
#endif
#endif /* SE050_WIREGUARD_H */