Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

refactor: Crypter/Wallet crypto operations #459

Merged
merged 1 commit into from
Oct 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 15 additions & 21 deletions pkg/didcomm/crypto/crypter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ SPDX-License-Identifier: Apache-2.0

package crypto

import "github.com/hyperledger/aries-framework-go/pkg/wallet"

// Provider interface for Crypter ctx
type Provider interface {
CryptoWallet() wallet.Crypto
}

// CrypterCreator method to create new crypter service
type CrypterCreator func(prov Provider) (Crypter, error)

// Crypter is an Aries envelope encrypter to support
// secure DIDComm exchange of envelopes between Aries agents
// TODO create a higher-level crypto that switches implementations based on the algorithm - Issue #273
Expand All @@ -16,29 +26,13 @@ type Crypter interface {
// []byte containing the encrypted envelope
// error if encryption failed
// TODO add key type of recipients and sender keys to be validated by the implementation - Issue #272
Encrypt(payload []byte, sender KeyPair, recipients [][]byte) ([]byte, error)
// Decrypt an envelope in an Aries compliant format with the recipient's private key
// and the recipient's public key both set in recipientKeyPair
Encrypt(payload []byte, senderKey []byte, recipients [][]byte) ([]byte, error)
// Decrypt an envelope in an Aries compliant format.
// The recipient's key will be matched from the wallet with the list of recipients in the envelope
//
// returns:
// []byte containing the decrypted payload
// error if decryption failed
// TODO add key type of recipients keys to be validated by the implementation - Issue #272
Decrypt(envelope []byte, recipientKeyPair KeyPair) ([]byte, error)
}

// KeyPair represents a private/public key pair each with 32 bytes in size
type KeyPair struct {
// Priv is a private key
Priv []byte
// Pub is a public key
Pub []byte
}

// IsKeyPairValid is a utility function that validates a KeyPair
func IsKeyPairValid(kp KeyPair) bool {
if kp.Priv == nil || kp.Pub == nil {
return false
}

return true
Decrypt(envelope []byte) ([]byte, error)
}
23 changes: 0 additions & 23 deletions pkg/didcomm/crypto/crypter_test.go

This file was deleted.

36 changes: 11 additions & 25 deletions pkg/didcomm/crypto/jwe/authcrypt/authcrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (
"errors"

chacha "golang.org/x/crypto/chacha20poly1305"

"github.com/hyperledger/aries-framework-go/pkg/didcomm/crypto"
"github.com/hyperledger/aries-framework-go/pkg/wallet"
)

// This package deals with Authcrypt encryption for Packing/Unpacking DID Comm exchange
Expand All @@ -30,25 +33,14 @@ const XC20P = ContentEncryption("XC20P") // XChacha20 encryption + Poly1305 auth
//nolint:gochecknoglobals
var randReader = rand.Reader

// errEmptyRecipients is used when recipients list is empty
var errEmptyRecipients = errors.New("empty recipients")

// errInvalidKeypair is used when a keypair is invalid
var errInvalidKeypair = errors.New("invalid keypair")

// errInvalidKey is used when a key is invalid
var errInvalidKey = errors.New("invalid key")

// errRecipientNotFound is used when a recipient is not found
var errRecipientNotFound = errors.New("recipient not found")

// errUnsupportedAlg is used when a bad encryption algorithm is used
var errUnsupportedAlg = errors.New("algorithm not supported")

// Crypter represents an Authcrypt Encrypter (Decrypter) that outputs/reads JWE envelopes
type Crypter struct {
alg ContentEncryption
nonceSize int
wallet wallet.Crypto
}

// Envelope represents a JWE envelope as per the Aries Encryption envelope specs
Expand Down Expand Up @@ -106,7 +98,8 @@ type jwk struct {
// C20P (chacha20-poly1305 ietf)
// XC20P (xchacha20-poly1305 ietf)
// The returned crypter contains all the information required to encrypt payloads.
func New(alg ContentEncryption) (*Crypter, error) {
func New(ctx crypto.Provider, alg ContentEncryption) (*Crypter, error) {
w := ctx.CryptoWallet()
var nonceSize int
switch alg {
case C20P:
Expand All @@ -117,16 +110,9 @@ func New(alg ContentEncryption) (*Crypter, error) {
return nil, errUnsupportedAlg
}

c := &Crypter{
alg,
nonceSize,
}

return c, nil
}

// IsChachaKeyValid will return true if key size is the same as chacha20poly1305.keySize
// false otherwise
func IsChachaKeyValid(key []byte) bool {
return len(key) == chacha.KeySize
return &Crypter{
alg: alg,
nonceSize: nonceSize,
wallet: w,
}, nil
}
Loading