e_xcbc_d.c 2.5 KB

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