private byte[] GenerateDerivationData(SessionKeyType sessionKeyType) { var data = new List <byte> { 0x01, (byte)sessionKeyType }; data.AddRange(this.SequenceCounter); data.AddRange(Enumerable.Repeat <byte>(0x00, 12)); return(data.ToArray()); }
// don't work with CFLIST atm private static byte[] CalculateKey(SessionKeyType type, AppNonce appNonce, NetId netId, DevNonce devNonce, AppKey appKey) { using var aes = Aes.Create("AesManaged"); var rawAppKey = new byte[AppKey.Size]; _ = appKey.Write(rawAppKey); aes.Key = rawAppKey; #pragma warning disable CA5358 // Review cipher mode usage with cryptography experts // Cipher is part of the LoRaWAN specification aes.Mode = CipherMode.ECB; #pragma warning restore CA5358 // Review cipher mode usage with cryptography experts aes.Padding = PaddingMode.None; var buffer = new byte[1 + AppNonce.Size + NetId.Size + DevNonce.Size + 7]; var pt = buffer.AsSpan(); Debug.Assert(pt.Length == 16); pt[0] = unchecked ((byte)type); pt = pt[1..];
private byte[] GenerateSessionKey(SessionKeyType sessionKeyType, byte[] staticKey) => TripleDES.Encrypt(this.GenerateDerivationData(sessionKeyType), staticKey);