tree: 8d202957f1c3a3e30dd0bc1d3a7a75e1bf8217b7
  1. api/
  2. backend-rustcrypto/
  3. client/
  4. server/
  5. tests/
  6. README.md
services/crypto/README.md

Crypto Service

IPC-based cryptographic service for the Pigweed kernel, providing hash, MAC, AEAD, and digital signature operations to unprivileged tasks.

Architecture

┌─────────────────┐     IPC      ┌─────────────────┐
│  Crypto Client  │─────────────▶│  Crypto Server  │
│   (user task)   │◀─────────────│  (RustCrypto)   │
└─────────────────┘              └─────────────────┘
  • Client (crypto_client) — Ergonomic Rust API for applications
  • Server (crypto_server) — Implements operations using RustCrypto crates
  • API (crypto_api) — Wire protocol definitions and backend traits

Supported Algorithms

CategoryAlgorithmOutput SizeNotes
HashSHA-25632 bytesFIPS 180-4
SHA-38448 bytesFIPS 180-4
SHA-51264 bytesFIPS 180-4
MACHMAC-SHA25632 bytesRFC 2104
HMAC-SHA38448 bytesRFC 2104
HMAC-SHA51264 bytesRFC 2104
AEADAES-256-GCM16-byte tagNIST SP 800-38D
SignatureECDSA P-25664 bytesRFC 6979 (deterministic)
ECDSA P-38496 bytesRFC 6979 (deterministic)

† ECDSA requires crate_features = ["ecdsa"] on client and server.

Client API

use crypto_client::CryptoClient;

// Bind to the crypto server channel
let crypto = CryptoClient::new(handle::CRYPTO);

// Hashing — returns fixed-size array directly
let hash: [u8; 32] = crypto.sha256(b"hello world")?;
let hash: [u8; 48] = crypto.sha384(data)?;
let hash: [u8; 64] = crypto.sha512(data)?;

// HMAC — returns tag directly  
let tag: [u8; 32] = crypto.hmac_sha256(key, data)?;
let tag: [u8; 48] = crypto.hmac_sha384(key, data)?;
let tag: [u8; 64] = crypto.hmac_sha512(key, data)?;

// AES-256-GCM seal/open
let ct_len = crypto.aes256_gcm_seal(&key, &nonce, plaintext, &mut ciphertext)?;
let pt_len = crypto.aes256_gcm_open(&key, &nonce, &ciphertext[..ct_len], &mut plaintext)?;

// ECDSA (requires "ecdsa" feature)
let sig: [u8; 64] = crypto.ecdsa_p256_sign(&private_key, message)?;
crypto.ecdsa_p256_verify(&public_key, message, &sig)?;  // Ok(()) = valid

Error Handling

pub enum ClientError {
    IpcError(pw_status::Error),   // Channel failure
    ServerError(CryptoError),      // Crypto operation failed
    InvalidResponse,               // Malformed response
    BufferTooSmall,               // Output buffer insufficient
    VerificationFailed,           // Signature/tag invalid
}

ClientError implements Display for logging.

Wire Protocol

Request format:

┌────────────────┬────────────────┬────────────────┬────────────────┐
│   opcode (1)   │  key_len (2)   │ nonce_len (1)  │  data_len (2)  │
├────────────────┴────────────────┴────────────────┴────────────────┤
│                          key bytes                                │
├───────────────────────────────────────────────────────────────────┤
│                         nonce bytes                               │
├───────────────────────────────────────────────────────────────────┤
│                         data bytes                                │
└───────────────────────────────────────────────────────────────────┘

Response format:

┌────────────────┬────────────────┬─────────────────────────────────┐
│   status (1)   │  reserved (1)  │          data_len (2)           │
├────────────────┴────────────────┴─────────────────────────────────┤
│                         result bytes                              │
└───────────────────────────────────────────────────────────────────┘

Build

# Build for AST1060 QEMU target
bazel build --config=k_qemu_ast1060 //target/ast1060/crypto:crypto

# Run tests in QEMU
bazel test --config=k_qemu_ast1060 //target/ast1060/crypto:crypto_test --test_output=all

# Build for AST1060-EVB hardware target
bazel build --config=k_ast1060_evb //target/ast1060-evb/crypto:crypto

Binary Sizes

ComponentFlashRAMStack
Client5.7 KB16 KB4 KB
Server42.7 KB48 KB8 KB
Kernel19.0 KB128 KB

System image: 299 KB (.bin), 579 KB (.elf)

Directory Structure

services/crypto/
├── api/          # Wire protocol, CryptoOp enum, backend traits
├── client/       # CryptoClient API for applications
├── server/       # RustCrypto-backed server implementation
└── tests/        # Functional tests (runs in QEMU)

License

Apache-2.0