ASCON Suite
ascon-c32.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2022 Southern Storm Software, Pty Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 /* Plain C implementation of the ASCON permutation for systems with a
24  * 32-bit native word size. */
25 
26 #include <ascon/permutation.h>
27 #include "ascon-select-backend.h"
28 #include "ascon-sliced32.h"
29 #include "ascon-util.h"
30 
31 #if defined(ASCON_BACKEND_C32)
32 
33 #define ROUND_CONSTANT_PAIR(rc1, rc2) \
34  (~((uint32_t)(rc1))), (~((uint32_t)(rc2)))
35 
36 void ascon_permute(ascon_state_t *state, uint8_t first_round)
37 {
38  static const uint32_t RC[12 * 2] = {
39  ROUND_CONSTANT_PAIR(12, 12),
40  ROUND_CONSTANT_PAIR( 9, 12),
41  ROUND_CONSTANT_PAIR(12, 9),
42  ROUND_CONSTANT_PAIR( 9, 9),
43  ROUND_CONSTANT_PAIR( 6, 12),
44  ROUND_CONSTANT_PAIR( 3, 12),
45  ROUND_CONSTANT_PAIR( 6, 9),
46  ROUND_CONSTANT_PAIR( 3, 9),
47  ROUND_CONSTANT_PAIR(12, 6),
48  ROUND_CONSTANT_PAIR( 9, 6),
49  ROUND_CONSTANT_PAIR(12, 3),
50  ROUND_CONSTANT_PAIR( 9, 3)
51  };
52  const uint32_t *rc = RC + first_round * 2;
53  uint32_t t0, t1, t2, t3, t4;
54 
55  /* Load the state into local variables */
56  uint32_t x0_e = state->W[0];
57  uint32_t x0_o = state->W[1];
58  uint32_t x1_e = state->W[2];
59  uint32_t x1_o = state->W[3];
60  uint32_t x2_e = state->W[4];
61  uint32_t x2_o = state->W[5];
62  uint32_t x3_e = state->W[6];
63  uint32_t x3_o = state->W[7];
64  uint32_t x4_e = state->W[8];
65  uint32_t x4_o = state->W[9];
66 
67  /* We move the "x2 = ~x2" term of the substitution layer outside
68  * the loop. The round constants are modified to "NOT value" to
69  * apply "x2 = ~x2" automatically each round. Then we only
70  * need to invert x2 for real before and after the loop. */
71  x2_e = ~x2_e;
72  x2_o = ~x2_o;
73 
74  /* Perform all permutation rounds */
75  while (first_round < 12) {
76  /* Add the round constants for this round to the state */
77  x2_e ^= rc[0];
78  x2_o ^= rc[1];
79  rc += 2;
80 
81  /* Substitution layer */
82  #define ascon_sbox(x0, x1, x2, x3, x4) \
83  do { \
84  x0 ^= x4; x4 ^= x3; x2 ^= x1; \
85  t0 = ~x0; t1 = ~x1; t2 = ~x2; t3 = ~x3; t4 = ~x4; \
86  t0 &= x1; t1 &= x2; t2 &= x3; t3 &= x4; t4 &= x0; \
87  x0 ^= t1; x1 ^= t2; x2 ^= t3; x3 ^= t4; x4 ^= t0; \
88  x1 ^= x0; x0 ^= x4; x3 ^= x2; /* x2 = ~x2; */ \
89  } while (0)
90  ascon_sbox(x0_e, x1_e, x2_e, x3_e, x4_e);
91  ascon_sbox(x0_o, x1_o, x2_o, x3_o, x4_o);
92 
93  /* Linear diffusion layer */
94  /* x0 ^= rightRotate19_64(x0) ^ rightRotate28_64(x0); */
95  t0 = x0_e ^ rightRotate4(x0_o);
96  t1 = x0_o ^ rightRotate5(x0_e);
97  x0_e ^= rightRotate9(t1);
98  x0_o ^= rightRotate10(t0);
99  /* x1 ^= rightRotate61_64(x1) ^ rightRotate39_64(x1); */
100  t0 = x1_e ^ rightRotate11(x1_e);
101  t1 = x1_o ^ rightRotate11(x1_o);
102  x1_e ^= rightRotate19(t1);
103  x1_o ^= rightRotate20(t0);
104  /* x2 ^= rightRotate1_64(x2) ^ rightRotate6_64(x2); */
105  t0 = x2_e ^ rightRotate2(x2_o);
106  t1 = x2_o ^ rightRotate3(x2_e);
107  x2_e ^= t1;
108  x2_o ^= rightRotate1(t0);
109  /* x3 ^= rightRotate10_64(x3) ^ rightRotate17_64(x3); */
110  t0 = x3_e ^ rightRotate3(x3_o);
111  t1 = x3_o ^ rightRotate4(x3_e);
112  x3_e ^= rightRotate5(t0);
113  x3_o ^= rightRotate5(t1);
114  /* x4 ^= rightRotate7_64(x4) ^ rightRotate41_64(x4); */
115  t0 = x4_e ^ rightRotate17(x4_e);
116  t1 = x4_o ^ rightRotate17(x4_o);
117  x4_e ^= rightRotate3(t1);
118  x4_o ^= rightRotate4(t0);
119 
120  /* Move onto the next round */
121  ++first_round;
122  }
123 
124  /* Apply the final NOT to x2 */
125  x2_e = ~x2_e;
126  x2_o = ~x2_o;
127 
128  /* Write the local variables back to the state */
129  state->W[0] = x0_e;
130  state->W[1] = x0_o;
131  state->W[2] = x1_e;
132  state->W[3] = x1_o;
133  state->W[4] = x2_e;
134  state->W[5] = x2_o;
135  state->W[6] = x3_e;
136  state->W[7] = x3_o;
137  state->W[8] = x4_e;
138  state->W[9] = x4_o;
139 }
140 
141 #endif /* ASCON_BACKEND_C32 */
#define ascon_sbox(x0, x1, x2, x3, x4)
#define ROUND_CONSTANT_PAIR(rc1, rc2)
Definition: ascon-c32.c:33
void ascon_permute(ascon_state_t *state, uint8_t first_round)
Permutes the ASCON state with a specified number of rounds.
Definition: ascon-c32.c:36
#define rightRotate11(a)
Definition: ascon-util.h:330
#define rightRotate9(a)
Definition: ascon-util.h:328
#define rightRotate4(a)
Definition: ascon-util.h:323
#define rightRotate3(a)
Definition: ascon-util.h:322
#define rightRotate20(a)
Definition: ascon-util.h:339
#define rightRotate10(a)
Definition: ascon-util.h:329
#define rightRotate5(a)
Definition: ascon-util.h:324
#define rightRotate17(a)
Definition: ascon-util.h:336
#define rightRotate19(a)
Definition: ascon-util.h:338
#define rightRotate1(a)
Definition: ascon-util.h:320
#define rightRotate2(a)
Definition: ascon-util.h:321
Direct access to the ASCON permutation primitive.
ascon_state_t state
[snippet_key]
Definition: snippets.c:2
Structure of the internal state of the ASCON permutation.
Definition: permutation.h:63
uint32_t W[10]
Definition: permutation.h:65