cipher_null.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright 2020-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. #include <string.h>
  10. #include <openssl/crypto.h>
  11. #include <openssl/core_dispatch.h>
  12. #include <openssl/proverr.h>
  13. #include "prov/implementations.h"
  14. #include "prov/ciphercommon.h"
  15. #include "prov/providercommon.h"
  16. typedef struct prov_cipher_null_ctx_st {
  17. int enc;
  18. size_t tlsmacsize;
  19. const unsigned char *tlsmac;
  20. } PROV_CIPHER_NULL_CTX;
  21. static OSSL_FUNC_cipher_newctx_fn null_newctx;
  22. static void *null_newctx(void *provctx)
  23. {
  24. if (!ossl_prov_is_running())
  25. return NULL;
  26. return OPENSSL_zalloc(sizeof(PROV_CIPHER_NULL_CTX));
  27. }
  28. static OSSL_FUNC_cipher_freectx_fn null_freectx;
  29. static void null_freectx(void *vctx)
  30. {
  31. OPENSSL_free(vctx);
  32. }
  33. static OSSL_FUNC_cipher_encrypt_init_fn null_einit;
  34. static int null_einit(void *vctx, const unsigned char *key, size_t keylen,
  35. const unsigned char *iv, size_t ivlen,
  36. const OSSL_PARAM params[])
  37. {
  38. PROV_CIPHER_NULL_CTX *ctx = (PROV_CIPHER_NULL_CTX *)vctx;
  39. if (!ossl_prov_is_running())
  40. return 0;
  41. ctx->enc = 1;
  42. return 1;
  43. }
  44. static OSSL_FUNC_cipher_decrypt_init_fn null_dinit;
  45. static int null_dinit(void *vctx, const unsigned char *key, size_t keylen,
  46. const unsigned char *iv, size_t ivlen,
  47. const OSSL_PARAM params[])
  48. {
  49. if (!ossl_prov_is_running())
  50. return 0;
  51. return 1;
  52. }
  53. static OSSL_FUNC_cipher_cipher_fn null_cipher;
  54. static int null_cipher(void *vctx, unsigned char *out, size_t *outl,
  55. size_t outsize, const unsigned char *in, size_t inl)
  56. {
  57. PROV_CIPHER_NULL_CTX *ctx = (PROV_CIPHER_NULL_CTX *)vctx;
  58. if (!ossl_prov_is_running())
  59. return 0;
  60. if (!ctx->enc && ctx->tlsmacsize > 0) {
  61. /*
  62. * TLS NULL cipher as per:
  63. * https://tools.ietf.org/html/rfc5246#section-6.2.3.1
  64. */
  65. if (inl < ctx->tlsmacsize)
  66. return 0;
  67. ctx->tlsmac = in + inl - ctx->tlsmacsize;
  68. inl -= ctx->tlsmacsize;
  69. }
  70. if (outsize < inl)
  71. return 0;
  72. if (in != out)
  73. memcpy(out, in, inl);
  74. *outl = inl;
  75. return 1;
  76. }
  77. static OSSL_FUNC_cipher_final_fn null_final;
  78. static int null_final(void *vctx, unsigned char *out, size_t *outl,
  79. size_t outsize)
  80. {
  81. if (!ossl_prov_is_running())
  82. return 0;
  83. *outl = 0;
  84. return 1;
  85. }
  86. static OSSL_FUNC_cipher_get_params_fn null_get_params;
  87. static int null_get_params(OSSL_PARAM params[])
  88. {
  89. return ossl_cipher_generic_get_params(params, 0, 0, 0, 8, 0);
  90. }
  91. static const OSSL_PARAM null_known_gettable_ctx_params[] = {
  92. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
  93. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
  94. { OSSL_CIPHER_PARAM_TLS_MAC, OSSL_PARAM_OCTET_PTR, NULL, 0, OSSL_PARAM_UNMODIFIED },
  95. OSSL_PARAM_END
  96. };
  97. static OSSL_FUNC_cipher_gettable_ctx_params_fn null_gettable_ctx_params;
  98. static const OSSL_PARAM *null_gettable_ctx_params(ossl_unused void *cctx,
  99. ossl_unused void *provctx)
  100. {
  101. return null_known_gettable_ctx_params;
  102. }
  103. static OSSL_FUNC_cipher_get_ctx_params_fn null_get_ctx_params;
  104. static int null_get_ctx_params(void *vctx, OSSL_PARAM params[])
  105. {
  106. PROV_CIPHER_NULL_CTX *ctx = (PROV_CIPHER_NULL_CTX *)vctx;
  107. OSSL_PARAM *p;
  108. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
  109. if (p != NULL && !OSSL_PARAM_set_size_t(p, 0)) {
  110. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  111. return 0;
  112. }
  113. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
  114. if (p != NULL && !OSSL_PARAM_set_size_t(p, 0)) {
  115. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  116. return 0;
  117. }
  118. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_TLS_MAC);
  119. if (p != NULL
  120. && !OSSL_PARAM_set_octet_ptr(p, ctx->tlsmac, ctx->tlsmacsize)) {
  121. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  122. return 0;
  123. }
  124. return 1;
  125. }
  126. static const OSSL_PARAM null_known_settable_ctx_params[] = {
  127. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_TLS_MAC_SIZE, NULL),
  128. OSSL_PARAM_END
  129. };
  130. static OSSL_FUNC_cipher_settable_ctx_params_fn null_settable_ctx_params;
  131. static const OSSL_PARAM *null_settable_ctx_params(ossl_unused void *cctx,
  132. ossl_unused void *provctx)
  133. {
  134. return null_known_settable_ctx_params;
  135. }
  136. static OSSL_FUNC_cipher_set_ctx_params_fn null_set_ctx_params;
  137. static int null_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  138. {
  139. PROV_CIPHER_NULL_CTX *ctx = (PROV_CIPHER_NULL_CTX *)vctx;
  140. const OSSL_PARAM *p;
  141. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_TLS_MAC_SIZE);
  142. if (p != NULL) {
  143. if (!OSSL_PARAM_get_size_t(p, &ctx->tlsmacsize)) {
  144. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  145. return 0;
  146. }
  147. }
  148. return 1;
  149. }
  150. const OSSL_DISPATCH ossl_null_functions[] = {
  151. { OSSL_FUNC_CIPHER_NEWCTX,
  152. (void (*)(void)) null_newctx },
  153. { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) null_freectx },
  154. { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) null_newctx },
  155. { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))null_einit },
  156. { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))null_dinit },
  157. { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))null_cipher },
  158. { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))null_final },
  159. { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))null_cipher },
  160. { OSSL_FUNC_CIPHER_GET_PARAMS, (void (*)(void)) null_get_params },
  161. { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,
  162. (void (*)(void))ossl_cipher_generic_gettable_params },
  163. { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (void (*)(void))null_get_ctx_params },
  164. { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,
  165. (void (*)(void))null_gettable_ctx_params },
  166. { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, (void (*)(void))null_set_ctx_params },
  167. { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,
  168. (void (*)(void))null_settable_ctx_params },
  169. OSSL_DISPATCH_END
  170. };