Skip to content

Commit f9574d2

Browse files
v0.3; Converting to C API
1 parent 329085c commit f9574d2

File tree

8 files changed

+193
-205
lines changed

8 files changed

+193
-205
lines changed

README.txt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,35 @@ See <SpritzCipher.h> for the details.
2626
The context/ctx (contain the state), holds indices and S-Box.
2727

2828
* Functions:
29-
wipe_spritz_ctx(spritz_ctx ctx):
29+
spritz_wipe_ctx(spritz_ctx ctx):
3030
Wipe spritz context data.
3131

32-
setup(spritz_ctx ctx, key, keyLen)
32+
spritz_setup(spritz_ctx ctx, key, keyLen)
3333
Setup spritz state (spritz_ctx) with a key.
34-
setupIV(spritz_ctx ctx, key, keyLen, nonce, nonceLen)
34+
spritz_setupIV(spritz_ctx ctx, key, keyLen, nonce, nonceLen)
3535
Setup spritz state with a key and nonce (Salt).
3636
spritz_rand_byte(spritz_ctx ctx)
3737
Generates a byte of keystream from spritz state (spritz_ctx).
3838

3939

40-
hash(digest, digestLen, data, dataLen)
40+
spritz_hash(digest, digestLen, data, dataLen)
4141
Spritz cryptographic hash function.
42-
mac(digest, digestLen, msg, msgLen, key, keyLen)
42+
spritz_mac(digest, digestLen, msg, msgLen, key, keyLen)
4343
Spritz Message Authentication Code (MAC) function.
4444

4545

46-
hash_setup(spritz_ctx hash_ctx)
46+
spritz_hash_setup(spritz_ctx hash_ctx)
4747
Setup spritz hash state.
48-
hash_update(spritz_ctx hash_ctx, data, dataLen)
48+
spritz_hash_update(spritz_ctx hash_ctx, data, dataLen)
4949
Add data chunk to hash.
50-
hash_final(spritz_ctx hash_ctx, digest, digestLen)
50+
spritz_hash_final(spritz_ctx hash_ctx, digest, digestLen)
5151
Output hash digest.
5252

53-
mac_setup(spritz_ctx mac_ctx, key, keyLen)
53+
spritz_mac_setup(spritz_ctx mac_ctx, key, keyLen)
5454
Setup spritz MAC state.
55-
mac_update(spritz_ctx mac_ctx, msg, msgLen)
55+
spritz_mac_update(spritz_ctx mac_ctx, msg, msgLen)
5656
Add message/data chunk to MAC.
57-
mac_final(spritz_ctx mac_ctx, digest, digestLen)
57+
spritz_mac_final(spritz_ctx mac_ctx, digest, digestLen)
5858
Output MAC digest.
5959

6060

SpritzCipher.cpp

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@
3030
#define SPRITZ_N_HALF 128 /* SPRITZ_N / 2 */
3131

3232

33-
SpritzCipher::SpritzCipher() { }
34-
35-
3633
static void
3734
swap(uint8_t *a, uint8_t *b)
3835
{
@@ -86,7 +83,9 @@ crush(spritz_ctx *ctx)
8683

8784
for (i = 0, j = SPRITZ_N_MINUS_1; i < SPRITZ_N_HALF; i++, j--) {
8885
#ifdef SAFE_TIMING_CRUSH
89-
if ((s_i = ctx->s[i]) > (s_j = ctx->s[j])) {
86+
s_i = ctx->s[i];
87+
s_j = ctx->s[j];
88+
if (s_i > s_j) {
9089
ctx->s[i] = s_j;
9190
ctx->s[j] = s_i;
9291
}
@@ -163,7 +162,7 @@ drip(spritz_ctx *ctx)
163162
return output(ctx);
164163
}
165164

166-
/* squeeze() for hash() and mac() */
165+
/* squeeze() for hash and mac */
167166
static void
168167
squeeze(spritz_ctx *ctx, uint8_t *out, uint8_t len)
169168
{
@@ -181,7 +180,7 @@ squeeze(spritz_ctx *ctx, uint8_t *out, uint8_t len)
181180

182181
/* Wipe spritz context (spritz_ctx) data */
183182
void
184-
SpritzCipher::wipe_spritz_ctx(spritz_ctx *ctx)
183+
spritz_wipe_ctx(spritz_ctx *ctx)
185184
{
186185
uint8_t i, d;
187186

@@ -202,7 +201,7 @@ SpritzCipher::wipe_spritz_ctx(spritz_ctx *ctx)
202201

203202
/* Setup spritz state (spritz_ctx) with a key */
204203
void
205-
SpritzCipher::setup(spritz_ctx *ctx,
204+
spritz_setup(spritz_ctx *ctx,
206205
const uint8_t *key, uint8_t keyLen)
207206
{
208207
stateInit(ctx);
@@ -211,97 +210,97 @@ SpritzCipher::setup(spritz_ctx *ctx,
211210

212211
/* Setup spritz state (spritz_ctx) with a key and nonce (Salt) */
213212
void
214-
SpritzCipher::setupIV(spritz_ctx *ctx,
213+
spritz_setupIV(spritz_ctx *ctx,
215214
const uint8_t *key, uint8_t keyLen,
216215
const uint8_t *nonce, uint8_t nonceLen)
217216
{
218-
setup(ctx, key, keyLen);
217+
spritz_setup(ctx, key, keyLen);
219218
absorbStop(ctx);
220219
absorbBytes(ctx, nonce, nonceLen);
221220
}
222221

223222
/* Generates a byte of keystream from spritz state (spritz_ctx) */
224223
uint8_t
225-
SpritzCipher::spritz_rand_byte(spritz_ctx *ctx)
224+
spritz_rand_byte(spritz_ctx *ctx)
226225
{
227226
return drip(ctx);
228227
}
229228

230229

231230
/* Setup spritz hash state (spritz_ctx) */
232231
void
233-
SpritzCipher::hash_setup(spritz_ctx *hash_ctx)
232+
spritz_hash_setup(spritz_ctx *hash_ctx)
234233
{
235234
stateInit(hash_ctx);
236235
}
237236

238237
/* Add data chunk to hash */
239238
void
240-
SpritzCipher::hash_update(spritz_ctx *hash_ctx,
241-
const uint8_t *data, unsigned int dataLen)
239+
spritz_hash_update(spritz_ctx *hash_ctx,
240+
const uint8_t *data, unsigned int dataLen)
242241
{
243242
absorbBytes(hash_ctx, data, dataLen);
244243
}
245244

246245
/* Output hash digest */
247246
void
248-
SpritzCipher::hash_final(spritz_ctx *hash_ctx,
249-
uint8_t *digest, uint8_t digestLen)
247+
spritz_hash_final(spritz_ctx *hash_ctx,
248+
uint8_t *digest, uint8_t digestLen)
250249
{
251250
absorbStop(hash_ctx);
252251
absorb(hash_ctx, digestLen);
253252
squeeze(hash_ctx, digest, digestLen);
254253
#ifdef WIPE_AFTER_USAGE
255-
wipe_spritz_ctx(hash_ctx);
254+
spritz_wipe_ctx(hash_ctx);
256255
#endif
257256
}
258257

259258
/* Cryptographic hash function */
260259
void
261-
SpritzCipher::hash(uint8_t *digest, uint8_t digestLen,
262-
const uint8_t *data, unsigned int dataLen)
260+
spritz_hash(uint8_t *digest, uint8_t digestLen,
261+
const uint8_t *data, unsigned int dataLen)
263262
{
264263
spritz_ctx hash_ctx;
265-
hash_setup(&hash_ctx);
266-
hash_update(&hash_ctx, data, dataLen);
267-
hash_final(&hash_ctx, digest, digestLen);
264+
spritz_hash_setup(&hash_ctx);
265+
spritz_hash_update(&hash_ctx, data, dataLen);
266+
spritz_hash_final(&hash_ctx, digest, digestLen);
268267
}
269268

270269

271270
/* Setup spritz MAC state (spritz_ctx) */
272271
void
273-
SpritzCipher::mac_setup(spritz_ctx *mac_ctx,
274-
const uint8_t *key, unsigned int keyLen)
272+
spritz_mac_setup(spritz_ctx *mac_ctx,
273+
const uint8_t *key, unsigned int keyLen)
275274
{
276-
stateInit(mac_ctx); /* hash_update() */
275+
stateInit(mac_ctx); /* Like spritz_hash_update() */
277276
absorbBytes(mac_ctx, key, keyLen);
278277
absorbStop(mac_ctx);
279278
}
280279

281280
/* Add message/data chunk to MAC */
282281
void
283-
SpritzCipher::mac_update(spritz_ctx *mac_ctx,
284-
const uint8_t *msg, unsigned int msgLen)
282+
spritz_mac_update(spritz_ctx *mac_ctx,
283+
const uint8_t *msg, unsigned int msgLen)
285284
{
286-
absorbBytes(mac_ctx, msg, msgLen); /* hash_update() */
285+
absorbBytes(mac_ctx, msg, msgLen); /* spritz_hash_update() */
287286
}
288287

289288
/* Output MAC digest */
290289
void
291-
SpritzCipher::mac_final(spritz_ctx *mac_ctx,
292-
uint8_t *digest, uint8_t digestLen)
290+
spritz_mac_final(spritz_ctx *mac_ctx,
291+
uint8_t *digest, uint8_t digestLen)
293292
{
294-
hash_final(mac_ctx, digest, digestLen);
293+
spritz_hash_final(mac_ctx, digest, digestLen);
295294
}
296295

297296
/* Message Authentication Code (MAC) function */
298297
void
299-
SpritzCipher::mac(uint8_t *digest, uint8_t digestLen,
300-
const uint8_t *msg, unsigned int msgLen,
301-
const uint8_t *key, unsigned int keyLen)
298+
spritz_mac(uint8_t *digest, uint8_t digestLen,
299+
const uint8_t *msg, unsigned int msgLen,
300+
const uint8_t *key, unsigned int keyLen)
302301
{
303302
spritz_ctx mac_ctx;
304-
mac_setup(&mac_ctx, key, keyLen);
305-
mac_update(&mac_ctx, msg, msgLen);
306-
mac_final(&mac_ctx, digest, digestLen);
303+
spritz_mac_setup(&mac_ctx, key, keyLen);
304+
spritz_mac_update(&mac_ctx, msg, msgLen);
305+
spritz_mac_final(&mac_ctx, digest, digestLen);
307306
}

0 commit comments

Comments
 (0)