2014-09-24 17:40:21 -04:00
|
|
|
/*
|
|
|
|
* GraxRabble
|
|
|
|
* Demo programs for libsodium.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-05-27 07:02:56 -04:00
|
|
|
#include <sodium.h> /* library header */
|
2014-09-24 17:40:21 -04:00
|
|
|
|
2015-05-27 09:47:49 -04:00
|
|
|
#include "utils.h" /* utility functions shared by demos */
|
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 09:39:34 -04:00
|
|
|
unsigned char pk[crypto_sign_PUBLICKEYBYTES]; /* Bob public */
|
|
|
|
unsigned char sk[crypto_sign_SECRETKEYBYTES]; /* Bob secret */
|
|
|
|
unsigned char m[MAX_INPUT_SIZE]; /* message */
|
|
|
|
unsigned char sm[MAX_INPUT_SIZE + crypto_sign_BYTES]; /* signed message */
|
|
|
|
unsigned long long int mlen; /* message length */
|
|
|
|
unsigned long long int smlen; /* signed length */
|
2014-09-24 17:40:21 -04:00
|
|
|
int r;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
fputs("Public: ", stdout);
|
|
|
|
print_hex(pk, sizeof pk);
|
2015-05-27 14:27:07 -04:00
|
|
|
putchar('\n');
|
2014-09-24 17:40:21 -04:00
|
|
|
fputs("Secret: ", stdout);
|
|
|
|
print_hex(sk, sizeof sk);
|
2015-05-27 14:27:07 -04:00
|
|
|
putchar('\n');
|
2014-09-24 17:40:21 -04:00
|
|
|
|
2015-05-27 14:01:03 -04:00
|
|
|
mlen = prompt_input("a message", (char*)m, sizeof m, 1);
|
|
|
|
putchar('\n');
|
2015-05-27 07:02:56 -04:00
|
|
|
|
2014-09-24 19:39:35 -04:00
|
|
|
puts("Notice the message has no prepended padding");
|
|
|
|
print_hex(m, mlen);
|
|
|
|
putchar('\n');
|
2014-09-24 17:40:21 -04:00
|
|
|
|
|
|
|
printf("Signing message with %s...\n", crypto_sign_primitive());
|
|
|
|
crypto_sign(sm, &smlen, m, mlen, sk);
|
2015-05-27 07:02:56 -04:00
|
|
|
|
2014-09-24 19:39:35 -04:00
|
|
|
puts("Notice the signed message has prepended signature");
|
|
|
|
print_hex(sm, smlen);
|
|
|
|
putchar('\n');
|
2015-05-27 07:02:56 -04:00
|
|
|
|
2015-05-27 14:00:05 -04:00
|
|
|
fputs("Signature: ", stdout);
|
2014-09-24 17:40:21 -04:00
|
|
|
print_hex(sm, crypto_sign_BYTES);
|
2015-05-27 14:00:05 -04:00
|
|
|
putchar('\n');
|
|
|
|
fputs("Message: ", stdout);
|
|
|
|
fwrite(sm + crypto_sign_BYTES, 1U, smlen - crypto_sign_BYTES, stdout);
|
|
|
|
putchar('\n');
|
2014-09-24 17:40:21 -04:00
|
|
|
|
|
|
|
puts("Validating message...");
|
|
|
|
r = crypto_sign_open(m, &mlen, sm, smlen, pk);
|
|
|
|
|
|
|
|
print_verification(r);
|
2015-05-27 07:02:56 -04:00
|
|
|
if (r == 0)
|
2015-05-27 14:27:07 -04:00
|
|
|
printf("Message: %s\n", m);
|
2015-05-27 07:02:56 -04:00
|
|
|
|
2015-05-27 09:46:17 -04:00
|
|
|
sodium_memzero(sk, sizeof sk); /* wipe sensitive data */
|
2014-09-24 17:40:21 -04:00
|
|
|
sodium_memzero(m, sizeof m);
|
|
|
|
sodium_memzero(sm, sizeof sm);
|
2015-05-27 14:27:07 -04:00
|
|
|
|
2014-09-24 17:40:21 -04:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|