Coverage Report

Created: 2025-03-18 06:55

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