pcl_md32_common.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (C) 2011-2018 Intel Corporation. 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. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. /* ====================================================================
  32. * Copyright (c) 1998-2017 The OpenSSL Project. All rights reserved.
  33. *
  34. * Redistribution and use in source and binary forms, with or without
  35. * modification, are permitted provided that the following conditions
  36. * are met:
  37. *
  38. * 1. Redistributions of source code must retain the above copyright
  39. * notice, this list of conditions and the following disclaimer.
  40. *
  41. * 2. Redistributions in binary form must reproduce the above copyright
  42. * notice, this list of conditions and the following disclaimer in
  43. * the documentation and/or other materials provided with the
  44. * distribution.
  45. *
  46. * 3. All advertising materials mentioning features or use of this
  47. * software must display the following acknowledgment:
  48. * "This product includes software developed by the OpenSSL Project
  49. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  50. *
  51. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  52. * endorse or promote products derived from this software without
  53. * prior written permission. For written permission, please contact
  54. * openssl-core@openssl.org.
  55. *
  56. * 5. Products derived from this software may not be called "OpenSSL"
  57. * nor may "OpenSSL" appear in their names without prior written
  58. * permission of the OpenSSL Project.
  59. *
  60. * 6. Redistributions of any form whatsoever must retain the following
  61. * acknowledgment:
  62. * "This product includes software developed by the OpenSSL Project
  63. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  64. *
  65. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  66. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  67. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  68. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  69. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  70. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  71. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  72. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  73. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  74. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  75. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  76. * OF THE POSSIBILITY OF SUCH DAMAGE.
  77. * ====================================================================
  78. *
  79. * This product includes cryptographic software written by Eric Young
  80. * (eay@cryptsoft.com). This product includes software written by Tim
  81. * Hudson (tjh@cryptsoft.com).
  82. *
  83. */
  84. /* Content from openssl-1.1.0e/crypto/include/internal/md32_common.h */
  85. #ifndef SGX_PCL_MD32_COMMON_H
  86. #define SGX_PCL_MD32_COMMON_H
  87. # define ROTATE(a,n) ({ register unsigned int ret; \
  88. asm ( \
  89. "roll %1,%0" \
  90. : "=r"(ret) \
  91. : "I"(n), "0"((unsigned int)(a)) \
  92. : "cc"); \
  93. ret; \
  94. })
  95. # define HOST_c2l(c,l) ({ unsigned int r=*((const unsigned int *)(c)); \
  96. asm ("bswapl %0":"=r"(r):"0"(r)); \
  97. (c)+=4; (l)=r; })
  98. # define HOST_l2c(l,c) ({ unsigned int r=(l); \
  99. asm ("bswapl %0":"=r"(r):"0"(r)); \
  100. *((unsigned int *)(c))=r; (c)+=4; r; })
  101. int pcl_SHA256_Update(SHA256_CTX *c, void *data_, size_t len)
  102. {
  103. unsigned char *data = (unsigned char *)data_;
  104. unsigned char *p;
  105. SHA_LONG l;
  106. size_t n;
  107. if (len == 0)
  108. return 1;
  109. l = (c->Nl + (((SHA_LONG) len) << 3)) & 0xffffffffUL;
  110. /*
  111. * 95-05-24 eay Fixed a bug with the overflow handling, thanks to Wei Dai
  112. * <weidai@eskimo.com> for pointing it out.
  113. */
  114. if (l < c->Nl) /* overflow */
  115. c->Nh++;
  116. c->Nh += (SHA_LONG) (len >> 29); /* might cause compiler warning on
  117. * 16-bit */
  118. c->Nl = l;
  119. /* PCL UNUSED START *
  120. n = c->num;
  121. if (n != 0) {
  122. p = (unsigned char *)c->data;
  123. if (len >= SHA_CBLOCK || len + n >= SHA_CBLOCK) {
  124. pcl_memcpy(p + n, data, SHA_CBLOCK - n);
  125. pcl_sha256_block_data_order(c, p, 1);
  126. n = SHA_CBLOCK - n;
  127. data += n;
  128. len -= n;
  129. c->num = 0;
  130. /*
  131. * We use memset rather than OPENSSL_cleanse() here deliberately.
  132. * Using OPENSSL_cleanse() here could be a performance issue. It
  133. * will get properly cleansed on finalisation so this isn't a
  134. * security problem.
  135. * /
  136. pcl_memset(p, 0, SHA_CBLOCK); /* keep it zeroed * /
  137. } else {
  138. pcl_memcpy(p + n, data, len);
  139. c->num += (unsigned int)len;
  140. return 1;
  141. }
  142. }
  143. /* PCL UNUSED END */
  144. n = len / SHA_CBLOCK;
  145. if (n > 0) {
  146. pcl_sha256_block_data_order(c, data, n);
  147. n *= SHA_CBLOCK;
  148. data += n;
  149. len -= n;
  150. }
  151. if (len != 0) {
  152. p = (unsigned char *)c->data;
  153. c->num = (unsigned int)len;
  154. pcl_memcpy(p, data, len);
  155. }
  156. return 1;
  157. }
  158. int pcl_SHA256_Final(unsigned char *md, SHA256_CTX *c)
  159. {
  160. unsigned char *p = (unsigned char *)c->data;
  161. size_t n = c->num;
  162. p[n] = 0x80; /* there is always room for one */
  163. n++;
  164. if (n > (SHA_CBLOCK - 8)) {
  165. pcl_memset(p + n, 0, SHA_CBLOCK - n);
  166. n = 0;
  167. pcl_sha256_block_data_order(c, p, 1);
  168. }
  169. pcl_memset(p + n, 0, SHA_CBLOCK - 8 - n);
  170. p += SHA_CBLOCK - 8;
  171. /* PCL UNUSED START *
  172. #if defined(DATA_ORDER_IS_BIG_ENDIAN)
  173. /* PCL UNUSED END */
  174. (void)HOST_l2c(c->Nh, p);
  175. (void)HOST_l2c(c->Nl, p);
  176. /* PCL UNUSED START *
  177. #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
  178. (void)HOST_l2c(c->Nl, p);
  179. (void)HOST_l2c(c->Nh, p);
  180. #endif
  181. /* PCL UNUSED END */
  182. p -= SHA_CBLOCK;
  183. pcl_sha256_block_data_order(c, p, 1);
  184. c->num = 0;
  185. pcl_memset(p, 0, SHA_CBLOCK);
  186. /* PCL UNUSED START *
  187. #ifndef HASH_MAKE_STRING
  188. # error "HASH_MAKE_STRING must be defined!"
  189. #else
  190. /* PCL UNUSED END */
  191. HASH_MAKE_STRING(c, md);
  192. /* PCL UNUSED START *
  193. #endif
  194. /* PCL UNUSED END */
  195. return 1;
  196. }
  197. #define MD32_REG_T int
  198. #endif // #ifndef SGX_PCL_MD32_COMMON_H