CMEncrypt.c.html 11 KB

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