]>
cvs.zerfleddert.de Git - hmcfgusb/blob - hm.c
818081fcc4cc0f64a4fd7dc51098bfaf342108fa
1 /* HomeMatic protocol-functions
3 * Copyright (c) 2014-15 Michael Gernoth <michael@gernoth.net>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
31 #include <sys/types.h>
40 uint8_t* hm_sign(uint8_t *key
, uint8_t *challenge
, uint8_t *m_frame
, uint8_t *exp_auth
, uint8_t *resp
)
47 printf("AES-request with challenge: %02x%02x%02x%02x%02x%02x\n",
48 challenge
[0], challenge
[1], challenge
[2],
49 challenge
[3], challenge
[4], challenge
[5]);
52 * Build signing key by XORing the first 6 bytes of
53 * the key with the challenge.
55 memcpy(signkey
, key
, sizeof(signkey
));
56 for (i
= 0; i
< 6; i
++) {
57 signkey
[i
] ^= challenge
[i
];
59 aes_key_setup(signkey
, ks
, 128);
62 * Generate payload for first encryption.
64 gettimeofday(&tv
, NULL
);
65 resp
[0] = tv
.tv_sec
>> 24 & 0xff;
66 resp
[1] = tv
.tv_sec
>> 16 & 0xff;
67 resp
[2] = tv
.tv_sec
>> 8 & 0xff;
68 resp
[3] = tv
.tv_sec
& 0xff;
69 resp
[4] = tv
.tv_usec
>> 8 & 0xff;
70 resp
[5] = tv
.tv_usec
& 0xff;
71 memcpy(&(resp
[6]), &(m_frame
[MSGID
]), 10);
74 hexdump(resp
, 16, "P > ");
76 aes_encrypt(resp
, resp
, ks
, 128);
79 hexdump(resp
, 16, "Pe > ");
82 * Device has to answer with the first 4 bytes of the
83 * encrypted payload to authenticate, so pass them to
88 memcpy(exp_auth
, resp
, 4);
92 * XOR parameters of the m_frame to the payload.
94 for (i
= 0; i
< PAYLOADLEN(m_frame
) - 1; i
++) {
99 resp
[i
] ^= m_frame
[PAYLOAD
+ 1+ i
];
103 hexdump(resp
, 16, "Pe^ > ");
106 * Encrypt payload again
108 aes_encrypt(resp
, resp
, ks
, 128);
111 hexdump(resp
, 16, "Pe^e> ");