cmeth_lib.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright 2015-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. /*
  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_RWLOCK *lock = to->lock;
  42. memcpy(to, cipher, sizeof(*to));
  43. to->lock = lock;
  44. }
  45. return to;
  46. }
  47. void EVP_CIPHER_meth_free(EVP_CIPHER *cipher)
  48. {
  49. if (cipher == NULL || cipher->origin != EVP_ORIG_METH)
  50. return;
  51. evp_cipher_free_int(cipher);
  52. }
  53. int EVP_CIPHER_meth_set_iv_length(EVP_CIPHER *cipher, int iv_len)
  54. {
  55. if (cipher->iv_len != 0)
  56. return 0;
  57. cipher->iv_len = iv_len;
  58. return 1;
  59. }
  60. int EVP_CIPHER_meth_set_flags(EVP_CIPHER *cipher, unsigned long flags)
  61. {
  62. if (cipher->flags != 0)
  63. return 0;
  64. cipher->flags = flags;
  65. return 1;
  66. }
  67. int EVP_CIPHER_meth_set_impl_ctx_size(EVP_CIPHER *cipher, int ctx_size)
  68. {
  69. if (cipher->ctx_size != 0)
  70. return 0;
  71. cipher->ctx_size = ctx_size;
  72. return 1;
  73. }
  74. int EVP_CIPHER_meth_set_init(EVP_CIPHER *cipher,
  75. int (*init) (EVP_CIPHER_CTX *ctx,
  76. const unsigned char *key,
  77. const unsigned char *iv,
  78. int enc))
  79. {
  80. if (cipher->init != NULL)
  81. return 0;
  82. cipher->init = init;
  83. return 1;
  84. }
  85. int EVP_CIPHER_meth_set_do_cipher(EVP_CIPHER *cipher,
  86. int (*do_cipher) (EVP_CIPHER_CTX *ctx,
  87. unsigned char *out,
  88. const unsigned char *in,
  89. size_t inl))
  90. {
  91. if (cipher->do_cipher != NULL)
  92. return 0;
  93. cipher->do_cipher = do_cipher;
  94. return 1;
  95. }
  96. int EVP_CIPHER_meth_set_cleanup(EVP_CIPHER *cipher,
  97. int (*cleanup) (EVP_CIPHER_CTX *))
  98. {
  99. if (cipher->cleanup != NULL)
  100. return 0;
  101. cipher->cleanup = cleanup;
  102. return 1;
  103. }
  104. int EVP_CIPHER_meth_set_set_asn1_params(EVP_CIPHER *cipher,
  105. int (*set_asn1_parameters) (EVP_CIPHER_CTX *,
  106. ASN1_TYPE *))
  107. {
  108. if (cipher->set_asn1_parameters != NULL)
  109. return 0;
  110. cipher->set_asn1_parameters = set_asn1_parameters;
  111. return 1;
  112. }
  113. int EVP_CIPHER_meth_set_get_asn1_params(EVP_CIPHER *cipher,
  114. int (*get_asn1_parameters) (EVP_CIPHER_CTX *,
  115. ASN1_TYPE *))
  116. {
  117. if (cipher->get_asn1_parameters != NULL)
  118. return 0;
  119. cipher->get_asn1_parameters = get_asn1_parameters;
  120. return 1;
  121. }
  122. int EVP_CIPHER_meth_set_ctrl(EVP_CIPHER *cipher,
  123. int (*ctrl) (EVP_CIPHER_CTX *, int type,
  124. int arg, void *ptr))
  125. {
  126. if (cipher->ctrl != NULL)
  127. return 0;
  128. cipher->ctrl = ctrl;
  129. return 1;
  130. }
  131. int (*EVP_CIPHER_meth_get_init(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *ctx,
  132. const unsigned char *key,
  133. const unsigned char *iv,
  134. int enc)
  135. {
  136. return cipher->init;
  137. }
  138. int (*EVP_CIPHER_meth_get_do_cipher(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *ctx,
  139. unsigned char *out,
  140. const unsigned char *in,
  141. size_t inl)
  142. {
  143. return cipher->do_cipher;
  144. }
  145. int (*EVP_CIPHER_meth_get_cleanup(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *)
  146. {
  147. return cipher->cleanup;
  148. }
  149. int (*EVP_CIPHER_meth_get_set_asn1_params(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
  150. ASN1_TYPE *)
  151. {
  152. return cipher->set_asn1_parameters;
  153. }
  154. int (*EVP_CIPHER_meth_get_get_asn1_params(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
  155. ASN1_TYPE *)
  156. {
  157. return cipher->get_asn1_parameters;
  158. }
  159. int (*EVP_CIPHER_meth_get_ctrl(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
  160. int type, int arg,
  161. void *ptr)
  162. {
  163. return cipher->ctrl;
  164. }