cipher_aes_cts.inc 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright 2020-2021 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 <openssl/proverr.h>
  11. #include "cipher_cts.h"
  12. #define CTS_FLAGS PROV_CIPHER_FLAG_CTS
  13. static OSSL_FUNC_cipher_encrypt_init_fn aes_cbc_cts_einit;
  14. static OSSL_FUNC_cipher_decrypt_init_fn aes_cbc_cts_dinit;
  15. static OSSL_FUNC_cipher_get_ctx_params_fn aes_cbc_cts_get_ctx_params;
  16. static OSSL_FUNC_cipher_set_ctx_params_fn aes_cbc_cts_set_ctx_params;
  17. static OSSL_FUNC_cipher_gettable_ctx_params_fn aes_cbc_cts_gettable_ctx_params;
  18. static OSSL_FUNC_cipher_settable_ctx_params_fn aes_cbc_cts_settable_ctx_params;
  19. CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(aes_cbc_cts)
  20. OSSL_PARAM_utf8_string(OSSL_CIPHER_PARAM_CTS_MODE, NULL, 0),
  21. CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(aes_cbc_cts)
  22. static int aes_cbc_cts_einit(void *ctx, const unsigned char *key, size_t keylen,
  23. const unsigned char *iv, size_t ivlen,
  24. const OSSL_PARAM params[])
  25. {
  26. if (!ossl_cipher_generic_einit(ctx, key, keylen, iv, ivlen, NULL))
  27. return 0;
  28. return aes_cbc_cts_set_ctx_params(ctx, params);
  29. }
  30. static int aes_cbc_cts_dinit(void *ctx, const unsigned char *key, size_t keylen,
  31. const unsigned char *iv, size_t ivlen,
  32. const OSSL_PARAM params[])
  33. {
  34. if (!ossl_cipher_generic_dinit(ctx, key, keylen, iv, ivlen, NULL))
  35. return 0;
  36. return aes_cbc_cts_set_ctx_params(ctx, params);
  37. }
  38. static int aes_cbc_cts_get_ctx_params(void *vctx, OSSL_PARAM params[])
  39. {
  40. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  41. OSSL_PARAM *p;
  42. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_CTS_MODE);
  43. if (p != NULL) {
  44. const char *name = ossl_cipher_cbc_cts_mode_id2name(ctx->cts_mode);
  45. if (name == NULL || !OSSL_PARAM_set_utf8_string(p, name)) {
  46. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  47. return 0;
  48. }
  49. }
  50. return ossl_cipher_generic_get_ctx_params(vctx, params);
  51. }
  52. CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(aes_cbc_cts)
  53. OSSL_PARAM_utf8_string(OSSL_CIPHER_PARAM_CTS_MODE, NULL, 0),
  54. CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_END(aes_cbc_cts)
  55. static int aes_cbc_cts_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  56. {
  57. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  58. const OSSL_PARAM *p;
  59. int id;
  60. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_CTS_MODE);
  61. if (p != NULL) {
  62. if (p->data_type != OSSL_PARAM_UTF8_STRING)
  63. goto err;
  64. id = ossl_cipher_cbc_cts_mode_name2id(p->data);
  65. if (id < 0)
  66. goto err;
  67. ctx->cts_mode = (unsigned int)id;
  68. }
  69. return ossl_cipher_generic_set_ctx_params(vctx, params);
  70. err:
  71. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  72. return 0;
  73. }
  74. /* ossl_aes256cbc_cts_functions */
  75. IMPLEMENT_cts_cipher(aes, AES, cbc, CBC, CTS_FLAGS, 256, 128, 128, block)
  76. /* ossl_aes192cbc_cts_functions */
  77. IMPLEMENT_cts_cipher(aes, AES, cbc, CBC, CTS_FLAGS, 192, 128, 128, block)
  78. /* ossl_aes128cbc_cts_functions */
  79. IMPLEMENT_cts_cipher(aes, AES, cbc, CBC, CTS_FLAGS, 128, 128, 128, block)