cipher_aes_cts.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright 2020 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. /* Dispatch functions for AES CBC CTS ciphers */
  10. #include "cipher_aes_cts.h"
  11. #include "prov/providercommonerr.h"
  12. static OSSL_FUNC_cipher_get_ctx_params_fn aes_cbc_cts_get_ctx_params;
  13. static OSSL_FUNC_cipher_set_ctx_params_fn aes_cbc_cts_set_ctx_params;
  14. static OSSL_FUNC_cipher_gettable_ctx_params_fn aes_cbc_cts_gettable_ctx_params;
  15. static OSSL_FUNC_cipher_settable_ctx_params_fn aes_cbc_cts_settable_ctx_params;
  16. CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(aes_cbc_cts)
  17. OSSL_PARAM_utf8_string(OSSL_CIPHER_PARAM_CTS_MODE, NULL, 0),
  18. CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(aes_cbc_cts)
  19. static int aes_cbc_cts_get_ctx_params(void *vctx, OSSL_PARAM params[])
  20. {
  21. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  22. OSSL_PARAM *p;
  23. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_CTS_MODE);
  24. if (p != NULL) {
  25. const char *name = ossl_aes_cbc_cts_mode_id2name(ctx->cts_mode);
  26. if (name == NULL || !OSSL_PARAM_set_utf8_string(p, name)) {
  27. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  28. return 0;
  29. }
  30. }
  31. return ossl_cipher_generic_get_ctx_params(vctx, params);
  32. }
  33. CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(aes_cbc_cts)
  34. OSSL_PARAM_utf8_string(OSSL_CIPHER_PARAM_CTS_MODE, NULL, 0),
  35. CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_END(aes_cbc_cts)
  36. static int aes_cbc_cts_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  37. {
  38. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  39. const OSSL_PARAM *p;
  40. int id;
  41. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_CTS_MODE);
  42. if (p != NULL) {
  43. if (p->data_type != OSSL_PARAM_UTF8_STRING)
  44. goto err;
  45. id = ossl_aes_cbc_cts_mode_name2id(p->data);
  46. if (id < 0)
  47. goto err;
  48. ctx->cts_mode = (unsigned int)id;
  49. }
  50. return ossl_cipher_generic_set_ctx_params(vctx, params);
  51. err:
  52. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  53. return 0;
  54. }
  55. /* NOTE: The underlying block cipher is AES CBC so we reuse most of the code */
  56. #define IMPLEMENT_cts_cipher(alg, UCALG, lcmode, UCMODE, flags, kbits, \
  57. blkbits, ivbits, typ) \
  58. static OSSL_FUNC_cipher_get_params_fn alg##_##kbits##_##lcmode##_get_params; \
  59. static int alg##_cts_##kbits##_##lcmode##_get_params(OSSL_PARAM params[]) \
  60. { \
  61. return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, flags, \
  62. kbits, blkbits, ivbits); \
  63. } \
  64. const OSSL_DISPATCH ossl_##alg##kbits##lcmode##_cts_functions[] = { \
  65. { OSSL_FUNC_CIPHER_NEWCTX, \
  66. (void (*)(void)) alg##_##kbits##_##lcmode##_newctx }, \
  67. { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx }, \
  68. { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx }, \
  69. { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))ossl_cipher_generic_einit }, \
  70. { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))ossl_cipher_generic_dinit }, \
  71. { OSSL_FUNC_CIPHER_UPDATE, \
  72. (void (*)(void)) ossl_##alg##_##lcmode##_cts_block_update }, \
  73. { OSSL_FUNC_CIPHER_FINAL, \
  74. (void (*)(void)) ossl_##alg##_##lcmode##_cts_block_final }, \
  75. { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher }, \
  76. { OSSL_FUNC_CIPHER_GET_PARAMS, \
  77. (void (*)(void)) alg##_cts_##kbits##_##lcmode##_get_params }, \
  78. { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
  79. (void (*)(void))ossl_cipher_generic_gettable_params }, \
  80. { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
  81. (void (*)(void))aes_cbc_cts_get_ctx_params }, \
  82. { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
  83. (void (*)(void))aes_cbc_cts_set_ctx_params }, \
  84. { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
  85. (void (*)(void))aes_cbc_cts_gettable_ctx_params }, \
  86. { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
  87. (void (*)(void))aes_cbc_cts_settable_ctx_params }, \
  88. { 0, NULL } \
  89. };
  90. /* ossl_aes256cbc_cts_functions */
  91. IMPLEMENT_cts_cipher(aes, AES, cbc, CBC, EVP_CIPH_FLAG_CTS, 256, 128, 128, block)
  92. /* ossl_aes192cbc_cts_functions */
  93. IMPLEMENT_cts_cipher(aes, AES, cbc, CBC, EVP_CIPH_FLAG_CTS, 192, 128, 128, block)
  94. /* ossl_aes128cbc_cts_functions */
  95. IMPLEMENT_cts_cipher(aes, AES, cbc, CBC, EVP_CIPH_FLAG_CTS, 128, 128, 128, block)