summary refs log tree commit diff
path: root/crypto/src/crypto/engines/XTEAEngine.cs
blob: 5fcfa4a57f59099d5ad77e5ea2410cfa5b14cb37 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;

using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto.Utilities;
using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Crypto.Engines
{
	/**
	* An XTEA engine.
	*/
	public class XteaEngine
		: IBlockCipher
	{
		private const int
			rounds		= 32,
			block_size	= 8,
//			key_size	= 16,
			delta		= unchecked((int) 0x9E3779B9);

		/*
		* the expanded key array of 4 subkeys
		*/
		private uint[] _S = new uint[4],
			_sum0 = new uint[32],
			_sum1 = new uint[32];
		private bool _initialised, _forEncryption;

		/**
		* Create an instance of the TEA encryption algorithm
		* and set some defaults
		*/
		public XteaEngine()
		{
			_initialised = false;
		}

		public virtual string AlgorithmName
		{
			get { return "XTEA"; }
		}

        public virtual bool IsPartialBlockOkay
		{
			get { return false; }
		}

        public virtual int GetBlockSize()
		{
			return block_size;
		}

		/**
		* initialise
		*
		* @param forEncryption whether or not we are for encryption.
		* @param params the parameters required to set up the cipher.
		* @exception ArgumentException if the params argument is
		* inappropriate.
		*/
        public virtual void Init(
			bool				forEncryption,
			ICipherParameters	parameters)
		{
			if (!(parameters is KeyParameter))
			{
				throw new ArgumentException("invalid parameter passed to TEA init - "
					+ Platform.GetTypeName(parameters));
			}

			_forEncryption = forEncryption;
			_initialised = true;

			KeyParameter p = (KeyParameter) parameters;

			setKey(p.GetKey());
		}

        public virtual int ProcessBlock(
			byte[]	inBytes,
			int		inOff,
			byte[]	outBytes,
			int		outOff)
		{
			if (!_initialised)
				throw new InvalidOperationException(AlgorithmName + " not initialised");

            Check.DataLength(inBytes, inOff, block_size, "input buffer too short");
            Check.OutputLength(outBytes, outOff, block_size, "output buffer too short");

            return _forEncryption
				?	encryptBlock(inBytes, inOff, outBytes, outOff)
				:	decryptBlock(inBytes, inOff, outBytes, outOff);
		}

        public virtual void Reset()
		{
		}

		/**
		* Re-key the cipher.
		*
		* @param  key  the key to be used
		*/
		private void setKey(
			byte[] key)
		{
			int i, j;
			for (i = j = 0; i < 4; i++,j+=4)
			{
				_S[i] = Pack.BE_To_UInt32(key, j);
			}

			for (i = j = 0; i < rounds; i++)
			{
				_sum0[i] = ((uint)j + _S[j & 3]);
				j += delta;
				_sum1[i] = ((uint)j + _S[j >> 11 & 3]);
			}
		}

		private int encryptBlock(
			byte[]  inBytes,
			int     inOff,
			byte[]  outBytes,
			int     outOff)
		{
			// Pack bytes into integers
			uint v0 = Pack.BE_To_UInt32(inBytes, inOff);
			uint v1 = Pack.BE_To_UInt32(inBytes, inOff + 4);

			for (int i = 0; i < rounds; i++)
			{
				v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ _sum0[i];
				v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ _sum1[i];
			}

			Pack.UInt32_To_BE(v0, outBytes, outOff);
			Pack.UInt32_To_BE(v1, outBytes, outOff + 4);

			return block_size;
		}

		private int decryptBlock(
			byte[]	inBytes,
			int		inOff,
			byte[]	outBytes,
			int		outOff)
		{
			// Pack bytes into integers
			uint v0 = Pack.BE_To_UInt32(inBytes, inOff);
			uint v1 = Pack.BE_To_UInt32(inBytes, inOff + 4);

			for (int i = rounds-1; i >= 0; i--)
			{
				v1  -= ((v0 << 4 ^ v0 >> 5) + v0) ^ _sum1[i];
				v0  -= ((v1 << 4 ^ v1 >> 5) + v1) ^ _sum0[i];
			}

			Pack.UInt32_To_BE(v0, outBytes, outOff);
			Pack.UInt32_To_BE(v1, outBytes, outOff + 4);

			return block_size;
		}
	}
}