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"
|
2022-08-01 03:11:13 -04:00
|
|
|
"strings"
|
2022-03-22 11:03:15 -04:00
|
|
|
"testing"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/crypto/testvector"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-08-01 03:11:13 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-06-30 09:24:36 -04:00
|
|
|
"go.uber.org/goleak"
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
|
|
|
|
2022-06-30 09:24:36 -04:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
goleak.VerifyTestMain(m)
|
|
|
|
}
|
|
|
|
|
2022-07-14 09:45:04 -04:00
|
|
|
func TestClusterKMS(t *testing.T) {
|
2022-08-01 03:11:13 -04:00
|
|
|
testVector := testvector.HKDF0xFF
|
2022-03-22 11:03:15 -04:00
|
|
|
assert := assert.New(t)
|
2022-08-01 03:11:13 -04:00
|
|
|
kms := New(testVector.Salt)
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
key, err := kms.GetDEK(context.Background(), "", "key-1", 32)
|
|
|
|
assert.Error(err)
|
|
|
|
assert.Nil(key)
|
|
|
|
|
2022-08-01 03:11:13 -04:00
|
|
|
err = kms.CreateKEK(context.Background(), "", testVector.Secret)
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.NoError(err)
|
2022-08-01 03:11:13 -04:00
|
|
|
assert.Equal(testVector.Secret, kms.masterKey)
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-08-01 03:11:13 -04:00
|
|
|
keyLower, err := kms.GetDEK(
|
|
|
|
context.Background(),
|
|
|
|
"",
|
|
|
|
strings.ToLower(testVector.InfoPrefix+testVector.Info),
|
|
|
|
int(testVector.Length),
|
|
|
|
)
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.NoError(err)
|
2022-08-01 03:11:13 -04:00
|
|
|
assert.Equal(testVector.Output, keyLower)
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-08-01 03:11:13 -04:00
|
|
|
// output of the KMS should be case sensitive
|
|
|
|
keyUpper, err := kms.GetDEK(
|
|
|
|
context.Background(),
|
|
|
|
"",
|
|
|
|
strings.ToUpper(testVector.InfoPrefix+testVector.Info),
|
|
|
|
int(testVector.Length),
|
|
|
|
)
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.NoError(err)
|
2022-08-01 03:11:13 -04:00
|
|
|
assert.NotEqual(key, keyUpper)
|
|
|
|
}
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-08-01 03:11:13 -04:00
|
|
|
func TestVectorsHKDF(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
kek []byte
|
|
|
|
salt []byte
|
|
|
|
dekID string
|
|
|
|
dekSize uint
|
|
|
|
wantKey []byte
|
|
|
|
}{
|
|
|
|
"rfc Test Case 1": {
|
|
|
|
kek: testvector.HKDFrfc1.Secret,
|
|
|
|
salt: testvector.HKDFrfc1.Salt,
|
|
|
|
dekID: testvector.HKDFrfc1.Info,
|
|
|
|
dekSize: testvector.HKDFrfc1.Length,
|
|
|
|
wantKey: testvector.HKDFrfc1.Output,
|
|
|
|
},
|
|
|
|
"rfc Test Case 2": {
|
|
|
|
kek: testvector.HKDFrfc2.Secret,
|
|
|
|
salt: testvector.HKDFrfc2.Salt,
|
|
|
|
dekID: testvector.HKDFrfc2.Info,
|
|
|
|
dekSize: testvector.HKDFrfc2.Length,
|
|
|
|
wantKey: testvector.HKDFrfc2.Output,
|
|
|
|
},
|
|
|
|
"rfc Test Case 3": {
|
|
|
|
kek: testvector.HKDFrfc3.Secret,
|
|
|
|
salt: testvector.HKDFrfc3.Salt,
|
|
|
|
dekID: testvector.HKDFrfc3.Info,
|
|
|
|
dekSize: testvector.HKDFrfc3.Length,
|
|
|
|
wantKey: testvector.HKDFrfc3.Output,
|
|
|
|
},
|
|
|
|
"HKDF zero": {
|
|
|
|
kek: testvector.HKDFZero.Secret,
|
|
|
|
salt: testvector.HKDFZero.Salt,
|
|
|
|
dekID: testvector.HKDFZero.InfoPrefix + testvector.HKDFZero.Info,
|
|
|
|
dekSize: testvector.HKDFZero.Length,
|
|
|
|
wantKey: testvector.HKDFZero.Output,
|
|
|
|
},
|
|
|
|
"HKDF 0xFF": {
|
|
|
|
kek: testvector.HKDF0xFF.Secret,
|
|
|
|
salt: testvector.HKDF0xFF.Salt,
|
|
|
|
dekID: testvector.HKDF0xFF.InfoPrefix + testvector.HKDF0xFF.Info,
|
|
|
|
dekSize: testvector.HKDF0xFF.Length,
|
|
|
|
wantKey: testvector.HKDF0xFF.Output,
|
|
|
|
},
|
|
|
|
}
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-08-01 03:11:13 -04:00
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
kms := New(tc.salt)
|
|
|
|
require.NoError(kms.CreateKEK(context.Background(), "", tc.kek))
|
|
|
|
|
|
|
|
out, err := kms.GetDEK(context.Background(), "", tc.dekID, int(tc.dekSize))
|
|
|
|
require.NoError(err)
|
|
|
|
assert.Equal(tc.wantKey, out)
|
|
|
|
})
|
|
|
|
}
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|