Faster sodium_is_zero() and sodium_increment() helpers

Also add sodium_add(), since people tend to reimplement this in order to
add constants to nonces.
This commit is contained in:
Frank Denis
2015-11-16 22:14:27 +01:00
parent b74f644d3f
commit 397d50664a
2 changed files with 50 additions and 11 deletions
+3
View File
@@ -46,6 +46,9 @@ int sodium_is_zero(const unsigned char *n, const size_t nlen);
SODIUM_EXPORT
void sodium_increment(unsigned char *n, const size_t nlen);
SODIUM_EXPORT
void sodium_add(unsigned char *a, const unsigned char *b, const size_t len);
SODIUM_EXPORT
char *sodium_bin2hex(char * const hex, const size_t hex_maxlen,
const unsigned char * const bin, const size_t bin_len);
+47 -11
View File
@@ -157,25 +157,61 @@ sodium_compare(const unsigned char *b1_, const unsigned char *b2_, size_t len)
int
sodium_is_zero(const unsigned char *n, const size_t nlen)
{
size_t i;
unsigned char c = 0U;
for (i = (size_t) 0U; i < nlen; i++) {
size_t i = 0U;
#if !defined(CPU_UNALIGNED_ACCESS) || !defined(NATIVE_LITTLE_ENDIAN)
uint_fast8_t c = 0U;
#else
uint_fast64_t c = 0U;
for (; i < (nlen & ~0x7); i += 8U) {
c |= *((const uint64_t *) (const void *) &n[i]);
}
#endif
for (; i < nlen; i++) {
c |= n[i];
}
return ((c - 1U) >> 8) & 1U;
return (int) (((c - 1U) >> 8) & 1U);
}
void
sodium_increment(unsigned char *n, const size_t nlen)
{
size_t i;
unsigned int c = 1U << 8;
for (i = (size_t) 0U; i < nlen; i++) {
c >>= 8;
c += n[i];
size_t i = 0U;
#if !defined(CPU_UNALIGNED_ACCESS) || !defined(NATIVE_LITTLE_ENDIAN)
uint_fast16_t c = 1U;
#else
uint_fast64_t c = 1U;
for (; i < (nlen & ~0x3); i += 4U) {
c += (uint_fast64_t) *((const uint32_t *) (const void *) &n[i]);
*((uint32_t *) (void *) &n[i]) = (uint32_t) c;
c >>= 32;
}
#endif
for (; i < nlen; i++) {
c += (uint_fast16_t) n[i];
n[i] = (unsigned char) c;
c >>= 8;
}
}
void
sodium_add(unsigned char *a, const unsigned char *b, const size_t len)
{
size_t i = 0U;
#if !defined(CPU_UNALIGNED_ACCESS) || !defined(NATIVE_LITTLE_ENDIAN)
uint_fast16_t c = 0U;
#else
uint_fast64_t c = 0U;
for (; i < (len & ~0x3); i += 4U) {
c += (uint_fast64_t) *((const uint32_t *) (const void *) &a[i]) +
(uint_fast64_t) *((const uint32_t *) (const void *) &b[i]);
*((uint32_t *) (void *) &a[i]) = (uint32_t) c;
c >>= 32;
}
#endif
for (; i < len; i++) {
c += (uint_fast16_t) a[i] + (uint_fast16_t) b[i];
a[i] = (unsigned char) c;
c >>= 8;
}
}