developer | 02e6591 | 2023-08-17 16:33:10 +0800 | [diff] [blame^] | 1 | /* sa_builder_extended_macsec.c |
| 2 | * |
| 3 | * MACsec specific functions (for initialization of SABuilder_Params_t |
| 4 | * structures and for building the MACsec specific part of an SA) in the |
| 5 | * Extended use case. |
| 6 | */ |
| 7 | |
| 8 | /***************************************************************************** |
| 9 | * Copyright (c) 2013-2020 by Rambus, Inc. and/or its subsidiaries. |
| 10 | * |
| 11 | * This program is free software: you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation, either version 2 of the License, or |
| 14 | * any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | *****************************************************************************/ |
| 24 | |
| 25 | /*---------------------------------------------------------------------------- |
| 26 | * This module implements (provides) the following interface(s): |
| 27 | */ |
| 28 | #include "c_sa_builder.h" |
| 29 | #ifdef SAB_ENABLE_MACSEC_EXTENDED |
| 30 | #include "sa_builder_extended_internal.h" |
| 31 | |
| 32 | /*---------------------------------------------------------------------------- |
| 33 | * This module uses (requires) the following interface(s): |
| 34 | */ |
| 35 | #include "basic_defs.h" |
| 36 | #include "log.h" |
| 37 | #include "sa_builder_internal.h" /* SABuilder_SetMACsecParams */ |
| 38 | #include "sa_builder_macsec.h" |
| 39 | |
| 40 | /*---------------------------------------------------------------------------- |
| 41 | * Definitions and macros |
| 42 | */ |
| 43 | #define SAB_MACSEC_ETHER_TYPE 0x88e5 |
| 44 | /* Various bits in the TCI byte */ |
| 45 | #define SAB_MACSEC_TCI_ES BIT_6 |
| 46 | #define SAB_MACSEC_TCI_SC BIT_5 |
| 47 | #define SAB_MACSEC_TCI_SCB BIT_4 |
| 48 | #define SAB_MACSEC_TCI_E BIT_3 |
| 49 | #define SAB_MACSEC_TCI_C BIT_2 |
| 50 | |
| 51 | /*---------------------------------------------------------------------------- |
| 52 | * Local variables |
| 53 | */ |
| 54 | |
| 55 | |
| 56 | /*---------------------------------------------------------------------------- |
| 57 | * SABuilder_SetExtendedMACsecParams |
| 58 | * |
| 59 | * Fill in MACsec-specific extensions into the SA.for Extended. |
| 60 | * |
| 61 | * SAParams_p (input) |
| 62 | * The SA parameters structure from which the SA is derived. |
| 63 | * SAState_p (input, output) |
| 64 | * Variables containing information about the SA being generated/ |
| 65 | * SABuffer_p (input, output). |
| 66 | * The buffer in which the SA is built. If NULL, no SA will be built, but |
| 67 | * state variables in SAState_p will still be updated. |
| 68 | * |
| 69 | * Return: |
| 70 | * SAB_STATUS_OK on success |
| 71 | * SAB_INVALID_PARAMETER when SAParams_p is invalid, or if any of |
| 72 | * the buffer arguments is a null pointer while the corresponding buffer |
| 73 | * would be required for the operation. |
| 74 | * SAB_UNSUPPORTED_FEATURE when SAParams_p describes an operations that |
| 75 | * is not supported on the hardware for which this SA builder |
| 76 | * is configured. |
| 77 | */ |
| 78 | SABuilder_Status_t |
| 79 | SABuilder_SetExtendedMACsecParams(SABuilder_Params_t *const SAParams_p, |
| 80 | SABuilder_State_t * const SAState_p, |
| 81 | uint32_t * const SABuffer_p) |
| 82 | { |
| 83 | SABuilder_Params_MACsec_t *SAParamsMACsec_p = |
| 84 | (SABuilder_Params_MACsec_t *)(SAParams_p->ProtocolExtension_p); |
| 85 | uint32_t TokenHeaderWord = SAB_HEADER_DEFAULT; |
| 86 | SABuilder_ESPProtocol_t ESPProto; |
| 87 | SABuilder_HeaderProtocol_t HeaderProto; |
| 88 | uint8_t IVByteCount; |
| 89 | uint8_t ICVByteCount; |
| 90 | uint8_t SeqOffset; |
| 91 | uint8_t TCI; /* TCI byte in SECtag */ |
| 92 | uint32_t flags = 0; |
| 93 | uint32_t VerifyInstructionWord, CtxInstructionWord; |
| 94 | |
| 95 | IDENTIFIER_NOT_USED(SAState_p); |
| 96 | |
| 97 | if (SAParamsMACsec_p == NULL) |
| 98 | { |
| 99 | LOG_CRIT("SABuilder: MACsec extension pointer is null\n"); |
| 100 | return SAB_INVALID_PARAMETER; |
| 101 | } |
| 102 | |
| 103 | SeqOffset = SAParams_p->OffsetSeqNum; |
| 104 | ICVByteCount = 16; |
| 105 | TCI = SAParamsMACsec_p->AN; |
| 106 | if ((SAParamsMACsec_p->MACsecFlags & SAB_MACSEC_ES) != 0) |
| 107 | { |
| 108 | TCI |= SAB_MACSEC_TCI_ES; |
| 109 | } |
| 110 | if ((SAParamsMACsec_p->MACsecFlags & SAB_MACSEC_SC) != 0) |
| 111 | { |
| 112 | IVByteCount = 8; |
| 113 | TCI |= SAB_MACSEC_TCI_SC; |
| 114 | } |
| 115 | else |
| 116 | { |
| 117 | IVByteCount = 0; |
| 118 | } |
| 119 | if ((SAParamsMACsec_p->MACsecFlags & SAB_MACSEC_SCB) != 0) |
| 120 | { |
| 121 | TCI |= SAB_MACSEC_TCI_SCB; |
| 122 | } |
| 123 | |
| 124 | if (SAParams_p->AuthAlgo == SAB_AUTH_AES_GCM) |
| 125 | TCI |= SAB_MACSEC_TCI_E | SAB_MACSEC_TCI_C; |
| 126 | |
| 127 | if (SAParams_p->direction == SAB_DIRECTION_OUTBOUND) |
| 128 | { |
| 129 | HeaderProto = SAB_HDR_MACSEC_OUT; |
| 130 | if (SAParams_p->AuthAlgo == SAB_AUTH_AES_GCM) |
| 131 | ESPProto = SAB_MACSEC_PROTO_OUT_GCM; |
| 132 | else |
| 133 | ESPProto = SAB_MACSEC_PROTO_OUT_GMAC; |
| 134 | VerifyInstructionWord = SAB_VERIFY_NONE; |
| 135 | CtxInstructionWord = SAB_CTX_OUT_SEQNUM + |
| 136 | ((unsigned int)(1<<24)) + SeqOffset; |
| 137 | } |
| 138 | else |
| 139 | { |
| 140 | HeaderProto = SAB_HDR_MACSEC_IN; |
| 141 | if (SAParams_p->AuthAlgo == SAB_AUTH_AES_GCM) |
| 142 | ESPProto = SAB_MACSEC_PROTO_IN_GCM; |
| 143 | else |
| 144 | ESPProto = SAB_MACSEC_PROTO_IN_GMAC; |
| 145 | VerifyInstructionWord = SAB_VERIFY_NONE + SAB_VERIFY_BIT_H + |
| 146 | SAB_VERIFY_BIT_SEQ + ICVByteCount; |
| 147 | CtxInstructionWord = SAB_CTX_SEQNUM + |
| 148 | ((unsigned int)(1<<24)) + SeqOffset; |
| 149 | } |
| 150 | |
| 151 | /* Write all parameters to their respective offsets */ |
| 152 | if (SABuffer_p != NULL) |
| 153 | { |
| 154 | /* Do not support large transform records as Macsec will never |
| 155 | use HMAC-SHA512 */ |
| 156 | SABuffer_p[FIRMWARE_EIP207_CS_FLOW_TR_FLAGS_WORD_OFFSET] = flags; |
| 157 | SABuffer_p[FIRMWARE_EIP207_CS_FLOW_TR_HDRPROC_CTX_WORD_OFFSET] = |
| 158 | SAParamsMACsec_p->ContextRef; |
| 159 | SABuffer_p[FIRMWARE_EIP207_CS_FLOW_TR_BYTE_PARAM_WORD_OFFSET] = |
| 160 | SAB_PACKBYTES(IVByteCount,ICVByteCount,HeaderProto,ESPProto); |
| 161 | SABuffer_p[FIRMWARE_EIP207_CS_FLOW_TR_TK_HDR_WORD_OFFSET] = TokenHeaderWord; |
| 162 | SABuffer_p[FIRMWARE_EIP207_CS_FLOW_TR_PAD_ALIGN_WORD_OFFSET] = |
| 163 | SAB_PACKBYTES(SAB_MACSEC_ETHER_TYPE>>8, |
| 164 | SAB_MACSEC_ETHER_TYPE &0xff, TCI, 0); |
| 165 | SABuffer_p[FIRMWARE_EIP207_CS_FLOW_TR_CCM_SALT_WORD_OFFSET] = |
| 166 | SAParamsMACsec_p->ConfOffset; |
| 167 | SABuffer_p[FIRMWARE_EIP207_CS_FLOW_TR_TK_VFY_INST_WORD_OFFSET] = |
| 168 | VerifyInstructionWord; |
| 169 | SABuffer_p[FIRMWARE_EIP207_CS_FLOW_TR_TK_CTX_INST_WORD_OFFSET] = |
| 170 | CtxInstructionWord; |
| 171 | SABuffer_p[FIRMWARE_EIP207_CS_FLOW_TR_TIME_STAMP_LO_WORD_OFFSET] = 0; |
| 172 | SABuffer_p[FIRMWARE_EIP207_CS_FLOW_TR_TIME_STAMP_HI_WORD_OFFSET] = 0; |
| 173 | SABuffer_p[FIRMWARE_EIP207_CS_FLOW_TR_STAT_OCT_LO_WORD_OFFSET] = 0; |
| 174 | SABuffer_p[FIRMWARE_EIP207_CS_FLOW_TR_STAT_OCT_HI_WORD_OFFSET] = 0; |
| 175 | SABuffer_p[FIRMWARE_EIP207_CS_FLOW_TR_STAT_PKT_WORD_OFFSET] = 0; |
| 176 | |
| 177 | SABuilderLib_CopyKeyMat(SABuffer_p, |
| 178 | FIRMWARE_EIP207_CS_FLOW_TR_TUNNEL_SRC_WORD_OFFSET, |
| 179 | SAParamsMACsec_p->SCI_p, 8); |
| 180 | } |
| 181 | return SAB_STATUS_OK; |
| 182 | } |
| 183 | |
| 184 | #endif /* SAB_ENABLE_MACSEC_EXTENDED */ |
| 185 | |
| 186 | |
| 187 | /* end of file sa_builder_extended_dtls.c */ |