FIPS 140-2 加密政策¶
FIPS 140-2(Federal Information Processing Standard Publication 140-2)是美國聯邦政府對密碼模組的安全標準,Level 1–4 涵蓋從軟體到硬體 HSM 的不同保障等級。政府機構、金融監管機構與部分醫療系統要求所有密碼操作使用 FIPS 核准的演算法與模組。
NextPDF Enterprise 的 FIPS 模組在 PHP 層強制執行密碼政策,確保任何 NextPDF 操作均不使用非 FIPS 演算法。
FIPS 核准演算法清單¶
允許¶
| 類型 | 核准演算法 |
|---|---|
| 對稱加密 | AES-128-GCM, AES-256-GCM, AES-128-CBC, AES-256-CBC |
| 非對稱簽章 | RSA-PSS (≥2048), RSA-PKCS1v15 (≥2048), ECDSA P-256/P-384/P-521 |
| 金鑰交換 | ECDH P-256/P-384/P-521, DH (≥2048 bits) |
| 雜湊 | SHA-256, SHA-384, SHA-512, SHA3-256, SHA3-384, SHA3-512 |
| HMAC | HMAC-SHA-256, HMAC-SHA-384, HMAC-SHA-512 |
| KDF | PBKDF2-HMAC-SHA-256 (≥100,000 iterations), HKDF-SHA-256 |
明確禁止¶
| 類型 | 禁止演算法 | 原因 |
|---|---|---|
| 雜湊 | MD5, SHA-1 | 已知碰撞攻擊 |
| 對稱 | RC4, DES, 3DES | 已棄用,不安全 |
| 非對稱 | RSA < 2048 bits | 金鑰強度不足 |
| 亂數 | /dev/urandom 直接用於金鑰 | 需 DRBG 機制 |
核心 API¶
FipsPolicy¶
use NextPDF\Enterprise\Security\Fips\FipsPolicy;
use NextPDF\Enterprise\Security\Fips\FipsLevel;
// FIPS 140-2 Level 1(軟體模式,最常見)
$policy = FipsPolicy::level1();
// 或帶有自定義配置
$policy = FipsPolicy::create(FipsLevel::Level1)
->requireOpenSslFipsProvider() // 要求 OpenSSL 3.x FIPS provider
->prohibitNonFipsAlgorithms() // 嘗試使用非 FIPS 演算法時拋出例外
->enableCryptoAuditLog($auditLogger) // 記錄每次密碼操作
->enforceKeyMinimumLength(
symmetric: 256, // AES-256
asymmetric: 4096, // RSA-4096
);
PHP Compatibility
This example uses PHP 8.5 syntax. If your environment runs PHP 8.1 or 7.4, use NextPDF Backport for a backward-compatible build.
FipsModeGuard¶
模式守衛確保 NextPDF 在 FIPS 環境啟動前驗證密碼模組合規性:
use NextPDF\Enterprise\Security\Fips\FipsModeGuard;
// 在應用程式啟動時執行
$guard = new FipsModeGuard($policy);
$status = $guard->verify();
if (!$status->isCompliant()) {
foreach ($status->violations() as $violation) {
// 每個違規項目包含修正建議
throw new FipsComplianceException(
'FIPS mode guard failed: ' . $violation->message() .
' Fix: ' . $violation->remediation()
);
}
}
// 成功後封印——後續任何非 FIPS 操作均拋出例外
$guard->seal();
FipsCryptoAuditLogger¶
記錄所有密碼操作,供合規稽核使用:
use NextPDF\Enterprise\Security\Fips\FipsCryptoAuditLogger;
use NextPDF\Enterprise\Security\Fips\CryptoOperation;
$auditLogger = new FipsCryptoAuditLogger(
storageAdapter: $immutableLogAdapter,
logLevel: AuditLogLevel::AllOperations, // 或 ViolationsOnly
);
// 自動由 FipsPolicy 注入到所有密碼操作
// 也可手動記錄自定義操作
$auditLogger->record(
CryptoOperation::create(
operationType: 'SIGN',
algorithm: 'ECDSA-P384',
keyId: $key->id(),
documentId: $doc->id(),
timestamp: new DateTimeImmutable(),
outcome: CryptoOutcome::Success,
)
);
OpenSSL FIPS Provider 配置¶
NextPDF Enterprise 的 FIPS 模式依賴 OpenSSL 3.x 的 FIPS provider:
# 確認 OpenSSL 版本(需 3.0+)
openssl version
# 驗證 FIPS provider 可用
openssl list -providers | grep fips
# openssl.cnf 配置(通常位於 /etc/ssl/openssl.cnf)
# openssl.cnf
[openssl_init]
providers = provider_sect
[provider_sect]
fips = fips_sect
default = default_sect
[fips_sect]
activate = 1
[default_sect]
activate = 1
use NextPDF\Enterprise\Security\Fips\FipsModeGuard;
// NextPDF 會自動偵測 OpenSSL FIPS provider 狀態
$guard = new FipsModeGuard(FipsPolicy::level1());
$guard->verify(); // 若 FIPS provider 未啟動則拋出 FipsProviderException
HSM 整合(Level 3)¶
use NextPDF\Enterprise\Security\Fips\HsmKeyProvider;
use NextPDF\Enterprise\Security\Fips\Pkcs11Adapter;
// FIPS 140-2 Level 3 需要使用 HSM 進行金鑰操作
$hsm = new HsmKeyProvider(
adapter: new Pkcs11Adapter(
libraryPath: '/usr/lib/pkcs11/libsofthsm2.so', // 或 Thales / nCipher
slot: 0,
pin: $hsmPin,
),
policy: FipsPolicy::level3(),
);
$signingKey = $hsm->getSigningKey(keyId: 'signing-key-2025');
效能影響¶
| 操作 | 標準模式 | FIPS 模式 | 開銷 |
|---|---|---|---|
| AES-256-GCM 加密(1 MB) | |||
| RSA-4096 簽章 | |||
| ECDSA P-384 簽章 |