Add combined functions

The _combined functions do not need a precalculated key schedule,
the round key is generated on-the-fly.

This is useful for memory constrained systems.
This commit is contained in:
Moritz Bitsch 2016-01-28 01:08:15 +01:00
parent df3b1d654f
commit d691bd6e86
2 changed files with 52 additions and 0 deletions

49
speck.c
View file

@ -62,6 +62,47 @@ void speck_decrypt(SPECK_TYPE const ct[static 2], SPECK_TYPE pt[static 2], SPECK
}
}
void speck_encrypt_combined(SPECK_TYPE const pt[static 2], SPECK_TYPE ct[static 2], SPECK_TYPE const K[static SPECK_KEY_LEN])
{
SPECK_TYPE i, b = K[0];
SPECK_TYPE a[SPECK_KEY_LEN - 1];
ct[0]=pt[0]; ct[1]=pt[1];
for (i = 0; i < (SPECK_KEY_LEN - 1); i++)
{
a[i] = K[i + 1];
}
R(ct[1], ct[0], b);
for(i = 0; i < SPECK_ROUNDS - 1; i++){
R(a[i % (SPECK_KEY_LEN - 1)], b, i);
R(ct[1], ct[0], b);
}
}
void speck_decrypt_combined(SPECK_TYPE const ct[static 2], SPECK_TYPE pt[static 2], SPECK_TYPE const K[static SPECK_KEY_LEN])
{
int i;
SPECK_TYPE b = K[0];
SPECK_TYPE a[SPECK_KEY_LEN - 1];
pt[0]=ct[0]; pt[1]=ct[1];
for (i = 0; i < (SPECK_KEY_LEN - 1); i++)
{
a[i] = K[i + 1];
}
for (i = 0; i < SPECK_ROUNDS - 1; i++)
{
R(a[i % (SPECK_KEY_LEN - 1)], b, i);
}
for(i = 0; i < SPECK_ROUNDS; i++){
RR(pt[1], pt[0], b);
RR(a[((SPECK_ROUNDS - 2) - i) % (SPECK_KEY_LEN - 1)], b, ((SPECK_ROUNDS - 2) - i));
}
}
#ifdef TEST
#include <string.h>
@ -92,12 +133,20 @@ int main(int argc, char** argv)
speck_expand(key, exp);
#ifdef TEST_COMBINED
speck_encrypt_combined(plain, buffer, key);
#else
speck_encrypt(plain, buffer, exp);
#endif
if (memcmp(buffer, enc, sizeof(enc))) {
printf("encryption failed\n");
return 1;
}
#ifdef TEST_COMBINED
speck_decrypt_combined(enc, buffer, key);
#else
speck_decrypt(enc, buffer, exp);
#endif
if (memcmp(buffer, plain, sizeof(enc))) {
printf("decryption failed\n");
return 1;

View file

@ -49,6 +49,9 @@ void speck_expand(SPECK_TYPE const K[static SPECK_KEY_LEN], SPECK_TYPE S[static
void speck_encrypt(SPECK_TYPE const pt[static 2], SPECK_TYPE ct[static 2], SPECK_TYPE const K[static SPECK_ROUNDS]);
void speck_decrypt(SPECK_TYPE const ct[static 2], SPECK_TYPE pt[static 2], SPECK_TYPE const K[static SPECK_ROUNDS]);
void speck_encrypt_combined(SPECK_TYPE const pt[static 2], SPECK_TYPE ct[static 2], SPECK_TYPE const K[static SPECK_KEY_LEN]);
void speck_decrypt_combined(SPECK_TYPE const ct[static 2], SPECK_TYPE pt[static 2], SPECK_TYPE const K[static SPECK_KEY_LEN]);
#ifdef __cplusplus
}
#endif