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