-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
264 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,98 @@ | ||
package identity | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"crypto/elliptic" | ||
"crypto/rand" | ||
"crypto/sha256" | ||
"crypto/x509" | ||
"encoding/asn1" | ||
"encoding/pem" | ||
"fmt" | ||
"github.com/aviate-labs/agent-go/principal" | ||
"math/big" | ||
) | ||
|
||
var prime256v1OID = asn1.ObjectIdentifier{1, 2, 840, 10045, 3, 1, 7} | ||
|
||
type Prime256v1Identity struct { | ||
privateKey *ecdsa.PrivateKey | ||
publicKey *ecdsa.PublicKey | ||
} | ||
|
||
func NewRandomPrime256v1Identity() (*Prime256v1Identity, error) { | ||
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return NewPrime256v1Identity(privateKey), nil | ||
} | ||
|
||
func (id Prime256v1Identity) Sender() principal.Principal { | ||
return principal.NewSelfAuthenticating(id.PublicKey()) | ||
} | ||
|
||
func (id Prime256v1Identity) Sign(msg []byte) []byte { | ||
hashData := sha256.Sum256(msg) | ||
sigR, sigS, _ := ecdsa.Sign(rand.Reader, id.privateKey, hashData[:]) | ||
var buffer [64]byte | ||
r := sigR.Bytes() | ||
s := sigS.Bytes() | ||
copy(buffer[(32-len(r)):], r) | ||
copy(buffer[(64-len(s)):], s) | ||
return buffer[:] | ||
} | ||
|
||
func (id Prime256v1Identity) Verify(msg, sig []byte) bool { | ||
r := new(big.Int).SetBytes(sig[:32]) | ||
s := new(big.Int).SetBytes(sig[32:]) | ||
hashData := sha256.Sum256(msg) | ||
return ecdsa.Verify(id.publicKey, hashData[:], r, s) | ||
} | ||
|
||
func NewPrime256v1Identity(privateKey *ecdsa.PrivateKey) *Prime256v1Identity { | ||
return &Prime256v1Identity{ | ||
privateKey: privateKey, | ||
publicKey: privateKey.Public().(*ecdsa.PublicKey), | ||
} | ||
} | ||
|
||
func NewPrime256v1IdentityFromPEM(data []byte) (*Prime256v1Identity, error) { | ||
block, remainder := pem.Decode(data) | ||
if block == nil || block.Type != "EC PRIVATE KEY" || len(remainder) != 0 { | ||
return nil, fmt.Errorf("invalid pem file") | ||
} | ||
privateKey, err := x509.ParseECPrivateKey(block.Bytes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return NewPrime256v1Identity(privateKey), nil | ||
} | ||
|
||
func derEncodePrime256v1PublicKey(key *ecdsa.PublicKey) ([]byte, error) { | ||
return asn1.Marshal(ecPublicKey{ | ||
Metadata: []asn1.ObjectIdentifier{ | ||
ecPublicKeyOID, | ||
prime256v1OID, | ||
}, | ||
PublicKey: asn1.BitString{ | ||
Bytes: marshal(elliptic.P256(), key.X, key.Y), | ||
}, | ||
}) | ||
} | ||
|
||
func (id Prime256v1Identity) PublicKey() []byte { | ||
der, _ := derEncodePrime256v1PublicKey(id.publicKey) | ||
return der | ||
} | ||
|
||
func (id Prime256v1Identity) ToPEM() ([]byte, error) { | ||
data, err := x509.MarshalECPrivateKey(id.privateKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return pem.EncodeToMemory(&pem.Block{ | ||
Type: "EC PRIVATE KEY", | ||
Bytes: data, | ||
}), nil | ||
} |
This file contains 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,54 @@ | ||
package identity | ||
|
||
import ( | ||
"bytes" | ||
"encoding/hex" | ||
"testing" | ||
) | ||
|
||
func TestNewPrime256v1Identity(t *testing.T) { | ||
id, _ := NewRandomPrime256v1Identity() | ||
data, err := id.ToPEM() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
id_, err := NewPrime256v1IdentityFromPEM(data) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !bytes.Equal(id.privateKey.D.Bytes(), id_.privateKey.D.Bytes()) { | ||
t.Error() | ||
} | ||
if !bytes.Equal(id.PublicKey(), id_.PublicKey()) { | ||
t.Error() | ||
} | ||
} | ||
|
||
func TestPrime256v1Identity_Sign(t *testing.T) { | ||
id, err := NewRandomPrime256v1Identity() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
data := []byte("hello") | ||
if !id.Verify(data, id.Sign(data)) { | ||
t.Error() | ||
} | ||
} | ||
|
||
func TestNewPrime256v1IdentityFromPEM(t *testing.T) { | ||
pem := ` | ||
-----BEGIN EC PRIVATE KEY----- | ||
MHcCAQEEIL1ybmbwx+uKYsscOZcv71MmKhrNqfPP0ke1unET5AY4oAoGCCqGSM49 | ||
AwEHoUQDQgAEUbbZV4NerZTPWfbQ749/GNLu8TaH8BUS/I7/+ipsu+MPywfnBFIZ | ||
Sks4xGbA/ZbazsrMl4v446U5UIVxCGGaKw== | ||
-----END EC PRIVATE KEY----- | ||
` | ||
der, _ := hex.DecodeString("3059301306072a8648ce3d020106082a8648ce3d0301070342000451b6d957835ead94cf59f6d0ef8f7f18d2eef13687f01512fc8efffa2a6cbbe30fcb07e70452194a4b38c466c0fd96dacecacc978bf8e3a53950857108619a2b") | ||
id, err := NewPrime256v1IdentityFromPEM([]byte(pem)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !bytes.Equal(id.PublicKey(), der) { | ||
t.Fatal("public key mismatch") | ||
} | ||
} |
This file contains 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
This file contains 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