-
Notifications
You must be signed in to change notification settings - Fork 24
feat(PLAT-3112): Add Elliptic Curve functionality to support nanotdf [PLAT-3112] #576
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a490b3f
Initial pass
e63464b
merge to latest
844131d
don't need cipher mode here after all
d8ba832
Uppercase to make types public
5e69210
Fix one stray lowercase reference
ff6221f
Merge branch 'main' of github.com:opentdf/platform into PLAT-3112-ocr…
4e21d83
Fix iports with replace directive
ed2ce62
more ocrypto import cleanups
c7125e3
punt - do not consume new ec code in this PR
22a9f56
Update sdk/go.mod
patmantru 8a14dd0
Update sdk/go.mod
patmantru 9140483
Update lib/ocrypto/ec_key_pair_test.go
patmantru fcade4a
remove import redirect
7179183
reverting some changes
0ac55ef
Use require instead of fatal error
699684c
Merge branch 'main' into PLAT-3112-ocrypto-EC-support
patmantru File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| package ocrypto | ||
|
|
||
| import ( | ||
| "crypto/ecdsa" | ||
| "crypto/elliptic" | ||
| "crypto/rand" | ||
| "crypto/x509" | ||
| "encoding/pem" | ||
| "errors" | ||
| "fmt" | ||
| ) | ||
|
|
||
| type eccMode uint8 | ||
|
|
||
| const ( | ||
| eccModeSecp256r1 eccMode = 0 | ||
| eccModeSecp384r1 eccMode = 1 | ||
| eccModeSecp521r1 eccMode = 2 | ||
| eccModeSecp256k1 eccMode = 3 | ||
| ) | ||
|
|
||
| type EcKeyPair struct { | ||
| privateKey *ecdsa.PrivateKey | ||
patmantru marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // NewECKeyPair Generates an EC key pair of the given bit size. | ||
| func NewECKeyPair(mode eccMode) (EcKeyPair, error) { | ||
|
|
||
| var c elliptic.Curve | ||
| switch mode { | ||
| case eccModeSecp256r1: | ||
| c = elliptic.P256() | ||
| break | ||
patmantru marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| case eccModeSecp384r1: | ||
| c = elliptic.P384() | ||
| break | ||
| case eccModeSecp521r1: | ||
| c = elliptic.P521() | ||
| break | ||
| case eccModeSecp256k1: | ||
| // TODO FIXME - unsupported? | ||
| return EcKeyPair{}, errors.New("unsupported ec key pair mode") | ||
| default: | ||
| return EcKeyPair{}, fmt.Errorf("invalid ec key pair mode %d", mode) | ||
| } | ||
|
|
||
| privateKey, err := ecdsa.GenerateKey(c, rand.Reader) | ||
| if err != nil { | ||
| return EcKeyPair{}, fmt.Errorf("ec.GenerateKey failed: %w", err) | ||
| } | ||
|
|
||
| ecKeyPair := EcKeyPair{privateKey: privateKey} | ||
| return ecKeyPair, nil | ||
| } | ||
|
|
||
| // PrivateKeyInPemFormat Returns private key in pem format. | ||
| func (keyPair EcKeyPair) PrivateKeyInPemFormat() (string, error) { | ||
| if keyPair.privateKey == nil { | ||
| return "", errors.New("failed to generate PEM formatted private key") | ||
| } | ||
|
|
||
| privateKeyBytes, err := x509.MarshalPKCS8PrivateKey(keyPair.privateKey) | ||
| if err != nil { | ||
| return "", fmt.Errorf("x509.MarshalPKCS8PrivateKey failed: %w", err) | ||
| } | ||
|
|
||
| privateKeyPem := pem.EncodeToMemory( | ||
| &pem.Block{ | ||
| Type: "PRIVATE KEY", | ||
| Bytes: privateKeyBytes, | ||
| }, | ||
| ) | ||
| return string(privateKeyPem), nil | ||
| } | ||
|
|
||
| // PublicKeyInPemFormat Returns public key in pem format. | ||
| func (keyPair EcKeyPair) PublicKeyInPemFormat() (string, error) { | ||
| if keyPair.privateKey == nil { | ||
| return "", errors.New("failed to generate PEM formatted public key") | ||
| } | ||
|
|
||
| publicKeyBytes, err := x509.MarshalPKIXPublicKey(&keyPair.privateKey.PublicKey) | ||
| if err != nil { | ||
| return "", fmt.Errorf("x509.MarshalPKIXPublicKey failed: %w", err) | ||
| } | ||
|
|
||
| publicKeyPem := pem.EncodeToMemory( | ||
| &pem.Block{ | ||
| Type: "PUBLIC KEY", | ||
| Bytes: publicKeyBytes, | ||
| }, | ||
| ) | ||
|
|
||
| return string(publicKeyPem), nil | ||
| } | ||
|
|
||
| // KeySize Return the size of this ec key pair. | ||
| func (keyPair EcKeyPair) KeySize() (int, error) { | ||
| if keyPair.privateKey == nil { | ||
| return -1, errors.New("failed to return key size") | ||
| } | ||
| return keyPair.privateKey.Params().N.BitLen(), nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package ocrypto | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestECKeyPair(t *testing.T) { | ||
| for _, modeGood := range []eccMode{eccModeSecp256r1, eccModeSecp384r1, eccModeSecp521r1} { | ||
| ecKeyPair, err := NewECKeyPair(modeGood) | ||
| if err != nil { | ||
| t.Fatalf("NewECKeyPair(%d): %v", modeGood, err) | ||
| } | ||
patmantru marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| _, err = ecKeyPair.PublicKeyInPemFormat() | ||
| if err != nil { | ||
| t.Fatalf("ec PublicKeyInPemFormat() error - %v", err) | ||
| } | ||
|
|
||
| _, err = ecKeyPair.PrivateKeyInPemFormat() | ||
| if err != nil { | ||
| t.Fatalf("ec PrivateKeyInPemFormat() error - %v", err) | ||
| } | ||
|
|
||
| keySize, err := ecKeyPair.KeySize() | ||
| if err != nil { | ||
| t.Fatalf("ec keysize error - %v", err) | ||
| } | ||
|
|
||
| // Set expected size based on mode | ||
| size := 0 | ||
| switch modeGood { | ||
| case eccModeSecp256r1: | ||
| size = 256 | ||
| break | ||
| case eccModeSecp384r1: | ||
| size = 384 | ||
| break | ||
| case eccModeSecp521r1: | ||
| size = 521 | ||
| break | ||
| default: | ||
| size = 99999 // deliberately bad value | ||
| } | ||
|
|
||
| if keySize != size { | ||
| t.Fatalf("invalid key size for mode %d, expected:%d actual:%d", | ||
| modeGood, size, keySize) | ||
| } | ||
| } | ||
|
|
||
| // Fail case | ||
| emptyECKeyPair := EcKeyPair{} | ||
|
|
||
| _, err := emptyECKeyPair.PrivateKeyInPemFormat() | ||
| if err == nil { | ||
| t.Fatal("EcKeyPair.PrivateKeyInPemFormat() fail to return error") | ||
| } | ||
|
|
||
| _, err = emptyECKeyPair.PublicKeyInPemFormat() | ||
| if err == nil { | ||
| t.Fatal("EcKeyPair.PublicKeyInPemFormat() fail to return error") | ||
| } | ||
|
|
||
| _, err = emptyECKeyPair.KeySize() | ||
| if err == nil { | ||
| t.Fatal("EcKeyPair.keySize() fail to return error") | ||
| } | ||
|
|
||
| for _, modeBad := range []eccMode{eccModeSecp256k1} { | ||
| _, err := NewECKeyPair(modeBad) | ||
| if err == nil { | ||
| t.Fatalf("did not fail as expected: NewECKeyPair(%d): %v", modeBad, err) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.