ciphercommon.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright 2019 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_numbers.h>
  11. #include <openssl/core_names.h>
  12. #include <openssl/evp.h>
  13. #include "internal/cryptlib.h"
  14. #include "crypto/modes.h"
  15. #include "crypto/ciphermode_platform.h"
  16. #define MAXCHUNK ((size_t)1 << (sizeof(long) * 8 - 2))
  17. #define MAXBITCHUNK ((size_t)1 << (sizeof(size_t) * 8 - 4))
  18. #define GENERIC_BLOCK_SIZE 16
  19. #define IV_STATE_UNINITIALISED 0 /* initial state is not initialized */
  20. #define IV_STATE_BUFFERED 1 /* iv has been copied to the iv buffer */
  21. #define IV_STATE_COPIED 2 /* iv has been copied from the iv buffer */
  22. #define IV_STATE_FINISHED 3 /* the iv has been used - so don't reuse it */
  23. #define PROV_CIPHER_FUNC(type, name, args) typedef type (* OSSL_##name##_fn)args
  24. typedef struct prov_cipher_hw_st PROV_CIPHER_HW;
  25. typedef struct prov_cipher_ctx_st PROV_CIPHER_CTX;
  26. typedef int (PROV_CIPHER_HW_FN)(PROV_CIPHER_CTX *dat, unsigned char *out,
  27. const unsigned char *in, size_t len);
  28. struct prov_cipher_ctx_st {
  29. block128_f block;
  30. union {
  31. cbc128_f cbc;
  32. ctr128_f ctr;
  33. } stream;
  34. unsigned int mode;
  35. size_t keylen; /* key size (in bytes) */
  36. size_t ivlen;
  37. size_t blocksize;
  38. size_t bufsz; /* Number of bytes in buf */
  39. unsigned int pad : 1; /* Whether padding should be used or not */
  40. unsigned int enc : 1; /* Set to 1 for encrypt, or 0 otherwise */
  41. /*
  42. * num contains the number of bytes of |iv| which are valid for modes that
  43. * manage partial blocks themselves.
  44. */
  45. unsigned int num;
  46. uint64_t flags;
  47. /* Buffer of partial blocks processed via update calls */
  48. unsigned char buf[GENERIC_BLOCK_SIZE];
  49. unsigned char iv[GENERIC_BLOCK_SIZE];
  50. const PROV_CIPHER_HW *hw; /* hardware specific functions */
  51. const void *ks; /* Pointer to algorithm specific key data */
  52. OPENSSL_CTX *libctx;
  53. };
  54. struct prov_cipher_hw_st {
  55. int (*init)(PROV_CIPHER_CTX *dat, const uint8_t *key, size_t keylen);
  56. PROV_CIPHER_HW_FN *cipher;
  57. };
  58. OSSL_OP_cipher_encrypt_init_fn cipher_generic_einit;
  59. OSSL_OP_cipher_decrypt_init_fn cipher_generic_dinit;
  60. OSSL_OP_cipher_update_fn cipher_generic_block_update;
  61. OSSL_OP_cipher_final_fn cipher_generic_block_final;
  62. OSSL_OP_cipher_update_fn cipher_generic_stream_update;
  63. OSSL_OP_cipher_final_fn cipher_generic_stream_final;
  64. OSSL_OP_cipher_cipher_fn cipher_generic_cipher;
  65. OSSL_OP_cipher_get_ctx_params_fn cipher_generic_get_ctx_params;
  66. OSSL_OP_cipher_set_ctx_params_fn cipher_generic_set_ctx_params;
  67. OSSL_OP_cipher_gettable_params_fn cipher_generic_gettable_params;
  68. OSSL_OP_cipher_gettable_ctx_params_fn cipher_generic_gettable_ctx_params;
  69. OSSL_OP_cipher_settable_ctx_params_fn cipher_generic_settable_ctx_params;
  70. OSSL_OP_cipher_gettable_ctx_params_fn cipher_aead_gettable_ctx_params;
  71. OSSL_OP_cipher_settable_ctx_params_fn cipher_aead_settable_ctx_params;
  72. int cipher_generic_get_params(OSSL_PARAM params[], unsigned int md,
  73. unsigned long flags,
  74. size_t kbits, size_t blkbits, size_t ivbits);
  75. void cipher_generic_initkey(void *vctx, size_t kbits, size_t blkbits,
  76. size_t ivbits, unsigned int mode, uint64_t flags,
  77. const PROV_CIPHER_HW *hw, void *provctx);
  78. #define IMPLEMENT_generic_cipher(alg, UCALG, lcmode, UCMODE, flags, kbits, \
  79. blkbits, ivbits, typ) \
  80. static OSSL_OP_cipher_get_params_fn alg##_##kbits##_##lcmode##_get_params; \
  81. static int alg##_##kbits##_##lcmode##_get_params(OSSL_PARAM params[]) \
  82. { \
  83. return cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, flags, \
  84. kbits, blkbits, ivbits); \
  85. } \
  86. static OSSL_OP_cipher_newctx_fn alg##_##kbits##_##lcmode##_newctx; \
  87. static void * alg##_##kbits##_##lcmode##_newctx(void *provctx) \
  88. { \
  89. PROV_##UCALG##_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); \
  90. if (ctx != NULL) { \
  91. cipher_generic_initkey(ctx, kbits, blkbits, ivbits, \
  92. EVP_CIPH_##UCMODE##_MODE, flags, \
  93. PROV_CIPHER_HW_##alg##_##lcmode(kbits), NULL); \
  94. } \
  95. return ctx; \
  96. } \
  97. const OSSL_DISPATCH alg##kbits##lcmode##_functions[] = { \
  98. { OSSL_FUNC_CIPHER_NEWCTX, \
  99. (void (*)(void)) alg##_##kbits##_##lcmode##_newctx }, \
  100. { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx }, \
  101. { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx }, \
  102. { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))cipher_generic_einit }, \
  103. { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))cipher_generic_dinit }, \
  104. { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))cipher_generic_##typ##_update },\
  105. { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))cipher_generic_##typ##_final }, \
  106. { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))cipher_generic_cipher }, \
  107. { OSSL_FUNC_CIPHER_GET_PARAMS, \
  108. (void (*)(void)) alg##_##kbits##_##lcmode##_get_params }, \
  109. { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
  110. (void (*)(void))cipher_generic_get_ctx_params }, \
  111. { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
  112. (void (*)(void))cipher_generic_set_ctx_params }, \
  113. { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
  114. (void (*)(void))cipher_generic_gettable_params }, \
  115. { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
  116. (void (*)(void))cipher_generic_gettable_ctx_params }, \
  117. { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
  118. (void (*)(void))cipher_generic_settable_ctx_params }, \
  119. { 0, NULL } \
  120. };
  121. PROV_CIPHER_HW_FN cipher_hw_generic_cbc;
  122. PROV_CIPHER_HW_FN cipher_hw_generic_ecb;
  123. PROV_CIPHER_HW_FN cipher_hw_generic_ofb128;
  124. PROV_CIPHER_HW_FN cipher_hw_generic_cfb128;
  125. PROV_CIPHER_HW_FN cipher_hw_generic_cfb8;
  126. PROV_CIPHER_HW_FN cipher_hw_generic_cfb1;
  127. PROV_CIPHER_HW_FN cipher_hw_generic_ctr;
  128. PROV_CIPHER_HW_FN cipher_hw_chunked_cbc;
  129. PROV_CIPHER_HW_FN cipher_hw_chunked_cfb8;
  130. PROV_CIPHER_HW_FN cipher_hw_chunked_cfb128;
  131. PROV_CIPHER_HW_FN cipher_hw_chunked_ofb128;
  132. #define cipher_hw_chunked_ecb cipher_hw_generic_ecb
  133. #define cipher_hw_chunked_ctr cipher_hw_generic_ctr
  134. #define cipher_hw_chunked_cfb1 cipher_hw_generic_cfb1
  135. #define IMPLEMENT_CIPHER_HW_OFB(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX) \
  136. static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx, \
  137. unsigned char *out, \
  138. const unsigned char *in, size_t len) \
  139. { \
  140. int num = ctx->num; \
  141. KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks); \
  142. \
  143. while (len >= MAXCHUNK) { \
  144. FUNC_PREFIX##_encrypt(in, out, MAXCHUNK, key, ctx->iv, &num); \
  145. len -= MAXCHUNK; \
  146. in += MAXCHUNK; \
  147. out += MAXCHUNK; \
  148. } \
  149. if (len > 0) { \
  150. FUNC_PREFIX##_encrypt(in, out, (long)len, key, ctx->iv, &num); \
  151. } \
  152. ctx->num = num; \
  153. return 1; \
  154. }
  155. #define IMPLEMENT_CIPHER_HW_ECB(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX) \
  156. static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx, \
  157. unsigned char *out, \
  158. const unsigned char *in, size_t len) \
  159. { \
  160. size_t i, bl = ctx->blocksize; \
  161. KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks); \
  162. \
  163. if (len < bl) \
  164. return 1; \
  165. for (i = 0, len -= bl; i <= len; i += bl) \
  166. FUNC_PREFIX##_encrypt(in + i, out + i, key, ctx->enc); \
  167. return 1; \
  168. }
  169. #define IMPLEMENT_CIPHER_HW_CBC(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX) \
  170. static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx, \
  171. unsigned char *out, \
  172. const unsigned char *in, size_t len) \
  173. { \
  174. KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks); \
  175. \
  176. while (len >= MAXCHUNK) { \
  177. FUNC_PREFIX##_encrypt(in, out, MAXCHUNK, key, ctx->iv, ctx->enc); \
  178. len -= MAXCHUNK; \
  179. in += MAXCHUNK; \
  180. out += MAXCHUNK; \
  181. } \
  182. if (len > 0) \
  183. FUNC_PREFIX##_encrypt(in, out, (long)len, key, ctx->iv, ctx->enc); \
  184. return 1; \
  185. }
  186. #define IMPLEMENT_CIPHER_HW_CFB(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX) \
  187. static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx, \
  188. unsigned char *out, \
  189. const unsigned char *in, size_t len) \
  190. { \
  191. size_t chunk = MAXCHUNK; \
  192. KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks); \
  193. int num = ctx->num; \
  194. \
  195. if (len < chunk) \
  196. chunk = len; \
  197. while (len > 0 && len >= chunk) { \
  198. FUNC_PREFIX##_encrypt(in, out, (long)chunk, key, ctx->iv, &num, \
  199. ctx->enc); \
  200. len -= chunk; \
  201. in += chunk; \
  202. out += chunk; \
  203. if (len < chunk) \
  204. chunk = len; \
  205. } \
  206. ctx->num = num; \
  207. return 1; \
  208. }
  209. #define CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(name) \
  210. static const OSSL_PARAM name##_known_gettable_ctx_params[] = { \
  211. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL), \
  212. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL), \
  213. OSSL_PARAM_uint(OSSL_CIPHER_PARAM_PADDING, NULL), \
  214. OSSL_PARAM_uint(OSSL_CIPHER_PARAM_NUM, NULL), \
  215. OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_IV, NULL, 0),
  216. #define CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(name) \
  217. OSSL_PARAM_END \
  218. }; \
  219. const OSSL_PARAM * name##_gettable_ctx_params(void) \
  220. { \
  221. return name##_known_gettable_ctx_params; \
  222. }
  223. #define CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(name) \
  224. static const OSSL_PARAM name##_known_settable_ctx_params[] = { \
  225. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL), \
  226. OSSL_PARAM_uint(OSSL_CIPHER_PARAM_PADDING, NULL), \
  227. OSSL_PARAM_uint(OSSL_CIPHER_PARAM_NUM, NULL),
  228. #define CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_END(name) \
  229. OSSL_PARAM_END \
  230. }; \
  231. const OSSL_PARAM * name##_settable_ctx_params(void) \
  232. { \
  233. return name##_known_settable_ctx_params; \
  234. }
  235. size_t fillblock(unsigned char *buf, size_t *buflen, size_t blocksize,
  236. const unsigned char **in, size_t *inlen);
  237. int trailingdata(unsigned char *buf, size_t *buflen, size_t blocksize,
  238. const unsigned char **in, size_t *inlen);