开源的socket服务端客户端,支持C# C++

crypto.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #pragma once
  2. // -------------------------------------------------- BASE64 -------------------------------------------------- //
  3. // Returns the size of the output. If called with out = NULL, will just return
  4. // the size of what the output would have been (without a terminating NULL).
  5. size_t base64_encode(const BYTE in[], BYTE out[], size_t len, int newline_flag);
  6. // Returns the size of the output. If called with out = NULL, will just return
  7. // the size of what the output would have been (without a terminating NULL).
  8. size_t base64_decode(const BYTE in[], BYTE out[], size_t len);
  9. // -------------------------------------------------- URL -------------------------------------------------- //
  10. int url_encode(const char* src, const int src_size, char* dest, const int dest_size);
  11. int url_decode(const char* src, const int src_size, char* dest, const int dest_size);
  12. // -------------------------------------------------- AES -------------------------------------------------- //
  13. /****************************** MACROS ******************************/
  14. #define AES_BLOCK_SIZE 16 // AES operates on 16 bytes at a time
  15. /**************************** DATA TYPES ****************************/
  16. typedef unsigned char BYTE; // 8-bit byte
  17. typedef unsigned int UINT; // 32-bit word, change to "long" for 16-bit machines
  18. /*********************** FUNCTION DECLARATIONS **********************/
  19. ///////////////////
  20. // AES
  21. ///////////////////
  22. // Key setup must be done before any AES en/de-cryption functions can be used.
  23. void aes_key_setup(const BYTE key[], // The key, must be 128, 192, or 256 bits
  24. UINT w[], // Output key schedule to be used later
  25. int keysize); // Bit length of the key, 128, 192, or 256
  26. void aes_encrypt(const BYTE in[], // 16 bytes of plaintext
  27. BYTE out[], // 16 bytes of ciphertext
  28. const UINT key[], // From the key setup
  29. int keysize); // Bit length of the key, 128, 192, or 256
  30. void aes_decrypt(const BYTE in[], // 16 bytes of ciphertext
  31. BYTE out[], // 16 bytes of plaintext
  32. const UINT key[], // From the key setup
  33. int keysize); // Bit length of the key, 128, 192, or 256
  34. ///////////////////
  35. // AES - CBC
  36. ///////////////////
  37. int aes_encrypt_cbc(const BYTE in[], // Plaintext
  38. size_t in_len, // Must be a multiple of AES_BLOCK_SIZE
  39. BYTE out[], // Ciphertext, same length as plaintext
  40. const UINT key[], // From the key setup
  41. int keysize, // Bit length of the key, 128, 192, or 256
  42. const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long
  43. // Only output the CBC-MAC of the input.
  44. int aes_encrypt_cbc_mac(const BYTE in[], // plaintext
  45. size_t in_len, // Must be a multiple of AES_BLOCK_SIZE
  46. BYTE out[], // Output MAC
  47. const UINT key[], // From the key setup
  48. int keysize, // Bit length of the key, 128, 192, or 256
  49. const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long
  50. ///////////////////
  51. // AES - CTR
  52. ///////////////////
  53. void increment_iv(BYTE iv[], // Must be a multiple of AES_BLOCK_SIZE
  54. int counter_size); // Bytes of the IV used for counting (low end)
  55. void aes_encrypt_ctr(const BYTE in[], // Plaintext
  56. size_t in_len, // Any byte length
  57. BYTE out[], // Ciphertext, same length as plaintext
  58. const UINT key[], // From the key setup
  59. int keysize, // Bit length of the key, 128, 192, or 256
  60. const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long
  61. void aes_decrypt_ctr(const BYTE in[], // Ciphertext
  62. size_t in_len, // Any byte length
  63. BYTE out[], // Plaintext, same length as ciphertext
  64. const UINT key[], // From the key setup
  65. int keysize, // Bit length of the key, 128, 192, or 256
  66. const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long
  67. ///////////////////
  68. // AES - CCM
  69. ///////////////////
  70. // Returns True if the input parameters do not violate any constraint.
  71. int aes_encrypt_ccm(const BYTE plaintext[], // IN - Plaintext.
  72. UINT plaintext_len, // IN - Plaintext length.
  73. const BYTE associated_data[], // IN - Associated Data included in authentication, but not encryption.
  74. unsigned short associated_data_len, // IN - Associated Data length in bytes.
  75. const BYTE nonce[], // IN - The Nonce to be used for encryption.
  76. unsigned short nonce_len, // IN - Nonce length in bytes.
  77. BYTE ciphertext[], // OUT - Ciphertext, a concatination of the plaintext and the MAC.
  78. UINT *ciphertext_len, // OUT - The length of the ciphertext, always plaintext_len + mac_len.
  79. UINT mac_len, // IN - The desired length of the MAC, must be 4, 6, 8, 10, 12, 14, or 16.
  80. const BYTE key[], // IN - The AES key for encryption.
  81. int keysize); // IN - The length of the key in bits. Valid values are 128, 192, 256.
  82. // Returns True if the input parameters do not violate any constraint.
  83. // Use mac_auth to ensure decryption/validation was preformed correctly.
  84. // If authentication does not succeed, the plaintext is zeroed out. To overwride
  85. // this, call with mac_auth = NULL. The proper proceedure is to decrypt with
  86. // authentication enabled (mac_auth != NULL) and make a second call to that
  87. // ignores authentication explicitly if the first call failes.
  88. int aes_decrypt_ccm(const BYTE ciphertext[], // IN - Ciphertext, the concatination of encrypted plaintext and MAC.
  89. UINT ciphertext_len, // IN - Ciphertext length in bytes.
  90. const BYTE assoc[], // IN - The Associated Data, required for authentication.
  91. unsigned short assoc_len, // IN - Associated Data length in bytes.
  92. const BYTE nonce[], // IN - The Nonce to use for decryption, same one as for encryption.
  93. unsigned short nonce_len, // IN - Nonce length in bytes.
  94. BYTE plaintext[], // OUT - The plaintext that was decrypted. Will need to be large enough to hold ciphertext_len - mac_len.
  95. UINT *plaintext_len, // OUT - Length in bytes of the output plaintext, always ciphertext_len - mac_len .
  96. UINT mac_len, // IN - The length of the MAC that was calculated.
  97. int *mac_auth, // OUT - TRUE if authentication succeeded, FALSE if it did not. NULL pointer will ignore the authentication.
  98. const BYTE key[], // IN - The AES key for decryption.
  99. int keysize); // IN - The length of the key in BITS. Valid values are 128, 192, 256.
  100. // -------------------------------------------------- DES -------------------------------------------------- //
  101. /****************************** MACROS ******************************/
  102. #define DES_BLOCK_SIZE 8 // DES operates on 8 bytes at a time
  103. /**************************** DATA TYPES ****************************/
  104. typedef enum {
  105. DES_ENCRYPT,
  106. DES_DECRYPT
  107. } DES_MODE;
  108. /*********************** FUNCTION DECLARATIONS **********************/
  109. void des_key_setup(const BYTE key[], BYTE schedule[][6], DES_MODE mode);
  110. void des_crypt(const BYTE in[], BYTE out[], const BYTE key[][6]);
  111. void three_des_key_setup(const BYTE key[], BYTE schedule[][16][6], DES_MODE mode);
  112. void three_des_crypt(const BYTE in[], BYTE out[], const BYTE key[][16][6]);
  113. // -------------------------------------------------- MD2 -------------------------------------------------- //
  114. /****************************** MACROS ******************************/
  115. #define MD2_BLOCK_SIZE 16
  116. /**************************** DATA TYPES ****************************/
  117. typedef struct {
  118. BYTE data[16];
  119. BYTE state[48];
  120. BYTE checksum[16];
  121. int len;
  122. } _MD2_CTX;
  123. /*********************** FUNCTION DECLARATIONS **********************/
  124. void md2_init(_MD2_CTX *ctx);
  125. void md2_update(_MD2_CTX *ctx, const BYTE data[], size_t len);
  126. void md2_final(_MD2_CTX *ctx, BYTE hash[]); // size of hash must be MD2_BLOCK_SIZE
  127. // -------------------------------------------------- MD5 -------------------------------------------------- //
  128. /****************************** MACROS ******************************/
  129. #define MD5_BLOCK_SIZE 16 // MD5 outputs a 16 byte digest
  130. /**************************** DATA TYPES ****************************/
  131. typedef struct {
  132. BYTE data[64];
  133. UINT datalen;
  134. unsigned long long bitlen;
  135. UINT state[4];
  136. } _MD5_CTX;
  137. /*********************** FUNCTION DECLARATIONS **********************/
  138. void md5_init(_MD5_CTX *ctx);
  139. void md5_update(_MD5_CTX *ctx, const BYTE data[], size_t len);
  140. void md5_final(_MD5_CTX *ctx, BYTE hash[]);
  141. // -------------------------------------------------- SHA1 -------------------------------------------------- //
  142. /****************************** MACROS ******************************/
  143. #define SHA1_BLOCK_SIZE 20 // SHA1 outputs a 20 byte digest
  144. /**************************** DATA TYPES ****************************/
  145. typedef struct {
  146. BYTE data[64];
  147. UINT datalen;
  148. unsigned long long bitlen;
  149. UINT state[5];
  150. UINT k[4];
  151. } _SHA1_CTX;
  152. /*********************** FUNCTION DECLARATIONS **********************/
  153. void sha1_init(_SHA1_CTX *ctx);
  154. void sha1_update(_SHA1_CTX *ctx, const BYTE data[], size_t len);
  155. void sha1_final(_SHA1_CTX *ctx, BYTE hash[]);
  156. // -------------------------------------------------- SHA256 -------------------------------------------------- //
  157. /****************************** MACROS ******************************/
  158. #define SHA256_BLOCK_SIZE 32 // SHA256 outputs a 32 byte digest
  159. /**************************** DATA TYPES ****************************/
  160. typedef struct {
  161. BYTE data[64];
  162. UINT datalen;
  163. unsigned long long bitlen;
  164. UINT state[8];
  165. } _SHA256_CTX;
  166. /*********************** FUNCTION DECLARATIONS **********************/
  167. void sha256_init(_SHA256_CTX *ctx);
  168. void sha256_update(_SHA256_CTX *ctx, const BYTE data[], size_t len);
  169. void sha256_final(_SHA256_CTX *ctx, BYTE hash[]);
  170. // -------------------------------------------------- ARCFOUR -------------------------------------------------- //
  171. /**************************** DATA TYPES ****************************/
  172. /*********************** FUNCTION DECLARATIONS **********************/
  173. // Input: state - the state used to generate the keystream
  174. // key - Key to use to initialize the state
  175. // len - length of key in bytes (valid lenth is 1 to 256)
  176. void arcfour_key_setup(BYTE state[], const BYTE key[], int len);
  177. // Pseudo-Random Generator Algorithm
  178. // Input: state - the state used to generate the keystream
  179. // out - Must be allocated to be of at least "len" length
  180. // len - number of bytes to generate
  181. void arcfour_generate_stream(BYTE state[], BYTE out[], size_t len);
  182. // -------------------------------------------------- BLOWFISH -------------------------------------------------- //
  183. /****************************** MACROS ******************************/
  184. #define BLOWFISH_BLOCK_SIZE 8 // Blowfish operates on 8 bytes at a time
  185. /**************************** DATA TYPES ****************************/
  186. typedef struct {
  187. WORD p[18];
  188. WORD s[4][256];
  189. } _BLOWFISH_KEY;
  190. /*********************** FUNCTION DECLARATIONS **********************/
  191. void blowfish_key_setup(const BYTE user_key[], _BLOWFISH_KEY *keystruct, size_t len);
  192. void blowfish_encrypt(const BYTE in[], BYTE out[], const _BLOWFISH_KEY *keystruct);
  193. void blowfish_decrypt(const BYTE in[], BYTE out[], const _BLOWFISH_KEY *keystruct);
  194. // -------------------------------------------------- ROT-13 -------------------------------------------------- //
  195. /*********************** FUNCTION DECLARATIONS **********************/
  196. // Performs IN PLACE rotation of the input. Assumes input is NULL terminated.
  197. // Preserves each charcter's case. Ignores non alphabetic characters.
  198. void rot13(char str[]);