| 1 | /* Kernel crypto API AF_ALG SP800-108 / SP800-132 KDF API |
| 2 | * |
| 3 | * Copyright (C) 2016 - 2022, Stephan Mueller <[email protected]> |
| 4 | * |
| 5 | * License: see COPYING file in root directory |
| 6 | * |
| 7 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 8 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 9 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF |
| 10 | * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE |
| 11 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 12 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 13 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 14 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 15 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 16 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 17 | * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH |
| 18 | * DAMAGE. |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * For performing a KDF operation, the following input is required |
| 23 | * from the caller: |
| 24 | * |
| 25 | * * Keying material to be used to derive the new keys from |
| 26 | * (denoted as Ko in SP800-108) |
| 27 | * * Label -- a free form binary string |
| 28 | * * Context -- a free form binary string |
| 29 | * |
| 30 | * The Ko keying material is provided when the caller invokes the |
| 31 | * kcapi_md_setkey function with the data. |
| 32 | * |
| 33 | * The Label and Context concatenated string is provided when obtaining random |
| 34 | * numbers, i.e. with the functions below. The caller must format |
| 35 | * the free-form Label || Context input as deemed necessary for the given |
| 36 | * purpose. Note, SP800-108 mandates that the Label and Context are separated |
| 37 | * by a 0x00 byte, i.e. the caller shall provide the input as |
| 38 | * Label || 0x00 || Context when trying to be compliant to SP800-108. For |
| 39 | * the feedback KDF, an IV is required as documented below. |
| 40 | * |
| 41 | * Example without proper error handling: |
| 42 | * char *keying_material = "\x00\x11\x22\x33\x44\x55\x66\x77"; |
| 43 | * char *label_context = "\xde\xad\xbe\xef\x00\xde\xad\xbe\xef"; |
| 44 | * kdf = kcapi_md_init(&handle, "hmac(sha256)", 0); |
| 45 | * kcapi_md_setkey(handle, keying_material, 8); |
| 46 | * kcapi_kdf_ctr(kdf, label_context, 9, outbuf, outbuflen); |
| 47 | */ |
| 48 | |
| 49 | #include <string.h> |
| 50 | #include <errno.h> |
| 51 | #include <limits.h> |
| 52 | #include <time.h> |
| 53 | |
| 54 | #include "kcapi.h"cxx . . $ `<file>/lib/kcapi.h`/ |
| 55 | #include "internal.h"cxx . . $ `<file>/lib/internal.h`/ |
| 56 | |
| 57 | #ifndef __has_builtin |
| 58 | # define __has_builtin(x) 0 |
| 59 | #endif |
| 60 | |
| 61 | #define GCC_VERSIONcxx . . $ `lib/kcapi-kdf.c:61:9`! (__GNUC__cxx . . $ `<built-in>:8:9`! * 10000 \ |
| 62 | + __GNUC_MINOR__cxx . . $ `<built-in>:9:9`! * 100 \ |
| 63 | + __GNUC_PATCHLEVEL__cxx . . $ `<built-in>:10:9`!) |
| 64 | #if GCC_VERSIONcxx . . $ `lib/kcapi-kdf.c:61:9`! >= 40400 || (defined(__clang__cxx . . $ `<built-in>:3:9`!) && __has_builtin(__builtin_bswap32)) |
| 65 | # define __HAVE_BUILTIN_BSWAP32__cxx . . $ `lib/kcapi-kdf.c:65:10`! |
| 66 | #endif |
| 67 | |
| 68 | /* Endian dependent byte swap operations. */ |
| 69 | #if __BYTE_ORDER__cxx . . $ `<built-in>:48:9`! == __ORDER_BIG_ENDIAN__cxx . . $ `<built-in>:46:9`! |
| 70 | # define be_bswap32(x) ((uint32_t)(x)) |
| 71 | #elif __BYTE_ORDER__cxx . . $ `<built-in>:48:9`! == __ORDER_LITTLE_ENDIAN__cxx . . $ `<built-in>:45:9`! |
| 72 | # ifdef __HAVE_BUILTIN_BSWAP32__cxx . . $ `lib/kcapi-kdf.c:65:10`! |
| 73 | # define be_bswap32cxx . . $ `lib/kcapi-kdf.c:73:11`!(x) (uint32_t)__builtin_bswap32((uint32_t)(x)) |
| 74 | # else |
| 75 | static inline uint32_t rol32(uint32_t x, int n) |
| 76 | { |
| 77 | return ( (x << (n&(32-1))) | (x >> ((32-n)&(32-1))) ); |
| 78 | } |
| 79 | |
| 80 | static inline uint32_t ror32(uint32_t x, int n) |
| 81 | { |
| 82 | return ( (x >> (n&(32-1))) | (x << ((32-n)&(32-1))) ); |
| 83 | } |
| 84 | |
| 85 | static inline uint32_t _bswap32(uint32_t x) |
| 86 | { |
| 87 | return ((rol32(x, 8) & 0x00ff00ffL) | (ror32(x, 8) & 0xff00ff00L)); |
| 88 | } |
| 89 | # define be_bswap32(x) _bswap32(x) |
| 90 | # endif |
| 91 | #else |
| 92 | # error "Endianess not defined" |
| 93 | #endif |
| 94 | |
| 95 | IMPL_SYMVERcxx . . $ `lib/internal.h:363:10`!(kdf_dpi, "1.3.1") |
| 96 | ssize_tcxx . . $ ssize_t# impl_kdf_dpicxx . . $ impl_kdf_dpi(6368db0609283dea).(struct kcapi_handlecxx . . $ kcapi_handle# *handle, |
| 97 | const uint8_tcxx . . $ uint8_t# *src, size_tcxx . . $ size_t# slen, |
| 98 | uint8_tcxx . . $ uint8_t# *dst, size_tcxx . . $ size_t# dlen) |
| 99 | { |
| 100 | uint32_tcxx . . $ uint32_t# h = kcapi_md_digestsizecxx . . $ kcapi_md_digestsize(994c7fcc3809be77).(handle); |
| 101 | ssize_tcxx . . $ ssize_t# err = 0; |
| 102 | uint8_tcxx . . $ uint8_t# *dst_orig = dst; |
| 103 | size_tcxx . . $ size_t# dlen_orig = dlen; |
| 104 | uint8_tcxx . . $ uint8_t# Ai[h]; |
| 105 | uint32_tcxx . . $ uint32_t# i = 1; |
| 106 | |
| 107 | if (dlen > INT_MAXcxx . . $ `<external>/67164224788392828/limits.h:120:9`!) |
| 108 | return -EMSGSIZEcxx . . $ `<external>/17922322188510340118/errno.h:74:9`!; |
| 109 | |
| 110 | if (!h) |
| 111 | return -EFAULTcxx . . $ `<external>/8987895939074593727/errno-base.h:18:9`!; |
| 112 | |
| 113 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(Ai, 0, h); |
| 114 | |
| 115 | while (dlen) { |
| 116 | uint32_tcxx . . $ uint32_t# ibe = be_bswap32cxx . . $ `lib/kcapi-kdf.c:73:11`!(i); |
| 117 | |
| 118 | /* Calculate A(i) */ |
| 119 | if (dst == dst_orig && src && slen) |
| 120 | /* 5.3 step 4 and 5.a */ |
| 121 | err = kcapi_md_digestcxx . . $ kcapi_md_digest(6368db0609283dea).(handle, src, slen, Ai, h); |
| 122 | else |
| 123 | /* 5.3 step 5.a */ |
| 124 | err = kcapi_md_digestcxx . . $ kcapi_md_digest(6368db0609283dea).(handle, Ai, h, Ai, h); |
| 125 | if (err < 0) |
| 126 | goto err; |
| 127 | |
| 128 | /* Calculate K(i) -- step 5.b */ |
| 129 | err = kcapi_md_updatecxx . . $ kcapi_md_update(1f5d0f6abfafb9e8).(handle, Ai, h); |
| 130 | if (err < 0) |
| 131 | goto err; |
| 132 | |
| 133 | err = kcapi_md_updatecxx . . $ kcapi_md_update(1f5d0f6abfafb9e8).(handle, (uint8_tcxx . . $ uint8_t# *)&ibe, |
| 134 | sizeof(uint32_tcxx . . $ uint32_t#)); |
| 135 | if (err < 0) |
| 136 | goto err; |
| 137 | if (src && slen) { |
| 138 | err = kcapi_md_updatecxx . . $ kcapi_md_update(1f5d0f6abfafb9e8).(handle, src, slen); |
| 139 | if (err < 0) |
| 140 | goto err; |
| 141 | } |
| 142 | |
| 143 | if (dlen < h) { |
| 144 | err = kcapi_md_finalcxx . . $ kcapi_md_final(e37e70272ed35378).(handle, dst, dlen); |
| 145 | if (err < 0) |
| 146 | goto err; |
| 147 | dlen = 0; |
| 148 | } else { |
| 149 | err = kcapi_md_finalcxx . . $ kcapi_md_final(e37e70272ed35378).(handle, dst, h); |
| 150 | if (err < 0) |
| 151 | goto err; |
| 152 | dlen -= h; |
| 153 | dst += h; |
| 154 | i++; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | kcapi_memset_securecxx . . $ kcapi_memset_secure(e37d57925061d5c9).(Ai, 0, h); |
| 159 | return 0; |
| 160 | |
| 161 | err: |
| 162 | kcapi_memset_securecxx . . $ kcapi_memset_secure(e37d57925061d5c9).(dst_orig, 0, dlen_orig); |
| 163 | kcapi_memset_securecxx . . $ kcapi_memset_secure(e37d57925061d5c9).(Ai, 0, h); |
| 164 | return err; |
| 165 | } |
| 166 | |
| 167 | ORIG_SYMVERcxx . . $ `lib/internal.h:366:10`!(kdf_dpi, "0.12.0") |
| 168 | int32_tcxx . . $ int32_t# orig_kdf_dpicxx . . $ orig_kdf_dpi(176ccef7f2585128).(struct kcapi_handlecxx . . $ kcapi_handle# *handle, |
| 169 | const uint8_tcxx . . $ uint8_t# *src, uint32_tcxx . . $ uint32_t# slen, |
| 170 | uint8_tcxx . . $ uint8_t# *dst, uint32_tcxx . . $ uint32_t# dlen) |
| 171 | { |
| 172 | return (int32_tcxx . . $ int32_t#)impl_kdf_dpicxx . . $ impl_kdf_dpi(6368db0609283dea).(handle, src, slen, dst, dlen); |
| 173 | } |
| 174 | |
| 175 | IMPL_SYMVERcxx . . $ `lib/internal.h:363:10`!(kdf_fb, "1.3.1") |
| 176 | ssize_tcxx . . $ ssize_t# impl_kdf_fbcxx . . $ impl_kdf_fb(6368db0609283dea).(struct kcapi_handlecxx . . $ kcapi_handle# *handle, |
| 177 | const uint8_tcxx . . $ uint8_t# *src, size_tcxx . . $ size_t# slen, |
| 178 | uint8_tcxx . . $ uint8_t# *dst, size_tcxx . . $ size_t# dlen) |
| 179 | { |
| 180 | uint32_tcxx . . $ uint32_t# h = kcapi_md_digestsizecxx . . $ kcapi_md_digestsize(994c7fcc3809be77).(handle); |
| 181 | ssize_tcxx . . $ ssize_t# err = 0; |
| 182 | uint8_tcxx . . $ uint8_t# *dst_orig = dst; |
| 183 | size_tcxx . . $ size_t# dlen_orig = dlen; |
| 184 | const uint8_tcxx . . $ uint8_t# *label; |
| 185 | size_tcxx . . $ size_t# labellen = 0; |
| 186 | uint32_tcxx . . $ uint32_t# i = 1; |
| 187 | |
| 188 | if (dlen > INT_MAXcxx . . $ `<external>/67164224788392828/limits.h:120:9`!) |
| 189 | return -EMSGSIZEcxx . . $ `<external>/17922322188510340118/errno.h:74:9`!; |
| 190 | |
| 191 | if (!h) |
| 192 | return -EFAULTcxx . . $ `<external>/8987895939074593727/errno-base.h:18:9`!; |
| 193 | |
| 194 | /* require the presence of an IV */ |
| 195 | if (!src || slen < h) |
| 196 | return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 197 | |
| 198 | /* calculate the offset of the label / context data */ |
| 199 | label = src + h; |
| 200 | labellen = slen - h; |
| 201 | |
| 202 | while (dlen) { |
| 203 | uint32_tcxx . . $ uint32_t# ibe = be_bswap32cxx . . $ `lib/kcapi-kdf.c:73:11`!(i); |
| 204 | |
| 205 | /* |
| 206 | * Feedback mode applies to all rounds except first which uses |
| 207 | * the IV. |
| 208 | */ |
| 209 | if (dst_orig == dst) |
| 210 | err = kcapi_md_updatecxx . . $ kcapi_md_update(1f5d0f6abfafb9e8).(handle, src, h); |
| 211 | else |
| 212 | err = kcapi_md_updatecxx . . $ kcapi_md_update(1f5d0f6abfafb9e8).(handle, dst - h, h); |
| 213 | if (err) |
| 214 | goto err; |
| 215 | |
| 216 | err = kcapi_md_updatecxx . . $ kcapi_md_update(1f5d0f6abfafb9e8).(handle, (uint8_tcxx . . $ uint8_t# *)&ibe, |
| 217 | sizeof(uint32_tcxx . . $ uint32_t#)); |
| 218 | if (err) |
| 219 | goto err; |
| 220 | if (labellen) { |
| 221 | err = kcapi_md_updatecxx . . $ kcapi_md_update(1f5d0f6abfafb9e8).(handle, label, labellen); |
| 222 | if (err < 0) |
| 223 | goto err; |
| 224 | } |
| 225 | |
| 226 | if (dlen < h) { |
| 227 | err = kcapi_md_finalcxx . . $ kcapi_md_final(e37e70272ed35378).(handle, dst, dlen); |
| 228 | if (err < 0) |
| 229 | goto err; |
| 230 | dlen = 0; |
| 231 | } else { |
| 232 | err = kcapi_md_finalcxx . . $ kcapi_md_final(e37e70272ed35378).(handle, dst, h); |
| 233 | if (err < 0) |
| 234 | goto err; |
| 235 | dlen -= h; |
| 236 | dst += h; |
| 237 | i++; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | return 0; |
| 242 | |
| 243 | err: |
| 244 | kcapi_memset_securecxx . . $ kcapi_memset_secure(e37d57925061d5c9).(dst_orig, 0, dlen_orig); |
| 245 | return err; |
| 246 | } |
| 247 | |
| 248 | ORIG_SYMVERcxx . . $ `lib/internal.h:366:10`!(kdf_fb, "0.12.0") |
| 249 | int32_tcxx . . $ int32_t# orig_kdf_fbcxx . . $ orig_kdf_fb(176ccef7f2585128).(struct kcapi_handlecxx . . $ kcapi_handle# *handle, |
| 250 | const uint8_tcxx . . $ uint8_t# *src, uint32_tcxx . . $ uint32_t# slen, |
| 251 | uint8_tcxx . . $ uint8_t# *dst, uint32_tcxx . . $ uint32_t# dlen) |
| 252 | { |
| 253 | return (int32_tcxx . . $ int32_t#)impl_kdf_fbcxx . . $ impl_kdf_fb(6368db0609283dea).(handle, src, slen, dst, dlen); |
| 254 | } |
| 255 | |
| 256 | IMPL_SYMVERcxx . . $ `lib/internal.h:363:10`!(kdf_ctr, "1.3.1") |
| 257 | ssize_tcxx . . $ ssize_t# impl_kdf_ctrcxx . . $ impl_kdf_ctr(6368db0609283dea).(struct kcapi_handlecxx . . $ kcapi_handle# *handle, |
| 258 | const uint8_tcxx . . $ uint8_t# *src, size_tcxx . . $ size_t# slen, |
| 259 | uint8_tcxx . . $ uint8_t# *dst, size_tcxx . . $ size_t# dlen) |
| 260 | { |
| 261 | uint32_tcxx . . $ uint32_t# h = kcapi_md_digestsizecxx . . $ kcapi_md_digestsize(994c7fcc3809be77).(handle); |
| 262 | ssize_tcxx . . $ ssize_t# err = 0; |
| 263 | uint8_tcxx . . $ uint8_t# *dst_orig = dst; |
| 264 | size_tcxx . . $ size_t# dlen_orig = dlen; |
| 265 | uint32_tcxx . . $ uint32_t# i = 1; |
| 266 | |
| 267 | if (dlen > INT_MAXcxx . . $ `<external>/67164224788392828/limits.h:120:9`!) |
| 268 | return -EMSGSIZEcxx . . $ `<external>/17922322188510340118/errno.h:74:9`!; |
| 269 | |
| 270 | if (!h) |
| 271 | return -EFAULTcxx . . $ `<external>/8987895939074593727/errno-base.h:18:9`!; |
| 272 | |
| 273 | while (dlen) { |
| 274 | uint32_tcxx . . $ uint32_t# ibe = be_bswap32cxx . . $ `lib/kcapi-kdf.c:73:11`!(i); |
| 275 | |
| 276 | err = kcapi_md_updatecxx . . $ kcapi_md_update(1f5d0f6abfafb9e8).(handle, (uint8_tcxx . . $ uint8_t# *)&ibe, |
| 277 | sizeof(uint32_tcxx . . $ uint32_t#)); |
| 278 | if (err) |
| 279 | goto err; |
| 280 | |
| 281 | if (src && slen) { |
| 282 | err = kcapi_md_updatecxx . . $ kcapi_md_update(1f5d0f6abfafb9e8).(handle, src, slen); |
| 283 | if (err < 0) |
| 284 | goto err; |
| 285 | } |
| 286 | |
| 287 | if (dlen < h) { |
| 288 | err = kcapi_md_finalcxx . . $ kcapi_md_final(e37e70272ed35378).(handle, dst, dlen); |
| 289 | if (err < 0) |
| 290 | goto err; |
| 291 | dlen = 0; |
| 292 | } else { |
| 293 | err = kcapi_md_finalcxx . . $ kcapi_md_final(e37e70272ed35378).(handle, dst, h); |
| 294 | if (err < 0) |
| 295 | goto err; |
| 296 | |
| 297 | dlen -= h; |
| 298 | dst += h; |
| 299 | i++; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | return 0; |
| 304 | |
| 305 | err: |
| 306 | kcapi_memset_securecxx . . $ kcapi_memset_secure(e37d57925061d5c9).(dst_orig, 0, dlen_orig); |
| 307 | return err; |
| 308 | } |
| 309 | |
| 310 | ORIG_SYMVERcxx . . $ `lib/internal.h:366:10`!(kdf_ctr, "0.12.0") |
| 311 | int32_tcxx . . $ int32_t# orig_kdf_ctrcxx . . $ orig_kdf_ctr(176ccef7f2585128).(struct kcapi_handlecxx . . $ kcapi_handle# *handle, |
| 312 | const uint8_tcxx . . $ uint8_t# *src, uint32_tcxx . . $ uint32_t# slen, |
| 313 | uint8_tcxx . . $ uint8_t# *dst, uint32_tcxx . . $ uint32_t# dlen) |
| 314 | { |
| 315 | return (int32_tcxx . . $ int32_t#)impl_kdf_ctrcxx . . $ impl_kdf_ctr(6368db0609283dea).(handle, src, slen, dst, dlen); |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * RFC 5869 KDF |
| 320 | */ |
| 321 | IMPL_SYMVERcxx . . $ `lib/internal.h:363:10`!(hkdf, "1.3.1") |
| 322 | ssize_tcxx . . $ ssize_t# impl_hkdfcxx . . $ impl_hkdf(13e6a99234fd95db).(const char *hashname, |
| 323 | const uint8_tcxx . . $ uint8_t# *ikm, size_tcxx . . $ size_t# ikmlen, |
| 324 | const uint8_tcxx . . $ uint8_t# *salt, uint32_tcxx . . $ uint32_t# saltlen, |
| 325 | const uint8_tcxx . . $ uint8_t# *info, size_tcxx . . $ size_t# infolen, |
| 326 | uint8_tcxx . . $ uint8_t# *dst, size_tcxx . . $ size_t# dlen) |
| 327 | { |
| 328 | #define HKDF_MAXHASHcxx . . $ `lib/kcapi-kdf.c:328:9`! 64 |
| 329 | uint32_tcxx . . $ uint32_t# h; |
| 330 | const uint8_tcxx . . $ uint8_t# null_salt[HKDF_MAXHASHcxx . . $ `lib/kcapi-kdf.c:328:9`!] = { 0 }; |
| 331 | uint8_tcxx . . $ uint8_t# prk_tmp[HKDF_MAXHASHcxx . . $ `lib/kcapi-kdf.c:328:9`!]; |
| 332 | uint8_tcxx . . $ uint8_t# *prev = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 333 | ssize_tcxx . . $ ssize_t# err = 0; |
| 334 | uint8_tcxx . . $ uint8_t# *dst_orig = dst; |
| 335 | size_tcxx . . $ size_t# dlen_orig = dlen; |
| 336 | uint8_tcxx . . $ uint8_t# ctr = 0x01; |
| 337 | struct kcapi_handlecxx . . $ kcapi_handle# *handle = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 338 | |
| 339 | if (!ikm || !ikmlen || !dst) |
| 340 | return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 341 | |
| 342 | err = kcapi_md_initcxx . . $ kcapi_md_init(b54728dab28c239a).(&handle, hashname, 0); |
| 343 | if (err) |
| 344 | return err; |
| 345 | |
| 346 | h = kcapi_md_digestsizecxx . . $ kcapi_md_digestsize(994c7fcc3809be77).(handle); |
| 347 | |
| 348 | if (!h) { |
| 349 | err = -EFAULTcxx . . $ `<external>/8987895939074593727/errno-base.h:18:9`!; |
| 350 | goto err; |
| 351 | } |
| 352 | |
| 353 | if (h > HKDF_MAXHASHcxx . . $ `lib/kcapi-kdf.c:328:9`!) { |
| 354 | kcapi_dologcxx . . $ kcapi_dolog(5f46d8f16dd28dce).(KCAPI_LOG_ERRcxx . . $ KCAPI_LOG_ERR., |
| 355 | "Null salt size too small for hash\n"); |
| 356 | h = HKDF_MAXHASHcxx . . $ `lib/kcapi-kdf.c:328:9`!; |
| 357 | err = -EFAULTcxx . . $ `<external>/8987895939074593727/errno-base.h:18:9`!; |
| 358 | goto err; |
| 359 | } |
| 360 | |
| 361 | if (dlen > h * 255) { |
| 362 | err = -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 363 | goto err; |
| 364 | } |
| 365 | |
| 366 | /* Extract phase */ |
| 367 | if (salt) |
| 368 | err = kcapi_md_setkeycxx . . $ kcapi_md_setkey(4d0e261613a805ee).(handle, salt, saltlen); |
| 369 | else |
| 370 | err = kcapi_md_setkeycxx . . $ kcapi_md_setkey(4d0e261613a805ee).(handle, null_salt, h); |
| 371 | |
| 372 | if (err) |
| 373 | goto err; |
| 374 | err = kcapi_md_digestcxx . . $ kcapi_md_digest(6368db0609283dea).(handle, ikm, ikmlen, prk_tmp, h); |
| 375 | if (err < 0) |
| 376 | goto err; |
| 377 | kcapi_md_destroycxx . . $ kcapi_md_destroy(aba29416d24fdd59).(handle); |
| 378 | handle = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 379 | |
| 380 | /* Expand phase */ |
| 381 | err = kcapi_md_initcxx . . $ kcapi_md_init(b54728dab28c239a).(&handle, hashname, 0); |
| 382 | if (err) |
| 383 | return err; |
| 384 | |
| 385 | err = kcapi_md_setkeycxx . . $ kcapi_md_setkey(4d0e261613a805ee).(handle, prk_tmp, h); |
| 386 | if (err) |
| 387 | goto err; |
| 388 | |
| 389 | /* T(1) and following */ |
| 390 | while (dlen) { |
| 391 | if (prev) { |
| 392 | err = kcapi_md_updatecxx . . $ kcapi_md_update(1f5d0f6abfafb9e8).(handle, prev, h); |
| 393 | if (err) |
| 394 | goto err; |
| 395 | } |
| 396 | |
| 397 | if (info) { |
| 398 | err = kcapi_md_updatecxx . . $ kcapi_md_update(1f5d0f6abfafb9e8).(handle, info, infolen); |
| 399 | if (err) |
| 400 | goto err; |
| 401 | } |
| 402 | |
| 403 | err = kcapi_md_updatecxx . . $ kcapi_md_update(1f5d0f6abfafb9e8).(handle, &ctr, 1); |
| 404 | if (err) |
| 405 | goto err; |
| 406 | |
| 407 | if (dlen < h) { |
| 408 | err = kcapi_md_finalcxx . . $ kcapi_md_final(e37e70272ed35378).(handle, dst, dlen); |
| 409 | if (err < 0) |
| 410 | goto err; |
| 411 | |
| 412 | dlen = 0; |
| 413 | } else { |
| 414 | err = kcapi_md_finalcxx . . $ kcapi_md_final(e37e70272ed35378).(handle, dst, h); |
| 415 | if (err < 0) |
| 416 | goto err; |
| 417 | |
| 418 | prev = dst; |
| 419 | dst += h; |
| 420 | dlen -= h; |
| 421 | ctr++; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | err = 0; |
| 426 | goto out; |
| 427 | |
| 428 | err: |
| 429 | kcapi_memset_securecxx . . $ kcapi_memset_secure(e37d57925061d5c9).(dst_orig, 0, dlen_orig); |
| 430 | out: |
| 431 | kcapi_memset_securecxx . . $ kcapi_memset_secure(e37d57925061d5c9).(prk_tmp, 0, h); |
| 432 | kcapi_md_destroycxx . . $ kcapi_md_destroy(aba29416d24fdd59).(handle); |
| 433 | return err; |
| 434 | } |
| 435 | |
| 436 | ORIG_SYMVERcxx . . $ `lib/internal.h:366:10`!(hkdf, "0.14.0") |
| 437 | int32_tcxx . . $ int32_t# orig_hkdfcxx . . $ orig_hkdf(70daf00401933bed).(const char *hashname, |
| 438 | const uint8_tcxx . . $ uint8_t# *ikm, uint32_tcxx . . $ uint32_t# ikmlen, |
| 439 | const uint8_tcxx . . $ uint8_t# *salt, uint32_tcxx . . $ uint32_t# saltlen, |
| 440 | const uint8_tcxx . . $ uint8_t# *info, uint32_tcxx . . $ uint32_t# infolen, |
| 441 | uint8_tcxx . . $ uint8_t# *dst, uint32_tcxx . . $ uint32_t# dlen) |
| 442 | { |
| 443 | return (int32_tcxx . . $ int32_t#)impl_hkdfcxx . . $ impl_hkdf(13e6a99234fd95db).(hashname, ikm, ikmlen, salt, saltlen, |
| 444 | info, infolen, dst, dlen); |
| 445 | } |
| 446 | |
| 447 | static inline uint64_tcxx . . $ uint64_t# kcapi_get_timecxx . . $ kcapi_get_time(f708dce9a8856c70).(void) |
| 448 | { |
| 449 | struct timespeccxx . . $ timespec# time; |
| 450 | |
| 451 | if (clock_gettimecxx . . $ clock_gettime(65aaefb2502b4987).(CLOCK_REALTIMEcxx . . $ `<external>/89459239771508533/time.h:46:10`!, &time) == 0) |
| 452 | return (uint64_tcxx . . $ uint64_t#)time.tv_nseccxx . . $ timespec#tv_nsec.; |
| 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | uint32_tcxx . . $ uint32_t# kcapi_pbkdf_iteration_countcxx . . $ kcapi_pbkdf_iteration_count(b323a3098472c9f4).(const char *hashname, uint64_tcxx . . $ uint64_t# timeshresh) |
| 458 | { |
| 459 | #define LOW_ITERATION_COUNTcxx . . $ `lib/kcapi-kdf.c:459:9`! (UINT32_Ccxx . . $ `<external>/3415975807581488833/stdint.h:249:10`!(1<<16)) |
| 460 | #define SAFE_ITERATION_COUNTcxx . . $ `lib/kcapi-kdf.c:460:9`! (UINT32_Ccxx . . $ `<external>/3415975807581488833/stdint.h:249:10`!(1<<18)) |
| 461 | #define SAFE_ITERATION_TIMEcxx . . $ `lib/kcapi-kdf.c:461:9`! (UINT32_Ccxx . . $ `<external>/3415975807581488833/stdint.h:249:10`!(1<<27)) /* more than 100,000,000 ns */ |
| 462 | uint32_tcxx . . $ uint32_t# i = 1; |
| 463 | uint32_tcxx . . $ uint32_t# j; |
| 464 | |
| 465 | /* Safety measure */ |
| 466 | if (!kcapi_get_timecxx . . $ kcapi_get_time(f708dce9a8856c70).()) |
| 467 | return (SAFE_ITERATION_COUNTcxx . . $ `lib/kcapi-kdf.c:460:9`!); |
| 468 | |
| 469 | if (timeshresh == 0) |
| 470 | timeshresh = SAFE_ITERATION_TIMEcxx . . $ `lib/kcapi-kdf.c:461:9`!; |
| 471 | |
| 472 | /* The outer loop catches rescheduling operations */ |
| 473 | for (j = 0; j < 2; j++) { |
| 474 | for (; i < UINT_MAXcxx . . $ `<external>/67164224788392828/limits.h:124:9`!; i<<=1) { |
| 475 | uint64_tcxx . . $ uint64_t# end, start = kcapi_get_timecxx . . $ kcapi_get_time(f708dce9a8856c70).(); |
| 476 | uint8_tcxx . . $ uint8_t# outbuf[16]; |
| 477 | ssize_tcxx . . $ ssize_t# ret = kcapi_pbkdfcxx . . $ kcapi_pbkdf(ee93b57b1aa2dcd2).(hashname, |
| 478 | (uint8_tcxx . . $ uint8_t# *)"passwordpassword", |
| 479 | 16, (uint8_tcxx . . $ uint8_t# *)"salt", 4, |
| 480 | i, outbuf, sizeof(outbuf)); |
| 481 | |
| 482 | end = kcapi_get_timecxx . . $ kcapi_get_time(f708dce9a8856c70).(); |
| 483 | |
| 484 | /* Safety measure */ |
| 485 | if (ret < 0) |
| 486 | return (SAFE_ITERATION_COUNTcxx . . $ `lib/kcapi-kdf.c:460:9`!); |
| 487 | |
| 488 | /* Take precautions if time runs backwards */ |
| 489 | if (end > start) |
| 490 | end = end - start; |
| 491 | else |
| 492 | end = start - end; |
| 493 | |
| 494 | if (end > timeshresh) |
| 495 | break; |
| 496 | else |
| 497 | j = 0; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | if (i < LOW_ITERATION_COUNTcxx . . $ `lib/kcapi-kdf.c:459:9`!) |
| 502 | i = LOW_ITERATION_COUNTcxx . . $ `lib/kcapi-kdf.c:459:9`!; |
| 503 | |
| 504 | return i; |
| 505 | } |
| 506 | |
| 507 | static inline int kcapi_alignedcxx . . $ kcapi_aligned(f360f2bcf3cfccb1).(const uint8_tcxx . . $ uint8_t# *ptr, uint32_tcxx . . $ uint32_t# alignmask) |
| 508 | { |
| 509 | if ((uintptr_tcxx . . $ uintptr_t#)ptr & alignmask) |
| 510 | return 0; |
| 511 | return 1; |
| 512 | } |
| 513 | |
| 514 | static inline void kcapi_xor_8cxx . . $ kcapi_xor_8(54346165b98501b0).(uint8_tcxx . . $ uint8_t# *dst, const uint8_tcxx . . $ uint8_t# *src, size_tcxx . . $ size_t# size) |
| 515 | { |
| 516 | for (; size; size--) |
| 517 | *dst++ ^= *src++; |
| 518 | } |
| 519 | |
| 520 | static inline void kcapi_xor_32_alignedcxx . . $ kcapi_xor_32_aligned(54346165b98501b0).(uint8_tcxx . . $ uint8_t# *dst, const uint8_tcxx . . $ uint8_t# *src, |
| 521 | size_tcxx . . $ size_t# size) |
| 522 | { |
| 523 | uint32_tcxx . . $ uint32_t# *dst_word = (uint32_tcxx . . $ uint32_t# *)dst; |
| 524 | uint32_tcxx . . $ uint32_t# *src_word = (uint32_tcxx . . $ uint32_t# *)src; |
| 525 | |
| 526 | for (; size >= sizeof(*src_word); size -= sizeof(*src_word)) |
| 527 | *dst_word++ ^= *src_word++; |
| 528 | |
| 529 | kcapi_xor_8cxx . . $ kcapi_xor_8(54346165b98501b0).((uint8_tcxx . . $ uint8_t# *)dst_word, (uint8_tcxx . . $ uint8_t# *)src_word, size); |
| 530 | } |
| 531 | |
| 532 | static inline void kcapi_xor_32cxx . . $ kcapi_xor_32(54346165b98501b0).(uint8_tcxx . . $ uint8_t# *dst, const uint8_tcxx . . $ uint8_t# *src, size_tcxx . . $ size_t# size) |
| 533 | { |
| 534 | if (kcapi_alignedcxx . . $ kcapi_aligned(f360f2bcf3cfccb1).(src, sizeof(uint32_tcxx . . $ uint32_t#) - 1) && |
| 535 | kcapi_alignedcxx . . $ kcapi_aligned(f360f2bcf3cfccb1).(dst, sizeof(uint32_tcxx . . $ uint32_t#) - 1)) |
| 536 | kcapi_xor_32_alignedcxx . . $ kcapi_xor_32_aligned(54346165b98501b0).(dst, src, size); |
| 537 | else |
| 538 | kcapi_xor_8cxx . . $ kcapi_xor_8(54346165b98501b0).(dst, src, size); |
| 539 | } |
| 540 | |
| 541 | #ifdef __LP64__cxx . . $ `<built-in>:51:9`! |
| 542 | static inline void kcapi_xor_64_alignedcxx . . $ kcapi_xor_64_aligned(54346165b98501b0).(uint8_tcxx . . $ uint8_t# *dst, const uint8_tcxx . . $ uint8_t# *src, |
| 543 | size_tcxx . . $ size_t# size) |
| 544 | { |
| 545 | uint64_tcxx . . $ uint64_t# *dst_dword = (uint64_tcxx . . $ uint64_t# *)dst; |
| 546 | uint64_tcxx . . $ uint64_t# *src_dword = (uint64_tcxx . . $ uint64_t# *)src; |
| 547 | |
| 548 | for (; size >= sizeof(*src_dword); size -= sizeof(*src_dword)) |
| 549 | *dst_dword++ ^= *src_dword++; |
| 550 | |
| 551 | kcapi_xor_32_alignedcxx . . $ kcapi_xor_32_aligned(54346165b98501b0).((uint8_tcxx . . $ uint8_t# *)dst_dword, (uint8_tcxx . . $ uint8_t# *)src_dword, size); |
| 552 | } |
| 553 | #endif |
| 554 | |
| 555 | static inline void kcapi_xor_64cxx . . $ kcapi_xor_64(54346165b98501b0).(uint8_tcxx . . $ uint8_t# *dst, const uint8_tcxx . . $ uint8_t# *src, size_tcxx . . $ size_t# size) |
| 556 | { |
| 557 | #ifdef __LP64__cxx . . $ `<built-in>:51:9`! |
| 558 | if (kcapi_alignedcxx . . $ kcapi_aligned(f360f2bcf3cfccb1).(src, sizeof(uint64_tcxx . . $ uint64_t#) - 1) && |
| 559 | kcapi_alignedcxx . . $ kcapi_aligned(f360f2bcf3cfccb1).(dst, sizeof(uint64_tcxx . . $ uint64_t#) - 1)) |
| 560 | kcapi_xor_64_alignedcxx . . $ kcapi_xor_64_aligned(54346165b98501b0).(dst, src, size); |
| 561 | else |
| 562 | #endif |
| 563 | kcapi_xor_32cxx . . $ kcapi_xor_32(54346165b98501b0).(dst, src, size); |
| 564 | } |
| 565 | |
| 566 | IMPL_SYMVERcxx . . $ `lib/internal.h:363:10`!(pbkdf, "1.3.1") |
| 567 | ssize_tcxx . . $ ssize_t# impl_pbkdfcxx . . $ impl_pbkdf(ee93b57b1aa2dcd2).(const char *hashname, |
| 568 | const uint8_tcxx . . $ uint8_t# *pw, uint32_tcxx . . $ uint32_t# pwlen, |
| 569 | const uint8_tcxx . . $ uint8_t# *salt, size_tcxx . . $ size_t# saltlen, |
| 570 | uint32_tcxx . . $ uint32_t# count, |
| 571 | uint8_tcxx . . $ uint8_t# *key, size_tcxx . . $ size_t# keylen) |
| 572 | { |
| 573 | struct kcapi_handlecxx . . $ kcapi_handle# *handle; |
| 574 | uint8_tcxx . . $ uint8_t# *key_orig = key; |
| 575 | size_tcxx . . $ size_t# keylen_orig = keylen; |
| 576 | uint32_tcxx . . $ uint32_t# h, i = 1; |
| 577 | #define MAX_DIGESTSIZEcxx . . $ `lib/kcapi-kdf.c:577:9`! 64 |
| 578 | uint8_tcxx . . $ uint8_t# u[MAX_DIGESTSIZEcxx . . $ `lib/kcapi-kdf.c:577:9`!] __attribute__ ((aligned (sizeof(uint64_tcxx . . $ uint64_t#)))); |
| 579 | ssize_tcxx . . $ ssize_t# err = 0; |
| 580 | |
| 581 | if (keylen > INT_MAXcxx . . $ `<external>/67164224788392828/limits.h:120:9`!) |
| 582 | return -EMSGSIZEcxx . . $ `<external>/17922322188510340118/errno.h:74:9`!; |
| 583 | |
| 584 | if (count == 0) |
| 585 | return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 586 | |
| 587 | err = kcapi_md_initcxx . . $ kcapi_md_init(b54728dab28c239a).(&handle, hashname, 0); |
| 588 | if (err) |
| 589 | return err; |
| 590 | |
| 591 | h = kcapi_md_digestsizecxx . . $ kcapi_md_digestsize(994c7fcc3809be77).(handle); |
| 592 | if (h > sizeof(u)) { |
| 593 | kcapi_dologcxx . . $ kcapi_dolog(5f46d8f16dd28dce).(KCAPI_LOG_ERRcxx . . $ KCAPI_LOG_ERR., |
| 594 | "Programming error in file %s at line %u\n", |
| 595 | __FILE__, __LINE__); |
| 596 | h = MAX_DIGESTSIZEcxx . . $ `lib/kcapi-kdf.c:577:9`!; |
| 597 | err = -EFAULTcxx . . $ `<external>/8987895939074593727/errno-base.h:18:9`!; |
| 598 | goto err; |
| 599 | } |
| 600 | |
| 601 | if (!h) { |
| 602 | err = -EFAULTcxx . . $ `<external>/8987895939074593727/errno-base.h:18:9`!; |
| 603 | goto err; |
| 604 | } |
| 605 | |
| 606 | err = kcapi_md_setkeycxx . . $ kcapi_md_setkey(4d0e261613a805ee).(handle, pw, pwlen); |
| 607 | if (err) |
| 608 | goto err; |
| 609 | |
| 610 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(key, 0, keylen); |
| 611 | |
| 612 | while (keylen) { |
| 613 | uint32_tcxx . . $ uint32_t# j; |
| 614 | uint32_tcxx . . $ uint32_t# ibe = be_bswap32cxx . . $ `lib/kcapi-kdf.c:73:11`!(i); |
| 615 | |
| 616 | err = kcapi_md_updatecxx . . $ kcapi_md_update(1f5d0f6abfafb9e8).(handle, salt, saltlen); |
| 617 | if (err < 0) |
| 618 | goto err; |
| 619 | |
| 620 | err = kcapi_md_updatecxx . . $ kcapi_md_update(1f5d0f6abfafb9e8).(handle, (uint8_tcxx . . $ uint8_t# *)&ibe, |
| 621 | sizeof(uint32_tcxx . . $ uint32_t#)); |
| 622 | if (err < 0) |
| 623 | goto err; |
| 624 | |
| 625 | for (j = 0; j < count; j++) { |
| 626 | if (j) { |
| 627 | err = kcapi_md_updatecxx . . $ kcapi_md_update(1f5d0f6abfafb9e8).(handle, u, h); |
| 628 | if (err) |
| 629 | goto err; |
| 630 | } |
| 631 | |
| 632 | err = kcapi_md_finalcxx . . $ kcapi_md_final(e37e70272ed35378).(handle, u, h); |
| 633 | if (err < 0) |
| 634 | goto err; |
| 635 | |
| 636 | kcapi_xor_64cxx . . $ kcapi_xor_64(54346165b98501b0).(key, u, keylen < h ? keylen : h); |
| 637 | } |
| 638 | |
| 639 | if (keylen < h) |
| 640 | keylen = 0; |
| 641 | else { |
| 642 | keylen -= h; |
| 643 | key += h; |
| 644 | i++; |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | err = 0; |
| 649 | |
| 650 | err: |
| 651 | kcapi_memset_securecxx . . $ kcapi_memset_secure(e37d57925061d5c9).(u, 0, h); |
| 652 | if (err) |
| 653 | kcapi_memset_securecxx . . $ kcapi_memset_secure(e37d57925061d5c9).(key_orig, 0, keylen_orig); |
| 654 | kcapi_md_destroycxx . . $ kcapi_md_destroy(aba29416d24fdd59).(handle); |
| 655 | |
| 656 | return err; |
| 657 | } |
| 658 | |
| 659 | ORIG_SYMVERcxx . . $ `lib/internal.h:366:10`!(pbkdf, "0.12.0") |
| 660 | int32_tcxx . . $ int32_t# orig_pbkdfcxx . . $ orig_pbkdf(e303449db7134c21).(const char *hashname, |
| 661 | const uint8_tcxx . . $ uint8_t# *pw, uint32_tcxx . . $ uint32_t# pwlen, |
| 662 | const uint8_tcxx . . $ uint8_t# *salt, uint32_tcxx . . $ uint32_t# saltlen, |
| 663 | uint32_tcxx . . $ uint32_t# count, |
| 664 | uint8_tcxx . . $ uint8_t# *key, uint32_tcxx . . $ uint32_t# keylen) |
| 665 | { |
| 666 | return (int32_tcxx . . $ int32_t#)impl_pbkdfcxx . . $ impl_pbkdf(ee93b57b1aa2dcd2).(hashname, pw, pwlen, salt, saltlen, count, |
| 667 | key, keylen); |
| 668 | } |