Skip to content

Commit 162b7bb

Browse files
committed
WIP
1 parent 5c067f4 commit 162b7bb

File tree

3 files changed

+86
-17
lines changed

3 files changed

+86
-17
lines changed

pkg/delegatedkeys/delegated_keys.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,53 @@ func GenerateDataKey(keyURI string) (*TinkDelegatedKey, []byte, error) {
158158

159159
return delegatedKey, wrappedKeyset, nil
160160
}
161+
162+
func GenerateSigningKey(keyURI string) (*TinkDelegatedKey, []byte, []byte, error) {
163+
kh, err := keyset.NewHandle(signature.ECDSAP256KeyTemplate())
164+
if err != nil {
165+
return nil, nil, nil, fmt.Errorf("failed to generate new keyset handle: %v", err)
166+
}
167+
168+
// Extract the public key
169+
publicKeysetHandle, err := kh.Public()
170+
if err != nil {
171+
return nil, nil, nil, fmt.Errorf("failed to extract public key: %v", err)
172+
}
173+
174+
var publicKeyBytes bytes.Buffer
175+
publicKeyWriter := keyset.NewBinaryWriter(&publicKeyBytes)
176+
if err := publicKeysetHandle.WriteWithNoSecrets(publicKeyWriter); err != nil {
177+
return nil, nil, nil, fmt.Errorf("failed to serialize public key: %v", err)
178+
}
179+
180+
delegatedKey := NewTinkDelegatedKey(kh, keyURI)
181+
wrappedKeyset, err := delegatedKey.WrapKeyset()
182+
if err != nil {
183+
return nil, nil, nil, fmt.Errorf("failed to wrap keyset: %v", err)
184+
}
185+
186+
return delegatedKey, wrappedKeyset, publicKeyBytes.Bytes(), nil
187+
}
188+
189+
func VerifySignature(publicKeyBytes, sig, data []byte) (bool, error) {
190+
// Load the public key into a keyset.Handle
191+
publicKeyReader := keyset.NewBinaryReader(bytes.NewReader(publicKeyBytes))
192+
publicKeyHandle, err := keyset.ReadWithNoSecrets(publicKeyReader)
193+
if err != nil {
194+
return false, fmt.Errorf("failed to load public key: %v", err)
195+
}
196+
197+
// Get a Verifier instance from the public key handle
198+
verifier, err := signature.NewVerifier(publicKeyHandle)
199+
if err != nil {
200+
return false, fmt.Errorf("failed to get verifier: %v", err)
201+
}
202+
203+
// Verify the signature
204+
err = verifier.Verify(sig, data)
205+
if err != nil {
206+
return false, nil
207+
}
208+
209+
return true, nil
210+
}

pkg/materials/materials.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package materials
22

3-
import "github.com/cloudopsy/dynamodb-encryption-go/pkg/delegatedkeys"
3+
import (
4+
"github.com/cloudopsy/dynamodb-encryption-go/pkg/delegatedkeys"
5+
)
46

57
// CryptographicMaterials defines a common interface for cryptographic materials.
68
type CryptographicMaterials interface {

pkg/provider/kms.go

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,36 +28,35 @@ func NewAwsKmsCryptographicMaterialsProvider(keyID string, encryptionContext map
2828
}, nil
2929
}
3030

31-
// GenerateDataKey generates a new data key using AWS KMS and wraps the Tink keyset.
32-
func (p *AwsKmsCryptographicMaterialsProvider) GenerateDataKey() (*delegatedkeys.TinkDelegatedKey, []byte, error) {
31+
// EncryptionMaterials retrieves and stores encryption materials for the given encryption context.
32+
func (p *AwsKmsCryptographicMaterialsProvider) EncryptionMaterials(ctx context.Context, materialName string) (materials.CryptographicMaterials, error) {
33+
// Generate a new Tink keyset and wrap it
3334
delegatedKey, wrappedKeyset, err := delegatedkeys.GenerateDataKey(p.KeyID)
3435
if err != nil {
35-
return nil, nil, fmt.Errorf("failed to generate data key: %v", err)
36+
return nil, fmt.Errorf("failed to generate and wrap data key: %v", err)
3637
}
3738

38-
return delegatedKey, wrappedKeyset, nil
39-
}
40-
41-
// DecryptDataKey unwraps the Tink keyset using AWS KMS.
42-
func (p *AwsKmsCryptographicMaterialsProvider) DecryptDataKey(encryptedKeyset []byte) (*delegatedkeys.TinkDelegatedKey, error) {
43-
return delegatedkeys.UnwrapKeyset(encryptedKeyset, p.KeyID)
44-
}
45-
46-
// EncryptionMaterials retrieves and stores encryption materials for the given encryption context.
47-
func (p *AwsKmsCryptographicMaterialsProvider) EncryptionMaterials(ctx context.Context, materialName string) (materials.CryptographicMaterials, error) {
48-
// Generate a new Tink keyset and wrap it
49-
delegatedKey, wrappedKeyset, err := p.GenerateDataKey()
39+
// Assume GenerateSigningKey is modified to return public key as well
40+
delegatedSigningKey, _, publicKeyBytes, err := delegatedkeys.GenerateSigningKey(p.KeyID)
5041
if err != nil {
5142
return nil, fmt.Errorf("failed to generate and wrap data key: %v", err)
5243
}
5344

45+
// Sign the wrappedKeyset
46+
signature, err := delegatedSigningKey.Sign(wrappedKeyset)
47+
if err != nil {
48+
return nil, fmt.Errorf("failed to sign wrappedKeyset: %v", err)
49+
}
50+
5451
// Prepare the material description with encryption context and wrapped keyset
5552
materialDescription := make(map[string]string)
5653
for key, value := range p.EncryptionContext {
5754
materialDescription[key] = value
5855
}
5956
materialDescription["ContentEncryptionAlgorithm"] = delegatedKey.Algorithm()
6057
materialDescription["WrappedKeyset"] = base64.StdEncoding.EncodeToString(wrappedKeyset)
58+
materialDescription["Signature"] = base64.StdEncoding.EncodeToString(signature)
59+
materialDescription["PublicKey"] = base64.StdEncoding.EncodeToString(publicKeyBytes)
6160

6261
// Create encryption materials with the material description and the encryption key
6362
encryptionMaterials := materials.NewEncryptionMaterials(materialDescription, delegatedKey, nil)
@@ -81,7 +80,25 @@ func (p *AwsKmsCryptographicMaterialsProvider) DecryptionMaterials(ctx context.C
8180
return nil, fmt.Errorf("failed to decode encrypted keyset: %v", err)
8281
}
8382

84-
delegatedKey, err := p.DecryptDataKey(encryptedKeyset)
83+
publicKeyBase64 := materialDescMap["PublicKey"]
84+
publicKeyBytes, err := base64.StdEncoding.DecodeString(publicKeyBase64)
85+
if err != nil {
86+
return nil, fmt.Errorf("failed to decode public key: %v", err)
87+
}
88+
89+
signatureBase64 := materialDescMap["Signature"]
90+
signatureBytes, err := base64.StdEncoding.DecodeString(signatureBase64)
91+
if err != nil {
92+
return nil, fmt.Errorf("failed to decode signature: %v", err)
93+
}
94+
95+
// Verify the wrapped keyset's signature
96+
valid, err := delegatedkeys.VerifySignature(publicKeyBytes, signatureBytes, encryptedKeyset)
97+
if err != nil || !valid {
98+
return nil, fmt.Errorf("failed to verify the wrapped keyset's signature: %v", err)
99+
}
100+
101+
delegatedKey, err := delegatedkeys.UnwrapKeyset(encryptedKeyset, p.KeyID)
85102
if err != nil {
86103
return nil, fmt.Errorf("failed to decrypt and unwrap data key: %v", err)
87104
}

0 commit comments

Comments
 (0)