summaryrefslogtreecommitdiff
path: root/src/pkg/crypto/xtea
diff options
context:
space:
mode:
Diffstat (limited to 'src/pkg/crypto/xtea')
-rw-r--r--src/pkg/crypto/xtea/block.go46
-rw-r--r--src/pkg/crypto/xtea/cipher.go40
-rw-r--r--src/pkg/crypto/xtea/xtea_test.go116
3 files changed, 101 insertions, 101 deletions
diff --git a/src/pkg/crypto/xtea/block.go b/src/pkg/crypto/xtea/block.go
index 7cf768153..dfb82e1e2 100644
--- a/src/pkg/crypto/xtea/block.go
+++ b/src/pkg/crypto/xtea/block.go
@@ -17,50 +17,50 @@ const numRounds = 64
// blockToUint32 reads an 8 byte slice into two uint32s.
// The block is treated as big endian.
func blockToUint32(src []byte) (uint32, uint32) {
- r0 := uint32(src[0])<<24 | uint32(src[1])<<16 | uint32(src[2])<<8 | uint32(src[3]);
- r1 := uint32(src[4])<<24 | uint32(src[5])<<16 | uint32(src[6])<<8 | uint32(src[7]);
- return r0, r1;
+ r0 := uint32(src[0])<<24 | uint32(src[1])<<16 | uint32(src[2])<<8 | uint32(src[3])
+ r1 := uint32(src[4])<<24 | uint32(src[5])<<16 | uint32(src[6])<<8 | uint32(src[7])
+ return r0, r1
}
// uint32ToBlock writes two unint32s into an 8 byte data block.
// Values are written as big endian.
func uint32ToBlock(v0, v1 uint32, dst []byte) {
- dst[0] = byte(v0 >> 24);
- dst[1] = byte(v0 >> 16);
- dst[2] = byte(v0 >> 8);
- dst[3] = byte(v0);
- dst[4] = byte(v1 >> 24);
- dst[5] = byte(v1 >> 16);
- dst[6] = byte(v1 >> 8);
- dst[7] = byte(v1 >> 0);
+ dst[0] = byte(v0 >> 24)
+ dst[1] = byte(v0 >> 16)
+ dst[2] = byte(v0 >> 8)
+ dst[3] = byte(v0)
+ dst[4] = byte(v1 >> 24)
+ dst[5] = byte(v1 >> 16)
+ dst[6] = byte(v1 >> 8)
+ dst[7] = byte(v1 >> 0)
}
// encryptBlock encrypts a single 8 byte block using XTEA.
func encryptBlock(c *Cipher, src, dst []byte) {
- v0, v1 := blockToUint32(src);
+ v0, v1 := blockToUint32(src)
// Two rounds of XTEA applied per loop
for i := 0; i < numRounds; {
- v0 += ((v1<<4 ^ v1>>5) + v1) ^ c.table[i];
- i++;
- v1 += ((v0<<4 ^ v0>>5) + v0) ^ c.table[i];
- i++;
+ v0 += ((v1<<4 ^ v1>>5) + v1) ^ c.table[i]
+ i++
+ v1 += ((v0<<4 ^ v0>>5) + v0) ^ c.table[i]
+ i++
}
- uint32ToBlock(v0, v1, dst);
+ uint32ToBlock(v0, v1, dst)
}
// decryptBlock decrypt a single 8 byte block using XTEA.
func decryptBlock(c *Cipher, src, dst []byte) {
- v0, v1 := blockToUint32(src);
+ v0, v1 := blockToUint32(src)
// Two rounds of XTEA applied per loop
for i := numRounds; i > 0; {
- i--;
- v1 -= ((v0<<4 ^ v0>>5) + v0) ^ c.table[i];
- i--;
- v0 -= ((v1<<4 ^ v1>>5) + v1) ^ c.table[i];
+ i--
+ v1 -= ((v0<<4 ^ v0>>5) + v0) ^ c.table[i]
+ i--
+ v0 -= ((v1<<4 ^ v1>>5) + v1) ^ c.table[i]
}
- uint32ToBlock(v0, v1, dst);
+ uint32ToBlock(v0, v1, dst)
}
diff --git a/src/pkg/crypto/xtea/cipher.go b/src/pkg/crypto/xtea/cipher.go
index 71545b5ac..4fb3acbef 100644
--- a/src/pkg/crypto/xtea/cipher.go
+++ b/src/pkg/crypto/xtea/cipher.go
@@ -9,8 +9,8 @@ package xtea
// For details, see http://www.cix.co.uk/~klockstone/xtea.pdf
import (
- "os";
- "strconv";
+ "os"
+ "strconv"
)
// The XTEA block size in bytes.
@@ -19,7 +19,7 @@ const BlockSize = 8
// A Cipher is an instance of an XTEA cipher using a particular key.
// table contains a series of precalculated values that are used each round.
type Cipher struct {
- table [64]uint32;
+ table [64]uint32
}
type KeySizeError int
@@ -32,7 +32,7 @@ func (k KeySizeError) String() string {
// The key argument should be the XTEA key.
// XTEA only supports 128 bit (16 byte) keys.
func NewCipher(key []byte) (*Cipher, os.Error) {
- k := len(key);
+ k := len(key)
switch k {
default:
return nil, KeySizeError(k)
@@ -40,25 +40,25 @@ func NewCipher(key []byte) (*Cipher, os.Error) {
break
}
- c := new(Cipher);
- initCipher(c, key);
+ c := new(Cipher)
+ initCipher(c, key)
- return c, nil;
+ return c, nil
}
// BlockSize returns the XTEA block size, 8 bytes.
// It is necessary to satisfy the Key interface in the
// package "crypto/modes".
-func (c *Cipher) BlockSize() int { return BlockSize }
+func (c *Cipher) BlockSize() int { return BlockSize }
// Encrypt encrypts the 8 byte buffer src using the key and stores the result in dst.
// Note that for amounts of data larger than a block,
// it is not safe to just call Encrypt on successive blocks;
// instead, use an encryption mode like XTEACBC (see modes.go).
-func (c *Cipher) Encrypt(src, dst []byte) { encryptBlock(c, src, dst) }
+func (c *Cipher) Encrypt(src, dst []byte) { encryptBlock(c, src, dst) }
// Decrypt decrypts the 8 byte buffer src using the key k and stores the result in dst.
-func (c *Cipher) Decrypt(src, dst []byte) { decryptBlock(c, src, dst) }
+func (c *Cipher) Decrypt(src, dst []byte) { decryptBlock(c, src, dst) }
// Reset zeros the table, so that it will no longer appear in the process's memory.
func (c *Cipher) Reset() {
@@ -71,22 +71,22 @@ func (c *Cipher) Reset() {
// of precalculated values that are based on the key.
func initCipher(c *Cipher, key []byte) {
// Load the key into four uint32s
- var k [4]uint32;
+ var k [4]uint32
for i := 0; i < len(k); i++ {
- j := i << 2; // Multiply by 4
- k[i] = uint32(key[j+0])<<24 | uint32(key[j+1])<<16 | uint32(key[j+2])<<8 | uint32(key[j+3]);
+ j := i << 2 // Multiply by 4
+ k[i] = uint32(key[j+0])<<24 | uint32(key[j+1])<<16 | uint32(key[j+2])<<8 | uint32(key[j+3])
}
// Precalculate the table
- const delta = 0x9E3779B9;
- var sum uint32 = 0;
+ const delta = 0x9E3779B9
+ var sum uint32 = 0
// Two rounds of XTEA applied per loop
for i := 0; i < numRounds; {
- c.table[i] = sum + k[sum&3];
- i++;
- sum += delta;
- c.table[i] = sum + k[(sum>>11)&3];
- i++;
+ c.table[i] = sum + k[sum&3]
+ i++
+ sum += delta
+ c.table[i] = sum + k[(sum>>11)&3]
+ i++
}
}
diff --git a/src/pkg/crypto/xtea/xtea_test.go b/src/pkg/crypto/xtea/xtea_test.go
index 26221c4b4..94756f79f 100644
--- a/src/pkg/crypto/xtea/xtea_test.go
+++ b/src/pkg/crypto/xtea/xtea_test.go
@@ -5,7 +5,7 @@
package xtea
import (
- "testing";
+ "testing"
)
// A sample test key for when we just want to initialise a cipher
@@ -14,20 +14,20 @@ var testKey = []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99,
// Test that the block size for XTEA is correct
func TestBlocksize(t *testing.T) {
if BlockSize != 8 {
- t.Errorf("BlockSize constant - expected 8, got %d", BlockSize);
- return;
+ t.Errorf("BlockSize constant - expected 8, got %d", BlockSize)
+ return
}
- c, err := NewCipher(testKey);
+ c, err := NewCipher(testKey)
if err != nil {
- t.Errorf("NewCipher(%d bytes) = %s", len(testKey), err);
- return;
+ t.Errorf("NewCipher(%d bytes) = %s", len(testKey), err)
+ return
}
- result := c.BlockSize();
+ result := c.BlockSize()
if result != 8 {
- t.Errorf("BlockSize function - expected 8, gotr %d", result);
- return;
+ t.Errorf("BlockSize function - expected 8, gotr %d", result)
+ return
}
}
@@ -45,16 +45,16 @@ var testTable = []uint32{
// Test that the cipher context is initialised correctly
func TestCipherInit(t *testing.T) {
- c, err := NewCipher(testKey);
+ c, err := NewCipher(testKey)
if err != nil {
- t.Errorf("NewCipher(%d bytes) = %s", len(testKey), err);
- return;
+ t.Errorf("NewCipher(%d bytes) = %s", len(testKey), err)
+ return
}
for i := 0; i < len(c.table); i++ {
if c.table[i] != testTable[i] {
- t.Errorf("NewCipher() failed to initialise Cipher.table[%d] correctly. Expected %08X, got %08X", i, testTable[i], c.table[i]);
- break;
+ t.Errorf("NewCipher() failed to initialise Cipher.table[%d] correctly. Expected %08X, got %08X", i, testTable[i], c.table[i])
+ break
}
}
}
@@ -65,17 +65,17 @@ func TestInvalidKeySize(t *testing.T) {
key := []byte{
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
- };
+ }
- _, err := NewCipher(key);
+ _, err := NewCipher(key)
if err == nil {
t.Errorf("Invalid key size %d didn't result in an error.", len(key))
}
// Test a short key
- key = []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
+ key = []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}
- _, err = NewCipher(key);
+ _, err = NewCipher(key)
if err == nil {
t.Errorf("Invalid key size %d didn't result in an error.", len(key))
}
@@ -83,51 +83,51 @@ func TestInvalidKeySize(t *testing.T) {
// Test that we can correctly decode some bytes we have encoded
func TestEncodeDecode(t *testing.T) {
- original := []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF};
- input := original;
- output := make([]byte, BlockSize);
+ original := []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}
+ input := original
+ output := make([]byte, BlockSize)
- c, err := NewCipher(testKey);
+ c, err := NewCipher(testKey)
if err != nil {
- t.Errorf("NewCipher(%d bytes) = %s", len(testKey), err);
- return;
+ t.Errorf("NewCipher(%d bytes) = %s", len(testKey), err)
+ return
}
// Encrypt the input block
- c.Encrypt(input, output);
+ c.Encrypt(input, output)
// Check that the output does not match the input
- differs := false;
+ differs := false
for i := 0; i < len(input); i++ {
if output[i] != input[i] {
- differs = true;
- break;
+ differs = true
+ break
}
}
if differs == false {
- t.Error("Cipher.Encrypt: Failed to encrypt the input block.");
- return;
+ t.Error("Cipher.Encrypt: Failed to encrypt the input block.")
+ return
}
// Decrypt the block we just encrypted
- input = output;
- output = make([]byte, BlockSize);
- c.Decrypt(input, output);
+ input = output
+ output = make([]byte, BlockSize)
+ c.Decrypt(input, output)
// Check that the output from decrypt matches our initial input
for i := 0; i < len(input); i++ {
if output[i] != original[i] {
- t.Errorf("Decrypted byte %d differed. Expected %02X, got %02X\n", i, original[i], output[i]);
- return;
+ t.Errorf("Decrypted byte %d differed. Expected %02X, got %02X\n", i, original[i], output[i])
+ return
}
}
}
// Test Vectors
type CryptTest struct {
- key []byte;
- plainText []byte;
- cipherText []byte;
+ key []byte
+ plainText []byte
+ cipherText []byte
}
var CryptTests = []CryptTest{
@@ -189,19 +189,19 @@ var CryptTests = []CryptTest{
// Test encryption
func TestCipherEncrypt(t *testing.T) {
for i, tt := range CryptTests {
- c, err := NewCipher(tt.key);
+ c, err := NewCipher(tt.key)
if err != nil {
- t.Errorf("NewCipher(%d bytes), vector %d = %s", len(tt.key), i, err);
- continue;
+ t.Errorf("NewCipher(%d bytes), vector %d = %s", len(tt.key), i, err)
+ continue
}
- out := make([]byte, len(tt.plainText));
- c.Encrypt(tt.plainText, out);
+ out := make([]byte, len(tt.plainText))
+ c.Encrypt(tt.plainText, out)
for j := 0; j < len(out); j++ {
if out[j] != tt.cipherText[j] {
- t.Errorf("Cipher.Encrypt %d: out[%d] = %02X, expected %02X", i, j, out[j], tt.cipherText[j]);
- break;
+ t.Errorf("Cipher.Encrypt %d: out[%d] = %02X, expected %02X", i, j, out[j], tt.cipherText[j])
+ break
}
}
}
@@ -210,19 +210,19 @@ func TestCipherEncrypt(t *testing.T) {
// Test decryption
func TestCipherDecrypt(t *testing.T) {
for i, tt := range CryptTests {
- c, err := NewCipher(tt.key);
+ c, err := NewCipher(tt.key)
if err != nil {
- t.Errorf("NewCipher(%d bytes), vector %d = %s", len(tt.key), i, err);
- continue;
+ t.Errorf("NewCipher(%d bytes), vector %d = %s", len(tt.key), i, err)
+ continue
}
- out := make([]byte, len(tt.cipherText));
- c.Decrypt(tt.cipherText, out);
+ out := make([]byte, len(tt.cipherText))
+ c.Decrypt(tt.cipherText, out)
for j := 0; j < len(out); j++ {
if out[j] != tt.plainText[j] {
- t.Errorf("Cipher.Decrypt %d: out[%d] = %02X, expected %02X", i, j, out[j], tt.plainText[j]);
- break;
+ t.Errorf("Cipher.Decrypt %d: out[%d] = %02X, expected %02X", i, j, out[j], tt.plainText[j])
+ break
}
}
}
@@ -230,17 +230,17 @@ func TestCipherDecrypt(t *testing.T) {
// Test resetting the cipher context
func TestReset(t *testing.T) {
- c, err := NewCipher(testKey);
+ c, err := NewCipher(testKey)
if err != nil {
- t.Errorf("NewCipher(%d bytes) = %s", len(testKey), err);
- return;
+ t.Errorf("NewCipher(%d bytes) = %s", len(testKey), err)
+ return
}
- c.Reset();
+ c.Reset()
for i := 0; i < len(c.table); i++ {
if c.table[i] != 0 {
- t.Errorf("Cipher.Reset: Failed to clear Cipher.table[%d]. expected 0, got %08X", i, c.table[i]);
- return;
+ t.Errorf("Cipher.Reset: Failed to clear Cipher.table[%d]. expected 0, got %08X", i, c.table[i])
+ return
}
}
}