| 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"cxx . . $ `<file>/lib/crypt-port.h`/ |
| 20 | |
| 21 | #if INCLUDE_sm3_yescryptcxx . . $ `crypt-hashes.h:20:9`! |
| 22 | |
| 23 | #include <string.h> |
| 24 | #include "alg-sm3-hmac.h"cxx . . $ `<file>/lib/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 IPADcxx . . $ `lib/alg-sm3-hmac.c:43:9`! 0x36 |
| 44 | #define OPADcxx . . $ `lib/alg-sm3-hmac.c:44:9`! 0x5C |
| 45 | |
| 46 | void |
| 47 | sm3_hmac_initcxx . . $ `lib/crypt-port.h:426:9`! (sm3_hmac_ctx_tcxx . . $ sm3_hmac_ctx_t# *ctx, const uint8_tcxx . . $ uint8_t# *key, size_t key_len) |
| 48 | { |
| 49 | /* Initialize */ |
| 50 | memsetcxx . . $ memset(d1de1e4fb215f1c6). (ctx, 0, sizeof (sm3_hmac_ctx_tcxx . . $ sm3_hmac_ctx_t#)); |
| 51 | |
| 52 | if (key_len > 64) |
| 53 | { |
| 54 | sm3_initcxx . . $ `lib/crypt-port.h:407:9`! (&ctx->sm3_ctxcxx . . $ $anonymous_type_d8e841edf38d0a58_0#sm3_ctx.); |
| 55 | sm3_updatecxx . . $ `lib/crypt-port.h:408:9`! (&ctx->sm3_ctxcxx . . $ $anonymous_type_d8e841edf38d0a58_0#sm3_ctx., key, key_len); |
| 56 | sm3_finalcxx . . $ `lib/crypt-port.h:409:9`! (ctx->keycxx . . $ $anonymous_type_d8e841edf38d0a58_0#key., &ctx->sm3_ctxcxx . . $ $anonymous_type_d8e841edf38d0a58_0#sm3_ctx.); |
| 57 | goto end; |
| 58 | } |
| 59 | |
| 60 | memcpycxx . . $ memcpy(cc817cb175f282d). (ctx->keycxx . . $ $anonymous_type_d8e841edf38d0a58_0#key., key, key_len); |
| 61 | |
| 62 | end: |
| 63 | for (int i = 0; i < 64; i++) |
| 64 | { |
| 65 | ctx->keycxx . . $ $anonymous_type_d8e841edf38d0a58_0#key.[i] ^= IPADcxx . . $ `lib/alg-sm3-hmac.c:43:9`!; |
| 66 | } |
| 67 | sm3_initcxx . . $ `lib/crypt-port.h:407:9`! (&ctx->sm3_ctxcxx . . $ $anonymous_type_d8e841edf38d0a58_0#sm3_ctx.); |
| 68 | sm3_updatecxx . . $ `lib/crypt-port.h:408:9`! (&ctx->sm3_ctxcxx . . $ $anonymous_type_d8e841edf38d0a58_0#sm3_ctx., ctx->keycxx . . $ $anonymous_type_d8e841edf38d0a58_0#key., 64); |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | sm3_hmac_updatecxx . . $ `lib/crypt-port.h:427:9`! (sm3_hmac_ctx_tcxx . . $ sm3_hmac_ctx_t# *ctx, const uint8_tcxx . . $ uint8_t# *data, size_t data_len) |
| 73 | { |
| 74 | sm3_updatecxx . . $ `lib/crypt-port.h:408:9`! (&ctx->sm3_ctxcxx . . $ $anonymous_type_d8e841edf38d0a58_0#sm3_ctx., data, data_len); |
| 75 | } |
| 76 | |
| 77 | void |
| 78 | sm3_hmac_finalcxx . . $ `lib/crypt-port.h:425:9`! (sm3_hmac_ctx_tcxx . . $ sm3_hmac_ctx_t# *ctx, uint8_tcxx . . $ uint8_t# mac[32]) |
| 79 | { |
| 80 | for (int i = 0; i < 64; i++) |
| 81 | { |
| 82 | ctx->keycxx . . $ $anonymous_type_d8e841edf38d0a58_0#key.[i] ^= (IPADcxx . . $ `lib/alg-sm3-hmac.c:43:9`! ^ OPADcxx . . $ `lib/alg-sm3-hmac.c:44:9`!); |
| 83 | } |
| 84 | sm3_finalcxx . . $ `lib/crypt-port.h:409:9`! (mac, &ctx->sm3_ctxcxx . . $ $anonymous_type_d8e841edf38d0a58_0#sm3_ctx.); |
| 85 | sm3_initcxx . . $ `lib/crypt-port.h:407:9`! (&ctx->sm3_ctxcxx . . $ $anonymous_type_d8e841edf38d0a58_0#sm3_ctx.); |
| 86 | sm3_updatecxx . . $ `lib/crypt-port.h:408:9`! (&ctx->sm3_ctxcxx . . $ $anonymous_type_d8e841edf38d0a58_0#sm3_ctx., ctx->keycxx . . $ $anonymous_type_d8e841edf38d0a58_0#key., 64); |
| 87 | sm3_updatecxx . . $ `lib/crypt-port.h:408:9`! (&ctx->sm3_ctxcxx . . $ $anonymous_type_d8e841edf38d0a58_0#sm3_ctx., mac, 32); |
| 88 | sm3_finalcxx . . $ `lib/crypt-port.h:409:9`! (mac, &ctx->sm3_ctxcxx . . $ $anonymous_type_d8e841edf38d0a58_0#sm3_ctx.); |
| 89 | |
| 90 | /* Zeroize sensitive information. */ |
| 91 | explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`! (ctx, sizeof (sm3_hmac_ctx_t)); |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | sm3_hmaccxx . . $ `lib/crypt-port.h:428:9`! (const unsigned char *data, size_t data_len, |
| 96 | const uint8_tcxx . . $ uint8_t# *key, size_t key_len, |
| 97 | uint8_tcxx . . $ uint8_t# mac[32], sm3_hmac_ctx_tcxx . . $ sm3_hmac_ctx_t# *ctx) |
| 98 | { |
| 99 | sm3_hmac_initcxx . . $ `lib/crypt-port.h:426:9`! (ctx, key, key_len); |
| 100 | sm3_hmac_updatecxx . . $ `lib/crypt-port.h:427:9`! (ctx, data, data_len); |
| 101 | sm3_hmac_finalcxx . . $ `lib/crypt-port.h:425:9`! (ctx, mac); |
| 102 | } |
| 103 | |
| 104 | void |
| 105 | sm3_hmac_bufcxx . . $ `lib/crypt-port.h:424:9`! (const unsigned char *data, size_t data_len, |
| 106 | const uint8_tcxx . . $ uint8_t# *key, size_t key_len, |
| 107 | uint8_tcxx . . $ uint8_t# mac[32]) |
| 108 | { |
| 109 | sm3_hmac_ctx_tcxx . . $ sm3_hmac_ctx_t# ctx; |
| 110 | sm3_hmaccxx . . $ `lib/crypt-port.h:428:9`! (data, data_len, key, key_len, mac, &ctx); |
| 111 | } |
| 112 | |
| 113 | #endif /* INCLUDE_sm3_yescrypt */ |