github.com/maenmax/kairep@v0.0.0-20210218001208-55bf3df36788/src/golang.org/x/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 // import "golang.org/x/crypto/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  // initCipher initializes the cipher context by creating a look up table
    61  // of precalculated values that are based on the key.
    62  func initCipher(c *Cipher, key []byte) {
    63  	// Load the key into four uint32s
    64  	var k [4]uint32
    65  	for i := 0; i < len(k); i++ {
    66  		j := i << 2 // Multiply by 4
    67  		k[i] = uint32(key[j+0])<<24 | uint32(key[j+1])<<16 | uint32(key[j+2])<<8 | uint32(key[j+3])
    68  	}
    69  
    70  	// Precalculate the table
    71  	const delta = 0x9E3779B9
    72  	var sum uint32 = 0
    73  
    74  	// Two rounds of XTEA applied per loop
    75  	for i := 0; i < numRounds; {
    76  		c.table[i] = sum + k[sum&3]
    77  		i++
    78  		sum += delta
    79  		c.table[i] = sum + k[(sum>>11)&3]
    80  		i++
    81  	}
    82  }