Coverage Report

Created: 2025-07-23 07:18

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