hpavkeys.c 7.0 KB

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