|
| 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 "crypto" |
| 20 | + |
| 21 | +// Key represents a cryptographic key |
| 22 | +type Key interface { |
| 23 | + |
| 24 | + // Bytes converts this key to its byte representation, |
| 25 | + // if this operation is allowed. |
| 26 | + Bytes() ([]byte, error) |
| 27 | + |
| 28 | + // SKI returns the subject key identifier of this key. |
| 29 | + SKI() []byte |
| 30 | + |
| 31 | + // Symmetric returns true if this key is a symmetric key, |
| 32 | + // false is this key is asymmetric |
| 33 | + Symmetric() bool |
| 34 | + |
| 35 | + // Private returns true if this key is an asymmetric private key, |
| 36 | + // false otherwise. |
| 37 | + Private() bool |
| 38 | + |
| 39 | + // PublicKey returns the corresponding public key if this key |
| 40 | + // is an asymmetric private key. If this key is already public, |
| 41 | + // PublicKey returns this key itself. |
| 42 | + PublicKey() (Key, error) |
| 43 | +} |
| 44 | + |
| 45 | +// KeyGenOpts contains options for key-generation with a CSP. |
| 46 | +type KeyGenOpts interface { |
| 47 | + |
| 48 | + // Algorithm returns an identifier for the algorithm to be used |
| 49 | + // to generate a key. |
| 50 | + Algorithm() string |
| 51 | + |
| 52 | + // Ephemeral returns true if the key to generate has to be ephemeral, |
| 53 | + // false otherwise. |
| 54 | + Ephemeral() bool |
| 55 | +} |
| 56 | + |
| 57 | +// KeyDerivOpts contains options for key-derivation with a CSP. |
| 58 | +type KeyDerivOpts interface { |
| 59 | + |
| 60 | + // Algorithm returns an identifier for the algorithm to be used |
| 61 | + // to derive a key. |
| 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 | + // Algorithm returns an identifier for the algorithm to be used |
| 72 | + // to import the raw material of a key. |
| 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 | + // Algorithm returns an identifier for the algorithm to be used |
| 83 | + // to hash. |
| 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 []byte, 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 | + Hash(msg []byte, opts HashOpts) (hash []byte, err error) |
| 119 | + |
| 120 | + // Sign signs digest using key k. |
| 121 | + // The opts argument should be appropriate for the algorithm used. |
| 122 | + // |
| 123 | + // Note that when a signature of a hash of a larger message is needed, |
| 124 | + // the caller is responsible for hashing the larger message and passing |
| 125 | + // the hash (as digest). |
| 126 | + Sign(k Key, digest []byte, opts SignerOpts) (signature []byte, err error) |
| 127 | + |
| 128 | + // Verify verifies signature against key k and digest |
| 129 | + // The opts argument should be appropriate for the algorithm used. |
| 130 | + Verify(k Key, signature, digest []byte) (valid bool, err error) |
| 131 | + |
| 132 | + // Encrypt encrypts plaintext using key k. |
| 133 | + // The opts argument should be appropriate for the algorithm used. |
| 134 | + Encrypt(k Key, plaintext []byte, opts EncrypterOpts) (ciphertext []byte, err error) |
| 135 | + |
| 136 | + // Decrypt decrypts ciphertext using key k. |
| 137 | + // The opts argument should be appropriate for the algorithm used. |
| 138 | + Decrypt(k Key, ciphertext []byte, opts DecrypterOpts) (plaintext []byte, err error) |
| 139 | +} |
0 commit comments