cipher_rc5.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. /* Dispatch functions for RC5 cipher modes ecb, cbc, ofb, cfb */
  10. /*
  11. * RC5 low level APIs are deprecated for public use, but still ok for internal
  12. * use.
  13. */
  14. #include "internal/deprecated.h"
  15. #include "cipher_rc5.h"
  16. #include "prov/implementations.h"
  17. #include "prov/providercommonerr.h"
  18. static OSSL_OP_cipher_freectx_fn rc5_freectx;
  19. static OSSL_OP_cipher_dupctx_fn rc5_dupctx;
  20. OSSL_OP_cipher_gettable_ctx_params_fn rc5_gettable_ctx_params;
  21. OSSL_OP_cipher_settable_ctx_params_fn rc5_settable_ctx_params;
  22. static void rc5_freectx(void *vctx)
  23. {
  24. PROV_RC5_CTX *ctx = (PROV_RC5_CTX *)vctx;
  25. OPENSSL_clear_free(ctx, sizeof(*ctx));
  26. }
  27. static void *rc5_dupctx(void *ctx)
  28. {
  29. PROV_RC5_CTX *in = (PROV_RC5_CTX *)ctx;
  30. PROV_RC5_CTX *ret = OPENSSL_malloc(sizeof(*ret));
  31. if (ret == NULL) {
  32. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  33. return NULL;
  34. }
  35. *ret = *in;
  36. return ret;
  37. }
  38. static int rc5_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  39. {
  40. PROV_RC5_CTX *ctx = (PROV_RC5_CTX *)vctx;
  41. const OSSL_PARAM *p;
  42. if (!cipher_var_keylen_set_ctx_params(vctx, params))
  43. return 0;
  44. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_ROUNDS);
  45. if (p != NULL) {
  46. unsigned int rounds;
  47. if (!OSSL_PARAM_get_uint(p, &rounds)) {
  48. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  49. return 0;
  50. }
  51. if (rounds != RC5_8_ROUNDS
  52. && rounds != RC5_12_ROUNDS
  53. && rounds != RC5_16_ROUNDS) {
  54. ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_NUMBER_OF_ROUNDS);
  55. return 0;
  56. }
  57. ctx->rounds = rounds;
  58. }
  59. return 1;
  60. }
  61. CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(rc5)
  62. OSSL_PARAM_uint(OSSL_CIPHER_PARAM_ROUNDS, NULL),
  63. CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(rc5)
  64. CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(rc5)
  65. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
  66. OSSL_PARAM_uint(OSSL_CIPHER_PARAM_ROUNDS, NULL),
  67. CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_END(rc5)
  68. static int rc5_get_ctx_params(void *vctx, OSSL_PARAM params[])
  69. {
  70. PROV_RC5_CTX *ctx = (PROV_RC5_CTX *)vctx;
  71. OSSL_PARAM *p;
  72. if (!cipher_generic_get_ctx_params(vctx, params))
  73. return 0;
  74. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_ROUNDS);
  75. if (p != NULL && !OSSL_PARAM_set_uint(p, ctx->rounds)) {
  76. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  77. return 0;
  78. }
  79. return 1;
  80. }
  81. #define IMPLEMENT_cipher(alg, UCALG, lcmode, UCMODE, flags, kbits, \
  82. blkbits, ivbits, typ) \
  83. static OSSL_OP_cipher_get_params_fn alg##_##kbits##_##lcmode##_get_params; \
  84. static int alg##_##kbits##_##lcmode##_get_params(OSSL_PARAM params[]) \
  85. { \
  86. return cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, flags, \
  87. kbits, blkbits, ivbits); \
  88. } \
  89. static OSSL_OP_cipher_newctx_fn alg##_##kbits##_##lcmode##_newctx; \
  90. static void * alg##_##kbits##_##lcmode##_newctx(void *provctx) \
  91. { \
  92. PROV_##UCALG##_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); \
  93. if (ctx != NULL) { \
  94. cipher_generic_initkey(ctx, kbits, blkbits, ivbits, \
  95. EVP_CIPH_##UCMODE##_MODE, flags, \
  96. PROV_CIPHER_HW_##alg##_##lcmode(kbits), NULL); \
  97. ctx->rounds = RC5_12_ROUNDS; \
  98. } \
  99. return ctx; \
  100. } \
  101. const OSSL_DISPATCH alg##kbits##lcmode##_functions[] = { \
  102. { OSSL_FUNC_CIPHER_NEWCTX, \
  103. (void (*)(void)) alg##_##kbits##_##lcmode##_newctx }, \
  104. { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx }, \
  105. { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx }, \
  106. { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))cipher_generic_einit }, \
  107. { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))cipher_generic_dinit }, \
  108. { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))cipher_generic_##typ##_update },\
  109. { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))cipher_generic_##typ##_final }, \
  110. { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))cipher_generic_cipher }, \
  111. { OSSL_FUNC_CIPHER_GET_PARAMS, \
  112. (void (*)(void)) alg##_##kbits##_##lcmode##_get_params }, \
  113. { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
  114. (void (*)(void))cipher_generic_gettable_params }, \
  115. { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
  116. (void (*)(void))rc5_get_ctx_params }, \
  117. { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
  118. (void (*)(void))rc5_gettable_ctx_params }, \
  119. { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
  120. (void (*)(void))rc5_set_ctx_params }, \
  121. { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
  122. (void (*)(void))rc5_settable_ctx_params }, \
  123. { 0, NULL } \
  124. };
  125. /* rc5128ecb_functions */
  126. IMPLEMENT_cipher(rc5, RC5, ecb, ECB, EVP_CIPH_VARIABLE_LENGTH, 128, 64, 0, block)
  127. /* rc5128cbc_functions */
  128. IMPLEMENT_cipher(rc5, RC5, cbc, CBC, EVP_CIPH_VARIABLE_LENGTH, 128, 64, 64, block)
  129. /* rc5128ofb64_functions */
  130. IMPLEMENT_cipher(rc5, RC5, ofb64, OFB, EVP_CIPH_VARIABLE_LENGTH, 128, 8, 64, stream)
  131. /* rc5128cfb64_functions */
  132. IMPLEMENT_cipher(rc5, RC5, cfb64, CFB, EVP_CIPH_VARIABLE_LENGTH, 128, 8, 64, stream)