github.com/aswedchain/aswed@v1.0.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 "fmt" 43 "hash" 44 45 ethcrypto "github.com/aswedchain/aswed/crypto" 46 ) 47 48 var ( 49 DefaultCurve = ethcrypto.S256() 50 ErrUnsupportedECDHAlgorithm = fmt.Errorf("ecies: unsupported ECDH algorithm") 51 ErrUnsupportedECIESParameters = fmt.Errorf("ecies: unsupported ECIES parameters") 52 ErrInvalidKeyLen = fmt.Errorf("ecies: invalid key size (> %d) in ECIESParams", maxKeyLen) 53 ) 54 55 // KeyLen is limited to prevent overflow of the counter 56 // in concatKDF. While the theoretical limit is much higher, 57 // no known cipher uses keys larger than 512 bytes. 58 const maxKeyLen = 512 59 60 type ECIESParams struct { 61 Hash func() hash.Hash // hash function 62 hashAlgo crypto.Hash 63 Cipher func([]byte) (cipher.Block, error) // symmetric cipher 64 BlockSize int // block size of symmetric cipher 65 KeyLen int // length of symmetric key 66 } 67 68 // Standard ECIES parameters: 69 // * ECIES using AES128 and HMAC-SHA-256-16 70 // * ECIES using AES256 and HMAC-SHA-256-32 71 // * ECIES using AES256 and HMAC-SHA-384-48 72 // * ECIES using AES256 and HMAC-SHA-512-64 73 74 var ( 75 ECIES_AES128_SHA256 = &ECIESParams{ 76 Hash: sha256.New, 77 hashAlgo: crypto.SHA256, 78 Cipher: aes.NewCipher, 79 BlockSize: aes.BlockSize, 80 KeyLen: 16, 81 } 82 83 ECIES_AES256_SHA256 = &ECIESParams{ 84 Hash: sha256.New, 85 hashAlgo: crypto.SHA256, 86 Cipher: aes.NewCipher, 87 BlockSize: aes.BlockSize, 88 KeyLen: 32, 89 } 90 91 ECIES_AES256_SHA384 = &ECIESParams{ 92 Hash: sha512.New384, 93 hashAlgo: crypto.SHA384, 94 Cipher: aes.NewCipher, 95 BlockSize: aes.BlockSize, 96 KeyLen: 32, 97 } 98 99 ECIES_AES256_SHA512 = &ECIESParams{ 100 Hash: sha512.New, 101 hashAlgo: crypto.SHA512, 102 Cipher: aes.NewCipher, 103 BlockSize: aes.BlockSize, 104 KeyLen: 32, 105 } 106 ) 107 108 var paramsFromCurve = map[elliptic.Curve]*ECIESParams{ 109 ethcrypto.S256(): ECIES_AES128_SHA256, 110 elliptic.P256(): ECIES_AES128_SHA256, 111 elliptic.P384(): ECIES_AES256_SHA384, 112 elliptic.P521(): ECIES_AES256_SHA512, 113 } 114 115 func AddParamsForCurve(curve elliptic.Curve, params *ECIESParams) { 116 paramsFromCurve[curve] = params 117 } 118 119 // ParamsFromCurve selects parameters optimal for the selected elliptic curve. 120 // Only the curves P256, P384, and P512 are supported. 121 func ParamsFromCurve(curve elliptic.Curve) (params *ECIESParams) { 122 return paramsFromCurve[curve] 123 } 124 125 func pubkeyParams(key *PublicKey) (*ECIESParams, error) { 126 params := key.Params 127 if params == nil { 128 if params = ParamsFromCurve(key.Curve); params == nil { 129 return nil, ErrUnsupportedECIESParameters 130 } 131 } 132 if params.KeyLen > maxKeyLen { 133 return nil, ErrInvalidKeyLen 134 } 135 return params, nil 136 }