rkey.c 8.0 KB

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