int srtp_cipher_get_key_length(const srtp_cipher_t *c)
{ return c->key_len;
}
/* *Atrivialplatformindependentrandomsource. *Foruseintestonly.
*/ void srtp_cipher_rand_for_tests(void *dest, uint32_t len)
{ /* Generic C-library (rand()) version */ /* This is a random source of last resort */
uint8_t *dst = (uint8_t *)dest; while (len) { int val = rand(); /* rand() returns 0-32767 (ugh) */ /* Is this a good enough way to get random bytes?
It is if it passes FIPS-140... */
*dst++ = val & 0xff;
len--;
}
}
/* set the initialization vector */
status = srtp_cipher_set_iv(c, (uint8_t *)test_case->idx,
srtp_direction_encrypt); if (status) {
srtp_cipher_dealloc(c); return status;
}
/* compare the resulting ciphertext with that in the test case */ if (len != test_case->ciphertext_length_octets) {
srtp_cipher_dealloc(c); return srtp_err_status_algo_fail;
}
status = srtp_err_status_ok; for (k = 0; k < test_case->ciphertext_length_octets; k++) { if (buffer[k] != test_case->ciphertext[k]) {
status = srtp_err_status_algo_fail;
debug_print(srtp_mod_cipher, "test case %d failed", case_num);
debug_print(srtp_mod_cipher, "(failure at byte %u)", k); break;
}
} if (status) {
debug_print(srtp_mod_cipher, "c computed: %s",
srtp_octet_string_hex_string(
buffer, 2 * test_case->plaintext_length_octets));
debug_print(srtp_mod_cipher, "c expected: %s",
srtp_octet_string_hex_string(
test_case->ciphertext, 2 * test_case->plaintext_length_octets));
/* set the initialization vector */
status = srtp_cipher_set_iv(c, (uint8_t *)test_case->idx,
srtp_direction_decrypt); if (status) {
srtp_cipher_dealloc(c); return status;
}
/* compare the resulting plaintext with that in the test case */ if (len != test_case->plaintext_length_octets) {
srtp_cipher_dealloc(c); return srtp_err_status_algo_fail;
}
status = srtp_err_status_ok; for (k = 0; k < test_case->plaintext_length_octets; k++) { if (buffer[k] != test_case->plaintext[k]) {
status = srtp_err_status_algo_fail;
debug_print(srtp_mod_cipher, "test case %d failed", case_num);
debug_print(srtp_mod_cipher, "(failure at byte %u)", k);
}
} if (status) {
debug_print(srtp_mod_cipher, "p computed: %s",
srtp_octet_string_hex_string(
buffer, 2 * test_case->plaintext_length_octets));
debug_print(srtp_mod_cipher, "p expected: %s",
srtp_octet_string_hex_string(
test_case->plaintext, 2 * test_case->plaintext_length_octets));
/* allocate cipher, using paramaters from the first test case */
test_case = test_data;
status = srtp_cipher_type_alloc(ct, &c, test_case->key_length_octets,
test_case->tag_length_octets); if (status) { return status;
}
/* choose a length at random (leaving room for IV and padding) */
length = srtp_cipher_rand_u32_for_tests() % (SELF_TEST_BUF_OCTETS - 64);
debug_print(srtp_mod_cipher, "random plaintext length %d\n", length);
srtp_cipher_rand_for_tests(buffer, length);
/* copy plaintext into second buffer */ for (i = 0; (unsignedint)i < length; i++) {
buffer2[i] = buffer[i];
}
/* choose a key at random */ if (test_case->key_length_octets > MAX_KEY_LEN) {
srtp_cipher_dealloc(c); return srtp_err_status_cant_check;
}
srtp_cipher_rand_for_tests(key, test_case->key_length_octets);
/* chose a random initialization vector */
srtp_cipher_rand_for_tests(iv, MAX_KEY_LEN);
/* initialize cipher */
status = srtp_cipher_init(c, key); if (status) {
srtp_cipher_dealloc(c); return status;
}
/* set initialization vector */
status = srtp_cipher_set_iv(c, (uint8_t *)test_case->idx,
srtp_direction_encrypt); if (status) {
srtp_cipher_dealloc(c); return status;
}
/* compare the resulting plaintext with the original one */ if (length != plaintext_len) {
srtp_cipher_dealloc(c); return srtp_err_status_algo_fail;
}
status = srtp_err_status_ok; for (k = 0; k < plaintext_len; k++) { if (buffer[k] != buffer2[k]) {
status = srtp_err_status_algo_fail;
debug_print(srtp_mod_cipher, "random test case %d failed",
case_num);
debug_print(srtp_mod_cipher, "(failure at byte %u)", k);
}
} if (status) {
srtp_cipher_dealloc(c); return srtp_err_status_algo_fail;
}
}
status = srtp_cipher_dealloc(c); if (status) { return status;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.