hpavkey.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. * int main (int argc, const char * argv []);
  84. *
  85. *
  86. *
  87. *--------------------------------------------------------------------*/
  88. #define DEFAULT_LEVEL 0
  89. int main (int argc, const char * argv [])
  90. {
  91. static const char * optv [] =
  92. {
  93. "DeL:MNqv",
  94. "phrase [phrase] [...]",
  95. "HomePlug AV key generator",
  96. "D\tconvert password to Device Access Key",
  97. "e\tenforce HomePlug AV password rules",
  98. "M\tconvert password to Network Membership Key",
  99. "N\tconvert password to Network Identifier",
  100. "L n\tSecurity Level is (n) [" LITERAL (DEFAULT_LEVEL) "]",
  101. "q\tquiet mode",
  102. "v\tverbose mode",
  103. (const char *) (0)
  104. };
  105. byte digest [SHA256_DIGEST_LENGTH];
  106. const char * phrase;
  107. const char * sp;
  108. signed type = 0;
  109. signed level = DEFAULT_LEVEL;
  110. flag_t flags = (flag_t)(0);
  111. signed c;
  112. optind = 1;
  113. while ((c = getoptv (argc, argv, optv)) != -1)
  114. {
  115. switch ((char) (c))
  116. {
  117. case 'D':
  118. type = HPAVKEY_DAK;
  119. break;
  120. case 'M':
  121. type = HPAVKEY_NMK;
  122. break;
  123. case 'N':
  124. type = HPAVKEY_NID;
  125. break;
  126. case 'L':
  127. level = (signed)(uintspec (optarg, 0, 1));
  128. break;
  129. case 'q':
  130. _setbits (flags, HPAVKEY_SILENCE);
  131. break;
  132. case 'v':
  133. _setbits (flags, HPAVKEY_VERBOSE);
  134. break;
  135. case 'e':
  136. _setbits (flags, HPAVKEY_ENFORCE);
  137. break;
  138. default:
  139. break;
  140. }
  141. }
  142. argc -= optind;
  143. argv += optind;
  144. while ((argc--) && (* argv))
  145. {
  146. sp = phrase = * argv++;
  147. while (isprint (*sp))
  148. {
  149. sp++;
  150. }
  151. if (_anyset (flags, HPAVKEY_ENFORCE))
  152. {
  153. if (*sp)
  154. {
  155. error (0, ENOTSUP, "Phrase \"%s\" contains illegal characters", phrase);
  156. continue;
  157. }
  158. if ((sp - phrase) < HPAVKEY_PHRASE_MIN)
  159. {
  160. error (0, ENOTSUP, "Phrase \"%s\" less than %d characters", phrase, HPAVKEY_PHRASE_MIN);
  161. continue;
  162. }
  163. if ((sp - phrase) > HPAVKEY_PHRASE_MAX)
  164. {
  165. error (0, ENOTSUP, "Phrase \"%s\" more than %d characters", phrase, HPAVKEY_PHRASE_MAX);
  166. continue;
  167. }
  168. }
  169. if (type == HPAVKEY_DAK)
  170. {
  171. HPAVKeyDAK (digest, phrase);
  172. HPAVKeyOut (digest, HPAVKEY_DAK_LEN, phrase, flags);
  173. continue;
  174. }
  175. if (type == HPAVKEY_NMK)
  176. {
  177. HPAVKeyNMK (digest, phrase);
  178. HPAVKeyOut (digest, HPAVKEY_NMK_LEN, phrase, flags);
  179. continue;
  180. }
  181. if (type == HPAVKEY_NID)
  182. {
  183. HPAVKeyNMK (digest, phrase);
  184. HPAVKeyNID (digest, digest, level);
  185. HPAVKeyOut (digest, HPAVKEY_NID_LEN, phrase, flags);
  186. continue;
  187. }
  188. HPAVKeySHA (digest, phrase);
  189. HPAVKeyOut (digest, HPAVKEY_SHA_LEN, phrase, flags);
  190. }
  191. return (0);
  192. }