github.com/inklabsfoundation/inkchain@v0.17.1-0.20181025012015-c3cef8062f19/bccsp/bccsp.go (about)

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