cipher_tdes_common.c 4.1 KB

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