cipher_desx_hw.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright 1995-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 <openssl/des.h>
  15. #include "cipher_tdes_default.h"
  16. /*
  17. * Note the PROV_TDES_CTX has been used for the DESX cipher, just to reduce
  18. * code size.
  19. */
  20. #define ks1 tks.ks[0]
  21. #define ks2 tks.ks[1].ks[0].cblock
  22. #define ks3 tks.ks[2].ks[0].cblock
  23. static int cipher_hw_desx_cbc_initkey(PROV_CIPHER_CTX *ctx,
  24. const unsigned char *key, size_t keylen)
  25. {
  26. PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
  27. DES_cblock *deskey = (DES_cblock *)key;
  28. DES_set_key_unchecked(deskey, &tctx->ks1);
  29. memcpy(&tctx->ks2, &key[8], 8);
  30. memcpy(&tctx->ks3, &key[16], 8);
  31. return 1;
  32. }
  33. static void cipher_hw_desx_copyctx(PROV_CIPHER_CTX *dst,
  34. const PROV_CIPHER_CTX *src)
  35. {
  36. PROV_TDES_CTX *sctx = (PROV_TDES_CTX *)src;
  37. PROV_TDES_CTX *dctx = (PROV_TDES_CTX *)dst;
  38. *dctx = *sctx;
  39. dst->ks = &dctx->tks.ks;
  40. }
  41. static int cipher_hw_desx_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out,
  42. const unsigned char *in, size_t inl)
  43. {
  44. PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
  45. while (inl >= MAXCHUNK) {
  46. DES_xcbc_encrypt(in, out, (long)MAXCHUNK, &tctx->ks1,
  47. (DES_cblock *)ctx->iv, &tctx->ks2, &tctx->ks3,
  48. ctx->enc);
  49. inl -= MAXCHUNK;
  50. in += MAXCHUNK;
  51. out += MAXCHUNK;
  52. }
  53. if (inl > 0)
  54. DES_xcbc_encrypt(in, out, (long)inl, &tctx->ks1,
  55. (DES_cblock *)ctx->iv, &tctx->ks2, &tctx->ks3,
  56. ctx->enc);
  57. return 1;
  58. }
  59. static const PROV_CIPHER_HW desx_cbc =
  60. {
  61. cipher_hw_desx_cbc_initkey,
  62. cipher_hw_desx_cbc,
  63. cipher_hw_desx_copyctx
  64. };
  65. const PROV_CIPHER_HW *ossl_prov_cipher_hw_tdes_desx_cbc(void)
  66. {
  67. return &desx_cbc;
  68. }