-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcrypto_sign.c
58 lines (44 loc) · 1.03 KB
/
crypto_sign.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "sodium.h"
#define MAX_MSG_LEN 64
int sign(uint8_t sm[], const uint8_t m[], const int mlen, const uint8_t sk[]) {
unsigned long long smlen;
if( crypto_sign(sm,&smlen, m, mlen, sk) == 0) {
return smlen;
} else {
return -1;
}
}
int verify(uint8_t m[], const uint8_t sm[], const int smlen, const uint8_t pk[]) {
unsigned long long mlen;
if( crypto_sign_open(m, &mlen, sm, smlen, pk) == 0) {
return mlen;
} else {
return -1;
}
}
int main() {
uint8_t sk[crypto_sign_SECRETKEYBYTES];
uint8_t pk[crypto_sign_PUBLICKEYBYTES];
uint8_t sm[MAX_MSG_LEN+crypto_sign_BYTES];
uint8_t m[MAX_MSG_LEN+crypto_sign_BYTES];
memset(m, '\0', MAX_MSG_LEN);
int mlen = snprintf(m, MAX_MSG_LEN, "%s", "Hello World!");
int rc = crypto_sign_keypair(pk, sk);
if(rc < 0) {
return 1;
}
int smlen = sign(sm, m, mlen, sk);
if(smlen < 0) {
return 1;
}
mlen = verify(m, sm, smlen, pk);
if(mlen < 0) {
return 1;
}
printf("Verified!\n");
return 0;
}