go-ethereum/swarm/storage/encryption/encryption_test.go

152 lines
12 KiB
Go
Raw Normal View History

2018-06-20 07:06:27 -05:00
// Copyright 2018 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package encryption
import (
"bytes"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/swarm/testutil"
"golang.org/x/crypto/sha3"
2018-06-20 07:06:27 -05:00
)
var expectedTransformedHex = "352187af3a843decc63ceca6cb01ea39dbcf77caf0a8f705f5c30d557044ceec9392b94a79376f1e5c10cd0c0f2a98e5353bf22b3ea4fdac6677ee553dec192e3db64e179d0474e96088fb4abd2babd67de123fb398bdf84d818f7bda2c1ab60b3ea0e0569ae54aa969658eb4844e6960d2ff44d7c087ee3aaffa1c0ee5df7e50b615f7ad90190f022934ad5300c7d1809bfe71a11cc04cece5274eb97a5f20350630522c1dbb7cebaf4f97f84e03f5cfd88f2b48880b25d12f4d5e75c150f704ef6b46c72e07db2b705ac3644569dccd22fd8f964f6ef787fda63c46759af334e6f665f70eac775a7017acea49f3c7696151cb1b9434fa4ac27fb803921ffb5ec58dafa168098d7d5b97e384be3384cf5bc235c3d887fef89fe76c0065f9b8d6ad837b442340d9e797b46ef5709ea3358bc415df11e4830de986ef0f1c418ffdcc80e9a3cda9bea0ab5676c0d4240465c43ba527e3b4ea50b4f6255b510e5d25774a75449b0bd71e56c537ade4fcf0f4d63c99ae1dbb5a844971e2c19941b8facfcfc8ee3056e7cb3c7114c5357e845b52f7103cb6e00d2308c37b12baa5b769e1cc7b00fc06f2d16e70cc27a82cb9c1a4e40cb0d43907f73df2c9db44f1b51a6b0bc6d09f77ac3be14041fae3f9df2da42df43ae110904f9ecee278030185254d7c6e918a5512024d047f77a992088cb3190a6587aa54d0c7231c1cd2e455e0d4c07f74bece68e29cd8ba0190c0bcfb26d24634af5d91a81ef5d4dd3d614836ce942ddbf7bb1399317f4c03faa675f325f18324bf9433844bfe5c4cc04130c8d5c329562b7cd66e72f7355de8f5375a72202971613c32bd7f3fcdcd51080758cd1d0a46dbe8f0374381dbc359f5864250c63dde8131cbd7c98ae2b0147d6ea4bf65d1443d511b18e6d608bbb46ac036353b4c51df306a10a6f6939c38629a5c18aaf89cac04bd3ad5156e6b92011c88341cb08551bab0a89e6a46538f5af33b86121dba17e3a434c273f385cd2e8cb90bdd32747d8425d929ccbd9b0815c73325988855549a8489dfd047daf777aaa3099e54cf997175a5d9e1edfe363e3b68c70e02f6bf4fcde6a0f3f7d0e7e98bde1a72ae8b6cd27b32990680cc4a04fc467f41c5adcaddabfc71928a3f6872c360c1d765260690dd28b269864c8e380d9c92ef6b89b0094c8f9bb22608b4156381b19b920e9583c9616ce5693b4d2a6c689f02e6a91584a8e501e107403d2689dd0045269dd9946c0e969fb656a3b39d84a798831f5f9290f163eb2f97d3ae25071324e95e2256d9c1e56eb83c26397855323edc202d56ad05894333b7f0ed3c1e4734782eb8bd5477242fd80d7a89b12866f85cfae476322f032465d6b1253993033fccd4723530630ab97a1566460af9c90c9da843c229406e65f3fa578bd6bf04dee9b6153807ddadb8ceefc5c601a8ab26023c67b1ab1e8e0f29ce94c78c308005a781853e7a2e0e51738939a657c987b5e611f32f47b5ff461c52e63e0ea390515a8e1f5393dae54ea526934b5f310b76e3fa050e40718cb4c8a20e58946d6ee1879f08c52764422fe542b3240e75eccb7aa75b1f8a651e37a3bc56b0932cdae0e985948468db1f98eb4b77b82081ea25d8a762db00f7898864984bd80e2f3f35f236bf57291dec28f550769943bcfb6f884b7687589b673642ef7fe5d7d5a87d3eca5017f83ccb9a3310520474479464cb3f433440e7e2f1e28c0aef700a45848573409e7ab66e0cfd4fe5d2147ace81bc65fd8891f6245cd69246bbf5c27830e5ab882dd1d02aba34ff6ca9af88df00fd602892f02fedbdc65dedec203faf3f8ff4a97314e0ddb58b9ab756a61a562597f4088b445fcc3b28a708ca7b1485dcd791b779fbf2b3ef1ec5c6205f595fbe45a02105034147e5a146089c200a49dae33ae051a08ea5f974a21540aaeffa7f9d9e3d35478016fb27b871036eb27217a5b834b461f535752fb5f1c8dded3ae14ce3a2ef6639e2fe41939e3509e46e347a95d50b2080f1ba42c804b290ddc912c952d1cec3f2661369f738feacc0dbf1ea27429c644e45f9e26f30c341acd34c7519b2a1663e334621691e810767e9918c2c547b2e23cce915f97d26aac8d0d2fcd3edb7986ad4e2b8a852edebad534cb6c0e9f0797d3563e5409d7e068e48356c67ce519246cd9c560e881453df97cbba562018811e6cf8c327f399d1d1253ab47a19f4a0ccc7c6d86a9603e0551da310ea595d71305c4aad96819120a92cdbaf1f77ec8df9cc7c838c0d4de1e8692dd81da38268d1d71324bcffdafbe5122e4b81828e021e936d83ae8021eac592aa52cd296b5ce392c7173d622f8e07d18f59bb1b08ba15211af6703463b09b593af3c37735296816d9f2e7a369354a5374ea3955e14ca8ac56d5bfe4aef7a21bd825d6ae85530bee5d2aaaa4914981b3dfdb2e92ec2a27c83d74b59e84ff5c056f7d8945745f2efc3dcf28f288c6cd8383700fb2312f7001f24dd40015e436ae23e052fe9070ea9535b9c989898a9bda3d5382cf10e432fae6ccf0c825b3e6436edd3a9f8846e5606f8563931b5f29ba407c5236e5730225dda211a8504ec1817bc935e1fd9a532b648c502df302ed2063aed008fd5676131ac9e95998e9447b02bd29d77e38fcfd2959f2de929b31970335eb2a74348cc6918bc35b9bf749eab0fe304c946cd9e1ca284e6853c42646e60b6b39e0d3fb3c260abfc5c1b4ca3c3770f344118ca7c7f5c1ad1f123f8f369cd60afc3cdb3e9e81968c5c9fa7c8b014ffe0508dd4f0a2a976d5d1ca8fc9ad7a237d92cfe7b41413d934d6e142824b252699397e48e4bac4e91ebc10602720684bd0863773c548f9a2f9724245e4
2018-06-20 07:06:27 -05:00
var hashFunc = sha3.NewLegacyKeccak256
var testKey Key
func init() {
var err error
testKey, err = hexutil.Decode("0x8abf1502f557f15026716030fb6384792583daf39608a3cd02ff2f47e9bc6e49")
if err != nil {
panic(err.Error())
}
}
2018-06-20 07:06:27 -05:00
func TestEncryptDataLongerThanPadding(t *testing.T) {
enc := New(testKey, 4095, uint32(0), hashFunc)
2018-06-20 07:06:27 -05:00
data := make([]byte, 4096)
expectedError := "Data length longer than padding, data length 4096 padding 4095"
_, err := enc.Encrypt(data)
2018-06-20 07:06:27 -05:00
if err == nil || err.Error() != expectedError {
t.Fatalf("Expected error \"%v\" got \"%v\"", expectedError, err)
}
}
func TestEncryptDataZeroPadding(t *testing.T) {
enc := New(testKey, 0, uint32(0), hashFunc)
2018-06-20 07:06:27 -05:00
data := make([]byte, 2048)
encrypted, err := enc.Encrypt(data)
2018-06-20 07:06:27 -05:00
if err != nil {
t.Fatalf("Expected no error got %v", err)
}
if len(encrypted) != 2048 {
t.Fatalf("Encrypted data length expected \"%v\" got %v", 2048, len(encrypted))
}
}
func TestEncryptDataLengthEqualsPadding(t *testing.T) {
enc := New(testKey, 4096, uint32(0), hashFunc)
2018-06-20 07:06:27 -05:00
data := make([]byte, 4096)
encrypted, err := enc.Encrypt(data)
2018-06-20 07:06:27 -05:00
if err != nil {
t.Fatalf("Expected no error got %v", err)
}
encryptedHex := common.Bytes2Hex(encrypted)
expectedTransformed := common.Hex2Bytes(expectedTransformedHex)
if !bytes.Equal(encrypted, expectedTransformed) {
t.Fatalf("Expected %v got %v", expectedTransformedHex, encryptedHex)
}
}
func TestEncryptDataLengthSmallerThanPadding(t *testing.T) {
enc := New(testKey, 4096, uint32(0), hashFunc)
2018-06-20 07:06:27 -05:00
data := make([]byte, 4080)
encrypted, err := enc.Encrypt(data)
2018-06-20 07:06:27 -05:00
if err != nil {
t.Fatalf("Expected no error got %v", err)
}
if len(encrypted) != 4096 {
t.Fatalf("Encrypted data length expected %v got %v", 4096, len(encrypted))
}
}
func TestEncryptDataCounterNonZero(t *testing.T) {
// TODO
}
func TestDecryptDataLengthNotEqualsPadding(t *testing.T) {
enc := New(testKey, 4096, uint32(0), hashFunc)
2018-06-20 07:06:27 -05:00
data := make([]byte, 4097)
expectedError := "Data length different than padding, data length 4097 padding 4096"
_, err := enc.Decrypt(data)
2018-06-20 07:06:27 -05:00
if err == nil || err.Error() != expectedError {
t.Fatalf("Expected error \"%v\" got \"%v\"", expectedError, err)
}
}
func TestEncryptDecryptIsIdentity(t *testing.T) {
testEncryptDecryptIsIdentity(t, 2048, 0, 2048, 32)
testEncryptDecryptIsIdentity(t, 4096, 0, 4096, 32)
testEncryptDecryptIsIdentity(t, 4096, 0, 1000, 32)
testEncryptDecryptIsIdentity(t, 32, 10, 32, 32)
}
func testEncryptDecryptIsIdentity(t *testing.T, padding int, initCtr uint32, dataLength int, keyLength int) {
key := GenerateRandomKey(keyLength)
enc := New(key, padding, initCtr, hashFunc)
2018-06-20 07:06:27 -05:00
data := testutil.RandomBytes(1, dataLength)
2018-06-20 07:06:27 -05:00
encrypted, err := enc.Encrypt(data)
2018-06-20 07:06:27 -05:00
if err != nil {
t.Fatalf("Expected no error got %v", err)
}
decrypted, err := enc.Decrypt(encrypted)
2018-06-20 07:06:27 -05:00
if err != nil {
t.Fatalf("Expected no error got %v", err)
}
if len(decrypted) != padding {
t.Fatalf("Expected decrypted data length %v got %v", padding, len(decrypted))
}
// we have to remove the extra bytes which were randomly added to fill until padding
if len(data) < padding {
decrypted = decrypted[:len(data)]
}
if !bytes.Equal(data, decrypted) {
t.Fatalf("Expected decrypted %v got %v", common.Bytes2Hex(data), common.Bytes2Hex(decrypted))
}
}