ofb_enc.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 "des_local.h"
  15. /*
  16. * The input and output are loaded in multiples of 8 bits. What this means is
  17. * that if you have numbits=12 and length=2 the first 12 bits will be
  18. * retrieved from the first byte and half the second. The second 12 bits
  19. * will come from the 3rd and half the 4th byte.
  20. */
  21. void DES_ofb_encrypt(const unsigned char *in, unsigned char *out, int numbits,
  22. long length, DES_key_schedule *schedule,
  23. DES_cblock *ivec)
  24. {
  25. register DES_LONG d0, d1, vv0, vv1, v0, v1, n = (numbits + 7) / 8;
  26. register DES_LONG mask0, mask1;
  27. register long l = length;
  28. register int num = numbits;
  29. DES_LONG ti[2];
  30. unsigned char *iv;
  31. if (num > 64)
  32. return;
  33. if (num > 32) {
  34. mask0 = 0xffffffffL;
  35. if (num >= 64)
  36. mask1 = mask0;
  37. else
  38. mask1 = (1L << (num - 32)) - 1;
  39. } else {
  40. if (num == 32)
  41. mask0 = 0xffffffffL;
  42. else
  43. mask0 = (1L << num) - 1;
  44. mask1 = 0x00000000L;
  45. }
  46. iv = &(*ivec)[0];
  47. c2l(iv, v0);
  48. c2l(iv, v1);
  49. ti[0] = v0;
  50. ti[1] = v1;
  51. while (l-- > 0) {
  52. ti[0] = v0;
  53. ti[1] = v1;
  54. DES_encrypt1((DES_LONG *)ti, schedule, DES_ENCRYPT);
  55. vv0 = ti[0];
  56. vv1 = ti[1];
  57. c2ln(in, d0, d1, n);
  58. in += n;
  59. d0 = (d0 ^ vv0) & mask0;
  60. d1 = (d1 ^ vv1) & mask1;
  61. l2cn(d0, d1, out, n);
  62. out += n;
  63. if (num == 32) {
  64. v0 = v1;
  65. v1 = vv0;
  66. } else if (num == 64) {
  67. v0 = vv0;
  68. v1 = vv1;
  69. } else if (num > 32) { /* && num != 64 */
  70. v0 = ((v1 >> (num - 32)) | (vv0 << (64 - num))) & 0xffffffffL;
  71. v1 = ((vv0 >> (num - 32)) | (vv1 << (64 - num))) & 0xffffffffL;
  72. } else { /* num < 32 */
  73. v0 = ((v0 >> num) | (v1 << (32 - num))) & 0xffffffffL;
  74. v1 = ((v1 >> num) | (vv0 << (32 - num))) & 0xffffffffL;
  75. }
  76. }
  77. iv = &(*ivec)[0];
  78. l2c(v0, iv);
  79. l2c(v1, iv);
  80. v0 = v1 = d0 = d1 = ti[0] = ti[1] = vv0 = vv1 = 0;
  81. }