summaryrefslogtreecommitdiff
path: root/keypair.c
blob: 5c099ad66cbc13e44fb8e21ad802e3048b86adcd (plain)
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
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <stdio.h>

#include "rnd.h"
#include "die.h"
#include "str.h"
#include "crypto.h"
#include "curve.h"
#include "ioops.h"
#include "config.h"
#include "keypair.h"

void generate_keypair(void)
{
	struct passwd *pw = getpwuid(getuid());
	unsigned char publickey[crypto_box_pub_key_size];
	unsigned char secretkey[crypto_box_sec_key_size];
	char file[128];

	xmemset(publickey, 0, sizeof(publickey));
	xmemset(secretkey, 0, sizeof(secretkey));

	curve25519_selftest();

	printf("Reading from %s (this may take a while) ...\n",
	       HIG_ENTROPY_SOURCE);

	gen_key_bytes(secretkey, sizeof(secretkey));
	crypto_scalarmult_curve25519_base(publickey, secretkey);

	slprintf(file, sizeof(file), "%s/%s", pw->pw_dir, FILE_PUBKEY);
	write_blob_or_die(file, publickey, sizeof(publickey));
	printf("Public key written to %s!\n", file);

	slprintf(file, sizeof(file), "%s/%s", pw->pw_dir, FILE_PRIVKEY);
	write_blob_or_die(file, secretkey, sizeof(secretkey));
	printf("Secret key written to %s!\n", file);

	xmemset(publickey, 0, sizeof(publickey));
	xmemset(secretkey, 0, sizeof(secretkey));
}

void verify_keypair(void)
{
	int result;
	struct passwd *pw = getpwuid(getuid());
	unsigned char publickey[crypto_box_pub_key_size];
	unsigned char publicres[crypto_box_pub_key_size];
	unsigned char secretkey[crypto_box_sec_key_size];
	char file[128];

	curve25519_selftest();

	xmemset(publickey, 0, sizeof(publickey));
	xmemset(publicres, 0, sizeof(publicres));
	xmemset(secretkey, 0, sizeof(secretkey));

	slprintf(file, sizeof(file), "%s/%s", pw->pw_dir, FILE_PUBKEY);
	read_blob_or_die(file, publickey, sizeof(publickey));

	slprintf(file, sizeof(file), "%s/%s", pw->pw_dir, FILE_PRIVKEY);
	read_blob_or_die(file, secretkey, sizeof(secretkey));

	crypto_scalarmult_curve25519_base(publicres, secretkey);
	result = crypto_verify_32(publicres, publickey);

	xmemset(publickey, 0, sizeof(publickey));
	xmemset(publicres, 0, sizeof(publicres));
	xmemset(secretkey, 0, sizeof(secretkey));

	if (result)
		panic("Keypair is corrupt! You need to regenerate!\n");
}
lass='diffstat-header'>Diffstat (limited to 'drivers/usb/host/ohci-s3c2410.c')