Forest Bond | 5449c68 | 2009-04-25 10:30:44 -0400 | [diff] [blame] | 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 BYTE[] to DWORD in a portable way |
| 30 | * s_vPutUINT32 - Convert from DWORD to BYTE[] 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 | |
| 42 | #if !defined(__TMACRO_H__) |
| 43 | #include "tmacro.h" |
| 44 | #endif |
| 45 | #if !defined(__TBIT_H__) |
| 46 | #include "tbit.h" |
| 47 | #endif |
| 48 | #if !defined(__MICHAEL_H__) |
| 49 | #include "michael.h" |
| 50 | #endif |
| 51 | |
| 52 | /*--------------------- Static Definitions -------------------------*/ |
| 53 | |
| 54 | /*--------------------- Static Variables --------------------------*/ |
| 55 | |
| 56 | /*--------------------- Static Functions --------------------------*/ |
| 57 | /* |
| 58 | static DWORD s_dwGetUINT32(BYTE * p); // Get DWORD from 4 bytes LSByte first |
| 59 | static VOID s_vPutUINT32(BYTE* p, DWORD val); // Put DWORD into 4 bytes LSByte first |
| 60 | */ |
| 61 | static VOID s_vClear(void); // Clear the internal message, |
| 62 | // resets the object to the state just after construction. |
| 63 | static VOID s_vSetKey(DWORD dwK0, DWORD dwK1); |
| 64 | static VOID s_vAppendByte(BYTE b); // Add a single byte to the internal message |
| 65 | |
| 66 | /*--------------------- Export Variables --------------------------*/ |
| 67 | static DWORD L, R; // Current state |
| 68 | |
| 69 | static DWORD K0, K1; // Key |
| 70 | static DWORD M; // Message accumulator (single word) |
| 71 | static UINT nBytesInM; // # bytes in M |
| 72 | |
| 73 | /*--------------------- Export Functions --------------------------*/ |
| 74 | |
| 75 | /* |
| 76 | static DWORD s_dwGetUINT32 (BYTE * p) |
| 77 | // Convert from BYTE[] to DWORD in a portable way |
| 78 | { |
| 79 | DWORD res = 0; |
| 80 | UINT i; |
| 81 | for(i=0; i<4; i++ ) |
| 82 | { |
| 83 | res |= (*p++) << (8*i); |
| 84 | } |
| 85 | return res; |
| 86 | } |
| 87 | |
| 88 | static VOID s_vPutUINT32 (BYTE* p, DWORD val) |
| 89 | // Convert from DWORD to BYTE[] in a portable way |
| 90 | { |
| 91 | UINT i; |
| 92 | for(i=0; i<4; i++ ) |
| 93 | { |
| 94 | *p++ = (BYTE) (val & 0xff); |
| 95 | val >>= 8; |
| 96 | } |
| 97 | } |
| 98 | */ |
| 99 | |
| 100 | static VOID s_vClear (void) |
| 101 | { |
| 102 | // Reset the state to the empty message. |
| 103 | L = K0; |
| 104 | R = K1; |
| 105 | nBytesInM = 0; |
| 106 | M = 0; |
| 107 | } |
| 108 | |
| 109 | static VOID s_vSetKey (DWORD dwK0, DWORD dwK1) |
| 110 | { |
| 111 | // Set the key |
| 112 | K0 = dwK0; |
| 113 | K1 = dwK1; |
| 114 | // and reset the message |
| 115 | s_vClear(); |
| 116 | } |
| 117 | |
| 118 | static VOID s_vAppendByte (BYTE b) |
| 119 | { |
| 120 | // Append the byte to our word-sized buffer |
| 121 | M |= b << (8*nBytesInM); |
| 122 | nBytesInM++; |
| 123 | // Process the word if it is full. |
| 124 | if( nBytesInM >= 4 ) |
| 125 | { |
| 126 | L ^= M; |
| 127 | R ^= ROL32( L, 17 ); |
| 128 | L += R; |
| 129 | R ^= ((L & 0xff00ff00) >> 8) | ((L & 0x00ff00ff) << 8); |
| 130 | L += R; |
| 131 | R ^= ROL32( L, 3 ); |
| 132 | L += R; |
| 133 | R ^= ROR32( L, 2 ); |
| 134 | L += R; |
| 135 | // Clear the buffer |
| 136 | M = 0; |
| 137 | nBytesInM = 0; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | VOID MIC_vInit (DWORD dwK0, DWORD dwK1) |
| 142 | { |
| 143 | // Set the key |
| 144 | s_vSetKey(dwK0, dwK1); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | VOID MIC_vUnInit (void) |
| 149 | { |
| 150 | // Wipe the key material |
| 151 | K0 = 0; |
| 152 | K1 = 0; |
| 153 | |
| 154 | // And the other fields as well. |
| 155 | //Note that this sets (L,R) to (K0,K1) which is just fine. |
| 156 | s_vClear(); |
| 157 | } |
| 158 | |
| 159 | VOID MIC_vAppend (PBYTE src, UINT nBytes) |
| 160 | { |
| 161 | // This is simple |
| 162 | while (nBytes > 0) |
| 163 | { |
| 164 | s_vAppendByte(*src++); |
| 165 | nBytes--; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | VOID MIC_vGetMIC (PDWORD pdwL, PDWORD pdwR) |
| 170 | { |
| 171 | // Append the minimum padding |
| 172 | s_vAppendByte(0x5a); |
| 173 | s_vAppendByte(0); |
| 174 | s_vAppendByte(0); |
| 175 | s_vAppendByte(0); |
| 176 | s_vAppendByte(0); |
| 177 | // and then zeroes until the length is a multiple of 4 |
| 178 | while( nBytesInM != 0 ) |
| 179 | { |
| 180 | s_vAppendByte(0); |
| 181 | } |
| 182 | // The s_vAppendByte function has already computed the result. |
| 183 | *pdwL = L; |
| 184 | *pdwR = R; |
| 185 | // Reset to the empty message. |
| 186 | s_vClear(); |
| 187 | } |
| 188 | |