From 6cf6981ed076d43514e86fa7e7a56c6bad1da583 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 10 Dec 2014 00:00:52 +0100 Subject: [PATCH 01/18] init --- .gitignore | 24 +++ LICENSE | 28 +++ README | 94 +++++++++ asn1.go | 556 ++++++++++++++++++++++++++++++++++++++++++++++++++ ecies.go | 326 +++++++++++++++++++++++++++++ ecies_test.go | 489 ++++++++++++++++++++++++++++++++++++++++++++ params.go | 187 +++++++++++++++++ 7 files changed, 1704 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README create mode 100644 asn1.go create mode 100644 ecies.go create mode 100644 ecies_test.go create mode 100644 params.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..802b6744a1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,24 @@ +# Compiled Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so + +# Folders +_obj +_test + +# Architecture specific extensions/prefixes +*.[568vq] +[568vq].out + +*.cgo1.go +*.cgo2.c +_cgo_defun.c +_cgo_gotypes.go +_cgo_export.* + +_testmain.go + +*.exe + +*~ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000..e1ed19a279 --- /dev/null +++ b/LICENSE @@ -0,0 +1,28 @@ +Copyright (c) 2013 Kyle Isom +Copyright (c) 2012 The Go Authors. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google Inc. nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README b/README new file mode 100644 index 0000000000..7ccf27a27f --- /dev/null +++ b/README @@ -0,0 +1,94 @@ +# NOTE + +This implementation is direct fork of Kylom's implementation. I claim no authorship over this code apart from some minor modifications. +Please be aware this code **has not yet been reviewed**. + +ecies implements the Elliptic Curve Integrated Encryption Scheme. + +The package is designed to be compliant with the appropriate NIST +standards, and therefore doesn't support the full SEC 1 algorithm set. + + +STATUS: + +ecies should be ready for use. The ASN.1 support is only complete so +far as to supported the listed algorithms before. + + +CAVEATS + +1. CMAC support is currently not present. + + +SUPPORTED ALGORITHMS + + SYMMETRIC CIPHERS HASH FUNCTIONS + AES128 SHA-1 + AES192 SHA-224 + AES256 SHA-256 + SHA-384 + ELLIPTIC CURVE SHA-512 + P256 + P384 KEY DERIVATION FUNCTION + P521 NIST SP 800-65a Concatenation KDF + +Curve P224 isn't supported because it does not provide a minimum security +level of AES128 with HMAC-SHA1. According to NIST SP 800-57, the security +level of P224 is 112 bits of security. Symmetric ciphers use CTR-mode; +message tags are computed using HMAC- function. + + +CURVE SELECTION + +According to NIST SP 800-57, the following curves should be selected: + + +----------------+-------+ + | SYMMETRIC SIZE | CURVE | + +----------------+-------+ + | 128-bit | P256 | + +----------------+-------+ + | 192-bit | P384 | + +----------------+-------+ + | 256-bit | P521 | + +----------------+-------+ + + +TODO + +1. Look at serialising the parameters with the SEC 1 ASN.1 module. +2. Validate ASN.1 formats with SEC 1. + + +TEST VECTORS + +The only test vectors I've found so far date from 1993, predating AES +and including only 163-bit curves. Therefore, there are no published +test vectors to compare to. + + +LICENSE + +ecies is released under the same license as the Go source code. See the +LICENSE file for details. + + +REFERENCES + +* SEC (Standard for Efficient Cryptography) 1, version 2.0: Elliptic + Curve Cryptography; Certicom, May 2009. + http://www.secg.org/download/aid-780/sec1-v2.pdf +* GEC (Guidelines for Efficient Cryptography) 2, version 0.3: Test + Vectors for SEC 1; Certicom, September 1999. + http://www.secg.org/collateral/gec2.pdf +* NIST SP 800-56a: Recommendation for Pair-Wise Key Establishment Schemes + Using Discrete Logarithm Cryptography. National Institute of Standards + and Technology, May 2007. + http://csrc.nist.gov/publications/nistpubs/800-56A/SP800-56A_Revision1_Mar08-2007.pdf +* Suite B Implementer’s Guide to NIST SP 800-56A. National Security + Agency, July 28, 2009. + http://www.nsa.gov/ia/_files/SuiteB_Implementer_G-113808.pdf +* NIST SP 800-57: Recommendation for Key Management – Part 1: General + (Revision 3). National Institute of Standards and Technology, July + 2012. + http://csrc.nist.gov/publications/nistpubs/800-57/sp800-57_part1_rev3_general.pdf + diff --git a/asn1.go b/asn1.go new file mode 100644 index 0000000000..3ef194ea02 --- /dev/null +++ b/asn1.go @@ -0,0 +1,556 @@ +package ecies + +import ( + "bytes" + "crypto" + "crypto/elliptic" + "crypto/sha1" + "crypto/sha256" + "crypto/sha512" + "encoding/asn1" + "encoding/pem" + "fmt" + "hash" + "math/big" +) + +var ( + secgScheme = []int{1, 3, 132, 1} + shaScheme = []int{2, 16, 840, 1, 101, 3, 4, 2} + ansiX962Scheme = []int{1, 2, 840, 10045} + x963Scheme = []int{1, 2, 840, 63, 0} +) + +var ErrInvalidPrivateKey = fmt.Errorf("ecies: invalid private key") + +func doScheme(base, v []int) asn1.ObjectIdentifier { + var oidInts asn1.ObjectIdentifier + oidInts = append(oidInts, base...) + return append(oidInts, v...) +} + +// curve OID code taken from crypto/x509, including +// - oidNameCurve* +// - namedCurveFromOID +// - oidFromNamedCurve +// RFC 5480, 2.1.1.1. Named Curve +// +// secp224r1 OBJECT IDENTIFIER ::= { +// iso(1) identified-organization(3) certicom(132) curve(0) 33 } +// +// secp256r1 OBJECT IDENTIFIER ::= { +// iso(1) member-body(2) us(840) ansi-X9-62(10045) curves(3) +// prime(1) 7 } +// +// secp384r1 OBJECT IDENTIFIER ::= { +// iso(1) identified-organization(3) certicom(132) curve(0) 34 } +// +// secp521r1 OBJECT IDENTIFIER ::= { +// iso(1) identified-organization(3) certicom(132) curve(0) 35 } +// +// NB: secp256r1 is equivalent to prime256v1 +type secgNamedCurve asn1.ObjectIdentifier + +var ( + secgNamedCurveP224 = secgNamedCurve{1, 3, 132, 0, 33} + secgNamedCurveP256 = secgNamedCurve{1, 2, 840, 10045, 3, 1, 7} + secgNamedCurveP384 = secgNamedCurve{1, 3, 132, 0, 34} + secgNamedCurveP521 = secgNamedCurve{1, 3, 132, 0, 35} + rawCurveP224 = []byte{6, 5, 4, 3, 1, 2, 9, 4, 0, 3, 3} + rawCurveP256 = []byte{6, 8, 4, 2, 1, 3, 4, 7, 2, 2, 0, 6, 6, 1, 3, 1, 7} + rawCurveP384 = []byte{6, 5, 4, 3, 1, 2, 9, 4, 0, 3, 4} + rawCurveP521 = []byte{6, 5, 4, 3, 1, 2, 9, 4, 0, 3, 5} +) + +func rawCurve(curve elliptic.Curve) []byte { + switch curve { + case elliptic.P224(): + return rawCurveP224 + case elliptic.P256(): + return rawCurveP256 + case elliptic.P384(): + return rawCurveP384 + case elliptic.P521(): + return rawCurveP521 + default: + return nil + } +} + +func (curve secgNamedCurve) Equal(curve2 secgNamedCurve) bool { + if len(curve) != len(curve2) { + return false + } + for i, _ := range curve { + if curve[i] != curve2[i] { + return false + } + } + return true +} + +func namedCurveFromOID(curve secgNamedCurve) elliptic.Curve { + switch { + case curve.Equal(secgNamedCurveP224): + return elliptic.P224() + case curve.Equal(secgNamedCurveP256): + return elliptic.P256() + case curve.Equal(secgNamedCurveP384): + return elliptic.P384() + case curve.Equal(secgNamedCurveP521): + return elliptic.P521() + } + return nil +} + +func oidFromNamedCurve(curve elliptic.Curve) (secgNamedCurve, bool) { + switch curve { + case elliptic.P224(): + return secgNamedCurveP224, true + case elliptic.P256(): + return secgNamedCurveP256, true + case elliptic.P384(): + return secgNamedCurveP384, true + case elliptic.P521(): + return secgNamedCurveP521, true + } + + return nil, false +} + +// asnAlgorithmIdentifier represents the ASN.1 structure of the same name. See RFC +// 5280, section 4.1.1.2. +type asnAlgorithmIdentifier struct { + Algorithm asn1.ObjectIdentifier + Parameters asn1.RawValue `asn1:"optional"` +} + +func (a asnAlgorithmIdentifier) Cmp(b asnAlgorithmIdentifier) bool { + if len(a.Algorithm) != len(b.Algorithm) { + return false + } + for i, _ := range a.Algorithm { + if a.Algorithm[i] != b.Algorithm[i] { + return false + } + } + return true +} + +type asnHashFunction asnAlgorithmIdentifier + +var ( + oidSHA1 = asn1.ObjectIdentifier{1, 3, 14, 3, 2, 26} + oidSHA224 = doScheme(shaScheme, []int{4}) + oidSHA256 = doScheme(shaScheme, []int{1}) + oidSHA384 = doScheme(shaScheme, []int{2}) + oidSHA512 = doScheme(shaScheme, []int{3}) +) + +func hashFromOID(oid asn1.ObjectIdentifier) func() hash.Hash { + switch { + case oid.Equal(oidSHA1): + return sha1.New + case oid.Equal(oidSHA224): + return sha256.New224 + case oid.Equal(oidSHA256): + return sha256.New + case oid.Equal(oidSHA384): + return sha512.New384 + case oid.Equal(oidSHA512): + return sha512.New + } + return nil +} + +func oidFromHash(hash crypto.Hash) (asn1.ObjectIdentifier, bool) { + switch hash { + case crypto.SHA1: + return oidSHA1, true + case crypto.SHA224: + return oidSHA224, true + case crypto.SHA256: + return oidSHA256, true + case crypto.SHA384: + return oidSHA384, true + case crypto.SHA512: + return oidSHA512, true + default: + return nil, false + } +} + +var ( + asnAlgoSHA1 = asnHashFunction{ + Algorithm: oidSHA1, + } + asnAlgoSHA224 = asnHashFunction{ + Algorithm: oidSHA224, + } + asnAlgoSHA256 = asnHashFunction{ + Algorithm: oidSHA256, + } + asnAlgoSHA384 = asnHashFunction{ + Algorithm: oidSHA384, + } + asnAlgoSHA512 = asnHashFunction{ + Algorithm: oidSHA512, + } +) + +// type ASNasnSubjectPublicKeyInfo struct { +// +// } +// + +type asnSubjectPublicKeyInfo struct { + Algorithm asn1.ObjectIdentifier + PublicKey asn1.BitString + Supplements ecpksSupplements `asn1:"optional"` +} + +type asnECPKAlgorithms struct { + Type asn1.ObjectIdentifier +} + +var idPublicKeyType = doScheme(ansiX962Scheme, []int{2}) +var idEcPublicKey = doScheme(idPublicKeyType, []int{1}) +var idEcPublicKeySupplemented = doScheme(idPublicKeyType, []int{0}) + +func curveToRaw(curve elliptic.Curve) (rv asn1.RawValue, ok bool) { + switch curve { + case elliptic.P224(), elliptic.P256(), elliptic.P384(), elliptic.P521(): + raw := rawCurve(curve) + return asn1.RawValue{ + Tag: 30, + Bytes: raw[2:], + FullBytes: raw, + }, true + default: + return rv, false + } +} + +func asnECPublicKeyType(curve elliptic.Curve) (algo asnAlgorithmIdentifier, ok bool) { + raw, ok := curveToRaw(curve) + if !ok { + return + } else { + return asnAlgorithmIdentifier{Algorithm: idEcPublicKey, + Parameters: raw}, true + } +} + +type asnECPrivKeyVer int + +var asnECPrivKeyVer1 asnECPrivKeyVer = 1 + +type asnPrivateKey struct { + Version asnECPrivKeyVer + Private []byte + Curve secgNamedCurve `asn1:"optional"` + Public asn1.BitString +} + +var asnECDH = doScheme(secgScheme, []int{12}) + +type asnECDHAlgorithm asnAlgorithmIdentifier + +var ( + dhSinglePass_stdDH_sha1kdf = asnECDHAlgorithm{ + Algorithm: doScheme(x963Scheme, []int{2}), + } + dhSinglePass_stdDH_sha256kdf = asnECDHAlgorithm{ + Algorithm: doScheme(secgScheme, []int{11, 1}), + } + dhSinglePass_stdDH_sha384kdf = asnECDHAlgorithm{ + Algorithm: doScheme(secgScheme, []int{11, 2}), + } + dhSinglePass_stdDH_sha224kdf = asnECDHAlgorithm{ + Algorithm: doScheme(secgScheme, []int{11, 0}), + } + dhSinglePass_stdDH_sha512kdf = asnECDHAlgorithm{ + Algorithm: doScheme(secgScheme, []int{11, 3}), + } +) + +func (a asnECDHAlgorithm) Cmp(b asnECDHAlgorithm) bool { + if len(a.Algorithm) != len(b.Algorithm) { + return false + } + for i, _ := range a.Algorithm { + if a.Algorithm[i] != b.Algorithm[i] { + return false + } + } + return true +} + +// asnNISTConcatenation is the only supported KDF at this time. +type asnKeyDerivationFunction asnAlgorithmIdentifier + +var asnNISTConcatenationKDF = asnKeyDerivationFunction{ + Algorithm: doScheme(secgScheme, []int{17, 1}), +} + +func (a asnKeyDerivationFunction) Cmp(b asnKeyDerivationFunction) bool { + if len(a.Algorithm) != len(b.Algorithm) { + return false + } + for i, _ := range a.Algorithm { + if a.Algorithm[i] != b.Algorithm[i] { + return false + } + } + return true +} + +var eciesRecommendedParameters = doScheme(secgScheme, []int{7}) +var eciesSpecifiedParameters = doScheme(secgScheme, []int{8}) + +type asnECIESParameters struct { + KDF asnKeyDerivationFunction `asn1:"optional"` + Sym asnSymmetricEncryption `asn1:"optional"` + MAC asnMessageAuthenticationCode `asn1:"optional"` +} + +type asnSymmetricEncryption asnAlgorithmIdentifier + +var ( + aes128CTRinECIES = asnSymmetricEncryption{ + Algorithm: doScheme(secgScheme, []int{21, 0}), + } + aes192CTRinECIES = asnSymmetricEncryption{ + Algorithm: doScheme(secgScheme, []int{21, 1}), + } + aes256CTRinECIES = asnSymmetricEncryption{ + Algorithm: doScheme(secgScheme, []int{21, 2}), + } +) + +func (a asnSymmetricEncryption) Cmp(b asnSymmetricEncryption) bool { + if len(a.Algorithm) != len(b.Algorithm) { + return false + } + for i, _ := range a.Algorithm { + if a.Algorithm[i] != b.Algorithm[i] { + return false + } + } + return true +} + +type asnMessageAuthenticationCode asnAlgorithmIdentifier + +var ( + hmacFull = asnMessageAuthenticationCode{ + Algorithm: doScheme(secgScheme, []int{22}), + } +) + +func (a asnMessageAuthenticationCode) Cmp(b asnMessageAuthenticationCode) bool { + if len(a.Algorithm) != len(b.Algorithm) { + return false + } + for i, _ := range a.Algorithm { + if a.Algorithm[i] != b.Algorithm[i] { + return false + } + } + return true +} + +type ecpksSupplements struct { + ECDomain secgNamedCurve + ECCAlgorithms eccAlgorithmSet +} + +type eccAlgorithmSet struct { + ECDH asnECDHAlgorithm `asn1:"optional"` + ECIES asnECIESParameters `asn1:"optional"` +} + +func marshalSubjectPublicKeyInfo(pub *PublicKey) (subj asnSubjectPublicKeyInfo, err error) { + subj.Algorithm = idEcPublicKeySupplemented + curve, ok := oidFromNamedCurve(pub.Curve) + if !ok { + err = ErrInvalidPublicKey + return + } + subj.Supplements.ECDomain = curve + if pub.Params != nil { + subj.Supplements.ECCAlgorithms.ECDH = paramsToASNECDH(pub.Params) + subj.Supplements.ECCAlgorithms.ECIES = paramsToASNECIES(pub.Params) + } + pubkey := elliptic.Marshal(pub.Curve, pub.X, pub.Y) + subj.PublicKey = asn1.BitString{ + BitLength: len(pubkey) * 8, + Bytes: pubkey, + } + return +} + +// Encode a public key to DER format. +func MarshalPublic(pub *PublicKey) ([]byte, error) { + subj, err := marshalSubjectPublicKeyInfo(pub) + if err != nil { + return nil, err + } + return asn1.Marshal(subj) +} + +// Decode a DER-encoded public key. +func UnmarshalPublic(in []byte) (pub *PublicKey, err error) { + var subj asnSubjectPublicKeyInfo + + if _, err = asn1.Unmarshal(in, &subj); err != nil { + return + } + if !subj.Algorithm.Equal(idEcPublicKeySupplemented) { + err = ErrInvalidPublicKey + return + } + pub = new(PublicKey) + pub.Curve = namedCurveFromOID(subj.Supplements.ECDomain) + x, y := elliptic.Unmarshal(pub.Curve, subj.PublicKey.Bytes) + if x == nil { + err = ErrInvalidPublicKey + return + } + pub.X = x + pub.Y = y + pub.Params = new(ECIESParams) + asnECIEStoParams(subj.Supplements.ECCAlgorithms.ECIES, pub.Params) + asnECDHtoParams(subj.Supplements.ECCAlgorithms.ECDH, pub.Params) + if pub.Params == nil { + if pub.Params = ParamsFromCurve(pub.Curve); pub.Params == nil { + err = ErrInvalidPublicKey + } + } + return +} + +func marshalPrivateKey(prv *PrivateKey) (ecprv asnPrivateKey, err error) { + ecprv.Version = asnECPrivKeyVer1 + ecprv.Private = prv.D.Bytes() + + var ok bool + ecprv.Curve, ok = oidFromNamedCurve(prv.PublicKey.Curve) + if !ok { + err = ErrInvalidPrivateKey + return + } + + var pub []byte + if pub, err = MarshalPublic(&prv.PublicKey); err != nil { + return + } else { + ecprv.Public = asn1.BitString{ + BitLength: len(pub) * 8, + Bytes: pub, + } + } + return +} + +// Encode a private key to DER format. +func MarshalPrivate(prv *PrivateKey) ([]byte, error) { + ecprv, err := marshalPrivateKey(prv) + if err != nil { + return nil, err + } + return asn1.Marshal(ecprv) +} + +// Decode a private key from a DER-encoded format. +func UnmarshalPrivate(in []byte) (prv *PrivateKey, err error) { + var ecprv asnPrivateKey + + if _, err = asn1.Unmarshal(in, &ecprv); err != nil { + return + } else if ecprv.Version != asnECPrivKeyVer1 { + err = ErrInvalidPrivateKey + return + } + + privateCurve := namedCurveFromOID(ecprv.Curve) + if privateCurve == nil { + err = ErrInvalidPrivateKey + return + } + + prv = new(PrivateKey) + prv.D = new(big.Int).SetBytes(ecprv.Private) + + if pub, err := UnmarshalPublic(ecprv.Public.Bytes); err != nil { + return nil, err + } else { + prv.PublicKey = *pub + } + + return +} + +// Export a public key to PEM format. +func ExportPublicPEM(pub *PublicKey) (out []byte, err error) { + der, err := MarshalPublic(pub) + if err != nil { + return + } + + var block pem.Block + block.Type = "ELLIPTIC CURVE PUBLIC KEY" + block.Bytes = der + + buf := new(bytes.Buffer) + err = pem.Encode(buf, &block) + if err != nil { + return + } else { + out = buf.Bytes() + } + return +} + +// Export a private key to PEM format. +func ExportPrivatePEM(prv *PrivateKey) (out []byte, err error) { + der, err := MarshalPrivate(prv) + if err != nil { + return + } + + var block pem.Block + block.Type = "ELLIPTIC CURVE PRIVATE KEY" + block.Bytes = der + + buf := new(bytes.Buffer) + err = pem.Encode(buf, &block) + if err != nil { + return + } else { + out = buf.Bytes() + } + return +} + +// Import a PEM-encoded public key. +func ImportPublicPEM(in []byte) (pub *PublicKey, err error) { + p, _ := pem.Decode(in) + if p == nil || p.Type != "ELLIPTIC CURVE PUBLIC KEY" { + return nil, ErrInvalidPublicKey + } + + pub, err = UnmarshalPublic(p.Bytes) + return +} + +// Import a PEM-encoded private key. +func ImportPrivatePEM(in []byte) (prv *PrivateKey, err error) { + p, _ := pem.Decode(in) + if p == nil || p.Type != "ELLIPTIC CURVE PRIVATE KEY" { + return nil, ErrInvalidPrivateKey + } + + prv, err = UnmarshalPrivate(p.Bytes) + return +} diff --git a/ecies.go b/ecies.go new file mode 100644 index 0000000000..0e2403d471 --- /dev/null +++ b/ecies.go @@ -0,0 +1,326 @@ +package ecies + +import ( + "crypto/cipher" + "crypto/ecdsa" + "crypto/elliptic" + "crypto/hmac" + "crypto/subtle" + "fmt" + "hash" + "io" + "math/big" +) + +var ( + ErrImport = fmt.Errorf("ecies: failed to import key") + ErrInvalidCurve = fmt.Errorf("ecies: invalid elliptic curve") + ErrInvalidParams = fmt.Errorf("ecies: invalid ECIES parameters") + ErrInvalidPublicKey = fmt.Errorf("ecies: invalid public key") + ErrSharedKeyTooBig = fmt.Errorf("ecies: shared key is too big") +) + +// PublicKey is a representation of an elliptic curve public key. +type PublicKey struct { + X *big.Int + Y *big.Int + elliptic.Curve + Params *ECIESParams +} + +// Export an ECIES public key as an ECDSA public key. +func (pub *PublicKey) ExportECDSA() *ecdsa.PublicKey { + return &ecdsa.PublicKey{pub.Curve, pub.X, pub.Y} +} + +// Import an ECDSA public key as an ECIES public key. +func ImportECDSAPublic(pub *ecdsa.PublicKey) *PublicKey { + return &PublicKey{ + X: pub.X, + Y: pub.Y, + Curve: pub.Curve, + Params: ParamsFromCurve(pub.Curve), + } +} + +// PrivateKey is a representation of an elliptic curve private key. +type PrivateKey struct { + PublicKey + D *big.Int +} + +// Export an ECIES private key as an ECDSA private key. +func (prv *PrivateKey) ExportECDSA() *ecdsa.PrivateKey { + pub := &prv.PublicKey + pubECDSA := pub.ExportECDSA() + return &ecdsa.PrivateKey{*pubECDSA, prv.D} +} + +// Import an ECDSA private key as an ECIES private key. +func ImportECDSA(prv *ecdsa.PrivateKey) *PrivateKey { + pub := ImportECDSAPublic(&prv.PublicKey) + return &PrivateKey{*pub, prv.D} +} + +// Generate an elliptic curve public / private keypair. If params is nil, +// the recommended default paramters for the key will be chosen. +func GenerateKey(rand io.Reader, curve elliptic.Curve, params *ECIESParams) (prv *PrivateKey, err error) { + pb, x, y, err := elliptic.GenerateKey(curve, rand) + if err != nil { + return + } + prv = new(PrivateKey) + prv.PublicKey.X = x + prv.PublicKey.Y = y + prv.PublicKey.Curve = curve + prv.D = new(big.Int).SetBytes(pb) + if params == nil { + params = ParamsFromCurve(curve) + } + prv.PublicKey.Params = params + return +} + +// MaxSharedKeyLength returns the maximum length of the shared key the +// public key can produce. +func MaxSharedKeyLength(pub *PublicKey) int { + return (pub.Curve.Params().BitSize + 7) / 8 +} + +// ECDH key agreement method used to establish secret keys for encryption. +func (prv *PrivateKey) GenerateShared(pub *PublicKey, skLen, macLen int) (sk []byte, err error) { + if prv.PublicKey.Curve != pub.Curve { + err = ErrInvalidCurve + return + } + x, _ := pub.Curve.ScalarMult(pub.X, pub.Y, prv.D.Bytes()) + if x == nil || (x.BitLen()+7)/8 < (skLen+macLen) { + err = ErrSharedKeyTooBig + return + } + sk = x.Bytes()[:skLen+macLen] + return +} + +var ( + ErrKeyDataTooLong = fmt.Errorf("ecies: can't supply requested key data") + ErrSharedTooLong = fmt.Errorf("ecies: shared secret is too long") + ErrInvalidMessage = fmt.Errorf("ecies: invalid message") +) + +var ( + big2To32 = new(big.Int).Exp(big.NewInt(2), big.NewInt(32), nil) + big2To32M1 = new(big.Int).Sub(big2To32, big.NewInt(1)) +) + +func incCounter(ctr []byte) { + if ctr[3]++; ctr[3] != 0 { + return + } else if ctr[2]++; ctr[2] != 0 { + return + } else if ctr[1]++; ctr[1] != 0 { + return + } else if ctr[0]++; ctr[0] != 0 { + return + } + return +} + +// NIST SP 800-56 Concatenation Key Derivation Function (see section 5.8.1). +func concatKDF(hash hash.Hash, z, s1 []byte, kdLen int) (k []byte, err error) { + if s1 == nil { + s1 = make([]byte, 0) + } + + reps := ((kdLen + 7) * 8) / (hash.BlockSize() * 8) + if big.NewInt(int64(reps)).Cmp(big2To32M1) > 0 { + fmt.Println(big2To32M1) + return nil, ErrKeyDataTooLong + } + + counter := []byte{0, 0, 0, 1} + k = make([]byte, 0) + + for i := 0; i <= reps; i++ { + hash.Write(counter) + hash.Write(z) + hash.Write(s1) + k = append(k, hash.Sum(nil)...) + hash.Reset() + incCounter(counter) + } + + k = k[:kdLen] + return +} + +// messageTag computes the MAC of a message (called the tag) as per +// SEC 1, 3.5. +func messageTag(hash func() hash.Hash, km, msg, shared []byte) []byte { + if shared == nil { + shared = make([]byte, 0) + } + mac := hmac.New(hash, km) + mac.Write(msg) + tag := mac.Sum(nil) + return tag +} + +// Generate an initialisation vector for CTR mode. +func generateIV(params *ECIESParams, rand io.Reader) (iv []byte, err error) { + iv = make([]byte, params.BlockSize) + _, err = io.ReadFull(rand, iv) + return +} + +// symEncrypt carries out CTR encryption using the block cipher specified in the +// parameters. +func symEncrypt(rand io.Reader, params *ECIESParams, key, m []byte) (ct []byte, err error) { + c, err := params.Cipher(key) + if err != nil { + return + } + + iv, err := generateIV(params, rand) + if err != nil { + return + } + ctr := cipher.NewCTR(c, iv) + + ct = make([]byte, len(m)+params.BlockSize) + copy(ct, iv) + ctr.XORKeyStream(ct[params.BlockSize:], m) + return +} + +// symDecrypt carries out CTR decryption using the block cipher specified in +// the parameters +func symDecrypt(rand io.Reader, params *ECIESParams, key, ct []byte) (m []byte, err error) { + c, err := params.Cipher(key) + if err != nil { + return + } + + ctr := cipher.NewCTR(c, ct[:params.BlockSize]) + + m = make([]byte, len(ct)-params.BlockSize) + ctr.XORKeyStream(m, ct[params.BlockSize:]) + return +} + +// Encrypt encrypts a message using ECIES as specified in SEC 1, 5.1. If +// the shared information parameters aren't being used, they should be +// nil. +func Encrypt(rand io.Reader, pub *PublicKey, m, s1, s2 []byte) (ct []byte, err error) { + params := pub.Params + if params == nil { + if params = ParamsFromCurve(pub.Curve); params == nil { + err = ErrUnsupportedECIESParameters + return + } + } + R, err := GenerateKey(rand, pub.Curve, params) + if err != nil { + return + } + + hash := params.Hash() + z, err := R.GenerateShared(pub, params.KeyLen, params.KeyLen) + if err != nil { + return + } + K, err := concatKDF(hash, z, s1, params.KeyLen+params.KeyLen) + if err != nil { + return + } + Ke := K[:params.KeyLen] + Km := K[params.KeyLen:] + hash.Write(Km) + Km = hash.Sum(nil) + hash.Reset() + + em, err := symEncrypt(rand, params, Ke, m) + if err != nil || len(em) <= params.BlockSize { + return + } + + d := messageTag(params.Hash, Km, em, s2) + + Rb := elliptic.Marshal(pub.Curve, R.PublicKey.X, R.PublicKey.Y) + ct = make([]byte, len(Rb)+len(em)+len(d)) + copy(ct, Rb) + copy(ct[len(Rb):], em) + copy(ct[len(Rb)+len(em):], d) + return +} + +// Decrypt decrypts an ECIES ciphertext. +func (prv *PrivateKey) Decrypt(rand io.Reader, c, s1, s2 []byte) (m []byte, err error) { + if c == nil || len(c) == 0 { + err = ErrInvalidMessage + return + } + params := prv.PublicKey.Params + if params == nil { + if params = ParamsFromCurve(prv.PublicKey.Curve); params == nil { + err = ErrUnsupportedECIESParameters + return + } + } + hash := params.Hash() + + var ( + rLen int + hLen int = hash.Size() + mStart int + mEnd int + ) + + switch c[0] { + case 2, 3, 4: + rLen = ((prv.PublicKey.Curve.Params().BitSize + 7) / 4) + if len(c) < (rLen + hLen + 1) { + err = ErrInvalidMessage + return + } + default: + err = ErrInvalidPublicKey + return + } + + mStart = rLen + mEnd = len(c) - hLen + + R := new(PublicKey) + R.Curve = prv.PublicKey.Curve + R.X, R.Y = elliptic.Unmarshal(R.Curve, c[:rLen]) + if R.X == nil { + err = ErrInvalidPublicKey + return + } + + z, err := prv.GenerateShared(R, params.KeyLen, params.KeyLen) + if err != nil { + return + } + + K, err := concatKDF(hash, z, s1, params.KeyLen+params.KeyLen) + if err != nil { + return + } + + Ke := K[:params.KeyLen] + Km := K[params.KeyLen:] + hash.Write(Km) + Km = hash.Sum(nil) + hash.Reset() + + d := messageTag(params.Hash, Km, c[mStart:mEnd], s2) + if subtle.ConstantTimeCompare(c[mEnd:], d) != 1 { + err = ErrInvalidMessage + return + } + + m, err = symDecrypt(rand, params, Ke, c[mStart:mEnd]) + return +} diff --git a/ecies_test.go b/ecies_test.go new file mode 100644 index 0000000000..943e4488ef --- /dev/null +++ b/ecies_test.go @@ -0,0 +1,489 @@ +package ecies + +import ( + "bytes" + "crypto/elliptic" + "crypto/rand" + "crypto/sha256" + "flag" + "fmt" + "io/ioutil" + "testing" +) + +var dumpEnc bool + +func init() { + flDump := flag.Bool("dump", false, "write encrypted test message to file") + flag.Parse() + dumpEnc = *flDump +} + +// Ensure the KDF generates appropriately sized keys. +func TestKDF(t *testing.T) { + msg := []byte("Hello, world") + h := sha256.New() + + k, err := concatKDF(h, msg, nil, 64) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } + if len(k) != 64 { + fmt.Printf("KDF: generated key is the wrong size (%d instead of 64\n", + len(k)) + t.FailNow() + } +} + +var skLen int +var ErrBadSharedKeys = fmt.Errorf("ecies: shared keys don't match") + +// cmpParams compares a set of ECIES parameters. We assume, as per the +// docs, that AES is the only supported symmetric encryption algorithm. +func cmpParams(p1, p2 *ECIESParams) bool { + if p1.hashAlgo != p2.hashAlgo { + return false + } else if p1.KeyLen != p2.KeyLen { + return false + } else if p1.BlockSize != p2.BlockSize { + return false + } + return true +} + +// cmpPublic returns true if the two public keys represent the same pojnt. +func cmpPublic(pub1, pub2 PublicKey) bool { + if pub1.X == nil || pub1.Y == nil { + fmt.Println(ErrInvalidPublicKey.Error()) + return false + } + if pub2.X == nil || pub2.Y == nil { + fmt.Println(ErrInvalidPublicKey.Error()) + return false + } + pub1Out := elliptic.Marshal(pub1.Curve, pub1.X, pub1.Y) + pub2Out := elliptic.Marshal(pub2.Curve, pub2.X, pub2.Y) + + return bytes.Equal(pub1Out, pub2Out) +} + +// cmpPrivate returns true if the two private keys are the same. +func cmpPrivate(prv1, prv2 *PrivateKey) bool { + if prv1 == nil || prv1.D == nil { + return false + } else if prv2 == nil || prv2.D == nil { + return false + } else if prv1.D.Cmp(prv2.D) != 0 { + return false + } else { + return cmpPublic(prv1.PublicKey, prv2.PublicKey) + } +} + +// Validate the ECDH component. +func TestSharedKey(t *testing.T) { + prv1, err := GenerateKey(rand.Reader, DefaultCurve, nil) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } + skLen = MaxSharedKeyLength(&prv1.PublicKey) / 2 + + prv2, err := GenerateKey(rand.Reader, DefaultCurve, nil) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } + + sk1, err := prv1.GenerateShared(&prv2.PublicKey, skLen, skLen) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } + + sk2, err := prv2.GenerateShared(&prv1.PublicKey, skLen, skLen) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } + + if !bytes.Equal(sk1, sk2) { + fmt.Println(ErrBadSharedKeys.Error()) + t.FailNow() + } +} + +// Verify that the key generation code fails when too much key data is +// requested. +func TestTooBigSharedKey(t *testing.T) { + prv1, err := GenerateKey(rand.Reader, DefaultCurve, nil) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } + + prv2, err := GenerateKey(rand.Reader, DefaultCurve, nil) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } + + _, err = prv1.GenerateShared(&prv2.PublicKey, skLen*2, skLen*2) + if err != ErrSharedKeyTooBig { + fmt.Println("ecdh: shared key should be too large for curve") + t.FailNow() + } + + _, err = prv2.GenerateShared(&prv1.PublicKey, skLen*2, skLen*2) + if err != ErrSharedKeyTooBig { + fmt.Println("ecdh: shared key should be too large for curve") + t.FailNow() + } +} + +// Ensure a public key can be successfully marshalled and unmarshalled, and +// that the decoded key is the same as the original. +func TestMarshalPublic(t *testing.T) { + prv, err := GenerateKey(rand.Reader, DefaultCurve, nil) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } + + out, err := MarshalPublic(&prv.PublicKey) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } + + pub, err := UnmarshalPublic(out) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } + + if !cmpPublic(prv.PublicKey, *pub) { + fmt.Println("ecies: failed to unmarshal public key") + t.FailNow() + } +} + +// Ensure that a private key can be encoded into DER format, and that +// the resulting key is properly parsed back into a public key. +func TestMarshalPrivate(t *testing.T) { + prv, err := GenerateKey(rand.Reader, DefaultCurve, nil) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } + + out, err := MarshalPrivate(prv) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } + + if dumpEnc { + ioutil.WriteFile("test.out", out, 0644) + } + + prv2, err := UnmarshalPrivate(out) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } + + if !cmpPrivate(prv, prv2) { + fmt.Println("ecdh: private key import failed") + t.FailNow() + } +} + +// Ensure that a private key can be successfully encoded to PEM format, and +// the resulting key is properly parsed back in. +func TestPrivatePEM(t *testing.T) { + prv, err := GenerateKey(rand.Reader, DefaultCurve, nil) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } + + out, err := ExportPrivatePEM(prv) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } + + if dumpEnc { + ioutil.WriteFile("test.key", out, 0644) + } + + prv2, err := ImportPrivatePEM(out) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } else if !cmpPrivate(prv, prv2) { + fmt.Println("ecdh: import from PEM failed") + t.FailNow() + } +} + +// Ensure that a public key can be successfully encoded to PEM format, and +// the resulting key is properly parsed back in. +func TestPublicPEM(t *testing.T) { + prv, err := GenerateKey(rand.Reader, DefaultCurve, nil) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } + + out, err := ExportPublicPEM(&prv.PublicKey) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } + + if dumpEnc { + ioutil.WriteFile("test.pem", out, 0644) + } + + pub2, err := ImportPublicPEM(out) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } else if !cmpPublic(prv.PublicKey, *pub2) { + fmt.Println("ecdh: import from PEM failed") + t.FailNow() + } +} + +// Benchmark the generation of P256 keys. +func BenchmarkGenerateKeyP256(b *testing.B) { + for i := 0; i < b.N; i++ { + if _, err := GenerateKey(rand.Reader, elliptic.P256(), nil); err != nil { + fmt.Println(err.Error()) + b.FailNow() + } + } +} + +// Benchmark the generation of P256 shared keys. +func BenchmarkGenSharedKeyP256(b *testing.B) { + prv, err := GenerateKey(rand.Reader, elliptic.P256(), nil) + if err != nil { + fmt.Println(err.Error()) + b.FailNow() + } + + for i := 0; i < b.N; i++ { + _, err := prv.GenerateShared(&prv.PublicKey, skLen, skLen) + if err != nil { + fmt.Println(err.Error()) + b.FailNow() + } + } +} + +// Verify that an encrypted message can be successfully decrypted. +func TestEncryptDecrypt(t *testing.T) { + prv1, err := GenerateKey(rand.Reader, DefaultCurve, nil) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } + + prv2, err := GenerateKey(rand.Reader, DefaultCurve, nil) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } + + message := []byte("Hello, world.") + ct, err := Encrypt(rand.Reader, &prv2.PublicKey, message, nil, nil) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } + + pt, err := prv2.Decrypt(rand.Reader, ct, nil, nil) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } + + if !bytes.Equal(pt, message) { + fmt.Println("ecies: plaintext doesn't match message") + t.FailNow() + } + + _, err = prv1.Decrypt(rand.Reader, ct, nil, nil) + if err == nil { + fmt.Println("ecies: encryption should not have succeeded") + t.FailNow() + } +} + +// TestMarshalEncryption validates the encode/decode produces a valid +// ECIES encryption key. +func TestMarshalEncryption(t *testing.T) { + prv1, err := GenerateKey(rand.Reader, DefaultCurve, nil) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } + + out, err := MarshalPrivate(prv1) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } + + prv2, err := UnmarshalPrivate(out) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } + + message := []byte("Hello, world.") + ct, err := Encrypt(rand.Reader, &prv2.PublicKey, message, nil, nil) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } + + pt, err := prv2.Decrypt(rand.Reader, ct, nil, nil) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } + + if !bytes.Equal(pt, message) { + fmt.Println("ecies: plaintext doesn't match message") + t.FailNow() + } + + _, err = prv1.Decrypt(rand.Reader, ct, nil, nil) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } + +} + +type testCase struct { + Curve elliptic.Curve + Name string + Expected bool +} + +var testCases = []testCase{ + testCase{ + Curve: elliptic.P224(), + Name: "P224", + Expected: false, + }, + testCase{ + Curve: elliptic.P256(), + Name: "P256", + Expected: true, + }, + testCase{ + Curve: elliptic.P384(), + Name: "P384", + Expected: true, + }, + testCase{ + Curve: elliptic.P521(), + Name: "P521", + Expected: true, + }, +} + +// Test parameter selection for each curve, and that P224 fails automatic +// parameter selection (see README for a discussion of P224). Ensures that +// selecting a set of parameters automatically for the given curve works. +func TestParamSelection(t *testing.T) { + for _, c := range testCases { + testParamSelection(t, c) + } +} + +func testParamSelection(t *testing.T, c testCase) { + params := ParamsFromCurve(c.Curve) + if params == nil && c.Expected { + fmt.Printf("%s (%s)\n", ErrInvalidParams.Error(), c.Name) + t.FailNow() + } else if params != nil && !c.Expected { + fmt.Printf("ecies: parameters should be invalid (%s)\n", + c.Name) + t.FailNow() + } + + prv1, err := GenerateKey(rand.Reader, DefaultCurve, nil) + if err != nil { + fmt.Printf("%s (%s)\n", err.Error(), c.Name) + t.FailNow() + } + + prv2, err := GenerateKey(rand.Reader, DefaultCurve, nil) + if err != nil { + fmt.Printf("%s (%s)\n", err.Error(), c.Name) + t.FailNow() + } + + message := []byte("Hello, world.") + ct, err := Encrypt(rand.Reader, &prv2.PublicKey, message, nil, nil) + if err != nil { + fmt.Printf("%s (%s)\n", err.Error(), c.Name) + t.FailNow() + } + + pt, err := prv2.Decrypt(rand.Reader, ct, nil, nil) + if err != nil { + fmt.Printf("%s (%s)\n", err.Error(), c.Name) + t.FailNow() + } + + if !bytes.Equal(pt, message) { + fmt.Printf("ecies: plaintext doesn't match message (%s)\n", + c.Name) + t.FailNow() + } + + _, err = prv1.Decrypt(rand.Reader, ct, nil, nil) + if err == nil { + fmt.Printf("ecies: encryption should not have succeeded (%s)\n", + c.Name) + t.FailNow() + } + +} + +// Ensure that the basic public key validation in the decryption operation +// works. +func TestBasicKeyValidation(t *testing.T) { + badBytes := []byte{0, 1, 5, 6, 7, 8, 9} + + prv, err := GenerateKey(rand.Reader, DefaultCurve, nil) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } + + message := []byte("Hello, world.") + ct, err := Encrypt(rand.Reader, &prv.PublicKey, message, nil, nil) + if err != nil { + fmt.Println(err.Error()) + t.FailNow() + } + + for _, b := range badBytes { + ct[0] = b + _, err := prv.Decrypt(rand.Reader, ct, nil, nil) + if err != ErrInvalidPublicKey { + fmt.Println("ecies: validated an invalid key") + t.FailNow() + } + } +} diff --git a/params.go b/params.go new file mode 100644 index 0000000000..b968c7c17a --- /dev/null +++ b/params.go @@ -0,0 +1,187 @@ +package ecies + +// This file contains parameters for ECIES encryption, specifying the +// symmetric encryption and HMAC parameters. + +import ( + "crypto" + "crypto/aes" + "crypto/cipher" + "crypto/elliptic" + "crypto/sha256" + "crypto/sha512" + "fmt" + "hash" +) + +// The default curve for this package is the NIST P256 curve, which +// provides security equivalent to AES-128. +var DefaultCurve = elliptic.P256() + +var ( + ErrUnsupportedECDHAlgorithm = fmt.Errorf("ecies: unsupported ECDH algorithm") + ErrUnsupportedECIESParameters = fmt.Errorf("ecies: unsupported ECIES parameters") +) + +type ECIESParams struct { + Hash func() hash.Hash // hash function + hashAlgo crypto.Hash + Cipher func([]byte) (cipher.Block, error) // symmetric cipher + BlockSize int // block size of symmetric cipher + KeyLen int // length of symmetric key +} + +// Standard ECIES parameters: +// * ECIES using AES128 and HMAC-SHA-256-16 +// * ECIES using AES256 and HMAC-SHA-256-32 +// * ECIES using AES256 and HMAC-SHA-384-48 +// * ECIES using AES256 and HMAC-SHA-512-64 +var ( + ECIES_AES128_SHA256 *ECIESParams + ECIES_AES256_SHA256 *ECIESParams + ECIES_AES256_SHA384 *ECIESParams + ECIES_AES256_SHA512 *ECIESParams +) + +func init() { + ECIES_AES128_SHA256 = &ECIESParams{ + Hash: sha256.New, + hashAlgo: crypto.SHA256, + Cipher: aes.NewCipher, + BlockSize: aes.BlockSize, + KeyLen: 16, + } + + ECIES_AES256_SHA256 = &ECIESParams{ + Hash: sha256.New, + hashAlgo: crypto.SHA256, + Cipher: aes.NewCipher, + BlockSize: aes.BlockSize, + KeyLen: 32, + } + + ECIES_AES256_SHA384 = &ECIESParams{ + Hash: sha512.New384, + hashAlgo: crypto.SHA384, + Cipher: aes.NewCipher, + BlockSize: aes.BlockSize, + KeyLen: 32, + } + + ECIES_AES256_SHA512 = &ECIESParams{ + Hash: sha512.New, + hashAlgo: crypto.SHA512, + Cipher: aes.NewCipher, + BlockSize: aes.BlockSize, + KeyLen: 32, + } +} + +var paramsFromCurve = map[elliptic.Curve]*ECIESParams{ + elliptic.P256(): ECIES_AES128_SHA256, + elliptic.P384(): ECIES_AES256_SHA384, + elliptic.P521(): ECIES_AES256_SHA512, +} + +func AddParamsForCurve(curve elliptic.Curve, params *ECIESParams) { + paramsFromCurve[curve] = params +} + +// ParamsFromCurve selects parameters optimal for the selected elliptic curve. +// Only the curves P256, P384, and P512 are supported. +func ParamsFromCurve(curve elliptic.Curve) (params *ECIESParams) { + return paramsFromCurve[curve] + + /* + switch curve { + case elliptic.P256(): + return ECIES_AES128_SHA256 + case elliptic.P384(): + return ECIES_AES256_SHA384 + case elliptic.P521(): + return ECIES_AES256_SHA512 + default: + return nil + } + */ +} + +// ASN.1 encode the ECIES parameters relevant to the encryption operations. +func paramsToASNECIES(params *ECIESParams) (asnParams asnECIESParameters) { + if nil == params { + return + } + asnParams.KDF = asnNISTConcatenationKDF + asnParams.MAC = hmacFull + switch params.KeyLen { + case 16: + asnParams.Sym = aes128CTRinECIES + case 24: + asnParams.Sym = aes192CTRinECIES + case 32: + asnParams.Sym = aes256CTRinECIES + } + return +} + +// ASN.1 encode the ECIES parameters relevant to ECDH. +func paramsToASNECDH(params *ECIESParams) (algo asnECDHAlgorithm) { + switch params.hashAlgo { + case crypto.SHA224: + algo = dhSinglePass_stdDH_sha224kdf + case crypto.SHA256: + algo = dhSinglePass_stdDH_sha256kdf + case crypto.SHA384: + algo = dhSinglePass_stdDH_sha384kdf + case crypto.SHA512: + algo = dhSinglePass_stdDH_sha512kdf + } + return +} + +// ASN.1 decode the ECIES parameters relevant to the encryption stage. +func asnECIEStoParams(asnParams asnECIESParameters, params *ECIESParams) { + if !asnParams.KDF.Cmp(asnNISTConcatenationKDF) { + params = nil + return + } else if !asnParams.MAC.Cmp(hmacFull) { + params = nil + return + } + + switch { + case asnParams.Sym.Cmp(aes128CTRinECIES): + params.KeyLen = 16 + params.BlockSize = 16 + params.Cipher = aes.NewCipher + case asnParams.Sym.Cmp(aes192CTRinECIES): + params.KeyLen = 24 + params.BlockSize = 16 + params.Cipher = aes.NewCipher + case asnParams.Sym.Cmp(aes256CTRinECIES): + params.KeyLen = 32 + params.BlockSize = 16 + params.Cipher = aes.NewCipher + default: + params = nil + } +} + +// ASN.1 decode the ECIES parameters relevant to ECDH. +func asnECDHtoParams(asnParams asnECDHAlgorithm, params *ECIESParams) { + if asnParams.Cmp(dhSinglePass_stdDH_sha224kdf) { + params.hashAlgo = crypto.SHA224 + params.Hash = sha256.New224 + } else if asnParams.Cmp(dhSinglePass_stdDH_sha256kdf) { + params.hashAlgo = crypto.SHA256 + params.Hash = sha256.New + } else if asnParams.Cmp(dhSinglePass_stdDH_sha384kdf) { + params.hashAlgo = crypto.SHA384 + params.Hash = sha512.New384 + } else if asnParams.Cmp(dhSinglePass_stdDH_sha512kdf) { + params.hashAlgo = crypto.SHA512 + params.Hash = sha512.New + } else { + params = nil + } +} From ce796dcdbf3d29181a60eb8c734ef5050a5306c4 Mon Sep 17 00:00:00 2001 From: Gustav Simonsson Date: Thu, 11 Dec 2014 13:25:01 +0100 Subject: [PATCH 02/18] Update reference pdf links --- README | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README b/README index 7ccf27a27f..2650c7b9f6 100644 --- a/README +++ b/README @@ -76,10 +76,10 @@ REFERENCES * SEC (Standard for Efficient Cryptography) 1, version 2.0: Elliptic Curve Cryptography; Certicom, May 2009. - http://www.secg.org/download/aid-780/sec1-v2.pdf + http://www.secg.org/sec1-v2.pdf * GEC (Guidelines for Efficient Cryptography) 2, version 0.3: Test Vectors for SEC 1; Certicom, September 1999. - http://www.secg.org/collateral/gec2.pdf + http://read.pudn.com/downloads168/doc/772358/TestVectorsforSEC%201-gec2.pdf * NIST SP 800-56a: Recommendation for Pair-Wise Key Establishment Schemes Using Discrete Logarithm Cryptography. National Institute of Standards and Technology, May 2007. From 8c056aebe10c8c56f7c25889780b04e00f9ca00b Mon Sep 17 00:00:00 2001 From: Gustav Simonsson Date: Wed, 4 Feb 2015 17:06:06 +0100 Subject: [PATCH 03/18] Set both key generation and ECDSA nonce to use mixed entropy * Move random entropy functions to new package randentropy * Add function to get n bytes entropy where up to first 32 bytes are mixed with OS entropy sources --- crypto/key_store_passphrase.go | 15 ++---- crypto/key_store_test.go | 8 +-- crypto/randentropy/rand_entropy.go | 82 ++++++++++++++++++++++++++++++ crypto/secp256k1/secp256.go | 5 +- 4 files changed, 92 insertions(+), 18 deletions(-) create mode 100644 crypto/randentropy/rand_entropy.go diff --git a/crypto/key_store_passphrase.go b/crypto/key_store_passphrase.go index 0862b78865..74408f8743 100644 --- a/crypto/key_store_passphrase.go +++ b/crypto/key_store_passphrase.go @@ -68,10 +68,10 @@ import ( "code.google.com/p/go.crypto/scrypt" "crypto/aes" "crypto/cipher" - crand "crypto/rand" "encoding/hex" "encoding/json" "errors" + "github.com/ethereum/go-ethereum/crypto/randentropy" "io" "os" "path" @@ -116,7 +116,7 @@ func (ks keyStorePassphrase) GetKeyAddresses() (addresses [][]byte, err error) { func (ks keyStorePassphrase) StoreKey(key *Key, auth string) (err error) { authArray := []byte(auth) - salt := GetEntropyCSPRNG(32) + salt := randentropy.GetEntropyMixed(32) derivedKey, err := scrypt.Key(authArray, salt, scryptN, scryptr, scryptp, scryptdkLen) if err != nil { return err @@ -131,7 +131,7 @@ func (ks keyStorePassphrase) StoreKey(key *Key, auth string) (err error) { return err } - iv := GetEntropyCSPRNG(aes.BlockSize) // 16 + iv := randentropy.GetEntropyMixed(aes.BlockSize) // 16 AES256CBCEncrypter := cipher.NewCBCEncrypter(AES256Block, iv) cipherText := make([]byte, len(toEncrypt)) AES256CBCEncrypter.CryptBlocks(cipherText, toEncrypt) @@ -196,12 +196,3 @@ func DecryptKey(ks keyStorePassphrase, keyAddr []byte, auth string) (keyBytes [] } return keyBytes, keyId, err } - -func GetEntropyCSPRNG(n int) []byte { - mainBuff := make([]byte, n) - _, err := io.ReadFull(crand.Reader, mainBuff) - if err != nil { - panic("key generation: reading from crypto/rand failed: " + err.Error()) - } - return mainBuff -} diff --git a/crypto/key_store_test.go b/crypto/key_store_test.go index 0d229ab654..a136ba992c 100644 --- a/crypto/key_store_test.go +++ b/crypto/key_store_test.go @@ -1,7 +1,7 @@ package crypto import ( - crand "crypto/rand" + "github.com/ethereum/go-ethereum/crypto/randentropy" "reflect" "testing" ) @@ -9,7 +9,7 @@ import ( func TestKeyStorePlain(t *testing.T) { ks := NewKeyStorePlain(DefaultDataDir()) pass := "" // not used but required by API - k1, err := ks.GenerateNewKey(crand.Reader, pass) + k1, err := ks.GenerateNewKey(new(randentropy.RandEntropy), pass) if err != nil { t.Fatal(err) } @@ -37,7 +37,7 @@ func TestKeyStorePlain(t *testing.T) { func TestKeyStorePassphrase(t *testing.T) { ks := NewKeyStorePassphrase(DefaultDataDir()) pass := "foo" - k1, err := ks.GenerateNewKey(crand.Reader, pass) + k1, err := ks.GenerateNewKey(new(randentropy.RandEntropy), pass) if err != nil { t.Fatal(err) } @@ -63,7 +63,7 @@ func TestKeyStorePassphrase(t *testing.T) { func TestKeyStorePassphraseDecryptionFail(t *testing.T) { ks := NewKeyStorePassphrase(DefaultDataDir()) pass := "foo" - k1, err := ks.GenerateNewKey(crand.Reader, pass) + k1, err := ks.GenerateNewKey(new(randentropy.RandEntropy), pass) if err != nil { t.Fatal(err) } diff --git a/crypto/randentropy/rand_entropy.go b/crypto/randentropy/rand_entropy.go new file mode 100644 index 0000000000..28181030c6 --- /dev/null +++ b/crypto/randentropy/rand_entropy.go @@ -0,0 +1,82 @@ +package randentropy + +import ( + crand "crypto/rand" + "encoding/binary" + "github.com/ethereum/go-ethereum/crypto/sha3" + "io" + "os" + "strings" + "time" +) + +type RandEntropy struct { +} + +func (*RandEntropy) Read(bytes []byte) (n int, err error) { + readBytes := GetEntropyMixed(len(bytes)) + copy(bytes, readBytes) + return len(bytes), nil +} + +// TODO: copied from crypto.go , move to sha3 package? +func Sha3(data []byte) []byte { + d := sha3.NewKeccak256() + d.Write(data) + + return d.Sum(nil) +} + +// TODO: verify. this needs to be audited +// we start with crypt/rand, then XOR in additional entropy from OS +func GetEntropyMixed(n int) []byte { + startTime := time.Now().UnixNano() + // for each source, we take SHA3 of the source and use it as seed to math/rand + // then read bytes from it and XOR them onto the bytes read from crypto/rand + mainBuff := GetEntropyCSPRNG(n) + // 1. OS entropy sources + startTimeBytes := make([]byte, 32) + binary.PutVarint(startTimeBytes, startTime) + startTimeHash := Sha3(startTimeBytes) + mixBytes(mainBuff, startTimeHash) + + pid := os.Getpid() + pidBytes := make([]byte, 32) + binary.PutUvarint(pidBytes, uint64(pid)) + pidHash := Sha3(pidBytes) + mixBytes(mainBuff, pidHash) + + osEnv := os.Environ() + osEnvBytes := []byte(strings.Join(osEnv, "")) + osEnvHash := Sha3(osEnvBytes) + mixBytes(mainBuff, osEnvHash) + + // not all OS have hostname in env variables + osHostName, err := os.Hostname() + if err != nil { + osHostNameBytes := []byte(osHostName) + osHostNameHash := Sha3(osHostNameBytes) + mixBytes(mainBuff, osHostNameHash) + } + return mainBuff +} + +func GetEntropyCSPRNG(n int) []byte { + mainBuff := make([]byte, n) + _, err := io.ReadFull(crand.Reader, mainBuff) + if err != nil { + panic("reading from crypto/rand failed: " + err.Error()) + } + return mainBuff +} + +func mixBytes(buff []byte, mixBuff []byte) []byte { + bytesToMix := len(buff) + if bytesToMix > 32 { + bytesToMix = 32 + } + for i := 0; i < bytesToMix; i++ { + buff[i] ^= mixBuff[i] + } + return buff +} diff --git a/crypto/secp256k1/secp256.go b/crypto/secp256k1/secp256.go index c01598b842..c1e37629e0 100644 --- a/crypto/secp256k1/secp256.go +++ b/crypto/secp256k1/secp256.go @@ -15,6 +15,7 @@ import "C" import ( "bytes" "errors" + "github.com/ethereum/go-ethereum/crypto/randentropy" "unsafe" ) @@ -68,7 +69,7 @@ func GenerateKeyPair() ([]byte, []byte) { const seckey_len = 32 var pubkey []byte = make([]byte, pubkey_len) - var seckey []byte = RandByte(seckey_len) + var seckey []byte = randentropy.GetEntropyMixed(seckey_len) var pubkey_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&pubkey[0])) var seckey_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&seckey[0])) @@ -124,7 +125,7 @@ int secp256k1_ecdsa_sign_compact(const unsigned char *msg, int msglen, */ func Sign(msg []byte, seckey []byte) ([]byte, error) { - nonce := RandByte(32) + nonce := randentropy.GetEntropyMixed(32) var sig []byte = make([]byte, 65) var recid C.int From 52a46e61f948d9c5f4a4e993bc1870bd79a19b56 Mon Sep 17 00:00:00 2001 From: Gustav Simonsson Date: Wed, 11 Feb 2015 20:03:52 +0100 Subject: [PATCH 04/18] Correct ECIES shared key length check * Ensure the ECIES shared key is padded with zero bytes if it's smaller than the requested key length. * Split the ECIES shared key error into two; one for when the generated key is too big for the params and one for when it's nil (point of infinity returned by the curve scalar multiplication). --- ecies.go | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/ecies.go b/ecies.go index 0e2403d471..18952fc0b6 100644 --- a/ecies.go +++ b/ecies.go @@ -13,11 +13,12 @@ import ( ) var ( - ErrImport = fmt.Errorf("ecies: failed to import key") - ErrInvalidCurve = fmt.Errorf("ecies: invalid elliptic curve") - ErrInvalidParams = fmt.Errorf("ecies: invalid ECIES parameters") - ErrInvalidPublicKey = fmt.Errorf("ecies: invalid public key") - ErrSharedKeyTooBig = fmt.Errorf("ecies: shared key is too big") + ErrImport = fmt.Errorf("ecies: failed to import key") + ErrInvalidCurve = fmt.Errorf("ecies: invalid elliptic curve") + ErrInvalidParams = fmt.Errorf("ecies: invalid ECIES parameters") + ErrInvalidPublicKey = fmt.Errorf("ecies: invalid public key") + ErrSharedKeyIsPointAtInfinity = fmt.Errorf("ecies: shared key is point at infinity") + ErrSharedKeyTooBig = fmt.Errorf("ecies: shared key params are too big") ) // PublicKey is a representation of an elliptic curve public key. @@ -90,16 +91,20 @@ func MaxSharedKeyLength(pub *PublicKey) int { // ECDH key agreement method used to establish secret keys for encryption. func (prv *PrivateKey) GenerateShared(pub *PublicKey, skLen, macLen int) (sk []byte, err error) { if prv.PublicKey.Curve != pub.Curve { - err = ErrInvalidCurve - return + return nil, ErrInvalidCurve + } + if skLen+macLen > MaxSharedKeyLength(pub) { + return nil, ErrSharedKeyTooBig } x, _ := pub.Curve.ScalarMult(pub.X, pub.Y, prv.D.Bytes()) - if x == nil || (x.BitLen()+7)/8 < (skLen+macLen) { - err = ErrSharedKeyTooBig - return + if x == nil { + return nil, ErrSharedKeyIsPointAtInfinity } - sk = x.Bytes()[:skLen+macLen] - return + + sk = make([]byte, skLen+macLen) + skBytes := x.Bytes() + copy(sk[len(sk)-len(skBytes):], skBytes) + return sk, nil } var ( From 5136fc9ab71f77b0741c52b312ca9fdbfb5240c3 Mon Sep 17 00:00:00 2001 From: Gustav Simonsson Date: Thu, 12 Feb 2015 02:31:00 +0100 Subject: [PATCH 05/18] Fix ECIES params nil bug * Change ECIES params init function to static var as it does not have state; fixes TestMarshalencryption. --- params.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/params.go b/params.go index b968c7c17a..fd1ceedd01 100644 --- a/params.go +++ b/params.go @@ -36,14 +36,8 @@ type ECIESParams struct { // * ECIES using AES256 and HMAC-SHA-256-32 // * ECIES using AES256 and HMAC-SHA-384-48 // * ECIES using AES256 and HMAC-SHA-512-64 -var ( - ECIES_AES128_SHA256 *ECIESParams - ECIES_AES256_SHA256 *ECIESParams - ECIES_AES256_SHA384 *ECIESParams - ECIES_AES256_SHA512 *ECIESParams -) -func init() { +var ( ECIES_AES128_SHA256 = &ECIESParams{ Hash: sha256.New, hashAlgo: crypto.SHA256, @@ -75,7 +69,7 @@ func init() { BlockSize: aes.BlockSize, KeyLen: 32, } -} +) var paramsFromCurve = map[elliptic.Curve]*ECIESParams{ elliptic.P256(): ECIES_AES128_SHA256, From 39434e383b9e6fee30371afd5a9841de75671f56 Mon Sep 17 00:00:00 2001 From: Gustav Simonsson Date: Fri, 13 Feb 2015 15:38:18 +0100 Subject: [PATCH 06/18] Unexport randEntropy type and use exported Reader instead --- crypto/key_store_test.go | 6 +++--- crypto/randentropy/rand_entropy.go | 6 ++++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/crypto/key_store_test.go b/crypto/key_store_test.go index a136ba992c..485d8f5367 100644 --- a/crypto/key_store_test.go +++ b/crypto/key_store_test.go @@ -9,7 +9,7 @@ import ( func TestKeyStorePlain(t *testing.T) { ks := NewKeyStorePlain(DefaultDataDir()) pass := "" // not used but required by API - k1, err := ks.GenerateNewKey(new(randentropy.RandEntropy), pass) + k1, err := ks.GenerateNewKey(randentropy.Reader, pass) if err != nil { t.Fatal(err) } @@ -37,7 +37,7 @@ func TestKeyStorePlain(t *testing.T) { func TestKeyStorePassphrase(t *testing.T) { ks := NewKeyStorePassphrase(DefaultDataDir()) pass := "foo" - k1, err := ks.GenerateNewKey(new(randentropy.RandEntropy), pass) + k1, err := ks.GenerateNewKey(randentropy.Reader, pass) if err != nil { t.Fatal(err) } @@ -63,7 +63,7 @@ func TestKeyStorePassphrase(t *testing.T) { func TestKeyStorePassphraseDecryptionFail(t *testing.T) { ks := NewKeyStorePassphrase(DefaultDataDir()) pass := "foo" - k1, err := ks.GenerateNewKey(new(randentropy.RandEntropy), pass) + k1, err := ks.GenerateNewKey(randentropy.Reader, pass) if err != nil { t.Fatal(err) } diff --git a/crypto/randentropy/rand_entropy.go b/crypto/randentropy/rand_entropy.go index 28181030c6..b87fa564e0 100644 --- a/crypto/randentropy/rand_entropy.go +++ b/crypto/randentropy/rand_entropy.go @@ -10,10 +10,12 @@ import ( "time" ) -type RandEntropy struct { +var Reader io.Reader = &randEntropy{} + +type randEntropy struct { } -func (*RandEntropy) Read(bytes []byte) (n int, err error) { +func (*randEntropy) Read(bytes []byte) (n int, err error) { readBytes := GetEntropyMixed(len(bytes)) copy(bytes, readBytes) return len(bytes), nil From 6a7b0ef904def27c535cba6f85d686bc4dbab281 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 13 Feb 2015 18:17:17 +0100 Subject: [PATCH 07/18] Updated coin.js abi --- cmd/mist/assets/examples/coin.js | 113 ++++++++++++++++--------------- 1 file changed, 57 insertions(+), 56 deletions(-) diff --git a/cmd/mist/assets/examples/coin.js b/cmd/mist/assets/examples/coin.js index d69af5dcb2..77daac8469 100644 --- a/cmd/mist/assets/examples/coin.js +++ b/cmd/mist/assets/examples/coin.js @@ -1,64 +1,65 @@ -var contract = web3.eth.contractFromAbi([ +var walletABI = [ { - "constant":false, - "inputs":[ - {"name":"_h","type":"hash256"} - ], - "name":"confirm", - "outputs":[], - "type":"function" + "name":"confirm", + "type":"function", + "constant":false, + "inputs":[ + {"name":"_h","type":"hash256"} + ], + "outputs":[] },{ - "constant":false, - "inputs":[ - {"name":_to,"type":"address"}, - {"name":"_value","type":"uint256"}, - {"name":"_data","type":"bytes"} - ], - "name":"execute", - "outputs":[ - {"name":"_r","type":"hash256"} - ], - "type":"function" + "name":"execute", + "constant":false, + "type":"function", + "inputs":[ + {"name":"_to","type":"address"}, + {"name":"_value","type":"uint256"}, + {"name":"_data","type":"bytes"} + ], + "outputs":[ + {"name":"_r","type":"hash256"} + ] },{ - "constant":false, - "inputs":[ - {"name":"_to","type":"address"} - ],"name":"kill", - "outputs":[], - "type":"function" + "name":"kill", + "type":"function", + "constant":false, + "inputs":[ + {"name":"_to","type":"address"} + ], + "outputs":[] },{ - "constant":false, - "inputs":[ - {"name":"_from","type":"address"}, - {"name":"_to","type":"address"} - ], - "name":"changeOwner", - "outputs":[], - "type":"function" + "name":"changeOwner", + "type":"function", + "constant":false, + "inputs":[ + {"name":"_from","type":"address"}, + {"name":"_to","type":"address"} + ], + "outputs":[] },{ - "inputs":[ - {"indexed":false,"name":"value","type":"uint256"} - ], - "name":"CashIn", - "type":"event" + "name":"CashIn", + "type":"event", + "inputs":[ + {"indexed":false,"name":"value","type":"uint256"} + ] },{ - "inputs":[ - {"indexed":true,"name":"out","type":"string32"}, - {"indexed":false,"name":"owner","type":"address"}, - {"indexed":false,"name":"value","type":"uint256"}, - {"indexed":false,"name":"to","type":"address"} - ], - "name":"SingleTransact", - "type":"event" + "name":"SingleTransact", + "type":"event", + "inputs":[ + {"indexed":true,"name":"out","type":"string32"}, + {"indexed":false,"name":"owner","type":"address"}, + {"indexed":false,"name":"value","type":"uint256"}, + {"indexed":false,"name":"to","type":"address"} + ] },{ - "inputs":[ - {"indexed":true,"name":"out","type":"string32"}, - {"indexed":false,"name":"owner","type":"address"}, - {"indexed":false,"name":"operation","type":"hash256"}, - {"indexed":false,"name":"value","type":"uint256"}, - {"indexed":false,"name":"to","type":"address"} - ], - "name":"MultiTransact", - "type":"event" + "name":"MultiTransact", + "type":"event", + "inputs":[ + {"indexed":true,"name":"out","type":"string32"}, + {"indexed":false,"name":"owner","type":"address"}, + {"indexed":false,"name":"operation","type":"hash256"}, + {"indexed":false,"name":"value","type":"uint256"}, + {"indexed":false,"name":"to","type":"address"} + ] } -]); +]; From f35d62b75977231bb45d2e298c3f39744c875e67 Mon Sep 17 00:00:00 2001 From: Gustav Simonsson Date: Fri, 13 Feb 2015 18:22:36 +0100 Subject: [PATCH 08/18] Remove secp256_rand.go and update tests --- crypto/secp256k1/secp256_rand.go | 97 -------------------------------- crypto/secp256k1/secp256_test.go | 21 +++---- 2 files changed, 11 insertions(+), 107 deletions(-) delete mode 100644 crypto/secp256k1/secp256_rand.go diff --git a/crypto/secp256k1/secp256_rand.go b/crypto/secp256k1/secp256_rand.go deleted file mode 100644 index bb10025fc0..0000000000 --- a/crypto/secp256k1/secp256_rand.go +++ /dev/null @@ -1,97 +0,0 @@ -package secp256k1 - -import ( - crand "crypto/rand" - "io" - mrand "math/rand" - "os" - "strings" - "time" -) - -/* -Note: - -- On windows cryto/rand uses CrytoGenRandom which uses RC4 which is insecure -- Android random number generator is known to be insecure. -- Linux uses /dev/urandom , which is thought to be secure and uses entropy pool - -Therefore the output is salted. -*/ - -//finalizer from MurmerHash3 -func mmh3f(key uint64) uint64 { - key ^= key >> 33 - key *= 0xff51afd7ed558ccd - key ^= key >> 33 - key *= 0xc4ceb9fe1a85ec53 - key ^= key >> 33 - return key -} - -//knuth hash -func knuth_hash(in []byte) uint64 { - var acc uint64 = 3074457345618258791 - for i := 0; i < len(in); i++ { - acc += uint64(in[i]) - acc *= 3074457345618258799 - } - return acc -} - -var _rand *mrand.Rand - -func init() { - var seed1 uint64 = mmh3f(uint64(time.Now().UnixNano())) - var seed2 uint64 = knuth_hash([]byte(strings.Join(os.Environ(), ""))) - var seed3 uint64 = mmh3f(uint64(os.Getpid())) - - _rand = mrand.New(mrand.NewSource(int64(seed1 ^ seed2 ^ seed3))) -} - -func saltByte(n int) []byte { - buff := make([]byte, n) - for i := 0; i < len(buff); i++ { - var v uint64 = uint64(_rand.Int63()) - var b byte - for j := 0; j < 8; j++ { - b ^= byte(v & 0xff) - v = v >> 8 - } - buff[i] = b - } - return buff -} - -//On Unix-like systems, Reader reads from /dev/urandom. -//On Windows systems, Reader uses the CryptGenRandom API. - -//use entropy pool etc and cryptographic random number generator -//mix in time -//mix in mix in cpu cycle count -func RandByte(n int) []byte { - buff := make([]byte, n) - ret, err := io.ReadFull(crand.Reader, buff) - if len(buff) != ret || err != nil { - return nil - } - - buff2 := saltByte(n) - for i := 0; i < n; i++ { - buff[i] ^= buff2[2] - } - return buff -} - -/* - On Unix-like systems, Reader reads from /dev/urandom. - On Windows systems, Reader uses the CryptGenRandom API. -*/ -func RandByteWeakCrypto(n int) []byte { - buff := make([]byte, n) - ret, err := io.ReadFull(crand.Reader, buff) - if len(buff) != ret || err != nil { - return nil - } - return buff -} diff --git a/crypto/secp256k1/secp256_test.go b/crypto/secp256k1/secp256_test.go index 468c50db91..5e657cd724 100644 --- a/crypto/secp256k1/secp256_test.go +++ b/crypto/secp256k1/secp256_test.go @@ -3,6 +3,7 @@ package secp256k1 import ( "bytes" "fmt" + "github.com/ethereum/go-ethereum/crypto/randentropy" "log" "testing" ) @@ -12,7 +13,7 @@ const SigSize = 65 //64+1 func Test_Secp256_00(t *testing.T) { - var nonce []byte = RandByte(32) //going to get bitcoins stolen! + var nonce []byte = randentropy.GetEntropyMixed(32) //going to get bitcoins stolen! if len(nonce) != 32 { t.Fatal() @@ -50,7 +51,7 @@ func Test_Secp256_01(t *testing.T) { //test size of messages func Test_Secp256_02s(t *testing.T) { pubkey, seckey := GenerateKeyPair() - msg := RandByte(32) + msg := randentropy.GetEntropyMixed(32) sig, _ := Sign(msg, seckey) CompactSigTest(sig) if sig == nil { @@ -73,7 +74,7 @@ func Test_Secp256_02s(t *testing.T) { //test signing message func Test_Secp256_02(t *testing.T) { pubkey1, seckey := GenerateKeyPair() - msg := RandByte(32) + msg := randentropy.GetEntropyMixed(32) sig, _ := Sign(msg, seckey) if sig == nil { t.Fatal("Signature nil") @@ -96,7 +97,7 @@ func Test_Secp256_02(t *testing.T) { //test pubkey recovery func Test_Secp256_02a(t *testing.T) { pubkey1, seckey1 := GenerateKeyPair() - msg := RandByte(32) + msg := randentropy.GetEntropyMixed(32) sig, _ := Sign(msg, seckey1) if sig == nil { @@ -125,7 +126,7 @@ func Test_Secp256_02a(t *testing.T) { func Test_Secp256_03(t *testing.T) { _, seckey := GenerateKeyPair() for i := 0; i < TESTS; i++ { - msg := RandByte(32) + msg := randentropy.GetEntropyMixed(32) sig, _ := Sign(msg, seckey) CompactSigTest(sig) @@ -141,7 +142,7 @@ func Test_Secp256_03(t *testing.T) { func Test_Secp256_04(t *testing.T) { for i := 0; i < TESTS; i++ { pubkey1, seckey := GenerateKeyPair() - msg := RandByte(32) + msg := randentropy.GetEntropyMixed(32) sig, _ := Sign(msg, seckey) CompactSigTest(sig) @@ -164,7 +165,7 @@ func Test_Secp256_04(t *testing.T) { // -SIPA look at this func randSig() []byte { - sig := RandByte(65) + sig := randentropy.GetEntropyMixed(65) sig[32] &= 0x70 sig[64] %= 4 return sig @@ -172,7 +173,7 @@ func randSig() []byte { func Test_Secp256_06a_alt0(t *testing.T) { pubkey1, seckey := GenerateKeyPair() - msg := RandByte(32) + msg := randentropy.GetEntropyMixed(32) sig, _ := Sign(msg, seckey) if sig == nil { @@ -203,12 +204,12 @@ func Test_Secp256_06a_alt0(t *testing.T) { func Test_Secp256_06b(t *testing.T) { pubkey1, seckey := GenerateKeyPair() - msg := RandByte(32) + msg := randentropy.GetEntropyMixed(32) sig, _ := Sign(msg, seckey) fail_count := 0 for i := 0; i < TESTS; i++ { - msg = RandByte(32) + msg = randentropy.GetEntropyMixed(32) pubkey2, _ := RecoverPubkey(msg, sig) if bytes.Equal(pubkey1, pubkey2) == true { t.Fail() From 4d49d7b5a64774990c55dc2046195985bd716259 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 13 Feb 2015 18:30:06 +0100 Subject: [PATCH 09/18] Reset hash rate to 0 when mining is stopped --- pow/ezp/pow.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pow/ezp/pow.go b/pow/ezp/pow.go index 5571e73cdb..f4a8b80e5e 100644 --- a/pow/ezp/pow.go +++ b/pow/ezp/pow.go @@ -42,6 +42,8 @@ func (pow *EasyPow) Search(block pow.Block, stop <-chan struct{}) []byte { starti := i start := time.Now().UnixNano() + defer func() { pow.HashRate = 0 }() + // Make sure stop is empty empty: for { @@ -55,7 +57,6 @@ empty: for { select { case <-stop: - pow.HashRate = 0 return nil default: i++ From 384305f4aa8daef684efc76a374e1d6686c9c83f Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 13 Feb 2015 18:33:08 +0100 Subject: [PATCH 10/18] Fixed QML errors --- cmd/mist/assets/qml/views/catalog.qml | 1 - cmd/mist/assets/qml/views/miner.qml | 3 --- cmd/mist/assets/qml/views/wallet.qml | 4 +--- 3 files changed, 1 insertion(+), 7 deletions(-) diff --git a/cmd/mist/assets/qml/views/catalog.qml b/cmd/mist/assets/qml/views/catalog.qml index a7832e9faa..884493eef4 100644 --- a/cmd/mist/assets/qml/views/catalog.qml +++ b/cmd/mist/assets/qml/views/catalog.qml @@ -137,7 +137,6 @@ Rectangle { anchors { left: root.left right: root.right - top: sizeGrip.bottom bottom: root.bottom } diff --git a/cmd/mist/assets/qml/views/miner.qml b/cmd/mist/assets/qml/views/miner.qml index 0938644b9e..6a199a9258 100644 --- a/cmd/mist/assets/qml/views/miner.qml +++ b/cmd/mist/assets/qml/views/miner.qml @@ -19,9 +19,6 @@ Rectangle { id: lastBlockLabel objectName: "lastBlockLabel" text: "---" - font.pixelSize: 10 - anchors.right: peerGroup.left - anchors.rightMargin: 5 onTextChanged: { //menuItem.secondaryTitle = text } diff --git a/cmd/mist/assets/qml/views/wallet.qml b/cmd/mist/assets/qml/views/wallet.qml index 838fa87490..d1d38bb721 100644 --- a/cmd/mist/assets/qml/views/wallet.qml +++ b/cmd/mist/assets/qml/views/wallet.qml @@ -18,10 +18,8 @@ Rectangle { Label { objectName: "balanceLabel" visible: false - font.pixelSize: 10 - anchors.right: lastBlockLabel.left - anchors.rightMargin: 5 onTextChanged: { + balance.text = text menuItem.secondaryTitle = text } } From 1c1a3033beed74b22adc9827929e72ae04ed42cc Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Fri, 13 Feb 2015 21:41:13 +0100 Subject: [PATCH 11/18] eth: add default bootnode --- eth/backend.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/eth/backend.go b/eth/backend.go index 28a0650666..8c20735749 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -19,8 +19,14 @@ import ( "github.com/ethereum/go-ethereum/whisper" ) -var logger = ethlogger.NewLogger("SERV") -var jsonlogger = ethlogger.NewJsonLogger() +var ( + logger = ethlogger.NewLogger("SERV") + jsonlogger = ethlogger.NewJsonLogger() + + defaultBootNodes = []*discover.Node{ + discover.MustParseNode("enode://6cdd090303f394a1cac34ecc9f7cda18127eafa2a3a06de39f6d920b0e583e062a7362097c7c65ee490a758b442acd5c80c6fce4b148c6a391e946b45131365b@54.169.166.226:30303"), + } +) type Config struct { Name string @@ -50,6 +56,9 @@ type Config struct { } func (cfg *Config) parseBootNodes() []*discover.Node { + if cfg.BootNodes == "" { + return defaultBootNodes + } var ns []*discover.Node for _, url := range strings.Split(cfg.BootNodes, " ") { if url == "" { From 95cfaa1b37c417de45cc754b0ade9746f15f0805 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 13 Feb 2015 23:26:41 +0100 Subject: [PATCH 12/18] disabled test --- tests/vm/gh_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/vm/gh_test.go b/tests/vm/gh_test.go index d9b1ded7a3..17f9459102 100644 --- a/tests/vm/gh_test.go +++ b/tests/vm/gh_test.go @@ -262,6 +262,7 @@ func TestStateLog(t *testing.T) { } func TestStateTransaction(t *testing.T) { + t.Skip() const fn = "../files/StateTests/stTransactionTest.json" RunVmTest(fn, t) } From 4bef3ce284574c7e0e9a76004e076fc686b13bf6 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Fri, 13 Feb 2015 23:54:34 +0100 Subject: [PATCH 13/18] p2p: print Cap as name/version --- p2p/protocol.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/p2p/protocol.go b/p2p/protocol.go index fe359fc549..5fa395eda4 100644 --- a/p2p/protocol.go +++ b/p2p/protocol.go @@ -1,5 +1,7 @@ package p2p +import "fmt" + // Protocol represents a P2P subprotocol implementation. type Protocol struct { // Name should contain the official protocol name, @@ -37,6 +39,10 @@ func (cap Cap) RlpData() interface{} { return []interface{}{cap.Name, cap.Version} } +func (cap Cap) String() string { + return fmt.Sprintf("%s/%d", cap.Name, cap.Version) +} + type capsByName []Cap func (cs capsByName) Len() int { return len(cs) } From 8464e43eaf7d4841f90c8345d6d0afdf3f90ad49 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Fri, 13 Feb 2015 23:55:50 +0100 Subject: [PATCH 14/18] cmd/mist: fix peer window --- cmd/mist/assets/qml/main.qml | 23 ++++++++--------------- cmd/mist/gui.go | 34 +++++++++++++++++++++++++++++----- cmd/mist/ui_lib.go | 5 ----- 3 files changed, 37 insertions(+), 25 deletions(-) diff --git a/cmd/mist/assets/qml/main.qml b/cmd/mist/assets/qml/main.qml index ecc1214073..fd3e3020af 100644 --- a/cmd/mist/assets/qml/main.qml +++ b/cmd/mist/assets/qml/main.qml @@ -341,7 +341,7 @@ ApplicationWindow { } Label { - id: peerLabel + id: peerCounterLabel font.pixelSize: 10 text: "0 / 0" } @@ -926,7 +926,6 @@ ApplicationWindow { } } - function setWalletValue(value) { walletValueLabel.text = value } @@ -936,17 +935,11 @@ ApplicationWindow { var view = mainView.addPlugin(name) } - function setPeers(text) { - peerLabel.text = text - } + function clearPeers() { peerModel.clear() } + function addPeer(peer) { peerModel.append(peer) } - function addPeer(peer) { - // We could just append the whole peer object but it cries if you try to alter them - peerModel.append({ip: peer.ip, port: peer.port, lastResponse:timeAgo(peer.lastSend), latency: peer.latency, version: peer.version, caps: peer.caps}) - } - - function resetPeers(){ - peerModel.clear() + function setPeerCounters(text) { + peerCounterLabel.text = text } function timeAgo(unixTs){ @@ -984,9 +977,9 @@ ApplicationWindow { anchors.fill: parent id: peerTable model: peerModel - TableViewColumn{width: 200; role: "ip" ; title: "IP" } - TableViewColumn{width: 260; role: "version" ; title: "Version" } - TableViewColumn{width: 180; role: "caps" ; title: "Capabilities" } + TableViewColumn{width: 180; role: "addr" ; title: "Remote Address" } + TableViewColumn{width: 280; role: "nodeID" ; title: "Node ID" } + TableViewColumn{width: 180; role: "caps" ; title: "Capabilities" } } } } diff --git a/cmd/mist/gui.go b/cmd/mist/gui.go index c4ce1d4636..208b553d20 100644 --- a/cmd/mist/gui.go +++ b/cmd/mist/gui.go @@ -31,6 +31,7 @@ import ( "os" "path" "runtime" + "sort" "strconv" "time" @@ -449,6 +450,7 @@ func (gui *Gui) update() { case <-peerUpdateTicker.C: gui.setPeerInfo() + case <-generalUpdateTicker.C: statusText := "#" + gui.eth.ChainManager().CurrentBlock().Number().String() lastBlockLabel.Set("text", statusText) @@ -499,12 +501,34 @@ NumGC: %d )) } +type qmlpeer struct{ Addr, NodeID, Caps string } + +type peersByID []*qmlpeer + +func (s peersByID) Len() int { return len(s) } +func (s peersByID) Less(i, j int) bool { return s[i].NodeID < s[j].NodeID } +func (s peersByID) Swap(i, j int) { s[i], s[j] = s[j], s[i] } + func (gui *Gui) setPeerInfo() { - gui.win.Root().Call("setPeers", fmt.Sprintf("%d / %d", gui.eth.PeerCount(), gui.eth.MaxPeers)) - gui.win.Root().Call("resetPeers") - //for _, peer := range gui.xeth.Peers() { - //gui.win.Root().Call("addPeer", peer) - //} + peers := gui.eth.Peers() + qpeers := make(peersByID, len(peers)) + for i, p := range peers { + qpeers[i] = &qmlpeer{ + NodeID: p.ID().String(), + Addr: p.RemoteAddr().String(), + Caps: fmt.Sprint(p.Caps()), + } + } + // we need to sort the peers because they jump around randomly + // otherwise. order returned by eth.Peers is random because they + // are taken from a map. + sort.Sort(qpeers) + + gui.win.Root().Call("setPeerCounters", fmt.Sprintf("%d / %d", len(peers), gui.eth.MaxPeers())) + gui.win.Root().Call("clearPeers") + for _, p := range qpeers { + gui.win.Root().Call("addPeer", p) + } } func (gui *Gui) privateKey() string { diff --git a/cmd/mist/ui_lib.go b/cmd/mist/ui_lib.go index ab48386f40..368fb002b2 100644 --- a/cmd/mist/ui_lib.go +++ b/cmd/mist/ui_lib.go @@ -73,11 +73,6 @@ func (self *UiLib) Notef(args []interface{}) { guilogger.Infoln(args...) } -func (self *UiLib) PastPeers() *ethutil.List { - return ethutil.NewList([]string{}) - //return ethutil.NewList(eth.PastPeers()) -} - func (self *UiLib) ImportTx(rlpTx string) { tx := types.NewTransactionFromBytes(ethutil.Hex2Bytes(rlpTx)) err := self.eth.TxPool().Add(tx) From 84f7c966f725ef0f5c62b4427857d112c0d1e828 Mon Sep 17 00:00:00 2001 From: obscuren Date: Sat, 14 Feb 2015 00:25:47 +0100 Subject: [PATCH 15/18] Moved ECIES to repo & added secondary title for webview * ECIES moved from obscuren to ethereum * Added html META[name=badge] to reflect menuItem.secondaryTitle --- cmd/mist/assets/examples/info.html | 4 ++-- cmd/mist/assets/qml/views/browser.qml | 12 ++++++++++++ crypto/crypto.go | 2 +- p2p/crypto.go | 2 +- whisper/envelope.go | 2 +- whisper/whisper.go | 2 +- 6 files changed, 18 insertions(+), 6 deletions(-) diff --git a/cmd/mist/assets/examples/info.html b/cmd/mist/assets/examples/info.html index daad8c7065..d8816b19fa 100644 --- a/cmd/mist/assets/examples/info.html +++ b/cmd/mist/assets/examples/info.html @@ -1,8 +1,8 @@ - + @@ -60,7 +60,7 @@ var web3 = require('web3'); var eth = web3.eth; - web3.setProvider(new web3.providers.HttpSyncProvider('http://localhost:8080')); + web3.setProvider(new web3.providers.HttpSyncProvider('http://localhost:8545')); document.querySelector("#number").innerHTML = eth.number; document.querySelector("#coinbase").innerHTML = eth.coinbase diff --git a/cmd/mist/assets/qml/views/browser.qml b/cmd/mist/assets/qml/views/browser.qml index 04b2229ecb..3cbabf0b65 100644 --- a/cmd/mist/assets/qml/views/browser.qml +++ b/cmd/mist/assets/qml/views/browser.qml @@ -333,6 +333,17 @@ Rectangle { top: navBar.bottom } z: 10 + + Timer { + interval: 500; running: true; repeat: true + onTriggered: { + webview.runJavaScript("document.querySelector('meta[name=badge]').getAttribute('content')", function(badge) { + if (badge) { + menuItem.secondaryTitle = badge; + } + }); + } + } onLoadingChanged: { if (loadRequest.status == WebEngineView.LoadSucceededStatus) { @@ -340,6 +351,7 @@ Rectangle { menuItem.title = pageTitle; }); + //var topBarStyle webView.runJavaScript("document.querySelector(\"meta[name='ethereum-dapp-url-bar-style']\").getAttribute(\"content\")", function(topBarStyle){ if (topBarStyle=="transparent") { diff --git a/crypto/crypto.go b/crypto/crypto.go index 2c8f829770..e59250eb2c 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -18,10 +18,10 @@ import ( "code.google.com/p/go-uuid/uuid" "code.google.com/p/go.crypto/pbkdf2" "code.google.com/p/go.crypto/ripemd160" + "github.com/ethereum/go-ethereum/crypto/ecies" "github.com/ethereum/go-ethereum/crypto/secp256k1" "github.com/ethereum/go-ethereum/crypto/sha3" "github.com/ethereum/go-ethereum/ethutil" - "github.com/obscuren/ecies" ) func init() { diff --git a/p2p/crypto.go b/p2p/crypto.go index 2692d708c9..7e4b437128 100644 --- a/p2p/crypto.go +++ b/p2p/crypto.go @@ -8,10 +8,10 @@ import ( "io" "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/crypto/ecies" "github.com/ethereum/go-ethereum/crypto/secp256k1" ethlogger "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/p2p/discover" - "github.com/obscuren/ecies" ) var clogger = ethlogger.NewLogger("CRYPTOID") diff --git a/whisper/envelope.go b/whisper/envelope.go index 3c477ad9f8..d30397c984 100644 --- a/whisper/envelope.go +++ b/whisper/envelope.go @@ -7,9 +7,9 @@ import ( "time" "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/crypto/ecies" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/rlp" - "github.com/obscuren/ecies" ) const ( diff --git a/whisper/whisper.go b/whisper/whisper.go index 066f2c4ea8..50c2f98fd2 100644 --- a/whisper/whisper.go +++ b/whisper/whisper.go @@ -8,10 +8,10 @@ import ( "time" "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/crypto/ecies" "github.com/ethereum/go-ethereum/event/filter" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/p2p" - "github.com/obscuren/ecies" "gopkg.in/fatih/set.v0" ) From 32c7ebc51dcb31f21efe1b9c75f2b86cd216f510 Mon Sep 17 00:00:00 2001 From: obscuren Date: Sat, 14 Feb 2015 16:52:14 +0100 Subject: [PATCH 16/18] Fixed mining & limited hash power --- core/types/block.go | 3 ++- core/types/receipt.go | 2 +- miner/agent.go | 19 +++++++++++-------- miner/worker.go | 40 +++++++++++++++++++++++++++++----------- pow/ezp/pow.go | 2 +- 5 files changed, 44 insertions(+), 22 deletions(-) diff --git a/core/types/block.go b/core/types/block.go index 562a212397..fa28f5cc75 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -209,6 +209,7 @@ func (self *Block) ParentHash() []byte { func (self *Block) String() string { return fmt.Sprintf(`BLOCK(%x): Size: %v TD: %v { +NoNonce: %x Header: [ %v @@ -218,7 +219,7 @@ Transactions: Uncles: %v } -`, self.header.Hash(), self.Size(), self.Td, self.header, self.transactions, self.uncles) +`, self.header.Hash(), self.Size(), self.Td, self.header.HashNoNonce(), self.header, self.transactions, self.uncles) } func (self *Header) String() string { diff --git a/core/types/receipt.go b/core/types/receipt.go index bac64e41d6..49e68e2334 100644 --- a/core/types/receipt.go +++ b/core/types/receipt.go @@ -17,7 +17,7 @@ type Receipt struct { } func NewReceipt(root []byte, cumalativeGasUsed *big.Int) *Receipt { - return &Receipt{PostState: ethutil.CopyBytes(root), CumulativeGasUsed: cumalativeGasUsed} + return &Receipt{PostState: ethutil.CopyBytes(root), CumulativeGasUsed: new(big.Int).Set(cumalativeGasUsed)} } func NewRecieptFromValue(val *ethutil.Value) *Receipt { diff --git a/miner/agent.go b/miner/agent.go index ddd8e66756..9046f5d5ab 100644 --- a/miner/agent.go +++ b/miner/agent.go @@ -17,32 +17,35 @@ type CpuMiner struct { func NewCpuMiner(index int, pow pow.PoW) *CpuMiner { miner := &CpuMiner{ - c: make(chan *types.Block, 1), - quit: make(chan struct{}), - quitCurrentOp: make(chan struct{}, 1), - pow: pow, - index: index, + pow: pow, + index: index, } - go miner.update() return miner } func (self *CpuMiner) Work() chan<- *types.Block { return self.c } func (self *CpuMiner) Pow() pow.PoW { return self.pow } -func (self *CpuMiner) SetNonceCh(ch chan<- Work) { self.returnCh = ch } +func (self *CpuMiner) SetWorkCh(ch chan<- Work) { self.returnCh = ch } func (self *CpuMiner) Stop() { close(self.quit) close(self.quitCurrentOp) } +func (self *CpuMiner) Start() { + self.quit = make(chan struct{}) + self.quitCurrentOp = make(chan struct{}, 1) + self.c = make(chan *types.Block, 1) + + go self.update() +} + func (self *CpuMiner) update() { out: for { select { case block := <-self.c: - // make sure it's open self.quitCurrentOp <- struct{}{} go self.mine(block) diff --git a/miner/worker.go b/miner/worker.go index 9244e06b9d..96c8bdd39a 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -48,8 +48,9 @@ type Work struct { type Agent interface { Work() chan<- *types.Block - SetNonceCh(chan<- Work) + SetWorkCh(chan<- Work) Stop() + Start() Pow() pow.PoW } @@ -86,6 +87,11 @@ func (self *worker) start() { self.quit = make(chan struct{}) + // spin up agents + for _, agent := range self.agents { + agent.Start() + } + go self.update() go self.wait() } @@ -98,7 +104,7 @@ func (self *worker) stop() { func (self *worker) register(agent Agent) { self.agents = append(self.agents, agent) - agent.SetNonceCh(self.recv) + agent.SetWorkCh(self.recv) } func (self *worker) update() { @@ -108,15 +114,17 @@ out: for { select { case event := <-events.Chan(): - switch event := event.(type) { + switch event.(type) { case core.ChainEvent: self.commitNewWork() case core.TxPreEvent: - if err := self.commitTransaction(event.Tx); err != nil { - self.push() - } + self.commitNewWork() } case <-self.quit: + // stop all agents + for _, agent := range self.agents { + agent.Stop() + } break out } } @@ -131,8 +139,11 @@ func (self *worker) wait() { if block.Number().Uint64() == work.Number && block.Nonce() == nil { self.current.block.Header().Nonce = work.Nonce - self.chain.InsertChain(types.Blocks{self.current.block}) - self.mux.Post(core.NewMinedBlockEvent{self.current.block}) + if err := self.chain.InsertChain(types.Blocks{self.current.block}); err == nil { + self.mux.Post(core.NewMinedBlockEvent{self.current.block}) + } else { + self.commitNewWork() + } } break } @@ -141,9 +152,10 @@ func (self *worker) wait() { func (self *worker) push() { if self.mining { - self.current.state.Update(ethutil.Big0) + self.current.block.Header().GasUsed = self.current.totalUsedGas self.current.block.SetRoot(self.current.state.Root()) + // push new work to agents for _, agent := range self.agents { agent.Work() <- self.current.block } @@ -169,14 +181,18 @@ func (self *worker) commitNewWork() { // Break on gas limit break default: - minerlogger.Infoln(err) remove = append(remove, tx) } + + if err != nil { + minerlogger.Infoln(err) + } } self.eth.TxPool().RemoveSet(remove) self.current.coinbase.AddAmount(core.BlockReward) + self.current.state.Update(ethutil.Big0) self.push() } @@ -213,7 +229,9 @@ func (self *worker) commitTransaction(tx *types.Transaction) error { snapshot := self.current.state.Copy() receipt, txGas, err := self.proc.ApplyTransaction(self.current.coinbase, self.current.state, self.current.block, tx, self.current.totalUsedGas, true) if err != nil { - self.current.state.Set(snapshot) + if core.IsNonceErr(err) || core.IsGasLimitErr(err) { + self.current.state.Set(snapshot) + } return err } diff --git a/pow/ezp/pow.go b/pow/ezp/pow.go index f4a8b80e5e..540381243f 100644 --- a/pow/ezp/pow.go +++ b/pow/ezp/pow.go @@ -21,7 +21,7 @@ type EasyPow struct { } func New() *EasyPow { - return &EasyPow{turbo: true} + return &EasyPow{turbo: false} } func (pow *EasyPow) GetHashrate() int64 { From befb4bc1c102a62eaad9b4980e54cd4067078c2a Mon Sep 17 00:00:00 2001 From: obscuren Date: Sat, 14 Feb 2015 17:13:21 +0100 Subject: [PATCH 17/18] Fixed a few errors in the browser special meta tags objects --- cmd/mist/assets/qml/views/browser.qml | 58 +++++++++++++-------------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/cmd/mist/assets/qml/views/browser.qml b/cmd/mist/assets/qml/views/browser.qml index 3cbabf0b65..ac160e2b10 100644 --- a/cmd/mist/assets/qml/views/browser.qml +++ b/cmd/mist/assets/qml/views/browser.qml @@ -334,16 +334,16 @@ Rectangle { } z: 10 - Timer { - interval: 500; running: true; repeat: true - onTriggered: { - webview.runJavaScript("document.querySelector('meta[name=badge]').getAttribute('content')", function(badge) { - if (badge) { - menuItem.secondaryTitle = badge; - } - }); - } - } + Timer { + interval: 500; running: true; repeat: true + onTriggered: { + webview.runJavaScript("try{document.querySelector('meta[name=badge]').getAttribute('content')}catch(e){}", function(badge) { + if (badge) { + menuItem.secondaryTitle = badge; + } + }); + } + } onLoadingChanged: { if (loadRequest.status == WebEngineView.LoadSucceededStatus) { @@ -351,31 +351,27 @@ Rectangle { menuItem.title = pageTitle; }); + webView.runJavaScript("try{document.querySelector(\"meta[name='ethereum-dapp-url-bar-style']\").getAttribute(\"content\")}catch(e){}", function(topBarStyle){ + if (!topBarStyle) return; - //var topBarStyle - webView.runJavaScript("document.querySelector(\"meta[name='ethereum-dapp-url-bar-style']\").getAttribute(\"content\")", function(topBarStyle){ if (topBarStyle=="transparent") { - // Adjust for a transparent sidebar Dapp - navBarBackground.visible = false; - back.visible = false; - appInfoPane.anchors.leftMargin = -16; - appInfoPaneShadow.anchors.leftMargin = -16; - webview.anchors.topMargin = -74; + navBarBackground.visible = false; + back.visible = false; + appInfoPane.anchors.leftMargin = -16; + appInfoPaneShadow.anchors.leftMargin = -16; + webview.anchors.topMargin = -74; webview.runJavaScript("document.querySelector('body').classList.add('ethereum-dapp-url-bar-style-transparent')") } else { - navBarBackground.visible = true; - back.visible = true; - appInfoPane.anchors.leftMargin = 0; - appInfoPaneShadow.anchors.leftMargin = 0; - webview.anchors.topMargin = 0; - - }; + navBarBackground.visible = true; + back.visible = true; + appInfoPane.anchors.leftMargin = 0; + appInfoPaneShadow.anchors.leftMargin = 0; + webview.anchors.topMargin = 0; + }; }); - - webview.runJavaScript(eth.readFile("bignumber.min.js")); webview.runJavaScript(eth.readFile("ethereum.js/dist/ethereum.js")); @@ -383,10 +379,12 @@ Rectangle { var matches = cleanTitle.match(/^[a-z]*\:\/\/([^\/?#]+)(?:[\/?#]|$)/i); var domain = matches && matches[1]; - appDomain.text = domain //webview.url.replace("a", "z") - appTitle.text = webview.title + if (domain) + appDomain.text = domain //webview.url.replace("a", "z") + if (webview.title) + appTitle.text = webview.title - showFullUrlBar(false); + showFullUrlBar(false); } } onJavaScriptConsoleMessage: { From c29b01ce75c1feacf13e57b906ce0613168cd895 Mon Sep 17 00:00:00 2001 From: obscuren Date: Sat, 14 Feb 2015 17:18:28 +0100 Subject: [PATCH 18/18] Reset URL bar --- cmd/mist/assets/qml/views/browser.qml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/cmd/mist/assets/qml/views/browser.qml b/cmd/mist/assets/qml/views/browser.qml index ac160e2b10..2685f902b9 100644 --- a/cmd/mist/assets/qml/views/browser.qml +++ b/cmd/mist/assets/qml/views/browser.qml @@ -352,7 +352,15 @@ Rectangle { }); webView.runJavaScript("try{document.querySelector(\"meta[name='ethereum-dapp-url-bar-style']\").getAttribute(\"content\")}catch(e){}", function(topBarStyle){ - if (!topBarStyle) return; + if (!topBarStyle) { + showFullUrlBar(true); + navBarBackground.visible = true; + back.visible = true; + appInfoPane.anchors.leftMargin = 0; + appInfoPaneShadow.anchors.leftMargin = 0; + webview.anchors.topMargin = 0; + return; + } if (topBarStyle=="transparent") { // Adjust for a transparent sidebar Dapp