github.com/trustbloc/kms-go@v1.1.2/spi/crypto/crypto.go (about) 1 /* 2 Copyright SecureKey Technologies Inc. All Rights Reserved. 3 Copyright Gen Digital Inc. All Rights Reserved. 4 5 SPDX-License-Identifier: Apache-2.0 6 */ 7 8 // Package crypto contains the Crypto interface to be used by the framework. 9 // It will be created via Options creation in pkg/framework/context.Provider. 10 // BBS+ signature scheme is not included in the main Crypto interface. 11 // It is defined separately under the primitive sub-package including its implementation which should not be referenced 12 // directly. It is accessible via the framework's KMS BBS+ keys and tinkcrypto's bbs package's Signer and Verifier 13 // primitives or via webkms for remote KMS BBS+ signing. 14 package crypto 15 16 // Crypto interface provides all crypto operations needed in the Aries framework. 17 type Crypto interface { 18 // Encrypt will encrypt msg and aad using a matching AEAD primitive in kh key handle of a public key 19 // returns: 20 // cipherText in []byte 21 // nonce in []byte 22 // error in case of errors during encryption 23 Encrypt(msg, aad []byte, kh interface{}) ([]byte, []byte, error) 24 // Decrypt will decrypt cipher with aad and given nonce using a matching AEAD primitive in kh key handle of a 25 // private key 26 // returns: 27 // plainText in []byte 28 // error in case of errors 29 Decrypt(cipher, aad, nonce []byte, kh interface{}) ([]byte, error) 30 // Sign will sign msg using a matching signature primitive in kh key handle of a private key 31 // returns: 32 // signature in []byte 33 // error in case of errors 34 Sign(msg []byte, kh interface{}) ([]byte, error) 35 // Verify will verify a signature for the given msg using a matching signature primitive in kh key handle of 36 // a public key 37 // returns: 38 // error in case of errors or nil if signature verification was successful 39 Verify(signature, msg []byte, kh interface{}) error 40 // ComputeMAC computes message authentication code (MAC) for code data 41 // using a matching MAC primitive in kh key handle 42 ComputeMAC(data []byte, kh interface{}) ([]byte, error) 43 // VerifyMAC determines if mac is a correct authentication code (MAC) for data 44 // using a matching MAC primitive in kh key handle and returns nil if so, otherwise it returns an error. 45 VerifyMAC(mac, data []byte, kh interface{}) error 46 // WrapKey will execute key wrapping of cek using apu, apv and recipient public key 'recPubKey'. 47 // 'opts' allows setting the optional sender key handle using WithSender() option and the an authentication tag 48 // using WithTag() option. These allow ECDH-1PU key unwrapping (aka Authcrypt). 49 // The absence of these options uses ECDH-ES key wrapping (aka Anoncrypt). Another option that can 50 // be used is WithXC20PKW() to instruct the WrapKey to use XC20P key wrapping instead of the default A256GCM. 51 // returns: 52 // RecipientWrappedKey containing the wrapped cek value 53 // error in case of errors 54 WrapKey(cek, apu, apv []byte, recPubKey *PublicKey, 55 opts ...WrapKeyOpts) (*RecipientWrappedKey, error) 56 // UnwrapKey unwraps a key in recWK using recipient private key kh. 57 // 'opts' allows setting the optional sender key handle using WithSender() option and the an authentication tag 58 // using WithTag() option. These allow ECDH-1PU key unwrapping (aka Authcrypt). 59 // The absence of these options uses ECDH-ES key unwrapping (aka Anoncrypt). There is no need to 60 // use WithXC20PKW() for UnwrapKey since the function will use the wrapping algorithm based on recWK.Alg. 61 // returns: 62 // unwrapped key in raw bytes 63 // error in case of errors 64 UnwrapKey(recWK *RecipientWrappedKey, kh interface{}, opts ...WrapKeyOpts) ([]byte, error) 65 // SignMulti will create a signature of messages using a matching signing primitive found in kh key handle of a 66 // private key. 67 // returns: 68 // signature in []byte 69 // error in case of errors 70 SignMulti(messages [][]byte, kh interface{}) ([]byte, error) 71 // VerifyMulti will verify a signature of messages using a matching signing primitive found in kh key handle of a 72 // public key. 73 // returns: 74 // error in case of errors or nil if signature verification was successful 75 VerifyMulti(messages [][]byte, signature []byte, kh interface{}) error 76 // VerifyProof will verify a signature proof (generated e.g. by Verifier's DeriveProof() call) for revealedMessages 77 // using a matching signing primitive found in kh key handle of a public key. 78 // returns: 79 // error in case of errors or nil if signature proof verification was successful 80 VerifyProof(revealedMessages [][]byte, proof, nonce []byte, kh interface{}) error 81 // DeriveProof will create a signature proof for a list of revealed messages using BBS signature (can be built using 82 // a Signer's SignMulti() call) and a matching signing primitive found in kh key handle of a public key. 83 // returns: 84 // signature proof in []byte 85 // error in case of errors 86 DeriveProof(messages [][]byte, bbsSignature, nonce []byte, revealedIndexes []int, kh interface{}) ([]byte, error) 87 } 88 89 // RecipientWrappedKey contains recipient key material required to unwrap CEK. 90 type RecipientWrappedKey struct { 91 KID string `json:"kid,omitempty"` 92 EncryptedCEK []byte `json:"encryptedcek,omitempty"` 93 EPK PublicKey `json:"epk,omitempty"` 94 Alg string `json:"alg,omitempty"` 95 APU []byte `json:"apu,omitempty"` 96 APV []byte `json:"apv,omitempty"` 97 } 98 99 // PublicKey mainly to exchange EPK in RecipientWrappedKey. 100 type PublicKey struct { 101 KID string `json:"kid,omitempty"` 102 X []byte `json:"x,omitempty"` 103 N []byte `json:"n,omitempty"` 104 E []byte `json:"e,omitempty"` 105 Y []byte `json:"y,omitempty"` 106 Curve string `json:"curve,omitempty"` 107 Type string `json:"type,omitempty"` 108 } 109 110 // PrivateKey mainly used to exchange ephemeral private key in JWE encrypter. 111 type PrivateKey struct { 112 PublicKey PublicKey `json:"pubKey,omitempty"` 113 D []byte `json:"d,omitempty"` 114 }