mirror of
https://codeberg.org/madmo/speck.git
synced 2025-01-18 14:52:41 +00:00
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:
parent
df3b1d654f
commit
d691bd6e86
49
speck.c
49
speck.c
|
@ -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
|
#ifdef TEST
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
@ -92,12 +133,20 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
speck_expand(key, exp);
|
speck_expand(key, exp);
|
||||||
|
|
||||||
|
#ifdef TEST_COMBINED
|
||||||
|
speck_encrypt_combined(plain, buffer, key);
|
||||||
|
#else
|
||||||
speck_encrypt(plain, buffer, exp);
|
speck_encrypt(plain, buffer, exp);
|
||||||
|
#endif
|
||||||
if (memcmp(buffer, enc, sizeof(enc))) {
|
if (memcmp(buffer, enc, sizeof(enc))) {
|
||||||
printf("encryption failed\n");
|
printf("encryption failed\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
#ifdef TEST_COMBINED
|
||||||
|
speck_decrypt_combined(enc, buffer, key);
|
||||||
|
#else
|
||||||
speck_decrypt(enc, buffer, exp);
|
speck_decrypt(enc, buffer, exp);
|
||||||
|
#endif
|
||||||
if (memcmp(buffer, plain, sizeof(enc))) {
|
if (memcmp(buffer, plain, sizeof(enc))) {
|
||||||
printf("decryption failed\n");
|
printf("decryption failed\n");
|
||||||
return 1;
|
return 1;
|
||||||
|
|
3
speck.h
3
speck.h
|
@ -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_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_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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue