1
2 #include <stdint.h>
3 #include <stdlib.h>
4
5 #include "crypto_core_hchacha20.h"
6 #include "private/common.h"
7
8 #define QUARTERROUND(A, B, C, D) \
9 do { \
10 A += B; D = ROTL32(D ^ A, 16); \
11 C += D; B = ROTL32(B ^ C, 12); \
12 A += B; D = ROTL32(D ^ A, 8); \
13 C += D; B = ROTL32(B ^ C, 7); \
14 } while(0)
15
16 int
crypto_core_hchacha20(unsigned char * out,const unsigned char * in,const unsigned char * k,const unsigned char * c)17 crypto_core_hchacha20(unsigned char *out, const unsigned char *in,
18 const unsigned char *k, const unsigned char *c)
19 {
20 int i;
21 uint32_t x0, x1, x2, x3, x4, x5, x6, x7;
22 uint32_t x8, x9, x10, x11, x12, x13, x14, x15;
23
24 if (c == NULL) {
25 x0 = 0x61707865;
26 x1 = 0x3320646e;
27 x2 = 0x79622d32;
28 x3 = 0x6b206574;
29 } else {
30 x0 = LOAD32_LE(c + 0);
31 x1 = LOAD32_LE(c + 4);
32 x2 = LOAD32_LE(c + 8);
33 x3 = LOAD32_LE(c + 12);
34 }
35 x4 = LOAD32_LE(k + 0);
36 x5 = LOAD32_LE(k + 4);
37 x6 = LOAD32_LE(k + 8);
38 x7 = LOAD32_LE(k + 12);
39 x8 = LOAD32_LE(k + 16);
40 x9 = LOAD32_LE(k + 20);
41 x10 = LOAD32_LE(k + 24);
42 x11 = LOAD32_LE(k + 28);
43 x12 = LOAD32_LE(in + 0);
44 x13 = LOAD32_LE(in + 4);
45 x14 = LOAD32_LE(in + 8);
46 x15 = LOAD32_LE(in + 12);
47
48 for (i = 0; i < 10; i++) {
49 QUARTERROUND(x0, x4, x8, x12);
50 QUARTERROUND(x1, x5, x9, x13);
51 QUARTERROUND(x2, x6, x10, x14);
52 QUARTERROUND(x3, x7, x11, x15);
53 QUARTERROUND(x0, x5, x10, x15);
54 QUARTERROUND(x1, x6, x11, x12);
55 QUARTERROUND(x2, x7, x8, x13);
56 QUARTERROUND(x3, x4, x9, x14);
57 }
58
59 STORE32_LE(out + 0, x0);
60 STORE32_LE(out + 4, x1);
61 STORE32_LE(out + 8, x2);
62 STORE32_LE(out + 12, x3);
63 STORE32_LE(out + 16, x12);
64 STORE32_LE(out + 20, x13);
65 STORE32_LE(out + 24, x14);
66 STORE32_LE(out + 28, x15);
67
68 return 0;
69 }
70
71 size_t
crypto_core_hchacha20_outputbytes(void)72 crypto_core_hchacha20_outputbytes(void)
73 {
74 return crypto_core_hchacha20_OUTPUTBYTES;
75 }
76
77 size_t
crypto_core_hchacha20_inputbytes(void)78 crypto_core_hchacha20_inputbytes(void)
79 {
80 return crypto_core_hchacha20_INPUTBYTES;
81 }
82
83 size_t
crypto_core_hchacha20_keybytes(void)84 crypto_core_hchacha20_keybytes(void)
85 {
86 return crypto_core_hchacha20_KEYBYTES;
87 }
88
89 size_t
crypto_core_hchacha20_constbytes(void)90 crypto_core_hchacha20_constbytes(void)
91 {
92 return crypto_core_hchacha20_CONSTBYTES;
93 }
94