hpavkey.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. * system header files;
  10. *--------------------------------------------------------------------*/
  11. #include <cstdio>
  12. #include <cstdlib>
  13. #include <cstring>
  14. #include <cerrno>
  15. #include <iostream>
  16. /*====================================================================*
  17. * custom header files;
  18. *--------------------------------------------------------------------*/
  19. #include "../classes/oHPAVKey.hpp"
  20. #include "../classes/ogetoptv.hpp"
  21. #include "../classes/oflagword.hpp"
  22. #include "../classes/oerror.hpp"
  23. #include "../tools/types.h"
  24. /*====================================================================*
  25. * custom source files;
  26. *--------------------------------------------------------------------*/
  27. #ifndef MAKEFILE
  28. #include "../classes/ogetoptv.cpp"
  29. #include "../classes/oputoptv.cpp"
  30. #include "../classes/oversion.cpp"
  31. #include "../classes/oerror.cpp"
  32. #include "../classes/omemory.cpp"
  33. #include "../classes/oflagword.cpp"
  34. #endif
  35. #ifndef MAKEFILE
  36. #include "../classes/oHPAVKey.cpp"
  37. #include "../classes/oSHA256.cpp"
  38. #endif
  39. /*====================================================================*
  40. *
  41. * int main (int argc, const char *argv []);
  42. *
  43. *--------------------------------------------------------------------*/
  44. int main (int argc, const char *argv [])
  45. {
  46. static const char *optv [] =
  47. {
  48. "DeMNqv",
  49. oPUTOPTV_S_FUNNEL,
  50. "HomePlug AV key generator",
  51. "D\thash DAK passwords",
  52. "M\thash NMK passwords",
  53. "N\thash NID passwords",
  54. "q\tquiet mode",
  55. "v\tverbose mode",
  56. (const char *) (0)
  57. };
  58. oHPAVKey key;
  59. ogetoptv command;
  60. oflagword flagword;
  61. signed mode = 0;
  62. signed c;
  63. command.opterr (1).optmin (1);
  64. while ((c = command.getoptv (argc, argv, optv)) != -1)
  65. {
  66. switch (c)
  67. {
  68. case 'e':
  69. flagword.setbits (oHPAVKEY_B_ENFORCE);
  70. break;
  71. case 'D':
  72. mode = oHPAVKEY_N_DAK;
  73. break;
  74. case 'M':
  75. mode = oHPAVKEY_N_NMK;
  76. break;
  77. case 'N':
  78. mode = oHPAVKEY_N_NID;
  79. break;
  80. case 'q':
  81. flagword.setbits (oHPAVKEY_B_SILENCE);
  82. break;
  83. case 'v':
  84. flagword.setbits (oHPAVKEY_B_VERBOSE);
  85. break;
  86. default:
  87. break;
  88. }
  89. }
  90. while (command.argc () && * command.argv ())
  91. {
  92. const char * phrase = * command.argv ();
  93. if (flagword.anyset (oHPAVKEY_B_ENFORCE))
  94. {
  95. if (key.IllegalPassPhrase (phrase))
  96. {
  97. command++;
  98. continue;
  99. }
  100. }
  101. if (mode == oHPAVKEY_N_DAK)
  102. {
  103. key.ComputeDAK (phrase);
  104. if (flagword.allclear (oHPAVKEY_B_VERBOSE))
  105. {
  106. phrase = (const char *)(0);
  107. }
  108. key.Print (phrase);
  109. }
  110. else if (mode == oHPAVKEY_N_NMK)
  111. {
  112. key.ComputeNMK (phrase);
  113. if (flagword.allclear (oHPAVKEY_B_VERBOSE))
  114. {
  115. phrase = (const char *)(0);
  116. }
  117. key.Print (phrase);
  118. }
  119. else if (mode == oHPAVKEY_N_NID)
  120. {
  121. key.ComputeNMK (phrase).ComputeNID (0);
  122. if (flagword.allclear (oHPAVKEY_B_VERBOSE))
  123. {
  124. phrase = (const char *)(0);
  125. }
  126. key.Print (phrase);
  127. }
  128. command++;
  129. }
  130. std::exit (0);
  131. }