Esempio n. 1
0
/**
 * \brief Parse a led command [Level 2]
 * \param tokens Command tokens.
 * \param cmd Cmd structure.
 * \return The error result.
 */
static TuxDrvError
parse_tux_led_command(tokens_t tokens, delay_cmd_t *cmd)
{
    TuxDrvError ret =  E_TUXDRV_INVALIDCOMMAND;
    if (strcmp(tokens[2], "BLINK") == 0)
    {
        cmd->sub_command = BLINK;
        if (str_to_leds_t(tokens[3], &cmd->led_blink_parameters.leds) &&
            str_to_uint8(tokens[4], &cmd->led_blink_parameters.pulse_count) &&
            str_to_float(tokens[5], &cmd->led_blink_parameters.pulse_period))
        {
            ret = E_TUXDRV_NOERROR;
        }
    }
    else if (strcmp(tokens[2], "OFF") == 0)
    {
        cmd->sub_command = OFF;
        if (str_to_leds_t(tokens[3], &cmd->led_off_parameters.leds))
        {
            ret = E_TUXDRV_NOERROR;
        }
    }
    else if (strcmp(tokens[2], "ON") == 0)
    {
        cmd->sub_command = ON;
        if (str_to_leds_t(tokens[3], &cmd->led_on_parameters.leds) &&
            str_to_float(tokens[4], &cmd->led_on_parameters.intensity))
        {
            ret = E_TUXDRV_NOERROR;
        }
    }
    else if (strcmp(tokens[2], "PULSE") == 0)
    {
        cmd->sub_command = PULSE;
        if (str_to_leds_t(tokens[3], &cmd->led_pulse_parameters.leds) &&
            str_to_float(tokens[4], &cmd->led_pulse_parameters.min_intensity) &&
            str_to_float(tokens[5], &cmd->led_pulse_parameters.max_intensity) &&
            str_to_uint8(tokens[6], &cmd->led_pulse_parameters.pulse_count) &&
            str_to_float(tokens[7], &cmd->led_pulse_parameters.pulse_period) &&
            str_to_effect_type(tokens[8], &cmd->led_pulse_parameters.effect_type) &&
            str_to_float(tokens[9], &cmd->led_pulse_parameters.effect_speed) &&
            str_to_uint8(tokens[10], &cmd->led_pulse_parameters.effect_step))
        {
            ret = E_TUXDRV_NOERROR;
        }
    }
    else if (strcmp(tokens[2], "SET") == 0)
    {
        cmd->sub_command = SET;
        if (str_to_leds_t(tokens[3], &cmd->led_set_parameters.leds) &&
            str_to_float(tokens[4], &cmd->led_set_parameters.intensity) &&
            str_to_effect_type(tokens[5], &cmd->led_set_parameters.effect_type) &&
            str_to_float(tokens[6], &cmd->led_set_parameters.effect_speed) &&
            str_to_uint8(tokens[7], &cmd->led_set_parameters.effect_step))
        {
            ret = E_TUXDRV_NOERROR;
        }
    }
    return ret;
}
Esempio n. 2
0
/**
 * \brief Parse an IR command [Level 2]
 * \param tokens Command tokens.
 * \param cmd Cmd structure.
 * \return The error result.
 */
static TuxDrvError
parse_tux_ir_command(tokens_t tokens, delay_cmd_t *cmd)
{
    TuxDrvError ret =  E_TUXDRV_INVALIDCOMMAND;

    if (strcmp(tokens[2], "ON") == 0)
    {
        cmd->sub_command = ON;
        ret = E_TUXDRV_NOERROR;
    }
    else if (strcmp(tokens[2], "OFF") == 0)
    {
        cmd->sub_command = OFF;
        ret = E_TUXDRV_NOERROR;
    }
    else if (strcmp(tokens[2], "SEND") == 0)
    {
        cmd->sub_command = SEND;
        if (str_to_uint8(tokens[3], &cmd->ir_send_parameters.address) &&
            str_to_uint8(tokens[4], &cmd->ir_send_parameters.command))
        {
            ret = E_TUXDRV_NOERROR;
        }
    }

    return ret;
}
Esempio n. 3
0
/**
 * \brief Parse a spinning command [Level 2]
 * \param tokens Command tokens.
 * \param cmd Cmd structure.
 * \return The error result.
 */
static TuxDrvError
parse_tux_spinning_command(tokens_t tokens, delay_cmd_t *cmd)
{
    TuxDrvError ret =  E_TUXDRV_INVALIDCOMMAND;

    if (strcmp(tokens[2], "LEFT_ON") == 0)
    {
        cmd->sub_command = LEFT_ON;
        if (str_to_uint8(tokens[3], &cmd->spinning_on_parameters.nr_qturns))
        {
            ret = E_TUXDRV_NOERROR;
        }
    }
    else if (strcmp(tokens[2], "LEFT_ON_DURING") == 0)
    {
        cmd->sub_command = LEFT_ON_DURING;
        if (str_to_float(tokens[3], &cmd->spinning_on_during_parameters.duration))
        {
            ret = E_TUXDRV_NOERROR;
        }
    }
    else if (strcmp(tokens[2], "RIGHT_ON") == 0)
    {
        cmd->sub_command = RIGHT_ON;
        if (str_to_uint8(tokens[3], &cmd->spinning_on_parameters.nr_qturns))
        {
            ret = E_TUXDRV_NOERROR;
        }
    }
    else if (strcmp(tokens[2], "RIGHT_ON_DURING") == 0)
    {
        cmd->sub_command = RIGHT_ON_DURING;
        if (str_to_float(tokens[3], &cmd->spinning_on_during_parameters.duration))
        {
            ret = E_TUXDRV_NOERROR;
        }
    }
    else if (strcmp(tokens[2], "OFF") == 0)
    {
        cmd->sub_command = OFF;
        ret = E_TUXDRV_NOERROR;
    }
    else if (strcmp(tokens[2], "SPEED") == 0)
    {
        cmd->sub_command = SPEED;
        if (str_to_uint8(tokens[3], &cmd->spinning_speed_parameters.speed))
        {
            ret = E_TUXDRV_NOERROR;
        }
    }
    return ret;
}
Esempio n. 4
0
/**
 * \brief Parse a flippers command [Level 2]
 * \param tokens Command tokens.
 * \param cmd Cmd structure.
 * \return The error result.
 */
static TuxDrvError
parse_tux_flippers_command(tokens_t tokens, delay_cmd_t *cmd)
{
    TuxDrvError ret =  E_TUXDRV_INVALIDCOMMAND;

    if (strcmp(tokens[2], "DOWN") == 0)
    {
        cmd->sub_command = DOWN;
        ret = E_TUXDRV_NOERROR;
    }
    else if (strcmp(tokens[2], "OFF") == 0)
    {
        cmd->sub_command = OFF;
        ret = E_TUXDRV_NOERROR;
    }
    else if (strcmp(tokens[2], "ON") == 0)
    {
        cmd->sub_command = ON;
        if (str_to_uint8(tokens[3], &cmd->flippers_on_parameters.nr_movements) &&
            str_to_state_t(tokens[4], &cmd->flippers_on_parameters.state))
        {
            ret = E_TUXDRV_NOERROR;
        }
    }
    else if (strcmp(tokens[2], "ON_DURING") == 0)
    {
        cmd->sub_command = ON_DURING;
        if (str_to_float(tokens[3], &cmd->flippers_on_during_parameters.duration) &&
            str_to_state_t(tokens[4], &cmd->flippers_on_during_parameters.state))
        {
            ret = E_TUXDRV_NOERROR;
        }
        cmd->sub_command = ON_DURING;
        ret = E_TUXDRV_NOERROR;
    }
    else if (strcmp(tokens[2], "UP") == 0)
    {
        cmd->sub_command = UP;
        ret = E_TUXDRV_NOERROR;
    }
    else if (strcmp(tokens[2], "SPEED") == 0)
    {
        cmd->sub_command = SPEED;
        if (str_to_uint8(tokens[3], &cmd->flippers_speed_parameters.speed))
        {
            ret = E_TUXDRV_NOERROR;
        }
    }
    return ret;
}
Esempio n. 5
0
uint8_t gen_key_pair(char *seed, NtruEncParams *params, NtruEncKeyPair *kp) {
    uint16_t seed_len = strlen(seed);
    uint8_t seed_uint8[seed_len];
    str_to_uint8(seed, seed_uint8);
    NtruRandContext rand_ctx;
    NtruRandGen rng = NTRU_RNG_IGF2;
    ntru_rand_init_det(&rand_ctx, &rng, seed_uint8, seed_len);
    rand_ctx.seed = seed_uint8;
    rand_ctx.seed_len = seed_len;
    uint8_t result = 1;
    result &= ntru_gen_key_pair(params, kp, &rand_ctx);
    result &= ntru_rand_release(&rand_ctx);
    return result;
}
Esempio n. 6
0
uint8_t test_keygen() {
    NtruEncParams param_arr[] = ALL_PARAM_SETS;
    uint8_t valid = 1;

    uint8_t i;
    NtruRandGen rng = NTRU_RNG_DEFAULT;
    for (i=0; i<sizeof(param_arr)/sizeof(param_arr[0]); i++) {
        NtruEncParams params = param_arr[i];
        NtruEncKeyPair kp;
        NtruRandContext rand_ctx;
        ntru_rand_init(&rand_ctx, &rng);
        valid &= ntru_gen_key_pair(&params, &kp, &rand_ctx) == NTRU_SUCCESS;

        /* encrypt a random message */
        NtruTernPoly m;
        ntru_rand_tern(params.N, params.N/3, params.N/3, &m, &rand_ctx);
        NtruIntPoly m_int;
        ntru_tern_to_int(&m, &m_int);

        NtruTernPoly r;
        ntru_rand_tern(params.N, params.N/3, params.N/3, &r, &rand_ctx);
        ntru_rand_release(&rand_ctx);
        NtruIntPoly e;
        encrypt_poly(&m_int, &r, &kp.pub.h, &e, params.q);

        /* decrypt and verify */
        NtruIntPoly c;
        decrypt_poly(&e, &kp.priv, &c, params.q);
        valid &= ntru_equals_int(&m_int, &c);

        /* test deterministic key generation */
        valid &= gen_key_pair("my test password", &params, &kp) == NTRU_SUCCESS;
        char seed2_char[19];
        strcpy(seed2_char, "my test password");
        uint8_t seed2[strlen(seed2_char)];
        str_to_uint8(seed2_char, seed2);
        NtruEncKeyPair kp2;
        NtruRandGen rng = NTRU_RNG_IGF2;
        NtruRandContext rand_ctx2;
        ntru_rand_init_det(&rand_ctx2, &rng, seed2, strlen(seed2_char));
        valid &= ntru_gen_key_pair(&params, &kp2, &rand_ctx2) == NTRU_SUCCESS;
        ntru_rand_release(&rand_ctx2);
        valid &= equals_key_pair(&kp, &kp2);
    }

    print_result("test_keygen", valid);
    return valid;
}
Esempio n. 7
0
/**
 * \brief Parse a sound flash command [Level 2]
 * \param tokens Command tokens.
 * \param cmd Cmd structure.
 * \return The error result.
 */
static TuxDrvError
parse_tux_sound_flash_command(tokens_t tokens, delay_cmd_t *cmd)
{
    TuxDrvError ret =  E_TUXDRV_INVALIDCOMMAND;

    if (strcmp(tokens[2], "PLAY") == 0)
    {
        cmd->sub_command = PLAY;
        if (str_to_uint8(tokens[3], &cmd->sound_flash_play_parameters.track) &&
            str_to_float(tokens[4], &cmd->sound_flash_play_parameters.volume))
        {
            ret = E_TUXDRV_NOERROR;
        }
    }
    return ret;
}
Esempio n. 8
0
int parse_uint8(uint8_t *x, char *payload)
{
	int errcode;

	if (bug_on(!x))
		return -err_internal;

	payload = strtok(payload, " ,");
	if (!payload)
		return -err_parse_no_args;

	errcode = str_to_uint8(payload, x, 0);
	if (errcode < 0)
		return errcode;

	return 0;
}
Esempio n. 9
0
// 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;

}
Esempio n. 10
0
/**
 * \brief Parse a mouth command [Level 2]
 * \param tokens Command tokens.
 * \param cmd Cmd structure.
 * \return The error result.
 */
static TuxDrvError
parse_tux_mouth_command(tokens_t tokens, delay_cmd_t *cmd)
{
    TuxDrvError ret =  E_TUXDRV_INVALIDCOMMAND;

    if (strcmp(tokens[2], "CLOSE") == 0)
    {
        cmd->sub_command = CLOSE;
        ret = E_TUXDRV_NOERROR;
    }
    else if (strcmp(tokens[2], "OFF") == 0)
    {
        cmd->sub_command = OFF;
        ret = E_TUXDRV_NOERROR;
    }
    else if (strcmp(tokens[2], "ON") == 0)
    {
        cmd->sub_command = ON;
        if (str_to_uint8(tokens[3], &cmd->mouth_on_parameters.nr_movements) &&
            str_to_state_t(tokens[4], &cmd->mouth_on_parameters.state))
        {
            ret = E_TUXDRV_NOERROR;
        }
    }
    else if (strcmp(tokens[2], "ON_DURING") == 0)
    {
        cmd->sub_command = ON_DURING;
        if (str_to_float(tokens[3], &cmd->mouth_on_during_parameters.duration) &&
            str_to_state_t(tokens[4], &cmd->mouth_on_during_parameters.state))
        {
            ret = E_TUXDRV_NOERROR;
        }
    }
    else if (strcmp(tokens[2], "OPEN") == 0)
    {
        cmd->sub_command = OPEN;
        ret = E_TUXDRV_NOERROR;
    }
    return ret;
}
Esempio n. 11
0
/* tests ntru_encrypt() with a deterministic RNG */
uint8_t test_encr_decr_det(NtruEncParams *params, uint8_t *digest_expected) {
    NtruEncKeyPair kp;
    uint8_t valid = gen_key_pair("seed value for key generation", params, &kp) == NTRU_SUCCESS;
    uint8_t pub_arr[ntru_pub_len(params)];
    ntru_export_pub(&kp.pub, pub_arr);
    NtruEncPubKey pub2;
    ntru_import_pub(pub_arr, &pub2);
    valid &= ntru_equals_int(&kp.pub.h, &pub2.h);

    NtruRandContext rand_ctx_plaintext;
    uint16_t max_len = ntru_max_msg_len(params);
    uint8_t plain[max_len];
    NtruRandGen rng_plaintext = NTRU_RNG_IGF2;
    char plain_seed_char[25];
    strcpy(plain_seed_char, "seed value for plaintext");
    uint8_t plain_seed[25];
    str_to_uint8(plain_seed_char, plain_seed);
    valid &= ntru_rand_init_det(&rand_ctx_plaintext, &rng_plaintext, plain_seed, strlen(plain_seed_char)) == NTRU_SUCCESS;
    valid &= ntru_rand_generate(plain, max_len, &rand_ctx_plaintext) == NTRU_SUCCESS;
    valid &= ntru_rand_release(&rand_ctx_plaintext) == NTRU_SUCCESS;
    uint8_t plain2[max_len];
    memcpy(plain2, plain, max_len);
    uint16_t enc_len = ntru_enc_len(params);
    uint8_t encrypted[enc_len];
    uint8_t encrypted2[enc_len];

    char seed_char[11];
    strcpy(seed_char, "seed value");
    uint8_t seed[11];
    str_to_uint8(seed_char, seed);
    char seed2_char[11];
    strcpy(seed2_char, "seed value");
    uint8_t seed2[11];
    str_to_uint8(seed2_char, seed2);

    NtruRandContext rand_ctx;
    NtruRandGen rng = NTRU_RNG_IGF2;
    valid &= ntru_rand_init_det(&rand_ctx, &rng, seed, strlen(seed_char)) == NTRU_SUCCESS;
    NtruRandContext rand_ctx2;
    NtruRandGen rng2 = NTRU_RNG_IGF2;
    valid &= ntru_rand_init_det(&rand_ctx2, &rng2, seed2, strlen(seed2_char)) == NTRU_SUCCESS;

    uint8_t decrypted[max_len];
    uint16_t plain_len;
    uint16_t dec_len;
    for (plain_len=0; plain_len<=max_len; plain_len++) {
        valid &= ntru_encrypt((uint8_t*)&plain, plain_len, &kp.pub, params, &rand_ctx, (uint8_t*)&encrypted) == NTRU_SUCCESS;
        valid &= ntru_encrypt((uint8_t*)&plain2, plain_len, &pub2, params, &rand_ctx2, (uint8_t*)&encrypted2) == NTRU_SUCCESS;
        valid &= memcmp(encrypted, encrypted2, enc_len) == 0;

        valid &= ntru_decrypt((uint8_t*)&encrypted, &kp, params, (uint8_t*)&decrypted, &dec_len) == NTRU_SUCCESS;
        valid &= equals_arr((uint8_t*)&plain, (uint8_t*)&decrypted, plain_len);
    }

    uint8_t digest[20];
    ntru_sha1(encrypted, enc_len, digest);
    valid &= memcmp(digest, digest_expected, 20) == 0;

    valid &= ntru_rand_release(&rand_ctx) == NTRU_SUCCESS;
    valid &= ntru_rand_release(&rand_ctx2) == NTRU_SUCCESS;

    return valid;
}