void BitVector::read(const char *location) { _has_count = false; _count = 0; const char *follow = location; suif_assert((*follow == '0') && (follow[1] == 'x')); follow += 2; bool sign_extend = (*follow == '.'); while (*follow == '.') ++follow; const char *end = follow; while (*end != 0) ++end; suif_assert(end > follow); remove_reference(_block); size_t hex_digits_per_word = sizeof(BitVector::ChunkT) * 2; bool infinity_is_one = (sign_extend ? ((*follow > '7') || (*follow < '0')) : false); size_t word_count = ((end - follow) + hex_digits_per_word - 1) / hex_digits_per_word; _block = new BitVectorBlock(word_count, infinity_is_one, word_count); size_t word_num = word_count; if (((end - follow) % hex_digits_per_word) > 0) { BitVector::ChunkT high_word = (infinity_is_one ? ~0lu : 0lu); while (((end - follow) % hex_digits_per_word) > 0) { unsigned char new_half_byte = convert_from_hex(*follow); suif_assert(new_half_byte != 16); high_word = (high_word << 4) | new_half_byte; ++follow; } --word_num; _block->_data[word_num] = high_word; } while (follow < end) { BitVector::ChunkT this_word = 0; for (size_t digit_num = 0; digit_num < hex_digits_per_word; ++digit_num) { unsigned char new_half_byte = convert_from_hex(*follow); suif_assert(new_half_byte != 16); this_word = (this_word << 4) | new_half_byte; ++follow; } --word_num; _block->_data[word_num] = this_word; } assert(follow == end); assert(word_num == 0); _block->unpad(); }
void visit(std::string const& lhs, std::string const& rhs) { if (lhs == "QCAVSx") { pub_key_x = convert_from_hex(rhs.c_str()); } else if (lhs == "QCAVSy") { pub_key_y = convert_from_hex(rhs.c_str()); } else if (lhs == "dIUT") { priv_key = convert_from_hex(rhs.c_str()); } else if (lhs == "QIUTx") { pub_key_2_x = convert_from_hex(rhs.c_str()); } else if (lhs == "QIUTy") { pub_key_2_y = convert_from_hex(rhs.c_str()); } else if (lhs == "ZIUT") { shared_secret = convert_from_hex(rhs.c_str()); } if (shared_secret.size() > 0) { ecdh_test_case new_case; new_case.pub_key_x = pub_key_x; new_case.pub_key_y = pub_key_y; new_case.priv_key = priv_key; new_case.pub_key_2_x = pub_key_2_x; new_case.pub_key_2_y = pub_key_2_y; new_case.shared_secret = shared_secret; new_case.mode = mode; cases.push_back(new_case); shared_secret.clear(); pub_key_x.clear(); pub_key_y.clear(); priv_key.clear(); pub_key_2_x.clear(); pub_key_2_y.clear(); } }
static int l_decrypt(lua_State *L) { size_t keyLen, dataLen, outLen, hexDataLen; const char *key = luaL_checklstring(L, 1, &keyLen); const char *hexData = luaL_checklstring(L, 2, &hexDataLen); dataLen = hexDataLen/2; char data[dataLen]; convert_from_hex(hexData, data, dataLen); char decryptBuffer[dataLen]; int res = decrypt_str(decryptBuffer, data, dataLen, key, &outLen); if (res != 1) return luaL_error(L, "Error decrypting value %d, data:%s, dataLen: %d", res, data, dataLen); luaL_Buffer b; luaL_buffinit(L, &b); luaL_addlstring(&b, decryptBuffer, outLen); luaL_pushresult(&b); return 1; }
bool aes_test(char const *fn,bool bQuiet) { std::ifstream in_file(fn); char buf[500]; buf[499] = '\0'; bool bPass(true); bool bEncrypt(true), bKey(false), bPt(false), bCt(false); unsigned char key[64]; size_t key_len(0); unsigned char pt[64]; size_t pt_len(0); unsigned char ct[64]; size_t ct_len(0); unsigned idx; for (;;) { buf[0] = '\0'; in_file.getline(buf,sizeof(buf)-1,'\n'); if (strlen(buf) == 0) break; if (buf[0] == '#' || buf[0] == '\r' || buf[0] == '\n') continue; if (!memcmp(buf,"[ENCRYPT]",9)) { bEncrypt = true; } else if (!memcmp(buf,"COUNT = ",8)) { bPt = bCt = bKey = false; idx = strtoul(buf+8,NULL,10); } else if (!memcmp(buf,"KEY = ",6)) { key_len = convert_from_hex(key,buf+6); switch (key_len*8) { case 256: case 192: case 128: bKey = true; break; default: fprintf(stderr, "Got key of length %u\n", (unsigned)key_len); } } else if (!memcmp(buf,"PLAINTEXT = ",12)) { ct_len = pt_len = convert_from_hex(pt,buf+12); if (pt_len != 16) { fprintf(stderr, "Got plaintext of length %u\n", (unsigned)pt_len); } else { bPt = true; } } else if (!memcmp(buf,"CIPHERTEXT = ",13)) { ct_len = pt_len = convert_from_hex(ct,buf+13); if (ct_len != 16) { fprintf(stderr, "Got ciphertext of length %u\n", (unsigned)ct_len); } else { bCt = true; } } if (bKey && bPt && bCt) { if (!aes_enc_test( bQuiet, key_len, key, pt, ct)) { bPass = false; } } } return bPass; }
void visit(std::string const& lhs, std::string const& rhs) { if (lhs == "n") { n = convert_from_hex(rhs.c_str()); e.clear(); m.clear(); s.clear(); d.clear(); } else if (lhs == "e") { e = convert_from_hex(rhs.c_str()); } else if (lhs == "d") { d = convert_from_hex(rhs.c_str()); } else if (lhs == "Msg") { m = convert_from_hex(rhs.c_str()); } else if (lhs == "S") { s = convert_from_hex(rhs.c_str()); } else if (lhs == "SHAAlg") { if (rhs == "SHA1") { hash = MinTLS_SHA_160; } else if (rhs == "SHA256") { hash = MinTLS_SHA_256; } else if (rhs == "SHA224") { hash = MinTLS_SHA_224; } else if (rhs == "SHA384") { hash = MinTLS_SHA_384; } else if (rhs == "SHA512") { hash = MinTLS_SHA_512; } else { ASSERT_EQ(rhs, "rubbish"); } } else { ASSERT_EQ(lhs, "rubbish"); } if (n.size() > 0 && d.size() > 0 && e.size() > 0 && m.size() > 0 && s.size() > 0) { rsa_test_case case_; case_.n = n; case_.e = std::vector<uint8_t>(e.begin()+10,e.end()); case_.d = d; case_.m = m; case_.s = s; case_.hash = hash; cases.push_back(case_); m.clear(); s.clear(); } }