cipher_tdes_default_hw.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright 1995-2021 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 "cipher_tdes_default.h"
  15. #define ks1 tks.ks[0]
  16. #define ks2 tks.ks[1]
  17. #define ks3 tks.ks[2]
  18. static int ossl_cipher_hw_tdes_ede2_initkey(PROV_CIPHER_CTX *ctx,
  19. const unsigned char *key,
  20. 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. memcpy(&tctx->ks3, &tctx->ks1, sizeof(tctx->ks1));
  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. memcpy(&tctx->ks3, &tctx->ks1, sizeof(tctx->ks1));
  40. return 1;
  41. }
  42. static int ossl_cipher_hw_tdes_ofb(PROV_CIPHER_CTX *ctx, unsigned char *out,
  43. const unsigned char *in, size_t inl)
  44. {
  45. PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
  46. int num = ctx->num;
  47. while (inl >= MAXCHUNK) {
  48. DES_ede3_ofb64_encrypt(in, out, (long)MAXCHUNK, &tctx->ks1, &tctx->ks2,
  49. &tctx->ks3, (DES_cblock *)ctx->iv, &num);
  50. inl -= MAXCHUNK;
  51. in += MAXCHUNK;
  52. out += MAXCHUNK;
  53. }
  54. if (inl > 0) {
  55. DES_ede3_ofb64_encrypt(in, out, (long)inl, &tctx->ks1, &tctx->ks2,
  56. &tctx->ks3, (DES_cblock *)ctx->iv, &num);
  57. }
  58. ctx->num = num;
  59. return 1;
  60. }
  61. static int ossl_cipher_hw_tdes_cfb(PROV_CIPHER_CTX *ctx, unsigned char *out,
  62. const unsigned char *in, size_t inl)
  63. {
  64. PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
  65. int num = ctx->num;
  66. while (inl >= MAXCHUNK) {
  67. DES_ede3_cfb64_encrypt(in, out, (long)MAXCHUNK,
  68. &tctx->ks1, &tctx->ks2, &tctx->ks3,
  69. (DES_cblock *)ctx->iv, &num, ctx->enc);
  70. inl -= MAXCHUNK;
  71. in += MAXCHUNK;
  72. out += MAXCHUNK;
  73. }
  74. if (inl > 0) {
  75. DES_ede3_cfb64_encrypt(in, out, (long)inl,
  76. &tctx->ks1, &tctx->ks2, &tctx->ks3,
  77. (DES_cblock *)ctx->iv, &num, ctx->enc);
  78. }
  79. ctx->num = num;
  80. return 1;
  81. }
  82. /*
  83. * Although we have a CFB-r implementation for 3-DES, it doesn't pack the
  84. * right way, so wrap it here
  85. */
  86. static int ossl_cipher_hw_tdes_cfb1(PROV_CIPHER_CTX *ctx, unsigned char *out,
  87. const unsigned char *in, size_t inl)
  88. {
  89. PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
  90. size_t n;
  91. unsigned char c[1], d[1];
  92. if (ctx->use_bits == 0)
  93. inl *= 8;
  94. for (n = 0; n < inl; ++n) {
  95. c[0] = (in[n / 8] & (1 << (7 - n % 8))) ? 0x80 : 0;
  96. DES_ede3_cfb_encrypt(c, d, 1, 1,
  97. &tctx->ks1, &tctx->ks2, &tctx->ks3,
  98. (DES_cblock *)ctx->iv, ctx->enc);
  99. out[n / 8] = (out[n / 8] & ~(0x80 >> (unsigned int)(n % 8)))
  100. | ((d[0] & 0x80) >> (unsigned int)(n % 8));
  101. }
  102. return 1;
  103. }
  104. static int ossl_cipher_hw_tdes_cfb8(PROV_CIPHER_CTX *ctx, unsigned char *out,
  105. const unsigned char *in, size_t inl)
  106. {
  107. PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
  108. while (inl >= MAXCHUNK) {
  109. DES_ede3_cfb_encrypt(in, out, 8, (long)MAXCHUNK,
  110. &tctx->ks1, &tctx->ks2, &tctx->ks3,
  111. (DES_cblock *)ctx->iv, ctx->enc);
  112. inl -= MAXCHUNK;
  113. in += MAXCHUNK;
  114. out += MAXCHUNK;
  115. }
  116. if (inl > 0)
  117. DES_ede3_cfb_encrypt(in, out, 8, (long)inl,
  118. &tctx->ks1, &tctx->ks2, &tctx->ks3,
  119. (DES_cblock *)ctx->iv, ctx->enc);
  120. return 1;
  121. }
  122. PROV_CIPHER_HW_tdes_mode(ede3, ofb)
  123. PROV_CIPHER_HW_tdes_mode(ede3, cfb)
  124. PROV_CIPHER_HW_tdes_mode(ede3, cfb1)
  125. PROV_CIPHER_HW_tdes_mode(ede3, cfb8)
  126. PROV_CIPHER_HW_tdes_mode(ede2, ecb)
  127. PROV_CIPHER_HW_tdes_mode(ede2, cbc)
  128. PROV_CIPHER_HW_tdes_mode(ede2, ofb)
  129. PROV_CIPHER_HW_tdes_mode(ede2, cfb)