github.com/kaituanwang/hyperledger@v2.0.1+incompatible/bccsp/bccsp.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package bccsp
     8  
     9  import (
    10  	"crypto"
    11  	"hash"
    12  )
    13  
    14  // Key represents a cryptographic key
    15  type Key interface {
    16  
    17  	// Bytes converts this key to its byte representation,
    18  	// if this operation is allowed.
    19  	Bytes() ([]byte, error)
    20  
    21  	// SKI returns the subject key identifier of this key.
    22  	SKI() []byte
    23  
    24  	// Symmetric returns true if this key is a symmetric key,
    25  	// false is this key is asymmetric
    26  	Symmetric() bool
    27  
    28  	// Private returns true if this key is a private key,
    29  	// false otherwise.
    30  	Private() bool
    31  
    32  	// PublicKey returns the corresponding public key part of an asymmetric public/private key pair.
    33  	// This method returns an error in symmetric key schemes.
    34  	PublicKey() (Key, error)
    35  }
    36  
    37  // KeyGenOpts contains options for key-generation with a CSP.
    38  type KeyGenOpts interface {
    39  
    40  	// Algorithm returns the key generation algorithm identifier (to be used).
    41  	Algorithm() string
    42  
    43  	// Ephemeral returns true if the key to generate has to be ephemeral,
    44  	// false otherwise.
    45  	Ephemeral() bool
    46  }
    47  
    48  // KeyDerivOpts contains options for key-derivation with a CSP.
    49  type KeyDerivOpts interface {
    50  
    51  	// Algorithm returns the key derivation algorithm identifier (to be used).
    52  	Algorithm() string
    53  
    54  	// Ephemeral returns true if the key to derived has to be ephemeral,
    55  	// false otherwise.
    56  	Ephemeral() bool
    57  }
    58  
    59  // KeyImportOpts contains options for importing the raw material of a key with a CSP.
    60  type KeyImportOpts interface {
    61  
    62  	// Algorithm returns the key importation algorithm identifier (to be used).
    63  	Algorithm() string
    64  
    65  	// Ephemeral returns true if the key generated has to be ephemeral,
    66  	// false otherwise.
    67  	Ephemeral() bool
    68  }
    69  
    70  // HashOpts contains options for hashing with a CSP.
    71  type HashOpts interface {
    72  
    73  	// Algorithm returns the hash algorithm identifier (to be used).
    74  	Algorithm() string
    75  }
    76  
    77  // SignerOpts contains options for signing with a CSP.
    78  type SignerOpts interface {
    79  	crypto.SignerOpts
    80  }
    81  
    82  // EncrypterOpts contains options for encrypting with a CSP.
    83  type EncrypterOpts interface{}
    84  
    85  // DecrypterOpts contains options for decrypting with a CSP.
    86  type DecrypterOpts interface{}
    87  
    88  // BCCSP is the blockchain cryptographic service provider that offers
    89  // the implementation of cryptographic standards and algorithms.
    90  type BCCSP interface {
    91  
    92  	// KeyGen generates a key using opts.
    93  	KeyGen(opts KeyGenOpts) (k Key, err error)
    94  
    95  	// KeyDeriv derives a key from k using opts.
    96  	// The opts argument should be appropriate for the primitive used.
    97  	KeyDeriv(k Key, opts KeyDerivOpts) (dk Key, err error)
    98  
    99  	// KeyImport imports a key from its raw representation using opts.
   100  	// The opts argument should be appropriate for the primitive used.
   101  	KeyImport(raw interface{}, opts KeyImportOpts) (k Key, err error)
   102  
   103  	// GetKey returns the key this CSP associates to
   104  	// the Subject Key Identifier ski.
   105  	GetKey(ski []byte) (k Key, err error)
   106  
   107  	// Hash hashes messages msg using options opts.
   108  	// If opts is nil, the default hash function will be used.
   109  	Hash(msg []byte, opts HashOpts) (hash []byte, err error)
   110  
   111  	// GetHash returns and instance of hash.Hash using options opts.
   112  	// If opts is nil, the default hash function will be returned.
   113  	GetHash(opts HashOpts) (h hash.Hash, err error)
   114  
   115  	// Sign signs digest using key k.
   116  	// The opts argument should be appropriate for the algorithm used.
   117  	//
   118  	// Note that when a signature of a hash of a larger message is needed,
   119  	// the caller is responsible for hashing the larger message and passing
   120  	// the hash (as digest).
   121  	Sign(k Key, digest []byte, opts SignerOpts) (signature []byte, err error)
   122  
   123  	// Verify verifies signature against key k and digest
   124  	// The opts argument should be appropriate for the algorithm used.
   125  	Verify(k Key, signature, digest []byte, opts SignerOpts) (valid bool, err error)
   126  
   127  	// Encrypt encrypts plaintext using key k.
   128  	// The opts argument should be appropriate for the algorithm used.
   129  	Encrypt(k Key, plaintext []byte, opts EncrypterOpts) (ciphertext []byte, err error)
   130  
   131  	// Decrypt decrypts ciphertext using key k.
   132  	// The opts argument should be appropriate for the algorithm used.
   133  	Decrypt(k Key, ciphertext []byte, opts DecrypterOpts) (plaintext []byte, err error)
   134  }