/src/nettle/chacha-core-internal.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* chacha-core-internal.c |
2 | | |
3 | | Core functionality of the ChaCha stream cipher. |
4 | | Heavily based on the Salsa20 implementation in Nettle. |
5 | | |
6 | | Copyright (C) 2013 Joachim Strömbergson |
7 | | Copyright (C) 2012 Simon Josefsson, Niels Möller |
8 | | |
9 | | This file is part of GNU Nettle. |
10 | | |
11 | | GNU Nettle is free software: you can redistribute it and/or |
12 | | modify it under the terms of either: |
13 | | |
14 | | * the GNU Lesser General Public License as published by the Free |
15 | | Software Foundation; either version 3 of the License, or (at your |
16 | | option) any later version. |
17 | | |
18 | | or |
19 | | |
20 | | * the GNU General Public License as published by the Free |
21 | | Software Foundation; either version 2 of the License, or (at your |
22 | | option) any later version. |
23 | | |
24 | | or both in parallel, as here. |
25 | | |
26 | | GNU Nettle is distributed in the hope that it will be useful, |
27 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
28 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
29 | | General Public License for more details. |
30 | | |
31 | | You should have received copies of the GNU General Public License and |
32 | | the GNU Lesser General Public License along with this program. If |
33 | | not, see http://www.gnu.org/licenses/. |
34 | | */ |
35 | | |
36 | | /* Based on: |
37 | | chacha-ref.c version 2008.01.20. |
38 | | D. J. Bernstein |
39 | | Public domain. |
40 | | */ |
41 | | |
42 | | #if HAVE_CONFIG_H |
43 | | # include "config.h" |
44 | | #endif |
45 | | |
46 | | #include <assert.h> |
47 | | #include <string.h> |
48 | | |
49 | | #include "chacha.h" |
50 | | #include "chacha-internal.h" |
51 | | |
52 | | #include "macros.h" |
53 | | |
54 | | /* For fat builds */ |
55 | | #if HAVE_NATIVE_chacha_core |
56 | | void |
57 | | _nettle_chacha_core_c(uint32_t *dst, const uint32_t *src, unsigned rounds); |
58 | | #define _nettle_chacha_core _nettle_chacha_core_c |
59 | | #endif |
60 | | |
61 | | #ifndef CHACHA_DEBUG |
62 | | # define CHACHA_DEBUG 0 |
63 | | #endif |
64 | | |
65 | | #if CHACHA_DEBUG |
66 | | # include <stdio.h> |
67 | | # define DEBUG(i) do { \ |
68 | | unsigned debug_j; \ |
69 | | for (debug_j = 0; debug_j < 16; debug_j++) \ |
70 | | { \ |
71 | | if (debug_j == 0) \ |
72 | | fprintf(stderr, "%2d:", (i)); \ |
73 | | else if (debug_j % 4 == 0) \ |
74 | | fprintf(stderr, "\n "); \ |
75 | | fprintf(stderr, " %8x", x[debug_j]); \ |
76 | | } \ |
77 | | fprintf(stderr, "\n"); \ |
78 | | } while (0) |
79 | | #else |
80 | | # define DEBUG(i) |
81 | | #endif |
82 | | |
83 | | #ifdef WORDS_BIGENDIAN |
84 | | #define LE_SWAP32(v) \ |
85 | | ((ROTL32(8, v) & 0x00FF00FFUL) | \ |
86 | | (ROTL32(24, v) & 0xFF00FF00UL)) |
87 | | #else |
88 | 0 | #define LE_SWAP32(v) (v) |
89 | | #endif |
90 | | |
91 | 0 | #define QROUND(x0, x1, x2, x3) do { \ |
92 | 0 | x0 = x0 + x1; x3 = ROTL32(16, (x0 ^ x3)); \ |
93 | 0 | x2 = x2 + x3; x1 = ROTL32(12, (x1 ^ x2)); \ |
94 | 0 | x0 = x0 + x1; x3 = ROTL32(8, (x0 ^ x3)); \ |
95 | 0 | x2 = x2 + x3; x1 = ROTL32(7, (x1 ^ x2)); \ |
96 | 0 | } while(0) |
97 | | |
98 | | void |
99 | | _nettle_chacha_core(uint32_t *dst, const uint32_t *src, unsigned rounds) |
100 | 0 | { |
101 | 0 | uint32_t x[_CHACHA_STATE_LENGTH]; |
102 | 0 | unsigned i; |
103 | |
|
104 | 0 | assert ( (rounds & 1) == 0); |
105 | | |
106 | 0 | memcpy (x, src, sizeof(x)); |
107 | 0 | for (i = 0; i < rounds;i += 2) |
108 | 0 | { |
109 | 0 | DEBUG (i); |
110 | 0 | QROUND(x[0], x[4], x[8], x[12]); |
111 | 0 | QROUND(x[1], x[5], x[9], x[13]); |
112 | 0 | QROUND(x[2], x[6], x[10], x[14]); |
113 | 0 | QROUND(x[3], x[7], x[11], x[15]); |
114 | |
|
115 | 0 | DEBUG (i+1); |
116 | 0 | QROUND(x[0], x[5], x[10], x[15]); |
117 | 0 | QROUND(x[1], x[6], x[11], x[12]); |
118 | 0 | QROUND(x[2], x[7], x[8], x[13]); |
119 | 0 | QROUND(x[3], x[4], x[9], x[14]); |
120 | 0 | } |
121 | 0 | DEBUG (i); |
122 | |
|
123 | 0 | for (i = 0; i < _CHACHA_STATE_LENGTH; i++) |
124 | 0 | { |
125 | 0 | uint32_t t = x[i] + src[i]; |
126 | 0 | dst[i] = LE_SWAP32 (t); |
127 | 0 | } |
128 | 0 | } |
129 | | |
130 | | |
131 | | |
132 | | |
133 | | |
134 | | |
135 | | |