|
1 | 1 | using System;
|
2 | 2 | using System.IO;
|
3 | 3 | using System.Security.Cryptography;
|
| 4 | +using Google.Crypto.Tink; |
| 5 | +using Google.Protobuf; |
4 | 6 | using Miscreant;
|
5 | 7 |
|
6 | 8 | namespace Confluent.SchemaRegistry.Encryption
|
@@ -40,32 +42,58 @@ public int KeySize()
|
40 | 42 |
|
41 | 43 | public byte[] GenerateKey()
|
42 | 44 | {
|
43 |
| - return Aead.GenerateNonce(KeySize()); |
| 45 | + byte[] rawKey = Aead.GenerateNonce(KeySize()); |
| 46 | + switch (DekFormat) |
| 47 | + { |
| 48 | + case DekFormat.AES256_SIV: |
| 49 | + AesSivKey aesSiv = new AesSivKey(); |
| 50 | + aesSiv.Version = 0; |
| 51 | + aesSiv.KeyValue = ByteString.CopyFrom(rawKey); |
| 52 | + return aesSiv.ToByteArray(); |
| 53 | + case DekFormat.AES128_GCM: |
| 54 | + case DekFormat.AES256_GCM: |
| 55 | + AesGcmKey aesGcm = new AesGcmKey(); |
| 56 | + aesGcm.Version = 0; |
| 57 | + aesGcm.KeyValue = ByteString.CopyFrom(rawKey); |
| 58 | + return aesGcm.ToByteArray(); |
| 59 | + default: |
| 60 | + throw new ArgumentException(); |
| 61 | + } |
44 | 62 | }
|
45 | 63 |
|
46 | 64 | public byte[] Encrypt(byte[] key, byte[] plaintext)
|
47 | 65 | {
|
| 66 | + byte[] rawKey; |
48 | 67 | switch (DekFormat)
|
49 | 68 | {
|
50 | 69 | case DekFormat.AES256_SIV:
|
51 |
| - return EncryptWithAesSiv(key, plaintext); |
| 70 | + AesSivKey aesSiv = AesSivKey.Parser.ParseFrom(key); |
| 71 | + rawKey = aesSiv.KeyValue.ToByteArray(); |
| 72 | + return EncryptWithAesSiv(rawKey, plaintext); |
52 | 73 | case DekFormat.AES128_GCM:
|
53 | 74 | case DekFormat.AES256_GCM:
|
54 |
| - return EncryptWithAesGcm(key, plaintext); |
| 75 | + AesGcmKey aesGcm = AesGcmKey.Parser.ParseFrom(key); |
| 76 | + rawKey = aesGcm.KeyValue.ToByteArray(); |
| 77 | + return EncryptWithAesGcm(rawKey, plaintext); |
55 | 78 | default:
|
56 | 79 | throw new ArgumentException();
|
57 | 80 | }
|
58 | 81 | }
|
59 | 82 |
|
60 | 83 | public byte[] Decrypt(byte[] key, byte[] ciphertext)
|
61 | 84 | {
|
| 85 | + byte[] rawKey; |
62 | 86 | switch (DekFormat)
|
63 | 87 | {
|
64 | 88 | case DekFormat.AES256_SIV:
|
65 |
| - return DecryptWithAesSiv(key, ciphertext); |
| 89 | + AesSivKey aesSiv = AesSivKey.Parser.ParseFrom(key); |
| 90 | + rawKey = aesSiv.KeyValue.ToByteArray(); |
| 91 | + return DecryptWithAesSiv(rawKey, ciphertext); |
66 | 92 | case DekFormat.AES128_GCM:
|
67 | 93 | case DekFormat.AES256_GCM:
|
68 |
| - return DecryptWithAesGcm(key, ciphertext); |
| 94 | + AesGcmKey aesGcm = AesGcmKey.Parser.ParseFrom(key); |
| 95 | + rawKey = aesGcm.KeyValue.ToByteArray(); |
| 96 | + return DecryptWithAesGcm(rawKey, ciphertext); |
69 | 97 | default:
|
70 | 98 | throw new ArgumentException();
|
71 | 99 | }
|
|
0 commit comments