// Returns true if string is not correctly formatted bool not_formatted(unsigned char *str) { if (b64len(str) < E_KEY_SIZE) { err(ERR_FMT, "Number of Base64 encoded characters less than keysize"); return true; } return false; }
// Convert encoded string to dheluks data package // has_msg = false: package contains bare minimum (header + pubkey) // has_msg = true: package contains a message (header + pubkey + nonce + cphtxt) int str_to_pkg(unsigned char *str, dheluks_pkg_t *pkg, bool has_msg) { if (not_dheluks(str)) //if the string is not a dheluks string, return -1; //return error if (str[VSN_POSITION] == '0') { int codelen; //length of encoded data (not including header) size_t datalen; //length of decoded data (not including header) uint8_t *data; //holds decoded data codelen = b64len(str+HEADER_LEN); //codlen = length of b64 chars after header data = malloc(codelen); //it will be less than size of code datalen = str_to_uint8(str+HEADER_LEN, data, codelen); //convert string to data memcpy(pkg->pubkey, data, KEY_SIZE); //place key into package if (has_msg == true) { //if there supposed to be a message if (datalen - KEY_SIZE - NONCE_SIZE - DIGEST_SIZE < BLOCK_SIZE) { //but there actually isn't one, err(ERR_SZE, "Ciphertext not found"); return -1; //raise an error } pkg->csize = extrap_csize(datalen); //set cphtxt size pkg->cphtxt = malloc(pkg->csize); //allocate mem for cphtxt memcpy(pkg->nonce, data + KEY_SIZE, NONCE_SIZE); //place nonce memcpy(pkg->digest, data + KEY_SIZE + NONCE_SIZE, DIGEST_SIZE); memcpy(pkg->cphtxt, data + KEY_SIZE + NONCE_SIZE + DIGEST_SIZE, pkg->csize); //place cphtxt if (not_block(pkg->csize)) //if cphtxt not correct block size, return -1; //return error } return 0; //there were no errors } else //we were expecting version 0 return -1; }
static uint32_t enclen(uint32_t outlen, uint32_t saltlen, uint32_t t_cost, uint32_t m_cost, uint32_t lanes) { return strlen("$argon2x$m=,t=,p=$$") + numlen(t_cost) + numlen(m_cost) + numlen(lanes) + b64len(saltlen) + b64len(outlen); }