m_sha3.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Copyright 2017-2018 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 "internal/evp_int.h"
  14. #include "evp_locl.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. /*
  91. * Pad the data with 10*1. Note that |num| can be |bsz - 1|
  92. * in which case both byte operations below are performed on
  93. * same byte...
  94. */
  95. memset(ctx->buf + num, 0, bsz - num);
  96. ctx->buf[num] = ctx->pad;
  97. ctx->buf[bsz - 1] |= 0x80;
  98. (void)SHA3_absorb(ctx->A, ctx->buf, bsz, bsz);
  99. SHA3_squeeze(ctx->A, md, ctx->md_size, bsz);
  100. return 1;
  101. }
  102. static int shake_ctrl(EVP_MD_CTX *evp_ctx, int cmd, int p1, void *p2)
  103. {
  104. KECCAK1600_CTX *ctx = evp_ctx->md_data;
  105. switch (cmd) {
  106. case EVP_MD_CTRL_XOF_LEN:
  107. ctx->md_size = p1;
  108. return 1;
  109. default:
  110. return 0;
  111. }
  112. }
  113. #if defined(OPENSSL_CPUID_OBJ) && defined(__s390__) && defined(KECCAK1600_ASM)
  114. /*
  115. * IBM S390X support
  116. */
  117. # include "s390x_arch.h"
  118. # define S390X_SHA3_FC(ctx) ((ctx)->pad)
  119. # define S390X_sha3_224_CAPABLE ((OPENSSL_s390xcap_P.kimd[0] & \
  120. S390X_CAPBIT(S390X_SHA3_224)) && \
  121. (OPENSSL_s390xcap_P.klmd[0] & \
  122. S390X_CAPBIT(S390X_SHA3_224)))
  123. # define S390X_sha3_256_CAPABLE ((OPENSSL_s390xcap_P.kimd[0] & \
  124. S390X_CAPBIT(S390X_SHA3_256)) && \
  125. (OPENSSL_s390xcap_P.klmd[0] & \
  126. S390X_CAPBIT(S390X_SHA3_256)))
  127. # define S390X_sha3_384_CAPABLE ((OPENSSL_s390xcap_P.kimd[0] & \
  128. S390X_CAPBIT(S390X_SHA3_384)) && \
  129. (OPENSSL_s390xcap_P.klmd[0] & \
  130. S390X_CAPBIT(S390X_SHA3_384)))
  131. # define S390X_sha3_512_CAPABLE ((OPENSSL_s390xcap_P.kimd[0] & \
  132. S390X_CAPBIT(S390X_SHA3_512)) && \
  133. (OPENSSL_s390xcap_P.klmd[0] & \
  134. S390X_CAPBIT(S390X_SHA3_512)))
  135. # define S390X_shake128_CAPABLE ((OPENSSL_s390xcap_P.kimd[0] & \
  136. S390X_CAPBIT(S390X_SHAKE_128)) && \
  137. (OPENSSL_s390xcap_P.klmd[0] & \
  138. S390X_CAPBIT(S390X_SHAKE_128)))
  139. # define S390X_shake256_CAPABLE ((OPENSSL_s390xcap_P.kimd[0] & \
  140. S390X_CAPBIT(S390X_SHAKE_256)) && \
  141. (OPENSSL_s390xcap_P.klmd[0] & \
  142. S390X_CAPBIT(S390X_SHAKE_256)))
  143. /* Convert md-size to block-size. */
  144. # define S390X_KECCAK1600_BSZ(n) ((KECCAK1600_WIDTH - ((n) << 1)) >> 3)
  145. static int s390x_sha3_init(EVP_MD_CTX *evp_ctx)
  146. {
  147. KECCAK1600_CTX *ctx = evp_ctx->md_data;
  148. const size_t bsz = evp_ctx->digest->block_size;
  149. /*-
  150. * KECCAK1600_CTX structure's pad field is used to store the KIMD/KLMD
  151. * function code.
  152. */
  153. switch (bsz) {
  154. case S390X_KECCAK1600_BSZ(224):
  155. ctx->pad = S390X_SHA3_224;
  156. break;
  157. case S390X_KECCAK1600_BSZ(256):
  158. ctx->pad = S390X_SHA3_256;
  159. break;
  160. case S390X_KECCAK1600_BSZ(384):
  161. ctx->pad = S390X_SHA3_384;
  162. break;
  163. case S390X_KECCAK1600_BSZ(512):
  164. ctx->pad = S390X_SHA3_512;
  165. break;
  166. default:
  167. return 0;
  168. }
  169. memset(ctx->A, 0, sizeof(ctx->A));
  170. ctx->num = 0;
  171. ctx->block_size = bsz;
  172. ctx->md_size = evp_ctx->digest->md_size;
  173. return 1;
  174. }
  175. static int s390x_shake_init(EVP_MD_CTX *evp_ctx)
  176. {
  177. KECCAK1600_CTX *ctx = evp_ctx->md_data;
  178. const size_t bsz = evp_ctx->digest->block_size;
  179. /*-
  180. * KECCAK1600_CTX structure's pad field is used to store the KIMD/KLMD
  181. * function code.
  182. */
  183. switch (bsz) {
  184. case S390X_KECCAK1600_BSZ(128):
  185. ctx->pad = S390X_SHAKE_128;
  186. break;
  187. case S390X_KECCAK1600_BSZ(256):
  188. ctx->pad = S390X_SHAKE_256;
  189. break;
  190. default:
  191. return 0;
  192. }
  193. memset(ctx->A, 0, sizeof(ctx->A));
  194. ctx->num = 0;
  195. ctx->block_size = bsz;
  196. ctx->md_size = evp_ctx->digest->md_size;
  197. return 1;
  198. }
  199. static int s390x_sha3_update(EVP_MD_CTX *evp_ctx, const void *_inp, size_t len)
  200. {
  201. KECCAK1600_CTX *ctx = evp_ctx->md_data;
  202. const unsigned char *inp = _inp;
  203. const size_t bsz = ctx->block_size;
  204. size_t num, rem;
  205. if (len == 0)
  206. return 1;
  207. if ((num = ctx->num) != 0) {
  208. rem = bsz - num;
  209. if (len < rem) {
  210. memcpy(ctx->buf + num, inp, len);
  211. ctx->num += len;
  212. return 1;
  213. }
  214. memcpy(ctx->buf + num, inp, rem);
  215. inp += rem;
  216. len -= rem;
  217. s390x_kimd(ctx->buf, bsz, ctx->pad, ctx->A);
  218. ctx->num = 0;
  219. }
  220. rem = len % bsz;
  221. s390x_kimd(inp, len - rem, ctx->pad, ctx->A);
  222. if (rem) {
  223. memcpy(ctx->buf, inp + len - rem, rem);
  224. ctx->num = rem;
  225. }
  226. return 1;
  227. }
  228. static int s390x_sha3_final(EVP_MD_CTX *evp_ctx, unsigned char *md)
  229. {
  230. KECCAK1600_CTX *ctx = evp_ctx->md_data;
  231. s390x_klmd(ctx->buf, ctx->num, NULL, 0, ctx->pad, ctx->A);
  232. memcpy(md, ctx->A, ctx->md_size);
  233. return 1;
  234. }
  235. static int s390x_shake_final(EVP_MD_CTX *evp_ctx, unsigned char *md)
  236. {
  237. KECCAK1600_CTX *ctx = evp_ctx->md_data;
  238. s390x_klmd(ctx->buf, ctx->num, md, ctx->md_size, ctx->pad, ctx->A);
  239. return 1;
  240. }
  241. # define EVP_MD_SHA3(bitlen) \
  242. const EVP_MD *EVP_sha3_##bitlen(void) \
  243. { \
  244. static const EVP_MD s390x_sha3_##bitlen##_md = { \
  245. NID_sha3_##bitlen, \
  246. NID_RSA_SHA3_##bitlen, \
  247. bitlen / 8, \
  248. EVP_MD_FLAG_DIGALGID_ABSENT, \
  249. s390x_sha3_init, \
  250. s390x_sha3_update, \
  251. s390x_sha3_final, \
  252. NULL, \
  253. NULL, \
  254. (KECCAK1600_WIDTH - bitlen * 2) / 8, \
  255. sizeof(KECCAK1600_CTX), \
  256. }; \
  257. static const EVP_MD sha3_##bitlen##_md = { \
  258. NID_sha3_##bitlen, \
  259. NID_RSA_SHA3_##bitlen, \
  260. bitlen / 8, \
  261. EVP_MD_FLAG_DIGALGID_ABSENT, \
  262. sha3_init, \
  263. sha3_update, \
  264. sha3_final, \
  265. NULL, \
  266. NULL, \
  267. (KECCAK1600_WIDTH - bitlen * 2) / 8, \
  268. sizeof(KECCAK1600_CTX), \
  269. }; \
  270. return S390X_sha3_##bitlen##_CAPABLE ? \
  271. &s390x_sha3_##bitlen##_md : \
  272. &sha3_##bitlen##_md; \
  273. }
  274. # define EVP_MD_SHAKE(bitlen) \
  275. const EVP_MD *EVP_shake##bitlen(void) \
  276. { \
  277. static const EVP_MD s390x_shake##bitlen##_md = { \
  278. NID_shake##bitlen, \
  279. 0, \
  280. bitlen / 8, \
  281. EVP_MD_FLAG_XOF, \
  282. s390x_shake_init, \
  283. s390x_sha3_update, \
  284. s390x_shake_final, \
  285. NULL, \
  286. NULL, \
  287. (KECCAK1600_WIDTH - bitlen * 2) / 8, \
  288. sizeof(KECCAK1600_CTX), \
  289. shake_ctrl \
  290. }; \
  291. static const EVP_MD shake##bitlen##_md = { \
  292. NID_shake##bitlen, \
  293. 0, \
  294. bitlen / 8, \
  295. EVP_MD_FLAG_XOF, \
  296. shake_init, \
  297. sha3_update, \
  298. sha3_final, \
  299. NULL, \
  300. NULL, \
  301. (KECCAK1600_WIDTH - bitlen * 2) / 8, \
  302. sizeof(KECCAK1600_CTX), \
  303. shake_ctrl \
  304. }; \
  305. return S390X_shake##bitlen##_CAPABLE ? \
  306. &s390x_shake##bitlen##_md : \
  307. &shake##bitlen##_md; \
  308. }
  309. #else
  310. # define EVP_MD_SHA3(bitlen) \
  311. const EVP_MD *EVP_sha3_##bitlen(void) \
  312. { \
  313. static const EVP_MD sha3_##bitlen##_md = { \
  314. NID_sha3_##bitlen, \
  315. NID_RSA_SHA3_##bitlen, \
  316. bitlen / 8, \
  317. EVP_MD_FLAG_DIGALGID_ABSENT, \
  318. sha3_init, \
  319. sha3_update, \
  320. sha3_final, \
  321. NULL, \
  322. NULL, \
  323. (KECCAK1600_WIDTH - bitlen * 2) / 8, \
  324. sizeof(KECCAK1600_CTX), \
  325. }; \
  326. return &sha3_##bitlen##_md; \
  327. }
  328. # define EVP_MD_SHAKE(bitlen) \
  329. const EVP_MD *EVP_shake##bitlen(void) \
  330. { \
  331. static const EVP_MD shake##bitlen##_md = { \
  332. NID_shake##bitlen, \
  333. 0, \
  334. bitlen / 8, \
  335. EVP_MD_FLAG_XOF, \
  336. shake_init, \
  337. sha3_update, \
  338. sha3_final, \
  339. NULL, \
  340. NULL, \
  341. (KECCAK1600_WIDTH - bitlen * 2) / 8, \
  342. sizeof(KECCAK1600_CTX), \
  343. shake_ctrl \
  344. }; \
  345. return &shake##bitlen##_md; \
  346. }
  347. #endif
  348. EVP_MD_SHA3(224)
  349. EVP_MD_SHA3(256)
  350. EVP_MD_SHA3(384)
  351. EVP_MD_SHA3(512)
  352. EVP_MD_SHAKE(128)
  353. EVP_MD_SHAKE(256)