ofb_enc.c 2.4 KB

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