Introduction
I started refactoring my project to meet Go cipher interfaces and thought it would be interesting to share my new implementation of TQ's XOR cipher. I'm not entirely sure if my documentation/analysis is correct for TQ's cipher(primarily around it being a variant of RC4), but hopefully I can get some feedback on that and make some corrections if need be. Thanks, and happy holidays!
Implementation
I started refactoring my project to meet Go cipher interfaces and thought it would be interesting to share my new implementation of TQ's XOR cipher. I'm not entirely sure if my documentation/analysis is correct for TQ's cipher
Implementation
Code:
// Package tq implements encryption using TQ Digital Entertainments in-house
// developed symmetric, counter-based XOR-cipher. Counters are separated by
// encryption direction, thus two instances are required.
//
// Copyright 2016-2017 Gareth Warry. All rights reserved.
// Use of this source code is governed by an MIT-style license that can be
// found in the LICENSE file at the root of the project.
package tq
// BUG(agl): TQ's XOR Cipher is designed with multiple weaknesses.
// Key scheduling uses no NP-hard calculations or random arguments. The
// keystream is never modified after key scheduling, and encryption only relies
// on a counter as feedback. This cipher is a poor choice for new protocols.
import "strconv"
// A cipher is an instance of TQ's xor-cipher using a particular key.
type Cipher struct {
key1 [0x100]byte
key2 [0x100]byte
x uint16
}
// NewCipher creates and returns a new Cipher of TQ's XOR-cipher. The key
// argument should be the cipher's generator / seed of 8 bytes, static across
// all client connections: { 0x9D, 0x0F, 0xFA, 0x13, 0x62, 0x79, 0x5C, 0x6D }.
// Two must be created to use as stream ciphers (decryption and encryption).
func NewCipher(key []byte) (*Cipher, error) {
if len(key) == 8 {
cipher := new(Cipher)
for i := range cipher.key1 {
cipher.key1[i] = key[0]
cipher.key2[i] = key[4]
key[0] = (key[1] + byte(key[0] * key[2])) * key[0] + key[3]
key[4] = (key[5] - byte(key[4] * key[6])) * key[4] + key[7]
}
return cipher, nil
} else { return nil, KeySizeError(len(key)) }
}
// KeySizeError is called when generating the Cipher's keystream using an
// initial key of an invalid size (only supports 8 bytes in length).
type KeySizeError int
func (k KeySizeError) Error() string {
return "security/tq: invalid key size " + strconv.Itoa(int(k))
}
// Copy replaces keystream and counter values of a child TQ Cipher instance
// with the values from this Cipher instance. Can be used after generating keys
// to create a second cipher stream (required for the opposite direction).
func (c *Cipher) Copy(child *Cipher) {
copy(child.key1[:], c.key1[:])
copy(child.key2[:], c.key2[:])
child.x = c.x
}
// XORKeyStream sets dst with the result of XORing src with the key stream.
// Dst and src may be the same slice, but otherwise should not overlap.
func (c *Cipher) XORKeyStream(dst, src []byte) {
for i, v := range src {
dst[i] = v ^ 0xAB
dst[i] = dst[i] >> 4 | dst[i] << 4
dst[i] = dst[i] ^ c.key1[byte(c.x & 0xff)]
dst[i] = dst[i] ^ c.key2[byte(c.x >> 8)]
c.x++
}
}