Debian Codegraph @

lib/crypt-gost-yescrypt.c

raw symbols SCIP
1 /* Copyright (C) 2018 [email protected]
2 * Copyright (C) 2018 Björn Esser [email protected]
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted.
6 *
7 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
8 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
9 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
10 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
11 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
12 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
13 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
14 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
15 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
16 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
17 * SUCH DAMAGE.
18 */
19
20 #include "crypt-port.h"cxx . . $ `<file>/lib/crypt-port.h`/
21
22 #if INCLUDE_gost_yescryptcxx . . $ `crypt-hashes.h:13:9`!
23
24 #define YESCRYPT_INTERNALcxx . . $ `lib/crypt-gost-yescrypt.c:24:9`!
25 #include "alg-yescrypt.h"cxx . . $ `<file>/lib/alg-yescrypt.h`/
26 #undef YESCRYPT_INTERNALcxx . . $ `lib/crypt-gost-yescrypt.c:24:9`!
27
28 #include "alg-gost3411-2012-hmac.h"cxx . . $ `<file>/lib/alg-gost3411-2012-hmac.h`/
29
30 #include <errno.h>
31
32 /* upper level hmac for tests */
33 #ifndef outer_gost_hmac256
34 #define outer_gost_hmac256cxx . . $ `lib/crypt-gost-yescrypt.c:34:9`! gost_hmac256cxx . . $ `lib/crypt-port.h:420:9`!
35 #endif
36
37 /* For use in scratch space by crypt_gost_yescrypt_rn(). */
38 typedef structcxx . . $ $anonymous_type_a720e94f60a6ccee_0#
39 {
40 yescrypt_local_tcxx . . $ yescrypt_local_t# localcxx . . $ $anonymous_type_a720e94f60a6ccee_0#local.;
41 gost_hmac_256_tcxx . . $ gost_hmac_256_t# gostbufcxx . . $ $anonymous_type_a720e94f60a6ccee_0#gostbuf.;
42 uint8_tcxx . . $ uint8_t# outbufcxx . . $ $anonymous_type_a720e94f60a6ccee_0#outbuf.[CRYPT_OUTPUT_SIZEcxx . . $ `crypt.h:32:9`!];
43 uint8_tcxx . . $ uint8_t# gsettingcxx . . $ $anonymous_type_a720e94f60a6ccee_0#gsetting.[CRYPT_OUTPUT_SIZEcxx . . $ `crypt.h:32:9`!];
44 uint8_tcxx . . $ uint8_t# hkcxx . . $ $anonymous_type_a720e94f60a6ccee_0#hk.[32], intermcxx . . $ $anonymous_type_a720e94f60a6ccee_0#interm.[32], ycxx . . $ $anonymous_type_a720e94f60a6ccee_0#y.[32];
45 uint8_tcxx . . $ uint8_t# *retvalcxx . . $ $anonymous_type_a720e94f60a6ccee_0#retval.;
46 } crypt_gost_yescrypt_internal_tcxx . . $ crypt_gost_yescrypt_internal_t#;
47
48 static_assertcxx . . $ `<external>/4398592474888995393/assert.h:210:10`! (sizeof (crypt_gost_yescrypt_internal_tcxx . . $ crypt_gost_yescrypt_internal_t#) <= ALG_SPECIFIC_SIZEcxx . . $ `lib/crypt-port.h:510:9`!,
49 "ALG_SPECIFIC_SIZE is too small for GOST-YESCRYPT.");
50
51 /*
52 * As OUTPUT is initialized with a failure token before gensalt_yescrypt_rn
53 * is called, in case of an error we could just set an appropriate errno
54 * and return.
55 */
56 void
57 gensalt_gost_yescrypt_rncxx . . $ `crypt-hashes.h:41:9`! (unsigned long count,
58 const uint8_tcxx . . $ uint8_t# *rbytes, size_t nrbytes,
59 uint8_tcxx . . $ uint8_t# *output, size_t o_size)
60 {
61 /* Up to 512 bits (64 bytes) of entropy for computing the salt portion
62 of the MCF-setting are supported. */
63 nrbytes = (nrbytes > 64 ? 64 : nrbytes);
64
65 if (o_size < 4 + 8 * 6 + BASE64_LENcxx . . $ `lib/crypt-port.h:506:9`! (nrbytes) + 1 ||
66 CRYPT_GENSALT_OUTPUT_SIZEcxx . . $ `crypt.h:44:9`! < 4 + 8 * 6 + BASE64_LENcxx . . $ `lib/crypt-port.h:506:9`! (nrbytes) + 1)
67 {
68 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ERANGEcxx . . $ `<external>/8987895939074593727/errno-base.h:38:9`!;
69 return;
70 }
71
72 /* We pass 'o_size - 1' to gensalt, because we need to shift
73 the prefix by 1 char to insert the gost marker. */
74 gensalt_yescrypt_rncxx . . $ `crypt-hashes.h:61:9`! (count, rbytes, nrbytes, output, o_size - 1);
75
76 /* Check for failures. */
77 if (output[0] == '*')
78 return;
79
80 /* Shift output one byte further. */
81 memmovecxx . . $ memmove(cc817cb175f282d). (output + 1, output, strlencxx . . $ strlen(751346f8406fd082). ((const char *) output) + 1);
82
83 /* Insert the gost marker. */
84 output[1] = 'g';
85 }
86
87 void
88 crypt_gost_yescrypt_rncxx . . $ `crypt-hashes.h:40:9`! (const char *phrase, size_t phr_size,
89 const char *setting, size_t set_size,
90 uint8_tcxx . . $ uint8_t# *output, size_t o_size,
91 void *scratch, size_t s_size)
92 {
93 if (o_size < set_size + 1 + 43 + 1 ||
94 CRYPT_OUTPUT_SIZEcxx . . $ `crypt.h:32:9`! < set_size + 1 + 43 + 1 ||
95 s_size < sizeof (crypt_gost_yescrypt_internal_tcxx . . $ crypt_gost_yescrypt_internal_t#))
96 {
97 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ERANGEcxx . . $ `<external>/8987895939074593727/errno-base.h:38:9`!;
98 return;
99 }
100
101 /* Fail when called with wrong prefix. */
102 if (strncmpcxx . . $ strncmp(fd278af2f295fa2e). (setting, "$gy$", 4))
103 {
104 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
105 return;
106 }
107
108 crypt_gost_yescrypt_internal_tcxx . . $ crypt_gost_yescrypt_internal_t# *intbuf = scratch;
109
110 if (yescrypt_init_localcxx . . $ `lib/crypt-port.h:386:9`! (&intbuf->localcxx . . $ $anonymous_type_a720e94f60a6ccee_0#local.))
111 return;
112
113 /* convert gost setting to yescrypt setting */
114 intbuf->gsettingcxx . . $ $anonymous_type_a720e94f60a6ccee_0#gsetting.[0] = '$';
115 intbuf->gsettingcxx . . $ $anonymous_type_a720e94f60a6ccee_0#gsetting.[1] = 'y';
116 intbuf->gsettingcxx . . $ $anonymous_type_a720e94f60a6ccee_0#gsetting.[2] = '$';
117 strcpy_or_abortcxx . . $ `lib/crypt-port.h:188:9`! (&intbuf->gsettingcxx . . $ $anonymous_type_a720e94f60a6ccee_0#gsetting.[3], set_size - 3, setting + 4);
118
119 intbuf->retvalcxx . . $ $anonymous_type_a720e94f60a6ccee_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_a720e94f60a6ccee_0#local.,
120 (const uint8_tcxx . . $ uint8_t# *) phrase, phr_size,
121 intbuf->gsettingcxx . . $ $anonymous_type_a720e94f60a6ccee_0#gsetting., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!,
122 intbuf->outbufcxx . . $ $anonymous_type_a720e94f60a6ccee_0#outbuf. + 1, o_size - 1);
123
124 if (!intbuf->retvalcxx . . $ $anonymous_type_a720e94f60a6ccee_0#retval.)
125 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
126
127 if (yescrypt_free_localcxx . . $ `lib/crypt-port.h:384:9`! (&intbuf->localcxx . . $ $anonymous_type_a720e94f60a6ccee_0#local.) || !intbuf->retvalcxx . . $ $anonymous_type_a720e94f60a6ccee_0#retval.)
128 return;
129
130 intbuf->outbufcxx . . $ $anonymous_type_a720e94f60a6ccee_0#outbuf.[0] = '$';
131 intbuf->outbufcxx . . $ $anonymous_type_a720e94f60a6ccee_0#outbuf.[1] = 'g';
132
133 /* extract yescrypt output from "$y$param$salt$output" */
134 char *hptr = strchrcxx . . $ `<external>/13506128340496143423/string.h:264:11`! ((char *) intbuf->retval + 3, '$');
135 if (!hptr)
136 {
137 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
138 return;
139 }
140 hptr = strchrcxx . . $ `<external>/13506128340496143423/string.h:264:11`! (hptr + 1, '$');
141 if (!hptr)
142 {
143 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
144 return;
145 }
146 hptr++; /* start of output */
147
148 /* decode yescrypt output into its raw 256-bit form */
149 size_t ylen = sizeof (intbuf->ycxx . . $ $anonymous_type_a720e94f60a6ccee_0#y.);
150 if (!decode64cxx . . $ `lib/alg-yescrypt.h:346:9`! (intbuf->ycxx . . $ $anonymous_type_a720e94f60a6ccee_0#y., &ylen, (uint8_tcxx . . $ uint8_t# *) hptr, strlencxx . . $ strlen(751346f8406fd082). (hptr)) ||
151 ylen != sizeof (intbuf->ycxx . . $ $anonymous_type_a720e94f60a6ccee_0#y.))
152 {
153 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
154 return;
155 }
156
157 /*
158 * HMAC_GOSTR3411_2012_256(
159 * HMAC_GOSTR3411_2012_256(GOST2012_256(K), S),
160 * yescrypt(K, S)
161 * )
162 * yescrypt output is used in place of message,
163 * thus, its crypto properties are superseded by GOST.
164 * Password is always hashed for inner hmac to avoid
165 * collisions between hashed and unhashed passwords.
166 */
167 gost_hash256cxx . . $ `lib/crypt-port.h:419:9`! ((const uint8_tcxx . . $ uint8_t# *) phrase, phr_size, intbuf->hkcxx . . $ $anonymous_type_a720e94f60a6ccee_0#hk., &intbuf->gostbufcxx . . $ $anonymous_type_a720e94f60a6ccee_0#gostbuf..ctxcxx . . $ $anonymous_type_28221329a5e92a0c_0#ctx.);
168 gost_hmac256cxx . . $ `lib/crypt-port.h:420:9`! (intbuf->hkcxx . . $ $anonymous_type_a720e94f60a6ccee_0#hk., sizeof (intbuf->hkcxx . . $ $anonymous_type_a720e94f60a6ccee_0#hk.),
169 (const uint8_tcxx . . $ uint8_t# *) setting,
170 (size_t) ((uint8_tcxx . . $ uint8_t# *) hptr - intbuf->retvalcxx . . $ $anonymous_type_a720e94f60a6ccee_0#retval.),
171 intbuf->intermcxx . . $ $anonymous_type_a720e94f60a6ccee_0#interm., &intbuf->gostbufcxx . . $ $anonymous_type_a720e94f60a6ccee_0#gostbuf.);
172 outer_gost_hmac256cxx . . $ `lib/crypt-gost-yescrypt.c:34:9`! (intbuf->intermcxx . . $ $anonymous_type_a720e94f60a6ccee_0#interm., sizeof (intbuf->intermcxx . . $ $anonymous_type_a720e94f60a6ccee_0#interm.),
173 intbuf->ycxx . . $ $anonymous_type_a720e94f60a6ccee_0#y., sizeof (intbuf->ycxx . . $ $anonymous_type_a720e94f60a6ccee_0#y.), intbuf->ycxx . . $ $anonymous_type_a720e94f60a6ccee_0#y., &intbuf->gostbufcxx . . $ $anonymous_type_a720e94f60a6ccee_0#gostbuf.);
174
175 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_a720e94f60a6ccee_0#retval.),
176 intbuf->ycxx . . $ $anonymous_type_a720e94f60a6ccee_0#y., sizeof (intbuf->ycxx . . $ $anonymous_type_a720e94f60a6ccee_0#y.));
177
178 strcpy_or_abortcxx . . $ `lib/crypt-port.h:188:9`! (output, o_size, intbuf->outbufcxx . . $ $anonymous_type_a720e94f60a6ccee_0#outbuf.);
179 return;
180 }
181
182 #endif /* INCLUDE_gost_yescrypt */