CRYPTO_MEMCMP(3ossl)

πŸ“¦ version 3.5.0, in opensuse-leap 16.0

πŸ’… CRYPTO_MEMCMP πŸ’…

okay so bestie, welcome to CRYPTO_memcmp β€” it's the constant time memory comparison girlie from the OpenSSL 3ossl manual, and she is lowkey iconic. no cap, this is the function you want when you're comparing secrets and you do NOT want the timing of your comparison to snitch on you. πŸ•΅οΈβ€β™€οΈβœ¨


✨ NAME ✨

CRYPTO_memcmp β€” Constant time memory comparison πŸŽ€


🌸 SYNOPSIS 🌸

#include <openssl/crypto.h>

int CRYPTO_memcmp(const void *a, const void *b, size_t len);

literally the whole vibe right there, bestie 🫢


πŸ¦‹ DESCRIPTION πŸ¦‹

so the CRYPTO_memcmp function compares the len bytes pointed to by a and b β€” like, are they equal or not, that's the whole question πŸ’­

and here's the main character energy part: it takes an amount of time dependent on len, but independent of the contents of the memory regions pointed to by a and b. so it's not gonna leak anything by being all "hmm let me think about this byte for a sec" πŸ™ˆ timing-attack-proof queen, we stan πŸ‘‘


πŸ‘‘ RETURN VALUES πŸ‘‘

  • CRYPTO_memcmp() returns 0 if the memory regions are equal
  • and nonzero otherwise

that's it, that's the whole return contract, periodt πŸ’…


πŸ’€ NOTES πŸ’€

okay so important: unlike memcmp(2), this function cannot be used to order the two memory regions 🚫

why? because the return value when they differ is undefined, other than being nonzero. so like… it tells you that they're different, but it will NOT tell you which one is "bigger" or whatever. do not try to sort with this, bestie, it's giving undefined behaviour and it's not cute 😭

fr fr β€” if you need ordering, go get memcmp(2). if you need constant time equality, you're in the right place. iykyk πŸ’–

Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.

Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at <https://www.openssl.org/source/license.html>.


and that's the page, bestie!! πŸŒˆπŸ’œ this lil cutie is from OpenSSL 3.5.0, dated 2026-09-25 β€” go forth and compare your secrets in constant time, slay πŸ§šβ€β™€οΈβ­πŸ“

πŸ“¦ where this page comes from