MCPcopy Create free account
hub / github.com/chirpstack/chirpstack / decrypt_join_accept_payload

Method decrypt_join_accept_payload

lrwn/src/phy_payload.rs:761–800  ·  view source on GitHub ↗
(&mut self, key: &AES128Key)

Source from the content-addressed store, hash-verified

759 /// response, use the js_enc_key.
760 #[cfg(feature = "crypto")]
761 pub fn decrypt_join_accept_payload(&mut self, key: &AES128Key) -> Result<()> {
762 use aes::cipher::KeyInit;
763
764 if self.mic.is_none() {
765 return Err(anyhow!("mic must be set first"));
766 }
767
768 if let Payload::Raw(pl) = &self.payload {
769 // append MIC since it is encrypted too
770 let mut ct = pl.clone();
771 ct.extend_from_slice(&self.mic.unwrap());
772
773 if ct.len() % 16 != 0 {
774 return Err(anyhow!("ciphertext must be a multiple of 16 bytes"));
775 }
776
777 let key_bytes = key.to_bytes();
778 let key = Array::from(key_bytes);
779 let cipher = Aes128::new(&key);
780
781 let mut pt = Vec::new();
782
783 for i in 0..(ct.len() / 16) {
784 let index = i * 16;
785
786 let mut block = Block::try_from(&ct[index..index + 16])?;
787 cipher.encrypt_block(&mut block);
788 pt.extend_from_slice(block.as_slice());
789 }
790
791 let mut mic: [u8; 4] = [0; 4];
792 mic.clone_from_slice(&pt[pt.len() - 4..]);
793 self.mic = Some(mic);
794 self.payload = Payload::JoinAccept(JoinAcceptPayload::from_slice(&pt[..pt.len() - 4])?);
795
796 return Ok(());
797 }
798
799 Err(anyhow!("payload must be of type Raw"))
800 }
801
802 /// Encrypt the f_opts with the given key.
803 #[cfg(feature = "crypto")]

Callers

nothing calls this directly

Calls 3

JoinAcceptClass · 0.85
unwrapMethod · 0.80
to_bytesMethod · 0.45

Tested by

no test coverage detected