cmeth_lib.c 5.4 KB

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