-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sm3-yescrypt uses the output from the yescrypt hashing method in place of a hmac message. Thus, the yescrypt crypto properties are superseeded by the ShangMi 3 hash function with a 256 bit digest. This hashing method is useful in applications that need modern password hashing methods, but require to rely on cryptographic properties that have been approved by the Chinese Office of State Commercial Cryptography Administration (OSCCA). ShangMi 3 is a cryptographic hash function used in the Chinese National Standard. It was published by the National Cryptography Administration (Chinese: 国家密码管理局) on 2010-12-17 as "GM/T 0004-2012: SM3 cryptographic hash algorithm". SM3 is used for implementing digital signatures, message authentication codes, and pseudorandom number generators. The algorithm is public and is considered similar to SHA-256 in security and efficiency. SM3 is also used with Transport Layer Security. SM3 is defined in each of: * GM/T 0004-2012: SM3 cryptographic hash algorithm * GB/T 32905-2016: Information security techniques SM3 cryptographic hash algorithm * ISO/IEC 10118-3:2018: IT Security techniques—Hash-functions Part 3: Dedicated hash-functions * IETF RFC draft-sca-cfrg-sm3-02 See: https://datatracker.ietf.org/doc/html/draft-sca-cfrg-sm3
- Loading branch information
Showing
22 changed files
with
1,178 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* Copyright (C) 2024 Björn Esser <[email protected]> | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | ||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
* SUCH DAMAGE. | ||
*/ | ||
|
||
#include "crypt-port.h" | ||
|
||
#if INCLUDE_sm3crypt | ||
|
||
#include <string.h> | ||
#include "alg-sm3-hmac.h" | ||
|
||
/** | ||
* HMAC_k(m) = H((k ^ opad), H((k ^ ipad), m)) | ||
* pseudo-code: | ||
* function hmac(key, message) | ||
* opad = [0x5c * blocksize] | ||
* ipad = [0x36 * blocksize] | ||
* if (length(key) > blocksize) then | ||
* key = hash(key) | ||
* end if | ||
* for i from 0 to length(key) - 1 step 1 | ||
* ipad[i] = ipad[i] XOR key[i] | ||
* opad[i] = opad[i] XOR key[i] | ||
* end for | ||
* return hash(opad || hash(ipad || message)) | ||
* end function | ||
*/ | ||
|
||
#define IPAD 0x36 | ||
#define OPAD 0x5C | ||
|
||
void | ||
sm3_hmac_init (sm3_hmac_ctx_t *ctx, const uint8_t *key, size_t key_len) | ||
{ | ||
int i; | ||
|
||
if (key_len <= SM3_BLOCK_SIZE) | ||
{ | ||
memcpy (ctx->key, key, key_len); | ||
explicit_bzero (ctx->key + key_len, SM3_BLOCK_SIZE - key_len); | ||
} | ||
else | ||
{ | ||
sm3_init (&ctx->sm3_ctx); | ||
sm3_update (&ctx->sm3_ctx, key, key_len); | ||
sm3_final (ctx->key, &ctx->sm3_ctx); | ||
explicit_bzero (ctx->key + SM3_DIGEST_SIZE, | ||
SM3_BLOCK_SIZE - SM3_DIGEST_SIZE); | ||
} | ||
for (i = 0; i < SM3_BLOCK_SIZE; i++) | ||
{ | ||
ctx->key[i] ^= IPAD; | ||
} | ||
|
||
sm3_init (&ctx->sm3_ctx); | ||
sm3_update (&ctx->sm3_ctx, ctx->key, SM3_BLOCK_SIZE); | ||
} | ||
|
||
void | ||
sm3_hmac_update (sm3_hmac_ctx_t *ctx, const uint8_t *data, size_t data_len) | ||
{ | ||
sm3_update (&ctx->sm3_ctx, data, data_len); | ||
} | ||
|
||
void | ||
sm3_hmac_final (sm3_hmac_ctx_t *ctx, uint8_t mac[SM3_HMAC_MAC_SIZE]) | ||
{ | ||
int i; | ||
for (i = 0; i < SM3_BLOCK_SIZE; i++) | ||
{ | ||
ctx->key[i] ^= (IPAD ^ OPAD); | ||
} | ||
sm3_final (mac, &ctx->sm3_ctx); | ||
sm3_init (&ctx->sm3_ctx); | ||
sm3_update (&ctx->sm3_ctx, ctx->key, SM3_BLOCK_SIZE); | ||
sm3_update (&ctx->sm3_ctx, mac, SM3_DIGEST_SIZE); | ||
sm3_final (mac, &ctx->sm3_ctx); | ||
|
||
/* Zeroize sensitive information. */ | ||
explicit_bzero (&ctx, sizeof (ctx)); | ||
} | ||
|
||
void | ||
sm3_hmac (const unsigned char *data, size_t data_len, | ||
const uint8_t *key, size_t key_len, | ||
uint8_t mac[SM3_HMAC_MAC_SIZE], sm3_hmac_ctx_t *ctx) | ||
{ | ||
sm3_hmac_init (ctx, key, key_len); | ||
sm3_hmac_update (ctx, data, data_len); | ||
sm3_hmac_final (ctx, mac); | ||
|
||
/* Zeroize sensitive information. */ | ||
explicit_bzero (ctx, sizeof (sm3_hmac_ctx_t)); | ||
} | ||
|
||
void | ||
sm3_hmac_buf (const unsigned char *data, size_t data_len, | ||
const uint8_t *key, size_t key_len, | ||
uint8_t mac[SM3_HMAC_MAC_SIZE]) | ||
{ | ||
sm3_hmac_ctx_t ctx; | ||
sm3_hmac (data, data_len, key, key_len, mac, &ctx); | ||
} | ||
|
||
#endif /* INCLUDE_sm3_yescrypt */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* Copyright (C) 2024 Björn Esser <[email protected]> | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | ||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
* SUCH DAMAGE. | ||
*/ | ||
|
||
#ifndef _CRYPT_ALG_SM3_HMAC_H | ||
#define _CRYPT_ALG_SM3_HMAC_H | ||
|
||
#include "alg-sm3.h" | ||
|
||
#define SM3_HMAC_MAC_SIZE SM3_DIGEST_SIZE | ||
|
||
typedef struct | ||
{ | ||
sm3_ctx sm3_ctx; | ||
uint8_t key[SM3_BLOCK_SIZE]; | ||
} sm3_hmac_ctx_t; | ||
|
||
void sm3_hmac_init (sm3_hmac_ctx_t * ctx, const uint8_t * key, | ||
size_t key_len); | ||
void sm3_hmac_update (sm3_hmac_ctx_t * ctx, const uint8_t * data, | ||
size_t data_len); | ||
void sm3_hmac_final (sm3_hmac_ctx_t * ctx, uint8_t mac[SM3_HMAC_MAC_SIZE]); | ||
void sm3_hmac (const uint8_t * data, size_t data_len, | ||
const uint8_t * key, size_t key_len, | ||
uint8_t mac[SM3_HMAC_MAC_SIZE], sm3_hmac_ctx_t * ctx); | ||
void sm3_hmac_buf (const uint8_t * data, size_t data_len, | ||
const uint8_t * key, size_t key_len, | ||
uint8_t mac[SM3_HMAC_MAC_SIZE]); | ||
|
||
#endif /* _CRYPT_ALG_SM3_HMAC_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.