tls.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved.
  3. * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #ifndef _TLS_OFFLOAD_H
  34. #define _TLS_OFFLOAD_H
  35. #include <linux/types.h>
  36. #include <uapi/linux/tls.h>
  37. /* Maximum data size carried in a TLS record */
  38. #define TLS_MAX_PAYLOAD_SIZE ((size_t)1 << 14)
  39. #define TLS_HEADER_SIZE 5
  40. #define TLS_NONCE_OFFSET TLS_HEADER_SIZE
  41. #define TLS_CRYPTO_INFO_READY(info) ((info)->cipher_type)
  42. #define TLS_RECORD_TYPE_DATA 0x17
  43. #define TLS_AAD_SPACE_SIZE 13
  44. struct tls_sw_context {
  45. struct crypto_aead *aead_send;
  46. /* Sending context */
  47. char aad_space[TLS_AAD_SPACE_SIZE];
  48. unsigned int sg_plaintext_size;
  49. int sg_plaintext_num_elem;
  50. struct scatterlist sg_plaintext_data[MAX_SKB_FRAGS];
  51. unsigned int sg_encrypted_size;
  52. int sg_encrypted_num_elem;
  53. struct scatterlist sg_encrypted_data[MAX_SKB_FRAGS];
  54. /* AAD | sg_plaintext_data | sg_tag */
  55. struct scatterlist sg_aead_in[2];
  56. /* AAD | sg_encrypted_data (data contain overhead for hdr&iv&tag) */
  57. struct scatterlist sg_aead_out[2];
  58. };
  59. enum {
  60. TLS_PENDING_CLOSED_RECORD
  61. };
  62. struct tls_context {
  63. union {
  64. struct tls_crypto_info crypto_send;
  65. struct tls12_crypto_info_aes_gcm_128 crypto_send_aes_gcm_128;
  66. };
  67. void *priv_ctx;
  68. u16 prepend_size;
  69. u16 tag_size;
  70. u16 overhead_size;
  71. u16 iv_size;
  72. char *iv;
  73. u16 rec_seq_size;
  74. char *rec_seq;
  75. struct scatterlist *partially_sent_record;
  76. u16 partially_sent_offset;
  77. unsigned long flags;
  78. u16 pending_open_record_frags;
  79. int (*push_pending_record)(struct sock *sk, int flags);
  80. void (*free_resources)(struct sock *sk);
  81. void (*sk_write_space)(struct sock *sk);
  82. void (*sk_proto_close)(struct sock *sk, long timeout);
  83. int (*setsockopt)(struct sock *sk, int level,
  84. int optname, char __user *optval,
  85. unsigned int optlen);
  86. int (*getsockopt)(struct sock *sk, int level,
  87. int optname, char __user *optval,
  88. int __user *optlen);
  89. };
  90. int wait_on_pending_writer(struct sock *sk, long *timeo);
  91. int tls_sk_query(struct sock *sk, int optname, char __user *optval,
  92. int __user *optlen);
  93. int tls_sk_attach(struct sock *sk, int optname, char __user *optval,
  94. unsigned int optlen);
  95. int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx);
  96. int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
  97. int tls_sw_sendpage(struct sock *sk, struct page *page,
  98. int offset, size_t size, int flags);
  99. void tls_sw_close(struct sock *sk, long timeout);
  100. void tls_sk_destruct(struct sock *sk, struct tls_context *ctx);
  101. void tls_icsk_clean_acked(struct sock *sk);
  102. int tls_push_sg(struct sock *sk, struct tls_context *ctx,
  103. struct scatterlist *sg, u16 first_offset,
  104. int flags);
  105. int tls_push_pending_closed_record(struct sock *sk, struct tls_context *ctx,
  106. int flags, long *timeo);
  107. static inline bool tls_is_pending_closed_record(struct tls_context *ctx)
  108. {
  109. return test_bit(TLS_PENDING_CLOSED_RECORD, &ctx->flags);
  110. }
  111. static inline int tls_complete_pending_work(struct sock *sk,
  112. struct tls_context *ctx,
  113. int flags, long *timeo)
  114. {
  115. int rc = 0;
  116. if (unlikely(sk->sk_write_pending))
  117. rc = wait_on_pending_writer(sk, timeo);
  118. if (!rc && tls_is_pending_closed_record(ctx))
  119. rc = tls_push_pending_closed_record(sk, ctx, flags, timeo);
  120. return rc;
  121. }
  122. static inline bool tls_is_partially_sent_record(struct tls_context *ctx)
  123. {
  124. return !!ctx->partially_sent_record;
  125. }
  126. static inline bool tls_is_pending_open_record(struct tls_context *tls_ctx)
  127. {
  128. return tls_ctx->pending_open_record_frags;
  129. }
  130. static inline void tls_err_abort(struct sock *sk)
  131. {
  132. sk->sk_err = -EBADMSG;
  133. sk->sk_error_report(sk);
  134. }
  135. static inline bool tls_bigint_increment(unsigned char *seq, int len)
  136. {
  137. int i;
  138. for (i = len - 1; i >= 0; i--) {
  139. ++seq[i];
  140. if (seq[i] != 0)
  141. break;
  142. }
  143. return (i == -1);
  144. }
  145. static inline void tls_advance_record_sn(struct sock *sk,
  146. struct tls_context *ctx)
  147. {
  148. if (tls_bigint_increment(ctx->rec_seq, ctx->rec_seq_size))
  149. tls_err_abort(sk);
  150. tls_bigint_increment(ctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
  151. ctx->iv_size);
  152. }
  153. static inline void tls_fill_prepend(struct tls_context *ctx,
  154. char *buf,
  155. size_t plaintext_len,
  156. unsigned char record_type)
  157. {
  158. size_t pkt_len, iv_size = ctx->iv_size;
  159. pkt_len = plaintext_len + iv_size + ctx->tag_size;
  160. /* we cover nonce explicit here as well, so buf should be of
  161. * size KTLS_DTLS_HEADER_SIZE + KTLS_DTLS_NONCE_EXPLICIT_SIZE
  162. */
  163. buf[0] = record_type;
  164. buf[1] = TLS_VERSION_MINOR(ctx->crypto_send.version);
  165. buf[2] = TLS_VERSION_MAJOR(ctx->crypto_send.version);
  166. /* we can use IV for nonce explicit according to spec */
  167. buf[3] = pkt_len >> 8;
  168. buf[4] = pkt_len & 0xFF;
  169. memcpy(buf + TLS_NONCE_OFFSET,
  170. ctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, iv_size);
  171. }
  172. static inline struct tls_context *tls_get_ctx(const struct sock *sk)
  173. {
  174. struct inet_connection_sock *icsk = inet_csk(sk);
  175. return icsk->icsk_ulp_data;
  176. }
  177. static inline struct tls_sw_context *tls_sw_ctx(
  178. const struct tls_context *tls_ctx)
  179. {
  180. return (struct tls_sw_context *)tls_ctx->priv_ctx;
  181. }
  182. static inline struct tls_offload_context *tls_offload_ctx(
  183. const struct tls_context *tls_ctx)
  184. {
  185. return (struct tls_offload_context *)tls_ctx->priv_ctx;
  186. }
  187. int tls_proccess_cmsg(struct sock *sk, struct msghdr *msg,
  188. unsigned char *record_type);
  189. #endif /* _TLS_OFFLOAD_H */