Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
9.0 KiB

  1. /******************************************************************************
  2. * C A S T 3 S Y M M E T R I C C I P H E R
  3. * Copyright (c) 1995 Northern Telecom Ltd. All rights reserved.
  4. ******************************************************************************
  5. *
  6. * FILE: cast3.h
  7. *
  8. * AUTHOR(S): R.T.Lockhart, Dept. 9C42, BNR Ltd.
  9. * C. Adams, Dept 9C21, BNR Ltd.
  10. *
  11. * DESCRIPTION: CAST3 header file. This file defines the interface for the
  12. * CAST3 symmetric-key encryption/decryption code. This code supports key
  13. * lengths from 8 to 64 bits in multiples of 8.
  14. *
  15. * To use this CAST3 code:
  16. * Allocate a CAST3_CTX context structure, then set up a key schedule using
  17. * CAST3SetKeySchedule. Then do encryption, decryption or MAC calculations
  18. * using that same context. When finished, you may optionally call CAST3Cleanup
  19. * which zeroizes the context so as not to leave security-critical data in
  20. * memory.
  21. *
  22. * To encrypt/decrypt in Cipher Block Chaining (CBC) mode:
  23. * Call CAST3StartEncryptCBC, passing in a pointer to the 8-byte Initialization
  24. * Vector (IV). Then call CAST3UpdateEncryptCBC to encrypt your data. You may
  25. * call CAST3UpdateEncryptCBC any number of times to encrypt successive chunks
  26. * of data. When done, call CAST3EndEncryptCBC which applies data padding and
  27. * outputs any remaining ciphertext. To decrypt, follow a similar procedure
  28. * using CAST3StartDecryptCBC, CAST3UpdateDecryptCBC, and CAST3EndEncryptCBC.
  29. *
  30. * To calculate a MAC:
  31. * Call CAST3StartMAC, passing in a pointer to the 8-byte Initialization
  32. * Vector (IV). Then call CAST3UpdateMAC to update the MAC calculation session.
  33. * You may call CAST3UpdateMAC any number of times to process successive chunks
  34. * of data. When done, call CAST3EndMAC to end the session. At this point, the
  35. * MAC resides in the CAST3_CTX.cbcBuffer.asBYTE array.
  36. *
  37. * Error handling:
  38. * Most functions return an int that indicates the success or failure of the
  39. * operation. See the C3E #defines for a list of error conditions.
  40. *
  41. * Data size assumptions: BYTE - unsigned 8 bits
  42. * UINT32 - unsigned 32 bits
  43. *
  44. *****************************************************************************/
  45. #ifndef CAST3_H
  46. #define CAST3_H
  47. #include <skconfig.h> /* Algorithm configuration */
  48. /* Define this at compile time to use assembly optimization where possible */
  49. #define CAST3_ASSEMBLY_LANGUAGE
  50. /* Misc defs */
  51. #define CAST3_BLK_SIZE 8 /* Basic block size, in bytes */
  52. #define CAST3_MAX_KEY_NBITS 64 /* Maximum key length, in bits */
  53. #define CAST3_MAX_KEY_NBYTES (CAST3_MAX_KEY_NBITS / 8)
  54. #define CAST3_NUM_ROUNDS 12 /* Number of rounds */
  55. #define CAST3_LEN_DELTA 8 /* Output data space = input + this */
  56. /* CAST3 return codes. Negative denotes error. */
  57. #define C3E_OK 0 /* No error */
  58. #define C3E_DEPAD_FAILURE -1 /* The de-padding operation failed */
  59. #define C3E_BAD_KEYLEN -2 /* Key length not supported */
  60. #define C3E_SELFTEST_FAILED -3 /* Self-test failed */
  61. #define C3E_NOT_SUPPORTED -4 /* Function not supported */
  62. /*******************************************************************************
  63. * D A T A D E F I N I T I O N S
  64. ******************************************************************************/
  65. /* CAST3 Block
  66. * Forces block to be 32-bit aligned but allows both 32-bit and byte access.
  67. */
  68. typedef union {
  69. BYTE asBYTE[CAST3_BLK_SIZE];
  70. UINT32 as32[2];
  71. } CAST3_BLOCK;
  72. /* CAST3 Context
  73. * Stores context information for encryption, decryption, and MACs.
  74. */
  75. typedef struct {
  76. UINT32 schedule[CAST3_NUM_ROUNDS * 2];/* Key schedule (subkeys) */
  77. CAST3_BLOCK inBuffer; /* Input buffer */
  78. unsigned int inBufferCount; /* Number of bytes in inBuffer */
  79. CAST3_BLOCK lastDecBlock; /* Last decrypted block */
  80. BOOL lastBlockValid; /* TRUE if lastDecBlock has valid data */
  81. CAST3_BLOCK cbcBuffer; /* Cipher Block Chaining buffer & MAC */
  82. } CAST3_CTX;
  83. /*******************************************************************************
  84. * F U N C T I O N P R O T O T Y P E S
  85. ******************************************************************************/
  86. extern "C" {
  87. /* Sets up CAST3 key schedules, given a variable length key. Key length must
  88. * be a multiple of 8 bits, from 8 to CAST3_MAX_KEY_NBITS.
  89. */
  90. int CAST3SetKeySchedule(
  91. CAST3_CTX * context, /* Out: CAST3 context */
  92. const BYTE * key, /* In: CAST3 key */
  93. unsigned int keyNumBits /* Key length in bits */
  94. );
  95. /* Encrypts one 8-byte block in ECB mode and produces one 8-byte block
  96. * of ciphertext.
  97. */
  98. int CAST3EncryptOneBlock(
  99. const CAST3_CTX * context, /* In: CAST3 context */
  100. const BYTE * inData, /* In: 8-byte input block to encrypt */
  101. BYTE * outData /* Out: 8-byte output block */
  102. );
  103. /* Decrypts one 8-byte block in ECB mode and produces one 8-byte block
  104. * of plaintext.
  105. */
  106. void CAST3DecryptOneBlock(
  107. const CAST3_CTX * context, /* In: CAST3 context */
  108. const BYTE * inData, /* In: 8-byte input block to decrypt */
  109. BYTE * outData /* Out: 8-byte output block */
  110. );
  111. /* Starts an encryption session in CBC mode with the given IV.
  112. */
  113. int CAST3StartEncryptCBC(
  114. CAST3_CTX * context, /* In/out: CAST3 context */
  115. const BYTE * iv /* In: 8-byte CBC IV */
  116. );
  117. /* Encrypts a variable amount of data in CBC mode and outputs the corresponding
  118. * ciphertext. Set len equal to the length of inData. If the input is a multiple
  119. * of the blocksize (8), then the output will be equal to the size of the input;
  120. * otherwise it will be the closest multiple of 8, either higher or lower than
  121. * the input size, depending on leftovers from the last pass. To be safe, supply
  122. * a ptr to an output buffer of size at least (inData length + CAST3_LEN_DELTA).
  123. * Upon return, len is set to the actual length of output data, but may wrap if
  124. * inData length > UINT_MAX - CAST3_LEN_DELTA.
  125. */
  126. int CAST3UpdateEncryptCBC(
  127. CAST3_CTX * context, /* In/out: CAST3 context */
  128. const BYTE * inData, /* In: Data to encrypt */
  129. BYTE * outData, /* Out: Encrypted data */
  130. unsigned int * len /* In/out: Data length, in bytes */
  131. );
  132. /* Ends an encryption session in CBC mode. Applies RFC1423 data padding and
  133. * outputs a final buffer of ciphertext. Supply a ptr to an output buffer at
  134. * least CAST3_LEN_DELTA bytes long. Upon return, len is set to the actual length
  135. * of output data (currently, this is always 8).
  136. */
  137. int CAST3EndEncryptCBC(
  138. CAST3_CTX * context, /* In/out: CAST3 context */
  139. BYTE * outData, /* Out: Final encrypted data */
  140. unsigned int * len /* Out: Length of outData, in bytes */
  141. );
  142. /* Starts a decryption session in CBC mode with the given IV.
  143. */
  144. void CAST3StartDecryptCBC(
  145. CAST3_CTX * context, /* In/out: CAST3 context */
  146. const BYTE * iv /* In: 8-byte CBC IV */
  147. );
  148. /* Decrypts a variable amount of data in CBC mode and outputs the corresponding
  149. * plaintext. Set len equal to the length of inData. Supply a ptr to an output
  150. * buffer of at least (inData length + CAST3_LEN_DELTA) bytes. Upon return, len
  151. * is set to the actual length of output data.
  152. */
  153. void CAST3UpdateDecryptCBC(
  154. CAST3_CTX * context, /* In/out: CAST3 context */
  155. const BYTE * inData, /* In: Data to decrypt */
  156. #ifdef FOR_CSP
  157. BOOL fLastBlock, /* In: Is this the last block? */
  158. #endif // FOR_CSP
  159. BYTE * outData, /* Out: Decrypted data */
  160. unsigned int * len /* In/out: Data length, in bytes */
  161. );
  162. /* Ends a decryption session in CBC mode. Removes RFC1423 data padding and
  163. * outputs a final buffer of plaintext. Supply a ptr to an output buffer at least
  164. * CAST3_LEN_DELTA bytes long. Upon return, len is set to the actual length of
  165. * output data.
  166. */
  167. int CAST3EndDecryptCBC(
  168. CAST3_CTX * context, /* In/out: CAST3 context */
  169. BYTE * outData, /* Out: Final decrypted data */
  170. unsigned int * len /* Out: Length of outData */
  171. );
  172. /* Starts a MAC calculation session using the given IV.
  173. */
  174. int CAST3StartMAC(
  175. CAST3_CTX * context, /* In/out: CAST3 context */
  176. const BYTE * iv /* In: 8-byte CBC IV */
  177. );
  178. /* Updates a MAC calculation session for the supplied data.
  179. */
  180. int CAST3UpdateMAC(
  181. CAST3_CTX * context, /* In/out: CAST3 context */
  182. const BYTE * inData, /* In: Data to calculate MAC on */
  183. unsigned int len /* Input data length, in bytes */
  184. );
  185. /* Ends a MAC calculation session. Upon return, the CAST3_CTX.cbcBuffer array
  186. * contains the MAC. An N-byte MAC is the first N bytes of this array.
  187. */
  188. int CAST3EndMAC(
  189. CAST3_CTX * context /* In/out: CAST3 context */
  190. );
  191. /* Zeroizes the CAST3_CTX so as not to leave sensitive security parameters
  192. * around in memory.
  193. */
  194. void CAST3Cleanup(
  195. CAST3_CTX * context /* Out: CAST3 context to cleanup */
  196. );
  197. /* Runs a known-answer self-test on CAST3. Returns C3E_OK if the test passes
  198. * or C3E_SELFTEST_FAILED if it fails.
  199. */
  200. int CAST3SelfTest( void );
  201. /* Checks the specified key length for a valid value. Returns C3E_OK if it is
  202. * valid or C3E_BAD_KEYLEN if not.
  203. */
  204. int CAST3CheckKeyLen( unsigned int keyNumBits );
  205. }
  206. #endif /* CAST3_H */