Skip to content

Commit e7ffcd8

Browse files
authored
Use Google Tink format for DEKs (confluentinc#3) (confluentinc#2277)
* Use Google Tink format for DEKs * Fix test
1 parent 2cbbaaa commit e7ffcd8

File tree

7 files changed

+1162
-6
lines changed

7 files changed

+1162
-6
lines changed

src/Confluent.SchemaRegistry.Encryption/Confluent.SchemaRegistry.Encryption.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
</ItemGroup>
2929

3030
<ItemGroup>
31+
<PackageReference Include="Google.Protobuf" Version="3.26.1" />
3132
<PackageReference Include="HKDF.Standard" Version="2.0.0" />
3233
<PackageReference Include="Miscreant" Version="0.3.3" />
3334
</ItemGroup>

src/Confluent.SchemaRegistry.Encryption/Cryptor.cs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.IO;
33
using System.Security.Cryptography;
4+
using Google.Crypto.Tink;
5+
using Google.Protobuf;
46
using Miscreant;
57

68
namespace Confluent.SchemaRegistry.Encryption
@@ -40,32 +42,58 @@ public int KeySize()
4042

4143
public byte[] GenerateKey()
4244
{
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+
}
4462
}
4563

4664
public byte[] Encrypt(byte[] key, byte[] plaintext)
4765
{
66+
byte[] rawKey;
4867
switch (DekFormat)
4968
{
5069
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);
5273
case DekFormat.AES128_GCM:
5374
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);
5578
default:
5679
throw new ArgumentException();
5780
}
5881
}
5982

6083
public byte[] Decrypt(byte[] key, byte[] ciphertext)
6184
{
85+
byte[] rawKey;
6286
switch (DekFormat)
6387
{
6488
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);
6692
case DekFormat.AES128_GCM:
6793
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);
6997
default:
7098
throw new ArgumentException();
7199
}

src/Confluent.SchemaRegistry.Encryption/LocalKmsClient.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.Security.Cryptography;
44
using System.Text;
55
using System.Threading.Tasks;
6+
using Google.Crypto.Tink;
7+
using Google.Protobuf;
68

79
namespace Confluent.SchemaRegistry.Encryption
810
{
@@ -20,7 +22,12 @@ public LocalKmsClient(string secret)
2022
}
2123
Secret = secret;
2224
cryptor = new Cryptor(DekFormat.AES256_GCM);
23-
key = Hkdf.DeriveKey(HashAlgorithmName.SHA256, Encoding.UTF8.GetBytes(secret), cryptor.KeySize());
25+
byte[] rawKey = Hkdf.DeriveKey(
26+
HashAlgorithmName.SHA256, Encoding.UTF8.GetBytes(secret), cryptor.KeySize());
27+
AesGcmKey aesGcm = new AesGcmKey();
28+
aesGcm.Version = 0;
29+
aesGcm.KeyValue = ByteString.CopyFrom(rawKey);
30+
key = aesGcm.ToByteArray();
2431
}
2532

2633
public bool DoesSupport(string uri)

0 commit comments

Comments
 (0)