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.

185 lines
4.8 KiB

10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
  1. /*
  2. * encrypt.h - Define the enryptor's interface
  3. *
  4. * Copyright (C) 2013 - 2015, Max Lv <max.c.lv@gmail.com>
  5. *
  6. * This file is part of the shadowsocks-libev.
  7. *
  8. * shadowsocks-libev is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * shadowsocks-libev is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with shadowsocks-libev; see the file COPYING. If not, see
  20. * <http://www.gnu.org/licenses/>.
  21. */
  22. #ifndef _ENCRYPT_H
  23. #define _ENCRYPT_H
  24. #ifndef __MINGW32__
  25. #include <sys/socket.h>
  26. #else
  27. #ifdef max
  28. #undef max
  29. #endif
  30. #ifdef min
  31. #undef min
  32. #endif
  33. #endif
  34. #include <string.h>
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <stdint.h>
  38. #if defined(USE_CRYPTO_OPENSSL)
  39. #include <openssl/evp.h>
  40. typedef EVP_CIPHER cipher_kt_t;
  41. typedef EVP_CIPHER_CTX cipher_evp_t;
  42. typedef EVP_MD digest_type_t;
  43. #define MAX_KEY_LENGTH EVP_MAX_KEY_LENGTH
  44. #define MAX_IV_LENGTH EVP_MAX_IV_LENGTH
  45. #define MAX_MD_SIZE EVP_MAX_MD_SIZE
  46. #elif defined(USE_CRYPTO_POLARSSL)
  47. #include <polarssl/cipher.h>
  48. #include <polarssl/md.h>
  49. typedef cipher_info_t cipher_kt_t;
  50. typedef cipher_context_t cipher_evp_t;
  51. typedef md_info_t digest_type_t;
  52. #define MAX_KEY_LENGTH 64
  53. #define MAX_IV_LENGTH POLARSSL_MAX_IV_LENGTH
  54. #define MAX_MD_SIZE POLARSSL_MD_MAX_SIZE
  55. #elif defined(USE_CRYPTO_MBEDTLS)
  56. #include <mbedtls/cipher.h>
  57. #include <mbedtls/md.h>
  58. typedef mbedtls_cipher_info_t cipher_kt_t;
  59. typedef mbedtls_cipher_context_t cipher_evp_t;
  60. typedef mbedtls_md_info_t digest_type_t;
  61. #define MAX_KEY_LENGTH 64
  62. #define MAX_IV_LENGTH MBEDTLS_MAX_IV_LENGTH
  63. #define MAX_MD_SIZE MBEDTLS_MD_MAX_SIZE
  64. /* we must have MBEDTLS_CIPHER_MODE_CFB defined */
  65. #if !defined(MBEDTLS_CIPHER_MODE_CFB)
  66. #error Cipher Feedback mode a.k.a CFB not supported by your mbed TLS.
  67. #endif
  68. #endif
  69. #ifdef USE_CRYPTO_APPLECC
  70. #include <CommonCrypto/CommonCrypto.h>
  71. #define kCCAlgorithmInvalid UINT32_MAX
  72. #define kCCContextValid 0
  73. #define kCCContextInvalid -1
  74. typedef struct {
  75. CCCryptorRef cryptor;
  76. int valid;
  77. CCOperation encrypt;
  78. CCAlgorithm cipher;
  79. CCMode mode;
  80. CCPadding padding;
  81. uint8_t iv[MAX_IV_LENGTH];
  82. uint8_t key[MAX_KEY_LENGTH];
  83. size_t iv_len;
  84. size_t key_len;
  85. } cipher_cc_t;
  86. #endif
  87. typedef struct {
  88. cipher_evp_t evp;
  89. #ifdef USE_CRYPTO_APPLECC
  90. cipher_cc_t cc;
  91. #endif
  92. uint8_t iv[MAX_IV_LENGTH];
  93. } cipher_ctx_t;
  94. #ifdef HAVE_STDINT_H
  95. #include <stdint.h>
  96. #elif HAVE_INTTYPES_H
  97. #include <inttypes.h>
  98. #endif
  99. #define SODIUM_BLOCK_SIZE 64
  100. #define CIPHER_NUM 17
  101. #define NONE -1
  102. #define TABLE 0
  103. #define RC4 1
  104. #define RC4_MD5 2
  105. #define AES_128_CFB 3
  106. #define AES_192_CFB 4
  107. #define AES_256_CFB 5
  108. #define BF_CFB 6
  109. #define CAMELLIA_128_CFB 7
  110. #define CAMELLIA_192_CFB 8
  111. #define CAMELLIA_256_CFB 9
  112. #define CAST5_CFB 10
  113. #define DES_CFB 11
  114. #define IDEA_CFB 12
  115. #define RC2_CFB 13
  116. #define SEED_CFB 14
  117. #define SALSA20 15
  118. #define CHACHA20 16
  119. #define ONETIMEAUTH_FLAG 0x10
  120. #define ADDRTYPE_MASK 0xF
  121. #define ONETIMEAUTH_BYTES 10U
  122. #define CLEN_BYTES 2U
  123. #define AUTH_BYTES (ONETIMEAUTH_BYTES + CLEN_BYTES)
  124. #define min(a, b) (((a) < (b)) ? (a) : (b))
  125. #define max(a, b) (((a) > (b)) ? (a) : (b))
  126. struct chunk {
  127. uint32_t idx;
  128. uint32_t len;
  129. uint32_t counter;
  130. char *buf;
  131. };
  132. struct enc_ctx {
  133. uint8_t init;
  134. uint64_t counter;
  135. cipher_ctx_t evp;
  136. };
  137. char * ss_encrypt_all(int buf_size, char *plaintext, ssize_t *len, int method, int auth);
  138. char * ss_decrypt_all(int buf_size, char *ciphertext, ssize_t *len, int method, int auth);
  139. char * ss_encrypt(int buf_size, char *plaintext, ssize_t *len,
  140. struct enc_ctx *ctx);
  141. char * ss_decrypt(int buf_size, char *ciphertext, ssize_t *len,
  142. struct enc_ctx *ctx);
  143. void enc_ctx_init(int method, struct enc_ctx *ctx, int enc);
  144. int enc_init(const char *pass, const char *method);
  145. int enc_get_iv_len(void);
  146. void cipher_context_release(cipher_ctx_t *evp);
  147. unsigned char *enc_md5(const unsigned char *d, size_t n, unsigned char *md);
  148. int ss_onetimeauth(char *auth, char *msg, int msg_len, uint8_t *iv);
  149. int ss_onetimeauth_verify(char *auth, char *msg, int msg_len, uint8_t *iv);
  150. int ss_check_hash(char **buf_ptr, ssize_t *buf_len, struct chunk *chunk, struct enc_ctx *ctx, int buf_size);
  151. char *ss_gen_hash(char *buf, ssize_t *buf_len, uint32_t *counter, struct enc_ctx *ctx, int buf_size);
  152. #endif // _ENCRYPT_H