PageRenderTime 60ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Microchip/TCPIP Stack/ARCFOUR.c

https://bitbucket.org/rohanl/microchip_solutions
C | 183 lines | 48 code | 17 blank | 118 comment | 3 complexity | 7491ac1173082a3fac00882961492065 MD5 | raw file
  1. /*********************************************************************
  2. *
  3. * ARCFOUR Cryptography Library
  4. * Library for Microchip TCP/IP Stack
  5. * - Provides encryption and decryption capabilities for the ARCFOUR
  6. * algorithm, typically used as a bulk cipher for SSL
  7. * - Reference: http://tools.ietf.org/html/draft-kaukonen-cipher-arcfour-01
  8. *
  9. *********************************************************************
  10. * FileName: ARCFOUR.c
  11. * Dependencies: None
  12. * Processor: PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F, PIC32
  13. * Compiler: Microchip C32 v1.05 or higher
  14. * Microchip C30 v3.12 or higher
  15. * Microchip C18 v3.30 or higher
  16. * HI-TECH PICC-18 PRO 9.63PL2 or higher
  17. * Company: Microchip Technology, Inc.
  18. *
  19. * Software License Agreement
  20. *
  21. * Copyright (C) 2002-2009 Microchip Technology Inc. All rights
  22. * reserved.
  23. *
  24. * Microchip licenses to you the right to use, modify, copy, and
  25. * distribute:
  26. * (i) the Software when embedded on a Microchip microcontroller or
  27. * digital signal controller product ("Device") which is
  28. * integrated into Licensee's product; or
  29. * (ii) ONLY the Software driver source files ENC28J60.c, ENC28J60.h,
  30. * ENCX24J600.c and ENCX24J600.h ported to a non-Microchip device
  31. * used in conjunction with a Microchip ethernet controller for
  32. * the sole purpose of interfacing with the ethernet controller.
  33. *
  34. * You should refer to the license agreement accompanying this
  35. * Software for additional information regarding your rights and
  36. * obligations.
  37. *
  38. * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT
  39. * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
  40. * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
  41. * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  42. * MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR
  43. * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
  44. * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
  45. * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
  46. * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
  47. * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
  48. * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
  49. *
  50. * IMPORTANT: The implementation and use of third party algorithms,
  51. * specifications and/or other technology may require a license from
  52. * various third parties. It is your responsibility to obtain
  53. * information regarding any applicable licensing obligations.
  54. *
  55. *
  56. * Author Date Comment
  57. *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  58. * Elliott Wood 2/27/07 Original
  59. ********************************************************************/
  60. #define __ARCFOUR_C
  61. #include "TCPIPConfig.h"
  62. #if defined(STACK_USE_SSL_SERVER) || defined(STACK_USE_SSL_CLIENT)
  63. #include "TCPIP Stack/TCPIP.h"
  64. /*****************************************************************************
  65. Function:
  66. void ARCFOURInitialize(ARCFOUR_CTX* ctx, BYTE* key, WORD len)
  67. Summary:
  68. Initializes an ARCFOUR encryption stream.
  69. Description:
  70. This function initializes an ARCFOUR encryption stream. Call this
  71. function to set up the initial state of the encryption context and the
  72. S-box. The stream will be initialized to its zero state with the
  73. supplied key.
  74. This function can be used to initialize for encryption and decryption.
  75. Precondition:
  76. None
  77. Parameters:
  78. ctx - A pointer to the allocated encryption context structure
  79. key - A pointer to the key to be used
  80. len - The length of the data in key
  81. Returns:
  82. None
  83. Remarks:
  84. For security, the key should be destroyed after this call.
  85. ***************************************************************************/
  86. void ARCFOURInitialize(ARCFOUR_CTX* ctx, BYTE* key, WORD len)
  87. {
  88. BYTE temp, i, j, *Sbox;
  89. // Initialize the context indicies
  90. i = 0;
  91. j = 0;
  92. Sbox = ctx->Sbox;
  93. // Initialize each S-box element with its index
  94. do
  95. {
  96. Sbox[i] = i;
  97. i++;
  98. } while(i != 0u);
  99. // Fill in the S-box
  100. do
  101. {
  102. j = j + Sbox[i] + key[i % len];
  103. temp = Sbox[i];
  104. Sbox[i] = Sbox[j];
  105. Sbox[j] = temp;
  106. i++;
  107. } while(i != 0u);
  108. // Reset the context indicies
  109. ctx->i = 0;
  110. ctx->j = 0;
  111. }
  112. /*****************************************************************************
  113. Function:
  114. void ARCFOURCrypt(ARCFOUR_CTX* ctx, BYTE* data, WORD len)
  115. Summary:
  116. Processes an array of data with the ARCFOUR algorithm.
  117. Description:
  118. This function uses the current ARCFOUR context to either encrypt or
  119. decrypt data in place. The algorithm is the same for both processes,
  120. so this function can perform either procedure.
  121. Precondition:
  122. The encryption context ctx has been initialized with ARCFOURInitialize.
  123. Parameters:
  124. ctx - A pointer to the initialized encryption context structure
  125. data - The data to be encrypted or decrypted (in place)
  126. len - The length of data
  127. Returns:
  128. None
  129. ***************************************************************************/
  130. void ARCFOURCrypt(ARCFOUR_CTX* ctx, BYTE* data, WORD len)
  131. {
  132. BYTE temp, temp2, i, j, *Sbox;
  133. // Buffer context variables in local RAM for faster access
  134. i = ctx->i;
  135. j = ctx->j;
  136. Sbox = ctx->Sbox;
  137. // Loop over each byte. Extract its key and XOR
  138. while(len--)
  139. {
  140. i++;
  141. temp = Sbox[i];
  142. j += temp;
  143. temp2 = Sbox[j];
  144. Sbox[i] = temp2;
  145. Sbox[j] = temp;
  146. temp += temp2;
  147. temp2 = Sbox[temp];
  148. *data++ ^= temp2;
  149. }
  150. // Save the new context
  151. ctx->i = i;
  152. ctx->j = j;
  153. }
  154. #endif //#if defined(STACK_USE_SSL_SERVER) || defined(STACK_USE_SSL_CLIENT)