Am Friday 14 September 2012 schrieb Niels Möller:
Tim Ruehsen tim.ruehsen@gmx.de writes:
The HL and LDUP macros were really ugly and in very most cases not even needed.
They're intended as a kind of "mini-language" for the tests. I have earlier (in the lsh project, years ago) tried using m4 for the same purpose, but in the end I concluded that it was overal less trouble to just use the C preprocessor.
But after all, you still won't need them. Just have a look in the patched hmac-test.c, where you can find are mixtures of hex data and strings.
What in particular do you find ugly? That they expand to two arguments? Short cryptic names? Something else?
Yes. Yes. Yes. But the main uglyness is that they don't track memory allocations. You could of course (as you proposed) include some memory tracking structures/functions. But that would even be less elegant (brrr..., it shakes me a bit).
Wold you like them better if they expanded to some constructor calls for proper string objects olding both length and data, which were then passed to the various test helper functions? I don't care very much about the implementation of the mini-language.
Yes, that is an alternative, though it requires to throw away most of the already working patches (sniff). H() would merge into HL().
Proposal (not tested): typedef struct { unsigned length; uint8_t *data; } testdata_t;
/* example HL() implementation with just one malloc() */ testdata_t *HL(const uint8_t *hex) { unsigned length = decode_hex_length(hex); testdata_t *data = xalloc(sizeof(testdata_t) + length);
data.length = length; data.data = &data.data + sizeof(data.data); decode_hex(data.data, hex);
return data; }
/* example test function with my first patch */ void test_cipher(const struct nettle_cipher *cipher, const uint8_t *key_hex, const uint8_t *cleartext_hex, const uint8_t *ciphertext_hex) { unsigned key_length = decode_hex_length(key_hex); const uint8_t *key = decode_hex_dup(key_hex); unsigned length = decode_hex_length(cleartext_hex); const uint8_t *cleartext = decode_hex_dup(cleartext_hex); const uint8_t *ciphertext = decode_hex_dup(ciphertext_hex);
...
free((void *)ciphertext); free((void *)cleartext); free((void *)key); }
/* example test function with testdata_t */ void test_cipher(const struct nettle_cipher *cipher, testdata_t *key, testdata_t *cleartext, testdata_t *ciphertext) { // use key.length instead of key_length, etc. ...
free(ciphertext); free(cleartext); free(key); }
/* example call to test function */ test_cipher(&nettle_aes128, HL("0001020305060708 0A0B0C0D0F101112"), HL("506812A45F08C889 B97F5980038B8359"), HL("D8F532538289EF7D 06B506A4FD5BE9C9"));
BTW xmalloc should be slightly changed to not allow size 0 (either fail or allocated a 1 byte size). From my man pages: If size is 0, then malloc() returns either NULL, or a unique pointer value that can later be successfully passed to free().
Regards, Tim