Refactor code structure for improved readability and maintainability
This commit is contained in:
10
CHANGELOG.md
Normal file
10
CHANGELOG.md
Normal file
@@ -0,0 +1,10 @@
|
||||
# Changelog
|
||||
|
||||
## v1.0.0
|
||||
|
||||
### Features
|
||||
|
||||
- Sync with `libsrtp` v3.0.0, commit tag: `7d20c51`
|
||||
- Return directly when do `srtp_cipher_type_test` to skip cipher test
|
||||
- Return directly when do `srtp_auth_type_test` to skip auth test
|
||||
- Remove unused code and doc to decrease code size
|
||||
60
CMakeLists.txt
Normal file
60
CMakeLists.txt
Normal file
@@ -0,0 +1,60 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
set(SOURCES_C
|
||||
libsrtp/srtp/srtp.c
|
||||
)
|
||||
|
||||
set(CIPHERS_SOURCES_C
|
||||
libsrtp/crypto/cipher/cipher.c
|
||||
libsrtp/crypto/cipher/null_cipher.c
|
||||
)
|
||||
|
||||
list(APPEND CIPHERS_SOURCES_C
|
||||
libsrtp/crypto/cipher/aes_icm_mbedtls.c
|
||||
libsrtp/crypto/cipher/aes_gcm_mbedtls.c
|
||||
libsrtp/crypto/cipher/cipher_test_cases.c
|
||||
)
|
||||
|
||||
set(HASHES_SOURCES_C
|
||||
libsrtp/crypto/hash/auth.c
|
||||
libsrtp/crypto/hash/null_auth.c
|
||||
)
|
||||
|
||||
list(APPEND HASHES_SOURCES_C
|
||||
libsrtp/crypto/hash/hmac_mbedtls.c
|
||||
libsrtp/crypto/hash/auth_test_cases.c
|
||||
)
|
||||
|
||||
set(KERNEL_SOURCES_C
|
||||
libsrtp/crypto/kernel/alloc.c
|
||||
libsrtp/crypto/kernel/crypto_kernel.c
|
||||
libsrtp/crypto/kernel/err.c
|
||||
libsrtp/crypto/kernel/key.c
|
||||
)
|
||||
|
||||
set(MATH_SOURCES_C
|
||||
libsrtp/crypto/math/datatypes.c
|
||||
)
|
||||
|
||||
set(REPLAY_SOURCES_C
|
||||
libsrtp/crypto/replay/rdb.c
|
||||
libsrtp/crypto/replay/rdbx.c
|
||||
)
|
||||
|
||||
set(SRTP_SRCS
|
||||
${SOURCES_C}
|
||||
${CIPHERS_SOURCES_C}
|
||||
${HASHES_SOURCES_C}
|
||||
${KERNEL_SOURCES_C}
|
||||
${MATH_SOURCES_C}
|
||||
${REPLAY_SOURCES_C}
|
||||
)
|
||||
|
||||
set(SRTP_INCLUDE_DIRS
|
||||
esp-port
|
||||
libsrtp/include
|
||||
libsrtp/crypto/include
|
||||
)
|
||||
|
||||
idf_component_register(SRCS ${SRTP_SRCS} INCLUDE_DIRS ${SRTP_INCLUDE_DIRS} REQUIRES mbedtls)
|
||||
target_compile_definitions(${COMPONENT_LIB} PUBLIC "-DHAVE_CONFIG_H" "-DGCM" "-DMBEDTLS")
|
||||
35
LICENSE
Normal file
35
LICENSE
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-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.
|
||||
*
|
||||
*/
|
||||
24
README.md
Normal file
24
README.md
Normal file
@@ -0,0 +1,24 @@
|
||||
# esp-libsrtp-GCM
|
||||
|
||||
由乐鑫提供的 libsrtp 修改而来,使其支持 AES-GCM.
|
||||
Claude 告诉我乐鑫把这个功能给注释掉了,不明白具体原因。
|
||||
我在测试环境试没找出什么问题来,还没在生产环境中测试,所以我不做任何可用性保证。
|
||||
|
||||
---
|
||||
|
||||
## 仓库原文
|
||||
|
||||
`esp-libsrtp` 是 **SRTP (安全实时传输协议)** 实现库 [libSRTP](https://github.com/cisco/libsrtp) 版本 v3.0.0 的封装。
|
||||
该库设计用于 **ESP32 系列平台**,为实时媒体流提供安全的加密和身份验证功能。
|
||||
|
||||
`esp-libsrtp` is a wrapper for **SRTP (Secure Real-time Transport Protocol)** realization [libSRTP](https://github.com/cisco/libsrtp) version v3.0.0.
|
||||
This library is designed to work on **ESP32 series platforms**, providing secure encryption and authentication for real-time media streams.
|
||||
|
||||
---
|
||||
|
||||
## 许可证 License
|
||||
|
||||
据 Claude 说它基于 BSD 许可证,详情请参阅:
|
||||
|
||||
- [LibSrtp](./libsrtp/LICENSE)
|
||||
- [ESP-ADF-Libs](https://github.com/espressif/esp-adf-libs)
|
||||
117
esp-port/config.h
Normal file
117
esp-port/config.h
Normal file
@@ -0,0 +1,117 @@
|
||||
/* clang-format off */
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
/* Define to the full name and version of this package. */
|
||||
#define PACKAGE_VERSION "3.0.0"
|
||||
|
||||
/* Define to the version of this package. */
|
||||
#define PACKAGE_STRING "esp_port 3.0.0"
|
||||
|
||||
/* Define to enabled debug logging for all modules. */
|
||||
/* #undef ENABLE_DEBUG_LOGGING */
|
||||
|
||||
/* Logging statements will be written to this file. */
|
||||
/* #undef ERR_REPORTING_FILE */
|
||||
|
||||
/* Define to redirect logging to stdout. */
|
||||
/* #undef ERR_REPORTING_STDOUT */
|
||||
|
||||
/* Define this to use OpenSSL crypto. */
|
||||
/* #undef OPENSSL */
|
||||
|
||||
/* Define this to use AES-GCM. */
|
||||
#define GCM 1
|
||||
|
||||
/* Define if building for a CISC machine (e.g. Intel). */
|
||||
#define CPU_CISC 1
|
||||
|
||||
/* Define if building for a RISC machine (assume slow byte access). */
|
||||
/* #undef CPU_RISC */
|
||||
|
||||
/* Define to use X86 inlined assembly code */
|
||||
/* #undef HAVE_X86 */
|
||||
|
||||
/* Define WORDS_BIGENDIAN to 1 if your processor stores words with the most
|
||||
significant byte first (like Motorola and SPARC, unlike Intel). */
|
||||
/* #undef WORDS_BIGENDIAN */
|
||||
|
||||
/* Define to 1 if you have the <arpa/inet.h> header file. */
|
||||
#define HAVE_ARPA_INET_H 1
|
||||
|
||||
/* Define to 1 if you have the <byteswap.h> header file. */
|
||||
/* #undef HAVE_BYTESWAP_H */
|
||||
|
||||
/* Define to 1 if you have the <inttypes.h> header file. */
|
||||
#define HAVE_INTTYPES_H 1
|
||||
|
||||
/* Define to 1 if you have the <machine/types.h> header file. */
|
||||
#define HAVE_MACHINE_TYPES_H 1
|
||||
|
||||
/* Define to 1 if you have the <netinet/in.h> header file. */
|
||||
#define HAVE_NETINET_IN_H 1
|
||||
|
||||
/* Define to 1 if you have the <stdint.h> header file. */
|
||||
#define HAVE_STDINT_H 1
|
||||
|
||||
/* Define to 1 if you have the <stdlib.h> header file. */
|
||||
#define HAVE_STDLIB_H 1
|
||||
|
||||
/* Define to 1 if you have the <sys/int_types.h> header file. */
|
||||
/* #undef HAVE_SYS_INT_TYPES_H */
|
||||
|
||||
/* Define to 1 if you have the <sys/socket.h> header file. */
|
||||
#define HAVE_SYS_SOCKET_H 1
|
||||
|
||||
/* Define to 1 if you have the <sys/types.h> header file. */
|
||||
#define HAVE_SYS_TYPES_H 1
|
||||
|
||||
/* Define to 1 if you have the <unistd.h> header file. */
|
||||
#define HAVE_UNISTD_H 1
|
||||
|
||||
/* Define to 1 if you have the <windows.h> header file. */
|
||||
/* #undef HAVE_WINDOWS_H */
|
||||
|
||||
/* Define to 1 if you have the <winsock2.h> header file. */
|
||||
/* #undef HAVE_WINSOCK2_H */
|
||||
|
||||
/* Define to 1 if you have the `inet_aton' function. */
|
||||
#define HAVE_INET_ATON 1
|
||||
|
||||
/* Define to 1 if you have the `sigaction' function. */
|
||||
/* #undef HAVE_SIGACTION */
|
||||
|
||||
/* Define to 1 if you have the `usleep' function. */
|
||||
#define HAVE_USLEEP 1
|
||||
|
||||
/* Define to 1 if the system has the type `uint8_t'. */
|
||||
#define HAVE_UINT8_T 1
|
||||
|
||||
/* Define to 1 if the system has the type `uint16_t'. */
|
||||
#define HAVE_UINT16_T 1
|
||||
|
||||
/* Define to 1 if the system has the type `uint32_t'. */
|
||||
#define HAVE_UINT32_T 1
|
||||
|
||||
/* Define to 1 if the system has the type `uint64_t'. */
|
||||
#define HAVE_UINT64_T 1
|
||||
|
||||
/* Define to 1 if the system has the type `int32_t'. */
|
||||
#define HAVE_INT32_T 1
|
||||
|
||||
/* The size of `unsigned long', as computed by sizeof. */
|
||||
#define SIZEOF_UNSIGNED_LONG 4
|
||||
|
||||
/* The size of `unsigned long long', as computed by sizeof. */
|
||||
#define SIZEOF_UNSIGNED_LONG_LONG 8
|
||||
|
||||
/* Define inline to what is supported by compiler */
|
||||
#define HAVE_INLINE 1
|
||||
/* #undef HAVE___INLINE */
|
||||
#ifndef HAVE_INLINE
|
||||
#ifdef HAVE___INLINE
|
||||
#define inline __inline
|
||||
#else
|
||||
#define inline
|
||||
#endif
|
||||
#endif
|
||||
10
idf_component.yml
Normal file
10
idf_component.yml
Normal file
@@ -0,0 +1,10 @@
|
||||
description: Espressif wrapper for libSRTP
|
||||
issues: https://github.com/espressif/esp-adf/issues
|
||||
repository: git://github.com/espressif/esp-adf-libs.git
|
||||
repository_info:
|
||||
commit_sha: f4bba1409f29901a18a3e3912f614fbd8cf0f937
|
||||
path: esp_libsrtp
|
||||
tags:
|
||||
- srtp
|
||||
url: https://github.com/espressif/esp-adf-libs/tree/master/esp_libsrtp
|
||||
version: 1.0.0
|
||||
35
libsrtp/LICENSE
Normal file
35
libsrtp/LICENSE
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-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.
|
||||
*
|
||||
*/
|
||||
556
libsrtp/README.md
Normal file
556
libsrtp/README.md
Normal file
@@ -0,0 +1,556 @@
|
||||
[](https://github.com/cisco/libsrtp/actions/workflows/cmake.yml)
|
||||
[](https://github.com/cisco/libsrtp/actions/workflows/autotools.yml)
|
||||
[](https://github.com/cisco/libsrtp/actions/workflows/meson.yml)
|
||||
[](https://scan.coverity.com/projects/cisco-libsrtp)
|
||||
[](https://oss-fuzz-build-logs.storage.googleapis.com/index.html#libsrtp)
|
||||
|
||||
<a name="introduction-to-libsrtp"></a>
|
||||
# Introduction to libSRTP
|
||||
|
||||
This package provides an implementation of the Secure Real-time
|
||||
Transport Protocol (SRTP), the Universal Security Transform (UST), and
|
||||
a supporting cryptographic kernel. The SRTP API is documented in include/srtp.h,
|
||||
and the library is in libsrtp2.a (after compilation).
|
||||
|
||||
This document describes libSRTP, the Open Source Secure RTP library
|
||||
from Cisco Systems, Inc. RTP is the Real-time Transport Protocol, an
|
||||
IETF standard for the transport of real-time data such as telephony,
|
||||
audio, and video, defined by [RFC 3550](https://tools.ietf.org/html/rfc3550).
|
||||
Secure RTP (SRTP) is an RTP profile for providing confidentiality to RTP data
|
||||
and authentication to the RTP header and payload. SRTP is an IETF Standard,
|
||||
defined in [RFC 3711](https://tools.ietf.org/html/rfc3711), and was developed
|
||||
in the IETF Audio/Video Transport (AVT) Working Group. This library supports
|
||||
all of the mandatory features of SRTP, but not all of the optional features. See
|
||||
the [Supported Features](#supported-features) section for more detailed information.
|
||||
|
||||
This document is also used to generate the documentation files in the /doc/
|
||||
folder where a more detailed reference to the libSRTP API and related functions
|
||||
can be created (requires installing doxygen.). The reference material is created
|
||||
automatically from comments embedded in some of the C header files. The
|
||||
documentation is organized into modules in order to improve its clarity. These
|
||||
modules do not directly correspond to files. An underlying cryptographic kernel
|
||||
provides much of the basic functionality of libSRTP but is mostly undocumented
|
||||
because it does its work behind the scenes.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
<a name="contact-us"></a>
|
||||
# Contact Us
|
||||
|
||||
- [libsrtp@lists.packetizer.com](mailto:libsrtp@lists.packetizer.com) general mailing list for news / announcements / discussions. This is an open list, see
|
||||
[https://lists.packetizer.com/mailman/listinfo/libsrtp](https://lists.packetizer.com/mailman/listinfo/libsrtp) for singing up.
|
||||
|
||||
- [libsrtp-security@lists.packetizer.com](mailto:libsrtp-security@lists.packetizer.com) for disclosing security issues to the libsrtp maintenance team. This is a closed list but anyone can send to it.
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
<a name="contents"></a>
|
||||
## Contents
|
||||
|
||||
- [Introduction to libSRTP](#introduction-to-libsrtp)
|
||||
- [Contact Us](#contact-us)
|
||||
- [Contents](#contents)
|
||||
- [License and Disclaimer](#license-and-disclaimer)
|
||||
- [libSRTP Overview](#libsrtp-overview)
|
||||
- [Secure RTP Background](#secure-rtp-background)
|
||||
- [Supported Features](#supported-features)
|
||||
- [Implementation Notes](#implementation-notes)
|
||||
- [Installing and Building libSRTP](#installing-and-building-libsrtp)
|
||||
- [Changing Build Configuration](#changing-build-configuration)
|
||||
- [Using Visual Studio](#using-visual-studio)
|
||||
- [Applications](#applications)
|
||||
- [Example Code](#example-code)
|
||||
- [Credits](#credits)
|
||||
- [References](#references)
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
<a name="license-and-disclaimer"></a>
|
||||
# License and Disclaimer
|
||||
|
||||
libSRTP is distributed under the following license, which is included
|
||||
in the source code distribution. It is reproduced in the manual in
|
||||
case you got the library from another source.
|
||||
|
||||
> Copyright (c) 2001-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.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
<a name="libsrtp-overview"></a>
|
||||
# libSRTP Overview
|
||||
|
||||
libSRTP provides functions for protecting RTP and RTCP. RTP packets
|
||||
can be encrypted and authenticated (using the `srtp_protect()`
|
||||
function), turning them into SRTP packets. Similarly, SRTP packets
|
||||
can be decrypted and have their authentication verified (using the
|
||||
`srtp_unprotect()` function), turning them into RTP packets. Similar
|
||||
functions apply security to RTCP packets.
|
||||
|
||||
The typedef `srtp_stream_t` points to a structure holding all of the
|
||||
state associated with an SRTP stream, including the keys and
|
||||
parameters for cipher and message authentication functions and the
|
||||
anti-replay data. A particular `srtp_stream_t` holds the information
|
||||
needed to protect a particular RTP and RTCP stream. This datatype
|
||||
is intentionally opaque in order to better seperate the libSRTP
|
||||
API from its implementation.
|
||||
|
||||
Within an SRTP session, there can be multiple streams, each
|
||||
originating from a particular sender. Each source uses a distinct
|
||||
stream context to protect the RTP and RTCP stream that it is
|
||||
originating. The typedef `srtp_t` points to a structure holding all of
|
||||
the state associated with an SRTP session. There can be multiple
|
||||
stream contexts associated with a single `srtp_t`. A stream context
|
||||
cannot exist indepent from an `srtp_t`, though of course an `srtp_t` can
|
||||
be created that contains only a single stream context. A device
|
||||
participating in an SRTP session must have a stream context for each
|
||||
source in that session, so that it can process the data that it
|
||||
receives from each sender.
|
||||
|
||||
In libSRTP, a session is created using the function `srtp_create()`.
|
||||
The policy to be implemented in the session is passed into this
|
||||
function as an `srtp_policy_t` structure. A single one of these
|
||||
structures describes the policy of a single stream. These structures
|
||||
can also be linked together to form an entire session policy. A linked
|
||||
list of `srtp_policy_t` structures is equivalent to a session policy.
|
||||
In such a policy, we refer to a single `srtp_policy_t` as an *element*.
|
||||
|
||||
An `srtp_policy_t` structure contains two `srtp_crypto_policy_t` structures
|
||||
that describe the cryptograhic policies for RTP and RTCP, as well as
|
||||
the SRTP master key and the SSRC value. The SSRC describes what to
|
||||
protect (e.g. which stream), and the `srtp_crypto_policy_t` structures
|
||||
describe how to protect it. The key is contained in a policy element
|
||||
because it simplifies the interface to the library. In many cases, it
|
||||
is desirable to use the same cryptographic policies across all of the
|
||||
streams in a session, but to use a distinct key for each stream. A
|
||||
`srtp_crypto_policy_t` structure can be initialized by using either the
|
||||
`srtp_crypto_policy_set_rtp_default()` or `srtp_crypto_policy_set_rtcp_default()`
|
||||
functions, which set a crypto policy structure to the default policies
|
||||
for RTP and RTCP protection, respectively.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
<a name="secure-rtp-background"></a>
|
||||
## Secure RTP Background
|
||||
|
||||
In this section we review SRTP and introduce some terms that are used
|
||||
in libSRTP. An RTP session is defined by a pair of destination
|
||||
transport addresses, that is, a network address plus a pair of UDP
|
||||
ports for RTP and RTCP. RTCP, the RTP control protocol, is used to
|
||||
coordinate between the participants in an RTP session, e.g. to provide
|
||||
feedback from receivers to senders. An *SRTP session* is
|
||||
similarly defined; it is just an RTP session for which the SRTP
|
||||
profile is being used. An SRTP session consists of the traffic sent
|
||||
to the SRTP or SRTCP destination transport addresses. Each
|
||||
participant in a session is identified by a synchronization source
|
||||
(SSRC) identifier. Some participants may not send any SRTP traffic;
|
||||
they are called receivers, even though they send out SRTCP traffic,
|
||||
such as receiver reports.
|
||||
|
||||
RTP allows multiple sources to send RTP and RTCP traffic during the
|
||||
same session. The synchronization source identifier (SSRC) is used to
|
||||
distinguish these sources. In libSRTP, we call the SRTP and SRTCP
|
||||
traffic from a particular source a *stream*. Each stream has its own
|
||||
SSRC, sequence number, rollover counter, and other data. A particular
|
||||
choice of options, cryptographic mechanisms, and keys is called a
|
||||
*policy*. Each stream within a session can have a distinct policy
|
||||
applied to it. A session policy is a collection of stream policies.
|
||||
|
||||
A single policy can be used for all of the streams in a given session,
|
||||
though the case in which a single *key* is shared across multiple
|
||||
streams requires care. When key sharing is used, the SSRC values that
|
||||
identify the streams **must** be distinct. This requirement can be
|
||||
enforced by using the convention that each SRTP and SRTCP key is used
|
||||
for encryption by only a single sender. In other words, the key is
|
||||
shared only across streams that originate from a particular device (of
|
||||
course, other SRTP participants will need to use the key for
|
||||
decryption). libSRTP supports this enforcement by detecting the case
|
||||
in which a key is used for both inbound and outbound data.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
<a name="supported-features"></a>
|
||||
## Supported Features
|
||||
|
||||
This library supports all of the mandatory-to-implement features of
|
||||
SRTP (as defined in [RFC 3711](https://tools.ietf.org/html/rfc3711)). Some of these
|
||||
features can be selected (or de-selected) at run time by setting an
|
||||
appropriate policy; this is done using the structure `srtp_policy_t`.
|
||||
Some other behaviors of the protocol can be adapted by defining an
|
||||
approriate event handler for the exceptional events; see the SRTPevents
|
||||
section in the generated documentation.
|
||||
|
||||
Some options that are described in the SRTP specification are not
|
||||
supported. This includes
|
||||
|
||||
- key derivation rates other than zero,
|
||||
- the cipher F8,
|
||||
- the use of the packet index to select between master keys.
|
||||
|
||||
The user should be aware that it is possible to misuse this library,
|
||||
and that the result may be that the security level it provides is
|
||||
inadequate. If you are implementing a feature using this library, you
|
||||
will want to read the Security Considerations section of [RFC 3711](https://tools.ietf.org/html/rfc3711#section-9).
|
||||
In addition, it is important that you read and understand the
|
||||
terms outlined in the [License and Disclaimer](#license-and-disclaimer) section.
|
||||
|
||||
This library also supports the AES-GCM Authenticated Encryption methods
|
||||
described in [RFC 7714](https://tools.ietf.org/html/rfc7714)
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
<a name="implementation-notes"></a>
|
||||
## Implementation Notes
|
||||
|
||||
* It is possible to configure which 3rd party (ie openssl/nss/etc) crypto backend
|
||||
libSRTP will be built with. If no 3rd party backend is set then libSRTP provides
|
||||
an internal implementation of AES and Sha1. The internal implementation only
|
||||
supports AES-128 & AES-256, so to use AES-192 or the AES-GCM group of ciphers a
|
||||
3rd party crypto backend must be configured. For this and performance reasons it
|
||||
is highly recommended to use a 3rd party crypto backend.
|
||||
|
||||
* The `srtp_protect()` function assumes that the buffer holding the
|
||||
rtp packet has enough storage allocated that the authentication
|
||||
tag can be written to the end of that packet. If this assumption
|
||||
is not valid, memory corruption will ensue.
|
||||
|
||||
* Automated tests for the crypto functions are provided through
|
||||
the `cipher_type_self_test()` and `auth_type_self_test()` functions.
|
||||
These functions should be used to test each port of this code
|
||||
to a new platform.
|
||||
|
||||
* Replay protection is contained in the crypto engine, and
|
||||
tests for it are provided.
|
||||
|
||||
* This implementation provides calls to initialize, protect, and
|
||||
unprotect RTP packets, and makes as few as possible assumptions
|
||||
about how these functions will be called. For example, the
|
||||
caller is not expected to provide packets in order (though if
|
||||
they're called more than 65k out of sequence, synchronization
|
||||
will be lost).
|
||||
|
||||
* The sequence number in the rtp packet is used as the low 16 bits
|
||||
of the sender's local packet index. Note that RTP will start its
|
||||
sequence number in a random place, and the SRTP layer just jumps
|
||||
forward to that number at its first invocation. An earlier
|
||||
version of this library used initial sequence numbers that are
|
||||
less than 32,768; this trick is no longer required as the
|
||||
`rdbx_estimate_index(...)` function has been made smarter as of
|
||||
version 1.0.1.
|
||||
|
||||
* The replay window for (S)RTCP is hardcoded to 128 bits in length.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
<a name="installing-and-building-libsrtp"></a>
|
||||
# Installing and Building libSRTP
|
||||
|
||||
To install libSRTP, download the latest release of the distribution
|
||||
from [https://github.com/cisco/libsrtp/releases](https://github.com/cisco/libsrtp/releases).
|
||||
You probably want to get the most recent release. Unpack the distribution and
|
||||
extract the source files; the directory into which the source files
|
||||
will go is named `libsrtp-A-B-C` where `A` is the version number, `B` is the
|
||||
major release number and `C` is the minor release number.
|
||||
|
||||
libSRTP uses the GNU `autoconf` and `make` utilities (BSD make will not work; if
|
||||
both versions of make are on your platform, you can invoke GNU make as
|
||||
`gmake`.). In the `libsrtp` directory, run the configure script and then
|
||||
make:
|
||||
|
||||
~~~.txt
|
||||
./configure [ options ]
|
||||
make
|
||||
~~~
|
||||
|
||||
The configure script accepts the following options:
|
||||
|
||||
Option | Description
|
||||
-------------------------------|--------------------
|
||||
\-\-help \-h | Display help
|
||||
\-\-enable-debug-logging | Enable debug logging in all modules
|
||||
\-\-enable-openssl | Enable OpenSSL crypto engine
|
||||
\-\-enable-nss | Enable NSS crypto engine
|
||||
\-\-enable-openssl-kdf | Enable OpenSSL KDF algorithm
|
||||
\-\-enable-log-stdout | Enable logging to stdout
|
||||
\-\-with-openssl-dir | Location of OpenSSL installation
|
||||
\-\-with-nss-dir | Location of NSS installation
|
||||
\-\-with-log-file | Use file for logging
|
||||
|
||||
By default there is no log output, logging can be enabled to be output to stdout
|
||||
or a given file using the configure options.
|
||||
|
||||
This package has been tested on the following platforms: Mac OS X
|
||||
(powerpc-apple-darwin1.4), Cygwin (i686-pc-cygwin), Solaris
|
||||
(sparc-sun-solaris2.6), RedHat Linux 7.1 and 9 (i686-pc-linux), and
|
||||
OpenBSD (sparc-unknown-openbsd2.7).
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
<a name="changing-build-configuration"></a>
|
||||
## Changing Build Configuration
|
||||
|
||||
To build the `./configure` script mentioned above, libSRTP relies on the
|
||||
[automake](https://www.gnu.org/software/automake/) toolchain. Since
|
||||
`./configure` is built from `configure.in` by automake, if you make changes in
|
||||
how `./configure` works (e.g., to add a new library dependency), you will need
|
||||
to rebuild `./configure` and commit the updated version. In addition to
|
||||
automake itself, you will need to have the `pkgconfig` tools installed as well.
|
||||
|
||||
For example, on macOS:
|
||||
|
||||
```
|
||||
brew install automake pkgconfig
|
||||
# Edit configure.in
|
||||
autoremake -ivf
|
||||
```
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
<a name="using-visual-studio"></a>
|
||||
## Using Visual Studio
|
||||
|
||||
On Windows one can use Visual Studio via CMake. CMake can be downloaded here:
|
||||
https://cmake.org/ . To create Visual Studio build files, for example run the
|
||||
following commands:
|
||||
|
||||
```
|
||||
# Create build subdirectory
|
||||
mkdir build
|
||||
cd build
|
||||
|
||||
# Make project files
|
||||
cmake .. -G "Visual Studio 15 2017"
|
||||
|
||||
# Or for 64 bit project files
|
||||
cmake .. -G "Visual Studio 15 2017 Win64"
|
||||
```
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
<a name="using-meson"></a>
|
||||
## Using Meson
|
||||
|
||||
On all platforms including Windows, one can build using [Meson](https://mesonbuild.com).
|
||||
Steps to download Meson are here: https://mesonbuild.com/Getting-meson.html
|
||||
|
||||
To build with Meson, you can do something like:
|
||||
|
||||
```
|
||||
# Setup the build subdirectory
|
||||
meson setup --prefix=/path/to/prefix builddir
|
||||
|
||||
# Build the project
|
||||
meson compile -C builddir
|
||||
|
||||
# Run tests
|
||||
meson test -C builddir
|
||||
|
||||
# Optionally, install
|
||||
meson install -C builddir
|
||||
```
|
||||
|
||||
To build with Visual Studio, run the above commands from inside a Visual Studio
|
||||
command prompt, or run `vcvarsall.bat` with the appropriate arguments inside
|
||||
a Command Prompt.
|
||||
|
||||
Note that you can also replace the above commands with the appropriate `ninja`
|
||||
targets: `ninja -C build`, `ninja -C build test`, `ninja -C build install`.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
<a name="applications"></a>
|
||||
# Applications
|
||||
|
||||
Several test drivers and a simple and portable srtp application are
|
||||
included in the `test/` subdirectory.
|
||||
|
||||
Test driver | Function tested
|
||||
--------- | -------
|
||||
kernel_driver | crypto kernel (ciphers, auth funcs, rng)
|
||||
srtp_driver | srtp in-memory tests (does not use the network)
|
||||
rdbx_driver | rdbx (extended replay database)
|
||||
roc_driver | extended sequence number functions
|
||||
replay_driver | replay database
|
||||
cipher_driver | ciphers
|
||||
auth_driver | hash functions
|
||||
|
||||
The app `rtpw` is a simple rtp application which reads words from
|
||||
`/usr/dict/words` and then sends them out one at a time using [s]rtp.
|
||||
Manual srtp keying uses the -k option; automated key management
|
||||
using gdoi will be added later.
|
||||
|
||||
usage:
|
||||
~~~.txt
|
||||
rtpw [[-d <debug>]* [-k|b <key> [-a][-e <key size>][-g]] [-s | -r] dest_ip dest_port] | [-l]
|
||||
~~~
|
||||
|
||||
Either the -s (sender) or -r (receiver) option must be chosen. The
|
||||
values `dest_ip`, `dest_port` are the IP address and UDP port to which
|
||||
the dictionary will be sent, respectively.
|
||||
|
||||
The options are:
|
||||
|
||||
Option | Description
|
||||
--------- | -------
|
||||
-s | (S)RTP sender - causes app to send words
|
||||
-r | (S)RTP receive - causes app to receive words
|
||||
-k <key> | use SRTP master key <key>, where the key is a hexadecimal (without the leading "0x")
|
||||
-b <key> | same as -k but with base64 encoded key
|
||||
-e <keysize> | encrypt/decrypt (for data confidentiality) (requires use of -k option as well) (use 128, 192, or 256 for keysize)
|
||||
-g | use AES-GCM mode (must be used with -e)
|
||||
-a | message authentication (requires use of -k option as well)
|
||||
-l | list the available debug modules
|
||||
-d <debug> | turn on debugging for module <debug>
|
||||
|
||||
In order to get random 30-byte values for use as key/salt pairs , you
|
||||
can use the following bash function to format the output of
|
||||
`/dev/random` (where that device is available).
|
||||
|
||||
~~~.txt
|
||||
function randhex() {
|
||||
cat /dev/random | od --read-bytes=32 --width=32 -x | awk '{ print $2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 $14 $15 $16 }'
|
||||
}
|
||||
~~~
|
||||
|
||||
An example of an SRTP session using two rtpw programs follows:
|
||||
|
||||
~~~.txt
|
||||
set k=c1eec3717da76195bb878578790af71c4ee9f859e197a414a78d5abc7451
|
||||
|
||||
[sh1]$ test/rtpw -s -k $k -e 128 -a 0.0.0.0 9999
|
||||
Security services: confidentiality message authentication
|
||||
set master key/salt to C1EEC3717DA76195BB878578790AF71C/4EE9F859E197A414A78D5ABC7451
|
||||
setting SSRC to 2078917053
|
||||
sending word: A
|
||||
sending word: a
|
||||
sending word: aa
|
||||
sending word: aal
|
||||
...
|
||||
|
||||
[sh2]$ test/rtpw -r -k $k -e 128 -a 0.0.0.0 9999
|
||||
security services: confidentiality message authentication
|
||||
set master key/salt to C1EEC3717DA76195BB878578790AF71C/4EE9F859E197A414A78D5ABC7451
|
||||
19 octets received from SSRC 2078917053 word: A
|
||||
19 octets received from SSRC 2078917053 word: a
|
||||
20 octets received from SSRC 2078917053 word: aa
|
||||
21 octets received from SSRC 2078917053 word: aal
|
||||
...
|
||||
~~~
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
<a name="example-code"></a>
|
||||
## Example Code
|
||||
|
||||
This section provides a simple example of how to use libSRTP. The
|
||||
example code lacks error checking, but is functional. Here we assume
|
||||
that the value ssrc is already set to describe the SSRC of the stream
|
||||
that we are sending, and that the functions `get_rtp_packet()` and
|
||||
`send_srtp_packet()` are available to us. The former puts an RTP packet
|
||||
into the buffer and returns the number of octets written to that
|
||||
buffer. The latter sends the RTP packet in the buffer, given the
|
||||
length as its second argument.
|
||||
|
||||
~~~.c
|
||||
srtp_t session;
|
||||
srtp_policy_t policy;
|
||||
|
||||
// Set key to predetermined value
|
||||
uint8_t key[30] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
|
||||
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
|
||||
0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D};
|
||||
|
||||
// initialize libSRTP
|
||||
srtp_init();
|
||||
|
||||
// default policy values
|
||||
memset(&policy, 0x0, sizeof(srtp_policy_t));
|
||||
|
||||
// set policy to describe a policy for an SRTP stream
|
||||
srtp_crypto_policy_set_rtp_default(&policy.rtp);
|
||||
srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
|
||||
policy.ssrc = ssrc;
|
||||
policy.key = key;
|
||||
policy.next = NULL;
|
||||
|
||||
// allocate and initialize the SRTP session
|
||||
srtp_create(&session, &policy);
|
||||
|
||||
// main loop: get rtp packets, send srtp packets
|
||||
while (1) {
|
||||
char rtp_buffer[2048];
|
||||
size_t rtp_len;
|
||||
char srtp_buffer[2048];
|
||||
size_t srtp_len = sizeof(srtp_buffer);
|
||||
|
||||
len = get_rtp_packet(rtp_buffer);
|
||||
srtp_protect(session, rtp_buffer, rtp_len, srtp_buffer, &srtp_len);
|
||||
send_srtp_packet(srtp_buffer, srtp_len);
|
||||
}
|
||||
~~~
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
<a name="credits"></a>
|
||||
# Credits
|
||||
|
||||
The original implementation and documentation of libSRTP was written
|
||||
by David McGrew of Cisco Systems, Inc. in order to promote the use,
|
||||
understanding, and interoperability of Secure RTP. Michael Jerris
|
||||
contributed support for building under MSVC. Andris Pavenis
|
||||
contributed many important fixes. Brian West contributed changes to
|
||||
enable dynamic linking. Yves Shumann reported documentation bugs.
|
||||
Randell Jesup contributed a working SRTCP implementation and other
|
||||
fixes. Steve Underwood contributed x86_64 portability changes. We also give
|
||||
thanks to Fredrik Thulin, Brian Weis, Mark Baugher, Jeff Chan, Bill
|
||||
Simon, Douglas Smith, Bill May, Richard Preistley, Joe Tardo and
|
||||
others for contributions, comments, and corrections.
|
||||
|
||||
This reference material, when applicable, in this documenation was generated
|
||||
using the doxygen utility for automatic documentation of source code.
|
||||
|
||||
Copyright 2001-2005 by David A. McGrew, Cisco Systems, Inc.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
<a name="references"></a>
|
||||
# References
|
||||
|
||||
SRTP and ICM References
|
||||
September, 2005
|
||||
|
||||
Secure RTP is defined in [RFC 3711](https://tools.ietf.org/html/rfc3711).
|
||||
The counter mode definition is in [Section 4.1.1](https://tools.ietf.org/html/rfc3711#section-4.1.1).
|
||||
|
||||
SHA-1 is defined in [FIPS PUB 180-4](http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf).
|
||||
|
||||
HMAC is defined in [RFC 2104](https://tools.ietf.org/html/rfc2104)
|
||||
and HMAC-SHA1 test vectors are available
|
||||
in [RFC 2202](https://tools.ietf.org/html/rfc2202#section-3).
|
||||
|
||||
AES-GCM usage in SRTP is defined in [RFC 7714](https://tools.ietf.org/html/rfc7714)
|
||||
2160
libsrtp/crypto/cipher/aes.c
Normal file
2160
libsrtp/crypto/cipher/aes.c
Normal file
File diff suppressed because it is too large
Load Diff
442
libsrtp/crypto/cipher/aes_gcm_mbedtls.c
Normal file
442
libsrtp/crypto/cipher/aes_gcm_mbedtls.c
Normal file
@@ -0,0 +1,442 @@
|
||||
/*
|
||||
* aes_gcm_mbedtls.c
|
||||
*
|
||||
* AES Galois Counter Mode
|
||||
*
|
||||
* YongCheng Yang
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* 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 <mbedtls/gcm.h>
|
||||
#include "aes_gcm.h"
|
||||
#include "alloc.h"
|
||||
#include "err.h" /* for srtp_debug */
|
||||
#include "crypto_types.h"
|
||||
#include "cipher_types.h"
|
||||
#include "cipher_test_cases.h"
|
||||
|
||||
srtp_debug_module_t srtp_mod_aes_gcm = {
|
||||
false, /* debugging is off by default */
|
||||
"aes gcm mbedtls" /* printable module name */
|
||||
};
|
||||
|
||||
/**
|
||||
* SRTP IV Formation for AES-GCM
|
||||
* https://tools.ietf.org/html/rfc7714#section-8.1
|
||||
* 0 0 0 0 0 0 0 0 0 0 1 1
|
||||
* 0 1 2 3 4 5 6 7 8 9 0 1
|
||||
* +--+--+--+--+--+--+--+--+--+--+--+--+
|
||||
* |00|00| SSRC | ROC | SEQ |---+
|
||||
* +--+--+--+--+--+--+--+--+--+--+--+--+ |
|
||||
* |
|
||||
* +--+--+--+--+--+--+--+--+--+--+--+--+ |
|
||||
* | Encryption Salt |->(+)
|
||||
* +--+--+--+--+--+--+--+--+--+--+--+--+ |
|
||||
* |
|
||||
* +--+--+--+--+--+--+--+--+--+--+--+--+ |
|
||||
* | Initialization Vector |<--+
|
||||
* +--+--+--+--+--+--+--+--+--+--+--+--+
|
||||
*
|
||||
* SRTCP IV Formation for AES-GCM
|
||||
* https://tools.ietf.org/html/rfc7714#section-9.1
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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_IV_LEN 12
|
||||
#define GCM_AUTH_TAG_LEN 16
|
||||
#define GCM_AUTH_TAG_LEN_8 8
|
||||
|
||||
#define FUNC_ENTRY() debug_print(srtp_mod_aes_gcm, "%s entry", __func__);
|
||||
|
||||
/*
|
||||
* static function declarations.
|
||||
*/
|
||||
static srtp_err_status_t srtp_aes_gcm_mbedtls_alloc(srtp_cipher_t **c,
|
||||
size_t key_len,
|
||||
size_t tlen);
|
||||
|
||||
static srtp_err_status_t srtp_aes_gcm_mbedtls_dealloc(srtp_cipher_t *c);
|
||||
|
||||
static srtp_err_status_t srtp_aes_gcm_mbedtls_context_init(void *cv,
|
||||
const uint8_t *key);
|
||||
|
||||
static srtp_err_status_t srtp_aes_gcm_mbedtls_set_iv(
|
||||
void *cv,
|
||||
uint8_t *iv,
|
||||
srtp_cipher_direction_t direction);
|
||||
|
||||
static srtp_err_status_t srtp_aes_gcm_mbedtls_set_aad(void *cv,
|
||||
const uint8_t *aad,
|
||||
size_t aad_len);
|
||||
|
||||
static srtp_err_status_t srtp_aes_gcm_mbedtls_encrypt(void *cv,
|
||||
const uint8_t *src,
|
||||
size_t src_len,
|
||||
uint8_t *dst,
|
||||
size_t *dst_len);
|
||||
|
||||
static srtp_err_status_t srtp_aes_gcm_mbedtls_decrypt(void *cv,
|
||||
const uint8_t *src,
|
||||
size_t src_len,
|
||||
uint8_t *dst,
|
||||
size_t *dst_len);
|
||||
|
||||
/*
|
||||
* Name of this crypto engine
|
||||
*/
|
||||
static const char srtp_aes_gcm_128_mbedtls_description[] =
|
||||
"AES-128 GCM using mbedtls";
|
||||
static const char srtp_aes_gcm_256_mbedtls_description[] =
|
||||
"AES-256 GCM using mbedtls";
|
||||
|
||||
/*
|
||||
* This is the vector function table for this crypto engine.
|
||||
*/
|
||||
/* clang-format off */
|
||||
const srtp_cipher_type_t srtp_aes_gcm_128 = {
|
||||
srtp_aes_gcm_mbedtls_alloc,
|
||||
srtp_aes_gcm_mbedtls_dealloc,
|
||||
srtp_aes_gcm_mbedtls_context_init,
|
||||
srtp_aes_gcm_mbedtls_set_aad,
|
||||
srtp_aes_gcm_mbedtls_encrypt,
|
||||
srtp_aes_gcm_mbedtls_decrypt,
|
||||
srtp_aes_gcm_mbedtls_set_iv,
|
||||
srtp_aes_gcm_128_mbedtls_description,
|
||||
&srtp_aes_gcm_128_test_case_0,
|
||||
SRTP_AES_GCM_128
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
/*
|
||||
* This is the vector function table for this crypto engine.
|
||||
*/
|
||||
/* clang-format off */
|
||||
const srtp_cipher_type_t srtp_aes_gcm_256 = {
|
||||
srtp_aes_gcm_mbedtls_alloc,
|
||||
srtp_aes_gcm_mbedtls_dealloc,
|
||||
srtp_aes_gcm_mbedtls_context_init,
|
||||
srtp_aes_gcm_mbedtls_set_aad,
|
||||
srtp_aes_gcm_mbedtls_encrypt,
|
||||
srtp_aes_gcm_mbedtls_decrypt,
|
||||
srtp_aes_gcm_mbedtls_set_iv,
|
||||
srtp_aes_gcm_256_mbedtls_description,
|
||||
&srtp_aes_gcm_256_test_case_0,
|
||||
SRTP_AES_GCM_256
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
/*
|
||||
* 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_mbedtls_alloc(srtp_cipher_t **c,
|
||||
size_t key_len,
|
||||
size_t tlen)
|
||||
{
|
||||
FUNC_ENTRY();
|
||||
srtp_aes_gcm_ctx_t *gcm;
|
||||
|
||||
debug_print(srtp_mod_aes_gcm, "allocating cipher with key length %zu",
|
||||
key_len);
|
||||
debug_print(srtp_mod_aes_gcm, "allocating cipher with tag length %zu",
|
||||
tlen);
|
||||
|
||||
/*
|
||||
* Verify the key_len is valid for one of: AES-128/256
|
||||
*/
|
||||
if (key_len != SRTP_AES_GCM_128_KEY_LEN_WSALT &&
|
||||
key_len != SRTP_AES_GCM_256_KEY_LEN_WSALT) {
|
||||
return (srtp_err_status_bad_param);
|
||||
}
|
||||
|
||||
if (tlen != GCM_AUTH_TAG_LEN && tlen != GCM_AUTH_TAG_LEN_8) {
|
||||
return (srtp_err_status_bad_param);
|
||||
}
|
||||
|
||||
/* allocate memory a cipher of type aes_gcm */
|
||||
*c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
|
||||
if (*c == NULL) {
|
||||
return (srtp_err_status_alloc_fail);
|
||||
}
|
||||
|
||||
gcm = (srtp_aes_gcm_ctx_t *)srtp_crypto_alloc(sizeof(srtp_aes_gcm_ctx_t));
|
||||
if (gcm == NULL) {
|
||||
srtp_crypto_free(*c);
|
||||
*c = NULL;
|
||||
return (srtp_err_status_alloc_fail);
|
||||
}
|
||||
|
||||
gcm->ctx =
|
||||
(mbedtls_gcm_context *)srtp_crypto_alloc(sizeof(mbedtls_gcm_context));
|
||||
if (gcm->ctx == NULL) {
|
||||
srtp_crypto_free(gcm);
|
||||
srtp_crypto_free(*c);
|
||||
*c = NULL;
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
mbedtls_gcm_init(gcm->ctx);
|
||||
|
||||
/* set pointers */
|
||||
(*c)->state = gcm;
|
||||
|
||||
/* setup cipher attributes */
|
||||
switch (key_len) {
|
||||
case SRTP_AES_GCM_128_KEY_LEN_WSALT:
|
||||
(*c)->type = &srtp_aes_gcm_128;
|
||||
(*c)->algorithm = SRTP_AES_GCM_128;
|
||||
gcm->key_size = SRTP_AES_128_KEY_LEN;
|
||||
gcm->tag_len = tlen;
|
||||
break;
|
||||
case SRTP_AES_GCM_256_KEY_LEN_WSALT:
|
||||
(*c)->type = &srtp_aes_gcm_256;
|
||||
(*c)->algorithm = SRTP_AES_GCM_256;
|
||||
gcm->key_size = SRTP_AES_256_KEY_LEN;
|
||||
gcm->tag_len = tlen;
|
||||
break;
|
||||
}
|
||||
|
||||
/* set key size */
|
||||
(*c)->key_len = key_len;
|
||||
|
||||
return (srtp_err_status_ok);
|
||||
}
|
||||
|
||||
/*
|
||||
* This function deallocates a GCM session
|
||||
*/
|
||||
static srtp_err_status_t srtp_aes_gcm_mbedtls_dealloc(srtp_cipher_t *c)
|
||||
{
|
||||
srtp_aes_gcm_ctx_t *ctx;
|
||||
FUNC_ENTRY();
|
||||
ctx = (srtp_aes_gcm_ctx_t *)c->state;
|
||||
if (ctx) {
|
||||
mbedtls_gcm_free(ctx->ctx);
|
||||
srtp_crypto_free(ctx->ctx);
|
||||
/* zeroize the key material */
|
||||
octet_string_set_to_zero(ctx, sizeof(srtp_aes_gcm_ctx_t));
|
||||
srtp_crypto_free(ctx);
|
||||
}
|
||||
|
||||
/* free memory */
|
||||
srtp_crypto_free(c);
|
||||
|
||||
return (srtp_err_status_ok);
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_aes_gcm_mbedtls_context_init(void *cv,
|
||||
const uint8_t *key)
|
||||
{
|
||||
FUNC_ENTRY();
|
||||
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
|
||||
uint32_t key_len_in_bits;
|
||||
int errCode = 0;
|
||||
c->dir = srtp_direction_any;
|
||||
c->aad_size = 0;
|
||||
|
||||
debug_print(srtp_mod_aes_gcm, "key: %s",
|
||||
srtp_octet_string_hex_string(key, c->key_size));
|
||||
key_len_in_bits = (c->key_size << 3);
|
||||
switch (c->key_size) {
|
||||
case SRTP_AES_256_KEY_LEN:
|
||||
case SRTP_AES_128_KEY_LEN:
|
||||
break;
|
||||
default:
|
||||
return (srtp_err_status_bad_param);
|
||||
break;
|
||||
}
|
||||
|
||||
errCode =
|
||||
mbedtls_gcm_setkey(c->ctx, MBEDTLS_CIPHER_ID_AES, key, key_len_in_bits);
|
||||
if (errCode != 0) {
|
||||
debug_print(srtp_mod_aes_gcm, "mbedtls error code: %d", errCode);
|
||||
return srtp_err_status_init_fail;
|
||||
}
|
||||
|
||||
return (srtp_err_status_ok);
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_aes_gcm_mbedtls_set_iv(
|
||||
void *cv,
|
||||
uint8_t *iv,
|
||||
srtp_cipher_direction_t direction)
|
||||
{
|
||||
FUNC_ENTRY();
|
||||
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
|
||||
|
||||
if (direction != srtp_direction_encrypt &&
|
||||
direction != srtp_direction_decrypt) {
|
||||
return (srtp_err_status_bad_param);
|
||||
}
|
||||
c->dir = direction;
|
||||
|
||||
debug_print(srtp_mod_aes_gcm, "setting iv: %s",
|
||||
srtp_octet_string_hex_string(iv, GCM_IV_LEN));
|
||||
c->iv_len = GCM_IV_LEN;
|
||||
memcpy(c->iv, iv, c->iv_len);
|
||||
return (srtp_err_status_ok);
|
||||
}
|
||||
|
||||
/*
|
||||
* 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_mbedtls_set_aad(void *cv,
|
||||
const uint8_t *aad,
|
||||
size_t aad_len)
|
||||
{
|
||||
FUNC_ENTRY();
|
||||
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
|
||||
|
||||
debug_print(srtp_mod_aes_gcm, "setting AAD: %s",
|
||||
srtp_octet_string_hex_string(aad, aad_len));
|
||||
|
||||
if (aad_len + c->aad_size > MAX_AD_SIZE) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
memcpy(c->aad + c->aad_size, aad, aad_len);
|
||||
c->aad_size += aad_len;
|
||||
|
||||
return (srtp_err_status_ok);
|
||||
}
|
||||
|
||||
/*
|
||||
* 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_mbedtls_encrypt(void *cv,
|
||||
const uint8_t *src,
|
||||
size_t src_len,
|
||||
uint8_t *dst,
|
||||
size_t *dst_len)
|
||||
{
|
||||
FUNC_ENTRY();
|
||||
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
|
||||
int errCode = 0;
|
||||
|
||||
if (c->dir != srtp_direction_encrypt) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
if (*dst_len < src_len + c->tag_len) {
|
||||
return srtp_err_status_buffer_small;
|
||||
}
|
||||
|
||||
errCode = mbedtls_gcm_crypt_and_tag(c->ctx, MBEDTLS_GCM_ENCRYPT, src_len,
|
||||
c->iv, c->iv_len, c->aad, c->aad_size,
|
||||
src, dst, c->tag_len, dst + src_len);
|
||||
|
||||
c->aad_size = 0;
|
||||
if (errCode != 0) {
|
||||
debug_print(srtp_mod_aes_gcm, "mbedtls error code: %d", errCode);
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
*dst_len = src_len + c->tag_len;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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_mbedtls_decrypt(void *cv,
|
||||
const uint8_t *src,
|
||||
size_t src_len,
|
||||
uint8_t *dst,
|
||||
size_t *dst_len)
|
||||
{
|
||||
FUNC_ENTRY();
|
||||
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
|
||||
int errCode = 0;
|
||||
|
||||
if (c->dir != srtp_direction_decrypt) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
if (src_len < c->tag_len) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
if (*dst_len < (src_len - c->tag_len)) {
|
||||
return srtp_err_status_buffer_small;
|
||||
}
|
||||
|
||||
debug_print(srtp_mod_aes_gcm, "AAD: %s",
|
||||
srtp_octet_string_hex_string(c->aad, c->aad_size));
|
||||
|
||||
errCode = mbedtls_gcm_auth_decrypt(
|
||||
c->ctx, (src_len - c->tag_len), c->iv, c->iv_len, c->aad, c->aad_size,
|
||||
src + (src_len - c->tag_len), c->tag_len, src, dst);
|
||||
c->aad_size = 0;
|
||||
if (errCode != 0) {
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
|
||||
/*
|
||||
* Reduce the buffer size by the tag length since the tag
|
||||
* is not part of the original payload
|
||||
*/
|
||||
*dst_len = (src_len - c->tag_len);
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
431
libsrtp/crypto/cipher/aes_gcm_nss.c
Normal file
431
libsrtp/crypto/cipher/aes_gcm_nss.c
Normal file
@@ -0,0 +1,431 @@
|
||||
/*
|
||||
* aes_gcm_nss.c
|
||||
*
|
||||
* AES Galois Counter Mode
|
||||
*
|
||||
* Richard L. Barnes
|
||||
* 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 "aes_gcm.h"
|
||||
#include "alloc.h"
|
||||
#include "err.h" /* for srtp_debug */
|
||||
#include "crypto_types.h"
|
||||
#include "cipher_types.h"
|
||||
#include "cipher_test_cases.h"
|
||||
#include <secerr.h>
|
||||
#include <nspr.h>
|
||||
|
||||
srtp_debug_module_t srtp_mod_aes_gcm = {
|
||||
false, /* debugging is off by default */
|
||||
"aes gcm nss" /* printable module name */
|
||||
};
|
||||
|
||||
/*
|
||||
* 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_IV_LEN 12
|
||||
#define GCM_AUTH_TAG_LEN 16
|
||||
#define GCM_AUTH_TAG_LEN_8 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_nss_alloc(srtp_cipher_t **c,
|
||||
size_t key_len,
|
||||
size_t tlen)
|
||||
{
|
||||
srtp_aes_gcm_ctx_t *gcm;
|
||||
NSSInitContext *nss;
|
||||
|
||||
debug_print(srtp_mod_aes_gcm, "allocating cipher with key length %zu",
|
||||
key_len);
|
||||
debug_print(srtp_mod_aes_gcm, "allocating cipher with tag length %zu",
|
||||
tlen);
|
||||
|
||||
/*
|
||||
* Verify the key_len is valid for one of: AES-128/256
|
||||
*/
|
||||
if (key_len != SRTP_AES_GCM_128_KEY_LEN_WSALT &&
|
||||
key_len != SRTP_AES_GCM_256_KEY_LEN_WSALT) {
|
||||
return (srtp_err_status_bad_param);
|
||||
}
|
||||
|
||||
if (tlen != GCM_AUTH_TAG_LEN && tlen != GCM_AUTH_TAG_LEN_8) {
|
||||
return (srtp_err_status_bad_param);
|
||||
}
|
||||
|
||||
/* Initialize NSS equiv of NSS_NoDB_Init(NULL) */
|
||||
nss = NSS_InitContext("", "", "", "", NULL,
|
||||
NSS_INIT_READONLY | NSS_INIT_NOCERTDB |
|
||||
NSS_INIT_NOMODDB | NSS_INIT_FORCEOPEN |
|
||||
NSS_INIT_OPTIMIZESPACE);
|
||||
if (!nss) {
|
||||
return (srtp_err_status_cipher_fail);
|
||||
}
|
||||
|
||||
/* allocate memory a cipher of type aes_gcm */
|
||||
*c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
|
||||
if (*c == NULL) {
|
||||
NSS_ShutdownContext(nss);
|
||||
return (srtp_err_status_alloc_fail);
|
||||
}
|
||||
|
||||
gcm = (srtp_aes_gcm_ctx_t *)srtp_crypto_alloc(sizeof(srtp_aes_gcm_ctx_t));
|
||||
if (gcm == NULL) {
|
||||
NSS_ShutdownContext(nss);
|
||||
srtp_crypto_free(*c);
|
||||
*c = NULL;
|
||||
return (srtp_err_status_alloc_fail);
|
||||
}
|
||||
|
||||
gcm->nss = nss;
|
||||
|
||||
/* set pointers */
|
||||
(*c)->state = gcm;
|
||||
|
||||
/* setup cipher attributes */
|
||||
switch (key_len) {
|
||||
case SRTP_AES_GCM_128_KEY_LEN_WSALT:
|
||||
(*c)->type = &srtp_aes_gcm_128;
|
||||
(*c)->algorithm = SRTP_AES_GCM_128;
|
||||
gcm->key_size = SRTP_AES_128_KEY_LEN;
|
||||
gcm->tag_size = tlen;
|
||||
gcm->params.ulTagBits = 8 * tlen;
|
||||
break;
|
||||
case SRTP_AES_GCM_256_KEY_LEN_WSALT:
|
||||
(*c)->type = &srtp_aes_gcm_256;
|
||||
(*c)->algorithm = SRTP_AES_GCM_256;
|
||||
gcm->key_size = SRTP_AES_256_KEY_LEN;
|
||||
gcm->tag_size = tlen;
|
||||
gcm->params.ulTagBits = 8 * tlen;
|
||||
break;
|
||||
default:
|
||||
/* this should never hit, but to be sure... */
|
||||
return (srtp_err_status_bad_param);
|
||||
}
|
||||
|
||||
/* set key size and tag size*/
|
||||
(*c)->key_len = key_len;
|
||||
|
||||
return (srtp_err_status_ok);
|
||||
}
|
||||
|
||||
/*
|
||||
* This function deallocates a GCM session
|
||||
*/
|
||||
static srtp_err_status_t srtp_aes_gcm_nss_dealloc(srtp_cipher_t *c)
|
||||
{
|
||||
srtp_aes_gcm_ctx_t *ctx;
|
||||
|
||||
ctx = (srtp_aes_gcm_ctx_t *)c->state;
|
||||
if (ctx) {
|
||||
/* release NSS resources */
|
||||
if (ctx->key) {
|
||||
PK11_FreeSymKey(ctx->key);
|
||||
}
|
||||
|
||||
if (ctx->nss) {
|
||||
NSS_ShutdownContext(ctx->nss);
|
||||
ctx->nss = NULL;
|
||||
}
|
||||
|
||||
/* zeroize the key material */
|
||||
octet_string_set_to_zero(ctx, sizeof(srtp_aes_gcm_ctx_t));
|
||||
srtp_crypto_free(ctx);
|
||||
}
|
||||
|
||||
/* free memory */
|
||||
srtp_crypto_free(c);
|
||||
|
||||
return (srtp_err_status_ok);
|
||||
}
|
||||
|
||||
/*
|
||||
* aes_gcm_nss_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_nss_context_init(void *cv,
|
||||
const uint8_t *key)
|
||||
{
|
||||
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
|
||||
|
||||
c->dir = srtp_direction_any;
|
||||
|
||||
debug_print(srtp_mod_aes_gcm, "key: %s",
|
||||
srtp_octet_string_hex_string(key, c->key_size));
|
||||
|
||||
if (c->key) {
|
||||
PK11_FreeSymKey(c->key);
|
||||
c->key = NULL;
|
||||
}
|
||||
|
||||
PK11SlotInfo *slot = PK11_GetBestSlot(CKM_AES_GCM, NULL);
|
||||
if (!slot) {
|
||||
return (srtp_err_status_cipher_fail);
|
||||
}
|
||||
|
||||
/* explicitly cast away const of key */
|
||||
SECItem key_item = { siBuffer, (unsigned char *)(uintptr_t)key,
|
||||
c->key_size };
|
||||
c->key = PK11_ImportSymKey(slot, CKM_AES_GCM, PK11_OriginUnwrap,
|
||||
CKA_ENCRYPT, &key_item, NULL);
|
||||
PK11_FreeSlot(slot);
|
||||
|
||||
if (!c->key) {
|
||||
return (srtp_err_status_cipher_fail);
|
||||
}
|
||||
|
||||
return (srtp_err_status_ok);
|
||||
}
|
||||
|
||||
/*
|
||||
* aes_gcm_nss_set_iv(c, iv) sets the counter value to the exor of iv with
|
||||
* the offset
|
||||
*/
|
||||
static srtp_err_status_t srtp_aes_gcm_nss_set_iv(
|
||||
void *cv,
|
||||
uint8_t *iv,
|
||||
srtp_cipher_direction_t direction)
|
||||
{
|
||||
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
|
||||
|
||||
if (direction != srtp_direction_encrypt &&
|
||||
direction != srtp_direction_decrypt) {
|
||||
return (srtp_err_status_bad_param);
|
||||
}
|
||||
c->dir = direction;
|
||||
|
||||
debug_print(srtp_mod_aes_gcm, "setting iv: %s",
|
||||
srtp_octet_string_hex_string(iv, GCM_IV_LEN));
|
||||
|
||||
memcpy(c->iv, iv, GCM_IV_LEN);
|
||||
|
||||
return (srtp_err_status_ok);
|
||||
}
|
||||
|
||||
/*
|
||||
* 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_nss_set_aad(void *cv,
|
||||
const uint8_t *aad,
|
||||
size_t aad_len)
|
||||
{
|
||||
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
|
||||
|
||||
debug_print(srtp_mod_aes_gcm, "setting AAD: %s",
|
||||
srtp_octet_string_hex_string(aad, aad_len));
|
||||
|
||||
if (aad_len + c->aad_size > MAX_AD_SIZE) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
memcpy(c->aad + c->aad_size, aad, aad_len);
|
||||
c->aad_size += aad_len;
|
||||
|
||||
return (srtp_err_status_ok);
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_aes_gcm_nss_do_crypto(void *cv,
|
||||
bool encrypt,
|
||||
const uint8_t *src,
|
||||
size_t src_len,
|
||||
uint8_t *dst,
|
||||
size_t *dst_len)
|
||||
{
|
||||
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
|
||||
|
||||
c->params.pIv = c->iv;
|
||||
c->params.ulIvLen = GCM_IV_LEN;
|
||||
c->params.pAAD = c->aad;
|
||||
c->params.ulAADLen = c->aad_size;
|
||||
|
||||
// Reset AAD
|
||||
c->aad_size = 0;
|
||||
|
||||
unsigned int out_len = 0;
|
||||
int rv;
|
||||
SECItem param = { siBuffer, (unsigned char *)&c->params,
|
||||
sizeof(CK_GCM_PARAMS) };
|
||||
if (encrypt) {
|
||||
if (c->dir != srtp_direction_encrypt) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
if (*dst_len < src_len + c->tag_size) {
|
||||
return srtp_err_status_buffer_small;
|
||||
}
|
||||
|
||||
rv = PK11_Encrypt(c->key, CKM_AES_GCM, ¶m, dst, &out_len, *dst_len,
|
||||
src, src_len);
|
||||
} else {
|
||||
if (c->dir != srtp_direction_decrypt) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
if (src_len < c->tag_size) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
if (*dst_len < src_len - c->tag_size) {
|
||||
return srtp_err_status_buffer_small;
|
||||
}
|
||||
|
||||
rv = PK11_Decrypt(c->key, CKM_AES_GCM, ¶m, dst, &out_len, *dst_len,
|
||||
src, src_len);
|
||||
}
|
||||
*dst_len = out_len;
|
||||
srtp_err_status_t status = srtp_err_status_ok;
|
||||
if (rv != SECSuccess) {
|
||||
status = srtp_err_status_cipher_fail;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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_nss_encrypt(void *cv,
|
||||
const uint8_t *src,
|
||||
size_t src_len,
|
||||
uint8_t *dst,
|
||||
size_t *dst_len)
|
||||
{
|
||||
return srtp_aes_gcm_nss_do_crypto(cv, true, src, src_len, dst, dst_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_nss_decrypt(void *cv,
|
||||
const uint8_t *src,
|
||||
size_t src_len,
|
||||
uint8_t *dst,
|
||||
size_t *dst_len)
|
||||
{
|
||||
uint8_t tagbuf[16];
|
||||
uint8_t *non_null_dst_buf = dst;
|
||||
if (!non_null_dst_buf && (*dst_len == 0)) {
|
||||
non_null_dst_buf = tagbuf;
|
||||
*dst_len = sizeof(tagbuf);
|
||||
} else if (!non_null_dst_buf) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
srtp_err_status_t status = srtp_aes_gcm_nss_do_crypto(
|
||||
cv, false, src, src_len, non_null_dst_buf, dst_len);
|
||||
if (status != srtp_err_status_ok) {
|
||||
int err = PR_GetError();
|
||||
if (err == SEC_ERROR_BAD_DATA) {
|
||||
status = srtp_err_status_auth_fail;
|
||||
}
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/*
|
||||
* Name of this crypto engine
|
||||
*/
|
||||
static const char srtp_aes_gcm_128_nss_description[] = "AES-128 GCM using NSS";
|
||||
static const char srtp_aes_gcm_256_nss_description[] = "AES-256 GCM using NSS";
|
||||
|
||||
/*
|
||||
* This is the vector function table for this crypto engine.
|
||||
*/
|
||||
/* clang-format off */
|
||||
const srtp_cipher_type_t srtp_aes_gcm_128 = {
|
||||
srtp_aes_gcm_nss_alloc,
|
||||
srtp_aes_gcm_nss_dealloc,
|
||||
srtp_aes_gcm_nss_context_init,
|
||||
srtp_aes_gcm_nss_set_aad,
|
||||
srtp_aes_gcm_nss_encrypt,
|
||||
srtp_aes_gcm_nss_decrypt,
|
||||
srtp_aes_gcm_nss_set_iv,
|
||||
srtp_aes_gcm_128_nss_description,
|
||||
&srtp_aes_gcm_128_test_case_0,
|
||||
SRTP_AES_GCM_128
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
/*
|
||||
* This is the vector function table for this crypto engine.
|
||||
*/
|
||||
/* clang-format off */
|
||||
const srtp_cipher_type_t srtp_aes_gcm_256 = {
|
||||
srtp_aes_gcm_nss_alloc,
|
||||
srtp_aes_gcm_nss_dealloc,
|
||||
srtp_aes_gcm_nss_context_init,
|
||||
srtp_aes_gcm_nss_set_aad,
|
||||
srtp_aes_gcm_nss_encrypt,
|
||||
srtp_aes_gcm_nss_decrypt,
|
||||
srtp_aes_gcm_nss_set_iv,
|
||||
srtp_aes_gcm_256_nss_description,
|
||||
&srtp_aes_gcm_256_test_case_0,
|
||||
SRTP_AES_GCM_256
|
||||
};
|
||||
/* clang-format on */
|
||||
432
libsrtp/crypto/cipher/aes_gcm_ossl.c
Normal file
432
libsrtp/crypto/cipher/aes_gcm_ossl.c
Normal file
@@ -0,0 +1,432 @@
|
||||
/*
|
||||
* 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"
|
||||
#include "cipher_test_cases.h"
|
||||
|
||||
srtp_debug_module_t srtp_mod_aes_gcm = {
|
||||
false, /* debugging is off by default */
|
||||
"aes gcm" /* printable module name */
|
||||
};
|
||||
|
||||
/*
|
||||
* 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 16
|
||||
#define GCM_AUTH_TAG_LEN_8 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,
|
||||
size_t key_len,
|
||||
size_t tlen)
|
||||
{
|
||||
srtp_aes_gcm_ctx_t *gcm;
|
||||
|
||||
debug_print(srtp_mod_aes_gcm, "allocating cipher with key length %zu",
|
||||
key_len);
|
||||
debug_print(srtp_mod_aes_gcm, "allocating cipher with tag length %zu",
|
||||
tlen);
|
||||
|
||||
/*
|
||||
* Verify the key_len is valid for one of: AES-128/256
|
||||
*/
|
||||
if (key_len != SRTP_AES_GCM_128_KEY_LEN_WSALT &&
|
||||
key_len != SRTP_AES_GCM_256_KEY_LEN_WSALT) {
|
||||
return (srtp_err_status_bad_param);
|
||||
}
|
||||
|
||||
if (tlen != GCM_AUTH_TAG_LEN && tlen != GCM_AUTH_TAG_LEN_8) {
|
||||
return (srtp_err_status_bad_param);
|
||||
}
|
||||
|
||||
/* allocate memory a cipher of type aes_gcm */
|
||||
*c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
|
||||
if (*c == NULL) {
|
||||
return (srtp_err_status_alloc_fail);
|
||||
}
|
||||
|
||||
gcm = (srtp_aes_gcm_ctx_t *)srtp_crypto_alloc(sizeof(srtp_aes_gcm_ctx_t));
|
||||
if (gcm == NULL) {
|
||||
srtp_crypto_free(*c);
|
||||
*c = NULL;
|
||||
return (srtp_err_status_alloc_fail);
|
||||
}
|
||||
|
||||
gcm->ctx = EVP_CIPHER_CTX_new();
|
||||
if (gcm->ctx == NULL) {
|
||||
srtp_crypto_free(gcm);
|
||||
srtp_crypto_free(*c);
|
||||
*c = NULL;
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
|
||||
/* set pointers */
|
||||
(*c)->state = gcm;
|
||||
|
||||
/* setup cipher attributes */
|
||||
switch (key_len) {
|
||||
case SRTP_AES_GCM_128_KEY_LEN_WSALT:
|
||||
(*c)->type = &srtp_aes_gcm_128;
|
||||
(*c)->algorithm = SRTP_AES_GCM_128;
|
||||
gcm->key_size = SRTP_AES_128_KEY_LEN;
|
||||
gcm->tag_len = tlen;
|
||||
break;
|
||||
case SRTP_AES_GCM_256_KEY_LEN_WSALT:
|
||||
(*c)->type = &srtp_aes_gcm_256;
|
||||
(*c)->algorithm = SRTP_AES_GCM_256;
|
||||
gcm->key_size = SRTP_AES_256_KEY_LEN;
|
||||
gcm->tag_len = tlen;
|
||||
break;
|
||||
}
|
||||
|
||||
/* set key size */
|
||||
(*c)->key_len = key_len;
|
||||
|
||||
return (srtp_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_free(ctx->ctx);
|
||||
/* zeroize the key material */
|
||||
octet_string_set_to_zero(ctx, sizeof(srtp_aes_gcm_ctx_t));
|
||||
srtp_crypto_free(ctx);
|
||||
}
|
||||
|
||||
/* free memory */
|
||||
srtp_crypto_free(c);
|
||||
|
||||
return (srtp_err_status_ok);
|
||||
}
|
||||
|
||||
/*
|
||||
* 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)
|
||||
{
|
||||
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
|
||||
const EVP_CIPHER *evp;
|
||||
|
||||
c->dir = srtp_direction_any;
|
||||
|
||||
debug_print(srtp_mod_aes_gcm, "key: %s",
|
||||
srtp_octet_string_hex_string(key, c->key_size));
|
||||
|
||||
switch (c->key_size) {
|
||||
case SRTP_AES_256_KEY_LEN:
|
||||
evp = EVP_aes_256_gcm();
|
||||
break;
|
||||
case SRTP_AES_128_KEY_LEN:
|
||||
evp = EVP_aes_128_gcm();
|
||||
break;
|
||||
default:
|
||||
return (srtp_err_status_bad_param);
|
||||
break;
|
||||
}
|
||||
|
||||
EVP_CIPHER_CTX_reset(c->ctx);
|
||||
|
||||
if (!EVP_CipherInit_ex(c->ctx, evp, NULL, key, NULL, 0)) {
|
||||
return srtp_err_status_init_fail;
|
||||
}
|
||||
|
||||
if (!EVP_CIPHER_CTX_ctrl(c->ctx, EVP_CTRL_GCM_SET_IVLEN, 12, 0)) {
|
||||
return srtp_err_status_init_fail;
|
||||
}
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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)
|
||||
{
|
||||
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
|
||||
|
||||
if (direction != srtp_direction_encrypt &&
|
||||
direction != srtp_direction_decrypt) {
|
||||
return (srtp_err_status_bad_param);
|
||||
}
|
||||
c->dir = direction;
|
||||
|
||||
debug_print(srtp_mod_aes_gcm, "setting iv: %s",
|
||||
srtp_octet_string_hex_string(iv, 12));
|
||||
|
||||
if (c->dir == srtp_direction_encrypt) {
|
||||
if (EVP_EncryptInit_ex(c->ctx, NULL, NULL, NULL, iv) != 1) {
|
||||
return srtp_err_status_init_fail;
|
||||
}
|
||||
} else {
|
||||
if (EVP_DecryptInit_ex(c->ctx, NULL, NULL, NULL, iv) != 1) {
|
||||
return srtp_err_status_init_fail;
|
||||
}
|
||||
}
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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,
|
||||
size_t aad_len)
|
||||
{
|
||||
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
|
||||
int len = 0;
|
||||
|
||||
debug_print(srtp_mod_aes_gcm, "setting AAD: %s",
|
||||
srtp_octet_string_hex_string(aad, aad_len));
|
||||
|
||||
if (c->dir == srtp_direction_encrypt) {
|
||||
if (EVP_EncryptUpdate(c->ctx, NULL, &len, aad, aad_len) != 1) {
|
||||
return srtp_err_status_algo_fail;
|
||||
}
|
||||
} else {
|
||||
if (EVP_DecryptUpdate(c->ctx, NULL, &len, aad, aad_len) != 1) {
|
||||
return srtp_err_status_algo_fail;
|
||||
}
|
||||
}
|
||||
|
||||
if (len != (int)aad_len) {
|
||||
return srtp_err_status_algo_fail;
|
||||
}
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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,
|
||||
const uint8_t *src,
|
||||
size_t src_len,
|
||||
uint8_t *dst,
|
||||
size_t *dst_len)
|
||||
{
|
||||
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
|
||||
int len = 0;
|
||||
|
||||
if (c->dir != srtp_direction_encrypt) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
if (*dst_len < src_len + c->tag_len) {
|
||||
return srtp_err_status_buffer_small;
|
||||
}
|
||||
|
||||
/*
|
||||
* Encrypt the data
|
||||
*/
|
||||
if (EVP_EncryptUpdate(c->ctx, dst, &len, src, src_len) != 1) {
|
||||
return srtp_err_status_algo_fail;
|
||||
}
|
||||
*dst_len = len;
|
||||
|
||||
/*
|
||||
* Calculate the tag
|
||||
*/
|
||||
if (EVP_EncryptFinal_ex(c->ctx, dst + len, &len) != 1) {
|
||||
return srtp_err_status_algo_fail;
|
||||
}
|
||||
*dst_len += len;
|
||||
|
||||
/*
|
||||
* Retrieve the tag
|
||||
*/
|
||||
if (EVP_CIPHER_CTX_ctrl(c->ctx, EVP_CTRL_GCM_GET_TAG, c->tag_len,
|
||||
dst + *dst_len) != 1) {
|
||||
return srtp_err_status_algo_fail;
|
||||
}
|
||||
*dst_len += c->tag_len;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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,
|
||||
const uint8_t *src,
|
||||
size_t src_len,
|
||||
uint8_t *dst,
|
||||
size_t *dst_len)
|
||||
{
|
||||
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
|
||||
int len = 0;
|
||||
|
||||
if (c->dir != srtp_direction_decrypt) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
if (src_len < c->tag_len) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
if (*dst_len < src_len - c->tag_len) {
|
||||
return srtp_err_status_buffer_small;
|
||||
}
|
||||
|
||||
/*
|
||||
* Decrypt the data
|
||||
*/
|
||||
if (EVP_DecryptUpdate(c->ctx, dst, &len, src, src_len - c->tag_len) != 1) {
|
||||
return srtp_err_status_algo_fail;
|
||||
}
|
||||
*dst_len = len;
|
||||
|
||||
/*
|
||||
* Set the tag before decrypting
|
||||
*
|
||||
* explicitly cast away const of src
|
||||
*/
|
||||
if (EVP_CIPHER_CTX_ctrl(
|
||||
c->ctx, EVP_CTRL_GCM_SET_TAG, c->tag_len,
|
||||
(void *)(uintptr_t)(src + (src_len - c->tag_len))) != 1) {
|
||||
return srtp_err_status_algo_fail;
|
||||
}
|
||||
|
||||
/*
|
||||
* Check the tag
|
||||
*/
|
||||
if (EVP_DecryptFinal_ex(c->ctx, dst + *dst_len, &len) != 1) {
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
*dst_len += len;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* Name of this crypto engine
|
||||
*/
|
||||
static const char srtp_aes_gcm_128_openssl_description[] =
|
||||
"AES-128 GCM using openssl";
|
||||
static const char srtp_aes_gcm_256_openssl_description[] =
|
||||
"AES-256 GCM using openssl";
|
||||
|
||||
/*
|
||||
* This is the vector function table for this crypto engine.
|
||||
*/
|
||||
/* clang-format off */
|
||||
const srtp_cipher_type_t srtp_aes_gcm_128 = {
|
||||
srtp_aes_gcm_openssl_alloc,
|
||||
srtp_aes_gcm_openssl_dealloc,
|
||||
srtp_aes_gcm_openssl_context_init,
|
||||
srtp_aes_gcm_openssl_set_aad,
|
||||
srtp_aes_gcm_openssl_encrypt,
|
||||
srtp_aes_gcm_openssl_decrypt,
|
||||
srtp_aes_gcm_openssl_set_iv,
|
||||
srtp_aes_gcm_128_openssl_description,
|
||||
&srtp_aes_gcm_128_test_case_0,
|
||||
SRTP_AES_GCM_128
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
/*
|
||||
* This is the vector function table for this crypto engine.
|
||||
*/
|
||||
/* clang-format off */
|
||||
const srtp_cipher_type_t srtp_aes_gcm_256 = {
|
||||
srtp_aes_gcm_openssl_alloc,
|
||||
srtp_aes_gcm_openssl_dealloc,
|
||||
srtp_aes_gcm_openssl_context_init,
|
||||
srtp_aes_gcm_openssl_set_aad,
|
||||
srtp_aes_gcm_openssl_encrypt,
|
||||
srtp_aes_gcm_openssl_decrypt,
|
||||
srtp_aes_gcm_openssl_set_iv,
|
||||
srtp_aes_gcm_256_openssl_description,
|
||||
&srtp_aes_gcm_256_test_case_0,
|
||||
SRTP_AES_GCM_256
|
||||
};
|
||||
/* clang-format on */
|
||||
474
libsrtp/crypto/cipher/aes_gcm_wssl.c
Normal file
474
libsrtp/crypto/cipher/aes_gcm_wssl.c
Normal file
@@ -0,0 +1,474 @@
|
||||
/*
|
||||
* aes_gcm_wssl.c
|
||||
*
|
||||
* AES Galois Counter Mode using wolfSSL
|
||||
*
|
||||
* Sean Parkinson, wolfSSL
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* 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
|
||||
#ifndef WOLFSSL_USER_SETTINGS
|
||||
#include <wolfssl/options.h>
|
||||
#endif
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/wolfcrypt/aes.h>
|
||||
#include "aes_gcm.h"
|
||||
#include "alloc.h"
|
||||
#include "err.h" /* for srtp_debug */
|
||||
#include "crypto_types.h"
|
||||
#include "cipher_types.h"
|
||||
#include "cipher_test_cases.h"
|
||||
|
||||
srtp_debug_module_t srtp_mod_aes_gcm = {
|
||||
0, /* debugging is off by default */
|
||||
"aes gcm wssl" /* printable module name */
|
||||
};
|
||||
|
||||
/**
|
||||
* SRTP IV Formation for AES-GCM
|
||||
* https://tools.ietf.org/html/rfc7714#section-8.1
|
||||
* 0 0 0 0 0 0 0 0 0 0 1 1
|
||||
* 0 1 2 3 4 5 6 7 8 9 0 1
|
||||
* +--+--+--+--+--+--+--+--+--+--+--+--+
|
||||
* |00|00| SSRC | ROC | SEQ |---+
|
||||
* +--+--+--+--+--+--+--+--+--+--+--+--+ |
|
||||
* |
|
||||
* +--+--+--+--+--+--+--+--+--+--+--+--+ |
|
||||
* | Encryption Salt |->(+)
|
||||
* +--+--+--+--+--+--+--+--+--+--+--+--+ |
|
||||
* |
|
||||
* +--+--+--+--+--+--+--+--+--+--+--+--+ |
|
||||
* | Initialization Vector |<--+
|
||||
* +--+--+--+--+--+--+--+--+--+--+--+--+
|
||||
*
|
||||
* SRTCP IV Formation for AES-GCM
|
||||
* https://tools.ietf.org/html/rfc7714#section-9.1
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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 AES_BLOCK_SIZE
|
||||
#define GCM_AUTH_TAG_LEN_8 8
|
||||
|
||||
#define FUNC_ENTRY() debug_print(srtp_mod_aes_gcm, "%s entry", __func__);
|
||||
|
||||
/*
|
||||
* 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_wolfssl_alloc(srtp_cipher_t **c,
|
||||
size_t key_len,
|
||||
size_t tlen)
|
||||
{
|
||||
FUNC_ENTRY();
|
||||
srtp_aes_gcm_ctx_t *gcm;
|
||||
|
||||
debug_print(srtp_mod_aes_gcm, "allocating cipher with key length %zu",
|
||||
key_len);
|
||||
debug_print(srtp_mod_aes_gcm, "allocating cipher with tag length %zu",
|
||||
tlen);
|
||||
|
||||
/*
|
||||
* Verify the key_len is valid for one of: AES-128/256
|
||||
*/
|
||||
if (key_len != SRTP_AES_GCM_128_KEY_LEN_WSALT &&
|
||||
key_len != SRTP_AES_GCM_256_KEY_LEN_WSALT) {
|
||||
return (srtp_err_status_bad_param);
|
||||
}
|
||||
|
||||
if (tlen != GCM_AUTH_TAG_LEN && tlen != GCM_AUTH_TAG_LEN_8) {
|
||||
return (srtp_err_status_bad_param);
|
||||
}
|
||||
|
||||
/* allocate memory a cipher of type aes_gcm */
|
||||
*c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
|
||||
if (*c == NULL) {
|
||||
return (srtp_err_status_alloc_fail);
|
||||
}
|
||||
|
||||
gcm = (srtp_aes_gcm_ctx_t *)srtp_crypto_alloc(sizeof(srtp_aes_gcm_ctx_t));
|
||||
if (gcm == NULL) {
|
||||
srtp_crypto_free(*c);
|
||||
*c = NULL;
|
||||
return (srtp_err_status_alloc_fail);
|
||||
}
|
||||
gcm->ctx = NULL;
|
||||
|
||||
/* set pointers */
|
||||
(*c)->state = gcm;
|
||||
|
||||
/* setup cipher attributes */
|
||||
switch (key_len) {
|
||||
case SRTP_AES_GCM_128_KEY_LEN_WSALT:
|
||||
(*c)->type = &srtp_aes_gcm_128;
|
||||
(*c)->algorithm = SRTP_AES_GCM_128;
|
||||
gcm->key_size = SRTP_AES_128_KEY_LEN;
|
||||
gcm->tag_len = tlen;
|
||||
break;
|
||||
case SRTP_AES_GCM_256_KEY_LEN_WSALT:
|
||||
(*c)->type = &srtp_aes_gcm_256;
|
||||
(*c)->algorithm = SRTP_AES_GCM_256;
|
||||
gcm->key_size = SRTP_AES_256_KEY_LEN;
|
||||
gcm->tag_len = tlen;
|
||||
break;
|
||||
}
|
||||
|
||||
/* set key size */
|
||||
(*c)->key_len = key_len;
|
||||
|
||||
return (srtp_err_status_ok);
|
||||
}
|
||||
|
||||
/*
|
||||
* This function deallocates a GCM session
|
||||
*/
|
||||
static srtp_err_status_t srtp_aes_gcm_wolfssl_dealloc(srtp_cipher_t *c)
|
||||
{
|
||||
srtp_aes_gcm_ctx_t *ctx;
|
||||
FUNC_ENTRY();
|
||||
ctx = (srtp_aes_gcm_ctx_t *)c->state;
|
||||
if (ctx != NULL) {
|
||||
if (ctx->ctx != NULL) {
|
||||
wc_AesFree(ctx->ctx);
|
||||
srtp_crypto_free(ctx->ctx);
|
||||
}
|
||||
/* zeroize the key material */
|
||||
octet_string_set_to_zero(ctx, sizeof(srtp_aes_gcm_ctx_t));
|
||||
srtp_crypto_free(ctx);
|
||||
}
|
||||
|
||||
/* free memory */
|
||||
srtp_crypto_free(c);
|
||||
|
||||
return (srtp_err_status_ok);
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_aes_gcm_wolfssl_context_init(void *cv,
|
||||
const uint8_t *key)
|
||||
{
|
||||
FUNC_ENTRY();
|
||||
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
|
||||
int err;
|
||||
|
||||
c->dir = srtp_direction_any;
|
||||
#ifndef WOLFSSL_AESGCM_STREAM
|
||||
c->aad_size = 0;
|
||||
#endif
|
||||
|
||||
debug_print(srtp_mod_aes_gcm, "key: %s",
|
||||
srtp_octet_string_hex_string(key, c->key_size));
|
||||
switch (c->key_size) {
|
||||
case SRTP_AES_256_KEY_LEN:
|
||||
case SRTP_AES_128_KEY_LEN:
|
||||
break;
|
||||
default:
|
||||
return (srtp_err_status_bad_param);
|
||||
break;
|
||||
}
|
||||
|
||||
if (c->ctx == NULL) {
|
||||
c->ctx = (Aes *)srtp_crypto_alloc(sizeof(Aes));
|
||||
if (c->ctx == NULL) {
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
err = wc_AesInit(c->ctx, NULL, INVALID_DEVID);
|
||||
if (err < 0) {
|
||||
srtp_crypto_free(c->ctx);
|
||||
c->ctx = NULL;
|
||||
debug_print(srtp_mod_aes_gcm, "wolfSSL error code: %d", err);
|
||||
return srtp_err_status_init_fail;
|
||||
}
|
||||
}
|
||||
|
||||
err = wc_AesGcmSetKey(c->ctx, (const unsigned char *)key, c->key_size);
|
||||
if (err < 0) {
|
||||
debug_print(srtp_mod_aes_gcm, "wolfSSL error code: %d", err);
|
||||
return srtp_err_status_init_fail;
|
||||
}
|
||||
|
||||
return (srtp_err_status_ok);
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_aes_gcm_wolfssl_set_iv(
|
||||
void *cv,
|
||||
uint8_t *iv,
|
||||
srtp_cipher_direction_t direction)
|
||||
{
|
||||
FUNC_ENTRY();
|
||||
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
|
||||
#ifdef WOLFSSL_AESGCM_STREAM
|
||||
int err;
|
||||
#endif
|
||||
|
||||
if (direction != srtp_direction_encrypt &&
|
||||
direction != srtp_direction_decrypt) {
|
||||
return (srtp_err_status_bad_param);
|
||||
}
|
||||
c->dir = direction;
|
||||
|
||||
debug_print(srtp_mod_aes_gcm, "setting iv: %s",
|
||||
srtp_octet_string_hex_string(iv, GCM_NONCE_MID_SZ));
|
||||
#ifndef WOLFSSL_AESGCM_STREAM
|
||||
c->iv_len = GCM_NONCE_MID_SZ;
|
||||
memcpy(c->iv, iv, c->iv_len);
|
||||
|
||||
c->aad_size = 0;
|
||||
#else
|
||||
err = wc_AesGcmInit(c->ctx, NULL, 0, iv, GCM_NONCE_MID_SZ);
|
||||
if (err < 0) {
|
||||
debug_print(srtp_mod_aes_gcm, "wolfSSL error code: %d", err);
|
||||
return srtp_err_status_init_fail;
|
||||
}
|
||||
#endif
|
||||
|
||||
return (srtp_err_status_ok);
|
||||
}
|
||||
|
||||
/*
|
||||
* 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_wolfssl_set_aad(void *cv,
|
||||
const uint8_t *aad,
|
||||
size_t aad_len)
|
||||
{
|
||||
FUNC_ENTRY();
|
||||
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
|
||||
#ifdef WOLFSSL_AESGCM_STREAM
|
||||
int err;
|
||||
#endif
|
||||
|
||||
debug_print(srtp_mod_aes_gcm, "setting AAD: %s",
|
||||
srtp_octet_string_hex_string(aad, aad_len));
|
||||
|
||||
#ifndef WOLFSSL_AESGCM_STREAM
|
||||
if (aad_len + c->aad_size > MAX_AD_SIZE) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
memcpy(c->aad + c->aad_size, aad, aad_len);
|
||||
c->aad_size += aad_len;
|
||||
#else
|
||||
if (c->dir == srtp_direction_encrypt) {
|
||||
err = wc_AesGcmEncryptUpdate(c->ctx, NULL, NULL, 0, aad, aad_len);
|
||||
} else {
|
||||
err = wc_AesGcmDecryptUpdate(c->ctx, NULL, NULL, 0, aad, aad_len);
|
||||
}
|
||||
if (err < 0) {
|
||||
debug_print(srtp_mod_aes_gcm, "wolfSSL error code: %d", err);
|
||||
return srtp_err_status_algo_fail;
|
||||
}
|
||||
#endif
|
||||
|
||||
return (srtp_err_status_ok);
|
||||
}
|
||||
|
||||
/*
|
||||
* 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_wolfssl_encrypt(void *cv,
|
||||
const uint8_t *src,
|
||||
size_t src_len,
|
||||
uint8_t *dst,
|
||||
size_t *dst_len)
|
||||
{
|
||||
FUNC_ENTRY();
|
||||
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
|
||||
int err;
|
||||
|
||||
if (c->dir != srtp_direction_encrypt) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
if (*dst_len < src_len + c->tag_len) {
|
||||
return srtp_err_status_buffer_small;
|
||||
}
|
||||
|
||||
#ifndef WOLFSSL_AESGCM_STREAM
|
||||
// tag must always be 16 bytes when passed to wc_AesGcmEncrypt, can truncate
|
||||
// to c->tag_len after
|
||||
uint8_t tag[GCM_AUTH_TAG_LEN];
|
||||
err = wc_AesGcmEncrypt(c->ctx, dst, src, src_len, c->iv, c->iv_len, tag,
|
||||
sizeof(tag), c->aad, c->aad_size);
|
||||
c->aad_size = 0;
|
||||
if (err == 0) {
|
||||
memcpy(dst + src_len, tag, c->tag_len);
|
||||
}
|
||||
#else
|
||||
err = wc_AesGcmEncryptUpdate(c->ctx, dst, src, src_len, NULL, 0);
|
||||
if (err < 0) {
|
||||
debug_print(srtp_mod_aes_gcm, "wolfSSL error code: %d", err);
|
||||
return srtp_err_status_algo_fail;
|
||||
}
|
||||
err = wc_AesGcmEncryptFinal(c->ctx, dst + src_len, c->tag_len);
|
||||
#endif
|
||||
if (err < 0) {
|
||||
debug_print(srtp_mod_aes_gcm, "wolfSSL error code: %d", err);
|
||||
printf("wolfSSL error code: %d\n", err);
|
||||
return srtp_err_status_algo_fail;
|
||||
}
|
||||
|
||||
*dst_len = src_len + c->tag_len;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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_wolfssl_decrypt(void *cv,
|
||||
const uint8_t *src,
|
||||
size_t src_len,
|
||||
uint8_t *dst,
|
||||
size_t *dst_len)
|
||||
{
|
||||
FUNC_ENTRY();
|
||||
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
|
||||
int err;
|
||||
|
||||
if (c->dir != srtp_direction_decrypt) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
if (src_len < c->tag_len) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
if (*dst_len < src_len - c->tag_len) {
|
||||
return srtp_err_status_buffer_small;
|
||||
}
|
||||
|
||||
#ifndef WOLFSSL_AESGCM_STREAM
|
||||
debug_print(srtp_mod_aes_gcm, "AAD: %s",
|
||||
srtp_octet_string_hex_string(c->aad, c->aad_size));
|
||||
|
||||
err = wc_AesGcmDecrypt(c->ctx, dst, src, (src_len - c->tag_len), c->iv,
|
||||
c->iv_len, src + (src_len - c->tag_len), c->tag_len,
|
||||
c->aad, c->aad_size);
|
||||
c->aad_size = 0;
|
||||
#else
|
||||
err = wc_AesGcmDecryptUpdate(c->ctx, dst, src, (src_len - c->tag_len), NULL,
|
||||
0);
|
||||
if (err < 0) {
|
||||
debug_print(srtp_mod_aes_gcm, "wolfSSL error code: %d", err);
|
||||
return srtp_err_status_algo_fail;
|
||||
}
|
||||
err =
|
||||
wc_AesGcmDecryptFinal(c->ctx, src + (src_len - c->tag_len), c->tag_len);
|
||||
#endif
|
||||
if (err < 0) {
|
||||
debug_print(srtp_mod_aes_gcm, "wolfSSL error code: %d", err);
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
|
||||
/*
|
||||
* Reduce the buffer size by the tag length since the tag
|
||||
* is not part of the original payload
|
||||
*/
|
||||
*dst_len = src_len -= c->tag_len;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* Name of this crypto engine
|
||||
*/
|
||||
static const char srtp_aes_gcm_128_wolfssl_description[] =
|
||||
"AES-128 GCM using wolfssl";
|
||||
static const char srtp_aes_gcm_256_wolfssl_description[] =
|
||||
"AES-256 GCM using wolfssl";
|
||||
|
||||
/*
|
||||
* This is the vector function table for this crypto engine.
|
||||
*/
|
||||
/* clang-format off */
|
||||
const srtp_cipher_type_t srtp_aes_gcm_128 = {
|
||||
srtp_aes_gcm_wolfssl_alloc,
|
||||
srtp_aes_gcm_wolfssl_dealloc,
|
||||
srtp_aes_gcm_wolfssl_context_init,
|
||||
srtp_aes_gcm_wolfssl_set_aad,
|
||||
srtp_aes_gcm_wolfssl_encrypt,
|
||||
srtp_aes_gcm_wolfssl_decrypt,
|
||||
srtp_aes_gcm_wolfssl_set_iv,
|
||||
srtp_aes_gcm_128_wolfssl_description,
|
||||
&srtp_aes_gcm_128_test_case_0,
|
||||
SRTP_AES_GCM_128
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
/*
|
||||
* This is the vector function table for this crypto engine.
|
||||
*/
|
||||
/* clang-format off */
|
||||
const srtp_cipher_type_t srtp_aes_gcm_256 = {
|
||||
srtp_aes_gcm_wolfssl_alloc,
|
||||
srtp_aes_gcm_wolfssl_dealloc,
|
||||
srtp_aes_gcm_wolfssl_context_init,
|
||||
srtp_aes_gcm_wolfssl_set_aad,
|
||||
srtp_aes_gcm_wolfssl_encrypt,
|
||||
srtp_aes_gcm_wolfssl_decrypt,
|
||||
srtp_aes_gcm_wolfssl_set_iv,
|
||||
srtp_aes_gcm_256_wolfssl_description,
|
||||
&srtp_aes_gcm_256_test_case_0,
|
||||
SRTP_AES_GCM_256
|
||||
};
|
||||
/* clang-format on */
|
||||
449
libsrtp/crypto/cipher/aes_icm.c
Normal file
449
libsrtp/crypto/cipher/aes_icm.c
Normal file
@@ -0,0 +1,449 @@
|
||||
/*
|
||||
* aes_icm.c
|
||||
*
|
||||
* AES Integer Counter Mode
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-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
|
||||
|
||||
#define ALIGN_32 0
|
||||
|
||||
#include "aes_icm.h"
|
||||
#include "alloc.h"
|
||||
#include "cipher_types.h"
|
||||
#include "cipher_test_cases.h"
|
||||
|
||||
srtp_debug_module_t srtp_mod_aes_icm = {
|
||||
false, /* debugging is off by default */
|
||||
"aes icm" /* printable module name */
|
||||
};
|
||||
|
||||
/*
|
||||
* integer counter mode works as follows:
|
||||
*
|
||||
* 16 bits
|
||||
* <----->
|
||||
* +------+------+------+------+------+------+------+------+
|
||||
* | nonce | pakcet index | ctr |---+
|
||||
* +------+------+------+------+------+------+------+------+ |
|
||||
* |
|
||||
* +------+------+------+------+------+------+------+------+ v
|
||||
* | salt |000000|->(+)
|
||||
* +------+------+------+------+------+------+------+------+ |
|
||||
* |
|
||||
* +---------+
|
||||
* | encrypt |
|
||||
* +---------+
|
||||
* |
|
||||
* +------+------+------+------+------+------+------+------+ |
|
||||
* | keystream block |<--+
|
||||
* +------+------+------+------+------+------+------+------+
|
||||
*
|
||||
* All fields are big-endian
|
||||
*
|
||||
* ctr is the block counter, which increments from zero for
|
||||
* each packet (16 bits wide)
|
||||
*
|
||||
* packet index is distinct for each packet (48 bits wide)
|
||||
*
|
||||
* nonce can be distinct across many uses of the same key, or
|
||||
* can be a fixed value per key, or can be per-packet randomness
|
||||
* (64 bits)
|
||||
*
|
||||
*/
|
||||
|
||||
static srtp_err_status_t srtp_aes_icm_alloc(srtp_cipher_t **c,
|
||||
size_t key_len,
|
||||
size_t tlen)
|
||||
{
|
||||
srtp_aes_icm_ctx_t *icm;
|
||||
(void)tlen;
|
||||
|
||||
debug_print(srtp_mod_aes_icm, "allocating cipher with key length %zu",
|
||||
key_len);
|
||||
|
||||
/*
|
||||
* The check for key_len = 30/46 does not apply. Our usage
|
||||
* of aes functions with key_len = values other than 30
|
||||
* has not broken anything. Don't know what would be the
|
||||
* effect of skipping this check for srtp in general.
|
||||
*/
|
||||
if (key_len != SRTP_AES_ICM_128_KEY_LEN_WSALT &&
|
||||
key_len != SRTP_AES_ICM_256_KEY_LEN_WSALT) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
/* allocate memory a cipher of type aes_icm */
|
||||
*c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
|
||||
if (*c == NULL) {
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
|
||||
icm = (srtp_aes_icm_ctx_t *)srtp_crypto_alloc(sizeof(srtp_aes_icm_ctx_t));
|
||||
if (icm == NULL) {
|
||||
srtp_crypto_free(*c);
|
||||
*c = NULL;
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
|
||||
/* set pointers */
|
||||
(*c)->state = icm;
|
||||
|
||||
switch (key_len) {
|
||||
case SRTP_AES_ICM_256_KEY_LEN_WSALT:
|
||||
(*c)->algorithm = SRTP_AES_ICM_256;
|
||||
(*c)->type = &srtp_aes_icm_256;
|
||||
break;
|
||||
default:
|
||||
(*c)->algorithm = SRTP_AES_ICM_128;
|
||||
(*c)->type = &srtp_aes_icm_128;
|
||||
break;
|
||||
}
|
||||
|
||||
/* set key size */
|
||||
icm->key_size = key_len;
|
||||
(*c)->key_len = key_len;
|
||||
|
||||
return srtp_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(ctx, sizeof(srtp_aes_icm_ctx_t));
|
||||
srtp_crypto_free(ctx);
|
||||
}
|
||||
|
||||
/* free the cipher context */
|
||||
srtp_crypto_free(c);
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* aes_icm_context_init(...) initializes the aes_icm_context
|
||||
* using the value in key[].
|
||||
*
|
||||
* the key is the secret key
|
||||
*
|
||||
* the salt is unpredictable (but not necessarily secret) data which
|
||||
* randomizes the starting point in the keystream
|
||||
*/
|
||||
|
||||
static srtp_err_status_t srtp_aes_icm_context_init(void *cv, const uint8_t *key)
|
||||
{
|
||||
srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
|
||||
srtp_err_status_t status;
|
||||
size_t base_key_len, copy_len;
|
||||
|
||||
if (c->key_size == SRTP_AES_ICM_128_KEY_LEN_WSALT ||
|
||||
c->key_size == SRTP_AES_ICM_256_KEY_LEN_WSALT) {
|
||||
base_key_len = c->key_size - SRTP_SALT_LEN;
|
||||
} else {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
/*
|
||||
* set counter and initial values to 'offset' value, being careful not to
|
||||
* go past the end of the key buffer
|
||||
*/
|
||||
v128_set_to_zero(&c->counter);
|
||||
v128_set_to_zero(&c->offset);
|
||||
|
||||
copy_len = c->key_size - base_key_len;
|
||||
/* force last two octets of the offset to be left zero (for srtp
|
||||
* compatibility) */
|
||||
if (copy_len > SRTP_SALT_LEN) {
|
||||
copy_len = SRTP_SALT_LEN;
|
||||
}
|
||||
|
||||
memcpy(&c->counter, key + base_key_len, copy_len);
|
||||
memcpy(&c->offset, key + base_key_len, copy_len);
|
||||
|
||||
debug_print(srtp_mod_aes_icm, "key: %s",
|
||||
srtp_octet_string_hex_string(key, base_key_len));
|
||||
debug_print(srtp_mod_aes_icm, "offset: %s", v128_hex_string(&c->offset));
|
||||
|
||||
/* expand key */
|
||||
status =
|
||||
srtp_aes_expand_encryption_key(key, base_key_len, &c->expanded_key);
|
||||
if (status) {
|
||||
v128_set_to_zero(&c->counter);
|
||||
v128_set_to_zero(&c->offset);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* indicate that the keystream_buffer is empty */
|
||||
c->bytes_in_buffer = 0;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* aes_icm_set_iv(c, iv) sets the counter value to the exor of iv with
|
||||
* the offset
|
||||
*/
|
||||
|
||||
static srtp_err_status_t srtp_aes_icm_set_iv(void *cv,
|
||||
uint8_t *iv,
|
||||
srtp_cipher_direction_t direction)
|
||||
{
|
||||
srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
|
||||
v128_t nonce;
|
||||
(void)direction;
|
||||
|
||||
/* set nonce (for alignment) */
|
||||
v128_copy_octet_string(&nonce, iv);
|
||||
|
||||
debug_print(srtp_mod_aes_icm, "setting iv: %s", v128_hex_string(&nonce));
|
||||
|
||||
v128_xor(&c->counter, &c->offset, &nonce);
|
||||
|
||||
debug_print(srtp_mod_aes_icm, "set_counter: %s",
|
||||
v128_hex_string(&c->counter));
|
||||
|
||||
/* indicate that the keystream_buffer is empty */
|
||||
c->bytes_in_buffer = 0;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* aes_icm_advance(...) refills the keystream_buffer and
|
||||
* advances the block index of the sicm_context forward by one
|
||||
*
|
||||
* this is an internal, hopefully inlined function
|
||||
*/
|
||||
static void srtp_aes_icm_advance(srtp_aes_icm_ctx_t *c)
|
||||
{
|
||||
/* fill buffer with new keystream */
|
||||
v128_copy(&c->keystream_buffer, &c->counter);
|
||||
srtp_aes_encrypt(&c->keystream_buffer, &c->expanded_key);
|
||||
c->bytes_in_buffer = sizeof(v128_t);
|
||||
|
||||
debug_print(srtp_mod_aes_icm, "counter: %s",
|
||||
v128_hex_string(&c->counter));
|
||||
debug_print(srtp_mod_aes_icm, "ciphertext: %s",
|
||||
v128_hex_string(&c->keystream_buffer));
|
||||
|
||||
/* clock counter forward */
|
||||
if (!++(c->counter.v8[15])) {
|
||||
++(c->counter.v8[14]);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* icm_encrypt deals with the following cases:
|
||||
*
|
||||
* bytes_to_encr < bytes_in_buffer
|
||||
* - add keystream into data
|
||||
*
|
||||
* bytes_to_encr > bytes_in_buffer
|
||||
* - add keystream into data until keystream_buffer is depleted
|
||||
* - loop over blocks, filling keystream_buffer and then
|
||||
* adding keystream into data
|
||||
* - fill buffer then add in remaining (< 16) bytes of keystream
|
||||
*/
|
||||
|
||||
static srtp_err_status_t srtp_aes_icm_encrypt(void *cv,
|
||||
const uint8_t *src,
|
||||
size_t src_len,
|
||||
uint8_t *dst,
|
||||
size_t *dst_len)
|
||||
{
|
||||
srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
|
||||
size_t bytes_to_encr = src_len;
|
||||
uint32_t *b;
|
||||
const uint32_t *s;
|
||||
|
||||
if (*dst_len < src_len) {
|
||||
return srtp_err_status_buffer_small;
|
||||
}
|
||||
|
||||
*dst_len = src_len;
|
||||
|
||||
unsigned char *buf = dst;
|
||||
|
||||
/* check that there's enough segment left*/
|
||||
size_t bytes_of_new_keystream = bytes_to_encr - c->bytes_in_buffer;
|
||||
size_t blocks_of_new_keystream = (bytes_of_new_keystream + 15) >> 4;
|
||||
if ((blocks_of_new_keystream + htons(c->counter.v16[7])) > 0xffff) {
|
||||
return srtp_err_status_terminus;
|
||||
}
|
||||
|
||||
debug_print(srtp_mod_aes_icm, "block index: %d", htons(c->counter.v16[7]));
|
||||
if (bytes_to_encr <= c->bytes_in_buffer) {
|
||||
/* deal with odd case of small bytes_to_encr */
|
||||
for (size_t i = (sizeof(v128_t) - c->bytes_in_buffer);
|
||||
i < (sizeof(v128_t) - c->bytes_in_buffer + bytes_to_encr); i++) {
|
||||
*buf++ = *src++ ^ c->keystream_buffer.v8[i];
|
||||
}
|
||||
|
||||
c->bytes_in_buffer -= bytes_to_encr;
|
||||
|
||||
/* return now to avoid the main loop */
|
||||
return srtp_err_status_ok;
|
||||
|
||||
} else {
|
||||
/* encrypt bytes until the remaining data is 16-byte aligned */
|
||||
for (size_t i = (sizeof(v128_t) - c->bytes_in_buffer);
|
||||
i < sizeof(v128_t); i++) {
|
||||
*buf++ = *src++ ^ c->keystream_buffer.v8[i];
|
||||
}
|
||||
|
||||
bytes_to_encr -= c->bytes_in_buffer;
|
||||
c->bytes_in_buffer = 0;
|
||||
}
|
||||
|
||||
/* now loop over entire 16-byte blocks of keystream */
|
||||
for (size_t i = 0; i < (bytes_to_encr / sizeof(v128_t)); i++) {
|
||||
/* fill buffer with new keystream */
|
||||
srtp_aes_icm_advance(c);
|
||||
|
||||
/*
|
||||
* add keystream into the data buffer (this would be a lot faster
|
||||
* if we could assume 32-bit alignment!)
|
||||
*/
|
||||
|
||||
#if ALIGN_32
|
||||
b = (uint32_t *)buf;
|
||||
s = (const uint32_t *)src;
|
||||
*b++ = *s++ ^ c->keystream_buffer.v32[0];
|
||||
*b++ = *s++ ^ c->keystream_buffer.v32[1];
|
||||
*b++ = *s++ ^ c->keystream_buffer.v32[2];
|
||||
*b++ = *s++ ^ c->keystream_buffer.v32[3];
|
||||
buf = (uint8_t *)b;
|
||||
src = (const uint8_t *)s;
|
||||
#else
|
||||
if ((((uintptr_t)buf) & 0x03) != 0) {
|
||||
*buf++ = *src++ ^ c->keystream_buffer.v8[0];
|
||||
*buf++ = *src++ ^ c->keystream_buffer.v8[1];
|
||||
*buf++ = *src++ ^ c->keystream_buffer.v8[2];
|
||||
*buf++ = *src++ ^ c->keystream_buffer.v8[3];
|
||||
*buf++ = *src++ ^ c->keystream_buffer.v8[4];
|
||||
*buf++ = *src++ ^ c->keystream_buffer.v8[5];
|
||||
*buf++ = *src++ ^ c->keystream_buffer.v8[6];
|
||||
*buf++ = *src++ ^ c->keystream_buffer.v8[7];
|
||||
*buf++ = *src++ ^ c->keystream_buffer.v8[8];
|
||||
*buf++ = *src++ ^ c->keystream_buffer.v8[9];
|
||||
*buf++ = *src++ ^ c->keystream_buffer.v8[10];
|
||||
*buf++ = *src++ ^ c->keystream_buffer.v8[11];
|
||||
*buf++ = *src++ ^ c->keystream_buffer.v8[12];
|
||||
*buf++ = *src++ ^ c->keystream_buffer.v8[13];
|
||||
*buf++ = *src++ ^ c->keystream_buffer.v8[14];
|
||||
*buf++ = *src++ ^ c->keystream_buffer.v8[15];
|
||||
} else {
|
||||
b = (uint32_t *)buf;
|
||||
s = (const uint32_t *)src;
|
||||
*b++ = *s++ ^ c->keystream_buffer.v32[0];
|
||||
*b++ = *s++ ^ c->keystream_buffer.v32[1];
|
||||
*b++ = *s++ ^ c->keystream_buffer.v32[2];
|
||||
*b++ = *s++ ^ c->keystream_buffer.v32[3];
|
||||
buf = (uint8_t *)b;
|
||||
src = (const uint8_t *)s;
|
||||
}
|
||||
#endif /* #if ALIGN_32 */
|
||||
}
|
||||
|
||||
/* if there is a tail end of the data, process it */
|
||||
if ((bytes_to_encr & 0xf) != 0) {
|
||||
/* fill buffer with new keystream */
|
||||
srtp_aes_icm_advance(c);
|
||||
|
||||
for (size_t i = 0; i < (bytes_to_encr & 0xf); i++) {
|
||||
*buf++ = *src++ ^ c->keystream_buffer.v8[i];
|
||||
}
|
||||
|
||||
/* reset the keystream buffer size to right value */
|
||||
c->bytes_in_buffer = sizeof(v128_t) - (bytes_to_encr & 0xf);
|
||||
} else {
|
||||
/* no tail, so just reset the keystream buffer size to zero */
|
||||
c->bytes_in_buffer = 0;
|
||||
}
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static const char srtp_aes_icm_128_description[] =
|
||||
"AES-128 integer counter mode";
|
||||
static const char srtp_aes_icm_256_description[] =
|
||||
"AES-256 integer counter mode";
|
||||
|
||||
/*
|
||||
* note: the encrypt function is identical to the decrypt function
|
||||
*/
|
||||
|
||||
const srtp_cipher_type_t srtp_aes_icm_128 = {
|
||||
srtp_aes_icm_alloc, /* */
|
||||
srtp_aes_icm_dealloc, /* */
|
||||
srtp_aes_icm_context_init, /* */
|
||||
0, /* set_aad */
|
||||
srtp_aes_icm_encrypt, /* */
|
||||
srtp_aes_icm_encrypt, /* */
|
||||
srtp_aes_icm_set_iv, /* */
|
||||
srtp_aes_icm_128_description, /* */
|
||||
&srtp_aes_icm_128_test_case_0, /* */
|
||||
SRTP_AES_ICM_128 /* */
|
||||
};
|
||||
|
||||
const srtp_cipher_type_t srtp_aes_icm_256 = {
|
||||
srtp_aes_icm_alloc, /* */
|
||||
srtp_aes_icm_dealloc, /* */
|
||||
srtp_aes_icm_context_init, /* */
|
||||
0, /* set_aad */
|
||||
srtp_aes_icm_encrypt, /* */
|
||||
srtp_aes_icm_encrypt, /* */
|
||||
srtp_aes_icm_set_iv, /* */
|
||||
srtp_aes_icm_256_description, /* */
|
||||
&srtp_aes_icm_256_test_case_0, /* */
|
||||
SRTP_AES_ICM_256 /* */
|
||||
};
|
||||
402
libsrtp/crypto/cipher/aes_icm_mbedtls.c
Normal file
402
libsrtp/crypto/cipher/aes_icm_mbedtls.c
Normal file
@@ -0,0 +1,402 @@
|
||||
/*
|
||||
* aes_icm_mbedtls.c
|
||||
*
|
||||
* AES Integer Counter Mode
|
||||
*
|
||||
* YongCheng Yang
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* 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 <mbedtls/aes.h>
|
||||
#include "aes_icm_ext.h"
|
||||
#include "crypto_types.h"
|
||||
#include "err.h" /* for srtp_debug */
|
||||
#include "alloc.h"
|
||||
#include "cipher_types.h"
|
||||
#include "cipher_test_cases.h"
|
||||
|
||||
srtp_debug_module_t srtp_mod_aes_icm = {
|
||||
false, /* debugging is off by default */
|
||||
"aes icm mbedtls" /* printable module name */
|
||||
};
|
||||
|
||||
/*
|
||||
* static function declarations.
|
||||
*/
|
||||
static srtp_err_status_t srtp_aes_icm_mbedtls_alloc(srtp_cipher_t **c,
|
||||
size_t key_len,
|
||||
size_t tlen);
|
||||
|
||||
static srtp_err_status_t srtp_aes_icm_mbedtls_dealloc(srtp_cipher_t *c);
|
||||
|
||||
static srtp_err_status_t srtp_aes_icm_mbedtls_context_init(void *cv,
|
||||
const uint8_t *key);
|
||||
|
||||
static srtp_err_status_t srtp_aes_icm_mbedtls_set_iv(
|
||||
void *cv,
|
||||
uint8_t *iv,
|
||||
srtp_cipher_direction_t dir);
|
||||
|
||||
static srtp_err_status_t srtp_aes_icm_mbedtls_encrypt(void *cv,
|
||||
const uint8_t *src,
|
||||
size_t src_len,
|
||||
uint8_t *dst,
|
||||
size_t *dst_len);
|
||||
|
||||
/*
|
||||
* Name of this crypto engine
|
||||
*/
|
||||
static const char srtp_aes_icm_128_mbedtls_description[] =
|
||||
"AES-128 counter mode using mbedtls";
|
||||
static const char srtp_aes_icm_192_mbedtls_description[] =
|
||||
"AES-192 counter mode using mbedtls";
|
||||
static const char srtp_aes_icm_256_mbedtls_description[] =
|
||||
"AES-256 counter mode using mbedtls";
|
||||
|
||||
/*
|
||||
* This is the function table for this crypto engine.
|
||||
* note: the encrypt function is identical to the decrypt function
|
||||
*/
|
||||
const srtp_cipher_type_t srtp_aes_icm_128 = {
|
||||
srtp_aes_icm_mbedtls_alloc, /* */
|
||||
srtp_aes_icm_mbedtls_dealloc, /* */
|
||||
srtp_aes_icm_mbedtls_context_init, /* */
|
||||
0, /* set_aad */
|
||||
srtp_aes_icm_mbedtls_encrypt, /* */
|
||||
srtp_aes_icm_mbedtls_encrypt, /* */
|
||||
srtp_aes_icm_mbedtls_set_iv, /* */
|
||||
srtp_aes_icm_128_mbedtls_description, /* */
|
||||
&srtp_aes_icm_128_test_case_0, /* */
|
||||
SRTP_AES_ICM_128 /* */
|
||||
};
|
||||
|
||||
/*
|
||||
* This is the function table for this crypto engine.
|
||||
* note: the encrypt function is identical to the decrypt function
|
||||
*/
|
||||
const srtp_cipher_type_t srtp_aes_icm_192 = {
|
||||
srtp_aes_icm_mbedtls_alloc, /* */
|
||||
srtp_aes_icm_mbedtls_dealloc, /* */
|
||||
srtp_aes_icm_mbedtls_context_init, /* */
|
||||
0, /* set_aad */
|
||||
srtp_aes_icm_mbedtls_encrypt, /* */
|
||||
srtp_aes_icm_mbedtls_encrypt, /* */
|
||||
srtp_aes_icm_mbedtls_set_iv, /* */
|
||||
srtp_aes_icm_192_mbedtls_description, /* */
|
||||
&srtp_aes_icm_192_test_case_0, /* */
|
||||
SRTP_AES_ICM_192 /* */
|
||||
};
|
||||
|
||||
/*
|
||||
* This is the function table for this crypto engine.
|
||||
* note: the encrypt function is identical to the decrypt function
|
||||
*/
|
||||
const srtp_cipher_type_t srtp_aes_icm_256 = {
|
||||
srtp_aes_icm_mbedtls_alloc, /* */
|
||||
srtp_aes_icm_mbedtls_dealloc, /* */
|
||||
srtp_aes_icm_mbedtls_context_init, /* */
|
||||
0, /* set_aad */
|
||||
srtp_aes_icm_mbedtls_encrypt, /* */
|
||||
srtp_aes_icm_mbedtls_encrypt, /* */
|
||||
srtp_aes_icm_mbedtls_set_iv, /* */
|
||||
srtp_aes_icm_256_mbedtls_description, /* */
|
||||
&srtp_aes_icm_256_test_case_0, /* */
|
||||
SRTP_AES_ICM_256 /* */
|
||||
};
|
||||
|
||||
/*
|
||||
* integer counter mode works as follows:
|
||||
*
|
||||
* https://tools.ietf.org/html/rfc3711#section-4.1.1
|
||||
*
|
||||
* E(k, IV) || E(k, IV + 1 mod 2^128) || E(k, IV + 2 mod 2^128) ...
|
||||
* IV = (k_s * 2^16) XOR (SSRC * 2^64) XOR (i * 2^16)
|
||||
*
|
||||
* IV SHALL be defined by the SSRC, the SRTP packet index i,
|
||||
* and the SRTP session salting key k_s.
|
||||
*
|
||||
* SSRC: 32bits.
|
||||
* Sequence number: 16bits.
|
||||
* nonce is 64bits. .
|
||||
* packet index = ROC || SEQ. (ROC: Rollover counter)
|
||||
*
|
||||
* 16 bits
|
||||
* <----->
|
||||
* +------+------+------+------+------+------+------+------+
|
||||
* | nonce | packet index | ctr |---+
|
||||
* +------+------+------+------+------+------+------+------+ |
|
||||
* |
|
||||
* +------+------+------+------+------+------+------+------+ v
|
||||
* | salt |000000|->(+)
|
||||
* +------+------+------+------+------+------+------+------+ |
|
||||
* |
|
||||
* +---------+
|
||||
* | encrypt |
|
||||
* +---------+
|
||||
* |
|
||||
* +------+------+------+------+------+------+------+------+ |
|
||||
* | keystream block |<--+
|
||||
* +------+------+------+------+------+------+------+------+
|
||||
*
|
||||
* All fields are big-endian
|
||||
*
|
||||
* ctr is the block counter, which increments from zero for
|
||||
* each packet (16 bits wide)
|
||||
*
|
||||
* packet index is distinct for each packet (48 bits wide)
|
||||
*
|
||||
* nonce can be distinct across many uses of the same key, or
|
||||
* can be a fixed value per key, or can be per-packet randomness
|
||||
* (64 bits)
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This function allocates a new instance of this crypto engine.
|
||||
* The key_len parameter should be one of 30, 38, or 46 for
|
||||
* AES-128, AES-192, and AES-256 respectively. Note, this key_len
|
||||
* value is inflated, as it also accounts for the 112 bit salt
|
||||
* value. The tlen argument is for the AEAD tag length, which
|
||||
* isn't used in counter mode.
|
||||
*/
|
||||
static srtp_err_status_t srtp_aes_icm_mbedtls_alloc(srtp_cipher_t **c,
|
||||
size_t key_len,
|
||||
size_t tlen)
|
||||
{
|
||||
srtp_aes_icm_ctx_t *icm;
|
||||
(void)tlen;
|
||||
|
||||
debug_print(srtp_mod_aes_icm, "allocating cipher with key length %zu",
|
||||
key_len);
|
||||
|
||||
/*
|
||||
* Verify the key_len is valid for one of: AES-128/192/256
|
||||
*/
|
||||
if (key_len != SRTP_AES_ICM_128_KEY_LEN_WSALT &&
|
||||
key_len != SRTP_AES_ICM_192_KEY_LEN_WSALT &&
|
||||
key_len != SRTP_AES_ICM_256_KEY_LEN_WSALT) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
/* allocate memory a cipher of type aes_icm */
|
||||
*c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
|
||||
if (*c == NULL) {
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
|
||||
icm = (srtp_aes_icm_ctx_t *)srtp_crypto_alloc(sizeof(srtp_aes_icm_ctx_t));
|
||||
if (icm == NULL) {
|
||||
srtp_crypto_free(*c);
|
||||
*c = NULL;
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
|
||||
icm->ctx =
|
||||
(mbedtls_aes_context *)srtp_crypto_alloc(sizeof(mbedtls_aes_context));
|
||||
if (icm->ctx == NULL) {
|
||||
srtp_crypto_free(icm);
|
||||
srtp_crypto_free(*c);
|
||||
*c = NULL;
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
|
||||
mbedtls_aes_init(icm->ctx);
|
||||
|
||||
/* set pointers */
|
||||
(*c)->state = icm;
|
||||
|
||||
/* setup cipher parameters */
|
||||
switch (key_len) {
|
||||
case SRTP_AES_ICM_128_KEY_LEN_WSALT:
|
||||
(*c)->algorithm = SRTP_AES_ICM_128;
|
||||
(*c)->type = &srtp_aes_icm_128;
|
||||
icm->key_size = SRTP_AES_128_KEY_LEN;
|
||||
break;
|
||||
case SRTP_AES_ICM_192_KEY_LEN_WSALT:
|
||||
(*c)->algorithm = SRTP_AES_ICM_192;
|
||||
(*c)->type = &srtp_aes_icm_192;
|
||||
icm->key_size = SRTP_AES_192_KEY_LEN;
|
||||
break;
|
||||
case SRTP_AES_ICM_256_KEY_LEN_WSALT:
|
||||
(*c)->algorithm = SRTP_AES_ICM_256;
|
||||
(*c)->type = &srtp_aes_icm_256;
|
||||
icm->key_size = SRTP_AES_256_KEY_LEN;
|
||||
break;
|
||||
}
|
||||
|
||||
/* set key size */
|
||||
(*c)->key_len = key_len;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* This function deallocates an instance of this engine
|
||||
*/
|
||||
static srtp_err_status_t srtp_aes_icm_mbedtls_dealloc(srtp_cipher_t *c)
|
||||
{
|
||||
srtp_aes_icm_ctx_t *ctx;
|
||||
|
||||
if (c == NULL) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
/*
|
||||
* Free the aes context
|
||||
*/
|
||||
ctx = (srtp_aes_icm_ctx_t *)c->state;
|
||||
if (ctx != NULL) {
|
||||
mbedtls_aes_free(ctx->ctx);
|
||||
srtp_crypto_free(ctx->ctx);
|
||||
/* zeroize the key material */
|
||||
octet_string_set_to_zero(ctx, sizeof(srtp_aes_icm_ctx_t));
|
||||
srtp_crypto_free(ctx);
|
||||
}
|
||||
|
||||
/* free memory */
|
||||
srtp_crypto_free(c);
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_aes_icm_mbedtls_context_init(void *cv,
|
||||
const uint8_t *key)
|
||||
{
|
||||
srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
|
||||
uint32_t key_size_in_bits = (c->key_size << 3);
|
||||
int errcode = 0;
|
||||
|
||||
/*
|
||||
* set counter and initial values to 'offset' value, being careful not to
|
||||
* go past the end of the key buffer
|
||||
*/
|
||||
v128_set_to_zero(&c->counter);
|
||||
v128_set_to_zero(&c->offset);
|
||||
memcpy(&c->counter, key + c->key_size, SRTP_SALT_LEN);
|
||||
memcpy(&c->offset, key + c->key_size, SRTP_SALT_LEN);
|
||||
|
||||
/* force last two octets of the offset to zero (for srtp compatibility) */
|
||||
c->offset.v8[SRTP_SALT_LEN] = c->offset.v8[SRTP_SALT_LEN + 1] = 0;
|
||||
c->counter.v8[SRTP_SALT_LEN] = c->counter.v8[SRTP_SALT_LEN + 1] = 0;
|
||||
debug_print(srtp_mod_aes_icm, "key: %s",
|
||||
srtp_octet_string_hex_string(key, c->key_size));
|
||||
debug_print(srtp_mod_aes_icm, "offset: %s", v128_hex_string(&c->offset));
|
||||
|
||||
switch (c->key_size) {
|
||||
case SRTP_AES_256_KEY_LEN:
|
||||
case SRTP_AES_192_KEY_LEN:
|
||||
case SRTP_AES_128_KEY_LEN:
|
||||
break;
|
||||
default:
|
||||
return srtp_err_status_bad_param;
|
||||
break;
|
||||
}
|
||||
|
||||
errcode = mbedtls_aes_setkey_enc(c->ctx, key, key_size_in_bits);
|
||||
if (errcode != 0) {
|
||||
debug_print(srtp_mod_aes_icm, "errCode: %d", errcode);
|
||||
}
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* aes_icm_set_iv(c, iv) sets the counter value to the exor of iv with
|
||||
* the offset
|
||||
*/
|
||||
static srtp_err_status_t srtp_aes_icm_mbedtls_set_iv(
|
||||
void *cv,
|
||||
uint8_t *iv,
|
||||
srtp_cipher_direction_t dir)
|
||||
{
|
||||
srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
|
||||
v128_t nonce;
|
||||
(void)dir;
|
||||
|
||||
c->nc_off = 0;
|
||||
/* set nonce (for alignment) */
|
||||
v128_copy_octet_string(&nonce, iv);
|
||||
|
||||
debug_print(srtp_mod_aes_icm, "setting iv: %s", v128_hex_string(&nonce));
|
||||
|
||||
v128_xor(&c->counter, &c->offset, &nonce);
|
||||
|
||||
debug_print(srtp_mod_aes_icm, "set_counter: %s",
|
||||
v128_hex_string(&c->counter));
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* This function encrypts a buffer using AES CTR mode
|
||||
*
|
||||
* Parameters:
|
||||
* c Crypto context
|
||||
* buf data to encrypt
|
||||
* enc_len length of encrypt buffer
|
||||
*/
|
||||
static srtp_err_status_t srtp_aes_icm_mbedtls_encrypt(void *cv,
|
||||
const uint8_t *src,
|
||||
size_t src_len,
|
||||
uint8_t *dst,
|
||||
size_t *dst_len)
|
||||
{
|
||||
srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
|
||||
|
||||
int errCode = 0;
|
||||
debug_print(srtp_mod_aes_icm, "rs0: %s", v128_hex_string(&c->counter));
|
||||
|
||||
if (*dst_len < src_len) {
|
||||
return srtp_err_status_buffer_small;
|
||||
}
|
||||
|
||||
errCode =
|
||||
mbedtls_aes_crypt_ctr(c->ctx, src_len, &(c->nc_off), c->counter.v8,
|
||||
c->stream_block.v8, src, dst);
|
||||
if (errCode != 0) {
|
||||
debug_print(srtp_mod_aes_icm, "encrypt error: %d", errCode);
|
||||
return srtp_err_status_cipher_fail;
|
||||
}
|
||||
|
||||
*dst_len = src_len;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
415
libsrtp/crypto/cipher/aes_icm_nss.c
Normal file
415
libsrtp/crypto/cipher/aes_icm_nss.c
Normal file
@@ -0,0 +1,415 @@
|
||||
/*
|
||||
* aes_icm_nss.c
|
||||
*
|
||||
* AES Integer Counter Mode
|
||||
*
|
||||
* Richard L. Barnes
|
||||
* 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 "aes_icm_ext.h"
|
||||
#include "crypto_types.h"
|
||||
#include "err.h" /* for srtp_debug */
|
||||
#include "alloc.h"
|
||||
#include "cipher_types.h"
|
||||
#include "cipher_test_cases.h"
|
||||
|
||||
srtp_debug_module_t srtp_mod_aes_icm = {
|
||||
false, /* debugging is off by default */
|
||||
"aes icm nss" /* printable module name */
|
||||
};
|
||||
|
||||
/*
|
||||
* integer counter mode works as follows:
|
||||
*
|
||||
* 16 bits
|
||||
* <----->
|
||||
* +------+------+------+------+------+------+------+------+
|
||||
* | nonce | packet index | ctr |---+
|
||||
* +------+------+------+------+------+------+------+------+ |
|
||||
* |
|
||||
* +------+------+------+------+------+------+------+------+ v
|
||||
* | salt |000000|->(+)
|
||||
* +------+------+------+------+------+------+------+------+ |
|
||||
* |
|
||||
* +---------+
|
||||
* | encrypt |
|
||||
* +---------+
|
||||
* |
|
||||
* +------+------+------+------+------+------+------+------+ |
|
||||
* | keystream block |<--+
|
||||
* +------+------+------+------+------+------+------+------+
|
||||
*
|
||||
* All fields are big-endian
|
||||
*
|
||||
* ctr is the block counter, which increments from zero for
|
||||
* each packet (16 bits wide)
|
||||
*
|
||||
* packet index is distinct for each packet (48 bits wide)
|
||||
*
|
||||
* nonce can be distinct across many uses of the same key, or
|
||||
* can be a fixed value per key, or can be per-packet randomness
|
||||
* (64 bits)
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This function allocates a new instance of this crypto engine.
|
||||
* The key_len parameter should be one of 30, 38, or 46 for
|
||||
* AES-128, AES-192, and AES-256 respectively. Note, this key_len
|
||||
* value is inflated, as it also accounts for the 112 bit salt
|
||||
* value. The tlen argument is for the AEAD tag length, which
|
||||
* isn't used in counter mode.
|
||||
*/
|
||||
static srtp_err_status_t srtp_aes_icm_nss_alloc(srtp_cipher_t **c,
|
||||
size_t key_len,
|
||||
size_t tlen)
|
||||
{
|
||||
srtp_aes_icm_ctx_t *icm;
|
||||
NSSInitContext *nss;
|
||||
(void)tlen;
|
||||
|
||||
debug_print(srtp_mod_aes_icm, "allocating cipher with key length %zu",
|
||||
key_len);
|
||||
|
||||
/*
|
||||
* Verify the key_len is valid for one of: AES-128/192/256
|
||||
*/
|
||||
if (key_len != SRTP_AES_ICM_128_KEY_LEN_WSALT &&
|
||||
key_len != SRTP_AES_ICM_192_KEY_LEN_WSALT &&
|
||||
key_len != SRTP_AES_ICM_256_KEY_LEN_WSALT) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
/* Initialize NSS equiv of NSS_NoDB_Init(NULL) */
|
||||
nss = NSS_InitContext("", "", "", "", NULL,
|
||||
NSS_INIT_READONLY | NSS_INIT_NOCERTDB |
|
||||
NSS_INIT_NOMODDB | NSS_INIT_FORCEOPEN |
|
||||
NSS_INIT_OPTIMIZESPACE);
|
||||
if (!nss) {
|
||||
return (srtp_err_status_cipher_fail);
|
||||
}
|
||||
|
||||
/* allocate memory a cipher of type aes_icm */
|
||||
*c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
|
||||
if (*c == NULL) {
|
||||
NSS_ShutdownContext(nss);
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
|
||||
icm = (srtp_aes_icm_ctx_t *)srtp_crypto_alloc(sizeof(srtp_aes_icm_ctx_t));
|
||||
if (icm == NULL) {
|
||||
NSS_ShutdownContext(nss);
|
||||
srtp_crypto_free(*c);
|
||||
*c = NULL;
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
|
||||
icm->key = NULL;
|
||||
icm->ctx = NULL;
|
||||
icm->nss = nss;
|
||||
|
||||
/* set pointers */
|
||||
(*c)->state = icm;
|
||||
|
||||
/* setup cipher parameters */
|
||||
switch (key_len) {
|
||||
case SRTP_AES_ICM_128_KEY_LEN_WSALT:
|
||||
(*c)->algorithm = SRTP_AES_ICM_128;
|
||||
(*c)->type = &srtp_aes_icm_128;
|
||||
icm->key_size = SRTP_AES_128_KEY_LEN;
|
||||
break;
|
||||
case SRTP_AES_ICM_192_KEY_LEN_WSALT:
|
||||
(*c)->algorithm = SRTP_AES_ICM_192;
|
||||
(*c)->type = &srtp_aes_icm_192;
|
||||
icm->key_size = SRTP_AES_192_KEY_LEN;
|
||||
break;
|
||||
case SRTP_AES_ICM_256_KEY_LEN_WSALT:
|
||||
(*c)->algorithm = SRTP_AES_ICM_256;
|
||||
(*c)->type = &srtp_aes_icm_256;
|
||||
icm->key_size = SRTP_AES_256_KEY_LEN;
|
||||
break;
|
||||
}
|
||||
|
||||
/* set key size */
|
||||
(*c)->key_len = key_len;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* This function deallocates an instance of this engine
|
||||
*/
|
||||
static srtp_err_status_t srtp_aes_icm_nss_dealloc(srtp_cipher_t *c)
|
||||
{
|
||||
srtp_aes_icm_ctx_t *ctx;
|
||||
|
||||
ctx = (srtp_aes_icm_ctx_t *)c->state;
|
||||
if (ctx) {
|
||||
/* free any PK11 values that have been created */
|
||||
if (ctx->key) {
|
||||
PK11_FreeSymKey(ctx->key);
|
||||
ctx->key = NULL;
|
||||
}
|
||||
|
||||
if (ctx->ctx) {
|
||||
PK11_DestroyContext(ctx->ctx, PR_TRUE);
|
||||
ctx->ctx = NULL;
|
||||
}
|
||||
|
||||
if (ctx->nss) {
|
||||
NSS_ShutdownContext(ctx->nss);
|
||||
ctx->nss = NULL;
|
||||
}
|
||||
|
||||
/* zeroize everything */
|
||||
octet_string_set_to_zero(ctx, sizeof(srtp_aes_icm_ctx_t));
|
||||
srtp_crypto_free(ctx);
|
||||
}
|
||||
|
||||
/* free memory */
|
||||
srtp_crypto_free(c);
|
||||
|
||||
return (srtp_err_status_ok);
|
||||
}
|
||||
|
||||
/*
|
||||
* aes_icm_nss_context_init(...) initializes the aes_icm_context
|
||||
* using the value in key[].
|
||||
*
|
||||
* the key is the secret key
|
||||
*
|
||||
* the salt is unpredictable (but not necessarily secret) data which
|
||||
* randomizes the starting point in the keystream
|
||||
*/
|
||||
static srtp_err_status_t srtp_aes_icm_nss_context_init(void *cv,
|
||||
const uint8_t *key)
|
||||
{
|
||||
srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
|
||||
|
||||
/*
|
||||
* set counter and initial values to 'offset' value, being careful not to
|
||||
* go past the end of the key buffer
|
||||
*/
|
||||
v128_set_to_zero(&c->counter);
|
||||
v128_set_to_zero(&c->offset);
|
||||
memcpy(&c->counter, key + c->key_size, SRTP_SALT_LEN);
|
||||
memcpy(&c->offset, key + c->key_size, SRTP_SALT_LEN);
|
||||
|
||||
/* force last two octets of the offset to zero (for srtp compatibility) */
|
||||
c->offset.v8[SRTP_SALT_LEN] = c->offset.v8[SRTP_SALT_LEN + 1] = 0;
|
||||
c->counter.v8[SRTP_SALT_LEN] = c->counter.v8[SRTP_SALT_LEN + 1] = 0;
|
||||
|
||||
debug_print(srtp_mod_aes_icm, "key: %s",
|
||||
srtp_octet_string_hex_string(key, c->key_size));
|
||||
debug_print(srtp_mod_aes_icm, "offset: %s", v128_hex_string(&c->offset));
|
||||
|
||||
if (c->key) {
|
||||
PK11_FreeSymKey(c->key);
|
||||
c->key = NULL;
|
||||
}
|
||||
|
||||
PK11SlotInfo *slot = PK11_GetBestSlot(CKM_AES_CTR, NULL);
|
||||
if (!slot) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
/* explicitly cast away const of key */
|
||||
SECItem keyItem = { siBuffer, (unsigned char *)(uintptr_t)key,
|
||||
c->key_size };
|
||||
c->key = PK11_ImportSymKey(slot, CKM_AES_CTR, PK11_OriginUnwrap,
|
||||
CKA_ENCRYPT, &keyItem, NULL);
|
||||
PK11_FreeSlot(slot);
|
||||
|
||||
if (!c->key) {
|
||||
return srtp_err_status_cipher_fail;
|
||||
}
|
||||
|
||||
return (srtp_err_status_ok);
|
||||
}
|
||||
|
||||
/*
|
||||
* aes_icm_set_iv(c, iv) sets the counter value to the exor of iv with
|
||||
* the offset
|
||||
*/
|
||||
static srtp_err_status_t srtp_aes_icm_nss_set_iv(void *cv,
|
||||
uint8_t *iv,
|
||||
srtp_cipher_direction_t dir)
|
||||
{
|
||||
srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
|
||||
v128_t nonce;
|
||||
(void)dir;
|
||||
|
||||
/* set nonce (for alignment) */
|
||||
v128_copy_octet_string(&nonce, iv);
|
||||
|
||||
debug_print(srtp_mod_aes_icm, "setting iv: %s", v128_hex_string(&nonce));
|
||||
|
||||
v128_xor(&c->counter, &c->offset, &nonce);
|
||||
|
||||
debug_print(srtp_mod_aes_icm, "set_counter: %s",
|
||||
v128_hex_string(&c->counter));
|
||||
|
||||
/* set up the PK11 context now that we have all the info */
|
||||
CK_AES_CTR_PARAMS param;
|
||||
param.ulCounterBits = 16;
|
||||
memcpy(param.cb, &c->counter, 16);
|
||||
|
||||
if (!c->key) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
if (c->ctx) {
|
||||
PK11_DestroyContext(c->ctx, PR_TRUE);
|
||||
}
|
||||
|
||||
SECItem paramItem = { siBuffer, (unsigned char *)¶m,
|
||||
sizeof(CK_AES_CTR_PARAMS) };
|
||||
c->ctx = PK11_CreateContextBySymKey(CKM_AES_CTR, CKA_ENCRYPT, c->key,
|
||||
¶mItem);
|
||||
if (!c->ctx) {
|
||||
return srtp_err_status_cipher_fail;
|
||||
}
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* This function encrypts a buffer using AES CTR mode
|
||||
*
|
||||
* Parameters:
|
||||
* c Crypto context
|
||||
* buf data to encrypt
|
||||
* enc_len length of encrypt buffer
|
||||
*/
|
||||
static srtp_err_status_t srtp_aes_icm_nss_encrypt(void *cv,
|
||||
const uint8_t *src,
|
||||
size_t src_len,
|
||||
uint8_t *dst,
|
||||
size_t *dst_len)
|
||||
{
|
||||
srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
|
||||
|
||||
if (!c->ctx) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
if (dst_len == NULL) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
if (*dst_len < src_len) {
|
||||
return srtp_err_status_buffer_small;
|
||||
}
|
||||
|
||||
int out_len = 0;
|
||||
int rv = PK11_CipherOp(c->ctx, dst, &out_len, *dst_len, src, src_len);
|
||||
*dst_len = out_len;
|
||||
srtp_err_status_t status = srtp_err_status_ok;
|
||||
if (rv != SECSuccess) {
|
||||
status = srtp_err_status_cipher_fail;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/*
|
||||
* Name of this crypto engine
|
||||
*/
|
||||
static const char srtp_aes_icm_128_nss_description[] =
|
||||
"AES-128 counter mode using NSS";
|
||||
static const char srtp_aes_icm_192_nss_description[] =
|
||||
"AES-192 counter mode using NSS";
|
||||
static const char srtp_aes_icm_256_nss_description[] =
|
||||
"AES-256 counter mode using NSS";
|
||||
|
||||
/*
|
||||
* This is the function table for this crypto engine.
|
||||
* note: the encrypt function is identical to the decrypt function
|
||||
*/
|
||||
const srtp_cipher_type_t srtp_aes_icm_128 = {
|
||||
srtp_aes_icm_nss_alloc, /* */
|
||||
srtp_aes_icm_nss_dealloc, /* */
|
||||
srtp_aes_icm_nss_context_init, /* */
|
||||
0, /* set_aad */
|
||||
srtp_aes_icm_nss_encrypt, /* */
|
||||
srtp_aes_icm_nss_encrypt, /* */
|
||||
srtp_aes_icm_nss_set_iv, /* */
|
||||
srtp_aes_icm_128_nss_description, /* */
|
||||
&srtp_aes_icm_128_test_case_0, /* */
|
||||
SRTP_AES_ICM_128 /* */
|
||||
};
|
||||
|
||||
/*
|
||||
* This is the function table for this crypto engine.
|
||||
* note: the encrypt function is identical to the decrypt function
|
||||
*/
|
||||
const srtp_cipher_type_t srtp_aes_icm_192 = {
|
||||
srtp_aes_icm_nss_alloc, /* */
|
||||
srtp_aes_icm_nss_dealloc, /* */
|
||||
srtp_aes_icm_nss_context_init, /* */
|
||||
0, /* set_aad */
|
||||
srtp_aes_icm_nss_encrypt, /* */
|
||||
srtp_aes_icm_nss_encrypt, /* */
|
||||
srtp_aes_icm_nss_set_iv, /* */
|
||||
srtp_aes_icm_192_nss_description, /* */
|
||||
&srtp_aes_icm_192_test_case_0, /* */
|
||||
SRTP_AES_ICM_192 /* */
|
||||
};
|
||||
|
||||
/*
|
||||
* This is the function table for this crypto engine.
|
||||
* note: the encrypt function is identical to the decrypt function
|
||||
*/
|
||||
const srtp_cipher_type_t srtp_aes_icm_256 = {
|
||||
srtp_aes_icm_nss_alloc, /* */
|
||||
srtp_aes_icm_nss_dealloc, /* */
|
||||
srtp_aes_icm_nss_context_init, /* */
|
||||
0, /* set_aad */
|
||||
srtp_aes_icm_nss_encrypt, /* */
|
||||
srtp_aes_icm_nss_encrypt, /* */
|
||||
srtp_aes_icm_nss_set_iv, /* */
|
||||
srtp_aes_icm_256_nss_description, /* */
|
||||
&srtp_aes_icm_256_test_case_0, /* */
|
||||
SRTP_AES_ICM_256 /* */
|
||||
};
|
||||
391
libsrtp/crypto/cipher/aes_icm_ossl.c
Normal file
391
libsrtp/crypto/cipher/aes_icm_ossl.c
Normal file
@@ -0,0 +1,391 @@
|
||||
/*
|
||||
* aes_icm_ossl.c
|
||||
*
|
||||
* AES Integer Counter Mode
|
||||
*
|
||||
* John A. Foley
|
||||
* Cisco Systems, Inc.
|
||||
*
|
||||
* 2/24/2012: This module was modified to use CiscoSSL for AES counter
|
||||
* mode. Eddy Lem contributed the code to allow this.
|
||||
*
|
||||
* 12/20/2012: Added support for AES-192 and AES-256.
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* 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_icm_ext.h"
|
||||
#include "crypto_types.h"
|
||||
#include "err.h" /* for srtp_debug */
|
||||
#include "alloc.h"
|
||||
#include "cipher_types.h"
|
||||
#include "cipher_test_cases.h"
|
||||
|
||||
srtp_debug_module_t srtp_mod_aes_icm = {
|
||||
false, /* debugging is off by default */
|
||||
"aes icm ossl" /* printable module name */
|
||||
};
|
||||
|
||||
/*
|
||||
* integer counter mode works as follows:
|
||||
*
|
||||
* 16 bits
|
||||
* <----->
|
||||
* +------+------+------+------+------+------+------+------+
|
||||
* | nonce | packet index | ctr |---+
|
||||
* +------+------+------+------+------+------+------+------+ |
|
||||
* |
|
||||
* +------+------+------+------+------+------+------+------+ v
|
||||
* | salt |000000|->(+)
|
||||
* +------+------+------+------+------+------+------+------+ |
|
||||
* |
|
||||
* +---------+
|
||||
* | encrypt |
|
||||
* +---------+
|
||||
* |
|
||||
* +------+------+------+------+------+------+------+------+ |
|
||||
* | keystream block |<--+
|
||||
* +------+------+------+------+------+------+------+------+
|
||||
*
|
||||
* All fields are big-endian
|
||||
*
|
||||
* ctr is the block counter, which increments from zero for
|
||||
* each packet (16 bits wide)
|
||||
*
|
||||
* packet index is distinct for each packet (48 bits wide)
|
||||
*
|
||||
* nonce can be distinct across many uses of the same key, or
|
||||
* can be a fixed value per key, or can be per-packet randomness
|
||||
* (64 bits)
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This function allocates a new instance of this crypto engine.
|
||||
* The key_len parameter should be one of 30, 38, or 46 for
|
||||
* AES-128, AES-192, and AES-256 respectively. Note, this key_len
|
||||
* value is inflated, as it also accounts for the 112 bit salt
|
||||
* value. The tlen argument is for the AEAD tag length, which
|
||||
* isn't used in counter mode.
|
||||
*/
|
||||
static srtp_err_status_t srtp_aes_icm_openssl_alloc(srtp_cipher_t **c,
|
||||
size_t key_len,
|
||||
size_t tlen)
|
||||
{
|
||||
srtp_aes_icm_ctx_t *icm;
|
||||
(void)tlen;
|
||||
|
||||
debug_print(srtp_mod_aes_icm, "allocating cipher with key length %zu",
|
||||
key_len);
|
||||
|
||||
/*
|
||||
* Verify the key_len is valid for one of: AES-128/192/256
|
||||
*/
|
||||
if (key_len != SRTP_AES_ICM_128_KEY_LEN_WSALT &&
|
||||
key_len != SRTP_AES_ICM_192_KEY_LEN_WSALT &&
|
||||
key_len != SRTP_AES_ICM_256_KEY_LEN_WSALT) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
/* allocate memory a cipher of type aes_icm */
|
||||
*c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
|
||||
if (*c == NULL) {
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
|
||||
icm = (srtp_aes_icm_ctx_t *)srtp_crypto_alloc(sizeof(srtp_aes_icm_ctx_t));
|
||||
if (icm == NULL) {
|
||||
srtp_crypto_free(*c);
|
||||
*c = NULL;
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
|
||||
icm->ctx = EVP_CIPHER_CTX_new();
|
||||
if (icm->ctx == NULL) {
|
||||
srtp_crypto_free(icm);
|
||||
srtp_crypto_free(*c);
|
||||
*c = NULL;
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
|
||||
/* set pointers */
|
||||
(*c)->state = icm;
|
||||
|
||||
/* setup cipher parameters */
|
||||
switch (key_len) {
|
||||
case SRTP_AES_ICM_128_KEY_LEN_WSALT:
|
||||
(*c)->algorithm = SRTP_AES_ICM_128;
|
||||
(*c)->type = &srtp_aes_icm_128;
|
||||
icm->key_size = SRTP_AES_128_KEY_LEN;
|
||||
break;
|
||||
case SRTP_AES_ICM_192_KEY_LEN_WSALT:
|
||||
(*c)->algorithm = SRTP_AES_ICM_192;
|
||||
(*c)->type = &srtp_aes_icm_192;
|
||||
icm->key_size = SRTP_AES_192_KEY_LEN;
|
||||
break;
|
||||
case SRTP_AES_ICM_256_KEY_LEN_WSALT:
|
||||
(*c)->algorithm = SRTP_AES_ICM_256;
|
||||
(*c)->type = &srtp_aes_icm_256;
|
||||
icm->key_size = SRTP_AES_256_KEY_LEN;
|
||||
break;
|
||||
}
|
||||
|
||||
/* set key size */
|
||||
(*c)->key_len = key_len;
|
||||
|
||||
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_free(ctx->ctx);
|
||||
/* zeroize the key material */
|
||||
octet_string_set_to_zero(ctx, sizeof(srtp_aes_icm_ctx_t));
|
||||
srtp_crypto_free(ctx);
|
||||
}
|
||||
|
||||
/* free memory */
|
||||
srtp_crypto_free(c);
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* aes_icm_openssl_context_init(...) initializes the aes_icm_context
|
||||
* using the value in key[].
|
||||
*
|
||||
* the key is the secret key
|
||||
*
|
||||
* the salt is unpredictable (but not necessarily secret) data which
|
||||
* randomizes the starting point in the keystream
|
||||
*/
|
||||
static srtp_err_status_t srtp_aes_icm_openssl_context_init(void *cv,
|
||||
const uint8_t *key)
|
||||
{
|
||||
srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
|
||||
const EVP_CIPHER *evp;
|
||||
|
||||
/*
|
||||
* set counter and initial values to 'offset' value, being careful not to
|
||||
* go past the end of the key buffer
|
||||
*/
|
||||
v128_set_to_zero(&c->counter);
|
||||
v128_set_to_zero(&c->offset);
|
||||
memcpy(&c->counter, key + c->key_size, SRTP_SALT_LEN);
|
||||
memcpy(&c->offset, key + c->key_size, SRTP_SALT_LEN);
|
||||
|
||||
/* force last two octets of the offset to zero (for srtp compatibility) */
|
||||
c->offset.v8[SRTP_SALT_LEN] = c->offset.v8[SRTP_SALT_LEN + 1] = 0;
|
||||
c->counter.v8[SRTP_SALT_LEN] = c->counter.v8[SRTP_SALT_LEN + 1] = 0;
|
||||
|
||||
debug_print(srtp_mod_aes_icm, "key: %s",
|
||||
srtp_octet_string_hex_string(key, c->key_size));
|
||||
debug_print(srtp_mod_aes_icm, "offset: %s", v128_hex_string(&c->offset));
|
||||
|
||||
switch (c->key_size) {
|
||||
case SRTP_AES_256_KEY_LEN:
|
||||
evp = EVP_aes_256_ctr();
|
||||
break;
|
||||
case SRTP_AES_192_KEY_LEN:
|
||||
evp = EVP_aes_192_ctr();
|
||||
break;
|
||||
case SRTP_AES_128_KEY_LEN:
|
||||
evp = EVP_aes_128_ctr();
|
||||
break;
|
||||
default:
|
||||
return srtp_err_status_bad_param;
|
||||
break;
|
||||
}
|
||||
|
||||
EVP_CIPHER_CTX_reset(c->ctx);
|
||||
|
||||
if (!EVP_EncryptInit_ex(c->ctx, evp, NULL, key, NULL)) {
|
||||
return srtp_err_status_fail;
|
||||
}
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* aes_icm_set_iv(c, iv) sets the counter value to the exor of iv with
|
||||
* the offset
|
||||
*/
|
||||
static srtp_err_status_t srtp_aes_icm_openssl_set_iv(
|
||||
void *cv,
|
||||
uint8_t *iv,
|
||||
srtp_cipher_direction_t dir)
|
||||
{
|
||||
srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
|
||||
v128_t nonce;
|
||||
(void)dir;
|
||||
|
||||
/* set nonce (for alignment) */
|
||||
v128_copy_octet_string(&nonce, iv);
|
||||
|
||||
debug_print(srtp_mod_aes_icm, "setting iv: %s", v128_hex_string(&nonce));
|
||||
|
||||
v128_xor(&c->counter, &c->offset, &nonce);
|
||||
|
||||
debug_print(srtp_mod_aes_icm, "set_counter: %s",
|
||||
v128_hex_string(&c->counter));
|
||||
|
||||
if (!EVP_EncryptInit_ex(c->ctx, NULL, NULL, NULL, c->counter.v8)) {
|
||||
return srtp_err_status_fail;
|
||||
}
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* This function encrypts a buffer using AES CTR mode
|
||||
*
|
||||
* Parameters:
|
||||
* c Crypto context
|
||||
* buf data to encrypt
|
||||
* enc_len length of encrypt buffer
|
||||
*/
|
||||
static srtp_err_status_t srtp_aes_icm_openssl_encrypt(void *cv,
|
||||
const uint8_t *src,
|
||||
size_t src_len,
|
||||
uint8_t *dst,
|
||||
size_t *dst_len)
|
||||
{
|
||||
srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
|
||||
int len = 0;
|
||||
|
||||
debug_print(srtp_mod_aes_icm, "rs0: %s", v128_hex_string(&c->counter));
|
||||
|
||||
if (dst_len == NULL) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
if (*dst_len < src_len) {
|
||||
return srtp_err_status_buffer_small;
|
||||
}
|
||||
|
||||
if (!EVP_EncryptUpdate(c->ctx, dst, &len, src, src_len)) {
|
||||
return srtp_err_status_cipher_fail;
|
||||
}
|
||||
*dst_len = len;
|
||||
|
||||
if (!EVP_EncryptFinal_ex(c->ctx, dst + len, &len)) {
|
||||
return srtp_err_status_cipher_fail;
|
||||
}
|
||||
*dst_len += len;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* Name of this crypto engine
|
||||
*/
|
||||
static const char srtp_aes_icm_128_openssl_description[] =
|
||||
"AES-128 counter mode using openssl";
|
||||
static const char srtp_aes_icm_192_openssl_description[] =
|
||||
"AES-192 counter mode using openssl";
|
||||
static const char srtp_aes_icm_256_openssl_description[] =
|
||||
"AES-256 counter mode using openssl";
|
||||
|
||||
/*
|
||||
* This is the function table for this crypto engine.
|
||||
* note: the encrypt function is identical to the decrypt function
|
||||
*/
|
||||
const srtp_cipher_type_t srtp_aes_icm_128 = {
|
||||
srtp_aes_icm_openssl_alloc, /* */
|
||||
srtp_aes_icm_openssl_dealloc, /* */
|
||||
srtp_aes_icm_openssl_context_init, /* */
|
||||
0, /* set_aad */
|
||||
srtp_aes_icm_openssl_encrypt, /* */
|
||||
srtp_aes_icm_openssl_encrypt, /* */
|
||||
srtp_aes_icm_openssl_set_iv, /* */
|
||||
srtp_aes_icm_128_openssl_description, /* */
|
||||
&srtp_aes_icm_128_test_case_0, /* */
|
||||
SRTP_AES_ICM_128 /* */
|
||||
};
|
||||
|
||||
/*
|
||||
* This is the function table for this crypto engine.
|
||||
* note: the encrypt function is identical to the decrypt function
|
||||
*/
|
||||
const srtp_cipher_type_t srtp_aes_icm_192 = {
|
||||
srtp_aes_icm_openssl_alloc, /* */
|
||||
srtp_aes_icm_openssl_dealloc, /* */
|
||||
srtp_aes_icm_openssl_context_init, /* */
|
||||
0, /* set_aad */
|
||||
srtp_aes_icm_openssl_encrypt, /* */
|
||||
srtp_aes_icm_openssl_encrypt, /* */
|
||||
srtp_aes_icm_openssl_set_iv, /* */
|
||||
srtp_aes_icm_192_openssl_description, /* */
|
||||
&srtp_aes_icm_192_test_case_0, /* */
|
||||
SRTP_AES_ICM_192 /* */
|
||||
};
|
||||
|
||||
/*
|
||||
* This is the function table for this crypto engine.
|
||||
* note: the encrypt function is identical to the decrypt function
|
||||
*/
|
||||
const srtp_cipher_type_t srtp_aes_icm_256 = {
|
||||
srtp_aes_icm_openssl_alloc, /* */
|
||||
srtp_aes_icm_openssl_dealloc, /* */
|
||||
srtp_aes_icm_openssl_context_init, /* */
|
||||
0, /* set_aad */
|
||||
srtp_aes_icm_openssl_encrypt, /* */
|
||||
srtp_aes_icm_openssl_encrypt, /* */
|
||||
srtp_aes_icm_openssl_set_iv, /* */
|
||||
srtp_aes_icm_256_openssl_description, /* */
|
||||
&srtp_aes_icm_256_test_case_0, /* */
|
||||
SRTP_AES_ICM_256 /* */
|
||||
};
|
||||
398
libsrtp/crypto/cipher/aes_icm_wssl.c
Normal file
398
libsrtp/crypto/cipher/aes_icm_wssl.c
Normal file
@@ -0,0 +1,398 @@
|
||||
/*
|
||||
* aes_icm_wssl.c
|
||||
*
|
||||
* AES Integer Counter Mode using wolfSSL
|
||||
*
|
||||
* Sean Parkinson, wolfSSL
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* 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
|
||||
#ifndef WOLFSSL_USER_SETTINGS
|
||||
#include <wolfssl/options.h>
|
||||
#endif
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/wolfcrypt/aes.h>
|
||||
#include "aes_icm_ext.h"
|
||||
#include "crypto_types.h"
|
||||
#include "err.h" /* for srtp_debug */
|
||||
#include "alloc.h"
|
||||
#include "cipher_types.h"
|
||||
#include "cipher_test_cases.h"
|
||||
|
||||
srtp_debug_module_t srtp_mod_aes_icm = {
|
||||
0, /* debugging is off by default */
|
||||
"aes icm wssl" /* printable module name */
|
||||
};
|
||||
|
||||
/*
|
||||
* integer counter mode works as follows:
|
||||
*
|
||||
* https://tools.ietf.org/html/rfc3711#section-4.1.1
|
||||
*
|
||||
* E(k, IV) || E(k, IV + 1 mod 2^128) || E(k, IV + 2 mod 2^128) ...
|
||||
* IV = (k_s * 2^16) XOR (SSRC * 2^64) XOR (i * 2^16)
|
||||
*
|
||||
* IV SHALL be defined by the SSRC, the SRTP packet index i,
|
||||
* and the SRTP session salting key k_s.
|
||||
*
|
||||
* SSRC: 32bits.
|
||||
* Sequence number: 16bits.
|
||||
* nonce is 64bits. .
|
||||
* packet index = ROC || SEQ. (ROC: Rollover counter)
|
||||
*
|
||||
* 16 bits
|
||||
* <----->
|
||||
* +------+------+------+------+------+------+------+------+
|
||||
* | nonce | packet index | ctr |---+
|
||||
* +------+------+------+------+------+------+------+------+ |
|
||||
* |
|
||||
* +------+------+------+------+------+------+------+------+ v
|
||||
* | salt |000000|->(+)
|
||||
* +------+------+------+------+------+------+------+------+ |
|
||||
* |
|
||||
* +---------+
|
||||
* | encrypt |
|
||||
* +---------+
|
||||
* |
|
||||
* +------+------+------+------+------+------+------+------+ |
|
||||
* | keystream block |<--+
|
||||
* +------+------+------+------+------+------+------+------+
|
||||
*
|
||||
* All fields are big-endian
|
||||
*
|
||||
* ctr is the block counter, which increments from zero for
|
||||
* each packet (16 bits wide)
|
||||
*
|
||||
* packet index is distinct for each packet (48 bits wide)
|
||||
*
|
||||
* nonce can be distinct across many uses of the same key, or
|
||||
* can be a fixed value per key, or can be per-packet randomness
|
||||
* (64 bits)
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This function allocates a new instance of this crypto engine.
|
||||
* The key_len parameter should be one of 30, 38, or 46 for
|
||||
* AES-128, AES-192, and AES-256 respectively. Note, this key_len
|
||||
* value is inflated, as it also accounts for the 112 bit salt
|
||||
* value. The tlen argument is for the AEAD tag length, which
|
||||
* isn't used in counter mode.
|
||||
*/
|
||||
static srtp_err_status_t srtp_aes_icm_wolfssl_alloc(srtp_cipher_t **c,
|
||||
size_t key_len,
|
||||
size_t tlen)
|
||||
{
|
||||
srtp_aes_icm_ctx_t *icm;
|
||||
(void)tlen;
|
||||
|
||||
debug_print(srtp_mod_aes_icm, "allocating cipher with key length %zu",
|
||||
key_len);
|
||||
|
||||
/*
|
||||
* Verify the key_len is valid for one of: AES-128/192/256
|
||||
*/
|
||||
if (key_len != SRTP_AES_ICM_128_KEY_LEN_WSALT &&
|
||||
key_len != SRTP_AES_ICM_192_KEY_LEN_WSALT &&
|
||||
key_len != SRTP_AES_ICM_256_KEY_LEN_WSALT) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
/* allocate memory a cipher of type aes_icm */
|
||||
*c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
|
||||
if (*c == NULL) {
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
|
||||
icm = (srtp_aes_icm_ctx_t *)srtp_crypto_alloc(sizeof(srtp_aes_icm_ctx_t));
|
||||
if (icm == NULL) {
|
||||
srtp_crypto_free(*c);
|
||||
*c = NULL;
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
icm->ctx = NULL;
|
||||
|
||||
(*c)->state = icm;
|
||||
|
||||
/* setup cipher parameters */
|
||||
switch (key_len) {
|
||||
case SRTP_AES_ICM_128_KEY_LEN_WSALT:
|
||||
(*c)->algorithm = SRTP_AES_ICM_128;
|
||||
(*c)->type = &srtp_aes_icm_128;
|
||||
icm->key_size = SRTP_AES_128_KEY_LEN;
|
||||
break;
|
||||
case SRTP_AES_ICM_192_KEY_LEN_WSALT:
|
||||
(*c)->algorithm = SRTP_AES_ICM_192;
|
||||
(*c)->type = &srtp_aes_icm_192;
|
||||
icm->key_size = SRTP_AES_192_KEY_LEN;
|
||||
break;
|
||||
case SRTP_AES_ICM_256_KEY_LEN_WSALT:
|
||||
(*c)->algorithm = SRTP_AES_ICM_256;
|
||||
(*c)->type = &srtp_aes_icm_256;
|
||||
icm->key_size = SRTP_AES_256_KEY_LEN;
|
||||
break;
|
||||
}
|
||||
|
||||
/* set key size */
|
||||
(*c)->key_len = key_len;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* This function deallocates an instance of this engine
|
||||
*/
|
||||
static srtp_err_status_t srtp_aes_icm_wolfssl_dealloc(srtp_cipher_t *c)
|
||||
{
|
||||
srtp_aes_icm_ctx_t *ctx;
|
||||
|
||||
if (c == NULL) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
/*
|
||||
* Free the aes context
|
||||
*/
|
||||
ctx = (srtp_aes_icm_ctx_t *)c->state;
|
||||
if (ctx != NULL) {
|
||||
if (ctx->ctx != NULL) {
|
||||
wc_AesFree(ctx->ctx);
|
||||
srtp_crypto_free(ctx->ctx);
|
||||
}
|
||||
/* zeroize the key material */
|
||||
octet_string_set_to_zero(ctx, sizeof(srtp_aes_icm_ctx_t));
|
||||
srtp_crypto_free(ctx);
|
||||
}
|
||||
|
||||
/* free memory */
|
||||
srtp_crypto_free(c);
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_aes_icm_wolfssl_context_init(void *cv,
|
||||
const uint8_t *key)
|
||||
{
|
||||
srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
|
||||
int err;
|
||||
|
||||
if (c->ctx == NULL) {
|
||||
c->ctx = (Aes *)srtp_crypto_alloc(sizeof(Aes));
|
||||
if (c->ctx == NULL) {
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
|
||||
err = wc_AesInit(c->ctx, NULL, INVALID_DEVID);
|
||||
if (err < 0) {
|
||||
debug_print(srtp_mod_aes_icm, "wolfSSL error code: %d", err);
|
||||
srtp_crypto_free(c->ctx);
|
||||
c->ctx = NULL;
|
||||
return srtp_err_status_init_fail;
|
||||
}
|
||||
}
|
||||
|
||||
/* set pointers */
|
||||
/*
|
||||
* set counter and initial values to 'offset' value, being careful not to
|
||||
* go past the end of the key buffer
|
||||
*/
|
||||
v128_set_to_zero(&c->counter);
|
||||
v128_set_to_zero(&c->offset);
|
||||
memcpy(&c->counter, key + c->key_size, SRTP_SALT_LEN);
|
||||
memcpy(&c->offset, key + c->key_size, SRTP_SALT_LEN);
|
||||
|
||||
/* force last two octets of the offset to zero (for srtp compatibility) */
|
||||
c->offset.v8[SRTP_SALT_LEN] = c->offset.v8[SRTP_SALT_LEN + 1] = 0;
|
||||
c->counter.v8[SRTP_SALT_LEN] = c->counter.v8[SRTP_SALT_LEN + 1] = 0;
|
||||
debug_print(srtp_mod_aes_icm, "key: %s",
|
||||
srtp_octet_string_hex_string(key, c->key_size));
|
||||
debug_print(srtp_mod_aes_icm, "offset: %s", v128_hex_string(&c->offset));
|
||||
|
||||
switch (c->key_size) {
|
||||
case SRTP_AES_256_KEY_LEN:
|
||||
case SRTP_AES_192_KEY_LEN:
|
||||
case SRTP_AES_128_KEY_LEN:
|
||||
break;
|
||||
default:
|
||||
return srtp_err_status_bad_param;
|
||||
break;
|
||||
}
|
||||
|
||||
/* Counter mode always encrypts. */
|
||||
err = wc_AesSetKey(c->ctx, key, c->key_size, NULL, AES_ENCRYPTION);
|
||||
if (err < 0) {
|
||||
debug_print(srtp_mod_aes_icm, "wolfSSL error code: %d", err);
|
||||
return srtp_err_status_fail;
|
||||
}
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* aes_icm_set_iv(c, iv) sets the counter value to the exor of iv with
|
||||
* the offset
|
||||
*/
|
||||
static srtp_err_status_t srtp_aes_icm_wolfssl_set_iv(
|
||||
void *cv,
|
||||
uint8_t *iv,
|
||||
srtp_cipher_direction_t dir)
|
||||
{
|
||||
srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
|
||||
v128_t nonce;
|
||||
int err;
|
||||
(void)dir;
|
||||
|
||||
/* set nonce (for alignment) */
|
||||
v128_copy_octet_string(&nonce, iv);
|
||||
|
||||
debug_print(srtp_mod_aes_icm, "setting iv: %s", v128_hex_string(&nonce));
|
||||
|
||||
v128_xor(&c->counter, &c->offset, &nonce);
|
||||
|
||||
debug_print(srtp_mod_aes_icm, "set_counter: %s",
|
||||
v128_hex_string(&c->counter));
|
||||
|
||||
err = wc_AesSetIV(c->ctx, c->counter.v8);
|
||||
if (err < 0) {
|
||||
debug_print(srtp_mod_aes_icm, "wolfSSL error code: %d", err);
|
||||
return srtp_err_status_fail;
|
||||
}
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* This function encrypts a buffer using AES CTR mode
|
||||
*
|
||||
* Parameters:
|
||||
* c Crypto context
|
||||
* buf data to encrypt
|
||||
* enc_len length of encrypt buffer
|
||||
*/
|
||||
static srtp_err_status_t srtp_aes_icm_wolfssl_encrypt(void *cv,
|
||||
const uint8_t *src,
|
||||
size_t src_len,
|
||||
uint8_t *dst,
|
||||
size_t *dst_len)
|
||||
{
|
||||
srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
|
||||
|
||||
int err;
|
||||
debug_print(srtp_mod_aes_icm, "rs0: %s", v128_hex_string(&c->counter));
|
||||
|
||||
if (dst_len == NULL) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
if (*dst_len < src_len) {
|
||||
return srtp_err_status_buffer_small;
|
||||
}
|
||||
|
||||
err = wc_AesCtrEncrypt(c->ctx, dst, src, src_len);
|
||||
if (err < 0) {
|
||||
debug_print(srtp_mod_aes_icm, "wolfSSL encrypt error: %d", err);
|
||||
return srtp_err_status_cipher_fail;
|
||||
}
|
||||
*dst_len = src_len;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* Name of this crypto engine
|
||||
*/
|
||||
static const char srtp_aes_icm_128_wolfssl_description[] =
|
||||
"AES-128 counter mode using wolfSSL";
|
||||
static const char srtp_aes_icm_192_wolfssl_description[] =
|
||||
"AES-192 counter mode using wolfSSL";
|
||||
static const char srtp_aes_icm_256_wolfssl_description[] =
|
||||
"AES-256 counter mode using wolfSSL";
|
||||
|
||||
/*
|
||||
* This is the function table for this crypto engine.
|
||||
* note: the encrypt function is identical to the decrypt function
|
||||
*/
|
||||
const srtp_cipher_type_t srtp_aes_icm_128 = {
|
||||
srtp_aes_icm_wolfssl_alloc, /* */
|
||||
srtp_aes_icm_wolfssl_dealloc, /* */
|
||||
srtp_aes_icm_wolfssl_context_init, /* */
|
||||
0, /* set_aad */
|
||||
srtp_aes_icm_wolfssl_encrypt, /* */
|
||||
srtp_aes_icm_wolfssl_encrypt, /* */
|
||||
srtp_aes_icm_wolfssl_set_iv, /* */
|
||||
srtp_aes_icm_128_wolfssl_description, /* */
|
||||
&srtp_aes_icm_128_test_case_0, /* */
|
||||
SRTP_AES_ICM_128 /* */
|
||||
};
|
||||
|
||||
/*
|
||||
* This is the function table for this crypto engine.
|
||||
* note: the encrypt function is identical to the decrypt function
|
||||
*/
|
||||
const srtp_cipher_type_t srtp_aes_icm_192 = {
|
||||
srtp_aes_icm_wolfssl_alloc, /* */
|
||||
srtp_aes_icm_wolfssl_dealloc, /* */
|
||||
srtp_aes_icm_wolfssl_context_init, /* */
|
||||
0, /* set_aad */
|
||||
srtp_aes_icm_wolfssl_encrypt, /* */
|
||||
srtp_aes_icm_wolfssl_encrypt, /* */
|
||||
srtp_aes_icm_wolfssl_set_iv, /* */
|
||||
srtp_aes_icm_192_wolfssl_description, /* */
|
||||
&srtp_aes_icm_192_test_case_0, /* */
|
||||
SRTP_AES_ICM_192 /* */
|
||||
};
|
||||
|
||||
/*
|
||||
* This is the function table for this crypto engine.
|
||||
* note: the encrypt function is identical to the decrypt function
|
||||
*/
|
||||
const srtp_cipher_type_t srtp_aes_icm_256 = {
|
||||
srtp_aes_icm_wolfssl_alloc, /* */
|
||||
srtp_aes_icm_wolfssl_dealloc, /* */
|
||||
srtp_aes_icm_wolfssl_context_init, /* */
|
||||
0, /* set_aad */
|
||||
srtp_aes_icm_wolfssl_encrypt, /* */
|
||||
srtp_aes_icm_wolfssl_encrypt, /* */
|
||||
srtp_aes_icm_wolfssl_set_iv, /* */
|
||||
srtp_aes_icm_256_wolfssl_description, /* */
|
||||
&srtp_aes_icm_256_test_case_0, /* */
|
||||
SRTP_AES_ICM_256 /* */
|
||||
};
|
||||
652
libsrtp/crypto/cipher/cipher.c
Normal file
652
libsrtp/crypto/cipher/cipher.c
Normal file
@@ -0,0 +1,652 @@
|
||||
/*
|
||||
* cipher.c
|
||||
*
|
||||
* cipher meta-functions
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-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 "cipher.h"
|
||||
#include "cipher_priv.h"
|
||||
#include "crypto_types.h"
|
||||
#include "err.h" /* for srtp_debug */
|
||||
#include "alloc.h" /* for crypto_alloc(), crypto_free() */
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
srtp_debug_module_t srtp_mod_cipher = {
|
||||
false, /* debugging is off by default */
|
||||
"cipher" /* printable module name */
|
||||
};
|
||||
|
||||
srtp_err_status_t srtp_cipher_type_alloc(const srtp_cipher_type_t *ct,
|
||||
srtp_cipher_t **c,
|
||||
size_t key_len,
|
||||
size_t tlen)
|
||||
{
|
||||
if (!ct || !ct->alloc) {
|
||||
return (srtp_err_status_bad_param);
|
||||
}
|
||||
return ((ct)->alloc((c), (key_len), (tlen)));
|
||||
}
|
||||
|
||||
srtp_err_status_t srtp_cipher_dealloc(srtp_cipher_t *c)
|
||||
{
|
||||
if (!c || !c->type) {
|
||||
return (srtp_err_status_bad_param);
|
||||
}
|
||||
return (((c)->type)->dealloc(c));
|
||||
}
|
||||
|
||||
srtp_err_status_t srtp_cipher_init(srtp_cipher_t *c, const uint8_t *key)
|
||||
{
|
||||
if (!c || !c->type || !c->state) {
|
||||
return (srtp_err_status_bad_param);
|
||||
}
|
||||
return (((c)->type)->init(((c)->state), (key)));
|
||||
}
|
||||
|
||||
srtp_err_status_t srtp_cipher_set_iv(srtp_cipher_t *c,
|
||||
uint8_t *iv,
|
||||
srtp_cipher_direction_t direction)
|
||||
{
|
||||
if (!c || !c->type || !c->state) {
|
||||
return (srtp_err_status_bad_param);
|
||||
}
|
||||
|
||||
return (((c)->type)->set_iv(((c)->state), iv, direction));
|
||||
}
|
||||
|
||||
srtp_err_status_t srtp_cipher_output(srtp_cipher_t *c,
|
||||
uint8_t *buffer,
|
||||
size_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,
|
||||
buffer, num_octets_to_output));
|
||||
}
|
||||
|
||||
srtp_err_status_t srtp_cipher_encrypt(srtp_cipher_t *c,
|
||||
const uint8_t *src,
|
||||
size_t src_len,
|
||||
uint8_t *dst,
|
||||
size_t *dst_len)
|
||||
{
|
||||
if (!c || !c->type || !c->state) {
|
||||
return (srtp_err_status_bad_param);
|
||||
}
|
||||
|
||||
return (((c)->type)->encrypt(((c)->state), src, src_len, dst, dst_len));
|
||||
}
|
||||
|
||||
srtp_err_status_t srtp_cipher_decrypt(srtp_cipher_t *c,
|
||||
const uint8_t *src,
|
||||
size_t src_len,
|
||||
uint8_t *dst,
|
||||
size_t *dst_len)
|
||||
{
|
||||
if (!c || !c->type || !c->state) {
|
||||
return (srtp_err_status_bad_param);
|
||||
}
|
||||
|
||||
return (((c)->type)->decrypt(((c)->state), src, src_len, dst, dst_len));
|
||||
}
|
||||
|
||||
srtp_err_status_t srtp_cipher_set_aad(srtp_cipher_t *c,
|
||||
const uint8_t *aad,
|
||||
size_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 */
|
||||
|
||||
size_t srtp_cipher_get_key_length(const srtp_cipher_t *c)
|
||||
{
|
||||
return c->key_len;
|
||||
}
|
||||
|
||||
/*
|
||||
* A trivial platform independent random source.
|
||||
* For use in test only.
|
||||
*/
|
||||
void srtp_cipher_rand_for_tests(uint8_t *dest, size_t len)
|
||||
{
|
||||
/* Generic C-library (rand()) version */
|
||||
/* This is a random source of last resort */
|
||||
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... */
|
||||
*dest++ = val & 0xff;
|
||||
len--;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* A trivial platform independent 32 bit random number.
|
||||
* For use in test only.
|
||||
*/
|
||||
uint32_t srtp_cipher_rand_u32_for_tests(void)
|
||||
{
|
||||
uint32_t r;
|
||||
srtp_cipher_rand_for_tests((uint8_t *)&r, sizeof(r));
|
||||
return r;
|
||||
}
|
||||
|
||||
#define SELF_TEST_BUF_OCTETS 128
|
||||
#define NUM_RAND_TESTS 128
|
||||
#define MAX_KEY_LEN 64
|
||||
/*
|
||||
* srtp_cipher_type_test(ct, test_data) tests a cipher of type ct against
|
||||
* test cases provided in a list test_data of values of key, salt, iv,
|
||||
* plaintext, and ciphertext that is known to be good
|
||||
*/
|
||||
srtp_err_status_t srtp_cipher_type_test(
|
||||
const srtp_cipher_type_t *ct,
|
||||
const srtp_cipher_test_case_t *test_data)
|
||||
{
|
||||
return srtp_err_status_ok;
|
||||
const srtp_cipher_test_case_t *test_case = test_data;
|
||||
srtp_cipher_t *c;
|
||||
srtp_err_status_t status;
|
||||
uint8_t buffer[SELF_TEST_BUF_OCTETS];
|
||||
uint8_t buffer2[SELF_TEST_BUF_OCTETS];
|
||||
size_t len;
|
||||
size_t case_num = 0;
|
||||
|
||||
debug_print(srtp_mod_cipher, "running self-test for cipher %s",
|
||||
ct->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 srtp_err_status_cant_check;
|
||||
}
|
||||
|
||||
/*
|
||||
* loop over all test cases, perform known-answer tests of both the
|
||||
* encryption and decryption functions
|
||||
*/
|
||||
while (test_case != NULL) {
|
||||
/* allocate cipher */
|
||||
status = srtp_cipher_type_alloc(ct, &c, test_case->key_length_octets,
|
||||
test_case->tag_length_octets);
|
||||
if (status) {
|
||||
return status;
|
||||
}
|
||||
|
||||
/*
|
||||
* test the encrypt function
|
||||
*/
|
||||
debug_print0(srtp_mod_cipher, "testing encryption");
|
||||
|
||||
/* initialize cipher */
|
||||
status = srtp_cipher_init(c, test_case->key);
|
||||
if (status) {
|
||||
srtp_cipher_dealloc(c);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* copy plaintext into test buffer */
|
||||
if (test_case->ciphertext_length_octets > SELF_TEST_BUF_OCTETS) {
|
||||
srtp_cipher_dealloc(c);
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
for (size_t k = 0; k < test_case->plaintext_length_octets; k++) {
|
||||
buffer[k] = test_case->plaintext[k];
|
||||
}
|
||||
|
||||
debug_print(srtp_mod_cipher, "plaintext: %s",
|
||||
srtp_octet_string_hex_string(
|
||||
buffer, test_case->plaintext_length_octets));
|
||||
|
||||
/* set the initialization vector */
|
||||
status = srtp_cipher_set_iv(c, test_case->idx, srtp_direction_encrypt);
|
||||
if (status) {
|
||||
srtp_cipher_dealloc(c);
|
||||
return status;
|
||||
}
|
||||
|
||||
if (c->algorithm == SRTP_AES_GCM_128 ||
|
||||
c->algorithm == SRTP_AES_GCM_256) {
|
||||
debug_print(srtp_mod_cipher, "IV: %s",
|
||||
srtp_octet_string_hex_string(test_case->idx, 12));
|
||||
|
||||
/*
|
||||
* Set the AAD
|
||||
*/
|
||||
status = srtp_cipher_set_aad(c, test_case->aad,
|
||||
test_case->aad_length_octets);
|
||||
if (status) {
|
||||
srtp_cipher_dealloc(c);
|
||||
return status;
|
||||
}
|
||||
debug_print(srtp_mod_cipher, "AAD: %s",
|
||||
srtp_octet_string_hex_string(
|
||||
test_case->aad, test_case->aad_length_octets));
|
||||
}
|
||||
|
||||
/* encrypt */
|
||||
len = sizeof(buffer);
|
||||
status = srtp_cipher_encrypt(
|
||||
c, buffer, test_case->plaintext_length_octets, buffer, &len);
|
||||
if (status) {
|
||||
srtp_cipher_dealloc(c);
|
||||
return status;
|
||||
}
|
||||
|
||||
debug_print(srtp_mod_cipher, "ciphertext: %s",
|
||||
srtp_octet_string_hex_string(
|
||||
buffer, test_case->ciphertext_length_octets));
|
||||
|
||||
/* 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 (size_t 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 %zu failed", case_num);
|
||||
debug_print(srtp_mod_cipher, "(failure at byte %zu)", 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));
|
||||
|
||||
srtp_cipher_dealloc(c);
|
||||
return srtp_err_status_algo_fail;
|
||||
}
|
||||
|
||||
/*
|
||||
* test the decrypt function
|
||||
*/
|
||||
debug_print0(srtp_mod_cipher, "testing decryption");
|
||||
|
||||
/* re-initialize cipher for decryption */
|
||||
status = srtp_cipher_init(c, test_case->key);
|
||||
if (status) {
|
||||
srtp_cipher_dealloc(c);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* copy ciphertext into test buffer */
|
||||
if (test_case->ciphertext_length_octets > SELF_TEST_BUF_OCTETS) {
|
||||
srtp_cipher_dealloc(c);
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
for (size_t k = 0; k < test_case->ciphertext_length_octets; k++) {
|
||||
buffer[k] = test_case->ciphertext[k];
|
||||
}
|
||||
|
||||
debug_print(srtp_mod_cipher, "ciphertext: %s",
|
||||
srtp_octet_string_hex_string(
|
||||
buffer, test_case->plaintext_length_octets));
|
||||
|
||||
/* set the initialization vector */
|
||||
status = srtp_cipher_set_iv(c, test_case->idx, srtp_direction_decrypt);
|
||||
if (status) {
|
||||
srtp_cipher_dealloc(c);
|
||||
return status;
|
||||
}
|
||||
|
||||
if (c->algorithm == SRTP_AES_GCM_128 ||
|
||||
c->algorithm == SRTP_AES_GCM_256) {
|
||||
/*
|
||||
* Set the AAD
|
||||
*/
|
||||
status = srtp_cipher_set_aad(c, test_case->aad,
|
||||
test_case->aad_length_octets);
|
||||
if (status) {
|
||||
srtp_cipher_dealloc(c);
|
||||
return status;
|
||||
}
|
||||
debug_print(srtp_mod_cipher, "AAD: %s",
|
||||
srtp_octet_string_hex_string(
|
||||
test_case->aad, test_case->aad_length_octets));
|
||||
}
|
||||
|
||||
/* decrypt */
|
||||
len = sizeof(buffer);
|
||||
status = srtp_cipher_decrypt(
|
||||
c, buffer, test_case->ciphertext_length_octets, buffer, &len);
|
||||
if (status) {
|
||||
srtp_cipher_dealloc(c);
|
||||
return status;
|
||||
}
|
||||
|
||||
debug_print(srtp_mod_cipher, "plaintext: %s",
|
||||
srtp_octet_string_hex_string(
|
||||
buffer, test_case->plaintext_length_octets));
|
||||
|
||||
/* 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 (size_t 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 %zu failed", case_num);
|
||||
debug_print(srtp_mod_cipher, "(failure at byte %zu)", 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));
|
||||
|
||||
srtp_cipher_dealloc(c);
|
||||
return srtp_err_status_algo_fail;
|
||||
}
|
||||
|
||||
/* deallocate the cipher */
|
||||
status = srtp_cipher_dealloc(c);
|
||||
if (status) {
|
||||
return status;
|
||||
}
|
||||
|
||||
/*
|
||||
* the cipher passed the test case, so move on to the next test
|
||||
* case in the list; if NULL, we'l proceed to the next test
|
||||
*/
|
||||
test_case = test_case->next_test_case;
|
||||
++case_num;
|
||||
}
|
||||
|
||||
/* now run some random invertibility tests */
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
for (size_t j = 0; j < NUM_RAND_TESTS; j++) {
|
||||
size_t plaintext_len;
|
||||
size_t encrypted_len;
|
||||
size_t decrypted_len;
|
||||
uint8_t key[MAX_KEY_LEN];
|
||||
uint8_t iv[MAX_KEY_LEN];
|
||||
|
||||
/* choose a length at random (leaving room for IV and padding) */
|
||||
plaintext_len =
|
||||
srtp_cipher_rand_u32_for_tests() % (SELF_TEST_BUF_OCTETS - 64);
|
||||
debug_print(srtp_mod_cipher, "random plaintext length %zu\n",
|
||||
plaintext_len);
|
||||
srtp_cipher_rand_for_tests(buffer, plaintext_len);
|
||||
|
||||
debug_print(srtp_mod_cipher, "plaintext: %s",
|
||||
srtp_octet_string_hex_string(buffer, plaintext_len));
|
||||
|
||||
/* copy plaintext into second buffer */
|
||||
for (size_t i = 0; i < plaintext_len; 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, test_case->idx, srtp_direction_encrypt);
|
||||
if (status) {
|
||||
srtp_cipher_dealloc(c);
|
||||
return status;
|
||||
}
|
||||
|
||||
if (c->algorithm == SRTP_AES_GCM_128 ||
|
||||
c->algorithm == SRTP_AES_GCM_256) {
|
||||
/*
|
||||
* Set the AAD
|
||||
*/
|
||||
status = srtp_cipher_set_aad(c, test_case->aad,
|
||||
test_case->aad_length_octets);
|
||||
if (status) {
|
||||
srtp_cipher_dealloc(c);
|
||||
return status;
|
||||
}
|
||||
debug_print(srtp_mod_cipher, "AAD: %s",
|
||||
srtp_octet_string_hex_string(
|
||||
test_case->aad, test_case->aad_length_octets));
|
||||
}
|
||||
|
||||
/* encrypt buffer with cipher */
|
||||
encrypted_len = sizeof(buffer);
|
||||
status = srtp_cipher_encrypt(c, buffer, plaintext_len, buffer,
|
||||
&encrypted_len);
|
||||
if (status) {
|
||||
srtp_cipher_dealloc(c);
|
||||
return status;
|
||||
}
|
||||
|
||||
debug_print(srtp_mod_cipher, "ciphertext: %s",
|
||||
srtp_octet_string_hex_string(buffer, encrypted_len));
|
||||
|
||||
/*
|
||||
* re-initialize cipher for decryption, re-set the iv, then
|
||||
* decrypt the ciphertext
|
||||
*/
|
||||
status = srtp_cipher_init(c, key);
|
||||
if (status) {
|
||||
srtp_cipher_dealloc(c);
|
||||
return status;
|
||||
}
|
||||
status = srtp_cipher_set_iv(c, (uint8_t *)test_case->idx,
|
||||
srtp_direction_decrypt);
|
||||
if (status) {
|
||||
srtp_cipher_dealloc(c);
|
||||
return status;
|
||||
}
|
||||
if (c->algorithm == SRTP_AES_GCM_128 ||
|
||||
c->algorithm == SRTP_AES_GCM_256) {
|
||||
/*
|
||||
* Set the AAD
|
||||
*/
|
||||
status = srtp_cipher_set_aad(c, test_case->aad,
|
||||
test_case->aad_length_octets);
|
||||
if (status) {
|
||||
srtp_cipher_dealloc(c);
|
||||
return status;
|
||||
}
|
||||
debug_print(srtp_mod_cipher, "AAD: %s",
|
||||
srtp_octet_string_hex_string(
|
||||
test_case->aad, test_case->aad_length_octets));
|
||||
}
|
||||
decrypted_len = sizeof(buffer);
|
||||
status = srtp_cipher_decrypt(c, buffer, encrypted_len, buffer,
|
||||
&decrypted_len);
|
||||
if (status) {
|
||||
srtp_cipher_dealloc(c);
|
||||
return status;
|
||||
}
|
||||
|
||||
debug_print(srtp_mod_cipher, "plaintext[2]: %s",
|
||||
srtp_octet_string_hex_string(buffer, decrypted_len));
|
||||
|
||||
/* compare the resulting plaintext with the original one */
|
||||
if (decrypted_len != plaintext_len) {
|
||||
srtp_cipher_dealloc(c);
|
||||
return srtp_err_status_algo_fail;
|
||||
}
|
||||
status = srtp_err_status_ok;
|
||||
for (size_t 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 %zu failed",
|
||||
case_num);
|
||||
debug_print(srtp_mod_cipher, "(failure at byte %zu)", k);
|
||||
}
|
||||
}
|
||||
if (status) {
|
||||
srtp_cipher_dealloc(c);
|
||||
return srtp_err_status_algo_fail;
|
||||
}
|
||||
}
|
||||
|
||||
status = srtp_cipher_dealloc(c);
|
||||
if (status) {
|
||||
return status;
|
||||
}
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* srtp_cipher_type_self_test(ct) performs srtp_cipher_type_test on ct's
|
||||
* internal list of test data.
|
||||
*/
|
||||
srtp_err_status_t srtp_cipher_type_self_test(const srtp_cipher_type_t *ct)
|
||||
{
|
||||
return srtp_cipher_type_test(ct, ct->test_data);
|
||||
}
|
||||
|
||||
/*
|
||||
* cipher_bits_per_second(c, l, t) computes (an estimate of) the
|
||||
* number of bits that a cipher implementation can encrypt in a second
|
||||
*
|
||||
* c is a cipher (which MUST be allocated and initialized already), l
|
||||
* is the length in octets of the test data to be encrypted, and t is
|
||||
* the number of trials
|
||||
*
|
||||
* if an error is encountered, the value 0 is returned
|
||||
*/
|
||||
uint64_t srtp_cipher_bits_per_second(srtp_cipher_t *c,
|
||||
size_t octets_in_buffer,
|
||||
size_t num_trials)
|
||||
{
|
||||
v128_t nonce;
|
||||
clock_t timer;
|
||||
uint8_t *enc_buf;
|
||||
size_t len = octets_in_buffer;
|
||||
size_t out_len;
|
||||
size_t tag_len = SRTP_MAX_TAG_LEN;
|
||||
uint8_t aad[4] = { 0, 0, 0, 0 };
|
||||
size_t aad_len = 4;
|
||||
|
||||
enc_buf = (uint8_t *)srtp_crypto_alloc(octets_in_buffer + tag_len);
|
||||
if (enc_buf == NULL) {
|
||||
return 0; /* indicate bad parameters by returning null */
|
||||
}
|
||||
/* time repeated trials */
|
||||
v128_set_to_zero(&nonce);
|
||||
timer = clock();
|
||||
for (size_t i = 0; i < num_trials; i++, nonce.v32[3] = (uint32_t)i) {
|
||||
// Set IV
|
||||
if (srtp_cipher_set_iv(c, (uint8_t *)&nonce, srtp_direction_encrypt) !=
|
||||
srtp_err_status_ok) {
|
||||
srtp_crypto_free(enc_buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Set (empty) AAD if supported by the cipher
|
||||
if (c->type->set_aad) {
|
||||
if (srtp_cipher_set_aad(c, aad, aad_len) != srtp_err_status_ok) {
|
||||
srtp_crypto_free(enc_buf);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Encrypt the buffer
|
||||
out_len = octets_in_buffer + tag_len;
|
||||
if (srtp_cipher_encrypt(c, enc_buf, len, enc_buf, &out_len) !=
|
||||
srtp_err_status_ok) {
|
||||
srtp_crypto_free(enc_buf);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
timer = clock() - timer;
|
||||
|
||||
srtp_crypto_free(enc_buf);
|
||||
|
||||
if (timer == 0) {
|
||||
/* Too fast! */
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (uint64_t)CLOCKS_PER_SEC * num_trials * 8 * octets_in_buffer / timer;
|
||||
}
|
||||
365
libsrtp/crypto/cipher/cipher_test_cases.c
Normal file
365
libsrtp/crypto/cipher/cipher_test_cases.c
Normal file
@@ -0,0 +1,365 @@
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2013-2021, 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "cipher_test_cases.h"
|
||||
#include <stddef.h>
|
||||
|
||||
/*
|
||||
* KAT values for AES self-test. These
|
||||
* values came from the legacy libsrtp code.
|
||||
*/
|
||||
/* clang-format off */
|
||||
static const uint8_t srtp_aes_icm_128_test_case_0_key[SRTP_AES_ICM_128_KEY_LEN_WSALT] = {
|
||||
0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
|
||||
0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c,
|
||||
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
|
||||
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
/* clang-format off */
|
||||
static uint8_t srtp_aes_icm_128_test_case_0_nonce[16] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
/* clang-format off */
|
||||
static const uint8_t srtp_aes_icm_128_test_case_0_plaintext[32] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
/* clang-format off */
|
||||
static const uint8_t srtp_aes_icm_128_test_case_0_ciphertext[32] = {
|
||||
0xe0, 0x3e, 0xad, 0x09, 0x35, 0xc9, 0x5e, 0x80,
|
||||
0xe1, 0x66, 0xb1, 0x6d, 0xd9, 0x2b, 0x4e, 0xb4,
|
||||
0xd2, 0x35, 0x13, 0x16, 0x2b, 0x02, 0xd0, 0xf7,
|
||||
0x2a, 0x43, 0xa2, 0xfe, 0x4a, 0x5f, 0x97, 0xab
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
const srtp_cipher_test_case_t srtp_aes_icm_128_test_case_0 = {
|
||||
SRTP_AES_ICM_128_KEY_LEN_WSALT, /* octets in key */
|
||||
srtp_aes_icm_128_test_case_0_key, /* key */
|
||||
srtp_aes_icm_128_test_case_0_nonce, /* packet index */
|
||||
32, /* octets in plaintext */
|
||||
srtp_aes_icm_128_test_case_0_plaintext, /* plaintext */
|
||||
32, /* octets in ciphertext */
|
||||
srtp_aes_icm_128_test_case_0_ciphertext, /* ciphertext */
|
||||
0, /* */
|
||||
NULL, /* */
|
||||
0, /* */
|
||||
NULL /* pointer to next testcase */
|
||||
};
|
||||
|
||||
/*
|
||||
* KAT values for AES-192-CTR self-test. These
|
||||
* values came from section 7 of RFC 6188.
|
||||
*/
|
||||
/* clang-format off */
|
||||
static const uint8_t srtp_aes_icm_192_test_case_0_key[SRTP_AES_ICM_192_KEY_LEN_WSALT] = {
|
||||
0xea, 0xb2, 0x34, 0x76, 0x4e, 0x51, 0x7b, 0x2d,
|
||||
0x3d, 0x16, 0x0d, 0x58, 0x7d, 0x8c, 0x86, 0x21,
|
||||
0x97, 0x40, 0xf6, 0x5f, 0x99, 0xb6, 0xbc, 0xf7,
|
||||
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
|
||||
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
/* clang-format off */
|
||||
static uint8_t srtp_aes_icm_192_test_case_0_nonce[16] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
/* clang-format off */
|
||||
static const uint8_t srtp_aes_icm_192_test_case_0_plaintext[32] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
/* clang-format off */
|
||||
static const uint8_t srtp_aes_icm_192_test_case_0_ciphertext[32] = {
|
||||
0x35, 0x09, 0x6c, 0xba, 0x46, 0x10, 0x02, 0x8d,
|
||||
0xc1, 0xb5, 0x75, 0x03, 0x80, 0x4c, 0xe3, 0x7c,
|
||||
0x5d, 0xe9, 0x86, 0x29, 0x1d, 0xcc, 0xe1, 0x61,
|
||||
0xd5, 0x16, 0x5e, 0xc4, 0x56, 0x8f, 0x5c, 0x9a
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
const srtp_cipher_test_case_t srtp_aes_icm_192_test_case_0 = {
|
||||
SRTP_AES_ICM_192_KEY_LEN_WSALT, /* octets in key */
|
||||
srtp_aes_icm_192_test_case_0_key, /* key */
|
||||
srtp_aes_icm_192_test_case_0_nonce, /* packet index */
|
||||
32, /* octets in plaintext */
|
||||
srtp_aes_icm_192_test_case_0_plaintext, /* plaintext */
|
||||
32, /* octets in ciphertext */
|
||||
srtp_aes_icm_192_test_case_0_ciphertext, /* ciphertext */
|
||||
0, /* */
|
||||
NULL, /* */
|
||||
0, /* */
|
||||
NULL /* pointer to next testcase */
|
||||
};
|
||||
|
||||
/*
|
||||
* KAT values for AES-256-CTR self-test. These
|
||||
* values came from section 7 of RFC 6188.
|
||||
*/
|
||||
/* clang-format off */
|
||||
static const uint8_t srtp_aes_icm_256_test_case_0_key[SRTP_AES_ICM_256_KEY_LEN_WSALT] = {
|
||||
0x57, 0xf8, 0x2f, 0xe3, 0x61, 0x3f, 0xd1, 0x70,
|
||||
0xa8, 0x5e, 0xc9, 0x3c, 0x40, 0xb1, 0xf0, 0x92,
|
||||
0x2e, 0xc4, 0xcb, 0x0d, 0xc0, 0x25, 0xb5, 0x82,
|
||||
0x72, 0x14, 0x7c, 0xc4, 0x38, 0x94, 0x4a, 0x98,
|
||||
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
|
||||
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
/* clang-format off */
|
||||
static uint8_t srtp_aes_icm_256_test_case_0_nonce[16] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
/* clang-format off */
|
||||
static const uint8_t srtp_aes_icm_256_test_case_0_plaintext[32] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
/* clang-format off */
|
||||
static const uint8_t srtp_aes_icm_256_test_case_0_ciphertext[32] = {
|
||||
0x92, 0xbd, 0xd2, 0x8a, 0x93, 0xc3, 0xf5, 0x25,
|
||||
0x11, 0xc6, 0x77, 0xd0, 0x8b, 0x55, 0x15, 0xa4,
|
||||
0x9d, 0xa7, 0x1b, 0x23, 0x78, 0xa8, 0x54, 0xf6,
|
||||
0x70, 0x50, 0x75, 0x6d, 0xed, 0x16, 0x5b, 0xac
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
const srtp_cipher_test_case_t srtp_aes_icm_256_test_case_0 = {
|
||||
SRTP_AES_ICM_256_KEY_LEN_WSALT, /* octets in key */
|
||||
srtp_aes_icm_256_test_case_0_key, /* key */
|
||||
srtp_aes_icm_256_test_case_0_nonce, /* packet index */
|
||||
32, /* octets in plaintext */
|
||||
srtp_aes_icm_256_test_case_0_plaintext, /* plaintext */
|
||||
32, /* octets in ciphertext */
|
||||
srtp_aes_icm_256_test_case_0_ciphertext, /* ciphertext */
|
||||
0, /* */
|
||||
NULL, /* */
|
||||
0, /* */
|
||||
NULL /* pointer to next testcase */
|
||||
};
|
||||
|
||||
/*
|
||||
* 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_128_test_case_0_key[SRTP_AES_GCM_128_KEY_LEN_WSALT] = {
|
||||
0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
|
||||
0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
|
||||
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
|
||||
0x09, 0x0a, 0x0b, 0x0c,
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
/* clang-format off */
|
||||
static uint8_t srtp_aes_gcm_128_test_case_0_iv[12] = {
|
||||
0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
|
||||
0xde, 0xca, 0xf8, 0x88
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
/* clang-format off */
|
||||
static const uint8_t srtp_aes_gcm_128_test_case_0_plaintext[60] = {
|
||||
0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
|
||||
0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
|
||||
0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
|
||||
0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
|
||||
0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
|
||||
0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
|
||||
0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
|
||||
0xba, 0x63, 0x7b, 0x39
|
||||
};
|
||||
|
||||
/* clang-format off */
|
||||
static const uint8_t srtp_aes_gcm_128_test_case_0_aad[20] = {
|
||||
0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
|
||||
0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
|
||||
0xab, 0xad, 0xda, 0xd2
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
/* clang-format off */
|
||||
static const uint8_t srtp_aes_gcm_128_test_case_0_ciphertext[76] = {
|
||||
0x42, 0x83, 0x1e, 0xc2, 0x21, 0x77, 0x74, 0x24,
|
||||
0x4b, 0x72, 0x21, 0xb7, 0x84, 0xd0, 0xd4, 0x9c,
|
||||
0xe3, 0xaa, 0x21, 0x2f, 0x2c, 0x02, 0xa4, 0xe0,
|
||||
0x35, 0xc1, 0x7e, 0x23, 0x29, 0xac, 0xa1, 0x2e,
|
||||
0x21, 0xd5, 0x14, 0xb2, 0x54, 0x66, 0x93, 0x1c,
|
||||
0x7d, 0x8f, 0x6a, 0x5a, 0xac, 0x84, 0xaa, 0x05,
|
||||
0x1b, 0xa3, 0x0b, 0x39, 0x6a, 0x0a, 0xac, 0x97,
|
||||
0x3d, 0x58, 0xe0, 0x91,
|
||||
/* the last 16 bytes are the tag */
|
||||
0x5b, 0xc9, 0x4f, 0xbc, 0x32, 0x21, 0xa5, 0xdb,
|
||||
0x94, 0xfa, 0xe9, 0x5a, 0xe7, 0x12, 0x1a, 0x47,
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
static const srtp_cipher_test_case_t srtp_aes_gcm_128_test_case_0a = {
|
||||
SRTP_AES_GCM_128_KEY_LEN_WSALT, /* octets in key */
|
||||
srtp_aes_gcm_128_test_case_0_key, /* key */
|
||||
srtp_aes_gcm_128_test_case_0_iv, /* packet index */
|
||||
60, /* octets in plaintext */
|
||||
srtp_aes_gcm_128_test_case_0_plaintext, /* plaintext */
|
||||
68, /* octets in ciphertext */
|
||||
srtp_aes_gcm_128_test_case_0_ciphertext, /* ciphertext + tag */
|
||||
20, /* octets in AAD */
|
||||
srtp_aes_gcm_128_test_case_0_aad, /* AAD */
|
||||
8, /* */
|
||||
NULL /* pointer to next testcase */
|
||||
};
|
||||
|
||||
const srtp_cipher_test_case_t srtp_aes_gcm_128_test_case_0 = {
|
||||
SRTP_AES_GCM_128_KEY_LEN_WSALT, /* octets in key */
|
||||
srtp_aes_gcm_128_test_case_0_key, /* key */
|
||||
srtp_aes_gcm_128_test_case_0_iv, /* packet index */
|
||||
60, /* octets in plaintext */
|
||||
srtp_aes_gcm_128_test_case_0_plaintext, /* plaintext */
|
||||
76, /* octets in ciphertext */
|
||||
srtp_aes_gcm_128_test_case_0_ciphertext, /* ciphertext + tag */
|
||||
20, /* octets in AAD */
|
||||
srtp_aes_gcm_128_test_case_0_aad, /* AAD */
|
||||
16, /* */
|
||||
&srtp_aes_gcm_128_test_case_0a /* pointer to next testcase */
|
||||
};
|
||||
|
||||
/* clang-format off */
|
||||
static const uint8_t srtp_aes_gcm_256_test_case_0_key[SRTP_AES_GCM_256_KEY_LEN_WSALT] = {
|
||||
0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
|
||||
0xa5, 0x59, 0x09, 0xc5, 0x54, 0x66, 0x93, 0x1c,
|
||||
0xaf, 0xf5, 0x26, 0x9a, 0x21, 0xd5, 0x14, 0xb2,
|
||||
0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
|
||||
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
|
||||
0x09, 0x0a, 0x0b, 0x0c,
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
/* clang-format off */
|
||||
static uint8_t srtp_aes_gcm_256_test_case_0_iv[12] = {
|
||||
0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
|
||||
0xde, 0xca, 0xf8, 0x88
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
/* clang-format off */
|
||||
static const uint8_t srtp_aes_gcm_256_test_case_0_plaintext[60] = {
|
||||
0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
|
||||
0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
|
||||
0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
|
||||
0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
|
||||
0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
|
||||
0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
|
||||
0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
|
||||
0xba, 0x63, 0x7b, 0x39
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
/* clang-format off */
|
||||
static const uint8_t srtp_aes_gcm_256_test_case_0_aad[20] = {
|
||||
0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
|
||||
0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
|
||||
0xab, 0xad, 0xda, 0xd2
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
/* clang-format off */
|
||||
static const uint8_t srtp_aes_gcm_256_test_case_0_ciphertext[76] = {
|
||||
0x0b, 0x11, 0xcf, 0xaf, 0x68, 0x4d, 0xae, 0x46,
|
||||
0xc7, 0x90, 0xb8, 0x8e, 0xb7, 0x6a, 0x76, 0x2a,
|
||||
0x94, 0x82, 0xca, 0xab, 0x3e, 0x39, 0xd7, 0x86,
|
||||
0x1b, 0xc7, 0x93, 0xed, 0x75, 0x7f, 0x23, 0x5a,
|
||||
0xda, 0xfd, 0xd3, 0xe2, 0x0e, 0x80, 0x87, 0xa9,
|
||||
0x6d, 0xd7, 0xe2, 0x6a, 0x7d, 0x5f, 0xb4, 0x80,
|
||||
0xef, 0xef, 0xc5, 0x29, 0x12, 0xd1, 0xaa, 0x10,
|
||||
0x09, 0xc9, 0x86, 0xc1,
|
||||
/* the last 16 bytes are the tag */
|
||||
0x45, 0xbc, 0x03, 0xe6, 0xe1, 0xac, 0x0a, 0x9f,
|
||||
0x81, 0xcb, 0x8e, 0x5b, 0x46, 0x65, 0x63, 0x1d,
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
static const srtp_cipher_test_case_t srtp_aes_gcm_256_test_case_0a = {
|
||||
SRTP_AES_GCM_256_KEY_LEN_WSALT, /* octets in key */
|
||||
srtp_aes_gcm_256_test_case_0_key, /* key */
|
||||
srtp_aes_gcm_256_test_case_0_iv, /* packet index */
|
||||
60, /* octets in plaintext */
|
||||
srtp_aes_gcm_256_test_case_0_plaintext, /* plaintext */
|
||||
68, /* octets in ciphertext */
|
||||
srtp_aes_gcm_256_test_case_0_ciphertext, /* ciphertext + tag */
|
||||
20, /* octets in AAD */
|
||||
srtp_aes_gcm_256_test_case_0_aad, /* AAD */
|
||||
8, /* */
|
||||
NULL /* pointer to next testcase */
|
||||
};
|
||||
|
||||
const srtp_cipher_test_case_t srtp_aes_gcm_256_test_case_0 = {
|
||||
SRTP_AES_GCM_256_KEY_LEN_WSALT, /* octets in key */
|
||||
srtp_aes_gcm_256_test_case_0_key, /* key */
|
||||
srtp_aes_gcm_256_test_case_0_iv, /* packet index */
|
||||
60, /* octets in plaintext */
|
||||
srtp_aes_gcm_256_test_case_0_plaintext, /* plaintext */
|
||||
76, /* octets in ciphertext */
|
||||
srtp_aes_gcm_256_test_case_0_ciphertext, /* ciphertext + tag */
|
||||
20, /* octets in AAD */
|
||||
srtp_aes_gcm_256_test_case_0_aad, /* AAD */
|
||||
16, /* */
|
||||
&srtp_aes_gcm_256_test_case_0a /* pointer to next testcase */
|
||||
};
|
||||
53
libsrtp/crypto/cipher/cipher_test_cases.h
Normal file
53
libsrtp/crypto/cipher/cipher_test_cases.h
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2013-2021, 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CIPHER_TEST_CASES_H
|
||||
#define CIPHER_TEST_CASES_H
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include "cipher.h"
|
||||
|
||||
extern const srtp_cipher_test_case_t srtp_aes_icm_128_test_case_0;
|
||||
extern const srtp_cipher_test_case_t srtp_aes_icm_192_test_case_0;
|
||||
extern const srtp_cipher_test_case_t srtp_aes_icm_256_test_case_0;
|
||||
|
||||
extern const srtp_cipher_test_case_t srtp_aes_gcm_128_test_case_0;
|
||||
extern const srtp_cipher_test_case_t srtp_aes_gcm_256_test_case_0;
|
||||
|
||||
#endif
|
||||
166
libsrtp/crypto/cipher/null_cipher.c
Normal file
166
libsrtp/crypto/cipher/null_cipher.c
Normal file
@@ -0,0 +1,166 @@
|
||||
/*
|
||||
* null_cipher.c
|
||||
*
|
||||
* A null cipher implementation. This cipher leaves the plaintext
|
||||
* unchanged.
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-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 "datatypes.h"
|
||||
#include "null_cipher.h"
|
||||
#include "err.h" /* for srtp_debug */
|
||||
#include "alloc.h"
|
||||
#include "cipher_types.h"
|
||||
|
||||
static srtp_err_status_t srtp_null_cipher_alloc(srtp_cipher_t **c,
|
||||
size_t key_len,
|
||||
size_t tlen)
|
||||
{
|
||||
extern const srtp_cipher_type_t srtp_null_cipher;
|
||||
(void)tlen;
|
||||
|
||||
debug_print(srtp_mod_cipher, "allocating cipher with key length %zu",
|
||||
key_len);
|
||||
|
||||
/* allocate memory a cipher of type null_cipher */
|
||||
*c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
|
||||
if (*c == NULL) {
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
|
||||
/* set pointers */
|
||||
(*c)->algorithm = SRTP_NULL_CIPHER;
|
||||
(*c)->type = &srtp_null_cipher;
|
||||
(*c)->state = (void *)0x1; /* The null cipher does not maintain state */
|
||||
|
||||
/* set key size */
|
||||
(*c)->key_len = key_len;
|
||||
|
||||
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(c, sizeof(srtp_cipher_t));
|
||||
|
||||
/* free memory of type null_cipher */
|
||||
srtp_crypto_free(c);
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_null_cipher_init(void *cv, const uint8_t *key)
|
||||
{
|
||||
/* srtp_null_cipher_ctx_t *c = (srtp_null_cipher_ctx_t *)cv; */
|
||||
(void)cv;
|
||||
(void)key;
|
||||
debug_print0(srtp_mod_cipher, "initializing null cipher");
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_null_cipher_set_iv(void *cv,
|
||||
uint8_t *iv,
|
||||
srtp_cipher_direction_t dir)
|
||||
{
|
||||
/* srtp_null_cipher_ctx_t *c = (srtp_null_cipher_ctx_t *)cv; */
|
||||
(void)cv;
|
||||
(void)iv;
|
||||
(void)dir;
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_null_cipher_encrypt(void *cv,
|
||||
const uint8_t *src,
|
||||
size_t src_len,
|
||||
uint8_t *dst,
|
||||
size_t *dst_len)
|
||||
{
|
||||
(void)cv;
|
||||
if (src != dst) {
|
||||
if (*dst_len < src_len) {
|
||||
return srtp_err_status_buffer_small;
|
||||
}
|
||||
memcpy(dst, src, src_len);
|
||||
}
|
||||
*dst_len = src_len;
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static const char srtp_null_cipher_description[] = "null cipher";
|
||||
|
||||
static const srtp_cipher_test_case_t srtp_null_cipher_test_0 = {
|
||||
0, /* octets in key */
|
||||
NULL, /* key */
|
||||
0, /* packet index */
|
||||
0, /* octets in plaintext */
|
||||
NULL, /* plaintext */
|
||||
0, /* octets in plaintext */
|
||||
NULL, /* ciphertext */
|
||||
0, /* */
|
||||
NULL, /* */
|
||||
0, /* */
|
||||
NULL /* pointer to next testcase */
|
||||
};
|
||||
|
||||
/*
|
||||
* note: the decrypt function is identical to the encrypt function
|
||||
*/
|
||||
|
||||
const srtp_cipher_type_t srtp_null_cipher = {
|
||||
srtp_null_cipher_alloc, /* */
|
||||
srtp_null_cipher_dealloc, /* */
|
||||
srtp_null_cipher_init, /* */
|
||||
0, /* set_aad */
|
||||
srtp_null_cipher_encrypt, /* */
|
||||
srtp_null_cipher_encrypt, /* */
|
||||
srtp_null_cipher_set_iv, /* */
|
||||
srtp_null_cipher_description, /* */
|
||||
&srtp_null_cipher_test_0, /* */
|
||||
SRTP_NULL_CIPHER /* */
|
||||
};
|
||||
195
libsrtp/crypto/hash/auth.c
Normal file
195
libsrtp/crypto/hash/auth.c
Normal file
@@ -0,0 +1,195 @@
|
||||
/*
|
||||
* auth.c
|
||||
*
|
||||
* some bookkeeping functions for authentication functions
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-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 "auth.h"
|
||||
#include "err.h" /* for srtp_debug */
|
||||
#include "datatypes.h" /* for octet_string */
|
||||
|
||||
/* the debug module for authentiation */
|
||||
|
||||
srtp_debug_module_t srtp_mod_auth = {
|
||||
false, /* debugging is off by default */
|
||||
"auth func" /* printable name for module */
|
||||
};
|
||||
|
||||
size_t srtp_auth_get_key_length(const srtp_auth_t *a)
|
||||
{
|
||||
return a->key_len;
|
||||
}
|
||||
|
||||
size_t srtp_auth_get_tag_length(const srtp_auth_t *a)
|
||||
{
|
||||
return a->out_len;
|
||||
}
|
||||
|
||||
size_t srtp_auth_get_prefix_length(const srtp_auth_t *a)
|
||||
{
|
||||
return a->prefix_len;
|
||||
}
|
||||
|
||||
/*
|
||||
* srtp_auth_type_test() tests an auth function of type ct against
|
||||
* test cases provided in a list test_data of values of key, data, and tag
|
||||
* that is known to be good
|
||||
*/
|
||||
|
||||
/* should be big enough for most occasions */
|
||||
#define SELF_TEST_TAG_BUF_OCTETS 32
|
||||
|
||||
srtp_err_status_t srtp_auth_type_test(const srtp_auth_type_t *at,
|
||||
const srtp_auth_test_case_t *test_data)
|
||||
{
|
||||
return srtp_err_status_ok;
|
||||
const srtp_auth_test_case_t *test_case = test_data;
|
||||
srtp_auth_t *a;
|
||||
srtp_err_status_t status;
|
||||
uint8_t tag[SELF_TEST_TAG_BUF_OCTETS];
|
||||
size_t i = 0;
|
||||
size_t case_num = 0;
|
||||
|
||||
debug_print(srtp_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 srtp_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 srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
/* allocate auth */
|
||||
status = srtp_auth_type_alloc(at, &a, test_case->key_length_octets,
|
||||
test_case->tag_length_octets);
|
||||
if (status) {
|
||||
return status;
|
||||
}
|
||||
|
||||
/* initialize auth */
|
||||
status = srtp_auth_init(a, test_case->key);
|
||||
if (status) {
|
||||
srtp_auth_dealloc(a);
|
||||
return status;
|
||||
}
|
||||
|
||||
status = srtp_auth_start(a);
|
||||
if (status) {
|
||||
srtp_auth_dealloc(a);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* zeroize tag then compute */
|
||||
octet_string_set_to_zero(tag, test_case->tag_length_octets);
|
||||
status = srtp_auth_compute(a, test_case->data,
|
||||
test_case->data_length_octets, tag);
|
||||
if (status) {
|
||||
srtp_auth_dealloc(a);
|
||||
return status;
|
||||
}
|
||||
|
||||
debug_print(srtp_mod_auth, "key: %s",
|
||||
srtp_octet_string_hex_string(test_case->key,
|
||||
test_case->key_length_octets));
|
||||
debug_print(srtp_mod_auth, "data: %s",
|
||||
srtp_octet_string_hex_string(
|
||||
test_case->data, test_case->data_length_octets));
|
||||
debug_print(
|
||||
srtp_mod_auth, "tag computed: %s",
|
||||
srtp_octet_string_hex_string(tag, test_case->tag_length_octets));
|
||||
debug_print(srtp_mod_auth, "tag expected: %s",
|
||||
srtp_octet_string_hex_string(test_case->tag,
|
||||
test_case->tag_length_octets));
|
||||
|
||||
/* check the result */
|
||||
status = srtp_err_status_ok;
|
||||
for (i = 0; i < test_case->tag_length_octets; i++) {
|
||||
if (tag[i] != test_case->tag[i]) {
|
||||
status = srtp_err_status_algo_fail;
|
||||
debug_print(srtp_mod_auth, "test case %zu failed", case_num);
|
||||
debug_print(srtp_mod_auth, " (mismatch at octet %zu)", i);
|
||||
}
|
||||
}
|
||||
if (status) {
|
||||
srtp_auth_dealloc(a);
|
||||
return srtp_err_status_algo_fail;
|
||||
}
|
||||
|
||||
/* deallocate the auth function */
|
||||
status = srtp_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 srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* srtp_auth_type_self_test(at) performs srtp_auth_type_test on at's internal
|
||||
* list of test data.
|
||||
*/
|
||||
|
||||
srtp_err_status_t srtp_auth_type_self_test(const srtp_auth_type_t *at)
|
||||
{
|
||||
return srtp_auth_type_test(at, at->test_data);
|
||||
}
|
||||
70
libsrtp/crypto/hash/auth_test_cases.c
Normal file
70
libsrtp/crypto/hash/auth_test_cases.c
Normal file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2013-2021, 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "auth_test_cases.h"
|
||||
#include <stddef.h>
|
||||
|
||||
/* clang-format off */
|
||||
static const uint8_t srtp_hmac_test_case_0_key[20] = {
|
||||
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
|
||||
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
|
||||
0x0b, 0x0b, 0x0b, 0x0b
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
/* clang-format off */
|
||||
static const uint8_t srtp_hmac_test_case_0_data[8] = {
|
||||
0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65 /* "Hi There" */
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
/* clang-format off */
|
||||
static const uint8_t srtp_hmac_test_case_0_tag[20] = {
|
||||
0xb6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64,
|
||||
0xe2, 0x8b, 0xc0, 0xb6, 0xfb, 0x37, 0x8c, 0x8e,
|
||||
0xf1, 0x46, 0xbe, 0x00
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
const srtp_auth_test_case_t srtp_hmac_test_case_0 = {
|
||||
sizeof(srtp_hmac_test_case_0_key), /* octets in key */
|
||||
srtp_hmac_test_case_0_key, /* key */
|
||||
sizeof(srtp_hmac_test_case_0_data), /* octets in data */
|
||||
srtp_hmac_test_case_0_data, /* data */
|
||||
sizeof(srtp_hmac_test_case_0_tag), /* octets in tag */
|
||||
srtp_hmac_test_case_0_tag, /* tag */
|
||||
NULL /* pointer to next testcase */
|
||||
};
|
||||
48
libsrtp/crypto/hash/auth_test_cases.h
Normal file
48
libsrtp/crypto/hash/auth_test_cases.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2013-2021, 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef AUTH_TEST_CASES_H
|
||||
#define AUTH_TEST_CASES_H
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include "auth.h"
|
||||
|
||||
extern const srtp_auth_test_case_t srtp_hmac_test_case_0;
|
||||
|
||||
#endif
|
||||
248
libsrtp/crypto/hash/hmac.c
Normal file
248
libsrtp/crypto/hash/hmac.c
Normal file
@@ -0,0 +1,248 @@
|
||||
/*
|
||||
* hmac.c
|
||||
*
|
||||
* implementation of hmac srtp_auth_type_t
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*/
|
||||
/*
|
||||
*
|
||||
* Copyright(c) 2001-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 "hmac.h"
|
||||
#include "alloc.h"
|
||||
#include "cipher_types.h"
|
||||
#include "auth_test_cases.h"
|
||||
|
||||
/* the debug module for authentiation */
|
||||
|
||||
srtp_debug_module_t srtp_mod_hmac = {
|
||||
false, /* debugging is off by default */
|
||||
"hmac sha-1" /* printable name for module */
|
||||
};
|
||||
|
||||
static srtp_err_status_t srtp_hmac_alloc(srtp_auth_t **a,
|
||||
size_t key_len,
|
||||
size_t out_len)
|
||||
{
|
||||
extern const srtp_auth_type_t srtp_hmac;
|
||||
uint8_t *pointer;
|
||||
|
||||
debug_print(srtp_mod_hmac, "allocating auth func with key length %zu",
|
||||
key_len);
|
||||
debug_print(srtp_mod_hmac, " tag length %zu",
|
||||
out_len);
|
||||
|
||||
/*
|
||||
* check key length - note that we don't support keys larger
|
||||
* than 20 bytes yet
|
||||
*/
|
||||
if (key_len > 20) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
/* check output length - should be less than 20 bytes */
|
||||
if (out_len > 20) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
/* allocate memory for auth and srtp_hmac_ctx_t structures */
|
||||
pointer = (uint8_t *)srtp_crypto_alloc(sizeof(srtp_hmac_ctx_t) +
|
||||
sizeof(srtp_auth_t));
|
||||
if (pointer == NULL) {
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
|
||||
/* set pointers */
|
||||
*a = (srtp_auth_t *)pointer;
|
||||
(*a)->type = &srtp_hmac;
|
||||
(*a)->state = pointer + sizeof(srtp_auth_t);
|
||||
(*a)->out_len = out_len;
|
||||
(*a)->key_len = key_len;
|
||||
(*a)->prefix_len = 0;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_hmac_dealloc(srtp_auth_t *a)
|
||||
{
|
||||
/* zeroize entire state*/
|
||||
octet_string_set_to_zero(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_hmac_init(void *statev,
|
||||
const uint8_t *key,
|
||||
size_t key_len)
|
||||
{
|
||||
srtp_hmac_ctx_t *state = (srtp_hmac_ctx_t *)statev;
|
||||
uint8_t ipad[64];
|
||||
|
||||
/*
|
||||
* check key length - note that we don't support keys larger
|
||||
* than 20 bytes yet
|
||||
*/
|
||||
if (key_len > 20) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
/*
|
||||
* set values of ipad and opad by exoring the key into the
|
||||
* appropriate constant values
|
||||
*/
|
||||
for (size_t i = 0; i < key_len; i++) {
|
||||
ipad[i] = key[i] ^ 0x36;
|
||||
state->opad[i] = key[i] ^ 0x5c;
|
||||
}
|
||||
/* set the rest of ipad, opad to constant values */
|
||||
for (size_t i = key_len; i < 64; i++) {
|
||||
ipad[i] = 0x36;
|
||||
((uint8_t *)state->opad)[i] = 0x5c;
|
||||
}
|
||||
|
||||
debug_print(srtp_mod_hmac, "ipad: %s",
|
||||
srtp_octet_string_hex_string(ipad, 64));
|
||||
|
||||
/* initialize sha1 context */
|
||||
srtp_sha1_init(&state->init_ctx);
|
||||
|
||||
/* hash ipad ^ key */
|
||||
srtp_sha1_update(&state->init_ctx, ipad, 64);
|
||||
memcpy(&state->ctx, &state->init_ctx, sizeof(srtp_sha1_ctx_t));
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_hmac_start(void *statev)
|
||||
{
|
||||
srtp_hmac_ctx_t *state = (srtp_hmac_ctx_t *)statev;
|
||||
|
||||
memcpy(&state->ctx, &state->init_ctx, sizeof(srtp_sha1_ctx_t));
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_hmac_update(void *statev,
|
||||
const uint8_t *message,
|
||||
size_t msg_octets)
|
||||
{
|
||||
srtp_hmac_ctx_t *state = (srtp_hmac_ctx_t *)statev;
|
||||
|
||||
debug_print(srtp_mod_hmac, "input: %s",
|
||||
srtp_octet_string_hex_string(message, msg_octets));
|
||||
|
||||
/* hash message into sha1 context */
|
||||
srtp_sha1_update(&state->ctx, message, msg_octets);
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_hmac_compute(void *statev,
|
||||
const uint8_t *message,
|
||||
size_t msg_octets,
|
||||
size_t tag_len,
|
||||
uint8_t *result)
|
||||
{
|
||||
srtp_hmac_ctx_t *state = (srtp_hmac_ctx_t *)statev;
|
||||
uint32_t hash_value[5];
|
||||
uint32_t H[5];
|
||||
size_t i;
|
||||
|
||||
/* check tag length, return error if we can't provide the value expected */
|
||||
if (tag_len > 20) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
/* hash message, copy output into H */
|
||||
srtp_hmac_update(state, message, msg_octets);
|
||||
srtp_sha1_final(&state->ctx, H);
|
||||
|
||||
/*
|
||||
* note that we don't need to debug_print() the input, since the
|
||||
* function hmac_update() already did that for us
|
||||
*/
|
||||
debug_print(srtp_mod_hmac, "intermediate state: %s",
|
||||
srtp_octet_string_hex_string((uint8_t *)H, 20));
|
||||
|
||||
/* re-initialize hash context */
|
||||
srtp_sha1_init(&state->ctx);
|
||||
|
||||
/* hash opad ^ key */
|
||||
srtp_sha1_update(&state->ctx, (uint8_t *)state->opad, 64);
|
||||
|
||||
/* hash the result of the inner hash */
|
||||
srtp_sha1_update(&state->ctx, (uint8_t *)H, 20);
|
||||
|
||||
/* the result is returned in the array hash_value[] */
|
||||
srtp_sha1_final(&state->ctx, hash_value);
|
||||
|
||||
/* copy hash_value to *result */
|
||||
for (i = 0; i < tag_len; i++) {
|
||||
result[i] = ((uint8_t *)hash_value)[i];
|
||||
}
|
||||
|
||||
debug_print(srtp_mod_hmac, "output: %s",
|
||||
srtp_octet_string_hex_string((uint8_t *)hash_value, tag_len));
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static const char srtp_hmac_description[] =
|
||||
"hmac sha-1 authentication function";
|
||||
|
||||
/*
|
||||
* srtp_auth_type_t hmac is the hmac metaobject
|
||||
*/
|
||||
|
||||
const srtp_auth_type_t srtp_hmac = {
|
||||
srtp_hmac_alloc, /* */
|
||||
srtp_hmac_dealloc, /* */
|
||||
srtp_hmac_init, /* */
|
||||
srtp_hmac_compute, /* */
|
||||
srtp_hmac_update, /* */
|
||||
srtp_hmac_start, /* */
|
||||
srtp_hmac_description, /* */
|
||||
&srtp_hmac_test_case_0, /* */
|
||||
SRTP_HMAC_SHA1 /* */
|
||||
};
|
||||
224
libsrtp/crypto/hash/hmac_mbedtls.c
Normal file
224
libsrtp/crypto/hash/hmac_mbedtls.c
Normal file
@@ -0,0 +1,224 @@
|
||||
/*
|
||||
* hmac_mbedtls.c
|
||||
*
|
||||
* Implementation of hmac srtp_auth_type_t that leverages Mbedtls
|
||||
*
|
||||
* YongCheng Yang
|
||||
*/
|
||||
/*
|
||||
*
|
||||
* 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 "auth.h"
|
||||
#include "alloc.h"
|
||||
#include "err.h" /* for srtp_debug */
|
||||
#include "auth_test_cases.h"
|
||||
#include <mbedtls/md.h>
|
||||
|
||||
#define SHA1_DIGEST_SIZE 20
|
||||
|
||||
/* the debug module for authentiation */
|
||||
|
||||
srtp_debug_module_t srtp_mod_hmac = {
|
||||
false, /* debugging is off by default */
|
||||
"hmac sha-1 mbedtls" /* printable name for module */
|
||||
};
|
||||
|
||||
static srtp_err_status_t srtp_hmac_mbedtls_alloc(srtp_auth_t **a,
|
||||
size_t key_len,
|
||||
size_t out_len)
|
||||
{
|
||||
extern const srtp_auth_type_t srtp_hmac;
|
||||
|
||||
debug_print(srtp_mod_hmac, "allocating auth func with key length %zu",
|
||||
key_len);
|
||||
debug_print(srtp_mod_hmac, " tag length %zu",
|
||||
out_len);
|
||||
|
||||
/* check output length - should be less than 20 bytes */
|
||||
if (out_len > SHA1_DIGEST_SIZE) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
*a = (srtp_auth_t *)srtp_crypto_alloc(sizeof(srtp_auth_t));
|
||||
if (*a == NULL) {
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
// allocate the buffer of mbedtls context.
|
||||
(*a)->state = srtp_crypto_alloc(sizeof(mbedtls_md_context_t));
|
||||
if ((*a)->state == NULL) {
|
||||
srtp_crypto_free(*a);
|
||||
*a = NULL;
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
mbedtls_md_init((mbedtls_md_context_t *)(*a)->state);
|
||||
|
||||
/* set pointers */
|
||||
(*a)->type = &srtp_hmac;
|
||||
(*a)->out_len = out_len;
|
||||
(*a)->key_len = key_len;
|
||||
(*a)->prefix_len = 0;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_hmac_mbedtls_dealloc(srtp_auth_t *a)
|
||||
{
|
||||
mbedtls_md_context_t *hmac_ctx;
|
||||
hmac_ctx = (mbedtls_md_context_t *)a->state;
|
||||
mbedtls_md_free(hmac_ctx);
|
||||
srtp_crypto_free(hmac_ctx);
|
||||
/* zeroize entire state*/
|
||||
octet_string_set_to_zero(a, sizeof(srtp_auth_t));
|
||||
|
||||
/* free memory */
|
||||
srtp_crypto_free(a);
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_hmac_mbedtls_start(void *statev)
|
||||
{
|
||||
mbedtls_md_context_t *state = (mbedtls_md_context_t *)statev;
|
||||
if (mbedtls_md_hmac_reset(state) != 0) {
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_hmac_mbedtls_init(void *statev,
|
||||
const uint8_t *key,
|
||||
size_t key_len)
|
||||
{
|
||||
mbedtls_md_context_t *state = (mbedtls_md_context_t *)statev;
|
||||
const mbedtls_md_info_t *info = NULL;
|
||||
|
||||
info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1);
|
||||
if (info == NULL) {
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
|
||||
if (mbedtls_md_setup(state, info, 1) != 0) {
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
|
||||
debug_print(srtp_mod_hmac, "mbedtls setup, name: %s",
|
||||
mbedtls_md_get_name(info));
|
||||
debug_print(srtp_mod_hmac, "mbedtls setup, size: %d",
|
||||
mbedtls_md_get_size(info));
|
||||
|
||||
if (mbedtls_md_hmac_starts(state, key, key_len) != 0) {
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_hmac_mbedtls_update(void *statev,
|
||||
const uint8_t *message,
|
||||
size_t msg_octets)
|
||||
{
|
||||
mbedtls_md_context_t *state = (mbedtls_md_context_t *)statev;
|
||||
|
||||
debug_print(srtp_mod_hmac, "input: %s",
|
||||
srtp_octet_string_hex_string(message, msg_octets));
|
||||
|
||||
if (mbedtls_md_hmac_update(state, message, msg_octets) != 0) {
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_hmac_mbedtls_compute(void *statev,
|
||||
const uint8_t *message,
|
||||
size_t msg_octets,
|
||||
size_t tag_len,
|
||||
uint8_t *result)
|
||||
{
|
||||
mbedtls_md_context_t *state = (mbedtls_md_context_t *)statev;
|
||||
uint8_t hash_value[SHA1_DIGEST_SIZE];
|
||||
size_t i;
|
||||
|
||||
/* check tag length, return error if we can't provide the value expected */
|
||||
if (tag_len > SHA1_DIGEST_SIZE) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
/* hash message, copy output into H */
|
||||
if (mbedtls_md_hmac_update(statev, message, msg_octets) != 0) {
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
|
||||
if (mbedtls_md_hmac_finish(state, hash_value) != 0) {
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
|
||||
/* copy hash_value to *result */
|
||||
for (i = 0; i < tag_len; i++) {
|
||||
result[i] = hash_value[i];
|
||||
}
|
||||
|
||||
debug_print(srtp_mod_hmac, "output: %s",
|
||||
srtp_octet_string_hex_string(hash_value, tag_len));
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/* end test case 0 */
|
||||
|
||||
static const char srtp_hmac_mbedtls_description[] =
|
||||
"hmac sha-1 authentication function using mbedtls";
|
||||
|
||||
/*
|
||||
* srtp_auth_type_t hmac is the hmac metaobject
|
||||
*/
|
||||
|
||||
const srtp_auth_type_t srtp_hmac = {
|
||||
srtp_hmac_mbedtls_alloc, /* */
|
||||
srtp_hmac_mbedtls_dealloc, /* */
|
||||
srtp_hmac_mbedtls_init, /* */
|
||||
srtp_hmac_mbedtls_compute, /* */
|
||||
srtp_hmac_mbedtls_update, /* */
|
||||
srtp_hmac_mbedtls_start, /* */
|
||||
srtp_hmac_mbedtls_description, /* */
|
||||
&srtp_hmac_test_case_0, /* */
|
||||
SRTP_HMAC_SHA1 /* */
|
||||
};
|
||||
291
libsrtp/crypto/hash/hmac_nss.c
Normal file
291
libsrtp/crypto/hash/hmac_nss.c
Normal file
@@ -0,0 +1,291 @@
|
||||
/*
|
||||
*
|
||||
* 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 "auth.h"
|
||||
#include "alloc.h"
|
||||
#include "err.h" /* for srtp_debug */
|
||||
#include "auth_test_cases.h"
|
||||
|
||||
#define NSS_PKCS11_2_0_COMPAT 1
|
||||
|
||||
#include <nss.h>
|
||||
#include <pk11pub.h>
|
||||
|
||||
#define SHA1_DIGEST_SIZE 20
|
||||
|
||||
/* the debug module for authentiation */
|
||||
|
||||
srtp_debug_module_t srtp_mod_hmac = {
|
||||
false, /* debugging is off by default */
|
||||
"hmac sha-1 nss" /* printable name for module */
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
NSSInitContext *nss;
|
||||
PK11SymKey *key;
|
||||
PK11Context *ctx;
|
||||
} srtp_hmac_nss_ctx_t;
|
||||
|
||||
static srtp_err_status_t srtp_hmac_alloc(srtp_auth_t **a,
|
||||
size_t key_len,
|
||||
size_t out_len)
|
||||
{
|
||||
extern const srtp_auth_type_t srtp_hmac;
|
||||
srtp_hmac_nss_ctx_t *hmac;
|
||||
NSSInitContext *nss;
|
||||
|
||||
debug_print(srtp_mod_hmac, "allocating auth func with key length %zu",
|
||||
key_len);
|
||||
debug_print(srtp_mod_hmac, " tag length %zu",
|
||||
out_len);
|
||||
|
||||
/* check output length - should be less than 20 bytes */
|
||||
if (out_len > SHA1_DIGEST_SIZE) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
/* Initialize NSS equiv of NSS_NoDB_Init(NULL) */
|
||||
nss = NSS_InitContext("", "", "", "", NULL,
|
||||
NSS_INIT_READONLY | NSS_INIT_NOCERTDB |
|
||||
NSS_INIT_NOMODDB | NSS_INIT_FORCEOPEN |
|
||||
NSS_INIT_OPTIMIZESPACE);
|
||||
if (!nss) {
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
|
||||
*a = (srtp_auth_t *)srtp_crypto_alloc(sizeof(srtp_auth_t));
|
||||
if (*a == NULL) {
|
||||
NSS_ShutdownContext(nss);
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
|
||||
hmac =
|
||||
(srtp_hmac_nss_ctx_t *)srtp_crypto_alloc(sizeof(srtp_hmac_nss_ctx_t));
|
||||
if (hmac == NULL) {
|
||||
NSS_ShutdownContext(nss);
|
||||
srtp_crypto_free(*a);
|
||||
*a = NULL;
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
|
||||
hmac->nss = nss;
|
||||
hmac->key = NULL;
|
||||
hmac->ctx = NULL;
|
||||
|
||||
/* set pointers */
|
||||
(*a)->state = hmac;
|
||||
(*a)->type = &srtp_hmac;
|
||||
(*a)->out_len = out_len;
|
||||
(*a)->key_len = key_len;
|
||||
(*a)->prefix_len = 0;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_hmac_dealloc(srtp_auth_t *a)
|
||||
{
|
||||
srtp_hmac_nss_ctx_t *hmac;
|
||||
|
||||
hmac = (srtp_hmac_nss_ctx_t *)a->state;
|
||||
if (hmac) {
|
||||
/* free any PK11 values that have been created */
|
||||
if (hmac->key) {
|
||||
PK11_FreeSymKey(hmac->key);
|
||||
hmac->key = NULL;
|
||||
}
|
||||
|
||||
if (hmac->ctx) {
|
||||
PK11_DestroyContext(hmac->ctx, PR_TRUE);
|
||||
hmac->ctx = NULL;
|
||||
}
|
||||
|
||||
if (hmac->nss) {
|
||||
NSS_ShutdownContext(hmac->nss);
|
||||
hmac->nss = NULL;
|
||||
}
|
||||
|
||||
/* zeroize everything */
|
||||
octet_string_set_to_zero(hmac, sizeof(srtp_hmac_nss_ctx_t));
|
||||
srtp_crypto_free(hmac);
|
||||
}
|
||||
|
||||
/* free memory */
|
||||
srtp_crypto_free(a);
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_hmac_start(void *statev)
|
||||
{
|
||||
srtp_hmac_nss_ctx_t *hmac;
|
||||
hmac = (srtp_hmac_nss_ctx_t *)statev;
|
||||
|
||||
if (PK11_DigestBegin(hmac->ctx) != SECSuccess) {
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_hmac_init(void *statev,
|
||||
const uint8_t *key,
|
||||
size_t key_len)
|
||||
{
|
||||
srtp_hmac_nss_ctx_t *hmac;
|
||||
hmac = (srtp_hmac_nss_ctx_t *)statev;
|
||||
PK11SymKey *sym_key;
|
||||
PK11Context *ctx;
|
||||
|
||||
if (hmac->ctx) {
|
||||
PK11_DestroyContext(hmac->ctx, PR_TRUE);
|
||||
hmac->ctx = NULL;
|
||||
}
|
||||
|
||||
if (hmac->key) {
|
||||
PK11_FreeSymKey(hmac->key);
|
||||
hmac->key = NULL;
|
||||
}
|
||||
|
||||
PK11SlotInfo *slot = PK11_GetBestSlot(CKM_SHA_1_HMAC, NULL);
|
||||
if (!slot) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
/* explicitly cast away const of key */
|
||||
SECItem key_item = { siBuffer, (unsigned char *)(uintptr_t)key, key_len };
|
||||
sym_key = PK11_ImportSymKey(slot, CKM_SHA_1_HMAC, PK11_OriginUnwrap,
|
||||
CKA_SIGN, &key_item, NULL);
|
||||
PK11_FreeSlot(slot);
|
||||
|
||||
if (!sym_key) {
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
|
||||
SECItem param_item = { siBuffer, NULL, 0 };
|
||||
ctx = PK11_CreateContextBySymKey(CKM_SHA_1_HMAC, CKA_SIGN, sym_key,
|
||||
¶m_item);
|
||||
if (!ctx) {
|
||||
PK11_FreeSymKey(sym_key);
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
|
||||
hmac->key = sym_key;
|
||||
hmac->ctx = ctx;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_hmac_update(void *statev,
|
||||
const uint8_t *message,
|
||||
size_t msg_octets)
|
||||
{
|
||||
srtp_hmac_nss_ctx_t *hmac;
|
||||
hmac = (srtp_hmac_nss_ctx_t *)statev;
|
||||
|
||||
debug_print(srtp_mod_hmac, "input: %s",
|
||||
srtp_octet_string_hex_string(message, msg_octets));
|
||||
|
||||
if (PK11_DigestOp(hmac->ctx, message, msg_octets) != SECSuccess) {
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_hmac_compute(void *statev,
|
||||
const uint8_t *message,
|
||||
size_t msg_octets,
|
||||
size_t tag_len,
|
||||
uint8_t *result)
|
||||
{
|
||||
srtp_hmac_nss_ctx_t *hmac;
|
||||
hmac = (srtp_hmac_nss_ctx_t *)statev;
|
||||
uint8_t hash_value[SHA1_DIGEST_SIZE];
|
||||
unsigned int len;
|
||||
|
||||
debug_print(srtp_mod_hmac, "input: %s",
|
||||
srtp_octet_string_hex_string(message, msg_octets));
|
||||
|
||||
/* check tag length, return error if we can't provide the value expected */
|
||||
if (tag_len > SHA1_DIGEST_SIZE) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
if (PK11_DigestOp(hmac->ctx, message, msg_octets) != SECSuccess) {
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
|
||||
if (PK11_DigestFinal(hmac->ctx, hash_value, &len, SHA1_DIGEST_SIZE) !=
|
||||
SECSuccess) {
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
|
||||
if (len < tag_len) {
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
|
||||
/* copy hash_value to *result */
|
||||
for (size_t i = 0; i < tag_len; i++) {
|
||||
result[i] = hash_value[i];
|
||||
}
|
||||
|
||||
debug_print(srtp_mod_hmac, "output: %s",
|
||||
srtp_octet_string_hex_string(hash_value, tag_len));
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static const char srtp_hmac_description[] =
|
||||
"hmac sha-1 authentication function";
|
||||
|
||||
/*
|
||||
* srtp_auth_type_t hmac is the hmac metaobject
|
||||
*/
|
||||
|
||||
const srtp_auth_type_t srtp_hmac = {
|
||||
srtp_hmac_alloc, /* */
|
||||
srtp_hmac_dealloc, /* */
|
||||
srtp_hmac_init, /* */
|
||||
srtp_hmac_compute, /* */
|
||||
srtp_hmac_update, /* */
|
||||
srtp_hmac_start, /* */
|
||||
srtp_hmac_description, /* */
|
||||
&srtp_hmac_test_case_0, /* */
|
||||
SRTP_HMAC_SHA1 /* */
|
||||
};
|
||||
343
libsrtp/crypto/hash/hmac_ossl.c
Normal file
343
libsrtp/crypto/hash/hmac_ossl.c
Normal file
@@ -0,0 +1,343 @@
|
||||
/*
|
||||
* hmac_ossl.c
|
||||
*
|
||||
* Implementation of hmac srtp_auth_type_t that leverages OpenSSL
|
||||
*
|
||||
* 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 "auth.h"
|
||||
#include "alloc.h"
|
||||
#include "err.h" /* for srtp_debug */
|
||||
#include "auth_test_cases.h"
|
||||
#include <openssl/evp.h>
|
||||
|
||||
#if defined(OPENSSL_VERSION_MAJOR) && (OPENSSL_VERSION_MAJOR >= 3)
|
||||
#define SRTP_OSSL_USE_EVP_MAC
|
||||
/* before this version reinit of EVP_MAC_CTX was not supported so need to
|
||||
* duplicate the CTX each time */
|
||||
#define SRTP_OSSL_MIN_REINIT_VERSION 0x30000030L
|
||||
#endif
|
||||
|
||||
#ifndef SRTP_OSSL_USE_EVP_MAC
|
||||
#include <openssl/hmac.h>
|
||||
#endif
|
||||
|
||||
#define SHA1_DIGEST_SIZE 20
|
||||
|
||||
/* the debug module for authentication */
|
||||
|
||||
srtp_debug_module_t srtp_mod_hmac = {
|
||||
false, /* debugging is off by default */
|
||||
"hmac sha-1 openssl" /* printable name for module */
|
||||
};
|
||||
|
||||
/*
|
||||
* There are three different behaviors of OpenSSL HMAC for different versions.
|
||||
*
|
||||
* 1. Pre-3.0 - Use HMAC API
|
||||
* 2. 3.0.0 - 3.0.2 - EVP API is required, but doesn't support reinitialization,
|
||||
* so we have to use EVP_MAC_CTX_dup
|
||||
* 3. 3.0.3 and later - EVP API is required and supports reinitialization
|
||||
*
|
||||
* The distinction between cases 2 & 3 needs to be made at runtime, because in a
|
||||
* shared library context you might end up building against 3.0.3 and running
|
||||
* against 3.0.2.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
#ifdef SRTP_OSSL_USE_EVP_MAC
|
||||
EVP_MAC *mac;
|
||||
EVP_MAC_CTX *ctx;
|
||||
int use_dup;
|
||||
EVP_MAC_CTX *ctx_dup;
|
||||
#else
|
||||
HMAC_CTX *ctx;
|
||||
#endif
|
||||
} srtp_hmac_ossl_ctx_t;
|
||||
|
||||
static srtp_err_status_t srtp_hmac_alloc(srtp_auth_t **a,
|
||||
size_t key_len,
|
||||
size_t out_len)
|
||||
{
|
||||
extern const srtp_auth_type_t srtp_hmac;
|
||||
srtp_hmac_ossl_ctx_t *hmac;
|
||||
|
||||
debug_print(srtp_mod_hmac, "allocating auth func with key length %zu",
|
||||
key_len);
|
||||
debug_print(srtp_mod_hmac, " tag length %zu",
|
||||
out_len);
|
||||
|
||||
/* check output length - should be less than 20 bytes */
|
||||
if (out_len > SHA1_DIGEST_SIZE) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
*a = (srtp_auth_t *)srtp_crypto_alloc(sizeof(srtp_auth_t));
|
||||
if (*a == NULL) {
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
|
||||
hmac =
|
||||
(srtp_hmac_ossl_ctx_t *)srtp_crypto_alloc(sizeof(srtp_hmac_ossl_ctx_t));
|
||||
if (hmac == NULL) {
|
||||
srtp_crypto_free(*a);
|
||||
*a = NULL;
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
|
||||
#ifdef SRTP_OSSL_USE_EVP_MAC
|
||||
hmac->mac = EVP_MAC_fetch(NULL, "HMAC", NULL);
|
||||
if (hmac->mac == NULL) {
|
||||
srtp_crypto_free(hmac);
|
||||
srtp_crypto_free(*a);
|
||||
*a = NULL;
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
|
||||
hmac->ctx = EVP_MAC_CTX_new(hmac->mac);
|
||||
if (hmac->ctx == NULL) {
|
||||
EVP_MAC_free(hmac->mac);
|
||||
srtp_crypto_free(hmac);
|
||||
srtp_crypto_free(*a);
|
||||
*a = NULL;
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
|
||||
hmac->use_dup =
|
||||
OpenSSL_version_num() < SRTP_OSSL_MIN_REINIT_VERSION ? 1 : 0;
|
||||
|
||||
if (hmac->use_dup) {
|
||||
debug_print0(srtp_mod_hmac, "using EVP_MAC_CTX_dup");
|
||||
hmac->ctx_dup = hmac->ctx;
|
||||
hmac->ctx = NULL;
|
||||
}
|
||||
#else
|
||||
hmac->ctx = HMAC_CTX_new();
|
||||
if (hmac->ctx == NULL) {
|
||||
srtp_crypto_free(hmac);
|
||||
srtp_crypto_free(*a);
|
||||
*a = NULL;
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* set pointers */
|
||||
(*a)->state = hmac;
|
||||
(*a)->type = &srtp_hmac;
|
||||
(*a)->out_len = out_len;
|
||||
(*a)->key_len = key_len;
|
||||
(*a)->prefix_len = 0;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_hmac_dealloc(srtp_auth_t *a)
|
||||
{
|
||||
srtp_hmac_ossl_ctx_t *hmac = (srtp_hmac_ossl_ctx_t *)a->state;
|
||||
|
||||
if (hmac) {
|
||||
#ifdef SRTP_OSSL_USE_EVP_MAC
|
||||
EVP_MAC_CTX_free(hmac->ctx);
|
||||
EVP_MAC_CTX_free(hmac->ctx_dup);
|
||||
EVP_MAC_free(hmac->mac);
|
||||
#else
|
||||
HMAC_CTX_free(hmac->ctx);
|
||||
#endif
|
||||
/* zeroize entire state*/
|
||||
octet_string_set_to_zero(hmac, sizeof(srtp_hmac_ossl_ctx_t));
|
||||
|
||||
srtp_crypto_free(hmac);
|
||||
}
|
||||
|
||||
/* zeroize entire state*/
|
||||
octet_string_set_to_zero(a, sizeof(srtp_auth_t));
|
||||
|
||||
/* free memory */
|
||||
srtp_crypto_free(a);
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_hmac_start(void *statev)
|
||||
{
|
||||
srtp_hmac_ossl_ctx_t *hmac = (srtp_hmac_ossl_ctx_t *)statev;
|
||||
|
||||
#ifdef SRTP_OSSL_USE_EVP_MAC
|
||||
if (hmac->use_dup) {
|
||||
EVP_MAC_CTX_free(hmac->ctx);
|
||||
hmac->ctx = EVP_MAC_CTX_dup(hmac->ctx_dup);
|
||||
if (hmac->ctx == NULL) {
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
} else {
|
||||
if (EVP_MAC_init(hmac->ctx, NULL, 0, NULL) == 0) {
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
}
|
||||
#else
|
||||
if (HMAC_Init_ex(hmac->ctx, NULL, 0, NULL, NULL) == 0) {
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
#endif
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_hmac_init(void *statev,
|
||||
const uint8_t *key,
|
||||
size_t key_len)
|
||||
{
|
||||
srtp_hmac_ossl_ctx_t *hmac = (srtp_hmac_ossl_ctx_t *)statev;
|
||||
|
||||
#ifdef SRTP_OSSL_USE_EVP_MAC
|
||||
OSSL_PARAM params[2];
|
||||
|
||||
params[0] = OSSL_PARAM_construct_utf8_string("digest", "SHA1", 0);
|
||||
params[1] = OSSL_PARAM_construct_end();
|
||||
|
||||
if (EVP_MAC_init(hmac->use_dup ? hmac->ctx_dup : hmac->ctx, key, key_len,
|
||||
params) == 0) {
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
#else
|
||||
if (HMAC_Init_ex(hmac->ctx, key, key_len, EVP_sha1(), NULL) == 0) {
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
#endif
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_hmac_update(void *statev,
|
||||
const uint8_t *message,
|
||||
size_t msg_octets)
|
||||
{
|
||||
srtp_hmac_ossl_ctx_t *hmac = (srtp_hmac_ossl_ctx_t *)statev;
|
||||
|
||||
debug_print(srtp_mod_hmac, "input: %s",
|
||||
srtp_octet_string_hex_string(message, msg_octets));
|
||||
|
||||
#ifdef SRTP_OSSL_USE_EVP_MAC
|
||||
if (EVP_MAC_update(hmac->ctx, message, msg_octets) == 0) {
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
#else
|
||||
if (HMAC_Update(hmac->ctx, message, msg_octets) == 0) {
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
#endif
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_hmac_compute(void *statev,
|
||||
const uint8_t *message,
|
||||
size_t msg_octets,
|
||||
size_t tag_len,
|
||||
uint8_t *result)
|
||||
{
|
||||
srtp_hmac_ossl_ctx_t *hmac = (srtp_hmac_ossl_ctx_t *)statev;
|
||||
uint8_t hash_value[SHA1_DIGEST_SIZE];
|
||||
#ifdef SRTP_OSSL_USE_EVP_MAC
|
||||
size_t len;
|
||||
#else
|
||||
unsigned int len;
|
||||
#endif
|
||||
|
||||
debug_print(srtp_mod_hmac, "input: %s",
|
||||
srtp_octet_string_hex_string(message, msg_octets));
|
||||
|
||||
/* check tag length, return error if we can't provide the value expected */
|
||||
if (tag_len > SHA1_DIGEST_SIZE) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
/* hash message, copy output into H */
|
||||
#ifdef SRTP_OSSL_USE_EVP_MAC
|
||||
if (EVP_MAC_update(hmac->ctx, message, msg_octets) == 0) {
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
|
||||
if (EVP_MAC_final(hmac->ctx, hash_value, &len, sizeof hash_value) == 0) {
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
#else
|
||||
if (HMAC_Update(hmac->ctx, message, msg_octets) == 0) {
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
|
||||
if (HMAC_Final(hmac->ctx, hash_value, &len) == 0) {
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
#endif
|
||||
if (len < tag_len) {
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
|
||||
/* copy hash_value to *result */
|
||||
for (size_t i = 0; i < tag_len; i++) {
|
||||
result[i] = hash_value[i];
|
||||
}
|
||||
|
||||
debug_print(srtp_mod_hmac, "output: %s",
|
||||
srtp_octet_string_hex_string(hash_value, tag_len));
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static const char srtp_hmac_description[] =
|
||||
"hmac sha-1 authentication function";
|
||||
|
||||
/*
|
||||
* srtp_auth_type_t hmac is the hmac metaobject
|
||||
*/
|
||||
|
||||
const srtp_auth_type_t srtp_hmac = {
|
||||
srtp_hmac_alloc, /* */
|
||||
srtp_hmac_dealloc, /* */
|
||||
srtp_hmac_init, /* */
|
||||
srtp_hmac_compute, /* */
|
||||
srtp_hmac_update, /* */
|
||||
srtp_hmac_start, /* */
|
||||
srtp_hmac_description, /* */
|
||||
&srtp_hmac_test_case_0, /* */
|
||||
SRTP_HMAC_SHA1 /* */
|
||||
};
|
||||
228
libsrtp/crypto/hash/hmac_wssl.c
Normal file
228
libsrtp/crypto/hash/hmac_wssl.c
Normal file
@@ -0,0 +1,228 @@
|
||||
/*
|
||||
* hmac_wssl.c
|
||||
*
|
||||
* Implementation of hmac srtp_auth_type_t that uses wolfSSL
|
||||
*
|
||||
* Sean Parkinson, wolfSSL
|
||||
*/
|
||||
/*
|
||||
*
|
||||
* 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
|
||||
#ifndef WOLFSSL_USER_SETTINGS
|
||||
#include <wolfssl/options.h>
|
||||
#endif
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/wolfcrypt/hmac.h>
|
||||
#include "auth.h"
|
||||
#include "alloc.h"
|
||||
#include "err.h" /* for srtp_debug */
|
||||
#include "auth_test_cases.h"
|
||||
|
||||
#define SHA1_DIGEST_SIZE 20
|
||||
|
||||
/* the debug module for authentiation */
|
||||
|
||||
srtp_debug_module_t srtp_mod_hmac = {
|
||||
0, /* debugging is off by default */
|
||||
"hmac sha-1 wssl" /* printable name for module */
|
||||
};
|
||||
|
||||
static srtp_err_status_t srtp_hmac_wolfssl_alloc(srtp_auth_t **a,
|
||||
size_t key_len,
|
||||
size_t out_len)
|
||||
{
|
||||
extern const srtp_auth_type_t srtp_hmac;
|
||||
int err;
|
||||
|
||||
debug_print(srtp_mod_hmac, "allocating auth func with key length %zu",
|
||||
key_len);
|
||||
debug_print(srtp_mod_hmac, " tag length %zu",
|
||||
out_len);
|
||||
|
||||
/* check output length - should be less than 20 bytes */
|
||||
if (out_len > SHA1_DIGEST_SIZE) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
*a = (srtp_auth_t *)srtp_crypto_alloc(sizeof(srtp_auth_t));
|
||||
if (*a == NULL) {
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
// allocate the buffer of wolfssl context.
|
||||
(*a)->state = srtp_crypto_alloc(sizeof(Hmac));
|
||||
if ((*a)->state == NULL) {
|
||||
srtp_crypto_free(*a);
|
||||
*a = NULL;
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
err = wc_HmacInit((Hmac *)(*a)->state, NULL, INVALID_DEVID);
|
||||
if (err < 0) {
|
||||
srtp_crypto_free((*a)->state);
|
||||
srtp_crypto_free(*a);
|
||||
*a = NULL;
|
||||
debug_print(srtp_mod_hmac, "wolfSSL error code: %d", err);
|
||||
return srtp_err_status_init_fail;
|
||||
}
|
||||
|
||||
/* set pointers */
|
||||
(*a)->type = &srtp_hmac;
|
||||
(*a)->out_len = out_len;
|
||||
(*a)->key_len = key_len;
|
||||
(*a)->prefix_len = 0;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_hmac_wolfssl_dealloc(srtp_auth_t *a)
|
||||
{
|
||||
wc_HmacFree((Hmac *)a->state);
|
||||
srtp_crypto_free(a->state);
|
||||
/* zeroize entire state*/
|
||||
octet_string_set_to_zero(a, sizeof(srtp_auth_t));
|
||||
|
||||
/* free memory */
|
||||
srtp_crypto_free(a);
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_hmac_wolfssl_start(void *statev)
|
||||
{
|
||||
(void)statev;
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_hmac_wolfssl_init(void *statev,
|
||||
const uint8_t *key,
|
||||
size_t key_len)
|
||||
{
|
||||
Hmac *state = (Hmac *)statev;
|
||||
int err;
|
||||
|
||||
err = wc_HmacSetKey(state, WC_SHA, key, key_len);
|
||||
if (err < 0) {
|
||||
debug_print(srtp_mod_hmac, "wolfSSL error code: %d", err);
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_hmac_wolfssl_update(void *statev,
|
||||
const uint8_t *message,
|
||||
size_t msg_octets)
|
||||
{
|
||||
Hmac *state = (Hmac *)statev;
|
||||
int err;
|
||||
|
||||
debug_print(srtp_mod_hmac, "input: %s",
|
||||
srtp_octet_string_hex_string(message, msg_octets));
|
||||
|
||||
err = wc_HmacUpdate(state, message, msg_octets);
|
||||
if (err < 0) {
|
||||
debug_print(srtp_mod_hmac, "wolfSSL error code: %d", err);
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_hmac_wolfssl_compute(void *statev,
|
||||
const uint8_t *message,
|
||||
size_t msg_octets,
|
||||
size_t tag_len,
|
||||
uint8_t *result)
|
||||
{
|
||||
Hmac *state = (Hmac *)statev;
|
||||
uint8_t hash_value[WC_SHA_DIGEST_SIZE];
|
||||
int err;
|
||||
int i;
|
||||
|
||||
debug_print(srtp_mod_hmac, "input: %s",
|
||||
srtp_octet_string_hex_string(message, msg_octets));
|
||||
|
||||
/* check tag length, return error if we can't provide the value expected */
|
||||
if (tag_len > WC_SHA_DIGEST_SIZE) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
/* hash message, copy output into H */
|
||||
err = wc_HmacUpdate(state, message, msg_octets);
|
||||
if (err < 0) {
|
||||
debug_print(srtp_mod_hmac, "wolfSSL error code: %d", err);
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
|
||||
err = wc_HmacFinal(state, hash_value);
|
||||
if (err < 0) {
|
||||
debug_print(srtp_mod_hmac, "wolfSSL error code: %d", err);
|
||||
return srtp_err_status_auth_fail;
|
||||
}
|
||||
|
||||
/* copy hash_value to *result */
|
||||
for (i = 0; i < (int)tag_len; i++) {
|
||||
result[i] = hash_value[i];
|
||||
}
|
||||
|
||||
debug_print(srtp_mod_hmac, "output: %s",
|
||||
srtp_octet_string_hex_string(hash_value, tag_len));
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/* end test case 0 */
|
||||
|
||||
static const char srtp_hmac_wolfssl_description[] =
|
||||
"hmac sha-1 authentication function using wolfSSL";
|
||||
|
||||
/*
|
||||
* srtp_auth_type_t hmac is the hmac metaobject
|
||||
*/
|
||||
|
||||
const srtp_auth_type_t srtp_hmac = {
|
||||
srtp_hmac_wolfssl_alloc, /* */
|
||||
srtp_hmac_wolfssl_dealloc, /* */
|
||||
srtp_hmac_wolfssl_init, /* */
|
||||
srtp_hmac_wolfssl_compute, /* */
|
||||
srtp_hmac_wolfssl_update, /* */
|
||||
srtp_hmac_wolfssl_start, /* */
|
||||
srtp_hmac_wolfssl_description, /* */
|
||||
&srtp_hmac_test_case_0, /* */
|
||||
SRTP_HMAC_SHA1 /* */
|
||||
};
|
||||
181
libsrtp/crypto/hash/null_auth.c
Normal file
181
libsrtp/crypto/hash/null_auth.c
Normal file
@@ -0,0 +1,181 @@
|
||||
/*
|
||||
* null_auth.c
|
||||
*
|
||||
* implements the do-nothing auth algorithm
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-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 "null_auth.h"
|
||||
#include "err.h" /* for srtp_debug */
|
||||
#include "alloc.h"
|
||||
#include "cipher_types.h"
|
||||
|
||||
static srtp_err_status_t srtp_null_auth_alloc(srtp_auth_t **a,
|
||||
size_t key_len,
|
||||
size_t out_len)
|
||||
{
|
||||
extern const srtp_auth_type_t srtp_null_auth;
|
||||
uint8_t *pointer;
|
||||
|
||||
debug_print(srtp_mod_auth, "allocating auth func with key length %zu",
|
||||
key_len);
|
||||
debug_print(srtp_mod_auth, " tag length %zu",
|
||||
out_len);
|
||||
|
||||
/* allocate memory for auth and srtp_null_auth_ctx_t structures */
|
||||
pointer = (uint8_t *)srtp_crypto_alloc(sizeof(srtp_null_auth_ctx_t) +
|
||||
sizeof(srtp_auth_t));
|
||||
if (pointer == NULL) {
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
|
||||
/* set pointers */
|
||||
*a = (srtp_auth_t *)pointer;
|
||||
(*a)->type = &srtp_null_auth;
|
||||
(*a)->state = pointer + sizeof(srtp_auth_t);
|
||||
(*a)->out_len = out_len;
|
||||
(*a)->prefix_len = out_len;
|
||||
(*a)->key_len = key_len;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_null_auth_dealloc(srtp_auth_t *a)
|
||||
{
|
||||
extern const srtp_auth_type_t srtp_null_auth;
|
||||
|
||||
/* zeroize entire state*/
|
||||
octet_string_set_to_zero(a, sizeof(srtp_null_auth_ctx_t) +
|
||||
sizeof(srtp_auth_t));
|
||||
|
||||
/* free memory */
|
||||
srtp_crypto_free(a);
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_null_auth_init(void *statev,
|
||||
const uint8_t *key,
|
||||
size_t key_len)
|
||||
{
|
||||
/* srtp_null_auth_ctx_t *state = (srtp_null_auth_ctx_t *)statev; */
|
||||
(void)statev;
|
||||
(void)key;
|
||||
(void)key_len;
|
||||
|
||||
/* accept any length of key, and do nothing */
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_null_auth_compute(void *statev,
|
||||
const uint8_t *message,
|
||||
size_t msg_octets,
|
||||
size_t tag_len,
|
||||
uint8_t *result)
|
||||
{
|
||||
/* srtp_null_auth_ctx_t *state = (srtp_null_auth_ctx_t *)statev; */
|
||||
(void)statev;
|
||||
(void)message;
|
||||
(void)msg_octets;
|
||||
(void)tag_len;
|
||||
(void)result;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_null_auth_update(void *statev,
|
||||
const uint8_t *message,
|
||||
size_t msg_octets)
|
||||
{
|
||||
/* srtp_null_auth_ctx_t *state = (srtp_null_auth_ctx_t *)statev; */
|
||||
(void)statev;
|
||||
(void)message;
|
||||
(void)msg_octets;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_status_t srtp_null_auth_start(void *statev)
|
||||
{
|
||||
/* srtp_null_auth_ctx_t *state = (srtp_null_auth_ctx_t *)statev; */
|
||||
(void)statev;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* srtp_auth_type_t - defines description, test case, and null_auth
|
||||
* metaobject
|
||||
*/
|
||||
|
||||
/* begin test case 0 */
|
||||
|
||||
static const srtp_auth_test_case_t srtp_null_auth_test_case_0 = {
|
||||
0, /* octets in key */
|
||||
NULL, /* key */
|
||||
0, /* octets in data */
|
||||
NULL, /* data */
|
||||
0, /* octets in tag */
|
||||
NULL, /* tag */
|
||||
NULL /* pointer to next testcase */
|
||||
};
|
||||
|
||||
/* end test case 0 */
|
||||
|
||||
static const char srtp_null_auth_description[] = "null authentication function";
|
||||
|
||||
const srtp_auth_type_t srtp_null_auth = {
|
||||
srtp_null_auth_alloc, /* */
|
||||
srtp_null_auth_dealloc, /* */
|
||||
srtp_null_auth_init, /* */
|
||||
srtp_null_auth_compute, /* */
|
||||
srtp_null_auth_update, /* */
|
||||
srtp_null_auth_start, /* */
|
||||
srtp_null_auth_description, /* */
|
||||
&srtp_null_auth_test_case_0, /* */
|
||||
SRTP_NULL_AUTH /* */
|
||||
};
|
||||
463
libsrtp/crypto/hash/sha1.c
Normal file
463
libsrtp/crypto/hash/sha1.c
Normal file
@@ -0,0 +1,463 @@
|
||||
/*
|
||||
* sha1.c
|
||||
*
|
||||
* an implementation of the Secure Hash Algorithm v.1 (SHA-1),
|
||||
* specified in FIPS 180-1
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-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 "sha1.h"
|
||||
|
||||
srtp_debug_module_t srtp_mod_sha1 = {
|
||||
false, /* debugging is off by default */
|
||||
"sha-1" /* printable module name */
|
||||
};
|
||||
|
||||
/* SN == Rotate left N bits */
|
||||
#define S1(X) ((X << 1) | (X >> 31))
|
||||
#define S5(X) ((X << 5) | (X >> 27))
|
||||
#define S30(X) ((X << 30) | (X >> 2))
|
||||
|
||||
#define f0(B, C, D) ((B & C) | (~B & D))
|
||||
#define f1(B, C, D) (B ^ C ^ D)
|
||||
#define f2(B, C, D) ((B & C) | (B & D) | (C & D))
|
||||
#define f3(B, C, D) (B ^ C ^ D)
|
||||
|
||||
/*
|
||||
* nota bene: the variable K0 appears in the curses library, so we
|
||||
* give longer names to these variables to avoid spurious warnings
|
||||
* on systems that uses curses
|
||||
*/
|
||||
|
||||
uint32_t SHA_K0 = 0x5A827999; /* Kt for 0 <= t <= 19 */
|
||||
uint32_t SHA_K1 = 0x6ED9EBA1; /* Kt for 20 <= t <= 39 */
|
||||
uint32_t SHA_K2 = 0x8F1BBCDC; /* Kt for 40 <= t <= 59 */
|
||||
uint32_t SHA_K3 = 0xCA62C1D6; /* Kt for 60 <= t <= 79 */
|
||||
|
||||
/*
|
||||
* srtp_sha1_core(M, H) computes the core compression function, where M is
|
||||
* the next part of the message (in network byte order) and H is the
|
||||
* intermediate state { H0, H1, ...} (in host byte order)
|
||||
*
|
||||
* this function does not do any of the padding required in the
|
||||
* complete SHA1 function
|
||||
*
|
||||
* this function is used in the SEAL 3.0 key setup routines
|
||||
* (crypto/cipher/seal.c)
|
||||
*/
|
||||
|
||||
void srtp_sha1_core(const uint32_t M[16], uint32_t hash_value[5])
|
||||
{
|
||||
uint32_t H0;
|
||||
uint32_t H1;
|
||||
uint32_t H2;
|
||||
uint32_t H3;
|
||||
uint32_t H4;
|
||||
uint32_t W[80];
|
||||
uint32_t A, B, C, D, E, TEMP;
|
||||
size_t t;
|
||||
|
||||
/* copy hash_value into H0, H1, H2, H3, H4 */
|
||||
H0 = hash_value[0];
|
||||
H1 = hash_value[1];
|
||||
H2 = hash_value[2];
|
||||
H3 = hash_value[3];
|
||||
H4 = hash_value[4];
|
||||
|
||||
/* copy/xor message into array */
|
||||
|
||||
W[0] = be32_to_cpu(M[0]);
|
||||
W[1] = be32_to_cpu(M[1]);
|
||||
W[2] = be32_to_cpu(M[2]);
|
||||
W[3] = be32_to_cpu(M[3]);
|
||||
W[4] = be32_to_cpu(M[4]);
|
||||
W[5] = be32_to_cpu(M[5]);
|
||||
W[6] = be32_to_cpu(M[6]);
|
||||
W[7] = be32_to_cpu(M[7]);
|
||||
W[8] = be32_to_cpu(M[8]);
|
||||
W[9] = be32_to_cpu(M[9]);
|
||||
W[10] = be32_to_cpu(M[10]);
|
||||
W[11] = be32_to_cpu(M[11]);
|
||||
W[12] = be32_to_cpu(M[12]);
|
||||
W[13] = be32_to_cpu(M[13]);
|
||||
W[14] = be32_to_cpu(M[14]);
|
||||
W[15] = be32_to_cpu(M[15]);
|
||||
TEMP = W[13] ^ W[8] ^ W[2] ^ W[0];
|
||||
W[16] = S1(TEMP);
|
||||
TEMP = W[14] ^ W[9] ^ W[3] ^ W[1];
|
||||
W[17] = S1(TEMP);
|
||||
TEMP = W[15] ^ W[10] ^ W[4] ^ W[2];
|
||||
W[18] = S1(TEMP);
|
||||
TEMP = W[16] ^ W[11] ^ W[5] ^ W[3];
|
||||
W[19] = S1(TEMP);
|
||||
TEMP = W[17] ^ W[12] ^ W[6] ^ W[4];
|
||||
W[20] = S1(TEMP);
|
||||
TEMP = W[18] ^ W[13] ^ W[7] ^ W[5];
|
||||
W[21] = S1(TEMP);
|
||||
TEMP = W[19] ^ W[14] ^ W[8] ^ W[6];
|
||||
W[22] = S1(TEMP);
|
||||
TEMP = W[20] ^ W[15] ^ W[9] ^ W[7];
|
||||
W[23] = S1(TEMP);
|
||||
TEMP = W[21] ^ W[16] ^ W[10] ^ W[8];
|
||||
W[24] = S1(TEMP);
|
||||
TEMP = W[22] ^ W[17] ^ W[11] ^ W[9];
|
||||
W[25] = S1(TEMP);
|
||||
TEMP = W[23] ^ W[18] ^ W[12] ^ W[10];
|
||||
W[26] = S1(TEMP);
|
||||
TEMP = W[24] ^ W[19] ^ W[13] ^ W[11];
|
||||
W[27] = S1(TEMP);
|
||||
TEMP = W[25] ^ W[20] ^ W[14] ^ W[12];
|
||||
W[28] = S1(TEMP);
|
||||
TEMP = W[26] ^ W[21] ^ W[15] ^ W[13];
|
||||
W[29] = S1(TEMP);
|
||||
TEMP = W[27] ^ W[22] ^ W[16] ^ W[14];
|
||||
W[30] = S1(TEMP);
|
||||
TEMP = W[28] ^ W[23] ^ W[17] ^ W[15];
|
||||
W[31] = S1(TEMP);
|
||||
|
||||
/* process the remainder of the array */
|
||||
for (t = 32; t < 80; t++) {
|
||||
TEMP = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16];
|
||||
W[t] = S1(TEMP);
|
||||
}
|
||||
|
||||
A = H0;
|
||||
B = H1;
|
||||
C = H2;
|
||||
D = H3;
|
||||
E = H4;
|
||||
|
||||
for (t = 0; t < 20; t++) {
|
||||
TEMP = S5(A) + f0(B, C, D) + E + W[t] + SHA_K0;
|
||||
E = D;
|
||||
D = C;
|
||||
C = S30(B);
|
||||
B = A;
|
||||
A = TEMP;
|
||||
}
|
||||
for (; t < 40; t++) {
|
||||
TEMP = S5(A) + f1(B, C, D) + E + W[t] + SHA_K1;
|
||||
E = D;
|
||||
D = C;
|
||||
C = S30(B);
|
||||
B = A;
|
||||
A = TEMP;
|
||||
}
|
||||
for (; t < 60; t++) {
|
||||
TEMP = S5(A) + f2(B, C, D) + E + W[t] + SHA_K2;
|
||||
E = D;
|
||||
D = C;
|
||||
C = S30(B);
|
||||
B = A;
|
||||
A = TEMP;
|
||||
}
|
||||
for (; t < 80; t++) {
|
||||
TEMP = S5(A) + f3(B, C, D) + E + W[t] + SHA_K3;
|
||||
E = D;
|
||||
D = C;
|
||||
C = S30(B);
|
||||
B = A;
|
||||
A = TEMP;
|
||||
}
|
||||
|
||||
hash_value[0] = H0 + A;
|
||||
hash_value[1] = H1 + B;
|
||||
hash_value[2] = H2 + C;
|
||||
hash_value[3] = H3 + D;
|
||||
hash_value[4] = H4 + E;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void srtp_sha1_init(srtp_sha1_ctx_t *ctx)
|
||||
{
|
||||
/* initialize state vector */
|
||||
ctx->H[0] = 0x67452301;
|
||||
ctx->H[1] = 0xefcdab89;
|
||||
ctx->H[2] = 0x98badcfe;
|
||||
ctx->H[3] = 0x10325476;
|
||||
ctx->H[4] = 0xc3d2e1f0;
|
||||
|
||||
/* indicate that message buffer is empty */
|
||||
ctx->octets_in_buffer = 0;
|
||||
|
||||
/* reset message bit-count to zero */
|
||||
ctx->num_bits_in_msg = 0;
|
||||
}
|
||||
|
||||
void srtp_sha1_update(srtp_sha1_ctx_t *ctx,
|
||||
const uint8_t *msg,
|
||||
size_t octets_in_msg)
|
||||
{
|
||||
size_t i;
|
||||
uint8_t *buf = (uint8_t *)ctx->M;
|
||||
|
||||
/* update message bit-count */
|
||||
ctx->num_bits_in_msg += (uint32_t)octets_in_msg * 8;
|
||||
|
||||
/* loop over 16-word blocks of M */
|
||||
while (octets_in_msg > 0) {
|
||||
if (octets_in_msg + ctx->octets_in_buffer >= 64) {
|
||||
/*
|
||||
* copy words of M into msg buffer until that buffer is full,
|
||||
* converting them into host byte order as needed
|
||||
*/
|
||||
octets_in_msg -= (64 - ctx->octets_in_buffer);
|
||||
for (i = ctx->octets_in_buffer; i < 64; i++) {
|
||||
buf[i] = *msg++;
|
||||
}
|
||||
ctx->octets_in_buffer = 0;
|
||||
|
||||
/* process a whole block */
|
||||
|
||||
debug_print0(srtp_mod_sha1, "(update) running srtp_sha1_core()");
|
||||
|
||||
srtp_sha1_core(ctx->M, ctx->H);
|
||||
|
||||
} else {
|
||||
debug_print0(srtp_mod_sha1,
|
||||
"(update) not running srtp_sha1_core()");
|
||||
|
||||
for (i = ctx->octets_in_buffer;
|
||||
i < (ctx->octets_in_buffer + octets_in_msg); i++) {
|
||||
buf[i] = *msg++;
|
||||
}
|
||||
ctx->octets_in_buffer += octets_in_msg;
|
||||
octets_in_msg = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* srtp_sha1_final(ctx, output) computes the result for ctx and copies it
|
||||
* into the twenty octets located at *output
|
||||
*/
|
||||
|
||||
void srtp_sha1_final(srtp_sha1_ctx_t *ctx, uint32_t output[5])
|
||||
{
|
||||
uint32_t A, B, C, D, E, TEMP;
|
||||
uint32_t W[80];
|
||||
size_t i, t;
|
||||
|
||||
/*
|
||||
* process the remaining octets_in_buffer, padding and terminating as
|
||||
* necessary
|
||||
*/
|
||||
{
|
||||
size_t tail = ctx->octets_in_buffer % 4;
|
||||
|
||||
/* copy/xor message into array */
|
||||
for (i = 0; i < (ctx->octets_in_buffer + 3) / 4; i++) {
|
||||
W[i] = be32_to_cpu(ctx->M[i]);
|
||||
}
|
||||
|
||||
/* set the high bit of the octet immediately following the message */
|
||||
switch (tail) {
|
||||
case (3):
|
||||
W[i - 1] = (be32_to_cpu(ctx->M[i - 1]) & 0xffffff00) | 0x80;
|
||||
W[i] = 0x0;
|
||||
break;
|
||||
case (2):
|
||||
W[i - 1] = (be32_to_cpu(ctx->M[i - 1]) & 0xffff0000) | 0x8000;
|
||||
W[i] = 0x0;
|
||||
break;
|
||||
case (1):
|
||||
W[i - 1] = (be32_to_cpu(ctx->M[i - 1]) & 0xff000000) | 0x800000;
|
||||
W[i] = 0x0;
|
||||
break;
|
||||
case (0):
|
||||
W[i] = 0x80000000;
|
||||
break;
|
||||
}
|
||||
|
||||
/* zeroize remaining words */
|
||||
for (i++; i < 15; i++) {
|
||||
W[i] = 0x0;
|
||||
}
|
||||
|
||||
/*
|
||||
* if there is room at the end of the word array, then set the
|
||||
* last word to the bit-length of the message; otherwise, set that
|
||||
* word to zero and then we need to do one more run of the
|
||||
* compression algo.
|
||||
*/
|
||||
if (ctx->octets_in_buffer < 56) {
|
||||
W[15] = ctx->num_bits_in_msg;
|
||||
} else if (ctx->octets_in_buffer < 60) {
|
||||
W[15] = 0x0;
|
||||
}
|
||||
|
||||
/* process the word array */
|
||||
for (t = 16; t < 80; t++) {
|
||||
TEMP = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16];
|
||||
W[t] = S1(TEMP);
|
||||
}
|
||||
|
||||
A = ctx->H[0];
|
||||
B = ctx->H[1];
|
||||
C = ctx->H[2];
|
||||
D = ctx->H[3];
|
||||
E = ctx->H[4];
|
||||
|
||||
for (t = 0; t < 20; t++) {
|
||||
TEMP = S5(A) + f0(B, C, D) + E + W[t] + SHA_K0;
|
||||
E = D;
|
||||
D = C;
|
||||
C = S30(B);
|
||||
B = A;
|
||||
A = TEMP;
|
||||
}
|
||||
for (; t < 40; t++) {
|
||||
TEMP = S5(A) + f1(B, C, D) + E + W[t] + SHA_K1;
|
||||
E = D;
|
||||
D = C;
|
||||
C = S30(B);
|
||||
B = A;
|
||||
A = TEMP;
|
||||
}
|
||||
for (; t < 60; t++) {
|
||||
TEMP = S5(A) + f2(B, C, D) + E + W[t] + SHA_K2;
|
||||
E = D;
|
||||
D = C;
|
||||
C = S30(B);
|
||||
B = A;
|
||||
A = TEMP;
|
||||
}
|
||||
for (; t < 80; t++) {
|
||||
TEMP = S5(A) + f3(B, C, D) + E + W[t] + SHA_K3;
|
||||
E = D;
|
||||
D = C;
|
||||
C = S30(B);
|
||||
B = A;
|
||||
A = TEMP;
|
||||
}
|
||||
|
||||
ctx->H[0] += A;
|
||||
ctx->H[1] += B;
|
||||
ctx->H[2] += C;
|
||||
ctx->H[3] += D;
|
||||
ctx->H[4] += E;
|
||||
}
|
||||
|
||||
debug_print0(srtp_mod_sha1, "(final) running srtp_sha1_core()");
|
||||
|
||||
if (ctx->octets_in_buffer >= 56) {
|
||||
debug_print0(srtp_mod_sha1, "(final) running srtp_sha1_core() again");
|
||||
|
||||
/* we need to do one final run of the compression algo */
|
||||
|
||||
/*
|
||||
* set initial part of word array to zeros, and set the
|
||||
* final part to the number of bits in the message
|
||||
*/
|
||||
for (i = 0; i < 15; i++) {
|
||||
W[i] = 0x0;
|
||||
}
|
||||
W[15] = ctx->num_bits_in_msg;
|
||||
|
||||
/* process the word array */
|
||||
for (t = 16; t < 80; t++) {
|
||||
TEMP = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16];
|
||||
W[t] = S1(TEMP);
|
||||
}
|
||||
|
||||
A = ctx->H[0];
|
||||
B = ctx->H[1];
|
||||
C = ctx->H[2];
|
||||
D = ctx->H[3];
|
||||
E = ctx->H[4];
|
||||
|
||||
for (t = 0; t < 20; t++) {
|
||||
TEMP = S5(A) + f0(B, C, D) + E + W[t] + SHA_K0;
|
||||
E = D;
|
||||
D = C;
|
||||
C = S30(B);
|
||||
B = A;
|
||||
A = TEMP;
|
||||
}
|
||||
for (; t < 40; t++) {
|
||||
TEMP = S5(A) + f1(B, C, D) + E + W[t] + SHA_K1;
|
||||
E = D;
|
||||
D = C;
|
||||
C = S30(B);
|
||||
B = A;
|
||||
A = TEMP;
|
||||
}
|
||||
for (; t < 60; t++) {
|
||||
TEMP = S5(A) + f2(B, C, D) + E + W[t] + SHA_K2;
|
||||
E = D;
|
||||
D = C;
|
||||
C = S30(B);
|
||||
B = A;
|
||||
A = TEMP;
|
||||
}
|
||||
for (; t < 80; t++) {
|
||||
TEMP = S5(A) + f3(B, C, D) + E + W[t] + SHA_K3;
|
||||
E = D;
|
||||
D = C;
|
||||
C = S30(B);
|
||||
B = A;
|
||||
A = TEMP;
|
||||
}
|
||||
|
||||
ctx->H[0] += A;
|
||||
ctx->H[1] += B;
|
||||
ctx->H[2] += C;
|
||||
ctx->H[3] += D;
|
||||
ctx->H[4] += E;
|
||||
}
|
||||
|
||||
/* copy result into output buffer */
|
||||
output[0] = be32_to_cpu(ctx->H[0]);
|
||||
output[1] = be32_to_cpu(ctx->H[1]);
|
||||
output[2] = be32_to_cpu(ctx->H[2]);
|
||||
output[3] = be32_to_cpu(ctx->H[3]);
|
||||
output[4] = be32_to_cpu(ctx->H[4]);
|
||||
|
||||
/* indicate that message buffer in context is empty */
|
||||
ctx->octets_in_buffer = 0;
|
||||
|
||||
return;
|
||||
}
|
||||
83
libsrtp/crypto/include/aes.h
Normal file
83
libsrtp/crypto/include/aes.h
Normal file
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* aes.h
|
||||
*
|
||||
* header file for the AES block cipher
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef AES_H
|
||||
#define AES_H
|
||||
|
||||
#include "datatypes.h"
|
||||
#include "err.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* aes internals */
|
||||
|
||||
typedef struct {
|
||||
v128_t round[15];
|
||||
size_t num_rounds;
|
||||
} srtp_aes_expanded_key_t;
|
||||
|
||||
srtp_err_status_t srtp_aes_expand_encryption_key(
|
||||
const uint8_t *key,
|
||||
size_t key_len,
|
||||
srtp_aes_expanded_key_t *expanded_key);
|
||||
|
||||
srtp_err_status_t srtp_aes_expand_decryption_key(
|
||||
const uint8_t *key,
|
||||
size_t key_len,
|
||||
srtp_aes_expanded_key_t *expanded_key);
|
||||
|
||||
void srtp_aes_encrypt(v128_t *plaintext,
|
||||
const srtp_aes_expanded_key_t *exp_key);
|
||||
|
||||
void srtp_aes_decrypt(v128_t *plaintext,
|
||||
const srtp_aes_expanded_key_t *exp_key);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* AES_H */
|
||||
134
libsrtp/crypto/include/aes_gcm.h
Normal file
134
libsrtp/crypto/include/aes_gcm.h
Normal file
@@ -0,0 +1,134 @@
|
||||
/*
|
||||
* aes_gcm.h
|
||||
*
|
||||
* Header for 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef AES_GCM_H
|
||||
#define AES_GCM_H
|
||||
|
||||
#include "cipher.h"
|
||||
#include "srtp.h"
|
||||
#include "datatypes.h"
|
||||
|
||||
#ifdef OPENSSL
|
||||
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/aes.h>
|
||||
|
||||
typedef struct {
|
||||
size_t key_size;
|
||||
size_t tag_len;
|
||||
EVP_CIPHER_CTX *ctx;
|
||||
srtp_cipher_direction_t dir;
|
||||
} srtp_aes_gcm_ctx_t;
|
||||
|
||||
#endif /* OPENSSL */
|
||||
|
||||
#ifdef WOLFSSL
|
||||
#define MAX_AD_SIZE 2048
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
#ifndef WOLFSSL_USER_SETTINGS
|
||||
#include <wolfssl/options.h>
|
||||
#endif
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/wolfcrypt/aes.h>
|
||||
|
||||
typedef struct {
|
||||
size_t key_size;
|
||||
size_t tag_len;
|
||||
#ifndef WOLFSSL_AESGCM_STREAM
|
||||
size_t aad_size;
|
||||
size_t iv_len;
|
||||
uint8_t iv[GCM_NONCE_MID_SZ];
|
||||
uint8_t aad[MAX_AD_SIZE];
|
||||
#endif
|
||||
Aes *ctx;
|
||||
srtp_cipher_direction_t dir;
|
||||
} srtp_aes_gcm_ctx_t;
|
||||
|
||||
#endif /* WOLFSSL */
|
||||
|
||||
#ifdef MBEDTLS
|
||||
#define MAX_AD_SIZE 2048
|
||||
#include <mbedtls/aes.h>
|
||||
#include <mbedtls/gcm.h>
|
||||
|
||||
typedef struct {
|
||||
size_t key_size;
|
||||
size_t tag_len;
|
||||
size_t aad_size;
|
||||
size_t iv_len;
|
||||
uint8_t iv[12];
|
||||
uint8_t aad[MAX_AD_SIZE];
|
||||
mbedtls_gcm_context *ctx;
|
||||
srtp_cipher_direction_t dir;
|
||||
} srtp_aes_gcm_ctx_t;
|
||||
|
||||
#endif /* MBEDTLS */
|
||||
|
||||
#ifdef NSS
|
||||
|
||||
#define NSS_PKCS11_2_0_COMPAT 1
|
||||
|
||||
#include <nss.h>
|
||||
#include <pk11pub.h>
|
||||
|
||||
#define MAX_AD_SIZE 2048
|
||||
|
||||
typedef struct {
|
||||
size_t key_size;
|
||||
size_t tag_size;
|
||||
srtp_cipher_direction_t dir;
|
||||
NSSInitContext *nss;
|
||||
PK11SymKey *key;
|
||||
uint8_t iv[12];
|
||||
uint8_t aad[MAX_AD_SIZE];
|
||||
size_t aad_size;
|
||||
CK_GCM_PARAMS params;
|
||||
} srtp_aes_gcm_ctx_t;
|
||||
|
||||
#endif /* NSS */
|
||||
|
||||
#endif /* AES_GCM_H */
|
||||
62
libsrtp/crypto/include/aes_icm.h
Normal file
62
libsrtp/crypto/include/aes_icm.h
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* aes_icm.h
|
||||
*
|
||||
* Header for AES Integer Counter Mode.
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef AES_ICM_H
|
||||
#define AES_ICM_H
|
||||
|
||||
#include "aes.h"
|
||||
#include "cipher.h"
|
||||
|
||||
typedef struct {
|
||||
v128_t counter; /* holds the counter value */
|
||||
v128_t offset; /* initial offset value */
|
||||
v128_t keystream_buffer; /* buffers bytes of keystream */
|
||||
srtp_aes_expanded_key_t expanded_key; /* the cipher key */
|
||||
size_t bytes_in_buffer; /* number of unused bytes in buffer */
|
||||
size_t key_size; /* AES key size + 14 byte SALT */
|
||||
} srtp_aes_icm_ctx_t;
|
||||
|
||||
#endif /* AES_ICM_H */
|
||||
111
libsrtp/crypto/include/aes_icm_ext.h
Normal file
111
libsrtp/crypto/include/aes_icm_ext.h
Normal file
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* aes_icm.h
|
||||
*
|
||||
* Header for AES Integer Counter Mode.
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*
|
||||
*/
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef AES_ICM_H
|
||||
#define AES_ICM_H
|
||||
|
||||
#include "cipher.h"
|
||||
#include "datatypes.h"
|
||||
|
||||
#ifdef OPENSSL
|
||||
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/aes.h>
|
||||
|
||||
typedef struct {
|
||||
v128_t counter; /* holds the counter value */
|
||||
v128_t offset; /* initial offset value */
|
||||
size_t key_size;
|
||||
EVP_CIPHER_CTX *ctx;
|
||||
} srtp_aes_icm_ctx_t;
|
||||
|
||||
#endif /* OPENSSL */
|
||||
|
||||
#ifdef WOLFSSL
|
||||
|
||||
#include <wolfssl/wolfcrypt/aes.h>
|
||||
typedef struct {
|
||||
v128_t counter; /* holds the counter value */
|
||||
v128_t offset; /* initial offset value */
|
||||
int key_size;
|
||||
Aes *ctx;
|
||||
} srtp_aes_icm_ctx_t;
|
||||
|
||||
#endif /* WOLFSSL */
|
||||
|
||||
#ifdef MBEDTLS
|
||||
|
||||
#include <mbedtls/aes.h>
|
||||
typedef struct {
|
||||
v128_t counter; /* holds the counter value */
|
||||
v128_t offset; /* initial offset value */
|
||||
v128_t stream_block;
|
||||
size_t nc_off;
|
||||
size_t key_size;
|
||||
mbedtls_aes_context *ctx;
|
||||
} srtp_aes_icm_ctx_t;
|
||||
|
||||
#endif /* MBEDTLS */
|
||||
|
||||
#ifdef NSS
|
||||
|
||||
#define NSS_PKCS11_2_0_COMPAT 1
|
||||
|
||||
#include <nss.h>
|
||||
#include <pk11pub.h>
|
||||
|
||||
typedef struct {
|
||||
v128_t counter;
|
||||
v128_t offset;
|
||||
size_t key_size;
|
||||
uint8_t iv[16];
|
||||
NSSInitContext *nss;
|
||||
PK11SymKey *key;
|
||||
PK11Context *ctx;
|
||||
} srtp_aes_icm_ctx_t;
|
||||
|
||||
#endif /* NSS */
|
||||
|
||||
#endif /* AES_ICM_H */
|
||||
76
libsrtp/crypto/include/alloc.h
Normal file
76
libsrtp/crypto/include/alloc.h
Normal file
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* alloc.h
|
||||
*
|
||||
* interface to memory allocation and deallocation, with optional debugging
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*/
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CRYPTO_ALLOC_H
|
||||
#define CRYPTO_ALLOC_H
|
||||
|
||||
#include "datatypes.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
* srtp_crypto_alloc
|
||||
*
|
||||
* Allocates a block of memory of given size. The memory will be
|
||||
* initialized to zero's. Free the memory with a call to srtp_crypto_free.
|
||||
*
|
||||
* returns pointer to memory on success or else NULL
|
||||
*/
|
||||
void *srtp_crypto_alloc(size_t size);
|
||||
|
||||
/*
|
||||
* srtp_crypto_free
|
||||
*
|
||||
* Frees the block of memory ptr previously allocated with
|
||||
* srtp_crypto_alloc
|
||||
*/
|
||||
void srtp_crypto_free(void *ptr);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* CRYPTO_ALLOC_H */
|
||||
173
libsrtp/crypto/include/auth.h
Normal file
173
libsrtp/crypto/include/auth.h
Normal file
@@ -0,0 +1,173 @@
|
||||
/*
|
||||
* auth.h
|
||||
*
|
||||
* common interface to authentication functions
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef SRTP_AUTH_H
|
||||
#define SRTP_AUTH_H
|
||||
|
||||
#include "srtp.h"
|
||||
#include "crypto_types.h" /* for values of auth_type_id_t */
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef const struct srtp_auth_type_t *srtp_auth_type_pointer;
|
||||
typedef struct srtp_auth_t *srtp_auth_pointer_t;
|
||||
|
||||
typedef srtp_err_status_t (*srtp_auth_alloc_func)(srtp_auth_pointer_t *ap,
|
||||
size_t key_len,
|
||||
size_t out_len);
|
||||
|
||||
typedef srtp_err_status_t (*srtp_auth_init_func)(void *state,
|
||||
const uint8_t *key,
|
||||
size_t key_len);
|
||||
|
||||
typedef srtp_err_status_t (*srtp_auth_dealloc_func)(srtp_auth_pointer_t ap);
|
||||
|
||||
typedef srtp_err_status_t (*srtp_auth_compute_func)(void *state,
|
||||
const uint8_t *buffer,
|
||||
size_t octets_to_auth,
|
||||
size_t tag_len,
|
||||
uint8_t *tag);
|
||||
|
||||
typedef srtp_err_status_t (*srtp_auth_update_func)(void *state,
|
||||
const uint8_t *buffer,
|
||||
size_t octets_to_auth);
|
||||
|
||||
typedef srtp_err_status_t (*srtp_auth_start_func)(void *state);
|
||||
|
||||
/* some syntactic sugar on these function types */
|
||||
#define srtp_auth_type_alloc(at, a, klen, outlen) \
|
||||
((at)->alloc((a), (klen), (outlen)))
|
||||
|
||||
#define srtp_auth_init(a, key) \
|
||||
(((a)->type)->init((a)->state, (key), ((a)->key_len)))
|
||||
|
||||
#define srtp_auth_compute(a, buf, len, res) \
|
||||
(((a)->type)->compute((a)->state, (buf), (len), (a)->out_len, (res)))
|
||||
|
||||
#define srtp_auth_update(a, buf, len) \
|
||||
(((a)->type)->update((a)->state, (buf), (len)))
|
||||
|
||||
#define srtp_auth_start(a) (((a)->type)->start((a)->state))
|
||||
|
||||
#define srtp_auth_dealloc(c) (((c)->type)->dealloc(c))
|
||||
|
||||
/* functions to get information about a particular auth_t */
|
||||
size_t srtp_auth_get_key_length(const struct srtp_auth_t *a);
|
||||
|
||||
size_t srtp_auth_get_tag_length(const struct srtp_auth_t *a);
|
||||
|
||||
size_t srtp_auth_get_prefix_length(const struct srtp_auth_t *a);
|
||||
|
||||
/*
|
||||
* srtp_auth_test_case_t is a (list of) key/message/tag values that are
|
||||
* known to be correct for a particular cipher. this data can be used
|
||||
* to test an implementation in an on-the-fly self test of the
|
||||
* correctness of the implementation. (see the srtp_auth_type_self_test()
|
||||
* function below)
|
||||
*/
|
||||
typedef struct srtp_auth_test_case_t {
|
||||
size_t key_length_octets; /* octets in key */
|
||||
const uint8_t *key; /* key */
|
||||
size_t data_length_octets; /* octets in data */
|
||||
const uint8_t *data; /* data */
|
||||
size_t tag_length_octets; /* octets in tag */
|
||||
const uint8_t *tag; /* tag */
|
||||
const struct srtp_auth_test_case_t
|
||||
*next_test_case; /* pointer to next testcase */
|
||||
} srtp_auth_test_case_t;
|
||||
|
||||
/* srtp_auth_type_t */
|
||||
typedef struct srtp_auth_type_t {
|
||||
srtp_auth_alloc_func alloc;
|
||||
srtp_auth_dealloc_func dealloc;
|
||||
srtp_auth_init_func init;
|
||||
srtp_auth_compute_func compute;
|
||||
srtp_auth_update_func update;
|
||||
srtp_auth_start_func start;
|
||||
const char *description;
|
||||
const srtp_auth_test_case_t *test_data;
|
||||
srtp_auth_type_id_t id;
|
||||
} srtp_auth_type_t;
|
||||
|
||||
typedef struct srtp_auth_t {
|
||||
const srtp_auth_type_t *type;
|
||||
void *state;
|
||||
size_t out_len; /* length of output tag in octets */
|
||||
size_t key_len; /* length of key in octets */
|
||||
size_t prefix_len; /* length of keystream prefix */
|
||||
} srtp_auth_t;
|
||||
|
||||
/*
|
||||
* srtp_auth_type_self_test() tests an auth_type against test cases
|
||||
* provided in an array of values of key/message/tag that is known to
|
||||
* be good
|
||||
*/
|
||||
srtp_err_status_t srtp_auth_type_self_test(const srtp_auth_type_t *at);
|
||||
|
||||
/*
|
||||
* srtp_auth_type_test() tests an auth_type against external test cases
|
||||
* provided in an array of values of key/message/tag that is known to
|
||||
* be good
|
||||
*/
|
||||
srtp_err_status_t srtp_auth_type_test(const srtp_auth_type_t *at,
|
||||
const srtp_auth_test_case_t *test_data);
|
||||
|
||||
/*
|
||||
* srtp_replace_auth_type(ct, id)
|
||||
*
|
||||
* replaces srtp's kernel's auth type implementation for the auth_type id
|
||||
* with a new one passed in externally. The new auth type must pass all the
|
||||
* existing auth_type's self tests as well as its own.
|
||||
*/
|
||||
srtp_err_status_t srtp_replace_auth_type(const srtp_auth_type_t *ct,
|
||||
srtp_auth_type_id_t id);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* SRTP_AUTH_H */
|
||||
242
libsrtp/crypto/include/cipher.h
Normal file
242
libsrtp/crypto/include/cipher.h
Normal file
@@ -0,0 +1,242 @@
|
||||
/*
|
||||
* cipher.h
|
||||
*
|
||||
* common interface to ciphers
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*/
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef SRTP_CIPHER_H
|
||||
#define SRTP_CIPHER_H
|
||||
|
||||
#include "srtp.h"
|
||||
#include "crypto_types.h" /* for values of cipher_type_id_t */
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
* srtp_cipher_direction_t defines a particular cipher operation.
|
||||
*
|
||||
* A srtp_cipher_direction_t is an enum that describes a particular cipher
|
||||
* operation, i.e. encryption or decryption. For some ciphers, this
|
||||
* distinction does not matter, but for others, it is essential.
|
||||
*/
|
||||
typedef enum {
|
||||
srtp_direction_encrypt, /**< encryption (convert plaintext to ciphertext) */
|
||||
srtp_direction_decrypt, /**< decryption (convert ciphertext to plaintext) */
|
||||
srtp_direction_any /**< encryption or decryption */
|
||||
} srtp_cipher_direction_t;
|
||||
|
||||
/*
|
||||
* the srtp_cipher_pointer_t definition is needed
|
||||
* as srtp_cipher_t is not yet defined
|
||||
*/
|
||||
typedef struct srtp_cipher_t *srtp_cipher_pointer_t;
|
||||
|
||||
/*
|
||||
* a srtp_cipher_alloc_func_t allocates (but does not initialize) a
|
||||
* srtp_cipher_t
|
||||
*/
|
||||
typedef srtp_err_status_t (*srtp_cipher_alloc_func_t)(srtp_cipher_pointer_t *cp,
|
||||
size_t key_len,
|
||||
size_t tag_len);
|
||||
|
||||
/*
|
||||
* a srtp_cipher_init_func_t [re-]initializes a cipher_t with a given key
|
||||
*/
|
||||
typedef srtp_err_status_t (*srtp_cipher_init_func_t)(void *state,
|
||||
const uint8_t *key);
|
||||
|
||||
/* a srtp_cipher_dealloc_func_t de-allocates a cipher_t */
|
||||
typedef srtp_err_status_t (*srtp_cipher_dealloc_func_t)(
|
||||
srtp_cipher_pointer_t cp);
|
||||
|
||||
/*
|
||||
* a srtp_cipher_set_aad_func_t processes the AAD data for AEAD ciphers
|
||||
*/
|
||||
typedef srtp_err_status_t (*srtp_cipher_set_aad_func_t)(void *state,
|
||||
const uint8_t *aad,
|
||||
size_t aad_len);
|
||||
|
||||
/* a srtp_cipher_encrypt_func_t encrypts data in-place */
|
||||
typedef srtp_err_status_t (*srtp_cipher_encrypt_func_t)(void *state,
|
||||
const uint8_t *src,
|
||||
size_t src_len,
|
||||
uint8_t *dst,
|
||||
size_t *dst_len);
|
||||
|
||||
/* a srtp_cipher_decrypt_func_t decrypts data in-place */
|
||||
typedef srtp_err_status_t (*srtp_cipher_decrypt_func_t)(void *state,
|
||||
const uint8_t *src,
|
||||
size_t src_len,
|
||||
uint8_t *dst,
|
||||
size_t *dst_len);
|
||||
|
||||
/*
|
||||
* a srtp_cipher_set_iv_func_t function sets the current initialization vector
|
||||
*/
|
||||
typedef srtp_err_status_t (*srtp_cipher_set_iv_func_t)(
|
||||
void *state,
|
||||
uint8_t *iv,
|
||||
srtp_cipher_direction_t direction);
|
||||
|
||||
/*
|
||||
* srtp_cipher_test_case_t is a (list of) key, salt, plaintext, ciphertext,
|
||||
* and aad values that are known to be correct for a
|
||||
* particular cipher. this data can be used to test an implementation
|
||||
* in an on-the-fly self test of the correctness of the implementation.
|
||||
* (see the srtp_cipher_type_self_test() function below)
|
||||
*/
|
||||
typedef struct srtp_cipher_test_case_t {
|
||||
size_t key_length_octets; /* octets in key */
|
||||
const uint8_t *key; /* key */
|
||||
uint8_t *idx; /* packet index */
|
||||
size_t plaintext_length_octets; /* octets in plaintext */
|
||||
const uint8_t *plaintext; /* plaintext */
|
||||
size_t ciphertext_length_octets; /* octets in plaintext */
|
||||
const uint8_t *ciphertext; /* ciphertext */
|
||||
size_t aad_length_octets; /* octets in AAD */
|
||||
const uint8_t *aad; /* AAD */
|
||||
size_t tag_length_octets; /* Length of AEAD tag */
|
||||
const struct srtp_cipher_test_case_t
|
||||
*next_test_case; /* pointer to next testcase */
|
||||
} srtp_cipher_test_case_t;
|
||||
|
||||
/* srtp_cipher_type_t defines the 'metadata' for a particular cipher type */
|
||||
typedef struct srtp_cipher_type_t {
|
||||
srtp_cipher_alloc_func_t alloc;
|
||||
srtp_cipher_dealloc_func_t dealloc;
|
||||
srtp_cipher_init_func_t init;
|
||||
srtp_cipher_set_aad_func_t set_aad;
|
||||
srtp_cipher_encrypt_func_t encrypt;
|
||||
srtp_cipher_decrypt_func_t decrypt;
|
||||
srtp_cipher_set_iv_func_t set_iv;
|
||||
const char *description;
|
||||
const srtp_cipher_test_case_t *test_data;
|
||||
srtp_cipher_type_id_t id;
|
||||
} srtp_cipher_type_t;
|
||||
|
||||
/*
|
||||
* srtp_cipher_t defines an instantiation of a particular cipher, with fixed
|
||||
* key length, key and salt values
|
||||
*/
|
||||
typedef struct srtp_cipher_t {
|
||||
const srtp_cipher_type_t *type;
|
||||
void *state;
|
||||
size_t key_len;
|
||||
srtp_cipher_type_id_t algorithm;
|
||||
} srtp_cipher_t;
|
||||
|
||||
/* some bookkeeping functions */
|
||||
size_t srtp_cipher_get_key_length(const srtp_cipher_t *c);
|
||||
|
||||
/*
|
||||
* srtp_cipher_type_self_test() tests a cipher against test cases provided in
|
||||
* an array of values of key/srtp_xtd_seq_num_t/plaintext/ciphertext
|
||||
* that is known to be good
|
||||
*/
|
||||
srtp_err_status_t srtp_cipher_type_self_test(const srtp_cipher_type_t *ct);
|
||||
|
||||
/*
|
||||
* srtp_cipher_type_test() tests a cipher against external test cases provided
|
||||
* in
|
||||
* an array of values of key/srtp_xtd_seq_num_t/plaintext/ciphertext
|
||||
* that is known to be good
|
||||
*/
|
||||
srtp_err_status_t srtp_cipher_type_test(
|
||||
const srtp_cipher_type_t *ct,
|
||||
const srtp_cipher_test_case_t *test_data);
|
||||
|
||||
/*
|
||||
* srtp_cipher_bits_per_second(c, l, t) computes (an estimate of) the
|
||||
* number of bits that a cipher implementation can encrypt in a second
|
||||
*
|
||||
* c is a cipher (which MUST be allocated and initialized already), l
|
||||
* is the length in octets of the test data to be encrypted, and t is
|
||||
* the number of trials
|
||||
*
|
||||
* if an error is encountered, then the value 0 is returned
|
||||
*/
|
||||
uint64_t srtp_cipher_bits_per_second(srtp_cipher_t *c,
|
||||
size_t octets_in_buffer,
|
||||
size_t num_trials);
|
||||
|
||||
srtp_err_status_t srtp_cipher_type_alloc(const srtp_cipher_type_t *ct,
|
||||
srtp_cipher_t **c,
|
||||
size_t key_len,
|
||||
size_t tlen);
|
||||
srtp_err_status_t srtp_cipher_dealloc(srtp_cipher_t *c);
|
||||
srtp_err_status_t srtp_cipher_init(srtp_cipher_t *c, const uint8_t *key);
|
||||
srtp_err_status_t srtp_cipher_set_iv(srtp_cipher_t *c,
|
||||
uint8_t *iv,
|
||||
srtp_cipher_direction_t direction);
|
||||
srtp_err_status_t srtp_cipher_output(srtp_cipher_t *c,
|
||||
uint8_t *buffer,
|
||||
size_t *num_octets_to_output);
|
||||
srtp_err_status_t srtp_cipher_encrypt(srtp_cipher_t *c,
|
||||
const uint8_t *src,
|
||||
size_t src_len,
|
||||
uint8_t *dst,
|
||||
size_t *dst_len);
|
||||
srtp_err_status_t srtp_cipher_decrypt(srtp_cipher_t *c,
|
||||
const uint8_t *src,
|
||||
size_t src_len,
|
||||
uint8_t *dst,
|
||||
size_t *dst_len);
|
||||
srtp_err_status_t srtp_cipher_set_aad(srtp_cipher_t *c,
|
||||
const uint8_t *aad,
|
||||
size_t aad_len);
|
||||
|
||||
/*
|
||||
* srtp_replace_cipher_type(ct, id)
|
||||
*
|
||||
* replaces srtp's existing cipher implementation for the cipher_type id
|
||||
* with a new one passed in externally. The new cipher must pass all the
|
||||
* existing cipher_type's self tests as well as its own.
|
||||
*/
|
||||
srtp_err_status_t srtp_replace_cipher_type(const srtp_cipher_type_t *ct,
|
||||
srtp_cipher_type_id_t id);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* SRTP_CIPHER_H */
|
||||
62
libsrtp/crypto/include/cipher_priv.h
Normal file
62
libsrtp/crypto/include/cipher_priv.h
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
*
|
||||
* Copyright(c) 2001-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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef SRTP_CIHPER_PRIV_H
|
||||
#define SRTP_CIHPER_PRIV_H
|
||||
|
||||
#include "cipher.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
* A trivial platform independent random source.
|
||||
* For use in test only.
|
||||
*/
|
||||
void srtp_cipher_rand_for_tests(uint8_t *dest, size_t len);
|
||||
|
||||
/*
|
||||
* A trivial platform independent 32 bit random number.
|
||||
* For use in test only.
|
||||
*/
|
||||
uint32_t srtp_cipher_rand_u32_for_tests(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* SRTP_CIPHER_PRIV_H */
|
||||
80
libsrtp/crypto/include/cipher_types.h
Normal file
80
libsrtp/crypto/include/cipher_types.h
Normal file
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
*
|
||||
* Copyright(c) 2001-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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CIHPER_TYPES_H
|
||||
#define CIHPER_TYPES_H
|
||||
|
||||
#include "cipher.h"
|
||||
#include "auth.h"
|
||||
|
||||
/*
|
||||
* cipher types that can be included in the kernel
|
||||
*/
|
||||
|
||||
extern const srtp_cipher_type_t srtp_null_cipher;
|
||||
extern const srtp_cipher_type_t srtp_aes_icm_128;
|
||||
extern const srtp_cipher_type_t srtp_aes_icm_256;
|
||||
#ifdef GCM
|
||||
extern const srtp_cipher_type_t srtp_aes_icm_192;
|
||||
extern const srtp_cipher_type_t srtp_aes_gcm_128;
|
||||
extern const srtp_cipher_type_t srtp_aes_gcm_256;
|
||||
#endif
|
||||
|
||||
/*
|
||||
* auth func types that can be included in the kernel
|
||||
*/
|
||||
|
||||
extern const srtp_auth_type_t srtp_null_auth;
|
||||
extern const srtp_auth_type_t srtp_hmac;
|
||||
|
||||
/*
|
||||
* other generic debug modules that can be included in the kernel
|
||||
*/
|
||||
|
||||
extern srtp_debug_module_t srtp_mod_auth;
|
||||
extern srtp_debug_module_t srtp_mod_cipher;
|
||||
extern srtp_debug_module_t srtp_mod_alloc;
|
||||
|
||||
/* debug modules for cipher types */
|
||||
extern srtp_debug_module_t srtp_mod_aes_icm;
|
||||
|
||||
#if defined(OPENSSL) || defined(WOLFSSL) || defined(MBEDTLS) || defined(NSS)
|
||||
extern srtp_debug_module_t srtp_mod_aes_gcm;
|
||||
#endif
|
||||
|
||||
/* debug modules for auth types */
|
||||
extern srtp_debug_module_t srtp_mod_hmac;
|
||||
#endif
|
||||
215
libsrtp/crypto/include/crypto_kernel.h
Normal file
215
libsrtp/crypto/include/crypto_kernel.h
Normal file
@@ -0,0 +1,215 @@
|
||||
/*
|
||||
* crypto_kernel.h
|
||||
*
|
||||
* header for the cryptographic kernel
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*/
|
||||
/*
|
||||
*
|
||||
* Copyright(c) 2001-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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CRYPTO_KERNEL
|
||||
#define CRYPTO_KERNEL
|
||||
|
||||
#include "cipher.h"
|
||||
#include "auth.h"
|
||||
#include "err.h"
|
||||
#include "crypto_types.h"
|
||||
#include "key.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
* crypto_kernel_state_t defines the possible states:
|
||||
*
|
||||
* insecure - not yet initialized
|
||||
* secure - initialized and passed self-tests
|
||||
*/
|
||||
typedef enum {
|
||||
srtp_crypto_kernel_state_insecure,
|
||||
srtp_crypto_kernel_state_secure
|
||||
} srtp_crypto_kernel_state_t;
|
||||
|
||||
/*
|
||||
* linked list of cipher types
|
||||
*/
|
||||
typedef struct srtp_kernel_cipher_type {
|
||||
srtp_cipher_type_id_t id;
|
||||
const srtp_cipher_type_t *cipher_type;
|
||||
struct srtp_kernel_cipher_type *next;
|
||||
} srtp_kernel_cipher_type_t;
|
||||
|
||||
/*
|
||||
* linked list of auth types
|
||||
*/
|
||||
typedef struct srtp_kernel_auth_type {
|
||||
srtp_auth_type_id_t id;
|
||||
const srtp_auth_type_t *auth_type;
|
||||
struct srtp_kernel_auth_type *next;
|
||||
} srtp_kernel_auth_type_t;
|
||||
|
||||
/*
|
||||
* linked list of debug modules
|
||||
*/
|
||||
typedef struct srtp_kernel_debug_module {
|
||||
srtp_debug_module_t *mod;
|
||||
struct srtp_kernel_debug_module *next;
|
||||
} srtp_kernel_debug_module_t;
|
||||
|
||||
/*
|
||||
* crypto_kernel_t is the data structure for the crypto kernel
|
||||
*
|
||||
* note that there is *exactly one* instance of this data type,
|
||||
* a global variable defined in crypto_kernel.c
|
||||
*/
|
||||
typedef struct {
|
||||
srtp_crypto_kernel_state_t state; /* current state of kernel */
|
||||
srtp_kernel_cipher_type_t *cipher_type_list; /* list of all cipher types */
|
||||
srtp_kernel_auth_type_t *auth_type_list; /* list of all auth func types */
|
||||
srtp_kernel_debug_module_t
|
||||
*debug_module_list; /* list of all debug modules */
|
||||
} srtp_crypto_kernel_t;
|
||||
|
||||
/*
|
||||
* srtp_crypto_kernel_t external api
|
||||
*/
|
||||
|
||||
/*
|
||||
* The function srtp_crypto_kernel_init() initialized the crypto kernel and
|
||||
* runs the self-test operations on the random number generators and
|
||||
* crypto algorithms. Possible return values are:
|
||||
*
|
||||
* srtp_err_status_ok initialization successful
|
||||
* <other> init failure
|
||||
*
|
||||
* If any value other than srtp_err_status_ok is returned, the
|
||||
* crypto_kernel MUST NOT be used.
|
||||
*/
|
||||
srtp_err_status_t srtp_crypto_kernel_init(void);
|
||||
|
||||
/*
|
||||
* The function srtp_crypto_kernel_shutdown() de-initializes the
|
||||
* crypto_kernel, zeroizes keys and other cryptographic material, and
|
||||
* deallocates any dynamically allocated memory. Possible return
|
||||
* values are:
|
||||
*
|
||||
* srtp_err_status_ok shutdown successful
|
||||
* <other> shutdown failure
|
||||
*
|
||||
*/
|
||||
srtp_err_status_t srtp_crypto_kernel_shutdown(void);
|
||||
|
||||
/*
|
||||
* The function srtp_crypto_kernel_stats() checks the the crypto_kernel,
|
||||
* running tests on the ciphers, auth funcs, and rng, and prints out a
|
||||
* status report. Possible return values are:
|
||||
*
|
||||
* srtp_err_status_ok all tests were passed
|
||||
* <other> a test failed
|
||||
*
|
||||
*/
|
||||
srtp_err_status_t srtp_crypto_kernel_status(void);
|
||||
|
||||
/*
|
||||
* srtp_crypto_kernel_list_debug_modules() outputs a list of debugging modules
|
||||
*
|
||||
*/
|
||||
srtp_err_status_t srtp_crypto_kernel_list_debug_modules(void);
|
||||
|
||||
/*
|
||||
* srtp_crypto_kernel_load_cipher_type()
|
||||
*
|
||||
*/
|
||||
srtp_err_status_t srtp_crypto_kernel_load_cipher_type(
|
||||
const srtp_cipher_type_t *ct,
|
||||
srtp_cipher_type_id_t id);
|
||||
|
||||
srtp_err_status_t srtp_crypto_kernel_load_auth_type(const srtp_auth_type_t *ct,
|
||||
srtp_auth_type_id_t id);
|
||||
|
||||
srtp_err_status_t srtp_crypto_kernel_load_debug_module(
|
||||
srtp_debug_module_t *new_dm);
|
||||
|
||||
/*
|
||||
* srtp_crypto_kernel_alloc_cipher(id, cp, key_len);
|
||||
*
|
||||
* allocates a cipher of type id at location *cp, with key length
|
||||
* key_len octets. Return values are:
|
||||
*
|
||||
* srtp_err_status_ok no problems
|
||||
* srtp_err_status_alloc_fail an allocation failure occured
|
||||
* srtp_err_status_fail couldn't find cipher with identifier 'id'
|
||||
*/
|
||||
srtp_err_status_t srtp_crypto_kernel_alloc_cipher(srtp_cipher_type_id_t id,
|
||||
srtp_cipher_pointer_t *cp,
|
||||
size_t key_len,
|
||||
size_t tag_len);
|
||||
|
||||
/*
|
||||
* srtp_crypto_kernel_alloc_auth(id, ap, key_len, tag_len);
|
||||
*
|
||||
* allocates an auth function of type id at location *ap, with key
|
||||
* length key_len octets and output tag length of tag_len. Return
|
||||
* values are:
|
||||
*
|
||||
* srtp_err_status_ok no problems
|
||||
* srtp_err_status_alloc_fail an allocation failure occured
|
||||
* srtp_err_status_fail couldn't find auth with identifier 'id'
|
||||
*/
|
||||
srtp_err_status_t srtp_crypto_kernel_alloc_auth(srtp_auth_type_id_t id,
|
||||
srtp_auth_pointer_t *ap,
|
||||
size_t key_len,
|
||||
size_t tag_len);
|
||||
|
||||
/*
|
||||
* srtp_crypto_kernel_set_debug_module(mod_name, v)
|
||||
*
|
||||
* sets dynamic debugging to the value v (false for off, true for on) for the
|
||||
* debug module with the name mod_name
|
||||
*
|
||||
* returns srtp_err_status_ok on success, srtp_err_status_fail otherwise
|
||||
*/
|
||||
srtp_err_status_t srtp_crypto_kernel_set_debug_module(const char *mod_name,
|
||||
bool v);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* CRYPTO_KERNEL */
|
||||
116
libsrtp/crypto/include/crypto_types.h
Normal file
116
libsrtp/crypto/include/crypto_types.h
Normal file
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* crypto_types.h
|
||||
*
|
||||
* constants for cipher types and auth func types
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*/
|
||||
/*
|
||||
*
|
||||
* Copyright(c) 2001-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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef SRTP_CRYPTO_TYPES_H
|
||||
#define SRTP_CRYPTO_TYPES_H
|
||||
|
||||
/*
|
||||
* The null cipher performs no encryption.
|
||||
*
|
||||
* The SRTP_NULL_CIPHER leaves its inputs unaltered, during both the
|
||||
* encryption and decryption operations. This cipher can be chosen
|
||||
* to indicate that no encryption is to be performed.
|
||||
*/
|
||||
#define SRTP_NULL_CIPHER 0
|
||||
|
||||
/*
|
||||
* AES-128 Integer Counter Mode (AES ICM)
|
||||
*
|
||||
* AES-128 ICM is the variant of counter mode that is used by
|
||||
* Secure RTP. This cipher uses a 16-octet key concatenated with a
|
||||
* 14-octet offset (or salt) value.
|
||||
*/
|
||||
#define SRTP_AES_ICM_128 1
|
||||
|
||||
/*
|
||||
* AES-192 Integer Counter Mode (AES ICM)
|
||||
*
|
||||
* AES-128 ICM is the variant of counter mode that is used by
|
||||
* Secure RTP. This cipher uses a 24-octet key concatenated with a
|
||||
* 14-octet offset (or salt) value.
|
||||
*/
|
||||
#define SRTP_AES_ICM_192 4
|
||||
|
||||
/*
|
||||
* AES-256 Integer Counter Mode (AES ICM)
|
||||
*
|
||||
* AES-128 ICM is the variant of counter mode that is used by
|
||||
* Secure RTP. This cipher uses a 32-octet key concatenated with a
|
||||
* 14-octet offset (or salt) value.
|
||||
*/
|
||||
#define SRTP_AES_ICM_256 5
|
||||
|
||||
/*
|
||||
* AES-128_GCM Galois Counter Mode (AES GCM)
|
||||
*
|
||||
* AES-128 GCM is the variant of galois counter mode that is used by
|
||||
* Secure RTP. This cipher uses a 16-octet key.
|
||||
*/
|
||||
#define SRTP_AES_GCM_128 6
|
||||
|
||||
/*
|
||||
* AES-256_GCM Galois Counter Mode (AES GCM)
|
||||
*
|
||||
* AES-256 GCM is the variant of galois counter mode that is used by
|
||||
* Secure RTP. This cipher uses a 32-octet key.
|
||||
*/
|
||||
#define SRTP_AES_GCM_256 7
|
||||
|
||||
/*
|
||||
* The null authentication function performs no authentication.
|
||||
*
|
||||
* The NULL_AUTH function does nothing, and can be selected to indicate
|
||||
* that authentication should not be performed.
|
||||
*/
|
||||
#define SRTP_NULL_AUTH 0
|
||||
|
||||
/*
|
||||
* HMAC-SHA1
|
||||
*
|
||||
* SRTP_HMAC_SHA1 implements the Hash-based MAC using the NIST Secure
|
||||
* Hash Algorithm version 1 (SHA1).
|
||||
*/
|
||||
#define SRTP_HMAC_SHA1 3
|
||||
|
||||
#endif /* SRTP_CRYPTO_TYPES_H */
|
||||
264
libsrtp/crypto/include/datatypes.h
Normal file
264
libsrtp/crypto/include/datatypes.h
Normal file
@@ -0,0 +1,264 @@
|
||||
/*
|
||||
* datatypes.h
|
||||
*
|
||||
* data types for bit vectors and finite fields
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef DATATYPES_H
|
||||
#define DATATYPES_H
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#ifdef HAVE_NETINET_IN_H
|
||||
#include <netinet/in.h>
|
||||
#elif defined HAVE_WINSOCK2_H
|
||||
#include <winsock2.h>
|
||||
#endif
|
||||
|
||||
#if defined(__SSE2__)
|
||||
#include <emmintrin.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if defined(_WIN32) || defined(WIN32)
|
||||
#include <BaseTsd.h>
|
||||
typedef SSIZE_T ssize_t;
|
||||
#endif
|
||||
|
||||
typedef union {
|
||||
uint8_t v8[16];
|
||||
uint16_t v16[8];
|
||||
uint32_t v32[4];
|
||||
uint64_t v64[2];
|
||||
} v128_t;
|
||||
|
||||
#define MAX_PRINT_STRING_LEN 1024
|
||||
|
||||
char *srtp_octet_string_hex_string(const void *str, size_t length);
|
||||
|
||||
char *v128_bit_string(v128_t *x);
|
||||
|
||||
char *v128_hex_string(v128_t *x);
|
||||
|
||||
void v128_copy_octet_string(v128_t *x, const uint8_t s[16]);
|
||||
|
||||
void v128_left_shift(v128_t *x, size_t shift_index);
|
||||
|
||||
/*
|
||||
* the following macros define the data manipulation functions
|
||||
*
|
||||
*/
|
||||
|
||||
#if defined(__SSE2__)
|
||||
|
||||
#define v128_set_to_zero(x) \
|
||||
(_mm_storeu_si128((__m128i *)(x), _mm_setzero_si128()))
|
||||
|
||||
#define v128_copy(x, y) \
|
||||
(_mm_storeu_si128((__m128i *)(x), _mm_loadu_si128((const __m128i *)(y))))
|
||||
|
||||
#define v128_xor(z, x, y) \
|
||||
(_mm_storeu_si128((__m128i *)(z), \
|
||||
_mm_xor_si128(_mm_loadu_si128((const __m128i *)(x)), \
|
||||
_mm_loadu_si128((const __m128i *)(y)))))
|
||||
|
||||
#define v128_xor_eq(z, x) \
|
||||
(_mm_storeu_si128((__m128i *)(z), \
|
||||
_mm_xor_si128(_mm_loadu_si128((const __m128i *)(x)), \
|
||||
_mm_loadu_si128((const __m128i *)(z)))))
|
||||
|
||||
#else /* defined(__SSE2__) */
|
||||
|
||||
#define v128_set_to_zero(x) \
|
||||
((x)->v32[0] = 0, (x)->v32[1] = 0, (x)->v32[2] = 0, (x)->v32[3] = 0)
|
||||
|
||||
#define v128_copy(x, y) \
|
||||
((x)->v32[0] = (y)->v32[0], (x)->v32[1] = (y)->v32[1], \
|
||||
(x)->v32[2] = (y)->v32[2], (x)->v32[3] = (y)->v32[3])
|
||||
|
||||
#define v128_xor(z, x, y) \
|
||||
((z)->v32[0] = (x)->v32[0] ^ (y)->v32[0], \
|
||||
(z)->v32[1] = (x)->v32[1] ^ (y)->v32[1], \
|
||||
(z)->v32[2] = (x)->v32[2] ^ (y)->v32[2], \
|
||||
(z)->v32[3] = (x)->v32[3] ^ (y)->v32[3])
|
||||
|
||||
#define v128_xor_eq(z, x) \
|
||||
((z)->v64[0] ^= (x)->v64[0], (z)->v64[1] ^= (x)->v64[1])
|
||||
|
||||
#endif /* defined(__SSE2__) */
|
||||
|
||||
/* NOTE! This assumes an odd ordering! */
|
||||
/* This will not be compatible directly with math on some processors */
|
||||
/* bit 0 is first 32-bit word, low order bit. in little-endian, that's
|
||||
the first byte of the first 32-bit word. In big-endian, that's
|
||||
the 3rd byte of the first 32-bit word */
|
||||
/* The get/set bit code is used by the replay code ONLY, and it doesn't
|
||||
really care which bit is which. AES does care which bit is which, but
|
||||
doesn't use the 128-bit get/set or 128-bit shifts */
|
||||
|
||||
#define v128_get_bit(x, bit) (((((x)->v32[(bit) >> 5]) >> ((bit)&31)) & 1))
|
||||
|
||||
#define v128_set_bit(x, bit) \
|
||||
((((x)->v32[(bit) >> 5]) |= ((uint32_t)1 << ((bit)&31))))
|
||||
|
||||
#define v128_clear_bit(x, bit) \
|
||||
((((x)->v32[(bit) >> 5]) &= ~((uint32_t)1 << ((bit)&31))))
|
||||
|
||||
/*
|
||||
* srtp_octet_string_equal(a, b, len) returns true if the octet strings
|
||||
* a and b are equal. It returns false otherwise. The running time of the
|
||||
* comparison depends only on length, making this safe to use for (e.g.)
|
||||
* verifying authentication tags.
|
||||
*/
|
||||
|
||||
bool srtp_octet_string_equal(const uint8_t *a, const uint8_t *b, size_t len);
|
||||
|
||||
/*
|
||||
* A portable way to zero out memory as recommended by
|
||||
* https://cryptocoding.net/index.php/Coding_rules#Clean_memory_of_secret_data
|
||||
* This is used to zero memory when OPENSSL_cleanse() is not available.
|
||||
*/
|
||||
void srtp_cleanse(void *s, size_t len);
|
||||
|
||||
/*
|
||||
* Functions as a wrapper that delegates to either srtp_cleanse() or
|
||||
* OPENSSL_cleanse() if available to zero memory.
|
||||
*/
|
||||
void octet_string_set_to_zero(void *s, size_t len);
|
||||
|
||||
#if defined(HAVE_CONFIG_H)
|
||||
|
||||
/*
|
||||
* Convert big endian integers to CPU byte order.
|
||||
*/
|
||||
#ifdef WORDS_BIGENDIAN
|
||||
/* Nothing to do. */
|
||||
#define be32_to_cpu(x) (x)
|
||||
#define be64_to_cpu(x) (x)
|
||||
#elif defined(HAVE_BYTESWAP_H)
|
||||
/* We have (hopefully) optimized versions in byteswap.h */
|
||||
#include <byteswap.h>
|
||||
#define be32_to_cpu(x) bswap_32((x))
|
||||
#define be64_to_cpu(x) bswap_64((x))
|
||||
#elif defined(__APPLE__)
|
||||
// Mac OS X / Darwin features
|
||||
#include <libkern/OSByteOrder.h>
|
||||
#define be32_to_cpu(x) OSSwapInt32(x)
|
||||
#define be64_to_cpu(x) OSSwapInt64(x)
|
||||
#else /* WORDS_BIGENDIAN */
|
||||
|
||||
#if defined(__GNUC__)
|
||||
/* Fall back. */
|
||||
static inline uint32_t be32_to_cpu(uint32_t v)
|
||||
{
|
||||
return __builtin_bswap32(v);
|
||||
}
|
||||
#else /* HAVE_X86 */
|
||||
#ifdef HAVE_NETINET_IN_H
|
||||
#include <netinet/in.h>
|
||||
#elif defined HAVE_WINSOCK2_H
|
||||
#include <winsock2.h>
|
||||
#endif /* HAVE_NETINET_IN_H */
|
||||
#define be32_to_cpu(x) ntohl((x))
|
||||
#endif /* HAVE_X86 */
|
||||
|
||||
static inline uint64_t be64_to_cpu(uint64_t v)
|
||||
{
|
||||
#if defined(__GNUC__)
|
||||
v = __builtin_bswap64(v);
|
||||
#else
|
||||
/* use the native 64-bit math */
|
||||
v = (uint64_t)((be32_to_cpu((uint32_t)(v >> 32))) |
|
||||
(((uint64_t)be32_to_cpu((uint32_t)v)) << 32));
|
||||
#endif
|
||||
return v;
|
||||
}
|
||||
|
||||
#endif /* WORDS_BIGENDIAN */
|
||||
|
||||
#endif /* HAVE_CONFIG_H */
|
||||
|
||||
/*
|
||||
* functions manipulating bitvector_t
|
||||
*
|
||||
* A bitvector_t consists of an array of words and an integer
|
||||
* representing the number of significant bits stored in the array.
|
||||
* The bits are packed as follows: the least significant bit is that
|
||||
* of word[0], while the most significant bit is the nth most
|
||||
* significant bit of word[m], where length = bits_per_word * m + n.
|
||||
*
|
||||
*/
|
||||
|
||||
#define bits_per_word 32
|
||||
#define bytes_per_word 4
|
||||
|
||||
typedef struct {
|
||||
size_t length;
|
||||
uint32_t *word;
|
||||
} bitvector_t;
|
||||
|
||||
#define bitvector_get_bit(v, bit_index) \
|
||||
(((((v)->word[((bit_index) >> 5)]) >> ((bit_index)&31)) & 1))
|
||||
|
||||
#define bitvector_set_bit(v, bit_index) \
|
||||
((((v)->word[((bit_index) >> 5)] |= ((uint32_t)1 << ((bit_index)&31)))))
|
||||
|
||||
#define bitvector_get_length(v) (((v)->length))
|
||||
|
||||
bool bitvector_alloc(bitvector_t *v, size_t length);
|
||||
|
||||
void bitvector_dealloc(bitvector_t *v);
|
||||
|
||||
void bitvector_set_to_zero(bitvector_t *x);
|
||||
|
||||
void bitvector_left_shift(bitvector_t *x, size_t index);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* DATATYPES_H */
|
||||
171
libsrtp/crypto/include/err.h
Normal file
171
libsrtp/crypto/include/err.h
Normal file
@@ -0,0 +1,171 @@
|
||||
/*
|
||||
* err.h
|
||||
*
|
||||
* error status codes
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*/
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef ERR_H
|
||||
#define ERR_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
#include "srtp.h"
|
||||
|
||||
#if defined(__clang__) || (defined(__GNUC__) && defined(__has_attribute))
|
||||
#if __has_attribute(format)
|
||||
#define LIBSRTP_FORMAT_PRINTF(fmt, args) \
|
||||
__attribute__((format(__printf__, fmt, args)))
|
||||
#else
|
||||
#define LIBSRTP_FORMAT_PRINTF(fmt, args)
|
||||
#endif
|
||||
#else
|
||||
#define LIBSRTP_FORMAT_PRINTF(fmt, args)
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @defgroup Error Error Codes
|
||||
*
|
||||
* Error status codes are represented by the enumeration srtp_err_status_t.
|
||||
*
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
typedef enum {
|
||||
srtp_err_level_error,
|
||||
srtp_err_level_warning,
|
||||
srtp_err_level_info,
|
||||
srtp_err_level_debug
|
||||
} srtp_err_reporting_level_t;
|
||||
|
||||
/*
|
||||
* err_reporting_init prepares the error system. If
|
||||
* ERR_REPORTING_STDOUT is defined, it will log to stdout.
|
||||
*
|
||||
*/
|
||||
|
||||
srtp_err_status_t srtp_err_reporting_init(void);
|
||||
|
||||
typedef void(srtp_err_report_handler_func_t)(srtp_err_reporting_level_t level,
|
||||
const char *msg);
|
||||
|
||||
srtp_err_status_t srtp_install_err_report_handler(
|
||||
srtp_err_report_handler_func_t func);
|
||||
|
||||
/*
|
||||
* srtp_err_report reports a 'printf' formatted error
|
||||
* string, followed by a an arg list. The level argument
|
||||
* is one of srtp_err_reporting_level_t.
|
||||
*
|
||||
* Errors will be reported to stdout, if ERR_REPORTING_STDOUT
|
||||
* is defined.
|
||||
*
|
||||
*/
|
||||
|
||||
void srtp_err_report(srtp_err_reporting_level_t level, const char *format, ...)
|
||||
LIBSRTP_FORMAT_PRINTF(2, 3);
|
||||
|
||||
/*
|
||||
* debug_module_t defines a debug module
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
bool on; /* true if debugging is on, false if it is off */
|
||||
const char *name; /* printable name for debug module */
|
||||
} srtp_debug_module_t;
|
||||
|
||||
#ifdef ENABLE_DEBUG_LOGGING
|
||||
|
||||
#ifndef debug_print0
|
||||
#define debug_print0(mod, format) \
|
||||
srtp_err_report(srtp_err_level_debug, ("%s: " format "\n"), mod.name)
|
||||
#endif
|
||||
|
||||
#ifndef debug_print
|
||||
#define debug_print(mod, format, arg) \
|
||||
srtp_err_report(srtp_err_level_debug, ("%s: " format "\n"), mod.name, arg)
|
||||
#endif
|
||||
|
||||
#ifndef debug_print2
|
||||
#define debug_print2(mod, format, arg1, arg2) \
|
||||
srtp_err_report(srtp_err_level_debug, ("%s: " format "\n"), mod.name, \
|
||||
arg1, arg2)
|
||||
#endif
|
||||
|
||||
#else
|
||||
|
||||
#ifndef debug_print0
|
||||
#define debug_print0(mod, format) \
|
||||
if (mod.on) \
|
||||
srtp_err_report(srtp_err_level_debug, ("%s: " format "\n"), mod.name)
|
||||
#endif
|
||||
|
||||
#ifndef debug_print
|
||||
#define debug_print(mod, format, arg) \
|
||||
if (mod.on) \
|
||||
srtp_err_report(srtp_err_level_debug, ("%s: " format "\n"), mod.name, arg)
|
||||
#endif
|
||||
|
||||
#ifndef debug_print2
|
||||
#define debug_print2(mod, format, arg1, arg2) \
|
||||
if (mod.on) \
|
||||
srtp_err_report(srtp_err_level_debug, ("%s: " format "\n"), mod.name, \
|
||||
arg1, arg2)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* ERR_H */
|
||||
58
libsrtp/crypto/include/hmac.h
Normal file
58
libsrtp/crypto/include/hmac.h
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* hmac.h
|
||||
*
|
||||
* interface to hmac srtp_auth_type_t
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*
|
||||
*/
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef HMAC_H
|
||||
#define HMAC_H
|
||||
|
||||
#include "auth.h"
|
||||
#include "sha1.h"
|
||||
|
||||
typedef struct {
|
||||
uint8_t opad[64];
|
||||
srtp_sha1_ctx_t ctx;
|
||||
srtp_sha1_ctx_t init_ctx;
|
||||
} srtp_hmac_ctx_t;
|
||||
|
||||
#endif /* HMAC_H */
|
||||
86
libsrtp/crypto/include/key.h
Normal file
86
libsrtp/crypto/include/key.h
Normal file
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* key.h
|
||||
*
|
||||
* key usage limits enforcement
|
||||
*
|
||||
* David A. Mcgrew
|
||||
* Cisco Systems, Inc.
|
||||
*/
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef KEY_H
|
||||
#define KEY_H
|
||||
|
||||
#include "rdbx.h" /* for srtp_xtd_seq_num_t */
|
||||
#include "err.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct srtp_key_limit_ctx_t *srtp_key_limit_t;
|
||||
|
||||
typedef enum {
|
||||
srtp_key_event_normal,
|
||||
srtp_key_event_soft_limit,
|
||||
srtp_key_event_hard_limit
|
||||
} srtp_key_event_t;
|
||||
|
||||
srtp_err_status_t srtp_key_limit_set(srtp_key_limit_t key,
|
||||
const srtp_xtd_seq_num_t s);
|
||||
|
||||
srtp_err_status_t srtp_key_limit_clone(srtp_key_limit_t original,
|
||||
srtp_key_limit_t *new_key);
|
||||
|
||||
srtp_key_event_t srtp_key_limit_update(srtp_key_limit_t key);
|
||||
|
||||
typedef enum {
|
||||
srtp_key_state_normal,
|
||||
srtp_key_state_past_soft_limit,
|
||||
srtp_key_state_expired
|
||||
} srtp_key_state_t;
|
||||
|
||||
typedef struct srtp_key_limit_ctx_t {
|
||||
srtp_xtd_seq_num_t num_left;
|
||||
srtp_key_state_t state;
|
||||
} srtp_key_limit_ctx_t;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* KEY_H */
|
||||
62
libsrtp/crypto/include/null_auth.h
Normal file
62
libsrtp/crypto/include/null_auth.h
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* null-auth.h
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef NULL_AUTH_H
|
||||
#define NULL_AUTH_H
|
||||
|
||||
#include "auth.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
char foo;
|
||||
} srtp_null_auth_ctx_t;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* NULL_AUTH_H */
|
||||
57
libsrtp/crypto/include/null_cipher.h
Normal file
57
libsrtp/crypto/include/null_cipher.h
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* null-cipher.h
|
||||
*
|
||||
* header file for the null cipher
|
||||
*
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef NULL_CIPHER_H
|
||||
#define NULL_CIPHER_H
|
||||
|
||||
#include "datatypes.h"
|
||||
#include "cipher.h"
|
||||
|
||||
typedef struct {
|
||||
char foo; /* empty, for now */
|
||||
} srtp_null_cipher_ctx_t;
|
||||
|
||||
#endif /* NULL_CIPHER_H */
|
||||
122
libsrtp/crypto/include/rdb.h
Normal file
122
libsrtp/crypto/include/rdb.h
Normal file
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* replay-database.h
|
||||
*
|
||||
* interface for a replay database for packet security
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef REPLAY_DB_H
|
||||
#define REPLAY_DB_H
|
||||
|
||||
#include "datatypes.h" /* for v128_t */
|
||||
#include "err.h" /* for srtp_err_status_t */
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
* if the ith least significant bit is one, then the packet index
|
||||
* window_end-i is in the database
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
uint32_t window_start; /* packet index of the first bit in bitmask */
|
||||
v128_t bitmask;
|
||||
} srtp_rdb_t;
|
||||
|
||||
/*
|
||||
* srtp_rdb_init
|
||||
*
|
||||
* initalizes rdb
|
||||
*
|
||||
* returns srtp_err_status_ok on success, srtp_err_status_t_fail otherwise
|
||||
*/
|
||||
srtp_err_status_t srtp_rdb_init(srtp_rdb_t *rdb);
|
||||
|
||||
/*
|
||||
* srtp_rdb_check
|
||||
*
|
||||
* checks to see if index appears in rdb
|
||||
*
|
||||
* returns srtp_err_status_fail if the index already appears in rdb,
|
||||
* returns srtp_err_status_ok otherwise
|
||||
*/
|
||||
srtp_err_status_t srtp_rdb_check(const srtp_rdb_t *rdb, uint32_t rdb_index);
|
||||
|
||||
/*
|
||||
* srtp_rdb_add_index
|
||||
*
|
||||
* adds index to srtp_rdb_t (and does *not* check if index appears in db)
|
||||
*
|
||||
* returns srtp_err_status_ok on success, srtp_err_status_fail otherwise
|
||||
*
|
||||
*/
|
||||
srtp_err_status_t srtp_rdb_add_index(srtp_rdb_t *rdb, uint32_t rdb_index);
|
||||
|
||||
/*
|
||||
* the functions srtp_rdb_increment() and srtp_rdb_get_value() are for use by
|
||||
* senders, not receivers - DO NOT use these functions on the same
|
||||
* srtp_rdb_t upon which srtp_rdb_add_index is used!
|
||||
*/
|
||||
|
||||
/*
|
||||
* srtp_rdb_increment(db) increments the sequence number in db, if it is
|
||||
* not too high
|
||||
*
|
||||
* return values:
|
||||
*
|
||||
* srtp_err_status_ok no problem
|
||||
* srtp_err_status_key_expired sequence number too high
|
||||
*
|
||||
*/
|
||||
srtp_err_status_t srtp_rdb_increment(srtp_rdb_t *rdb);
|
||||
|
||||
/*
|
||||
* srtp_rdb_get_value(db) returns the current sequence number of db
|
||||
*/
|
||||
uint32_t srtp_rdb_get_value(const srtp_rdb_t *rdb);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* REPLAY_DB_H */
|
||||
209
libsrtp/crypto/include/rdbx.h
Normal file
209
libsrtp/crypto/include/rdbx.h
Normal file
@@ -0,0 +1,209 @@
|
||||
/*
|
||||
* rdbx.h
|
||||
*
|
||||
* replay database with extended packet indices, using a rollover counter
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef RDBX_H
|
||||
#define RDBX_H
|
||||
|
||||
#include "datatypes.h"
|
||||
#include "err.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* #define ROC_TEST */
|
||||
|
||||
#ifndef ROC_TEST
|
||||
|
||||
typedef uint16_t srtp_sequence_number_t; /* 16 bit sequence number */
|
||||
typedef uint32_t srtp_rollover_counter_t; /* 32 bit rollover counter */
|
||||
|
||||
#else /* use small seq_num and roc datatypes for testing purposes */
|
||||
|
||||
typedef uint8_t srtp_sequence_number_t; /* 8 bit sequence number */
|
||||
typedef uint16_t srtp_rollover_counter_t; /* 16 bit rollover counter */
|
||||
|
||||
#endif
|
||||
|
||||
#define seq_num_median (1 << (8 * sizeof(srtp_sequence_number_t) - 1))
|
||||
#define seq_num_max (1 << (8 * sizeof(srtp_sequence_number_t)))
|
||||
|
||||
/*
|
||||
* An rtp_xtd_seq_num_t is a 64-bit unsigned integer used as an 'extended'
|
||||
* sequence number.
|
||||
*/
|
||||
typedef uint64_t srtp_xtd_seq_num_t;
|
||||
|
||||
/*
|
||||
* An srtp_rdbx_t is a replay database with extended range; it uses an
|
||||
* xtd_seq_num_t and a bitmask of recently received indices.
|
||||
*/
|
||||
typedef struct {
|
||||
srtp_xtd_seq_num_t index;
|
||||
bitvector_t bitmask;
|
||||
} srtp_rdbx_t;
|
||||
|
||||
/*
|
||||
* srtp_rdbx_init(rdbx_ptr, ws)
|
||||
*
|
||||
* initializes the rdbx pointed to by its argument with the window size ws,
|
||||
* setting the rollover counter and sequence number to zero
|
||||
*/
|
||||
srtp_err_status_t srtp_rdbx_init(srtp_rdbx_t *rdbx, size_t ws);
|
||||
|
||||
/*
|
||||
* srtp_rdbx_dealloc(rdbx_ptr)
|
||||
*
|
||||
* frees memory associated with the rdbx
|
||||
*/
|
||||
srtp_err_status_t srtp_rdbx_dealloc(srtp_rdbx_t *rdbx);
|
||||
|
||||
/*
|
||||
* srtp_rdbx_estimate_index(rdbx, guess, s)
|
||||
*
|
||||
* given an rdbx and a sequence number s (from a newly arrived packet),
|
||||
* sets the contents of *guess to contain the best guess of the packet
|
||||
* index to which s corresponds, and returns the difference between
|
||||
* *guess and the locally stored synch info
|
||||
*/
|
||||
ssize_t srtp_rdbx_estimate_index(const srtp_rdbx_t *rdbx,
|
||||
srtp_xtd_seq_num_t *guess,
|
||||
srtp_sequence_number_t s);
|
||||
|
||||
/*
|
||||
* srtp_rdbx_check(rdbx, delta);
|
||||
*
|
||||
* srtp_rdbx_check(&r, delta) checks to see if the xtd_seq_num_t
|
||||
* which is at rdbx->window_start + delta is in the rdb
|
||||
*
|
||||
*/
|
||||
srtp_err_status_t srtp_rdbx_check(const srtp_rdbx_t *rdbx, ssize_t difference);
|
||||
|
||||
/*
|
||||
* srtp_replay_add_index(rdbx, delta)
|
||||
*
|
||||
* adds the srtp_xtd_seq_num_t at rdbx->window_start + delta to replay_db
|
||||
* (and does *not* check if that xtd_seq_num_t appears in db)
|
||||
*
|
||||
* this function should be called *only* after replay_check has
|
||||
* indicated that the index does not appear in the rdbx, and a mutex
|
||||
* should protect the rdbx between these calls if necessary.
|
||||
*/
|
||||
srtp_err_status_t srtp_rdbx_add_index(srtp_rdbx_t *rdbx, ssize_t delta);
|
||||
|
||||
/*
|
||||
* srtp_rdbx_set_roc(rdbx, roc) initalizes the srtp_rdbx_t at the location rdbx
|
||||
* to have the rollover counter value roc. If that value is less than
|
||||
* the current rollover counter value, then the function returns
|
||||
* srtp_err_status_replay_old; otherwise, srtp_err_status_ok is returned.
|
||||
*
|
||||
*/
|
||||
srtp_err_status_t srtp_rdbx_set_roc(srtp_rdbx_t *rdbx, uint32_t roc);
|
||||
|
||||
/*
|
||||
* srtp_rdbx_get_packet_index(rdbx) returns the value of the rollover counter
|
||||
* for
|
||||
* the srtp_rdbx_t pointed to by rdbx
|
||||
*
|
||||
*/
|
||||
srtp_xtd_seq_num_t srtp_rdbx_get_packet_index(const srtp_rdbx_t *rdbx);
|
||||
|
||||
/*
|
||||
* srtp_xtd_seq_num_t functions - these are *internal* functions of rdbx, and
|
||||
* shouldn't be used to manipulate rdbx internal values. use the rdbx
|
||||
* api instead!
|
||||
*/
|
||||
|
||||
/*
|
||||
* srtp_rdbx_get_ws(rdbx_ptr)
|
||||
*
|
||||
* gets the window size which was used to initialize the rdbx
|
||||
*/
|
||||
size_t srtp_rdbx_get_window_size(const srtp_rdbx_t *rdbx);
|
||||
|
||||
/* index_init(&pi) initializes a packet index pi (sets it to zero) */
|
||||
void srtp_index_init(srtp_xtd_seq_num_t *pi);
|
||||
|
||||
/* index_advance(&pi, s) advances a xtd_seq_num_t forward by s */
|
||||
void srtp_index_advance(srtp_xtd_seq_num_t *pi, srtp_sequence_number_t s);
|
||||
|
||||
/*
|
||||
* srtp_index_guess(local, guess, s)
|
||||
*
|
||||
* given a srtp_xtd_seq_num_t local (which represents the highest
|
||||
* known-to-be-good index) and a sequence number s (from a newly
|
||||
* arrived packet), sets the contents of *guess to contain the best
|
||||
* guess of the packet index to which s corresponds, and returns the
|
||||
* difference between *guess and *local
|
||||
*/
|
||||
ssize_t srtp_index_guess(const srtp_xtd_seq_num_t *local,
|
||||
srtp_xtd_seq_num_t *guess,
|
||||
srtp_sequence_number_t s);
|
||||
|
||||
/*
|
||||
* srtp_rdbx_get_roc(rdbx)
|
||||
*
|
||||
* Get the current rollover counter
|
||||
*
|
||||
*/
|
||||
uint32_t srtp_rdbx_get_roc(const srtp_rdbx_t *rdbx);
|
||||
|
||||
/*
|
||||
* srtp_rdbx_set_roc_seq(rdbx, roc, seq) initializes the srtp_rdbx_t at the
|
||||
* location rdbx to have the rollover counter value roc and packet sequence
|
||||
* number seq. If the new rollover counter value is less than the current
|
||||
* rollover counter value, then the function returns
|
||||
* srtp_err_status_replay_old, otherwise, srtp_err_status_ok is returned.
|
||||
*/
|
||||
srtp_err_status_t srtp_rdbx_set_roc_seq(srtp_rdbx_t *rdbx,
|
||||
uint32_t roc,
|
||||
uint16_t seq);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* RDBX_H */
|
||||
90
libsrtp/crypto/include/sha1.h
Normal file
90
libsrtp/crypto/include/sha1.h
Normal file
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* sha1.h
|
||||
*
|
||||
* interface to the Secure Hash Algorithm v.1 (SHA-1), specified in
|
||||
* FIPS 180-1
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef SHA1_H
|
||||
#define SHA1_H
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include "err.h"
|
||||
#include "datatypes.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
uint32_t H[5]; /* state vector */
|
||||
uint32_t M[16]; /* message buffer */
|
||||
size_t octets_in_buffer; /* octets of message in buffer */
|
||||
uint32_t num_bits_in_msg; /* total number of bits in message */
|
||||
} srtp_sha1_ctx_t;
|
||||
|
||||
/*
|
||||
* srtp_sha1_init(&ctx) initializes the SHA1 context ctx
|
||||
*
|
||||
* srtp_sha1_update(&ctx, msg, len) hashes the len octets starting at msg
|
||||
* into the SHA1 context
|
||||
*
|
||||
* srtp_sha1_final(&ctx, output) performs the final processing of the SHA1
|
||||
* context and writes the result to the 20 octets at output
|
||||
*
|
||||
*/
|
||||
void srtp_sha1_init(srtp_sha1_ctx_t *ctx);
|
||||
|
||||
void srtp_sha1_update(srtp_sha1_ctx_t *ctx,
|
||||
const uint8_t *M,
|
||||
size_t octets_in_msg);
|
||||
|
||||
void srtp_sha1_final(srtp_sha1_ctx_t *ctx, uint32_t output[5]);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* SHA1_H */
|
||||
95
libsrtp/crypto/kernel/alloc.c
Normal file
95
libsrtp/crypto/kernel/alloc.c
Normal file
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* alloc.c
|
||||
*
|
||||
* memory allocation and deallocation
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*/
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-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 "alloc.h"
|
||||
#include "crypto_kernel.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
/* the debug module for memory allocation */
|
||||
|
||||
srtp_debug_module_t srtp_mod_alloc = {
|
||||
false, /* debugging is off by default */
|
||||
"alloc" /* printable name for module */
|
||||
};
|
||||
|
||||
/*
|
||||
* Nota bene: the debugging statements for srtp_crypto_alloc() and
|
||||
* srtp_crypto_free() have identical prefixes, which include the addresses
|
||||
* of the memory locations on which they are operating. This fact can
|
||||
* be used to locate memory leaks, by turning on memory debugging,
|
||||
* grepping for 'alloc', then matching alloc and free calls by
|
||||
* address.
|
||||
*/
|
||||
|
||||
void *srtp_crypto_alloc(size_t size)
|
||||
{
|
||||
void *ptr;
|
||||
|
||||
if (!size) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ptr = calloc(1, size);
|
||||
|
||||
if (ptr) {
|
||||
debug_print(srtp_mod_alloc, "(location: %p) allocated", ptr);
|
||||
} else {
|
||||
debug_print(srtp_mod_alloc, "allocation failed (asked for %zu bytes)\n",
|
||||
size);
|
||||
}
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void srtp_crypto_free(void *ptr)
|
||||
{
|
||||
debug_print(srtp_mod_alloc, "(location: %p) freed", ptr);
|
||||
|
||||
free(ptr);
|
||||
}
|
||||
560
libsrtp/crypto/kernel/crypto_kernel.c
Normal file
560
libsrtp/crypto/kernel/crypto_kernel.c
Normal file
@@ -0,0 +1,560 @@
|
||||
/*
|
||||
* crypto_kernel.c
|
||||
*
|
||||
* header for the cryptographic kernel
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*/
|
||||
/*
|
||||
*
|
||||
* Copyright(c) 2001-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 "crypto_kernel.h"
|
||||
#include "cipher_types.h"
|
||||
#include "alloc.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
/* the debug module for the crypto_kernel */
|
||||
|
||||
srtp_debug_module_t srtp_mod_crypto_kernel = {
|
||||
false, /* debugging is off by default */
|
||||
"crypto kernel" /* printable name for module */
|
||||
};
|
||||
|
||||
/* crypto_kernel is a global variable, the only one of its datatype */
|
||||
|
||||
static srtp_crypto_kernel_t crypto_kernel = {
|
||||
srtp_crypto_kernel_state_insecure, /* start off in insecure state */
|
||||
NULL, /* no cipher types yet */
|
||||
NULL, /* no auth types yet */
|
||||
NULL /* no debug modules yet */
|
||||
};
|
||||
|
||||
#define MAX_RNG_TRIALS 25
|
||||
|
||||
srtp_err_status_t srtp_crypto_kernel_init(void)
|
||||
{
|
||||
srtp_err_status_t status;
|
||||
|
||||
/* check the security state */
|
||||
if (crypto_kernel.state == srtp_crypto_kernel_state_secure) {
|
||||
/*
|
||||
* we're already in the secure state, but we've been asked to
|
||||
* re-initialize, so we just re-run the self-tests and then return
|
||||
*/
|
||||
return srtp_crypto_kernel_status();
|
||||
}
|
||||
|
||||
/* initialize error reporting system */
|
||||
status = srtp_err_reporting_init();
|
||||
if (status) {
|
||||
return status;
|
||||
}
|
||||
|
||||
/* load debug modules */
|
||||
status = srtp_crypto_kernel_load_debug_module(&srtp_mod_crypto_kernel);
|
||||
if (status) {
|
||||
return status;
|
||||
}
|
||||
status = srtp_crypto_kernel_load_debug_module(&srtp_mod_auth);
|
||||
if (status) {
|
||||
return status;
|
||||
}
|
||||
status = srtp_crypto_kernel_load_debug_module(&srtp_mod_cipher);
|
||||
if (status) {
|
||||
return status;
|
||||
}
|
||||
status = srtp_crypto_kernel_load_debug_module(&srtp_mod_alloc);
|
||||
if (status) {
|
||||
return status;
|
||||
}
|
||||
|
||||
/* load cipher types */
|
||||
status = srtp_crypto_kernel_load_cipher_type(&srtp_null_cipher,
|
||||
SRTP_NULL_CIPHER);
|
||||
if (status) {
|
||||
return status;
|
||||
}
|
||||
status = srtp_crypto_kernel_load_cipher_type(&srtp_aes_icm_128,
|
||||
SRTP_AES_ICM_128);
|
||||
if (status) {
|
||||
return status;
|
||||
}
|
||||
status = srtp_crypto_kernel_load_cipher_type(&srtp_aes_icm_256,
|
||||
SRTP_AES_ICM_256);
|
||||
if (status) {
|
||||
return status;
|
||||
}
|
||||
status = srtp_crypto_kernel_load_debug_module(&srtp_mod_aes_icm);
|
||||
if (status) {
|
||||
return status;
|
||||
}
|
||||
#ifdef GCM
|
||||
status = srtp_crypto_kernel_load_cipher_type(&srtp_aes_icm_192,
|
||||
SRTP_AES_ICM_192);
|
||||
if (status) {
|
||||
return status;
|
||||
}
|
||||
status = srtp_crypto_kernel_load_cipher_type(&srtp_aes_gcm_128,
|
||||
SRTP_AES_GCM_128);
|
||||
if (status) {
|
||||
return status;
|
||||
}
|
||||
status = srtp_crypto_kernel_load_cipher_type(&srtp_aes_gcm_256,
|
||||
SRTP_AES_GCM_256);
|
||||
if (status) {
|
||||
return status;
|
||||
}
|
||||
status = srtp_crypto_kernel_load_debug_module(&srtp_mod_aes_gcm);
|
||||
if (status) {
|
||||
return status;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* load auth func types */
|
||||
status = srtp_crypto_kernel_load_auth_type(&srtp_null_auth, SRTP_NULL_AUTH);
|
||||
if (status) {
|
||||
return status;
|
||||
}
|
||||
status = srtp_crypto_kernel_load_auth_type(&srtp_hmac, SRTP_HMAC_SHA1);
|
||||
if (status) {
|
||||
return status;
|
||||
}
|
||||
status = srtp_crypto_kernel_load_debug_module(&srtp_mod_hmac);
|
||||
if (status) {
|
||||
return status;
|
||||
}
|
||||
|
||||
/* change state to secure */
|
||||
crypto_kernel.state = srtp_crypto_kernel_state_secure;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
srtp_err_status_t srtp_crypto_kernel_status(void)
|
||||
{
|
||||
srtp_err_status_t status;
|
||||
srtp_kernel_cipher_type_t *ctype = crypto_kernel.cipher_type_list;
|
||||
srtp_kernel_auth_type_t *atype = crypto_kernel.auth_type_list;
|
||||
|
||||
/* for each cipher type, describe and test */
|
||||
while (ctype != NULL) {
|
||||
srtp_err_report(srtp_err_level_info, "cipher: %s\n",
|
||||
ctype->cipher_type->description);
|
||||
srtp_err_report(srtp_err_level_info, " self-test: ");
|
||||
status = srtp_cipher_type_self_test(ctype->cipher_type);
|
||||
if (status) {
|
||||
srtp_err_report(srtp_err_level_error, "failed with error code %d\n",
|
||||
status);
|
||||
exit(status);
|
||||
}
|
||||
srtp_err_report(srtp_err_level_info, "passed\n");
|
||||
ctype = ctype->next;
|
||||
}
|
||||
|
||||
/* for each auth type, describe and test */
|
||||
while (atype != NULL) {
|
||||
srtp_err_report(srtp_err_level_info, "auth func: %s\n",
|
||||
atype->auth_type->description);
|
||||
srtp_err_report(srtp_err_level_info, " self-test: ");
|
||||
status = srtp_auth_type_self_test(atype->auth_type);
|
||||
if (status) {
|
||||
srtp_err_report(srtp_err_level_error, "failed with error code %d\n",
|
||||
status);
|
||||
exit(status);
|
||||
}
|
||||
srtp_err_report(srtp_err_level_info, "passed\n");
|
||||
atype = atype->next;
|
||||
}
|
||||
|
||||
srtp_crypto_kernel_list_debug_modules();
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
srtp_err_status_t srtp_crypto_kernel_list_debug_modules(void)
|
||||
{
|
||||
srtp_kernel_debug_module_t *dm = crypto_kernel.debug_module_list;
|
||||
|
||||
/* describe each debug module */
|
||||
srtp_err_report(srtp_err_level_info, "debug modules loaded:\n");
|
||||
while (dm != NULL) {
|
||||
srtp_err_report(srtp_err_level_info, " %s ", dm->mod->name);
|
||||
if (dm->mod->on) {
|
||||
srtp_err_report(srtp_err_level_info, "(on)\n");
|
||||
} else {
|
||||
srtp_err_report(srtp_err_level_info, "(off)\n");
|
||||
}
|
||||
dm = dm->next;
|
||||
}
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
srtp_err_status_t srtp_crypto_kernel_shutdown(void)
|
||||
{
|
||||
/*
|
||||
* free dynamic memory used in crypto_kernel at present
|
||||
*/
|
||||
|
||||
/* walk down cipher type list, freeing memory */
|
||||
while (crypto_kernel.cipher_type_list != NULL) {
|
||||
srtp_kernel_cipher_type_t *ctype = crypto_kernel.cipher_type_list;
|
||||
crypto_kernel.cipher_type_list = ctype->next;
|
||||
debug_print(srtp_mod_crypto_kernel, "freeing memory for cipher %s",
|
||||
ctype->cipher_type->description);
|
||||
srtp_crypto_free(ctype);
|
||||
}
|
||||
|
||||
/* walk down authetication module list, freeing memory */
|
||||
while (crypto_kernel.auth_type_list != NULL) {
|
||||
srtp_kernel_auth_type_t *atype = crypto_kernel.auth_type_list;
|
||||
crypto_kernel.auth_type_list = atype->next;
|
||||
debug_print(srtp_mod_crypto_kernel,
|
||||
"freeing memory for authentication %s",
|
||||
atype->auth_type->description);
|
||||
srtp_crypto_free(atype);
|
||||
}
|
||||
|
||||
/* walk down debug module list, freeing memory */
|
||||
while (crypto_kernel.debug_module_list != NULL) {
|
||||
srtp_kernel_debug_module_t *kdm = crypto_kernel.debug_module_list;
|
||||
crypto_kernel.debug_module_list = kdm->next;
|
||||
debug_print(srtp_mod_crypto_kernel,
|
||||
"freeing memory for debug module %s", kdm->mod->name);
|
||||
srtp_crypto_free(kdm);
|
||||
}
|
||||
|
||||
/* return to insecure state */
|
||||
crypto_kernel.state = srtp_crypto_kernel_state_insecure;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static inline srtp_err_status_t srtp_crypto_kernel_do_load_cipher_type(
|
||||
const srtp_cipher_type_t *new_ct,
|
||||
srtp_cipher_type_id_t id,
|
||||
bool replace)
|
||||
{
|
||||
srtp_kernel_cipher_type_t *ctype;
|
||||
srtp_kernel_cipher_type_t *new_ctype = NULL;
|
||||
srtp_err_status_t status;
|
||||
|
||||
/* defensive coding */
|
||||
if (new_ct == NULL) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
if (new_ct->id != id) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
/* check cipher type by running self-test */
|
||||
status = srtp_cipher_type_self_test(new_ct);
|
||||
if (status) {
|
||||
return status;
|
||||
}
|
||||
|
||||
/* walk down list, checking if this type is in the list already */
|
||||
ctype = crypto_kernel.cipher_type_list;
|
||||
while (ctype != NULL) {
|
||||
if (id == ctype->id) {
|
||||
if (!replace) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
status =
|
||||
srtp_cipher_type_test(new_ct, ctype->cipher_type->test_data);
|
||||
if (status) {
|
||||
return status;
|
||||
}
|
||||
new_ctype = ctype;
|
||||
break;
|
||||
} else if (new_ct == ctype->cipher_type) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
ctype = ctype->next;
|
||||
}
|
||||
|
||||
/* if not found, put new_ct at the head of the list */
|
||||
if (ctype == NULL) {
|
||||
/* allocate memory */
|
||||
new_ctype = (srtp_kernel_cipher_type_t *)srtp_crypto_alloc(
|
||||
sizeof(srtp_kernel_cipher_type_t));
|
||||
if (new_ctype == NULL) {
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
new_ctype->next = crypto_kernel.cipher_type_list;
|
||||
|
||||
/* set head of list to new cipher type */
|
||||
crypto_kernel.cipher_type_list = new_ctype;
|
||||
}
|
||||
|
||||
/* set fields */
|
||||
new_ctype->cipher_type = new_ct;
|
||||
new_ctype->id = id;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
srtp_err_status_t srtp_crypto_kernel_load_cipher_type(
|
||||
const srtp_cipher_type_t *new_ct,
|
||||
srtp_cipher_type_id_t id)
|
||||
{
|
||||
return srtp_crypto_kernel_do_load_cipher_type(new_ct, id, false);
|
||||
}
|
||||
|
||||
srtp_err_status_t srtp_replace_cipher_type(const srtp_cipher_type_t *new_ct,
|
||||
srtp_cipher_type_id_t id)
|
||||
{
|
||||
return srtp_crypto_kernel_do_load_cipher_type(new_ct, id, true);
|
||||
}
|
||||
|
||||
srtp_err_status_t srtp_crypto_kernel_do_load_auth_type(
|
||||
const srtp_auth_type_t *new_at,
|
||||
srtp_auth_type_id_t id,
|
||||
bool replace)
|
||||
{
|
||||
srtp_kernel_auth_type_t *atype;
|
||||
srtp_kernel_auth_type_t *new_atype = NULL;
|
||||
srtp_err_status_t status;
|
||||
|
||||
/* defensive coding */
|
||||
if (new_at == NULL) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
if (new_at->id != id) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
/* check auth type by running self-test */
|
||||
status = srtp_auth_type_self_test(new_at);
|
||||
if (status) {
|
||||
return status;
|
||||
}
|
||||
|
||||
/* walk down list, checking if this type is in the list already */
|
||||
atype = crypto_kernel.auth_type_list;
|
||||
while (atype != NULL) {
|
||||
if (id == atype->id) {
|
||||
if (!replace) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
status = srtp_auth_type_test(new_at, atype->auth_type->test_data);
|
||||
if (status) {
|
||||
return status;
|
||||
}
|
||||
new_atype = atype;
|
||||
break;
|
||||
} else if (new_at == atype->auth_type) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
atype = atype->next;
|
||||
}
|
||||
|
||||
/* if not found, put new_at at the head of the list */
|
||||
if (atype == NULL) {
|
||||
/* allocate memory */
|
||||
new_atype = (srtp_kernel_auth_type_t *)srtp_crypto_alloc(
|
||||
sizeof(srtp_kernel_auth_type_t));
|
||||
if (new_atype == NULL) {
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
|
||||
new_atype->next = crypto_kernel.auth_type_list;
|
||||
/* set head of list to new auth type */
|
||||
crypto_kernel.auth_type_list = new_atype;
|
||||
}
|
||||
|
||||
/* set fields */
|
||||
new_atype->auth_type = new_at;
|
||||
new_atype->id = id;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
srtp_err_status_t srtp_crypto_kernel_load_auth_type(
|
||||
const srtp_auth_type_t *new_at,
|
||||
srtp_auth_type_id_t id)
|
||||
{
|
||||
return srtp_crypto_kernel_do_load_auth_type(new_at, id, false);
|
||||
}
|
||||
|
||||
srtp_err_status_t srtp_replace_auth_type(const srtp_auth_type_t *new_at,
|
||||
srtp_auth_type_id_t id)
|
||||
{
|
||||
return srtp_crypto_kernel_do_load_auth_type(new_at, id, true);
|
||||
}
|
||||
|
||||
const srtp_cipher_type_t *srtp_crypto_kernel_get_cipher_type(
|
||||
srtp_cipher_type_id_t id)
|
||||
{
|
||||
srtp_kernel_cipher_type_t *ctype;
|
||||
|
||||
/* walk down list, looking for id */
|
||||
ctype = crypto_kernel.cipher_type_list;
|
||||
while (ctype != NULL) {
|
||||
if (id == ctype->id) {
|
||||
return ctype->cipher_type;
|
||||
}
|
||||
ctype = ctype->next;
|
||||
}
|
||||
|
||||
/* haven't found the right one, indicate failure by returning NULL */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
srtp_err_status_t srtp_crypto_kernel_alloc_cipher(srtp_cipher_type_id_t id,
|
||||
srtp_cipher_pointer_t *cp,
|
||||
size_t key_len,
|
||||
size_t tag_len)
|
||||
{
|
||||
const srtp_cipher_type_t *ct;
|
||||
|
||||
/*
|
||||
* if the crypto_kernel is not yet initialized, we refuse to allocate
|
||||
* any ciphers - this is a bit extra-paranoid
|
||||
*/
|
||||
if (crypto_kernel.state != srtp_crypto_kernel_state_secure) {
|
||||
return srtp_err_status_init_fail;
|
||||
}
|
||||
|
||||
ct = srtp_crypto_kernel_get_cipher_type(id);
|
||||
if (!ct) {
|
||||
return srtp_err_status_fail;
|
||||
}
|
||||
|
||||
return ((ct)->alloc(cp, key_len, tag_len));
|
||||
}
|
||||
|
||||
const srtp_auth_type_t *srtp_crypto_kernel_get_auth_type(srtp_auth_type_id_t id)
|
||||
{
|
||||
srtp_kernel_auth_type_t *atype;
|
||||
|
||||
/* walk down list, looking for id */
|
||||
atype = crypto_kernel.auth_type_list;
|
||||
while (atype != NULL) {
|
||||
if (id == atype->id) {
|
||||
return atype->auth_type;
|
||||
}
|
||||
atype = atype->next;
|
||||
}
|
||||
|
||||
/* haven't found the right one, indicate failure by returning NULL */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
srtp_err_status_t srtp_crypto_kernel_alloc_auth(srtp_auth_type_id_t id,
|
||||
srtp_auth_pointer_t *ap,
|
||||
size_t key_len,
|
||||
size_t tag_len)
|
||||
{
|
||||
const srtp_auth_type_t *at;
|
||||
|
||||
/*
|
||||
* if the crypto_kernel is not yet initialized, we refuse to allocate
|
||||
* any auth functions - this is a bit extra-paranoid
|
||||
*/
|
||||
if (crypto_kernel.state != srtp_crypto_kernel_state_secure) {
|
||||
return srtp_err_status_init_fail;
|
||||
}
|
||||
|
||||
at = srtp_crypto_kernel_get_auth_type(id);
|
||||
if (!at) {
|
||||
return srtp_err_status_fail;
|
||||
}
|
||||
|
||||
return ((at)->alloc(ap, key_len, tag_len));
|
||||
}
|
||||
|
||||
srtp_err_status_t srtp_crypto_kernel_load_debug_module(
|
||||
srtp_debug_module_t *new_dm)
|
||||
{
|
||||
srtp_kernel_debug_module_t *kdm, *new;
|
||||
|
||||
/* defensive coding */
|
||||
if (new_dm == NULL || new_dm->name == NULL) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
/* walk down list, checking if this type is in the list already */
|
||||
kdm = crypto_kernel.debug_module_list;
|
||||
while (kdm != NULL) {
|
||||
if (strncmp(new_dm->name, kdm->mod->name, 64) == 0) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
kdm = kdm->next;
|
||||
}
|
||||
|
||||
/* put new_dm at the head of the list */
|
||||
/* allocate memory */
|
||||
new = (srtp_kernel_debug_module_t *)srtp_crypto_alloc(
|
||||
sizeof(srtp_kernel_debug_module_t));
|
||||
if (new == NULL) {
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
|
||||
/* set fields */
|
||||
new->mod = new_dm;
|
||||
new->next = crypto_kernel.debug_module_list;
|
||||
|
||||
/* set head of list to new cipher type */
|
||||
crypto_kernel.debug_module_list = new;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
srtp_err_status_t srtp_crypto_kernel_set_debug_module(const char *name, bool on)
|
||||
{
|
||||
srtp_kernel_debug_module_t *kdm;
|
||||
|
||||
/* walk down list, checking if this type is in the list already */
|
||||
kdm = crypto_kernel.debug_module_list;
|
||||
while (kdm != NULL) {
|
||||
if (strncmp(name, kdm->mod->name, 64) == 0) {
|
||||
kdm->mod->on = on;
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
kdm = kdm->next;
|
||||
}
|
||||
|
||||
return srtp_err_status_fail;
|
||||
}
|
||||
108
libsrtp/crypto/kernel/err.c
Normal file
108
libsrtp/crypto/kernel/err.c
Normal file
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* err.c
|
||||
*
|
||||
* error status reporting functions
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*/
|
||||
/*
|
||||
*
|
||||
* Copyright(c) 2001-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 "err.h"
|
||||
#include "datatypes.h"
|
||||
#include <string.h>
|
||||
|
||||
/* srtp_err_file is the FILE to which errors are reported */
|
||||
|
||||
static FILE *srtp_err_file = NULL;
|
||||
|
||||
srtp_err_status_t srtp_err_reporting_init(void)
|
||||
{
|
||||
#ifdef ERR_REPORTING_STDOUT
|
||||
srtp_err_file = stdout;
|
||||
#elif defined(ERR_REPORTING_FILE)
|
||||
/* open file for error reporting */
|
||||
srtp_err_file = fopen(ERR_REPORTING_FILE, "w");
|
||||
if (srtp_err_file == NULL) {
|
||||
return srtp_err_status_init_fail;
|
||||
}
|
||||
#endif
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
static srtp_err_report_handler_func_t *srtp_err_report_handler = NULL;
|
||||
|
||||
srtp_err_status_t srtp_install_err_report_handler(
|
||||
srtp_err_report_handler_func_t func)
|
||||
{
|
||||
srtp_err_report_handler = func;
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
void srtp_err_report(srtp_err_reporting_level_t level, const char *format, ...)
|
||||
{
|
||||
char msg[512];
|
||||
va_list args;
|
||||
if (srtp_err_file != NULL) {
|
||||
va_start(args, format);
|
||||
vfprintf(srtp_err_file, format, args);
|
||||
va_end(args);
|
||||
}
|
||||
if (srtp_err_report_handler != NULL) {
|
||||
va_start(args, format);
|
||||
if (vsnprintf(msg, sizeof(msg), format, args) > 0) {
|
||||
/* strip trailing \n, callback should not have one */
|
||||
size_t l = strlen(msg);
|
||||
if (l && msg[l - 1] == '\n') {
|
||||
msg[l - 1] = '\0';
|
||||
}
|
||||
srtp_err_report_handler(level, msg);
|
||||
/*
|
||||
* NOTE, need to be carefull, there is a potential that
|
||||
* octet_string_set_to_zero() could
|
||||
* call srtp_err_report() in the future, leading to recursion
|
||||
*/
|
||||
octet_string_set_to_zero(msg, sizeof(msg));
|
||||
}
|
||||
va_end(args);
|
||||
}
|
||||
}
|
||||
90
libsrtp/crypto/kernel/key.c
Normal file
90
libsrtp/crypto/kernel/key.c
Normal file
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* key.c
|
||||
*
|
||||
* key usage limits enforcement
|
||||
*
|
||||
* David A. Mcgrew
|
||||
* Cisco Systems, Inc.
|
||||
*/
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-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 "key.h"
|
||||
|
||||
#define soft_limit 0x10000
|
||||
|
||||
srtp_err_status_t srtp_key_limit_set(srtp_key_limit_t key,
|
||||
const srtp_xtd_seq_num_t s)
|
||||
{
|
||||
if (s < soft_limit) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
key->num_left = s;
|
||||
key->state = srtp_key_state_normal;
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
srtp_err_status_t srtp_key_limit_clone(srtp_key_limit_t original,
|
||||
srtp_key_limit_t *new_key)
|
||||
{
|
||||
if (original == NULL) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
*new_key = original;
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
srtp_key_event_t srtp_key_limit_update(srtp_key_limit_t key)
|
||||
{
|
||||
key->num_left--;
|
||||
if (key->num_left >= soft_limit) {
|
||||
return srtp_key_event_normal; /* we're above the soft limit */
|
||||
}
|
||||
if (key->state == srtp_key_state_normal) {
|
||||
/* we just passed the soft limit, so change the state */
|
||||
key->state = srtp_key_state_past_soft_limit;
|
||||
}
|
||||
if (key->num_left < 1) {
|
||||
/* we just hit the hard limit */
|
||||
key->state = srtp_key_state_expired;
|
||||
return srtp_key_event_hard_limit;
|
||||
}
|
||||
return srtp_key_event_soft_limit;
|
||||
}
|
||||
509
libsrtp/crypto/math/datatypes.c
Normal file
509
libsrtp/crypto/math/datatypes.c
Normal file
@@ -0,0 +1,509 @@
|
||||
/*
|
||||
* datatypes.c
|
||||
*
|
||||
* data types for finite fields and functions for input, output, and
|
||||
* manipulation
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*/
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-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 "datatypes.h"
|
||||
#include "alloc.h"
|
||||
|
||||
#ifdef OPENSSL
|
||||
#include <openssl/crypto.h>
|
||||
#endif
|
||||
|
||||
#if defined(__SSE2__)
|
||||
#include <tmmintrin.h>
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#define ALIGNMENT(N) __declspec(align(N))
|
||||
#else
|
||||
#define ALIGNMENT(N) __attribute__((aligned(N)))
|
||||
#endif
|
||||
|
||||
/*
|
||||
* bit_string is a buffer that is used to hold output strings, e.g.
|
||||
* for printing.
|
||||
*/
|
||||
|
||||
/* the value MAX_PRINT_STRING_LEN is defined in datatypes.h */
|
||||
/* include space for null terminator */
|
||||
static char bit_string[MAX_PRINT_STRING_LEN + 1];
|
||||
|
||||
static uint8_t srtp_nibble_to_hex_char(uint8_t nibble)
|
||||
{
|
||||
static const char buf[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
|
||||
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
|
||||
return buf[nibble & 0xF];
|
||||
}
|
||||
|
||||
char *srtp_octet_string_hex_string(const void *s, size_t length)
|
||||
{
|
||||
const uint8_t *str = (const uint8_t *)s;
|
||||
size_t i;
|
||||
|
||||
/* double length, since one octet takes two hex characters */
|
||||
length *= 2;
|
||||
|
||||
/* truncate string if it would be too long */
|
||||
if (length > MAX_PRINT_STRING_LEN) {
|
||||
length = MAX_PRINT_STRING_LEN - 2;
|
||||
}
|
||||
|
||||
for (i = 0; i < length; i += 2) {
|
||||
bit_string[i] = srtp_nibble_to_hex_char(*str >> 4);
|
||||
bit_string[i + 1] = srtp_nibble_to_hex_char(*str++ & 0xF);
|
||||
}
|
||||
bit_string[i] = 0; /* null terminate string */
|
||||
return bit_string;
|
||||
}
|
||||
|
||||
char *v128_hex_string(v128_t *x)
|
||||
{
|
||||
size_t i, j;
|
||||
|
||||
for (i = j = 0; i < 16; i++) {
|
||||
bit_string[j++] = srtp_nibble_to_hex_char(x->v8[i] >> 4);
|
||||
bit_string[j++] = srtp_nibble_to_hex_char(x->v8[i] & 0xF);
|
||||
}
|
||||
|
||||
bit_string[j] = 0; /* null terminate string */
|
||||
return bit_string;
|
||||
}
|
||||
|
||||
char *v128_bit_string(v128_t *x)
|
||||
{
|
||||
size_t j, i;
|
||||
uint32_t mask;
|
||||
|
||||
for (j = i = 0; j < 4; j++) {
|
||||
for (mask = 0x80000000; mask > 0; mask >>= 1) {
|
||||
if (x->v32[j] & mask) {
|
||||
bit_string[i] = '1';
|
||||
} else {
|
||||
bit_string[i] = '0';
|
||||
}
|
||||
++i;
|
||||
}
|
||||
}
|
||||
bit_string[128] = 0; /* null terminate string */
|
||||
|
||||
return bit_string;
|
||||
}
|
||||
|
||||
void v128_copy_octet_string(v128_t *x, const uint8_t s[16])
|
||||
{
|
||||
#if defined(__SSE2__)
|
||||
_mm_storeu_si128((__m128i *)(x), _mm_loadu_si128((const __m128i *)(s)));
|
||||
#else
|
||||
#ifdef ALIGNMENT_32BIT_REQUIRED
|
||||
if ((((uint32_t)&s[0]) & 0x3) != 0)
|
||||
#endif
|
||||
{
|
||||
x->v8[0] = s[0];
|
||||
x->v8[1] = s[1];
|
||||
x->v8[2] = s[2];
|
||||
x->v8[3] = s[3];
|
||||
x->v8[4] = s[4];
|
||||
x->v8[5] = s[5];
|
||||
x->v8[6] = s[6];
|
||||
x->v8[7] = s[7];
|
||||
x->v8[8] = s[8];
|
||||
x->v8[9] = s[9];
|
||||
x->v8[10] = s[10];
|
||||
x->v8[11] = s[11];
|
||||
x->v8[12] = s[12];
|
||||
x->v8[13] = s[13];
|
||||
x->v8[14] = s[14];
|
||||
x->v8[15] = s[15];
|
||||
}
|
||||
#ifdef ALIGNMENT_32BIT_REQUIRED
|
||||
else {
|
||||
v128_t *v = (v128_t *)&s[0];
|
||||
|
||||
v128_copy(x, v);
|
||||
}
|
||||
#endif
|
||||
#endif /* defined(__SSE2__) */
|
||||
}
|
||||
|
||||
#if defined(__SSSE3__)
|
||||
|
||||
/* clang-format off */
|
||||
|
||||
ALIGNMENT(16)
|
||||
static const uint8_t right_shift_masks[5][16] = {
|
||||
{ 0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u,
|
||||
8u, 9u, 10u, 11u, 12u, 13u, 14u, 15u },
|
||||
{ 0x80, 0x80, 0x80, 0x80, 0u, 1u, 2u, 3u,
|
||||
4u, 5u, 6u, 7u, 8u, 9u, 10u, 11u },
|
||||
{ 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
|
||||
0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u },
|
||||
{ 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
|
||||
0x80, 0x80, 0x80, 0x80, 0u, 1u, 2u, 3u },
|
||||
/* needed for bitvector_left_shift */
|
||||
{ 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 }
|
||||
};
|
||||
|
||||
ALIGNMENT(16)
|
||||
static const uint8_t left_shift_masks[4][16] = {
|
||||
{ 0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u,
|
||||
8u, 9u, 10u, 11u, 12u, 13u, 14u, 15u },
|
||||
{ 4u, 5u, 6u, 7u, 8u, 9u, 10u, 11u,
|
||||
12u, 13u, 14u, 15u, 0x80, 0x80, 0x80, 0x80 },
|
||||
{ 8u, 9u, 10u, 11u, 12u, 13u, 14u, 15u,
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 },
|
||||
{ 12u, 13u, 14u, 15u, 0x80, 0x80, 0x80, 0x80,
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 }
|
||||
};
|
||||
|
||||
/* clang-format on */
|
||||
|
||||
void v128_left_shift(v128_t *x, size_t shift)
|
||||
{
|
||||
if (shift > 127) {
|
||||
v128_set_to_zero(x);
|
||||
return;
|
||||
}
|
||||
|
||||
const int base_index = shift >> 5;
|
||||
const int bit_index = shift & 31;
|
||||
|
||||
__m128i mm = _mm_loadu_si128((const __m128i *)x);
|
||||
__m128i mm_shift_right = _mm_cvtsi32_si128(bit_index);
|
||||
__m128i mm_shift_left = _mm_cvtsi32_si128(32 - bit_index);
|
||||
mm = _mm_shuffle_epi8(mm, ((const __m128i *)left_shift_masks)[base_index]);
|
||||
|
||||
__m128i mm1 = _mm_srl_epi32(mm, mm_shift_right);
|
||||
__m128i mm2 = _mm_sll_epi32(mm, mm_shift_left);
|
||||
mm2 = _mm_srli_si128(mm2, 4);
|
||||
mm1 = _mm_or_si128(mm1, mm2);
|
||||
|
||||
_mm_storeu_si128((__m128i *)x, mm1);
|
||||
}
|
||||
|
||||
#else /* defined(__SSSE3__) */
|
||||
|
||||
void v128_left_shift(v128_t *x, size_t shift)
|
||||
{
|
||||
const size_t base_index = shift >> 5;
|
||||
const size_t bit_index = shift & 31;
|
||||
|
||||
if (shift > 127) {
|
||||
v128_set_to_zero(x);
|
||||
return;
|
||||
}
|
||||
|
||||
if (bit_index == 0) {
|
||||
for (size_t i = 0; i < 4 - base_index; i++) {
|
||||
x->v32[i] = x->v32[i + base_index];
|
||||
}
|
||||
} else {
|
||||
for (size_t i = 0; i < 4 - base_index - 1; i++) {
|
||||
x->v32[i] = (x->v32[i + base_index] >> bit_index) ^
|
||||
(x->v32[i + base_index + 1] << (32 - bit_index));
|
||||
}
|
||||
x->v32[4 - base_index - 1] = x->v32[4 - 1] >> bit_index;
|
||||
}
|
||||
|
||||
/* now wrap up the final portion */
|
||||
for (size_t i = 4 - base_index; i < 4; i++) {
|
||||
x->v32[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* defined(__SSSE3__) */
|
||||
|
||||
/* functions manipulating bitvector_t */
|
||||
|
||||
bool bitvector_alloc(bitvector_t *v, size_t length)
|
||||
{
|
||||
size_t l;
|
||||
|
||||
/* Round length up to a multiple of bits_per_word */
|
||||
length = (length + bits_per_word - 1) & ~(size_t)((bits_per_word - 1));
|
||||
|
||||
l = length / bits_per_word * bytes_per_word;
|
||||
l = (l + 15ul) & ~15ul;
|
||||
|
||||
/* allocate memory, then set parameters */
|
||||
if (l == 0) {
|
||||
v->word = NULL;
|
||||
v->length = 0;
|
||||
return false;
|
||||
} else {
|
||||
v->word = (uint32_t *)srtp_crypto_alloc(l);
|
||||
if (v->word == NULL) {
|
||||
v->length = 0;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
v->length = length;
|
||||
|
||||
/* initialize bitvector to zero */
|
||||
bitvector_set_to_zero(v);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void bitvector_dealloc(bitvector_t *v)
|
||||
{
|
||||
if (v->word != NULL) {
|
||||
srtp_crypto_free(v->word);
|
||||
}
|
||||
v->word = NULL;
|
||||
v->length = 0;
|
||||
}
|
||||
|
||||
void bitvector_set_to_zero(bitvector_t *x)
|
||||
{
|
||||
/* C99 guarantees that memset(0) will set the value 0 for uint32_t */
|
||||
memset(x->word, 0, x->length >> 3);
|
||||
}
|
||||
|
||||
#if defined(__SSSE3__)
|
||||
|
||||
void bitvector_left_shift(bitvector_t *x, size_t shift)
|
||||
{
|
||||
if ((uint32_t)shift >= x->length) {
|
||||
bitvector_set_to_zero(x);
|
||||
return;
|
||||
}
|
||||
|
||||
const size_t base_index = shift >> 5;
|
||||
const size_t bit_index = shift & 31;
|
||||
const size_t vec_length = (x->length + 127u) >> 7;
|
||||
const __m128i *from = ((const __m128i *)x->word) + (base_index >> 2);
|
||||
__m128i *to = (__m128i *)x->word;
|
||||
__m128i *const end = to + vec_length;
|
||||
|
||||
__m128i mm_right_shift_mask =
|
||||
((const __m128i *)right_shift_masks)[4u - (base_index & 3u)];
|
||||
__m128i mm_left_shift_mask =
|
||||
((const __m128i *)left_shift_masks)[base_index & 3u];
|
||||
__m128i mm_shift_right = _mm_cvtsi32_si128(bit_index);
|
||||
__m128i mm_shift_left = _mm_cvtsi32_si128(32 - bit_index);
|
||||
|
||||
__m128i mm_current = _mm_loadu_si128(from);
|
||||
__m128i mm_current_r = _mm_srl_epi32(mm_current, mm_shift_right);
|
||||
__m128i mm_current_l = _mm_sll_epi32(mm_current, mm_shift_left);
|
||||
|
||||
while ((end - from) >= 2) {
|
||||
++from;
|
||||
__m128i mm_next = _mm_loadu_si128(from);
|
||||
|
||||
__m128i mm_next_r = _mm_srl_epi32(mm_next, mm_shift_right);
|
||||
__m128i mm_next_l = _mm_sll_epi32(mm_next, mm_shift_left);
|
||||
mm_current_l = _mm_alignr_epi8(mm_next_l, mm_current_l, 4);
|
||||
mm_current = _mm_or_si128(mm_current_r, mm_current_l);
|
||||
|
||||
mm_current = _mm_shuffle_epi8(mm_current, mm_left_shift_mask);
|
||||
|
||||
__m128i mm_temp_next = _mm_srli_si128(mm_next_l, 4);
|
||||
mm_temp_next = _mm_or_si128(mm_next_r, mm_temp_next);
|
||||
|
||||
mm_temp_next = _mm_shuffle_epi8(mm_temp_next, mm_right_shift_mask);
|
||||
mm_current = _mm_or_si128(mm_temp_next, mm_current);
|
||||
|
||||
_mm_storeu_si128(to, mm_current);
|
||||
++to;
|
||||
|
||||
mm_current_r = mm_next_r;
|
||||
mm_current_l = mm_next_l;
|
||||
}
|
||||
|
||||
mm_current_l = _mm_srli_si128(mm_current_l, 4);
|
||||
mm_current = _mm_or_si128(mm_current_r, mm_current_l);
|
||||
|
||||
mm_current = _mm_shuffle_epi8(mm_current, mm_left_shift_mask);
|
||||
|
||||
_mm_storeu_si128(to, mm_current);
|
||||
++to;
|
||||
|
||||
while (to < end) {
|
||||
_mm_storeu_si128(to, _mm_setzero_si128());
|
||||
++to;
|
||||
}
|
||||
}
|
||||
|
||||
#else /* defined(__SSSE3__) */
|
||||
|
||||
void bitvector_left_shift(bitvector_t *x, size_t shift)
|
||||
{
|
||||
const size_t base_index = shift >> 5;
|
||||
const size_t bit_index = shift & 31;
|
||||
const size_t word_length = x->length >> 5;
|
||||
|
||||
if (shift >= x->length) {
|
||||
bitvector_set_to_zero(x);
|
||||
return;
|
||||
}
|
||||
|
||||
if (bit_index == 0) {
|
||||
for (size_t i = 0; i < word_length - base_index; i++) {
|
||||
x->word[i] = x->word[i + base_index];
|
||||
}
|
||||
} else {
|
||||
for (size_t i = 0; i < word_length - base_index - 1; i++) {
|
||||
x->word[i] = (x->word[i + base_index] >> bit_index) ^
|
||||
(x->word[i + base_index + 1] << (32 - bit_index));
|
||||
}
|
||||
x->word[word_length - base_index - 1] =
|
||||
x->word[word_length - 1] >> bit_index;
|
||||
}
|
||||
|
||||
/* now wrap up the final portion */
|
||||
for (size_t i = word_length - base_index; i < word_length; i++) {
|
||||
x->word[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* defined(__SSSE3__) */
|
||||
|
||||
bool srtp_octet_string_equal(const uint8_t *a, const uint8_t *b, size_t length)
|
||||
{
|
||||
/*
|
||||
* We use this somewhat obscure implementation to try to ensure the running
|
||||
* time only depends on len, even accounting for compiler optimizations.
|
||||
* The accumulator ends up zero iff the strings are equal.
|
||||
*/
|
||||
const uint8_t *end = b + length;
|
||||
uint32_t accumulator = 0;
|
||||
|
||||
#if defined(__SSE2__)
|
||||
__m128i mm_accumulator1 = _mm_setzero_si128();
|
||||
__m128i mm_accumulator2 = _mm_setzero_si128();
|
||||
for (size_t i = 0, n = length >> 5; i < n; ++i, a += 32, b += 32) {
|
||||
__m128i mm_a1 = _mm_loadu_si128((const __m128i *)a);
|
||||
__m128i mm_b1 = _mm_loadu_si128((const __m128i *)b);
|
||||
__m128i mm_a2 = _mm_loadu_si128((const __m128i *)(a + 16));
|
||||
__m128i mm_b2 = _mm_loadu_si128((const __m128i *)(b + 16));
|
||||
mm_a1 = _mm_xor_si128(mm_a1, mm_b1);
|
||||
mm_a2 = _mm_xor_si128(mm_a2, mm_b2);
|
||||
mm_accumulator1 = _mm_or_si128(mm_accumulator1, mm_a1);
|
||||
mm_accumulator2 = _mm_or_si128(mm_accumulator2, mm_a2);
|
||||
}
|
||||
|
||||
mm_accumulator1 = _mm_or_si128(mm_accumulator1, mm_accumulator2);
|
||||
|
||||
if ((end - b) >= 16) {
|
||||
__m128i mm_a1 = _mm_loadu_si128((const __m128i *)a);
|
||||
__m128i mm_b1 = _mm_loadu_si128((const __m128i *)b);
|
||||
mm_a1 = _mm_xor_si128(mm_a1, mm_b1);
|
||||
mm_accumulator1 = _mm_or_si128(mm_accumulator1, mm_a1);
|
||||
a += 16;
|
||||
b += 16;
|
||||
}
|
||||
|
||||
if ((end - b) >= 8) {
|
||||
__m128i mm_a1 = _mm_loadl_epi64((const __m128i *)a);
|
||||
__m128i mm_b1 = _mm_loadl_epi64((const __m128i *)b);
|
||||
mm_a1 = _mm_xor_si128(mm_a1, mm_b1);
|
||||
mm_accumulator1 = _mm_or_si128(mm_accumulator1, mm_a1);
|
||||
a += 8;
|
||||
b += 8;
|
||||
}
|
||||
|
||||
mm_accumulator1 = _mm_or_si128(
|
||||
mm_accumulator1, _mm_unpackhi_epi64(mm_accumulator1, mm_accumulator1));
|
||||
mm_accumulator1 =
|
||||
_mm_or_si128(mm_accumulator1, _mm_srli_si128(mm_accumulator1, 4));
|
||||
accumulator = _mm_cvtsi128_si32(mm_accumulator1);
|
||||
#else
|
||||
uint32_t accumulator2 = 0;
|
||||
for (size_t i = 0, n = length >> 3; i < n; ++i, a += 8, b += 8) {
|
||||
uint32_t a_val1, b_val1;
|
||||
uint32_t a_val2, b_val2;
|
||||
memcpy(&a_val1, a, sizeof(a_val1));
|
||||
memcpy(&b_val1, b, sizeof(b_val1));
|
||||
memcpy(&a_val2, a + 4, sizeof(a_val2));
|
||||
memcpy(&b_val2, b + 4, sizeof(b_val2));
|
||||
accumulator |= a_val1 ^ b_val1;
|
||||
accumulator2 |= a_val2 ^ b_val2;
|
||||
}
|
||||
|
||||
accumulator |= accumulator2;
|
||||
|
||||
if ((end - b) >= 4) {
|
||||
uint32_t a_val, b_val;
|
||||
memcpy(&a_val, a, sizeof(a_val));
|
||||
memcpy(&b_val, b, sizeof(b_val));
|
||||
accumulator |= a_val ^ b_val;
|
||||
a += 4;
|
||||
b += 4;
|
||||
}
|
||||
#endif
|
||||
|
||||
while (b < end) {
|
||||
accumulator |= (*a++ ^ *b++);
|
||||
}
|
||||
|
||||
/* Return true if equal */
|
||||
return accumulator == 0;
|
||||
}
|
||||
|
||||
void srtp_cleanse(void *s, size_t len)
|
||||
{
|
||||
#if defined(__GNUC__)
|
||||
memset(s, 0, len);
|
||||
__asm__ __volatile__("" : : "r"(s) : "memory");
|
||||
#else
|
||||
volatile uint8_t *p = (volatile uint8_t *)s;
|
||||
while (len--) {
|
||||
*p++ = 0;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void octet_string_set_to_zero(void *s, size_t len)
|
||||
{
|
||||
#if defined(OPENSSL)
|
||||
OPENSSL_cleanse(s, len);
|
||||
#else
|
||||
srtp_cleanse(s, len);
|
||||
#endif
|
||||
}
|
||||
140
libsrtp/crypto/replay/rdb.c
Normal file
140
libsrtp/crypto/replay/rdb.c
Normal file
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
* rdb.c
|
||||
*
|
||||
* Implements a replay database for packet security
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-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 "rdb.h"
|
||||
|
||||
#define rdb_bits_in_bitmask (8 * sizeof(v128_t))
|
||||
|
||||
/*
|
||||
* this implementation of a replay database works as follows:
|
||||
*
|
||||
* window_start is the index of the first packet in the window
|
||||
* bitmask a bit-buffer, containing the most recently entered
|
||||
* index as the leftmost bit
|
||||
*
|
||||
*/
|
||||
|
||||
/* srtp_rdb_init initalizes rdb */
|
||||
srtp_err_status_t srtp_rdb_init(srtp_rdb_t *rdb)
|
||||
{
|
||||
v128_set_to_zero(&rdb->bitmask);
|
||||
rdb->window_start = 0;
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* srtp_rdb_check checks to see if index appears in rdb
|
||||
*/
|
||||
srtp_err_status_t srtp_rdb_check(const srtp_rdb_t *rdb, uint32_t p_index)
|
||||
{
|
||||
/* if the index appears after (or at very end of) the window, its good */
|
||||
if (p_index >= rdb->window_start + rdb_bits_in_bitmask) {
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/* if the index appears before the window, its bad */
|
||||
if (p_index < rdb->window_start) {
|
||||
return srtp_err_status_replay_old;
|
||||
}
|
||||
|
||||
/* otherwise, the index appears within the window, so check the bitmask */
|
||||
if (v128_get_bit(&rdb->bitmask, (p_index - rdb->window_start)) == 1) {
|
||||
return srtp_err_status_replay_fail;
|
||||
}
|
||||
|
||||
/* otherwise, the index is okay */
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* srtp_rdb_add_index adds index to srtp_rdb_t (and does *not* check if
|
||||
* index appears in db)
|
||||
*
|
||||
* this function should be called only after srtp_rdb_check has
|
||||
* indicated that the index does not appear in the rdb, e.g., a mutex
|
||||
* should protect the rdb between these calls
|
||||
*/
|
||||
srtp_err_status_t srtp_rdb_add_index(srtp_rdb_t *rdb, uint32_t p_index)
|
||||
{
|
||||
uint32_t delta;
|
||||
|
||||
if (p_index < rdb->window_start) {
|
||||
return srtp_err_status_replay_fail;
|
||||
}
|
||||
|
||||
delta = (p_index - rdb->window_start);
|
||||
if (delta < rdb_bits_in_bitmask) {
|
||||
/* if the p_index is within the window, set the appropriate bit */
|
||||
v128_set_bit(&rdb->bitmask, delta);
|
||||
|
||||
} else {
|
||||
delta -= rdb_bits_in_bitmask - 1;
|
||||
|
||||
/* shift the window forward by delta bits*/
|
||||
v128_left_shift(&rdb->bitmask, delta);
|
||||
v128_set_bit(&rdb->bitmask, rdb_bits_in_bitmask - 1);
|
||||
rdb->window_start += delta;
|
||||
}
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
srtp_err_status_t srtp_rdb_increment(srtp_rdb_t *rdb)
|
||||
{
|
||||
if (rdb->window_start >= 0x7fffffff) {
|
||||
return srtp_err_status_key_expired;
|
||||
}
|
||||
++rdb->window_start;
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
uint32_t srtp_rdb_get_value(const srtp_rdb_t *rdb)
|
||||
{
|
||||
return rdb->window_start;
|
||||
}
|
||||
338
libsrtp/crypto/replay/rdbx.c
Normal file
338
libsrtp/crypto/replay/rdbx.c
Normal file
@@ -0,0 +1,338 @@
|
||||
/*
|
||||
* rdbx.c
|
||||
*
|
||||
* a replay database with extended range, using a rollover counter
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-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 "rdbx.h"
|
||||
|
||||
/*
|
||||
* from RFC 3711:
|
||||
*
|
||||
* A receiver reconstructs the index i of a packet with sequence
|
||||
* number SEQ using the estimate
|
||||
*
|
||||
* i = 2^16 * v + SEQ,
|
||||
*
|
||||
* where v is chosen from the set { ROC-1, ROC, ROC+1 } such that i is
|
||||
* closest to the value 2^16 * ROC + s_l. If the value r+1 is used,
|
||||
* then the rollover counter r in the cryptographic context is
|
||||
* incremented by one (if the packet containing s is authentic).
|
||||
*/
|
||||
|
||||
/*
|
||||
* rdbx implementation notes
|
||||
*
|
||||
* A srtp_xtd_seq_num_t is essentially a sequence number for which some of
|
||||
* the data on the wire are implicit. It logically consists of a
|
||||
* rollover counter and a sequence number; the sequence number is the
|
||||
* explicit part, and the rollover counter is the implicit part.
|
||||
*
|
||||
* Upon receiving a sequence_number (e.g. in a newly received SRTP
|
||||
* packet), the complete srtp_xtd_seq_num_t can be estimated by using a
|
||||
* local srtp_xtd_seq_num_t as a basis. This is done using the function
|
||||
* srtp_index_guess(&local, &guess, seq_from_packet). This function
|
||||
* returns the difference of the guess and the local value. The local
|
||||
* srtp_xtd_seq_num_t can be moved forward to the guess using the function
|
||||
* srtp_index_advance(&guess, delta), where delta is the difference.
|
||||
*
|
||||
*
|
||||
* A srtp_rdbx_t consists of a srtp_xtd_seq_num_t and a bitmask. The index is
|
||||
* highest sequence number that has been received, and the bitmask indicates
|
||||
* which of the recent indicies have been received as well. The
|
||||
* highest bit in the bitmask corresponds to the index in the bitmask.
|
||||
*/
|
||||
|
||||
void srtp_index_init(srtp_xtd_seq_num_t *pi)
|
||||
{
|
||||
*pi = 0;
|
||||
}
|
||||
|
||||
void srtp_index_advance(srtp_xtd_seq_num_t *pi, srtp_sequence_number_t s)
|
||||
{
|
||||
*pi += s;
|
||||
}
|
||||
|
||||
/*
|
||||
* srtp_index_guess(local, guess, s)
|
||||
*
|
||||
* given a srtp_xtd_seq_num_t local (which represents the last
|
||||
* known-to-be-good received srtp_xtd_seq_num_t) and a sequence number s
|
||||
* (from a newly arrived packet), sets the contents of *guess to
|
||||
* contain the best guess of the packet index to which s corresponds,
|
||||
* and returns the difference between *guess and *local
|
||||
*
|
||||
* nota bene - the output is a signed integer, DON'T cast it to a
|
||||
* unsigned integer!
|
||||
*/
|
||||
|
||||
ssize_t srtp_index_guess(const srtp_xtd_seq_num_t *local,
|
||||
srtp_xtd_seq_num_t *guess,
|
||||
srtp_sequence_number_t s)
|
||||
{
|
||||
uint32_t local_roc = (uint32_t)(*local >> 16);
|
||||
uint16_t local_seq = (uint16_t)*local;
|
||||
uint32_t guess_roc;
|
||||
uint16_t guess_seq;
|
||||
ssize_t difference;
|
||||
|
||||
if (local_seq < seq_num_median) {
|
||||
if (s - local_seq > seq_num_median) {
|
||||
guess_roc = local_roc - 1;
|
||||
difference = s - local_seq - seq_num_max;
|
||||
} else {
|
||||
guess_roc = local_roc;
|
||||
difference = s - local_seq;
|
||||
}
|
||||
} else {
|
||||
if (local_seq - seq_num_median > s) {
|
||||
guess_roc = local_roc + 1;
|
||||
difference = s - local_seq + seq_num_max;
|
||||
} else {
|
||||
guess_roc = local_roc;
|
||||
difference = s - local_seq;
|
||||
}
|
||||
}
|
||||
guess_seq = s;
|
||||
|
||||
/* Note: guess_roc is 32 bits, so this generates a 48-bit result! */
|
||||
*guess = (((uint64_t)guess_roc) << 16) | guess_seq;
|
||||
|
||||
return difference;
|
||||
}
|
||||
|
||||
/*
|
||||
* rdbx
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* srtp_rdbx_init(&r, ws) initializes the srtp_rdbx_t pointed to by r with
|
||||
* window size ws
|
||||
*/
|
||||
srtp_err_status_t srtp_rdbx_init(srtp_rdbx_t *rdbx, size_t ws)
|
||||
{
|
||||
if (ws == 0) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
if (!bitvector_alloc(&rdbx->bitmask, ws)) {
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
|
||||
srtp_index_init(&rdbx->index);
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* srtp_rdbx_dealloc(&r) frees memory for the srtp_rdbx_t pointed to by r
|
||||
*/
|
||||
srtp_err_status_t srtp_rdbx_dealloc(srtp_rdbx_t *rdbx)
|
||||
{
|
||||
bitvector_dealloc(&rdbx->bitmask);
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* srtp_rdbx_set_roc(rdbx, roc) initializes the srtp_rdbx_t at the location rdbx
|
||||
* to have the rollover counter value roc. If that value is less than
|
||||
* the current rollover counter value, then the function returns
|
||||
* srtp_err_status_replay_old; otherwise, srtp_err_status_ok is returned.
|
||||
*
|
||||
*/
|
||||
srtp_err_status_t srtp_rdbx_set_roc(srtp_rdbx_t *rdbx, uint32_t roc)
|
||||
{
|
||||
bitvector_set_to_zero(&rdbx->bitmask);
|
||||
|
||||
/* make sure that we're not moving backwards */
|
||||
if (roc < (rdbx->index >> 16)) {
|
||||
return srtp_err_status_replay_old;
|
||||
}
|
||||
|
||||
rdbx->index &= 0xffff; /* retain lowest 16 bits */
|
||||
rdbx->index |= ((uint64_t)roc) << 16; /* set ROC */
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* srtp_rdbx_get_packet_index(rdbx) returns the value of the packet index
|
||||
* for the srtp_rdbx_t pointed to by rdbx
|
||||
*
|
||||
*/
|
||||
srtp_xtd_seq_num_t srtp_rdbx_get_packet_index(const srtp_rdbx_t *rdbx)
|
||||
{
|
||||
return rdbx->index;
|
||||
}
|
||||
|
||||
/*
|
||||
* srtp_rdbx_get_window_size(rdbx) returns the value of the window size
|
||||
* for the srtp_rdbx_t pointed to by rdbx
|
||||
*
|
||||
*/
|
||||
size_t srtp_rdbx_get_window_size(const srtp_rdbx_t *rdbx)
|
||||
{
|
||||
return bitvector_get_length(&rdbx->bitmask);
|
||||
}
|
||||
|
||||
/*
|
||||
* srtp_rdbx_check(&r, delta) checks to see if the srtp_xtd_seq_num_t
|
||||
* which is at rdbx->index + delta is in the rdb
|
||||
*/
|
||||
srtp_err_status_t srtp_rdbx_check(const srtp_rdbx_t *rdbx, ssize_t delta)
|
||||
{
|
||||
if (delta > 0) { /* if delta is positive, it's good */
|
||||
return srtp_err_status_ok;
|
||||
} else if ((int)(bitvector_get_length(&rdbx->bitmask) - 1) + delta < 0) {
|
||||
/* if delta is lower than the bitmask, it's bad */
|
||||
return srtp_err_status_replay_old;
|
||||
} else if (bitvector_get_bit(&rdbx->bitmask,
|
||||
(bitvector_get_length(&rdbx->bitmask) - 1) +
|
||||
delta) == 1) {
|
||||
/* delta is within the window, so check the bitmask */
|
||||
return srtp_err_status_replay_fail;
|
||||
}
|
||||
/* otherwise, the index is okay */
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* srtp_rdbx_add_index adds the srtp_xtd_seq_num_t at rdbx->window_start + d to
|
||||
* replay_db (and does *not* check if that srtp_xtd_seq_num_t appears in db)
|
||||
*
|
||||
* this function should be called only after replay_check has
|
||||
* indicated that the index does not appear in the rdbx, e.g., a mutex
|
||||
* should protect the rdbx between these calls if need be
|
||||
*/
|
||||
srtp_err_status_t srtp_rdbx_add_index(srtp_rdbx_t *rdbx, ssize_t delta)
|
||||
{
|
||||
if (delta > 0) {
|
||||
/* shift forward by delta */
|
||||
srtp_index_advance(&rdbx->index, (srtp_sequence_number_t)delta);
|
||||
bitvector_left_shift(&rdbx->bitmask, delta);
|
||||
bitvector_set_bit(&rdbx->bitmask,
|
||||
bitvector_get_length(&rdbx->bitmask) - 1);
|
||||
} else {
|
||||
/* delta is in window */
|
||||
bitvector_set_bit(&rdbx->bitmask,
|
||||
bitvector_get_length(&rdbx->bitmask) - 1 + delta);
|
||||
}
|
||||
|
||||
/* note that we need not consider the case that delta == 0 */
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* srtp_rdbx_estimate_index(rdbx, guess, s)
|
||||
*
|
||||
* given an rdbx and a sequence number s (from a newly arrived packet),
|
||||
* sets the contents of *guess to contain the best guess of the packet
|
||||
* index to which s corresponds, and returns the difference between
|
||||
* *guess and the locally stored synch info
|
||||
*/
|
||||
ssize_t srtp_rdbx_estimate_index(const srtp_rdbx_t *rdbx,
|
||||
srtp_xtd_seq_num_t *guess,
|
||||
srtp_sequence_number_t s)
|
||||
{
|
||||
/*
|
||||
* if the sequence number and rollover counter in the rdbx are
|
||||
* non-zero, then use the srtp_index_guess(...) function, otherwise, just
|
||||
* set the rollover counter to zero (since the srtp_index_guess(...)
|
||||
* function might incorrectly guess that the rollover counter is
|
||||
* 0xffffffff)
|
||||
*/
|
||||
|
||||
if (rdbx->index > seq_num_median) {
|
||||
return srtp_index_guess(&rdbx->index, guess, s);
|
||||
}
|
||||
|
||||
*guess = s;
|
||||
|
||||
return s - rdbx->index;
|
||||
}
|
||||
|
||||
/*
|
||||
* srtp_rdbx_get_roc(rdbx)
|
||||
*
|
||||
* Get the current rollover counter
|
||||
*
|
||||
*/
|
||||
uint32_t srtp_rdbx_get_roc(const srtp_rdbx_t *rdbx)
|
||||
{
|
||||
uint32_t roc;
|
||||
|
||||
roc = (uint32_t)(rdbx->index >> 16);
|
||||
|
||||
return roc;
|
||||
}
|
||||
|
||||
/*
|
||||
* srtp_rdbx_set_roc_seq(rdbx, roc, seq) initializes the srtp_rdbx_t at the
|
||||
* location rdbx to have the rollover counter value roc and packet sequence
|
||||
* number seq. If the new rollover counter value is less than the current
|
||||
* rollover counter value, then the function returns
|
||||
* srtp_err_status_replay_old, otherwise, srtp_err_status_ok is returned.
|
||||
*/
|
||||
srtp_err_status_t srtp_rdbx_set_roc_seq(srtp_rdbx_t *rdbx,
|
||||
uint32_t roc,
|
||||
uint16_t seq)
|
||||
{
|
||||
/* make sure that we're not moving backwards */
|
||||
if (roc < (rdbx->index >> 16)) {
|
||||
return srtp_err_status_replay_old;
|
||||
}
|
||||
|
||||
rdbx->index = seq;
|
||||
rdbx->index |= ((uint64_t)roc) << 16; /* set ROC */
|
||||
|
||||
bitvector_set_to_zero(&rdbx->bitmask);
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
1496
libsrtp/include/srtp.h
Normal file
1496
libsrtp/include/srtp.h
Normal file
File diff suppressed because it is too large
Load Diff
7
libsrtp/include/srtp3/meson.build
Normal file
7
libsrtp/include/srtp3/meson.build
Normal file
@@ -0,0 +1,7 @@
|
||||
# Copy public headers scattered across the source tree into a single directory
|
||||
# so that we can use it in declare_dependency()
|
||||
foreach h : public_headers
|
||||
configure_file(input: h,
|
||||
output: '@BASENAME@.h',
|
||||
copy: true)
|
||||
endforeach
|
||||
253
libsrtp/include/srtp_priv.h
Normal file
253
libsrtp/include/srtp_priv.h
Normal file
@@ -0,0 +1,253 @@
|
||||
/*
|
||||
* srtp_priv.h
|
||||
*
|
||||
* private internal data structures and functions for libSRTP
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*/
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef SRTP_PRIV_H
|
||||
#define SRTP_PRIV_H
|
||||
|
||||
// Leave this as the top level import. Ensures the existence of defines
|
||||
#include "config.h"
|
||||
|
||||
#include "srtp.h"
|
||||
#include "rdbx.h"
|
||||
#include "rdb.h"
|
||||
#include "cipher.h"
|
||||
#include "auth.h"
|
||||
#include "aes.h"
|
||||
#include "crypto_kernel.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define SRTP_VER_STRING PACKAGE_STRING
|
||||
#define SRTP_VERSION PACKAGE_VERSION
|
||||
|
||||
typedef struct srtp_stream_ctx_t_ srtp_stream_ctx_t;
|
||||
typedef srtp_stream_ctx_t *srtp_stream_t;
|
||||
typedef struct srtp_stream_list_ctx_t_ *srtp_stream_list_t;
|
||||
|
||||
/*
|
||||
* the following declarations are libSRTP internal functions
|
||||
*/
|
||||
|
||||
/*
|
||||
* srtp_get_stream(ssrc) returns a pointer to the stream corresponding
|
||||
* to ssrc, or NULL if no stream exists for that ssrc
|
||||
*/
|
||||
srtp_stream_t srtp_get_stream(srtp_t srtp, uint32_t ssrc);
|
||||
|
||||
/*
|
||||
* libsrtp internal datatypes
|
||||
*/
|
||||
typedef enum direction_t {
|
||||
dir_unknown = 0,
|
||||
dir_srtp_sender = 1,
|
||||
dir_srtp_receiver = 2
|
||||
} direction_t;
|
||||
|
||||
/*
|
||||
* srtp_session_keys_t will contain the encryption, hmac, salt keys
|
||||
* for both SRTP and SRTCP. The session keys will also contain the
|
||||
* MKI ID which is used to identify the session keys.
|
||||
*/
|
||||
typedef struct srtp_session_keys_t {
|
||||
srtp_cipher_t *rtp_cipher;
|
||||
srtp_cipher_t *rtp_xtn_hdr_cipher;
|
||||
srtp_auth_t *rtp_auth;
|
||||
srtp_cipher_t *rtcp_cipher;
|
||||
srtp_auth_t *rtcp_auth;
|
||||
uint8_t salt[SRTP_AEAD_SALT_LEN];
|
||||
uint8_t c_salt[SRTP_AEAD_SALT_LEN];
|
||||
uint8_t *mki_id;
|
||||
srtp_key_limit_ctx_t *limit;
|
||||
} srtp_session_keys_t;
|
||||
|
||||
/*
|
||||
* an srtp_stream_t has its own SSRC, encryption key, authentication
|
||||
* key, sequence number, and replay database
|
||||
*
|
||||
* note that the keys might not actually be unique, in which case the
|
||||
* srtp_cipher_t and srtp_auth_t pointers will point to the same structures
|
||||
*/
|
||||
typedef struct srtp_stream_ctx_t_ {
|
||||
uint32_t ssrc;
|
||||
srtp_session_keys_t *session_keys;
|
||||
size_t num_master_keys;
|
||||
bool use_mki;
|
||||
size_t mki_size;
|
||||
srtp_rdbx_t rtp_rdbx;
|
||||
srtp_sec_serv_t rtp_services;
|
||||
srtp_rdb_t rtcp_rdb;
|
||||
srtp_sec_serv_t rtcp_services;
|
||||
direction_t direction;
|
||||
bool allow_repeat_tx;
|
||||
uint8_t *enc_xtn_hdr;
|
||||
size_t enc_xtn_hdr_count;
|
||||
uint32_t pending_roc;
|
||||
} strp_stream_ctx_t_;
|
||||
|
||||
/*
|
||||
* an srtp_ctx_t holds a stream list and a service description
|
||||
*/
|
||||
typedef struct srtp_ctx_t_ {
|
||||
srtp_stream_list_t stream_list; /* linked list of streams */
|
||||
struct srtp_stream_ctx_t_ *stream_template; /* act as template for other */
|
||||
/* streams */
|
||||
void *user_data; /* user custom data */
|
||||
} srtp_ctx_t_;
|
||||
|
||||
/*
|
||||
* srtp_hdr_t represents an RTP or SRTP header. The bit-fields in
|
||||
* this structure should be declared "unsigned int" instead of
|
||||
* "unsigned char", but doing so causes the MS compiler to not
|
||||
* fully pack the bit fields.
|
||||
*
|
||||
* In this implementation, an srtp_hdr_t is assumed to be 32-bit aligned
|
||||
*
|
||||
* (note that this definition follows that of RFC 1889 Appendix A, but
|
||||
* is not identical)
|
||||
*/
|
||||
|
||||
#ifndef WORDS_BIGENDIAN
|
||||
|
||||
typedef struct {
|
||||
unsigned char cc : 4; /* CSRC count */
|
||||
unsigned char x : 1; /* header extension flag */
|
||||
unsigned char p : 1; /* padding flag */
|
||||
unsigned char version : 2; /* protocol version */
|
||||
unsigned char pt : 7; /* payload type */
|
||||
unsigned char m : 1; /* marker bit */
|
||||
uint16_t seq; /* sequence number */
|
||||
uint32_t ts; /* timestamp */
|
||||
uint32_t ssrc; /* synchronization source */
|
||||
} srtp_hdr_t;
|
||||
|
||||
#else /* BIG_ENDIAN */
|
||||
|
||||
typedef struct {
|
||||
unsigned char version : 2; /* protocol version */
|
||||
unsigned char p : 1; /* padding flag */
|
||||
unsigned char x : 1; /* header extension flag */
|
||||
unsigned char cc : 4; /* CSRC count */
|
||||
unsigned char m : 1; /* marker bit */
|
||||
unsigned char pt : 7; /* payload type */
|
||||
uint16_t seq; /* sequence number */
|
||||
uint32_t ts; /* timestamp */
|
||||
uint32_t ssrc; /* synchronization source */
|
||||
} srtp_hdr_t;
|
||||
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
uint16_t profile_specific; /* profile-specific info */
|
||||
uint16_t length; /* number of 32-bit words in extension */
|
||||
} srtp_hdr_xtnd_t;
|
||||
|
||||
/*
|
||||
* srtcp_hdr_t represents a secure rtcp header
|
||||
*
|
||||
* in this implementation, an srtcp header is assumed to be 32-bit
|
||||
* aligned
|
||||
*/
|
||||
|
||||
#ifndef WORDS_BIGENDIAN
|
||||
|
||||
typedef struct {
|
||||
unsigned char rc : 5; /* reception report count */
|
||||
unsigned char p : 1; /* padding flag */
|
||||
unsigned char version : 2; /* protocol version */
|
||||
unsigned char pt : 8; /* payload type */
|
||||
uint16_t len; /* length */
|
||||
uint32_t ssrc; /* synchronization source */
|
||||
} srtcp_hdr_t;
|
||||
|
||||
typedef struct {
|
||||
unsigned int index : 31; /* srtcp packet index in network order! */
|
||||
unsigned int e : 1; /* encrypted? 1=yes */
|
||||
/* optional mikey/etc go here */
|
||||
/* and then the variable-length auth tag */
|
||||
} srtcp_trailer_t;
|
||||
|
||||
#else /* BIG_ENDIAN */
|
||||
|
||||
typedef struct {
|
||||
unsigned char version : 2; /* protocol version */
|
||||
unsigned char p : 1; /* padding flag */
|
||||
unsigned char rc : 5; /* reception report count */
|
||||
unsigned char pt : 8; /* payload type */
|
||||
uint16_t len; /* length */
|
||||
uint32_t ssrc; /* synchronization source */
|
||||
} srtcp_hdr_t;
|
||||
|
||||
typedef struct {
|
||||
unsigned int e : 1; /* encrypted? 1=yes */
|
||||
unsigned int index : 31; /* srtcp packet index */
|
||||
/* optional mikey/etc go here */
|
||||
/* and then the variable-length auth tag */
|
||||
} srtcp_trailer_t;
|
||||
|
||||
#endif
|
||||
|
||||
/*
|
||||
* srtp_handle_event(srtp, srtm, evnt) calls the event handling
|
||||
* function, if there is one.
|
||||
*
|
||||
* This macro is not included in the documentation as it is
|
||||
* an internal-only function.
|
||||
*/
|
||||
|
||||
#define srtp_handle_event(srtp, strm, evnt) \
|
||||
if (srtp_event_handler) { \
|
||||
srtp_event_data_t data; \
|
||||
data.session = srtp; \
|
||||
data.ssrc = ntohl(strm->ssrc); \
|
||||
data.event = evnt; \
|
||||
srtp_event_handler(&data); \
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* SRTP_PRIV_H */
|
||||
121
libsrtp/include/stream_list_priv.h
Normal file
121
libsrtp/include/stream_list_priv.h
Normal file
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* stream_list_priv.h
|
||||
*
|
||||
* list of SRTP streams, keyed by SSRC
|
||||
*
|
||||
* Alba Mendez
|
||||
*/
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-2017, Cisco Systems, Inc.
|
||||
* Copyright (c) 2022, Dolby Laboratories, 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef SRTP_STREAM_LIST_PRIV_H
|
||||
#define SRTP_STREAM_LIST_PRIV_H
|
||||
|
||||
#include "srtp_priv.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* srtp_stream_list_t holds a list of srtp_stream_t, each identified
|
||||
* by their SSRC.
|
||||
*
|
||||
* the API was extracted to allow downstreams to override its
|
||||
* implementation by defining the `SRTP_NO_STREAM_LIST` preprocessor
|
||||
* directive, which removes the default implementation of these
|
||||
* functions.
|
||||
*
|
||||
* this is still an internal interface; there is no stability
|
||||
* guarantee--downstreams should watch this file for changes in
|
||||
* signatures or semantics.
|
||||
*/
|
||||
|
||||
/**
|
||||
* allocate and initialize a stream list instance
|
||||
*/
|
||||
srtp_err_status_t srtp_stream_list_alloc(srtp_stream_list_t *list_ptr);
|
||||
|
||||
/**
|
||||
* deallocate a stream list instance
|
||||
*
|
||||
* the list must be empty or else an error is returned.
|
||||
*/
|
||||
srtp_err_status_t srtp_stream_list_dealloc(srtp_stream_list_t list);
|
||||
|
||||
/**
|
||||
* insert a stream into the list
|
||||
*
|
||||
* returns srtp_err_status_alloc_fail if insertion failed due to unavailable
|
||||
* capacity in the list. if operation succeeds, srtp_err_status_ok is returned
|
||||
*
|
||||
* if another stream with the same SSRC already exists in the list,
|
||||
* behavior is undefined. if the SSRC field is mutated while the
|
||||
* stream is inserted, further operations have undefined behavior
|
||||
*/
|
||||
srtp_err_status_t srtp_stream_list_insert(srtp_stream_list_t list,
|
||||
srtp_stream_t stream);
|
||||
|
||||
/*
|
||||
* look up the stream corresponding to the specified SSRC and return it.
|
||||
* if no such SSRC is found, NULL is returned.
|
||||
*/
|
||||
srtp_stream_t srtp_stream_list_get(srtp_stream_list_t list, uint32_t ssrc);
|
||||
|
||||
/**
|
||||
* remove the stream from the list.
|
||||
*
|
||||
* The stream to be removed is referenced "by value", i.e., by the pointer to be
|
||||
* removed from the list. This pointer is obtained using `srtp_stream_list_get`
|
||||
* or as callback parameter in `srtp_stream_list_for_each`.
|
||||
*/
|
||||
void srtp_stream_list_remove(srtp_stream_list_t list, srtp_stream_t stream);
|
||||
|
||||
/**
|
||||
* iterate through all stored streams. while iterating, it is allowed to delete
|
||||
* the current element; any other mutation to the list is undefined behavior.
|
||||
* returning non-zero from callback aborts the iteration.
|
||||
*/
|
||||
void srtp_stream_list_for_each(srtp_stream_list_t list,
|
||||
bool (*callback)(srtp_stream_t, void *),
|
||||
void *data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* SRTP_STREAM_LIST_PRIV_H */
|
||||
5034
libsrtp/srtp/srtp.c
Normal file
5034
libsrtp/srtp/srtp.c
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user