cipher_tdes_common.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright 2019-2021 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. }
  78. return ossl_cipher_generic_set_ctx_params(ctx, params);
  79. }
  80. int ossl_tdes_einit(void *vctx, const unsigned char *key, size_t keylen,
  81. const unsigned char *iv, size_t ivlen,
  82. const OSSL_PARAM params[])
  83. {
  84. return tdes_init(vctx, key, keylen, iv, ivlen, params, 1);
  85. }
  86. int ossl_tdes_dinit(void *vctx, const unsigned char *key, size_t keylen,
  87. const unsigned char *iv, size_t ivlen,
  88. const OSSL_PARAM params[])
  89. {
  90. return tdes_init(vctx, key, keylen, iv, ivlen, params, 0);
  91. }
  92. CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(ossl_tdes)
  93. OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY, NULL, 0),
  94. CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(ossl_tdes)
  95. static int tdes_generatekey(PROV_CIPHER_CTX *ctx, void *ptr)
  96. {
  97. DES_cblock *deskey = ptr;
  98. size_t kl = ctx->keylen;
  99. if (kl == 0 || RAND_priv_bytes_ex(ctx->libctx, ptr, kl, 0) <= 0)
  100. return 0;
  101. DES_set_odd_parity(deskey);
  102. if (kl >= 16) {
  103. DES_set_odd_parity(deskey + 1);
  104. if (kl >= 24)
  105. DES_set_odd_parity(deskey + 2);
  106. }
  107. return 1;
  108. }
  109. int ossl_tdes_get_ctx_params(void *vctx, OSSL_PARAM params[])
  110. {
  111. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  112. OSSL_PARAM *p;
  113. if (!ossl_cipher_generic_get_ctx_params(vctx, params))
  114. return 0;
  115. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_RANDOM_KEY);
  116. if (p != NULL && !tdes_generatekey(ctx, p->data)) {
  117. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);
  118. return 0;
  119. }
  120. return 1;
  121. }