summaryrefslogtreecommitdiff
path: root/ecp/util/util.c
blob: 2b671271c21d7d62e03d1f3cf7671d44d10a2f9d (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>

#include <core.h>

#include "util.h"

int ecp_util_load_key(ecp_ecdh_public_t *public, ecp_ecdh_private_t *private, char *filename) {
    int fd;
    ssize_t rv;

    if ((fd = open(filename, O_RDONLY)) < 0) return ECP_ERR;
    rv = read(fd, public, sizeof(ecp_ecdh_public_t));
    if (rv != sizeof(ecp_ecdh_public_t)) {
        close(fd);
        return ECP_ERR;
    }
    rv = read(fd, private, sizeof(ecp_ecdh_private_t));
    if (rv != sizeof(ecp_ecdh_private_t)) {
        close(fd);
        return ECP_ERR;
    }
    close(fd);
    return ECP_OK;
}

int ecp_util_save_key(ecp_ecdh_public_t *public, ecp_ecdh_private_t *private, char *filename) {
    int fd;
    ssize_t rv;

    if ((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) return ECP_ERR;
    rv = write(fd, public, sizeof(ecp_ecdh_public_t));
    if (rv != sizeof(ecp_ecdh_public_t)) {
        close(fd);
        return ECP_ERR;
    }
    rv = write(fd, private, sizeof(ecp_ecdh_private_t));
    if (rv != sizeof(ecp_ecdh_private_t)) {
        close(fd);
        return ECP_ERR;
    }
    close(fd);
    return ECP_OK;
}

int ecp_util_load_pub(ecp_ecdh_public_t *public, char *filename) {
    int fd;
    ssize_t rv;

    if ((fd = open(filename, O_RDONLY)) < 0) return ECP_ERR;
    rv = read(fd, public, sizeof(ecp_ecdh_public_t));
    if (rv != sizeof(ecp_ecdh_public_t)) {
        close(fd);
        return ECP_ERR;
    }
    close(fd);
    return ECP_OK;
}

int ecp_util_save_pub(ecp_ecdh_public_t *public, char *filename) {
    int fd;
    ssize_t rv;

    if ((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) return ECP_ERR;
    rv = write(fd, public, sizeof(ecp_ecdh_public_t));
    if (rv != sizeof(ecp_ecdh_public_t)) {
        close(fd);
        return ECP_ERR;
    }
    close(fd);
    return ECP_OK;
}

int ecp_util_load_node(ECPNode *node, char *filename) {
    int fd;
    ssize_t rv;

    if ((fd = open(filename, O_RDONLY)) < 0) return ECP_ERR;
    rv = read(fd, &node->key_perma.public, sizeof(node->key_perma.public));
    if (rv != sizeof(node->key_perma.public)) {
        close(fd);
        return ECP_ERR;
    }
    rv = read(fd, &node->addr, sizeof(node->addr));
    if (rv != sizeof(node->addr)) {
        close(fd);
        return ECP_ERR;
    }
    close(fd);

    node->key_perma.valid = 1;
    return ECP_OK;
}

int ecp_util_save_node(ECPNode *node, char *filename) {
    int fd;
    ssize_t rv;

    if (!node->key_perma.valid) return ECP_ERR;

    if ((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) return ECP_ERR;
    rv = write(fd, &node->key_perma.public, sizeof(node->key_perma.public));
    if (rv != sizeof(node->key_perma.public)) {
        close(fd);
        return ECP_ERR;
    }
    rv = write(fd, &node->addr, sizeof(node->addr));
    if (rv != sizeof(node->addr)) {
        close(fd);
        return ECP_ERR;
    }
    close(fd);
    return ECP_OK;
}