cbc128.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright 2008-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 <openssl/crypto.h>
  10. #include "modes_lcl.h"
  11. #include <string.h>
  12. #if !defined(STRICT_ALIGNMENT) && !defined(PEDANTIC)
  13. # define STRICT_ALIGNMENT 0
  14. #endif
  15. void CRYPTO_cbc128_encrypt(const unsigned char *in, unsigned char *out,
  16. size_t len, const void *key,
  17. unsigned char ivec[16], block128_f block)
  18. {
  19. size_t n;
  20. const unsigned char *iv = ivec;
  21. if (len == 0)
  22. return;
  23. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  24. if (STRICT_ALIGNMENT &&
  25. ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0) {
  26. while (len >= 16) {
  27. for (n = 0; n < 16; ++n)
  28. out[n] = in[n] ^ iv[n];
  29. (*block) (out, out, key);
  30. iv = out;
  31. len -= 16;
  32. in += 16;
  33. out += 16;
  34. }
  35. } else {
  36. while (len >= 16) {
  37. for (n = 0; n < 16; n += sizeof(size_t))
  38. *(size_t *)(out + n) =
  39. *(size_t *)(in + n) ^ *(size_t *)(iv + n);
  40. (*block) (out, out, key);
  41. iv = out;
  42. len -= 16;
  43. in += 16;
  44. out += 16;
  45. }
  46. }
  47. #endif
  48. while (len) {
  49. for (n = 0; n < 16 && n < len; ++n)
  50. out[n] = in[n] ^ iv[n];
  51. for (; n < 16; ++n)
  52. out[n] = iv[n];
  53. (*block) (out, out, key);
  54. iv = out;
  55. if (len <= 16)
  56. break;
  57. len -= 16;
  58. in += 16;
  59. out += 16;
  60. }
  61. memcpy(ivec, iv, 16);
  62. }
  63. void CRYPTO_cbc128_decrypt(const unsigned char *in, unsigned char *out,
  64. size_t len, const void *key,
  65. unsigned char ivec[16], block128_f block)
  66. {
  67. size_t n;
  68. union {
  69. size_t t[16 / sizeof(size_t)];
  70. unsigned char c[16];
  71. } tmp;
  72. if (len == 0)
  73. return;
  74. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  75. if (in != out) {
  76. const unsigned char *iv = ivec;
  77. if (STRICT_ALIGNMENT &&
  78. ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0) {
  79. while (len >= 16) {
  80. (*block) (in, out, key);
  81. for (n = 0; n < 16; ++n)
  82. out[n] ^= iv[n];
  83. iv = in;
  84. len -= 16;
  85. in += 16;
  86. out += 16;
  87. }
  88. } else if (16 % sizeof(size_t) == 0) { /* always true */
  89. while (len >= 16) {
  90. size_t *out_t = (size_t *)out, *iv_t = (size_t *)iv;
  91. (*block) (in, out, key);
  92. for (n = 0; n < 16 / sizeof(size_t); n++)
  93. out_t[n] ^= iv_t[n];
  94. iv = in;
  95. len -= 16;
  96. in += 16;
  97. out += 16;
  98. }
  99. }
  100. memcpy(ivec, iv, 16);
  101. } else {
  102. if (STRICT_ALIGNMENT &&
  103. ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0) {
  104. unsigned char c;
  105. while (len >= 16) {
  106. (*block) (in, tmp.c, key);
  107. for (n = 0; n < 16; ++n) {
  108. c = in[n];
  109. out[n] = tmp.c[n] ^ ivec[n];
  110. ivec[n] = c;
  111. }
  112. len -= 16;
  113. in += 16;
  114. out += 16;
  115. }
  116. } else if (16 % sizeof(size_t) == 0) { /* always true */
  117. while (len >= 16) {
  118. size_t c, *out_t = (size_t *)out, *ivec_t = (size_t *)ivec;
  119. const size_t *in_t = (const size_t *)in;
  120. (*block) (in, tmp.c, key);
  121. for (n = 0; n < 16 / sizeof(size_t); n++) {
  122. c = in_t[n];
  123. out_t[n] = tmp.t[n] ^ ivec_t[n];
  124. ivec_t[n] = c;
  125. }
  126. len -= 16;
  127. in += 16;
  128. out += 16;
  129. }
  130. }
  131. }
  132. #endif
  133. while (len) {
  134. unsigned char c;
  135. (*block) (in, tmp.c, key);
  136. for (n = 0; n < 16 && n < len; ++n) {
  137. c = in[n];
  138. out[n] = tmp.c[n] ^ ivec[n];
  139. ivec[n] = c;
  140. }
  141. if (len <= 16) {
  142. for (; n < 16; ++n)
  143. ivec[n] = in[n];
  144. break;
  145. }
  146. len -= 16;
  147. in += 16;
  148. out += 16;
  149. }
  150. }