提交 74cbc377 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Changes in libsrtp.gyp to make 2.3.0 work:

- Define GCM
- Update source files and includes
上级 660a5be9

要显示的修改太多。

为保证性能只显示 1000 of 1000+ 个文件。

......@@ -7,6 +7,7 @@
{
'variables': {
'use_openssl%': 1,
'use_nss%': 0, # Not used here.
},
'target_defaults': {
'defines': [
......@@ -24,9 +25,10 @@
['use_openssl==1', {
'defines': [
'OPENSSL',
'GCM',
],
}],
# NOTE: Avoid having to set os_posix
# NOTE: Avoid having to set os_posix.
# ['os_posix==1', {
['OS!="win"', {
'defines': [
......@@ -86,13 +88,12 @@
'direct_dependent_settings': {
'include_dirs': [
# NOTE: I don't think we must export this.
# './config',
'srtp/include',
# NOTE: I don't think we must export this.
# 'srtp/crypto/include',
],
'conditions': [
# NOTE: Avoid having to set os_posix
# NOTE: Avoid having to set os_posix.
# ['os_posix==1', {
['OS!="win"', {
'defines': [
......@@ -132,19 +133,20 @@
# includes
'srtp/include/ekt.h',
'srtp/include/getopt_s.h',
'srtp/include/rtp.h',
'srtp/include/rtp_priv.h',
'srtp/include/srtp.h',
'srtp/include/srtp_priv.h',
'srtp/include/ut_sim.h',
# headers
'srtp/crypto/include/aes.h',
'srtp/crypto/include/aes_gcm.h',
'srtp/crypto/include/aes_icm.h',
'srtp/crypto/include/aes_icm_ext.h',
'srtp/crypto/include/alloc.h',
'srtp/crypto/include/auth.h',
'srtp/crypto/include/cipher.h',
'srtp/crypto/include/cipher_priv.h',
'srtp/crypto/include/cipher_types.h',
'srtp/crypto/include/crypto_kernel.h',
'srtp/crypto/include/crypto_math.h',
'srtp/crypto/include/crypto_types.h',
'srtp/crypto/include/datatypes.h',
'srtp/crypto/include/err.h',
......@@ -192,8 +194,6 @@
'srtp/crypto/cipher/aes_gcm_ossl.c',
'srtp/crypto/cipher/aes_icm_ossl.c',
'srtp/crypto/hash/hmac_ossl.c',
'srtp/crypto/include/aes_gcm_ossl.h',
'srtp/crypto/include/aes_icm_ossl.h',
],
}],
],
......
......@@ -23,6 +23,7 @@ old?
*.app
# srtp things
build
Debug
Makefile
Root
......@@ -35,10 +36,8 @@ crypto/test/cipher_driver
crypto/test/datatypes_driver
crypto/test/env
crypto/test/kernel_driver
crypto/test/rand_gen
crypto/test/sha1_driver
crypto/test/stat_driver
crypto/test/rand_gen_soak
tables/aes_tables
test/dtls_srtp_driver
test/rdbx_driver
......
dist: trusty
dist: xenial
sudo: false
language: c
......@@ -9,40 +9,59 @@ env:
matrix:
include:
# default linux build with gcc
# linux build
- os: linux
env:
- TEST="linux gcc"
- TEST="linux (gcc / valgrind)"
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- gcc-6
- valgrind
script:
- CC=gcc-6 EXTRA_CFLAGS=-Werror ./configure
- make
- make runtest
- make runtest-valgrind
- make distclean
- mkdir build && cd build
- cmake ..
- make
- make test
# linux build with openssl and gcc
# linux build with openssl
- os: linux
env:
- TEST="linux gcc (openssl)"
- TEST="linux openssl (gcc / valgrind)"
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- gcc-6
- valgrind
script:
- CC=gcc-6 EXTRA_CFLAGS=-Werror ./configure --enable-openssl
- make
- make runtest
- make runtest-valgrind
- make distclean
- mkdir build && cd build
- cmake -DENABLE_OPENSSL=ON ..
- make
- make test
- cd ..
- mkdir build_shared && cd build_shared
- cmake -DENABLE_OPENSSL=ON -DBUILD_SHARED_LIBS=ON ..
- make
- make test
# linux build with openssl and clang
- os: linux
env:
- TEST="linux clang (openssl)"
- TEST="linux openssl (clang)"
addons:
apt:
packages:
......@@ -52,15 +71,64 @@ matrix:
- make
- make runtest
# default osx build with xcode (clang)
# linux build with nss
- os: linux
env:
- TEST="linux nss (gcc / valgrind)"
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- gcc-6
- valgrind
- libnss3-dev
script:
- CC=gcc-6 EXTRA_CFLAGS=-Werror ./configure --enable-nss
- make
- make runtest
- make runtest-valgrind
# default osx build
- os: osx
env:
- TEST="osx XCode 8.2"
osx_image: xcode8.2
- TEST="osx"
script:
- EXTRA_CFLAGS=-Werror ./configure
- make
- make runtest
- make distclean
- mkdir build && cd build
- cmake ..
- make
- make test
# osx build with openssl
- os: osx
osx_image: xcode11.2
env:
- TEST="osx openssl"
before_install:
- brew install openssl@1.1
script:
- PKG_CONFIG_PATH=$(brew --prefix openssl@1.1)/lib/pkgconfig EXTRA_CFLAGS=-Werror ./configure --enable-openssl
- make
- make runtest
- make distclean
- mkdir build && cd build
- cmake -DOPENSSL_ROOT_DIR=$(brew --prefix openssl@1.1) -DENABLE_OPENSSL=ON ..
- make
- make test
# osx build with nss
- os: osx
osx_image: xcode11.2
env:
- TEST="osx nss"
script:
- PKG_CONFIG_PATH=$(brew --prefix nss)/lib/pkgconfig EXTRA_CFLAGS=-Werror ./configure --enable-nss
- make
- make runtest
# code format check
- os: linux
......@@ -73,22 +141,6 @@ matrix:
script:
- CLANG_FORMAT=clang-format-3.9 ./format.sh -d
# valgrind
- os: linux
env:
- TEST="valgrind (openssl)"
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- gcc-6
- valgrind
script:
- CC=gcc-6 ./configure --enable-openssl
- make
- make runtest-valgrind
# big-endian
- os: linux
sudo: true
......@@ -113,6 +165,18 @@ matrix:
- file test/srtp_driver
- make runtest
# linux build of fuzzer
- os: linux
env:
- TEST="fuzzer (build only)"
addons:
apt:
packages:
- clang
script:
- CC=clang CXX=clang++ CXXFLAGS="-fsanitize=fuzzer-no-link,address,undefined -g -O3" CFLAGS="-fsanitize=fuzzer-no-link,address,undefined -g -O3" LDFLAGS="-fsanitize=fuzzer-no-link,address,undefined" ./configure
- LIBFUZZER="-fsanitize=fuzzer" make srtp-fuzzer
# coverity scan
- os: linux
env:
......@@ -129,3 +193,67 @@ matrix:
branch_pattern: master
script:
- echo -n | openssl s_client -connect scan.coverity.com:443 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' | sudo tee -a /etc/ssl/certs/ca-
# windows build
- os: windows
env:
- TEST="windows"
script:
- export PATH="c:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin":$PATH
- mkdir build && cd build
- cmake -G "Visual Studio 15 2017" ..
- msbuild.exe libsrtp2.sln -p:Configuration=Release
- msbuild.exe RUN_TESTS.vcxproj -p:Configuration=Release
- cd ..
- mkdir build_shared && cd build_shared
- cmake -G "Visual Studio 15 2017" -DBUILD_SHARED_LIBS=ON ..
- msbuild.exe libsrtp2.sln -p:Configuration=Release
- msbuild.exe RUN_TESTS.vcxproj -p:Configuration=Release
# android build
- os: linux
env:
- TEST="android"
script:
- wget -q https://dl.google.com/android/repository/android-ndk-r20b-linux-x86_64.zip
- unzip -qq android-ndk-r20b-linux-x86_64.zip
- ANDROID_NDK=`pwd`/android-ndk-r20b
- mkdir build_android
- cd build_android
- cmake -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake -DANDROID_ABI=arm64-v8a ..
- make
- cd ..
- TOOLCHAIN=$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64
AR=$TOOLCHAIN/bin/aarch64-linux-android-ar
AS=$TOOLCHAIN/bin/aarch64-linux-android-as
CC=$TOOLCHAIN/bin/aarch64-linux-android21-clang
CXX=$TOOLCHAIN/bin/aarch64-linux-android21-clang++
LD=$TOOLCHAIN/bin/aarch64-linux-android-ld
RANLIB=$TOOLCHAIN/bin/aarch64-linux-android-ranlib
STRIP=$TOOLCHAIN/bin/aarch64-linux-android-strip
./configure --host aarch64-linux-android
- make
# ios build with openssl
- os: osx
osx_image: xcode11.2
env:
- TEST="ios"
script:
- wget -q https://raw.githubusercontent.com/leetal/ios-cmake/master/ios.toolchain.cmake
- mkdir build && cd build
- cmake -DCMAKE_TOOLCHAIN_FILE=../ios.toolchain.cmake -DPLATFORM=OS64 ..
- make
- cd ..
- CFLAGS="-arch arm64 --sysroot=$(xcrun --sdk iphoneos --show-sdk-path) -miphoneos-version-min=8.0 -fembed-bitcode"
LDFLAGS="-arch arm64 --sysroot=$(xcrun --sdk iphoneos --show-sdk-path) -miphoneos-version-min=8.0 -fembed-bitcode"
AR="$(xcrun --find --sdk iphoneos ar)"
AS="$(xcrun --find --sdk iphoneos as)"
CC="$(xcrun --find --sdk iphoneos clang)"
CXX="$(xcrun --find --sdk iphoneos clang++)"
LD="$(xcrun --find --sdk iphoneos ld)"
RANLIB="$(xcrun --find --sdk iphoneos ranlib)"
STRIP="$(xcrun --find --sdk iphoneos strip)"
./configure --host arm-apple-darwin
- make
- make shared_library
Changelog
2.2.0-pre (This version)
2.3.0-pre (This version)
Major changes
A fuzzer was added to the project based on libFuzzer. This is run as part of Google oss-fuzz, the current status can be found at https://oss-fuzz-build-logs.storage.googleapis.com/index.html#libsrtp . Details available in PR #442 and issue #393.
CMake support was added as an alternative build system. This was primarily added to replace the Visual Studio project files currently checked in but has been extend to support building on all platforms. Initial PR #449 and #455 but has been support has been continuously improved.
NSS support for crypto backend was added. This is can be used as an alternative to openssl. Initial PR #413 but there has been numerous improvements.
PR #474 - Add flag to disable pcap dependency when build test apps.
PR #472 - Add ci builds for ios and android.
PR #463 - problem on srtp_aes_icm_openssl_encrypt.
PR #471 - Allow the pcap file to be read from a physical file.
PR #457 - Fix docs crypto_policy_* -> srtp_crypto_policy_* .
PR #452 - crypto/cipher: Reset the EVP_CIPHER_CTX structure before each test.
PR #444 - Extend rtp_ecoder to support RTCP and multiplexed SSRC's.
PR #443 - rtp_decoder avoid crash when no packets found.
PR #429 - Rand for test only.
PR #421 - Fix GCM IV logging.
PR #415 - Fixes unaligned trailer access.
PR #414 - Detect and handle broken "OPENSSL_cleanse".
2.2.0
Major changes
......
cmake_minimum_required(VERSION 2.8)
project(libsrtp2 LANGUAGES C)
set(PACKAGE_VERSION 2.3.0)
set(PACKAGE_STRING "${CMAKE_PROJECT_NAME} ${PACKAGE_VERSION}")
include(TestBigEndian)
include(CheckIncludeFile)
include(CheckFunctionExists)
include(CheckTypeSize)
include(CheckCSourceCompiles)
test_big_endian(WORDS_BIGENDIAN)
if (CMAKE_SYSTEM_PROCESSOR MATCHES "(x86)|(X86)|(amd64)|(AMD64)")
set (HAVE_X86 TRUE)
else ()
set (HAVE_X86 FALSE)
endif ()
check_include_file(arpa/inet.h HAVE_ARPA_INET_H)
check_include_file(byteswap.h HAVE_BYTESWAP_H)
check_include_file(inttypes.h HAVE_INTTYPES_H)
check_include_file(machine/types.h HAVE_MACHINE_TYPES_H)
check_include_file(netinet/in.h HAVE_NETINET_IN_H)
check_include_file(stdint.h HAVE_STDINT_H)
check_include_file(stdlib.h HAVE_STDLIB_H)
check_include_file(sys/int_types.h HAVE_SYS_INT_TYPES_H)
check_include_file(sys/socket.h HAVE_SYS_SOCKET_H)
check_include_file(sys/types.h HAVE_SYS_TYPES_H)
check_include_file(unistd.h HAVE_UNISTD_H)
check_include_file(windows.h HAVE_WINDOWS_H)
check_include_file(winsock2.h HAVE_WINSOCK2_H)
check_function_exists(sigaction HAVE_SIGACTION)
check_function_exists(inet_aton HAVE_INET_ATON)
check_function_exists(usleep HAVE_USLEEP)
check_type_size(uint8_t UINT8_T)
check_type_size(uint16_t UINT16_T)
check_type_size(uint32_t UINT32_T)
check_type_size(uint64_t UINT64_T)
check_type_size(int32_t INT32_T)
check_type_size("unsigned long" SIZEOF_UNSIGNED_LONG)
check_type_size("unsigned long long" SIZEOF_UNSIGNED_LONG_LONG)
check_c_source_compiles("inline void func(); void func() { } int main() { func(); return 0; }" HAVE_INLINE)
if(NOT HAVE_INLINE)
check_c_source_compiles("__inline void func(); void func() { } int main() { func(); return 0; }" HAVE___INLINE)
endif()
set(ENABLE_DEBUG_LOGGING OFF CACHE BOOL "Enable debug logging in all modules")
set(ERR_REPORTING_STDOUT OFF CACHE BOOL "Enable logging to stdout")
set(ERR_REPORTING_FILE "" CACHE FILEPATH "Use file for logging")
set(ENABLE_OPENSSL OFF CACHE BOOL "Enable OpenSSL crypto engine")
set(TEST_APPS ON CACHE BOOL "Build test applications")
option(BUILD_SHARED_LIBS "Build shared library" OFF)
if(ENABLE_OPENSSL)
find_package(OpenSSL REQUIRED)
include_directories(${OPENSSL_INCLUDE_DIR})
endif()
set(OPENSSL ${ENABLE_OPENSSL} CACHE BOOL INTERNAL)
set(GCM ${ENABLE_OPENSSL} CACHE BOOL INTERNAL)
set(CONFIG_FILE_DIR ${CMAKE_CURRENT_BINARY_DIR})
include_directories(${CONFIG_FILE_DIR})
configure_file(config_in_cmake.h ${CONFIG_FILE_DIR}/config.h)
add_definitions(-DHAVE_CONFIG_H)
set(SOURCES_C
srtp/ekt.c
srtp/srtp.c
)
set(CIPHERS_SOURCES_C
crypto/cipher/cipher.c
crypto/cipher/null_cipher.c
)
if(ENABLE_OPENSSL)
list(APPEND CIPHERS_SOURCES_C
crypto/cipher/aes_icm_ossl.c
crypto/cipher/aes_gcm_ossl.c
)
else()
list(APPEND CIPHERS_SOURCES_C
crypto/cipher/aes.c
crypto/cipher/aes_icm.c
)
endif()
set(HASHES_SOURCES_C
crypto/hash/auth.c
crypto/hash/null_auth.c
)
if(ENABLE_OPENSSL)
list(APPEND HASHES_SOURCES_C
crypto/hash/hmac_ossl.c
)
else()
list(APPEND HASHES_SOURCES_C
crypto/hash/hmac.c
crypto/hash/sha1.c
)
endif()
set(KERNEL_SOURCES_C
crypto/kernel/alloc.c
crypto/kernel/crypto_kernel.c
crypto/kernel/err.c
crypto/kernel/key.c
)
set(MATH_SOURCES_C
crypto/math/datatypes.c
crypto/math/stat.c
)
set(REPLAY_SOURCES_C
crypto/replay/rdb.c
crypto/replay/rdbx.c
crypto/replay/ut_sim.c
)
set(SOURCES_H
crypto/include/aes.h
crypto/include/aes_icm.h
crypto/include/alloc.h
crypto/include/auth.h
crypto/include/cipher.h
crypto/include/cipher_types.h
crypto/include/crypto_kernel.h
crypto/include/crypto_types.h
crypto/include/datatypes.h
crypto/include/err.h
crypto/include/hmac.h
crypto/include/integers.h
crypto/include/key.h
crypto/include/null_auth.h
crypto/include/null_cipher.h
crypto/include/rdb.h
crypto/include/rdbx.h
crypto/include/sha1.h
crypto/include/stat.h
include/srtp.h
include/srtp_priv.h
include/ut_sim.h
${CONFIG_FILE_DIR}/config.h
)
if(BUILD_SHARED_LIBS AND WIN32)
list(APPEND SOURCES_C
srtp.def
)
endif()
source_group("src" FILES ${SOURCES_C})
source_group("src\\Ciphers" FILES ${CIPHERS_SOURCES_C})
source_group("src\\Hashes" FILES ${HASHES_SOURCES_C})
source_group("src\\Kernel" FILES ${KERNEL_SOURCES_C})
source_group("src\\Math" FILES ${MATH_SOURCES_C})
source_group("src\\Replay" FILES ${REPLAY_SOURCES_C})
source_group("include" FILES ${SOURCES_H})
add_library(srtp2
${SOURCES_C}
${CIPHERS_SOURCES_C}
${HASHES_SOURCES_C}
${KERNEL_SOURCES_C}
${MATH_SOURCES_C}
${REPLAY_SOURCES_C}
${SOURCES_H}
)
target_include_directories(srtp2 PUBLIC crypto/include include)
if(ENABLE_OPENSSL)
target_link_libraries(srtp2 OpenSSL::Crypto)
endif()
if(WIN32)
target_link_libraries(srtp2 ws2_32)
endif()
install(TARGETS srtp2 DESTINATION lib)
install(FILES include/srtp.h crypto/include/auth.h
crypto/include/cipher.h
crypto/include/cipher_types.h
DESTINATION include/srtp2)
if(TEST_APPS)
enable_testing()
if(NOT (BUILD_SHARED_LIBS AND WIN32))
add_executable(test_srtp test/test_srtp.c)
target_link_libraries(test_srtp srtp2)
add_test(test_srtp test_srtp)
endif()
add_executable(srtp_driver test/srtp_driver.c
test/util.c test/getopt_s.c)
target_link_libraries(srtp_driver srtp2)
add_test(srtp_driver srtp_driver -v)
endif()
......@@ -14,26 +14,47 @@
# distribution cleans and builds a .tgz
# tags builds etags file from all .c and .h files
USE_OPENSSL = @USE_OPENSSL@
DYNAMIC_PATH_VAR = @DYNAMIC_PATH_VAR@
CRYPTO_LIBDIR = @CRYPTO_LIBDIR@
USE_EXTERNAL_CRYPTO = @USE_EXTERNAL_CRYPTO@
HAVE_PCAP = @HAVE_PCAP@
# Specify how tests should find shared libraries on macOS and Linux
#
# macOS purges DYLD_LIBRARY_PATH when spawning subprocesses, so it's
# not possible to pass this in from the outside; we have to specify
# it for any subprocesses we call. No support for dynamic linked
# tests on Windows.
ifneq ($(strip $(CRYPTO_LIBDIR)),)
ifneq ($(OS),Windows_NT)
UNAME_S = $(shell uname -s)
ifeq ($(UNAME_S),Linux)
FIND_LIBRARIES = LD_LIBRARY_PATH=$(CRYPTO_LIBDIR)
endif
ifeq ($(UNAME_S),Darwin)
FIND_LIBRARIES = DYLD_LIBRARY_PATH=$(CRYPTO_LIBDIR)
endif
CRYPTO_LIBDIR_FORWARD = CRYPTO_LIBDIR=$(CRYPTO_LIBDIR)
endif
endif
.PHONY: all shared_library test
all: test
runtest: test
@echo "running libsrtp2 test applications..."
crypto/test/cipher_driver$(EXE) -v >/dev/null
crypto/test/kernel_driver$(EXE) -v >/dev/null
test/test_srtp$(EXE) >/dev/null
test/rdbx_driver$(EXE) -v >/dev/null
test/srtp_driver$(EXE) -v >/dev/null
test/roc_driver$(EXE) -v >/dev/null
test/replay_driver$(EXE) -v >/dev/null
test/dtls_srtp_driver$(EXE) >/dev/null
cd test; $(abspath $(srcdir))/test/rtpw_test.sh -w $(abspath $(srcdir))/test/words.txt >/dev/null
ifeq (1, $(USE_OPENSSL))
cd test; $(abspath $(srcdir))/test/rtpw_test_gcm.sh -w $(abspath $(srcdir))/test/words.txt >/dev/null
$(FIND_LIBRARIES) crypto/test/cipher_driver$(EXE) -v >/dev/null
$(FIND_LIBRARIES) crypto/test/kernel_driver$(EXE) -v >/dev/null
$(FIND_LIBRARIES) test/test_srtp$(EXE) >/dev/null
$(FIND_LIBRARIES) test/rdbx_driver$(EXE) -v >/dev/null
$(FIND_LIBRARIES) test/srtp_driver$(EXE) -v >/dev/null
$(FIND_LIBRARIES) test/roc_driver$(EXE) -v >/dev/null
$(FIND_LIBRARIES) test/replay_driver$(EXE) -v >/dev/null
$(FIND_LIBRARIES) test/dtls_srtp_driver$(EXE) >/dev/null
cd test; $(CRYPTO_LIBDIR_FORWARD) $(abspath $(srcdir))/test/rtpw_test.sh -w $(abspath $(srcdir))/test/words.txt >/dev/null
ifeq (1, $(USE_EXTERNAL_CRYPTO))
cd test; $(CRYPTO_LIBDIR_FORWARD) $(abspath $(srcdir))/test/rtpw_test_gcm.sh -w $(abspath $(srcdir))/test/words.txt >/dev/null
endif
@echo "libsrtp2 test applications passed."
$(MAKE) -C crypto runtest
......@@ -47,10 +68,14 @@ runtest-valgrind: test
# makefile variables
CC = @CC@
CXX = @CXX@
INCDIR = -Icrypto/include -I$(srcdir)/include -I$(srcdir)/crypto/include
DEFS = @DEFS@
CPPFLAGS= @CPPFLAGS@
CFLAGS = @CFLAGS@
CXXFLAGS= @CXXFLAGS@
srtp-fuzzer: CFLAGS += -g
srtp-fuzzer: CXXFLAGS += -g
LIBS = @LIBS@
LDFLAGS = -L. @LDFLAGS@
COMPILE = $(CC) $(DEFS) $(INCDIR) $(CPPFLAGS) $(CFLAGS)
......@@ -95,7 +120,7 @@ SHAREDLIBSUFFIXNOVER = dll
SHAREDLIBSUFFIX = $(SHAREDLIBSUFFIXNOVER)
else ifeq (darwin,$(findstring darwin,@host@))
SHAREDLIB_DIR = $(libdir)
SHAREDLIB_LDFLAGS = -dynamiclib -twolevel_namespace -undefined dynamic_lookup \
SHAREDLIB_LDFLAGS = -dynamiclib -twolevel_namespace \
-fno-common -headerpad_max_install_names -install_name $(libdir)/$@
SHAREDLIBSUFFIXNOVER = dylib
SHAREDLIBSUFFIX = $(SHAREDLIBVERSION).$(SHAREDLIBSUFFIXNOVER)
......@@ -151,7 +176,7 @@ libsrtp2.so: $(srtpobj) $(cryptobj)
-o libsrtp2.so $^ $(LDFLAGS)
# test applications
ifneq (1, $(USE_OPENSSL))
ifneq (1, $(USE_EXTERNAL_CRYPTO))
AES_CALC = crypto/test/aes_calc$(EXE)
endif
......@@ -241,6 +266,11 @@ tags:
libsrtp2doc:
$(MAKE) -C doc
# fuzzer
srtp-fuzzer: libsrtp2.a
$(MAKE) -C fuzzer
.PHONY: clean superclean distclean install
install:
......@@ -279,6 +309,7 @@ clean:
rm -rf *.pict *.jpg *.dat
rm -rf freed allocated tmp
$(MAKE) -C doc clean
$(MAKE) -C fuzzer clean
superclean: clean
rm -rf crypto/include/config.h config.log config.cache config.status \
......
[![Build Status](https://travis-ci.org/cisco/libsrtp.svg?branch=master)](https://travis-ci.org/cisco/libsrtp)
[![Coverity Scan Build Status](https://scan.coverity.com/projects/14274/badge.svg)](https://scan.coverity.com/projects/cisco-libsrtp)
[![OSS-Fuzz Status](https://oss-fuzz-build-logs.storage.googleapis.com/badges/systemd.svg)](https://oss-fuzz-build-logs.storage.googleapis.com/index.html#libsrtp)
<a name="introduction-to-libsrtp"></a>
# Introduction to libSRTP
......@@ -31,7 +32,7 @@ because it does its work behind the scenes.
--------------------------------------------------------------------------------
<a name="contact"></a>
<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
......@@ -46,7 +47,7 @@ because it does its work behind the scenes.
## Contents
- [Introduction to libSRTP](#introduction-to-libsrtp)
- [Contact Us](#contact)
- [Contact Us](#contact-us)
- [Contents](#contents)
- [License and Disclaimer](#license-and-disclaimer)
- [libSRTP Overview](#libsrtp-overview)
......@@ -55,6 +56,7 @@ because it does its work behind the scenes.
- [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)
......@@ -137,16 +139,16 @@ 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` strucutre contains two `crypto_policy_t` structures
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 `crypto_policy_t` structures
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
`crypto_policy_t` structure can be initialized by using either the
`crypto_policy_set_rtp_default()` or `crypto_policy_set_rtcp_default()`
`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.
......@@ -313,6 +315,26 @@ brew install automake pkgconfig
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="applications"></a>
......@@ -425,8 +447,8 @@ srtp_init();
memset(&policy, 0x0, sizeof(srtp_policy_t));
// set policy to describe a policy for an SRTP stream
crypto_policy_set_rtp_default(&policy.rtp);
crypto_policy_set_rtcp_default(&policy.rtcp);
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;
......
......@@ -18,6 +18,9 @@
/* Define to redirect logging to stdout. */
#undef ERR_REPORTING_STDOUT
/* Define this to use AES-GCM. */
#undef GCM
/* Define to 1 if you have the <arpa/inet.h> header file. */
#undef HAVE_ARPA_INET_H
......@@ -42,6 +45,12 @@
/* Define to 1 if you have the `dl' library (-ldl). */
#undef HAVE_LIBDL
/* Define to 1 if you have the `nspr4' library (-lnspr4). */
#undef HAVE_LIBNSPR4
/* Define to 1 if you have the `nss3' library (-lnss3). */
#undef HAVE_LIBNSS3
/* Define to 1 if you have the `socket' library (-lsocket). */
#undef HAVE_LIBSOCKET
......@@ -57,6 +66,9 @@
/* Define to 1 if you have the <netinet/in.h> header file. */
#undef HAVE_NETINET_IN_H
/* Define to 1 if you have the <nss.h> header file. */
#undef HAVE_NSS_H
/* Define to 1 if you have the `winpcap' library (-lwpcap) */
#undef HAVE_PCAP
......@@ -120,9 +132,15 @@
/* Define to use X86 inlined assembly code */
#undef HAVE_X86
/* Define this to use NSS crypto. */
#undef NSS
/* Define this to use OpenSSL crypto. */
#undef OPENSSL
/* Define this if OPENSSL_cleanse is broken. */
#undef OPENSSL_CLEANSE_BROKEN
/* Define this to use OpenSSL KDF for SRTP. */
#undef OPENSSL_KDF
......
/* clang-format off */
/* Define to the full name and version of this package. */
#cmakedefine PACKAGE_VERSION "@PACKAGE_VERSION@"
/* Define to the version of this package. */
#cmakedefine PACKAGE_STRING "@PACKAGE_STRING@"
/* Define to enabled debug logging for all mudules. */
#cmakedefine ENABLE_DEBUG_LOGGING 1
/* Logging statments will be writen to this file. */
#cmakedefine ERR_REPORTING_FILE "@ERR_REPORTING_FILE@"
/* Define to redirect logging to stdout. */
#cmakedefine ERR_REPORTING_STDOUT 1
/* Define this to use OpenSSL crypto. */
#cmakedefine OPENSSL 1
/* Define this to use AES-GCM. */
#cmakedefine 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 */
#cmakedefine HAVE_X86 1
/* Define WORDS_BIGENDIAN to 1 if your processor stores words with the most
significant byte first (like Motorola and SPARC, unlike Intel). */
#cmakedefine WORDS_BIGENDIAN 1
/* Define to 1 if you have the <arpa/inet.h> header file. */
#cmakedefine HAVE_ARPA_INET_H 1
/* Define to 1 if you have the <byteswap.h> header file. */
#cmakedefine HAVE_BYTESWAP_H 1
/* Define to 1 if you have the <inttypes.h> header file. */
#cmakedefine HAVE_INTTYPES_H 1
/* Define to 1 if you have the <machine/types.h> header file. */
#cmakedefine HAVE_MACHINE_TYPES_H 1
/* Define to 1 if you have the <netinet/in.h> header file. */
#cmakedefine HAVE_NETINET_IN_H 1
/* Define to 1 if you have the <stdint.h> header file. */
#cmakedefine HAVE_STDINT_H 1
/* Define to 1 if you have the <stdlib.h> header file. */
#cmakedefine HAVE_STDLIB_H 1
/* Define to 1 if you have the <sys/int_types.h> header file. */
#cmakedefine HAVE_SYS_INT_TYPES_H 1
/* Define to 1 if you have the <sys/socket.h> header file. */
#cmakedefine HAVE_SYS_SOCKET_H 1
/* Define to 1 if you have the <sys/types.h> header file. */
#cmakedefine HAVE_SYS_TYPES_H 1
/* Define to 1 if you have the <unistd.h> header file. */
#cmakedefine HAVE_UNISTD_H 1
/* Define to 1 if you have the <windows.h> header file. */
#cmakedefine HAVE_WINDOWS_H 1
/* Define to 1 if you have the <winsock2.h> header file. */
#cmakedefine HAVE_WINSOCK2_H 1
/* Define to 1 if you have the `inet_aton' function. */
#cmakedefine HAVE_INET_ATON 1
/* Define to 1 if you have the `sigaction' function. */
#cmakedefine HAVE_SIGACTION 1
/* Define to 1 if you have the `usleep' function. */
#cmakedefine HAVE_USLEEP 1
/* Define to 1 if the system has the type `uint8_t'. */
#cmakedefine HAVE_UINT8_T 1
/* Define to 1 if the system has the type `uint16_t'. */
#cmakedefine HAVE_UINT16_T 1
/* Define to 1 if the system has the type `uint32_t'. */
#cmakedefine HAVE_UINT32_T 1
/* Define to 1 if the system has the type `uint64_t'. */
#cmakedefine HAVE_UINT64_T 1
/* Define to 1 if the system has the type `int32_t'. */
#cmakedefine HAVE_INT32_T 1
/* The size of `unsigned long', as computed by sizeof. */
@SIZEOF_UNSIGNED_LONG_CODE@
/* The size of `unsigned long long', as computed by sizeof. */
@SIZEOF_UNSIGNED_LONG_LONG_CODE@
/* Define inline to what is supported by compiler */
#cmakedefine HAVE_INLINE 1
#cmakedefine HAVE___INLINE 1
#ifndef HAVE_INLINE
#ifdef HAVE___INLINE
#define inline __inline
#else
#define inline
#endif
#endif
dnl Process this file with autoconf to produce a configure script.
AC_INIT([libsrtp2], [2.2.0], [https://github.com/cisco/libsrtp/issues])
AC_INIT([libsrtp2], [2.3.0], [https://github.com/cisco/libsrtp/issues])
dnl Must come before AC_PROG_CC
EMPTY_CFLAGS="no"
......@@ -11,6 +11,7 @@ fi
dnl Checks for programs.
AC_PROG_CC
AC_PROG_CPP
AC_PROG_CXX
AC_ARG_VAR(
[EXTRA_CFLAGS],
[C compiler flags appended to the regular C compiler flags instead of overriding them])
......@@ -208,6 +209,13 @@ AC_ARG_ENABLE([openssl],
[AS_HELP_STRING([--enable-openssl], [compile in OpenSSL crypto engine])],
[], [enable_openssl=no])
AC_MSG_RESULT([$enable_openssl])
AC_MSG_CHECKING([whether to leverage NSS crypto])
AC_ARG_ENABLE([nss],
[AS_HELP_STRING([--enable-nss], [compile in NSS crypto engine])],
[], [enable_nss=no])
AC_MSG_RESULT([$enable_nss])
if test "$enable_openssl" = "yes"; then
AC_MSG_CHECKING([for user specified OpenSSL directory])
AC_ARG_WITH([openssl-dir],
......@@ -249,10 +257,36 @@ if test "$enable_openssl" = "yes"; then
AC_SEARCH_LIBS([EVP_aes_128_gcm], [crypto],
[], [AC_MSG_FAILURE([can't find openssl >= 1.0.1 crypto lib])])
AC_DEFINE([GCM], [1], [Define this to use AES-GCM.])
AC_DEFINE([OPENSSL], [1], [Define this to use OpenSSL crypto.])
AES_ICM_OBJS="crypto/cipher/aes_icm_ossl.o crypto/cipher/aes_gcm_ossl.o"
HMAC_OBJS=crypto/hash/hmac_ossl.o
AC_SUBST([USE_OPENSSL], [1])
AC_SUBST([USE_EXTERNAL_CRYPTO], [1])
AC_MSG_CHECKING([if OPENSSL_cleanse is broken])
AC_RUN_IFELSE([AC_LANG_PROGRAM([
#include <stdio.h>
#include <openssl/crypto.h>
], [
#define BUFFER_SIZE (16)
char buffer[[BUFFER_SIZE]];
int i;
for (i = 0; i < BUFFER_SIZE; i++) {
buffer[[i]] = i & 0xff;
}
OPENSSL_cleanse(buffer, BUFFER_SIZE);
for (i = 0; i < BUFFER_SIZE; i++) {
if (buffer[[i]]) {
printf("Buffer contents not zero at position %d (is %d)\n", i,
buffer[[i]]);
return 1;
}
}
])], [openssl_cleanse_broken=no], [
openssl_cleanse_broken=yes
AC_DEFINE([OPENSSL_CLEANSE_BROKEN], [1], [Define this if OPENSSL_cleanse is broken.])
])
AC_MSG_RESULT([$openssl_cleanse_broken])
AC_MSG_CHECKING([whether to leverage OpenSSL KDF algorithm])
AC_ARG_ENABLE([openssl-kdf],
......@@ -264,6 +298,61 @@ if test "$enable_openssl" = "yes"; then
[], [AC_MSG_FAILURE([can't find openssl KDF lib])])
AC_DEFINE([OPENSSL_KDF], [1], [Define this to use OpenSSL KDF for SRTP.])
fi
elif test "$enable_nss" = "yes"; then
AC_MSG_CHECKING([for user specified NSS directory])
AC_ARG_WITH([nss-dir],
[AS_HELP_STRING([--with-nss-dir], [Location of NSS installation])],
[if test "x$PKG_CONFIG" != "x" && test -f $with_nss_dir/lib/pkgconfig/nss.pc; then
if test "x$PKG_CONFIG_PATH" = "x"; then
export PKG_CONFIG_PATH="$with_nss_dir/lib/pkgconfig"
else
export PKG_CONFIG_PATH="$with_nss_dir/lib/pkgconfig:$PKG_CONFIG_PATH"
fi
AC_MSG_RESULT([$with_nss_dir])
elif test -d $with_nss_dir/lib; then
CFLAGS="$CFLAGS -I$with_nss_dir/include"
CFLAGS="$CFLAGS -I$with_nss_dir/../public/nss"
if test "x$LDFLAGS" = "x"; then
LDFLAGS="-L$with_nss_dir/lib"
else
LDFLAGS="$LDFLAGS -L$with_nss_dir/lib"
fi
nss_skip_pkg_config=yes
AC_MSG_RESULT([$with_nss_dir])
else
AC_MSG_RESULT([invalid])
AC_MSG_FAILURE([Invalid NSS location: $with_nss_dir])
fi
AC_SUBST([CRYPTO_LIBDIR], [$with_nss_dir/lib])],
[AC_MSG_RESULT([no])])
if test "x$PKG_CONFIG" != "x" && test "$nss_skip_pkg_config" != "yes"; then
PKG_CHECK_MODULES([nss], [nss],
[CFLAGS="$CFLAGS $nss_CFLAGS"
LIBS="$nss_LIBS $LIBS"])
else
AC_CHECK_HEADERS(
[nss.h],
[], [AC_MSG_FAILURE([can't find useable NSS headers])],
[AC_INCLUDES_DEFAULT])
AC_CHECK_LIB(
[nspr4], [PR_GetError],
[], [AC_MSG_WARN([can't find libnspr4])])
AC_CHECK_LIB(
[nss3], [NSS_NoDB_Init],
[], [AC_MSG_FAILURE([can't find useable libnss3])])
fi
AC_DEFINE([GCM], [1], [Define this to use AES-GCM.])
AC_DEFINE([NSS], [1], [Define this to use NSS crypto.])
AES_ICM_OBJS="crypto/cipher/aes_icm_nss.o crypto/cipher/aes_gcm_nss.o"
# TODO(RLB): Use NSS for HMAC
HMAC_OBJS="crypto/hash/hmac.o crypto/hash/sha1.o"
# TODO(RLB): Use NSS for KDF
AC_SUBST([USE_EXTERNAL_CRYPTO], [1])
else
AES_ICM_OBJS="crypto/cipher/aes_icm.o crypto/cipher/aes.o"
HMAC_OBJS="crypto/hash/hmac.o crypto/hash/sha1.o"
......@@ -274,15 +363,18 @@ AC_SUBST([HMAC_OBJS])
dnl Checking for PCAP
PCAP_LIB=""
AC_CHECK_LIB([pcap], [pcap_create],
AC_ARG_ENABLE([pcap], AS_HELP_STRING([--disable-pcap], [Build without `pcap' library (-lpcap)]))
AS_IF([test "x$enable_pcap" != "xno"], [
AC_CHECK_LIB([pcap], [pcap_create],
[PCAP_LIB="-lpcap"
AC_DEFINE([HAVE_PCAP], [1], [Define to 1 if you have the `pcap' library (-lpcap)])
AC_SUBST([HAVE_PCAP], [1])])
AC_CHECK_LIB([wpcap], [pcap_create],
AC_CHECK_LIB([wpcap], [pcap_create],
[PCAP_LIB="-lwpcap"
AC_DEFINE([HAVE_PCAP], [1], [Define to 1 if you have the `winpcap' library (-lwpcap)])
AC_SUBST([HAVE_PCAP], [1])])
])
AC_SUBST([PCAP_LIB])
AC_MSG_CHECKING([whether to redirect logging to stdout])
......@@ -322,7 +414,7 @@ AS_IF([test "x$EXTRA_CFLAGS" != "x"],
AC_CONFIG_HEADER([crypto/include/config.h:config_in.h])
AC_CONFIG_FILES([Makefile crypto/Makefile doc/Makefile libsrtp2.pc])
AC_CONFIG_FILES([Makefile crypto/Makefile doc/Makefile fuzzer/Makefile libsrtp2.pc])
AC_OUTPUT
# This is needed when building outside the source dir.
......
......@@ -17,15 +17,34 @@ LIBS = @LIBS@
LDFLAGS = @LDFLAGS@ -L. -L..
COMPILE = $(CC) $(DEFS) $(INCDIR) $(CPPFLAGS) $(CFLAGS)
CRYPTOLIB = -lsrtp2
CRYPTO_LIBDIR = @CRYPTO_LIBDIR@
RANLIB = @RANLIB@
# Specify how tests should find shared libraries on macOS and Linux
#
# macOS purges DYLD_LIBRARY_PATH when spawning subprocesses, so it's
# not possible to pass this in from the outside; we have to specify
# it for any subprocesses we call. No support for dynamic linked
# tests on Windows.
ifneq ($(strip $(CRYPTO_LIBDIR)),)
ifneq ($(OS),Windows_NT)
UNAME_S = $(shell uname -s)
ifeq ($(UNAME_S),Linux)
FIND_LIBRARIES = LD_LIBRARY_PATH=$(CRYPTO_LIBDIR)
endif
ifeq ($(UNAME_S),Darwin)
FIND_LIBRARIES = DYLD_LIBRARY_PATH=$(CRYPTO_LIBDIR)
endif
endif
endif
# EXE defines the suffix on executables - it's .exe for cygwin, and
# null on linux, bsd, and OS X and other OSes. we define this so that
# `make clean` will work on the cygwin platform
EXE = @EXE@
# Random source.
USE_OPENSSL = @USE_OPENSSL@
USE_EXTERNAL_CRYPTO = @USE_EXTERNAL_CRYPTO@
ifdef ARCH
DEFS += -D$(ARCH)=1
......@@ -40,7 +59,7 @@ endif
dummy : all runtest
# test applications
ifneq (1, $(USE_OPENSSL))
ifneq (1, $(USE_EXTERNAL_CRYPTO))
AES_CALC = test/aes_calc$(EXE)
endif
......@@ -62,17 +81,17 @@ c256=8ea2b7ca516745bfeafc49904b496089
runtest: $(testapp)
test/env$(EXE) # print out information on the build environment
$(FIND_LIBRARIES) test/env$(EXE) # print out information on the build environment
@echo "running crypto test applications..."
ifneq (1, $(USE_OPENSSL))
test `test/aes_calc $(k128) $(p128)` = $(c128)
test `test/aes_calc $(k256) $(p256)` = $(c256)
ifneq (1, $(USE_EXTERNAL_CRYPTO))
$(FIND_LIBRARIES) test `test/aes_calc $(k128) $(p128)` = $(c128)
$(FIND_LIBRARIES) test `test/aes_calc $(k256) $(p256)` = $(c256)
endif
test/cipher_driver$(EXE) -v >/dev/null
test/datatypes_driver$(EXE) -v >/dev/null
test/stat_driver$(EXE) >/dev/null
test/sha1_driver$(EXE) -v >/dev/null
test/kernel_driver$(EXE) -v >/dev/null
$(FIND_LIBRARIES) test/cipher_driver$(EXE) -v >/dev/null
$(FIND_LIBRARIES) test/datatypes_driver$(EXE) -v >/dev/null
$(FIND_LIBRARIES) test/stat_driver$(EXE) >/dev/null
$(FIND_LIBRARIES) test/sha1_driver$(EXE) -v >/dev/null
$(FIND_LIBRARIES) test/kernel_driver$(EXE) -v >/dev/null
@echo "crypto test applications passed."
......
......@@ -49,8 +49,7 @@
#endif
#include <openssl/evp.h>
#include "aes_icm_ossl.h"
#include "aes_gcm_ossl.h"
#include "aes_gcm.h"
#include "alloc.h"
#include "err.h" /* for srtp_debug */
#include "crypto_types.h"
......@@ -124,13 +123,13 @@ static srtp_err_status_t srtp_aes_gcm_openssl_alloc(srtp_cipher_t **c,
/* setup cipher attributes */
switch (key_len) {
case SRTP_AES_GCM_128_KEY_LEN_WSALT:
(*c)->type = &srtp_aes_gcm_128_openssl;
(*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_openssl;
(*c)->type = &srtp_aes_gcm_256;
(*c)->algorithm = SRTP_AES_GCM_256;
gcm->key_size = SRTP_AES_256_KEY_LEN;
gcm->tag_len = tlen;
......@@ -193,6 +192,7 @@ static srtp_err_status_t srtp_aes_gcm_openssl_context_init(void *cv,
break;
}
EVP_CIPHER_CTX_cleanup(c->ctx);
if (!EVP_CipherInit_ex(c->ctx, evp, NULL, key, NULL, 0)) {
return (srtp_err_status_init_fail);
}
......@@ -218,7 +218,7 @@ static srtp_err_status_t srtp_aes_gcm_openssl_set_iv(
c->dir = direction;
debug_print(srtp_mod_aes_gcm, "setting iv: %s",
v128_hex_string((v128_t *)iv));
srtp_octet_string_hex_string(iv, 12));
if (!EVP_CIPHER_CTX_ctrl(c->ctx, EVP_CTRL_GCM_SET_IVLEN, 12, 0)) {
return (srtp_err_status_init_fail);
......@@ -247,6 +247,9 @@ static srtp_err_status_t srtp_aes_gcm_openssl_set_aad(void *cv,
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
int rv;
debug_print(srtp_mod_aes_gcm, "setting AAD: %s",
srtp_octet_string_hex_string(aad, aad_len));
/*
* Set dummy tag, OpenSSL requires the Tag to be set before
* processing AAD
......@@ -548,7 +551,7 @@ static const srtp_cipher_test_case_t srtp_aes_gcm_test_case_1 = {
/*
* This is the vector function table for this crypto engine.
*/
const srtp_cipher_type_t srtp_aes_gcm_128_openssl = {
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,
......@@ -565,7 +568,7 @@ const srtp_cipher_type_t srtp_aes_gcm_128_openssl = {
/*
* This is the vector function table for this crypto engine.
*/
const srtp_cipher_type_t srtp_aes_gcm_256_openssl = {
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,
......
......@@ -53,7 +53,7 @@
#endif
#include <openssl/evp.h>
#include "aes_icm_ossl.h"
#include "aes_icm_ext.h"
#include "crypto_types.h"
#include "err.h" /* for srtp_debug */
#include "alloc.h"
......@@ -248,6 +248,7 @@ static srtp_err_status_t srtp_aes_icm_openssl_context_init(void *cv,
break;
}
EVP_CIPHER_CTX_cleanup(c->ctx);
if (!EVP_EncryptInit_ex(c->ctx, evp, NULL, key, NULL)) {
return srtp_err_status_fail;
} else {
......@@ -308,7 +309,7 @@ static srtp_err_status_t srtp_aes_icm_openssl_encrypt(void *cv,
}
*enc_len = len;
if (!EVP_EncryptFinal_ex(c->ctx, buf, &len)) {
if (!EVP_EncryptFinal_ex(c->ctx, buf + len, &len)) {
return srtp_err_status_cipher_fail;
}
*enc_len += len;
......
......@@ -49,6 +49,7 @@
#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() */
......@@ -165,24 +166,11 @@ int srtp_cipher_get_key_length(const srtp_cipher_t *c)
}
/*
* A trivial platform independent random source. The random
* data is used for some of the cipher self-tests.
* A trivial platform independent random source.
* For use in test only.
*/
static srtp_err_status_t srtp_cipher_rand(void *dest, uint32_t len)
void srtp_cipher_rand_for_tests(void *dest, uint32_t len)
{
#if defined(HAVE_RAND_S)
uint8_t *dst = (uint8_t *)dest;
while (len) {
unsigned int val;
errno_t err = rand_s(&val);
if (err != 0)
return srtp_err_status_fail;
*dst++ = val & 0xff;
len--;
}
#else
/* Generic C-library (rand()) version */
/* This is a random source of last resort */
uint8_t *dst = (uint8_t *)dest;
......@@ -194,8 +182,17 @@ static srtp_err_status_t srtp_cipher_rand(void *dest, uint32_t len)
*dst++ = val & 0xff;
len--;
}
#endif
return srtp_err_status_ok;
}
/*
* 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(&r, sizeof(r));
return r;
}
#define SELF_TEST_BUF_OCTETS 128
......@@ -246,7 +243,7 @@ srtp_err_status_t srtp_cipher_type_test(
/*
* test the encrypt function
*/
debug_print(srtp_mod_cipher, "testing encryption", NULL);
debug_print0(srtp_mod_cipher, "testing encryption");
/* initialize cipher */
status = srtp_cipher_init(c, test_case->key);
......@@ -350,7 +347,7 @@ srtp_err_status_t srtp_cipher_type_test(
/*
* test the decrypt function
*/
debug_print(srtp_mod_cipher, "testing decryption", NULL);
debug_print0(srtp_mod_cipher, "testing decryption");
/* re-initialize cipher for decryption */
status = srtp_cipher_init(c, test_case->key);
......@@ -465,13 +462,9 @@ srtp_err_status_t srtp_cipher_type_test(
uint8_t iv[MAX_KEY_LEN];
/* choose a length at random (leaving room for IV and padding) */
length = rand() % (SELF_TEST_BUF_OCTETS - 64);
length = srtp_cipher_rand_u32_for_tests() % (SELF_TEST_BUF_OCTETS - 64);
debug_print(srtp_mod_cipher, "random plaintext length %d\n", length);
status = srtp_cipher_rand(buffer, length);
if (status) {
srtp_cipher_dealloc(c);
return status;
}
srtp_cipher_rand_for_tests(buffer, length);
debug_print(srtp_mod_cipher, "plaintext: %s",
srtp_octet_string_hex_string(buffer, length));
......@@ -486,18 +479,10 @@ srtp_err_status_t srtp_cipher_type_test(
srtp_cipher_dealloc(c);
return srtp_err_status_cant_check;
}
status = srtp_cipher_rand(key, test_case->key_length_octets);
if (status) {
srtp_cipher_dealloc(c);
return status;
}
srtp_cipher_rand_for_tests(key, test_case->key_length_octets);
/* chose a random initialization vector */
status = srtp_cipher_rand(iv, MAX_KEY_LEN);
if (status) {
srtp_cipher_dealloc(c);
return status;
}
srtp_cipher_rand_for_tests(iv, MAX_KEY_LEN);
/* initialize cipher */
status = srtp_cipher_init(c, key);
......
......@@ -97,7 +97,7 @@ 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; */
debug_print(srtp_mod_cipher, "initializing null cipher", NULL);
debug_print0(srtp_mod_cipher, "initializing null cipher");
return srtp_err_status_ok;
}
......
......@@ -261,14 +261,13 @@ void srtp_sha1_update(srtp_sha1_ctx_t *ctx,
/* process a whole block */
debug_print(srtp_mod_sha1, "(update) running srtp_sha1_core()",
NULL);
debug_print0(srtp_mod_sha1, "(update) running srtp_sha1_core()");
srtp_sha1_core(ctx->M, ctx->H);
} else {
debug_print(srtp_mod_sha1, "(update) not running srtp_sha1_core()",
NULL);
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++) {
......@@ -391,11 +390,10 @@ void srtp_sha1_final(srtp_sha1_ctx_t *ctx, uint32_t *output)
ctx->H[4] += E;
}
debug_print(srtp_mod_sha1, "(final) running srtp_sha1_core()", NULL);
debug_print0(srtp_mod_sha1, "(final) running srtp_sha1_core()");
if (ctx->octets_in_buffer >= 56) {
debug_print(srtp_mod_sha1, "(final) running srtp_sha1_core() again",
NULL);
debug_print0(srtp_mod_sha1, "(final) running srtp_sha1_core() again");
/* we need to do one final run of the compression algo */
......
/*
* aes_gcm_ossl.h
* aes_gcm.h
*
* Header for AES Galois Counter Mode.
*
......@@ -43,12 +43,15 @@
*
*/
#ifndef AES_GCM_OSSL_H
#define AES_GCM_OSSL_H
#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>
......@@ -59,4 +62,28 @@ typedef struct {
srtp_cipher_direction_t dir;
} srtp_aes_gcm_ctx_t;
#endif /* AES_GCM_OSSL_H */
#endif /* OPENSSL */
#ifdef NSS
#include <nss.h>
#include <pk11pub.h>
#define MAX_AD_SIZE 2048
typedef struct {
int key_size;
int tag_size;
srtp_cipher_direction_t dir;
NSSInitContext *nss;
PK11SymKey *key;
uint8_t iv[12];
uint8_t aad[MAX_AD_SIZE];
int aad_size;
CK_GCM_PARAMS params;
uint8_t tag[16];
} srtp_aes_gcm_ctx_t;
#endif /* NSS */
#endif /* AES_GCM_H */
......@@ -48,6 +48,9 @@
#include "cipher.h"
#include "datatypes.h"
#ifdef OPENSSL
#include <openssl/evp.h>
#include <openssl/aes.h>
......@@ -58,4 +61,23 @@ typedef struct {
EVP_CIPHER_CTX *ctx;
} srtp_aes_icm_ctx_t;
#endif /* OPENSSL */
#ifdef NSS
#include <nss.h>
#include <pk11pub.h>
typedef struct {
v128_t counter;
v128_t offset;
int key_size;
uint8_t iv[16];
NSSInitContext *nss;
PK11SymKey *key;
PK11Context *ctx;
} srtp_aes_icm_ctx_t;
#endif /* NSS */
#endif /* AES_ICM_H */
/*
*
* 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(void *dest, uint32_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 */
......@@ -47,10 +47,10 @@
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 OPENSSL
#ifdef GCM
extern const srtp_cipher_type_t srtp_aes_icm_192;
extern const srtp_cipher_type_t srtp_aes_gcm_128_openssl;
extern const srtp_cipher_type_t srtp_aes_gcm_256_openssl;
extern const srtp_cipher_type_t srtp_aes_gcm_128;
extern const srtp_cipher_type_t srtp_aes_gcm_256;
#endif
/*
......@@ -74,6 +74,9 @@ extern srtp_debug_module_t srtp_mod_aes_icm;
#ifdef OPENSSL
extern srtp_debug_module_t srtp_mod_aes_gcm;
#endif
#ifdef NSS
extern srtp_debug_module_t srtp_mod_aes_gcm;
#endif
/* debug modules for auth types */
extern srtp_debug_module_t srtp_mod_hmac;
......
......@@ -241,13 +241,13 @@ void v128_set_bit_to(v128_t *x, int i, int y);
#endif /* DATATYPES_USE_MACROS */
/*
* octet_string_is_eq(a, b, len) returns 1 if the length len strings a
* and b are not equal. It returns 0 otherwise. The running time of the
* srtp_octet_string_is_eq(a, b, len) returns 1 if the length len strings
* a and b are not equal. It returns 0 otherwise. The running time of the
* comparison depends only on len, making this safe to use for (e.g.)
* verifying authentication tags.
*/
int octet_string_is_eq(uint8_t *a, uint8_t *b, int len);
int srtp_octet_string_is_eq(uint8_t *a, uint8_t *b, int len);
/*
* A portable way to zero out memory as recommended by
......
......@@ -109,6 +109,8 @@ typedef struct {
#ifdef ENABLE_DEBUG_LOGGING
#define debug_print0(mod, format) \
srtp_err_report(srtp_err_level_debug, ("%s: " format "\n"), mod.name)
#define debug_print(mod, format, arg) \
srtp_err_report(srtp_err_level_debug, ("%s: " format "\n"), mod.name, arg)
#define debug_print2(mod, format, arg1, arg2) \
......@@ -117,6 +119,9 @@ typedef struct {
#else
#define debug_print0(mod, format) \
if (mod.on) \
srtp_err_report(srtp_err_level_debug, ("%s: " format "\n"), mod.name)
#define debug_print(mod, format, arg) \
if (mod.on) \
srtp_err_report(srtp_err_level_debug, ("%s: " format "\n"), mod.name, arg)
......
......@@ -80,7 +80,7 @@ void *srtp_crypto_alloc(size_t size)
if (ptr) {
debug_print(srtp_mod_alloc, "(location: %p) allocated", ptr);
} else {
debug_print(srtp_mod_alloc, "allocation failed (asked for %d bytes)\n",
debug_print(srtp_mod_alloc, "allocation failed (asked for %zu bytes)\n",
size);
}
......
......@@ -130,18 +130,18 @@ srtp_err_status_t srtp_crypto_kernel_init()
if (status) {
return status;
}
#ifdef OPENSSL
#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_openssl,
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_openssl,
status = srtp_crypto_kernel_load_cipher_type(&srtp_aes_gcm_256,
SRTP_AES_GCM_256);
if (status) {
return status;
......
......@@ -80,6 +80,7 @@ srtp_err_status_t srtp_install_err_report_handler(
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);
......@@ -88,7 +89,6 @@ void srtp_err_report(srtp_err_reporting_level_t level, const char *format, ...)
}
if (srtp_err_report_handler != NULL) {
va_start(args, format);
char msg[512];
if (vsnprintf(msg, sizeof(msg), format, args) > 0) {
/* strip trailing \n, callback should not have one */
size_t l = strlen(msg);
......
......@@ -410,7 +410,7 @@ void bitvector_left_shift(bitvector_t *x, int shift)
x->word[i] = 0;
}
int octet_string_is_eq(uint8_t *a, uint8_t *b, int len)
int srtp_octet_string_is_eq(uint8_t *a, uint8_t *b, int len)
{
uint8_t *end = b + len;
uint8_t accumulator = 0;
......@@ -436,7 +436,7 @@ void srtp_cleanse(void *s, size_t len)
void octet_string_set_to_zero(void *s, size_t len)
{
#ifdef OPENSSL
#if defined(OPENSSL) && !defined(OPENSSL_CLEANSE_BROKEN)
OPENSSL_cleanse(s, len);
#else
srtp_cleanse(s, len);
......
......@@ -196,7 +196,7 @@ srtp_err_status_t stat_test_runs(uint8_t *data)
}
if (srtp_mod_stat.on) {
debug_print(srtp_mod_stat, "runs test", NULL);
debug_print0(srtp_mod_stat, "runs test");
for (i = 0; i < 6; i++)
debug_print(srtp_mod_stat, " runs[]: %d", runs[i]);
for (i = 0; i < 6; i++)
......
......@@ -49,10 +49,13 @@
#endif
#include "ut_sim.h"
#include "cipher_priv.h"
int ut_compar(const void *a, const void *b)
{
return rand() > (RAND_MAX / 2) ? -1 : 1;
uint8_t r;
srtp_cipher_rand_for_tests(&r, sizeof(r));
return r > (UINT8_MAX / 2) ? -1 : 1;
}
void ut_init(ut_connection *utc)
......
......@@ -48,12 +48,12 @@
#endif
#include <stdio.h> /* for printf() */
#include <stdlib.h> /* for rand() */
#include "getopt_s.h"
#include "cipher.h"
#ifdef OPENSSL
#include "aes_icm_ossl.h"
#include "aes_gcm_ossl.h"
#include "cipher_priv.h"
#ifdef GCM
#include "aes_icm_ext.h"
#include "aes_gcm.h"
#else
#include "aes_icm.h"
#endif
......@@ -118,10 +118,10 @@ void check_status(srtp_err_status_t s)
extern srtp_cipher_type_t srtp_null_cipher;
extern srtp_cipher_type_t srtp_aes_icm_128;
extern srtp_cipher_type_t srtp_aes_icm_256;
#ifdef OPENSSL
#ifdef GCM
extern srtp_cipher_type_t srtp_aes_icm_192;
extern srtp_cipher_type_t srtp_aes_gcm_128_openssl;
extern srtp_cipher_type_t srtp_aes_gcm_256_openssl;
extern srtp_cipher_type_t srtp_aes_gcm_128;
extern srtp_cipher_type_t srtp_aes_gcm_256;
#endif
int main(int argc, char *argv[])
......@@ -187,21 +187,19 @@ int main(int argc, char *argv[])
cipher_driver_test_array_throughput(
&srtp_aes_icm_256, SRTP_AES_ICM_256_KEY_LEN_WSALT, num_cipher);
#ifdef OPENSSL
#ifdef GCM
for (num_cipher = 1; num_cipher < max_num_cipher; num_cipher *= 8)
cipher_driver_test_array_throughput(
&srtp_aes_icm_192, SRTP_AES_ICM_192_KEY_LEN_WSALT, num_cipher);
for (num_cipher = 1; num_cipher < max_num_cipher; num_cipher *= 8) {
cipher_driver_test_array_throughput(&srtp_aes_gcm_128_openssl,
SRTP_AES_GCM_128_KEY_LEN_WSALT,
num_cipher);
cipher_driver_test_array_throughput(
&srtp_aes_gcm_128, SRTP_AES_GCM_128_KEY_LEN_WSALT, num_cipher);
}
for (num_cipher = 1; num_cipher < max_num_cipher; num_cipher *= 8) {
cipher_driver_test_array_throughput(&srtp_aes_gcm_256_openssl,
SRTP_AES_GCM_256_KEY_LEN_WSALT,
num_cipher);
cipher_driver_test_array_throughput(
&srtp_aes_gcm_256, SRTP_AES_GCM_256_KEY_LEN_WSALT, num_cipher);
}
#endif
}
......@@ -210,10 +208,10 @@ int main(int argc, char *argv[])
cipher_driver_self_test(&srtp_null_cipher);
cipher_driver_self_test(&srtp_aes_icm_128);
cipher_driver_self_test(&srtp_aes_icm_256);
#ifdef OPENSSL
#ifdef GCM
cipher_driver_self_test(&srtp_aes_icm_192);
cipher_driver_self_test(&srtp_aes_gcm_128_openssl);
cipher_driver_self_test(&srtp_aes_gcm_256_openssl);
cipher_driver_self_test(&srtp_aes_gcm_128);
cipher_driver_self_test(&srtp_aes_gcm_256);
#endif
}
......@@ -277,9 +275,9 @@ int main(int argc, char *argv[])
status = srtp_cipher_dealloc(c);
check_status(status);
#ifdef OPENSSL
/* run the throughput test on the aes_gcm_128_openssl cipher */
status = srtp_cipher_type_alloc(&srtp_aes_gcm_128_openssl, &c,
#ifdef GCM
/* run the throughput test on the aes_gcm_128 cipher */
status = srtp_cipher_type_alloc(&srtp_aes_gcm_128, &c,
SRTP_AES_GCM_128_KEY_LEN_WSALT, 8);
if (status) {
fprintf(stderr, "error: can't allocate GCM 128 cipher\n");
......@@ -291,15 +289,13 @@ int main(int argc, char *argv[])
cipher_driver_test_throughput(c);
}
if (do_validation) {
status = cipher_driver_test_buffering(c);
check_status(status);
}
// GCM ciphers don't do buffering; they're "one shot"
status = srtp_cipher_dealloc(c);
check_status(status);
/* run the throughput test on the aes_gcm_256_openssl cipher */
status = srtp_cipher_type_alloc(&srtp_aes_gcm_256_openssl, &c,
/* run the throughput test on the aes_gcm_256 cipher */
status = srtp_cipher_type_alloc(&srtp_aes_gcm_256, &c,
SRTP_AES_GCM_256_KEY_LEN_WSALT, 16);
if (status) {
fprintf(stderr, "error: can't allocate GCM 256 cipher\n");
......@@ -311,10 +307,8 @@ int main(int argc, char *argv[])
cipher_driver_test_throughput(c);
}
if (do_validation) {
status = cipher_driver_test_buffering(c);
check_status(status);
}
// GCM ciphers don't do buffering; they're "one shot"
status = srtp_cipher_dealloc(c);
check_status(status);
#endif
......@@ -396,7 +390,7 @@ srtp_err_status_t cipher_driver_test_buffering(srtp_cipher_t *c)
end = buffer1 + buflen;
while (current < end) {
/* choose a short length */
len = rand() & 0x01f;
len = srtp_cipher_rand_u32_for_tests() & 0x01f;
/* make sure that len doesn't cause us to overreach the buffer */
if (current + len > end)
......@@ -479,9 +473,8 @@ srtp_err_status_t cipher_array_alloc_init(srtp_cipher_t ***ca,
return status;
/* generate random key and initialize cipher */
for (j = 0; j < klen; j++)
key[j] = (uint8_t)rand();
for (; j < klen_pad; j++)
srtp_cipher_rand_for_tests(key, klen);
for (j = klen; j < klen_pad; j++)
key[j] = 0;
status = srtp_cipher_init(*cipher_array, key);
if (status)
......@@ -535,7 +528,7 @@ uint64_t cipher_array_bits_per_second(srtp_cipher_t *cipher_array[],
v128_t nonce;
clock_t timer;
unsigned char *enc_buf;
int cipher_index = rand() % num_cipher;
int cipher_index = srtp_cipher_rand_u32_for_tests() % num_cipher;
/* Over-alloc, for NIST CBC padding */
enc_buf = srtp_crypto_alloc(octets_in_buffer + 17);
......
......@@ -60,6 +60,8 @@ void print_string(char *s);
void test_bswap(void);
void test_set_to_zero(void);
int main(void)
{
/*
......@@ -135,6 +137,7 @@ int main(void)
printf(" } \n");
test_bswap();
test_set_to_zero();
return 0;
}
......@@ -228,3 +231,26 @@ void test_bswap(void)
printf("bswapped octet string: %s\n",
octet_string_hex_string((uint8_t *)&y, 8));
}
void test_set_to_zero(void)
{
#define BUFFER_SIZE (16)
uint8_t buffer[BUFFER_SIZE];
size_t i;
for (i = 0; i < BUFFER_SIZE; i++) {
buffer[i] = i & 0xff;
}
printf("Buffer before: %s\n", octet_string_hex_string(buffer, BUFFER_SIZE));
octet_string_set_to_zero(buffer, BUFFER_SIZE);
printf("Buffer after: %s\n", octet_string_hex_string(buffer, BUFFER_SIZE));
for (i = 0; i < BUFFER_SIZE; i++) {
if (buffer[i]) {
fprintf(stderr,
"Buffer contents not zero at position %zu (is %d)\n", i,
buffer[i]);
abort();
}
}
#undef BUFFER_SIZE
}
......@@ -54,12 +54,7 @@
#include "srtp.h"
#include "cipher.h"
typedef struct {
void *state;
} random_source_t;
srtp_err_status_t random_source_alloc(void);
#include "cipher_priv.h"
void err_check(srtp_err_status_t s)
{
......@@ -76,9 +71,9 @@ int main(int argc, char *argv[])
int i, j;
extern srtp_cipher_type_t srtp_aes_icm_128;
extern srtp_cipher_type_t srtp_aes_icm_256;
#ifdef OPENSSL
extern srtp_cipher_type_t srtp_aes_gcm_128_openssl;
extern srtp_cipher_type_t srtp_aes_gcm_256_openssl;
#ifdef GCM
extern srtp_cipher_type_t srtp_aes_gcm_128;
extern srtp_cipher_type_t srtp_aes_gcm_256;
#endif
srtp_cipher_t *c;
/* clang-format off */
......@@ -107,8 +102,7 @@ int main(int argc, char *argv[])
printf("poker %d\n", stat_test_poker(buffer));
printf("runs %d\n", stat_test_runs(buffer));
for (i = 0; i < 2500; i++)
buffer[i] = rand();
srtp_cipher_rand_for_tests(buffer, 2500);
printf("running stat_tests on rand(), expecting success\n");
printf("monobit %d\n", stat_test_monobit(buffer));
printf("poker %d\n", stat_test_poker(buffer));
......@@ -180,14 +174,14 @@ int main(int argc, char *argv[])
}
}
#ifdef OPENSSL
#ifdef GCM
{
printf("running stat_tests on AES-128-GCM, expecting success\n");
/* set buffer to cipher output */
for (i = 0; i < 2500; i++) {
buffer[i] = 0;
}
err_check(srtp_cipher_type_alloc(&srtp_aes_gcm_128_openssl, &c,
err_check(srtp_cipher_type_alloc(&srtp_aes_gcm_128, &c,
SRTP_AES_GCM_128_KEY_LEN_WSALT, 8));
err_check(srtp_cipher_init(c, key));
err_check(
......@@ -219,7 +213,7 @@ int main(int argc, char *argv[])
for (i = 0; i < 2500; i++) {
buffer[i] = 0;
}
err_check(srtp_cipher_type_alloc(&srtp_aes_gcm_256_openssl, &c,
err_check(srtp_cipher_type_alloc(&srtp_aes_gcm_256, &c,
SRTP_AES_GCM_256_KEY_LEN_WSALT, 16));
err_check(srtp_cipher_init(c, key));
err_check(
......
# Makefile for libSRTP fuzzer
srcdir = @srcdir@
top_srcdir = @top_srcdir@
top_builddir = @top_builddir@
VPATH = @srcdir@
CC = @CC@
CXX = @CXX@
INCDIR = -Iinclude -I$(srcdir)/include -I$(top_srcdir)/include -I$(top_srcdir)/crypto/include/
DEFS = @DEFS@
CPPFLAGS= @CPPFLAGS@ -g
CXXFLAGS= @CXXFLAGS@ -g
CFLAGS = @CFLAGS@ -g
LIBS = @LIBS@
COMPILE = $(CC) $(DEFS) $(INCDIR) $(CPPFLAGS) $(CFLAGS)
COMPILECXX = $(CXX) $(DEFS) $(INCDIR) $(CPPFLAGS) $(CXXFLAGS)
CRYPTOLIB = -lsrtp2
.PHONY: clean
all : srtp-fuzzer
mt19937.o: mt19937.cpp
$(COMPILECXX) -c -std=c++11 mt19937.cpp -o mt19937.o
fuzzer.o: fuzzer.c fuzzer.h testmem.h
$(COMPILE) fuzzer.c -c -o fuzzer.o
testmem.o: testmem.c
$(COMPILE) -O0 testmem.c -c -o testmem.o
srtp-fuzzer: fuzzer.o mt19937.o testmem.o
$(COMPILECXX) -L. -L.. fuzzer.o mt19937.o testmem.o $(LIBFUZZER) $(CRYPTOLIB) $(LIBS) -o srtp-fuzzer
clean:
rm -rf srtp-fuzzer *.o
# libsrt fuzzer
By Guido Vranken <guidovranken@gmail.com> -- https://guidovranken.wordpress.com/
This is an advanced fuzzer for libSRTP (https://github.com/cisco/libsrtp). It implements several special techniques, described below, that are not often found in fuzzers or elsewhere. All are encouraged to transpose these ideas to their own fuzzers for the betterment of software security.
Feel free to contact me for business enquiries.
## Building
From the repository's root directory:
```sh
CC=clang CXX=clang++ CXXFLAGS="-fsanitize=fuzzer-no-link,address,undefined -g -O3" CFLAGS="-fsanitize=fuzzer-no-link,address,undefined -g -O3" LDFLAGS="-fsanitize=fuzzer-no-link,address,undefined" ./configure
LIBFUZZER="-fsanitize=fuzzer" make srtp-fuzzer
```
## Features
### Portable PRNG
```mt19937.c``` exports the C++11 Mersenne Twister implementaton. Hence, a modern C++ compiler is required to compile this file.
This approach has the following advantages:
- rand() is fickle -- its behavior eg. the sequence of numbers that it generates for a given seed, may differ across systems and libc's.
- C++11 mt19937 is portable, meaning that its behavior will be consistent across platforms. This is important to keep the fuzzing corpus portable.
- No need to implement a portable PRNG ourselves, or risk license incompatability by importing it from other projects.
### Size 0 allocations
To test whether allocations of size 0 eg. ```malloc(0)``` are ever dereferenced and written to, the custom allocater will return an intentionally invalid pointer pointer address for these requests.
For more information, see the comments in ```fuzz_alloc()```.
### Random allocation failures
The custom allocator will periodically return ```NULL``` for heap requests. This tests the library's resilience and correct operation in the event of global memory shortages.
The interval of ```NULL``` return values is deterministic as it relies on the PRNG, so for a given fuzzer input (that encodes the PRNG seed as well), behavior of that input with regards to allocator behaviour is consistent, allowing for reliable reproduction of bugs.
### Detecting inadequate pointer arithmetic
This feature is only available on 32 bit builds.
Unless the ```--no_mmap``` flag is given, the fuzzer will use a special allocation technique for some of the allocation requests. It will use ```mmap()``` to reserve memory at the extremities of the virtual address space -- sometimes at 0x00010000 and sometimes at 0xFFFF0000. This approach can assist in detecting invalid or inadequate pointer arithmetic. For example, consider the following code:
```c
if ( start + n < end ) {
memset(start, 0, n);
}
```
where ```start``` and ```end``` demarcate a memory region, and ```n``` is some positive integer.
If ```n``` is a sufficiently large value, a pointer addition overflow will occur, leading to a page fault. By routinely placing allocations at the high virtual address ```0xFFFF0000```, the chances of detecting this bug are increased. So let's say ```start``` was previously allocated at ```0xFFFF0000```, and ```end``` is ```0xFFFF1000```, and ```n``` is 0xFFFFF. Then the expression effectively becomes:
```c
if ( 0xFFFF0000 + 0x000FFFFF < 0xFFFF1000 ) {
memset(0xFFFF0000, 0, 0x000FFFF);
}
```
The addition ```0xFFFF0000 + 0x000FFFFF``` overflows so the result is ```0x000EFFFF```. Hence:
```c
if ( 0x000EFFFF < 0xFFFF1000 ) { // Expression resolves as true !
```
The subsequent ```memset``` is executed contrary to the programmer's intentions, and a segmentation fault will occur.
While this is a corner case, it can not be ruled out that it might occur in a production environment. What's more, the analyst examining the crash can reason about how the value of ```n``` comes about in the first place, and concoct a crafted input that leads to a very high ```n``` value, making the "exploit" succeed even with average virtual addresses.
Aside from using ```mmap``` to allocate at address ```0xFFFF0000```, the fuzzer will also place allocations at the low virtual address ```0x00010000``` to detect invalid pointer arithmetic involving subtraction:
```c
if ( end - n > start ) {
```
### Output memory testing
```testmem.c``` exports ```fuzz_testmem```. All this function does is copy the input buffer to a newly allocated heap region, and then free that heap region. If AddressSanitizer is enabled, this ensures that the input buffer to ```fuzz_testmem``` is a legal memory region.
If MemorySanitizer is enabled, then ``fuzz_testmem``` calls ```fuzz_testmem_msan````. The latter function writes the data at hand to ```/dev/null```. This is an nice trick to make MemorySanitizer evaluate this data, and crash if it contains uninitialized bytes.
This function has been implemented in a separate file for a reason: from the perspective of an optimizing compiler, this is a meaningless operation, and as such it might be optimized away. Hence, this file must be compiled without optimizations (```-O0``` flag).
## Contributing
When extending the current fuzzer, use variable types whose width is consistent across systems where possible. This is necessary to retain corpus portability. For example, use ```uint64_t``` rather than ```unsigned long```.
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论