Ejemplo n.º 1
0
static void encryptRndNonceTest()
{
    uint8_t buff[44];
    Bits_memset(buff, 0, 44);

    uint8_t nonce[24];
    Bits_memset(nonce, 0, 24);

    uint8_t secret[32];
    Bits_memset(secret, 0, 32);

    struct Message m = { .bytes=&buff[32], .length=12, .padding=32};
    CString_strcpy((char*) m.bytes, "hello world");

    CryptoAuth_encryptRndNonce(nonce, &m, secret);

    uint8_t* expected = (uint8_t*) "1391ac5d03ba9f7099bffbb6e6c69d67ae5bd79391a5b94399b293dc";
    uint8_t output[57];
    Hex_encode(output, 57, m.bytes, m.length);

    //printf("\n%s\n%s\n", (char*) expected, (char*) output);
    Assert_true(!Bits_memcmp(expected, output, 56));

    Assert_true(!CryptoAuth_decryptRndNonce(nonce, &m, secret));
    Assert_true(m.length == 12 && !Bits_memcmp(m.bytes, "hello world", m.length));
}

static struct Random* evilRandom(struct Allocator* alloc, struct Log* logger)
{
    struct RandomSeed* evilSeed = DeterminentRandomSeed_new(alloc);
    return Random_newWithSeed(alloc, logger, evilSeed, NULL);
}

static void createNew()
{
    struct Allocator* allocator = MallocAllocator_new(BUFFER_SIZE);
    struct CryptoAuth* ca =
        CryptoAuth_new(allocator, privateKey, eventBase, NULL, evilRandom(allocator, NULL));
    /*for (int i = 0; i < 32; i++) {
        printf("%.2x", ca->publicKey[i]);
    }*/
    Assert_true(Bits_memcmp(ca->publicKey, publicKey, 32) == 0);
    Allocator_free(allocator);
}

static uint8_t receiveMessage(struct Message* message, struct Interface* iface)
{
    Message_pop(message, NULL, 4, NULL);
    *((struct Message**)iface->receiverContext) = message;
    return Error_NONE;
}
Ejemplo n.º 2
0
int main()
{
    struct Allocator* alloc = MallocAllocator_new(1<<20);
    struct RandomSeed* seed = DeterminentRandomSeed_new(alloc, NULL);
    struct Writer* w = FileWriter_new(stdout, alloc);
    struct Log* logger = WriterLog_new(w, alloc);
    struct Random* rand = Random_newWithSeed(alloc, logger, seed, NULL);

    // < 8 length
    checkBytes(rand, 0, 3);
    checkBytes(rand, 3, 6);

    // unaligned beginning (length and align add up to a multiple of 8)
    checkBytes(rand, 3, 29); // 1 byte aligned
    checkBytes(rand, 6, 10); // 2 byte aligned
    checkBytes(rand, 4, 20); // 4 byte aligned

    // unaligned end
    checkBytes(rand, 0, 9); // 1 byte
    checkBytes(rand, 0, 14); // 2 byte
    checkBytes(rand, 0, 20); // 4 byte

    // both unaligned
    checkBytes(rand, 1, 28); // 1,1 byte aligned
    checkBytes(rand, 3, 19); // 1,2 byte aligned
    checkBytes(rand, 5, 23); // 1,4 byte aligned
    checkBytes(rand, 2, 9);  // 2,1 byte aligned
    checkBytes(rand, 6, 12); // 2,2 byte aligned
    checkBytes(rand, 6, 14); // 2,4 byte aligned
    checkBytes(rand, 4, 19); // 4,1 byte aligned
    checkBytes(rand, 4, 10); // 4,2 byte aligned
    checkBytes(rand, 4, 32); // 4,4 byte aligned


    test179(alloc, logger);


    /* torture
    uint8_t selections[2];
    for (int i = 0; i < 100000; i++) {
        Random_bytes(rand, selections, 2);
        checkBytes(rand, selections[0] % 8, selections[1] % 128);
    }*/

    Allocator_free(alloc);
    return 0;
}
Ejemplo n.º 3
0
static void encryptRndNonceTest()
{
    uint8_t buff[44];
    Bits_memset(buff, 0, 44);

    uint8_t nonce[24];
    Bits_memset(nonce, 0, 24);

    uint8_t secret[32];
    Bits_memset(secret, 0, 32);

    struct Message m = { .bytes=&buff[32], .length=HELLOWORLDLEN, .padding=32};
    CString_strcpy((char*) m.bytes, HELLOWORLDLOWER);

    CryptoAuth_encryptRndNonce(nonce, &m, secret);

    uint8_t* expected = (uint8_t*) "1391ac5d03ba9f7099bffbb6e6c69d67ae5bd79391a5b94399b293dc";
    uint8_t output[57];
    Hex_encode(output, 57, m.bytes, m.length);

    printf("\n%s\n%s\n", (char*) expected, (char*) output);
    Assert_true(!Bits_memcmp(expected, output, 56));

    Assert_true(!CryptoAuth_decryptRndNonce(nonce, &m, secret));
    Assert_true(m.length == HELLOWORLDLEN && !Bits_memcmp(m.bytes, HELLOWORLDLOWER, m.length));
}

static struct Random* evilRandom(struct Allocator* alloc, struct Log* logger)
{
    struct RandomSeed* evilSeed = DeterminentRandomSeed_new(alloc, NULL);
    return Random_newWithSeed(alloc, logger, evilSeed, NULL);
}

struct Context
{
    struct Allocator* alloc;
    struct CryptoAuth* ca;
    struct CryptoAuth_Session* sess;
    struct Log* log;
    struct EventBase* base;
};

static struct Context* setUp(uint8_t* myPrivateKey,
                             uint8_t* herPublicKey,
                             uint8_t* authPassword,
                             struct Allocator* alloc)
{
    struct Context* ctx = Allocator_calloc(alloc, sizeof(struct Context), 1);
    struct Log* log = ctx->log = FileWriterLog_new(stdout, alloc);
    struct EventBase* base = ctx->base = EventBase_new(alloc);
    struct CryptoAuth* ca = ctx->ca =
        CryptoAuth_new(alloc, myPrivateKey, base, log, evilRandom(alloc, log));

    struct CryptoAuth_Session* sess = ctx->sess =
        CryptoAuth_newSession(ca, alloc, herPublicKey, NULL, false, Gcc_FILE);

    if (authPassword) {
        CryptoAuth_setAuth(String_CONST(authPassword), NULL, sess);
    }

    return ctx;
}

static void testHello(uint8_t* password, uint8_t* expectedOutput)
{
    Assert_true(CString_strlen((char*)expectedOutput) == 264);
    struct Allocator* alloc = MallocAllocator_new(1<<20);
    struct Context* ctx = setUp(NULL, HERPUBKEY, password, alloc);
    struct Message* msg = Message_new(0, CryptoHeader_SIZE + 12, alloc);
    Message_push(msg, HELLOWORLD, HELLOWORLDLEN, NULL);

    Assert_true(!CryptoAuth_encrypt(ctx->sess, msg));

    char* actual = Hex_print(msg->bytes, msg->length, alloc);
    if (CString_strcmp(actual, expectedOutput)) {
        Assert_failure("Test failed.\n"
                       "Expected %s\n"
                       "     Got %s\n", expectedOutput, actual);
    }
    Allocator_free(alloc);
}