encrypt a MACsec frame for this Secure Association
(self, orig_pkt, assoclen=None)
| 151 | return prev_layer / next_layer |
| 152 | |
| 153 | def encrypt(self, orig_pkt, assoclen=None): |
| 154 | """encrypt a MACsec frame for this Secure Association""" |
| 155 | hdr = copy.deepcopy(orig_pkt) |
| 156 | del hdr[MACsec].payload |
| 157 | del hdr[MACsec].type |
| 158 | pktlen = len(orig_pkt) |
| 159 | if self.send_sci: |
| 160 | hdrlen = NOSCI_LEN + SCI_LEN |
| 161 | else: |
| 162 | hdrlen = NOSCI_LEN |
| 163 | if assoclen is None or not self.do_encrypt: |
| 164 | if self.do_encrypt: |
| 165 | assoclen = hdrlen |
| 166 | else: |
| 167 | assoclen = pktlen |
| 168 | iv = self.make_iv(orig_pkt) |
| 169 | assoc, pt, _ = MACsecSA.split_pkt(orig_pkt, assoclen) |
| 170 | encryptor = Cipher( |
| 171 | algorithms.AES(self.key), |
| 172 | modes.GCM(iv), |
| 173 | backend=default_backend() |
| 174 | ).encryptor() |
| 175 | encryptor.authenticate_additional_data(assoc) |
| 176 | ct = encryptor.update(pt) + encryptor.finalize() |
| 177 | hdr[MACsec].payload = Raw(assoc[hdrlen:assoclen] + ct + encryptor.tag) |
| 178 | return hdr |
| 179 | |
| 180 | def decrypt(self, orig_pkt, assoclen=None): |
| 181 | """decrypt a MACsec frame for this Secure Association""" |