refactor: Move functions from kms pkg to new libs/crypto pkg (#1650)

This commit is contained in:
Jim authored and GitHub committed 2021-11-04 08:55:30 -04:00
1 parent a86ee59bcd
commit dd2c3807cd
16 files changed
+601 -186

No files matched your search

@@ -2,15 +2,12 @@ package vault
import (
"context"
"crypto/ed25519"
"crypto/hmac"
"crypto/sha256"
"database/sql"
"github.com/hashicorp/boundary/internal/credential/vault/store"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/errors"
"github.com/hashicorp/boundary/internal/kms"
"github.com/hashicorp/boundary/internal/libs/crypto"
"github.com/hashicorp/boundary/internal/oplog"
wrapping "github.com/hashicorp/go-kms-wrapping"
"github.com/hashicorp/go-kms-wrapping/structwrapping"
@@ -103,17 +100,14 @@ func (c *ClientCertificate) hmacCertificateKey(ctx context.Context, cipher wrapp
if cipher == nil {
return errors.New(ctx, errors.InvalidParameter, op, "missing cipher")
}
reader, err := kms.NewDerivedReader(cipher, 32, []byte(c.StoreId), nil)
// this operation currently uses the legacy WithEd25519 option for hmac'ing.
// we should likely deprecate this and introduce a new "crypto version" of
// this attribute.
hm, err := crypto.HmacSha256(ctx, c.CertificateKey, cipher, []byte(c.StoreId), nil, crypto.WithEd25519())
if err != nil {
return errors.Wrap(ctx, err, op)
}
key, _, err := ed25519.GenerateKey(reader)
if err != nil {
return errors.New(ctx, errors.Encrypt, op, "unable to generate derived key")
}
mac := hmac.New(sha256.New, key)
_, _ = mac.Write(c.CertificateKey)
c.CertificateKeyHmac = mac.Sum(nil)
c.CertificateKeyHmac = []byte(hm)
return nil
}
+6 -9
View File
@@ -2,18 +2,16 @@ package vault
import (
"context"
"crypto/hmac"
"crypto/sha256"
"database/sql"
"time"
"github.com/hashicorp/boundary/internal/credential/vault/store"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/errors"
"github.com/hashicorp/boundary/internal/libs/crypto"
"github.com/hashicorp/boundary/internal/oplog"
wrapping "github.com/hashicorp/go-kms-wrapping"
"github.com/hashicorp/go-kms-wrapping/structwrapping"
"golang.org/x/crypto/blake2b"
"google.golang.org/protobuf/proto"
)
@@ -74,16 +72,15 @@ func newToken(storeId string, token TokenSecret, accessor []byte, expiration tim
accessorCopy := make([]byte, len(accessor))
copy(accessorCopy, accessor)
key := blake2b.Sum256(accessorCopy)
mac := hmac.New(sha256.New, key[:])
_, _ = mac.Write(tokenCopy)
hmac := mac.Sum(nil)
hmac, err := crypto.HmacSha256WithPrk(context.Background(), tokenCopy, accessorCopy)
if err != nil {
return nil, errors.WrapDeprecated(err, op, errors.WithCode(errors.Encrypt))
}
t := &Token{
expiration: expiration.Round(time.Second),
Token: &store.Token{
StoreId: storeId,
TokenHmac: hmac,
TokenHmac: []byte(hmac),
Token: tokenCopy,
Status: string(CurrentToken),
},