ASCON Suite
ascon-c64.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  * 64-bit or better native word size. */
25 
26 #include <ascon/permutation.h>
27 #include "ascon-select-backend.h"
28 #include "ascon-util.h"
29 
30 #if defined(ASCON_BACKEND_C64) || defined(ASCON_BACKEND_C64_DIRECT_XOR)
31 
32 #define ROUND_CONSTANT(round) \
33  (~(uint64_t)(((0x0F - (round)) << 4) | (round)))
34 
35 void ascon_permute(ascon_state_t *state, uint8_t first_round)
36 {
37  static const uint64_t RC[12] = {
38  ROUND_CONSTANT(0),
39  ROUND_CONSTANT(1),
40  ROUND_CONSTANT(2),
41  ROUND_CONSTANT(3),
42  ROUND_CONSTANT(4),
43  ROUND_CONSTANT(5),
44  ROUND_CONSTANT(6),
45  ROUND_CONSTANT(7),
46  ROUND_CONSTANT(8),
47  ROUND_CONSTANT(9),
48  ROUND_CONSTANT(10),
49  ROUND_CONSTANT(11)
50  };
51  uint64_t t0, t1, t2, t3, t4;
52 #if defined(ASCON_BACKEND_C64_DIRECT_XOR)
53  uint64_t x0 = be_load_word64(state->B);
54  uint64_t x1 = be_load_word64(state->B + 8);
55  uint64_t x2 = be_load_word64(state->B + 16);
56  uint64_t x3 = be_load_word64(state->B + 24);
57  uint64_t x4 = be_load_word64(state->B + 32);
58 #else
59  uint64_t x0 = state->S[0];
60  uint64_t x1 = state->S[1];
61  uint64_t x2 = state->S[2];
62  uint64_t x3 = state->S[3];
63  uint64_t x4 = state->S[4];
64 #endif
65  x2 = ~x2;
66  while (first_round < 12) {
67  /* Add the round constant to the state */
68  x2 ^= RC[first_round];
69 
70  /* Substitution layer - apply the s-box using bit-slicing
71  * according to the algorithm recommended in the specification.
72  *
73  * The final "x2 = ~x2" term will be implicitly performed
74  * by the inverted round constant for the next round.
75  */
76  x0 ^= x4; x4 ^= x3; x2 ^= x1;
77  t0 = ~x0; t1 = ~x1; t2 = ~x2; t3 = ~x3; t4 = ~x4;
78  t0 &= x1; t1 &= x2; t2 &= x3; t3 &= x4; t4 &= x0;
79  x0 ^= t1; x1 ^= t2; x2 ^= t3; x3 ^= t4; x4 ^= t0;
80  x1 ^= x0; x0 ^= x4; x3 ^= x2; /* x2 = ~x2; */
81 
82  /* Linear diffusion layer */
83  x0 ^= rightRotate19_64(x0) ^ rightRotate28_64(x0);
84  x1 ^= rightRotate61_64(x1) ^ rightRotate39_64(x1);
85  x2 ^= rightRotate1_64(x2) ^ rightRotate6_64(x2);
86  x3 ^= rightRotate10_64(x3) ^ rightRotate17_64(x3);
87  x4 ^= rightRotate7_64(x4) ^ rightRotate41_64(x4);
88 
89  /* Move onto the next round */
90  ++first_round;
91  }
92  x2 = ~x2;
93 #if defined(ASCON_BACKEND_C64_DIRECT_XOR)
94  be_store_word64(state->B, x0);
95  be_store_word64(state->B + 8, x1);
96  be_store_word64(state->B + 16, x2);
97  be_store_word64(state->B + 24, x3);
98  be_store_word64(state->B + 32, x4);
99 #else
100  state->S[0] = x0;
101  state->S[1] = x1;
102  state->S[2] = x2;
103  state->S[3] = x3;
104  state->S[4] = x4;
105 #endif
106 }
107 
108 #endif /* ASCON_BACKEND_C64 */
#define rightRotate39_64(a)
Definition: ascon-util.h:606
#define rightRotate61_64(a)
Definition: ascon-util.h:628
#define rightRotate41_64(a)
Definition: ascon-util.h:608
#define rightRotate1_64(a)
Definition: ascon-util.h:568
#define rightRotate10_64(a)
Definition: ascon-util.h:577
#define be_store_word64(ptr, x)
Definition: ascon-util.h:118
#define rightRotate19_64(a)
Definition: ascon-util.h:586
#define rightRotate6_64(a)
Definition: ascon-util.h:573
#define rightRotate7_64(a)
Definition: ascon-util.h:574
#define rightRotate28_64(a)
Definition: ascon-util.h:595
#define rightRotate17_64(a)
Definition: ascon-util.h:584
#define be_load_word64(ptr)
Definition: ascon-util.h:107
#define ROUND_CONSTANT(round)
Definition: ascon-x2-c64.c:46
Direct access to the ASCON permutation primitive.
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
ascon_state_t state
[snippet_key]
Definition: snippets.c:2
Structure of the internal state of the ASCON permutation.
Definition: permutation.h:63
uint64_t S[5]
Definition: permutation.h:64
uint8_t B[40]
Definition: permutation.h:66