Skip to content

Commit

Permalink
Bug 1307590 - Add NO_SANITIZE_ALIGNMENT to ignore misaligned int poin…
Browse files Browse the repository at this point in the history
…ters r=franziskus

Differential Revision: https://nss-dev.phacility.com/D62
  • Loading branch information
Tim Taubert committed Oct 5, 2016
1 parent 6c2b0fe commit e25b0f9
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 25 deletions.
10 changes: 10 additions & 0 deletions lib/freebl/blapii.h
Expand Up @@ -40,4 +40,14 @@ extern PRBool bl_parentForkedAfterC_Initialize;

SEC_END_PROTOS

#if defined(NSS_X86_OR_X64)
#define HAVE_UNALIGNED_ACCESS 1
#endif

#if (defined(__GNUC__) || defined(__clang__)) && defined(HAVE_UNALIGNED_ACCESS)
#define NO_SANITIZE_ALIGNMENT __attribute__((no_sanitize("alignment")))
#else
#define NO_SANITIZE_ALIGNMENT
#endif

#endif /* _BLAPII_H_ */
2 changes: 1 addition & 1 deletion lib/freebl/camellia.c
Expand Up @@ -34,7 +34,7 @@
* macros
*/

#if defined(SHA_ALLOW_UNALIGNED_ACCESS)
#if defined(HAVE_UNALIGNED_ACCESS)

/* require a CPU that allows unaligned access */

Expand Down
3 changes: 2 additions & 1 deletion lib/freebl/chacha20_vec.c
Expand Up @@ -9,6 +9,7 @@
#include <string.h>

#include "chacha20.h"
#include "blapii.h"

#ifndef CHACHA_RNDS
#define CHACHA_RNDS 20 /* 8 (high speed), 20 (conservative), 12 (middle) */
Expand Down Expand Up @@ -133,7 +134,7 @@ typedef unsigned vec __attribute__((vector_size(16)));
STORE(op + d + 8, LOAD(in + d + 8) ^ REVV_BE(v2)); \
STORE(op + d + 12, LOAD(in + d + 12) ^ REVV_BE(v3));

void
void NO_SANITIZE_ALIGNMENT
ChaCha20XOR(unsigned char *out, const unsigned char *in, unsigned int inlen,
const unsigned char key[32], const unsigned char nonce[12],
uint32_t counter)
Expand Down
10 changes: 2 additions & 8 deletions lib/freebl/des.c
Expand Up @@ -10,16 +10,10 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "des.h"
#include "blapii.h"
#include <stddef.h> /* for ptrdiff_t */
/* #define USE_INDEXING 1 */

/* Some processors automatically fix up unaligned memory access, so they can
* read or write a HALF (4 bytes) at a time whether the address is 4-byte
* aligned or not. */
#if defined(NSS_X86_OR_X64)
#define HAVE_UNALIGNED_ACCESS 1
#endif

/*
* The tables below are the 8 sbox functions, with the 6-bit input permutation
* and the 32-bit output permutation pre-computed.
Expand Down Expand Up @@ -561,7 +555,7 @@ DES_MakeSchedule(HALF *ks, const BYTE *key, DESDirection direction)
right ^= temp = ((left >> 4) ^ right) & 0x0f0f0f0f; \
left ^= temp << 4;

void
void NO_SANITIZE_ALIGNMENT
DES_Do1Block(HALF *ks, const BYTE *inbuf, BYTE *outbuf)
{
register HALF left, right;
Expand Down
9 changes: 5 additions & 4 deletions lib/freebl/desblapi.c
Expand Up @@ -14,6 +14,7 @@
#endif

#include "des.h"
#include "blapii.h"
#include <stddef.h>
#include "secerr.h"

Expand Down Expand Up @@ -54,7 +55,7 @@ DES_EDE3_ECB(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
}
}

static void
static void NO_SANITIZE_ALIGNMENT
DES_CBCEn(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
{
const BYTE *bufend = in + len;
Expand All @@ -71,7 +72,7 @@ DES_CBCEn(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
}
}

static void
static void NO_SANITIZE_ALIGNMENT
DES_CBCDe(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
{
const BYTE *bufend;
Expand All @@ -91,7 +92,7 @@ DES_CBCDe(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
}
}

static void
static void NO_SANITIZE_ALIGNMENT
DES_EDE3CBCEn(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
{
const BYTE *bufend = in + len;
Expand All @@ -110,7 +111,7 @@ DES_EDE3CBCEn(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
}
}

static void
static void NO_SANITIZE_ALIGNMENT
DES_EDE3CBCDe(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
{
const BYTE *bufend;
Expand Down
2 changes: 1 addition & 1 deletion lib/freebl/ecl/curve25519_64.c
Expand Up @@ -199,7 +199,7 @@ fsquare(felem *output, const felem *in)
}

/* Take a 32-byte number and expand it into polynomial form */
static void
static void NO_SANITIZE_ALIGNMENT
fexpand(felem *output, const u8 *in)
{
output[0] = *((const uint64_t *)(in)) & MASK51;
Expand Down
1 change: 1 addition & 0 deletions lib/freebl/ecl/ecl-priv.h
Expand Up @@ -8,6 +8,7 @@
#include "ecl.h"
#include "mpi.h"
#include "mplogic.h"
#include "../blapii.h"

/* MAX_FIELD_SIZE_DIGITS is the maximum size of field element supported */
/* the following needs to go away... */
Expand Down
5 changes: 3 additions & 2 deletions lib/freebl/md5.c
Expand Up @@ -13,6 +13,7 @@
#include "prlong.h"

#include "blapi.h"
#include "blapii.h"

#define MD5_HASH_LEN 16
#define MD5_BUFFER_SIZE 64
Expand Down Expand Up @@ -338,7 +339,7 @@ md5_prep_buffer_le(MD5Context *cx, const PRUint8 *beBuf)
#define II(a, b, c, d, bufint, s, ti) \
a = b + cls(a + I(b, c, d) + bufint + ti, s)

static void
static void NO_SANITIZE_ALIGNMENT
md5_compress(MD5Context *cx, const PRUint32 *wBuf)
{
PRUint32 a, b, c, d;
Expand Down Expand Up @@ -445,7 +446,7 @@ MD5_Update(MD5Context *cx, const unsigned char *input, unsigned int inputLen)
/* Iterate over 64-byte chunks of the message. */
while (inputLen >= MD5_BUFFER_SIZE) {
#ifdef IS_LITTLE_ENDIAN
#ifdef NSS_X86_OR_X64
#ifdef HAVE_UNALIGNED_ACCESS
/* x86 can handle arithmetic on non-word-aligned buffers */
wBuf = (PRUint32 *)input;
#else
Expand Down
3 changes: 2 additions & 1 deletion lib/freebl/poly1305-donna-x64-sse2-incremental-source.c
Expand Up @@ -12,6 +12,7 @@
#include <stdint.h>

#include "poly1305.h"
#include "blapii.h"

#define ALIGN(x) __attribute__((aligned(x)))
#define INLINE inline
Expand Down Expand Up @@ -91,7 +92,7 @@ static poly1305_state_internal INLINE
}

/* copy 0-63 bytes */
static void INLINE
static void INLINE NO_SANITIZE_ALIGNMENT
poly1305_block_copy(uint8_t *dst, const uint8_t *src, size_t bytes)
{
size_t offset = src - dst;
Expand Down
4 changes: 2 additions & 2 deletions lib/freebl/sha_fast.c
Expand Up @@ -115,7 +115,7 @@ SHA1_Update(SHA1Context *ctx, const unsigned char *dataIn, unsigned int len)
shaCompress(&ctx->H[H2X], ctx->W);
}
}
#if !defined(SHA_ALLOW_UNALIGNED_ACCESS)
#if !defined(HAVE_UNALIGNED_ACCESS)
if ((ptrdiff_t)dataIn % sizeof(PRUint32)) {
while (len >= 64U) {
memcpy(ctx->B, dataIn, 64);
Expand Down Expand Up @@ -240,7 +240,7 @@ SHA1_EndRaw(SHA1Context *ctx, unsigned char *hashout,
* results in code that is 3 times faster than the previous NSS sha_fast
* code on AMD64.
*/
static void
static void NO_SANITIZE_ALIGNMENT
shaCompress(volatile SHA_HW_t *X, const PRUint32 *inbuf)
{
register SHA_HW_t A, B, C, D, E;
Expand Down
7 changes: 2 additions & 5 deletions lib/freebl/sha_fast.h
Expand Up @@ -6,6 +6,7 @@
#define _SHA_FAST_H_

#include "prlong.h"
#include "blapii.h"

#define SHA1_INPUT_LEN 64

Expand Down Expand Up @@ -118,10 +119,6 @@ swap4b(PRUint32 value)
#define SHA_ROTL(X, n) (tmp = (X), ((tmp) << (n)) | ((tmp) >> (32 - (n))))
#endif

#if defined(NSS_X86_OR_X64)
#define SHA_ALLOW_UNALIGNED_ACCESS 1
#endif

#if !defined(SHA_HTONL)
#define SHA_MASK 0x00FF00FF
#if defined(IS_LITTLE_ENDIAN)
Expand All @@ -137,7 +134,7 @@ swap4b(PRUint32 value)
#define SHA_BYTESWAP(x) x = SHA_HTONL(x)

#define SHA_STORE(n) ((PRUint32*)hashout)[n] = SHA_HTONL(ctx->H[n])
#if defined(SHA_ALLOW_UNALIGNED_ACCESS)
#if defined(HAVE_UNALIGNED_ACCESS)
#define SHA_STORE_RESULT \
SHA_STORE(0); \
SHA_STORE(1); \
Expand Down

0 comments on commit e25b0f9

Please sign in to comment.