cbc128.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright 2008-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. #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. memcpy(ivec, iv, 16);
  67. }
  68. void CRYPTO_cbc128_decrypt(const unsigned char *in, unsigned char *out,
  69. size_t len, const void *key,
  70. unsigned char ivec[16], block128_f block)
  71. {
  72. size_t n;
  73. union {
  74. size_t t[16 / sizeof(size_t)];
  75. unsigned char c[16];
  76. } tmp;
  77. if (len == 0)
  78. return;
  79. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  80. if (in != out) {
  81. const unsigned char *iv = ivec;
  82. if (STRICT_ALIGNMENT &&
  83. ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0) {
  84. while (len >= 16) {
  85. (*block) (in, out, key);
  86. for (n = 0; n < 16; ++n)
  87. out[n] ^= iv[n];
  88. iv = in;
  89. len -= 16;
  90. in += 16;
  91. out += 16;
  92. }
  93. } else if (16 % sizeof(size_t) == 0) { /* always true */
  94. while (len >= 16) {
  95. size_t_aX *out_t = (size_t_aX *)out;
  96. size_t_aX *iv_t = (size_t_aX *)iv;
  97. (*block) (in, out, key);
  98. for (n = 0; n < 16 / sizeof(size_t); n++)
  99. out_t[n] ^= iv_t[n];
  100. iv = in;
  101. len -= 16;
  102. in += 16;
  103. out += 16;
  104. }
  105. }
  106. memcpy(ivec, iv, 16);
  107. } else {
  108. if (STRICT_ALIGNMENT &&
  109. ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0) {
  110. unsigned char c;
  111. while (len >= 16) {
  112. (*block) (in, tmp.c, key);
  113. for (n = 0; n < 16; ++n) {
  114. c = in[n];
  115. out[n] = tmp.c[n] ^ ivec[n];
  116. ivec[n] = c;
  117. }
  118. len -= 16;
  119. in += 16;
  120. out += 16;
  121. }
  122. } else if (16 % sizeof(size_t) == 0) { /* always true */
  123. while (len >= 16) {
  124. size_t c;
  125. size_t_aX *out_t = (size_t_aX *)out;
  126. size_t_aX *ivec_t = (size_t_aX *)ivec;
  127. const size_t_aX *in_t = (const size_t_aX *)in;
  128. (*block) (in, tmp.c, key);
  129. for (n = 0; n < 16 / sizeof(size_t); n++) {
  130. c = in_t[n];
  131. out_t[n] = tmp.t[n] ^ ivec_t[n];
  132. ivec_t[n] = c;
  133. }
  134. len -= 16;
  135. in += 16;
  136. out += 16;
  137. }
  138. }
  139. }
  140. #endif
  141. while (len) {
  142. unsigned char c;
  143. (*block) (in, tmp.c, key);
  144. for (n = 0; n < 16 && n < len; ++n) {
  145. c = in[n];
  146. out[n] = tmp.c[n] ^ ivec[n];
  147. ivec[n] = c;
  148. }
  149. if (len <= 16) {
  150. for (; n < 16; ++n)
  151. ivec[n] = in[n];
  152. break;
  153. }
  154. len -= 16;
  155. in += 16;
  156. out += 16;
  157. }
  158. }