| 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 |
#define YESCRYPT_INTERNAL |
| 24 |
#include "alg-yescrypt.h" |
| 25 |
#undef YESCRYPT_INTERNAL |
| 26 |
|
| 27 |
#include "alg-sm3-hmac.h" |
| 28 |
|
| 29 |
#include <errno.h> |
| 30 |
|
| 31 |
/* upper level hmac for tests */ |
| 32 |
#ifndef outer_sm3_hmac |
| 33 |
#define outer_sm3_hmac sm3_hmac |
| 34 |
#endif |
| 35 |
|
| 36 |
/* For use in scratch space by crypt_gost_yescrypt_rn(). */ |
| 37 |
typedef struct |
| 38 |
{ |
| 39 |
sm3_hmac_ctx_t sm3buf; |
| 40 |
yescrypt_local_t local; |
| 41 |
uint8_t outbuf[CRYPT_OUTPUT_SIZE], |
| 42 |
sm3setting[CRYPT_OUTPUT_SIZE], |
| 43 |
hk[32], |
| 44 |
interm[32], |
| 45 |
y[32], |
| 46 |
*retval; |
| 47 |
} crypt_sm3_yescrypt_internal_t; |
| 48 |
|
| 49 |
static_assert (sizeof (crypt_sm3_yescrypt_internal_t) <= ALG_SPECIFIC_SIZE, |
| 50 |
"ALG_SPECIFIC_SIZE is too small for SM3-YESCRYPT."); |
| 51 |
|
| 52 |
/* |
| 53 |
* As OUTPUT is initialized with a failure token before gensalt_yescrypt_rn |
| 54 |
* is called, in case of an error we could just set an appropriate errno |
| 55 |
* and return. |
| 56 |
*/ |
| 57 |
void |
| 58 |
gensalt_sm3_yescrypt_rn (unsigned long count, |
| 59 |
const uint8_t *rbytes, size_t nrbytes, |
| 60 |
uint8_t *output, size_t o_size) |
| 61 |
{ |
| 62 |
/* Up to 512 bits (64 bytes) of entropy for computing the salt portion |
| 63 |
of the MCF-setting are supported. */ |
| 64 |
nrbytes = (nrbytes > 64 ? 64 : nrbytes); |
| 65 |
|
| 66 |
if (o_size < 6 + 8 * 6 + BASE64_LEN (nrbytes) + 1 || |
| 67 |
CRYPT_GENSALT_OUTPUT_SIZE < 6 + 8 * 6 + BASE64_LEN (nrbytes) + 1) |
| 68 |
{ |
| 69 |
errno = ERANGE; |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
/* We pass 'o_size - 3' to gensalt, because we need to shift |
| 74 |
the prefix by 3 chars to insert the sm3 marker. */ |
| 75 |
gensalt_yescrypt_rn (count, rbytes, nrbytes, output, o_size - 3); |
| 76 |
|
| 77 |
/* Check for failures. */ |
| 78 |
if (output[0] == '*') |
| 79 |
return; |
| 80 |
|
| 81 |
/* Shift output three bytes further. */ |
| 82 |
memmove (output + 3, output, strlen ((const char *) output) + 1); |
| 83 |
|
| 84 |
/* Insert the sm3 marker. */ |
| 85 |
output[1] = 's'; |
| 86 |
output[2] = 'm'; |
| 87 |
output[3] = '3'; |
| 88 |
} |
| 89 |
|
| 90 |
void |
| 91 |
crypt_sm3_yescrypt_rn (const char *phrase, size_t phr_size, |
| 92 |
const char *setting, size_t set_size, |
| 93 |
uint8_t *output, size_t o_size, |
| 94 |
void *scratch, size_t s_size) |
| 95 |
{ |
| 96 |
if (o_size < set_size + 1 + 43 + 1 || |
| 97 |
CRYPT_OUTPUT_SIZE < set_size + 1 + 43 + 1 || |
| 98 |
s_size < sizeof (crypt_sm3_yescrypt_internal_t)) |
| 99 |
{ |
| 100 |
errno = ERANGE; |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
/* Fail when called with wrong prefix. */ |
| 105 |
if (strncmp (setting, "$sm3y$", 6)) |
| 106 |
{ |
| 107 |
errno = EINVAL; |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
crypt_sm3_yescrypt_internal_t *intbuf = scratch; |
| 112 |
|
| 113 |
if (yescrypt_init_local (&intbuf->local)) |
| 114 |
return; |
| 115 |
|
| 116 |
/* convert gost setting to yescrypt setting */ |
| 117 |
intbuf->sm3setting[0] = '$'; |
| 118 |
intbuf->sm3setting[1] = 'y'; |
| 119 |
intbuf->sm3setting[2] = '$'; |
| 120 |
strcpy_or_abort (&intbuf->sm3setting[3], set_size - 3, setting + 6); |
| 121 |
|
| 122 |
intbuf->retval = yescrypt_r (NULL, &intbuf->local, |
| 123 |
(const uint8_t *) phrase, phr_size, |
| 124 |
intbuf->sm3setting, NULL, |
| 125 |
intbuf->outbuf + 3, o_size - 3); |
| 126 |
|
| 127 |
if (!intbuf->retval) |
| 128 |
errno = EINVAL; |
| 129 |
|
| 130 |
if (yescrypt_free_local (&intbuf->local) || !intbuf->retval) |
| 131 |
return; |
| 132 |
|
| 133 |
intbuf->outbuf[0] = '$'; |
| 134 |
intbuf->outbuf[1] = 's'; |
| 135 |
intbuf->outbuf[2] = 'm'; |
| 136 |
intbuf->outbuf[3] = '3'; |
| 137 |
|
| 138 |
/* extract yescrypt output from "$y$param$salt$output" */ |
| 139 |
char *hptr = strchr ((const char *) intbuf->retval + 3, '$'); |
| 140 |
if (!hptr) |
| 141 |
{ |
| 142 |
errno = EINVAL; |
| 143 |
return; |
| 144 |
} |
| 145 |
hptr = strchr (hptr + 1, '$'); |
| 146 |
if (!hptr) |
| 147 |
{ |
| 148 |
errno = EINVAL; |
| 149 |
return; |
| 150 |
} |
| 151 |
hptr++; /* start of output */ |
| 152 |
|
| 153 |
/* decode yescrypt output into its raw 256-bit form */ |
| 154 |
size_t ylen = sizeof (intbuf->y); |
| 155 |
if (!decode64 (intbuf->y, &ylen, (uint8_t *) hptr, strlen (hptr)) || |
| 156 |
ylen != sizeof (intbuf->y)) |
| 157 |
{ |
| 158 |
errno = EINVAL; |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
/* |
| 163 |
* SM3_HMAC( |
| 164 |
* SM3_HMAC(SM3(K), S), |
| 165 |
* yescrypt(K, S) |
| 166 |
* ) |
| 167 |
* yescrypt output is used in place of message, |
| 168 |
* thus, its crypto properties are superseded by SM3. |
| 169 |
* Password is always hashed for inner hmac to avoid |
| 170 |
* collisions between hashed and unhashed passwords. |
| 171 |
*/ |
| 172 |
sm3_hash ((const uint8_t *) phrase, phr_size, intbuf->hk, |
| 173 |
&intbuf->sm3buf.sm3_ctx); |
| 174 |
sm3_hmac (intbuf->hk, sizeof (intbuf->hk), |
| 175 |
(const uint8_t *) setting, |
| 176 |
(size_t) ((uint8_t *) hptr - intbuf->retval), |
| 177 |
intbuf->interm, &intbuf->sm3buf); |
| 178 |
outer_sm3_hmac (intbuf->interm, sizeof (intbuf->interm), |
| 179 |
intbuf->y, sizeof (intbuf->y), intbuf->y, |
| 180 |
&intbuf->sm3buf); |
| 181 |
|
| 182 |
encode64 ((uint8_t *) hptr, o_size - (size_t) ((uint8_t *) hptr - intbuf->retval), |
| 183 |
intbuf->y, sizeof (intbuf->y)); |
| 184 |
|
| 185 |
strcpy_or_abort (output, o_size, intbuf->outbuf); |
| 186 |
return; |
| 187 |
} |
| 188 |
|
| 189 |
#endif /* INCLUDE_sm3_yescrypt */ |