cipher_tdes.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2019 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. #include "cipher_local.h"
  10. #include "internal/ciphers/cipher_tdes.h"
  11. #include "crypto/rand.h"
  12. #include "internal/provider_algs.h"
  13. #include "internal/providercommonerr.h"
  14. void *tdes_newctx(void *provctx, int mode, size_t kbits, size_t blkbits,
  15. size_t ivbits, uint64_t flags, const PROV_CIPHER_HW *hw)
  16. {
  17. PROV_TDES_CTX *tctx = OPENSSL_zalloc(sizeof(*tctx));
  18. if (tctx != NULL)
  19. cipher_generic_initkey(tctx, kbits, blkbits, ivbits, mode, flags, hw,
  20. provctx);
  21. return tctx;
  22. }
  23. void tdes_freectx(void *vctx)
  24. {
  25. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  26. OPENSSL_clear_free(ctx, sizeof(*ctx));
  27. }
  28. static int tdes_init(void *vctx, const unsigned char *key, size_t keylen,
  29. const unsigned char *iv, size_t ivlen, int enc)
  30. {
  31. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  32. ctx->enc = enc;
  33. if (iv != NULL) {
  34. if (ivlen != TDES_IVLEN) {
  35. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IVLEN);
  36. return 0;
  37. }
  38. memcpy(ctx->iv, iv, TDES_IVLEN);
  39. }
  40. if (key != NULL) {
  41. if (keylen != ctx->keylen) {
  42. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEYLEN);
  43. return 0;
  44. }
  45. return ctx->hw->init(ctx, key, ctx->keylen);
  46. }
  47. return 1;
  48. }
  49. int tdes_einit(void *vctx, const unsigned char *key, size_t keylen,
  50. const unsigned char *iv, size_t ivlen)
  51. {
  52. return tdes_init(vctx, key, keylen, iv, ivlen, 1);
  53. }
  54. int tdes_dinit(void *vctx, const unsigned char *key, size_t keylen,
  55. const unsigned char *iv, size_t ivlen)
  56. {
  57. return tdes_init(vctx, key, keylen, iv, ivlen, 0);
  58. }
  59. static int tdes_generatekey(PROV_CIPHER_CTX *ctx, void *ptr)
  60. {
  61. DES_cblock *deskey = ptr;
  62. size_t kl = ctx->keylen;
  63. if (kl == 0 || rand_priv_bytes_ex(ctx->libctx, ptr, kl) <= 0)
  64. return 0;
  65. DES_set_odd_parity(deskey);
  66. if (kl >= 16)
  67. DES_set_odd_parity(deskey + 1);
  68. if (kl >= 24) {
  69. DES_set_odd_parity(deskey + 2);
  70. return 1;
  71. }
  72. return 0;
  73. }
  74. CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(tdes)
  75. OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY, NULL, 0),
  76. CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(tdes)
  77. int tdes_get_ctx_params(void *vctx, OSSL_PARAM params[])
  78. {
  79. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  80. OSSL_PARAM *p;
  81. if (!cipher_generic_get_ctx_params(vctx, params))
  82. return 0;
  83. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_RANDOM_KEY);
  84. if (p != NULL && !tdes_generatekey(ctx, p->data)) {
  85. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);
  86. return 0;
  87. }
  88. return 1;
  89. }
  90. /*
  91. * TODO(3.0) - ECB mode does not use an IV - but existing test code is setting
  92. * an IV. Fixing this could potentially make applications break.
  93. */
  94. /* tdes_ede3_ecb_functions */
  95. IMPLEMENT_tdes_cipher(ede3, EDE3, ecb, ECB, TDES_FLAGS, 64*3, 64, 64, block);
  96. /* tdes_ede3_cbc_functions */
  97. IMPLEMENT_tdes_cipher(ede3, EDE3, cbc, CBC, TDES_FLAGS, 64*3, 64, 64, block);