Lift alignment requirements in crypto_generichash()

This commit is contained in:
Frank Denis 2017-12-21 17:53:09 +01:00
parent 2604a41774
commit 1e7839a90c
3 changed files with 51 additions and 23 deletions

View File

@ -20,7 +20,7 @@ extern "C" {
# pragma pack(push, 1)
#endif
typedef struct CRYPTO_ALIGN(64) crypto_generichash_blake2b_state {
typedef struct crypto_generichash_blake2b_state {
uint64_t h[8];
uint64_t t[2];
uint64_t f[2];

View File

@ -6,51 +6,78 @@ int
main(void)
{
#define MAXLEN 64
crypto_generichash_state st;
unsigned char in[MAXLEN], out[crypto_generichash_BYTES_MAX],
k[crypto_generichash_KEYBYTES_MAX];
crypto_generichash_state *st;
unsigned char in[MAXLEN];
unsigned char out[crypto_generichash_BYTES_MAX];
unsigned char k[crypto_generichash_KEYBYTES_MAX];
size_t h, i, j;
assert(crypto_generichash_statebytes() >= sizeof st);
for (h = 0; h < crypto_generichash_KEYBYTES_MAX; ++h)
assert(crypto_generichash_statebytes() >= sizeof *st);
st = sodium_malloc(crypto_generichash_statebytes());
for (h = 0; h < crypto_generichash_KEYBYTES_MAX; ++h) {
k[h] = (unsigned char) h;
}
for (i = 0; i < MAXLEN; ++i) {
in[i] = (unsigned char) i;
if (crypto_generichash_init(&st, k,
if (crypto_generichash_init(st, k,
1 + i % crypto_generichash_KEYBYTES_MAX,
1 + i % crypto_generichash_BYTES_MAX) != 0) {
printf("crypto_generichash_init()\n");
return 1;
}
crypto_generichash_update(&st, in, i);
crypto_generichash_update(&st, in, i);
crypto_generichash_update(&st, in, i);
if (crypto_generichash_final(&st, out,
crypto_generichash_update(st, in, i);
crypto_generichash_update(st, in, i);
crypto_generichash_update(st, in, i);
if (crypto_generichash_final(st, out,
1 + i % crypto_generichash_BYTES_MAX) != 0) {
printf("crypto_generichash_final() should have returned 0\n");
}
for (j = 0; j < 1 + i % crypto_generichash_BYTES_MAX; ++j) {
printf("%02x", (unsigned int)out[j]);
printf("%02x", (unsigned int) out[j]);
}
printf("\n");
if (crypto_generichash_final(&st, out,
if (crypto_generichash_final(st, out,
1 + i % crypto_generichash_BYTES_MAX) != -1) {
printf("crypto_generichash_final() should have returned -1\n");
}
}
sodium_free(st);
assert(crypto_generichash_init(&st, k, sizeof k, 0U) == -1);
assert(crypto_generichash_init(&st, k, sizeof k,
/* unaligned state */
st = sodium_malloc(crypto_generichash_statebytes() + 1U);
i = 0;
if (crypto_generichash_init(st, k,
crypto_generichash_KEYBYTES_MAX,
crypto_generichash_BYTES_MAX) != 0) {
printf("crypto_generichash_init(2)\n");
return 1;
}
crypto_generichash_update(st, in, i);
crypto_generichash_update(st, in, i);
crypto_generichash_update(st, in, i);
if (crypto_generichash_final(st, out,
crypto_generichash_BYTES_MAX) != 0) {
printf("crypto_generichash_final(2) should have returned 0\n");
}
for (j = 0; j < crypto_generichash_BYTES_MAX; ++j) {
printf("%02x", (unsigned int) out[j]);
}
printf("\n");
assert(crypto_generichash_init(st, k, sizeof k, 0U) == -1);
assert(crypto_generichash_init(st, k, sizeof k,
crypto_generichash_BYTES_MAX + 1U) == -1);
assert(crypto_generichash_init(&st, k, crypto_generichash_KEYBYTES_MAX + 1U,
assert(crypto_generichash_init(st, k, crypto_generichash_KEYBYTES_MAX + 1U,
sizeof out) == -1);
assert(crypto_generichash_init(&st, k, 0U, sizeof out) == 0);
assert(crypto_generichash_init(&st, k, 1U, sizeof out) == 0);
assert(crypto_generichash_init(&st, NULL, 1U, 0U) == -1);
assert(crypto_generichash_init(&st, NULL, crypto_generichash_KEYBYTES,
assert(crypto_generichash_init(st, k, 0U, sizeof out) == 0);
assert(crypto_generichash_init(st, k, 1U, sizeof out) == 0);
assert(crypto_generichash_init(st, NULL, 1U, 0U) == -1);
assert(crypto_generichash_init(st, NULL, crypto_generichash_KEYBYTES,
1U) == 0);
assert(crypto_generichash_init(&st, NULL, crypto_generichash_KEYBYTES,
assert(crypto_generichash_init(st, NULL, crypto_generichash_KEYBYTES,
0U) == -1);
sodium_free(st);
return 0;
}

View File

@ -62,3 +62,4 @@ d09b717a0c80f581c07b8813e0ae79cec2188f77122f7477954610655a20420f13eb1b68cacde8c1
23ac1ccd5e7df51b65b284650158d662e7ef51ebae01b879f39cec484b688c792f8e854bd8ca31ffe8796d28f10e49ab402dab47878a21cb95556dc32b0a
f8f5323ebcc28bf927e72d342b5b70d80ba67794afb4c28debad21b0dae24c7a9252e862eb4b83bea6d9c0bb7c108983c987f13d73f250c7f14483f0454a24
55b97ca594d68ccf69a0a93fe7fa4004c7e2947a8cac4ca4a44e17ac6876f472e3f221b341a28004cd35a79cfad7fabb9378ce5af03e4c0445ebbe9540943bbd
10ebb67700b1868efb4417987acf4690ae9d972fb7a590c2f02871799aaa4786b5e996e8f0f4eb981fc214b005f42d2ff4233499391653df7aefcbc13fc51568