cipher_rc4.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright 2019-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 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. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  38. return NULL;
  39. }
  40. *ret = *in;
  41. return ret;
  42. }
  43. static int rc4_einit(void *ctx, const unsigned char *key, size_t keylen,
  44. const unsigned char *iv, size_t ivlen,
  45. const OSSL_PARAM params[])
  46. {
  47. if (!ossl_cipher_generic_einit(ctx, key, keylen, iv, ivlen, NULL))
  48. return 0;
  49. return ossl_cipher_var_keylen_set_ctx_params(ctx, params);
  50. }
  51. static int rc4_dinit(void *ctx, const unsigned char *key, size_t keylen,
  52. const unsigned char *iv, size_t ivlen,
  53. const OSSL_PARAM params[])
  54. {
  55. if (!ossl_cipher_generic_dinit(ctx, key, keylen, iv, ivlen, NULL))
  56. return 0;
  57. return ossl_cipher_var_keylen_set_ctx_params(ctx, params);
  58. }
  59. #define IMPLEMENT_cipher(alg, UCALG, flags, kbits, blkbits, ivbits, typ) \
  60. static OSSL_FUNC_cipher_get_params_fn alg##_##kbits##_get_params; \
  61. static int alg##_##kbits##_get_params(OSSL_PARAM params[]) \
  62. { \
  63. return ossl_cipher_generic_get_params(params, 0, flags, \
  64. kbits, blkbits, ivbits); \
  65. } \
  66. static OSSL_FUNC_cipher_newctx_fn alg##_##kbits##_newctx; \
  67. static void * alg##_##kbits##_newctx(void *provctx) \
  68. { \
  69. PROV_##UCALG##_CTX *ctx; \
  70. if (!ossl_prov_is_running()) \
  71. return NULL; \
  72. ctx = OPENSSL_zalloc(sizeof(*ctx)); \
  73. if (ctx != NULL) { \
  74. ossl_cipher_generic_initkey(ctx, kbits, blkbits, ivbits, 0, flags, \
  75. ossl_prov_cipher_hw_##alg(kbits), NULL); \
  76. } \
  77. return ctx; \
  78. } \
  79. const OSSL_DISPATCH ossl_##alg##kbits##_functions[] = { \
  80. { OSSL_FUNC_CIPHER_NEWCTX, \
  81. (void (*)(void)) alg##_##kbits##_newctx }, \
  82. { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx }, \
  83. { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx }, \
  84. { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))rc4_einit }, \
  85. { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))rc4_dinit }, \
  86. { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))ossl_cipher_generic_##typ##_update },\
  87. { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))ossl_cipher_generic_##typ##_final }, \
  88. { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher }, \
  89. { OSSL_FUNC_CIPHER_GET_PARAMS, \
  90. (void (*)(void)) alg##_##kbits##_get_params }, \
  91. { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
  92. (void (*)(void))ossl_cipher_generic_get_ctx_params }, \
  93. { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
  94. (void (*)(void))ossl_cipher_var_keylen_set_ctx_params }, \
  95. { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
  96. (void (*)(void))ossl_cipher_generic_gettable_params }, \
  97. { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
  98. (void (*)(void))ossl_cipher_generic_gettable_ctx_params }, \
  99. { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
  100. (void (*)(void))ossl_cipher_var_keylen_settable_ctx_params }, \
  101. { 0, NULL } \
  102. };
  103. /* ossl_rc440_functions */
  104. IMPLEMENT_cipher(rc4, RC4, RC4_FLAGS, 40, 8, 0, stream)
  105. /* ossl_rc4128_functions */
  106. IMPLEMENT_cipher(rc4, RC4, RC4_FLAGS, 128, 8, 0, stream)