2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-05-10 06:35:17 -04:00
|
|
|
package cluster
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/crypto"
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
|
|
|
|
2022-10-05 09:02:46 -04:00
|
|
|
// KMS implements the kms.CloudKMS interface for in cluster key management.
|
|
|
|
type KMS struct {
|
2022-03-22 11:03:15 -04:00
|
|
|
masterKey []byte
|
2022-07-29 03:52:47 -04:00
|
|
|
salt []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new ClusterKMS.
|
2022-10-05 09:02:46 -04:00
|
|
|
func New(salt []byte) *KMS {
|
|
|
|
return &KMS{salt: salt}
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-07-14 09:45:04 -04:00
|
|
|
// CreateKEK sets the ClusterKMS masterKey.
|
2022-10-05 09:02:46 -04:00
|
|
|
func (c *KMS) CreateKEK(ctx context.Context, keyID string, kek []byte) error {
|
2022-03-22 11:03:15 -04:00
|
|
|
c.masterKey = kek
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDEK derives a key from the KMS masterKey.
|
2022-10-05 09:02:46 -04:00
|
|
|
func (c *KMS) GetDEK(ctx context.Context, kekID string, dekID string, dekSize int) ([]byte, error) {
|
2022-03-22 11:03:15 -04:00
|
|
|
if len(c.masterKey) == 0 {
|
|
|
|
return nil, errors.New("master key not set for Constellation KMS")
|
|
|
|
}
|
2022-07-29 03:52:47 -04:00
|
|
|
return crypto.DeriveKey(c.masterKey, c.salt, []byte(dekID), uint(dekSize))
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|