cipher_desx_hw.c 1.8 KB

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