1 #ifndef __TCPCRYPT_CRYPTO_H__
2 #define __TCPCRYPT_CRYPTO_H__
3
4 typedef void *(*crypt_ctr)(void);
5
6 enum {
7 TYPE_PKEY = 0,
8 TYPE_SYM,
9 };
10
11 struct cipher_list {
12 uint8_t c_id;
13 int c_type;
14 crypt_ctr c_ctr;
15 struct cipher_list *c_next;
16 };
17
18 extern struct cipher_list *crypt_cipher_list(void);
19
20 /* low-level interface */
21
22 struct crypt {
23 void *c_priv;
24 void (*c_destroy)(struct crypt *c);
25 int (*c_set_key)(struct crypt *c, void *key, int len);
26 int (*c_get_key)(struct crypt *c, void **out);
27 void (*c_mac)(struct crypt *, const struct iovec *iov, int num, void *out,
28 int *outlen);
29 void (*c_extract)(struct crypt *c, struct iovec *iov, int num,
30 void *out, int *outlen);
31 void (*c_expand)(struct crypt *c, void *tag, int taglen,
32 void *out, int outlen);
33 int (*c_encrypt)(struct crypt *c, void *iv, void *data, int len);
34 int (*c_decrypt)(struct crypt *c, void *iv, void *data, int len);
35 int (*c_aead_encrypt)(struct crypt *c, void *iv, void *aad,
36 int aadlen, void *data, int dlen, void *tag);
37 int (*c_aead_decrypt)(struct crypt *c, void *iv, void *aad,
38 int aadlen, void *data, int dlen, void *tag);
39 int (*c_compute_key)(struct crypt *c, void *out);
40 };
41
42 extern struct crypt *crypt_HMAC_SHA256_new(void);
43 extern struct crypt *crypt_HKDF_SHA256_new(void);
44 extern struct crypt *crypt_AES128_new(void);
45 extern struct crypt *crypt_AES256_new(void);
46 extern struct crypt *crypt_RSA_new(void);
47 extern struct crypt *crypt_ECDHE256_new(void);
48 extern struct crypt *crypt_ECDHE521_new(void);
49
50 extern struct crypt *crypt_init(int sz);
51 extern void crypt_register(int type, uint8_t id, crypt_ctr ctr);
52 extern struct cipher_list *crypt_find_cipher(int type, unsigned int id);
53
crypt_destroy(struct crypt * c)54 static inline void crypt_destroy(struct crypt *c)
55 {
56 c->c_destroy(c);
57 }
58
crypt_set_key(struct crypt * c,void * key,int len)59 static inline int crypt_set_key(struct crypt *c, void *key, int len)
60 {
61 return c->c_set_key(c, key, len);
62 }
63
crypt_get_key(struct crypt * c,void ** out)64 static inline int crypt_get_key(struct crypt *c, void **out)
65 {
66 return c->c_get_key(c, out);
67 }
68
crypt_mac(struct crypt * c,struct iovec * iov,int num,void * out,int * outlen)69 static inline void crypt_mac(struct crypt *c, struct iovec *iov, int num,
70 void *out, int *outlen)
71 {
72 c->c_mac(c, iov, num, out, outlen);
73 }
74
crypt_priv(struct crypt * c)75 static inline void *crypt_priv(struct crypt *c)
76 {
77 return c->c_priv;
78 }
79
crypt_extract(struct crypt * c,struct iovec * iov,int num,void * out,int * outlen)80 static inline void crypt_extract(struct crypt *c, struct iovec *iov, int num,
81 void *out, int *outlen)
82 {
83 c->c_extract(c, iov, num, out, outlen);
84 }
85
crypt_expand(struct crypt * c,void * tag,int taglen,void * out,int outlen)86 static inline void crypt_expand(struct crypt *c, void *tag, int taglen,
87 void *out, int outlen)
88 {
89 c->c_expand(c, tag, taglen, out, outlen);
90 }
91
crypt_encrypt(struct crypt * c,void * iv,void * data,int len)92 static inline int crypt_encrypt(struct crypt *c, void *iv, void *data, int len)
93 {
94 return c->c_encrypt(c, iv, data, len);
95 }
96
crypt_decrypt(struct crypt * c,void * iv,void * data,int len)97 static inline int crypt_decrypt(struct crypt *c, void *iv, void *data, int len)
98 {
99 return c->c_decrypt(c, iv, data, len);
100 }
101
crypt_compute_key(struct crypt * c,void * out)102 static inline int crypt_compute_key(struct crypt *c, void *out)
103 {
104 return c->c_compute_key(c, out);
105 }
106
crypt_new(crypt_ctr ctr)107 static inline void *crypt_new(crypt_ctr ctr)
108 {
109 crypt_ctr *r = ctr();
110
111 *r = ctr;
112
113 return r;
114 }
115
116 /* pub crypto */
117
118 struct crypt_pub {
119 crypt_ctr cp_ctr; /* must be first */
120 struct crypt *cp_hkdf;
121 struct crypt *cp_pub;
122 int cp_n_c;
123 int cp_n_s;
124 int cp_k_len;
125 int cp_min_key;
126 int cp_max_key;
127 int cp_cipher_len;
128 int cp_key_agreement;
129 };
130
crypt_pub_destroy(struct crypt_pub * cp)131 static inline void crypt_pub_destroy(struct crypt_pub *cp)
132 {
133 crypt_destroy(cp->cp_hkdf);
134 crypt_destroy(cp->cp_pub);
135 free(cp);
136 }
137
138 /* sym crypto */
139
140 struct crypt_sym {
141 crypt_ctr cs_ctr; /* must be first */
142 struct crypt *cs_cipher;
143 struct crypt *cs_mac;
144 struct crypt *cs_ack_mac;
145 int cs_mac_len;
146 int cs_key_len;
147 int cs_iv_len;
148 };
149
crypt_sym_destroy(struct crypt_sym * cs)150 static inline void crypt_sym_destroy(struct crypt_sym *cs)
151 {
152 crypt_destroy(cs->cs_cipher);
153 crypt_destroy(cs->cs_mac);
154 crypt_destroy(cs->cs_ack_mac);
155 free(cs);
156 }
157
158 #endif /* __TCPCRYPT_CRYPTO_H__ */
159