You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
5.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2014 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "pc/external_hmac.h"
  11. #include <stdlib.h> // For malloc/free.
  12. #include <string.h>
  13. #include "rtc_base/logging.h"
  14. #include "rtc_base/zero_memory.h"
  15. #include "third_party/libsrtp/include/srtp.h"
  16. // Begin test case 0 */
  17. static const uint8_t kExternalHmacTestCase0Key[20] = {
  18. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  19. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
  20. static const uint8_t kExternalHmacTestCase0Data[8] = {
  21. 0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65 // "Hi There"
  22. };
  23. static const uint8_t kExternalHmacFakeTag[10] = {0xba, 0xdd, 0xba, 0xdd, 0xba,
  24. 0xdd, 0xba, 0xdd, 0xba, 0xdd};
  25. static const srtp_auth_test_case_t kExternalHmacTestCase0 = {
  26. 20, // Octets in key
  27. const_cast<uint8_t*>(kExternalHmacTestCase0Key), // Key
  28. 8, // Octets in data
  29. const_cast<uint8_t*>(kExternalHmacTestCase0Data), // Data
  30. 10, // Octets in tag
  31. const_cast<uint8_t*>(kExternalHmacFakeTag), // Tag
  32. NULL // Pointer to next
  33. // testcase
  34. };
  35. static const char kExternalHmacDescription[] =
  36. "external hmac sha-1 authentication";
  37. // srtp_auth_type_t external_hmac is the hmac metaobject
  38. static const srtp_auth_type_t external_hmac = {
  39. external_hmac_alloc,
  40. external_hmac_dealloc,
  41. external_hmac_init,
  42. external_hmac_compute,
  43. external_hmac_update,
  44. external_hmac_start,
  45. const_cast<char*>(kExternalHmacDescription),
  46. const_cast<srtp_auth_test_case_t*>(&kExternalHmacTestCase0),
  47. EXTERNAL_HMAC_SHA1};
  48. srtp_err_status_t external_hmac_alloc(srtp_auth_t** a,
  49. int key_len,
  50. int out_len) {
  51. uint8_t* pointer;
  52. // Check key length - note that we don't support keys larger
  53. // than 20 bytes yet
  54. if (key_len > 20)
  55. return srtp_err_status_bad_param;
  56. // Check output length - should be less than 20 bytes/
  57. if (out_len > 20)
  58. return srtp_err_status_bad_param;
  59. // Allocate memory for auth and hmac_ctx_t structures.
  60. pointer = new uint8_t[(sizeof(ExternalHmacContext) + sizeof(srtp_auth_t))];
  61. if (pointer == NULL)
  62. return srtp_err_status_alloc_fail;
  63. // Set pointers
  64. *a = reinterpret_cast<srtp_auth_t*>(pointer);
  65. // |external_hmac| is const and libsrtp expects |type| to be non-const.
  66. // const conversion is required. |external_hmac| is constant because we don't
  67. // want to increase global count in Chrome.
  68. (*a)->type = const_cast<srtp_auth_type_t*>(&external_hmac);
  69. (*a)->state = pointer + sizeof(srtp_auth_t);
  70. (*a)->out_len = out_len;
  71. (*a)->key_len = key_len;
  72. (*a)->prefix_len = 0;
  73. return srtp_err_status_ok;
  74. }
  75. srtp_err_status_t external_hmac_dealloc(srtp_auth_t* a) {
  76. rtc::ExplicitZeroMemory(a, sizeof(ExternalHmacContext) + sizeof(srtp_auth_t));
  77. // Free memory
  78. delete[] a;
  79. return srtp_err_status_ok;
  80. }
  81. srtp_err_status_t external_hmac_init(void* state,
  82. const uint8_t* key,
  83. int key_len) {
  84. if (key_len > HMAC_KEY_LENGTH)
  85. return srtp_err_status_bad_param;
  86. ExternalHmacContext* context = static_cast<ExternalHmacContext*>(state);
  87. memcpy(context->key, key, key_len);
  88. context->key_length = key_len;
  89. return srtp_err_status_ok;
  90. }
  91. srtp_err_status_t external_hmac_start(void* /*state*/) {
  92. return srtp_err_status_ok;
  93. }
  94. srtp_err_status_t external_hmac_update(void* /*state*/,
  95. const uint8_t* /*message*/,
  96. int /*msg_octets*/) {
  97. return srtp_err_status_ok;
  98. }
  99. srtp_err_status_t external_hmac_compute(void* /*state*/,
  100. const uint8_t* /*message*/,
  101. int /*msg_octets*/,
  102. int tag_len,
  103. uint8_t* result) {
  104. memcpy(result, kExternalHmacFakeTag, tag_len);
  105. return srtp_err_status_ok;
  106. }
  107. srtp_err_status_t external_crypto_init() {
  108. // |external_hmac| is const. const_cast is required as libsrtp expects
  109. // non-const.
  110. srtp_err_status_t status = srtp_replace_auth_type(
  111. const_cast<srtp_auth_type_t*>(&external_hmac), EXTERNAL_HMAC_SHA1);
  112. if (status) {
  113. RTC_LOG(LS_ERROR) << "Error in replacing default auth module, error: "
  114. << status;
  115. return srtp_err_status_fail;
  116. }
  117. return srtp_err_status_ok;
  118. }