Ejemplo n.º 1
0
        /// <summary>计算 LoRaMAC 的 MIC</summary>
        /// <param name="buffer">数据缓冲区</param>
        /// <param name="key">AES密钥</param>
        /// <param name="address">帧地址</param>
        /// <param name="dir">方向,0上行,1下行</param>
        /// <param name="sequenceCounter">帧序号计数器</param>
        public UInt32 ComputeMic(Byte[] buffer, Byte[] key, UInt32 address, Boolean dir, UInt32 sequenceCounter)
        {
            var size = buffer.Length;

            var block = new Byte[] { 0x49, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

            block[5] = (Byte)(dir ? 1 : 0);

            block[6] = (Byte)((address) & 0xFF);
            block[7] = (Byte)((address >> 8) & 0xFF);
            block[8] = (Byte)((address >> 16) & 0xFF);
            block[9] = (Byte)((address >> 24) & 0xFF);

            block[10] = (Byte)((sequenceCounter) & 0xFF);
            block[11] = (Byte)((sequenceCounter >> 8) & 0xFF);
            block[12] = (Byte)((sequenceCounter >> 16) & 0xFF);
            block[13] = (Byte)((sequenceCounter >> 24) & 0xFF);

            block[15] = (Byte)(size & 0xFF);

            var ctx = new AES_CMAC_CTX();

            ctx.Init();
            ctx.SetKey(key);
            ctx.Update(block);
            ctx.Update(buffer);

            return(ctx.Final());
        }
Ejemplo n.º 2
0
        /// <summary>计算组网校验码</summary>
        /// <param name="buffer"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public UInt32 JoinComputeMic(Byte[] buffer, Byte[] key)
        {
            var ctx = new AES_CMAC_CTX();

            ctx.Init();
            ctx.SetKey(key);
            ctx.Update(buffer);

            return(ctx.Final());
        }