VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Crypto/chacha256.c
blob: f32e607b37b3bc858349be81f2dcf599ea2e0670 (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
This code is written by kerukuro for cppcrypto library (http://cppcrypto.sourceforge.net/)
and released into public domain.
*/

/* adapted for VeraCrypt */

#include "chacha256.h"
#include "cpu.h"
#include "misc.h"



#define rotater32(x,n)	rotr32(x, n)
#define rotatel32(x,n)	rotl32(x, n)

#if CRYPTOPP_BOOL_SSE2_INTRINSICS_AVAILABLE
void chacha_ECRYPT_encrypt_bytes(size_t bytes, uint32* x, const unsigned char* m, unsigned char* out, unsigned char* output, unsigned int r);
#endif

static VC_INLINE void xor_block_512(const unsigned char* in, const unsigned char* prev, unsigned char* out)
{
#if CRYPTOPP_BOOL_SSE2_INTRINSICS_AVAILABLE && !defined(_UEFI) && (!defined (TC_WINDOWS_DRIVER) || (!defined (DEBUG) && defined (_WIN64)))
    if (HasSSE2())
    {
        __m128i b1 = _mm_loadu_si128((const __m128i*) in);
        __m128i p1 = _mm_loadu_si128((const __m128i*) prev);
        __m128i b2 = _mm_loadu_si128((const __m128i*) (in + 16));
        __m128i p2 = _mm_loadu_si128((const __m128i*) (prev + 16));

        _mm_storeu_si128((__m128i*) out, _mm_xor_si128(b1, p1));
        _mm_storeu_si128((__m128i*) (out + 16), _mm_xor_si128(b2, p2));

        b1 = _mm_loadu_si128((const __m128i*) (in + 32));
        p1 = _mm_loadu_si128((const __m128i*) (prev + 32));
        b2 = _mm_loadu_si128((const __m128i*) (in + 48));
        p2 = _mm_loadu_si128((const __m128i*) (prev + 48));

        _mm_storeu_si128((__m128i*) (out + 32), _mm_xor_si128(b1, p1));
        _mm_storeu_si128((__m128i*) (out + 48), _mm_xor_si128(b2, p2));

    }
    else
#endif
	{
		int i;
        for (i = 0; i < 64; i++)
            out[i] = in[i] ^ prev[i];
    }

}

static VC_INLINE void chacha_core(uint32* x, int r)
{
	int i;
    for (i = 0; i < r; i++)
    {
        x[0] += x[4];
        x[12] = rotatel32(x[12] ^ x[0], 16);
        x[8] += x[12];
        x[4] = rotatel32(x[4] ^ x[8], 12);
        x[0] += x[4];
        x[12] = rotatel32(x[12] ^ x[0], 8);
        x[8] += x[12];
        x[4] = rotatel32(x[4] ^ x[8], 7);

        x[1] += x[5];
        x[13] = rotatel32(x[13] ^ x[1], 16);
        x[9] += x[13];
        x[5] = rotatel32(x[5] ^ x[9], 12);
        x[1] += x[5];
        x[13] = rotatel32(x[13] ^ x[1], 8);
        x[9] += x[13];
        x[5] = rotatel32(x[5] ^ x[9], 7);

        x[2] += x[6];
        x[14] = rotatel32(x[14] ^ x[2], 16);
        x[10] += x[14];
        x[6] = rotatel32(x[6] ^ x[10], 12);
        x[2] += x[6];
        x[14] = rotatel32(x[14] ^ x[2], 8);
        x[10] += x[14];
        x[6] = rotatel32(x[6] ^ x[10], 7);

        x[3] += x[7];
        x[15] = rotatel32(x[15] ^ x[3], 16);
        x[11] += x[15];
        x[7] = rotatel32(x[7] ^ x[11], 12);
        x[3] += x[7];
        x[15] = rotatel32(x[15] ^ x[3], 8);
        x[11] += x[15];
        x[7] = rotatel32(x[7] ^ x[11], 7);

        x[0] += x[5];
        x[15] = rotatel32(x[15] ^ x[0], 16);
        x[10] += x[15];
        x[5] = rotatel32(x[5] ^ x[10], 12);
        x[0] += x[5];
        x[15] = rotatel32(x[15] ^ x[0], 8);
        x[10] += x[15];
        x[5] = rotatel32(x[5] ^ x[10], 7);

        x[1] += x[6];
        x[12] = rotatel32(x[12] ^ x[1], 16);
        x[11] += x[12];
        x[6] = rotatel32(x[6] ^ x[11], 12);
        x[1] += x[6];
        x[12] = rotatel32(x[12] ^ x[1], 8);
        x[11] += x[12];
        x[6] = rotatel32(x[6] ^ x[11], 7);

        x[2] += x[7];
        x[13] = rotatel32(x[13] ^ x[2], 16);
        x[8] += x[13];
        x[7] = rotatel32(x[7] ^ x[8], 12);
        x[2] += x[7];
        x[13] = rotatel32(x[13] ^ x[2], 8);
        x[8] += x[13];
        x[7] = rotatel32(x[7] ^ x[8], 7);

        x[3] += x[4];
        x[14] = rotatel32(x[14] ^ x[3], 16);
        x[9] += x[14];
        x[4] = rotatel32(x[4] ^ x[9], 12);
        x[3] += x[4];
        x[14] = rotatel32(x[14] ^ x[3], 8);
        x[9] += x[14];
        x[4] = rotatel32(x[4] ^ x[9], 7);
    }
}

static VC_INLINE void chacha_hash(const uint32* in, uint32* out, int r)
{
    uint32 x[16];
	int i;
    memcpy(x, in, 64);
    chacha_core(x, r);
    for (i = 0; i < 16; ++i)
        out[i] = x[i] + in[i];
}

static VC_INLINE void incrementSalsaCounter(uint32* input, uint32* block, int r)
{
    chacha_hash(input, block, r);
    if (!++input[12])
        ++input[13];
}

static VC_INLINE void do_encrypt(const unsigned char* in, size_t len, unsigned char* out, int r, size_t* posPtr, uint32* input, uint32* block)
{
    size_t i = 0, pos = *posPtr;
    if (pos)
    {
        while (pos < len && pos < 64)
        {
            out[i] = in[i] ^ ((unsigned char*)block)[pos++];
            ++i;
        }
        len -= i;
    }
    if (len)
        pos = 0;

#if CRYPTOPP_SSSE3_AVAILABLE && !defined(_UEFI) && (!defined (TC_WINDOWS_DRIVER) || (!defined (DEBUG) && defined (_WIN64)))
    if (HasSSSE3())
    {
        size_t fullblocks = len - len % 64;
        if (fullblocks)
        {
            chacha_ECRYPT_encrypt_bytes(fullblocks, input, in + i, out + i, (unsigned char*)block, r);
            i += fullblocks;
            len -= fullblocks;
        }
        if (len)
        {
            chacha_ECRYPT_encrypt_bytes(len, input, in + i, out + i, (unsigned char*)block, r);
            pos = len;
        }
        *posPtr = pos;
        return;
    }
#endif

    for (; len; len -= VC_MIN(64, len))
    {
        incrementSalsaCounter(input, block, r);
        if (len >= 64)
        {
            xor_block_512(in + i, (unsigned char*)block, out + i);
            i += 64;
        }
        else
        {
            for (; pos < len; pos++, i++)
                out[i] = in[i] ^ ((unsigned char*)block)[pos];
        }
    }
    *posPtr = pos;
}

void ChaCha256Init(ChaCha256Ctx* ctx, const unsigned char* key, const unsigned char* iv, int rounds)
{    
    ctx->internalRounds = rounds / 2;
    ctx->pos = 0;
    
    ctx->input_[12] = 0;
    ctx->input_[13] = 0;
    memcpy(ctx->input_ + 4, key, 32);
    memcpy(ctx->input_ + 14, iv, 8);
    ctx->input_[0] = 0x61707865;
    ctx->input_[1] = 0x3320646E;
    ctx->input_[2] = 0x79622D32;
    ctx->input_[3] = 0x6B206574;
}

void ChaCha256Encrypt(ChaCha256Ctx* ctx, const unsigned char* in, size_t len, unsigned char* out)
{
    do_encrypt(in, len, out, ctx->internalRounds, &ctx->pos, ctx->input_, ctx->block_);
}