-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrypto_api.c
122 lines (99 loc) · 3.05 KB
/
crypto_api.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
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
117
118
119
120
121
122
/*
* This file is part of the BFE library.
* See the accompanying documentation for complete details.
*
* The code is provided under the CC0 license, see LICENSE for more details.
* SPDX-License-Identifier: CC0-1.0
*/
#include "include/crypto_api.h"
#include "include/bfe-bf.h"
#include <string.h>
int crypto_kem_keypair(unsigned char* serialized_pk, unsigned char* serialized_sk) {
bfe_bf_secret_key_t sk;
bfe_bf_public_key_t pk;
// generate new keys
bfe_bf_init_secret_key(&sk);
bfe_bf_init_public_key(&pk);
int status = bfe_bf_keygen(&pk, &sk, CRYPTO_BYTES, 1 << 19, 0.0009765625);
if (!status) {
bfe_bf_public_key_serialize(serialized_pk, &pk);
memcpy(serialized_sk, serialized_pk, CRYPTO_PUBLICKEYBYTES);
bfe_bf_secret_key_serialize(serialized_sk + CRYPTO_PUBLICKEYBYTES, &sk);
}
bfe_bf_clear_secret_key(&sk);
bfe_bf_clear_public_key(&pk);
return status;
}
int crypto_kem_enc(unsigned char* serialized_ct, unsigned char* k,
const unsigned char* serialized_pk) {
bfe_bf_public_key_t pk;
// deserialize the public key
int status = bfe_bf_public_key_deserialize(&pk, serialized_pk);
if (status) {
return status;
}
// encaps a new key
bfe_bf_ciphertext_t ciphertext;
bfe_bf_init_ciphertext(&ciphertext, &pk);
status = bfe_bf_encaps(&ciphertext, k, &pk);
if (status) {
goto ret;
}
// serialize the ciphertext
bfe_bf_ciphertext_serialize(serialized_ct, &ciphertext);
ret:
// clean up
bfe_bf_clear_ciphertext(&ciphertext);
bfe_bf_clear_public_key(&pk);
return status;
}
int crypto_kem_dec(unsigned char* k, const unsigned char* serialized_ct,
const unsigned char* serialized_sk) {
bfe_bf_public_key_t pk;
// deserialize the public key
int status = bfe_bf_public_key_deserialize(&pk, serialized_sk);
if (status) {
return status;
}
// deserialize the secret key
bfe_bf_secret_key_t sk;
status = bfe_bf_secret_key_deserialize(&sk, serialized_sk + CRYPTO_PUBLICKEYBYTES);
if (status) {
goto ret;
}
// deserialize the ciphertext
bfe_bf_ciphertext_t ct;
status = bfe_bf_ciphertext_deserialize(&ct, serialized_ct);
if (status) {
goto ret;
}
// decaps ciphertext
status = bfe_bf_decaps(k, &pk, &sk, &ct);
ret:
bfe_bf_clear_ciphertext(&ct);
bfe_bf_clear_secret_key(&sk);
bfe_bf_clear_public_key(&pk);
return status;
}
int crypto_kem_punc(unsigned char* serialized_sk, const unsigned char* serialized_ct) {
// deserialize the secret key
bfe_bf_secret_key_t sk;
int status = bfe_bf_secret_key_deserialize(&sk, serialized_sk + CRYPTO_PUBLICKEYBYTES);
if (status) {
return status;
}
// deserialize the ciphertext
bfe_bf_ciphertext_t ciphertext;
status = bfe_bf_ciphertext_deserialize(&ciphertext, serialized_ct);
if (status) {
goto ret;
}
// puncture secret key and serialized it again
bfe_bf_puncture(&sk, &ciphertext);
bfe_bf_secret_key_serialize(serialized_sk + CRYPTO_PUBLICKEYBYTES, &sk);
ret:
// clean up
bfe_bf_clear_ciphertext(&ciphertext);
bfe_bf_clear_secret_key(&sk);
return status;
}