Keyboard shortcuts

Press ← or → to navigate between chapters

Press ? to show this help

Press Esc to hide this help

SDK を使う

Rust の quantaegis-sdk と Python の quantaegis は、本文の暗号化、署名、方式の制限、鍵の更新をアプリケーションから扱うための API を提供します。新旧の鍵や方式が共存する期間を設けたいときに使えます。

やりたいこと使う API
受信者向けのメッセージや保存レコードを保護し、注文 ID などに結び付けるEnvelope.seal と Envelope.open。本文の暗号化と追加認証データ(AAD)の照合をまとめて扱えます
既存データを読みながら新しい方式や鍵に切り替えるEnvelope.open_with_keyring と SecurityPolicy。新旧の鍵で復号できる期間と、古い方式を拒否する時点を決められます
配布物の署名鍵を更新し、更新前後の署名を検証するAgileSignature(Python)、AgileSigningKey と AgileVerifyingKey(Rust)
既存のツールから公開鍵を受け取る対象方式の SPKI コーデックで読み込み、SDK の鍵型に変換します
既存のプロトコルが定める形式で鍵カプセルや署名を扱うプロトコルの実装と、各方式の生の鍵・署名を扱う API を使います

高水準のエンベロープと鍵リングの API は Rust と Python で使えます。C ABI と JS/WASM で使える個別方式や、標準鍵形式の対応範囲は言語別の対応表を参照してください。

基本操作

暗号化では、受信者が生成した公開鍵を送信者が信頼できる経路で受け取ります。送信者は seal に公開鍵、本文、必要に応じて AAD を渡し、受信者は自分の秘密鍵と同じ AAD で open します。たとえば別の注文 ID を指定すると、受信側は本文を受け付けません。AAD はアプリケーション側で管理します。暗号化の基本例

例の Envelope が作るエンベロープは QuantAegis の QAEN v1 形式です。送受信側がこの形式を扱える構成で使います。inspect は暗号文ヘッダから方式を取り出すための API です。受信したヘッダの値は振り分けに使い、本文を利用するのは認証付き復号が成功した後にします。エンベロープの構成 · Python の例外とポリシー

保存済みの暗号文を X-Wing に切り替える

ML-KEM-768 で暗号化済みの注文レコードを保存しているサービスを想定します。新規書き込みと読み込みに別々のポリシーを渡すと、保存済みデータを読み続けながら新規書き込みを先に X-Wing へ切り替えられます。

  1. 先に読み込み側へ新旧の秘密鍵を配備します。移行中は、ML-KEM-768 と X-Wing の両方を許可する CRYPTREC_CATEGORY3_PLUS で読み込みます。
  2. 新規書き込みには X-Wing の公開鍵と HYBRID_REQUIRED を指定します。
  3. 保存済みレコードを元の AAD で認証付き復号し、X-Wing で再暗号化します。置換前に新しい暗号文を読み戻せることを確認します。
  4. 移行対象と必要なバックアップの扱いを確認してから、読み込み側も HYBRID_REQUIRED に切り替えます。旧鍵が鍵リングに残っていても、元の ML-KEM エンベロープは PolicyViolationError で拒否されます。

Python 環境を準備し、リポジトリのルートで実行します。例はレコードをメモリ上で作ります。実際の保管先への書き込みと置換はアプリケーションが担当します。

python docs/examples/migrate_envelopes.py
from quantaegis import Envelope, PolicyViolationError, SecurityPolicy

transition = SecurityPolicy.CRYPTREC_CATEGORY3_PLUS
hybrid_only = SecurityPolicy.HYBRID_REQUIRED

# Stand in for an existing stored record and its recipient key.
old_public, old_secret = Envelope.generate_keypair("ml-kem-768", policy=transition)
old_aad = b"order=42"
old_packet = Envelope.seal(old_public, b"ship on Friday", aad=old_aad, policy=transition)

# Write new records with X-Wing. Keep both keys available for reads.
new_public, new_secret = Envelope.generate_keypair("x-wing", policy=hybrid_only)
keyring = [new_secret, old_secret]
new_aad = b"order=43"
new_packet = Envelope.seal(
    new_public, b"ship on Monday", aad=new_aad, policy=hybrid_only
)

old_plaintext = Envelope.open_with_keyring(
    keyring, old_packet, aad=old_aad, policy=transition
)
assert old_plaintext == b"ship on Friday"
assert Envelope.open_with_keyring(
    keyring, new_packet, aad=new_aad, policy=transition
) == b"ship on Monday"
print("Read both records during migration")

# Re-encrypt the authenticated old record and check the replacement before saving it.
replacement = Envelope.seal(new_public, old_plaintext, aad=old_aad, policy=hybrid_only)
assert Envelope.open(
    new_secret, replacement, aad=old_aad, policy=hybrid_only
) == old_plaintext
print("Re-encrypted the old record with X-Wing")

try:
    Envelope.open_with_keyring(keyring, old_packet, aad=old_aad, policy=hybrid_only)
except PolicyViolationError:
    print("Rejected the original ML-KEM envelope after cutover")
else:
    raise AssertionError("Expected the hybrid-only policy to reject the old envelope")

Rust で同じ移行を試す

Rust では AgileKeyPair と SecurityPolicy を使い、結果を quantaegis_core::Error で判定します。

cargo run --locked -p quantaegis-sdk --example envelope_migration
//! Migrate an existing ML-KEM envelope to X-Wing and enforce the new policy.

use quantaegis_core::{Error, Result};
use quantaegis_sdk::{AeadAlgorithm, AgileKeyPair, Envelope, KemAlgorithm, SecurityPolicy};

fn main() -> Result<()> {
    let transition = SecurityPolicy::CryptrecCategory3Plus;
    let hybrid_only = SecurityPolicy::HybridRequired;
    let old = AgileKeyPair::generate(transition, KemAlgorithm::MlKem768)?;
    let new = AgileKeyPair::generate(hybrid_only, KemAlgorithm::XWing)?;

    let aad = b"order=42";
    let old_packet = Envelope::seal(
        transition,
        &old.public,
        AeadAlgorithm::Aes256Gcm,
        b"ship on Friday",
        aad,
    )?;

    let keyring = [&new.secret, &old.secret];
    let plaintext = Envelope::open_with_keyring(transition, &keyring, &old_packet, aad)?;
    assert_eq!(plaintext, b"ship on Friday");
    let replacement = Envelope::seal(
        hybrid_only,
        &new.public,
        AeadAlgorithm::Aes256Gcm,
        &plaintext,
        aad,
    )?;

    assert_eq!(
        Envelope::open_with_keyring(hybrid_only, &keyring, &replacement, aad)?,
        plaintext
    );
    assert_eq!(
        Envelope::open_with_keyring(hybrid_only, &keyring, &old_packet, aad),
        Err(Error::PolicyViolation)
    );
    println!("Opened the X-Wing replacement and rejected the old envelope");
    Ok(())
}

配布物の署名鍵を更新する

配布元が ML-DSA-65 から MLDSA65-Ed25519-SHA512 に切り替える例です。新方式で署名した配布物を出す前に、利用者へ信頼できる経路で新しい検証鍵を配ります。移行中の利用者は新旧の検証鍵を保持し、同じ ctx を指定して両方の署名を検証します。署名対象が実ファイルなら、配布元と利用者は同じファイルのバイト列を使います。

python docs/examples/rotate_signing_keys.py
from quantaegis import AgileSignature, PolicyViolationError, SecurityPolicy

transition = SecurityPolicy.CRYPTREC_CATEGORY3_PLUS
hybrid_only = SecurityPolicy.HYBRID_REQUIRED
context = b"release-manifest-v1"

old_verify, old_sign = AgileSignature.generate_keypair("ml-dsa-65", policy=transition)
old_manifest = b"release=1.0"
old_signature = AgileSignature.sign(
    old_sign, old_manifest, ctx=context, policy=transition
)

new_verify, new_sign = AgileSignature.generate_keypair(
    "mldsa65-ed25519-sha512", policy=hybrid_only
)
new_manifest = b"release=2.0"
new_signature = AgileSignature.sign(
    new_sign, new_manifest, ctx=context, policy=hybrid_only
)

# Distribute the updated set of trusted verification keys before the cutover.
trusted_keys = [new_verify, old_verify]
for manifest, signature in (
    (old_manifest, old_signature),
    (new_manifest, new_signature),
):
    algorithm = AgileSignature.verify_with_keyring(
        trusted_keys, manifest, signature, ctx=context, policy=transition
    )
    print(f"Verified with {algorithm}")

AgileSignature.verify_with_keyring(
    trusted_keys, new_manifest, new_signature, ctx=context, policy=hybrid_only
)
try:
    AgileSignature.verify_with_keyring(
        trusted_keys, old_manifest, old_signature, ctx=context, policy=hybrid_only
    )
except PolicyViolationError:
    print("Rejected the earlier ML-DSA-only signature after cutover")
else:
    raise AssertionError("Expected the hybrid-only policy to reject the old signature")

HYBRID_REQUIRED が制限するのは方式です。同じ方式で旧鍵の受け付けを終了する場合は、信頼する鍵リングからその鍵を外します。過去の配布物を引き続き検証する運用では、その配布物用の検証鍵とポリシーを維持してください。この例の AgileSignature.sign は方式識別子付きの QuantAegis 署名を返します。生の ML-DSA 署名が必要な場合は、個別 API の署名の例を使えます。Rust の署名 API

既存の公開鍵を SDK に取り込む

受信者の ML-KEM 公開鍵を SPKI PEM で受け取った場合は、まずコーデックで方式名と生の公開鍵を取り出します。次に AgileKem.public_from_raw で Envelope.seal が受け取る鍵形式に変換します。例では受信者が手元でサンプル鍵を作り、送信者に渡す PEM を用意します。

python docs/examples/import_recipient_key.py
from quantaegis import (
    AgileKem,
    Envelope,
    MlKem768,
    ml_kem_public_from_spki_pem,
    ml_kem_public_to_spki_pem,
)

# The recipient exports a raw public key for distribution as SPKI PEM.
raw_public, raw_secret = MlKem768.keygen()
recipient_pem = ml_kem_public_to_spki_pem("ml-kem-768", raw_public)

# The sender loads a trusted copy of the PEM and adapts the key for Envelope.
algorithm, raw_public = ml_kem_public_from_spki_pem(recipient_pem)
recipient = AgileKem.public_from_raw(algorithm, raw_public)
packet = Envelope.seal(recipient, b"ship on Friday", aad=b"order=42")

# The recipient adapts the private key locally and recovers the message.
secret = AgileKem.secret_from_raw("ml-kem-768", raw_secret)
assert Envelope.open(secret, packet, aad=b"order=42") == b"ship on Friday"
print("Opened an envelope addressed to an imported SPKI key")

この例は公開鍵を交換する手順です。秘密鍵の移行や、ML-DSA・SLH-DSA のコーデックを選ぶときは、対応方式と鍵形式の注意点を確認してください。

この版は 1.0 未満の評価版です。公開 API の成熟度、独立監査、検証の範囲はセキュリティ方針で確認できます。