github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/third_party/code.google.com/p/go.crypto/xtea/cipher.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package xtea implements XTEA encryption, as defined in Needham and Wheeler's
     6  // 1997 technical report, "Tea extensions."
     7  package xtea
     8  
     9  // For details, see http://www.cix.co.uk/~klockstone/xtea.pdf
    10  
    11  import "strconv"
    12  
    13  // The XTEA block size in bytes.
    14  const BlockSize = 8
    15  
    16  // A Cipher is an instance of an XTEA cipher using a particular key.
    17  // table contains a series of precalculated values that are used each round.
    18  type Cipher struct {
    19  	table [64]uint32
    20  }
    21  
    22  type KeySizeError int
    23  
    24  func (k KeySizeError) Error() string {
    25  	return "crypto/xtea: invalid key size " + strconv.Itoa(int(k))
    26  }
    27  
    28  // NewCipher creates and returns a new Cipher.
    29  // The key argument should be the XTEA key.
    30  // XTEA only supports 128 bit (16 byte) keys.
    31  func NewCipher(key []byte) (*Cipher, error) {
    32  	k := len(key)
    33  	switch k {
    34  	default:
    35  		return nil, KeySizeError(k)
    36  	case 16:
    37  		break
    38  	}
    39  
    40  	c := new(Cipher)
    41  	initCipher(c, key)
    42  
    43  	return c, nil
    44  }
    45  
    46  // BlockSize returns the XTEA block size, 8 bytes.
    47  // It is necessary to satisfy the Block interface in the
    48  // package "crypto/cipher".
    49  func (c *Cipher) BlockSize() int { return BlockSize }
    50  
    51  // Encrypt encrypts the 8 byte buffer src using the key and stores the result in dst.
    52  // Note that for amounts of data larger than a block,
    53  // it is not safe to just call Encrypt on successive blocks;
    54  // instead, use an encryption mode like CBC (see crypto/cipher/cbc.go).
    55  func (c *Cipher) Encrypt(dst, src []byte) { encryptBlock(c, dst, src) }
    56  
    57  // Decrypt decrypts the 8 byte buffer src using the key k and stores the result in dst.
    58  func (c *Cipher) Decrypt(dst, src []byte) { decryptBlock(c, dst, src) }
    59  
    60  // Reset zeros the table, so that it will no longer appear in the process's memory.
    61  func (c *Cipher) Reset() {
    62  	for i := 0; i < len(c.table); i++ {
    63  		c.table[i] = 0
    64  	}
    65  }
    66  
    67  // initCipher initializes the cipher context by creating a look up table
    68  // of precalculated values that are based on the key.
    69  func initCipher(c *Cipher, key []byte) {
    70  	// Load the key into four uint32s
    71  	var k [4]uint32
    72  	for i := 0; i < len(k); i++ {
    73  		j := i << 2 // Multiply by 4
    74  		k[i] = uint32(key[j+0])<<24 | uint32(key[j+1])<<16 | uint32(key[j+2])<<8 | uint32(key[j+3])
    75  	}
    76  
    77  	// Precalculate the table
    78  	const delta = 0x9E3779B9
    79  	var sum uint32 = 0
    80  
    81  	// Two rounds of XTEA applied per loop
    82  	for i := 0; i < numRounds; {
    83  		c.table[i] = sum + k[sum&3]
    84  		i++
    85  		sum += delta
    86  		c.table[i] = sum + k[(sum>>11)&3]
    87  		i++
    88  	}
    89  }