2
0

cmeth_lib.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright 2015-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. /*
  10. * EVP _meth_ APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <string.h>
  15. #include <openssl/evp.h>
  16. #include "crypto/evp.h"
  17. #include "internal/provider.h"
  18. #include "evp_local.h"
  19. EVP_CIPHER *EVP_CIPHER_meth_new(int cipher_type, int block_size, int key_len)
  20. {
  21. EVP_CIPHER *cipher = evp_cipher_new();
  22. if (cipher != NULL) {
  23. cipher->nid = cipher_type;
  24. cipher->block_size = block_size;
  25. cipher->key_len = key_len;
  26. cipher->origin = EVP_ORIG_METH;
  27. }
  28. return cipher;
  29. }
  30. EVP_CIPHER *EVP_CIPHER_meth_dup(const EVP_CIPHER *cipher)
  31. {
  32. EVP_CIPHER *to = NULL;
  33. /*
  34. * Non-legacy EVP_CIPHERs can't be duplicated like this.
  35. * Use EVP_CIPHER_up_ref() instead.
  36. */
  37. if (cipher->prov != NULL)
  38. return NULL;
  39. if ((to = EVP_CIPHER_meth_new(cipher->nid, cipher->block_size,
  40. cipher->key_len)) != NULL) {
  41. CRYPTO_REF_COUNT refcnt = to->refcnt;
  42. memcpy(to, cipher, sizeof(*to));
  43. to->refcnt = refcnt;
  44. to->origin = EVP_ORIG_METH;
  45. }
  46. return to;
  47. }
  48. void EVP_CIPHER_meth_free(EVP_CIPHER *cipher)
  49. {
  50. if (cipher == NULL || cipher->origin != EVP_ORIG_METH)
  51. return;
  52. evp_cipher_free_int(cipher);
  53. }
  54. int EVP_CIPHER_meth_set_iv_length(EVP_CIPHER *cipher, int iv_len)
  55. {
  56. if (cipher->iv_len != 0)
  57. return 0;
  58. cipher->iv_len = iv_len;
  59. return 1;
  60. }
  61. int EVP_CIPHER_meth_set_flags(EVP_CIPHER *cipher, unsigned long flags)
  62. {
  63. if (cipher->flags != 0)
  64. return 0;
  65. cipher->flags = flags;
  66. return 1;
  67. }
  68. int EVP_CIPHER_meth_set_impl_ctx_size(EVP_CIPHER *cipher, int ctx_size)
  69. {
  70. if (cipher->ctx_size != 0)
  71. return 0;
  72. cipher->ctx_size = ctx_size;
  73. return 1;
  74. }
  75. int EVP_CIPHER_meth_set_init(EVP_CIPHER *cipher,
  76. int (*init) (EVP_CIPHER_CTX *ctx,
  77. const unsigned char *key,
  78. const unsigned char *iv,
  79. int enc))
  80. {
  81. if (cipher->init != NULL)
  82. return 0;
  83. cipher->init = init;
  84. return 1;
  85. }
  86. int EVP_CIPHER_meth_set_do_cipher(EVP_CIPHER *cipher,
  87. int (*do_cipher) (EVP_CIPHER_CTX *ctx,
  88. unsigned char *out,
  89. const unsigned char *in,
  90. size_t inl))
  91. {
  92. if (cipher->do_cipher != NULL)
  93. return 0;
  94. cipher->do_cipher = do_cipher;
  95. return 1;
  96. }
  97. int EVP_CIPHER_meth_set_cleanup(EVP_CIPHER *cipher,
  98. int (*cleanup) (EVP_CIPHER_CTX *))
  99. {
  100. if (cipher->cleanup != NULL)
  101. return 0;
  102. cipher->cleanup = cleanup;
  103. return 1;
  104. }
  105. int EVP_CIPHER_meth_set_set_asn1_params(EVP_CIPHER *cipher,
  106. int (*set_asn1_parameters) (EVP_CIPHER_CTX *,
  107. ASN1_TYPE *))
  108. {
  109. if (cipher->set_asn1_parameters != NULL)
  110. return 0;
  111. cipher->set_asn1_parameters = set_asn1_parameters;
  112. return 1;
  113. }
  114. int EVP_CIPHER_meth_set_get_asn1_params(EVP_CIPHER *cipher,
  115. int (*get_asn1_parameters) (EVP_CIPHER_CTX *,
  116. ASN1_TYPE *))
  117. {
  118. if (cipher->get_asn1_parameters != NULL)
  119. return 0;
  120. cipher->get_asn1_parameters = get_asn1_parameters;
  121. return 1;
  122. }
  123. int EVP_CIPHER_meth_set_ctrl(EVP_CIPHER *cipher,
  124. int (*ctrl) (EVP_CIPHER_CTX *, int type,
  125. int arg, void *ptr))
  126. {
  127. if (cipher->ctrl != NULL)
  128. return 0;
  129. cipher->ctrl = ctrl;
  130. return 1;
  131. }
  132. int (*EVP_CIPHER_meth_get_init(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *ctx,
  133. const unsigned char *key,
  134. const unsigned char *iv,
  135. int enc)
  136. {
  137. return cipher->init;
  138. }
  139. int (*EVP_CIPHER_meth_get_do_cipher(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *ctx,
  140. unsigned char *out,
  141. const unsigned char *in,
  142. size_t inl)
  143. {
  144. return cipher->do_cipher;
  145. }
  146. int (*EVP_CIPHER_meth_get_cleanup(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *)
  147. {
  148. return cipher->cleanup;
  149. }
  150. int (*EVP_CIPHER_meth_get_set_asn1_params(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
  151. ASN1_TYPE *)
  152. {
  153. return cipher->set_asn1_parameters;
  154. }
  155. int (*EVP_CIPHER_meth_get_get_asn1_params(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
  156. ASN1_TYPE *)
  157. {
  158. return cipher->get_asn1_parameters;
  159. }
  160. int (*EVP_CIPHER_meth_get_ctrl(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
  161. int type, int arg,
  162. void *ptr)
  163. {
  164. return cipher->ctrl;
  165. }