test-eap-mschapv2.c (4182B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2016 Markus Ongyerth. All rights reserved. 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 * 21 */ 22 23 #ifdef HAVE_CONFIG_H 24 #include <config.h> 25 #endif 26 27 #include <stdio.h> 28 #include <string.h> 29 #include <assert.h> 30 #include <ell/ell.h> 31 32 #include "src/eap-mschapv2.h" 33 #include "src/mschaputil.h" 34 35 /* The test values here are taken from the RFCs the functions are defined in */ 36 37 /* https://tools.ietf.org/html/rfc2759 */ 38 const char *user = "User"; 39 const char *nt_password = "clientPass"; 40 const uint8_t password_hash[] = { 41 0x44, 0xEB, 0xBA, 0x8D, 42 0x53, 0x12, 0xB8, 0xD6, 43 0x11, 0x47, 0x44, 0x11, 44 0xF5, 0x69, 0x89, 0xAE 45 }; 46 const uint8_t server_challenge[] = { 47 0x5B, 0x5D, 0x7C, 0x7D, 48 0x7B, 0x3F, 0x2F, 0x3E, 49 0x3C, 0x2C, 0x60, 0x21, 50 0x32, 0x26, 0x26, 0x28 51 }; 52 const uint8_t peer_challenge[] = { 53 0x21, 0x40, 0x23, 0x24, 54 0x25, 0x5E, 0x26, 0x2A, 55 0x28, 0x29, 0x5F, 0x2B, 56 0x3A, 0x33, 0x7C, 0x7E 57 }; 58 const uint8_t nt_response[] = { 59 0x82, 0x30, 0x9E, 0xCD, 60 0x8D, 0x70, 0x8B, 0x5E, 61 0xA0, 0x8F, 0xAA, 0x39, 62 0x81, 0xCD, 0x83, 0x54, 63 0x42, 0x33, 0x11, 0x4A, 64 0x3D, 0x85, 0xD6, 0xDF 65 }; 66 const uint8_t password_hash_hash[] = { 67 0x41, 0xC0, 0x0C, 0x58, 68 0x4B, 0xD2, 0xD9, 0x1C, 69 0x40, 0x17, 0xA2, 0xA1, 70 0x2F, 0xA5, 0x9F, 0x3F 71 }; 72 const char *authenticator_response = 73 "S=407A5589115FD0D6209F510FE9C04566932CDA56"; 74 /* https://tools.ietf.org/html/draft-ietf-pppext-mschapv2-keys-02 */ 75 const uint8_t master_key[] = { 76 0xFD, 0xEC, 0xE3, 0x71, 77 0x7A, 0x8C, 0x83, 0x8C, 78 0xB3, 0x88, 0xE5, 0x27, 79 0xAE, 0x3C, 0xDD, 0x31 80 }; 81 const uint8_t m_session_key[] = { 82 0x8B, 0x7C, 0xDC, 0x14, 83 0x9B, 0x99, 0x3A, 0x1B, 84 0xA1, 0x18, 0xCB, 0x15, 85 0x3F, 0x56, 0xDC, 0xCB 86 }; 87 88 static void test_nt_password_hash(const void *data) 89 { 90 uint8_t hash[16]; 91 92 assert(mschap_nt_password_hash(nt_password, hash)); 93 assert(!memcmp(hash, password_hash, sizeof(password_hash))); 94 } 95 96 static void test_generate_nt_response(const void *data) 97 { 98 uint8_t nt_resp[24]; 99 100 assert(mschapv2_generate_nt_response(password_hash, peer_challenge, 101 server_challenge, user, nt_resp)); 102 assert(!memcmp(nt_resp, nt_response, sizeof(nt_response))); 103 } 104 105 static void test_authenticator_response(const void *data) 106 { 107 char buf[43]; 108 buf[42] = '\0'; 109 110 assert(mschapv2_generate_authenticator_response(password_hash_hash, 111 nt_response, peer_challenge, 112 server_challenge, user, 113 buf)); 114 assert(!strcmp(buf, authenticator_response)); 115 } 116 117 static void test_get_master_key(const void *data) 118 { 119 uint8_t m_key[16]; 120 121 assert(mschapv2_get_master_key(password_hash_hash, nt_response, m_key)); 122 assert(!memcmp(m_key, master_key, sizeof(master_key))); 123 } 124 125 static void test_get_asym_key(const void *data) 126 { 127 uint8_t msk[20]; 128 129 assert(mschapv2_get_asymmetric_start_key(master_key, msk, sizeof(msk), 130 true, true)); 131 assert(!memcmp(msk, m_session_key, sizeof(m_session_key))); 132 } 133 134 static bool test_precheck(const void *data) 135 { 136 return l_checksum_is_supported(L_CHECKSUM_MD4, false); 137 } 138 139 #define add_test(name, func) l_test_add_func_precheck(name, func, \ 140 test_precheck, 0) 141 142 int main(int argc, char *argv[]) 143 { 144 l_test_init(&argc, &argv); 145 146 add_test("MSHAPv2 nt_password-hash", test_nt_password_hash); 147 add_test("MSHAPv2 generate_nt_response", test_generate_nt_response); 148 add_test("MSHAPv2 get_master_key", test_get_master_key); 149 add_test("MSHAPv2 get_asym_state_key", test_get_asym_key); 150 add_test("MSHAPv2 authenticator_response", test_authenticator_response); 151 152 return l_test_run(); 153 }