cmeth_lib.c 5.3 KB

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