/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 "bswap-internal.h" |
53 | | #include "macros.h" |
54 | | |
55 | | /* For fat builds */ |
56 | | #if HAVE_NATIVE_chacha_core |
57 | | void |
58 | | _nettle_chacha_core_c(uint32_t *dst, const uint32_t *src, unsigned rounds); |
59 | | #define _nettle_chacha_core _nettle_chacha_core_c |
60 | | #endif |
61 | | |
62 | | #ifndef CHACHA_DEBUG |
63 | | # define CHACHA_DEBUG 0 |
64 | | #endif |
65 | | |
66 | | #if CHACHA_DEBUG |
67 | | # include <stdio.h> |
68 | | # define DEBUG(i) do { \ |
69 | | unsigned debug_j; \ |
70 | | for (debug_j = 0; debug_j < 16; debug_j++) \ |
71 | | { \ |
72 | | if (debug_j == 0) \ |
73 | | fprintf(stderr, "%2d:", (i)); \ |
74 | | else if (debug_j % 4 == 0) \ |
75 | | fprintf(stderr, "\n "); \ |
76 | | fprintf(stderr, " %8x", x[debug_j]); \ |
77 | | } \ |
78 | | fprintf(stderr, "\n"); \ |
79 | | } while (0) |
80 | | #else |
81 | | # define DEBUG(i) |
82 | | #endif |
83 | | |
84 | 0 | #define QROUND(x0, x1, x2, x3) do { \ |
85 | 0 | x0 = x0 + x1; x3 = ROTL32(16, (x0 ^ x3)); \ |
86 | 0 | x2 = x2 + x3; x1 = ROTL32(12, (x1 ^ x2)); \ |
87 | 0 | x0 = x0 + x1; x3 = ROTL32(8, (x0 ^ x3)); \ |
88 | 0 | x2 = x2 + x3; x1 = ROTL32(7, (x1 ^ x2)); \ |
89 | 0 | } while(0) |
90 | | |
91 | | void |
92 | | _nettle_chacha_core(uint32_t *dst, const uint32_t *src, unsigned rounds) |
93 | 0 | { |
94 | 0 | uint32_t x[_CHACHA_STATE_LENGTH]; |
95 | 0 | unsigned i; |
96 | |
|
97 | 0 | assert ( (rounds & 1) == 0); |
98 | | |
99 | 0 | memcpy (x, src, sizeof(x)); |
100 | 0 | for (i = 0; i < rounds;i += 2) |
101 | 0 | { |
102 | 0 | DEBUG (i); |
103 | 0 | QROUND(x[0], x[4], x[8], x[12]); |
104 | 0 | QROUND(x[1], x[5], x[9], x[13]); |
105 | 0 | QROUND(x[2], x[6], x[10], x[14]); |
106 | 0 | QROUND(x[3], x[7], x[11], x[15]); |
107 | |
|
108 | 0 | DEBUG (i+1); |
109 | 0 | QROUND(x[0], x[5], x[10], x[15]); |
110 | 0 | QROUND(x[1], x[6], x[11], x[12]); |
111 | 0 | QROUND(x[2], x[7], x[8], x[13]); |
112 | 0 | QROUND(x[3], x[4], x[9], x[14]); |
113 | 0 | } |
114 | 0 | DEBUG (i); |
115 | |
|
116 | 0 | for (i = 0; i < _CHACHA_STATE_LENGTH; i++) |
117 | 0 | { |
118 | 0 | uint32_t t = x[i] + src[i]; |
119 | 0 | dst[i] = bswap32_if_be (t); |
120 | 0 | } |
121 | 0 | } |
122 | | |
123 | | |
124 | | |
125 | | |
126 | | |
127 | | |
128 | | |