Finish refactoring of cipher.h API.
diff --git a/crypto/cipher/aes_gcm_ossl.c b/crypto/cipher/aes_gcm_ossl.c
index 08faba4..c4e951d 100644
--- a/crypto/cipher/aes_gcm_ossl.c
+++ b/crypto/cipher/aes_gcm_ossl.c
@@ -243,7 +243,7 @@
* aad Additional data to process for AEAD cipher suites
* aad_len length of aad buffer
*/
-static srtp_err_status_t srtp_aes_gcm_openssl_set_aad (srtp_aes_gcm_ctx_t *c, unsigned char *aad, unsigned int aad_len)
+static srtp_err_status_t srtp_aes_gcm_openssl_set_aad (srtp_aes_gcm_ctx_t *c, uint8_t *aad, uint32_t aad_len)
{
int rv;
@@ -294,7 +294,7 @@
* buf data to encrypt
* len length of encrypt buffer
*/
-static srtp_err_status_t srtp_aes_gcm_openssl_get_tag (srtp_aes_gcm_ctx_t *c, unsigned char *buf, int *len)
+static srtp_err_status_t srtp_aes_gcm_openssl_get_tag (srtp_aes_gcm_ctx_t *c, uint8_t *buf, uint32_t *len)
{
/*
* Calculate the tag
diff --git a/crypto/cipher/cipher.c b/crypto/cipher/cipher.c
index bcf2ed3..72f2614 100644
--- a/crypto/cipher/cipher.c
+++ b/crypto/cipher/cipher.c
@@ -104,14 +104,46 @@
srtp_err_status_t srtp_cipher_encrypt (srtp_cipher_t *c, uint8_t *buffer, uint32_t *num_octets_to_output)
{
+ if (!c || !c->type || !c->state) {
+ return (srtp_err_status_bad_param);
+ }
+
return (((c)->type)->encrypt(((c)->state), buffer, num_octets_to_output));
}
srtp_err_status_t srtp_cipher_decrypt (srtp_cipher_t *c, uint8_t *buffer, uint32_t *num_octets_to_output)
{
+ if (!c || !c->type || !c->state) {
+ return (srtp_err_status_bad_param);
+ }
+
return (((c)->type)->decrypt(((c)->state), buffer, num_octets_to_output));
}
+srtp_err_status_t srtp_cipher_get_tag (srtp_cipher_t *c, uint8_t *buffer, uint32_t *tag_len)
+{
+ if (!c || !c->type || !c->state) {
+ return (srtp_err_status_bad_param);
+ }
+ if (!((c)->type)->get_tag) {
+ return (srtp_err_status_no_such_op);
+ }
+
+ return (((c)->type)->get_tag(((c)->state), buffer, tag_len));
+}
+
+srtp_err_status_t srtp_cipher_set_aad (srtp_cipher_t *c, uint8_t *aad, uint32_t aad_len)
+{
+ if (!c || !c->type || !c->state) {
+ return (srtp_err_status_bad_param);
+ }
+ if (!((c)->type)->set_aad) {
+ return (srtp_err_status_no_such_op);
+ }
+
+ return (((c)->type)->set_aad(((c)->state), aad, aad_len));
+}
+
/* some bookkeeping functions */
int srtp_cipher_get_key_length (const srtp_cipher_t *c)
@@ -134,7 +166,7 @@
srtp_err_status_t status;
uint8_t buffer[SELF_TEST_BUF_OCTETS];
uint8_t buffer2[SELF_TEST_BUF_OCTETS];
- int tag_len;
+ uint32_t tag_len;
unsigned int len;
int i, j, case_num = 0;
@@ -199,8 +231,7 @@
/*
* Set the AAD
*/
- status = cipher_set_aad(c, test_case->aad,
- test_case->aad_length_octets);
+ status = srtp_cipher_set_aad(c, test_case->aad, test_case->aad_length_octets);
if (status) {
srtp_cipher_dealloc(c);
return status;
@@ -222,7 +253,7 @@
/*
* Get the GCM tag
*/
- status = cipher_get_tag(c, buffer + len, &tag_len);
+ status = srtp_cipher_get_tag(c, buffer + len, &tag_len);
if (status) {
srtp_cipher_dealloc(c);
return status;
@@ -296,8 +327,7 @@
/*
* Set the AAD
*/
- status = cipher_set_aad(c, test_case->aad,
- test_case->aad_length_octets);
+ status = srtp_cipher_set_aad(c, test_case->aad, test_case->aad_length_octets);
if (status) {
srtp_cipher_dealloc(c);
return status;
@@ -424,8 +454,7 @@
/*
* Set the AAD
*/
- status = cipher_set_aad(c, test_case->aad,
- test_case->aad_length_octets);
+ status = srtp_cipher_set_aad(c, test_case->aad, test_case->aad_length_octets);
if (status) {
srtp_cipher_dealloc(c);
return status;
@@ -446,7 +475,7 @@
/*
* Get the GCM tag
*/
- status = cipher_get_tag(c, buffer + length, &tag_len);
+ status = srtp_cipher_get_tag(c, buffer + length, &tag_len);
if (status) {
srtp_cipher_dealloc(c);
return status;
@@ -474,8 +503,7 @@
/*
* Set the AAD
*/
- status = cipher_set_aad(c, test_case->aad,
- test_case->aad_length_octets);
+ status = srtp_cipher_set_aad(c, test_case->aad, test_case->aad_length_octets);
if (status) {
srtp_cipher_dealloc(c);
return status;
diff --git a/crypto/include/cipher.h b/crypto/include/cipher.h
index b14650a..64046c2 100644
--- a/crypto/include/cipher.h
+++ b/crypto/include/cipher.h
@@ -95,7 +95,7 @@
* a cipher_set_aad_func_t processes the AAD data for AEAD ciphers
*/
typedef srtp_err_status_t (*cipher_set_aad_func_t)
- (void *state, uint8_t *aad, unsigned int aad_len);
+ (void *state, uint8_t *aad, uint32_t aad_len);
/* a cipher_encrypt_func_t encrypts data in-place */
@@ -117,7 +117,7 @@
* tag that was calculated by an AEAD cipher.
*/
typedef srtp_err_status_t (*cipher_get_tag_func_t)
- (void *state, void *tag, int *len);
+ (void *state, uint8_t *tag, uint32_t *len);
/*
@@ -168,15 +168,6 @@
int algorithm;
} srtp_cipher_t;
-#define cipher_get_tag(c, buf, len) \
- (((c)->type)->get_tag(((c)->state), (buf), (len)))
-
-#define cipher_set_aad(c, a, l) \
- (((c) && (((c)->type)->set_aad)) ? \
- (((c)->type)->set_aad(((c)->state), (a), (l))) : \
- srtp_err_status_no_such_op)
-
-
/* some bookkeeping functions */
int srtp_cipher_get_key_length(const srtp_cipher_t *c);
@@ -216,5 +207,7 @@
srtp_err_status_t srtp_cipher_output(srtp_cipher_t *c, uint8_t *buffer, uint32_t *num_octets_to_output);
srtp_err_status_t srtp_cipher_encrypt(srtp_cipher_t *c, uint8_t *buffer, uint32_t *num_octets_to_output);
srtp_err_status_t srtp_cipher_decrypt(srtp_cipher_t *c, uint8_t *buffer, uint32_t *num_octets_to_output);
+srtp_err_status_t srtp_cipher_get_tag(srtp_cipher_t *c, uint8_t *buffer, uint32_t *tag_len);
+srtp_err_status_t srtp_cipher_set_aad(srtp_cipher_t *c, uint8_t *aad, uint32_t aad_len);
#endif /* CIPHER_H */
diff --git a/srtp/srtp.c b/srtp/srtp.c
index d483708..85a3496 100644
--- a/srtp/srtp.c
+++ b/srtp/srtp.c
@@ -887,7 +887,7 @@
srtp_xtd_seq_num_t est; /* estimated xtd_seq_num_t of *hdr */
int delta; /* delta of local pkt idx and that in hdr */
srtp_err_status_t status;
- int tag_len;
+ uint32_t tag_len;
v128_t iv;
unsigned int aad_len;
@@ -972,7 +972,7 @@
* Set the AAD over the RTP header
*/
aad_len = (uint8_t *)enc_start - (uint8_t *)hdr;
- status = cipher_set_aad(stream->rtp_cipher, (uint8_t*)hdr, aad_len);
+ status = srtp_cipher_set_aad(stream->rtp_cipher, (uint8_t*)hdr, aad_len);
if (status) {
return ( srtp_err_status_cipher_fail);
}
@@ -987,7 +987,7 @@
* If we're doing GCM, we need to get the tag
* and append that to the output
*/
- status = cipher_get_tag(stream->rtp_cipher,
+ status = srtp_cipher_get_tag(stream->rtp_cipher,
(uint8_t*)enc_start+enc_octet_len, &tag_len);
if (status) {
return ( srtp_err_status_cipher_fail);
@@ -1090,7 +1090,7 @@
* Set the AAD for AES-GCM, which is the RTP header
*/
aad_len = (uint8_t *)enc_start - (uint8_t *)hdr;
- status = cipher_set_aad(stream->rtp_cipher, (uint8_t*)hdr, aad_len);
+ status = srtp_cipher_set_aad(stream->rtp_cipher, (uint8_t*)hdr, aad_len);
if (status) {
return ( srtp_err_status_cipher_fail);
}
@@ -2271,7 +2271,7 @@
unsigned int enc_octet_len = 0; /* number of octets in encrypted portion */
uint8_t *auth_tag = NULL; /* location of auth_tag within packet */
srtp_err_status_t status;
- int tag_len;
+ uint32_t tag_len;
uint32_t seq_num;
v128_t iv;
uint32_t tseq;
@@ -2338,8 +2338,7 @@
* If payload encryption is enabled, then the AAD consist of
* the RTCP header and the seq# at the end of the packet
*/
- status = cipher_set_aad(stream->rtcp_cipher, (uint8_t*)hdr,
- octets_in_rtcp_header);
+ status = srtp_cipher_set_aad(stream->rtcp_cipher, (uint8_t*)hdr, octets_in_rtcp_header);
if (status) {
return ( srtp_err_status_cipher_fail);
}
@@ -2349,8 +2348,7 @@
* the entire packet as described in section 10.3 in revision 07
* of the draft.
*/
- status = cipher_set_aad(stream->rtcp_cipher, (uint8_t*)hdr,
- *pkt_octet_len);
+ status = srtp_cipher_set_aad(stream->rtcp_cipher, (uint8_t*)hdr, *pkt_octet_len);
if (status) {
return ( srtp_err_status_cipher_fail);
}
@@ -2359,8 +2357,7 @@
* put the idx# into network byte order and process it as AAD
*/
tseq = htonl(*trailer);
- status = cipher_set_aad(stream->rtcp_cipher, (uint8_t*)&tseq,
- sizeof(srtcp_trailer_t));
+ status = srtp_cipher_set_aad(stream->rtcp_cipher, (uint8_t*)&tseq, sizeof(srtcp_trailer_t));
if (status) {
return ( srtp_err_status_cipher_fail);
}
@@ -2375,8 +2372,7 @@
/*
* Get the tag and append that to the output
*/
- status = cipher_get_tag(stream->rtcp_cipher, (uint8_t*)auth_tag,
- &tag_len);
+ status = srtp_cipher_get_tag(stream->rtcp_cipher, (uint8_t*)auth_tag, &tag_len);
if (status) {
return ( srtp_err_status_cipher_fail);
}
@@ -2394,8 +2390,7 @@
/*
* Get the tag and append that to the output
*/
- status = cipher_get_tag(stream->rtcp_cipher, (uint8_t*)auth_tag,
- &tag_len);
+ status = srtp_cipher_get_tag(stream->rtcp_cipher, (uint8_t*)auth_tag, &tag_len);
if (status) {
return ( srtp_err_status_cipher_fail);
}
@@ -2488,8 +2483,7 @@
* If payload encryption is enabled, then the AAD consist of
* the RTCP header and the seq# at the end of the packet
*/
- status = cipher_set_aad(stream->rtcp_cipher, (uint8_t*)hdr,
- octets_in_rtcp_header);
+ status = srtp_cipher_set_aad(stream->rtcp_cipher, (uint8_t*)hdr, octets_in_rtcp_header);
if (status) {
return ( srtp_err_status_cipher_fail);
}
@@ -2499,8 +2493,8 @@
* the entire packet as described in section 10.3 in revision 07
* of the draft.
*/
- status = cipher_set_aad(stream->rtcp_cipher, (uint8_t*)hdr,
- (*pkt_octet_len - tag_len - sizeof(srtcp_trailer_t)));
+ status = srtp_cipher_set_aad(stream->rtcp_cipher, (uint8_t*)hdr,
+ (*pkt_octet_len - tag_len - sizeof(srtcp_trailer_t)));
if (status) {
return ( srtp_err_status_cipher_fail);
}
@@ -2510,8 +2504,7 @@
* put the idx# into network byte order, and process it as AAD
*/
tseq = htonl(*trailer);
- status = cipher_set_aad(stream->rtcp_cipher, (uint8_t*)&tseq,
- sizeof(srtcp_trailer_t));
+ status = srtp_cipher_set_aad(stream->rtcp_cipher, (uint8_t*)&tseq, sizeof(srtcp_trailer_t));
if (status) {
return ( srtp_err_status_cipher_fail);
}