forked from XKCP/XKCP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XoofffModes.h
241 lines (209 loc) · 11.4 KB
/
XoofffModes.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
The eXtended Keccak Code Package (XKCP)
https://github.com/XKCP/XKCP
Xoofff, designed by Joan Daemen, Seth Hoffert, Gilles Van Assche and Ronny Van Keer.
Implementation by Ronny Van Keer, hereby denoted as "the implementer".
For more information, feedback or questions, please refer to the Keccak Team website:
https://keccak.team/
To the extent possible under law, the implementer has waived all copyright
and related or neighboring rights to the source code in this file.
http://creativecommons.org/publicdomain/zero/1.0/
*/
#ifndef _XoofffModes_h_
#define _XoofffModes_h_
#include "config.h"
#ifdef XKCP_has_Xoodoo
#include <stddef.h>
#include <stdint.h>
#include "align.h"
#include "Xoofff.h"
/**
* XoofffSANE Tag Length in bytes.
*/
#define XoofffSANE_TagLength 16
typedef struct {
Xoofff_Instance xoofff;
unsigned int e;
} XoofffSANE_Instance;
/**
* Function to initialize a XoofffSANE instance with given key and nonce.
* @param xpInstance Pointer to the instance to be initialized.
* @param Key Pointer to the key (K).
* @param KeyBitLen The length of the key in bits.
* @param Nonce Pointer to the nonce (N).
* @param NonceBitLen The length of the nonce in bits.
* @param tag The buffer where to store the tag.
* This buffer must be minimum XoofffSANE_TagLength bytes long.
* @return 0 if successful, 1 otherwise.
*/
int XoofffSANE_Initialize(XoofffSANE_Instance *xpInstance, const BitSequence *Key, BitLength KeyBitLen,
const BitSequence *Nonce, BitLength NonceBitLen, unsigned char *tag);
/**
* Function to wrap plaintext into ciphertext.
* @param xpInstance Pointer to the instance initialized by XoofffSANE_Initialize().
* @param plaintext Pointer to plaintext data to wrap.
* @param ciphertext Pointer to buffer where the full wrapped data will be stored.
* The ciphertext buffer must not overlap plaintext.
* @param dataBitLen The size of the plaintext/ciphertext data.
* @param AD Pointer to the Associated Data.
* @param ADBitLen The number of bytes provided in the Associated Data.
* @param tag The buffer where to store the tag.
* This buffer must be minimum XoofffSANE_TagLength bytes long.
* @return 0 if successful, 1 otherwise.
*/
int XoofffSANE_Wrap(XoofffSANE_Instance *xpInstance, const BitSequence *plaintext, BitSequence *ciphertext, BitLength dataBitLen,
const BitSequence *AD, BitLength ADBitLen, unsigned char *tag);
/**
* Function to unwrap ciphertext into plaintext.
* @param xpInstance Pointer to the instance initialized by XoofffSANE_Initialize().
* @param ciphertext Pointer to ciphertext data to unwrap.
* @param plaintext Pointer to buffer where the full unwrapped data will be stored.
* The plaintext buffer must not overlap ciphertext.
* @param dataBitLen The size of the ciphertext/plaintext data.
* @param AD Pointer to the Associated Data.
* @param ADBitLen The number of bytes provided in the Associated Data.
* @param tag The buffer where to read the tag to check (when lastFlag is set).
* This buffer must be minimum XoofffSANE_TagLength bytes long.
* @return 0 if successful, 1 otherwise.
*/
int XoofffSANE_Unwrap(XoofffSANE_Instance *xpInstance, const BitSequence *ciphertext, BitSequence *plaintext, BitLength dataBitLen,
const BitSequence *AD, BitLength ADBitLen, const unsigned char *tag);
/* ------------------------------------------------------------------------- */
/**
* XoofffSANSE Tag Length in bytes.
*/
#define XoofffSANSE_TagLength 32
typedef struct {
Xoofff_Instance xoofff;
unsigned int e;
} XoofffSANSE_Instance;
/**
* Function to initialize a XoofffSANSE instance with given key and nonce.
* @param xpInstance Pointer to the instance to be initialized.
* @param Key Pointer to the key (K).
* @param KeyBitLen The length of the key in bits.
* @return 0 if successful, 1 otherwise.
*/
int XoofffSANSE_Initialize(XoofffSANSE_Instance *xpInstance, const BitSequence *Key, BitLength KeyBitLen);
/**
* Function to wrap plaintext into ciphertext.
* @param xpInstance Pointer to the instance initialized by XoofffSANSE_MaskDerivation().
* @param plaintext Pointer to plaintext data to wrap.
* @param ciphertext Pointer to buffer where the full wrapped data will be stored.
* The ciphertext buffer must not overlap plaintext.
* @param dataBitLen The size of the plaintext/ciphertext data.
* @param AD Pointer to the Associated Data.
* @param ADBitLen The number of bytes provided in the Associated Data.
* @param tag The buffer where to store the tag.
* This buffer must be minimum XoofffSANSE_TagLength bytes long.
* @return 0 if successful, 1 otherwise.
*/
int XoofffSANSE_Wrap(XoofffSANSE_Instance *xpInstance, const BitSequence *plaintext, BitSequence *ciphertext, BitLength dataBitLen,
const BitSequence *AD, BitLength ADBitLen, unsigned char *tag);
/**
* Function to unwrap ciphertext into plaintext.
* @param xpInstance Pointer to the instance initialized by XoofffSANSE_MaskDerivation().
* @param ciphertext Pointer to ciphertext data to unwrap.
* @param plaintext Pointer to buffer where the full unwrapped data will be stored.
* The plaintext buffer must not overlap ciphertext.
* @param dataBitLen The size of the ciphertext/plaintext data.
* @param AD Pointer to the Associated Data.
* @param ADBitLen The number of bytes provided in the Associated Data.
* @param tag The buffer where to read the tag to check (when lastFlag is set).
* This buffer must be minimum XoofffSANSE_TagLength bytes long.
* @return 0 if successful, 1 otherwise.
*/
int XoofffSANSE_Unwrap(XoofffSANSE_Instance *xpInstance, const BitSequence *ciphertext, BitSequence *plaintext, BitLength dataBitLen,
const BitSequence *AD, BitLength ADBitLen, const unsigned char *tag);
/* ------------------------------------------------------------------------- */
/**
* Definition of the constant l, used to split the input into two parts.
* The left part of the input will be a multiple of l bits.
*/
#define XoofffWBC_l 8
/**
* Definition of the constant b block length.
*/
#define XoofffWBC_b (SnP_widthInBytes*8)
/**
* Macro to initialize a XoofffWBC instance with given key.
* @param xp Pointer to the instance to be initialized.
* @param Key Pointer to the key (K).
* @param KeyBitLen The length of the key in bits.
* @return 0 if successful, 1 otherwise.
*/
#define XoofffWBC_Initialize(xp, Key, KeyBitLen) Xoofff_MaskDerivation(xp, Key, KeyBitLen)
/**
* Function to encipher plaintext into ciphertext.
* @param xpInstance Pointer to the instance initialized by XoofffWBC_Initialize().
* @param plaintext Pointer to plaintext data to encipher.
* @param ciphertext Pointer to buffer where the enciphered data will be stored.
* The ciphertext buffer must not overlap plaintext.
* @param dataBitLen The size in bits of the plaintext/ciphertext data.
* @param W Pointer to the tweak W.
* @param WBitLen The number of bits provided in the tweak.
* @return 0 if successful, 1 otherwise.
*/
int XoofffWBC_Encipher(Xoofff_Instance *xpInstance, const BitSequence *plaintext, BitSequence *ciphertext, BitLength dataBitLen,
const BitSequence *W, BitLength WBitLen);
/**
* Function to decipher ciphertext into plaintext.
* @param xpInstance Pointer to the instance initialized by XoofffWBC_Initialize().
* @param ciphertext Pointer to ciphertext data to decipher.
* @param plaintext Pointer to buffer where the deciphered data will be stored.
* The plaintext buffer must not overlap ciphertext.
* @param dataBitLen The size in bits of the plaintext/ciphertext data.
* @param W Pointer to the tweak W.
* @param WBitLen The number of bits provided in the tweak.
* @return 0 if successful, 1 otherwise.
*/
int XoofffWBC_Decipher(Xoofff_Instance *xpInstance, const BitSequence *ciphertext, BitSequence *plaintext, BitLength dataBitLen,
const BitSequence *W, BitLength WBitLen);
/* ------------------------------------------------------------------------- */
/**
* Definition of the constant t, expansion length (in bits).
*/
#define XoofffWBCAE_t 128
/**
* Macro to initialize a XoofffWBC instance with given key.
* @param xp Pointer to the instance to be initialized.
* @param Key Pointer to the key (K).
* @param KeyBitLen The length of the key in bits.
* @return 0 if successful, 1 otherwise.
*/
#define XoofffWBCAE_Initialize(xp, Key, KeyBitLen) Xoofff_MaskDerivation(xp, Key, KeyBitLen)
/**
* Function to encipher plaintext into ciphertext.
* @param xpInstance Pointer to the instance initialized by XoofffWBC_Initialize().
* @param plaintext Pointer to plaintext data to encipher.
* The last ::XoofffWBCAE_t bits of the buffer will be overwritten with zeros.
* @param ciphertext Pointer to buffer where the enciphered data will be stored.
* The ciphertext buffer must not overlap plaintext.
* Ciphertext will be ::XoofffWBCAE_t bits longer than plaintext.
* @param dataBitLen The size in bits of the plaintext data.
* Plaintext and ciphertext buffers must be ::XoofffWBCAE_t bits longer than dataBitLen.
* @param AD Pointer to the metadata AD.
* @param ADBitLen The number of bits provided in the metadata.
* @return 0 if successful, 1 otherwise.
*/
int XoofffWBCAE_Encipher(Xoofff_Instance *xpInstance, BitSequence *plaintext, BitSequence *ciphertext, BitLength dataBitLen,
const BitSequence *AD, BitLength ADBitLen);
/**
* Function to decipher ciphertext into plaintext.
* @param xpInstance Pointer to the instance initialized by XoofffWBC_Initialize().
* @param ciphertext Pointer to ciphertext data to decipher.
* Ciphertext is ::XoofffWBCAE_t bits longer than plaintext.
* @param plaintext Pointer to buffer where the deciphered data will be stored.
* The plaintext buffer must not overlap ciphertext.
* @param dataBitLen The size in bits of the plaintext data.
* Ciphertext and plaintext buffers must be ::XoofffWBCAE_t bits longer than dataBitLen.
* @param AD Pointer to the metadata AD.
* @param ADBitLen The number of bits provided in the metadata.
* @return 0 if successful, 1 otherwise.
*/
int XoofffWBCAE_Decipher(Xoofff_Instance *xpInstance, const BitSequence *ciphertext, BitSequence *plaintext, BitLength dataBitLen,
const BitSequence *AD, BitLength ADBitLen);
#else
#error This requires an implementation of Xoodoo
#endif
#endif