byte_starts(3)

πŸ’– byte_starts(3) πŸ’–

okay so bestie, welcome to section 3 of the Library Functions Manual β€” yes we're in the Library Functions Manual, the section for library calls, and today's main character is byte_starts. she's small, she's fast, and she's about to tell you whether your buffer starts with a string. let's go 🫢


✨ NAME ✨

byte_starts β€” find out if a buffer starts with a string πŸ’…


🌸 SYNTAX 🌸

#include <libowfat/byte.h>

int byte_starts(const char *buf,size_t buflen,const char *str);

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so here's the tea. byte_starts returns 1 if buflen>=strlen(str) and the first strlen(str) bytes of buf match the contents of str. otherwise? it returns 0. that's it, that's the whole vibe. ✨

this function is meant to be used in protocol parsing and with a string constant for str β€” and when you do that, she'll use gcc/clang macro trickery to reduce down to a call to memcmp then. literally so iconic of her. πŸ”₯

now for the serious bit, bestie, because I love you and I need you to be safe:

byte_starts compares as few bytes as possible. An attacker observing the execution timing can thus learn where the first mismatch happened. 😭

that's a timing side channel, no cap. she stops the second she finds a mismatch, which is fast and cute for parsing, but it leaks info if someone's watching the clock.

so if you're comparing keys, passphrases, cookies or hashes? use byte_equal_notimingattack instead. periodt. πŸ‘‘


πŸ‘‘ SEE ALSO πŸ‘‘


anyway that's the page πŸŽ€ this was byte_starts from the Library Functions Manual, section 3, and the footer's just her name again because she doesn't need a whole version-and-date moment β€” she's timeless like that. mwah, stay safe, compare your secrets with the notimingattack one, bye bestie πŸ’–πŸ¦‹πŸŒΈ