rkey.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * rkey.c - random key generator;
  11. *
  12. * generate random SHA256, device access, network membership and
  13. * network identifier keys using a seed file;
  14. *
  15. * read the seed file, increment the seed for each key generated
  16. * then save the seed when done; exit the loop in an orderly way
  17. * on keyboard interrupt;
  18. *
  19. * Contributor(s);
  20. * Charles Maier <cmaier@qca.qualcomm.com>
  21. *
  22. *--------------------------------------------------------------------*/
  23. /*====================================================================*
  24. * system header files;
  25. *--------------------------------------------------------------------*/
  26. #include <unistd.h>
  27. #include <stdlib.h>
  28. #include <limits.h>
  29. #include <signal.h>
  30. #include <errno.h>
  31. /*====================================================================*
  32. * custom header files;
  33. *--------------------------------------------------------------------*/
  34. #include "../tools/getoptv.h"
  35. #include "../tools/putoptv.h"
  36. #include "../tools/memory.h"
  37. #include "../tools/number.h"
  38. #include "../tools/error.h"
  39. #include "../tools/files.h"
  40. #include "../tools/flags.h"
  41. #include "../key/HPAVKey.h"
  42. #include "../key/SHA256.h"
  43. /*====================================================================*
  44. * custom source files;
  45. *--------------------------------------------------------------------*/
  46. #ifndef MAKEFILE
  47. #include "../tools/getoptv.c"
  48. #include "../tools/putoptv.c"
  49. #include "../tools/version.c"
  50. #include "../tools/uintspec.c"
  51. #include "../tools/todigit.c"
  52. #include "../tools/strincr.c"
  53. #include "../tools/hexout.c"
  54. #include "../tools/error.c"
  55. #endif
  56. #ifndef MAKEFILE
  57. #include "../key/HPAVKeyDAK.c"
  58. #include "../key/HPAVKeyNMK.c"
  59. #include "../key/HPAVKeyNID.c"
  60. #include "../key/HPAVKeySHA.c"
  61. #include "../key/HPAVKeyOut.c"
  62. #include "../key/SHA256Reset.c"
  63. #include "../key/SHA256Write.c"
  64. #include "../key/SHA256Block.c"
  65. #include "../key/SHA256Fetch.c"
  66. #endif
  67. /*====================================================================*
  68. * program constants;
  69. *--------------------------------------------------------------------*/
  70. #define DEFAULT_LEVEL 0
  71. #define DEFAULT_COUNT 1
  72. /*====================================================================*
  73. * program variables;
  74. *--------------------------------------------------------------------*/
  75. static unsigned count = DEFAULT_COUNT;
  76. /*====================================================================*
  77. *
  78. * void stop (signo_t signal);
  79. *
  80. * terminate the program; we want to ensure an organized program
  81. * exit such that the current pass phrase is saved;
  82. *
  83. *
  84. *--------------------------------------------------------------------*/
  85. #if defined (__linux__)
  86. static void stop (signo_t signal)
  87. {
  88. count = 0;
  89. return;
  90. }
  91. #endif
  92. /*====================================================================*
  93. *
  94. * int main (int argc, const char * argv []);
  95. *
  96. *--------------------------------------------------------------------*/
  97. int main (int argc, const char * argv [])
  98. {
  99. static const char * optv [] =
  100. {
  101. "DL:MNn:oqv",
  102. "seedfile",
  103. "generate HomePlug AV compliant keys",
  104. "D\tDAK - Device Access Keys",
  105. "L n\tSecurity Level is n [" LITERAL (DEFAULT_LEVEL) "]",
  106. "M\tNMK - Network Membership Keys",
  107. "N\tNID - Network Identifier",
  108. "n n\tgenerate n keys [" LITERAL (DEFAULT_COUNT) "]",
  109. "o\tuse old seedfile value",
  110. "q\tquiet mode",
  111. "v\tverbose mode",
  112. (const char *) (0)
  113. };
  114. #if defined (__linux__)
  115. struct sigaction sa;
  116. #endif
  117. char phrase [HPAVKEY_PHRASE_MAX + 1];
  118. uint8_t digest [SHA256_DIGEST_LENGTH];
  119. unsigned level = DEFAULT_LEVEL;
  120. signed type = 0;
  121. signed next = 1;
  122. signed fd;
  123. flag_t flags = (flag_t) (0);
  124. signed c;
  125. while (~ (c = getoptv (argc, argv, optv)))
  126. {
  127. switch ((char) (c))
  128. {
  129. case 'D':
  130. type = HPAVKEY_DAK;
  131. break;
  132. case 'M':
  133. type = HPAVKEY_NMK;
  134. break;
  135. case 'N':
  136. type = HPAVKEY_NID;
  137. break;
  138. case 'n':
  139. count = (unsigned) (uintspec (optarg, 0, UINT_MAX));
  140. break;
  141. case 'L':
  142. level = (unsigned) (uintspec (optarg, 0, 1));
  143. break;
  144. case 'o':
  145. next = 0;
  146. break;
  147. case 'q':
  148. _setbits (flags, HPAVKEY_SILENCE);
  149. break;
  150. case 'v':
  151. _setbits (flags, HPAVKEY_VERBOSE);
  152. break;
  153. default:
  154. break;
  155. }
  156. }
  157. argc -= optind;
  158. argv += optind;
  159. if (argc != 1)
  160. {
  161. error (1, ECANCELED, "No secret file given");
  162. }
  163. memset (phrase, 0, sizeof (phrase));
  164. if ((fd = open (* argv, O_BINARY | O_CREAT | O_RDWR, FILE_FILEMODE)) == -1)
  165. {
  166. error (1, errno, "Can't open %s", * argv);
  167. }
  168. if (read (fd, phrase, sizeof (phrase) -1) == -1)
  169. {
  170. error (1, errno, "Can't read seedfile");
  171. }
  172. for (c = 0; c < (signed) (sizeof (phrase) -1); c++)
  173. {
  174. if (phrase [c] < HPAVKEY_CHAR_MIN)
  175. {
  176. phrase [c] = HPAVKEY_CHAR_MIN;
  177. continue;
  178. }
  179. if (phrase [c] > HPAVKEY_CHAR_MAX)
  180. {
  181. phrase [c] = HPAVKEY_CHAR_MAX;
  182. continue;
  183. }
  184. }
  185. #if defined (__linux__)
  186. memset (& sa, 0, sizeof (struct sigaction));
  187. sa.sa_handler = stop;
  188. sigaction (SIGTERM, & sa, (struct sigaction *) (0));
  189. sigaction (SIGQUIT, & sa, (struct sigaction *) (0));
  190. sigaction (SIGTSTP, & sa, (struct sigaction *) (0));
  191. sigaction (SIGINT, & sa, (struct sigaction *) (0));
  192. sigaction (SIGHUP, & sa, (struct sigaction *) (0));
  193. #endif
  194. while (count-- > 0)
  195. {
  196. memset (digest, 0, sizeof (digest));
  197. if (next && strincr ((uint8_t *) (phrase), (size_t) (sizeof (phrase) -1), HPAVKEY_CHAR_MIN, HPAVKEY_CHAR_MAX))
  198. {
  199. error (1, errno, "Can't increment seedfile");
  200. }
  201. if (type == HPAVKEY_DAK)
  202. {
  203. HPAVKeyDAK (digest, phrase);
  204. HPAVKeyOut (digest, HPAVKEY_DAK_LEN, phrase, flags);
  205. continue;
  206. }
  207. if (type == HPAVKEY_NMK)
  208. {
  209. HPAVKeyNMK (digest, phrase);
  210. HPAVKeyOut (digest, HPAVKEY_NMK_LEN, phrase, flags);
  211. continue;
  212. }
  213. if (type == HPAVKEY_NID)
  214. {
  215. HPAVKeyNMK (digest, phrase);
  216. HPAVKeyNID (digest, digest, level);
  217. HPAVKeyOut (digest, HPAVKEY_NID_LEN, phrase, flags);
  218. continue;
  219. }
  220. HPAVKeySHA (digest, phrase);
  221. HPAVKeyOut (digest, HPAVKEY_SHA_LEN, phrase, flags);
  222. }
  223. if (lseek (fd, 0, SEEK_SET) == -1)
  224. {
  225. error (1, errno, "Can't rewind seedfile");
  226. }
  227. if (write (fd, phrase, sizeof (phrase) -1) == -1)
  228. {
  229. error (1, errno, "Can't update seedfile");
  230. }
  231. close (fd);
  232. return (0);
  233. }