/* * aes_gcm_ossl.c * * AES Galois Counter Mode * * John A. Foley * Cisco Systems, Inc. * */ /* * * Copyright (c) 2013-2017, Cisco Systems, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. * * Neither the name of the Cisco Systems, Inc. nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * */ #ifdef HAVE_CONFIG_H #include <config.h> #endif #include <openssl/evp.h> #include "aes_gcm.h" #include "alloc.h" #include "err.h" /* for srtp_debug */ #include "crypto_types.h" #include "cipher_types.h" srtp_debug_module_t srtp_mod_aes_gcm = …; /* * For now we only support 8 and 16 octet tags. The spec allows for * optional 12 byte tag, which may be supported in the future. */ #define GCM_AUTH_TAG_LEN … #define GCM_AUTH_TAG_LEN_8 … /* * This function allocates a new instance of this crypto engine. * The key_len parameter should be one of 28 or 44 for * AES-128-GCM or AES-256-GCM respectively. Note that the * key length includes the 14 byte salt value that is used when * initializing the KDF. */ static srtp_err_status_t srtp_aes_gcm_openssl_alloc(srtp_cipher_t **c, int key_len, int tlen) { … } /* * This function deallocates a GCM session */ static srtp_err_status_t srtp_aes_gcm_openssl_dealloc(srtp_cipher_t *c) { … } /* * aes_gcm_openssl_context_init(...) initializes the aes_gcm_context * using the value in key[]. * * the key is the secret key */ static srtp_err_status_t srtp_aes_gcm_openssl_context_init(void *cv, const uint8_t *key) { … } /* * aes_gcm_openssl_set_iv(c, iv) sets the counter value to the exor of iv with * the offset */ static srtp_err_status_t srtp_aes_gcm_openssl_set_iv( void *cv, uint8_t *iv, srtp_cipher_direction_t direction) { … } /* * This function processes the AAD * * Parameters: * c Crypto context * 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(void *cv, const uint8_t *aad, uint32_t aad_len) { … } /* * This function encrypts a buffer using AES GCM mode * * Parameters: * c Crypto context * buf data to encrypt * enc_len length of encrypt buffer */ static srtp_err_status_t srtp_aes_gcm_openssl_encrypt(void *cv, unsigned char *buf, unsigned int *enc_len) { … } /* * This function calculates and returns the GCM tag for a given context. * This should be called after encrypting the data. The *len value * is increased by the tag size. The caller must ensure that *buf has * enough room to accept the appended tag. * * Parameters: * c Crypto context * buf data to encrypt * len length of encrypt buffer */ static srtp_err_status_t srtp_aes_gcm_openssl_get_tag(void *cv, uint8_t *buf, uint32_t *len) { … } /* * This function decrypts a buffer using AES GCM mode * * Parameters: * c Crypto context * buf data to encrypt * enc_len length of encrypt buffer */ static srtp_err_status_t srtp_aes_gcm_openssl_decrypt(void *cv, unsigned char *buf, unsigned int *enc_len) { … } /* * Name of this crypto engine */ static const char srtp_aes_gcm_128_openssl_description[] = …; static const char srtp_aes_gcm_256_openssl_description[] = …; /* * KAT values for AES self-test. These * values we're derived from independent test code * using OpenSSL. */ /* clang-format off */ static const uint8_t srtp_aes_gcm_test_case_0_key[SRTP_AES_GCM_128_KEY_LEN_WSALT] = …; /* clang-format on */ /* clang-format off */ static uint8_t srtp_aes_gcm_test_case_0_iv[12] = …; /* clang-format on */ /* clang-format off */ static const uint8_t srtp_aes_gcm_test_case_0_plaintext[60] = …; /* clang-format off */ static const uint8_t srtp_aes_gcm_test_case_0_aad[20] = …; /* clang-format on */ /* clang-format off */ static const uint8_t srtp_aes_gcm_test_case_0_ciphertext[76] = …; /* clang-format on */ static const srtp_cipher_test_case_t srtp_aes_gcm_test_case_0a = …; static const srtp_cipher_test_case_t srtp_aes_gcm_test_case_0 = …; /* clang-format off */ static const uint8_t srtp_aes_gcm_test_case_1_key[SRTP_AES_GCM_256_KEY_LEN_WSALT] = …; /* clang-format on */ /* clang-format off */ static uint8_t srtp_aes_gcm_test_case_1_iv[12] = …; /* clang-format on */ /* clang-format off */ static const uint8_t srtp_aes_gcm_test_case_1_plaintext[60] = …; /* clang-format on */ /* clang-format off */ static const uint8_t srtp_aes_gcm_test_case_1_aad[20] = …; /* clang-format on */ /* clang-format off */ static const uint8_t srtp_aes_gcm_test_case_1_ciphertext[76] = …; /* clang-format on */ static const srtp_cipher_test_case_t srtp_aes_gcm_test_case_1a = …; static const srtp_cipher_test_case_t srtp_aes_gcm_test_case_1 = …; /* * This is the vector function table for this crypto engine. */ const srtp_cipher_type_t srtp_aes_gcm_128 = …; /* * This is the vector function table for this crypto engine. */ const srtp_cipher_type_t srtp_aes_gcm_256 = …;