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 
10 #include <setjmp.h>
11 #include <stdio.h>
12 
13 #include "zrtp.h"
14 #include "cmockery/cmockery.h"
15 
16 zrtp_global_t *zrtp;
17 
18 extern zrtp_dk_ctx *zrtp_dk_init(zrtp_cipher_t *cipher, zrtp_stringn_t *key, zrtp_stringn_t *salt);
19 extern zrtp_status_t zrtp_derive_key(zrtp_dk_ctx *ctx, zrtp_srtp_prf_label label, zrtp_stringn_t *result_key);
20 extern void zrtp_dk_deinit(zrtp_dk_ctx *ctx);
21 
22 static uint8_t dk_master_key[16] = {
23 	0xE1, 0xF9, 0x7A, 0x0D, 0x3E, 0x01, 0x8B, 0xE0,
24 	0xD6, 0x4F, 0xA3, 0x2C, 0x06, 0xDE, 0x41, 0x39
25 };
26 
27 static uint8_t dk_master_salt[14] = {
28 	0x0E, 0xC6, 0x75, 0xAD, 0x49, 0x8A, 0xFE, 0xEB,
29 	0xB6, 0x96, 0x0B, 0x3A, 0xAB, 0xE6
30 };
31 
32 
33 static uint8_t dk_cipher_key[16] = {
34 	0xC6, 0x1E, 0x7A, 0x93, 0x74, 0x4F, 0x39, 0xEE,
35 	0x10, 0x73, 0x4A, 0xFE, 0x3F, 0xF7, 0xA0, 0x87
36 };
37 
38 static uint8_t dk_cipher_salt[14] = {
39 	0x30, 0xCB, 0xBC, 0x08, 0x86, 0x3D, 0x8C, 0x85,
40 	0xD4, 0x9D, 0xB3, 0x4A, 0x9A, 0xE1
41 };
42 
43 static uint8_t dk_auth_key[94] = {
44 	0xCE, 0xBE, 0x32, 0x1F, 0x6F, 0xF7, 0x71, 0x6B,
45 	0x6F, 0xD4, 0xAB, 0x49, 0xAF, 0x25, 0x6A, 0x15,
46 	0x6D, 0x38, 0xBA, 0xA4, 0x8F, 0x0A, 0x0A, 0xCF,
47 	0x3C, 0x34, 0xE2, 0x35, 0x9E, 0x6C, 0xDB, 0xCE,
48 	0xE0, 0x49, 0x64, 0x6C, 0x43, 0xD9, 0x32, 0x7A,
49 	0xD1, 0x75, 0x57, 0x8E, 0xF7, 0x22, 0x70, 0x98,
50 	0x63, 0x71, 0xC1, 0x0C, 0x9A, 0x36, 0x9A, 0xC2,
51 	0xF9, 0x4A, 0x8C, 0x5F, 0xBC, 0xDD, 0xDC, 0x25,
52 	0x6D, 0x6E, 0x91, 0x9A, 0x48, 0xB6, 0x10, 0xEF,
53 	0x17, 0xC2, 0x04, 0x1E, 0x47, 0x40, 0x35, 0x76,
54 	0x6B, 0x68, 0x64, 0x2C, 0x59, 0xBB, 0xFC, 0x2F,
55 	0x34, 0xDB, 0x60, 0xDB, 0xDF, 0xB2
56 };
57 
58 
setup()59 void setup() {
60 	zrtp_status_t s;
61 	zrtp_config_t zrtp_config;
62 
63 	zrtp_config_defaults(&zrtp_config);
64 
65 	s = zrtp_init(&zrtp_config, &zrtp);
66 	assert_int_equal(s, zrtp_status_ok);
67 }
68 
teardown()69 void teardown() {
70 	zrtp_down(zrtp);
71 }
72 
hex_cmp(uint8_t * a,uint8_t * b,uint32_t len)73 zrtp_status_t hex_cmp(uint8_t *a, uint8_t *b, uint32_t len)
74 {
75 	uint32_t i;
76 	zrtp_status_t res = zrtp_status_ok;
77 	for (i = 0; i<len; i++) {
78 		if (a[i] != b[i]) {
79 			res = zrtp_status_fail;
80 			break;
81 		}
82 	}
83 	return res;
84 }
85 
dk_test()86 static void dk_test() {
87 
88 	zrtp_status_t res;
89 	zrtp_string16_t master_key, master_salt, cipher_key, cipher_salt;
90 	zrtp_string128_t auth_key;
91 	zrtp_dk_ctx *ctx;
92 
93 	zrtp_cipher_t *cipher = zrtp_comp_find(ZRTP_CC_CIPHER, ZRTP_CIPHER_AES128, zrtp);
94 	assert_non_null(cipher);
95 
96 	master_key.length = master_key.max_length = 16;
97 	zrtp_memcpy(master_key.buffer, dk_master_key, 16);
98 
99 	master_salt.length = 14;
100 	master_salt.max_length = 16;
101 	zrtp_memcpy(master_salt.buffer, dk_master_salt, 14);
102 
103 
104 	ctx = zrtp_dk_init(cipher, (zrtp_stringn_t*)&master_key, (zrtp_stringn_t*)&master_salt);
105 	assert_non_null(ctx);
106 
107 	cipher_key.length = 16;
108 	cipher_key.max_length = 16;
109 
110 	zrtp_derive_key(ctx, label_rtp_encryption, (zrtp_stringn_t*)&cipher_key);
111 	res = hex_cmp((uint8_t*)cipher_key.buffer, dk_cipher_key, cipher_key.length);
112 	assert_int_equal(res, zrtp_status_ok);
113 
114 
115 	cipher_salt.length = 14;
116 	cipher_salt.max_length = 16;
117 
118 	zrtp_derive_key(ctx, label_rtp_salt, (zrtp_stringn_t*)&cipher_salt);
119 	res = hex_cmp((uint8_t*)cipher_salt.buffer, dk_cipher_salt, cipher_salt.length);
120 	assert_int_equal(res, zrtp_status_ok);
121 
122 
123 	auth_key.length = 94;
124 	auth_key.max_length = 128;
125 
126 	zrtp_derive_key(ctx, label_rtp_msg_auth, (zrtp_stringn_t*)&auth_key);
127 	res = hex_cmp((uint8_t*)auth_key.buffer, dk_auth_key, auth_key.length);
128 	assert_int_equal(res, zrtp_status_ok);
129 
130 	zrtp_dk_deinit(ctx);
131 }
132 
133 
main(void)134 int main(void) {
135 	const UnitTest tests[] = {
136 		unit_test_setup_teardown(dk_test, setup, teardown),
137   	};
138 
139 	return run_tests(tests);
140 }
141