Path: blob/main/sys/contrib/openzfs/module/icp/algs/sha2/sha2_generic.c
48674 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/2122/*23* Based on public domain code in cppcrypto 0.10.24* Copyright (c) 2022 Tino Reichardt <[email protected]>25*/2627#include <sys/zfs_context.h>28#include <sys/zfs_impl.h>29#include <sys/sha2.h>3031#include <sha2/sha2_impl.h>3233/*34* On i386, gcc brings this for sha512_generic():35* error: the frame size of 1040 bytes is larger than 102436*/37#if defined(__GNUC__) && defined(_ILP32)38#pragma GCC diagnostic ignored "-Wframe-larger-than="39#endif4041/* SHA256 */42static const uint32_t SHA256_K[64] = {430x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,440x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,450xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,460x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,470xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,480x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,490x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,500xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,510x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,520x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,530xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,540xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,550x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,560x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,570x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,580x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f259};6061#define Ch(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))62#define Maj(x, y, z) (((y) & (z)) | (((y) | (z)) & (x)))6364#define rotr32(x, n) (((x) >> n) | ((x) << (32 - n)))65#define sum0(x) (rotr32((x), 2) ^ rotr32((x), 13) ^ rotr32((x), 22))66#define sum1(x) (rotr32((x), 6) ^ rotr32((x), 11) ^ rotr32((x), 25))67#define sigma0(x) (rotr32((x), 7) ^ rotr32((x), 18) ^ ((x) >> 3))68#define sigma1(x) (rotr32((x), 17) ^ rotr32((x), 19) ^ ((x) >> 10))6970#define WU(j) (W[j & 15] += sigma1(W[(j + 14) & 15]) \71+ W[(j + 9) & 15] + sigma0(W[(j + 1) & 15]))7273#define COMPRESS(i, j, K) \74T1 = h + sum1(e) + Ch(e, f, g) + K[i + j] + (i? WU(j): W[j]); \75T2 = sum0(a) + Maj(a, b, c); \76h = g, g = f, f = e, e = d + T1; \77d = c, c = b, b = a, a = T1 + T2;7879static void80icp_sha256_generic(uint32_t state[8], const void *data, size_t num_blks)81{82uint64_t blk;8384for (blk = 0; blk < num_blks; blk++) {85uint32_t W[16];86uint32_t a, b, c, d, e, f, g, h;87uint32_t T1, T2;88int i;8990for (i = 0; i < 16; i++) {91W[i] = BE_32( \92(((const uint32_t *)(data))[blk * 16 + i]));93}9495a = state[0];96b = state[1];97c = state[2];98d = state[3];99e = state[4];100f = state[5];101g = state[6];102h = state[7];103104for (i = 0; i <= 63; i += 16) {105COMPRESS(i, 0, SHA256_K);106COMPRESS(i, 1, SHA256_K);107COMPRESS(i, 2, SHA256_K);108COMPRESS(i, 3, SHA256_K);109COMPRESS(i, 4, SHA256_K);110COMPRESS(i, 5, SHA256_K);111COMPRESS(i, 6, SHA256_K);112COMPRESS(i, 7, SHA256_K);113COMPRESS(i, 8, SHA256_K);114COMPRESS(i, 9, SHA256_K);115COMPRESS(i, 10, SHA256_K);116COMPRESS(i, 11, SHA256_K);117COMPRESS(i, 12, SHA256_K);118COMPRESS(i, 13, SHA256_K);119COMPRESS(i, 14, SHA256_K);120COMPRESS(i, 15, SHA256_K);121}122123state[0] += a;124state[1] += b;125state[2] += c;126state[3] += d;127state[4] += e;128state[5] += f;129state[6] += g;130state[7] += h;131}132}133134#undef sum0135#undef sum1136#undef sigma0137#undef sigma1138139#define rotr64(x, n) (((x) >> n) | ((x) << (64 - n)))140#define sum0(x) (rotr64((x), 28) ^ rotr64((x), 34) ^ rotr64((x), 39))141#define sum1(x) (rotr64((x), 14) ^ rotr64((x), 18) ^ rotr64((x), 41))142#define sigma0(x) (rotr64((x), 1) ^ rotr64((x), 8) ^ ((x) >> 7))143#define sigma1(x) (rotr64((x), 19) ^ rotr64((x), 61) ^ ((x) >> 6))144145/* SHA512 */146static const uint64_t SHA512_K[80] = {1470x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f,1480xe9b5dba58189dbbc, 0x3956c25bf348b538, 0x59f111f1b605d019,1490x923f82a4af194f9b, 0xab1c5ed5da6d8118, 0xd807aa98a3030242,1500x12835b0145706fbe, 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2,1510x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235,1520xc19bf174cf692694, 0xe49b69c19ef14ad2, 0xefbe4786384f25e3,1530x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65, 0x2de92c6f592b0275,1540x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5,1550x983e5152ee66dfab, 0xa831c66d2db43210, 0xb00327c898fb213f,1560xbf597fc7beef0ee4, 0xc6e00bf33da88fc2, 0xd5a79147930aa725,1570x06ca6351e003826f, 0x142929670a0e6e70, 0x27b70a8546d22ffc,1580x2e1b21385c26c926, 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df,1590x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6,1600x92722c851482353b, 0xa2bfe8a14cf10364, 0xa81a664bbc423001,1610xc24b8b70d0f89791, 0xc76c51a30654be30, 0xd192e819d6ef5218,1620xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8,1630x19a4c116b8d2d0c8, 0x1e376c085141ab53, 0x2748774cdf8eeb99,1640x34b0bcb5e19b48a8, 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb,1650x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3, 0x748f82ee5defb2fc,1660x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec,1670x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915,1680xc67178f2e372532b, 0xca273eceea26619c, 0xd186b8c721c0c207,1690xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178, 0x06f067aa72176fba,1700x0a637dc5a2c898a6, 0x113f9804bef90dae, 0x1b710b35131c471b,1710x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc,1720x431d67c49c100d4c, 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a,1730x5fcb6fab3ad6faec, 0x6c44198c4a475817174};175176static void177icp_sha512_generic(uint64_t state[8], const void *data, size_t num_blks)178{179uint64_t blk;180181for (blk = 0; blk < num_blks; blk++) {182uint64_t W[16];183uint64_t a, b, c, d, e, f, g, h;184uint64_t T1, T2;185int i;186187for (i = 0; i < 16; i++) {188W[i] = BE_64( \189(((const uint64_t *)(data))[blk * 16 + i]));190}191192a = state[0];193b = state[1];194c = state[2];195d = state[3];196e = state[4];197f = state[5];198g = state[6];199h = state[7];200201for (i = 0; i <= 79; i += 16) {202COMPRESS(i, 0, SHA512_K);203COMPRESS(i, 1, SHA512_K);204COMPRESS(i, 2, SHA512_K);205COMPRESS(i, 3, SHA512_K);206COMPRESS(i, 4, SHA512_K);207COMPRESS(i, 5, SHA512_K);208COMPRESS(i, 6, SHA512_K);209COMPRESS(i, 7, SHA512_K);210COMPRESS(i, 8, SHA512_K);211COMPRESS(i, 9, SHA512_K);212COMPRESS(i, 10, SHA512_K);213COMPRESS(i, 11, SHA512_K);214COMPRESS(i, 12, SHA512_K);215COMPRESS(i, 13, SHA512_K);216COMPRESS(i, 14, SHA512_K);217COMPRESS(i, 15, SHA512_K);218}219state[0] += a;220state[1] += b;221state[2] += c;222state[3] += d;223state[4] += e;224state[5] += f;225state[6] += g;226state[7] += h;227}228}229230static void231icp_sha256_update(sha256_ctx *ctx, const uint8_t *data, size_t len)232{233uint64_t pos = ctx->count[0];234uint64_t total = ctx->count[1];235uint8_t *m = ctx->wbuf;236const sha256_ops_t *ops = ctx->ops;237238if (pos && pos + len >= 64) {239memcpy(m + pos, data, 64 - pos);240ops->transform(ctx->state, m, 1);241len -= 64 - pos;242total += (64 - pos) * 8;243data += 64 - pos;244pos = 0;245}246247if (len >= 64) {248uint32_t blocks = len / 64;249uint32_t bytes = blocks * 64;250ops->transform(ctx->state, data, blocks);251len -= bytes;252total += (bytes) * 8;253data += bytes;254}255memcpy(m + pos, data, len);256257pos += len;258total += len * 8;259ctx->count[0] = pos;260ctx->count[1] = total;261}262263static void264icp_sha512_update(sha512_ctx *ctx, const uint8_t *data, size_t len)265{266uint64_t pos = ctx->count[0];267uint64_t total = ctx->count[1];268uint8_t *m = ctx->wbuf;269const sha512_ops_t *ops = ctx->ops;270271if (pos && pos + len >= 128) {272memcpy(m + pos, data, 128 - pos);273ops->transform(ctx->state, m, 1);274len -= 128 - pos;275total += (128 - pos) * 8;276data += 128 - pos;277pos = 0;278}279280if (len >= 128) {281uint64_t blocks = len / 128;282uint64_t bytes = blocks * 128;283ops->transform(ctx->state, data, blocks);284len -= bytes;285total += (bytes) * 8;286data += bytes;287}288memcpy(m + pos, data, len);289290pos += len;291total += len * 8;292ctx->count[0] = pos;293ctx->count[1] = total;294}295296static void297icp_sha256_final(sha256_ctx *ctx, uint8_t *result, int bits)298{299uint64_t mlen, pos = ctx->count[0];300uint8_t *m = ctx->wbuf;301uint32_t *R = (uint32_t *)result;302const sha256_ops_t *ops = ctx->ops;303304m[pos++] = 0x80;305if (pos > 56) {306memset(m + pos, 0, 64 - pos);307ops->transform(ctx->state, m, 1);308pos = 0;309}310311memset(m + pos, 0, 64 - pos);312mlen = BE_64(ctx->count[1]);313memcpy(m + (64 - 8), &mlen, 64 / 8);314ops->transform(ctx->state, m, 1);315316switch (bits) {317case 224: /* 28 - unused currently /TR */318R[0] = BE_32(ctx->state[0]);319R[1] = BE_32(ctx->state[1]);320R[2] = BE_32(ctx->state[2]);321R[3] = BE_32(ctx->state[3]);322R[4] = BE_32(ctx->state[4]);323R[5] = BE_32(ctx->state[5]);324R[6] = BE_32(ctx->state[6]);325break;326case 256: /* 32 */327R[0] = BE_32(ctx->state[0]);328R[1] = BE_32(ctx->state[1]);329R[2] = BE_32(ctx->state[2]);330R[3] = BE_32(ctx->state[3]);331R[4] = BE_32(ctx->state[4]);332R[5] = BE_32(ctx->state[5]);333R[6] = BE_32(ctx->state[6]);334R[7] = BE_32(ctx->state[7]);335break;336}337338memset(ctx, 0, sizeof (*ctx));339}340341static void342icp_sha512_final(sha512_ctx *ctx, uint8_t *result, int bits)343{344uint64_t mlen, pos = ctx->count[0];345uint8_t *m = ctx->wbuf, *r;346uint64_t *R = (uint64_t *)result;347const sha512_ops_t *ops = ctx->ops;348349m[pos++] = 0x80;350if (pos > 112) {351memset(m + pos, 0, 128 - pos);352ops->transform(ctx->state, m, 1);353pos = 0;354}355356memset(m + pos, 0, 128 - pos);357mlen = BE_64(ctx->count[1]);358memcpy(m + (128 - 8), &mlen, 64 / 8);359ops->transform(ctx->state, m, 1);360361switch (bits) {362case 224: /* 28 => 3,5 x 8 */363r = result + 24;364R[0] = BE_64(ctx->state[0]);365R[1] = BE_64(ctx->state[1]);366R[2] = BE_64(ctx->state[2]);367/* last 4 bytes are special here */368*r++ = (uint8_t)(ctx->state[3] >> 56);369*r++ = (uint8_t)(ctx->state[3] >> 48);370*r++ = (uint8_t)(ctx->state[3] >> 40);371*r++ = (uint8_t)(ctx->state[3] >> 32);372break;373case 256: /* 32 */374R[0] = BE_64(ctx->state[0]);375R[1] = BE_64(ctx->state[1]);376R[2] = BE_64(ctx->state[2]);377R[3] = BE_64(ctx->state[3]);378break;379case 384: /* 48 */380R[0] = BE_64(ctx->state[0]);381R[1] = BE_64(ctx->state[1]);382R[2] = BE_64(ctx->state[2]);383R[3] = BE_64(ctx->state[3]);384R[4] = BE_64(ctx->state[4]);385R[5] = BE_64(ctx->state[5]);386break;387case 512: /* 64 */388R[0] = BE_64(ctx->state[0]);389R[1] = BE_64(ctx->state[1]);390R[2] = BE_64(ctx->state[2]);391R[3] = BE_64(ctx->state[3]);392R[4] = BE_64(ctx->state[4]);393R[5] = BE_64(ctx->state[5]);394R[6] = BE_64(ctx->state[6]);395R[7] = BE_64(ctx->state[7]);396break;397}398399memset(ctx, 0, sizeof (*ctx));400}401402/* SHA2 Init function */403void404SHA2Init(int algotype, SHA2_CTX *ctx)405{406sha256_ctx *ctx256 = &ctx->sha256;407sha512_ctx *ctx512 = &ctx->sha512;408409ASSERT3S(algotype, >=, SHA512_HMAC_MECH_INFO_TYPE);410ASSERT3S(algotype, <=, SHA512_256);411412memset(ctx, 0, sizeof (*ctx));413ctx->algotype = algotype;414switch (ctx->algotype) {415case SHA256:416ctx256->state[0] = 0x6a09e667;417ctx256->state[1] = 0xbb67ae85;418ctx256->state[2] = 0x3c6ef372;419ctx256->state[3] = 0xa54ff53a;420ctx256->state[4] = 0x510e527f;421ctx256->state[5] = 0x9b05688c;422ctx256->state[6] = 0x1f83d9ab;423ctx256->state[7] = 0x5be0cd19;424ctx256->count[0] = 0;425ctx256->ops = sha256_get_ops();426break;427case SHA512:428case SHA512_HMAC_MECH_INFO_TYPE:429ctx512->state[0] = 0x6a09e667f3bcc908ULL;430ctx512->state[1] = 0xbb67ae8584caa73bULL;431ctx512->state[2] = 0x3c6ef372fe94f82bULL;432ctx512->state[3] = 0xa54ff53a5f1d36f1ULL;433ctx512->state[4] = 0x510e527fade682d1ULL;434ctx512->state[5] = 0x9b05688c2b3e6c1fULL;435ctx512->state[6] = 0x1f83d9abfb41bd6bULL;436ctx512->state[7] = 0x5be0cd19137e2179ULL;437ctx512->count[0] = 0;438ctx512->count[1] = 0;439ctx512->ops = sha512_get_ops();440break;441case SHA512_256:442ctx512->state[0] = 0x22312194fc2bf72cULL;443ctx512->state[1] = 0x9f555fa3c84c64c2ULL;444ctx512->state[2] = 0x2393b86b6f53b151ULL;445ctx512->state[3] = 0x963877195940eabdULL;446ctx512->state[4] = 0x96283ee2a88effe3ULL;447ctx512->state[5] = 0xbe5e1e2553863992ULL;448ctx512->state[6] = 0x2b0199fc2c85b8aaULL;449ctx512->state[7] = 0x0eb72ddc81c52ca2ULL;450ctx512->count[0] = 0;451ctx512->count[1] = 0;452ctx512->ops = sha512_get_ops();453break;454}455}456457/* SHA2 Update function */458void459SHA2Update(SHA2_CTX *ctx, const void *data, size_t len)460{461/* check for zero input length */462if (len == 0)463return;464465ASSERT3P(data, !=, NULL);466467switch (ctx->algotype) {468case SHA256:469icp_sha256_update(&ctx->sha256, data, len);470break;471case SHA512:472case SHA512_HMAC_MECH_INFO_TYPE:473icp_sha512_update(&ctx->sha512, data, len);474break;475case SHA512_256:476icp_sha512_update(&ctx->sha512, data, len);477break;478}479}480481/* SHA2Final function */482void483SHA2Final(void *digest, SHA2_CTX *ctx)484{485switch (ctx->algotype) {486case SHA256:487icp_sha256_final(&ctx->sha256, digest, 256);488break;489case SHA512:490case SHA512_HMAC_MECH_INFO_TYPE:491icp_sha512_final(&ctx->sha512, digest, 512);492break;493case SHA512_256:494icp_sha512_final(&ctx->sha512, digest, 256);495break;496}497}498499/* the generic implementation is always okay */500static boolean_t501icp_sha2_is_supported(void)502{503return (B_TRUE);504}505506const sha256_ops_t sha256_generic_impl = {507.name = "generic",508.transform = icp_sha256_generic,509.is_supported = icp_sha2_is_supported510};511512const sha512_ops_t sha512_generic_impl = {513.name = "generic",514.transform = icp_sha512_generic,515.is_supported = icp_sha2_is_supported516};517518519