rkey.c.html 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. rkey.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='reword.c.html' title=' reword.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='RNDPasswords.c.html' title=' RNDPasswords.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. * rkey.c - random key generator;
  64. *
  65. * generate random SHA256, device access, network membership and
  66. * network identifier keys using a seed file;
  67. *
  68. * read the seed file, increment the seed for each key generated
  69. * then save the seed when done; exit the loop in an orderly way
  70. * on keyboard interrupt;
  71. *
  72. *
  73. * Contributor(s);
  74. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  75. *
  76. *--------------------------------------------------------------------*/
  77. #define _GETOPT_H
  78. /*====================================================================*
  79. * system header files;
  80. *--------------------------------------------------------------------*/
  81. #include &lt;unistd.h&gt;
  82. #include &lt;stdlib.h&gt;
  83. #include &lt;limits.h&gt;
  84. #include &lt;signal.h&gt;
  85. #include &lt;errno.h&gt;
  86. /*====================================================================*
  87. * custom header files;
  88. *--------------------------------------------------------------------*/
  89. #include &quot;../tools/getoptv.h&quot;
  90. #include &quot;../tools/putoptv.h&quot;
  91. #include &quot;../tools/memory.h&quot;
  92. #include &quot;../tools/number.h&quot;
  93. #include &quot;../tools/error.h&quot;
  94. #include &quot;../tools/files.h&quot;
  95. #include &quot;../tools/flags.h&quot;
  96. #include &quot;../key/HPAVKey.h&quot;
  97. #include &quot;../key/SHA256.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/todigit.c&quot;
  107. #include &quot;../tools/strincr.c&quot;
  108. #include &quot;../tools/hexout.c&quot;
  109. #include &quot;../tools/error.c&quot;
  110. #endif
  111. #ifndef MAKEFILE
  112. #include &quot;../key/HPAVKeyDAK.c&quot;
  113. #include &quot;../key/HPAVKeyNMK.c&quot;
  114. #include &quot;../key/HPAVKeyNID.c&quot;
  115. #include &quot;../key/HPAVKeySHA.c&quot;
  116. #include &quot;../key/HPAVKeyOut.c&quot;
  117. #include &quot;../key/SHA256Reset.c&quot;
  118. #include &quot;../key/SHA256Write.c&quot;
  119. #include &quot;../key/SHA256Block.c&quot;
  120. #include &quot;../key/SHA256Fetch.c&quot;
  121. #endif
  122. /*====================================================================*
  123. * program constants;
  124. *--------------------------------------------------------------------*/
  125. #define DEFAULT_LEVEL 0
  126. #define DEFAULT_COUNT 1
  127. /*====================================================================*
  128. * program variables;
  129. *--------------------------------------------------------------------*/
  130. static unsigned count = DEFAULT_COUNT;
  131. /*====================================================================*
  132. *
  133. * void stop (signo_t signal);
  134. *
  135. * terminate the program; we want to ensure an organized program
  136. * exit such that the current pass phrase is saved;
  137. *
  138. *
  139. *--------------------------------------------------------------------*/
  140. #if defined (__linux__)
  141. static void stop (signo_t signal)
  142. {
  143. count = 0;
  144. return;
  145. }
  146. #endif
  147. /*====================================================================*
  148. *
  149. * int main (int argc, const char * argv []);
  150. *
  151. *
  152. *
  153. *--------------------------------------------------------------------*/
  154. int main (int argc, const char * argv [])
  155. {
  156. static const char * optv [] =
  157. {
  158. &quot;DL:MNn:oqv&quot;,
  159. &quot;seedfile&quot;,
  160. &quot;generate HomePlug AV compliant keys&quot;,
  161. &quot;D\tDAK - Device Access Keys&quot;,
  162. &quot;L n\tSecurity Level is n [&quot; LITERAL (DEFAULT_LEVEL) &quot;]&quot;,
  163. &quot;M\tNMK - Network Membership Keys&quot;,
  164. &quot;N\tNID - Network Identifier&quot;,
  165. &quot;n n\tgenerate n keys [&quot; LITERAL (DEFAULT_COUNT) &quot;]&quot;,
  166. &quot;o\tuse old seedfile value&quot;,
  167. &quot;q\tquiet mode&quot;,
  168. &quot;v\tverbose mode&quot;,
  169. (const char *)(0)
  170. };
  171. #if defined (__linux__)
  172. struct sigaction sa;
  173. #endif
  174. char phrase [HPAVKEY_PHRASE_MAX + 1];
  175. uint8_t digest [SHA256_DIGEST_LENGTH];
  176. unsigned level = DEFAULT_LEVEL;
  177. signed type = 0;
  178. signed next = 1;
  179. signed fd;
  180. flag_t flags = (flag_t)(0);
  181. signed c;
  182. while ((c = getoptv (argc, argv, optv)) != -1)
  183. {
  184. switch ((char)(c))
  185. {
  186. case 'D':
  187. type = HPAVKEY_DAK;
  188. break;
  189. case 'M':
  190. type = HPAVKEY_NMK;
  191. break;
  192. case 'N':
  193. type = HPAVKEY_NID;
  194. break;
  195. case 'n':
  196. count = (unsigned)(uintspec (optarg, 0, UINT_MAX));
  197. break;
  198. case 'L':
  199. level = (unsigned)(uintspec (optarg, 0, 1));
  200. break;
  201. case 'o':
  202. next = 0;
  203. break;
  204. case 'q':
  205. _setbits (flags, HPAVKEY_SILENCE);
  206. break;
  207. case 'v':
  208. _setbits (flags, HPAVKEY_VERBOSE);
  209. break;
  210. default:
  211. break;
  212. }
  213. }
  214. argc -= optind;
  215. argv += optind;
  216. if (argc != 1)
  217. {
  218. error (1, ECANCELED, &quot;No secret file given&quot;);
  219. }
  220. memset (phrase, 0, sizeof (phrase));
  221. if ((fd = open (* argv, O_BINARY|O_CREAT|O_RDWR, FILE_FILEMODE)) == -1)
  222. {
  223. error (1, errno, &quot;Can't open %s&quot;, * argv);
  224. }
  225. if (read (fd, phrase, sizeof (phrase) - 1) == -1)
  226. {
  227. error (1, errno, &quot;Can't read seedfile&quot;);
  228. }
  229. for (c = 0; c &lt; (signed)(sizeof (phrase) - 1); c++)
  230. {
  231. if (phrase [c] &lt; HPAVKEY_CHAR_MIN)
  232. {
  233. phrase [c] = HPAVKEY_CHAR_MIN;
  234. continue;
  235. }
  236. if (phrase [c] &gt; HPAVKEY_CHAR_MAX)
  237. {
  238. phrase [c] = HPAVKEY_CHAR_MAX;
  239. continue;
  240. }
  241. }
  242. #if defined (__linux__)
  243. memset (&amp;sa, 0, sizeof (struct sigaction));
  244. sa.sa_handler = stop;
  245. sigaction (SIGTERM, &amp;sa, (struct sigaction *)(0));
  246. sigaction (SIGQUIT, &amp;sa, (struct sigaction *)(0));
  247. sigaction (SIGTSTP, &amp;sa, (struct sigaction *)(0));
  248. sigaction (SIGINT, &amp;sa, (struct sigaction *)(0));
  249. sigaction (SIGHUP, &amp;sa, (struct sigaction *)(0));
  250. #endif
  251. while (count-- &gt; 0)
  252. {
  253. memset (digest, 0, sizeof (digest));
  254. if (next &amp;&amp; strincr ((uint8_t *)(phrase), (size_t) (sizeof (phrase) - 1), HPAVKEY_CHAR_MIN, HPAVKEY_CHAR_MAX))
  255. {
  256. error (1, errno, &quot;Can't increment seedfile&quot;);
  257. }
  258. if (type == HPAVKEY_DAK)
  259. {
  260. HPAVKeyDAK (digest, phrase);
  261. HPAVKeyOut (digest, HPAVKEY_DAK_LEN, phrase, flags);
  262. continue;
  263. }
  264. if (type == HPAVKEY_NMK)
  265. {
  266. HPAVKeyNMK (digest, phrase);
  267. HPAVKeyOut (digest, HPAVKEY_NMK_LEN, phrase, flags);
  268. continue;
  269. }
  270. if (type == HPAVKEY_NID)
  271. {
  272. HPAVKeyNMK (digest, phrase);
  273. HPAVKeyNID (digest, digest, level);
  274. HPAVKeyOut (digest, HPAVKEY_NID_LEN, phrase, flags);
  275. continue;
  276. }
  277. HPAVKeySHA (digest, phrase);
  278. HPAVKeyOut (digest, HPAVKEY_SHA_LEN, phrase, flags);
  279. }
  280. if (lseek (fd, 0, SEEK_SET) == -1)
  281. {
  282. error (1, errno, &quot;Can't rewind seedfile&quot;);
  283. }
  284. if (write (fd, phrase, sizeof (phrase) - 1) == -1)
  285. {
  286. error (1, errno, &quot;Can't update seedfile&quot;);
  287. }
  288. close (fd);
  289. return (0);
  290. }
  291. </pre>
  292. <div class='footerlink'>
  293. [<a href='reword.c.html' title=' reword.c '>PREV</a>]
  294. [<a href='toolkit.html' title=' Index '>HOME</a>]
  295. [<a href='RNDPasswords.c.html' title=' RNDPasswords.c '>NEXT</a>]
  296. </div>
  297. </body>
  298. </html>