github.com/palcoin-project/palcd@v1.0.0/btcec/ciphering_test.go (about)

     1  // Copyright (c) 2015-2016 The btcsuite developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package btcec
     6  
     7  import (
     8  	"bytes"
     9  	"encoding/hex"
    10  	"testing"
    11  )
    12  
    13  func TestGenerateSharedSecret(t *testing.T) {
    14  	privKey1, err := NewPrivateKey(S256())
    15  	if err != nil {
    16  		t.Errorf("private key generation error: %s", err)
    17  		return
    18  	}
    19  	privKey2, err := NewPrivateKey(S256())
    20  	if err != nil {
    21  		t.Errorf("private key generation error: %s", err)
    22  		return
    23  	}
    24  
    25  	secret1 := GenerateSharedSecret(privKey1, privKey2.PubKey())
    26  	secret2 := GenerateSharedSecret(privKey2, privKey1.PubKey())
    27  
    28  	if !bytes.Equal(secret1, secret2) {
    29  		t.Errorf("ECDH failed, secrets mismatch - first: %x, second: %x",
    30  			secret1, secret2)
    31  	}
    32  }
    33  
    34  // Test 1: Encryption and decryption
    35  func TestCipheringBasic(t *testing.T) {
    36  	privkey, err := NewPrivateKey(S256())
    37  	if err != nil {
    38  		t.Fatal("failed to generate private key")
    39  	}
    40  
    41  	in := []byte("Hey there dude. How are you doing? This is a test.")
    42  
    43  	out, err := Encrypt(privkey.PubKey(), in)
    44  	if err != nil {
    45  		t.Fatal("failed to encrypt:", err)
    46  	}
    47  
    48  	dec, err := Decrypt(privkey, out)
    49  	if err != nil {
    50  		t.Fatal("failed to decrypt:", err)
    51  	}
    52  
    53  	if !bytes.Equal(in, dec) {
    54  		t.Error("decrypted data doesn't match original")
    55  	}
    56  }
    57  
    58  // Test 2: Byte compatibility with Pyelliptic
    59  func TestCiphering(t *testing.T) {
    60  	pb, _ := hex.DecodeString("fe38240982f313ae5afb3e904fb8215fb11af1200592b" +
    61  		"fca26c96c4738e4bf8f")
    62  	privkey, _ := PrivKeyFromBytes(S256(), pb)
    63  
    64  	in := []byte("This is just a test.")
    65  	out, _ := hex.DecodeString("b0d66e5adaa5ed4e2f0ca68e17b8f2fc02ca002009e3" +
    66  		"3487e7fa4ab505cf34d98f131be7bd258391588ca7804acb30251e71a04e0020ecf" +
    67  		"df0f84608f8add82d7353af780fbb28868c713b7813eb4d4e61f7b75d7534dd9856" +
    68  		"9b0ba77cf14348fcff80fee10e11981f1b4be372d93923e9178972f69937ec850ed" +
    69  		"6c3f11ff572ddd5b2bedf9f9c0b327c54da02a28fcdce1f8369ffec")
    70  
    71  	dec, err := Decrypt(privkey, out)
    72  	if err != nil {
    73  		t.Fatal("failed to decrypt:", err)
    74  	}
    75  
    76  	if !bytes.Equal(in, dec) {
    77  		t.Error("decrypted data doesn't match original")
    78  	}
    79  }
    80  
    81  func TestCipheringErrors(t *testing.T) {
    82  	privkey, err := NewPrivateKey(S256())
    83  	if err != nil {
    84  		t.Fatal("failed to generate private key")
    85  	}
    86  
    87  	tests1 := []struct {
    88  		ciphertext []byte // input ciphertext
    89  	}{
    90  		{bytes.Repeat([]byte{0x00}, 133)},                   // errInputTooShort
    91  		{bytes.Repeat([]byte{0x00}, 134)},                   // errUnsupportedCurve
    92  		{bytes.Repeat([]byte{0x02, 0xCA}, 134)},             // errInvalidXLength
    93  		{bytes.Repeat([]byte{0x02, 0xCA, 0x00, 0x20}, 134)}, // errInvalidYLength
    94  		{[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // IV
    95  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    96  			0x02, 0xCA, 0x00, 0x20, // curve and X length
    97  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // X
    98  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    99  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   100  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   101  			0x00, 0x20, // Y length
   102  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Y
   103  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   104  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   105  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   106  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ciphertext
   107  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   108  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // MAC
   109  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   110  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   111  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   112  		}}, // invalid pubkey
   113  		{[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // IV
   114  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   115  			0x02, 0xCA, 0x00, 0x20, // curve and X length
   116  			0x11, 0x5C, 0x42, 0xE7, 0x57, 0xB2, 0xEF, 0xB7, // X
   117  			0x67, 0x1C, 0x57, 0x85, 0x30, 0xEC, 0x19, 0x1A,
   118  			0x13, 0x59, 0x38, 0x1E, 0x6A, 0x71, 0x12, 0x7A,
   119  			0x9D, 0x37, 0xC4, 0x86, 0xFD, 0x30, 0xDA, 0xE5,
   120  			0x00, 0x20, // Y length
   121  			0x7E, 0x76, 0xDC, 0x58, 0xF6, 0x93, 0xBD, 0x7E, // Y
   122  			0x70, 0x10, 0x35, 0x8C, 0xE6, 0xB1, 0x65, 0xE4,
   123  			0x83, 0xA2, 0x92, 0x10, 0x10, 0xDB, 0x67, 0xAC,
   124  			0x11, 0xB1, 0xB5, 0x1B, 0x65, 0x19, 0x53, 0xD2,
   125  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ciphertext
   126  			// padding not aligned to 16 bytes
   127  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   128  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // MAC
   129  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   130  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   131  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   132  		}}, // errInvalidPadding
   133  		{[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // IV
   134  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   135  			0x02, 0xCA, 0x00, 0x20, // curve and X length
   136  			0x11, 0x5C, 0x42, 0xE7, 0x57, 0xB2, 0xEF, 0xB7, // X
   137  			0x67, 0x1C, 0x57, 0x85, 0x30, 0xEC, 0x19, 0x1A,
   138  			0x13, 0x59, 0x38, 0x1E, 0x6A, 0x71, 0x12, 0x7A,
   139  			0x9D, 0x37, 0xC4, 0x86, 0xFD, 0x30, 0xDA, 0xE5,
   140  			0x00, 0x20, // Y length
   141  			0x7E, 0x76, 0xDC, 0x58, 0xF6, 0x93, 0xBD, 0x7E, // Y
   142  			0x70, 0x10, 0x35, 0x8C, 0xE6, 0xB1, 0x65, 0xE4,
   143  			0x83, 0xA2, 0x92, 0x10, 0x10, 0xDB, 0x67, 0xAC,
   144  			0x11, 0xB1, 0xB5, 0x1B, 0x65, 0x19, 0x53, 0xD2,
   145  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ciphertext
   146  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   147  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // MAC
   148  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   149  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   150  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   151  		}}, // ErrInvalidMAC
   152  	}
   153  
   154  	for i, test := range tests1 {
   155  		_, err = Decrypt(privkey, test.ciphertext)
   156  		if err == nil {
   157  			t.Errorf("Decrypt #%d did not get error", i)
   158  		}
   159  	}
   160  
   161  	// test error from removePKCSPadding
   162  	tests2 := []struct {
   163  		in []byte // input data
   164  	}{
   165  		{bytes.Repeat([]byte{0x11}, 17)},
   166  		{bytes.Repeat([]byte{0x07}, 15)},
   167  	}
   168  	for i, test := range tests2 {
   169  		_, err = removePKCSPadding(test.in)
   170  		if err == nil {
   171  			t.Errorf("removePKCSPadding #%d did not get error", i)
   172  		}
   173  	}
   174  }