Mercurial > trustbridge > nss-cmake-static
comparison nss/lib/freebl/chacha20/chacha20.c @ 0:1e5118fa0cb1
This is NSS with a Cmake Buildsyste
To compile a static NSS library for Windows we've used the
Chromium-NSS fork and added a Cmake buildsystem to compile
it statically for Windows. See README.chromium for chromium
changes and README.trustbridge for our modifications.
author | Andre Heinecke <andre.heinecke@intevation.de> |
---|---|
date | Mon, 28 Jul 2014 10:47:06 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:1e5118fa0cb1 |
---|---|
1 /* This Source Code Form is subject to the terms of the Mozilla Public | |
2 * License, v. 2.0. If a copy of the MPL was not distributed with this | |
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |
4 | |
5 /* Adopted from the public domain code in NaCl by djb. */ | |
6 | |
7 #include <string.h> | |
8 #include <stdio.h> | |
9 | |
10 #include "prtypes.h" | |
11 #include "chacha20.h" | |
12 | |
13 #define ROTL32(v, n) (((v) << (n)) | ((v) >> (32 - (n)))) | |
14 #define ROTATE(v, c) ROTL32((v), (c)) | |
15 #define XOR(v, w) ((v) ^ (w)) | |
16 #define PLUS(x, y) ((x) + (y)) | |
17 | |
18 #define U32TO8_LITTLE(p, v) \ | |
19 { (p)[0] = ((v) ) & 0xff; (p)[1] = ((v) >> 8) & 0xff; \ | |
20 (p)[2] = ((v) >> 16) & 0xff; (p)[3] = ((v) >> 24) & 0xff; } | |
21 #define U8TO32_LITTLE(p) \ | |
22 (((PRUint32)((p)[0]) ) | ((PRUint32)((p)[1]) << 8) | \ | |
23 ((PRUint32)((p)[2]) << 16) | ((PRUint32)((p)[3]) << 24) ) | |
24 | |
25 #define QUARTERROUND(a,b,c,d) \ | |
26 x[a] = PLUS(x[a],x[b]); x[d] = ROTATE(XOR(x[d],x[a]),16); \ | |
27 x[c] = PLUS(x[c],x[d]); x[b] = ROTATE(XOR(x[b],x[c]),12); \ | |
28 x[a] = PLUS(x[a],x[b]); x[d] = ROTATE(XOR(x[d],x[a]), 8); \ | |
29 x[c] = PLUS(x[c],x[d]); x[b] = ROTATE(XOR(x[b],x[c]), 7); | |
30 | |
31 static void ChaChaCore(unsigned char output[64], const PRUint32 input[16], | |
32 int num_rounds) { | |
33 PRUint32 x[16]; | |
34 int i; | |
35 | |
36 memcpy(x, input, sizeof(PRUint32) * 16); | |
37 for (i = num_rounds; i > 0; i -= 2) { | |
38 QUARTERROUND( 0, 4, 8,12) | |
39 QUARTERROUND( 1, 5, 9,13) | |
40 QUARTERROUND( 2, 6,10,14) | |
41 QUARTERROUND( 3, 7,11,15) | |
42 QUARTERROUND( 0, 5,10,15) | |
43 QUARTERROUND( 1, 6,11,12) | |
44 QUARTERROUND( 2, 7, 8,13) | |
45 QUARTERROUND( 3, 4, 9,14) | |
46 } | |
47 | |
48 for (i = 0; i < 16; ++i) { | |
49 x[i] = PLUS(x[i], input[i]); | |
50 } | |
51 for (i = 0; i < 16; ++i) { | |
52 U32TO8_LITTLE(output + 4 * i, x[i]); | |
53 } | |
54 } | |
55 | |
56 static const unsigned char sigma[16] = "expand 32-byte k"; | |
57 | |
58 void ChaCha20XOR(unsigned char *out, const unsigned char *in, unsigned int inLen, | |
59 const unsigned char key[32], const unsigned char nonce[8], | |
60 uint64_t counter) { | |
61 unsigned char block[64]; | |
62 PRUint32 input[16]; | |
63 unsigned int u; | |
64 unsigned int i; | |
65 | |
66 input[4] = U8TO32_LITTLE(key + 0); | |
67 input[5] = U8TO32_LITTLE(key + 4); | |
68 input[6] = U8TO32_LITTLE(key + 8); | |
69 input[7] = U8TO32_LITTLE(key + 12); | |
70 | |
71 input[8] = U8TO32_LITTLE(key + 16); | |
72 input[9] = U8TO32_LITTLE(key + 20); | |
73 input[10] = U8TO32_LITTLE(key + 24); | |
74 input[11] = U8TO32_LITTLE(key + 28); | |
75 | |
76 input[0] = U8TO32_LITTLE(sigma + 0); | |
77 input[1] = U8TO32_LITTLE(sigma + 4); | |
78 input[2] = U8TO32_LITTLE(sigma + 8); | |
79 input[3] = U8TO32_LITTLE(sigma + 12); | |
80 | |
81 input[12] = counter; | |
82 input[13] = counter >> 32; | |
83 input[14] = U8TO32_LITTLE(nonce + 0); | |
84 input[15] = U8TO32_LITTLE(nonce + 4); | |
85 | |
86 while (inLen >= 64) { | |
87 ChaChaCore(block, input, 20); | |
88 for (i = 0; i < 64; i++) { | |
89 out[i] = in[i] ^ block[i]; | |
90 } | |
91 | |
92 input[12]++; | |
93 if (input[12] == 0) { | |
94 input[13]++; | |
95 } | |
96 | |
97 inLen -= 64; | |
98 in += 64; | |
99 out += 64; | |
100 } | |
101 | |
102 if (inLen > 0) { | |
103 ChaChaCore(block, input, 20); | |
104 for (i = 0; i < inLen; i++) { | |
105 out[i] = in[i] ^ block[i]; | |
106 } | |
107 } | |
108 } |