ASCON Suite
ascon-direct-xor.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 /* SnP helper functions for backends that use the Direct-XOR method */
24 
25 #include <ascon/permutation.h>
26 #include <ascon/utility.h>
27 #include "ascon-select-backend.h"
28 #include "ascon-util.h"
29 #include "ascon-util-snp.h"
30 #include <string.h>
31 
32 #if defined(ASCON_BACKEND_DIRECT_XOR)
33 
34 #if defined(ASCON_CHECK_ACQUIRE_RELEASE)
35 #include <stdlib.h>
36 #include <stdio.h>
37 
38 static int acquired = 0;
39 #endif
40 
42 {
43 #if defined(ASCON_CHECK_ACQUIRE_RELEASE)
44  if (acquired) {
45  fprintf(stderr, "acquire and release operations are not balanced\n");
46  abort();
47  }
48  acquired = 1;
49 #endif
50  state->S[0] = 0;
51  state->S[1] = 0;
52  state->S[2] = 0;
53  state->S[3] = 0;
54  state->S[4] = 0;
56 }
57 
59 {
60 #if defined(ASCON_CHECK_ACQUIRE_RELEASE)
61  if (!acquired) {
62  fprintf(stderr, "acquire and release operations are not balanced\n");
63  abort();
64  }
65  acquired = 0;
66 #endif
67  if (state) {
70  }
71 }
72 
73 void ascon_add_bytes
74  (ascon_state_t *state, const uint8_t *data, unsigned offset, unsigned size)
75 {
76  while (offset < 40 && size > 0) {
77  state->B[offset] ^= *data++;
78  ++offset;
79  --size;
80  }
81 }
82 
84  (ascon_state_t *state, const uint8_t *data, unsigned offset, unsigned size)
85 {
86  while (offset < 40 && size > 0) {
87  state->B[offset] = *data++;
88  ++offset;
89  --size;
90  }
91 }
92 
94  (ascon_state_t *state, unsigned offset, unsigned size)
95 {
96  while (offset < 40 && size > 0) {
97  state->B[offset] = 0;
98  ++offset;
99  --size;
100  }
101 }
102 
104  (const ascon_state_t *state, uint8_t *data, unsigned offset, unsigned size)
105 {
106  while (offset < 40 && size > 0) {
107  *data++ = state->B[offset];
108  ++offset;
109  --size;
110  }
111 }
112 
114  (const ascon_state_t *state, const uint8_t *input, uint8_t *output,
115  unsigned offset, unsigned size)
116 {
117  while (offset < 40 && size > 0) {
118  *output++ = *input++ ^ state->B[offset];
119  ++offset;
120  --size;
121  }
122 }
123 
125  (ascon_state_t *state, const uint8_t *input, uint8_t *output,
126  unsigned offset, unsigned size)
127 {
128  while (offset < 40 && size > 0) {
129  unsigned char in = *input++;
130  *output++ = in ^ state->B[offset];
131  state->B[offset] = in;
132  ++offset;
133  --size;
134  }
135 }
136 
138 {
139  /* Not needed in this implementation */
140  (void)state;
141 #if defined(ASCON_CHECK_ACQUIRE_RELEASE)
142  if (!acquired) {
143  fprintf(stderr, "acquire and release operations are not balanced\n");
144  abort();
145  }
146  acquired = 0;
147 #endif
148 }
149 
151 {
152  /* Not needed in this implementation */
153  (void)state;
154 #if defined(ASCON_CHECK_ACQUIRE_RELEASE)
155  if (acquired) {
156  fprintf(stderr, "acquire and release operations are not balanced\n");
157  abort();
158  }
159  acquired = 1;
160 #endif
161 }
162 
163 void ascon_copy(ascon_state_t *dest, const ascon_state_t *src)
164 {
165  memcpy(dest->B, src->B, sizeof(dest->B));
166 }
167 
168 #endif /* ASCON_BACKEND_DIRECT_XOR */
#define ascon_backend_free(state)
#define ascon_backend_init(state)
Direct access to the ASCON permutation primitive.
void ascon_overwrite_with_zeroes(ascon_state_t *state, unsigned offset, unsigned size)
Overwrites a part of the ASCON state with zeroes.
void ascon_free(ascon_state_t *state)
Frees an ASCON permutation state and attempts to destroy any sensitive material.
void ascon_release(ascon_state_t *state)
Temporarily releases access to any shared hardware resources that a permutation state was using.
void ascon_overwrite_bytes(ascon_state_t *state, const uint8_t *data, unsigned offset, unsigned size)
Overwrites existing bytes in the ASCON state.
void ascon_copy(ascon_state_t *dest, const ascon_state_t *src)
Copies the entire ASCON permutation state from a source to a destination.
void ascon_extract_bytes(const ascon_state_t *state, uint8_t *data, unsigned offset, unsigned size)
Extracts bytes from the ASCON state.
void ascon_extract_and_overwrite_bytes(ascon_state_t *state, const uint8_t *input, uint8_t *output, unsigned offset, unsigned size)
Extracts bytes from the ASCON state and XOR's them with input bytes to produce output bytes....
void ascon_add_bytes(ascon_state_t *state, const uint8_t *data, unsigned offset, unsigned size)
Adds bytes to the ASCON state by XOR'ing them with existing bytes.
void ascon_extract_and_add_bytes(const ascon_state_t *state, const uint8_t *input, uint8_t *output, unsigned offset, unsigned size)
Extracts bytes from the ASCON state and XOR's them with input bytes to produce output bytes.
void ascon_acquire(ascon_state_t *state)
Re-acquires access to any shared hardware resources that a permutation state was using.
void ascon_init(ascon_state_t *state)
Initializes the words of the ASCON permutation state to zero.
ascon_state_t state
[snippet_key]
Definition: snippets.c:2
unsigned char data[8]
[snippet_key]
Definition: snippets.c:14
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
System utilities of use to applications that use ASCON.
void ascon_clean(void *buf, unsigned size)
Cleans a buffer that contains sensitive material.
Definition: ascon-clean.c:38