cmeth_lib.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. #include <string.h>
  10. #include <openssl/evp.h>
  11. #include "internal/evp_int.h"
  12. #include "evp_locl.h"
  13. EVP_CIPHER *EVP_CIPHER_meth_new(int cipher_type, int block_size, int key_len)
  14. {
  15. EVP_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_CIPHER));
  16. if (cipher != NULL) {
  17. cipher->nid = cipher_type;
  18. cipher->block_size = block_size;
  19. cipher->key_len = key_len;
  20. }
  21. return cipher;
  22. }
  23. EVP_CIPHER *EVP_CIPHER_meth_dup(const EVP_CIPHER *cipher)
  24. {
  25. EVP_CIPHER *to = EVP_CIPHER_meth_new(cipher->nid, cipher->block_size,
  26. cipher->key_len);
  27. if (to != NULL)
  28. memcpy(to, cipher, sizeof(*to));
  29. return to;
  30. }
  31. void EVP_CIPHER_meth_free(EVP_CIPHER *cipher)
  32. {
  33. OPENSSL_free(cipher);
  34. }
  35. int EVP_CIPHER_meth_set_iv_length(EVP_CIPHER *cipher, int iv_len)
  36. {
  37. cipher->iv_len = iv_len;
  38. return 1;
  39. }
  40. int EVP_CIPHER_meth_set_flags(EVP_CIPHER *cipher, unsigned long flags)
  41. {
  42. cipher->flags = flags;
  43. return 1;
  44. }
  45. int EVP_CIPHER_meth_set_impl_ctx_size(EVP_CIPHER *cipher, int ctx_size)
  46. {
  47. cipher->ctx_size = ctx_size;
  48. return 1;
  49. }
  50. int EVP_CIPHER_meth_set_init(EVP_CIPHER *cipher,
  51. int (*init) (EVP_CIPHER_CTX *ctx,
  52. const unsigned char *key,
  53. const unsigned char *iv,
  54. int enc))
  55. {
  56. cipher->init = init;
  57. return 1;
  58. }
  59. int EVP_CIPHER_meth_set_do_cipher(EVP_CIPHER *cipher,
  60. int (*do_cipher) (EVP_CIPHER_CTX *ctx,
  61. unsigned char *out,
  62. const unsigned char *in,
  63. size_t inl))
  64. {
  65. cipher->do_cipher = do_cipher;
  66. return 1;
  67. }
  68. int EVP_CIPHER_meth_set_cleanup(EVP_CIPHER *cipher,
  69. int (*cleanup) (EVP_CIPHER_CTX *))
  70. {
  71. cipher->cleanup = cleanup;
  72. return 1;
  73. }
  74. int EVP_CIPHER_meth_set_set_asn1_params(EVP_CIPHER *cipher,
  75. int (*set_asn1_parameters) (EVP_CIPHER_CTX *,
  76. ASN1_TYPE *))
  77. {
  78. cipher->set_asn1_parameters = set_asn1_parameters;
  79. return 1;
  80. }
  81. int EVP_CIPHER_meth_set_get_asn1_params(EVP_CIPHER *cipher,
  82. int (*get_asn1_parameters) (EVP_CIPHER_CTX *,
  83. ASN1_TYPE *))
  84. {
  85. cipher->get_asn1_parameters = get_asn1_parameters;
  86. return 1;
  87. }
  88. int EVP_CIPHER_meth_set_ctrl(EVP_CIPHER *cipher,
  89. int (*ctrl) (EVP_CIPHER_CTX *, int type,
  90. int arg, void *ptr))
  91. {
  92. cipher->ctrl = ctrl;
  93. return 1;
  94. }
  95. int (*EVP_CIPHER_meth_get_init(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *ctx,
  96. const unsigned char *key,
  97. const unsigned char *iv,
  98. int enc)
  99. {
  100. return cipher->init;
  101. }
  102. int (*EVP_CIPHER_meth_get_do_cipher(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *ctx,
  103. unsigned char *out,
  104. const unsigned char *in,
  105. size_t inl)
  106. {
  107. return cipher->do_cipher;
  108. }
  109. int (*EVP_CIPHER_meth_get_cleanup(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *)
  110. {
  111. return cipher->cleanup;
  112. }
  113. int (*EVP_CIPHER_meth_get_set_asn1_params(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
  114. ASN1_TYPE *)
  115. {
  116. return cipher->set_asn1_parameters;
  117. }
  118. int (*EVP_CIPHER_meth_get_get_asn1_params(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
  119. ASN1_TYPE *)
  120. {
  121. return cipher->get_asn1_parameters;
  122. }
  123. int (*EVP_CIPHER_meth_get_ctrl(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
  124. int type, int arg,
  125. void *ptr)
  126. {
  127. return cipher->ctrl;
  128. }