Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions lib/ocrypto/asym_encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"errors"
"fmt"
"io"
"strconv"
"strings"

"golang.org/x/crypto/hkdf"
Expand All @@ -36,6 +37,9 @@ type PublicKeyEncryptor interface {
// Type required to use the scheme for encryption - notably, if it procduces extra metadata.
Type() SchemeType

// KeyType returns the key type, e.g. RSA or EC.
KeyType() KeyType

// For EC schemes, this method returns the public part of the ephemeral key.
// Otherwise, it returns nil.
EphemeralKey() []byte
Expand Down Expand Up @@ -139,10 +143,38 @@ func (e AsymEncryption) Type() SchemeType {
return RSA
}

func (e AsymEncryption) KeyType() KeyType {
switch e.PublicKey.Size() {
case 2048 / 8: //nolint:mnd // standard key size in bytes
return RSA2048Key
case 4096 / 8: //nolint:mnd // large key size in bytes
return RSA4096Key
default:
bitlen := e.PublicKey.Size() * 8 //nolint:mnd // convert to bits
return KeyType("rsa:" + strconv.Itoa(bitlen))
}
}

func (e ECEncryptor) Type() SchemeType {
return EC
}

func (e ECEncryptor) KeyType() KeyType {
switch e.pub.Curve() {
case ecdh.P256():
return EC256Key
case ecdh.P384():
return EC384Key
case ecdh.P521():
return EC521Key
default:
if n, ok := e.pub.Curve().(fmt.Stringer); ok {
return KeyType("ec:[" + n.String() + "]")
}
return KeyType("ec:[unknown]")
}
}

func (e AsymEncryption) EphemeralKey() []byte {
return nil
}
Expand Down
Loading