55 lines
1.4 KiB
C
55 lines
1.4 KiB
C
|
|
#include "skein.h"
|
||
|
|
#include <stdint.h>
|
||
|
|
#include <stdio.h>
|
||
|
|
#include <stdlib.h>
|
||
|
|
|
||
|
|
static uint8_t *read_file(const char *path, size_t *length) {
|
||
|
|
FILE *file = fopen(path, "rb");
|
||
|
|
if (file == NULL) {
|
||
|
|
return NULL;
|
||
|
|
}
|
||
|
|
fseek(file, 0, SEEK_END);
|
||
|
|
long size = ftell(file);
|
||
|
|
rewind(file);
|
||
|
|
uint8_t *bytes = malloc((size_t)size);
|
||
|
|
if (size < 0 || bytes == NULL ||
|
||
|
|
fread(bytes, 1, (size_t)size, file) != (size_t)size) {
|
||
|
|
return NULL;
|
||
|
|
}
|
||
|
|
fclose(file);
|
||
|
|
*length = (size_t)size;
|
||
|
|
return bytes;
|
||
|
|
}
|
||
|
|
|
||
|
|
int main(void) {
|
||
|
|
size_t message_len;
|
||
|
|
size_t rust_hash_len;
|
||
|
|
uint8_t *message = read_file("fixtures/message.bin", &message_len);
|
||
|
|
uint8_t *rust_hash = read_file("fixtures/rust-skein256.bin", &rust_hash_len);
|
||
|
|
if (message == NULL || rust_hash == NULL || rust_hash_len != 32) {
|
||
|
|
fprintf(stderr, "Could not read Skein fixtures\n");
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
uint8_t extension_hash[32];
|
||
|
|
Skein_256_Ctxt_t context;
|
||
|
|
Skein_256_Init(&context, 256);
|
||
|
|
Skein_256_Update(&context, message, message_len);
|
||
|
|
Skein_256_Final(&context, extension_hash);
|
||
|
|
|
||
|
|
int equal = 1;
|
||
|
|
for (size_t i = 0; i < sizeof(extension_hash); i++) {
|
||
|
|
if (extension_hash[i] != rust_hash[i]) {
|
||
|
|
equal = 0;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
printf("PHP-Skein-Hash output matches Contractless Rust: %s\n",
|
||
|
|
equal ? "true" : "false");
|
||
|
|
|
||
|
|
free(rust_hash);
|
||
|
|
free(message);
|
||
|
|
return equal ? 0 : 2;
|
||
|
|
}
|
||
|
|
|