25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

217 satır
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. #ifndef STRICT_ALIGNMENT
  52. # define STRICT_ALIGNMENT 0
  53. #endif
  54. void CRYPTO_cbc128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
  55. const void *key, uint8_t ivec[16],
  56. block128_f block) {
  57. size_t n;
  58. const uint8_t *iv = ivec;
  59. assert(key != NULL && ivec != NULL);
  60. assert(len == 0 || (in != NULL && out != NULL));
  61. if (STRICT_ALIGNMENT &&
  62. ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0) {
  63. while (len >= 16) {
  64. for (n = 0; n < 16; ++n) {
  65. out[n] = in[n] ^ iv[n];
  66. }
  67. (*block)(out, out, key);
  68. iv = out;
  69. len -= 16;
  70. in += 16;
  71. out += 16;
  72. }
  73. } else {
  74. while (len >= 16) {
  75. for (n = 0; n < 16; n += sizeof(size_t)) {
  76. *(size_t *)(out + n) = *(size_t *)(in + n) ^ *(size_t *)(iv + n);
  77. }
  78. (*block)(out, out, key);
  79. iv = out;
  80. len -= 16;
  81. in += 16;
  82. out += 16;
  83. }
  84. }
  85. while (len) {
  86. for (n = 0; n < 16 && n < len; ++n) {
  87. out[n] = in[n] ^ iv[n];
  88. }
  89. for (; n < 16; ++n) {
  90. out[n] = iv[n];
  91. }
  92. (*block)(out, out, key);
  93. iv = out;
  94. if (len <= 16) {
  95. break;
  96. }
  97. len -= 16;
  98. in += 16;
  99. out += 16;
  100. }
  101. memcpy(ivec, iv, 16);
  102. }
  103. void CRYPTO_cbc128_decrypt(const uint8_t *in, uint8_t *out, size_t len,
  104. const void *key, uint8_t ivec[16],
  105. block128_f block) {
  106. size_t n;
  107. union {
  108. size_t t[16 / sizeof(size_t)];
  109. uint8_t c[16];
  110. } tmp;
  111. assert(key != NULL && ivec != NULL);
  112. assert(len == 0 || (in != NULL && out != NULL));
  113. const uintptr_t inptr = (uintptr_t) in;
  114. const uintptr_t outptr = (uintptr_t) out;
  115. /* If |in| and |out| alias, |in| must be ahead. */
  116. assert(inptr >= outptr || inptr + len <= outptr);
  117. if ((inptr >= 32 && outptr <= inptr - 32) || inptr < outptr) {
  118. /* If |out| is at least two blocks behind |in| or completely disjoint, there
  119. * is no need to decrypt to a temporary block. */
  120. const uint8_t *iv = ivec;
  121. if (STRICT_ALIGNMENT &&
  122. ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0) {
  123. while (len >= 16) {
  124. (*block)(in, out, key);
  125. for (n = 0; n < 16; ++n) {
  126. out[n] ^= iv[n];
  127. }
  128. iv = in;
  129. len -= 16;
  130. in += 16;
  131. out += 16;
  132. }
  133. } else if (16 % sizeof(size_t) == 0) { /* always true */
  134. while (len >= 16) {
  135. size_t *out_t = (size_t *)out, *iv_t = (size_t *)iv;
  136. (*block)(in, out, key);
  137. for (n = 0; n < 16 / sizeof(size_t); n++) {
  138. out_t[n] ^= iv_t[n];
  139. }
  140. iv = in;
  141. len -= 16;
  142. in += 16;
  143. out += 16;
  144. }
  145. }
  146. memcpy(ivec, iv, 16);
  147. } else {
  148. /* |out| is less than two blocks behind |in|. Decrypting an input block
  149. * directly to |out| would overwrite a ciphertext block before it is used as
  150. * the next block's IV. Decrypt to a temporary block instead. */
  151. if (STRICT_ALIGNMENT &&
  152. ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0) {
  153. uint8_t c;
  154. while (len >= 16) {
  155. (*block)(in, tmp.c, key);
  156. for (n = 0; n < 16; ++n) {
  157. c = in[n];
  158. out[n] = tmp.c[n] ^ ivec[n];
  159. ivec[n] = c;
  160. }
  161. len -= 16;
  162. in += 16;
  163. out += 16;
  164. }
  165. } else if (16 % sizeof(size_t) == 0) { /* always true */
  166. while (len >= 16) {
  167. size_t c, *out_t = (size_t *)out, *ivec_t = (size_t *)ivec;
  168. const size_t *in_t = (const size_t *)in;
  169. (*block)(in, tmp.c, key);
  170. for (n = 0; n < 16 / sizeof(size_t); n++) {
  171. c = in_t[n];
  172. out_t[n] = tmp.t[n] ^ ivec_t[n];
  173. ivec_t[n] = c;
  174. }
  175. len -= 16;
  176. in += 16;
  177. out += 16;
  178. }
  179. }
  180. }
  181. while (len) {
  182. uint8_t c;
  183. (*block)(in, tmp.c, key);
  184. for (n = 0; n < 16 && n < len; ++n) {
  185. c = in[n];
  186. out[n] = tmp.c[n] ^ ivec[n];
  187. ivec[n] = c;
  188. }
  189. if (len <= 16) {
  190. for (; n < 16; ++n) {
  191. ivec[n] = in[n];
  192. }
  193. break;
  194. }
  195. len -= 16;
  196. in += 16;
  197. out += 16;
  198. }
  199. }