2014-09-24 17:40:21 -04:00
|
|
|
/*
|
|
|
|
* GraxRabble
|
2015-05-27 15:36:54 -04:00
|
|
|
* example programs for libsodium.
|
2014-09-24 17:40:21 -04:00
|
|
|
*/
|
|
|
|
|
2015-05-27 07:02:56 -04:00
|
|
|
#include <sodium.h> /* library header */
|
2014-09-24 17:40:21 -04:00
|
|
|
|
2015-05-27 15:36:54 -04:00
|
|
|
#include "utils.h" /* utility functions shared by examples */
|
2014-09-24 17:40:21 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Signs a message with secret key which will authenticate a message.
|
|
|
|
* Everybody else can use the public key to ensure that the message is both
|
|
|
|
* valid and untampered.
|
|
|
|
*
|
2014-09-25 13:21:44 -04:00
|
|
|
* Note that the signed message will have 16 bytes of signature prepended.
|
|
|
|
* Ensure that the signed buffer is at least crypto_sign_BYTES longer then
|
|
|
|
* the actual message.
|
2014-09-24 17:40:21 -04:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
sign(void)
|
|
|
|
{
|
2015-05-27 14:53:54 -04:00
|
|
|
unsigned char pk[crypto_sign_PUBLICKEYBYTES]; /* Bob's public key */
|
|
|
|
unsigned char sk[crypto_sign_SECRETKEYBYTES]; /* Bob's secret key */
|
2015-05-27 15:19:17 -04:00
|
|
|
unsigned char message[MAX_INPUT_LEN];
|
|
|
|
unsigned char message_signed[crypto_sign_BYTES + MAX_INPUT_LEN];
|
2015-05-27 14:53:54 -04:00
|
|
|
unsigned long long message_len;
|
|
|
|
unsigned long long message_signed_len;
|
|
|
|
int ret;
|
2014-09-24 17:40:21 -04:00
|
|
|
|
|
|
|
puts("Example: crypto_sign\n");
|
|
|
|
|
|
|
|
puts("Generating keypair...");
|
2015-05-27 07:02:56 -04:00
|
|
|
crypto_sign_keypair(pk, sk); /* generate Bob's keys */
|
2014-09-24 17:40:21 -04:00
|
|
|
|
2015-05-27 15:09:32 -04:00
|
|
|
printf("Public key: ");
|
2014-09-24 17:40:21 -04:00
|
|
|
print_hex(pk, sizeof pk);
|
2015-05-27 15:09:32 -04:00
|
|
|
printf("Secret key: ");
|
2014-09-24 17:40:21 -04:00
|
|
|
print_hex(sk, sizeof sk);
|
2015-05-27 14:53:54 -04:00
|
|
|
puts("The secret key, as returned by crypto_sign_keypair(), actually includes "
|
|
|
|
"a copy of the public key, in order to avoid a scalar multiplication "
|
|
|
|
"when signing messages.");
|
2014-09-24 17:40:21 -04:00
|
|
|
|
2015-05-27 14:53:54 -04:00
|
|
|
message_len = prompt_input("a message", (char*)message, sizeof message, 1);
|
2014-09-24 17:40:21 -04:00
|
|
|
|
|
|
|
printf("Signing message with %s...\n", crypto_sign_primitive());
|
2015-05-27 14:53:54 -04:00
|
|
|
crypto_sign(message_signed, &message_signed_len, message, message_len, sk);
|
2015-05-27 07:02:56 -04:00
|
|
|
|
2015-05-27 14:53:54 -04:00
|
|
|
printf("Signed message:");
|
|
|
|
print_hex(message_signed, message_signed_len);
|
|
|
|
printf("A %u bytes signature was prepended to the message\n",
|
|
|
|
crypto_sign_BYTES);
|
2015-05-27 07:02:56 -04:00
|
|
|
|
2015-05-27 15:09:32 -04:00
|
|
|
printf("Signature: ");
|
2015-05-27 14:53:54 -04:00
|
|
|
print_hex(message_signed, crypto_sign_BYTES);
|
2015-05-27 15:09:32 -04:00
|
|
|
printf("Message: ");
|
2015-05-27 14:53:54 -04:00
|
|
|
fwrite(message_signed + crypto_sign_BYTES, 1U,
|
|
|
|
message_signed_len - crypto_sign_BYTES, stdout);
|
2015-05-27 14:00:05 -04:00
|
|
|
putchar('\n');
|
2014-09-24 17:40:21 -04:00
|
|
|
|
|
|
|
puts("Validating message...");
|
2015-05-27 14:53:54 -04:00
|
|
|
ret = crypto_sign_open(message, &message_len, message_signed,
|
|
|
|
message_signed_len, pk);
|
|
|
|
print_verification(ret);
|
|
|
|
if (ret == 0)
|
|
|
|
printf("Message: %s\n", message);
|
2015-05-27 07:02:56 -04:00
|
|
|
|
2015-05-27 09:46:17 -04:00
|
|
|
sodium_memzero(sk, sizeof sk); /* wipe sensitive data */
|
2015-05-27 14:27:07 -04:00
|
|
|
|
2015-05-27 14:53:54 -04:00
|
|
|
return ret;
|
2014-09-24 17:40:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-05-27 06:41:43 -04:00
|
|
|
main(void)
|
2014-09-24 17:40:21 -04:00
|
|
|
{
|
2015-05-27 10:10:07 -04:00
|
|
|
init();
|
2014-09-24 17:40:21 -04:00
|
|
|
|
2015-05-27 06:41:43 -04:00
|
|
|
return sign() != 0;
|
2014-09-24 17:40:21 -04:00
|
|
|
}
|