66 lines
1.7 KiB
C
66 lines
1.7 KiB
C
#include "monocypher.h"
|
|
#include "seed.h"
|
|
#include <assert.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define PRINT_HEX(arr, len) print_hex(#arr, (const uint8_t *)(arr), (len))
|
|
|
|
void print_hex(const char *name, const uint8_t *data, size_t len) {
|
|
printf("%s (%zu bytes):\n", name, len);
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
if (i % 16 == 0)
|
|
printf(" %04zx: ", i);
|
|
|
|
printf("%02X ", data[i]);
|
|
|
|
if ((i + 1) % 16 == 0 || i + 1 == len)
|
|
printf("\n");
|
|
}
|
|
}
|
|
|
|
int main(void) {
|
|
/* These can be used if not on bare metal */
|
|
// uint8_t seed[32];
|
|
// arc4random_buf(seed, 32);
|
|
|
|
uint8_t sk[64]; /* secret key */
|
|
uint8_t pk[32]; /* Matching public key */
|
|
PRINT_HEX(seed, seed_len);
|
|
|
|
/* The seed is wiped automatically here */
|
|
crypto_eddsa_key_pair(sk, pk, seed);
|
|
|
|
print_hex("PubKey", pk, 32);
|
|
print_hex("SecKey", sk, 64);
|
|
|
|
uint8_t message[4];
|
|
|
|
/* Generate a random message */
|
|
arc4random_buf(message, 4);
|
|
PRINT_HEX(message, sizeof(message));
|
|
|
|
uint8_t sig[64];
|
|
|
|
/* Sign it and store the signature in sig[] */
|
|
crypto_eddsa_sign(sig, sk, (const uint8_t *)message, sizeof(message));
|
|
print_hex("Signature:", sig, 64);
|
|
|
|
/* Assert valid */
|
|
assert(!crypto_eddsa_check(sig, pk, (const uint8_t *)message, sizeof(message)));
|
|
|
|
/* Tamper with payload to invalidate the signature */
|
|
arc4random_buf(message, 4);
|
|
PRINT_HEX(message, sizeof(message));
|
|
|
|
/* Assert invalid */
|
|
assert(crypto_eddsa_check(sig, pk, (const uint8_t *)message, sizeof(message)));
|
|
|
|
/* Wipe the secret key if it is no longer needed */
|
|
crypto_wipe(sk, 32);
|
|
|
|
printf("Program finished: Ok!\n");
|
|
}
|