Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MIC calculation for MSG down #11

Closed
tothero opened this issue Oct 31, 2017 · 2 comments
Closed

MIC calculation for MSG down #11

tothero opened this issue Oct 31, 2017 · 2 comments

Comments

@tothero
Copy link

tothero commented Oct 31, 2017

Hi,
the function "lw_mtype_msg_up" is used for parsing up & down messages. a little bit confusing but ok. But while calculating the MIC you use in BOTH cases the fcnt32 of up messages.

fcnt16 = ((uint32_t)msg[LW_DATA_OFF_FCNT+1]<<8) + msg[LW_DATA_OFF_FCNT];
fcntlsb = (uint16_t)cur->ufcnt;
fcntmsb = (uint16_t)(cur->ufcnt>>16);
if(fcnt16<fcntlsb){
    fcntmsb++;
}
lw_key.fcnt32 = ((uint32_t)fcntmsb<<16) + fcnt16;

lw_msg_mic(&mic, &lw_key);
if(mic.data != plmic.data){
    if(lw_key.fcnt32 == fcnt16){
        return LW_ERR_MIC;
    }
    lw_key.fcnt32 = fcnt16;
    lw_msg_mic(&mic, &lw_key);
    if(mic.data != plmic.data){
        return LW_ERR_MIC;
    }
}

Is this your intention or a bug?
I think the current implementation will give an error for framecounts > 16 bit. For down messages you should select the global 32bit down fcnt,

@JiapengLi
Copy link
Owner

Hi @tothero
Thank you for your feedback. Agree with you parse downlink message through lw_mtype_msg_up is messy, the idea is to reuse APIs, because down/up messages are very similar. I'll see if there is clean way to do it, maybe rename the api to a general one like lw_mtype_msg and call it in lw_mtype_msg_up and lw_mtype_msg_down functions.

I didn't do test again. The code is intended to designed like that. Design it in that way the parser can compatible with counter roll back (16 bits only, 32bits counter roll back not supported).

fcntmsb = (uint16_t)(cur->ufcnt>>16);
if(fcnt16<fcntlsb){
    fcntmsb++;
}

This piece of code is for counter overflow check. So it should support 32bits counter.

@tothero
Copy link
Author

tothero commented Nov 1, 2017

Thanks for your response. I will see if a can make a PR for you after doing some testing at this point. For LoRaWAN 1.1 there will be only 32 Bit Framecounter no 16 Bit are allowed anymore.

Btw i made very simple lib just for parsing based on parts of your code. check it out here:
https://github.com/Lobaro/util-lorawan-packets

Maybe or not it could make sense to integrate this into your tool to add a layer of separation between parsing and other stuff your tool does.

Greetings
Theo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants