mirror of
https://github.com/Telecominfraproject/OpenCellular.git
synced 2026-01-03 21:49:32 +00:00
Various cryptographic operations leak timing information if comparisons are not executed in constant time. This change adds DCRYPTO_equals(), a constant runtime comparator. Also replace crypto related callsites that used memcmp() as a binary comparator. BUG=none BRANCH=none TEST=tcg tests pass Change-Id: I3d3da3c0524c3a349d60675902d1f2d338ad455f Signed-off-by: nagendra modadugu <ngm@google.com> Reviewed-on: https://chromium-review.googlesource.com/410163 Commit-Ready: Nagendra Modadugu <ngm@google.com> Tested-by: Nagendra Modadugu <ngm@google.com> Reviewed-by: Marius Schilder <mschilder@chromium.org> Reviewed-by: Bill Richardson <wfrichar@chromium.org>
21 lines
439 B
C
21 lines
439 B
C
/* Copyright 2016 The Chromium OS Authors. All rights reserved.
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
* found in the LICENSE file.
|
|
*/
|
|
|
|
#include "dcrypto.h"
|
|
|
|
/* Constant time comparator. */
|
|
int DCRYPTO_equals(const void *a, const void *b, size_t len)
|
|
{
|
|
size_t i;
|
|
const uint8_t *pa = a;
|
|
const uint8_t *pb = b;
|
|
uint8_t diff = 0;
|
|
|
|
for (i = 0; i < len; i++)
|
|
diff |= pa[i] ^ pb[i];
|
|
|
|
return !diff;
|
|
}
|