github.com/ronperry/cryptoedge@v0.0.0-20150815114006-cc363e290743/lioness/cipher_test.go (about)

     1  package lioness
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/aes"
     6  	"crypto/sha256"
     7  	"encoding/hex"
     8  	"fmt"
     9  	"testing"
    10  )
    11  
    12  func Test_Construct(t *testing.T) {
    13  	l, err := Construct(aes.NewCipher, sha256.New, 32, nil, ModeZero)
    14  	if err != nil {
    15  		t.Fatalf("New returned error: %s", err)
    16  	}
    17  	_ = l
    18  }
    19  
    20  func Test_Explode(t *testing.T) {
    21  	l, err := Construct(aes.NewCipher, sha256.New, 32, nil, ModeZero)
    22  	if err != nil {
    23  		t.Fatalf("New returned error: %s", err)
    24  	}
    25  	l.ExplodeKey([]byte("Test Key"))
    26  }
    27  
    28  func Test_ZeroEncrypt(t *testing.T) {
    29  	key := []byte("test key")
    30  	data := []byte("really important test data that is encrypted and decrypted yeeha")
    31  	l, err := Construct(aes.NewCipher, sha256.New, 32, key, ModeZero)
    32  	if err != nil {
    33  		t.Fatalf("New returned error: %s", err)
    34  	}
    35  	e, err := l.Encrypt(data)
    36  	if err != nil {
    37  		t.Fatalf("Encrypt returned error: %s", err)
    38  	}
    39  	if bytes.Equal(data, e) {
    40  		t.Error("No operation")
    41  	}
    42  	d, err := l.Decrypt(e)
    43  	if err != nil {
    44  		t.Fatalf("Decrypt returned error: %s", err)
    45  	}
    46  	if !bytes.Equal(data, d) {
    47  		t.Error("En-De-Crypt failed")
    48  	}
    49  }
    50  
    51  func Test_IVEncrypt(t *testing.T) {
    52  	key := []byte("test key")
    53  	data := []byte("really important test data that is encrypted and decrypted yeeha")
    54  	l, err := Construct(aes.NewCipher, sha256.New, 32, key, ModeIV)
    55  	if err != nil {
    56  		t.Fatalf("New returned error: %s", err)
    57  	}
    58  	e, err := l.Encrypt(data)
    59  	if err != nil {
    60  		t.Fatalf("Encrypt returned error: %s", err)
    61  	}
    62  	if bytes.Equal(data, e) {
    63  		t.Error("No operation")
    64  	}
    65  	d, err := l.Decrypt(e)
    66  	if err != nil {
    67  		t.Fatalf("Decrypt returned error: %s", err)
    68  	}
    69  	if !bytes.Equal(data, d) {
    70  		t.Error("En-De-Crypt failed")
    71  	}
    72  }
    73  
    74  func Test_ZeroEncryptReverse(t *testing.T) {
    75  	key := []byte("test key")
    76  	data := []byte("really important test data that is encrypted and decrypted yeeha")
    77  	l, err := Construct(aes.NewCipher, sha256.New, 32, key, ModeZero)
    78  	if err != nil {
    79  		t.Fatalf("New returned error: %s", err)
    80  	}
    81  	e, err := l.Decrypt(data)
    82  	if err != nil {
    83  		t.Fatalf("Encrypt returned error: %s", err)
    84  	}
    85  	if bytes.Equal(data, e) {
    86  		t.Error("No operation")
    87  	}
    88  	d, err := l.Encrypt(e)
    89  	if err != nil {
    90  		t.Fatalf("Decrypt returned error: %s", err)
    91  	}
    92  	if !bytes.Equal(data, d) {
    93  		t.Error("En-De-Crypt failed")
    94  	}
    95  }
    96  
    97  func Test_IVEncryptReverse(t *testing.T) {
    98  	key := []byte("test key")
    99  	data := []byte("really important test data that is encrypted and decrypted yeeha")
   100  	l, err := Construct(aes.NewCipher, sha256.New, 32, key, ModeIV)
   101  	if err != nil {
   102  		t.Fatalf("New returned error: %s", err)
   103  	}
   104  	e, err := l.Decrypt(data)
   105  	if err != nil {
   106  		t.Fatalf("Encrypt returned error: %s", err)
   107  	}
   108  	if bytes.Equal(data, e) {
   109  		t.Error("No operation")
   110  	}
   111  	d, err := l.Encrypt(e)
   112  	if err != nil {
   113  		t.Fatalf("Decrypt returned error: %s", err)
   114  	}
   115  	if !bytes.Equal(data, d) {
   116  		t.Error("En-De-Crypt failed")
   117  	}
   118  }
   119  
   120  func Test_Xor(t *testing.T) {
   121  	l := new(Lioness)
   122  	l.keylen = 100
   123  	data1 := []byte("1234567890123456")
   124  	data2 := []byte("133456789012345")
   125  	x := l.xor(data1, data2)
   126  	y := l.xor(x, data1)
   127  	if bytes.Equal(data1, x) {
   128  		t.Error("Xor no operation")
   129  	}
   130  	if !bytes.Equal(data2, y) {
   131  		t.Error("Xor failed")
   132  	}
   133  }
   134  
   135  func Test_Rop(t *testing.T) {
   136  	key := []byte("trivial key of some length 12345")
   137  	plaintext := []byte("input data")
   138  	l, err := Construct(aes.NewCipher, sha256.New, 32, nil, ModeZero)
   139  	if err != nil {
   140  		t.Fatalf("New returned error: %s", err)
   141  	}
   142  	ciphertext, err := l.rop(make([]byte, aes.BlockSize), key, plaintext)
   143  	if err != nil {
   144  		t.Fatalf("CTREnc failed: %s", err)
   145  	}
   146  	plaintext2, err := l.rop(make([]byte, aes.BlockSize), key, ciphertext)
   147  	if err != nil {
   148  		t.Fatalf("CTREnc failed: %s", err)
   149  	}
   150  	if hex.EncodeToString(ciphertext) != "798eff3651a457968fc7" {
   151  		t.Error("Encryption produces wrong result")
   152  	}
   153  	if !bytes.Equal(plaintext, plaintext2) {
   154  		t.Error("Plaintexts do not match")
   155  	}
   156  }
   157  
   158  func Test_RopHMAC(t *testing.T) {
   159  	key := []byte("trivial key")
   160  	plaintext := []byte("input data")
   161  	l, err := Construct(aes.NewCipher, sha256.New, 32, nil, ModeZero)
   162  	if err != nil {
   163  		t.Fatalf("New returned error: %s", err)
   164  	}
   165  	hm := l.ropHMAC(key, plaintext)
   166  	if hex.EncodeToString(hm) != "53b7726810eea80d5f8746d3bdf5fe994a39d880dfe77fe45a2f5a4ff4e86c9c" {
   167  		t.Error("HMAC wrong")
   168  	}
   169  }
   170  
   171  func ExampleNew() {
   172  	// Define encryption key and data to be encrypted
   173  	key := []byte("This is the secret encryption key")
   174  	// Data must be keylen +1 bytes or longer
   175  	data := []byte("Some data to be encrypted. It must be long enough to cover at least one key length.")
   176  
   177  	// Create new lioness with default algorithms, mode and automatic key expansion.
   178  	l, err := New(key)
   179  
   180  	// Errors returned if keylength and algorithm choice conflict.
   181  	if err != nil {
   182  		fmt.Printf("Error occured in New: %s", err)
   183  	}
   184  
   185  	// Encrypt data. Error is returned if data is not long enough
   186  	encrypted, err := l.Encrypt(data)
   187  	if err != nil {
   188  		fmt.Printf("Error occured in Encrypt: %s", err)
   189  	}
   190  
   191  	// Decrypt data.
   192  	decrypted, err := l.Decrypt(encrypted)
   193  	if err != nil {
   194  		fmt.Printf("Error occured in Decrypt: %s", err)
   195  	}
   196  	fmt.Printf("Data after decryption: %s\n", decrypted)
   197  
   198  	// Output: Data after decryption: Some data to be encrypted. It must be long enough to cover at least one key length.
   199  }
   200  
   201  func ExampleConstruct() {
   202  	// Define encryption key and data to be encrypted
   203  	key := []byte("This is the secret encryption key")
   204  	// Data must be keylen +1 bytes or longer
   205  	data := []byte("Some data to be encrypted. It must be long enough to cover at least one key length.")
   206  
   207  	// Create new lioness. aes will be used for stream encryption (in CTR mode), sha256 is our hmac hash.
   208  	// The keylen is set to 32 (AES256) and we are using ModeZero (IV is all zero). The key given will
   209  	// be expanded automatically to fill the subkeys k1-k4.
   210  	l, err := Construct(aes.NewCipher, sha256.New, 32, key, ModeZero)
   211  
   212  	// Errors returned if keylength and algorithm choice conflict.
   213  	if err != nil {
   214  		fmt.Printf("Error occured in New: %s", err)
   215  	}
   216  
   217  	// Encrypt data. Error is returned if data is not long enough
   218  	encrypted, err := l.Encrypt(data)
   219  	if err != nil {
   220  		fmt.Printf("Error occured in Encrypt: %s", err)
   221  	}
   222  
   223  	// Decrypt data.
   224  	decrypted, err := l.Decrypt(encrypted)
   225  	if err != nil {
   226  		fmt.Printf("Error occured in Decrypt: %s", err)
   227  	}
   228  	fmt.Printf("Data after decryption: %s\n", decrypted)
   229  
   230  	// Output: Data after decryption: Some data to be encrypted. It must be long enough to cover at least one key length.
   231  }
   232  
   233  func ExampleLioness_Setkeys() {
   234  	// Define four (independent) encryption keys. Keys must have keylen length.
   235  	key1 := []byte("11111178901234567890123456781111")
   236  	key2 := []byte("22222278901234567890123456782222")
   237  	key3 := []byte("33333378901234567890123456783333")
   238  	key4 := []byte("44444478901234567890123456784444")
   239  
   240  	// Data must be keylen +1 bytes or longer
   241  	data := []byte("Some data to be encrypted. It must be long enough to cover at least one key length.")
   242  
   243  	// Create new lioness. aes will be used for stream encryption (in CTR mode), sha256 is our hmac hash.
   244  	// The keylen is set to 32 (AES256) and we are using ModeIV (L is used as IV). Key is nil, we will use
   245  	// SetKeys instead
   246  	l, err := Construct(aes.NewCipher, sha256.New, 32, nil, ModeIV)
   247  	if err != nil {
   248  		fmt.Printf("Error occured in New: %s", err)
   249  	}
   250  
   251  	// Set the four keys required for the operation. An error will occur if the key lengths != keylen
   252  	err = l.Setkeys(key1, key2, key3, key4)
   253  	if err != nil {
   254  		fmt.Printf("Error occured in EetKey: %s", err)
   255  	}
   256  
   257  	// Encrypt data. Error is returned if data is not long enough
   258  	encrypted, err := l.Encrypt(data)
   259  	if err != nil {
   260  		fmt.Printf("Error occured in Encrypt: %s", err)
   261  	}
   262  
   263  	// Decrypt data.
   264  	decrypted, err := l.Decrypt(encrypted)
   265  	if err != nil {
   266  		fmt.Printf("Error occured in Decrypt: %s", err)
   267  	}
   268  	fmt.Printf("Data after setkeys and decryption: %s\n", decrypted)
   269  
   270  	// Output: Data after setkeys and decryption: Some data to be encrypted. It must be long enough to cover at least one key length.
   271  }