18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * net/tipc/crypto.h: Include file for TIPC crypto 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 2019, Ericsson AB 68c2ecf20Sopenharmony_ci * All rights reserved. 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without 98c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions are met: 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright 128c2ecf20Sopenharmony_ci * notice, this list of conditions and the following disclaimer. 138c2ecf20Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright 148c2ecf20Sopenharmony_ci * notice, this list of conditions and the following disclaimer in the 158c2ecf20Sopenharmony_ci * documentation and/or other materials provided with the distribution. 168c2ecf20Sopenharmony_ci * 3. Neither the names of the copyright holders nor the names of its 178c2ecf20Sopenharmony_ci * contributors may be used to endorse or promote products derived from 188c2ecf20Sopenharmony_ci * this software without specific prior written permission. 198c2ecf20Sopenharmony_ci * 208c2ecf20Sopenharmony_ci * Alternatively, this software may be distributed under the terms of the 218c2ecf20Sopenharmony_ci * GNU General Public License ("GPL") version 2 as published by the Free 228c2ecf20Sopenharmony_ci * Software Foundation. 238c2ecf20Sopenharmony_ci * 248c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 258c2ecf20Sopenharmony_ci * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 268c2ecf20Sopenharmony_ci * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 278c2ecf20Sopenharmony_ci * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 288c2ecf20Sopenharmony_ci * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 298c2ecf20Sopenharmony_ci * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 308c2ecf20Sopenharmony_ci * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 318c2ecf20Sopenharmony_ci * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 328c2ecf20Sopenharmony_ci * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 338c2ecf20Sopenharmony_ci * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 348c2ecf20Sopenharmony_ci * POSSIBILITY OF SUCH DAMAGE. 358c2ecf20Sopenharmony_ci */ 368c2ecf20Sopenharmony_ci#ifdef CONFIG_TIPC_CRYPTO 378c2ecf20Sopenharmony_ci#ifndef _TIPC_CRYPTO_H 388c2ecf20Sopenharmony_ci#define _TIPC_CRYPTO_H 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci#include "core.h" 418c2ecf20Sopenharmony_ci#include "node.h" 428c2ecf20Sopenharmony_ci#include "msg.h" 438c2ecf20Sopenharmony_ci#include "bearer.h" 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci#define TIPC_EVERSION 7 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci/* AEAD aes(gcm) */ 488c2ecf20Sopenharmony_ci#define TIPC_AES_GCM_KEY_SIZE_128 16 498c2ecf20Sopenharmony_ci#define TIPC_AES_GCM_KEY_SIZE_192 24 508c2ecf20Sopenharmony_ci#define TIPC_AES_GCM_KEY_SIZE_256 32 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci#define TIPC_AES_GCM_SALT_SIZE 4 538c2ecf20Sopenharmony_ci#define TIPC_AES_GCM_IV_SIZE 12 548c2ecf20Sopenharmony_ci#define TIPC_AES_GCM_TAG_SIZE 16 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci/* 578c2ecf20Sopenharmony_ci * TIPC crypto modes: 588c2ecf20Sopenharmony_ci * - CLUSTER_KEY: 598c2ecf20Sopenharmony_ci * One single key is used for both TX & RX in all nodes in the cluster. 608c2ecf20Sopenharmony_ci * - PER_NODE_KEY: 618c2ecf20Sopenharmony_ci * Each nodes in the cluster has one TX key, for RX a node needs to know 628c2ecf20Sopenharmony_ci * its peers' TX key for the decryption of messages from those nodes. 638c2ecf20Sopenharmony_ci */ 648c2ecf20Sopenharmony_cienum { 658c2ecf20Sopenharmony_ci CLUSTER_KEY = 1, 668c2ecf20Sopenharmony_ci PER_NODE_KEY = (1 << 1), 678c2ecf20Sopenharmony_ci}; 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ciextern int sysctl_tipc_max_tfms __read_mostly; 708c2ecf20Sopenharmony_ciextern int sysctl_tipc_key_exchange_enabled __read_mostly; 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci/* 738c2ecf20Sopenharmony_ci * TIPC encryption message format: 748c2ecf20Sopenharmony_ci * 758c2ecf20Sopenharmony_ci * 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 768c2ecf20Sopenharmony_ci * 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 778c2ecf20Sopenharmony_ci * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 788c2ecf20Sopenharmony_ci * w0:|Ver=7| User |D|TX |RX |K|M|N| Rsvd | 798c2ecf20Sopenharmony_ci * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 808c2ecf20Sopenharmony_ci * w1:| Seqno | 818c2ecf20Sopenharmony_ci * w2:| (8 octets) | 828c2ecf20Sopenharmony_ci * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 838c2ecf20Sopenharmony_ci * w3:\ Prevnode \ 848c2ecf20Sopenharmony_ci * / (4 or 16 octets) / 858c2ecf20Sopenharmony_ci * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 868c2ecf20Sopenharmony_ci * \ \ 878c2ecf20Sopenharmony_ci * / Encrypted complete TIPC V2 header and user data / 888c2ecf20Sopenharmony_ci * \ \ 898c2ecf20Sopenharmony_ci * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 908c2ecf20Sopenharmony_ci * | | 918c2ecf20Sopenharmony_ci * | AuthTag | 928c2ecf20Sopenharmony_ci * | (16 octets) | 938c2ecf20Sopenharmony_ci * | | 948c2ecf20Sopenharmony_ci * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 958c2ecf20Sopenharmony_ci * 968c2ecf20Sopenharmony_ci * Word0: 978c2ecf20Sopenharmony_ci * Ver : = 7 i.e. TIPC encryption message version 988c2ecf20Sopenharmony_ci * User : = 7 (for LINK_PROTOCOL); = 13 (for LINK_CONFIG) or = 0 998c2ecf20Sopenharmony_ci * D : The destined bit i.e. the message's destination node is 1008c2ecf20Sopenharmony_ci * "known" or not at the message encryption 1018c2ecf20Sopenharmony_ci * TX : TX key used for the message encryption 1028c2ecf20Sopenharmony_ci * RX : Currently RX active key corresponding to the destination 1038c2ecf20Sopenharmony_ci * node's TX key (when the "D" bit is set) 1048c2ecf20Sopenharmony_ci * K : Keep-alive bit (for RPS, LINK_PROTOCOL/STATE_MSG only) 1058c2ecf20Sopenharmony_ci * M : Bit indicates if sender has master key 1068c2ecf20Sopenharmony_ci * N : Bit indicates if sender has no RX keys corresponding to the 1078c2ecf20Sopenharmony_ci * receiver's TX (when the "D" bit is set) 1088c2ecf20Sopenharmony_ci * Rsvd : Reserved bit, field 1098c2ecf20Sopenharmony_ci * Word1-2: 1108c2ecf20Sopenharmony_ci * Seqno : The 64-bit sequence number of the encrypted message, also 1118c2ecf20Sopenharmony_ci * part of the nonce used for the message encryption/decryption 1128c2ecf20Sopenharmony_ci * Word3-: 1138c2ecf20Sopenharmony_ci * Prevnode: The source node address, or ID in case LINK_CONFIG only 1148c2ecf20Sopenharmony_ci * AuthTag : The authentication tag for the message integrity checking 1158c2ecf20Sopenharmony_ci * generated by the message encryption 1168c2ecf20Sopenharmony_ci */ 1178c2ecf20Sopenharmony_cistruct tipc_ehdr { 1188c2ecf20Sopenharmony_ci union { 1198c2ecf20Sopenharmony_ci struct { 1208c2ecf20Sopenharmony_ci#if defined(__LITTLE_ENDIAN_BITFIELD) 1218c2ecf20Sopenharmony_ci __u8 destined:1, 1228c2ecf20Sopenharmony_ci user:4, 1238c2ecf20Sopenharmony_ci version:3; 1248c2ecf20Sopenharmony_ci __u8 reserved_1:1, 1258c2ecf20Sopenharmony_ci rx_nokey:1, 1268c2ecf20Sopenharmony_ci master_key:1, 1278c2ecf20Sopenharmony_ci keepalive:1, 1288c2ecf20Sopenharmony_ci rx_key_active:2, 1298c2ecf20Sopenharmony_ci tx_key:2; 1308c2ecf20Sopenharmony_ci#elif defined(__BIG_ENDIAN_BITFIELD) 1318c2ecf20Sopenharmony_ci __u8 version:3, 1328c2ecf20Sopenharmony_ci user:4, 1338c2ecf20Sopenharmony_ci destined:1; 1348c2ecf20Sopenharmony_ci __u8 tx_key:2, 1358c2ecf20Sopenharmony_ci rx_key_active:2, 1368c2ecf20Sopenharmony_ci keepalive:1, 1378c2ecf20Sopenharmony_ci master_key:1, 1388c2ecf20Sopenharmony_ci rx_nokey:1, 1398c2ecf20Sopenharmony_ci reserved_1:1; 1408c2ecf20Sopenharmony_ci#else 1418c2ecf20Sopenharmony_ci#error "Please fix <asm/byteorder.h>" 1428c2ecf20Sopenharmony_ci#endif 1438c2ecf20Sopenharmony_ci __be16 reserved_2; 1448c2ecf20Sopenharmony_ci } __packed; 1458c2ecf20Sopenharmony_ci __be32 w0; 1468c2ecf20Sopenharmony_ci }; 1478c2ecf20Sopenharmony_ci __be64 seqno; 1488c2ecf20Sopenharmony_ci union { 1498c2ecf20Sopenharmony_ci __be32 addr; 1508c2ecf20Sopenharmony_ci __u8 id[NODE_ID_LEN]; /* For a LINK_CONFIG message only! */ 1518c2ecf20Sopenharmony_ci }; 1528c2ecf20Sopenharmony_ci#define EHDR_SIZE (offsetof(struct tipc_ehdr, addr) + sizeof(__be32)) 1538c2ecf20Sopenharmony_ci#define EHDR_CFG_SIZE (sizeof(struct tipc_ehdr)) 1548c2ecf20Sopenharmony_ci#define EHDR_MIN_SIZE (EHDR_SIZE) 1558c2ecf20Sopenharmony_ci#define EHDR_MAX_SIZE (EHDR_CFG_SIZE) 1568c2ecf20Sopenharmony_ci#define EMSG_OVERHEAD (EHDR_SIZE + TIPC_AES_GCM_TAG_SIZE) 1578c2ecf20Sopenharmony_ci} __packed; 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ciint tipc_crypto_start(struct tipc_crypto **crypto, struct net *net, 1608c2ecf20Sopenharmony_ci struct tipc_node *node); 1618c2ecf20Sopenharmony_civoid tipc_crypto_stop(struct tipc_crypto **crypto); 1628c2ecf20Sopenharmony_civoid tipc_crypto_timeout(struct tipc_crypto *rx); 1638c2ecf20Sopenharmony_ciint tipc_crypto_xmit(struct net *net, struct sk_buff **skb, 1648c2ecf20Sopenharmony_ci struct tipc_bearer *b, struct tipc_media_addr *dst, 1658c2ecf20Sopenharmony_ci struct tipc_node *__dnode); 1668c2ecf20Sopenharmony_ciint tipc_crypto_rcv(struct net *net, struct tipc_crypto *rx, 1678c2ecf20Sopenharmony_ci struct sk_buff **skb, struct tipc_bearer *b); 1688c2ecf20Sopenharmony_ciint tipc_crypto_key_init(struct tipc_crypto *c, struct tipc_aead_key *ukey, 1698c2ecf20Sopenharmony_ci u8 mode, bool master_key); 1708c2ecf20Sopenharmony_civoid tipc_crypto_key_flush(struct tipc_crypto *c); 1718c2ecf20Sopenharmony_ciint tipc_crypto_key_distr(struct tipc_crypto *tx, u8 key, 1728c2ecf20Sopenharmony_ci struct tipc_node *dest); 1738c2ecf20Sopenharmony_civoid tipc_crypto_msg_rcv(struct net *net, struct sk_buff *skb); 1748c2ecf20Sopenharmony_civoid tipc_crypto_rekeying_sched(struct tipc_crypto *tx, bool changed, 1758c2ecf20Sopenharmony_ci u32 new_intv); 1768c2ecf20Sopenharmony_ciint tipc_aead_key_validate(struct tipc_aead_key *ukey, struct genl_info *info); 1778c2ecf20Sopenharmony_cibool tipc_ehdr_validate(struct sk_buff *skb); 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_cistatic inline u32 msg_key_gen(struct tipc_msg *m) 1808c2ecf20Sopenharmony_ci{ 1818c2ecf20Sopenharmony_ci return msg_bits(m, 4, 16, 0xffff); 1828c2ecf20Sopenharmony_ci} 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_cistatic inline void msg_set_key_gen(struct tipc_msg *m, u32 gen) 1858c2ecf20Sopenharmony_ci{ 1868c2ecf20Sopenharmony_ci msg_set_bits(m, 4, 16, 0xffff, gen); 1878c2ecf20Sopenharmony_ci} 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_cistatic inline u32 msg_key_mode(struct tipc_msg *m) 1908c2ecf20Sopenharmony_ci{ 1918c2ecf20Sopenharmony_ci return msg_bits(m, 4, 0, 0xf); 1928c2ecf20Sopenharmony_ci} 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_cistatic inline void msg_set_key_mode(struct tipc_msg *m, u32 mode) 1958c2ecf20Sopenharmony_ci{ 1968c2ecf20Sopenharmony_ci msg_set_bits(m, 4, 0, 0xf, mode); 1978c2ecf20Sopenharmony_ci} 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci#endif /* _TIPC_CRYPTO_H */ 2008c2ecf20Sopenharmony_ci#endif 201