crypto.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /**
  3. * net/tipc/crypto.h: Include file for TIPC crypto
  4. *
  5. * Copyright (c) 2019, Ericsson AB
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the names of the copyright holders nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * Alternatively, this software may be distributed under the terms of the
  21. * GNU General Public License ("GPL") version 2 as published by the Free
  22. * Software Foundation.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #ifdef CONFIG_TIPC_CRYPTO
  37. #ifndef _TIPC_CRYPTO_H
  38. #define _TIPC_CRYPTO_H
  39. #include "core.h"
  40. #include "node.h"
  41. #include "msg.h"
  42. #include "bearer.h"
  43. #define TIPC_EVERSION 7
  44. /* AEAD aes(gcm) */
  45. #define TIPC_AES_GCM_KEY_SIZE_128 16
  46. #define TIPC_AES_GCM_KEY_SIZE_192 24
  47. #define TIPC_AES_GCM_KEY_SIZE_256 32
  48. #define TIPC_AES_GCM_SALT_SIZE 4
  49. #define TIPC_AES_GCM_IV_SIZE 12
  50. #define TIPC_AES_GCM_TAG_SIZE 16
  51. /**
  52. * TIPC crypto modes:
  53. * - CLUSTER_KEY:
  54. * One single key is used for both TX & RX in all nodes in the cluster.
  55. * - PER_NODE_KEY:
  56. * Each nodes in the cluster has one TX key, for RX a node needs to know
  57. * its peers' TX key for the decryption of messages from those nodes.
  58. */
  59. enum {
  60. CLUSTER_KEY = 1,
  61. PER_NODE_KEY = (1 << 1),
  62. };
  63. extern int sysctl_tipc_max_tfms __read_mostly;
  64. extern int sysctl_tipc_key_exchange_enabled __read_mostly;
  65. /**
  66. * TIPC encryption message format:
  67. *
  68. * 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
  69. * 1 0 9 8 7 6 5 4|3 2 1 0 9 8 7 6|5 4 3 2 1 0 9 8|7 6 5 4 3 2 1 0
  70. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  71. * w0:|Ver=7| User |D|TX |RX |K|M|N| Rsvd |
  72. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  73. * w1:| Seqno |
  74. * w2:| (8 octets) |
  75. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  76. * w3:\ Prevnode \
  77. * / (4 or 16 octets) /
  78. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  79. * \ \
  80. * / Encrypted complete TIPC V2 header and user data /
  81. * \ \
  82. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  83. * | |
  84. * | AuthTag |
  85. * | (16 octets) |
  86. * | |
  87. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  88. *
  89. * Word0:
  90. * Ver : = 7 i.e. TIPC encryption message version
  91. * User : = 7 (for LINK_PROTOCOL); = 13 (for LINK_CONFIG) or = 0
  92. * D : The destined bit i.e. the message's destination node is
  93. * "known" or not at the message encryption
  94. * TX : TX key used for the message encryption
  95. * RX : Currently RX active key corresponding to the destination
  96. * node's TX key (when the "D" bit is set)
  97. * K : Keep-alive bit (for RPS, LINK_PROTOCOL/STATE_MSG only)
  98. * M : Bit indicates if sender has master key
  99. * N : Bit indicates if sender has no RX keys corresponding to the
  100. * receiver's TX (when the "D" bit is set)
  101. * Rsvd : Reserved bit, field
  102. * Word1-2:
  103. * Seqno : The 64-bit sequence number of the encrypted message, also
  104. * part of the nonce used for the message encryption/decryption
  105. * Word3-:
  106. * Prevnode: The source node address, or ID in case LINK_CONFIG only
  107. * AuthTag : The authentication tag for the message integrity checking
  108. * generated by the message encryption
  109. */
  110. struct tipc_ehdr {
  111. union {
  112. struct {
  113. #if defined(__LITTLE_ENDIAN_BITFIELD)
  114. __u8 destined:1,
  115. user:4,
  116. version:3;
  117. __u8 reserved_1:1,
  118. rx_nokey:1,
  119. master_key:1,
  120. keepalive:1,
  121. rx_key_active:2,
  122. tx_key:2;
  123. #elif defined(__BIG_ENDIAN_BITFIELD)
  124. __u8 version:3,
  125. user:4,
  126. destined:1;
  127. __u8 tx_key:2,
  128. rx_key_active:2,
  129. keepalive:1,
  130. master_key:1,
  131. rx_nokey:1,
  132. reserved_1:1;
  133. #else
  134. #error "Please fix <asm/byteorder.h>"
  135. #endif
  136. __be16 reserved_2;
  137. } __packed;
  138. __be32 w0;
  139. };
  140. __be64 seqno;
  141. union {
  142. __be32 addr;
  143. __u8 id[NODE_ID_LEN]; /* For a LINK_CONFIG message only! */
  144. };
  145. #define EHDR_SIZE (offsetof(struct tipc_ehdr, addr) + sizeof(__be32))
  146. #define EHDR_CFG_SIZE (sizeof(struct tipc_ehdr))
  147. #define EHDR_MIN_SIZE (EHDR_SIZE)
  148. #define EHDR_MAX_SIZE (EHDR_CFG_SIZE)
  149. #define EMSG_OVERHEAD (EHDR_SIZE + TIPC_AES_GCM_TAG_SIZE)
  150. } __packed;
  151. int tipc_crypto_start(struct tipc_crypto **crypto, struct net *net,
  152. struct tipc_node *node);
  153. void tipc_crypto_stop(struct tipc_crypto **crypto);
  154. void tipc_crypto_timeout(struct tipc_crypto *rx);
  155. int tipc_crypto_xmit(struct net *net, struct sk_buff **skb,
  156. struct tipc_bearer *b, struct tipc_media_addr *dst,
  157. struct tipc_node *__dnode);
  158. int tipc_crypto_rcv(struct net *net, struct tipc_crypto *rx,
  159. struct sk_buff **skb, struct tipc_bearer *b);
  160. int tipc_crypto_key_init(struct tipc_crypto *c, struct tipc_aead_key *ukey,
  161. u8 mode, bool master_key);
  162. void tipc_crypto_key_flush(struct tipc_crypto *c);
  163. int tipc_crypto_key_distr(struct tipc_crypto *tx, u8 key,
  164. struct tipc_node *dest);
  165. void tipc_crypto_msg_rcv(struct net *net, struct sk_buff *skb);
  166. void tipc_crypto_rekeying_sched(struct tipc_crypto *tx, bool changed,
  167. u32 new_intv);
  168. int tipc_aead_key_validate(struct tipc_aead_key *ukey, struct genl_info *info);
  169. bool tipc_ehdr_validate(struct sk_buff *skb);
  170. static inline u32 msg_key_gen(struct tipc_msg *m)
  171. {
  172. return msg_bits(m, 4, 16, 0xffff);
  173. }
  174. static inline void msg_set_key_gen(struct tipc_msg *m, u32 gen)
  175. {
  176. msg_set_bits(m, 4, 16, 0xffff, gen);
  177. }
  178. static inline u32 msg_key_mode(struct tipc_msg *m)
  179. {
  180. return msg_bits(m, 4, 0, 0xf);
  181. }
  182. static inline void msg_set_key_mode(struct tipc_msg *m, u32 mode)
  183. {
  184. msg_set_bits(m, 4, 0, 0xf, mode);
  185. }
  186. #endif /* _TIPC_CRYPTO_H */
  187. #endif