cipher_tdes_common.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright 2019-2024 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. * DES low level APIs are deprecated for public use, but still ok for internal
  11. * use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <openssl/rand.h>
  15. #include <openssl/proverr.h>
  16. #include "prov/ciphercommon.h"
  17. #include "cipher_tdes.h"
  18. #include "prov/implementations.h"
  19. #include "prov/providercommon.h"
  20. void *ossl_tdes_newctx(void *provctx, int mode, size_t kbits, size_t blkbits,
  21. size_t ivbits, uint64_t flags, const PROV_CIPHER_HW *hw)
  22. {
  23. PROV_TDES_CTX *tctx;
  24. if (!ossl_prov_is_running())
  25. return NULL;
  26. tctx = OPENSSL_zalloc(sizeof(*tctx));
  27. if (tctx != NULL) {
  28. OSSL_FIPS_IND_INIT(tctx)
  29. ossl_cipher_generic_initkey(tctx, kbits, blkbits, ivbits, mode, flags,
  30. hw, provctx);
  31. }
  32. return tctx;
  33. }
  34. void *ossl_tdes_dupctx(void *ctx)
  35. {
  36. PROV_TDES_CTX *in = (PROV_TDES_CTX *)ctx;
  37. PROV_TDES_CTX *ret;
  38. if (!ossl_prov_is_running())
  39. return NULL;
  40. ret = OPENSSL_malloc(sizeof(*ret));
  41. if (ret == NULL)
  42. return NULL;
  43. OSSL_FIPS_IND_COPY(ret, in)
  44. in->base.hw->copyctx(&ret->base, &in->base);
  45. return ret;
  46. }
  47. void ossl_tdes_freectx(void *vctx)
  48. {
  49. PROV_TDES_CTX *ctx = (PROV_TDES_CTX *)vctx;
  50. ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
  51. OPENSSL_clear_free(ctx, sizeof(*ctx));
  52. }
  53. #ifdef FIPS_MODULE
  54. static int tdes_encrypt_check_approved(PROV_TDES_CTX *ctx, int enc)
  55. {
  56. /* Triple-DES encryption is not approved in FIPS 140-3 */
  57. if (enc && !OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0,
  58. ctx->base.libctx,
  59. "Triple-DES", "Encryption",
  60. ossl_fips_config_tdes_encrypt_disallowed))
  61. return 0;
  62. return 1;
  63. }
  64. #endif
  65. static int tdes_init(void *vctx, const unsigned char *key, size_t keylen,
  66. const unsigned char *iv, size_t ivlen,
  67. const OSSL_PARAM params[], int enc)
  68. {
  69. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  70. if (!ossl_prov_is_running())
  71. return 0;
  72. ctx->num = 0;
  73. ctx->bufsz = 0;
  74. ctx->enc = enc;
  75. if (iv != NULL) {
  76. if (!ossl_cipher_generic_initiv(ctx, iv, ivlen))
  77. return 0;
  78. } else if (ctx->iv_set
  79. && (ctx->mode == EVP_CIPH_CBC_MODE
  80. || ctx->mode == EVP_CIPH_CFB_MODE
  81. || ctx->mode == EVP_CIPH_OFB_MODE)) {
  82. /* reset IV to keep compatibility with 1.1.1 */
  83. memcpy(ctx->iv, ctx->oiv, ctx->ivlen);
  84. }
  85. if (key != NULL) {
  86. if (keylen != ctx->keylen) {
  87. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  88. return 0;
  89. }
  90. if (!ctx->hw->init(ctx, key, ctx->keylen))
  91. return 0;
  92. ctx->key_set = 1;
  93. }
  94. if (!ossl_tdes_set_ctx_params(ctx, params))
  95. return 0;
  96. #ifdef FIPS_MODULE
  97. if (!tdes_encrypt_check_approved((PROV_TDES_CTX *)ctx, enc))
  98. return 0;
  99. #endif
  100. return 1;
  101. }
  102. int ossl_tdes_einit(void *vctx, const unsigned char *key, size_t keylen,
  103. const unsigned char *iv, size_t ivlen,
  104. const OSSL_PARAM params[])
  105. {
  106. return tdes_init(vctx, key, keylen, iv, ivlen, params, 1);
  107. }
  108. int ossl_tdes_dinit(void *vctx, const unsigned char *key, size_t keylen,
  109. const unsigned char *iv, size_t ivlen,
  110. const OSSL_PARAM params[])
  111. {
  112. return tdes_init(vctx, key, keylen, iv, ivlen, params, 0);
  113. }
  114. CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(ossl_tdes)
  115. OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY, NULL, 0),
  116. OSSL_FIPS_IND_GETTABLE_CTX_PARAM()
  117. CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(ossl_tdes)
  118. static int tdes_generatekey(PROV_CIPHER_CTX *ctx, void *ptr)
  119. {
  120. DES_cblock *deskey = ptr;
  121. size_t kl = ctx->keylen;
  122. if (kl == 0 || RAND_priv_bytes_ex(ctx->libctx, ptr, kl, 0) <= 0)
  123. return 0;
  124. DES_set_odd_parity(deskey);
  125. if (kl >= 16) {
  126. DES_set_odd_parity(deskey + 1);
  127. if (kl >= 24)
  128. DES_set_odd_parity(deskey + 2);
  129. }
  130. return 1;
  131. }
  132. int ossl_tdes_get_ctx_params(void *vctx, OSSL_PARAM params[])
  133. {
  134. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  135. OSSL_PARAM *p;
  136. if (!ossl_cipher_generic_get_ctx_params(vctx, params))
  137. return 0;
  138. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_RANDOM_KEY);
  139. if (p != NULL && !tdes_generatekey(ctx, p->data)) {
  140. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);
  141. return 0;
  142. }
  143. if (!OSSL_FIPS_IND_GET_CTX_PARAM((PROV_TDES_CTX *)vctx, params))
  144. return 0;
  145. return 1;
  146. }
  147. CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(ossl_tdes)
  148. OSSL_PARAM_uint(OSSL_CIPHER_PARAM_PADDING, NULL),
  149. OSSL_PARAM_uint(OSSL_CIPHER_PARAM_NUM, NULL),
  150. OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_CIPHER_PARAM_FIPS_ENCRYPT_CHECK)
  151. CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_END(ossl_tdes)
  152. int ossl_tdes_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  153. {
  154. if (!OSSL_FIPS_IND_SET_CTX_PARAM((PROV_TDES_CTX *)vctx,
  155. OSSL_FIPS_IND_SETTABLE0, params,
  156. OSSL_CIPHER_PARAM_FIPS_ENCRYPT_CHECK))
  157. return 0;
  158. return ossl_cipher_generic_set_ctx_params(vctx, params);
  159. }
  160. int ossl_tdes_get_params(OSSL_PARAM params[], unsigned int md, uint64_t flags,
  161. size_t kbits, size_t blkbits, size_t ivbits)
  162. {
  163. #ifdef FIPS_MODULE
  164. const int decrypt_only = 1;
  165. #else
  166. const int decrypt_only = 0;
  167. #endif
  168. OSSL_PARAM *p;
  169. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_DECRYPT_ONLY);
  170. if (p != NULL && !OSSL_PARAM_set_int(p, decrypt_only)) {
  171. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  172. return 0;
  173. }
  174. return ossl_cipher_generic_get_params(params, md, flags,
  175. kbits, blkbits, ivbits);
  176. }