-
Notifications
You must be signed in to change notification settings - Fork 126
/
verifysig.c
79 lines (60 loc) · 1.91 KB
/
verifysig.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <verifysig.h>
EVP_PKEY* pkey = NULL;
void init_public_key() {
OpenSSL_add_all_digests();
BIO* bio = BIO_new_mem_buf(key, (int)sizeof(key));
assert(bio != NULL);
pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL);
assert(pkey != NULL);
BIO_free(bio);
}
int verify_sig(const byte* msg, size_t mlen, const byte* sig, size_t slen)
{
/* Returned to caller */
int result = -1;
if(!msg || !mlen || !sig || !slen || !pkey) {
return -1;
}
EVP_MD_CTX* ctx = NULL;
do
{
ctx = EVP_MD_CTX_create();
if(ctx == NULL) {
printf("EVP_MD_CTX_create failed, error 0x%lx\n", ERR_get_error());
break;
}
const EVP_MD* md = EVP_get_digestbyname(hn);
if(md == NULL) {
printf("EVP_get_digestbyname failed, error 0x%lx\n", ERR_get_error());
break;
}
int rc = EVP_DigestInit_ex(ctx, md, NULL);
if(rc != 1) {
printf("EVP_DigestInit_ex failed, error 0x%lx\n", ERR_get_error());
break;
}
rc = EVP_DigestVerifyInit(ctx, NULL, md, NULL, pkey);
if(rc != 1) {
printf("EVP_DigestVerifyInit failed, error 0x%lx\n", ERR_get_error());
break;
}
rc = EVP_DigestVerifyUpdate(ctx, msg, mlen);
if(rc != 1) {
printf("EVP_DigestVerifyUpdate failed, error 0x%lx\n", ERR_get_error());
break;
}
/* Clear any errors for the call below */
ERR_clear_error();
rc = EVP_DigestVerifyFinal(ctx, sig, slen);
if(rc != 1) {
printf("EVP_DigestVerifyFinal failed, error 0x%lx\n", ERR_get_error());
break;
}
result = 0;
} while(0);
if(ctx) {
EVP_MD_CTX_destroy(ctx);
ctx = NULL;
}
return !!result;
}