2
0

e_xcbc_d.c 2.4 KB

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