salsa20.c
Go to the documentation of this file.
1 /**
2  * @file salsa20.c
3  * @brief Salsa20 encryption algorithm
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneCRYPTO Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/crypto.h"
36 #include "cipher/salsa20.h"
37 
38 //Check crypto library configuration
39 #if (SALSA20_SUPPORT == ENABLED)
40 
41 //Salsa20 quarter-round function
42 #define QUARTER_ROUND(a, b, c, d) \
43 { \
44  b ^= ROL32(a + d, 7); \
45  c ^= ROL32(b + a, 9); \
46  d ^= ROL32(c + b, 13); \
47  a ^= ROL32(d + c, 18); \
48 }
49 
50 
51 /**
52  * @brief Salsa20 core function
53  * @param[in] input Pointer to the 64-octet input block
54  * @param[out] output Pointer to the 64-octet output block
55  * @param[in] nr Number of rounds to be applied (8, 12 or 20)
56  **/
57 
58 void salsa20ProcessBlock(const uint8_t *input, uint8_t *output, uint_t nr)
59 {
60  uint_t i;
61  uint32_t x[16];
62 
63  //Copy the input words to the working state
64  for(i = 0; i < 16; i++)
65  {
66  x[i] = LOAD32LE(input + i * 4);
67  }
68 
69  //The Salsa20 core function alternates between column rounds and row rounds
70  for(i = 0; i < nr; i += 2)
71  {
72  //The column round function modifies the columns of the matrix in parallel
73  //by feeding a permutation of each column through the quarter round function
74  QUARTER_ROUND(x[0], x[4], x[8], x[12]);
75  QUARTER_ROUND(x[5], x[9], x[13], x[1]);
76  QUARTER_ROUND(x[10], x[14], x[2], x[6]);
77  QUARTER_ROUND(x[15], x[3], x[7], x[11]);
78 
79  //The row round function modifies the rows of the matrix in parallel by
80  //feeding a permutation of each row through the quarter round function
81  QUARTER_ROUND(x[0], x[1], x[2], x[3]);
82  QUARTER_ROUND(x[5], x[6], x[7], x[4]);
83  QUARTER_ROUND(x[10], x[11], x[8], x[9]);
84  QUARTER_ROUND(x[15], x[12], x[13], x[14]);
85  }
86 
87  //Add the original input words to the output words
88  for(i = 0; i < 16; i++)
89  {
90  x[i] += LOAD32LE(input + i * 4);
91  STORE32LE(x[i], output + i * 4);
92  }
93 }
94 
95 #endif
unsigned int uint_t
Definition: compiler_port.h:50
#define LOAD32LE(p)
Definition: cpu_endian.h:203
#define STORE32LE(a, p)
Definition: cpu_endian.h:279
General definitions for cryptographic algorithms.
uint8_t x
Definition: lldp_ext_med.h:211
void salsa20ProcessBlock(const uint8_t *input, uint8_t *output, uint_t nr)
Salsa20 core function.
Definition: salsa20.c:58
#define QUARTER_ROUND(a, b, c, d)
Definition: salsa20.c:42
Salsa20 encryption algorithm.