dk_test.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * libZRTP SDK library, implements the ZRTP secure VoIP protocol.
  3. * Copyright (c) 2006-2009 Philip R. Zimmermann. All rights reserved.
  4. * Contact: http://philzimmermann.com
  5. * For licensing and other legal details, see the file zrtp_legal.c.
  6. *
  7. * Viktor Krykun <v.krikun at zfoneproject.com>
  8. */
  9. #include <setjmp.h>
  10. #include <stdio.h>
  11. #include "zrtp.h"
  12. #include "cmockery/cmockery.h"
  13. zrtp_global_t *zrtp;
  14. extern zrtp_dk_ctx *zrtp_dk_init(zrtp_cipher_t *cipher, zrtp_stringn_t *key, zrtp_stringn_t *salt);
  15. extern zrtp_status_t zrtp_derive_key(zrtp_dk_ctx *ctx, zrtp_srtp_prf_label label, zrtp_stringn_t *result_key);
  16. extern void zrtp_dk_deinit(zrtp_dk_ctx *ctx);
  17. static uint8_t dk_master_key[16] = {
  18. 0xE1, 0xF9, 0x7A, 0x0D, 0x3E, 0x01, 0x8B, 0xE0,
  19. 0xD6, 0x4F, 0xA3, 0x2C, 0x06, 0xDE, 0x41, 0x39
  20. };
  21. static uint8_t dk_master_salt[14] = {
  22. 0x0E, 0xC6, 0x75, 0xAD, 0x49, 0x8A, 0xFE, 0xEB,
  23. 0xB6, 0x96, 0x0B, 0x3A, 0xAB, 0xE6
  24. };
  25. static uint8_t dk_cipher_key[16] = {
  26. 0xC6, 0x1E, 0x7A, 0x93, 0x74, 0x4F, 0x39, 0xEE,
  27. 0x10, 0x73, 0x4A, 0xFE, 0x3F, 0xF7, 0xA0, 0x87
  28. };
  29. static uint8_t dk_cipher_salt[14] = {
  30. 0x30, 0xCB, 0xBC, 0x08, 0x86, 0x3D, 0x8C, 0x85,
  31. 0xD4, 0x9D, 0xB3, 0x4A, 0x9A, 0xE1
  32. };
  33. static uint8_t dk_auth_key[94] = {
  34. 0xCE, 0xBE, 0x32, 0x1F, 0x6F, 0xF7, 0x71, 0x6B,
  35. 0x6F, 0xD4, 0xAB, 0x49, 0xAF, 0x25, 0x6A, 0x15,
  36. 0x6D, 0x38, 0xBA, 0xA4, 0x8F, 0x0A, 0x0A, 0xCF,
  37. 0x3C, 0x34, 0xE2, 0x35, 0x9E, 0x6C, 0xDB, 0xCE,
  38. 0xE0, 0x49, 0x64, 0x6C, 0x43, 0xD9, 0x32, 0x7A,
  39. 0xD1, 0x75, 0x57, 0x8E, 0xF7, 0x22, 0x70, 0x98,
  40. 0x63, 0x71, 0xC1, 0x0C, 0x9A, 0x36, 0x9A, 0xC2,
  41. 0xF9, 0x4A, 0x8C, 0x5F, 0xBC, 0xDD, 0xDC, 0x25,
  42. 0x6D, 0x6E, 0x91, 0x9A, 0x48, 0xB6, 0x10, 0xEF,
  43. 0x17, 0xC2, 0x04, 0x1E, 0x47, 0x40, 0x35, 0x76,
  44. 0x6B, 0x68, 0x64, 0x2C, 0x59, 0xBB, 0xFC, 0x2F,
  45. 0x34, 0xDB, 0x60, 0xDB, 0xDF, 0xB2
  46. };
  47. void setup() {
  48. zrtp_status_t s;
  49. zrtp_config_t zrtp_config;
  50. zrtp_config_defaults(&zrtp_config);
  51. s = zrtp_init(&zrtp_config, &zrtp);
  52. assert_int_equal(s, zrtp_status_ok);
  53. }
  54. void teardown() {
  55. zrtp_down(zrtp);
  56. }
  57. zrtp_status_t hex_cmp(uint8_t *a, uint8_t *b, uint32_t len)
  58. {
  59. uint32_t i;
  60. zrtp_status_t res = zrtp_status_ok;
  61. for (i = 0; i<len; i++) {
  62. if (a[i] != b[i]) {
  63. res = zrtp_status_fail;
  64. break;
  65. }
  66. }
  67. return res;
  68. }
  69. static void dk_test() {
  70. zrtp_status_t res;
  71. zrtp_string16_t master_key, master_salt, cipher_key, cipher_salt;
  72. zrtp_string128_t auth_key;
  73. zrtp_dk_ctx *ctx;
  74. zrtp_cipher_t *cipher = zrtp_comp_find(ZRTP_CC_CIPHER, ZRTP_CIPHER_AES128, zrtp);
  75. assert_non_null(cipher);
  76. master_key.length = master_key.max_length = 16;
  77. zrtp_memcpy(master_key.buffer, dk_master_key, 16);
  78. master_salt.length = 14;
  79. master_salt.max_length = 16;
  80. zrtp_memcpy(master_salt.buffer, dk_master_salt, 14);
  81. ctx = zrtp_dk_init(cipher, (zrtp_stringn_t*)&master_key, (zrtp_stringn_t*)&master_salt);
  82. assert_non_null(ctx);
  83. cipher_key.length = 16;
  84. cipher_key.max_length = 16;
  85. zrtp_derive_key(ctx, label_rtp_encryption, (zrtp_stringn_t*)&cipher_key);
  86. res = hex_cmp((uint8_t*)cipher_key.buffer, dk_cipher_key, cipher_key.length);
  87. assert_int_equal(res, zrtp_status_ok);
  88. cipher_salt.length = 14;
  89. cipher_salt.max_length = 16;
  90. zrtp_derive_key(ctx, label_rtp_salt, (zrtp_stringn_t*)&cipher_salt);
  91. res = hex_cmp((uint8_t*)cipher_salt.buffer, dk_cipher_salt, cipher_salt.length);
  92. assert_int_equal(res, zrtp_status_ok);
  93. auth_key.length = 94;
  94. auth_key.max_length = 128;
  95. zrtp_derive_key(ctx, label_rtp_msg_auth, (zrtp_stringn_t*)&auth_key);
  96. res = hex_cmp((uint8_t*)auth_key.buffer, dk_auth_key, auth_key.length);
  97. assert_int_equal(res, zrtp_status_ok);
  98. zrtp_dk_deinit(ctx);
  99. }
  100. int main(void) {
  101. const UnitTest tests[] = {
  102. unit_test_setup_teardown(dk_test, setup, teardown),
  103. };
  104. return run_tests(tests);
  105. }