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