| 1 |
/* Copyright (C) 2024 Björn Esser <[email protected]> |
| 2 |
* |
| 3 |
* Redistribution and use in source and binary forms, with or without |
| 4 |
* modification, are permitted. |
| 5 |
* |
| 6 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 7 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 8 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 9 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 10 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 11 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 12 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 13 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 14 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 15 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 16 |
* SUCH DAMAGE. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "crypt-port.h" |
| 20 |
|
| 21 |
#if INCLUDE_sm3_yescrypt |
| 22 |
|
| 23 |
#include <string.h> |
| 24 |
#include "alg-sm3-hmac.h" |
| 25 |
|
| 26 |
/** |
| 27 |
* HMAC_k(m) = H((k ^ opad), H((k ^ ipad), m)) |
| 28 |
* pseudo-code: |
| 29 |
* function hmac(key, message) |
| 30 |
* opad = [0x5c * blocksize] |
| 31 |
* ipad = [0x36 * blocksize] |
| 32 |
* if (length(key) > blocksize) then |
| 33 |
* key = hash(key) |
| 34 |
* end if |
| 35 |
* for i from 0 to length(key) - 1 step 1 |
| 36 |
* ipad[i] = ipad[i] XOR key[i] |
| 37 |
* opad[i] = opad[i] XOR key[i] |
| 38 |
* end for |
| 39 |
* return hash(opad || hash(ipad || message)) |
| 40 |
* end function |
| 41 |
*/ |
| 42 |
|
| 43 |
#define IPAD 0x36 |
| 44 |
#define OPAD 0x5C |
| 45 |
|
| 46 |
void |
| 47 |
sm3_hmac_init (sm3_hmac_ctx_t *ctx, const uint8_t *key, size_t key_len) |
| 48 |
{ |
| 49 |
/* Initialize */ |
| 50 |
memset (ctx, 0, sizeof (sm3_hmac_ctx_t)); |
| 51 |
|
| 52 |
if (key_len > 64) |
| 53 |
{ |
| 54 |
sm3_init (&ctx->sm3_ctx); |
| 55 |
sm3_update (&ctx->sm3_ctx, key, key_len); |
| 56 |
sm3_final (ctx->key, &ctx->sm3_ctx); |
| 57 |
goto end; |
| 58 |
} |
| 59 |
|
| 60 |
memcpy (ctx->key, key, key_len); |
| 61 |
|
| 62 |
end: |
| 63 |
for (int i = 0; i < 64; i++) |
| 64 |
{ |
| 65 |
ctx->key[i] ^= IPAD; |
| 66 |
} |
| 67 |
sm3_init (&ctx->sm3_ctx); |
| 68 |
sm3_update (&ctx->sm3_ctx, ctx->key, 64); |
| 69 |
} |
| 70 |
|
| 71 |
void |
| 72 |
sm3_hmac_update (sm3_hmac_ctx_t *ctx, const uint8_t *data, size_t data_len) |
| 73 |
{ |
| 74 |
sm3_update (&ctx->sm3_ctx, data, data_len); |
| 75 |
} |
| 76 |
|
| 77 |
void |
| 78 |
sm3_hmac_final (sm3_hmac_ctx_t *ctx, uint8_t mac[32]) |
| 79 |
{ |
| 80 |
for (int i = 0; i < 64; i++) |
| 81 |
{ |
| 82 |
ctx->key[i] ^= (IPAD ^ OPAD); |
| 83 |
} |
| 84 |
sm3_final (mac, &ctx->sm3_ctx); |
| 85 |
sm3_init (&ctx->sm3_ctx); |
| 86 |
sm3_update (&ctx->sm3_ctx, ctx->key, 64); |
| 87 |
sm3_update (&ctx->sm3_ctx, mac, 32); |
| 88 |
sm3_final (mac, &ctx->sm3_ctx); |
| 89 |
|
| 90 |
/* Zeroize sensitive information. */ |
| 91 |
explicit_bzero (ctx, sizeof (sm3_hmac_ctx_t)); |
| 92 |
} |
| 93 |
|
| 94 |
void |
| 95 |
sm3_hmac (const unsigned char *data, size_t data_len, |
| 96 |
const uint8_t *key, size_t key_len, |
| 97 |
uint8_t mac[32], sm3_hmac_ctx_t *ctx) |
| 98 |
{ |
| 99 |
sm3_hmac_init (ctx, key, key_len); |
| 100 |
sm3_hmac_update (ctx, data, data_len); |
| 101 |
sm3_hmac_final (ctx, mac); |
| 102 |
} |
| 103 |
|
| 104 |
void |
| 105 |
sm3_hmac_buf (const unsigned char *data, size_t data_len, |
| 106 |
const uint8_t *key, size_t key_len, |
| 107 |
uint8_t mac[32]) |
| 108 |
{ |
| 109 |
sm3_hmac_ctx_t ctx; |
| 110 |
sm3_hmac (data, data_len, key, key_len, mac, &ctx); |
| 111 |
} |
| 112 |
|
| 113 |
#endif /* INCLUDE_sm3_yescrypt */ |