cipher_rc4.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 RC4 ciphers */
  10. /*
  11. * RC4 low level APIs are deprecated for public use, but still ok for internal
  12. * use.
  13. */
  14. #include "internal/deprecated.h"
  15. #include "cipher_rc4.h"
  16. #include "prov/implementations.h"
  17. #include "prov/providercommon.h"
  18. #define RC4_FLAGS PROV_CIPHER_FLAG_VARIABLE_LENGTH
  19. static OSSL_FUNC_cipher_encrypt_init_fn rc4_einit;
  20. static OSSL_FUNC_cipher_decrypt_init_fn rc4_dinit;
  21. static OSSL_FUNC_cipher_freectx_fn rc4_freectx;
  22. static OSSL_FUNC_cipher_dupctx_fn rc4_dupctx;
  23. static void rc4_freectx(void *vctx)
  24. {
  25. PROV_RC4_CTX *ctx = (PROV_RC4_CTX *)vctx;
  26. ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
  27. OPENSSL_clear_free(ctx, sizeof(*ctx));
  28. }
  29. static void *rc4_dupctx(void *ctx)
  30. {
  31. PROV_RC4_CTX *in = (PROV_RC4_CTX *)ctx;
  32. PROV_RC4_CTX *ret;
  33. if (!ossl_prov_is_running())
  34. return NULL;
  35. ret = OPENSSL_malloc(sizeof(*ret));
  36. if (ret == NULL)
  37. return NULL;
  38. *ret = *in;
  39. return ret;
  40. }
  41. static int rc4_einit(void *ctx, const unsigned char *key, size_t keylen,
  42. const unsigned char *iv, size_t ivlen,
  43. const OSSL_PARAM params[])
  44. {
  45. if (!ossl_cipher_generic_einit(ctx, key, keylen, iv, ivlen, NULL))
  46. return 0;
  47. return ossl_cipher_var_keylen_set_ctx_params(ctx, params);
  48. }
  49. static int rc4_dinit(void *ctx, const unsigned char *key, size_t keylen,
  50. const unsigned char *iv, size_t ivlen,
  51. const OSSL_PARAM params[])
  52. {
  53. if (!ossl_cipher_generic_dinit(ctx, key, keylen, iv, ivlen, NULL))
  54. return 0;
  55. return ossl_cipher_var_keylen_set_ctx_params(ctx, params);
  56. }
  57. #define IMPLEMENT_cipher(alg, UCALG, flags, kbits, blkbits, ivbits, typ) \
  58. static OSSL_FUNC_cipher_get_params_fn alg##_##kbits##_get_params; \
  59. static int alg##_##kbits##_get_params(OSSL_PARAM params[]) \
  60. { \
  61. return ossl_cipher_generic_get_params(params, 0, flags, \
  62. kbits, blkbits, ivbits); \
  63. } \
  64. static OSSL_FUNC_cipher_newctx_fn alg##_##kbits##_newctx; \
  65. static void *alg##_##kbits##_newctx(void *provctx) \
  66. { \
  67. PROV_##UCALG##_CTX *ctx; \
  68. if (!ossl_prov_is_running()) \
  69. return NULL; \
  70. ctx = OPENSSL_zalloc(sizeof(*ctx)); \
  71. if (ctx != NULL) { \
  72. ossl_cipher_generic_initkey(ctx, kbits, blkbits, ivbits, 0, flags, \
  73. ossl_prov_cipher_hw_##alg(kbits), NULL); \
  74. } \
  75. return ctx; \
  76. } \
  77. const OSSL_DISPATCH ossl_##alg##kbits##_functions[] = { \
  78. { OSSL_FUNC_CIPHER_NEWCTX, \
  79. (void (*)(void)) alg##_##kbits##_newctx }, \
  80. { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx }, \
  81. { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx }, \
  82. { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))rc4_einit }, \
  83. { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))rc4_dinit }, \
  84. { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))ossl_cipher_generic_##typ##_update },\
  85. { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))ossl_cipher_generic_##typ##_final }, \
  86. { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher }, \
  87. { OSSL_FUNC_CIPHER_GET_PARAMS, \
  88. (void (*)(void)) alg##_##kbits##_get_params }, \
  89. { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
  90. (void (*)(void))ossl_cipher_generic_get_ctx_params }, \
  91. { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
  92. (void (*)(void))ossl_cipher_var_keylen_set_ctx_params }, \
  93. { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
  94. (void (*)(void))ossl_cipher_generic_gettable_params }, \
  95. { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
  96. (void (*)(void))ossl_cipher_generic_gettable_ctx_params }, \
  97. { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
  98. (void (*)(void))ossl_cipher_var_keylen_settable_ctx_params }, \
  99. OSSL_DISPATCH_END \
  100. };
  101. /* ossl_rc440_functions */
  102. IMPLEMENT_cipher(rc4, RC4, RC4_FLAGS, 40, 8, 0, stream)
  103. /* ossl_rc4128_functions */
  104. IMPLEMENT_cipher(rc4, RC4, RC4_FLAGS, 128, 8, 0, stream)