MACPasswords.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. /*====================================================================*
  42. *
  43. * void MACPasswords (unsigned vendor, unsigned device, unsigned number, unsigned count, unsigned group, char space, flag_t flags);
  44. *
  45. * keys.h
  46. *
  47. * print a range of device address/password pairs on stdout; print
  48. * an optional usage flag in first column for PTS compatability;
  49. *
  50. * Contributor(s):
  51. * Charles Maier
  52. *
  53. *--------------------------------------------------------------------*/
  54. #ifndef MACPASSWORDS_SOURCE
  55. #define MACPASSWORDS_SOURCE
  56. #include <stdio.h>
  57. #include <ctype.h>
  58. #include <stdlib.h>
  59. #include <stdint.h>
  60. #include <limits.h>
  61. #include "../tools/types.h"
  62. #include "../tools/flags.h"
  63. #include "../key/keys.h"
  64. static uint64_t MACSeed = 0;
  65. static uint64_t MACSRand (uint64_t seed)
  66. {
  67. uint64_t temp = MACSeed;
  68. MACSeed = seed;
  69. return (temp);
  70. }
  71. static unsigned MACRand ()
  72. {
  73. MACSeed *= 0x41C64E6D;
  74. MACSeed += 0x00003029;
  75. return ((unsigned)((MACSeed >> 0x10) & 0x7FFFFFFF));
  76. }
  77. /*====================================================================*
  78. *
  79. * void MACPassword (unsigned device, char const charset [], unsigned limit, unsigned count, unsigned group, char space);
  80. *
  81. * keys.h
  82. *
  83. * Contributor(s):
  84. * Charles Maier
  85. *
  86. *--------------------------------------------------------------------*/
  87. void MACPassword (unsigned device, char const charset [], unsigned limit, unsigned count, unsigned group, char space)
  88. {
  89. MACSRand (device);
  90. while (count--)
  91. {
  92. unsigned index = MACRand () % limit;
  93. putc (charset [index & limit], stdout);
  94. if ((count) && (group) && !(count % group))
  95. {
  96. putc (space, stdout);
  97. }
  98. }
  99. return;
  100. }
  101. /*====================================================================*
  102. *
  103. * void MACPasswords (unsigned vendor, unsigned device, unsigned number, unsigned count, unsigned group, char space, flag_t flags);
  104. *
  105. * keys.h
  106. *
  107. * print a range of device address/password pairs on stdout; print
  108. * an optional usage flag in first column for PTS compatability;
  109. *
  110. * Contributor(s):
  111. * Charles Maier
  112. *
  113. *--------------------------------------------------------------------*/
  114. void MACPasswords (unsigned vendor, unsigned device, unsigned number, unsigned count, unsigned group, char space, flag_t flags)
  115. {
  116. char charset [UCHAR_MAX];
  117. unsigned offset = 0;
  118. if (vendor >> 24)
  119. {
  120. return;
  121. }
  122. if (device >> 24)
  123. {
  124. return;
  125. }
  126. if (number >> 24)
  127. {
  128. return;
  129. }
  130. MACSRand (vendor);
  131. while (offset < sizeof (charset))
  132. {
  133. unsigned c = MACRand () % (SCHAR_MAX + 1);
  134. if (isupper (c))
  135. {
  136. charset [offset++] = c;
  137. }
  138. }
  139. while (number--)
  140. {
  141. if (_anyset (flags, PASSWORD_VERBOSE))
  142. {
  143. putc ('0', stdout);
  144. putc (' ', stdout);
  145. }
  146. if (_allclr (flags, PASSWORD_SILENCE))
  147. {
  148. printf ("%06X", vendor & 0x00FFFFFF);
  149. printf ("%06X", device & 0x00FFFFFF);
  150. putc (' ', stdout);
  151. }
  152. MACPassword (device, charset, sizeof (charset), count, group, space);
  153. putc ('\n', stdout);
  154. device++;
  155. }
  156. return;
  157. }
  158. #endif