random.c crypto-api.c gnutls_privkey.c gnutls_pcert.c \
gnutls_pubkey.c locks.c gnutls_dtls.c system_override.c \
crypto-backend.c verify-tofu.c pin.c tpm.c fips.c \
- safe-memset.c inet_pton.c atfork.c atfork.h
+ safe-memfuncs.c inet_pton.c atfork.c atfork.h
if ENABLE_SELF_CHECKS
COBJECTS += crypto-selftests.c crypto-selftests-pk.c
ptr = ctext;
ptr += ctext_len;
- if (memcmp(ptr, tag, h->tag_size) != 0)
+ if (gnutls_memcmp(ptr, tag, h->tag_size) != 0)
return gnutls_assert_val(GNUTLS_E_DECRYPTION_FAILED);
h->nonce_set = 0;
}
_gnutls_cipher_tag(&cipher_hd, final, TAG_SIZE);
- if (memcmp(ticket->tag, final, TAG_SIZE) != 0) {
+ if (gnutls_memcmp(ticket->tag, final, TAG_SIZE) != 0) {
gnutls_assert();
ret = GNUTLS_E_DECRYPTION_FAILED;
goto cleanup;
if (unlikely(ret < 0))
return gnutls_assert_val(ret);
- if (unlikely(memcmp(tag, &ciphertext->data[ciphertext->size-tag_size], tag_size) != 0)) {
+ if (unlikely(gnutls_memcmp(tag, &ciphertext->data[ciphertext->size-tag_size], tag_size) != 0)) {
/* HMAC was not the same. */
return gnutls_assert_val(GNUTLS_E_DECRYPTION_FAILED);
}
* a memcmp comparison is negligible over the crypto operations.
*/
if (unlikely
- (memcmp(tag, tag_ptr, tag_size) != 0 || pad_failed != 0)) {
+ (gnutls_memcmp(tag, tag_ptr, tag_size) != 0 || pad_failed != 0)) {
/* HMAC was not the same. */
dummy_wait(params, compressed, pad_failed, pad,
length + preamble_size);
/* a variant of memset that doesn't get optimized out */
void gnutls_memset(void *data, int c, size_t size);
+/* constant time memcmp */
+int gnutls_memcmp(const void *s1, const void *s2, size_t n);
+
typedef void (*gnutls_log_func) (int, const char *);
typedef void (*gnutls_audit_log_func) (gnutls_session_t, const char *);
void gnutls_global_set_log_function(gnutls_log_func log_func);
gnutls_aead_cipher_deinit;
gnutls_dh_params_import_raw2;
gnutls_memset;
+ gnutls_memcmp;
gnutls_pkcs12_bag_set_privkey;
} GNUTLS_3_0_0;
# include <gnutls_int.h>
#endif
-/*-
+/**
* gnutls_memset:
* @data: the memory to set
* @c: the constant byte to fill the memory with
*
* Returns: void.
*
- * Since: 3.3.3
- -*/
+ * Since: 3.4.0
+ **/
void gnutls_memset(void *data, int c, size_t size)
{
volatile unsigned volatile_zero = 0;
}
}
+/**
+ * gnutls_memcmp:
+ * @s1: the first address to compare
+ * @s2: the second address to compare
+ * @n: the size of memory to compare
+ *
+ * This function will operate similarly to memcmp(), but instead
+ * of comparing it will return 0 on memory match and non-zero
+ * on difference.
+ *
+ * Returns: void.
+ *
+ * Since: 3.4.0
+ **/
+int gnutls_memcmp(const void *s1, const void *s2, size_t n)
+{
+ unsigned i;
+ unsigned status = 0;
+ const uint8_t *_s1 = s1;
+ const uint8_t *_s2 = s2;
+
+ for (i=0;i<n;i++) {
+ status |= (_s1[i] ^ _s2[i]);
+ }
+
+ return status;
+}
+
#ifdef TEST_SAFE_MEMSET
int main()
{