Ejemplo n.º 1
0
/* encode byte */
int bcm_encode_byte(bcm_encode_t *pkt, uint8 byte)
{
	assert(pkt != 0);

	if (!isLengthValid(pkt, 1))
		return 0;

	pkt->buf[pkt->length++] = byte;
	return 1;
}
bool SecureStorage_GetItem(const SecureStorageS *storage, const unsigned char *sKey, int16_t keyLen, unsigned char **sVal) {
  int16_t len = 0, saltLen = 0;
  bool ret = false;
  unsigned char *caEncKey = NULL, *caEncVal = NULL, *rKey = NULL, *caKey = NULL, *caVal = NULL;
  unsigned char cahKey[SHA256_LEN + UTILS_STR_LEN_SIZE + 1];
  htab *t = NULL;

  if (storage == NULL || sKey == NULL || sVal == NULL) {
    snprintf(errStr, sizeof(errStr), "SecureStorage_GetItem: Storage and key must not be NULL");
    return false;
  }
  *sVal = NULL;
  if (keyLen <= 0) {
    snprintf(errStr, sizeof(errStr), "SecureStorage_GetItem: key len %d must be positive\n", keyLen);
    return false;
  }
  t = storage->Data;
  if (READABLE_STORAGE == true)
    saltLen = 0;
  else if (Utils_GetCharArrayLen(storage->caSalt, &saltLen, KEY_VAL_MIN_STR_LEN, KEY_VAL_MAX_STR_LEN) == false)
    return false;
  if (generateAlignedCharAray(sKey, keyLen, storage->caSalt, saltLen, &caKey) == false) return false;
  if (isLengthValid(caKey, KEY_VAL_MIN_STR_LEN, KEY_VAL_MAX_STR_LEN - 1) == false ||
      Utils_GetCharArrayLen(caKey, &len, KEY_VAL_MIN_STR_LEN, KEY_VAL_MAX_STR_LEN) == false) {
    Utils_Free(caKey);
    return false;
  }
  if (getRandomFromKey(storage, caKey, cahKey, &rKey) == false) {
    Utils_Free(caKey);
    return false;
  }
  if (SECURE_DEBUG) {
    Utils_PrintHexStr(stderr, "Get hash key:", cahKey, SHA256_LEN + UTILS_STR_LEN_SIZE);
  }
  ret = encrypt(caKey, rKey, storage->caSecret, &caEncKey);
  Utils_Free(rKey);
  if (ret == false) {
    Utils_Free(caKey);
    return false;
  }
  ret = getValue(t, caEncKey, &caEncVal);
  Utils_Free(caEncKey);
  if (ret == false) {
    snprintf(errStr, sizeof(errStr), "Error: key '%s' was not found", caKey);
    Utils_Free(caKey);
    return false;
  }
  Utils_Free(caKey);
  ret = decrypt(caEncVal, storage->caSecret, &caVal);
  Utils_ConvertCharArrayToStr(caVal, sVal);
  debug_print("The val '%s'\n", *sVal);
  Utils_Free(caEncVal);
  Utils_Free(caVal);
  return ret;
}
Ejemplo n.º 3
0
/* decode byte */
int bcm_decode_byte(bcm_decode_t *pkt, uint8 *byte)
{
	assert(pkt != 0);
	assert(byte != 0);

	if (!isLengthValid(pkt, 1))
		return 0;

	*byte = pkt->buf[pkt->offset++];
	return 1;
}
Ejemplo n.º 4
0
/* encode 16-bit little endian */
int bcm_encode_le16(bcm_encode_t *pkt, uint16 value)
{
	assert(pkt != 0);

	if (!isLengthValid(pkt, 2))
		return 0;

	pkt->buf[pkt->length++] = value;
	pkt->buf[pkt->length++] = value >> 8;
	return 2;
}
Ejemplo n.º 5
0
/* decode bytes */
int bcm_decode_bytes(bcm_decode_t *pkt, int length, uint8 *bytes)
{
	assert(pkt != 0);
	assert(bytes != 0);

	if (!isLengthValid(pkt, length))
		return 0;

	memcpy(bytes, &pkt->buf[pkt->offset], length);
	pkt->offset += length;
	return length;
}
Ejemplo n.º 6
0
/* encode bytes */
int bcm_encode_bytes(bcm_encode_t *pkt, int length, uint8 *bytes)
{
	assert(pkt != 0);
	assert(bytes != 0);

	if (!isLengthValid(pkt, length))
		return 0;

	memcpy(&pkt->buf[pkt->length], bytes, length);
	pkt->length += length;
	return length;
}
Ejemplo n.º 7
0
/* encode 32-bit big endian */
int bcm_encode_be32(bcm_encode_t *pkt, uint32 value)
{
	assert(pkt != 0);

	if (!isLengthValid(pkt, 4))
		return 0;

	pkt->buf[pkt->length++] = value >> 24;
	pkt->buf[pkt->length++] = value >> 16;
	pkt->buf[pkt->length++] = value >> 8;
	pkt->buf[pkt->length++] = value;
	return 4;
}
Ejemplo n.º 8
0
/* decode 16-bit little endian */
int bcm_decode_le16(bcm_decode_t *pkt, uint16 *value)
{
	assert(pkt != 0);
	assert(value != 0);

	if (!isLengthValid(pkt, 2))
		return 0;

	*value =
		pkt->buf[pkt->offset] |
		pkt->buf[pkt->offset + 1] << 8;
	pkt->offset += 2;
	return 2;
}
Ejemplo n.º 9
0
/* decode 32-bit big endian */
int bcm_decode_be32(bcm_decode_t *pkt, uint32 *value)
{
	assert(pkt != 0);
	assert(value != 0);

	if (!isLengthValid(pkt, 4))
		return 0;

	*value =
		pkt->buf[pkt->offset] << 24 |
		pkt->buf[pkt->offset + 1] << 16 |
		pkt->buf[pkt->offset + 2] << 8 |
		pkt->buf[pkt->offset + 3];
	pkt->offset += 4;
	return 4;
}
bool SecureStorage_RemoveItem(const SecureStorageS *storage, const unsigned char *sKey, int16_t keyLen) {
  int16_t saltLen = 0;
  bool ret = false, ret1 = false;
  unsigned char *caEncKey = NULL, *rKey = NULL, *caKey = NULL;
  unsigned char cahKey[SHA256_LEN + UTILS_STR_LEN_SIZE + 1];

  if (storage == NULL || sKey == NULL) {
    snprintf(errStr, sizeof(errStr), "SecureStorage_RemoveItem: Storage and key must not be NULL");
    return false;
  }
  if (keyLen <= 0) {
    snprintf(errStr, sizeof(errStr), "SecureStorage_RemoveItem: key len %d must be positives\n", keyLen);
    return false;
  }
  if (READABLE_STORAGE == true)
    saltLen = 0;
  else if (Utils_GetCharArrayLen(storage->caSalt, &saltLen, KEY_VAL_MIN_STR_LEN, KEY_VAL_MAX_STR_LEN) == false)
    return false;
  if (generateAlignedCharAray(sKey, keyLen, storage->caSalt, saltLen, &caKey) == false) return false;
  if (isLengthValid(caKey, KEY_VAL_MIN_STR_LEN, KEY_VAL_MAX_STR_LEN - 1) == false || getRandomFromKey(storage, caKey, cahKey, &rKey) == false) {
    Utils_Free(caKey);
    return false;
  }
  ret1 = clearKey(storage, cahKey);
  if (ret1 == false) { // continue to try to remove the "real" key value
    snprintf(errStr, sizeof(errStr), "Error: key for random '%s' was not found", cahKey);
  }
  ret = encrypt(caKey, rKey, storage->caSecret, &caEncKey);
  Utils_Free(rKey);
  if (ret == false) {
    Utils_Free(caKey);
    return false;
  }
  if (clearKey(storage, caEncKey) == false) {
    Utils_Free(caEncKey);
    snprintf(errStr, sizeof(errStr), "Error: key '%s' was not found", caKey);
    return false;
  }
  Utils_Free(caKey);
  Utils_Free(caEncKey);
  return ret1;
}
bool SecureStorage_AddItem(const SecureStorageS *storage, const unsigned char *sKey, int16_t keyLen, const unsigned char *sVal, int16_t valLen) {
  int16_t len = 0, saltLen = 0;
  bool keyWasAlreadyStored = false;
  unsigned char *caEncKey = NULL, *caEncVal = NULL, *caKey = NULL, *caVal = NULL;
  unsigned char cahKey[SHA256_LEN + UTILS_STR_LEN_SIZE + 1], rKey[FULL_IV_LEN + 1], rKey1[FULL_IV_LEN + 1];
  unsigned char *tKey = NULL;

  if (storage == NULL || sKey == NULL || sVal == NULL) {
    snprintf(errStr, sizeof(errStr), "SecureStorage_AddItem: Storage, key and value must not be NULL");
    return false;
  }
  if (keyLen <= 0 || valLen <= 0) {
    snprintf(errStr, sizeof(errStr), "SecureStorage_AddItem: key len %d and val len %d must be positives\n", keyLen, valLen);
    return false;
  }
  if (READABLE_STORAGE == true)
    saltLen = 0;
  else if (Utils_GetCharArrayLen(storage->caSalt, &saltLen, KEY_VAL_MIN_STR_LEN, KEY_VAL_MAX_STR_LEN) == false)
    return false;
  if (isSecretValid(storage->caSecret) == false) return false;
  if (generateAlignedCharAray(sKey, keyLen, storage->caSalt, saltLen, &caKey) == false) return false;
  if (generateAlignedCharAray(sVal, valLen, NULL, 0, &caVal) == false) {
    Utils_Free(caKey);
    return false;
  }
  if (isLengthValid(caKey, KEY_VAL_MIN_STR_LEN, KEY_VAL_MAX_STR_LEN - 1) == false ||
      isLengthValid(caVal, KEY_VAL_MIN_STR_LEN, KEY_VAL_MAX_STR_LEN - 1) == false) {
    Utils_Free(caKey);
    Utils_Free(caVal);
    return false;
  }
  keyWasAlreadyStored = getRandomFromKey(storage, caKey, cahKey, &tKey);
  if (SECURE_DEBUG && keyWasAlreadyStored == true) {
    Utils_PrintHexStr(stderr, "random of key ", tKey, SHA256_LEN);
  }
  if (keyWasAlreadyStored) {
    memcpy(rKey, tKey, FULL_IV_LEN);
    rKey[FULL_IV_LEN] = 0;
    Utils_Free(tKey);
  } else {
    generateRandomToKey(caKey, cahKey, rKey);
    if (SECURE_DEBUG) {
      Utils_PrintHexStr(stderr, "Generate random of key ", rKey, SHA256_LEN);
    }
  }
  debug_print("Encrypt key '%s', val '%s', secret '%s'\n", caKey, caVal, storage->caSecret);
  if (SECURE_DEBUG) {
    Utils_PrintHexStr(stderr, "Use random of key ", rKey, SHA256_LEN);
  }
  if (encrypt(caKey, rKey, storage->caSecret, &caEncKey) == false) {
    Utils_Free(caKey);
    Utils_Free(caVal);
    return false;
  }
  Crypto_Random(rKey1 + UTILS_STR_LEN_SIZE, IV_LEN);
  Utils_SetCharArrayLen(rKey1, IV_LEN);
  if (encrypt(caVal, rKey1, storage->caSecret, &caEncVal) == false) {
    Utils_Free(caKey);
    Utils_Free(caVal);
    Utils_Free(caEncKey);
    return false;
  }
  debug_print("Add key: usedKey: %d, key '%s', val '%s'\n", keyWasAlreadyStored, caKey, caVal);
  Utils_Free(caKey);
  Utils_Free(caVal);
  if (Utils_GetCharArrayLen(caEncKey, &len, KEY_VAL_MIN_STR_LEN, KEY_VAL_MAX_STR_LEN) == false) return false;
  if (keyWasAlreadyStored != true) {
    updateData(storage, cahKey, rKey);
    if (SECURE_DEBUG) {
      Utils_PrintHexStr(stderr, "Add hash key:", cahKey, SHA256_LEN + UTILS_STR_LEN_SIZE);
      Utils_PrintHexStr(stderr, "And random:", rKey, FULL_IV_LEN);
    }
  }
  updateData(storage, caEncKey, caEncVal);
  Utils_Free(caEncKey);
  Utils_Free(caEncVal);
  return true;
}