| 18 | } |
| 19 | |
| 20 | int8_t GCMParser::parse(uint8_t *d, DataParserContext &ctx, bool hastag) { |
| 21 | if(ctx.length < 12) return DATA_PARSE_INCOMPLETE; |
| 22 | |
| 23 | uint32_t headersize = 0; |
| 24 | uint8_t* ptr = (uint8_t*) d; |
| 25 | if(hastag) { |
| 26 | if(*ptr != GCM_TAG) return DATA_PARSE_BOUNDARY_FLAG_MISSING; |
| 27 | ptr++; |
| 28 | headersize++; |
| 29 | } |
| 30 | // Encrypted APDU |
| 31 | // http://www.weigu.lu/tutorials/sensors2bus/04_encryption/index.html |
| 32 | |
| 33 | uint8_t systemTitleLength = *ptr; |
| 34 | ptr++; |
| 35 | headersize++; |
| 36 | |
| 37 | uint8_t initialization_vector[12]; |
| 38 | memset(ctx.system_title, 0, 8); |
| 39 | memset(initialization_vector, 0, 12); |
| 40 | if(systemTitleLength > 0) { |
| 41 | memcpy(ctx.system_title, ptr, systemTitleLength); |
| 42 | memcpy(initialization_vector, ctx.system_title, systemTitleLength); |
| 43 | ptr += systemTitleLength; |
| 44 | headersize += systemTitleLength; |
| 45 | } |
| 46 | |
| 47 | uint32_t len = 0; |
| 48 | if(((*ptr) & 0xFF) == 0x81) { |
| 49 | // 1-byte payload length |
| 50 | ptr++; |
| 51 | len = *ptr++; |
| 52 | headersize += 2; |
| 53 | } else if(((*ptr) & 0xFF) == 0x82) { |
| 54 | // 2-byte payload length |
| 55 | ptr++; |
| 56 | len = *ptr++ << 8; |
| 57 | len |= *ptr++; |
| 58 | headersize += 3; |
| 59 | } else if(((*ptr) & 0xFF) == 0x84) { |
| 60 | // 4-byte payload length |
| 61 | ptr++; |
| 62 | len = *ptr++ << 24; |
| 63 | len |= *ptr++ << 16; |
| 64 | len |= *ptr++ << 8; |
| 65 | len |= *ptr++; |
| 66 | headersize += 5; |
| 67 | } else { |
| 68 | len = *ptr++; |
| 69 | headersize++; |
| 70 | } |
| 71 | if(len + headersize > ctx.length) |
| 72 | return DATA_PARSE_INCOMPLETE; |
| 73 | |
| 74 | uint8_t additional_authenticated_data[17]; |
| 75 | memcpy(additional_authenticated_data, ptr, 1); |
| 76 | |
| 77 | // Security tag |
nothing calls this directly
no outgoing calls
no test coverage detected