+ if (rdata->message[TYPE] == 0x02) {
+ if (rdata->message[PAYLOAD] == 0x04) {
+ int32_t req_kNo;
+ uint8_t challenge[6];
+ uint8_t respbuf[16];
+ uint8_t *resp;
+
+ req_kNo = rdata->message[rdata->message[LEN]] / 2;
+ memcpy(challenge, &(rdata->message[PAYLOAD+1]), 6);
+
+ if (req_kNo != kNo) {
+ fprintf(stderr, "AES request for unknown key %d!\n", req_kNo);
+ } else {
+ resp = hm_sign(key, challenge, msg, NULL, respbuf);
+ if (resp) {
+ uint8_t rbuf[64];
+
+ memset(rbuf, 0, sizeof(rbuf));
+ rbuf[MSGID] = rdata->message[MSGID];
+ rbuf[CTL] = rdata->message[CTL];
+ rbuf[TYPE] = 0x03;
+ SET_SRC(rbuf, DST(rdata->message));
+ SET_DST(rbuf, SRC(rdata->message));
+ memcpy(&(rbuf[PAYLOAD]), resp, 16);
+ SET_LEN_FROM_PAYLOADLEN(rbuf, 16);
+
+ usleep(110000); /* Determined by a fair dice roll */
+ return send_hm_message(dev, rdata, rbuf);
+ }
+ }
+ } else if (rdata->message[PAYLOAD] >= 0x80 && rdata->message[PAYLOAD] <= 0x8f) {
+ fprintf(stderr, "NACK\n");
+ } else { /* ACK or ACKinfo */
+ break;
+ }
+ } else {
+ fprintf(stderr, "Unexpected message received: ");
+ for (i = 0; i < rdata->message[LEN]; i++) {
+ fprintf(stderr, "%02x", rdata->message[i+1]);
+ }
+ fprintf(stderr, "\n");
+ }