Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 56 additions & 55 deletions ascon.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#include <stdio.h>
typedef unsigned __int64 bit64;
#include <stdint.h>
typedef uint64_t bit64;

bit64 state[5] = { 0 }, t[5] = { 0 };
bit64 constants[16] = {0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87, 0x78, 0x69, 0x5a, 0x4b, 0x3c, 0x2d, 0x1e, 0x0f};

bit64 print_state(bit64 state[5]){
void print_state(bit64 local_state[5]) {
for(int i = 0; i < 5; i++){
printf("%016I64x\n", state[i]);
printf("%016lx\n", local_state[i]);
}
}

Expand All @@ -16,9 +17,9 @@ bit64 rotate(bit64 x, int l) {
return temp;
}

void add_constant(bit64 state[5], int i, int a) {
// Menambah konstan pada state blok ke 2 sesuai dengan spec Ascon
state[2] = state[2] ^ constants[12 - a + i];
void add_constant(bit64 local_state[5], int i, int a) {
// Menambah konstan pada local_state blok ke 2 sesuai dengan spec Ascon
local_state[2] = local_state[2] ^ constants[12 - a + i];
}
void sbox(bit64 x[5]) {
// Mensubtitusikan angka menjadi angka baru pada state sesuai dengan sbox
Expand All @@ -34,75 +35,75 @@ void sbox(bit64 x[5]) {
x[0] ^= t[1]; x[1] ^= t[2]; x[2] ^= t[3]; x[3] ^= t[4]; x[4] ^= t[0];
x[1] ^= x[0]; x[0] ^= x[4]; x[3] ^= x[2]; x[2] =~ x[2];
}
void linear(bit64 state[5]) {
// Kita akan melakukan operasi rotasi terhadap state dengan tiap
void linear(bit64 local_state[5]) {
// Kita akan melakukan operasi rotasi terhadap local_state dengan tiap
// 64 bit memiliki rotasi yang berbeda.
// besar bit rotasi ditentukan pada spec ascon paper.

bit64 temp0, temp1;
temp0 = rotate(state[0], 19);
temp1 = rotate(state[0], 28);
state[0] ^= temp0 ^ temp1;
temp0 = rotate(state[1], 61);
temp1 = rotate(state[1], 39);
state[1] ^= temp0 ^ temp1;
temp0 = rotate(state[2], 1);
temp1 = rotate(state[2], 6);
state[2] ^= temp0 ^ temp1;
temp0 = rotate(state[3], 10);
temp1 = rotate(state[3], 17);
state[3] ^= temp0 ^ temp1;
temp0 = rotate(state[4], 7);
temp1 = rotate(state[4], 41);
state[4] ^= temp0 ^ temp1;
temp0 = rotate(local_state[0], 19);
temp1 = rotate(local_state[0], 28);
local_state[0] ^= temp0 ^ temp1;
temp0 = rotate(local_state[1], 61);
temp1 = rotate(local_state[1], 39);
local_state[1] ^= temp0 ^ temp1;
temp0 = rotate(local_state[2], 1);
temp1 = rotate(local_state[2], 6);
local_state[2] ^= temp0 ^ temp1;
temp0 = rotate(local_state[3], 10);
temp1 = rotate(local_state[3], 17);
local_state[3] ^= temp0 ^ temp1;
temp0 = rotate(local_state[4], 7);
temp1 = rotate(local_state[4], 41);
local_state[4] ^= temp0 ^ temp1;
}

void p(bit64 state[5], int a){
void p(bit64 local_state[5], int a){
for (int i = 0; i < a; i++){
add_constant(state, i, a);
sbox(state);
linear(state);
add_constant(local_state, i, a);
sbox(local_state);
linear(local_state);
}
}

void initialization(bit64 state[5], bit64 key[2]) {
p(state, 12);
state[3] ^= key[0];
state[4] ^= key[1];
void initialization(bit64 local_state[5], const bit64 key[2]) {
p(local_state, 12);
local_state[3] ^= key[0];
local_state[4] ^= key[1];
}

void associated_data(bit64 state[5], int length, bit64 associated_data_text[]) {
void associated_data(bit64 local_state[5], int length, const bit64 associated_data_text[]) {
for (int i = 0; i < length; i++){
state[0] = associated_data_text[i] ^ state[0];
p(state, 6);
local_state[0] = associated_data_text[i] ^ local_state[0];
p(local_state, 6);
}
state[5] = state[5] ^ 0x0000000000000001;
local_state[5] = local_state[5] ^ 0x0000000000000001;
}

void finalization(bit64 state[5], bit64 key[2]) {
state[1] ^= key[0];
state[2] ^= key[1];
p(state, 12);
state[3] ^= key[0];
state[4] ^= key[1];
void finalization(bit64 local_state[5], const bit64 key[2]) {
local_state[1] ^= key[0];
local_state[2] ^= key[1];
p(local_state, 12);
local_state[3] ^= key[0];
local_state[4] ^= key[1];

}

void encrypt(bit64 state[5], int length, bit64 plaintext[], bit64 ciphertext[]) {
ciphertext[0] = plaintext[0] ^ state[0];
void encrypt(bit64 local_state[5], int length, const bit64 plaintext[], bit64 ciphertext[]) {
ciphertext[0] = plaintext[0] ^ local_state[0];
for (int i = 1; i < length; i++){
p(state, 6);
ciphertext[i] = plaintext[i] ^ state[0];
state[0] = ciphertext[i];
p(local_state, 6);
ciphertext[i] = plaintext[i] ^ local_state[0];
local_state[0] = ciphertext[i];
}
}

void decrypt(bit64 state[5], int length, bit64 plaintext[], bit64 ciphertext[]){
plaintext[0] = ciphertext[0] ^ state[0];
void decrypt(bit64 local_state[5], int length, bit64 plaintext[], const bit64 ciphertext[]){
plaintext[0] = ciphertext[0] ^ local_state[0];
for (int i = 1; i < length; i++){
p(state, 6);
plaintext[i] = ciphertext[i] ^ state[0];
state[0] = ciphertext[i];
p(local_state, 6);
plaintext[i] = ciphertext[i] ^ local_state[0];
local_state[0] = ciphertext[i];
}
}

Expand All @@ -127,9 +128,9 @@ int main() {
associated_data(state, 3, associated_data_text);
print_state(state);
encrypt(state, 2, plaintext, ciphertext);
printf("\nciphertext: %016I64x %016I64x\n", ciphertext[0], ciphertext[1]);
printf("\nciphertext: %016lx %016lx\n", ciphertext[0], ciphertext[1]);
finalization(state, key);
printf("tag: %016I64x %016I64x\n", state[3], state[4]);
printf("tag: %016lx %016lx\n", state[3], state[4]);



Expand All @@ -152,6 +153,6 @@ int main() {
print_state(state);
associated_data(state, 3, associated_data_text);
decrypt(state, 2, plaintextdecrypt, ciphertextdecrypt);
printf("\nplaintext: %016I64x %016I64x\n", plaintextdecrypt[0], plaintextdecrypt[1]);
printf("\nplaintext: %016lx %016lx\n", plaintextdecrypt[0], plaintextdecrypt[1]);
finalization(state, key);
printf("tag: %016I64x %016I64x\n", state[3], state[4]);}
printf("tag: %016lx %016lx\n", state[3], state[4]);}