m_sha3.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * Copyright 2017-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 <stdio.h>
  10. #include <string.h>
  11. #include <openssl/evp.h>
  12. #include <openssl/objects.h>
  13. #include "crypto/evp.h"
  14. #include "evp_local.h"
  15. size_t SHA3_absorb(uint64_t A[5][5], const unsigned char *inp, size_t len,
  16. size_t r);
  17. void SHA3_squeeze(uint64_t A[5][5], unsigned char *out, size_t len, size_t r);
  18. #define KECCAK1600_WIDTH 1600
  19. typedef struct {
  20. uint64_t A[5][5];
  21. size_t block_size; /* cached ctx->digest->block_size */
  22. size_t md_size; /* output length, variable in XOF */
  23. size_t num; /* used bytes in below buffer */
  24. unsigned char buf[KECCAK1600_WIDTH / 8 - 32];
  25. unsigned char pad;
  26. } KECCAK1600_CTX;
  27. static int init(EVP_MD_CTX *evp_ctx, unsigned char pad)
  28. {
  29. KECCAK1600_CTX *ctx = evp_ctx->md_data;
  30. size_t bsz = evp_ctx->digest->block_size;
  31. if (bsz <= sizeof(ctx->buf)) {
  32. memset(ctx->A, 0, sizeof(ctx->A));
  33. ctx->num = 0;
  34. ctx->block_size = bsz;
  35. ctx->md_size = evp_ctx->digest->md_size;
  36. ctx->pad = pad;
  37. return 1;
  38. }
  39. return 0;
  40. }
  41. static int sha3_init(EVP_MD_CTX *evp_ctx)
  42. {
  43. return init(evp_ctx, '\x06');
  44. }
  45. static int shake_init(EVP_MD_CTX *evp_ctx)
  46. {
  47. return init(evp_ctx, '\x1f');
  48. }
  49. static int sha3_update(EVP_MD_CTX *evp_ctx, const void *_inp, size_t len)
  50. {
  51. KECCAK1600_CTX *ctx = evp_ctx->md_data;
  52. const unsigned char *inp = _inp;
  53. size_t bsz = ctx->block_size;
  54. size_t num, rem;
  55. if (len == 0)
  56. return 1;
  57. if ((num = ctx->num) != 0) { /* process intermediate buffer? */
  58. rem = bsz - num;
  59. if (len < rem) {
  60. memcpy(ctx->buf + num, inp, len);
  61. ctx->num += len;
  62. return 1;
  63. }
  64. /*
  65. * We have enough data to fill or overflow the intermediate
  66. * buffer. So we append |rem| bytes and process the block,
  67. * leaving the rest for later processing...
  68. */
  69. memcpy(ctx->buf + num, inp, rem);
  70. inp += rem, len -= rem;
  71. (void)SHA3_absorb(ctx->A, ctx->buf, bsz, bsz);
  72. ctx->num = 0;
  73. /* ctx->buf is processed, ctx->num is guaranteed to be zero */
  74. }
  75. if (len >= bsz)
  76. rem = SHA3_absorb(ctx->A, inp, len, bsz);
  77. else
  78. rem = len;
  79. if (rem) {
  80. memcpy(ctx->buf, inp + len - rem, rem);
  81. ctx->num = rem;
  82. }
  83. return 1;
  84. }
  85. static int sha3_final(EVP_MD_CTX *evp_ctx, unsigned char *md)
  86. {
  87. KECCAK1600_CTX *ctx = evp_ctx->md_data;
  88. size_t bsz = ctx->block_size;
  89. size_t num = ctx->num;
  90. if (ctx->md_size == 0)
  91. return 1;
  92. /*
  93. * Pad the data with 10*1. Note that |num| can be |bsz - 1|
  94. * in which case both byte operations below are performed on
  95. * same byte...
  96. */
  97. memset(ctx->buf + num, 0, bsz - num);
  98. ctx->buf[num] = ctx->pad;
  99. ctx->buf[bsz - 1] |= 0x80;
  100. (void)SHA3_absorb(ctx->A, ctx->buf, bsz, bsz);
  101. SHA3_squeeze(ctx->A, md, ctx->md_size, bsz);
  102. return 1;
  103. }
  104. static int shake_ctrl(EVP_MD_CTX *evp_ctx, int cmd, int p1, void *p2)
  105. {
  106. KECCAK1600_CTX *ctx = evp_ctx->md_data;
  107. switch (cmd) {
  108. case EVP_MD_CTRL_XOF_LEN:
  109. ctx->md_size = p1;
  110. return 1;
  111. default:
  112. return 0;
  113. }
  114. }
  115. #if defined(OPENSSL_CPUID_OBJ) && defined(__s390__) && defined(KECCAK1600_ASM)
  116. /*
  117. * IBM S390X support
  118. */
  119. # include "s390x_arch.h"
  120. # define S390X_SHA3_FC(ctx) ((ctx)->pad)
  121. # define S390X_sha3_224_CAPABLE ((OPENSSL_s390xcap_P.kimd[0] & \
  122. S390X_CAPBIT(S390X_SHA3_224)) && \
  123. (OPENSSL_s390xcap_P.klmd[0] & \
  124. S390X_CAPBIT(S390X_SHA3_224)))
  125. # define S390X_sha3_256_CAPABLE ((OPENSSL_s390xcap_P.kimd[0] & \
  126. S390X_CAPBIT(S390X_SHA3_256)) && \
  127. (OPENSSL_s390xcap_P.klmd[0] & \
  128. S390X_CAPBIT(S390X_SHA3_256)))
  129. # define S390X_sha3_384_CAPABLE ((OPENSSL_s390xcap_P.kimd[0] & \
  130. S390X_CAPBIT(S390X_SHA3_384)) && \
  131. (OPENSSL_s390xcap_P.klmd[0] & \
  132. S390X_CAPBIT(S390X_SHA3_384)))
  133. # define S390X_sha3_512_CAPABLE ((OPENSSL_s390xcap_P.kimd[0] & \
  134. S390X_CAPBIT(S390X_SHA3_512)) && \
  135. (OPENSSL_s390xcap_P.klmd[0] & \
  136. S390X_CAPBIT(S390X_SHA3_512)))
  137. # define S390X_shake128_CAPABLE ((OPENSSL_s390xcap_P.kimd[0] & \
  138. S390X_CAPBIT(S390X_SHAKE_128)) && \
  139. (OPENSSL_s390xcap_P.klmd[0] & \
  140. S390X_CAPBIT(S390X_SHAKE_128)))
  141. # define S390X_shake256_CAPABLE ((OPENSSL_s390xcap_P.kimd[0] & \
  142. S390X_CAPBIT(S390X_SHAKE_256)) && \
  143. (OPENSSL_s390xcap_P.klmd[0] & \
  144. S390X_CAPBIT(S390X_SHAKE_256)))
  145. /* Convert md-size to block-size. */
  146. # define S390X_KECCAK1600_BSZ(n) ((KECCAK1600_WIDTH - ((n) << 1)) >> 3)
  147. static int s390x_sha3_init(EVP_MD_CTX *evp_ctx)
  148. {
  149. KECCAK1600_CTX *ctx = evp_ctx->md_data;
  150. const size_t bsz = evp_ctx->digest->block_size;
  151. /*-
  152. * KECCAK1600_CTX structure's pad field is used to store the KIMD/KLMD
  153. * function code.
  154. */
  155. switch (bsz) {
  156. case S390X_KECCAK1600_BSZ(224):
  157. ctx->pad = S390X_SHA3_224;
  158. break;
  159. case S390X_KECCAK1600_BSZ(256):
  160. ctx->pad = S390X_SHA3_256;
  161. break;
  162. case S390X_KECCAK1600_BSZ(384):
  163. ctx->pad = S390X_SHA3_384;
  164. break;
  165. case S390X_KECCAK1600_BSZ(512):
  166. ctx->pad = S390X_SHA3_512;
  167. break;
  168. default:
  169. return 0;
  170. }
  171. memset(ctx->A, 0, sizeof(ctx->A));
  172. ctx->num = 0;
  173. ctx->block_size = bsz;
  174. ctx->md_size = evp_ctx->digest->md_size;
  175. return 1;
  176. }
  177. static int s390x_shake_init(EVP_MD_CTX *evp_ctx)
  178. {
  179. KECCAK1600_CTX *ctx = evp_ctx->md_data;
  180. const size_t bsz = evp_ctx->digest->block_size;
  181. /*-
  182. * KECCAK1600_CTX structure's pad field is used to store the KIMD/KLMD
  183. * function code.
  184. */
  185. switch (bsz) {
  186. case S390X_KECCAK1600_BSZ(128):
  187. ctx->pad = S390X_SHAKE_128;
  188. break;
  189. case S390X_KECCAK1600_BSZ(256):
  190. ctx->pad = S390X_SHAKE_256;
  191. break;
  192. default:
  193. return 0;
  194. }
  195. memset(ctx->A, 0, sizeof(ctx->A));
  196. ctx->num = 0;
  197. ctx->block_size = bsz;
  198. ctx->md_size = evp_ctx->digest->md_size;
  199. return 1;
  200. }
  201. static int s390x_sha3_update(EVP_MD_CTX *evp_ctx, const void *_inp, size_t len)
  202. {
  203. KECCAK1600_CTX *ctx = evp_ctx->md_data;
  204. const unsigned char *inp = _inp;
  205. const size_t bsz = ctx->block_size;
  206. size_t num, rem;
  207. if (len == 0)
  208. return 1;
  209. if ((num = ctx->num) != 0) {
  210. rem = bsz - num;
  211. if (len < rem) {
  212. memcpy(ctx->buf + num, inp, len);
  213. ctx->num += len;
  214. return 1;
  215. }
  216. memcpy(ctx->buf + num, inp, rem);
  217. inp += rem;
  218. len -= rem;
  219. s390x_kimd(ctx->buf, bsz, ctx->pad, ctx->A);
  220. ctx->num = 0;
  221. }
  222. rem = len % bsz;
  223. s390x_kimd(inp, len - rem, ctx->pad, ctx->A);
  224. if (rem) {
  225. memcpy(ctx->buf, inp + len - rem, rem);
  226. ctx->num = rem;
  227. }
  228. return 1;
  229. }
  230. static int s390x_sha3_final(EVP_MD_CTX *evp_ctx, unsigned char *md)
  231. {
  232. KECCAK1600_CTX *ctx = evp_ctx->md_data;
  233. s390x_klmd(ctx->buf, ctx->num, NULL, 0, ctx->pad, ctx->A);
  234. memcpy(md, ctx->A, ctx->md_size);
  235. return 1;
  236. }
  237. static int s390x_shake_final(EVP_MD_CTX *evp_ctx, unsigned char *md)
  238. {
  239. KECCAK1600_CTX *ctx = evp_ctx->md_data;
  240. s390x_klmd(ctx->buf, ctx->num, md, ctx->md_size, ctx->pad, ctx->A);
  241. return 1;
  242. }
  243. # define EVP_MD_SHA3(bitlen) \
  244. const EVP_MD *EVP_sha3_##bitlen(void) \
  245. { \
  246. static const EVP_MD s390x_sha3_##bitlen##_md = { \
  247. NID_sha3_##bitlen, \
  248. NID_RSA_SHA3_##bitlen, \
  249. bitlen / 8, \
  250. EVP_MD_FLAG_DIGALGID_ABSENT, \
  251. s390x_sha3_init, \
  252. s390x_sha3_update, \
  253. s390x_sha3_final, \
  254. NULL, \
  255. NULL, \
  256. (KECCAK1600_WIDTH - bitlen * 2) / 8, \
  257. sizeof(KECCAK1600_CTX), \
  258. }; \
  259. static const EVP_MD sha3_##bitlen##_md = { \
  260. NID_sha3_##bitlen, \
  261. NID_RSA_SHA3_##bitlen, \
  262. bitlen / 8, \
  263. EVP_MD_FLAG_DIGALGID_ABSENT, \
  264. sha3_init, \
  265. sha3_update, \
  266. sha3_final, \
  267. NULL, \
  268. NULL, \
  269. (KECCAK1600_WIDTH - bitlen * 2) / 8, \
  270. sizeof(KECCAK1600_CTX), \
  271. }; \
  272. return S390X_sha3_##bitlen##_CAPABLE ? \
  273. &s390x_sha3_##bitlen##_md : \
  274. &sha3_##bitlen##_md; \
  275. }
  276. # define EVP_MD_SHAKE(bitlen) \
  277. const EVP_MD *EVP_shake##bitlen(void) \
  278. { \
  279. static const EVP_MD s390x_shake##bitlen##_md = { \
  280. NID_shake##bitlen, \
  281. 0, \
  282. bitlen / 8, \
  283. EVP_MD_FLAG_XOF, \
  284. s390x_shake_init, \
  285. s390x_sha3_update, \
  286. s390x_shake_final, \
  287. NULL, \
  288. NULL, \
  289. (KECCAK1600_WIDTH - bitlen * 2) / 8, \
  290. sizeof(KECCAK1600_CTX), \
  291. shake_ctrl \
  292. }; \
  293. static const EVP_MD shake##bitlen##_md = { \
  294. NID_shake##bitlen, \
  295. 0, \
  296. bitlen / 8, \
  297. EVP_MD_FLAG_XOF, \
  298. shake_init, \
  299. sha3_update, \
  300. sha3_final, \
  301. NULL, \
  302. NULL, \
  303. (KECCAK1600_WIDTH - bitlen * 2) / 8, \
  304. sizeof(KECCAK1600_CTX), \
  305. shake_ctrl \
  306. }; \
  307. return S390X_shake##bitlen##_CAPABLE ? \
  308. &s390x_shake##bitlen##_md : \
  309. &shake##bitlen##_md; \
  310. }
  311. #else
  312. # define EVP_MD_SHA3(bitlen) \
  313. const EVP_MD *EVP_sha3_##bitlen(void) \
  314. { \
  315. static const EVP_MD sha3_##bitlen##_md = { \
  316. NID_sha3_##bitlen, \
  317. NID_RSA_SHA3_##bitlen, \
  318. bitlen / 8, \
  319. EVP_MD_FLAG_DIGALGID_ABSENT, \
  320. sha3_init, \
  321. sha3_update, \
  322. sha3_final, \
  323. NULL, \
  324. NULL, \
  325. (KECCAK1600_WIDTH - bitlen * 2) / 8, \
  326. sizeof(KECCAK1600_CTX), \
  327. }; \
  328. return &sha3_##bitlen##_md; \
  329. }
  330. # define EVP_MD_SHAKE(bitlen) \
  331. const EVP_MD *EVP_shake##bitlen(void) \
  332. { \
  333. static const EVP_MD shake##bitlen##_md = { \
  334. NID_shake##bitlen, \
  335. 0, \
  336. bitlen / 8, \
  337. EVP_MD_FLAG_XOF, \
  338. shake_init, \
  339. sha3_update, \
  340. sha3_final, \
  341. NULL, \
  342. NULL, \
  343. (KECCAK1600_WIDTH - bitlen * 2) / 8, \
  344. sizeof(KECCAK1600_CTX), \
  345. shake_ctrl \
  346. }; \
  347. return &shake##bitlen##_md; \
  348. }
  349. #endif
  350. EVP_MD_SHA3(224)
  351. EVP_MD_SHA3(256)
  352. EVP_MD_SHA3(384)
  353. EVP_MD_SHA3(512)
  354. EVP_MD_SHAKE(128)
  355. EVP_MD_SHAKE(256)