/drivers/staging/wlags49_h2/wl_enc.c

https://bitbucket.org/wisechild/galaxy-nexus · C · 232 lines · 74 code · 50 blank · 108 comment · 6 complexity · 028d3a35d5752e98777cf586b2f2d22b MD5 · raw file

  1. /*******************************************************************************
  2. * Agere Systems Inc.
  3. * Wireless device driver for Linux (wlags49).
  4. *
  5. * Copyright (c) 1998-2003 Agere Systems Inc.
  6. * All rights reserved.
  7. * http://www.agere.com
  8. *
  9. * Initially developed by TriplePoint, Inc.
  10. * http://www.triplepoint.com
  11. *
  12. *------------------------------------------------------------------------------
  13. *
  14. * This file defines functions related to WEP key coding/decoding.
  15. *
  16. *------------------------------------------------------------------------------
  17. *
  18. * SOFTWARE LICENSE
  19. *
  20. * This software is provided subject to the following terms and conditions,
  21. * which you should read carefully before using the software. Using this
  22. * software indicates your acceptance of these terms and conditions. If you do
  23. * not agree with these terms and conditions, do not use the software.
  24. *
  25. * Copyright © 2003 Agere Systems Inc.
  26. * All rights reserved.
  27. *
  28. * Redistribution and use in source or binary forms, with or without
  29. * modifications, are permitted provided that the following conditions are met:
  30. *
  31. * . Redistributions of source code must retain the above copyright notice, this
  32. * list of conditions and the following Disclaimer as comments in the code as
  33. * well as in the documentation and/or other materials provided with the
  34. * distribution.
  35. *
  36. * . Redistributions in binary form must reproduce the above copyright notice,
  37. * this list of conditions and the following Disclaimer in the documentation
  38. * and/or other materials provided with the distribution.
  39. *
  40. * . Neither the name of Agere Systems Inc. nor the names of the contributors
  41. * may be used to endorse or promote products derived from this software
  42. * without specific prior written permission.
  43. *
  44. * Disclaimer
  45. *
  46. * THIS SOFTWARE IS PROVIDED “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES,
  47. * INCLUDING, BUT NOT LIMITED TO, INFRINGEMENT AND THE IMPLIED WARRANTIES OF
  48. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ANY
  49. * USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE IS SOLELY AT THE USERS OWN
  50. * RISK. IN NO EVENT SHALL AGERE SYSTEMS INC. OR CONTRIBUTORS BE LIABLE FOR ANY
  51. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  52. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  53. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  54. * ON ANY THEORY OF LIABILITY, INCLUDING, BUT NOT LIMITED TO, CONTRACT, STRICT
  55. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  56. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  57. * DAMAGE.
  58. *
  59. ******************************************************************************/
  60. /*******************************************************************************
  61. * include files
  62. ******************************************************************************/
  63. #include <linux/string.h>
  64. #include <wl_version.h>
  65. #include <debug.h>
  66. #include <hcf.h>
  67. #include <wl_enc.h>
  68. /*******************************************************************************
  69. * global definitions
  70. ******************************************************************************/
  71. #if DBG
  72. extern dbg_info_t *DbgInfo;
  73. #endif /* DBG */
  74. /*******************************************************************************
  75. * wl_wep_code()
  76. *******************************************************************************
  77. *
  78. * DESCRIPTION:
  79. *
  80. * This function encodes a set of wep keys for privacy
  81. *
  82. * PARAMETERS:
  83. *
  84. * szCrypt -
  85. * szDest -
  86. * Data -
  87. * nLen -
  88. *
  89. * RETURNS:
  90. *
  91. * OK
  92. *
  93. ******************************************************************************/
  94. int wl_wep_code( char *szCrypt, char *szDest, void *Data, int nLen )
  95. {
  96. int i;
  97. int t;
  98. int k ;
  99. char bits;
  100. char *szData = (char *) Data;
  101. /*------------------------------------------------------------------------*/
  102. for( i = bits = 0 ; i < MACADDRESS_STR_LEN; i++ ) {
  103. bits ^= szCrypt[i];
  104. bits += szCrypt[i];
  105. }
  106. for( i = t = *szDest = 0; i < nLen; i++, t++ ) {
  107. k = szData[i] ^ ( bits + i );
  108. switch( i % 3 ) {
  109. case 0 :
  110. szDest[t] = ((k & 0xFC) >> 2) + CH_START ;
  111. szDest[t+1] = ((k & 0x03) << 4) + CH_START ;
  112. szDest[t+2] = '\0';
  113. break;
  114. case 1 :
  115. szDest[t] += (( k & 0xF0 ) >> 4 );
  116. szDest[t+1] = (( k & 0x0F ) << 2 ) + CH_START ;
  117. szDest[t+2] = '\0';
  118. break;
  119. case 2 :
  120. szDest[t] += (( k & 0xC0 ) >> 6 );
  121. szDest[t+1] = ( k & 0x3F ) + CH_START ;
  122. szDest[t+2] = '\0';
  123. t++;
  124. break;
  125. }
  126. }
  127. return( strlen( szDest )) ;
  128. }
  129. /*============================================================================*/
  130. /*******************************************************************************
  131. * wl_wep_decode()
  132. *******************************************************************************
  133. *
  134. * DESCRIPTION:
  135. *
  136. * This function decodes a set of WEP keys for use by the card.
  137. *
  138. * PARAMETERS:
  139. *
  140. * szCrypt -
  141. * szDest -
  142. * Data -
  143. *
  144. * RETURNS:
  145. *
  146. * OK
  147. *
  148. ******************************************************************************/
  149. int wl_wep_decode( char *szCrypt, void *Dest, char *szData )
  150. {
  151. int i;
  152. int t;
  153. int nLen;
  154. char bits;
  155. char *szDest = Dest;
  156. /*------------------------------------------------------------------------*/
  157. for( i = bits = 0 ; i < 12; i++ ) {
  158. bits ^= szCrypt[i] ;
  159. bits += szCrypt[i] ;
  160. }
  161. nLen = ( strlen( szData ) * 3) / 4 ;
  162. for( i = t = 0; i < nLen; i++, t++ ) {
  163. switch( i % 3 ) {
  164. case 0 :
  165. szDest[i] = ((( szData[t]-CH_START ) & 0x3f ) << 2 ) +
  166. ((( szData[t+1]-CH_START ) & 0x30 ) >> 4 );
  167. break;
  168. case 1 :
  169. szDest[i] = ((( szData[t]-CH_START ) & 0x0f ) << 4 ) +
  170. ((( szData[t+1]-CH_START ) & 0x3c ) >> 2 );
  171. break;
  172. case 2 :
  173. szDest[i] = ((( szData[t]-CH_START ) & 0x03 ) << 6 ) +
  174. (( szData[t+1]-CH_START ) & 0x3f );
  175. t++;
  176. break;
  177. }
  178. szDest[i] ^= ( bits + i ) ;
  179. }
  180. return( i ) ;
  181. }
  182. /*============================================================================*/