| | | 1 | | using System.Security.Cryptography; |
| | | 2 | | |
| | | 3 | | namespace LOCKnet.Core.Crypto; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// AES-256-GCM-Implementierung von <see cref="IEncryptionService"/>. |
| | | 7 | | /// |
| | | 8 | | /// Paket-Format: <c>[Nonce (12 Bytes)][Tag (16 Bytes)][Ciphertext (n Bytes)]</c> |
| | | 9 | | /// </summary> |
| | | 10 | | public sealed class AesGcmEncryptionService : IEncryptionService |
| | | 11 | | { |
| | | 12 | | private const int KeyBytes = 32; // AES-256 |
| | | 13 | | private const int NonceBytes = 12; // GCM-Standard-Nonce |
| | | 14 | | private const int TagBytes = 16; // GCM-Authentifizierungs-Tag |
| | | 15 | | private const int HeaderBytes = NonceBytes + TagBytes; // 28 Bytes Overhead |
| | | 16 | | |
| | | 17 | | /// <inheritdoc/> |
| | | 18 | | public byte[] Encrypt(byte[] plaintext, byte[] key) |
| | 81 | 19 | | => Encrypt(plaintext, key, []); |
| | | 20 | | |
| | | 21 | | /// <inheritdoc/> |
| | | 22 | | public byte[] Encrypt(byte[] plaintext, byte[] key, byte[] associatedData) |
| | 152 | 23 | | { |
| | 152 | 24 | | ArgumentNullException.ThrowIfNull(plaintext); |
| | 151 | 25 | | ArgumentNullException.ThrowIfNull(key); |
| | 151 | 26 | | ArgumentNullException.ThrowIfNull(associatedData); |
| | 151 | 27 | | if (key.Length != KeyBytes) |
| | 1 | 28 | | throw new ArgumentException($"Key muss genau {KeyBytes} Bytes lang sein.", nameof(key)); |
| | | 29 | | |
| | 150 | 30 | | var nonce = RandomNumberGenerator.GetBytes(NonceBytes); |
| | 150 | 31 | | var tag = new byte[TagBytes]; |
| | 150 | 32 | | var ciphertext = new byte[plaintext.Length]; |
| | | 33 | | |
| | 150 | 34 | | using var aes = new AesGcm(key, TagBytes); |
| | 150 | 35 | | aes.Encrypt(nonce, plaintext, ciphertext, tag, associatedData); |
| | | 36 | | |
| | | 37 | | // Paket zusammensetzen: Nonce | Tag | Ciphertext |
| | 150 | 38 | | var packet = new byte[HeaderBytes + ciphertext.Length]; |
| | 150 | 39 | | nonce.CopyTo(packet, 0); |
| | 150 | 40 | | tag.CopyTo(packet, NonceBytes); |
| | 150 | 41 | | ciphertext.CopyTo(packet, HeaderBytes); |
| | | 42 | | |
| | 150 | 43 | | return packet; |
| | 150 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <inheritdoc/> |
| | | 47 | | public byte[] Decrypt(byte[] cipherPacket, byte[] key) |
| | 54 | 48 | | => Decrypt(cipherPacket, key, []); |
| | | 49 | | |
| | | 50 | | /// <inheritdoc/> |
| | | 51 | | public byte[] Decrypt(byte[] cipherPacket, byte[] key, byte[] associatedData) |
| | 103 | 52 | | { |
| | 103 | 53 | | ArgumentNullException.ThrowIfNull(cipherPacket); |
| | 102 | 54 | | ArgumentNullException.ThrowIfNull(key); |
| | 102 | 55 | | ArgumentNullException.ThrowIfNull(associatedData); |
| | | 56 | | |
| | 102 | 57 | | if (key.Length != KeyBytes) |
| | 0 | 58 | | throw new ArgumentException($"Key muss genau {KeyBytes} Bytes lang sein.", nameof(key)); |
| | | 59 | | |
| | 102 | 60 | | if (cipherPacket.Length < HeaderBytes) |
| | 1 | 61 | | throw new ArgumentException( |
| | 1 | 62 | | $"Paket zu kurz — mindestens {HeaderBytes} Bytes erwartet.", nameof(cipherPacket)); |
| | | 63 | | |
| | | 64 | | // Paket zerlegen |
| | 101 | 65 | | var nonce = cipherPacket[..NonceBytes]; |
| | 101 | 66 | | var tag = cipherPacket[NonceBytes..HeaderBytes]; |
| | 101 | 67 | | var ciphertext = cipherPacket[HeaderBytes..]; |
| | | 68 | | |
| | 101 | 69 | | var plaintext = new byte[ciphertext.Length]; |
| | | 70 | | |
| | 101 | 71 | | using var aes = new AesGcm(key, TagBytes); |
| | 101 | 72 | | aes.Decrypt(nonce, ciphertext, tag, plaintext, associatedData); |
| | | 73 | | |
| | 88 | 74 | | return plaintext; |
| | 88 | 75 | | } |
| | | 76 | | } |