Crypto.h 12 KB

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