spomky-labs/php-aes-gcm
PHP library implementing AES-GCM (Galois/Counter Mode) authenticated encryption. Provides encrypt/decrypt with IV/nonce handling, auth tags, and AAD support for securing data with integrity. Useful for token payloads, messages, and secure storage.
passwords, tokens, or PII).config['app.cipher']) but offers GCM mode (unlike Laravel’s default CBC mode), which is faster and more secure for most use cases.encrypt()/decrypt() for scenarios needing nonce handling, tag verification, or performance optimization.sodium_compat for PHP < 7.2).env() or a KMS like AWS KMS). Poor key handling could negate security benefits.| Risk Area | Mitigation Strategy |
|---|---|
| Backward Compatibility | Test with Laravel’s existing encrypted data (if migrating from CBC). GCM is not compatible with CBC. |
| Performance Overhead | Benchmark against Laravel’s default encryption for high-throughput systems. |
| Key Rotation | Implement a key versioning system (e.g., encrypt old data with new keys). |
| Error Handling | Wrap library calls in try-catch blocks to handle CryptographicException. |
| Side-Channel Attacks | Use constant-time comparison for tags (library may not handle this by default). |
.env?)Crypt facade or create a custom AesGcm facade.encrypt(): Simpler but uses CBC (vulnerable to padding oracle attacks).| Step | Action | Tools/Examples |
|---|---|---|
| 1. Proof of Concept | Replace a single encrypted field (e.g., user->api_token) with GCM. |
Use SpomkyLabs\AesGcm\AesGcm directly in a service class. |
| 2. Facade/Service | Create a Laravel service class (e.g., app/Services/AesGcmEncryptor.php) with: |
encrypt($plaintext, $key, $nonce) and decrypt($ciphertext, $key, $nonce). |
| 3. Database Layer | Add encryption to Eloquent models (e.g., getAttribute(), setAttribute()). |
Use attributesToEncrypt trait or model observers. |
| 4. API Layer | Encrypt/decrypt API payloads (e.g., request/response bodies). | Middleware or Illuminate\Http\Resources. |
| 5. Key Management | Integrate with a KMS (e.g., AWS KMS via aws/aws-sdk-php). |
Use SpomkyLabs\AesGcm\KeyProvider interface. |
| 6. Deprecation | Phase out old CBC-encrypted data (if applicable). | Write a migration to re-encrypt data. |
php-compat polyfills.10,000 encrypt/decrypt ops).schedule:run) or external KMS triggers.InvalidTag) are harder to debug than CBC. Log:
| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Key Loss/Corruption | Permanent data loss. | Use key backup (e.g., AWS KMS) and multi-key redundancy. |
| Nonce Reuse | Security compromise. | Enforce unique nonces (e.g., UUIDv4 or counter + IV). |
| Tag Tampering | Data integrity breach. | Validate tags on every decryption (library handles this by default). |
| PHP/OpenSSL Misconfiguration | Decryption failures. | Monitor openssl_error_string() and set OPENSSL_IAE (invalid AES key). |
| Library Bug | Undefined behavior. | Pin version in composer.json and fork if critical. |
How can I help you explore Laravel packages today?