ciphercommon.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/params.h>
  10. #include <openssl/core_dispatch.h>
  11. #include <openssl/core_names.h>
  12. #include <openssl/evp.h>
  13. #include "internal/cryptlib.h"
  14. #include "crypto/modes.h"
  15. # define MAXCHUNK ((size_t)1 << 30)
  16. # define MAXBITCHUNK ((size_t)1 << (sizeof(size_t) * 8 - 4))
  17. #define GENERIC_BLOCK_SIZE 16
  18. #define IV_STATE_UNINITIALISED 0 /* initial state is not initialized */
  19. #define IV_STATE_BUFFERED 1 /* iv has been copied to the iv buffer */
  20. #define IV_STATE_COPIED 2 /* iv has been copied from the iv buffer */
  21. #define IV_STATE_FINISHED 3 /* the iv has been used - so don't reuse it */
  22. #define PROV_CIPHER_FUNC(type, name, args) typedef type (* OSSL_##name##_fn)args
  23. typedef struct prov_cipher_hw_st PROV_CIPHER_HW;
  24. typedef struct prov_cipher_ctx_st PROV_CIPHER_CTX;
  25. typedef int (PROV_CIPHER_HW_FN)(PROV_CIPHER_CTX *dat, unsigned char *out,
  26. const unsigned char *in, size_t len);
  27. /* Internal flags that can be queried */
  28. #define PROV_CIPHER_FLAG_AEAD 0x0001
  29. #define PROV_CIPHER_FLAG_CUSTOM_IV 0x0002
  30. #define PROV_CIPHER_FLAG_CTS 0x0004
  31. #define PROV_CIPHER_FLAG_TLS1_MULTIBLOCK 0x0008
  32. #define PROV_CIPHER_FLAG_RAND_KEY 0x0010
  33. /* Internal flags that are only used within the provider */
  34. #define PROV_CIPHER_FLAG_VARIABLE_LENGTH 0x0100
  35. #define PROV_CIPHER_FLAG_INVERSE_CIPHER 0x0200
  36. struct prov_cipher_ctx_st {
  37. /* place buffer at the beginning for memory alignment */
  38. /* The original value of the iv */
  39. unsigned char oiv[GENERIC_BLOCK_SIZE];
  40. /* Buffer of partial blocks processed via update calls */
  41. unsigned char buf[GENERIC_BLOCK_SIZE];
  42. unsigned char iv[GENERIC_BLOCK_SIZE];
  43. block128_f block;
  44. union {
  45. cbc128_f cbc;
  46. ctr128_f ctr;
  47. ecb128_f ecb;
  48. } stream;
  49. unsigned int mode;
  50. size_t keylen; /* key size (in bytes) */
  51. size_t ivlen;
  52. size_t blocksize;
  53. size_t bufsz; /* Number of bytes in buf */
  54. unsigned int cts_mode; /* Use to set the type for CTS modes */
  55. unsigned int pad : 1; /* Whether padding should be used or not */
  56. unsigned int enc : 1; /* Set to 1 for encrypt, or 0 otherwise */
  57. unsigned int iv_set : 1; /* Set when the iv is copied to the iv/oiv buffers */
  58. unsigned int key_set : 1; /* Set when key is set on the context */
  59. unsigned int updated : 1; /* Set to 1 during update for one shot ciphers */
  60. unsigned int variable_keylength : 1;
  61. unsigned int inverse_cipher : 1; /* set to 1 to use inverse cipher */
  62. unsigned int use_bits : 1; /* Set to 0 for cfb1 to use bits instead of bytes */
  63. unsigned int tlsversion; /* If TLS padding is in use the TLS version number */
  64. unsigned char *tlsmac; /* tls MAC extracted from the last record */
  65. int alloced; /*
  66. * Whether the tlsmac data has been allocated or
  67. * points into the user buffer.
  68. */
  69. size_t tlsmacsize; /* Size of the TLS MAC */
  70. int removetlspad; /* Whether TLS padding should be removed or not */
  71. size_t removetlsfixed; /*
  72. * Length of the fixed size data to remove when
  73. * processing TLS data (equals mac size plus
  74. * IV size if applicable)
  75. */
  76. /*
  77. * num contains the number of bytes of |iv| which are valid for modes that
  78. * manage partial blocks themselves.
  79. */
  80. unsigned int num;
  81. const PROV_CIPHER_HW *hw; /* hardware specific functions */
  82. const void *ks; /* Pointer to algorithm specific key data */
  83. OSSL_LIB_CTX *libctx;
  84. };
  85. struct prov_cipher_hw_st {
  86. int (*init)(PROV_CIPHER_CTX *dat, const uint8_t *key, size_t keylen);
  87. PROV_CIPHER_HW_FN *cipher;
  88. void (*copyctx)(PROV_CIPHER_CTX *dst, const PROV_CIPHER_CTX *src);
  89. };
  90. void ossl_cipher_generic_reset_ctx(PROV_CIPHER_CTX *ctx);
  91. OSSL_FUNC_cipher_encrypt_init_fn ossl_cipher_generic_einit;
  92. OSSL_FUNC_cipher_decrypt_init_fn ossl_cipher_generic_dinit;
  93. OSSL_FUNC_cipher_update_fn ossl_cipher_generic_block_update;
  94. OSSL_FUNC_cipher_final_fn ossl_cipher_generic_block_final;
  95. OSSL_FUNC_cipher_update_fn ossl_cipher_generic_stream_update;
  96. OSSL_FUNC_cipher_final_fn ossl_cipher_generic_stream_final;
  97. OSSL_FUNC_cipher_cipher_fn ossl_cipher_generic_cipher;
  98. OSSL_FUNC_cipher_get_ctx_params_fn ossl_cipher_generic_get_ctx_params;
  99. OSSL_FUNC_cipher_set_ctx_params_fn ossl_cipher_generic_set_ctx_params;
  100. OSSL_FUNC_cipher_gettable_params_fn ossl_cipher_generic_gettable_params;
  101. OSSL_FUNC_cipher_gettable_ctx_params_fn ossl_cipher_generic_gettable_ctx_params;
  102. OSSL_FUNC_cipher_settable_ctx_params_fn ossl_cipher_generic_settable_ctx_params;
  103. OSSL_FUNC_cipher_set_ctx_params_fn ossl_cipher_var_keylen_set_ctx_params;
  104. OSSL_FUNC_cipher_settable_ctx_params_fn ossl_cipher_var_keylen_settable_ctx_params;
  105. OSSL_FUNC_cipher_gettable_ctx_params_fn ossl_cipher_aead_gettable_ctx_params;
  106. OSSL_FUNC_cipher_settable_ctx_params_fn ossl_cipher_aead_settable_ctx_params;
  107. int ossl_cipher_generic_get_params(OSSL_PARAM params[], unsigned int md,
  108. uint64_t flags,
  109. size_t kbits, size_t blkbits, size_t ivbits);
  110. void ossl_cipher_generic_initkey(void *vctx, size_t kbits, size_t blkbits,
  111. size_t ivbits, unsigned int mode,
  112. uint64_t flags,
  113. const PROV_CIPHER_HW *hw, void *provctx);
  114. #define IMPLEMENT_generic_cipher_func(alg, UCALG, lcmode, UCMODE, flags, kbits,\
  115. blkbits, ivbits, typ) \
  116. const OSSL_DISPATCH ossl_##alg##kbits##lcmode##_functions[] = { \
  117. { OSSL_FUNC_CIPHER_NEWCTX, \
  118. (void (*)(void)) alg##_##kbits##_##lcmode##_newctx }, \
  119. { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx }, \
  120. { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx }, \
  121. { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))ossl_cipher_generic_einit }, \
  122. { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))ossl_cipher_generic_dinit }, \
  123. { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))ossl_cipher_generic_##typ##_update },\
  124. { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))ossl_cipher_generic_##typ##_final }, \
  125. { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher }, \
  126. { OSSL_FUNC_CIPHER_GET_PARAMS, \
  127. (void (*)(void)) alg##_##kbits##_##lcmode##_get_params }, \
  128. { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
  129. (void (*)(void))ossl_cipher_generic_get_ctx_params }, \
  130. { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
  131. (void (*)(void))ossl_cipher_generic_set_ctx_params }, \
  132. { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
  133. (void (*)(void))ossl_cipher_generic_gettable_params }, \
  134. { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
  135. (void (*)(void))ossl_cipher_generic_gettable_ctx_params }, \
  136. { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
  137. (void (*)(void))ossl_cipher_generic_settable_ctx_params }, \
  138. { 0, NULL } \
  139. };
  140. #define IMPLEMENT_var_keylen_cipher_func(alg, UCALG, lcmode, UCMODE, flags, \
  141. kbits, blkbits, ivbits, typ) \
  142. const OSSL_DISPATCH ossl_##alg##kbits##lcmode##_functions[] = { \
  143. { OSSL_FUNC_CIPHER_NEWCTX, \
  144. (void (*)(void)) alg##_##kbits##_##lcmode##_newctx }, \
  145. { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx }, \
  146. { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx }, \
  147. { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))ossl_cipher_generic_einit },\
  148. { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))ossl_cipher_generic_dinit },\
  149. { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))ossl_cipher_generic_##typ##_update },\
  150. { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))ossl_cipher_generic_##typ##_final }, \
  151. { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher }, \
  152. { OSSL_FUNC_CIPHER_GET_PARAMS, \
  153. (void (*)(void)) alg##_##kbits##_##lcmode##_get_params }, \
  154. { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
  155. (void (*)(void))ossl_cipher_generic_get_ctx_params }, \
  156. { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
  157. (void (*)(void))ossl_cipher_var_keylen_set_ctx_params }, \
  158. { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
  159. (void (*)(void))ossl_cipher_generic_gettable_params }, \
  160. { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
  161. (void (*)(void))ossl_cipher_generic_gettable_ctx_params }, \
  162. { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
  163. (void (*)(void))ossl_cipher_var_keylen_settable_ctx_params }, \
  164. { 0, NULL } \
  165. };
  166. #define IMPLEMENT_generic_cipher_genfn(alg, UCALG, lcmode, UCMODE, flags, \
  167. kbits, blkbits, ivbits, typ) \
  168. static OSSL_FUNC_cipher_get_params_fn alg##_##kbits##_##lcmode##_get_params; \
  169. static int alg##_##kbits##_##lcmode##_get_params(OSSL_PARAM params[]) \
  170. { \
  171. return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
  172. flags, kbits, blkbits, ivbits); \
  173. } \
  174. static OSSL_FUNC_cipher_newctx_fn alg##_##kbits##_##lcmode##_newctx; \
  175. static void * alg##_##kbits##_##lcmode##_newctx(void *provctx) \
  176. { \
  177. PROV_##UCALG##_CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx))\
  178. : NULL; \
  179. if (ctx != NULL) { \
  180. ossl_cipher_generic_initkey(ctx, kbits, blkbits, ivbits, \
  181. EVP_CIPH_##UCMODE##_MODE, flags, \
  182. ossl_prov_cipher_hw_##alg##_##lcmode(kbits),\
  183. provctx); \
  184. } \
  185. return ctx; \
  186. } \
  187. #define IMPLEMENT_generic_cipher(alg, UCALG, lcmode, UCMODE, flags, kbits, \
  188. blkbits, ivbits, typ) \
  189. IMPLEMENT_generic_cipher_genfn(alg, UCALG, lcmode, UCMODE, flags, kbits, \
  190. blkbits, ivbits, typ) \
  191. IMPLEMENT_generic_cipher_func(alg, UCALG, lcmode, UCMODE, flags, kbits, \
  192. blkbits, ivbits, typ)
  193. #define IMPLEMENT_var_keylen_cipher(alg, UCALG, lcmode, UCMODE, flags, kbits, \
  194. blkbits, ivbits, typ) \
  195. IMPLEMENT_generic_cipher_genfn(alg, UCALG, lcmode, UCMODE, flags, kbits, \
  196. blkbits, ivbits, typ) \
  197. IMPLEMENT_var_keylen_cipher_func(alg, UCALG, lcmode, UCMODE, flags, kbits, \
  198. blkbits, ivbits, typ)
  199. PROV_CIPHER_HW_FN ossl_cipher_hw_generic_cbc;
  200. PROV_CIPHER_HW_FN ossl_cipher_hw_generic_ecb;
  201. PROV_CIPHER_HW_FN ossl_cipher_hw_generic_ofb128;
  202. PROV_CIPHER_HW_FN ossl_cipher_hw_generic_cfb128;
  203. PROV_CIPHER_HW_FN ossl_cipher_hw_generic_cfb8;
  204. PROV_CIPHER_HW_FN ossl_cipher_hw_generic_cfb1;
  205. PROV_CIPHER_HW_FN ossl_cipher_hw_generic_ctr;
  206. PROV_CIPHER_HW_FN ossl_cipher_hw_chunked_cbc;
  207. PROV_CIPHER_HW_FN ossl_cipher_hw_chunked_cfb8;
  208. PROV_CIPHER_HW_FN ossl_cipher_hw_chunked_cfb128;
  209. PROV_CIPHER_HW_FN ossl_cipher_hw_chunked_ofb128;
  210. #define ossl_cipher_hw_chunked_ecb ossl_cipher_hw_generic_ecb
  211. #define ossl_cipher_hw_chunked_ctr ossl_cipher_hw_generic_ctr
  212. #define ossl_cipher_hw_chunked_cfb1 ossl_cipher_hw_generic_cfb1
  213. #define IMPLEMENT_CIPHER_HW_OFB(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX) \
  214. static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx, \
  215. unsigned char *out, \
  216. const unsigned char *in, size_t len) \
  217. { \
  218. int num = ctx->num; \
  219. KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks); \
  220. \
  221. while (len >= MAXCHUNK) { \
  222. FUNC_PREFIX##_encrypt(in, out, MAXCHUNK, key, ctx->iv, &num); \
  223. len -= MAXCHUNK; \
  224. in += MAXCHUNK; \
  225. out += MAXCHUNK; \
  226. } \
  227. if (len > 0) { \
  228. FUNC_PREFIX##_encrypt(in, out, (long)len, key, ctx->iv, &num); \
  229. } \
  230. ctx->num = num; \
  231. return 1; \
  232. }
  233. #define IMPLEMENT_CIPHER_HW_ECB(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX) \
  234. static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx, \
  235. unsigned char *out, \
  236. const unsigned char *in, size_t len) \
  237. { \
  238. size_t i, bl = ctx->blocksize; \
  239. KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks); \
  240. \
  241. if (len < bl) \
  242. return 1; \
  243. for (i = 0, len -= bl; i <= len; i += bl) \
  244. FUNC_PREFIX##_encrypt(in + i, out + i, key, ctx->enc); \
  245. return 1; \
  246. }
  247. #define IMPLEMENT_CIPHER_HW_CBC(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX) \
  248. static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx, \
  249. unsigned char *out, \
  250. const unsigned char *in, size_t len) \
  251. { \
  252. KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks); \
  253. \
  254. while (len >= MAXCHUNK) { \
  255. FUNC_PREFIX##_encrypt(in, out, MAXCHUNK, key, ctx->iv, ctx->enc); \
  256. len -= MAXCHUNK; \
  257. in += MAXCHUNK; \
  258. out += MAXCHUNK; \
  259. } \
  260. if (len > 0) \
  261. FUNC_PREFIX##_encrypt(in, out, (long)len, key, ctx->iv, ctx->enc); \
  262. return 1; \
  263. }
  264. #define IMPLEMENT_CIPHER_HW_CFB(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX) \
  265. static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx, \
  266. unsigned char *out, \
  267. const unsigned char *in, size_t len) \
  268. { \
  269. size_t chunk = MAXCHUNK; \
  270. KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks); \
  271. int num = ctx->num; \
  272. \
  273. if (len < chunk) \
  274. chunk = len; \
  275. while (len > 0 && len >= chunk) { \
  276. FUNC_PREFIX##_encrypt(in, out, (long)chunk, key, ctx->iv, &num, \
  277. ctx->enc); \
  278. len -= chunk; \
  279. in += chunk; \
  280. out += chunk; \
  281. if (len < chunk) \
  282. chunk = len; \
  283. } \
  284. ctx->num = num; \
  285. return 1; \
  286. }
  287. #define IMPLEMENT_CIPHER_HW_COPYCTX(name, CTX_TYPE) \
  288. static void name(PROV_CIPHER_CTX *dst, const PROV_CIPHER_CTX *src) \
  289. { \
  290. CTX_TYPE *sctx = (CTX_TYPE *)src; \
  291. CTX_TYPE *dctx = (CTX_TYPE *)dst; \
  292. \
  293. *dctx = *sctx; \
  294. dst->ks = &dctx->ks.ks; \
  295. }
  296. #define CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(name) \
  297. static const OSSL_PARAM name##_known_gettable_ctx_params[] = { \
  298. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL), \
  299. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL), \
  300. OSSL_PARAM_uint(OSSL_CIPHER_PARAM_PADDING, NULL), \
  301. OSSL_PARAM_uint(OSSL_CIPHER_PARAM_NUM, NULL), \
  302. OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_IV, NULL, 0), \
  303. OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_UPDATED_IV, NULL, 0),
  304. #define CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(name) \
  305. OSSL_PARAM_END \
  306. }; \
  307. const OSSL_PARAM * name##_gettable_ctx_params(ossl_unused void *cctx, \
  308. ossl_unused void *provctx) \
  309. { \
  310. return name##_known_gettable_ctx_params; \
  311. }
  312. #define CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(name) \
  313. static const OSSL_PARAM name##_known_settable_ctx_params[] = { \
  314. OSSL_PARAM_uint(OSSL_CIPHER_PARAM_PADDING, NULL), \
  315. OSSL_PARAM_uint(OSSL_CIPHER_PARAM_NUM, NULL),
  316. #define CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_END(name) \
  317. OSSL_PARAM_END \
  318. }; \
  319. const OSSL_PARAM * name##_settable_ctx_params(ossl_unused void *cctx, \
  320. ossl_unused void *provctx) \
  321. { \
  322. return name##_known_settable_ctx_params; \
  323. }
  324. int ossl_cipher_generic_initiv(PROV_CIPHER_CTX *ctx, const unsigned char *iv,
  325. size_t ivlen);
  326. size_t ossl_cipher_fillblock(unsigned char *buf, size_t *buflen,
  327. size_t blocksize,
  328. const unsigned char **in, size_t *inlen);
  329. int ossl_cipher_trailingdata(unsigned char *buf, size_t *buflen,
  330. size_t blocksize,
  331. const unsigned char **in, size_t *inlen);