MetaMUI Crypto Primitives - Unified API Specification

Side-channel claim limit. Nothing on this page claims constant-time behaviour or side-channel resistance for any target: the side-channel harness covers nothing (finding A48) and no measurement exists. The Rust and C cores are designed for constant time and unmeasured; managed runtimes (JIT, GC, bounds checks) make no timing claim. Where this page names constant-time or side-channel techniques, in prose or in code, they are design intent or recommendations, not measured properties.

Overview

This document defines the unified API specification for MetaMUI Crypto Primitives across all 10 supported platforms. The API design prioritizes consistency, security, and ease of use while respecting platform-specific idioms.

Supported Platforms

  1. TypeScript/JavaScript (NPM)
  2. Python (PyPI)
  3. Rust (Crates.io)
  4. Swift (SPM)
  5. Kotlin (Maven)
  6. WebAssembly (WASM)
  7. C (Native Library) - NEW
  8. C# (.NET) - NEW
  9. Go (Go Modules) - NEW
  10. Java (Maven) - NEW

Core API Principles

1. Consistent Naming

2. Security First

3. Error Handling

Algorithm Categories

Hash Functions

Symmetric Encryption

Asymmetric Cryptography

Post-Quantum Algorithms

Message Authentication

Key Derivation

Platform-Specific API Definitions

C API (libmetamui-crypto)

// Header: metamui_crypto.h

// Initialization and cleanup
int metamui_init(void);
void metamui_cleanup(void);

// Hash functions
typedef struct metamui_hash_ctx metamui_hash_ctx;

metamui_hash_ctx* metamui_hash_init(const char* algorithm);
int metamui_hash_update(metamui_hash_ctx* ctx, const uint8_t* data, size_t len);
int metamui_hash_final(metamui_hash_ctx* ctx, uint8_t* out, size_t* out_len);
void metamui_hash_free(metamui_hash_ctx* ctx);

// One-shot hashing
int metamui_hash(const char* algorithm, 
                 const uint8_t* data, size_t data_len,
                 uint8_t* out, size_t* out_len);

// Symmetric encryption
typedef struct metamui_cipher_ctx metamui_cipher_ctx;

metamui_cipher_ctx* metamui_cipher_init(const char* algorithm,
                                        const uint8_t* key, size_t key_len,
                                        const uint8_t* nonce, size_t nonce_len);
int metamui_cipher_encrypt(metamui_cipher_ctx* ctx,
                           const uint8_t* plaintext, size_t pt_len,
                           uint8_t* ciphertext, size_t* ct_len,
                           const uint8_t* aad, size_t aad_len);
int metamui_cipher_decrypt(metamui_cipher_ctx* ctx,
                           const uint8_t* ciphertext, size_t ct_len,
                           uint8_t* plaintext, size_t* pt_len,
                           const uint8_t* aad, size_t aad_len);
void metamui_cipher_free(metamui_cipher_ctx* ctx);

// Key generation
int metamui_keygen(const char* algorithm,
                   uint8_t* private_key, size_t* priv_len,
                   uint8_t* public_key, size_t* pub_len);

// Digital signatures
int metamui_sign(const char* algorithm,
                 const uint8_t* private_key, size_t priv_len,
                 const uint8_t* message, size_t msg_len,
                 uint8_t* signature, size_t* sig_len);

int metamui_verify(const char* algorithm,
                   const uint8_t* public_key, size_t pub_len,
                   const uint8_t* message, size_t msg_len,
                   const uint8_t* signature, size_t sig_len);

// Error handling
const char* metamui_error_string(int error_code);
int metamui_get_last_error(void);

// Memory utilities
void metamui_secure_zero(void* ptr, size_t len);

C# API (MetaMUI.Crypto)

namespace MetaMUI.Crypto
{
    // Hash functions
    public static class Hash
    {
        public static byte[] Compute(HashAlgorithm algorithm, byte[] data);
        public static byte[] Compute(HashAlgorithm algorithm, Stream stream);
        
        public static IHasher Create(HashAlgorithm algorithm);
    }
    
    public interface IHasher : IDisposable
    {
        void Update(byte[] data);
        void Update(byte[] data, int offset, int count);
        byte[] Final();
        void Reset();
    }
    
    // Symmetric encryption
    public static class Cipher
    {
        public static byte[] Encrypt(CipherAlgorithm algorithm, byte[] key, byte[] nonce, 
                                     byte[] plaintext, byte[] aad = null);
        public static byte[] Decrypt(CipherAlgorithm algorithm, byte[] key, byte[] nonce,
                                     byte[] ciphertext, byte[] aad = null);
        
        public static ICipher Create(CipherAlgorithm algorithm, byte[] key, byte[] nonce);
    }
    
    public interface ICipher : IDisposable
    {
        byte[] Encrypt(byte[] plaintext, byte[] aad = null);
        byte[] Decrypt(byte[] ciphertext, byte[] aad = null);
        void EncryptInPlace(byte[] buffer, int offset, int length, byte[] aad = null);
        void DecryptInPlace(byte[] buffer, int offset, int length, byte[] aad = null);
    }
    
    // Asymmetric cryptography
    public static class AsymmetricCrypto
    {
        public static KeyPair GenerateKeyPair(AsymmetricAlgorithm algorithm);
        
        public static byte[] Sign(AsymmetricAlgorithm algorithm, byte[] privateKey, byte[] message);
        public static bool Verify(AsymmetricAlgorithm algorithm, byte[] publicKey, 
                                  byte[] message, byte[] signature);
        
        public static byte[] Encrypt(AsymmetricAlgorithm algorithm, byte[] publicKey, byte[] plaintext);
        public static byte[] Decrypt(AsymmetricAlgorithm algorithm, byte[] privateKey, byte[] ciphertext);
    }
    
    public class KeyPair
    {
        public byte[] PrivateKey { get; }
        public byte[] PublicKey { get; }
        public AsymmetricAlgorithm Algorithm { get; }
        
        public void Dispose();
    }
    
    // Post-Quantum cryptography
    public static class PostQuantum
    {
        // KEM (Key Encapsulation Mechanism)
        public static class KEM
        {
            public static KEMKeyPair GenerateKeyPair(KEMAlgorithm algorithm);
            public static (byte[] ciphertext, byte[] sharedSecret) Encapsulate(
                KEMAlgorithm algorithm, byte[] publicKey);
            public static byte[] Decapsulate(KEMAlgorithm algorithm, byte[] privateKey, 
                                            byte[] ciphertext);
        }
        
        // Digital signatures
        public static class Signatures
        {
            public static PQCKeyPair GenerateKeyPair(PQCSignatureAlgorithm algorithm);
            public static byte[] Sign(PQCSignatureAlgorithm algorithm, byte[] privateKey, 
                                     byte[] message);
            public static bool Verify(PQCSignatureAlgorithm algorithm, byte[] publicKey,
                                     byte[] message, byte[] signature);
        }
    }
    
    // Key derivation
    public static class KeyDerivation
    {
        public static byte[] HKDF(HashAlgorithm hash, byte[] ikm, byte[] salt, 
                                  byte[] info, int length);
        public static byte[] PBKDF2(HashAlgorithm hash, byte[] password, byte[] salt,
                                   int iterations, int length);
        public static byte[] Argon2(Argon2Type type, byte[] password, byte[] salt,
                                   Argon2Parameters parameters);
    }
    
    // Secure memory
    public static class SecureMemory
    {
        public static void Zero(byte[] buffer);
        public static void Zero(byte[] buffer, int offset, int count);
        public static SecureBuffer Allocate(int size);
    }
    
    public sealed class SecureBuffer : IDisposable
    {
        public byte[] Data { get; }
        public int Length { get; }
        public void Dispose();
    }
}

Go API

package metamui

import (
    "errors"
    "io"
)

// Hash functions
type Hash interface {
    Write(p []byte) (n int, err error)
    Sum(b []byte) []byte
    Reset()
    Size() int
    BlockSize() int
}

func NewHash(algorithm HashAlgorithm) (Hash, error)
func ComputeHash(algorithm HashAlgorithm, data []byte) ([]byte, error)

// Symmetric encryption
type Cipher interface {
    Encrypt(plaintext, aad []byte) ([]byte, error)
    Decrypt(ciphertext, aad []byte) ([]byte, error)
    NonceSize() int
    KeySize() int
}

func NewCipher(algorithm CipherAlgorithm, key, nonce []byte) (Cipher, error)
func Encrypt(algorithm CipherAlgorithm, key, nonce, plaintext, aad []byte) ([]byte, error)
func Decrypt(algorithm CipherAlgorithm, key, nonce, ciphertext, aad []byte) ([]byte, error)

// Asymmetric cryptography
type KeyPair struct {
    PrivateKey []byte
    PublicKey  []byte
    Algorithm  AsymmetricAlgorithm
}

func GenerateKeyPair(algorithm AsymmetricAlgorithm) (*KeyPair, error)
func Sign(algorithm AsymmetricAlgorithm, privateKey, message []byte) ([]byte, error)
func Verify(algorithm AsymmetricAlgorithm, publicKey, message, signature []byte) (bool, error)

// Post-Quantum KEM
type KEMKeyPair struct {
    PrivateKey []byte
    PublicKey  []byte
    Algorithm  KEMAlgorithm
}

func GenerateKEMKeyPair(algorithm KEMAlgorithm) (*KEMKeyPair, error)
func Encapsulate(algorithm KEMAlgorithm, publicKey []byte) (ciphertext, sharedSecret []byte, err error)
func Decapsulate(algorithm KEMAlgorithm, privateKey, ciphertext []byte) ([]byte, error)

// Post-Quantum signatures
type PQCKeyPair struct {
    PrivateKey []byte
    PublicKey  []byte
    Algorithm  PQCSignatureAlgorithm
}

func GeneratePQCKeyPair(algorithm PQCSignatureAlgorithm) (*PQCKeyPair, error)
func SignPQC(algorithm PQCSignatureAlgorithm, privateKey, message []byte) ([]byte, error)
func VerifyPQC(algorithm PQCSignatureAlgorithm, publicKey, message, signature []byte) (bool, error)

// Key derivation
func HKDF(hash HashAlgorithm, ikm, salt, info []byte, length int) ([]byte, error)
func PBKDF2(hash HashAlgorithm, password, salt []byte, iterations, length int) ([]byte, error)
func Argon2(argonType Argon2Type, password, salt []byte, params *Argon2Parameters) ([]byte, error)

// MAC
type MAC interface {
    Write(p []byte) (n int, err error)
    Sum(b []byte) []byte
    Reset()
    Size() int
}

func NewMAC(algorithm MACAlgorithm, key []byte) (MAC, error)
func ComputeMAC(algorithm MACAlgorithm, key, message []byte) ([]byte, error)

// Secure memory
func SecureZero(b []byte)

// Algorithm enums
type HashAlgorithm int
type CipherAlgorithm int
type AsymmetricAlgorithm int
type KEMAlgorithm int
type PQCSignatureAlgorithm int
type MACAlgorithm int
type Argon2Type int

const (
    SHA256 HashAlgorithm = iota
    SHA512
    SHA3_256
    SHA3_512
    BLAKE2b
    BLAKE2s
    BLAKE3
    // ... other algorithms
)

// Error types
var (
    ErrInvalidAlgorithm = errors.New("invalid algorithm")
    ErrInvalidKey       = errors.New("invalid key")
    ErrInvalidNonce     = errors.New("invalid nonce")
    ErrDecryptionFailed = errors.New("decryption failed")
    ErrVerificationFailed = errors.New("verification failed")
)

Java API

package id.metamui.crypto;

import java.security.SecureRandom;
import java.util.Arrays;

// Core interfaces
public interface Disposable extends AutoCloseable {
    void dispose();
    @Override
    default void close() { dispose(); }
}

// Hash functions
public class Hash {
    public enum Algorithm {
        SHA256, SHA512, SHA3_256, SHA3_512,
        BLAKE2B, BLAKE2S, BLAKE3,
        ARGON2I, ARGON2D, ARGON2ID,
        SCRYPT, BCRYPT
    }
    
    public static byte[] compute(Algorithm algorithm, byte[] data) throws CryptoException;
    public static Hasher create(Algorithm algorithm) throws CryptoException;
    
    public interface Hasher extends Disposable {
        void update(byte[] data);
        void update(byte[] data, int offset, int length);
        byte[] finalizeHash();
        void reset();
    }
}

// Symmetric encryption
public class Cipher {
    public enum Algorithm {
        AES_128_GCM, AES_192_GCM, AES_256_GCM,
        AES_128_CTR, AES_192_CTR, AES_256_CTR,
        CHACHA20_POLY1305, XCHACHA20_POLY1305,
        CAMELLIA_128_GCM, CAMELLIA_192_GCM, CAMELLIA_256_GCM,
        ARIA_128_GCM, ARIA_192_GCM, ARIA_256_GCM,
        SERPENT_256_GCM, TWOFISH_256_GCM
    }
    
    public static byte[] encrypt(Algorithm algorithm, byte[] key, byte[] nonce,
                                 byte[] plaintext, byte[] aad) throws CryptoException;
    
    public static byte[] decrypt(Algorithm algorithm, byte[] key, byte[] nonce,
                                 byte[] ciphertext, byte[] aad) throws CryptoException;
    
    public static CipherInstance create(Algorithm algorithm, byte[] key, byte[] nonce) 
            throws CryptoException;
    
    public interface CipherInstance extends Disposable {
        byte[] encrypt(byte[] plaintext, byte[] aad) throws CryptoException;
        byte[] decrypt(byte[] ciphertext, byte[] aad) throws CryptoException;
        void encryptInPlace(byte[] buffer, int offset, int length, byte[] aad) 
                throws CryptoException;
        void decryptInPlace(byte[] buffer, int offset, int length, byte[] aad)
                throws CryptoException;
    }
}

// Asymmetric cryptography
public class AsymmetricCrypto {
    public enum Algorithm {
        ED25519, X25519, SECP256K1,
        P256, P384, P521,
        RSA2048, RSA3072, RSA4096
    }
    
    public static class KeyPair implements Disposable {
        private final byte[] privateKey;
        private final byte[] publicKey;
        private final Algorithm algorithm;
        
        public byte[] getPrivateKey() { return Arrays.copyOf(privateKey, privateKey.length); }
        public byte[] getPublicKey() { return Arrays.copyOf(publicKey, publicKey.length); }
        public Algorithm getAlgorithm() { return algorithm; }
        
        @Override
        public void dispose() {
            SecureMemory.zero(privateKey);
        }
    }
    
    public static KeyPair generateKeyPair(Algorithm algorithm) throws CryptoException;
    
    public static byte[] sign(Algorithm algorithm, byte[] privateKey, byte[] message)
            throws CryptoException;
    
    public static boolean verify(Algorithm algorithm, byte[] publicKey, 
                                 byte[] message, byte[] signature) throws CryptoException;
}

// Post-Quantum cryptography
public class PostQuantum {
    
    public static class KEM {
        public enum Algorithm {
            ML_KEM_768, KYBER_512, KYBER_768, KYBER_1024,
            NTRU, SABER, FRODOKEM
        }
        
        public static class KeyPair implements Disposable {
            private final byte[] privateKey;
            private final byte[] publicKey;
            private final Algorithm algorithm;
            
            // getters and dispose similar to AsymmetricCrypto.KeyPair
        }
        
        public static class EncapsulationResult {
            public final byte[] ciphertext;
            public final byte[] sharedSecret;
            
            public EncapsulationResult(byte[] ciphertext, byte[] sharedSecret) {
                this.ciphertext = ciphertext;
                this.sharedSecret = sharedSecret;
            }
        }
        
        public static KeyPair generateKeyPair(Algorithm algorithm) throws CryptoException;
        
        public static EncapsulationResult encapsulate(Algorithm algorithm, byte[] publicKey)
                throws CryptoException;
        
        public static byte[] decapsulate(Algorithm algorithm, byte[] privateKey, 
                                        byte[] ciphertext) throws CryptoException;
    }
    
    public static class Signatures {
        public enum Algorithm {
            ML_DSA_65, DILITHIUM_2, DILITHIUM_3, DILITHIUM_5,
            FALCON_512, SPHINCS_PLUS_256
        }
        
        public static class KeyPair implements Disposable {
            // Similar to KEM.KeyPair
        }
        
        public static KeyPair generateKeyPair(Algorithm algorithm) throws CryptoException;
        
        public static byte[] sign(Algorithm algorithm, byte[] privateKey, byte[] message)
                throws CryptoException;
        
        public static boolean verify(Algorithm algorithm, byte[] publicKey,
                                     byte[] message, byte[] signature) throws CryptoException;
    }
}

// Key derivation
public class KeyDerivation {
    
    public static byte[] hkdf(Hash.Algorithm hash, byte[] ikm, byte[] salt,
                              byte[] info, int length) throws CryptoException;
    
    public static byte[] pbkdf2(Hash.Algorithm hash, byte[] password, byte[] salt,
                                int iterations, int length) throws CryptoException;
    
    public enum Argon2Type {
        ARGON2I, ARGON2D, ARGON2ID
    }
    
    public static class Argon2Parameters {
        public final int memory;      // in KiB
        public final int iterations;
        public final int parallelism;
        public final int outputLength;
        
        public Argon2Parameters(int memory, int iterations, int parallelism, int outputLength) {
            this.memory = memory;
            this.iterations = iterations;
            this.parallelism = parallelism;
            this.outputLength = outputLength;
        }
    }
    
    public static byte[] argon2(Argon2Type type, byte[] password, byte[] salt,
                                Argon2Parameters params) throws CryptoException;
}

// MAC
public class MAC {
    public enum Algorithm {
        HMAC_SHA256, HMAC_SHA512, HMAC_BLAKE2B,
        POLY1305, GMAC
    }
    
    public static byte[] compute(Algorithm algorithm, byte[] key, byte[] message)
            throws CryptoException;
    
    public static MACInstance create(Algorithm algorithm, byte[] key) throws CryptoException;
    
    public interface MACInstance extends Disposable {
        void update(byte[] data);
        void update(byte[] data, int offset, int length);
        byte[] finalizeMAC();
        void reset();
    }
}

// Secure memory management
public class SecureMemory {
    public static void zero(byte[] buffer);
    public static void zero(byte[] buffer, int offset, int length);
    
    public static class SecureBuffer implements Disposable {
        private final byte[] data;
        
        public SecureBuffer(int size) {
            this.data = new byte[size];
        }
        
        public byte[] getData() { return data; }
        public int getLength() { return data.length; }
        
        @Override
        public void dispose() {
            zero(data);
        }
    }
}

// Exception handling
public class CryptoException extends Exception {
    public enum ErrorCode {
        INVALID_ALGORITHM,
        INVALID_KEY,
        INVALID_NONCE,
        INVALID_INPUT,
        DECRYPTION_FAILED,
        VERIFICATION_FAILED,
        INSUFFICIENT_MEMORY,
        OPERATION_FAILED
    }
    
    private final ErrorCode errorCode;
    
    public CryptoException(ErrorCode errorCode, String message) {
        super(message);
        this.errorCode = errorCode;
    }
    
    public CryptoException(ErrorCode errorCode, String message, Throwable cause) {
        super(message, cause);
        this.errorCode = errorCode;
    }
    
    public ErrorCode getErrorCode() { return errorCode; }
}

Cross-Platform Compatibility

Data Format Standards

Test Vector Compatibility

Version Management

Security Considerations

Memory Management

Error Handling

Platform Security Features

Performance Guidelines

Optimization Priorities

  1. Correctness and security
  2. API consistency
  3. Performance optimization
  4. Memory efficiency

Benchmarking

Documentation Requirements

API Documentation

Platform-Specific Guides

Licensing and Distribution

Package Distribution

License Enforcement

Implementation Timeline

Phase 1: Foundation (Months 1-3)

Phase 2: Expansion (Months 4-6)

Phase 3: Completion (Months 7-9)

Phase 4: Polish (Months 10-12)

Conclusion

This unified API specification describes consistent cryptographic operations across all supported platforms while maintaining platform-specific idioms and best practices.