2
0

cipher_tdes_default_hw.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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];
  92. unsigned char d[1] = { 0 };
  93. if (ctx->use_bits == 0)
  94. inl *= 8;
  95. for (n = 0; n < inl; ++n) {
  96. c[0] = (in[n / 8] & (1 << (7 - n % 8))) ? 0x80 : 0;
  97. DES_ede3_cfb_encrypt(c, d, 1, 1,
  98. &tctx->ks1, &tctx->ks2, &tctx->ks3,
  99. (DES_cblock *)ctx->iv, ctx->enc);
  100. out[n / 8] = (out[n / 8] & ~(0x80 >> (unsigned int)(n % 8)))
  101. | ((d[0] & 0x80) >> (unsigned int)(n % 8));
  102. }
  103. return 1;
  104. }
  105. static int ossl_cipher_hw_tdes_cfb8(PROV_CIPHER_CTX *ctx, unsigned char *out,
  106. const unsigned char *in, size_t inl)
  107. {
  108. PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
  109. while (inl >= MAXCHUNK) {
  110. DES_ede3_cfb_encrypt(in, out, 8, (long)MAXCHUNK,
  111. &tctx->ks1, &tctx->ks2, &tctx->ks3,
  112. (DES_cblock *)ctx->iv, ctx->enc);
  113. inl -= MAXCHUNK;
  114. in += MAXCHUNK;
  115. out += MAXCHUNK;
  116. }
  117. if (inl > 0)
  118. DES_ede3_cfb_encrypt(in, out, 8, (long)inl,
  119. &tctx->ks1, &tctx->ks2, &tctx->ks3,
  120. (DES_cblock *)ctx->iv, ctx->enc);
  121. return 1;
  122. }
  123. PROV_CIPHER_HW_tdes_mode(ede3, ofb)
  124. PROV_CIPHER_HW_tdes_mode(ede3, cfb)
  125. PROV_CIPHER_HW_tdes_mode(ede3, cfb1)
  126. PROV_CIPHER_HW_tdes_mode(ede3, cfb8)
  127. PROV_CIPHER_HW_tdes_mode(ede2, ecb)
  128. PROV_CIPHER_HW_tdes_mode(ede2, cbc)
  129. PROV_CIPHER_HW_tdes_mode(ede2, ofb)
  130. PROV_CIPHER_HW_tdes_mode(ede2, cfb)