Skip to content

Commit

Permalink
Merge pull request #17 from Jasuf/master
Browse files Browse the repository at this point in the history
First try at repacking. Very ugly but works
  • Loading branch information
khang06 authored May 19, 2022
2 parents bcc5c0f + ba1c262 commit 49bae80
Show file tree
Hide file tree
Showing 5 changed files with 444 additions and 29 deletions.
50 changes: 36 additions & 14 deletions blkstuff/aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,29 @@ void oqs_aes128_enc_c(const uint8_t *plaintext, const void *_schedule, uint8_t *
xor_round_key(ciphertext, schedule, 10);
}

// It's not enc nor dec, it's something in between
void oqs_mhy128_enc_c(const uint8_t *plaintext, const void *_schedule, uint8_t *ciphertext) {
const uint8_t *schedule = (const uint8_t *) _schedule;
int i; // To count the rounds

// First Round
memcpy(ciphertext, plaintext, 16);
xor_round_key(ciphertext, schedule, 0);

// Middle rounds
for (i = 0; i < 9; i++) {
sub_bytes_inv(ciphertext, 16);
shift_rows_inv(ciphertext);
mix_cols_inv(ciphertext);
xor_round_key(ciphertext, schedule, i + 1);
}

// Final Round
sub_bytes_inv(ciphertext, 16);
shift_rows_inv(ciphertext);
xor_round_key(ciphertext, schedule, 10);
}

void oqs_aes128_dec_c(const uint8_t *ciphertext, const void *_schedule, uint8_t *plaintext) {
const uint8_t *schedule = (const uint8_t *) _schedule;
int i; // To count the rounds
Expand All @@ -339,25 +362,24 @@ void oqs_aes128_dec_c(const uint8_t *ciphertext, const void *_schedule, uint8_t
xor_round_key(plaintext, schedule, 0);
}

// It's not enc nor dec, it's something in between
void oqs_mhy128_enc_c(const uint8_t *plaintext, const void *_schedule, uint8_t *ciphertext) {
void oqs_mhy128_dec_c(const uint8_t *ciphertext, const void *_schedule, uint8_t *plaintext) {
const uint8_t *schedule = (const uint8_t *) _schedule;
int i; // To count the rounds

// First Round
memcpy(ciphertext, plaintext, 16);
xor_round_key(ciphertext, schedule, 0);
// Reverse the final Round
memcpy(plaintext, ciphertext, 16);
xor_round_key(plaintext, schedule, 10);
shift_rows(plaintext);
sub_bytes(plaintext, 16);

// Middle rounds
// Reverse the middle rounds
for (i = 0; i < 9; i++) {
sub_bytes_inv(ciphertext, 16);
shift_rows_inv(ciphertext);
mix_cols_inv(ciphertext);
xor_round_key(ciphertext, schedule, i + 1);
xor_round_key(plaintext, schedule, 9 - i);
mix_cols(plaintext);
shift_rows(plaintext);
sub_bytes(plaintext, 16);
}

// Final Round
sub_bytes_inv(ciphertext, 16);
shift_rows_inv(ciphertext);
xor_round_key(ciphertext, schedule, 10);
// Reverse the first Round
xor_round_key(plaintext, schedule, 0);
}
12 changes: 12 additions & 0 deletions blkstuff/magic_constants.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include <cassert>
#include <stdint.h>

// giant constants/precalculated stuff go here
Expand Down Expand Up @@ -693,6 +694,17 @@ static inline uint8_t gf256_mul(uint8_t a, uint8_t b)
return gf256_exp[(gf256_log[a] + gf256_log[b]) % 255];
}

static inline uint8_t gf256_div(uint8_t a, uint8_t b)
{
if (a == 0)
return 0;

if (b == 0)
assert(b == 0);

return gf256_exp[(255 + gf256_log[a] - gf256_log[b]) % 255];
}

#pragma pack(1)
struct blk_header
{
Expand Down
Loading

0 comments on commit 49bae80

Please sign in to comment.