github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/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 // Redistribution and use in source and binary forms, with or without 4 // modification, are permitted provided that the following conditions are 5 // met: 6 // 7 // * Redistributions of source code must retain the above copyright 8 // notice, this list of conditions and the following disclaimer. 9 // * Redistributions in binary form must reproduce the above 10 // copyright notice, this list of conditions and the following disclaimer 11 // in the documentation and/or other materials provided with the 12 // distribution. 13 // * Neither the name of Google Inc. nor the names of its 14 // contributors may be used to endorse or promote products derived from 15 // this software without specific prior written permission. 16 // 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29 package ecies 30 31 // This file contains parameters for ECIES encryption, specifying the 32 // symmetric encryption and HMAC parameters. 33 34 import ( 35 "crypto" 36 "crypto/aes" 37 "crypto/cipher" 38 "crypto/elliptic" 39 "crypto/sha256" 40 "crypto/sha512" 41 "fmt" 42 "hash" 43 44 ethcrypto "github.com/quickchainproject/quickchain/crypto" 45 ) 46 47 var ( 48 DefaultCurve = ethcrypto.S256() 49 ErrUnsupportedECDHAlgorithm = fmt.Errorf("ecies: unsupported ECDH algorithm") 50 ErrUnsupportedECIESParameters = fmt.Errorf("ecies: unsupported ECIES parameters") 51 ) 52 53 type ECIESParams struct { 54 Hash func() hash.Hash // hash function 55 hashAlgo crypto.Hash 56 Cipher func([]byte) (cipher.Block, error) // symmetric cipher 57 BlockSize int // block size of symmetric cipher 58 KeyLen int // length of symmetric key 59 } 60 61 // Standard ECIES parameters: 62 // * ECIES using AES128 and HMAC-SHA-256-16 63 // * ECIES using AES256 and HMAC-SHA-256-32 64 // * ECIES using AES256 and HMAC-SHA-384-48 65 // * ECIES using AES256 and HMAC-SHA-512-64 66 67 var ( 68 ECIES_AES128_SHA256 = &ECIESParams{ 69 Hash: sha256.New, 70 hashAlgo: crypto.SHA256, 71 Cipher: aes.NewCipher, 72 BlockSize: aes.BlockSize, 73 KeyLen: 16, 74 } 75 76 ECIES_AES256_SHA256 = &ECIESParams{ 77 Hash: sha256.New, 78 hashAlgo: crypto.SHA256, 79 Cipher: aes.NewCipher, 80 BlockSize: aes.BlockSize, 81 KeyLen: 32, 82 } 83 84 ECIES_AES256_SHA384 = &ECIESParams{ 85 Hash: sha512.New384, 86 hashAlgo: crypto.SHA384, 87 Cipher: aes.NewCipher, 88 BlockSize: aes.BlockSize, 89 KeyLen: 32, 90 } 91 92 ECIES_AES256_SHA512 = &ECIESParams{ 93 Hash: sha512.New, 94 hashAlgo: crypto.SHA512, 95 Cipher: aes.NewCipher, 96 BlockSize: aes.BlockSize, 97 KeyLen: 32, 98 } 99 ) 100 101 var paramsFromCurve = map[elliptic.Curve]*ECIESParams{ 102 ethcrypto.S256(): ECIES_AES128_SHA256, 103 elliptic.P256(): ECIES_AES128_SHA256, 104 elliptic.P384(): ECIES_AES256_SHA384, 105 elliptic.P521(): ECIES_AES256_SHA512, 106 } 107 108 func AddParamsForCurve(curve elliptic.Curve, params *ECIESParams) { 109 paramsFromCurve[curve] = params 110 } 111 112 // ParamsFromCurve selects parameters optimal for the selected elliptic curve. 113 // Only the curves P256, P384, and P512 are supported. 114 func ParamsFromCurve(curve elliptic.Curve) (params *ECIESParams) { 115 return paramsFromCurve[curve] 116 }