-
Notifications
You must be signed in to change notification settings - Fork 9
/
sign.c
73 lines (64 loc) · 1.6 KB
/
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <openssl/conf.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
int pass_cb(char *buf, int size, int rwflag, void *u) {
int len;
char *tmp;
/* We'd probably do something else if 'rwflag' is 1 */
if (u) {
tmp = "test";
len = strlen(tmp);
memcpy(buf, tmp, len);
return len;
} else {
return 0;
}
}
EVP_PKEY* load_private_key(const char* file) {
BIO *keybio;
if ((keybio = BIO_new_file(file, "r")) == NULL) {
ERR_print_errors_fp(stderr);
exit(0);
}
EVP_PKEY* pkey = PEM_read_bio_PrivateKey(keybio, NULL, pass_cb, "test key");
if (pkey == NULL) {
ERR_print_errors_fp(stderr);
exit(0);
}
return pkey;
}
int main(int arc, char *argv[]) {
ERR_load_crypto_strings();
OpenSSL_add_all_algorithms();
OPENSSL_no_config();
EVP_MD_CTX* mdctx = NULL;
const EVP_MD* md = NULL;
char msg[] = "Bajja\n";
unsigned char sig[1024];
md = EVP_get_digestbyname("SHA256");
unsigned int sig_len = 0;
int i = 0;;
EVP_PKEY* pkey = load_private_key("test.key");
// Create a Message Digest Context for the operations
mdctx = EVP_MD_CTX_new();
ENGINE* engine = NULL;
assert(mdctx != NULL);
EVP_SignInit_ex(mdctx, md, engine);
EVP_SignUpdate(mdctx, msg, strlen(msg));
EVP_SignFinal(mdctx, sig, &sig_len, pkey);
printf("sig_len: %d\n", sig_len);
EVP_MD_CTX_free(mdctx);
printf("Digest is: ");
for (i = 0; i < sig_len; i++) {
printf("%02x", sig[i]);
}
printf("\n");
EVP_cleanup();
CRYPTO_cleanup_all_ex_data();
ERR_free_strings();
return 0;
}