cipher_rc4.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 RC4 ciphers */
  10. #include "cipher_rc4.h"
  11. #include "internal/provider_algs.h"
  12. /* TODO (3.0) Figure out what flags are required */
  13. #define RC4_FLAGS EVP_CIPH_FLAG_DEFAULT_ASN1
  14. static OSSL_OP_cipher_freectx_fn rc4_freectx;
  15. static OSSL_OP_cipher_dupctx_fn rc4_dupctx;
  16. static void rc4_freectx(void *vctx)
  17. {
  18. PROV_RC4_CTX *ctx = (PROV_RC4_CTX *)vctx;
  19. OPENSSL_clear_free(ctx, sizeof(*ctx));
  20. }
  21. static void *rc4_dupctx(void *ctx)
  22. {
  23. PROV_RC4_CTX *in = (PROV_RC4_CTX *)ctx;
  24. PROV_RC4_CTX *ret = OPENSSL_malloc(sizeof(*ret));
  25. if (ret == NULL) {
  26. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  27. return NULL;
  28. }
  29. *ret = *in;
  30. return ret;
  31. }
  32. #define IMPLEMENT_cipher(alg, UCALG, flags, kbits, blkbits, ivbits, typ) \
  33. static OSSL_OP_cipher_get_params_fn alg##_##kbits##_get_params; \
  34. static int alg##_##kbits##_get_params(OSSL_PARAM params[]) \
  35. { \
  36. return cipher_generic_get_params(params, 0, flags, \
  37. kbits, blkbits, ivbits); \
  38. } \
  39. static OSSL_OP_cipher_newctx_fn alg##_##kbits##_newctx; \
  40. static void * alg##_##kbits##_newctx(void *provctx) \
  41. { \
  42. PROV_##UCALG##_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); \
  43. if (ctx != NULL) { \
  44. cipher_generic_initkey(ctx, kbits, blkbits, ivbits, 0, flags, \
  45. PROV_CIPHER_HW_##alg(kbits), NULL); \
  46. } \
  47. return ctx; \
  48. } \
  49. const OSSL_DISPATCH alg##kbits##_functions[] = { \
  50. { OSSL_FUNC_CIPHER_NEWCTX, \
  51. (void (*)(void)) alg##_##kbits##_newctx }, \
  52. { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx }, \
  53. { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx }, \
  54. { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))cipher_generic_einit }, \
  55. { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))cipher_generic_dinit }, \
  56. { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))cipher_generic_##typ##_update },\
  57. { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))cipher_generic_##typ##_final }, \
  58. { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))cipher_generic_cipher }, \
  59. { OSSL_FUNC_CIPHER_GET_PARAMS, \
  60. (void (*)(void)) alg##_##kbits##_get_params }, \
  61. { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
  62. (void (*)(void))cipher_generic_get_ctx_params }, \
  63. { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
  64. (void (*)(void))cipher_generic_set_ctx_params }, \
  65. { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
  66. (void (*)(void))cipher_generic_gettable_params }, \
  67. { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
  68. (void (*)(void))cipher_generic_gettable_ctx_params }, \
  69. { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
  70. (void (*)(void))cipher_generic_settable_ctx_params }, \
  71. { 0, NULL } \
  72. };
  73. /* rc440_functions */
  74. IMPLEMENT_cipher(rc4, RC4, EVP_CIPH_VARIABLE_LENGTH, 40, 64, 0, stream)
  75. /* rc4128_functions */
  76. IMPLEMENT_cipher(rc4, RC4, EVP_CIPH_VARIABLE_LENGTH, 128, 64, 0, stream)