forked from hyperledger-archives/aries-framework-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add Wrap/Unwrap crypto functions
This change introduces Wrap() and Unwrap() to Cryto api. The functions work for ECDHES keys only for now. Other keys (ECDH1PU, Ed25519) can be introduced in a future change. It also includes setting APU and APV in the key derivation process (they are set to empty `[]byte{}` in the current JWE building service). closes hyperledger-archives#2257 Signed-off-by: Baha Shaaban <baha.shaaban@securekey.com>
- Loading branch information
Baha Shaaban
committed
Oct 20, 2020
1 parent
b37cbb8
commit d762c89
Showing
10 changed files
with
593 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package tinkcrypto | ||
|
||
import ( | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"crypto/ecdsa" | ||
"crypto/elliptic" | ||
"crypto/rand" | ||
|
||
hybrid "github.com/google/tink/go/hybrid/subtle" | ||
josecipher "github.com/square/go-jose/v3/cipher" | ||
) | ||
|
||
type keyWrapper interface { | ||
getCurve(curve string) (elliptic.Curve, error) | ||
generateKey(curve elliptic.Curve) (*ecdsa.PrivateKey, error) | ||
createCipher(key []byte) (cipher.Block, error) | ||
wrap(block cipher.Block, cek []byte) ([]byte, error) | ||
unWrap(block cipher.Block, encryptedKey []byte) ([]byte, error) | ||
} | ||
|
||
type keyWrapperSupport struct{} | ||
|
||
func (w *keyWrapperSupport) getCurve(curve string) (elliptic.Curve, error) { | ||
return hybrid.GetCurve(curve) | ||
} | ||
|
||
func (w *keyWrapperSupport) generateKey(curve elliptic.Curve) (*ecdsa.PrivateKey, error) { | ||
return ecdsa.GenerateKey(curve, rand.Reader) | ||
} | ||
|
||
func (w *keyWrapperSupport) createCipher(kek []byte) (cipher.Block, error) { | ||
return aes.NewCipher(kek) | ||
} | ||
|
||
func (w *keyWrapperSupport) wrap(block cipher.Block, cek []byte) ([]byte, error) { | ||
return josecipher.KeyWrap(block, cek) | ||
} | ||
|
||
func (w *keyWrapperSupport) unWrap(block cipher.Block, encryptedKey []byte) ([]byte, error) { | ||
return josecipher.KeyUnwrap(block, encryptedKey) | ||
} |
Oops, something went wrong.