[SLOF] [PATCH 4/4] Add testprograms

Alexey Kardashevskiy aik at ozlabs.ru
Thu Jul 1 14:11:45 AEST 2021



On 30/06/2021 22:26, Stefan Berger wrote:
> 
> On 6/29/21 11:10 PM, Alexey Kardashevskiy wrote:
>> What does this actually test? It runs sha384/etc but it is not 
>> comparing the outcome with known good values or anything like this.
>>
> I visually compared the results against the expected results from test 
> vectors or results from  command line output of echo -n "..." | 
> sha512sum for example.

if would make more sense if you hardcoded those outputs to the test and 
compared, then the next reader won't have questions and will know 
exactly what it tests.



> 
> 
>>
>> On 16/06/2021 00:41, Stefan Berger wrote:
>>> ---
>>>   lib/libtpm/sha.c    | 42 ++++++++++++++++++++++++++++++
>>>   lib/libtpm/sha512.c | 62 +++++++++++++++++++++++++++++++++++++++++++++
>>>   2 files changed, 104 insertions(+)
>>>
>>> diff --git a/lib/libtpm/sha.c b/lib/libtpm/sha.c
>>> index 98ab300..d8330f4 100644
>>> --- a/lib/libtpm/sha.c
>>> +++ b/lib/libtpm/sha.c
>>> @@ -197,3 +197,45 @@ void sha1(const uint8_t *data, uint32_t length, 
>>> uint8_t *hash)
>>>       sha1_do(&ctx, data, length);
>>>       memcpy(hash, &ctx.h[0], 20);
>>>   }
>>> +
>>> +#ifdef MAIN
>>> +
>>> +#include <stdio.h>
>>> +// gcc -DMAIN sha.c -o sha-test -I ../../include -I ../../slof -I 
>>> ../../lib/libc/include
>>> +int main(void)
>>> +{
>>> +    unsigned i, j;
>>> +    uint8_t hash[20];
>>> +    char buffer[70];
>>> +
>>> +    sha1("abc", 3, hash);
>>> +    for (i = 0; i < sizeof(hash); i++) {
>>> +        printf("%02x", hash[i]);
>>> +    }
>>> +    printf("\n");
>>> +
>>> +    sha1("", 0, hash);
>>> +    for (i = 0; i < sizeof(hash); i++) {
>>> +        printf("%02x", hash[i]);
>>> +    }
>>> +    printf("\n");
>>> +
>>> + sha1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 
>>> 448/8, hash);
>>> +    for (i = 0; i < sizeof(hash); i++) {
>>> +        printf("%02x", hash[i]);
>>> +    }
>>> +    printf("\n");
>>> +
>>> +    for (i = 54; i < sizeof(buffer); i++) {
>>> +        memset(buffer, 0, sizeof(buffer));
>>> +        for (j = 0; j <= i; j++)
>>> +        buffer[j] = 'a';
>>> +        printf("input %d: %s\n", i, buffer);
>>> +        sha1(buffer, i, hash);
>>> +        for (j = 0; j < sizeof(hash); j++) {
>>> +            printf("%02x", hash[j]);
>>> +        }
>>> +        printf("\n");
>>> +    }
>>> +}
>>> +#endif
>>> diff --git a/lib/libtpm/sha512.c b/lib/libtpm/sha512.c
>>> index a59f659..addeacf 100644
>>> --- a/lib/libtpm/sha512.c
>>> +++ b/lib/libtpm/sha512.c
>>> @@ -241,3 +241,65 @@ void sha512(const uint8_t *data, uint32_t 
>>> length, uint8_t *hash)
>>>       sha512_do(&ctx, data, length);
>>>       memcpy(hash, ctx.h, sizeof(ctx.h));
>>>   }
>>> +
>>> +
>>> +#ifdef MAIN
>>> +
>>> +#include <stdio.h>
>>> +// gcc -DMAIN sha512.c -o sha512-test -I ../../include -I ../../slof 
>>> -I ../../lib/libc/include
>>> +int main(void)
>>> +{
>>> +    unsigned i, j;
>>> +    uint8_t hash[64];
>>> +    char buffer[128];
>>> +
>>> +    sha512("abc", 3, hash);
>>> +    for (i = 0; i < sizeof(hash); i++) {
>>> +        printf("%02x", hash[i]);
>>> +    }
>>> +    printf("\n");
>>> +
>>> +    sha384("abc", 3, hash);
>>> +    for (i = 0; i < 384/8; i++) {
>>> +        printf("%02x", hash[i]);
>>> +    }
>>> +    printf("\n");
>>> +
>>> +    sha512("", 0, hash);
>>> +    for (i = 0; i < sizeof(hash); i++) {
>>> +        printf("%02x", hash[i]);
>>> +    }
>>> +    printf("\n");
>>> +
>>> +    sha384("", 0, hash);
>>> +    for (i = 0; i < 384/8; i++) {
>>> +        printf("%02x", hash[i]);
>>> +    }
>>> +    printf("\n");
>>> +
>>> + sha512("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 
>>> 448/8, hash);
>>> +    for (i = 0; i < sizeof(hash); i++) {
>>> +        printf("%02x", hash[i]);
>>> +    }
>>> +    printf("\n");
>>> +
>>> + sha384("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 
>>> 448/8, hash);
>>> +    for (i = 0; i < 384/8; i++) {
>>> +        printf("%02x", hash[i]);
>>> +    }
>>> +    printf("\n");
>>> +
>>> +
>>> +    for (i = 110; i < sizeof(buffer); i++) {
>>> +        memset(buffer, 0, sizeof(buffer));
>>> +        for (j = 0; j <= i; j++)
>>> +        buffer[j] = 'a';
>>> +        printf("input %d: %s\n", i, buffer);
>>> +        sha512(buffer, i, hash);
>>> +        for (j = 0; j < sizeof(hash); j++) {
>>> +            printf("%02x", hash[j]);
>>> +        }
>>> +        printf("\n");
>>> +    }
>>> +}
>>> +#endif
>>>
>>

-- 
Alexey


More information about the SLOF mailing list