1 /*-
2  * Copyright 2016 Vsevolod Stakhov
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef KEYPAIR_PRIVATE_H_
17 #define KEYPAIR_PRIVATE_H_
18 
19 #include "config.h"
20 #include "ref.h"
21 #include "cryptobox.h"
22 
23 #ifdef  __cplusplus
24 extern "C" {
25 #endif
26 /*
27  * KEX cached data
28  */
29 struct rspamd_cryptobox_nm {
30 	guchar nm[rspamd_cryptobox_MAX_NMBYTES];
31 	guint64 sk_id; /* Used to store secret key id */
32 	ref_entry_t ref;
33 };
34 
35 /*
36  * Generic keypair
37  */
38 struct rspamd_cryptobox_keypair {
39 	guchar id[rspamd_cryptobox_HASHBYTES];
40 	enum rspamd_cryptobox_keypair_type type;
41 	enum rspamd_cryptobox_mode alg;
42 	ref_entry_t ref;
43 };
44 
45 /*
46  * NIST p256 ecdh keypair
47  */
48 #define RSPAMD_CRYPTOBOX_KEYPAIR_NIST(x) ((struct rspamd_cryptobox_keypair_nist *)(x))
49 struct rspamd_cryptobox_keypair_nist {
50 	struct rspamd_cryptobox_keypair parent;
51 	guchar sk[32];
52 	guchar pk[65];
53 };
54 
55 /*
56  * Curve25519 ecdh keypair
57  */
58 #define RSPAMD_CRYPTOBOX_KEYPAIR_25519(x) ((struct rspamd_cryptobox_keypair_25519 *)(x))
59 struct rspamd_cryptobox_keypair_25519 {
60 	struct rspamd_cryptobox_keypair parent;
61 	guchar sk[32];
62 	guchar pk[32];
63 };
64 
65 /*
66  * NIST p256 ecdsa keypair
67  */
68 #define RSPAMD_CRYPTOBOX_KEYPAIR_SIG_NIST(x) ((struct rspamd_cryptobox_keypair_sig_nist *)(x))
69 struct rspamd_cryptobox_keypair_sig_nist {
70 	struct rspamd_cryptobox_keypair parent;
71 	guchar sk[32];
72 	guchar pk[65];
73 };
74 
75 /*
76  * Ed25519 keypair
77  */
78 #define RSPAMD_CRYPTOBOX_KEYPAIR_SIG_25519(x) ((struct rspamd_cryptobox_keypair_sig_25519 *)(x))
79 struct rspamd_cryptobox_keypair_sig_25519 {
80 	struct rspamd_cryptobox_keypair parent;
81 	guchar sk[64];
82 	guchar pk[32];
83 };
84 
85 /*
86  * Public component of the keypair
87  */
88 struct rspamd_cryptobox_pubkey {
89 	guchar id[rspamd_cryptobox_HASHBYTES];
90 	struct rspamd_cryptobox_nm *nm;
91 	enum rspamd_cryptobox_keypair_type type;
92 	enum rspamd_cryptobox_mode alg;
93 	ref_entry_t ref;
94 };
95 
96 /*
97  * Public p256 ecdh
98  */
99 #define RSPAMD_CRYPTOBOX_PUBKEY_NIST(x) ((struct rspamd_cryptobox_pubkey_nist *)(x))
100 struct rspamd_cryptobox_pubkey_nist {
101 	struct rspamd_cryptobox_pubkey parent;
102 	guchar pk[65];
103 };
104 
105 /*
106  * Public curve25519 ecdh
107  */
108 #define RSPAMD_CRYPTOBOX_PUBKEY_25519(x) ((struct rspamd_cryptobox_pubkey_25519 *)(x))
109 struct rspamd_cryptobox_pubkey_25519 {
110 	struct rspamd_cryptobox_pubkey parent;
111 	guchar pk[32];
112 };
113 
114 /*
115  * Public p256 ecdsa
116  */
117 #define RSPAMD_CRYPTOBOX_PUBKEY_SIG_NIST(x) ((struct rspamd_cryptobox_pubkey_sig_nist *)(x))
118 struct rspamd_cryptobox_pubkey_sig_nist {
119 	struct rspamd_cryptobox_pubkey parent;
120 	guchar pk[65];
121 };
122 
123 /*
124  * Public ed25519
125  */
126 #define RSPAMD_CRYPTOBOX_PUBKEY_SIG_25519(x) ((struct rspamd_cryptobox_pubkey_sig_25519 *)(x))
127 struct rspamd_cryptobox_pubkey_sig_25519 {
128 	struct rspamd_cryptobox_pubkey parent;
129 	guchar pk[32];
130 };
131 
132 void rspamd_cryptobox_nm_dtor (struct rspamd_cryptobox_nm *nm);
133 
134 void rspamd_cryptobox_keypair_dtor (struct rspamd_cryptobox_keypair *kp);
135 
136 void rspamd_cryptobox_pubkey_dtor (struct rspamd_cryptobox_pubkey *p);
137 
138 #ifdef  __cplusplus
139 }
140 #endif
141 
142 #endif /* KEYPAIR_PRIVATE_H_ */
143