~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to src/org/bouncycastle/crypto/engines/TEAEngine.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:20:32 UTC
  • Revision ID: brian.thomason@canonical.com-20111220172032-rdtm13jgdxtksacr
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.bouncycastle.crypto.engines;
 
2
 
 
3
import org.bouncycastle.crypto.BlockCipher;
 
4
import org.bouncycastle.crypto.CipherParameters;
 
5
import org.bouncycastle.crypto.DataLengthException;
 
6
import org.bouncycastle.crypto.params.KeyParameter;
 
7
 
 
8
/**
 
9
 * An TEA engine.
 
10
 */
 
11
public class TEAEngine
 
12
    implements BlockCipher
 
13
{
 
14
    private static final int rounds     = 32,
 
15
                             block_size = 8,
 
16
//                             key_size   = 16,
 
17
                             delta      = 0x9E3779B9,
 
18
                             d_sum      = 0xC6EF3720; // sum on decrypt
 
19
    /*
 
20
     * the expanded key array of 4 subkeys
 
21
     */
 
22
    private int _a, _b, _c, _d;
 
23
    private boolean _initialised;
 
24
    private boolean _forEncryption;
 
25
 
 
26
    /**
 
27
     * Create an instance of the TEA encryption algorithm
 
28
     * and set some defaults
 
29
     */
 
30
    public TEAEngine()
 
31
    {
 
32
        _initialised = false;
 
33
    }
 
34
 
 
35
    public String getAlgorithmName()
 
36
    {
 
37
        return "TEA";
 
38
    }
 
39
 
 
40
    public int getBlockSize()
 
41
    {
 
42
        return block_size;
 
43
    }
 
44
 
 
45
    /**
 
46
     * initialise
 
47
     *
 
48
     * @param forEncryption whether or not we are for encryption.
 
49
     * @param params the parameters required to set up the cipher.
 
50
     * @exception IllegalArgumentException if the params argument is
 
51
     * inappropriate.
 
52
     */
 
53
    public void init(
 
54
        boolean             forEncryption,
 
55
        CipherParameters    params)
 
56
    {
 
57
        if (!(params instanceof KeyParameter))
 
58
        {
 
59
            throw new IllegalArgumentException("invalid parameter passed to TEA init - " + params.getClass().getName());
 
60
        }
 
61
 
 
62
        _forEncryption = forEncryption;
 
63
        _initialised = true;
 
64
 
 
65
        KeyParameter       p = (KeyParameter)params;
 
66
 
 
67
        setKey(p.getKey());
 
68
    }
 
69
 
 
70
    public int processBlock(
 
71
        byte[]  in,
 
72
        int     inOff,
 
73
        byte[]  out,
 
74
        int     outOff)
 
75
    {
 
76
        if (!_initialised)
 
77
        {
 
78
            throw new IllegalStateException(getAlgorithmName()+" not initialised");
 
79
        }
 
80
        
 
81
        if ((inOff + block_size) > in.length)
 
82
        {
 
83
            throw new DataLengthException("input buffer too short");
 
84
        }
 
85
        
 
86
        if ((outOff + block_size) > out.length)
 
87
        {
 
88
            throw new DataLengthException("output buffer too short");
 
89
        }
 
90
        
 
91
        return (_forEncryption) ? encryptBlock(in, inOff, out, outOff)
 
92
                                    : decryptBlock(in, inOff, out, outOff);
 
93
    }
 
94
 
 
95
    public void reset()
 
96
    {
 
97
    }
 
98
 
 
99
    /**
 
100
     * Re-key the cipher.
 
101
     * <p>
 
102
     * @param  key  the key to be used
 
103
     */
 
104
    private void setKey(
 
105
        byte[]      key)
 
106
    {
 
107
        _a = bytesToInt(key, 0);
 
108
        _b = bytesToInt(key, 4);
 
109
        _c = bytesToInt(key, 8);
 
110
        _d = bytesToInt(key, 12);
 
111
    }
 
112
 
 
113
    private int encryptBlock(
 
114
        byte[]  in,
 
115
        int     inOff,
 
116
        byte[]  out,
 
117
        int     outOff)
 
118
    {
 
119
        // Pack bytes into integers
 
120
        int v0 = bytesToInt(in, inOff);
 
121
        int v1 = bytesToInt(in, inOff + 4);
 
122
        
 
123
        int sum = 0;
 
124
        
 
125
        for (int i = 0; i != rounds; i++)
 
126
        {
 
127
            sum += delta;
 
128
            v0  += ((v1 << 4) + _a) ^ (v1 + sum) ^ ((v1 >>> 5) + _b);
 
129
            v1  += ((v0 << 4) + _c) ^ (v0 + sum) ^ ((v0 >>> 5) + _d);
 
130
        }
 
131
 
 
132
        unpackInt(v0, out, outOff);
 
133
        unpackInt(v1, out, outOff + 4);
 
134
        
 
135
        return block_size;
 
136
    }
 
137
 
 
138
    private int decryptBlock(
 
139
        byte[]  in,
 
140
        int     inOff,
 
141
        byte[]  out,
 
142
        int     outOff)
 
143
    {
 
144
        // Pack bytes into integers
 
145
        int v0 = bytesToInt(in, inOff);
 
146
        int v1 = bytesToInt(in, inOff + 4);
 
147
        
 
148
        int sum = d_sum;
 
149
        
 
150
        for (int i = 0; i != rounds; i++)
 
151
        {
 
152
            v1  -= ((v0 << 4) + _c) ^ (v0 + sum) ^ ((v0 >>> 5) + _d);
 
153
            v0  -= ((v1 << 4) + _a) ^ (v1 + sum) ^ ((v1 >>> 5) + _b);
 
154
            sum -= delta;
 
155
        }
 
156
        
 
157
        unpackInt(v0, out, outOff);
 
158
        unpackInt(v1, out, outOff + 4);
 
159
        
 
160
        return block_size;
 
161
    }
 
162
 
 
163
    private int bytesToInt(byte[] in, int inOff)
 
164
    {
 
165
        return ((in[inOff++]) << 24) |
 
166
                 ((in[inOff++] & 255) << 16) |
 
167
                 ((in[inOff++] & 255) <<  8) |
 
168
                 ((in[inOff] & 255));
 
169
    }
 
170
 
 
171
    private void unpackInt(int v, byte[] out, int outOff)
 
172
    {
 
173
        out[outOff++] = (byte)(v >>> 24);
 
174
        out[outOff++] = (byte)(v >>> 16);
 
175
        out[outOff++] = (byte)(v >>>  8);
 
176
        out[outOff  ] = (byte)v;
 
177
    }
 
178
}