Coverage Report

Created: 2026-02-14 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/nettle/chacha-core-internal.c
Line
Count
Source
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
2.34M
#define QROUND(x0, x1, x2, x3) do { \
85
2.34M
  x0 = x0 + x1; x3 = ROTL32(16, (x0 ^ x3)); \
86
2.34M
  x2 = x2 + x3; x1 = ROTL32(12, (x1 ^ x2)); \
87
2.34M
  x0 = x0 + x1; x3 = ROTL32(8,  (x0 ^ x3)); \
88
2.34M
  x2 = x2 + x3; x1 = ROTL32(7,  (x1 ^ x2)); \
89
2.34M
  } while(0)
90
91
void
92
_nettle_chacha_core(uint32_t *dst, const uint32_t *src, unsigned rounds)
93
29.3k
{
94
29.3k
  uint32_t x[_CHACHA_STATE_LENGTH];
95
29.3k
  unsigned i;
96
97
29.3k
  assert ( (rounds & 1) == 0);
98
99
29.3k
  memcpy (x, src, sizeof(x));
100
322k
  for (i = 0; i < rounds;i += 2)
101
293k
    {
102
293k
      DEBUG (i);
103
293k
      QROUND(x[0], x[4], x[8],  x[12]);
104
293k
      QROUND(x[1], x[5], x[9],  x[13]);
105
293k
      QROUND(x[2], x[6], x[10], x[14]);
106
293k
      QROUND(x[3], x[7], x[11], x[15]);
107
108
293k
      DEBUG (i+1);
109
293k
      QROUND(x[0], x[5], x[10], x[15]);
110
293k
      QROUND(x[1], x[6], x[11], x[12]);
111
293k
      QROUND(x[2], x[7], x[8],  x[13]);
112
293k
      QROUND(x[3], x[4], x[9],  x[14]);
113
293k
    }
114
29.3k
  DEBUG (i);
115
116
498k
  for (i = 0; i < _CHACHA_STATE_LENGTH; i++)
117
468k
    {
118
468k
      uint32_t t = x[i] + src[i];
119
468k
      dst[i] = bswap32_if_be (t);
120
468k
    }
121
29.3k
}
122
123
124
125
126
127
128