pcl_sha256.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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/sha/sha256.c */
  85. #include <openssl/aes.h>
  86. #include <openssl/sha.h>
  87. #include <openssl/modes.h>
  88. #include <sgx_tseal.h>
  89. #include <pcl_common.h>
  90. #include <pcl_internal.h>
  91. #include <pcl_crypto_internal.h>
  92. int pcl_SHA256_Init(SHA256_CTX *c)
  93. {
  94. pcl_memset(c, 0, sizeof(*c));
  95. c->h[0] = 0x6a09e667UL;
  96. c->h[1] = 0xbb67ae85UL;
  97. c->h[2] = 0x3c6ef372UL;
  98. c->h[3] = 0xa54ff53aUL;
  99. c->h[4] = 0x510e527fUL;
  100. c->h[5] = 0x9b05688cUL;
  101. c->h[6] = 0x1f83d9abUL;
  102. c->h[7] = 0x5be0cd19UL;
  103. c->md_len = SHA256_DIGEST_LENGTH;
  104. return 1;
  105. }
  106. #define HASH_MAKE_STRING(c,s) do { \
  107. unsigned long ll; \
  108. unsigned int nn; \
  109. for (nn=0;nn<SHA256_DIGEST_LENGTH/4;nn++) \
  110. { ll=(c)->h[nn]; (void)HOST_l2c(ll,(s)); } \
  111. } while (0)
  112. static void pcl_sha256_block_data_order(SHA256_CTX *ctx, const void *in, size_t num);
  113. #include <pcl_md32_common.h>
  114. static const SHA_LONG pcl_K256[64] = {
  115. 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
  116. 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
  117. 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
  118. 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
  119. 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
  120. 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
  121. 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
  122. 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
  123. 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
  124. 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
  125. 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
  126. 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
  127. 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
  128. 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
  129. 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
  130. 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
  131. };
  132. /*
  133. * FIPS specification refers to right rotations, while our ROTATE macro
  134. * is left one. This is why you might notice that rotation coefficients
  135. * differ from those observed in FIPS document by 32-N...
  136. */
  137. # define Sigma0(x) (ROTATE((x),30) ^ ROTATE((x),19) ^ ROTATE((x),10))
  138. # define Sigma1(x) (ROTATE((x),26) ^ ROTATE((x),21) ^ ROTATE((x),7))
  139. # define sigma0(x) (ROTATE((x),25) ^ ROTATE((x),14) ^ ((x)>>3))
  140. # define sigma1(x) (ROTATE((x),15) ^ ROTATE((x),13) ^ ((x)>>10))
  141. # define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
  142. # define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
  143. static void pcl_sha256_block_data_order(SHA256_CTX *ctx, const void *in,
  144. size_t num)
  145. {
  146. unsigned MD32_REG_T a, b, c, d, e, f, g, h, s0, s1, T1, T2;
  147. SHA_LONG X[16], l;
  148. int i;
  149. const unsigned char *data = (const unsigned char *) in;
  150. while (num--) {
  151. a = ctx->h[0];
  152. b = ctx->h[1];
  153. c = ctx->h[2];
  154. d = ctx->h[3];
  155. e = ctx->h[4];
  156. f = ctx->h[5];
  157. g = ctx->h[6];
  158. h = ctx->h[7];
  159. for (i = 0; i < 16; i++) {
  160. (void)HOST_c2l(data, l);
  161. T1 = X[i] = l;
  162. T1 += h + Sigma1(e) + Ch(e, f, g) + pcl_K256[i];
  163. T2 = Sigma0(a) + Maj(a, b, c);
  164. h = g;
  165. g = f;
  166. f = e;
  167. e = d + T1;
  168. d = c;
  169. c = b;
  170. b = a;
  171. a = T1 + T2;
  172. }
  173. for (; i < 64; i++) {
  174. s0 = X[(i + 1) & 0x0f];
  175. s0 = sigma0(s0);
  176. s1 = X[(i + 14) & 0x0f];
  177. s1 = sigma1(s1);
  178. T1 = X[i & 0xf] += s0 + s1 + X[(i + 9) & 0xf];
  179. T1 += h + Sigma1(e) + Ch(e, f, g) + pcl_K256[i];
  180. T2 = Sigma0(a) + Maj(a, b, c);
  181. h = g;
  182. g = f;
  183. f = e;
  184. e = d + T1;
  185. d = c;
  186. c = b;
  187. b = a;
  188. a = T1 + T2;
  189. }
  190. ctx->h[0] += a;
  191. ctx->h[1] += b;
  192. ctx->h[2] += c;
  193. ctx->h[3] += d;
  194. ctx->h[4] += e;
  195. ctx->h[5] += f;
  196. ctx->h[6] += g;
  197. ctx->h[7] += h;
  198. }
  199. }