CMEncrypt.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or
  8. * without modification, are permitted (subject to the limitations
  9. * in the disclaimer below) provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. *
  20. * * Neither the name of Qualcomm Atheros nor the names of
  21. * its contributors may be used to endorse or promote products
  22. * derived from this software without specific prior written
  23. * permission.
  24. *
  25. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  26. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  27. * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  28. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  31. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  37. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. *--------------------------------------------------------------------*/
  41. /*====================================================================*
  42. *
  43. * CMEncrypt - Send Encrypted Payload;
  44. *
  45. * this program sends and receives raw ethernet frames and so needs
  46. * root privileges; if you install it using "chmod 555" and "chown
  47. * root:root" then you must login as root to run it; otherwise, you
  48. * can install it using "chmod 4555" and "chown root:root" so that
  49. * anyone can run it; the program will refuse to run until you get
  50. * things right;
  51. *
  52. *
  53. * Contributor(s):
  54. * Charles Maier
  55. *
  56. *--------------------------------------------------------------------*/
  57. /*====================================================================*"
  58. * system header files;
  59. *--------------------------------------------------------------------*/
  60. #include <unistd.h>
  61. #include <stdlib.h>
  62. #include <stdint.h>
  63. #include <time.h>
  64. /*====================================================================*
  65. * custom header files;
  66. *--------------------------------------------------------------------*/
  67. #include "../tools/getoptv.h"
  68. #include "../tools/putoptv.h"
  69. #include "../tools/memory.h"
  70. #include "../tools/number.h"
  71. #include "../tools/symbol.h"
  72. #include "../tools/types.h"
  73. #include "../tools/flags.h"
  74. #include "../tools/files.h"
  75. #include "../tools/error.h"
  76. #include "../key/SHA256.h"
  77. #include "../plc/plc.h"
  78. /*====================================================================*
  79. * custom source files;
  80. *--------------------------------------------------------------------*/
  81. #ifndef MAKEFILE
  82. #include "../tools/getoptv.c"
  83. #include "../tools/putoptv.c"
  84. #include "../tools/version.c"
  85. #include "../tools/uintspec.c"
  86. #include "../tools/hexencode.c"
  87. #include "../tools/hexdecode.c"
  88. #include "../tools/hexdump.c"
  89. #include "../tools/todigit.c"
  90. #include "../tools/error.c"
  91. #include "../tools/synonym.c"
  92. #endif
  93. #ifndef MAKEFILE
  94. #include "../plc/Confirm.c"
  95. #include "../plc/Failure.c"
  96. #include "../plc/Request.c"
  97. #include "../plc/Devices.c"
  98. #endif
  99. #ifndef MAKEFILE
  100. #include "../ether/channel.c"
  101. #include "../ether/openchannel.c"
  102. #include "../ether/closechannel.c"
  103. #include "../ether/readpacket.c"
  104. #include "../ether/sendpacket.c"
  105. #endif
  106. #ifndef MAKEFILE
  107. #include "../key/SHA256Reset.c"
  108. #include "../key/SHA256Write.c"
  109. #include "../key/SHA256Block.c"
  110. #include "../key/SHA256Fetch.c"
  111. #endif
  112. #ifndef MAKEFILE
  113. #include "../mme/EthernetHeader.c"
  114. #include "../mme/HomePlugHeader.c"
  115. #include "../mme/MMECode.c"
  116. #endif
  117. /*====================================================================*
  118. * program constants;
  119. *--------------------------------------------------------------------*/
  120. #define CMENCRYPT_PEKS 0x0F
  121. #define CMENCRYPT_AVLN 0x00
  122. #define CMENCRYPT_PID 0x04
  123. /*====================================================================*
  124. *
  125. * int main (int argc, char const * argv[]);
  126. *
  127. *
  128. *--------------------------------------------------------------------*/
  129. int main (int argc, char const * argv [])
  130. {
  131. extern struct channel channel;
  132. static char const * optv [] =
  133. {
  134. "A:f:i:K:P:qv",
  135. "device [device] [...]",
  136. "Send an encrypted payload using CM_ENCRYPTED_PAYLOAD",
  137. "A n\tAVLN Status [" LITERAL (CMENCRYPT_AVLN) "]",
  138. "K n\tPayload Encryption Key Select (PEKS) [" LITERAL (CMENCRYPT_PEKS) "]",
  139. "P n\tProtocol Identifier (PID) [" LITERAL (CMENCRYPT_PID) "]",
  140. #if defined (WINPCAP) || defined (LIBPCAP)
  141. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  142. #else
  143. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  144. #endif
  145. "f f\tpayload file",
  146. "q\tquiet mode",
  147. "v\tverbose mode",
  148. (char const *) (0)
  149. };
  150. #ifndef __GNUC__
  151. #pragma pack(push,1)
  152. #endif
  153. struct __packed cm_encrypted_payload
  154. {
  155. uint8_t PEKS;
  156. uint8_t AVLN;
  157. uint8_t PID;
  158. uint16_t PRN;
  159. uint8_t PMN;
  160. uint8_t UUID [16];
  161. uint16_t LEN;
  162. }
  163. template =
  164. {
  165. CMENCRYPT_PEKS,
  166. CMENCRYPT_AVLN,
  167. CMENCRYPT_PID,
  168. 0x0000,
  169. 0x00,
  170. {
  171. 0x00,
  172. 0x00,
  173. 0x00,
  174. 0x00,
  175. 0x00,
  176. 0x00,
  177. 0x00,
  178. 0x00,
  179. 0x00,
  180. 0x00,
  181. 0x00,
  182. 0x00,
  183. 0x00,
  184. 0x00,
  185. 0x00,
  186. 0x00
  187. },
  188. 0x0000
  189. };
  190. #ifndef __GNUC__
  191. #pragma pack (pop)
  192. #endif
  193. struct _file_ file =
  194. {
  195. -1,
  196. (char const *) (0)
  197. };
  198. struct sha256 sha256;
  199. uint8_t digest [SHA256_DIGEST_LENGTH];
  200. time_t timer = time ((time_t *)(0));
  201. uint8_t packet [ETHER_MAX_LEN];
  202. uint8_t * buffer;
  203. signed extent;
  204. signed c;
  205. if (getenv (PLCDEVICE))
  206. {
  207. #if defined (WINPCAP) || defined (LIBPCAP)
  208. channel.ifindex = atoi (getenv (PLCDEVICE));
  209. #else
  210. channel.ifname = strdup (getenv (PLCDEVICE));
  211. #endif
  212. }
  213. optind = 1;
  214. while ((c = getoptv (argc, argv, optv)) != -1)
  215. {
  216. switch (c)
  217. {
  218. case 'f':
  219. if ((file.file = open (file.name = optarg, O_BINARY|O_RDONLY)) == -1)
  220. {
  221. error (1, errno, "%s", file.name);
  222. }
  223. break;
  224. case 'P':
  225. template.PID = (byte)(uintspec (optarg, 0x00, 0x0F));
  226. break;
  227. case 'A':
  228. template.AVLN = (byte)(uintspec (optarg, 0x00, 0x08));
  229. break;
  230. case 'K':
  231. template.PEKS = (byte)(uintspec (optarg, 0x00, 0xFF));
  232. break;
  233. case 'i':
  234. #if defined (WINPCAP) || defined (LIBPCAP)
  235. channel.ifindex = atoi (optarg);
  236. #else
  237. channel.ifname = optarg;
  238. #endif
  239. break;
  240. case 'q':
  241. _setbits (channel.flags, CHANNEL_SILENCE);
  242. break;
  243. case 'v':
  244. _setbits (channel.flags, CHANNEL_VERBOSE);
  245. break;
  246. default:
  247. break;
  248. }
  249. }
  250. argc -= optind;
  251. argv += optind;
  252. /*
  253. * load entire file into memory;
  254. */
  255. if (file.file == -1)
  256. {
  257. error (1, ECANCELED, "No payload file given: Use -f <file>");
  258. }
  259. if ((extent = lseek (file.file, 0, SEEK_END)) == -1)
  260. {
  261. error (1, errno, FILE_CANTSIZE, file.name);
  262. }
  263. if (!(buffer = malloc (extent)))
  264. {
  265. error (1, errno, FILE_CANTLOAD, file.name);
  266. }
  267. if (lseek (file.file, 0, SEEK_SET))
  268. {
  269. error (1, errno, FILE_CANTHOME, file.name);
  270. }
  271. if (read (file.file, buffer, extent) != extent)
  272. {
  273. error (1, errno, FILE_CANTREAD, file.name);
  274. }
  275. close (file.file);
  276. SHA256Reset (&sha256);
  277. SHA256Write (&sha256, buffer, extent);
  278. SHA256Fetch (&sha256, digest);
  279. if (!argc)
  280. {
  281. error (1, ECANCELED, "No destination given");
  282. }
  283. openchannel (&channel);
  284. while ((argc) && (* argv))
  285. {
  286. signed offset = 0;
  287. signed remain = extent;
  288. #if 0
  289. signed length = sizeof (struct packet_ms) - sizeof (template);
  290. #else
  291. signed length = 502 - sizeof (template);
  292. #endif
  293. if (!hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  294. {
  295. error (1, errno, PLC_BAD_MAC, * argv);
  296. }
  297. template.PRN = (uint16_t)(timer);
  298. template.PMN = 0;
  299. memcpy (template.UUID, digest, sizeof (template.UUID));
  300. while (remain)
  301. {
  302. uint8_t * memory = packet;
  303. if (length > remain)
  304. {
  305. length = remain;
  306. }
  307. template.PMN++;
  308. template.LEN = HTOLE16 (length);
  309. memset (memory, 0, sizeof (struct message));
  310. memory += EthernetHeader ((struct ethernet_hdr *)(memory), channel.peer, channel.host, channel.type);
  311. memory += HomePlugHeader ((struct homeplug_hdr *)(memory), 0, (CM_ENCRYPTED_PAYLOAD | MMTYPE_IND));
  312. memcpy (memory, &template, sizeof (template));
  313. memory += sizeof (template);
  314. memcpy (memory, buffer + offset, length);
  315. memory += length;
  316. extent = (signed)(memory - packet);
  317. if (extent < (ETHER_MIN_LEN - ETHER_CRC_LEN))
  318. {
  319. extent = (ETHER_MIN_LEN - ETHER_CRC_LEN);
  320. }
  321. if (sendpacket (&channel, packet, extent) < extent)
  322. {
  323. error (1, errno, CHANNEL_CANTSEND);
  324. }
  325. offset += length;
  326. remain -= length;
  327. }
  328. argc--;
  329. argv++;
  330. }
  331. closechannel (&channel);
  332. free (buffer);
  333. exit (0);
  334. }