github.com/theQRL/go-zond@v0.1.1/crypto/ecies/params.go (about) 1 // Copyright (c) 2013 Kyle Isom <kyle@tyrfingr.is> 2 // Copyright (c) 2012 The Go Authors. All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above 11 // copyright notice, this list of conditions and the following disclaimer 12 // in the documentation and/or other materials provided with the 13 // distribution. 14 // * Neither the name of Google Inc. nor the names of its 15 // contributors may be used to endorse or promote products derived from 16 // this software without specific prior written permission. 17 // 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 30 package ecies 31 32 // This file contains parameters for ECIES encryption, specifying the 33 // symmetric encryption and HMAC parameters. 34 35 import ( 36 "crypto" 37 "crypto/aes" 38 "crypto/cipher" 39 "crypto/elliptic" 40 "crypto/sha256" 41 "crypto/sha512" 42 "errors" 43 "fmt" 44 "hash" 45 46 ethcrypto "github.com/theQRL/go-zond/crypto" 47 ) 48 49 var ( 50 DefaultCurve = ethcrypto.S256() 51 ErrUnsupportedECDHAlgorithm = errors.New("ecies: unsupported ECDH algorithm") 52 ErrUnsupportedECIESParameters = errors.New("ecies: unsupported ECIES parameters") 53 ErrInvalidKeyLen = fmt.Errorf("ecies: invalid key size (> %d) in ECIESParams", maxKeyLen) 54 ) 55 56 // KeyLen is limited to prevent overflow of the counter 57 // in concatKDF. While the theoretical limit is much higher, 58 // no known cipher uses keys larger than 512 bytes. 59 const maxKeyLen = 512 60 61 type ECIESParams struct { 62 Hash func() hash.Hash // hash function 63 hashAlgo crypto.Hash 64 Cipher func([]byte) (cipher.Block, error) // symmetric cipher 65 BlockSize int // block size of symmetric cipher 66 KeyLen int // length of symmetric key 67 } 68 69 // Standard ECIES parameters: 70 // * ECIES using AES128 and HMAC-SHA-256-16 71 // * ECIES using AES256 and HMAC-SHA-256-32 72 // * ECIES using AES256 and HMAC-SHA-384-48 73 // * ECIES using AES256 and HMAC-SHA-512-64 74 75 var ( 76 ECIES_AES128_SHA256 = &ECIESParams{ 77 Hash: sha256.New, 78 hashAlgo: crypto.SHA256, 79 Cipher: aes.NewCipher, 80 BlockSize: aes.BlockSize, 81 KeyLen: 16, 82 } 83 84 ECIES_AES192_SHA384 = &ECIESParams{ 85 Hash: sha512.New384, 86 hashAlgo: crypto.SHA384, 87 Cipher: aes.NewCipher, 88 BlockSize: aes.BlockSize, 89 KeyLen: 24, 90 } 91 92 ECIES_AES256_SHA256 = &ECIESParams{ 93 Hash: sha256.New, 94 hashAlgo: crypto.SHA256, 95 Cipher: aes.NewCipher, 96 BlockSize: aes.BlockSize, 97 KeyLen: 32, 98 } 99 100 ECIES_AES256_SHA384 = &ECIESParams{ 101 Hash: sha512.New384, 102 hashAlgo: crypto.SHA384, 103 Cipher: aes.NewCipher, 104 BlockSize: aes.BlockSize, 105 KeyLen: 32, 106 } 107 108 ECIES_AES256_SHA512 = &ECIESParams{ 109 Hash: sha512.New, 110 hashAlgo: crypto.SHA512, 111 Cipher: aes.NewCipher, 112 BlockSize: aes.BlockSize, 113 KeyLen: 32, 114 } 115 ) 116 117 var paramsFromCurve = map[elliptic.Curve]*ECIESParams{ 118 ethcrypto.S256(): ECIES_AES128_SHA256, 119 elliptic.P256(): ECIES_AES128_SHA256, 120 elliptic.P384(): ECIES_AES192_SHA384, 121 elliptic.P521(): ECIES_AES256_SHA512, 122 } 123 124 func AddParamsForCurve(curve elliptic.Curve, params *ECIESParams) { 125 paramsFromCurve[curve] = params 126 } 127 128 // ParamsFromCurve selects parameters optimal for the selected elliptic curve. 129 // Only the curves P256, P384, and P512 are supported. 130 func ParamsFromCurve(curve elliptic.Curve) (params *ECIESParams) { 131 return paramsFromCurve[curve] 132 } 133 134 func pubkeyParams(key *PublicKey) (*ECIESParams, error) { 135 params := key.Params 136 if params == nil { 137 if params = ParamsFromCurve(key.Curve); params == nil { 138 return nil, ErrUnsupportedECIESParameters 139 } 140 } 141 if params.KeyLen > maxKeyLen { 142 return nil, ErrInvalidKeyLen 143 } 144 return params, nil 145 }