Skip to content

Instantly share code, notes, and snippets.

@menangen
Last active March 13, 2023 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save menangen/05ed78491ccd83865914349eb1c51ad0 to your computer and use it in GitHub Desktop.
Save menangen/05ed78491ccd83865914349eb1c51ad0 to your computer and use it in GitHub Desktop.
Speck 32 Block 64 Key - C / C ++
#include <stdio.h>
#include <inttypes.h>
#define ROUNDS 32
static inline void
speck_hash(
uint8_t& x, uint8_t& y,
const uint8_t key)
{
x = (x >> 5) | (x << (8 - 5));
x += y;
x ^= key;
x ^= 167 | (key - y);
y ^= (y << 2) | (y >> (8 - 2));
y ^= x;
}
void
speck_block(
const uint8_t plaintext[2],
const uint8_t key[2]
)
{
uint8_t ciphertext[2];
ciphertext[0] = plaintext[0];
ciphertext[1] = plaintext[1];
uint8_t b = key[0];
uint8_t a = key[1];
for (unsigned char i = 0; i < ROUNDS; i++) {
speck_hash(ciphertext[1], ciphertext[0], b);
unsigned char K, L, M, N;
K = i + 1;
K ^= (K << 3) | (K >> (8 - 3));
L = K ^ 277;
M = K ^ 193;
N = (L >> 2) | (M << (8 - 2));
N ^= i;
speck_hash(a, b, N);
}
printf("Hash: 0x%02x%02x \n", ciphertext[0], ciphertext[1]);
}
int main(void)
{
uint8_t plaintext[2] = { 0xFF, 71 };
uint8_t key[2] = { 167, 79};
speck_block(plaintext, key);
printf("Plaintext: 0x%02x 0x%02x \n", plaintext[0], plaintext[1]);
printf("Key: 0x%02x 0x%02x \n", key[0], key[1] );
return 0;
}
#include <stdio.h>
#include <inttypes.h>
#include <assert.h>
#define ROUNDS 22
#define WORD sizeof(uint16_t)
void
speck_round(uint16_t * x, uint16_t * y, uint16_t k)
{
*x = (*x >> 7) | (*x << (8 * WORD - 7));
*x += *y;
*x ^= k;
*y = (*y << 2) | (*y >> (8 * WORD - 2));
*y ^= *x;
}
void speck_block(
uint16_t * plaintext,
uint16_t * key,
uint16_t * ciphertext
)
{
ciphertext[0] = plaintext[0];
ciphertext[1] = plaintext[1];
uint16_t b = key[0];
uint16_t a0 = key[1];
uint16_t a1 = key[2];
uint16_t a2 = key[3];
for (uint16_t i = 0; i < ROUNDS; i++) {
speck_round(&ciphertext[1], &ciphertext[0], b);
uint16_t a = a0;
speck_round(&a, &b, i);
a0 = a1;
a1 = a2;
a2 = a;
}
}
int main(void)
{
uint16_t plaintext[2] = {0x694c, 0x6574};
uint16_t key[4] = {0x0100, 0x0908, 0x1110, 0x1918};
uint16_t ciphertext[2];
speck_block(&plaintext[0], &key[0], &ciphertext[0]);
printf("Plaintext: 0x%04x 0x%04x\n", plaintext[0], plaintext[1]);
printf("Key: 0x%04x 0x%04x 0x%04x 0x%04x\n", key[0], key[1], key[2], key[3]);
printf("Ciphertext: 0x%04x 0x%04x\n", ciphertext[0], ciphertext[1]);
printf("Expected: 0x42f2 0xa868\n");
assert( ciphertext[0] == 0x42f2);
assert( ciphertext[1] == 0xa868);
return 0;
}
#include <stdio.h>
#include <inttypes.h>
#define ROUNDS 22
static inline void speck_round(uint16_t& x, uint16_t& y, const uint16_t k)
{
x = (x >> 7) | (x << (8 * sizeof(x) - 7)); // x = ROTR(x, 7)
x += y;
x ^= k;
y = (y << 2) | (y >> (8 * sizeof(y) - 2)); // y = ROTL(y, 2)
y ^= x;
}
void speck_block( const uint16_t plaintext[2]
, const uint16_t key[4]
, uint16_t ciphertext[2]
)
{
ciphertext[0] = plaintext[0];
ciphertext[1] = plaintext[1];
uint16_t b = key[0];
uint16_t a0 = key[1];
uint16_t a1 = key[2];
uint16_t a2 = key[3];
for (unsigned i = 0; i < ROUNDS; i++) {
speck_round(ciphertext[1], ciphertext[0], b);
uint16_t a = a0;
speck_round(a, b, i);
a0 = a1;
a1 = a2;
a2 = a;
}
}
int main(void)
{
uint16_t plaintext[2] = {0x694c, 0x6574};
uint16_t key[4] = {0x0100, 0x0908, 0x1110, 0x1918};
uint16_t ciphertext[2];
speck_block(plaintext, key, ciphertext);
printf("Plaintext: 0x%04x 0x%04x\n", plaintext[0], plaintext[1]);
printf("Key: 0x%04x 0x%04x 0x%04x 0x%04x\n", key[0], key[1], key[2], key[3]);
printf("Ciphertext: 0x%04x 0x%04x\n", ciphertext[0], ciphertext[1]);
printf("Expected: 0x42f2 0xa868\n");
return 0;
}
#include <stdio.h>
#include <inttypes.h>
#define ROUNDS 16
static inline void
speck_round(
uint8_t& x, uint8_t& y,
const uint8_t key)
{
x = (x >> 5) | (x << (8 - 5));
x += y;
x ^= key;
x ^= 167 | (key - y);
y ^= (y << 2) | (y >> (8 - 2));
y ^= x;
}
void
speck_block(
const uint8_t plaintext,
const uint8_t key[2]
)
{
uint8_t ciphertext = plaintext;
uint8_t b = key[0];
uint8_t a = key[1];
for (unsigned i = 0; i < ROUNDS; i++) {
speck_round(ciphertext, a, b);
speck_round(a, b, i);
}
printf("Ciphertext: 0x%02x \n", ciphertext);
}
int main(void)
{
uint8_t plaintext = 76;
uint8_t key[2] = { 167, 79};
speck_block(plaintext, key);
printf("Plaintext: 0x%02x \n", plaintext);
printf("Key: 0x%02x 0x%02x \n", key[0], key[1] );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment