tourze/quic-crypto 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tourze/quic-crypto

Composer 安装命令:

composer require tourze/quic-crypto

包简介

QUIC协议加密库 - AEAD加密、包头保护、密钥派生

README 文档

README

English | 中文

Latest Version PHP Version License Build Status Code Coverage

A comprehensive QUIC protocol cryptographic library providing AEAD encryption, header protection, key derivation, and other core security features.

Table of Contents

Features

  • AEAD Encryption: Support for AES-128-GCM, AES-256-GCM, ChaCha20-Poly1305
  • Header Protection: QUIC packet header protection implementation
  • Key Derivation: HKDF-based key derivation following RFC 5869
  • Key Management: Complete QUIC key lifecycle management
  • Crypto Suites: Standard QUIC cipher suite management and selection
  • Secure Random: Cryptographically secure random number generation
  • RFC Compliance: Strict adherence to RFC 9001 (QUIC-TLS) and related standards

Installation

composer require tourze/quic-crypto

Requirements

  • PHP 8.1 or higher
  • OpenSSL extension (required)
  • Sodium extension (optional, for enhanced security)
  • Hash extension (required)

Quick Start

Get started with the QUIC Crypto Library in minutes:

<?php
require_once 'vendor/autoload.php';

use Tourze\QUIC\Crypto\AES128GCM;
use Tourze\QUIC\Crypto\KeyDerivation;
use Tourze\QUIC\Crypto\SecureRandom;

// 1. 生成安全的加密密钥
$key = SecureRandom::generateAESKey(128);

// 2. 创建 AEAD 加密实例
$aes = new AES128GCM($key);

// 3. 加密数据
$plaintext = 'Hello, QUIC!';
$nonce = SecureRandom::generateNonce(12);
$aad = 'additional authenticated data';
$ciphertext = $aes->encrypt($plaintext, $nonce, $aad);

// 4. Decrypt data
$decrypted = $aes->decrypt($ciphertext, $nonce, $aad);
echo $decrypted; // Output: Hello, QUIC!

// 5. Key derivation example
$secret = SecureRandom::generate(32);
$derivedKey = KeyDerivation::quicKDF($secret, 'quic key', 16);

Basic Usage

AEAD Encryption

use Tourze\QUIC\Crypto\AES128GCM;

// Create AES-128-GCM instance
$key = random_bytes(16);
$aes = new AES128GCM($key);

// Encrypt data
$plaintext = 'Hello, QUIC!';
$nonce = random_bytes(12);
$aad = 'additional data';
$ciphertext = $aes->encrypt($plaintext, $nonce, $aad);

// Decrypt data
$decrypted = $aes->decrypt($ciphertext, $nonce, $aad);

Key Derivation

use Tourze\QUIC\Crypto\KeyDerivation;

// HKDF key derivation
$ikm = 'input key material';
$salt = 'optional salt';
$info = 'context info';
$length = 32;

$derivedKey = KeyDerivation::hkdf($ikm, $length, $info, $salt);

// QUIC-specific key derivation
$secret = random_bytes(32);
$label = 'quic key';
$quicKey = KeyDerivation::quicKDF($secret, $label, 16);

Header Protection

use Tourze\QUIC\Crypto\HeaderProtection;
use Tourze\QUIC\Crypto\AES128GCM;

$aesKey = random_bytes(16);
$hpKey = random_bytes(16);
$aes = new AES128GCM($aesKey);

$hp = new HeaderProtection($aes, $hpKey);

// Protect header
$header = "\x40\x01\x02\x03\x04";
$sample = random_bytes(16);
$protectedHeader = $hp->protect($header, $sample);

// Unprotect header
$originalHeader = $hp->unprotect($protectedHeader, $sample);

Crypto Suite Management

use Tourze\QUIC\Crypto\CryptoSuite;

// Create crypto suite
$key = random_bytes(16);
$suite = CryptoSuite::createAES128GCM($key);

// Get suite information
echo $suite->getName(); // TLS_AES_128_GCM_SHA256
echo $suite->getKeyLength(); // 16
echo $suite->getHashAlgorithm(); // sha256

// Check supported suites
$supported = CryptoSuite::getSupportedSuites();
$best = CryptoSuite::selectBestSuite();

Key Manager

use Tourze\QUIC\Crypto\KeyManager;
use Tourze\QUIC\Crypto\CryptoSuite;

$suite = CryptoSuite::createAES128GCM(random_bytes(16));
$keyManager = new KeyManager($suite);

// Derive initial secrets
$connectionId = random_bytes(8);
$secrets = $keyManager->deriveInitialSecrets($connectionId);

// Get current keys
$writeKey = $keyManager->getWriteKey(true); // Client key
$writeIv = $keyManager->getWriteIv(true);
$hpKey = $keyManager->getHpKey(true);

// Construct nonce
$packetNumber = 12345;
$nonce = $keyManager->constructNonce($writeIv, $packetNumber);

Secure Random

use Tourze\QUIC\Crypto\SecureRandom;

// Generate random bytes
$randomBytes = SecureRandom::generate(32);

// Generate nonce
$nonce = SecureRandom::generateNonce(12);

// Generate keys
$aesKey = SecureRandom::generateAESKey(128); // AES-128
$chachaKey = SecureRandom::generateChaCha20Key(); // ChaCha20

// Generate connection ID
$connectionId = SecureRandom::generateConnectionId(8);

// Generate UUID
$uuid = SecureRandom::generateUuid();

// Timing-safe comparison
$equal = SecureRandom::timingSafeEquals($string1, $string2);

API Documentation

Exception Handling

All methods may throw CryptoException:

use Tourze\QUIC\Crypto\Exception\CryptoException;

try {
    $ciphertext = $aes->encrypt($plaintext, $nonce, $aad);
} catch (CryptoException $e) {
    echo 'Encryption failed: ' . $e->getMessage();
    echo 'Error code: ' . $e->getCode();
}

Supported Algorithms

  • AEAD: AES-128-GCM, AES-256-GCM, ChaCha20-Poly1305
  • Hash: SHA-256, SHA-384, SHA-512
  • Cipher Suites: TLS_AES_128_GCM_SHA256, TLS_AES_256_GCM_SHA384, TLS_CHACHA20_POLY1305_SHA256

Security Considerations

  1. Key Management: Clear sensitive keys immediately after use
  2. Random Numbers: Use cryptographically secure random number generators
  3. Timing Attacks: Timing-safe string comparison functions are provided
  4. Memory Safety: Automatic cleanup of sensitive data in memory
  5. Constant-Time Operations: Critical operations are designed to be constant-time

Testing

Run the test suite:

./vendor/bin/phpunit packages/quic-crypto/tests

Contributing

We welcome contributions! Please follow these guidelines:

  1. Issues: Report bugs or feature requests via GitHub Issues

  2. Pull Requests:

    • Fork the repository
    • Create a feature branch (git checkout -b feature/amazing-feature)
    • Commit your changes (git commit -m 'Add amazing feature')
    • Push to the branch (git push origin feature/amazing-feature)
    • Open a Pull Request
  3. Code Style: Follow PSR-12 coding standards

  4. Testing: Ensure all tests pass and add tests for new features

  5. Documentation: Update documentation for any API changes

References

License

The MIT License (MIT). Please see License File for more information.

tourze/quic-crypto 适用场景与选型建议

tourze/quic-crypto 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 46 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 06 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 tourze/quic-crypto 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tourze/quic-crypto 我们能提供哪些服务?
定制开发 / 二次开发

基于 tourze/quic-crypto 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 46
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-06-03