Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

213 строки
6.5 KiB

  1. /* ====================================================================
  2. * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * openssl-core@openssl.org.
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. * ==================================================================== */
  48. #include <assert.h>
  49. #include <string.h>
  50. #include "internal.h"
  51. void CRYPTO_cbc128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
  52. const void *key, uint8_t ivec[16],
  53. block128_f block) {
  54. size_t n;
  55. const uint8_t *iv = ivec;
  56. assert(key != NULL && ivec != NULL);
  57. assert(len == 0 || (in != NULL && out != NULL));
  58. if (STRICT_ALIGNMENT &&
  59. ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0) {
  60. while (len >= 16) {
  61. for (n = 0; n < 16; ++n) {
  62. out[n] = in[n] ^ iv[n];
  63. }
  64. (*block)(out, out, key);
  65. iv = out;
  66. len -= 16;
  67. in += 16;
  68. out += 16;
  69. }
  70. } else {
  71. while (len >= 16) {
  72. for (n = 0; n < 16; n += sizeof(size_t)) {
  73. *(size_t *)(out + n) = *(size_t *)(in + n) ^ *(size_t *)(iv + n);
  74. }
  75. (*block)(out, out, key);
  76. iv = out;
  77. len -= 16;
  78. in += 16;
  79. out += 16;
  80. }
  81. }
  82. while (len) {
  83. for (n = 0; n < 16 && n < len; ++n) {
  84. out[n] = in[n] ^ iv[n];
  85. }
  86. for (; n < 16; ++n) {
  87. out[n] = iv[n];
  88. }
  89. (*block)(out, out, key);
  90. iv = out;
  91. if (len <= 16) {
  92. break;
  93. }
  94. len -= 16;
  95. in += 16;
  96. out += 16;
  97. }
  98. OPENSSL_memcpy(ivec, iv, 16);
  99. }
  100. void CRYPTO_cbc128_decrypt(const uint8_t *in, uint8_t *out, size_t len,
  101. const void *key, uint8_t ivec[16],
  102. block128_f block) {
  103. size_t n;
  104. union {
  105. size_t t[16 / sizeof(size_t)];
  106. uint8_t c[16];
  107. } tmp;
  108. assert(key != NULL && ivec != NULL);
  109. assert(len == 0 || (in != NULL && out != NULL));
  110. const uintptr_t inptr = (uintptr_t) in;
  111. const uintptr_t outptr = (uintptr_t) out;
  112. /* If |in| and |out| alias, |in| must be ahead. */
  113. assert(inptr >= outptr || inptr + len <= outptr);
  114. if ((inptr >= 32 && outptr <= inptr - 32) || inptr < outptr) {
  115. /* If |out| is at least two blocks behind |in| or completely disjoint, there
  116. * is no need to decrypt to a temporary block. */
  117. const uint8_t *iv = ivec;
  118. if (STRICT_ALIGNMENT &&
  119. ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0) {
  120. while (len >= 16) {
  121. (*block)(in, out, key);
  122. for (n = 0; n < 16; ++n) {
  123. out[n] ^= iv[n];
  124. }
  125. iv = in;
  126. len -= 16;
  127. in += 16;
  128. out += 16;
  129. }
  130. } else if (16 % sizeof(size_t) == 0) { /* always true */
  131. while (len >= 16) {
  132. size_t *out_t = (size_t *)out, *iv_t = (size_t *)iv;
  133. (*block)(in, out, key);
  134. for (n = 0; n < 16 / sizeof(size_t); n++) {
  135. out_t[n] ^= iv_t[n];
  136. }
  137. iv = in;
  138. len -= 16;
  139. in += 16;
  140. out += 16;
  141. }
  142. }
  143. OPENSSL_memcpy(ivec, iv, 16);
  144. } else {
  145. /* |out| is less than two blocks behind |in|. Decrypting an input block
  146. * directly to |out| would overwrite a ciphertext block before it is used as
  147. * the next block's IV. Decrypt to a temporary block instead. */
  148. if (STRICT_ALIGNMENT &&
  149. ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0) {
  150. uint8_t c;
  151. while (len >= 16) {
  152. (*block)(in, tmp.c, key);
  153. for (n = 0; n < 16; ++n) {
  154. c = in[n];
  155. out[n] = tmp.c[n] ^ ivec[n];
  156. ivec[n] = c;
  157. }
  158. len -= 16;
  159. in += 16;
  160. out += 16;
  161. }
  162. } else if (16 % sizeof(size_t) == 0) { /* always true */
  163. while (len >= 16) {
  164. size_t c, *out_t = (size_t *)out, *ivec_t = (size_t *)ivec;
  165. const size_t *in_t = (const size_t *)in;
  166. (*block)(in, tmp.c, key);
  167. for (n = 0; n < 16 / sizeof(size_t); n++) {
  168. c = in_t[n];
  169. out_t[n] = tmp.t[n] ^ ivec_t[n];
  170. ivec_t[n] = c;
  171. }
  172. len -= 16;
  173. in += 16;
  174. out += 16;
  175. }
  176. }
  177. }
  178. while (len) {
  179. uint8_t c;
  180. (*block)(in, tmp.c, key);
  181. for (n = 0; n < 16 && n < len; ++n) {
  182. c = in[n];
  183. out[n] = tmp.c[n] ^ ivec[n];
  184. ivec[n] = c;
  185. }
  186. if (len <= 16) {
  187. for (; n < 16; ++n) {
  188. ivec[n] = in[n];
  189. }
  190. break;
  191. }
  192. len -= 16;
  193. in += 16;
  194. out += 16;
  195. }
  196. }