cipher_tdes_common.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. }
  66. if (key != NULL) {
  67. if (keylen != ctx->keylen) {
  68. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  69. return 0;
  70. }
  71. if (!ctx->hw->init(ctx, key, ctx->keylen))
  72. return 0;
  73. }
  74. return ossl_cipher_generic_set_ctx_params(ctx, params);
  75. }
  76. int ossl_tdes_einit(void *vctx, const unsigned char *key, size_t keylen,
  77. const unsigned char *iv, size_t ivlen,
  78. const OSSL_PARAM params[])
  79. {
  80. return tdes_init(vctx, key, keylen, iv, ivlen, params, 1);
  81. }
  82. int ossl_tdes_dinit(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, 0);
  87. }
  88. CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(ossl_tdes)
  89. OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY, NULL, 0),
  90. CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(ossl_tdes)
  91. static int tdes_generatekey(PROV_CIPHER_CTX *ctx, void *ptr)
  92. {
  93. DES_cblock *deskey = ptr;
  94. size_t kl = ctx->keylen;
  95. if (kl == 0 || RAND_priv_bytes_ex(ctx->libctx, ptr, kl) <= 0)
  96. return 0;
  97. DES_set_odd_parity(deskey);
  98. if (kl >= 16)
  99. DES_set_odd_parity(deskey + 1);
  100. if (kl >= 24) {
  101. DES_set_odd_parity(deskey + 2);
  102. return 1;
  103. }
  104. return 0;
  105. }
  106. int ossl_tdes_get_ctx_params(void *vctx, OSSL_PARAM params[])
  107. {
  108. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  109. OSSL_PARAM *p;
  110. if (!ossl_cipher_generic_get_ctx_params(vctx, params))
  111. return 0;
  112. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_RANDOM_KEY);
  113. if (p != NULL && !tdes_generatekey(ctx, p->data)) {
  114. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);
  115. return 0;
  116. }
  117. return 1;
  118. }