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.

369 lines
10 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. /*
  2. Copyright (c) 2014 Google Inc.
  3. Copyright (c) 2014, 2019, MariaDB Corporation.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; version 2 of the License.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */
  14. #include <my_global.h>
  15. #include <string.h>
  16. #define template _template /* bug in WolfSSL 4.4.0, see also violite.h */
  17. #include <openssl/evp.h>
  18. #undef template
  19. #include <openssl/aes.h>
  20. #include <openssl/err.h>
  21. #include <openssl/rand.h>
  22. #include <my_crypt.h>
  23. #include <ssl_compat.h>
  24. #include <cstdint>
  25. #define CTX_ALIGN 16
  26. class MyCTX
  27. {
  28. public:
  29. char ctx_buf[EVP_CIPHER_CTX_SIZE + CTX_ALIGN];
  30. EVP_CIPHER_CTX* ctx;
  31. MyCTX()
  32. {
  33. #if CTX_ALIGN > 0
  34. uintptr_t p= ((uintptr_t)ctx_buf + (CTX_ALIGN - 1)) & ~(CTX_ALIGN - 1);
  35. ctx = reinterpret_cast<EVP_CIPHER_CTX*>(p);
  36. #else
  37. ctx = (EVP_CIPHER_CTX*)ctx_buf;
  38. #endif
  39. EVP_CIPHER_CTX_init(ctx);
  40. }
  41. virtual ~MyCTX()
  42. {
  43. EVP_CIPHER_CTX_reset(ctx);
  44. ERR_remove_state(0);
  45. }
  46. virtual int init(const EVP_CIPHER *cipher, int encrypt, const uchar *key,
  47. uint klen, const uchar *iv, uint ivlen)
  48. {
  49. compile_time_assert(MY_AES_CTX_SIZE >= sizeof(MyCTX));
  50. if (unlikely(!cipher))
  51. return MY_AES_BAD_KEYSIZE;
  52. if (EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, encrypt) != 1)
  53. return MY_AES_OPENSSL_ERROR;
  54. DBUG_ASSERT(EVP_CIPHER_CTX_key_length(ctx) == (int)klen);
  55. DBUG_ASSERT(EVP_CIPHER_CTX_iv_length(ctx) <= (int)ivlen);
  56. return MY_AES_OK;
  57. }
  58. virtual int update(const uchar *src, uint slen, uchar *dst, uint *dlen)
  59. {
  60. #ifdef HAVE_WOLFSSL
  61. // WolfSSL checks parameters and does not like NULL pointers to be passed to function below.
  62. if (!src)
  63. {
  64. static uchar dummy[MY_AES_BLOCK_SIZE];
  65. DBUG_ASSERT(!slen);
  66. src=dummy;
  67. }
  68. #endif
  69. if (EVP_CipherUpdate(ctx, dst, (int*)dlen, src, slen) != 1)
  70. return MY_AES_OPENSSL_ERROR;
  71. return MY_AES_OK;
  72. }
  73. virtual int finish(uchar *dst, uint *dlen)
  74. {
  75. if (EVP_CipherFinal_ex(ctx, dst, (int*)dlen) != 1)
  76. return MY_AES_BAD_DATA;
  77. return MY_AES_OK;
  78. }
  79. };
  80. class MyCTX_nopad : public MyCTX
  81. {
  82. public:
  83. const uchar *key;
  84. uint klen, source_tail_len;
  85. uchar oiv[MY_AES_BLOCK_SIZE];
  86. uchar source_tail[MY_AES_BLOCK_SIZE];
  87. MyCTX_nopad() : MyCTX() { }
  88. ~MyCTX_nopad() override = default;
  89. int init(const EVP_CIPHER *cipher, int encrypt, const uchar *key, uint klen,
  90. const uchar *iv, uint ivlen) override
  91. {
  92. compile_time_assert(MY_AES_CTX_SIZE >= sizeof(MyCTX_nopad));
  93. this->key= key;
  94. this->klen= klen;
  95. this->source_tail_len= 0;
  96. if (ivlen)
  97. memcpy(oiv, iv, ivlen);
  98. DBUG_ASSERT(ivlen == 0 || ivlen == sizeof(oiv));
  99. int res= MyCTX::init(cipher, encrypt, key, klen, iv, ivlen);
  100. EVP_CIPHER_CTX_set_padding(ctx, 0);
  101. return res;
  102. }
  103. /** Update last partial source block, stored in source_tail array. */
  104. void update_source_tail(const uchar* src, uint slen)
  105. {
  106. if (!slen)
  107. return;
  108. uint new_tail_len= (source_tail_len + slen) % MY_AES_BLOCK_SIZE;
  109. if (new_tail_len)
  110. {
  111. if (slen + source_tail_len < MY_AES_BLOCK_SIZE)
  112. {
  113. memcpy(source_tail + source_tail_len, src, slen);
  114. }
  115. else
  116. {
  117. DBUG_ASSERT(slen > new_tail_len);
  118. memcpy(source_tail, src + slen - new_tail_len, new_tail_len);
  119. }
  120. }
  121. source_tail_len= new_tail_len;
  122. }
  123. int update(const uchar *src, uint slen, uchar *dst, uint *dlen) override
  124. {
  125. update_source_tail(src, slen);
  126. return MyCTX::update(src, slen, dst, dlen);
  127. }
  128. int finish(uchar *dst, uint *dlen) override
  129. {
  130. if (source_tail_len)
  131. {
  132. /*
  133. Not much we can do, block ciphers cannot encrypt data that aren't
  134. a multiple of the block length. At least not without padding.
  135. Let's do something CTR-like for the last partial block.
  136. */
  137. uchar mask[MY_AES_BLOCK_SIZE];
  138. uint mlen;
  139. int rc= my_aes_crypt(MY_AES_ECB, ENCRYPTION_FLAG_ENCRYPT | ENCRYPTION_FLAG_NOPAD,
  140. oiv, sizeof(mask), mask, &mlen, key, klen, 0, 0);
  141. DBUG_ASSERT(rc == MY_AES_OK);
  142. if (rc)
  143. return rc;
  144. DBUG_ASSERT(mlen == sizeof(mask));
  145. for (uint i=0; i < source_tail_len; i++)
  146. dst[i]= source_tail[i] ^ mask[i];
  147. }
  148. *dlen= source_tail_len;
  149. return MY_AES_OK;
  150. }
  151. };
  152. #define make_aes_dispatcher(mode) \
  153. static inline const EVP_CIPHER *aes_ ## mode(uint klen) \
  154. { \
  155. switch (klen) { \
  156. case 16: return EVP_aes_128_ ## mode(); \
  157. case 24: return EVP_aes_192_ ## mode(); \
  158. case 32: return EVP_aes_256_ ## mode(); \
  159. default: return 0; \
  160. } \
  161. }
  162. make_aes_dispatcher(ecb)
  163. make_aes_dispatcher(cbc)
  164. #ifdef HAVE_EncryptAes128Ctr
  165. make_aes_dispatcher(ctr)
  166. #endif /* HAVE_EncryptAes128Ctr */
  167. #ifdef HAVE_EncryptAes128Gcm
  168. make_aes_dispatcher(gcm)
  169. /*
  170. special implementation for GCM; to fit OpenSSL AES-GCM into the
  171. existing my_aes_* API it does the following:
  172. - IV tail (over 12 bytes) goes to AAD
  173. - the tag is appended to the ciphertext
  174. */
  175. class MyCTX_gcm : public MyCTX
  176. {
  177. public:
  178. const uchar *aad;
  179. int aadlen;
  180. MyCTX_gcm() : MyCTX() { }
  181. ~MyCTX_gcm() override { }
  182. int init(const EVP_CIPHER *cipher, int encrypt, const uchar *key, uint klen,
  183. const uchar *iv, uint ivlen) override
  184. {
  185. compile_time_assert(MY_AES_CTX_SIZE >= sizeof(MyCTX_gcm));
  186. int res= MyCTX::init(cipher, encrypt, key, klen, iv, ivlen);
  187. int real_ivlen= EVP_CIPHER_CTX_iv_length(ctx);
  188. aad= iv + real_ivlen;
  189. aadlen= ivlen - real_ivlen;
  190. return res;
  191. }
  192. int update(const uchar *src, uint slen, uchar *dst, uint *dlen) override
  193. {
  194. /*
  195. note that this GCM class cannot do streaming decryption, because
  196. it needs the tag (which is located at the end of encrypted data)
  197. before decrypting the data. it can encrypt data piecewise, like, first
  198. half, then the second half, but it must decrypt all at once
  199. */
  200. if (!EVP_CIPHER_CTX_encrypting(ctx))
  201. {
  202. /* encrypted string must contain authenticaton tag (see MDEV-11174) */
  203. if (slen < MY_AES_BLOCK_SIZE)
  204. return MY_AES_BAD_DATA;
  205. slen-= MY_AES_BLOCK_SIZE;
  206. if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, MY_AES_BLOCK_SIZE,
  207. (void*)(src + slen)))
  208. return MY_AES_OPENSSL_ERROR;
  209. }
  210. int unused;
  211. if (aadlen && !EVP_CipherUpdate(ctx, NULL, &unused, aad, aadlen))
  212. return MY_AES_OPENSSL_ERROR;
  213. aadlen= 0;
  214. return MyCTX::update(src, slen, dst, dlen);
  215. }
  216. int finish(uchar *dst, uint *dlen) override
  217. {
  218. int fin;
  219. if (!EVP_CipherFinal_ex(ctx, dst, &fin))
  220. return MY_AES_BAD_DATA;
  221. DBUG_ASSERT(fin == 0);
  222. if (EVP_CIPHER_CTX_encrypting(ctx))
  223. {
  224. if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, MY_AES_BLOCK_SIZE, dst))
  225. return MY_AES_OPENSSL_ERROR;
  226. *dlen= MY_AES_BLOCK_SIZE;
  227. }
  228. else
  229. *dlen= 0;
  230. return MY_AES_OK;
  231. }
  232. };
  233. #endif
  234. const EVP_CIPHER *(*ciphers[])(uint)= {
  235. aes_ecb, aes_cbc
  236. #ifdef HAVE_EncryptAes128Ctr
  237. , aes_ctr
  238. #ifdef HAVE_EncryptAes128Gcm
  239. , aes_gcm
  240. #endif
  241. #endif
  242. };
  243. extern "C" {
  244. int my_aes_crypt_init(void *ctx, enum my_aes_mode mode, int flags,
  245. const unsigned char* key, unsigned int klen,
  246. const unsigned char* iv, unsigned int ivlen)
  247. {
  248. #ifdef HAVE_EncryptAes128Ctr
  249. #ifdef HAVE_EncryptAes128Gcm
  250. if (mode == MY_AES_GCM)
  251. if (flags & ENCRYPTION_FLAG_NOPAD)
  252. return MY_AES_OPENSSL_ERROR;
  253. else
  254. new (ctx) MyCTX_gcm();
  255. else
  256. #endif
  257. if (mode == MY_AES_CTR)
  258. new (ctx) MyCTX();
  259. else
  260. #endif
  261. if (flags & ENCRYPTION_FLAG_NOPAD)
  262. new (ctx) MyCTX_nopad();
  263. else
  264. new (ctx) MyCTX();
  265. return ((MyCTX*)ctx)->init(ciphers[mode](klen), flags & 1,
  266. key, klen, iv, ivlen);
  267. }
  268. int my_aes_crypt_update(void *ctx, const uchar *src, uint slen,
  269. uchar *dst, uint *dlen)
  270. {
  271. return ((MyCTX*)ctx)->update(src, slen, dst, dlen);
  272. }
  273. int my_aes_crypt_finish(void *ctx, uchar *dst, uint *dlen)
  274. {
  275. int res= ((MyCTX*)ctx)->finish(dst, dlen);
  276. ((MyCTX*)ctx)->~MyCTX();
  277. return res;
  278. }
  279. int my_aes_crypt(enum my_aes_mode mode, int flags,
  280. const uchar *src, uint slen, uchar *dst, uint *dlen,
  281. const uchar *key, uint klen, const uchar *iv, uint ivlen)
  282. {
  283. void *ctx= alloca(MY_AES_CTX_SIZE);
  284. int res1, res2;
  285. uint d1= 0, d2;
  286. if ((res1= my_aes_crypt_init(ctx, mode, flags, key, klen, iv, ivlen)))
  287. return res1;
  288. res1= my_aes_crypt_update(ctx, src, slen, dst, &d1);
  289. res2= my_aes_crypt_finish(ctx, dst + d1, &d2);
  290. if (res1 || res2)
  291. ERR_remove_state(0); /* in case of failure clear error queue */
  292. else
  293. *dlen= d1 + d2;
  294. return res1 ? res1 : res2;
  295. }
  296. /*
  297. calculate the length of the cyphertext from the length of the plaintext
  298. for different AES encryption modes with padding enabled.
  299. Without padding (ENCRYPTION_FLAG_NOPAD) cyphertext has the same length
  300. as the plaintext
  301. */
  302. unsigned int my_aes_get_size(enum my_aes_mode mode __attribute__((unused)), unsigned int source_length)
  303. {
  304. #ifdef HAVE_EncryptAes128Ctr
  305. if (mode == MY_AES_CTR)
  306. return source_length;
  307. #ifdef HAVE_EncryptAes128Gcm
  308. if (mode == MY_AES_GCM)
  309. return source_length + MY_AES_BLOCK_SIZE;
  310. #endif
  311. #endif
  312. return (source_length / MY_AES_BLOCK_SIZE + 1) * MY_AES_BLOCK_SIZE;
  313. }
  314. unsigned int my_aes_ctx_size(enum my_aes_mode)
  315. {
  316. return MY_AES_CTX_SIZE;
  317. }
  318. int my_random_bytes(uchar *buf, int num)
  319. {
  320. if (RAND_bytes(buf, num) != 1)
  321. return MY_AES_OPENSSL_ERROR;
  322. return MY_AES_OK;
  323. }
  324. }