cbc128.c 4.8 KB

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