예제 #1
0
static int test_discreet(const struct ccdigest_info *di, test_vector *vector) {
    uint8_t answer[128];
    size_t total = vector->len;
    size_t chunk = vector->len/2;
    uint8_t *p = vector->input;
    uint8_t ctxfrontguard[4096];
    ccdigest_di_decl(di, ctx);
    uint8_t ctxrearguard[4096];
    memset(ctxfrontguard, 0xee, 4096);
    memset(ctxrearguard, 0xee, 4096);
    // break it up into pieces.
    ccdigest_init(di, ctx);
    ok(guard_ok(ctxfrontguard, 0xee, 4096), "context is safe");
    ok(guard_ok(ctxrearguard, 0xee, 4096), "context is safe");
    do {
        ccdigest_update(di, ctx, chunk, p);
        total -= chunk;
        p += chunk;
        chunk /= 2;
        if(chunk == 0) chunk = total;
    } while(total);
    ok(guard_ok(ctxfrontguard, 0xee, 4096), "context is safe");
    ok(guard_ok(ctxrearguard, 0xee, 4096), "context is safe");
    
    ccdigest_final(di, ctx, answer);
    
    ok(guard_ok(ctxfrontguard, 0xee, 4096), "context is safe");
    ok(guard_ok(ctxrearguard, 0xee, 4096), "context is safe");
    ok(test_answer(di, vector, answer), "check answer");
    return 1;
}
예제 #2
0
int main(int argc, char **argv) {
  if(argc == 1) {
    try {
      run();
    }
    catch(const std::string & msg) {
      std::cout <<"Test_answer raised exception "<< msg << std::endl;
    }
  }
  else if(argc == 2) {
    std::vector<std::string> v;
    try {
      std::cout<<argv[1] << " "<< parse(argv[1], v)<<std::endl;
      for(auto s : v) {
        std::cout<< s <<"\n";
      }
    }
    catch(const std::string & msg) {
      std::cout<< "Raised exception "<< msg << std::endl;
    }
  }
  else if(argc == 3) {
    test_answer(argv[1], argv[2]);
  }
}
예제 #3
0
static int test_oneshot(const struct ccmode_cbc *cbc, char *mode_name, test_vector *vector) {
    uint8_t answer[CMAC_BLOCKSIZE];
    byteBuffer key = hexStringToBytes(vector->keyStr);
    byteBuffer in = hexStringToBytes(vector->inStr);
    cccmac(cbc, key->bytes, in->len, in->bytes, answer);
    ok(test_answer(mode_name, vector, answer, "one-shot"), "check answer");
    return 1;
}
예제 #4
0
static int test_oneshot(const struct ccdigest_info *di, test_vector *vector) {
    uint8_t answer[vector->result_len];
    byteBuffer salt = hexStringToBytes(vector->saltStr);
    ccpbkdf2_hmac(di, strlen(vector->password), vector->password, salt->len, salt->bytes, vector->iterations, vector->result_len, answer);
    ok(test_answer(di, vector, vector->result_len, answer), "check answer");
    free(salt);
    return 1;
}
예제 #5
0
static int test_discreet(const struct ccmode_cbc *cbc, char *mode_name, test_vector *vector) {

    uint8_t answer[CMAC_BLOCKSIZE];
    uint8_t ctxfrontguard[4096];
    cccmac_mode_decl(cbc, cmac);
    uint8_t ctxrearguard[4096];
    memset(ctxfrontguard, 0xee, 4096);
    memset(ctxrearguard, 0xee, 4096);
    
    byteBuffer key = hexStringToBytes(vector->keyStr);
    byteBuffer in = hexStringToBytes(vector->inStr);
    size_t nblocks = in->len/CMAC_BLOCKSIZE;
    size_t partial = in->len%CMAC_BLOCKSIZE;
    uint8_t *data = in->bytes;
    
    if(nblocks < 2) nblocks = 0; // must have >= 1 block for final
    else if(!partial) nblocks--; // have to have data for final
    cccmac_init(cbc, cmac, key->bytes);
    ok(guard_ok(ctxfrontguard, 0xee, 4096), "context is safe");
    ok(guard_ok(ctxrearguard, 0xee, 4096), "context is safe");
    byteBuffer correct_answer_k1 = hexStringToBytes(vector->k1Str);
    byteBuffer correct_answer_k2 = hexStringToBytes(vector->k2Str);
    byteBuffer answer_k1 = bytesToBytes(cccmac_k1(cmac), 16);
    byteBuffer answer_k2 = bytesToBytes(cccmac_k2(cmac), 16);
    showBytesAreEqual(correct_answer_k1, answer_k1, "Subkey K1 is correct");
    showBytesAreEqual(correct_answer_k2, answer_k2, "Subkey K2 is correct");

    for(size_t i=0; i<nblocks; i++) {
        cccmac_block_update(cbc, cmac, 1, data);
        data+=CMAC_BLOCKSIZE;
    }
    ok(guard_ok(ctxfrontguard, 0xee, 4096), "context is safe");
    ok(guard_ok(ctxrearguard, 0xee, 4096), "context is safe");
    
    cccmac_final(cbc, cmac, in->len - nblocks * CMAC_BLOCKSIZE, data, answer);
    
    ok(guard_ok(ctxfrontguard, 0xee, 4096), "context is safe");
    ok(guard_ok(ctxrearguard, 0xee, 4096), "context is safe");
    ok(test_answer(mode_name, vector, answer, "discreet calls"), "check answer");
    return 1;
}
예제 #6
0
static int test_oneshot(const struct ccdigest_info *di, test_vector *vector) {
    uint8_t answer[128];
    ccdigest(di, vector->len, vector->input, answer);
    ok(test_answer(di, vector, answer), "check answer");
    return 1;
}
예제 #7
0
void run() {
  test_answer("5=" ,"5");
  test_answer("5+3=" , "8");
  test_answer("5+3+6 =" , "14");
  test_answer("5-3 =", "2");
  test_answer("5-3 -    1 =", "1");
  test_answer("5+3 - 1 =", "7");
  test_answer("5*3=", "15");
  test_answer("5 * 3 * 2=", "30");
  test_answer("5 - 3 *2=", "-1");
  test_answer("4 / 2 =", "2");
  test_answer("3 /2=", "1.5");
  test_answer("4 + 4 / 2 =", "6");
  test_answer("-5=", "-5");
  test_answer("5 - -3=", "8");
  test_answer("5 + - 3 =", "2");
  test_answer("5 * -3=", "-15");
  test_answer("(5)=", "5");
  test_answer("(5 + 3)=", "8");
  test_answer("(5 + 3) / 2=", "4");
  test_answer("(5 + (11-2) * 2) / 2=", "11.5");
  test_answer("5.3 + 8.2 * 2.1 =", "22.52");
  //TODO: fix this case
  test_answer("(3.2-((4.2+8.1)*2)-1)=", "-22.4");
  test_answer("1E1 - 2E-2=", "9.98");
  test_answer("5--2E-2=", "5.02");
  //test_answer("4 + 3E", "");
  test_answer("A * 2=", "0");
  test_answer("6.5 - 2.5/2 = A", "5.25");
  test_answer("3*2.5 = B", "7.5");
  test_answer("A + B = C", "12.75");
  test_answer("C =", "12.75");
  test_answer("12345678 * 100=", "1.2345678E9");
  //test_answer("999999999+3=", "1E9");
  test_answer("99999*99999*2000=", "1.99996E13");
  test_answer("0.33333333- 1/3=", "0");
  test_answer("3/5 - 0.59999999 =", "0.00000001");
  // TODO: CHECK THIS ONE LATER
  test_answer("1/2*2.0000001=", "1.0000001");
//test_answer("1-(-4)^(1/2)=", "");
  test_answer("(1-1)/2=", "0");
  test_answer("4^-5=", "0.00097656");
  test_answer("1 + 1* 1/1=", "2");
  
  test_exception("+=", "Too few operands");
  test_exception("5++ 3 =", "Too few operands");
  test_exception("5 -+3 =", "Too few operands");
  test_exception("5 - --3 =", "Too few operands");
  test_exception("5 /0 =", "Division by zero");
  test_exception("2/(1-1)=", "Division by zero");
//  test_exception("1000000000000 =", "Integer Overflow");
//  test_exception("999999999999 + 1 =", "Integer Overflow");
//  test_exception("999999999999 * 2 =", "Integer Overflow");
  test_exception("5 5+ 3 =", "Too many operands");
  test_exception("5 + 3", "Missing =");
  test_exception("a=", "Unexpected Token 'a'");
  test_exception("5a=", "Unexpected Token '5a'");
  // test_exception("5/(a)=", "Unexpected Token '('");
  // test_exception("999999999999 * 999999999999=", "Integer Overflow");
  test_exception("(55=", "mismatched parens");
  test_exception("55)=", "mismatched parens");
  test_exception("(3-(2+((4*2.1)-1)=", "mismatched parens");
  test_exception("4.75--8.43*--1.01=", "Too few operands");
}