github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/sdk/go/tests/secp256k1_test.go (about)

     1  /**
     2   * Copyright 2017 Intel Corporation
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   * ------------------------------------------------------------------------------
    16   */
    17  
    18  package tests
    19  
    20  import (
    21  	. "sawtooth_sdk/signing"
    22  	"testing"
    23  )
    24  
    25  var (
    26  	data   = []byte{0x01, 0x02, 0x03}
    27  	PEMSTR = `-----BEGIN EC PRIVATE KEY-----
    28  MHQCAQEEIISGREvlByLRnbhovpK8wSd5hnymtY8hdQCOvZQ473CpoAcGBSuBBAAK
    29  oUQDQgAEWC6TyM1jpYu3f/GGIuktDk4nM1qyOf9PEPHkRkN8zK2HxxNwDi+yN3hR
    30  8Ag+VeTwbRRZOlBdFBsgPxz3/864hw==
    31  -----END EC PRIVATE KEY-----
    32  `
    33  	PEMSTRPRIV = "8486444be50722d19db868be92bcc12779867ca6b58f2175008ebd9438ef70a9"
    34  	ENCPEM     = `-----BEGIN EC PRIVATE KEY-----
    35  Proc-Type: 4,ENCRYPTED
    36  DEK-Info: AES-128-CBC,23CDF282F2217A9334A2413D78DAE04C
    37  
    38  PQy89wdLsayP/FG68wgmL1EdlI3S5pN8ibCFrnp5OAtVNrYUD/TH9DMYVmRCNUB4
    39  e+vXoQzd1IysjoFpV21zajSAxCmcbU4CGCDEea3GPwirOSE0ZjPHPp15IkRuGFYm
    40  L/8e9mXvEQPAmBC0NMiltnk4/26iN7hB1QxSQQwy/Zc=
    41  -----END EC PRIVATE KEY-----
    42  `
    43  	ENCPEMPRIV = "2cc32bc33935a5dbad8118abc63dfb627bb91a98d5e6310f5d60f5d65f6adb2f"
    44  	PEMPUBSTR  = "03582e93c8cd63a58bb77ff18622e92d0e4e27335ab239ff4f10f1e446437cccad"
    45  	ENCPEMPUB  = "0257510b4718fd79b21dee3173ffb48ab9a668a35a377be7b7dc432243a940c510"
    46  	PUBSTR     = "035e1de3048a62f9f478440a22fd7655b80f0aac997be963b119ac54b3bfdea3b7"
    47  	SIGSTR     = "0062bc154dca72472e66062c4539c8befb2680d79d59b3cc539dd182ff36072b199adc1118db5fc1884d50cdec9d31a2356af03175439ccb841c7b0e3ae83297"
    48  )
    49  
    50  func TestSigning(t *testing.T) {
    51  	context := NewSecp256k1Context()
    52  	priv_1 := context.NewRandomPrivateKey()
    53  	pub_1 := context.GetPublicKey(priv_1)
    54  
    55  	sig_1 := context.Sign(data, priv_1)
    56  
    57  	if !context.Verify(sig_1, data, pub_1) {
    58  		t.Error(
    59  			"Context fails t to verify signature",
    60  			priv_1, pub_1, sig_1,
    61  		)
    62  	}
    63  
    64  	priv_2 := context.NewRandomPrivateKey()
    65  
    66  	sig_2 := context.Sign(data, priv_2)
    67  
    68  	if context.Verify(sig_2, data, pub_1) {
    69  		t.Error(
    70  			"Context verifies wrong signature",
    71  			priv_2, pub_1, sig_2,
    72  		)
    73  	}
    74  
    75  	// Verify that everything returns the right algorithm name
    76  	assertSecp256k1(context.GetAlgorithmName(), t)
    77  	assertSecp256k1(priv_1.GetAlgorithmName(), t)
    78  	assertSecp256k1(pub_1.GetAlgorithmName(), t)
    79  	assertSecp256k1(priv_2.GetAlgorithmName(), t)
    80  }
    81  
    82  func assertSecp256k1(name string, t *testing.T) {
    83  	if name != "secp256k1" {
    84  		t.Error("Wrong name", name)
    85  	}
    86  }
    87  
    88  func TestPemLoader(t *testing.T) {
    89  	// Load the keys
    90  	priv, err := PemToSecp256k1PrivateKey(PEMSTR, "")
    91  	if err != nil {
    92  		t.Error("Failed to load unencrypted PEM key")
    93  	}
    94  
    95  	epriv, err := PemToSecp256k1PrivateKey(ENCPEM, "password")
    96  	if err != nil {
    97  		t.Error("Failed to load encrypted PEM key")
    98  	}
    99  
   100  	// Test that they match expected
   101  	if priv.AsHex() != PEMSTRPRIV {
   102  		t.Error("Failed to parse unencrypted PEM key")
   103  	}
   104  
   105  	if epriv.AsHex() != ENCPEMPRIV {
   106  		t.Error("Failed to parse encrypted PEM key")
   107  	}
   108  
   109  	// Test that the correct public keys are generated
   110  	context := NewSecp256k1Context()
   111  
   112  	pub := context.GetPublicKey(priv).AsHex()
   113  	epub := context.GetPublicKey(epriv).AsHex()
   114  
   115  	if pub != PEMPUBSTR {
   116  		t.Error("Failed to generate correct public key from unencrypted PEM key")
   117  	}
   118  
   119  	if epub != ENCPEMPUB {
   120  		t.Error("Failed to generate correct public key from encrypted PEM key")
   121  	}
   122  }
   123  
   124  func TestOtherSigning(t *testing.T) {
   125  	context := CreateContext("secp256k1")
   126  	priv := context.NewRandomPrivateKey()
   127  
   128  	factory := NewCryptoFactory(context)
   129  	signer := factory.NewSigner(priv)
   130  
   131  	pub := signer.GetPublicKey()
   132  	sig := signer.Sign(data)
   133  
   134  	if !context.Verify(sig, data, pub) {
   135  		t.Error(
   136  			"Context fails t to verify signature",
   137  			priv, pub, sig,
   138  		)
   139  	}
   140  }