@ -72,8 +72,14 @@ func (c Context) WriteTo(w io.Writer) (n int64, err error) {
// data key generation and unsealing of KMS-generated
// data key generation and unsealing of KMS-generated
// data keys.
// data keys.
type KMS interface {
type KMS interface {
// KeyID - returns configured KMS key id.
// DefaultKeyID returns the default master key ID. It should be
KeyID ( ) string
// used for SSE-S3 and whenever a S3 client requests SSE-KMS but
// does not specify an explicit SSE-KMS key ID.
DefaultKeyID ( ) string
// CreateKey creates a new master key with the given key ID
// at the KMS.
CreateKey ( keyID string ) error
// GenerateKey generates a new random data key using
// GenerateKey generates a new random data key using
// the master key referenced by the keyID. It returns
// the master key referenced by the keyID. It returns
@ -90,21 +96,9 @@ type KMS interface {
// match the context used to generate the sealed key.
// match the context used to generate the sealed key.
UnsealKey ( keyID string , sealedKey [ ] byte , context Context ) ( key [ 32 ] byte , err error )
UnsealKey ( keyID string , sealedKey [ ] byte , context Context ) ( key [ 32 ] byte , err error )
// UpdateKey re-wraps the sealedKey if the master key, referenced by
// Info returns descriptive information about the KMS,
// `keyID`, has changed in the meantime. This usually happens when the
// like the default key ID and authentication method.
// KMS operator performs a key-rotation operation of the master key.
Info ( ) KMSInfo
// UpdateKey fails if the provided sealedKey cannot be decrypted using
// the master key referenced by keyID.
//
// UpdateKey makes no guarantees whatsoever about whether the returned
// rotatedKey is actually different from the sealedKey. If nothing has
// changed at the KMS or if the KMS does not support updating generated
// keys this method may behave like a NOP and just return the sealedKey
// itself.
UpdateKey ( keyID string , sealedKey [ ] byte , context Context ) ( rotatedKey [ ] byte , err error )
// Returns KMSInfo
Info ( ) ( kmsInfo KMSInfo )
}
}
type masterKeyKMS struct {
type masterKeyKMS struct {
@ -112,7 +106,8 @@ type masterKeyKMS struct {
masterKey [ 32 ] byte
masterKey [ 32 ] byte
}
}
// KMSInfo stores the details of KMS
// KMSInfo contains some describing information about
// the KMS.
type KMSInfo struct {
type KMSInfo struct {
Endpoint string
Endpoint string
Name string
Name string
@ -125,10 +120,14 @@ type KMSInfo struct {
// to the generated keys.
// to the generated keys.
func NewMasterKey ( keyID string , key [ 32 ] byte ) KMS { return & masterKeyKMS { keyID : keyID , masterKey : key } }
func NewMasterKey ( keyID string , key [ 32 ] byte ) KMS { return & masterKeyKMS { keyID : keyID , masterKey : key } }
func ( kms * masterKeyKMS ) KeyID ( ) string {
func ( kms * masterKeyKMS ) Default KeyID( ) string {
return kms . keyID
return kms . keyID
}
}
func ( kms * masterKeyKMS ) CreateKey ( keyID string ) error {
return errors . New ( "crypto: creating keys is not supported by a static master key" )
}
func ( kms * masterKeyKMS ) GenerateKey ( keyID string , ctx Context ) ( key [ 32 ] byte , sealedKey [ ] byte , err error ) {
func ( kms * masterKeyKMS ) GenerateKey ( keyID string , ctx Context ) ( key [ 32 ] byte , sealedKey [ ] byte , err error ) {
if _ , err = io . ReadFull ( rand . Reader , key [ : ] ) ; err != nil {
if _ , err = io . ReadFull ( rand . Reader , key [ : ] ) ; err != nil {
logger . CriticalIf ( context . Background ( ) , errOutOfEntropy )
logger . CriticalIf ( context . Background ( ) , errOutOfEntropy )
@ -166,13 +165,6 @@ func (kms *masterKeyKMS) UnsealKey(keyID string, sealedKey []byte, ctx Context)
return key , nil
return key , nil
}
}
func ( kms * masterKeyKMS ) UpdateKey ( keyID string , sealedKey [ ] byte , ctx Context ) ( [ ] byte , error ) {
if _ , err := kms . UnsealKey ( keyID , sealedKey , ctx ) ; err != nil {
return nil , err
}
return sealedKey , nil // The master key cannot update data keys -> Do nothing.
}
func ( kms * masterKeyKMS ) deriveKey ( keyID string , context Context ) ( key [ 32 ] byte ) {
func ( kms * masterKeyKMS ) deriveKey ( keyID string , context Context ) ( key [ 32 ] byte ) {
if context == nil {
if context == nil {
context = Context { }
context = Context { }