Debian Codegraph @

lib/crypt-sm3-yescrypt.c

raw symbols SCIP
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 #define YESCRYPT_INTERNALcxx . . $ `lib/crypt-sm3-yescrypt.c:23:9`!
24 #include "alg-yescrypt.h"cxx . . $ `<file>/lib/alg-yescrypt.h`/
25 #undef YESCRYPT_INTERNALcxx . . $ `lib/crypt-sm3-yescrypt.c:23:9`!
26
27 #include "alg-sm3-hmac.h"cxx . . $ `<file>/lib/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_hmaccxx . . $ `lib/crypt-sm3-yescrypt.c:33:9`! sm3_hmaccxx . . $ `lib/crypt-port.h:428:9`!
34 #endif
35
36 /* For use in scratch space by crypt_gost_yescrypt_rn(). */
37 typedef structcxx . . $ $anonymous_type_14d50d395d2374bf_0#
38 {
39 sm3_hmac_ctx_tcxx . . $ sm3_hmac_ctx_t# sm3bufcxx . . $ $anonymous_type_14d50d395d2374bf_0#sm3buf.;
40 yescrypt_local_tcxx . . $ yescrypt_local_t# localcxx . . $ $anonymous_type_14d50d395d2374bf_0#local.;
41 uint8_tcxx . . $ uint8_t# outbufcxx . . $ $anonymous_type_14d50d395d2374bf_0#outbuf.[CRYPT_OUTPUT_SIZEcxx . . $ `crypt.h:32:9`!],
42 sm3settingcxx . . $ $anonymous_type_14d50d395d2374bf_0#sm3setting.[CRYPT_OUTPUT_SIZEcxx . . $ `crypt.h:32:9`!],
43 hkcxx . . $ $anonymous_type_14d50d395d2374bf_0#hk.[32],
44 intermcxx . . $ $anonymous_type_14d50d395d2374bf_0#interm.[32],
45 ycxx . . $ $anonymous_type_14d50d395d2374bf_0#y.[32],
46 *retvalcxx . . $ $anonymous_type_14d50d395d2374bf_0#retval.;
47 } crypt_sm3_yescrypt_internal_tcxx . . $ crypt_sm3_yescrypt_internal_t#;
48
49 static_assertcxx . . $ `<external>/4398592474888995393/assert.h:210:10`! (sizeof (crypt_sm3_yescrypt_internal_tcxx . . $ crypt_sm3_yescrypt_internal_t#) <= ALG_SPECIFIC_SIZEcxx . . $ `lib/crypt-port.h:510:9`!,
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_rncxx . . $ `crypt-hashes.h:55:9`! (unsigned long count,
59 const uint8_tcxx . . $ uint8_t# *rbytes, size_t nrbytes,
60 uint8_tcxx . . $ 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_LENcxx . . $ `lib/crypt-port.h:506:9`! (nrbytes) + 1 ||
67 CRYPT_GENSALT_OUTPUT_SIZEcxx . . $ `crypt.h:44:9`! < 6 + 8 * 6 + BASE64_LENcxx . . $ `lib/crypt-port.h:506:9`! (nrbytes) + 1)
68 {
69 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ERANGEcxx . . $ `<external>/8987895939074593727/errno-base.h:38:9`!;
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_rncxx . . $ `crypt-hashes.h:61:9`! (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 memmovecxx . . $ memmove(cc817cb175f282d). (output + 3, output, strlencxx . . $ strlen(751346f8406fd082). ((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_rncxx . . $ `crypt-hashes.h:54:9`! (const char *phrase, size_t phr_size,
92 const char *setting, size_t set_size,
93 uint8_tcxx . . $ 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_SIZEcxx . . $ `crypt.h:32:9`! < set_size + 1 + 43 + 1 ||
98 s_size < sizeof (crypt_sm3_yescrypt_internal_tcxx . . $ crypt_sm3_yescrypt_internal_t#))
99 {
100 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ERANGEcxx . . $ `<external>/8987895939074593727/errno-base.h:38:9`!;
101 return;
102 }
103
104 /* Fail when called with wrong prefix. */
105 if (strncmpcxx . . $ strncmp(fd278af2f295fa2e). (setting, "$sm3y$", 6))
106 {
107 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
108 return;
109 }
110
111 crypt_sm3_yescrypt_internal_tcxx . . $ crypt_sm3_yescrypt_internal_t# *intbuf = scratch;
112
113 if (yescrypt_init_localcxx . . $ `lib/crypt-port.h:386:9`! (&intbuf->localcxx . . $ $anonymous_type_14d50d395d2374bf_0#local.))
114 return;
115
116 /* convert gost setting to yescrypt setting */
117 intbuf->sm3settingcxx . . $ $anonymous_type_14d50d395d2374bf_0#sm3setting.[0] = '$';
118 intbuf->sm3settingcxx . . $ $anonymous_type_14d50d395d2374bf_0#sm3setting.[1] = 'y';
119 intbuf->sm3settingcxx . . $ $anonymous_type_14d50d395d2374bf_0#sm3setting.[2] = '$';
120 strcpy_or_abortcxx . . $ `lib/crypt-port.h:188:9`! (&intbuf->sm3settingcxx . . $ $anonymous_type_14d50d395d2374bf_0#sm3setting.[3], set_size - 3, setting + 6);
121
122 intbuf->retvalcxx . . $ $anonymous_type_14d50d395d2374bf_0#retval. = yescrypt_rcxx . . $ `lib/crypt-port.h:389:9`! (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, &intbuf->localcxx . . $ $anonymous_type_14d50d395d2374bf_0#local.,
123 (const uint8_tcxx . . $ uint8_t# *) phrase, phr_size,
124 intbuf->sm3settingcxx . . $ $anonymous_type_14d50d395d2374bf_0#sm3setting., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!,
125 intbuf->outbufcxx . . $ $anonymous_type_14d50d395d2374bf_0#outbuf. + 3, o_size - 3);
126
127 if (!intbuf->retvalcxx . . $ $anonymous_type_14d50d395d2374bf_0#retval.)
128 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
129
130 if (yescrypt_free_localcxx . . $ `lib/crypt-port.h:384:9`! (&intbuf->localcxx . . $ $anonymous_type_14d50d395d2374bf_0#local.) || !intbuf->retvalcxx . . $ $anonymous_type_14d50d395d2374bf_0#retval.)
131 return;
132
133 intbuf->outbufcxx . . $ $anonymous_type_14d50d395d2374bf_0#outbuf.[0] = '$';
134 intbuf->outbufcxx . . $ $anonymous_type_14d50d395d2374bf_0#outbuf.[1] = 's';
135 intbuf->outbufcxx . . $ $anonymous_type_14d50d395d2374bf_0#outbuf.[2] = 'm';
136 intbuf->outbufcxx . . $ $anonymous_type_14d50d395d2374bf_0#outbuf.[3] = '3';
137
138 /* extract yescrypt output from "$y$param$salt$output" */
139 char *hptr = strchrcxx . . $ `<external>/13506128340496143423/string.h:264:11`! ((char *) intbuf->retval + 3, '$');
140 if (!hptr)
141 {
142 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
143 return;
144 }
145 hptr = strchrcxx . . $ `<external>/13506128340496143423/string.h:264:11`! (hptr + 1, '$');
146 if (!hptr)
147 {
148 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
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->ycxx . . $ $anonymous_type_14d50d395d2374bf_0#y.);
155 if (!decode64cxx . . $ `lib/alg-yescrypt.h:346:9`! (intbuf->ycxx . . $ $anonymous_type_14d50d395d2374bf_0#y., &ylen, (uint8_tcxx . . $ uint8_t# *) hptr, strlencxx . . $ strlen(751346f8406fd082). (hptr)) ||
156 ylen != sizeof (intbuf->ycxx . . $ $anonymous_type_14d50d395d2374bf_0#y.))
157 {
158 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
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_hashcxx . . $ `lib/crypt-port.h:410:9`! ((const uint8_tcxx . . $ uint8_t# *) phrase, phr_size, intbuf->hkcxx . . $ $anonymous_type_14d50d395d2374bf_0#hk.,
173 &intbuf->sm3bufcxx . . $ $anonymous_type_14d50d395d2374bf_0#sm3buf..sm3_ctxcxx . . $ $anonymous_type_d8e841edf38d0a58_0#sm3_ctx.);
174 sm3_hmaccxx . . $ `lib/crypt-port.h:428:9`! (intbuf->hkcxx . . $ $anonymous_type_14d50d395d2374bf_0#hk., sizeof (intbuf->hkcxx . . $ $anonymous_type_14d50d395d2374bf_0#hk.),
175 (const uint8_tcxx . . $ uint8_t# *) setting,
176 (size_t) ((uint8_tcxx . . $ uint8_t# *) hptr - intbuf->retvalcxx . . $ $anonymous_type_14d50d395d2374bf_0#retval.),
177 intbuf->intermcxx . . $ $anonymous_type_14d50d395d2374bf_0#interm., &intbuf->sm3bufcxx . . $ $anonymous_type_14d50d395d2374bf_0#sm3buf.);
178 outer_sm3_hmaccxx . . $ `lib/crypt-sm3-yescrypt.c:33:9`! (intbuf->intermcxx . . $ $anonymous_type_14d50d395d2374bf_0#interm., sizeof (intbuf->intermcxx . . $ $anonymous_type_14d50d395d2374bf_0#interm.),
179 intbuf->ycxx . . $ $anonymous_type_14d50d395d2374bf_0#y., sizeof (intbuf->ycxx . . $ $anonymous_type_14d50d395d2374bf_0#y.), intbuf->ycxx . . $ $anonymous_type_14d50d395d2374bf_0#y.,
180 &intbuf->sm3bufcxx . . $ $anonymous_type_14d50d395d2374bf_0#sm3buf.);
181
182 encode64cxx . . $ `lib/alg-yescrypt.h:347:9`! ((uint8_tcxx . . $ uint8_t# *) hptr, o_size - (size_t) ((uint8_tcxx . . $ uint8_t# *) hptr - intbuf->retvalcxx . . $ $anonymous_type_14d50d395d2374bf_0#retval.),
183 intbuf->ycxx . . $ $anonymous_type_14d50d395d2374bf_0#y., sizeof (intbuf->ycxx . . $ $anonymous_type_14d50d395d2374bf_0#y.));
184
185 strcpy_or_abortcxx . . $ `lib/crypt-port.h:188:9`! (output, o_size, intbuf->outbufcxx . . $ $anonymous_type_14d50d395d2374bf_0#outbuf.);
186 return;
187 }
188
189 #endif /* INCLUDE_sm3_yescrypt */