e_xcbc_d.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 <stdio.h>
  15. #include "internal/cryptlib.h"
  16. #ifndef OPENSSL_NO_DES
  17. # include <openssl/evp.h>
  18. # include <openssl/objects.h>
  19. # include "crypto/evp.h"
  20. # include <openssl/des.h>
  21. # include "evp_local.h"
  22. static int desx_cbc_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  23. const unsigned char *iv, int enc);
  24. static int desx_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  25. const unsigned char *in, size_t inl);
  26. typedef struct {
  27. DES_key_schedule ks; /* key schedule */
  28. DES_cblock inw;
  29. DES_cblock outw;
  30. } DESX_CBC_KEY;
  31. # define data(ctx) EVP_C_DATA(DESX_CBC_KEY,ctx)
  32. static const EVP_CIPHER d_xcbc_cipher = {
  33. NID_desx_cbc,
  34. 8, 24, 8,
  35. EVP_CIPH_CBC_MODE,
  36. EVP_ORIG_GLOBAL,
  37. desx_cbc_init_key,
  38. desx_cbc_cipher,
  39. NULL,
  40. sizeof(DESX_CBC_KEY),
  41. EVP_CIPHER_set_asn1_iv,
  42. EVP_CIPHER_get_asn1_iv,
  43. NULL,
  44. NULL
  45. };
  46. const EVP_CIPHER *EVP_desx_cbc(void)
  47. {
  48. return &d_xcbc_cipher;
  49. }
  50. static int desx_cbc_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  51. const unsigned char *iv, int enc)
  52. {
  53. DES_cblock *deskey = (DES_cblock *)key;
  54. DES_set_key_unchecked(deskey, &data(ctx)->ks);
  55. memcpy(&data(ctx)->inw[0], &key[8], 8);
  56. memcpy(&data(ctx)->outw[0], &key[16], 8);
  57. return 1;
  58. }
  59. static int desx_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  60. const unsigned char *in, size_t inl)
  61. {
  62. while (inl >= EVP_MAXCHUNK) {
  63. DES_xcbc_encrypt(in, out, (long)EVP_MAXCHUNK, &data(ctx)->ks,
  64. (DES_cblock *)ctx->iv,
  65. &data(ctx)->inw, &data(ctx)->outw,
  66. EVP_CIPHER_CTX_encrypting(ctx));
  67. inl -= EVP_MAXCHUNK;
  68. in += EVP_MAXCHUNK;
  69. out += EVP_MAXCHUNK;
  70. }
  71. if (inl)
  72. DES_xcbc_encrypt(in, out, (long)inl, &data(ctx)->ks,
  73. (DES_cblock *)ctx->iv,
  74. &data(ctx)->inw, &data(ctx)->outw,
  75. EVP_CIPHER_CTX_encrypting(ctx));
  76. return 1;
  77. }
  78. #endif