hpavkeys.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * hpavkeys.c -
  11. *
  12. * Contributor(s):
  13. * Charles Maier <cmaier@qca.qualcomm.com>
  14. *
  15. *--------------------------------------------------------------------*/
  16. /*====================================================================*
  17. * system header files;
  18. *--------------------------------------------------------------------*/
  19. #include <stdio.h>
  20. #include <ctype.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. /*====================================================================*
  24. * custom header files;
  25. *--------------------------------------------------------------------*/
  26. #include "../tools/getoptv.h"
  27. #include "../tools/putoptv.h"
  28. #include "../tools/version.h"
  29. #include "../tools/number.h"
  30. #include "../tools/types.h"
  31. #include "../tools/flags.h"
  32. #include "../tools/error.h"
  33. #include "../key/HPAVKey.h"
  34. #include "../key/SHA256.h"
  35. /*====================================================================*
  36. * custom source files;
  37. *--------------------------------------------------------------------*/
  38. #ifndef MAKEFILE
  39. #include "../tools/getoptv.c"
  40. #include "../tools/putoptv.c"
  41. #include "../tools/version.c"
  42. #include "../tools/uintspec.c"
  43. #include "../tools/todigit.c"
  44. #include "../tools/hexout.c"
  45. #include "../tools/error.c"
  46. #endif
  47. #ifndef MAKEFILE
  48. #include "../key/HPAVKeyDAK.c"
  49. #include "../key/HPAVKeyNMK.c"
  50. #include "../key/HPAVKeyNID.c"
  51. #include "../key/HPAVKeySHA.c"
  52. #include "../key/HPAVKeyOut.c"
  53. #include "../key/SHA256.c"
  54. #endif
  55. /*====================================================================*
  56. *
  57. * void generate (signed type, signed level, flag_t flags);
  58. *
  59. * read pass phrases from stdin, compute the digest for each and
  60. * print both on stdout; ignore illegal pass phrases;
  61. *
  62. * a pass phrase consists of consecutive ASCII characters in the
  63. * range 0x20 through 0x7F; other characters are noise and serve
  64. * to delimit the phrase; phrases less than HPAVKEY_PHRASE_MIN characters
  65. * or more than HPAVKEY_PHRASE_MAX characters are also illegal;
  66. *
  67. * effectively, each text line is a candidate phrase where spaces
  68. * are legal and significant; tabs characters are illegal and act
  69. * as line breaks;
  70. *
  71. * detected errors are reported along with the input line number;
  72. *
  73. *
  74. *--------------------------------------------------------------------*/
  75. void generate (signed class, signed level, flag_t flags)
  76. {
  77. uint8_t digest [SHA256_DIGEST_LENGTH];
  78. char phrase [BUFSIZ];
  79. char * sp = phrase;
  80. unsigned line = 1;
  81. signed c = getc (stdin);
  82. while (c != EOF)
  83. {
  84. if (! isprint (c))
  85. {
  86. if (c == '\n')
  87. {
  88. line++;
  89. }
  90. c = getc (stdin);
  91. continue;
  92. }
  93. sp = phrase;
  94. while (isprint (c))
  95. {
  96. if ((sp - phrase) < (signed) (sizeof (phrase) -1))
  97. {
  98. * sp++ = c;
  99. }
  100. c = getc (stdin);
  101. }
  102. if ((c != '\r') && (c != '\n') && (c != EOF))
  103. {
  104. error (0, ENOTSUP, "Illegal characters on line %d", line);
  105. continue;
  106. }
  107. * sp = (char) (0);
  108. if (_anyset (flags, HPAVKEY_ENFORCE))
  109. {
  110. if ((sp - phrase) < HPAVKEY_PHRASE_MIN)
  111. {
  112. error (0, ENOTSUP, "Less than %d characters on line %d", HPAVKEY_PHRASE_MIN, line);
  113. continue;
  114. }
  115. if ((sp - phrase) > HPAVKEY_PHRASE_MAX)
  116. {
  117. error (0, ENOTSUP, "More than %d characters on line %d", HPAVKEY_PHRASE_MAX, line);
  118. continue;
  119. }
  120. }
  121. if (class == HPAVKEY_DAK)
  122. {
  123. HPAVKeyDAK (digest, phrase);
  124. HPAVKeyOut (digest, HPAVKEY_DAK_LEN, phrase, flags);
  125. continue;
  126. }
  127. if (class == HPAVKEY_NMK)
  128. {
  129. HPAVKeyNMK (digest, phrase);
  130. HPAVKeyOut (digest, HPAVKEY_NMK_LEN, phrase, flags);
  131. continue;
  132. }
  133. if (class == HPAVKEY_NID)
  134. {
  135. HPAVKeyNMK (digest, phrase);
  136. HPAVKeyNID (digest, digest, level);
  137. HPAVKeyOut (digest, HPAVKEY_NID_LEN, phrase, flags);
  138. continue;
  139. }
  140. HPAVKeySHA (digest, phrase);
  141. HPAVKeyOut (digest, HPAVKEY_SHA_LEN, phrase, flags);
  142. }
  143. return;
  144. }
  145. /*====================================================================*
  146. *
  147. * int main (int argc, const char * argv []);
  148. *
  149. *--------------------------------------------------------------------*/
  150. #define DEFAULT_LEVEL 0
  151. int main (int argc, const char * argv [])
  152. {
  153. static const char * optv [] =
  154. {
  155. "DeL:MNqv",
  156. "file [file] [...]",
  157. "HomePlug AV key generator",
  158. "D\tconvert password to Device Access Key",
  159. "e\tenforce HomePlug AV password rules",
  160. "M\tconvert password to Network Membership Key",
  161. "N\tconvert password to Network Identification Key",
  162. "L n\tSecurity Level is (n) [" LITERAL (DEFAULT_LEVEL) "]",
  163. "q\tquiet mode",
  164. "v\tverbose mode",
  165. (const char *) (0)
  166. };
  167. flag_t flags = (flag_t) (0);
  168. signed type = 0;
  169. signed level = DEFAULT_LEVEL;
  170. signed c;
  171. optind = 1;
  172. while (~ (c = getoptv (argc, argv, optv)))
  173. {
  174. switch ((char) (c))
  175. {
  176. case 'D':
  177. type = HPAVKEY_DAK;
  178. break;
  179. case 'M':
  180. type = HPAVKEY_NMK;
  181. break;
  182. case 'N':
  183. type = HPAVKEY_NID;
  184. break;
  185. case 'L':
  186. level = (signed) (uintspec (optarg, 0, 1));
  187. break;
  188. case 'q':
  189. _setbits (flags, HPAVKEY_SILENCE);
  190. break;
  191. case 'v':
  192. _setbits (flags, HPAVKEY_VERBOSE);
  193. break;
  194. case 'e':
  195. _setbits (flags, HPAVKEY_ENFORCE);
  196. break;
  197. default:
  198. break;
  199. }
  200. }
  201. argc -= optind;
  202. argv += optind;
  203. if (! argc)
  204. {
  205. generate (type, level, flags);
  206. }
  207. while ((argc) && (* argv))
  208. {
  209. if (! freopen (* argv, "r", stdin))
  210. {
  211. error (0, errno, "%s", * argv);
  212. }
  213. else
  214. {
  215. generate (type, level, flags);
  216. }
  217. argv++;
  218. argc--;
  219. }
  220. return (0);
  221. }