michael.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. *
  20. * File: michael.cpp
  21. *
  22. * Purpose: The implementation of LIST data structure.
  23. *
  24. * Author: Kyle Hsu
  25. *
  26. * Date: Sep 4, 2002
  27. *
  28. * Functions:
  29. * s_dwGetUINT32 - Convert from unsigned char [] to unsigned long in a portable way
  30. * s_vPutUINT32 - Convert from unsigned long to unsigned char [] in a portable way
  31. * s_vClear - Reset the state to the empty message.
  32. * s_vSetKey - Set the key.
  33. * MIC_vInit - Set the key.
  34. * s_vAppendByte - Append the byte to our word-sized buffer.
  35. * MIC_vAppend - call s_vAppendByte.
  36. * MIC_vGetMIC - Append the minimum padding and call s_vAppendByte.
  37. *
  38. * Revision History:
  39. *
  40. */
  41. #include "tmacro.h"
  42. #include "michael.h"
  43. /*--------------------- Static Definitions -------------------------*/
  44. /*--------------------- Static Variables --------------------------*/
  45. /*--------------------- Static Functions --------------------------*/
  46. static void s_vClear(void); // Clear the internal message,
  47. // resets the object to the state just after construction.
  48. static void s_vSetKey(u32 dwK0, u32 dwK1);
  49. static void s_vAppendByte(unsigned char b); // Add a single byte to the internal message
  50. /*--------------------- Export Variables --------------------------*/
  51. static u32 L, R; /* Current state */
  52. static u32 K0, K1; /* Key */
  53. static u32 M; /* Message accumulator (single word) */
  54. static unsigned int nBytesInM; // # bytes in M
  55. /*--------------------- Export Functions --------------------------*/
  56. static void s_vClear(void)
  57. {
  58. // Reset the state to the empty message.
  59. L = K0;
  60. R = K1;
  61. nBytesInM = 0;
  62. M = 0;
  63. }
  64. static void s_vSetKey(u32 dwK0, u32 dwK1)
  65. {
  66. // Set the key
  67. K0 = dwK0;
  68. K1 = dwK1;
  69. // and reset the message
  70. s_vClear();
  71. }
  72. static void s_vAppendByte(unsigned char b)
  73. {
  74. // Append the byte to our word-sized buffer
  75. M |= b << (8*nBytesInM);
  76. nBytesInM++;
  77. // Process the word if it is full.
  78. if (nBytesInM >= 4) {
  79. L ^= M;
  80. R ^= ROL32(L, 17);
  81. L += R;
  82. R ^= ((L & 0xff00ff00) >> 8) | ((L & 0x00ff00ff) << 8);
  83. L += R;
  84. R ^= ROL32(L, 3);
  85. L += R;
  86. R ^= ROR32(L, 2);
  87. L += R;
  88. // Clear the buffer
  89. M = 0;
  90. nBytesInM = 0;
  91. }
  92. }
  93. void MIC_vInit(u32 dwK0, u32 dwK1)
  94. {
  95. // Set the key
  96. s_vSetKey(dwK0, dwK1);
  97. }
  98. void MIC_vUnInit(void)
  99. {
  100. // Wipe the key material
  101. K0 = 0;
  102. K1 = 0;
  103. // And the other fields as well.
  104. //Note that this sets (L,R) to (K0,K1) which is just fine.
  105. s_vClear();
  106. }
  107. void MIC_vAppend(unsigned char *src, unsigned int nBytes)
  108. {
  109. // This is simple
  110. while (nBytes > 0) {
  111. s_vAppendByte(*src++);
  112. nBytes--;
  113. }
  114. }
  115. void MIC_vGetMIC(u32 *pdwL, u32 *pdwR)
  116. {
  117. // Append the minimum padding
  118. s_vAppendByte(0x5a);
  119. s_vAppendByte(0);
  120. s_vAppendByte(0);
  121. s_vAppendByte(0);
  122. s_vAppendByte(0);
  123. // and then zeroes until the length is a multiple of 4
  124. while (nBytesInM != 0)
  125. s_vAppendByte(0);
  126. // The s_vAppendByte function has already computed the result.
  127. *pdwL = L;
  128. *pdwR = R;
  129. // Reset to the empty message.
  130. s_vClear();
  131. }