func DeriveLegacyNwkSKey(appKey types.AES128Key, jn types.JoinNonce, nid types.NetID, dn types.DevNonce) types.AES128Key {
	return deriveLegacySKey(appKey, 0x01, jn, nid, dn)
}
DeriveLegacyNwkSKey derives the LoRaWAN 1.0 Network Session Key. AppNonce is entered as JoinNonce.
- If a LoRaWAN 1.0 device joins a LoRaWAN 1.0/1.1 network, the AppKey is used as "key"
- If a LoRaWAN 1.1 device joins a LoRaWAN 1.0 network, the NwkKey is used as "key"
package crypto
import (
	"crypto/aes"
	"go.thethings.network/lorawan-stack/v3/pkg/types"
)
func deriveSKey(key types.AES128Key, t byte, jn types.JoinNonce, joinEUI types.EUI64, dn types.DevNonce) (derived types.AES128Key) {
	buf := make([]byte, 16)
	buf[0] = t
	copy(buf[1:4], reverse(jn[:]))
	copy(buf[4:12], reverse(joinEUI[:]))
	copy(buf[12:14], reverse(dn[:]))
	block, _ := aes.NewCipher(key[:])
	block.Encrypt(derived[:], buf)
	return
}
func deriveLegacySKey(key types.AES128Key, t byte, jn types.JoinNonce, nid types.NetID, dn types.DevNonce) (derived types.AES128Key) {
	buf := make([]byte, 16)
	buf[0] = t
	copy(buf[1:4], reverse(jn[:]))
	copy(buf[4:7], reverse(nid[:]))
	copy(buf[7:9], reverse(dn[:]))
	block, _ := aes.NewCipher(key[:])
	block.Encrypt(derived[:], buf)
	return
}
func DeriveFNwkSIntKey(nwkKey types.AES128Key, jn types.JoinNonce, joinEUI types.EUI64, dn types.DevNonce) types.AES128Key {
	return deriveSKey(nwkKey, 0x01, jn, joinEUI, dn)
}
func DeriveSNwkSIntKey(nwkKey types.AES128Key, jn types.JoinNonce, joinEUI types.EUI64, dn types.DevNonce) types.AES128Key {
	return deriveSKey(nwkKey, 0x03, jn, joinEUI, dn)
}
func DeriveNwkSEncKey(nwkKey types.AES128Key, jn types.JoinNonce, joinEUI types.EUI64, dn types.DevNonce) types.AES128Key {
	return deriveSKey(nwkKey, 0x04, jn, joinEUI, dn)
}
func DeriveAppSKey(key types.AES128Key, jn types.JoinNonce, joinEUI types.EUI64, dn types.DevNonce) types.AES128Key {
	return deriveSKey(key, 0x02, jn, joinEUI, dn)
}
func DeriveLegacyAppSKey(key types.AES128Key, jn types.JoinNonce, nid types.NetID, dn types.DevNonce) types.AES128Key {
	return deriveLegacySKey(key, 0x02, jn, nid, dn)
}