Commit 017a9cea by Hongjun Wu Committed by Sebastian Renner

tinyjambu with for loop

parent 109ff80f
/* /*
TinyJAMBU-192: 192-bit key, 96-bit IV TinyJAMBU-192: 192-bit key, 96-bit IV
Optimized Implementation for 32-bit processor Optimized Implementation for 32-bit processor
The state consists of four 32-bit registers The state consists of four 32-bit registers
state[3] || state[2] || state[1] || state[0] state[3] || state[2] || state[1] || state[0]
Implemented by Hongjun Wu Implemented by Hongjun Wu
*/ */
#include <string.h>
#include <stdio.h>
#include "crypto_aead.h" #include "crypto_aead.h"
#define FrameBitsIV 0x10 #define FrameBitsIV 0x10
...@@ -165,7 +163,7 @@ int crypto_aead_encrypt( ...@@ -165,7 +163,7 @@ int crypto_aead_encrypt(
((unsigned int*)mac)[1] = state[2]; ((unsigned int*)mac)[1] = state[2];
*clen = mlen + 8; *clen = mlen + 8;
memcpy(c + mlen, mac, 8); for (j = 0; j < 8; j++) c[mlen+j] = mac[j];
return 0; return 0;
} }
......
/* /*
TinyJAMBU: 192-bit key, 96-bit IV TinyJAMBU: 192-bit key, 96-bit IV
Reference implementation for 32-bit CPU Reference implementation for 32-bit CPU
The state consists of four 32-bit registers The state consists of four 32-bit registers
state[3] || state[2] || state[1] || state[0] state[3] || state[2] || state[1] || state[0]
Implemented by: Hongjun Wu Implemented by: Hongjun Wu
*/ */
#include <string.h>
#include <stdio.h>
#include "crypto_aead.h" #include "crypto_aead.h"
#define FrameBitsIV 0x10 #define FrameBitsIV 0x10
...@@ -42,21 +40,21 @@ void state_update(unsigned int *state, const unsigned char *key, unsigned int nu ...@@ -42,21 +40,21 @@ void state_update(unsigned int *state, const unsigned char *key, unsigned int nu
/* The input to initialization is the 192-bit key; 96-bit IV;*/ /* The input to initialization is the 192-bit key; 96-bit IV;*/
void initialization(const unsigned char *key, const unsigned char *iv, unsigned int *state) void initialization(const unsigned char *key, const unsigned char *iv, unsigned int *state)
{ {
int i; int i;
//initialize the state as 0 //initialize the state as 0
for (i = 0; i < 4; i++) state[i] = 0; for (i = 0; i < 4; i++) state[i] = 0;
//update the state with the key //update the state with the key
state_update(state, key, NROUND2); state_update(state, key, NROUND2);
//introduce IV into the state //introduce IV into the state
for (i = 0; i < 3; i++) for (i = 0; i < 3; i++)
{ {
state[1] ^= FrameBitsIV; state[1] ^= FrameBitsIV;
state_update(state, key, NROUND1); state_update(state, key, NROUND1);
state[3] ^= ((unsigned int*)iv)[i]; state[3] ^= ((unsigned int*)iv)[i];
} }
} }
//process the associated data //process the associated data
...@@ -92,15 +90,15 @@ int crypto_aead_encrypt( ...@@ -92,15 +90,15 @@ int crypto_aead_encrypt(
const unsigned char *k const unsigned char *k
) )
{ {
unsigned long long i; unsigned long long i;
unsigned int j; unsigned int j;
unsigned char mac[8]; unsigned char mac[8];
unsigned int state[4]; unsigned int state[4];
//initialization stage //initialization stage
initialization(k, npub, state); initialization(k, npub, state);
//process the associated data //process the associated data
process_ad(k, ad, adlen, state); process_ad(k, ad, adlen, state);
//process the plaintext //process the plaintext
...@@ -133,10 +131,10 @@ int crypto_aead_encrypt( ...@@ -133,10 +131,10 @@ int crypto_aead_encrypt(
state_update(state, k, NROUND1); state_update(state, k, NROUND1);
((unsigned int*)mac)[1] = state[2]; ((unsigned int*)mac)[1] = state[2];
*clen = mlen + 8; *clen = mlen + 8;
memcpy(c + mlen, mac, 8); for (j = 0; j < 8; j++) c[mlen+j] = mac[j];
return 0; return 0;
} }
//decrypt a message //decrypt a message
...@@ -195,6 +193,6 @@ int crypto_aead_decrypt( ...@@ -195,6 +193,6 @@ int crypto_aead_decrypt(
//verification of the authentication tag //verification of the authentication tag
for (j = 0; j < 8; j++) { check |= (mac[j] ^ c[clen - 8 + j]); } for (j = 0; j < 8; j++) { check |= (mac[j] ^ c[clen - 8 + j]); }
if (check == 0) return 0; if (check == 0) return 0;
else return -1; else return -1;
} }
/* /*
TinyJAMBU-256: 256-bit key, 96-bit IV TinyJAMBU-256: 256-bit key, 96-bit IV
Optimized implementation Optimized implementation
The state consists of four 32-bit registers The state consists of four 32-bit registers
state[3] || state[2] || state[1] || state[0] state[3] || state[2] || state[1] || state[0]
Implemented by: Hongjun Wu Implemented by: Hongjun Wu
*/ */
#include <string.h>
#include <stdio.h>
#include "crypto_aead.h" #include "crypto_aead.h"
#define FrameBitsIV 0x10 #define FrameBitsIV 0x10
...@@ -61,18 +59,18 @@ void initialization(const unsigned char *key, const unsigned char *iv, unsigned ...@@ -61,18 +59,18 @@ void initialization(const unsigned char *key, const unsigned char *iv, unsigned
int i; int i;
//initialize the state as 0 //initialize the state as 0
for (i = 0; i < 4; i++) state[i] = 0; for (i = 0; i < 4; i++) state[i] = 0;
//update the state with the key //update the state with the key
state_update(state, key, NROUND2); state_update(state, key, NROUND2);
//introduce IV into the state //introduce IV into the state
for (i = 0; i < 3; i++) for (i = 0; i < 3; i++)
{ {
state[1] ^= FrameBitsIV; state[1] ^= FrameBitsIV;
state_update(state, key, NROUND1); state_update(state, key, NROUND1);
state[3] ^= ((unsigned int*)iv)[i]; state[3] ^= ((unsigned int*)iv)[i];
} }
} }
//process the associated data //process the associated data
...@@ -150,8 +148,8 @@ int crypto_aead_encrypt( ...@@ -150,8 +148,8 @@ int crypto_aead_encrypt(
((unsigned int*)mac)[1] = state[2]; ((unsigned int*)mac)[1] = state[2];
*clen = mlen + 8; *clen = mlen + 8;
memcpy(c + mlen, mac, 8); for (j = 0; j < 8; j++) c[mlen+j] = mac[j];
return 0; return 0;
} }
......
/* /*
TinyJAMBU-256: 256-bit key, 96-bit IV TinyJAMBU-256: 256-bit key, 96-bit IV
Reference Implementation for 32-bit processor Reference Implementation for 32-bit processor
The state consists of four 32-bit registers The state consists of four 32-bit registers
state[3] || state[2] || state[1] || state[0] state[3] || state[2] || state[1] || state[0]
Implemented by Hongjun Wu Implemented by Hongjun Wu
*/ */
#include <string.h>
#include <stdio.h>
#include "crypto_aead.h" #include "crypto_aead.h"
#define FrameBitsIV 0x10 #define FrameBitsIV 0x10
...@@ -42,21 +40,21 @@ void state_update(unsigned int *state, const unsigned char *key, unsigned int nu ...@@ -42,21 +40,21 @@ void state_update(unsigned int *state, const unsigned char *key, unsigned int nu
/* The input to initialization is the 128-bit key; 96-bit IV;*/ /* The input to initialization is the 128-bit key; 96-bit IV;*/
void initialization(const unsigned char *key, const unsigned char *iv, unsigned int *state) void initialization(const unsigned char *key, const unsigned char *iv, unsigned int *state)
{ {
int i; int i;
//initialize the state as 0 //initialize the state as 0
for (i = 0; i < 4; i++) state[i] = 0; for (i = 0; i < 4; i++) state[i] = 0;
//update the state with the key //update the state with the key
state_update(state, key, NROUND2); state_update(state, key, NROUND2);
//introduce IV into the state //introduce IV into the state
for (i = 0; i < 3; i++) for (i = 0; i < 3; i++)
{ {
state[1] ^= FrameBitsIV; state[1] ^= FrameBitsIV;
state_update(state, key, NROUND1); state_update(state, key, NROUND1);
state[3] ^= ((unsigned int*)iv)[i]; state[3] ^= ((unsigned int*)iv)[i];
} }
} }
//process the associated data //process the associated data
...@@ -92,15 +90,15 @@ int crypto_aead_encrypt( ...@@ -92,15 +90,15 @@ int crypto_aead_encrypt(
const unsigned char *k const unsigned char *k
) )
{ {
unsigned long long i; unsigned long long i;
unsigned int j; unsigned int j;
unsigned char mac[8]; unsigned char mac[8];
unsigned int state[4]; unsigned int state[4];
//initialization stage //initialization stage
initialization(k, npub, state); initialization(k, npub, state);
//process the associated data //process the associated data
process_ad(k, ad, adlen, state); process_ad(k, ad, adlen, state);
//process the plaintext //process the plaintext
...@@ -133,10 +131,10 @@ int crypto_aead_encrypt( ...@@ -133,10 +131,10 @@ int crypto_aead_encrypt(
state_update(state, k, NROUND1); state_update(state, k, NROUND1);
((unsigned int*)mac)[1] = state[2]; ((unsigned int*)mac)[1] = state[2];
*clen = mlen + 8; *clen = mlen + 8;
memcpy(c + mlen, mac, 8); for (j = 0; j < 8; j++) c[mlen+j] = mac[j];
return 0; return 0;
} }
//decrypt a message //decrypt a message
...@@ -194,6 +192,6 @@ int crypto_aead_decrypt( ...@@ -194,6 +192,6 @@ int crypto_aead_decrypt(
//verification of the authentication tag //verification of the authentication tag
for (j = 0; j < 8; j++) { check |= (mac[j] ^ c[clen - 8 + j]); } for (j = 0; j < 8; j++) { check |= (mac[j] ^ c[clen - 8 + j]); }
if (check == 0) return 0; if (check == 0) return 0;
else return -1; else return -1;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment