github.com/aaabigfish/gopkg@v1.1.0/crypto/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 crypto 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/ethereum/go-ethereum/crypto" 46 ) 47 48 var ( 49 // TODO ethcrypto.S256() 50 //DefaultCurve = ethcrypto.S256() 51 ErrUnsupportedECDHAlgorithm = fmt.Errorf("ecies: unsupported ECDH algorithm") 52 ErrUnsupportedECIESParameters = fmt.Errorf("ecies: unsupported ECIES parameters") 53 ) 54 55 type ECIESParams struct { 56 Hash func() hash.Hash // hash function 57 hashAlgo crypto.Hash 58 Cipher func([]byte) (cipher.Block, error) // symmetric cipher 59 BlockSize int // block size of symmetric cipher 60 KeyLen int // length of symmetric key 61 } 62 63 // Standard ECIES parameters: 64 // * ECIES using AES128 and HMAC-SHA-256-16 65 // * ECIES using AES256 and HMAC-SHA-256-32 66 // * ECIES using AES256 and HMAC-SHA-384-48 67 // * ECIES using AES256 and HMAC-SHA-512-64 68 69 var ( 70 ECIES_AES128_SHA256 = &ECIESParams{ 71 Hash: sha256.New, 72 hashAlgo: crypto.SHA256, 73 Cipher: aes.NewCipher, 74 BlockSize: aes.BlockSize, 75 KeyLen: 16, 76 } 77 78 ECIES_AES256_SHA256 = &ECIESParams{ 79 Hash: sha256.New, 80 hashAlgo: crypto.SHA256, 81 Cipher: aes.NewCipher, 82 BlockSize: aes.BlockSize, 83 KeyLen: 32, 84 } 85 86 ECIES_AES256_SHA384 = &ECIESParams{ 87 Hash: sha512.New384, 88 hashAlgo: crypto.SHA384, 89 Cipher: aes.NewCipher, 90 BlockSize: aes.BlockSize, 91 KeyLen: 32, 92 } 93 94 ECIES_AES256_SHA512 = &ECIESParams{ 95 Hash: sha512.New, 96 hashAlgo: crypto.SHA512, 97 Cipher: aes.NewCipher, 98 BlockSize: aes.BlockSize, 99 KeyLen: 32, 100 } 101 ) 102 103 var paramsFromCurve = map[elliptic.Curve]*ECIESParams{ 104 //ethcrypto.S256(): ECIES_AES128_SHA256, 105 elliptic.P256(): ECIES_AES128_SHA256, 106 elliptic.P384(): ECIES_AES256_SHA384, 107 elliptic.P521(): ECIES_AES256_SHA512, 108 } 109 110 func AddParamsForCurve(curve elliptic.Curve, params *ECIESParams) { 111 paramsFromCurve[curve] = params 112 } 113 114 // ParamsFromCurve selects parameters optimal for the selected elliptic curve. 115 // Only the curves P256, P384, and P512 are supported. 116 func ParamsFromCurve(curve elliptic.Curve) (params *ECIESParams) { 117 return paramsFromCurve[curve] 118 }