[go: up one dir, main page]

Skip to content

Commit

Permalink
Add support for generating shared keys in PKC packages.
Browse files Browse the repository at this point in the history
Also adds benchmarking to support this.
  • Loading branch information
kisom committed Aug 22, 2013
1 parent 35fb24a commit 29d46e8
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 4 deletions.
5 changes: 5 additions & 0 deletions box/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ func ecdh(key PrivateKey, peer PublicKey) ([]byte, bool) {
return append(skey, mkey...), true
}

// SharedKey precomputes a key for encrypting with strongbox.
func SharedKey(key PrivateKey, peer PublicKey) (secretbox.Key, bool) {
return ecdh(key, peer)
}

// GenerateKey generates an appropriate private and public keypair for
// use in box.
func GenerateKey() (PrivateKey, PublicKey, bool) {
Expand Down
11 changes: 11 additions & 0 deletions box/box_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,14 @@ func BenchmarkOpenSigned(b *testing.B) {
}
}
}

// Benchmark the SharedKey function.
func BenchmarkSharedKey(b *testing.B) {
for i := 0; i < b.N; i++ {
_, ok := SharedKey(testGoodKey, testPeerPub)
if !ok {
fmt.Println("Computing shared key failed: benchmark aborted.")
b.FailNow()
}
}
}
6 changes: 4 additions & 2 deletions secretbox/secretbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,11 @@ func Open(box []byte, key Key) (message []byte, ok bool) {
}

msgLen := len(box) - sha256.Size
if !checkTag(key[cryptKeySize:], box) {
return nil, ok
}
message, err := decrypt(key[:cryptKeySize], box[:msgLen])
ok = checkTag(key[cryptKeySize:], box)
ok = ok && err == nil
ok = err == nil
return
}

Expand Down
5 changes: 5 additions & 0 deletions stoutbox/stoutbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ func ecdh(key PrivateKey, peer PublicKey) ([]byte, bool) {
return append(skey, mkey...), true
}

// SharedKey precomputes a key for encrypting with strongbox.
func SharedKey(key PrivateKey, peer PublicKey) (strongbox.Key, bool) {
return ecdh(key, peer)
}

// GenerateKey generates an appropriate private and public keypair for
// use in stoutbox.
func GenerateKey() (PrivateKey, PublicKey, bool) {
Expand Down
11 changes: 11 additions & 0 deletions stoutbox/stoutbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,14 @@ func BenchmarkOpenSigned(b *testing.B) {
}
}
}

// Benchmark the SharedKey function.
func BenchmarkSharedKey(b *testing.B) {
for i := 0; i < b.N; i++ {
_, ok := SharedKey(testGoodKey, testPeerPub)
if !ok {
fmt.Println("Computing shared key failed: benchmark aborted.")
b.FailNow()
}
}
}
6 changes: 4 additions & 2 deletions strongbox/strongbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,11 @@ func Open(box []byte, key Key) (message []byte, ok bool) {
}

msgLen := len(box) - sha512.Size384
if !checkTag(key[cryptKeySize:], box) {
return nil, false
}
message, err := decrypt(key[:cryptKeySize], box[:msgLen])
ok = checkTag(key[cryptKeySize:], box)
ok = ok && err == nil
ok = err == nil
return
}

Expand Down

0 comments on commit 29d46e8

Please sign in to comment.