tls.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * Copyright (c) 2016 Tom Herbert <tom@herbertland.com>
  3. * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved.
  4. * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #ifndef _TLS_INT_H
  35. #define _TLS_INT_H
  36. #include <asm/byteorder.h>
  37. #include <linux/types.h>
  38. #include <linux/skmsg.h>
  39. #include <net/tls.h>
  40. #include <net/tls_prot.h>
  41. #define TLS_PAGE_ORDER (min_t(unsigned int, PAGE_ALLOC_COSTLY_ORDER, \
  42. TLS_MAX_PAYLOAD_SIZE >> PAGE_SHIFT))
  43. #define __TLS_INC_STATS(net, field) \
  44. __SNMP_INC_STATS((net)->mib.tls_statistics, field)
  45. #define TLS_INC_STATS(net, field) \
  46. SNMP_INC_STATS((net)->mib.tls_statistics, field)
  47. #define TLS_DEC_STATS(net, field) \
  48. SNMP_DEC_STATS((net)->mib.tls_statistics, field)
  49. struct tls_cipher_desc {
  50. unsigned int nonce;
  51. unsigned int iv;
  52. unsigned int key;
  53. unsigned int salt;
  54. unsigned int tag;
  55. unsigned int rec_seq;
  56. unsigned int iv_offset;
  57. unsigned int key_offset;
  58. unsigned int salt_offset;
  59. unsigned int rec_seq_offset;
  60. char *cipher_name;
  61. bool offloadable;
  62. size_t crypto_info;
  63. };
  64. #define TLS_CIPHER_MIN TLS_CIPHER_AES_GCM_128
  65. #define TLS_CIPHER_MAX TLS_CIPHER_ARIA_GCM_256
  66. extern const struct tls_cipher_desc tls_cipher_desc[TLS_CIPHER_MAX + 1 - TLS_CIPHER_MIN];
  67. static inline const struct tls_cipher_desc *get_cipher_desc(u16 cipher_type)
  68. {
  69. if (cipher_type < TLS_CIPHER_MIN || cipher_type > TLS_CIPHER_MAX)
  70. return NULL;
  71. return &tls_cipher_desc[cipher_type - TLS_CIPHER_MIN];
  72. }
  73. static inline char *crypto_info_iv(struct tls_crypto_info *crypto_info,
  74. const struct tls_cipher_desc *cipher_desc)
  75. {
  76. return (char *)crypto_info + cipher_desc->iv_offset;
  77. }
  78. static inline char *crypto_info_key(struct tls_crypto_info *crypto_info,
  79. const struct tls_cipher_desc *cipher_desc)
  80. {
  81. return (char *)crypto_info + cipher_desc->key_offset;
  82. }
  83. static inline char *crypto_info_salt(struct tls_crypto_info *crypto_info,
  84. const struct tls_cipher_desc *cipher_desc)
  85. {
  86. return (char *)crypto_info + cipher_desc->salt_offset;
  87. }
  88. static inline char *crypto_info_rec_seq(struct tls_crypto_info *crypto_info,
  89. const struct tls_cipher_desc *cipher_desc)
  90. {
  91. return (char *)crypto_info + cipher_desc->rec_seq_offset;
  92. }
  93. /* TLS records are maintained in 'struct tls_rec'. It stores the memory pages
  94. * allocated or mapped for each TLS record. After encryption, the records are
  95. * stores in a linked list.
  96. */
  97. struct tls_rec {
  98. struct list_head list;
  99. int tx_ready;
  100. int tx_flags;
  101. struct sk_msg msg_plaintext;
  102. struct sk_msg msg_encrypted;
  103. /* AAD | msg_plaintext.sg.data | sg_tag */
  104. struct scatterlist sg_aead_in[2];
  105. /* AAD | msg_encrypted.sg.data (data contains overhead for hdr & iv & tag) */
  106. struct scatterlist sg_aead_out[2];
  107. char content_type;
  108. struct scatterlist sg_content_type;
  109. struct sock *sk;
  110. char aad_space[TLS_AAD_SPACE_SIZE];
  111. u8 iv_data[TLS_MAX_IV_SIZE];
  112. /* Must be last --ends in a flexible-array member. */
  113. struct aead_request aead_req;
  114. };
  115. int __net_init tls_proc_init(struct net *net);
  116. void __net_exit tls_proc_fini(struct net *net);
  117. struct tls_context *tls_ctx_create(struct sock *sk);
  118. void tls_ctx_free(struct sock *sk, struct tls_context *ctx);
  119. void update_sk_prot(struct sock *sk, struct tls_context *ctx);
  120. int wait_on_pending_writer(struct sock *sk, long *timeo);
  121. void tls_err_abort(struct sock *sk, int err);
  122. void tls_strp_abort_strp(struct tls_strparser *strp, int err);
  123. int init_prot_info(struct tls_prot_info *prot,
  124. const struct tls_crypto_info *crypto_info,
  125. const struct tls_cipher_desc *cipher_desc);
  126. int tls_set_sw_offload(struct sock *sk, int tx,
  127. struct tls_crypto_info *new_crypto_info);
  128. void tls_update_rx_zc_capable(struct tls_context *tls_ctx);
  129. void tls_sw_strparser_arm(struct sock *sk, struct tls_context *ctx);
  130. void tls_sw_strparser_done(struct tls_context *tls_ctx);
  131. int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
  132. void tls_sw_splice_eof(struct socket *sock);
  133. void tls_sw_cancel_work_tx(struct tls_context *tls_ctx);
  134. void tls_sw_release_resources_tx(struct sock *sk);
  135. void tls_sw_free_ctx_tx(struct tls_context *tls_ctx);
  136. void tls_sw_free_resources_rx(struct sock *sk);
  137. void tls_sw_release_resources_rx(struct sock *sk);
  138. void tls_sw_free_ctx_rx(struct tls_context *tls_ctx);
  139. int tls_sw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
  140. int flags, int *addr_len);
  141. bool tls_sw_sock_is_readable(struct sock *sk);
  142. ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
  143. struct pipe_inode_info *pipe,
  144. size_t len, unsigned int flags);
  145. int tls_sw_read_sock(struct sock *sk, read_descriptor_t *desc,
  146. sk_read_actor_t read_actor);
  147. int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
  148. void tls_device_splice_eof(struct socket *sock);
  149. int tls_tx_records(struct sock *sk, int flags);
  150. void tls_sw_write_space(struct sock *sk, struct tls_context *ctx);
  151. void tls_device_write_space(struct sock *sk, struct tls_context *ctx);
  152. int tls_process_cmsg(struct sock *sk, struct msghdr *msg,
  153. unsigned char *record_type);
  154. int decrypt_skb(struct sock *sk, struct scatterlist *sgout);
  155. int tls_sw_fallback_init(struct sock *sk,
  156. struct tls_offload_context_tx *offload_ctx,
  157. struct tls_crypto_info *crypto_info);
  158. int tls_strp_dev_init(void);
  159. void tls_strp_dev_exit(void);
  160. void tls_strp_done(struct tls_strparser *strp);
  161. void tls_strp_stop(struct tls_strparser *strp);
  162. int tls_strp_init(struct tls_strparser *strp, struct sock *sk);
  163. void tls_strp_data_ready(struct tls_strparser *strp);
  164. void tls_strp_check_rcv(struct tls_strparser *strp);
  165. void tls_strp_msg_done(struct tls_strparser *strp);
  166. int tls_rx_msg_size(struct tls_strparser *strp, struct sk_buff *skb);
  167. void tls_rx_msg_ready(struct tls_strparser *strp);
  168. bool tls_strp_msg_load(struct tls_strparser *strp, bool force_refresh);
  169. int tls_strp_msg_cow(struct tls_sw_context_rx *ctx);
  170. struct sk_buff *tls_strp_msg_detach(struct tls_sw_context_rx *ctx);
  171. int tls_strp_msg_hold(struct tls_strparser *strp, struct sk_buff_head *dst);
  172. static inline struct tls_msg *tls_msg(struct sk_buff *skb)
  173. {
  174. struct sk_skb_cb *scb = (struct sk_skb_cb *)skb->cb;
  175. return &scb->tls;
  176. }
  177. static inline struct sk_buff *tls_strp_msg(struct tls_sw_context_rx *ctx)
  178. {
  179. DEBUG_NET_WARN_ON_ONCE(!ctx->strp.msg_ready || !ctx->strp.anchor->len);
  180. return ctx->strp.anchor;
  181. }
  182. static inline bool tls_strp_msg_ready(struct tls_sw_context_rx *ctx)
  183. {
  184. return READ_ONCE(ctx->strp.msg_ready);
  185. }
  186. static inline bool tls_strp_msg_mixed_decrypted(struct tls_sw_context_rx *ctx)
  187. {
  188. return ctx->strp.mixed_decrypted;
  189. }
  190. #ifdef CONFIG_TLS_DEVICE
  191. int tls_device_init(void);
  192. void tls_device_cleanup(void);
  193. int tls_set_device_offload(struct sock *sk);
  194. void tls_device_free_resources_tx(struct sock *sk);
  195. int tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx);
  196. void tls_device_offload_cleanup_rx(struct sock *sk);
  197. void tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq);
  198. int tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx);
  199. #else
  200. static inline int tls_device_init(void) { return 0; }
  201. static inline void tls_device_cleanup(void) {}
  202. static inline int
  203. tls_set_device_offload(struct sock *sk)
  204. {
  205. return -EOPNOTSUPP;
  206. }
  207. static inline void tls_device_free_resources_tx(struct sock *sk) {}
  208. static inline int
  209. tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx)
  210. {
  211. return -EOPNOTSUPP;
  212. }
  213. static inline void tls_device_offload_cleanup_rx(struct sock *sk) {}
  214. static inline void
  215. tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq) {}
  216. static inline int
  217. tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx)
  218. {
  219. return 0;
  220. }
  221. #endif
  222. int tls_push_sg(struct sock *sk, struct tls_context *ctx,
  223. struct scatterlist *sg, u16 first_offset,
  224. int flags);
  225. int tls_push_partial_record(struct sock *sk, struct tls_context *ctx,
  226. int flags);
  227. void tls_free_partial_record(struct sock *sk, struct tls_context *ctx);
  228. static inline bool tls_is_partially_sent_record(struct tls_context *ctx)
  229. {
  230. return !!ctx->partially_sent_record;
  231. }
  232. static inline bool tls_is_pending_open_record(struct tls_context *tls_ctx)
  233. {
  234. return tls_ctx->pending_open_record_frags;
  235. }
  236. static inline bool tls_bigint_increment(unsigned char *seq, int len)
  237. {
  238. int i;
  239. for (i = len - 1; i >= 0; i--) {
  240. ++seq[i];
  241. if (seq[i] != 0)
  242. break;
  243. }
  244. return (i == -1);
  245. }
  246. static inline void tls_bigint_subtract(unsigned char *seq, int n)
  247. {
  248. u64 rcd_sn;
  249. __be64 *p;
  250. BUILD_BUG_ON(TLS_MAX_REC_SEQ_SIZE != 8);
  251. p = (__be64 *)seq;
  252. rcd_sn = be64_to_cpu(*p);
  253. *p = cpu_to_be64(rcd_sn - n);
  254. }
  255. static inline void
  256. tls_advance_record_sn(struct sock *sk, struct tls_prot_info *prot,
  257. struct cipher_context *ctx)
  258. {
  259. if (tls_bigint_increment(ctx->rec_seq, prot->rec_seq_size))
  260. tls_err_abort(sk, -EBADMSG);
  261. if (prot->version != TLS_1_3_VERSION &&
  262. prot->cipher_type != TLS_CIPHER_CHACHA20_POLY1305)
  263. tls_bigint_increment(ctx->iv + prot->salt_size,
  264. prot->iv_size);
  265. }
  266. static inline void
  267. tls_xor_iv_with_seq(struct tls_prot_info *prot, char *iv, char *seq)
  268. {
  269. int i;
  270. if (prot->version == TLS_1_3_VERSION ||
  271. prot->cipher_type == TLS_CIPHER_CHACHA20_POLY1305) {
  272. for (i = 0; i < 8; i++)
  273. iv[i + 4] ^= seq[i];
  274. }
  275. }
  276. static inline void
  277. tls_fill_prepend(struct tls_context *ctx, char *buf, size_t plaintext_len,
  278. unsigned char record_type)
  279. {
  280. struct tls_prot_info *prot = &ctx->prot_info;
  281. size_t pkt_len, iv_size = prot->iv_size;
  282. pkt_len = plaintext_len + prot->tag_size;
  283. if (prot->version != TLS_1_3_VERSION &&
  284. prot->cipher_type != TLS_CIPHER_CHACHA20_POLY1305) {
  285. pkt_len += iv_size;
  286. memcpy(buf + TLS_NONCE_OFFSET,
  287. ctx->tx.iv + prot->salt_size, iv_size);
  288. }
  289. /* we cover nonce explicit here as well, so buf should be of
  290. * size KTLS_DTLS_HEADER_SIZE + KTLS_DTLS_NONCE_EXPLICIT_SIZE
  291. */
  292. buf[0] = prot->version == TLS_1_3_VERSION ?
  293. TLS_RECORD_TYPE_DATA : record_type;
  294. /* Note that VERSION must be TLS_1_2 for both TLS1.2 and TLS1.3 */
  295. buf[1] = TLS_1_2_VERSION_MINOR;
  296. buf[2] = TLS_1_2_VERSION_MAJOR;
  297. /* we can use IV for nonce explicit according to spec */
  298. buf[3] = pkt_len >> 8;
  299. buf[4] = pkt_len & 0xFF;
  300. }
  301. static inline
  302. void tls_make_aad(char *buf, size_t size, char *record_sequence,
  303. unsigned char record_type, struct tls_prot_info *prot)
  304. {
  305. if (prot->version != TLS_1_3_VERSION) {
  306. memcpy(buf, record_sequence, prot->rec_seq_size);
  307. buf += 8;
  308. } else {
  309. size += prot->tag_size;
  310. }
  311. buf[0] = prot->version == TLS_1_3_VERSION ?
  312. TLS_RECORD_TYPE_DATA : record_type;
  313. buf[1] = TLS_1_2_VERSION_MAJOR;
  314. buf[2] = TLS_1_2_VERSION_MINOR;
  315. buf[3] = size >> 8;
  316. buf[4] = size & 0xFF;
  317. }
  318. #endif