cipher_tdes_common.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright 2019-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. /*
  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_cipher_generic_initkey(tctx, kbits, blkbits, ivbits, mode, flags,
  29. hw, provctx);
  30. return tctx;
  31. }
  32. void *ossl_tdes_dupctx(void *ctx)
  33. {
  34. PROV_TDES_CTX *in = (PROV_TDES_CTX *)ctx;
  35. PROV_TDES_CTX *ret;
  36. if (!ossl_prov_is_running())
  37. return NULL;
  38. ret = OPENSSL_malloc(sizeof(*ret));
  39. if (ret == NULL)
  40. return NULL;
  41. in->base.hw->copyctx(&ret->base, &in->base);
  42. return ret;
  43. }
  44. void ossl_tdes_freectx(void *vctx)
  45. {
  46. PROV_TDES_CTX *ctx = (PROV_TDES_CTX *)vctx;
  47. ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
  48. OPENSSL_clear_free(ctx, sizeof(*ctx));
  49. }
  50. static int tdes_init(void *vctx, const unsigned char *key, size_t keylen,
  51. const unsigned char *iv, size_t ivlen,
  52. const OSSL_PARAM params[], int enc)
  53. {
  54. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  55. if (!ossl_prov_is_running())
  56. return 0;
  57. ctx->num = 0;
  58. ctx->bufsz = 0;
  59. ctx->enc = enc;
  60. if (iv != NULL) {
  61. if (!ossl_cipher_generic_initiv(ctx, iv, ivlen))
  62. return 0;
  63. } else if (ctx->iv_set
  64. && (ctx->mode == EVP_CIPH_CBC_MODE
  65. || ctx->mode == EVP_CIPH_CFB_MODE
  66. || ctx->mode == EVP_CIPH_OFB_MODE)) {
  67. /* reset IV to keep compatibility with 1.1.1 */
  68. memcpy(ctx->iv, ctx->oiv, ctx->ivlen);
  69. }
  70. if (key != NULL) {
  71. if (keylen != ctx->keylen) {
  72. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  73. return 0;
  74. }
  75. if (!ctx->hw->init(ctx, key, ctx->keylen))
  76. return 0;
  77. ctx->key_set = 1;
  78. }
  79. return ossl_cipher_generic_set_ctx_params(ctx, params);
  80. }
  81. int ossl_tdes_einit(void *vctx, const unsigned char *key, size_t keylen,
  82. const unsigned char *iv, size_t ivlen,
  83. const OSSL_PARAM params[])
  84. {
  85. return tdes_init(vctx, key, keylen, iv, ivlen, params, 1);
  86. }
  87. int ossl_tdes_dinit(void *vctx, const unsigned char *key, size_t keylen,
  88. const unsigned char *iv, size_t ivlen,
  89. const OSSL_PARAM params[])
  90. {
  91. return tdes_init(vctx, key, keylen, iv, ivlen, params, 0);
  92. }
  93. CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(ossl_tdes)
  94. OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY, NULL, 0),
  95. CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(ossl_tdes)
  96. static int tdes_generatekey(PROV_CIPHER_CTX *ctx, void *ptr)
  97. {
  98. DES_cblock *deskey = ptr;
  99. size_t kl = ctx->keylen;
  100. if (kl == 0 || RAND_priv_bytes_ex(ctx->libctx, ptr, kl, 0) <= 0)
  101. return 0;
  102. DES_set_odd_parity(deskey);
  103. if (kl >= 16) {
  104. DES_set_odd_parity(deskey + 1);
  105. if (kl >= 24)
  106. DES_set_odd_parity(deskey + 2);
  107. }
  108. return 1;
  109. }
  110. int ossl_tdes_get_ctx_params(void *vctx, OSSL_PARAM params[])
  111. {
  112. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  113. OSSL_PARAM *p;
  114. if (!ossl_cipher_generic_get_ctx_params(vctx, params))
  115. return 0;
  116. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_RANDOM_KEY);
  117. if (p != NULL && !tdes_generatekey(ctx, p->data)) {
  118. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);
  119. return 0;
  120. }
  121. return 1;
  122. }