Amazon Kinesis Webrtc C SDK
Crypto.h
Go to the documentation of this file.
1 #ifndef __KINESIS_VIDEO_WEBRTC_CLIENT_CRYPTO_CRYPTO__
2 #define __KINESIS_VIDEO_WEBRTC_CLIENT_CRYPTO_CRYPTO__
3 
4 #pragma once
5 
6 #ifdef __cplusplus
7 extern "C" {
8 #endif
9 
10 #ifdef KVS_USE_OPENSSL
11 #define KVS_RSA_F4 RSA_F4
12 #define KVS_MD5_DIGEST_LENGTH MD5_DIGEST_LENGTH
13 #define KVS_SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH
14 #define KVS_MD5_DIGEST(m, mlen, ob) MD5((m), (mlen), (ob));
15 #define KVS_SHA1_HMAC(k, klen, m, mlen, ob, plen) \
16  CHK(NULL != HMAC(EVP_sha1(), (k), (INT32) (klen), (m), (mlen), (ob), (plen)), STATUS_HMAC_GENERATION_ERROR);
17 #define KVS_CRYPTO_INIT() \
18  do { \
19  OpenSSL_add_ssl_algorithms(); \
20  SSL_load_error_strings(); \
21  SSL_library_init(); \
22  } while (0)
23 #define LOG_OPENSSL_ERROR(s) \
24  while ((sslErr = ERR_get_error()) != 0) { \
25  if (sslErr != SSL_ERROR_WANT_WRITE && sslErr != SSL_ERROR_WANT_READ) { \
26  DLOGW("%s failed with %s", (s), ERR_error_string(sslErr, NULL)); \
27  } \
28  }
29 
30 typedef enum {
31  KVS_SRTP_PROFILE_AES128_CM_HMAC_SHA1_80 = SRTP_AES128_CM_SHA1_80,
32  KVS_SRTP_PROFILE_AES128_CM_HMAC_SHA1_32 = SRTP_AES128_CM_SHA1_32,
33 } KVS_SRTP_PROFILE;
34 #elif KVS_USE_MBEDTLS
35 #define KVS_RSA_F4 0x10001L
36 #define KVS_MD5_DIGEST_LENGTH 16
37 #define KVS_SHA1_DIGEST_LENGTH 20
38 #define KVS_MD5_DIGEST(m, mlen, ob) mbedtls_md5_ret((m), (mlen), (ob));
39 #define KVS_SHA1_HMAC(k, klen, m, mlen, ob, plen) \
40  CHK(0 == mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1), (k), (klen), (m), (mlen), (ob)), STATUS_HMAC_GENERATION_ERROR); \
41  *(plen) = mbedtls_md_get_size(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1));
42 #define KVS_CRYPTO_INIT() \
43  do { \
44  } while (0)
45 #define LOG_MBEDTLS_ERROR(s, ret) \
46  do { \
47  CHAR __mbedtlsErr[1024]; \
48  if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) { \
49  mbedtls_strerror(ret, __mbedtlsErr, SIZEOF(__mbedtlsErr)); \
50  DLOGW("%s failed with %s", (s), __mbedtlsErr); \
51  } \
52  } while (0)
53 
54 typedef enum {
55  KVS_SRTP_PROFILE_AES128_CM_HMAC_SHA1_80 = MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_80,
56  KVS_SRTP_PROFILE_AES128_CM_HMAC_SHA1_32 = MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_32,
57 } KVS_SRTP_PROFILE;
58 #else
59 #error "A Crypto implementation is required."
60 #endif
61 
62 #ifdef __cplusplus
63 }
64 #endif
65 #endif //__KINESIS_VIDEO_WEBRTC_CLIENT_CRYPTO_CRYPTO__