i_cbc.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 <openssl/idea.h>
  10. #include "idea_lcl.h"
  11. void IDEA_cbc_encrypt(const unsigned char *in, unsigned char *out,
  12. long length, IDEA_KEY_SCHEDULE *ks, unsigned char *iv,
  13. int encrypt)
  14. {
  15. register unsigned long tin0, tin1;
  16. register unsigned long tout0, tout1, xor0, xor1;
  17. register long l = length;
  18. unsigned long tin[2];
  19. if (encrypt) {
  20. n2l(iv, tout0);
  21. n2l(iv, tout1);
  22. iv -= 8;
  23. for (l -= 8; l >= 0; l -= 8) {
  24. n2l(in, tin0);
  25. n2l(in, tin1);
  26. tin0 ^= tout0;
  27. tin1 ^= tout1;
  28. tin[0] = tin0;
  29. tin[1] = tin1;
  30. IDEA_encrypt(tin, ks);
  31. tout0 = tin[0];
  32. l2n(tout0, out);
  33. tout1 = tin[1];
  34. l2n(tout1, out);
  35. }
  36. if (l != -8) {
  37. n2ln(in, tin0, tin1, l + 8);
  38. tin0 ^= tout0;
  39. tin1 ^= tout1;
  40. tin[0] = tin0;
  41. tin[1] = tin1;
  42. IDEA_encrypt(tin, ks);
  43. tout0 = tin[0];
  44. l2n(tout0, out);
  45. tout1 = tin[1];
  46. l2n(tout1, out);
  47. }
  48. l2n(tout0, iv);
  49. l2n(tout1, iv);
  50. } else {
  51. n2l(iv, xor0);
  52. n2l(iv, xor1);
  53. iv -= 8;
  54. for (l -= 8; l >= 0; l -= 8) {
  55. n2l(in, tin0);
  56. tin[0] = tin0;
  57. n2l(in, tin1);
  58. tin[1] = tin1;
  59. IDEA_encrypt(tin, ks);
  60. tout0 = tin[0] ^ xor0;
  61. tout1 = tin[1] ^ xor1;
  62. l2n(tout0, out);
  63. l2n(tout1, out);
  64. xor0 = tin0;
  65. xor1 = tin1;
  66. }
  67. if (l != -8) {
  68. n2l(in, tin0);
  69. tin[0] = tin0;
  70. n2l(in, tin1);
  71. tin[1] = tin1;
  72. IDEA_encrypt(tin, ks);
  73. tout0 = tin[0] ^ xor0;
  74. tout1 = tin[1] ^ xor1;
  75. l2nn(tout0, tout1, out, l + 8);
  76. xor0 = tin0;
  77. xor1 = tin1;
  78. }
  79. l2n(xor0, iv);
  80. l2n(xor1, iv);
  81. }
  82. tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
  83. tin[0] = tin[1] = 0;
  84. }
  85. void IDEA_encrypt(unsigned long *d, IDEA_KEY_SCHEDULE *key)
  86. {
  87. register IDEA_INT *p;
  88. register unsigned long x1, x2, x3, x4, t0, t1, ul;
  89. x2 = d[0];
  90. x1 = (x2 >> 16);
  91. x4 = d[1];
  92. x3 = (x4 >> 16);
  93. p = &(key->data[0][0]);
  94. E_IDEA(0);
  95. E_IDEA(1);
  96. E_IDEA(2);
  97. E_IDEA(3);
  98. E_IDEA(4);
  99. E_IDEA(5);
  100. E_IDEA(6);
  101. E_IDEA(7);
  102. x1 &= 0xffff;
  103. idea_mul(x1, x1, *p, ul);
  104. p++;
  105. t0 = x3 + *(p++);
  106. t1 = x2 + *(p++);
  107. x4 &= 0xffff;
  108. idea_mul(x4, x4, *p, ul);
  109. d[0] = (t0 & 0xffff) | ((x1 & 0xffff) << 16);
  110. d[1] = (x4 & 0xffff) | ((t1 & 0xffff) << 16);
  111. }