static srtp_err_status_t srtp_hmac_dealloc (srtp_auth_t *a) { HMAC_CTX *hmac_ctx; hmac_ctx = (HMAC_CTX*)a->state; #if OPENSSL_VERSION_NUMBER < 0x10100000L HMAC_CTX_cleanup(hmac_ctx); /* zeroize entire state*/ octet_string_set_to_zero((uint8_t*)a, sizeof(HMAC_CTX) + sizeof(srtp_auth_t)); #else HMAC_CTX_free(hmac_ctx); /* zeroize entire state*/ octet_string_set_to_zero((uint8_t*)a, sizeof(srtp_auth_t)); #endif /* free memory */ srtp_crypto_free(a); return srtp_err_status_ok; }
/* * This function deallocates a GCM session */ err_status_t aes_gcm_openssl_dealloc (cipher_t *c) { aes_gcm_ctx_t *ctx; ctx = (aes_gcm_ctx_t*)c->state; if (ctx) { EVP_CIPHER_CTX_cleanup(&ctx->ctx); /* decrement ref_count for the appropriate engine */ switch (ctx->key_size) { case AES_256_KEYSIZE: aes_gcm_256_openssl.ref_count--; break; case AES_128_KEYSIZE: aes_gcm_128_openssl.ref_count--; break; default: return (err_status_dealloc_fail); break; } } /* zeroize entire state*/ octet_string_set_to_zero((uint8_t*)c, sizeof(cipher_t) + sizeof(aes_gcm_ctx_t)); /* free memory */ crypto_free(c); return (err_status_ok); }
err_status_t hmac_dealloc (auth_t *a) { extern auth_type_t hmac; hmac_ctx_t *hmac_ctx; hmac_ctx = (hmac_ctx_t*)a->state; if (hmac_ctx->ctx_initialized) { EVP_MD_CTX_cleanup(&hmac_ctx->ctx); } if (hmac_ctx->init_ctx_initialized) { EVP_MD_CTX_cleanup(&hmac_ctx->init_ctx); } /* zeroize entire state*/ octet_string_set_to_zero((uint8_t*)a, sizeof(hmac_ctx_t) + sizeof(auth_t)); /* free memory */ crypto_free(a); /* decrement global count of all hmac uses */ hmac.ref_count--; return err_status_ok; }
srtp_err_status_t srtp_cipher_output (srtp_cipher_t *c, uint8_t *buffer, uint32_t *num_octets_to_output) { /* zeroize the buffer */ octet_string_set_to_zero(buffer, *num_octets_to_output); /* exor keystream into buffer */ return (((c)->type)->encrypt(((c)->state), buffer, num_octets_to_output)); }
err_status_t cipher_output(cipher_t *c, uint8_t *buffer, int num_octets_to_output) { /* zeroize the buffer */ octet_string_set_to_zero(buffer, num_octets_to_output); /* exor keystream into buffer */ return cipher_encrypt(c, buffer, (unsigned int *) &num_octets_to_output); }
/* * Abstraction layer for encrypt. */ err_status_t aes_icm_output (aes_icm_ctx_t *c, uint8_t *buffer, int num_octets_to_output) { unsigned int len = num_octets_to_output; /* zeroize the buffer */ octet_string_set_to_zero(buffer, num_octets_to_output); /* exor keystream into buffer */ return aes_icm_openssl_encrypt(c, buffer, &len); }
static srtp_err_status_t srtp_hmac_dealloc (srtp_auth_t *a) { /* zeroize entire state*/ octet_string_set_to_zero((uint8_t*)a, sizeof(srtp_hmac_ctx_t) + sizeof(srtp_auth_t)); /* free memory */ srtp_crypto_free(a); return srtp_err_status_ok; }
static srtp_err_status_t srtp_null_cipher_dealloc (srtp_cipher_t *c) { extern const srtp_cipher_type_t srtp_null_cipher; /* zeroize entire state*/ octet_string_set_to_zero((uint8_t*)c, sizeof(srtp_cipher_t)); /* free memory of type null_cipher */ srtp_crypto_free(c); return srtp_err_status_ok; }
void ekt_write_data(ekt_stream_t ekt, uint8_t *base_tag, unsigned base_tag_len, int *packet_len, xtd_seq_num_t pkt_index) { uint32_t roc; uint16_t isn; unsigned emk_len; uint8_t *packet; /* if the pointer ekt is NULL, then EKT is not in effect */ if (!ekt) { debug_print(mod_srtp, "EKT not in use", NULL); return; } /* write zeros into the location of the base tag */ octet_string_set_to_zero(base_tag, base_tag_len); packet = base_tag + base_tag_len; /* copy encrypted master key into packet */ emk_len = ekt_octets_after_base_tag(ekt); memcpy(packet, ekt->encrypted_master_key, emk_len); debug_print(mod_srtp, "writing EKT EMK: %s,", octet_string_hex_string(packet, emk_len)); packet += emk_len; /* copy ROC into packet */ roc = (uint32_t)(pkt_index >> 16); *((uint32_t *)packet) = be32_to_cpu(roc); debug_print(mod_srtp, "writing EKT ROC: %s,", octet_string_hex_string(packet, sizeof(roc))); packet += sizeof(roc); /* copy ISN into packet */ isn = (uint16_t)pkt_index; *((uint16_t *)packet) = htons(isn); debug_print(mod_srtp, "writing EKT ISN: %s,", octet_string_hex_string(packet, sizeof(isn))); packet += sizeof(isn); /* copy SPI into packet */ *((uint16_t *)packet) = htons(ekt->data->spi); debug_print(mod_srtp, "writing EKT SPI: %s,", octet_string_hex_string(packet, sizeof(ekt->data->spi))); /* increase packet length appropriately */ *packet_len += EKT_OCTETS_AFTER_EMK + emk_len; }
err_status_t hmac_dealloc(auth_t *a) { extern auth_type_t hmac; /* zeroize entire state*/ octet_string_set_to_zero((uint8_t *)a, sizeof(hmac_ctx_t) + sizeof(auth_t)); /* free memory */ crypto_free(a); /* decrement global count of all hmac uses */ hmac.ref_count--; return err_status_ok; }
err_status_t aes_icm_dealloc(cipher_t *c) { extern cipher_type_t aes_icm; /* zeroize entire state*/ octet_string_set_to_zero((octet_t *)c, sizeof(aes_icm_ctx_t) + sizeof(cipher_t)); /* free memory */ crypto_free(c); /* decrement ref_count */ aes_icm.ref_count--; return err_status_ok; }
err_status_t null_cipher_dealloc(cipher_t *c) { extern cipher_type_t null_cipher; /* zeroize entire state*/ octet_string_set_to_zero((uint8_t *) c, sizeof(null_cipher_ctx_t) + sizeof(cipher_t)); /* free memory of type null_cipher */ crypto_free(c); /* decrement reference count */ null_cipher.ref_count--; return err_status_ok; }
/* * This function deallocates a GCM session */ static srtp_err_status_t srtp_aes_gcm_openssl_dealloc (srtp_cipher_t *c) { srtp_aes_gcm_ctx_t *ctx; ctx = (srtp_aes_gcm_ctx_t*)c->state; if (ctx) { EVP_CIPHER_CTX_cleanup(&ctx->ctx); /* zeroize the key material */ octet_string_set_to_zero((uint8_t*)ctx, sizeof(srtp_aes_gcm_ctx_t)); srtp_crypto_free(ctx); } /* free memory */ srtp_crypto_free(c); return (srtp_err_status_ok); }
/* * This function deallocates an instance of this engine */ err_status_t aes_icm_openssl_dealloc (cipher_t *c) { aes_icm_ctx_t *ctx; if (c == NULL) { return err_status_bad_param; } /* * Free the EVP context */ ctx = (aes_icm_ctx_t*)c->state; if (ctx != NULL) { EVP_CIPHER_CTX_cleanup(&ctx->ctx); /* decrement ref_count for the appropriate engine */ switch (ctx->key_size) { case AES_256_KEYSIZE: aes_icm_256.ref_count--; break; #ifndef BORINGSSL case AES_192_KEYSIZE: aes_icm_192.ref_count--; break; #endif case AES_128_KEYSIZE: aes_icm.ref_count--; break; default: return err_status_dealloc_fail; break; } } /* zeroize entire state*/ octet_string_set_to_zero((uint8_t*)c, sizeof(cipher_t) + sizeof(aes_icm_ctx_t)); /* free memory */ crypto_free(c); return err_status_ok; }
static srtp_err_status_t srtp_aes_icm_dealloc (srtp_cipher_t *c) { srtp_aes_icm_ctx_t *ctx; if (c == NULL) { return srtp_err_status_bad_param; } ctx = (srtp_aes_icm_ctx_t *)c->state; if (ctx) { /* zeroize the key material */ octet_string_set_to_zero((uint8_t*)ctx, sizeof(srtp_aes_icm_ctx_t)); srtp_crypto_free(ctx); } /* free the cipher context */ srtp_crypto_free(c); return srtp_err_status_ok; }
static srtp_err_status_t srtp_hmac_dealloc (srtp_auth_t *a) { srtp_hmac_ctx_t *hmac_ctx; hmac_ctx = (srtp_hmac_ctx_t*)a->state; if (hmac_ctx->ctx_initialized) { EVP_MD_CTX_cleanup(&hmac_ctx->ctx); } if (hmac_ctx->init_ctx_initialized) { EVP_MD_CTX_cleanup(&hmac_ctx->init_ctx); } /* zeroize entire state*/ octet_string_set_to_zero((uint8_t*)a, sizeof(srtp_hmac_ctx_t) + sizeof(srtp_auth_t)); /* free memory */ srtp_crypto_free(a); return srtp_err_status_ok; }
/* * This function deallocates an instance of this engine */ static srtp_err_status_t srtp_aes_icm_openssl_dealloc (srtp_cipher_t *c) { srtp_aes_icm_ctx_t *ctx; if (c == NULL) { return srtp_err_status_bad_param; } /* * Free the EVP context */ ctx = (srtp_aes_icm_ctx_t*)c->state; if (ctx != NULL) { EVP_CIPHER_CTX_cleanup(&ctx->ctx); /* zeroize the key material */ octet_string_set_to_zero((uint8_t*)ctx, sizeof(srtp_aes_icm_ctx_t)); srtp_crypto_free(ctx); } /* free memory */ srtp_crypto_free(c); return srtp_err_status_ok; }
err_status_t auth_type_self_test(const auth_type_t *at) { auth_test_case_t *test_case = at->test_data; auth_t *a; err_status_t status; uint8_t tag[SELF_TEST_TAG_BUF_OCTETS]; int i, case_num = 0; debug_print(mod_auth, "running self-test for auth function %s", at->description); /* * check to make sure that we have at least one test case, and * return an error if we don't - we need to be paranoid here */ if (test_case == NULL) return err_status_cant_check; /* loop over all test cases */ while (test_case != NULL) { /* check test case parameters */ if (test_case->tag_length_octets > SELF_TEST_TAG_BUF_OCTETS) return err_status_bad_param; /* allocate auth */ status = auth_type_alloc(at, &a, test_case->key_length_octets, test_case->tag_length_octets); if (status) return status; /* initialize auth */ status = auth_init(a, test_case->key); if (status) { auth_dealloc(a); return status; } /* zeroize tag then compute */ octet_string_set_to_zero(tag, test_case->tag_length_octets); status = auth_compute(a, test_case->data, test_case->data_length_octets, tag); if (status) { auth_dealloc(a); return status; } debug_print(mod_auth, "key: %s", octet_string_hex_string(test_case->key, test_case->key_length_octets)); debug_print(mod_auth, "data: %s", octet_string_hex_string(test_case->data, test_case->data_length_octets)); debug_print(mod_auth, "tag computed: %s", octet_string_hex_string(tag, test_case->tag_length_octets)); debug_print(mod_auth, "tag expected: %s", octet_string_hex_string(test_case->tag, test_case->tag_length_octets)); /* check the result */ status = err_status_ok; for (i=0; i < test_case->tag_length_octets; i++) if (tag[i] != test_case->tag[i]) { status = err_status_algo_fail; debug_print(mod_auth, "test case %d failed", case_num); debug_print(mod_auth, " (mismatch at octet %d)", i); } if (status) { auth_dealloc(a); return err_status_algo_fail; } /* deallocate the auth function */ status = auth_dealloc(a); if (status) return status; /* * the auth function passed the test case, so move on to the next test * case in the list; if NULL, we'll quit and return an OK */ test_case = test_case->next_test_case; ++case_num; } return err_status_ok; }