cipher_tdes_common.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright 2019-2020 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 "prov/ciphercommon.h"
  15. #include "cipher_tdes.h"
  16. #include <openssl/rand.h>
  17. #include "prov/implementations.h"
  18. #include "prov/providercommon.h"
  19. #include "prov/providercommonerr.h"
  20. void *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 *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 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, int enc)
  54. {
  55. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  56. if (!ossl_prov_is_running())
  57. return 0;
  58. ctx->num = 0;
  59. ctx->bufsz = 0;
  60. ctx->enc = enc;
  61. if (iv != NULL) {
  62. if (!ossl_cipher_generic_initiv(ctx, iv, ivlen))
  63. return 0;
  64. }
  65. if (key != NULL) {
  66. if (keylen != ctx->keylen) {
  67. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEYLEN);
  68. return 0;
  69. }
  70. return ctx->hw->init(ctx, key, ctx->keylen);
  71. }
  72. return 1;
  73. }
  74. int tdes_einit(void *vctx, const unsigned char *key, size_t keylen,
  75. const unsigned char *iv, size_t ivlen)
  76. {
  77. return tdes_init(vctx, key, keylen, iv, ivlen, 1);
  78. }
  79. int tdes_dinit(void *vctx, const unsigned char *key, size_t keylen,
  80. const unsigned char *iv, size_t ivlen)
  81. {
  82. return tdes_init(vctx, key, keylen, iv, ivlen, 0);
  83. }
  84. CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(tdes)
  85. OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY, NULL, 0),
  86. CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(tdes)
  87. static int tdes_generatekey(PROV_CIPHER_CTX *ctx, void *ptr)
  88. {
  89. DES_cblock *deskey = ptr;
  90. size_t kl = ctx->keylen;
  91. if (kl == 0 || RAND_priv_bytes_ex(ctx->libctx, ptr, kl) <= 0)
  92. return 0;
  93. DES_set_odd_parity(deskey);
  94. if (kl >= 16)
  95. DES_set_odd_parity(deskey + 1);
  96. if (kl >= 24) {
  97. DES_set_odd_parity(deskey + 2);
  98. return 1;
  99. }
  100. return 0;
  101. }
  102. int tdes_get_ctx_params(void *vctx, OSSL_PARAM params[])
  103. {
  104. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  105. OSSL_PARAM *p;
  106. if (!ossl_cipher_generic_get_ctx_params(vctx, params))
  107. return 0;
  108. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_RANDOM_KEY);
  109. if (p != NULL && !tdes_generatekey(ctx, p->data)) {
  110. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);
  111. return 0;
  112. }
  113. return 1;
  114. }