Example #1
0
void decrypt(const unsigned char *burst_binary, byte *KC, unsigned char *decrypted_data, unsigned FN, int ul)
{
    byte AtoB[114];
    int i;

    /* KC is all zero: no decryption */

    if(KC[0] == 0 && KC[1] == 0 && KC[2] == 0 && KC[3] == 0 &
            KC[4] == 0 && KC[5] == 0 && KC[6] == 0 && KC[7] == 0) {
        for (i = 0; i < 148; i++) {
            decrypted_data[i] = burst_binary[i];
        }
        return;
    }

    keysetup(KC, FN);
    runA51(AtoB);
    if (ul)
        runA51(AtoB);

    for (i = 0; i < 57; i++) {
        decrypted_data[i] = AtoB[i] ^ burst_binary[i];
    }

    /* stealing bits and midamble */
    for (i = 57; i < 85; i++) {
        decrypted_data[i] = burst_binary[i];
    }

    for (i = 0; i < 57; i++) {
        decrypted_data[i+85] = AtoB[i+57] ^ burst_binary[i+85];
    }

}
Example #2
0
static int benchmark(const uint32_t num_iterations)
{
    ALIGN(16) uint8_t k[KEYLEN];
    ALIGN(16) uint8_t* m = (uint8_t*)malloc(MAX_BUFFER_LEN);
    ALIGN(16) uint8_t* c = (uint8_t*)malloc(MAX_BUFFER_LEN);
    ALIGN(16) uint8_t t[TAGLEN];

    uint64_t mlen;
    const uint64_t hlen = 0L;

    const uint64_t calibration = calibrate_timer();
    uint64_t t0, t1;
    uint32_t i, j;

    prepare_k(k, KEYLEN);
    prepare_m(m, MAX_BUFFER_LEN);

    riv_context_t ctx;
    keysetup(&ctx, k);

    puts("#mlen cpb");

    double timings[num_iterations];
    const uint32_t median = num_iterations / 2;
    // To load the timing code into the instruction cache
    get_time(); 

    for (j = MIN_CONT_MSG_LEN; j < MAX_CONT_MSG_LEN; ++j) {
        mlen = j;
        
        t0 = get_time();
        t1 = get_time();

        for (i = 0; i < num_iterations; ++i) {
            t0 = get_time();
            
            FN_TO_BENCH(&ctx, m, mlen, NULL, hlen, c, t);

            t1 = get_time();
            timings[i] = (double)(t1 - t0 - calibration) / j;
        }

        // Sort the measurements and print the median
        qsort(timings, num_iterations, sizeof(double), compare_doubles);
        printf("%5d %4.3lf \n", j, timings[median]);
    }

    uint32_t interval_len;
    uint32_t j_outer;

    for (j_outer = MAX_CONT_MSG_LEN; j_outer < MAX_BUFFER_LEN; 
        j_outer = 2*j_outer) {
        
        interval_len = j_outer / NUM_INTERVALS;

        for (j = j_outer; j < 2*j_outer; j += interval_len) {
            mlen = j;
            
            t0 = get_time();
            t1 = get_time();

            for (i = 0; i < num_iterations; ++i) {
                t0 = get_time();
                
                FN_TO_BENCH(&ctx, m, mlen, NULL, hlen, c, t);

                t1 = get_time();
                timings[i] = (double)(t1 - t0 - calibration) / j;
            }

            // Sort the measurements and print the median
            qsort(timings, num_iterations, sizeof(double), compare_doubles);
            printf("%5d %4.3lf \n", j, timings[median]);
        }
    }


    t0 = get_time();
    t1 = get_time();
    mlen = MAX_BUFFER_LEN;

    for (i = 0; i < num_iterations; ++i) {
        t0 = get_time();
        
        FN_TO_BENCH(&ctx, m, mlen, NULL, hlen, c, t);

        t1 = get_time();
        timings[i] = (double)(t1 - t0 - calibration) / mlen;
    }

    // Sort the measurements and print the median
    qsort(timings, num_iterations, sizeof(double), compare_doubles);
    printf("%5lu %4.3lf \n", mlen, timings[median]);

    free(c);
    free(m);
    return 0;
}
Example #3
0
static int run_test(const unsigned char *k,
                    const unsigned char *h,
                    const unsigned long long hlen,
                    const unsigned char *expected_m,
                    unsigned long long mlen,
                    const unsigned char *expected_c,
                    const unsigned long long expected_clen, 
                    const unsigned char *expected_t)
{
    poet_ctx_t ctx;
    const unsigned long long expected_mlen = mlen;
    unsigned char* c = (expected_clen == 0) ? 
        NULL : (unsigned char*)malloc((size_t)expected_clen);
    unsigned char* m = (mlen == 0) ? 
        NULL : (unsigned char*)malloc((size_t)mlen);
    unsigned char t[TAGLEN];
    unsigned long long clen;

    keysetup(&ctx, k);
    process_header(&ctx, h, hlen);
    encrypt_final(&ctx, expected_m, mlen, c, &clen, t);

    if (clen != expected_clen) {
        printf("Expected ciphertext length %llu, but was %llu bytes \n", 
            expected_clen, clen);
    }

    if (memcmp(expected_c, c, expected_clen)
        || memcmp(expected_t, t, TAGLEN)) {
        test_output(&ctx, k, KEYLEN, h, hlen, expected_m, mlen, 
            c, expected_clen, t, TAGLEN);
        puts("Encryption produced incorrect result");
        free(m);
        free(c);
        return -1;
    }

    keysetup(&ctx, k);
    process_header(&ctx, h, hlen);

    const int result = decrypt_final(&ctx, c, clen, t, m, &mlen);

    if (mlen != expected_mlen) {
        printf("Expected plaintext length %llu, but was %llu bytes \n", 
            expected_mlen, mlen);
    }

    test_output(&ctx, k, KEYLEN, h, hlen, m, mlen, c, expected_clen, t, TAGLEN);

    if (memcmp(expected_m, m, expected_mlen)) {
        puts("Decryption produced incorrect result");
        free(m);
        free(c);
        return -1;
    }
    
    if (result != 0) {
        puts("Verification failed");
    }
    
    free(m);
    free(c);
    return result;
}