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