17dc32ad5SXiaodong Liu /**********************************************************************
27dc32ad5SXiaodong Liu Copyright(c) 2011-2017 Intel Corporation All rights reserved.
37dc32ad5SXiaodong Liu
47dc32ad5SXiaodong Liu Redistribution and use in source and binary forms, with or without
57dc32ad5SXiaodong Liu modification, are permitted provided that the following conditions
67dc32ad5SXiaodong Liu are met:
77dc32ad5SXiaodong Liu * Redistributions of source code must retain the above copyright
87dc32ad5SXiaodong Liu notice, this list of conditions and the following disclaimer.
97dc32ad5SXiaodong Liu * Redistributions in binary form must reproduce the above copyright
107dc32ad5SXiaodong Liu notice, this list of conditions and the following disclaimer in
117dc32ad5SXiaodong Liu the documentation and/or other materials provided with the
127dc32ad5SXiaodong Liu distribution.
137dc32ad5SXiaodong Liu * Neither the name of Intel Corporation nor the names of its
147dc32ad5SXiaodong Liu contributors may be used to endorse or promote products derived
157dc32ad5SXiaodong Liu from this software without specific prior written permission.
167dc32ad5SXiaodong Liu
177dc32ad5SXiaodong Liu THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
187dc32ad5SXiaodong Liu "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
197dc32ad5SXiaodong Liu LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
207dc32ad5SXiaodong Liu A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
217dc32ad5SXiaodong Liu OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
227dc32ad5SXiaodong Liu SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
237dc32ad5SXiaodong Liu LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
247dc32ad5SXiaodong Liu DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
257dc32ad5SXiaodong Liu THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
267dc32ad5SXiaodong Liu (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
277dc32ad5SXiaodong Liu OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
287dc32ad5SXiaodong Liu **********************************************************************/
297dc32ad5SXiaodong Liu
307dc32ad5SXiaodong Liu #include <string.h>
317dc32ad5SXiaodong Liu #include "mh_sha256_internal.h"
329de7f4ddSMarcel Cornu #include "isal_crypto_api.h"
337dc32ad5SXiaodong Liu
3438e16e11SMarcel Cornu int
_mh_sha256_init(struct isal_mh_sha256_ctx * ctx)35*f6da0bf4SMarcel Cornu _mh_sha256_init(struct isal_mh_sha256_ctx *ctx)
367dc32ad5SXiaodong Liu {
3727316f25SMarcel Cornu uint32_t(*mh_sha256_segs_digests)[ISAL_HASH_SEGS];
387dc32ad5SXiaodong Liu uint32_t i;
397dc32ad5SXiaodong Liu
407dc32ad5SXiaodong Liu if (ctx == NULL)
4115f45959SMarcel Cornu return ISAL_MH_SHA256_CTX_ERROR_NULL;
427dc32ad5SXiaodong Liu
437dc32ad5SXiaodong Liu memset(ctx, 0, sizeof(*ctx));
447dc32ad5SXiaodong Liu
4527316f25SMarcel Cornu mh_sha256_segs_digests = (uint32_t(*)[ISAL_HASH_SEGS]) ctx->mh_sha256_interim_digests;
4627316f25SMarcel Cornu for (i = 0; i < ISAL_HASH_SEGS; i++) {
477dc32ad5SXiaodong Liu mh_sha256_segs_digests[0][i] = MH_SHA256_H0;
487dc32ad5SXiaodong Liu mh_sha256_segs_digests[1][i] = MH_SHA256_H1;
497dc32ad5SXiaodong Liu mh_sha256_segs_digests[2][i] = MH_SHA256_H2;
507dc32ad5SXiaodong Liu mh_sha256_segs_digests[3][i] = MH_SHA256_H3;
517dc32ad5SXiaodong Liu mh_sha256_segs_digests[4][i] = MH_SHA256_H4;
527dc32ad5SXiaodong Liu mh_sha256_segs_digests[5][i] = MH_SHA256_H5;
537dc32ad5SXiaodong Liu mh_sha256_segs_digests[6][i] = MH_SHA256_H6;
547dc32ad5SXiaodong Liu mh_sha256_segs_digests[7][i] = MH_SHA256_H7;
557dc32ad5SXiaodong Liu }
567dc32ad5SXiaodong Liu
5715f45959SMarcel Cornu return ISAL_MH_SHA256_CTX_ERROR_NONE;
587dc32ad5SXiaodong Liu }
597dc32ad5SXiaodong Liu
6038e16e11SMarcel Cornu int
isal_mh_sha256_init(struct isal_mh_sha256_ctx * ctx)61*f6da0bf4SMarcel Cornu isal_mh_sha256_init(struct isal_mh_sha256_ctx *ctx)
629de7f4ddSMarcel Cornu {
633e138b34SPablo de Lara #ifdef FIPS_MODE
643e138b34SPablo de Lara return ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO;
653e138b34SPablo de Lara #else
669de7f4ddSMarcel Cornu #ifdef SAFE_PARAM
679de7f4ddSMarcel Cornu if (ctx == NULL)
689de7f4ddSMarcel Cornu return ISAL_CRYPTO_ERR_NULL_CTX;
699de7f4ddSMarcel Cornu #endif
70a922b2ebSMarcel Cornu return _mh_sha256_init(ctx);
716a8a917cSPablo de Lara #endif
729de7f4ddSMarcel Cornu }
739de7f4ddSMarcel Cornu
7438e16e11SMarcel Cornu int
isal_mh_sha256_update(struct isal_mh_sha256_ctx * ctx,const void * buffer,uint32_t len)75*f6da0bf4SMarcel Cornu isal_mh_sha256_update(struct isal_mh_sha256_ctx *ctx, const void *buffer, uint32_t len)
769de7f4ddSMarcel Cornu {
773e138b34SPablo de Lara #ifdef FIPS_MODE
783e138b34SPablo de Lara return ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO;
793e138b34SPablo de Lara #else
809de7f4ddSMarcel Cornu #ifdef SAFE_PARAM
819de7f4ddSMarcel Cornu if (ctx == NULL)
829de7f4ddSMarcel Cornu return ISAL_CRYPTO_ERR_NULL_CTX;
839de7f4ddSMarcel Cornu if (buffer == NULL)
849de7f4ddSMarcel Cornu return ISAL_CRYPTO_ERR_NULL_SRC;
859de7f4ddSMarcel Cornu #endif
86a922b2ebSMarcel Cornu return _mh_sha256_update(ctx, buffer, len);
876a8a917cSPablo de Lara #endif
889de7f4ddSMarcel Cornu }
899de7f4ddSMarcel Cornu
9038e16e11SMarcel Cornu int
isal_mh_sha256_finalize(struct isal_mh_sha256_ctx * ctx,void * mh_sha256_digest)91*f6da0bf4SMarcel Cornu isal_mh_sha256_finalize(struct isal_mh_sha256_ctx *ctx, void *mh_sha256_digest)
929de7f4ddSMarcel Cornu {
933e138b34SPablo de Lara #ifdef FIPS_MODE
943e138b34SPablo de Lara return ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO;
953e138b34SPablo de Lara #else
969de7f4ddSMarcel Cornu #ifdef SAFE_PARAM
979de7f4ddSMarcel Cornu if (ctx == NULL)
989de7f4ddSMarcel Cornu return ISAL_CRYPTO_ERR_NULL_CTX;
999de7f4ddSMarcel Cornu if (mh_sha256_digest == NULL)
1009de7f4ddSMarcel Cornu return ISAL_CRYPTO_ERR_NULL_AUTH;
1019de7f4ddSMarcel Cornu #endif
102a922b2ebSMarcel Cornu return _mh_sha256_finalize(ctx, mh_sha256_digest);
1036a8a917cSPablo de Lara #endif
1049de7f4ddSMarcel Cornu }
1059de7f4ddSMarcel Cornu
106a922b2ebSMarcel Cornu /*
107a922b2ebSMarcel Cornu * =============================================================================
108a922b2ebSMarcel Cornu * LEGACY / DEPRECATED API
109a922b2ebSMarcel Cornu * =============================================================================
110a922b2ebSMarcel Cornu */
111a922b2ebSMarcel Cornu
112a922b2ebSMarcel Cornu int
mh_sha256_init(struct isal_mh_sha256_ctx * ctx)113*f6da0bf4SMarcel Cornu mh_sha256_init(struct isal_mh_sha256_ctx *ctx)
114a922b2ebSMarcel Cornu {
115a922b2ebSMarcel Cornu return _mh_sha256_init(ctx);
116a922b2ebSMarcel Cornu }
117a922b2ebSMarcel Cornu
118a922b2ebSMarcel Cornu int
mh_sha256_update(struct isal_mh_sha256_ctx * ctx,const void * buffer,uint32_t len)119*f6da0bf4SMarcel Cornu mh_sha256_update(struct isal_mh_sha256_ctx *ctx, const void *buffer, uint32_t len)
120a922b2ebSMarcel Cornu {
121a922b2ebSMarcel Cornu return _mh_sha256_update(ctx, buffer, len);
122a922b2ebSMarcel Cornu }
123a922b2ebSMarcel Cornu
124a922b2ebSMarcel Cornu int
mh_sha256_finalize(struct isal_mh_sha256_ctx * ctx,void * mh_sha256_digest)125*f6da0bf4SMarcel Cornu mh_sha256_finalize(struct isal_mh_sha256_ctx *ctx, void *mh_sha256_digest)
126a922b2ebSMarcel Cornu {
127a922b2ebSMarcel Cornu return _mh_sha256_finalize(ctx, mh_sha256_digest);
128a922b2ebSMarcel Cornu }
129a922b2ebSMarcel Cornu
130a922b2ebSMarcel Cornu int
mh_sha256_update_base(struct isal_mh_sha256_ctx * ctx,const void * buffer,uint32_t len)131*f6da0bf4SMarcel Cornu mh_sha256_update_base(struct isal_mh_sha256_ctx *ctx, const void *buffer, uint32_t len)
132a922b2ebSMarcel Cornu {
133a922b2ebSMarcel Cornu return _mh_sha256_update_base(ctx, buffer, len);
134a922b2ebSMarcel Cornu }
135a922b2ebSMarcel Cornu
136a922b2ebSMarcel Cornu int
mh_sha256_finalize_base(struct isal_mh_sha256_ctx * ctx,void * mh_sha256_digest)137*f6da0bf4SMarcel Cornu mh_sha256_finalize_base(struct isal_mh_sha256_ctx *ctx, void *mh_sha256_digest)
138a922b2ebSMarcel Cornu {
139a922b2ebSMarcel Cornu return _mh_sha256_finalize_base(ctx, mh_sha256_digest);
140a922b2ebSMarcel Cornu }
141a922b2ebSMarcel Cornu
14238e16e11SMarcel Cornu #if (!defined(NOARCH)) && \
14338e16e11SMarcel Cornu (defined(__i386__) || defined(__x86_64__) || defined(_M_X64) || defined(_M_IX86))
1447dc32ad5SXiaodong Liu /***************mh_sha256_update***********/
1457dc32ad5SXiaodong Liu // mh_sha256_update_sse.c
146a922b2ebSMarcel Cornu #define MH_SHA256_UPDATE_FUNCTION _mh_sha256_update_sse
147a922b2ebSMarcel Cornu #define MH_SHA256_BLOCK_FUNCTION _mh_sha256_block_sse
1487dc32ad5SXiaodong Liu #include "mh_sha256_update_base.c"
1497dc32ad5SXiaodong Liu #undef MH_SHA256_UPDATE_FUNCTION
1507dc32ad5SXiaodong Liu #undef MH_SHA256_BLOCK_FUNCTION
1517dc32ad5SXiaodong Liu
1527dc32ad5SXiaodong Liu // mh_sha256_update_avx.c
153a922b2ebSMarcel Cornu #define MH_SHA256_UPDATE_FUNCTION _mh_sha256_update_avx
154a922b2ebSMarcel Cornu #define MH_SHA256_BLOCK_FUNCTION _mh_sha256_block_avx
1557dc32ad5SXiaodong Liu #include "mh_sha256_update_base.c"
1567dc32ad5SXiaodong Liu #undef MH_SHA256_UPDATE_FUNCTION
1577dc32ad5SXiaodong Liu #undef MH_SHA256_BLOCK_FUNCTION
1587dc32ad5SXiaodong Liu
1597dc32ad5SXiaodong Liu // mh_sha256_update_avx2.c
160a922b2ebSMarcel Cornu #define MH_SHA256_UPDATE_FUNCTION _mh_sha256_update_avx2
161a922b2ebSMarcel Cornu #define MH_SHA256_BLOCK_FUNCTION _mh_sha256_block_avx2
1627dc32ad5SXiaodong Liu #include "mh_sha256_update_base.c"
1637dc32ad5SXiaodong Liu #undef MH_SHA256_UPDATE_FUNCTION
1647dc32ad5SXiaodong Liu #undef MH_SHA256_BLOCK_FUNCTION
1657dc32ad5SXiaodong Liu
1667dc32ad5SXiaodong Liu /***************mh_sha256_finalize AND mh_sha256_tail***********/
1677dc32ad5SXiaodong Liu // mh_sha256_tail is used to calculate the last incomplete src data block
168*f6da0bf4SMarcel Cornu // mh_sha256_finalize is a isal_mh_sha256_ctx wrapper of mh_sha256_tail
1697dc32ad5SXiaodong Liu
1707dc32ad5SXiaodong Liu // mh_sha256_finalize_sse.c and mh_sha256_tail_sse.c
171a922b2ebSMarcel Cornu #define MH_SHA256_FINALIZE_FUNCTION _mh_sha256_finalize_sse
172a922b2ebSMarcel Cornu #define MH_SHA256_TAIL_FUNCTION _mh_sha256_tail_sse
173a922b2ebSMarcel Cornu #define MH_SHA256_BLOCK_FUNCTION _mh_sha256_block_sse
1747dc32ad5SXiaodong Liu #include "mh_sha256_finalize_base.c"
1757dc32ad5SXiaodong Liu #undef MH_SHA256_FINALIZE_FUNCTION
1767dc32ad5SXiaodong Liu #undef MH_SHA256_TAIL_FUNCTION
1777dc32ad5SXiaodong Liu #undef MH_SHA256_BLOCK_FUNCTION
1787dc32ad5SXiaodong Liu
1797dc32ad5SXiaodong Liu // mh_sha256_finalize_avx.c and mh_sha256_tail_avx.c
180a922b2ebSMarcel Cornu #define MH_SHA256_FINALIZE_FUNCTION _mh_sha256_finalize_avx
181a922b2ebSMarcel Cornu #define MH_SHA256_TAIL_FUNCTION _mh_sha256_tail_avx
182a922b2ebSMarcel Cornu #define MH_SHA256_BLOCK_FUNCTION _mh_sha256_block_avx
1837dc32ad5SXiaodong Liu #include "mh_sha256_finalize_base.c"
1847dc32ad5SXiaodong Liu #undef MH_SHA256_FINALIZE_FUNCTION
1857dc32ad5SXiaodong Liu #undef MH_SHA256_TAIL_FUNCTION
1867dc32ad5SXiaodong Liu #undef MH_SHA256_BLOCK_FUNCTION
1877dc32ad5SXiaodong Liu
1887dc32ad5SXiaodong Liu // mh_sha256_finalize_avx2.c and mh_sha256_tail_avx2.c
189a922b2ebSMarcel Cornu #define MH_SHA256_FINALIZE_FUNCTION _mh_sha256_finalize_avx2
190a922b2ebSMarcel Cornu #define MH_SHA256_TAIL_FUNCTION _mh_sha256_tail_avx2
191a922b2ebSMarcel Cornu #define MH_SHA256_BLOCK_FUNCTION _mh_sha256_block_avx2
1927dc32ad5SXiaodong Liu #include "mh_sha256_finalize_base.c"
1937dc32ad5SXiaodong Liu #undef MH_SHA256_FINALIZE_FUNCTION
1947dc32ad5SXiaodong Liu #undef MH_SHA256_TAIL_FUNCTION
1957dc32ad5SXiaodong Liu #undef MH_SHA256_BLOCK_FUNCTION
1967dc32ad5SXiaodong Liu
1977dc32ad5SXiaodong Liu /***************version info***********/
1987dc32ad5SXiaodong Liu
1997dc32ad5SXiaodong Liu struct slver {
2007dc32ad5SXiaodong Liu uint16_t snum;
2017dc32ad5SXiaodong Liu uint8_t ver;
2027dc32ad5SXiaodong Liu uint8_t core;
2037dc32ad5SXiaodong Liu };
2047dc32ad5SXiaodong Liu // Version info
2057dc32ad5SXiaodong Liu struct slver mh_sha256_init_slver_000002b1;
2067dc32ad5SXiaodong Liu struct slver mh_sha256_init_slver = { 0x02b1, 0x00, 0x00 };
2077dc32ad5SXiaodong Liu
2087dc32ad5SXiaodong Liu // mh_sha256_update version info
209a922b2ebSMarcel Cornu struct slver _mh_sha256_update_sse_slver_000002b4;
210a922b2ebSMarcel Cornu struct slver _mh_sha256_update_sse_slver = { 0x02b4, 0x00, 0x00 };
2117dc32ad5SXiaodong Liu
212a922b2ebSMarcel Cornu struct slver _mh_sha256_update_avx_slver_020002b6;
213a922b2ebSMarcel Cornu struct slver _mh_sha256_update_avx_slver = { 0x02b6, 0x00, 0x02 };
2147dc32ad5SXiaodong Liu
215a922b2ebSMarcel Cornu struct slver _mh_sha256_update_avx2_slver_040002b8;
216a922b2ebSMarcel Cornu struct slver _mh_sha256_update_avx2_slver = { 0x02b8, 0x00, 0x04 };
2177dc32ad5SXiaodong Liu
2187dc32ad5SXiaodong Liu // mh_sha256_finalize version info
219a922b2ebSMarcel Cornu struct slver _mh_sha256_finalize_sse_slver_000002b5;
220a922b2ebSMarcel Cornu struct slver _mh_sha256_finalize_sse_slver = { 0x02b5, 0x00, 0x00 };
2217dc32ad5SXiaodong Liu
222a922b2ebSMarcel Cornu struct slver _mh_sha256_finalize_avx_slver_020002b7;
223a922b2ebSMarcel Cornu struct slver _mh_sha256_finalize_avx_slver = { 0x02b7, 0x00, 0x02 };
2247dc32ad5SXiaodong Liu
225a922b2ebSMarcel Cornu struct slver _mh_sha256_finalize_avx2_slver_040002b9;
226a922b2ebSMarcel Cornu struct slver _mh_sha256_finalize_avx2_slver = { 0x02b9, 0x00, 0x04 };
227dc44e3c3SJerry Yu #endif
228