2
0

cipher_tdes_hw.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 "prov/ciphercommon.h"
  15. #include "cipher_tdes.h"
  16. #define ks1 tks.ks[0]
  17. #define ks2 tks.ks[1]
  18. #define ks3 tks.ks[2]
  19. int ossl_cipher_hw_tdes_ede3_initkey(PROV_CIPHER_CTX *ctx,
  20. const unsigned char *key, size_t keylen)
  21. {
  22. PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
  23. DES_cblock *deskey = (DES_cblock *)key;
  24. tctx->tstream.cbc = NULL;
  25. # if defined(SPARC_DES_CAPABLE)
  26. if (SPARC_DES_CAPABLE) {
  27. if (ctx->mode == EVP_CIPH_CBC_MODE) {
  28. des_t4_key_expand(&deskey[0], &tctx->ks1);
  29. des_t4_key_expand(&deskey[1], &tctx->ks2);
  30. des_t4_key_expand(&deskey[2], &tctx->ks3);
  31. tctx->tstream.cbc = ctx->enc ? des_t4_ede3_cbc_encrypt :
  32. des_t4_ede3_cbc_decrypt;
  33. return 1;
  34. }
  35. }
  36. # endif
  37. DES_set_key_unchecked(&deskey[0], &tctx->ks1);
  38. DES_set_key_unchecked(&deskey[1], &tctx->ks2);
  39. DES_set_key_unchecked(&deskey[2], &tctx->ks3);
  40. return 1;
  41. }
  42. void ossl_cipher_hw_tdes_copyctx(PROV_CIPHER_CTX *dst,
  43. const PROV_CIPHER_CTX *src)
  44. {
  45. PROV_TDES_CTX *sctx = (PROV_TDES_CTX *)src;
  46. PROV_TDES_CTX *dctx = (PROV_TDES_CTX *)dst;
  47. *dctx = *sctx;
  48. dst->ks = &dctx->tks.ks;
  49. }
  50. int ossl_cipher_hw_tdes_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out,
  51. const unsigned char *in, size_t inl)
  52. {
  53. PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
  54. if (tctx->tstream.cbc != NULL) {
  55. (*tctx->tstream.cbc) (in, out, inl, tctx->tks.ks, ctx->iv);
  56. return 1;
  57. }
  58. while (inl >= MAXCHUNK) {
  59. DES_ede3_cbc_encrypt(in, out, (long)MAXCHUNK, &tctx->ks1, &tctx->ks2,
  60. &tctx->ks3, (DES_cblock *)ctx->iv, ctx->enc);
  61. inl -= MAXCHUNK;
  62. in += MAXCHUNK;
  63. out += MAXCHUNK;
  64. }
  65. if (inl > 0)
  66. DES_ede3_cbc_encrypt(in, out, (long)inl, &tctx->ks1, &tctx->ks2,
  67. &tctx->ks3, (DES_cblock *)ctx->iv, ctx->enc);
  68. return 1;
  69. }
  70. int ossl_cipher_hw_tdes_ecb(PROV_CIPHER_CTX *ctx, unsigned char *out,
  71. const unsigned char *in, size_t len)
  72. {
  73. size_t i;
  74. PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
  75. if (len < DES_BLOCK_SIZE)
  76. return 1;
  77. for (i = 0, len -= DES_BLOCK_SIZE; i <= len; i += DES_BLOCK_SIZE) {
  78. DES_ecb3_encrypt((const_DES_cblock *)(in + i), (DES_cblock *)(out + i),
  79. &tctx->ks1, &tctx->ks2, &tctx->ks3, ctx->enc);
  80. }
  81. return 1;
  82. }
  83. PROV_CIPHER_HW_tdes_mode(ede3, ecb)
  84. PROV_CIPHER_HW_tdes_mode(ede3, cbc)