2
0

cipher_tdes_hw.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 "cipher_local.h"
  10. #include "internal/ciphers/cipher_tdes.h"
  11. #define ks1 tks.ks[0]
  12. #define ks2 tks.ks[1]
  13. #define ks3 tks.ks[2]
  14. int cipher_hw_tdes_ede3_initkey(PROV_CIPHER_CTX *ctx, const unsigned char *key,
  15. size_t keylen)
  16. {
  17. PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
  18. DES_cblock *deskey = (DES_cblock *)key;
  19. tctx->tstream.cbc = NULL;
  20. # if defined(SPARC_DES_CAPABLE)
  21. if (SPARC_DES_CAPABLE) {
  22. if (ctx->mode == EVP_CIPH_CBC_MODE) {
  23. des_t4_key_expand(&deskey[0], &tctx->ks1);
  24. des_t4_key_expand(&deskey[1], &tctx->ks2);
  25. des_t4_key_expand(&deskey[2], &tctx->ks3);
  26. tctx->tstream.cbc = ctx->enc ? des_t4_ede3_cbc_encrypt :
  27. des_t4_ede3_cbc_decrypt;
  28. return 1;
  29. }
  30. }
  31. # endif
  32. DES_set_key_unchecked(&deskey[0], &tctx->ks1);
  33. DES_set_key_unchecked(&deskey[1], &tctx->ks2);
  34. DES_set_key_unchecked(&deskey[2], &tctx->ks3);
  35. return 1;
  36. }
  37. int cipher_hw_tdes_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out,
  38. const unsigned char *in, size_t inl)
  39. {
  40. PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
  41. if (tctx->tstream.cbc != NULL) {
  42. (*tctx->tstream.cbc) (in, out, inl, tctx->tks.ks, ctx->iv);
  43. return 1;
  44. }
  45. while (inl >= MAXCHUNK) {
  46. DES_ede3_cbc_encrypt(in, out, (long)MAXCHUNK, &tctx->ks1, &tctx->ks2,
  47. &tctx->ks3, (DES_cblock *)ctx->iv, ctx->enc);
  48. inl -= MAXCHUNK;
  49. in += MAXCHUNK;
  50. out += MAXCHUNK;
  51. }
  52. if (inl > 0)
  53. DES_ede3_cbc_encrypt(in, out, (long)inl, &tctx->ks1, &tctx->ks2,
  54. &tctx->ks3, (DES_cblock *)ctx->iv, ctx->enc);
  55. return 1;
  56. }
  57. int cipher_hw_tdes_ecb(PROV_CIPHER_CTX *ctx, unsigned char *out,
  58. const unsigned char *in, size_t len)
  59. {
  60. size_t i;
  61. PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
  62. if (len < DES_BLOCK_SIZE)
  63. return 1;
  64. for (i = 0, len -= DES_BLOCK_SIZE; i <= len; i += DES_BLOCK_SIZE) {
  65. DES_ecb3_encrypt((const_DES_cblock *)(in + i), (DES_cblock *)(out + i),
  66. &tctx->ks1, &tctx->ks2, &tctx->ks3, ctx->enc);
  67. }
  68. return 1;
  69. }
  70. PROV_CIPHER_HW_tdes_mode(ede3, ecb)
  71. PROV_CIPHER_HW_tdes_mode(ede3, cbc)