cipher_rc5.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright 2019-2023 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 <openssl/proverr.h>
  16. #include "cipher_rc5.h"
  17. #include "prov/implementations.h"
  18. #include "prov/providercommon.h"
  19. #define RC5_FLAGS PROV_CIPHER_FLAG_VARIABLE_LENGTH
  20. static OSSL_FUNC_cipher_encrypt_init_fn rc5_einit;
  21. static OSSL_FUNC_cipher_decrypt_init_fn rc5_dinit;
  22. static OSSL_FUNC_cipher_freectx_fn rc5_freectx;
  23. static OSSL_FUNC_cipher_dupctx_fn rc5_dupctx;
  24. OSSL_FUNC_cipher_gettable_ctx_params_fn rc5_gettable_ctx_params;
  25. OSSL_FUNC_cipher_settable_ctx_params_fn rc5_settable_ctx_params;
  26. static OSSL_FUNC_cipher_set_ctx_params_fn rc5_set_ctx_params;
  27. static void rc5_freectx(void *vctx)
  28. {
  29. PROV_RC5_CTX *ctx = (PROV_RC5_CTX *)vctx;
  30. ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
  31. OPENSSL_clear_free(ctx, sizeof(*ctx));
  32. }
  33. static void *rc5_dupctx(void *ctx)
  34. {
  35. PROV_RC5_CTX *in = (PROV_RC5_CTX *)ctx;
  36. PROV_RC5_CTX *ret;
  37. if (!ossl_prov_is_running())
  38. return NULL;
  39. ret = OPENSSL_malloc(sizeof(*ret));
  40. if (ret == NULL)
  41. return NULL;
  42. *ret = *in;
  43. return ret;
  44. }
  45. static int rc5_einit(void *ctx, const unsigned char *key, size_t keylen,
  46. const unsigned char *iv, size_t ivlen,
  47. const OSSL_PARAM params[])
  48. {
  49. if (!ossl_cipher_generic_einit(ctx, key, keylen, iv, ivlen, NULL))
  50. return 0;
  51. return rc5_set_ctx_params(ctx, params);
  52. }
  53. static int rc5_dinit(void *ctx, const unsigned char *key, size_t keylen,
  54. const unsigned char *iv, size_t ivlen,
  55. const OSSL_PARAM params[])
  56. {
  57. if (!ossl_cipher_generic_dinit(ctx, key, keylen, iv, ivlen, NULL))
  58. return 0;
  59. return rc5_set_ctx_params(ctx, params);
  60. }
  61. static int rc5_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  62. {
  63. PROV_RC5_CTX *ctx = (PROV_RC5_CTX *)vctx;
  64. const OSSL_PARAM *p;
  65. if (params == NULL)
  66. return 1;
  67. if (!ossl_cipher_var_keylen_set_ctx_params(vctx, params))
  68. return 0;
  69. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_ROUNDS);
  70. if (p != NULL) {
  71. unsigned int rounds;
  72. if (!OSSL_PARAM_get_uint(p, &rounds)) {
  73. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  74. return 0;
  75. }
  76. if (rounds != RC5_8_ROUNDS
  77. && rounds != RC5_12_ROUNDS
  78. && rounds != RC5_16_ROUNDS) {
  79. ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_NUMBER_OF_ROUNDS);
  80. return 0;
  81. }
  82. ctx->rounds = rounds;
  83. }
  84. return 1;
  85. }
  86. CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(rc5)
  87. OSSL_PARAM_uint(OSSL_CIPHER_PARAM_ROUNDS, NULL),
  88. CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(rc5)
  89. CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(rc5)
  90. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
  91. OSSL_PARAM_uint(OSSL_CIPHER_PARAM_ROUNDS, NULL),
  92. CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_END(rc5)
  93. static int rc5_get_ctx_params(void *vctx, OSSL_PARAM params[])
  94. {
  95. PROV_RC5_CTX *ctx = (PROV_RC5_CTX *)vctx;
  96. OSSL_PARAM *p;
  97. if (!ossl_cipher_generic_get_ctx_params(vctx, params))
  98. return 0;
  99. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_ROUNDS);
  100. if (p != NULL && !OSSL_PARAM_set_uint(p, ctx->rounds)) {
  101. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  102. return 0;
  103. }
  104. return 1;
  105. }
  106. #define IMPLEMENT_cipher(alg, UCALG, lcmode, UCMODE, flags, kbits, \
  107. blkbits, ivbits, typ) \
  108. static OSSL_FUNC_cipher_get_params_fn alg##_##kbits##_##lcmode##_get_params; \
  109. static int alg##_##kbits##_##lcmode##_get_params(OSSL_PARAM params[]) \
  110. { \
  111. return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
  112. flags, kbits, blkbits, ivbits); \
  113. } \
  114. static OSSL_FUNC_cipher_newctx_fn alg##_##kbits##_##lcmode##_newctx; \
  115. static void *alg##_##kbits##_##lcmode##_newctx(void *provctx) \
  116. { \
  117. PROV_##UCALG##_CTX *ctx; \
  118. if (!ossl_prov_is_running()) \
  119. return NULL; \
  120. ctx = OPENSSL_zalloc(sizeof(*ctx)); \
  121. if (ctx != NULL) { \
  122. ossl_cipher_generic_initkey(ctx, kbits, blkbits, ivbits, \
  123. EVP_CIPH_##UCMODE##_MODE, flags, \
  124. ossl_prov_cipher_hw_##alg##_##lcmode(kbits),\
  125. NULL); \
  126. ctx->rounds = RC5_12_ROUNDS; \
  127. } \
  128. return ctx; \
  129. } \
  130. const OSSL_DISPATCH ossl_##alg##kbits##lcmode##_functions[] = { \
  131. { OSSL_FUNC_CIPHER_NEWCTX, \
  132. (void (*)(void)) alg##_##kbits##_##lcmode##_newctx }, \
  133. { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx }, \
  134. { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx }, \
  135. { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))rc5_einit }, \
  136. { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))rc5_dinit }, \
  137. { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))ossl_cipher_generic_##typ##_update },\
  138. { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))ossl_cipher_generic_##typ##_final }, \
  139. { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher }, \
  140. { OSSL_FUNC_CIPHER_GET_PARAMS, \
  141. (void (*)(void)) alg##_##kbits##_##lcmode##_get_params }, \
  142. { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
  143. (void (*)(void))ossl_cipher_generic_gettable_params }, \
  144. { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
  145. (void (*)(void))rc5_get_ctx_params }, \
  146. { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
  147. (void (*)(void))rc5_gettable_ctx_params }, \
  148. { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
  149. (void (*)(void))rc5_set_ctx_params }, \
  150. { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
  151. (void (*)(void))rc5_settable_ctx_params }, \
  152. OSSL_DISPATCH_END \
  153. };
  154. /* ossl_rc5128ecb_functions */
  155. IMPLEMENT_cipher(rc5, RC5, ecb, ECB, RC5_FLAGS, 128, 64, 0, block)
  156. /* ossl_rc5128cbc_functions */
  157. IMPLEMENT_cipher(rc5, RC5, cbc, CBC, RC5_FLAGS, 128, 64, 64, block)
  158. /* ossl_rc5128ofb64_functions */
  159. IMPLEMENT_cipher(rc5, RC5, ofb64, OFB, RC5_FLAGS, 128, 8, 64, stream)
  160. /* ossl_rc5128cfb64_functions */
  161. IMPLEMENT_cipher(rc5, RC5, cfb64, CFB, RC5_FLAGS, 128, 8, 64, stream)