From a2184eb00d2b02d48ec14e72615736281b9d5f81 Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Tue, 23 Apr 2013 19:47:32 +0000 Subject: - changed project file structure - skype runtime initialization moved to plugin load func - fixed password encription - disabled message sync git-svn-id: http://svn.miranda-ng.org/main/trunk@4518 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/Skype/Skype_10.vcxproj | 26 +- protocols/Skype/Skype_10.vcxproj.filters | 75 ++- protocols/Skype/Skype_11.vcxproj | 2 - protocols/Skype/Skype_11.vcxproj.filters | 6 - protocols/Skype/src/aes/aes.c | 765 -------------------------- protocols/Skype/src/aes/aes.cpp | 765 ++++++++++++++++++++++++++ protocols/Skype/src/skype.cpp | 261 ++++++++- protocols/Skype/src/skype.h | 13 +- protocols/Skype/src/skype_account.cpp | 18 +- protocols/Skype/src/skype_chat.cpp | 20 +- protocols/Skype/src/skype_contacts.cpp | 14 +- protocols/Skype/src/skype_database.cpp | 16 +- protocols/Skype/src/skype_dialogs.cpp | 65 +-- protocols/Skype/src/skype_events.cpp | 39 +- protocols/Skype/src/skype_instances.cpp | 6 +- protocols/Skype/src/skype_menus.cpp | 4 +- protocols/Skype/src/skype_netlib.cpp | 36 +- protocols/Skype/src/skype_proto.cpp | 40 +- protocols/Skype/src/skype_proto.h | 27 +- protocols/Skype/src/skype_runtime.cpp | 18 - protocols/Skype/src/skype_subclassing.cpp | 574 ------------------- protocols/Skype/src/skype_subclassing.h | 201 ------- protocols/Skype/src/skype_utils.cpp | 29 +- protocols/Skype/src/skypekit/account.cpp | 34 ++ protocols/Skype/src/skypekit/account.h | 26 + protocols/Skype/src/skypekit/common.h | 8 + protocols/Skype/src/skypekit/contact.cpp | 59 ++ protocols/Skype/src/skypekit/contact.h | 26 + protocols/Skype/src/skypekit/conversation.cpp | 3 + protocols/Skype/src/skypekit/conversation.h | 15 + protocols/Skype/src/skypekit/group.cpp | 19 + protocols/Skype/src/skypekit/group.h | 22 + protocols/Skype/src/skypekit/message.cpp | 3 + protocols/Skype/src/skypekit/message.h | 12 + protocols/Skype/src/skypekit/participant.cpp | 33 ++ protocols/Skype/src/skypekit/participant.h | 14 + protocols/Skype/src/skypekit/search.cpp | 53 ++ protocols/Skype/src/skypekit/search.h | 33 ++ protocols/Skype/src/skypekit/skype.cpp | 63 +++ protocols/Skype/src/skypekit/skype.h | 41 ++ protocols/Skype/src/skypekit/transfer.cpp | 19 + protocols/Skype/src/skypekit/transfer.h | 22 + 42 files changed, 1780 insertions(+), 1745 deletions(-) delete mode 100644 protocols/Skype/src/aes/aes.c create mode 100644 protocols/Skype/src/aes/aes.cpp delete mode 100644 protocols/Skype/src/skype_runtime.cpp delete mode 100644 protocols/Skype/src/skype_subclassing.cpp delete mode 100644 protocols/Skype/src/skype_subclassing.h create mode 100644 protocols/Skype/src/skypekit/account.cpp create mode 100644 protocols/Skype/src/skypekit/account.h create mode 100644 protocols/Skype/src/skypekit/common.h create mode 100644 protocols/Skype/src/skypekit/contact.cpp create mode 100644 protocols/Skype/src/skypekit/contact.h create mode 100644 protocols/Skype/src/skypekit/conversation.cpp create mode 100644 protocols/Skype/src/skypekit/conversation.h create mode 100644 protocols/Skype/src/skypekit/group.cpp create mode 100644 protocols/Skype/src/skypekit/group.h create mode 100644 protocols/Skype/src/skypekit/message.cpp create mode 100644 protocols/Skype/src/skypekit/message.h create mode 100644 protocols/Skype/src/skypekit/participant.cpp create mode 100644 protocols/Skype/src/skypekit/participant.h create mode 100644 protocols/Skype/src/skypekit/search.cpp create mode 100644 protocols/Skype/src/skypekit/search.h create mode 100644 protocols/Skype/src/skypekit/skype.cpp create mode 100644 protocols/Skype/src/skypekit/skype.h create mode 100644 protocols/Skype/src/skypekit/transfer.cpp create mode 100644 protocols/Skype/src/skypekit/transfer.h diff --git a/protocols/Skype/Skype_10.vcxproj b/protocols/Skype/Skype_10.vcxproj index 953a03c36e..dea0492a7f 100644 --- a/protocols/Skype/Skype_10.vcxproj +++ b/protocols/Skype/Skype_10.vcxproj @@ -83,6 +83,7 @@ true 4996;%(DisableSpecificWarnings) MultiThreadedDebugDLL + $(IntDir)/%(RelativeDir)/ Windows @@ -180,17 +181,36 @@ + - + + + + + + + + + + - + + + + + + + + + + @@ -203,10 +223,8 @@ - - diff --git a/protocols/Skype/Skype_10.vcxproj.filters b/protocols/Skype/Skype_10.vcxproj.filters index cb48bf40f0..d7470bc9bb 100644 --- a/protocols/Skype/Skype_10.vcxproj.filters +++ b/protocols/Skype/Skype_10.vcxproj.filters @@ -16,6 +16,12 @@ {f1aeb5a2-f105-4b8c-a656-08dbe6dc7b2e} + + {a39a08d1-b79a-47eb-8433-1351f9f01401} + + + {171bef99-1ab9-424f-835d-9e47ab62365c} + @@ -27,9 +33,6 @@ Source Files - - Source Files - Source Files @@ -69,13 +72,37 @@ Source Files - + Source Files - - Source Files + + Source Files\skypekit - + + Source Files\skypekit + + + Source Files\skypekit + + + Source Files\skypekit + + + Source Files\skypekit + + + Source Files\skypekit + + + Source Files\skypekit + + + Source Files\skypekit + + + Source Files\skypekit + + Source Files @@ -92,10 +119,40 @@ Header Files - + Header Files - + + Header Files\skypekit + + + Header Files\skypekit + + + Header Files\skypekit + + + Header Files\skypekit + + + Header Files\skypekit + + + Header Files\skypekit + + + Header Files\skypekit + + + Header Files\skypekit + + + Header Files\skypekit + + + Header Files\skypekit + + Header Files diff --git a/protocols/Skype/Skype_11.vcxproj b/protocols/Skype/Skype_11.vcxproj index d4917373b2..74862af1a0 100644 --- a/protocols/Skype/Skype_11.vcxproj +++ b/protocols/Skype/Skype_11.vcxproj @@ -186,7 +186,6 @@ - @@ -211,7 +210,6 @@ - diff --git a/protocols/Skype/Skype_11.vcxproj.filters b/protocols/Skype/Skype_11.vcxproj.filters index 9596201554..ad19d3605d 100644 --- a/protocols/Skype/Skype_11.vcxproj.filters +++ b/protocols/Skype/Skype_11.vcxproj.filters @@ -33,9 +33,6 @@ Source Files - - Source Files - Source Files @@ -98,9 +95,6 @@ Header Files - - Header Files - Header Files\base64 diff --git a/protocols/Skype/src/aes/aes.c b/protocols/Skype/src/aes/aes.c deleted file mode 100644 index 7e15f1547b..0000000000 --- a/protocols/Skype/src/aes/aes.c +++ /dev/null @@ -1,765 +0,0 @@ -/* - * FIPS-197 compliant AES implementation - * - * Copyright (C) 2001-2004 Christophe Devine - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#include "aes.h" - -/* uncomment the following line to run the test suite */ - -/* #define TEST */ - -/* uncomment the following line to use pre-computed tables */ -/* otherwise the tables will be generated at the first run */ - -/* #define FIXED_TABLES */ - -#ifndef FIXED_TABLES - -/* forward S-box & tables */ - -uint32 FSb[256]; -uint32 FT0[256]; -uint32 FT1[256]; -uint32 FT2[256]; -uint32 FT3[256]; - -/* reverse S-box & tables */ - -uint32 RSb[256]; -uint32 RT0[256]; -uint32 RT1[256]; -uint32 RT2[256]; -uint32 RT3[256]; - -/* round constants */ - -uint32 RCON[10]; - -/* tables generation flag */ - -int do_init = 1; - -/* tables generation routine */ - -#define ROTR8(x) ( ( ( x << 24 ) & 0xFFFFFFFF ) | \ - ( ( x & 0xFFFFFFFF ) >> 8 ) ) - -#define XTIME(x) ( ( x << 1 ) ^ ( ( x & 0x80 ) ? 0x1B : 0x00 ) ) -#define MUL(x,y) ( ( x && y ) ? pow[(log[x] + log[y]) % 255] : 0 ) - -void aes_gen_tables( void ) -{ - int i; - uint8 x, y; - uint8 pow[256]; - uint8 log[256]; - - /* compute pow and log tables over GF(2^8) */ - - for( i = 0, x = 1; i < 256; i++, x ^= XTIME( x ) ) - { - pow[i] = x; - log[x] = i; - } - - /* calculate the round constants */ - - for( i = 0, x = 1; i < 10; i++, x = XTIME( x ) ) - { - RCON[i] = (uint32) x << 24; - } - - /* generate the forward and reverse S-boxes */ - - FSb[0x00] = 0x63; - RSb[0x63] = 0x00; - - for( i = 1; i < 256; i++ ) - { - x = pow[255 - log[i]]; - - y = x; y = ( y << 1 ) | ( y >> 7 ); - x ^= y; y = ( y << 1 ) | ( y >> 7 ); - x ^= y; y = ( y << 1 ) | ( y >> 7 ); - x ^= y; y = ( y << 1 ) | ( y >> 7 ); - x ^= y ^ 0x63; - - FSb[i] = x; - RSb[x] = i; - } - - /* generate the forward and reverse tables */ - - for( i = 0; i < 256; i++ ) - { - x = (unsigned char) FSb[i]; y = XTIME( x ); - - FT0[i] = (uint32) ( x ^ y ) ^ - ( (uint32) x << 8 ) ^ - ( (uint32) x << 16 ) ^ - ( (uint32) y << 24 ); - - FT0[i] &= 0xFFFFFFFF; - - FT1[i] = ROTR8( FT0[i] ); - FT2[i] = ROTR8( FT1[i] ); - FT3[i] = ROTR8( FT2[i] ); - - y = (unsigned char) RSb[i]; - - RT0[i] = ( (uint32) MUL( 0x0B, y ) ) ^ - ( (uint32) MUL( 0x0D, y ) << 8 ) ^ - ( (uint32) MUL( 0x09, y ) << 16 ) ^ - ( (uint32) MUL( 0x0E, y ) << 24 ); - - RT0[i] &= 0xFFFFFFFF; - - RT1[i] = ROTR8( RT0[i] ); - RT2[i] = ROTR8( RT1[i] ); - RT3[i] = ROTR8( RT2[i] ); - } -} - -#else - -/* forward S-box */ - -static const uint32 FSb[256] = -{ - 0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, - 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76, - 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, - 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0, - 0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, - 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15, - 0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, - 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75, - 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, - 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84, - 0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B, - 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF, - 0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, - 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8, - 0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, - 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2, - 0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17, - 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73, - 0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, - 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB, - 0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, - 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79, - 0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9, - 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08, - 0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, - 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A, - 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, - 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E, - 0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94, - 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF, - 0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, - 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16 -}; - -/* forward tables */ - -#define FT \ -\ - V(C6,63,63,A5), V(F8,7C,7C,84), V(EE,77,77,99), V(F6,7B,7B,8D), \ - V(FF,F2,F2,0D), V(D6,6B,6B,BD), V(DE,6F,6F,B1), V(91,C5,C5,54), \ - V(60,30,30,50), V(02,01,01,03), V(CE,67,67,A9), V(56,2B,2B,7D), \ - V(E7,FE,FE,19), V(B5,D7,D7,62), V(4D,AB,AB,E6), V(EC,76,76,9A), \ - V(8F,CA,CA,45), V(1F,82,82,9D), V(89,C9,C9,40), V(FA,7D,7D,87), \ - V(EF,FA,FA,15), V(B2,59,59,EB), V(8E,47,47,C9), V(FB,F0,F0,0B), \ - V(41,AD,AD,EC), V(B3,D4,D4,67), V(5F,A2,A2,FD), V(45,AF,AF,EA), \ - V(23,9C,9C,BF), V(53,A4,A4,F7), V(E4,72,72,96), V(9B,C0,C0,5B), \ - V(75,B7,B7,C2), V(E1,FD,FD,1C), V(3D,93,93,AE), V(4C,26,26,6A), \ - V(6C,36,36,5A), V(7E,3F,3F,41), V(F5,F7,F7,02), V(83,CC,CC,4F), \ - V(68,34,34,5C), V(51,A5,A5,F4), V(D1,E5,E5,34), V(F9,F1,F1,08), \ - V(E2,71,71,93), V(AB,D8,D8,73), V(62,31,31,53), V(2A,15,15,3F), \ - V(08,04,04,0C), V(95,C7,C7,52), V(46,23,23,65), V(9D,C3,C3,5E), \ - V(30,18,18,28), V(37,96,96,A1), V(0A,05,05,0F), V(2F,9A,9A,B5), \ - V(0E,07,07,09), V(24,12,12,36), V(1B,80,80,9B), V(DF,E2,E2,3D), \ - V(CD,EB,EB,26), V(4E,27,27,69), V(7F,B2,B2,CD), V(EA,75,75,9F), \ - V(12,09,09,1B), V(1D,83,83,9E), V(58,2C,2C,74), V(34,1A,1A,2E), \ - V(36,1B,1B,2D), V(DC,6E,6E,B2), V(B4,5A,5A,EE), V(5B,A0,A0,FB), \ - V(A4,52,52,F6), V(76,3B,3B,4D), V(B7,D6,D6,61), V(7D,B3,B3,CE), \ - V(52,29,29,7B), V(DD,E3,E3,3E), V(5E,2F,2F,71), V(13,84,84,97), \ - V(A6,53,53,F5), V(B9,D1,D1,68), V(00,00,00,00), V(C1,ED,ED,2C), \ - V(40,20,20,60), V(E3,FC,FC,1F), V(79,B1,B1,C8), V(B6,5B,5B,ED), \ - V(D4,6A,6A,BE), V(8D,CB,CB,46), V(67,BE,BE,D9), V(72,39,39,4B), \ - V(94,4A,4A,DE), V(98,4C,4C,D4), V(B0,58,58,E8), V(85,CF,CF,4A), \ - V(BB,D0,D0,6B), V(C5,EF,EF,2A), V(4F,AA,AA,E5), V(ED,FB,FB,16), \ - V(86,43,43,C5), V(9A,4D,4D,D7), V(66,33,33,55), V(11,85,85,94), \ - V(8A,45,45,CF), V(E9,F9,F9,10), V(04,02,02,06), V(FE,7F,7F,81), \ - V(A0,50,50,F0), V(78,3C,3C,44), V(25,9F,9F,BA), V(4B,A8,A8,E3), \ - V(A2,51,51,F3), V(5D,A3,A3,FE), V(80,40,40,C0), V(05,8F,8F,8A), \ - V(3F,92,92,AD), V(21,9D,9D,BC), V(70,38,38,48), V(F1,F5,F5,04), \ - V(63,BC,BC,DF), V(77,B6,B6,C1), V(AF,DA,DA,75), V(42,21,21,63), \ - V(20,10,10,30), V(E5,FF,FF,1A), V(FD,F3,F3,0E), V(BF,D2,D2,6D), \ - V(81,CD,CD,4C), V(18,0C,0C,14), V(26,13,13,35), V(C3,EC,EC,2F), \ - V(BE,5F,5F,E1), V(35,97,97,A2), V(88,44,44,CC), V(2E,17,17,39), \ - V(93,C4,C4,57), V(55,A7,A7,F2), V(FC,7E,7E,82), V(7A,3D,3D,47), \ - V(C8,64,64,AC), V(BA,5D,5D,E7), V(32,19,19,2B), V(E6,73,73,95), \ - V(C0,60,60,A0), V(19,81,81,98), V(9E,4F,4F,D1), V(A3,DC,DC,7F), \ - V(44,22,22,66), V(54,2A,2A,7E), V(3B,90,90,AB), V(0B,88,88,83), \ - V(8C,46,46,CA), V(C7,EE,EE,29), V(6B,B8,B8,D3), V(28,14,14,3C), \ - V(A7,DE,DE,79), V(BC,5E,5E,E2), V(16,0B,0B,1D), V(AD,DB,DB,76), \ - V(DB,E0,E0,3B), V(64,32,32,56), V(74,3A,3A,4E), V(14,0A,0A,1E), \ - V(92,49,49,DB), V(0C,06,06,0A), V(48,24,24,6C), V(B8,5C,5C,E4), \ - V(9F,C2,C2,5D), V(BD,D3,D3,6E), V(43,AC,AC,EF), V(C4,62,62,A6), \ - V(39,91,91,A8), V(31,95,95,A4), V(D3,E4,E4,37), V(F2,79,79,8B), \ - V(D5,E7,E7,32), V(8B,C8,C8,43), V(6E,37,37,59), V(DA,6D,6D,B7), \ - V(01,8D,8D,8C), V(B1,D5,D5,64), V(9C,4E,4E,D2), V(49,A9,A9,E0), \ - V(D8,6C,6C,B4), V(AC,56,56,FA), V(F3,F4,F4,07), V(CF,EA,EA,25), \ - V(CA,65,65,AF), V(F4,7A,7A,8E), V(47,AE,AE,E9), V(10,08,08,18), \ - V(6F,BA,BA,D5), V(F0,78,78,88), V(4A,25,25,6F), V(5C,2E,2E,72), \ - V(38,1C,1C,24), V(57,A6,A6,F1), V(73,B4,B4,C7), V(97,C6,C6,51), \ - V(CB,E8,E8,23), V(A1,DD,DD,7C), V(E8,74,74,9C), V(3E,1F,1F,21), \ - V(96,4B,4B,DD), V(61,BD,BD,DC), V(0D,8B,8B,86), V(0F,8A,8A,85), \ - V(E0,70,70,90), V(7C,3E,3E,42), V(71,B5,B5,C4), V(CC,66,66,AA), \ - V(90,48,48,D8), V(06,03,03,05), V(F7,F6,F6,01), V(1C,0E,0E,12), \ - V(C2,61,61,A3), V(6A,35,35,5F), V(AE,57,57,F9), V(69,B9,B9,D0), \ - V(17,86,86,91), V(99,C1,C1,58), V(3A,1D,1D,27), V(27,9E,9E,B9), \ - V(D9,E1,E1,38), V(EB,F8,F8,13), V(2B,98,98,B3), V(22,11,11,33), \ - V(D2,69,69,BB), V(A9,D9,D9,70), V(07,8E,8E,89), V(33,94,94,A7), \ - V(2D,9B,9B,B6), V(3C,1E,1E,22), V(15,87,87,92), V(C9,E9,E9,20), \ - V(87,CE,CE,49), V(AA,55,55,FF), V(50,28,28,78), V(A5,DF,DF,7A), \ - V(03,8C,8C,8F), V(59,A1,A1,F8), V(09,89,89,80), V(1A,0D,0D,17), \ - V(65,BF,BF,DA), V(D7,E6,E6,31), V(84,42,42,C6), V(D0,68,68,B8), \ - V(82,41,41,C3), V(29,99,99,B0), V(5A,2D,2D,77), V(1E,0F,0F,11), \ - V(7B,B0,B0,CB), V(A8,54,54,FC), V(6D,BB,BB,D6), V(2C,16,16,3A) - -#define V(a,b,c,d) 0x##a##b##c##d -static const uint32 FT0[256] = { FT }; -#undef V - -#define V(a,b,c,d) 0x##d##a##b##c -static const uint32 FT1[256] = { FT }; -#undef V - -#define V(a,b,c,d) 0x##c##d##a##b -static const uint32 FT2[256] = { FT }; -#undef V - -#define V(a,b,c,d) 0x##b##c##d##a -static const uint32 FT3[256] = { FT }; -#undef V - -#undef FT - -/* reverse S-box */ - -static const uint32 RSb[256] = -{ - 0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38, - 0xBF, 0x40, 0xA3, 0x9E, 0x81, 0xF3, 0xD7, 0xFB, - 0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87, - 0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB, - 0x54, 0x7B, 0x94, 0x32, 0xA6, 0xC2, 0x23, 0x3D, - 0xEE, 0x4C, 0x95, 0x0B, 0x42, 0xFA, 0xC3, 0x4E, - 0x08, 0x2E, 0xA1, 0x66, 0x28, 0xD9, 0x24, 0xB2, - 0x76, 0x5B, 0xA2, 0x49, 0x6D, 0x8B, 0xD1, 0x25, - 0x72, 0xF8, 0xF6, 0x64, 0x86, 0x68, 0x98, 0x16, - 0xD4, 0xA4, 0x5C, 0xCC, 0x5D, 0x65, 0xB6, 0x92, - 0x6C, 0x70, 0x48, 0x50, 0xFD, 0xED, 0xB9, 0xDA, - 0x5E, 0x15, 0x46, 0x57, 0xA7, 0x8D, 0x9D, 0x84, - 0x90, 0xD8, 0xAB, 0x00, 0x8C, 0xBC, 0xD3, 0x0A, - 0xF7, 0xE4, 0x58, 0x05, 0xB8, 0xB3, 0x45, 0x06, - 0xD0, 0x2C, 0x1E, 0x8F, 0xCA, 0x3F, 0x0F, 0x02, - 0xC1, 0xAF, 0xBD, 0x03, 0x01, 0x13, 0x8A, 0x6B, - 0x3A, 0x91, 0x11, 0x41, 0x4F, 0x67, 0xDC, 0xEA, - 0x97, 0xF2, 0xCF, 0xCE, 0xF0, 0xB4, 0xE6, 0x73, - 0x96, 0xAC, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85, - 0xE2, 0xF9, 0x37, 0xE8, 0x1C, 0x75, 0xDF, 0x6E, - 0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89, - 0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B, - 0xFC, 0x56, 0x3E, 0x4B, 0xC6, 0xD2, 0x79, 0x20, - 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4, - 0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31, - 0xB1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xEC, 0x5F, - 0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D, - 0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF, - 0xA0, 0xE0, 0x3B, 0x4D, 0xAE, 0x2A, 0xF5, 0xB0, - 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61, - 0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, - 0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C, 0x7D -}; - -/* reverse tables */ - -#define RT \ -\ - V(51,F4,A7,50), V(7E,41,65,53), V(1A,17,A4,C3), V(3A,27,5E,96), \ - V(3B,AB,6B,CB), V(1F,9D,45,F1), V(AC,FA,58,AB), V(4B,E3,03,93), \ - V(20,30,FA,55), V(AD,76,6D,F6), V(88,CC,76,91), V(F5,02,4C,25), \ - V(4F,E5,D7,FC), V(C5,2A,CB,D7), V(26,35,44,80), V(B5,62,A3,8F), \ - V(DE,B1,5A,49), V(25,BA,1B,67), V(45,EA,0E,98), V(5D,FE,C0,E1), \ - V(C3,2F,75,02), V(81,4C,F0,12), V(8D,46,97,A3), V(6B,D3,F9,C6), \ - V(03,8F,5F,E7), V(15,92,9C,95), V(BF,6D,7A,EB), V(95,52,59,DA), \ - V(D4,BE,83,2D), V(58,74,21,D3), V(49,E0,69,29), V(8E,C9,C8,44), \ - V(75,C2,89,6A), V(F4,8E,79,78), V(99,58,3E,6B), V(27,B9,71,DD), \ - V(BE,E1,4F,B6), V(F0,88,AD,17), V(C9,20,AC,66), V(7D,CE,3A,B4), \ - V(63,DF,4A,18), V(E5,1A,31,82), V(97,51,33,60), V(62,53,7F,45), \ - V(B1,64,77,E0), V(BB,6B,AE,84), V(FE,81,A0,1C), V(F9,08,2B,94), \ - V(70,48,68,58), V(8F,45,FD,19), V(94,DE,6C,87), V(52,7B,F8,B7), \ - V(AB,73,D3,23), V(72,4B,02,E2), V(E3,1F,8F,57), V(66,55,AB,2A), \ - V(B2,EB,28,07), V(2F,B5,C2,03), V(86,C5,7B,9A), V(D3,37,08,A5), \ - V(30,28,87,F2), V(23,BF,A5,B2), V(02,03,6A,BA), V(ED,16,82,5C), \ - V(8A,CF,1C,2B), V(A7,79,B4,92), V(F3,07,F2,F0), V(4E,69,E2,A1), \ - V(65,DA,F4,CD), V(06,05,BE,D5), V(D1,34,62,1F), V(C4,A6,FE,8A), \ - V(34,2E,53,9D), V(A2,F3,55,A0), V(05,8A,E1,32), V(A4,F6,EB,75), \ - V(0B,83,EC,39), V(40,60,EF,AA), V(5E,71,9F,06), V(BD,6E,10,51), \ - V(3E,21,8A,F9), V(96,DD,06,3D), V(DD,3E,05,AE), V(4D,E6,BD,46), \ - V(91,54,8D,B5), V(71,C4,5D,05), V(04,06,D4,6F), V(60,50,15,FF), \ - V(19,98,FB,24), V(D6,BD,E9,97), V(89,40,43,CC), V(67,D9,9E,77), \ - V(B0,E8,42,BD), V(07,89,8B,88), V(E7,19,5B,38), V(79,C8,EE,DB), \ - V(A1,7C,0A,47), V(7C,42,0F,E9), V(F8,84,1E,C9), V(00,00,00,00), \ - V(09,80,86,83), V(32,2B,ED,48), V(1E,11,70,AC), V(6C,5A,72,4E), \ - V(FD,0E,FF,FB), V(0F,85,38,56), V(3D,AE,D5,1E), V(36,2D,39,27), \ - V(0A,0F,D9,64), V(68,5C,A6,21), V(9B,5B,54,D1), V(24,36,2E,3A), \ - V(0C,0A,67,B1), V(93,57,E7,0F), V(B4,EE,96,D2), V(1B,9B,91,9E), \ - V(80,C0,C5,4F), V(61,DC,20,A2), V(5A,77,4B,69), V(1C,12,1A,16), \ - V(E2,93,BA,0A), V(C0,A0,2A,E5), V(3C,22,E0,43), V(12,1B,17,1D), \ - V(0E,09,0D,0B), V(F2,8B,C7,AD), V(2D,B6,A8,B9), V(14,1E,A9,C8), \ - V(57,F1,19,85), V(AF,75,07,4C), V(EE,99,DD,BB), V(A3,7F,60,FD), \ - V(F7,01,26,9F), V(5C,72,F5,BC), V(44,66,3B,C5), V(5B,FB,7E,34), \ - V(8B,43,29,76), V(CB,23,C6,DC), V(B6,ED,FC,68), V(B8,E4,F1,63), \ - V(D7,31,DC,CA), V(42,63,85,10), V(13,97,22,40), V(84,C6,11,20), \ - V(85,4A,24,7D), V(D2,BB,3D,F8), V(AE,F9,32,11), V(C7,29,A1,6D), \ - V(1D,9E,2F,4B), V(DC,B2,30,F3), V(0D,86,52,EC), V(77,C1,E3,D0), \ - V(2B,B3,16,6C), V(A9,70,B9,99), V(11,94,48,FA), V(47,E9,64,22), \ - V(A8,FC,8C,C4), V(A0,F0,3F,1A), V(56,7D,2C,D8), V(22,33,90,EF), \ - V(87,49,4E,C7), V(D9,38,D1,C1), V(8C,CA,A2,FE), V(98,D4,0B,36), \ - V(A6,F5,81,CF), V(A5,7A,DE,28), V(DA,B7,8E,26), V(3F,AD,BF,A4), \ - V(2C,3A,9D,E4), V(50,78,92,0D), V(6A,5F,CC,9B), V(54,7E,46,62), \ - V(F6,8D,13,C2), V(90,D8,B8,E8), V(2E,39,F7,5E), V(82,C3,AF,F5), \ - V(9F,5D,80,BE), V(69,D0,93,7C), V(6F,D5,2D,A9), V(CF,25,12,B3), \ - V(C8,AC,99,3B), V(10,18,7D,A7), V(E8,9C,63,6E), V(DB,3B,BB,7B), \ - V(CD,26,78,09), V(6E,59,18,F4), V(EC,9A,B7,01), V(83,4F,9A,A8), \ - V(E6,95,6E,65), V(AA,FF,E6,7E), V(21,BC,CF,08), V(EF,15,E8,E6), \ - V(BA,E7,9B,D9), V(4A,6F,36,CE), V(EA,9F,09,D4), V(29,B0,7C,D6), \ - V(31,A4,B2,AF), V(2A,3F,23,31), V(C6,A5,94,30), V(35,A2,66,C0), \ - V(74,4E,BC,37), V(FC,82,CA,A6), V(E0,90,D0,B0), V(33,A7,D8,15), \ - V(F1,04,98,4A), V(41,EC,DA,F7), V(7F,CD,50,0E), V(17,91,F6,2F), \ - V(76,4D,D6,8D), V(43,EF,B0,4D), V(CC,AA,4D,54), V(E4,96,04,DF), \ - V(9E,D1,B5,E3), V(4C,6A,88,1B), V(C1,2C,1F,B8), V(46,65,51,7F), \ - V(9D,5E,EA,04), V(01,8C,35,5D), V(FA,87,74,73), V(FB,0B,41,2E), \ - V(B3,67,1D,5A), V(92,DB,D2,52), V(E9,10,56,33), V(6D,D6,47,13), \ - V(9A,D7,61,8C), V(37,A1,0C,7A), V(59,F8,14,8E), V(EB,13,3C,89), \ - V(CE,A9,27,EE), V(B7,61,C9,35), V(E1,1C,E5,ED), V(7A,47,B1,3C), \ - V(9C,D2,DF,59), V(55,F2,73,3F), V(18,14,CE,79), V(73,C7,37,BF), \ - V(53,F7,CD,EA), V(5F,FD,AA,5B), V(DF,3D,6F,14), V(78,44,DB,86), \ - V(CA,AF,F3,81), V(B9,68,C4,3E), V(38,24,34,2C), V(C2,A3,40,5F), \ - V(16,1D,C3,72), V(BC,E2,25,0C), V(28,3C,49,8B), V(FF,0D,95,41), \ - V(39,A8,01,71), V(08,0C,B3,DE), V(D8,B4,E4,9C), V(64,56,C1,90), \ - V(7B,CB,84,61), V(D5,32,B6,70), V(48,6C,5C,74), V(D0,B8,57,42) - -#define V(a,b,c,d) 0x##a##b##c##d -static const uint32 RT0[256] = { RT }; -#undef V - -#define V(a,b,c,d) 0x##d##a##b##c -static const uint32 RT1[256] = { RT }; -#undef V - -#define V(a,b,c,d) 0x##c##d##a##b -static const uint32 RT2[256] = { RT }; -#undef V - -#define V(a,b,c,d) 0x##b##c##d##a -static const uint32 RT3[256] = { RT }; -#undef V - -#undef RT - -/* round constants */ - -static const uint32 RCON[10] = -{ - 0x01000000, 0x02000000, 0x04000000, 0x08000000, - 0x10000000, 0x20000000, 0x40000000, 0x80000000, - 0x1B000000, 0x36000000 -}; - -int do_init = 0; - -void aes_gen_tables( void ) -{ -} - -#endif - -/* platform-independant 32-bit integer manipulation macros */ - -#define GET_UINT32(n,b,i) \ -{ \ - (n) = ( (uint32) (b)[(i) ] << 24 ) \ - | ( (uint32) (b)[(i) + 1] << 16 ) \ - | ( (uint32) (b)[(i) + 2] << 8 ) \ - | ( (uint32) (b)[(i) + 3] ); \ -} - -#define PUT_UINT32(n,b,i) \ -{ \ - (b)[(i) ] = (uint8) ( (n) >> 24 ); \ - (b)[(i) + 1] = (uint8) ( (n) >> 16 ); \ - (b)[(i) + 2] = (uint8) ( (n) >> 8 ); \ - (b)[(i) + 3] = (uint8) ( (n) ); \ -} - -/* decryption key schedule tables */ - -int KT_init = 1; - -uint32 KT0[256]; -uint32 KT1[256]; -uint32 KT2[256]; -uint32 KT3[256]; - -/* AES key scheduling routine */ - -int aes_set_key( aes_context *ctx, uint8 *key, int nbits ) -{ - int i; - uint32 *RK, *SK; - - if( do_init ) - { - aes_gen_tables(); - - do_init = 0; - } - - switch( nbits ) - { - case 128: ctx->nr = 10; break; - case 192: ctx->nr = 12; break; - case 256: ctx->nr = 14; break; - default : return( 1 ); - } - - RK = ctx->erk; - - for( i = 0; i < (nbits >> 5); i++ ) - { - GET_UINT32( RK[i], key, i * 4 ); - } - - /* setup encryption round keys */ - - switch( nbits ) - { - case 128: - - for( i = 0; i < 10; i++, RK += 4 ) - { - RK[4] = RK[0] ^ RCON[i] ^ - ( FSb[ (uint8) ( RK[3] >> 16 ) ] << 24 ) ^ - ( FSb[ (uint8) ( RK[3] >> 8 ) ] << 16 ) ^ - ( FSb[ (uint8) ( RK[3] ) ] << 8 ) ^ - ( FSb[ (uint8) ( RK[3] >> 24 ) ] ); - - RK[5] = RK[1] ^ RK[4]; - RK[6] = RK[2] ^ RK[5]; - RK[7] = RK[3] ^ RK[6]; - } - break; - - case 192: - - for( i = 0; i < 8; i++, RK += 6 ) - { - RK[6] = RK[0] ^ RCON[i] ^ - ( FSb[ (uint8) ( RK[5] >> 16 ) ] << 24 ) ^ - ( FSb[ (uint8) ( RK[5] >> 8 ) ] << 16 ) ^ - ( FSb[ (uint8) ( RK[5] ) ] << 8 ) ^ - ( FSb[ (uint8) ( RK[5] >> 24 ) ] ); - - RK[7] = RK[1] ^ RK[6]; - RK[8] = RK[2] ^ RK[7]; - RK[9] = RK[3] ^ RK[8]; - RK[10] = RK[4] ^ RK[9]; - RK[11] = RK[5] ^ RK[10]; - } - break; - - case 256: - - for( i = 0; i < 7; i++, RK += 8 ) - { - RK[8] = RK[0] ^ RCON[i] ^ - ( FSb[ (uint8) ( RK[7] >> 16 ) ] << 24 ) ^ - ( FSb[ (uint8) ( RK[7] >> 8 ) ] << 16 ) ^ - ( FSb[ (uint8) ( RK[7] ) ] << 8 ) ^ - ( FSb[ (uint8) ( RK[7] >> 24 ) ] ); - - RK[9] = RK[1] ^ RK[8]; - RK[10] = RK[2] ^ RK[9]; - RK[11] = RK[3] ^ RK[10]; - - RK[12] = RK[4] ^ - ( FSb[ (uint8) ( RK[11] >> 24 ) ] << 24 ) ^ - ( FSb[ (uint8) ( RK[11] >> 16 ) ] << 16 ) ^ - ( FSb[ (uint8) ( RK[11] >> 8 ) ] << 8 ) ^ - ( FSb[ (uint8) ( RK[11] ) ] ); - - RK[13] = RK[5] ^ RK[12]; - RK[14] = RK[6] ^ RK[13]; - RK[15] = RK[7] ^ RK[14]; - } - break; - } - - /* setup decryption round keys */ - - if( KT_init ) - { - for( i = 0; i < 256; i++ ) - { - KT0[i] = RT0[ FSb[i] ]; - KT1[i] = RT1[ FSb[i] ]; - KT2[i] = RT2[ FSb[i] ]; - KT3[i] = RT3[ FSb[i] ]; - } - - KT_init = 0; - } - - SK = ctx->drk; - - *SK++ = *RK++; - *SK++ = *RK++; - *SK++ = *RK++; - *SK++ = *RK++; - - for( i = 1; i < ctx->nr; i++ ) - { - RK -= 8; - - *SK++ = KT0[ (uint8) ( *RK >> 24 ) ] ^ - KT1[ (uint8) ( *RK >> 16 ) ] ^ - KT2[ (uint8) ( *RK >> 8 ) ] ^ - KT3[ (uint8) ( *RK ) ]; RK++; - - *SK++ = KT0[ (uint8) ( *RK >> 24 ) ] ^ - KT1[ (uint8) ( *RK >> 16 ) ] ^ - KT2[ (uint8) ( *RK >> 8 ) ] ^ - KT3[ (uint8) ( *RK ) ]; RK++; - - *SK++ = KT0[ (uint8) ( *RK >> 24 ) ] ^ - KT1[ (uint8) ( *RK >> 16 ) ] ^ - KT2[ (uint8) ( *RK >> 8 ) ] ^ - KT3[ (uint8) ( *RK ) ]; RK++; - - *SK++ = KT0[ (uint8) ( *RK >> 24 ) ] ^ - KT1[ (uint8) ( *RK >> 16 ) ] ^ - KT2[ (uint8) ( *RK >> 8 ) ] ^ - KT3[ (uint8) ( *RK ) ]; RK++; - } - - RK -= 8; - - *SK++ = *RK++; - *SK++ = *RK++; - *SK++ = *RK++; - *SK++ = *RK++; - - return( 0 ); -} - -/* AES 128-bit block encryption routine */ - -void aes_encrypt( aes_context *ctx, uint8 input[16], uint8 output[16] ) -{ - uint32 *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3; - - RK = ctx->erk; - - GET_UINT32( X0, input, 0 ); X0 ^= RK[0]; - GET_UINT32( X1, input, 4 ); X1 ^= RK[1]; - GET_UINT32( X2, input, 8 ); X2 ^= RK[2]; - GET_UINT32( X3, input, 12 ); X3 ^= RK[3]; - -#define AES_FROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3) \ -{ \ - RK += 4; \ - \ - X0 = RK[0] ^ FT0[ (uint8) ( Y0 >> 24 ) ] ^ \ - FT1[ (uint8) ( Y1 >> 16 ) ] ^ \ - FT2[ (uint8) ( Y2 >> 8 ) ] ^ \ - FT3[ (uint8) ( Y3 ) ]; \ - \ - X1 = RK[1] ^ FT0[ (uint8) ( Y1 >> 24 ) ] ^ \ - FT1[ (uint8) ( Y2 >> 16 ) ] ^ \ - FT2[ (uint8) ( Y3 >> 8 ) ] ^ \ - FT3[ (uint8) ( Y0 ) ]; \ - \ - X2 = RK[2] ^ FT0[ (uint8) ( Y2 >> 24 ) ] ^ \ - FT1[ (uint8) ( Y3 >> 16 ) ] ^ \ - FT2[ (uint8) ( Y0 >> 8 ) ] ^ \ - FT3[ (uint8) ( Y1 ) ]; \ - \ - X3 = RK[3] ^ FT0[ (uint8) ( Y3 >> 24 ) ] ^ \ - FT1[ (uint8) ( Y0 >> 16 ) ] ^ \ - FT2[ (uint8) ( Y1 >> 8 ) ] ^ \ - FT3[ (uint8) ( Y2 ) ]; \ -} - - AES_FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 1 */ - AES_FROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 2 */ - AES_FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 3 */ - AES_FROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 4 */ - AES_FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 5 */ - AES_FROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 6 */ - AES_FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 7 */ - AES_FROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 8 */ - AES_FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 9 */ - - if( ctx->nr > 10 ) - { - AES_FROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 10 */ - AES_FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 11 */ - } - - if( ctx->nr > 12 ) - { - AES_FROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 12 */ - AES_FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 13 */ - } - - /* last round */ - - RK += 4; - - X0 = RK[0] ^ ( FSb[ (uint8) ( Y0 >> 24 ) ] << 24 ) ^ - ( FSb[ (uint8) ( Y1 >> 16 ) ] << 16 ) ^ - ( FSb[ (uint8) ( Y2 >> 8 ) ] << 8 ) ^ - ( FSb[ (uint8) ( Y3 ) ] ); - - X1 = RK[1] ^ ( FSb[ (uint8) ( Y1 >> 24 ) ] << 24 ) ^ - ( FSb[ (uint8) ( Y2 >> 16 ) ] << 16 ) ^ - ( FSb[ (uint8) ( Y3 >> 8 ) ] << 8 ) ^ - ( FSb[ (uint8) ( Y0 ) ] ); - - X2 = RK[2] ^ ( FSb[ (uint8) ( Y2 >> 24 ) ] << 24 ) ^ - ( FSb[ (uint8) ( Y3 >> 16 ) ] << 16 ) ^ - ( FSb[ (uint8) ( Y0 >> 8 ) ] << 8 ) ^ - ( FSb[ (uint8) ( Y1 ) ] ); - - X3 = RK[3] ^ ( FSb[ (uint8) ( Y3 >> 24 ) ] << 24 ) ^ - ( FSb[ (uint8) ( Y0 >> 16 ) ] << 16 ) ^ - ( FSb[ (uint8) ( Y1 >> 8 ) ] << 8 ) ^ - ( FSb[ (uint8) ( Y2 ) ] ); - - PUT_UINT32( X0, output, 0 ); - PUT_UINT32( X1, output, 4 ); - PUT_UINT32( X2, output, 8 ); - PUT_UINT32( X3, output, 12 ); -} - -/* AES 128-bit block decryption routine */ - -void aes_decrypt( aes_context *ctx, uint8 input[16], uint8 output[16] ) -{ - uint32 *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3; - - RK = ctx->drk; - - GET_UINT32( X0, input, 0 ); X0 ^= RK[0]; - GET_UINT32( X1, input, 4 ); X1 ^= RK[1]; - GET_UINT32( X2, input, 8 ); X2 ^= RK[2]; - GET_UINT32( X3, input, 12 ); X3 ^= RK[3]; - -#define AES_RROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3) \ -{ \ - RK += 4; \ - \ - X0 = RK[0] ^ RT0[ (uint8) ( Y0 >> 24 ) ] ^ \ - RT1[ (uint8) ( Y3 >> 16 ) ] ^ \ - RT2[ (uint8) ( Y2 >> 8 ) ] ^ \ - RT3[ (uint8) ( Y1 ) ]; \ - \ - X1 = RK[1] ^ RT0[ (uint8) ( Y1 >> 24 ) ] ^ \ - RT1[ (uint8) ( Y0 >> 16 ) ] ^ \ - RT2[ (uint8) ( Y3 >> 8 ) ] ^ \ - RT3[ (uint8) ( Y2 ) ]; \ - \ - X2 = RK[2] ^ RT0[ (uint8) ( Y2 >> 24 ) ] ^ \ - RT1[ (uint8) ( Y1 >> 16 ) ] ^ \ - RT2[ (uint8) ( Y0 >> 8 ) ] ^ \ - RT3[ (uint8) ( Y3 ) ]; \ - \ - X3 = RK[3] ^ RT0[ (uint8) ( Y3 >> 24 ) ] ^ \ - RT1[ (uint8) ( Y2 >> 16 ) ] ^ \ - RT2[ (uint8) ( Y1 >> 8 ) ] ^ \ - RT3[ (uint8) ( Y0 ) ]; \ -} - - AES_RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 1 */ - AES_RROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 2 */ - AES_RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 3 */ - AES_RROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 4 */ - AES_RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 5 */ - AES_RROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 6 */ - AES_RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 7 */ - AES_RROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 8 */ - AES_RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 9 */ - - if( ctx->nr > 10 ) - { - AES_RROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 10 */ - AES_RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 11 */ - } - - if( ctx->nr > 12 ) - { - AES_RROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 12 */ - AES_RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 13 */ - } - - /* last round */ - - RK += 4; - - X0 = RK[0] ^ ( RSb[ (uint8) ( Y0 >> 24 ) ] << 24 ) ^ - ( RSb[ (uint8) ( Y3 >> 16 ) ] << 16 ) ^ - ( RSb[ (uint8) ( Y2 >> 8 ) ] << 8 ) ^ - ( RSb[ (uint8) ( Y1 ) ] ); - - X1 = RK[1] ^ ( RSb[ (uint8) ( Y1 >> 24 ) ] << 24 ) ^ - ( RSb[ (uint8) ( Y0 >> 16 ) ] << 16 ) ^ - ( RSb[ (uint8) ( Y3 >> 8 ) ] << 8 ) ^ - ( RSb[ (uint8) ( Y2 ) ] ); - - X2 = RK[2] ^ ( RSb[ (uint8) ( Y2 >> 24 ) ] << 24 ) ^ - ( RSb[ (uint8) ( Y1 >> 16 ) ] << 16 ) ^ - ( RSb[ (uint8) ( Y0 >> 8 ) ] << 8 ) ^ - ( RSb[ (uint8) ( Y3 ) ] ); - - X3 = RK[3] ^ ( RSb[ (uint8) ( Y3 >> 24 ) ] << 24 ) ^ - ( RSb[ (uint8) ( Y2 >> 16 ) ] << 16 ) ^ - ( RSb[ (uint8) ( Y1 >> 8 ) ] << 8 ) ^ - ( RSb[ (uint8) ( Y0 ) ] ); - - PUT_UINT32( X0, output, 0 ); - PUT_UINT32( X1, output, 4 ); - PUT_UINT32( X2, output, 8 ); - PUT_UINT32( X3, output, 12 ); -} - diff --git a/protocols/Skype/src/aes/aes.cpp b/protocols/Skype/src/aes/aes.cpp new file mode 100644 index 0000000000..7e15f1547b --- /dev/null +++ b/protocols/Skype/src/aes/aes.cpp @@ -0,0 +1,765 @@ +/* + * FIPS-197 compliant AES implementation + * + * Copyright (C) 2001-2004 Christophe Devine + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "aes.h" + +/* uncomment the following line to run the test suite */ + +/* #define TEST */ + +/* uncomment the following line to use pre-computed tables */ +/* otherwise the tables will be generated at the first run */ + +/* #define FIXED_TABLES */ + +#ifndef FIXED_TABLES + +/* forward S-box & tables */ + +uint32 FSb[256]; +uint32 FT0[256]; +uint32 FT1[256]; +uint32 FT2[256]; +uint32 FT3[256]; + +/* reverse S-box & tables */ + +uint32 RSb[256]; +uint32 RT0[256]; +uint32 RT1[256]; +uint32 RT2[256]; +uint32 RT3[256]; + +/* round constants */ + +uint32 RCON[10]; + +/* tables generation flag */ + +int do_init = 1; + +/* tables generation routine */ + +#define ROTR8(x) ( ( ( x << 24 ) & 0xFFFFFFFF ) | \ + ( ( x & 0xFFFFFFFF ) >> 8 ) ) + +#define XTIME(x) ( ( x << 1 ) ^ ( ( x & 0x80 ) ? 0x1B : 0x00 ) ) +#define MUL(x,y) ( ( x && y ) ? pow[(log[x] + log[y]) % 255] : 0 ) + +void aes_gen_tables( void ) +{ + int i; + uint8 x, y; + uint8 pow[256]; + uint8 log[256]; + + /* compute pow and log tables over GF(2^8) */ + + for( i = 0, x = 1; i < 256; i++, x ^= XTIME( x ) ) + { + pow[i] = x; + log[x] = i; + } + + /* calculate the round constants */ + + for( i = 0, x = 1; i < 10; i++, x = XTIME( x ) ) + { + RCON[i] = (uint32) x << 24; + } + + /* generate the forward and reverse S-boxes */ + + FSb[0x00] = 0x63; + RSb[0x63] = 0x00; + + for( i = 1; i < 256; i++ ) + { + x = pow[255 - log[i]]; + + y = x; y = ( y << 1 ) | ( y >> 7 ); + x ^= y; y = ( y << 1 ) | ( y >> 7 ); + x ^= y; y = ( y << 1 ) | ( y >> 7 ); + x ^= y; y = ( y << 1 ) | ( y >> 7 ); + x ^= y ^ 0x63; + + FSb[i] = x; + RSb[x] = i; + } + + /* generate the forward and reverse tables */ + + for( i = 0; i < 256; i++ ) + { + x = (unsigned char) FSb[i]; y = XTIME( x ); + + FT0[i] = (uint32) ( x ^ y ) ^ + ( (uint32) x << 8 ) ^ + ( (uint32) x << 16 ) ^ + ( (uint32) y << 24 ); + + FT0[i] &= 0xFFFFFFFF; + + FT1[i] = ROTR8( FT0[i] ); + FT2[i] = ROTR8( FT1[i] ); + FT3[i] = ROTR8( FT2[i] ); + + y = (unsigned char) RSb[i]; + + RT0[i] = ( (uint32) MUL( 0x0B, y ) ) ^ + ( (uint32) MUL( 0x0D, y ) << 8 ) ^ + ( (uint32) MUL( 0x09, y ) << 16 ) ^ + ( (uint32) MUL( 0x0E, y ) << 24 ); + + RT0[i] &= 0xFFFFFFFF; + + RT1[i] = ROTR8( RT0[i] ); + RT2[i] = ROTR8( RT1[i] ); + RT3[i] = ROTR8( RT2[i] ); + } +} + +#else + +/* forward S-box */ + +static const uint32 FSb[256] = +{ + 0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, + 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76, + 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, + 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0, + 0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, + 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15, + 0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, + 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75, + 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, + 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84, + 0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B, + 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF, + 0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, + 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8, + 0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, + 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2, + 0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17, + 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73, + 0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, + 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB, + 0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, + 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79, + 0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9, + 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08, + 0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, + 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A, + 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, + 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E, + 0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94, + 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF, + 0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, + 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16 +}; + +/* forward tables */ + +#define FT \ +\ + V(C6,63,63,A5), V(F8,7C,7C,84), V(EE,77,77,99), V(F6,7B,7B,8D), \ + V(FF,F2,F2,0D), V(D6,6B,6B,BD), V(DE,6F,6F,B1), V(91,C5,C5,54), \ + V(60,30,30,50), V(02,01,01,03), V(CE,67,67,A9), V(56,2B,2B,7D), \ + V(E7,FE,FE,19), V(B5,D7,D7,62), V(4D,AB,AB,E6), V(EC,76,76,9A), \ + V(8F,CA,CA,45), V(1F,82,82,9D), V(89,C9,C9,40), V(FA,7D,7D,87), \ + V(EF,FA,FA,15), V(B2,59,59,EB), V(8E,47,47,C9), V(FB,F0,F0,0B), \ + V(41,AD,AD,EC), V(B3,D4,D4,67), V(5F,A2,A2,FD), V(45,AF,AF,EA), \ + V(23,9C,9C,BF), V(53,A4,A4,F7), V(E4,72,72,96), V(9B,C0,C0,5B), \ + V(75,B7,B7,C2), V(E1,FD,FD,1C), V(3D,93,93,AE), V(4C,26,26,6A), \ + V(6C,36,36,5A), V(7E,3F,3F,41), V(F5,F7,F7,02), V(83,CC,CC,4F), \ + V(68,34,34,5C), V(51,A5,A5,F4), V(D1,E5,E5,34), V(F9,F1,F1,08), \ + V(E2,71,71,93), V(AB,D8,D8,73), V(62,31,31,53), V(2A,15,15,3F), \ + V(08,04,04,0C), V(95,C7,C7,52), V(46,23,23,65), V(9D,C3,C3,5E), \ + V(30,18,18,28), V(37,96,96,A1), V(0A,05,05,0F), V(2F,9A,9A,B5), \ + V(0E,07,07,09), V(24,12,12,36), V(1B,80,80,9B), V(DF,E2,E2,3D), \ + V(CD,EB,EB,26), V(4E,27,27,69), V(7F,B2,B2,CD), V(EA,75,75,9F), \ + V(12,09,09,1B), V(1D,83,83,9E), V(58,2C,2C,74), V(34,1A,1A,2E), \ + V(36,1B,1B,2D), V(DC,6E,6E,B2), V(B4,5A,5A,EE), V(5B,A0,A0,FB), \ + V(A4,52,52,F6), V(76,3B,3B,4D), V(B7,D6,D6,61), V(7D,B3,B3,CE), \ + V(52,29,29,7B), V(DD,E3,E3,3E), V(5E,2F,2F,71), V(13,84,84,97), \ + V(A6,53,53,F5), V(B9,D1,D1,68), V(00,00,00,00), V(C1,ED,ED,2C), \ + V(40,20,20,60), V(E3,FC,FC,1F), V(79,B1,B1,C8), V(B6,5B,5B,ED), \ + V(D4,6A,6A,BE), V(8D,CB,CB,46), V(67,BE,BE,D9), V(72,39,39,4B), \ + V(94,4A,4A,DE), V(98,4C,4C,D4), V(B0,58,58,E8), V(85,CF,CF,4A), \ + V(BB,D0,D0,6B), V(C5,EF,EF,2A), V(4F,AA,AA,E5), V(ED,FB,FB,16), \ + V(86,43,43,C5), V(9A,4D,4D,D7), V(66,33,33,55), V(11,85,85,94), \ + V(8A,45,45,CF), V(E9,F9,F9,10), V(04,02,02,06), V(FE,7F,7F,81), \ + V(A0,50,50,F0), V(78,3C,3C,44), V(25,9F,9F,BA), V(4B,A8,A8,E3), \ + V(A2,51,51,F3), V(5D,A3,A3,FE), V(80,40,40,C0), V(05,8F,8F,8A), \ + V(3F,92,92,AD), V(21,9D,9D,BC), V(70,38,38,48), V(F1,F5,F5,04), \ + V(63,BC,BC,DF), V(77,B6,B6,C1), V(AF,DA,DA,75), V(42,21,21,63), \ + V(20,10,10,30), V(E5,FF,FF,1A), V(FD,F3,F3,0E), V(BF,D2,D2,6D), \ + V(81,CD,CD,4C), V(18,0C,0C,14), V(26,13,13,35), V(C3,EC,EC,2F), \ + V(BE,5F,5F,E1), V(35,97,97,A2), V(88,44,44,CC), V(2E,17,17,39), \ + V(93,C4,C4,57), V(55,A7,A7,F2), V(FC,7E,7E,82), V(7A,3D,3D,47), \ + V(C8,64,64,AC), V(BA,5D,5D,E7), V(32,19,19,2B), V(E6,73,73,95), \ + V(C0,60,60,A0), V(19,81,81,98), V(9E,4F,4F,D1), V(A3,DC,DC,7F), \ + V(44,22,22,66), V(54,2A,2A,7E), V(3B,90,90,AB), V(0B,88,88,83), \ + V(8C,46,46,CA), V(C7,EE,EE,29), V(6B,B8,B8,D3), V(28,14,14,3C), \ + V(A7,DE,DE,79), V(BC,5E,5E,E2), V(16,0B,0B,1D), V(AD,DB,DB,76), \ + V(DB,E0,E0,3B), V(64,32,32,56), V(74,3A,3A,4E), V(14,0A,0A,1E), \ + V(92,49,49,DB), V(0C,06,06,0A), V(48,24,24,6C), V(B8,5C,5C,E4), \ + V(9F,C2,C2,5D), V(BD,D3,D3,6E), V(43,AC,AC,EF), V(C4,62,62,A6), \ + V(39,91,91,A8), V(31,95,95,A4), V(D3,E4,E4,37), V(F2,79,79,8B), \ + V(D5,E7,E7,32), V(8B,C8,C8,43), V(6E,37,37,59), V(DA,6D,6D,B7), \ + V(01,8D,8D,8C), V(B1,D5,D5,64), V(9C,4E,4E,D2), V(49,A9,A9,E0), \ + V(D8,6C,6C,B4), V(AC,56,56,FA), V(F3,F4,F4,07), V(CF,EA,EA,25), \ + V(CA,65,65,AF), V(F4,7A,7A,8E), V(47,AE,AE,E9), V(10,08,08,18), \ + V(6F,BA,BA,D5), V(F0,78,78,88), V(4A,25,25,6F), V(5C,2E,2E,72), \ + V(38,1C,1C,24), V(57,A6,A6,F1), V(73,B4,B4,C7), V(97,C6,C6,51), \ + V(CB,E8,E8,23), V(A1,DD,DD,7C), V(E8,74,74,9C), V(3E,1F,1F,21), \ + V(96,4B,4B,DD), V(61,BD,BD,DC), V(0D,8B,8B,86), V(0F,8A,8A,85), \ + V(E0,70,70,90), V(7C,3E,3E,42), V(71,B5,B5,C4), V(CC,66,66,AA), \ + V(90,48,48,D8), V(06,03,03,05), V(F7,F6,F6,01), V(1C,0E,0E,12), \ + V(C2,61,61,A3), V(6A,35,35,5F), V(AE,57,57,F9), V(69,B9,B9,D0), \ + V(17,86,86,91), V(99,C1,C1,58), V(3A,1D,1D,27), V(27,9E,9E,B9), \ + V(D9,E1,E1,38), V(EB,F8,F8,13), V(2B,98,98,B3), V(22,11,11,33), \ + V(D2,69,69,BB), V(A9,D9,D9,70), V(07,8E,8E,89), V(33,94,94,A7), \ + V(2D,9B,9B,B6), V(3C,1E,1E,22), V(15,87,87,92), V(C9,E9,E9,20), \ + V(87,CE,CE,49), V(AA,55,55,FF), V(50,28,28,78), V(A5,DF,DF,7A), \ + V(03,8C,8C,8F), V(59,A1,A1,F8), V(09,89,89,80), V(1A,0D,0D,17), \ + V(65,BF,BF,DA), V(D7,E6,E6,31), V(84,42,42,C6), V(D0,68,68,B8), \ + V(82,41,41,C3), V(29,99,99,B0), V(5A,2D,2D,77), V(1E,0F,0F,11), \ + V(7B,B0,B0,CB), V(A8,54,54,FC), V(6D,BB,BB,D6), V(2C,16,16,3A) + +#define V(a,b,c,d) 0x##a##b##c##d +static const uint32 FT0[256] = { FT }; +#undef V + +#define V(a,b,c,d) 0x##d##a##b##c +static const uint32 FT1[256] = { FT }; +#undef V + +#define V(a,b,c,d) 0x##c##d##a##b +static const uint32 FT2[256] = { FT }; +#undef V + +#define V(a,b,c,d) 0x##b##c##d##a +static const uint32 FT3[256] = { FT }; +#undef V + +#undef FT + +/* reverse S-box */ + +static const uint32 RSb[256] = +{ + 0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38, + 0xBF, 0x40, 0xA3, 0x9E, 0x81, 0xF3, 0xD7, 0xFB, + 0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87, + 0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB, + 0x54, 0x7B, 0x94, 0x32, 0xA6, 0xC2, 0x23, 0x3D, + 0xEE, 0x4C, 0x95, 0x0B, 0x42, 0xFA, 0xC3, 0x4E, + 0x08, 0x2E, 0xA1, 0x66, 0x28, 0xD9, 0x24, 0xB2, + 0x76, 0x5B, 0xA2, 0x49, 0x6D, 0x8B, 0xD1, 0x25, + 0x72, 0xF8, 0xF6, 0x64, 0x86, 0x68, 0x98, 0x16, + 0xD4, 0xA4, 0x5C, 0xCC, 0x5D, 0x65, 0xB6, 0x92, + 0x6C, 0x70, 0x48, 0x50, 0xFD, 0xED, 0xB9, 0xDA, + 0x5E, 0x15, 0x46, 0x57, 0xA7, 0x8D, 0x9D, 0x84, + 0x90, 0xD8, 0xAB, 0x00, 0x8C, 0xBC, 0xD3, 0x0A, + 0xF7, 0xE4, 0x58, 0x05, 0xB8, 0xB3, 0x45, 0x06, + 0xD0, 0x2C, 0x1E, 0x8F, 0xCA, 0x3F, 0x0F, 0x02, + 0xC1, 0xAF, 0xBD, 0x03, 0x01, 0x13, 0x8A, 0x6B, + 0x3A, 0x91, 0x11, 0x41, 0x4F, 0x67, 0xDC, 0xEA, + 0x97, 0xF2, 0xCF, 0xCE, 0xF0, 0xB4, 0xE6, 0x73, + 0x96, 0xAC, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85, + 0xE2, 0xF9, 0x37, 0xE8, 0x1C, 0x75, 0xDF, 0x6E, + 0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89, + 0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B, + 0xFC, 0x56, 0x3E, 0x4B, 0xC6, 0xD2, 0x79, 0x20, + 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4, + 0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31, + 0xB1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xEC, 0x5F, + 0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D, + 0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF, + 0xA0, 0xE0, 0x3B, 0x4D, 0xAE, 0x2A, 0xF5, 0xB0, + 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61, + 0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, + 0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C, 0x7D +}; + +/* reverse tables */ + +#define RT \ +\ + V(51,F4,A7,50), V(7E,41,65,53), V(1A,17,A4,C3), V(3A,27,5E,96), \ + V(3B,AB,6B,CB), V(1F,9D,45,F1), V(AC,FA,58,AB), V(4B,E3,03,93), \ + V(20,30,FA,55), V(AD,76,6D,F6), V(88,CC,76,91), V(F5,02,4C,25), \ + V(4F,E5,D7,FC), V(C5,2A,CB,D7), V(26,35,44,80), V(B5,62,A3,8F), \ + V(DE,B1,5A,49), V(25,BA,1B,67), V(45,EA,0E,98), V(5D,FE,C0,E1), \ + V(C3,2F,75,02), V(81,4C,F0,12), V(8D,46,97,A3), V(6B,D3,F9,C6), \ + V(03,8F,5F,E7), V(15,92,9C,95), V(BF,6D,7A,EB), V(95,52,59,DA), \ + V(D4,BE,83,2D), V(58,74,21,D3), V(49,E0,69,29), V(8E,C9,C8,44), \ + V(75,C2,89,6A), V(F4,8E,79,78), V(99,58,3E,6B), V(27,B9,71,DD), \ + V(BE,E1,4F,B6), V(F0,88,AD,17), V(C9,20,AC,66), V(7D,CE,3A,B4), \ + V(63,DF,4A,18), V(E5,1A,31,82), V(97,51,33,60), V(62,53,7F,45), \ + V(B1,64,77,E0), V(BB,6B,AE,84), V(FE,81,A0,1C), V(F9,08,2B,94), \ + V(70,48,68,58), V(8F,45,FD,19), V(94,DE,6C,87), V(52,7B,F8,B7), \ + V(AB,73,D3,23), V(72,4B,02,E2), V(E3,1F,8F,57), V(66,55,AB,2A), \ + V(B2,EB,28,07), V(2F,B5,C2,03), V(86,C5,7B,9A), V(D3,37,08,A5), \ + V(30,28,87,F2), V(23,BF,A5,B2), V(02,03,6A,BA), V(ED,16,82,5C), \ + V(8A,CF,1C,2B), V(A7,79,B4,92), V(F3,07,F2,F0), V(4E,69,E2,A1), \ + V(65,DA,F4,CD), V(06,05,BE,D5), V(D1,34,62,1F), V(C4,A6,FE,8A), \ + V(34,2E,53,9D), V(A2,F3,55,A0), V(05,8A,E1,32), V(A4,F6,EB,75), \ + V(0B,83,EC,39), V(40,60,EF,AA), V(5E,71,9F,06), V(BD,6E,10,51), \ + V(3E,21,8A,F9), V(96,DD,06,3D), V(DD,3E,05,AE), V(4D,E6,BD,46), \ + V(91,54,8D,B5), V(71,C4,5D,05), V(04,06,D4,6F), V(60,50,15,FF), \ + V(19,98,FB,24), V(D6,BD,E9,97), V(89,40,43,CC), V(67,D9,9E,77), \ + V(B0,E8,42,BD), V(07,89,8B,88), V(E7,19,5B,38), V(79,C8,EE,DB), \ + V(A1,7C,0A,47), V(7C,42,0F,E9), V(F8,84,1E,C9), V(00,00,00,00), \ + V(09,80,86,83), V(32,2B,ED,48), V(1E,11,70,AC), V(6C,5A,72,4E), \ + V(FD,0E,FF,FB), V(0F,85,38,56), V(3D,AE,D5,1E), V(36,2D,39,27), \ + V(0A,0F,D9,64), V(68,5C,A6,21), V(9B,5B,54,D1), V(24,36,2E,3A), \ + V(0C,0A,67,B1), V(93,57,E7,0F), V(B4,EE,96,D2), V(1B,9B,91,9E), \ + V(80,C0,C5,4F), V(61,DC,20,A2), V(5A,77,4B,69), V(1C,12,1A,16), \ + V(E2,93,BA,0A), V(C0,A0,2A,E5), V(3C,22,E0,43), V(12,1B,17,1D), \ + V(0E,09,0D,0B), V(F2,8B,C7,AD), V(2D,B6,A8,B9), V(14,1E,A9,C8), \ + V(57,F1,19,85), V(AF,75,07,4C), V(EE,99,DD,BB), V(A3,7F,60,FD), \ + V(F7,01,26,9F), V(5C,72,F5,BC), V(44,66,3B,C5), V(5B,FB,7E,34), \ + V(8B,43,29,76), V(CB,23,C6,DC), V(B6,ED,FC,68), V(B8,E4,F1,63), \ + V(D7,31,DC,CA), V(42,63,85,10), V(13,97,22,40), V(84,C6,11,20), \ + V(85,4A,24,7D), V(D2,BB,3D,F8), V(AE,F9,32,11), V(C7,29,A1,6D), \ + V(1D,9E,2F,4B), V(DC,B2,30,F3), V(0D,86,52,EC), V(77,C1,E3,D0), \ + V(2B,B3,16,6C), V(A9,70,B9,99), V(11,94,48,FA), V(47,E9,64,22), \ + V(A8,FC,8C,C4), V(A0,F0,3F,1A), V(56,7D,2C,D8), V(22,33,90,EF), \ + V(87,49,4E,C7), V(D9,38,D1,C1), V(8C,CA,A2,FE), V(98,D4,0B,36), \ + V(A6,F5,81,CF), V(A5,7A,DE,28), V(DA,B7,8E,26), V(3F,AD,BF,A4), \ + V(2C,3A,9D,E4), V(50,78,92,0D), V(6A,5F,CC,9B), V(54,7E,46,62), \ + V(F6,8D,13,C2), V(90,D8,B8,E8), V(2E,39,F7,5E), V(82,C3,AF,F5), \ + V(9F,5D,80,BE), V(69,D0,93,7C), V(6F,D5,2D,A9), V(CF,25,12,B3), \ + V(C8,AC,99,3B), V(10,18,7D,A7), V(E8,9C,63,6E), V(DB,3B,BB,7B), \ + V(CD,26,78,09), V(6E,59,18,F4), V(EC,9A,B7,01), V(83,4F,9A,A8), \ + V(E6,95,6E,65), V(AA,FF,E6,7E), V(21,BC,CF,08), V(EF,15,E8,E6), \ + V(BA,E7,9B,D9), V(4A,6F,36,CE), V(EA,9F,09,D4), V(29,B0,7C,D6), \ + V(31,A4,B2,AF), V(2A,3F,23,31), V(C6,A5,94,30), V(35,A2,66,C0), \ + V(74,4E,BC,37), V(FC,82,CA,A6), V(E0,90,D0,B0), V(33,A7,D8,15), \ + V(F1,04,98,4A), V(41,EC,DA,F7), V(7F,CD,50,0E), V(17,91,F6,2F), \ + V(76,4D,D6,8D), V(43,EF,B0,4D), V(CC,AA,4D,54), V(E4,96,04,DF), \ + V(9E,D1,B5,E3), V(4C,6A,88,1B), V(C1,2C,1F,B8), V(46,65,51,7F), \ + V(9D,5E,EA,04), V(01,8C,35,5D), V(FA,87,74,73), V(FB,0B,41,2E), \ + V(B3,67,1D,5A), V(92,DB,D2,52), V(E9,10,56,33), V(6D,D6,47,13), \ + V(9A,D7,61,8C), V(37,A1,0C,7A), V(59,F8,14,8E), V(EB,13,3C,89), \ + V(CE,A9,27,EE), V(B7,61,C9,35), V(E1,1C,E5,ED), V(7A,47,B1,3C), \ + V(9C,D2,DF,59), V(55,F2,73,3F), V(18,14,CE,79), V(73,C7,37,BF), \ + V(53,F7,CD,EA), V(5F,FD,AA,5B), V(DF,3D,6F,14), V(78,44,DB,86), \ + V(CA,AF,F3,81), V(B9,68,C4,3E), V(38,24,34,2C), V(C2,A3,40,5F), \ + V(16,1D,C3,72), V(BC,E2,25,0C), V(28,3C,49,8B), V(FF,0D,95,41), \ + V(39,A8,01,71), V(08,0C,B3,DE), V(D8,B4,E4,9C), V(64,56,C1,90), \ + V(7B,CB,84,61), V(D5,32,B6,70), V(48,6C,5C,74), V(D0,B8,57,42) + +#define V(a,b,c,d) 0x##a##b##c##d +static const uint32 RT0[256] = { RT }; +#undef V + +#define V(a,b,c,d) 0x##d##a##b##c +static const uint32 RT1[256] = { RT }; +#undef V + +#define V(a,b,c,d) 0x##c##d##a##b +static const uint32 RT2[256] = { RT }; +#undef V + +#define V(a,b,c,d) 0x##b##c##d##a +static const uint32 RT3[256] = { RT }; +#undef V + +#undef RT + +/* round constants */ + +static const uint32 RCON[10] = +{ + 0x01000000, 0x02000000, 0x04000000, 0x08000000, + 0x10000000, 0x20000000, 0x40000000, 0x80000000, + 0x1B000000, 0x36000000 +}; + +int do_init = 0; + +void aes_gen_tables( void ) +{ +} + +#endif + +/* platform-independant 32-bit integer manipulation macros */ + +#define GET_UINT32(n,b,i) \ +{ \ + (n) = ( (uint32) (b)[(i) ] << 24 ) \ + | ( (uint32) (b)[(i) + 1] << 16 ) \ + | ( (uint32) (b)[(i) + 2] << 8 ) \ + | ( (uint32) (b)[(i) + 3] ); \ +} + +#define PUT_UINT32(n,b,i) \ +{ \ + (b)[(i) ] = (uint8) ( (n) >> 24 ); \ + (b)[(i) + 1] = (uint8) ( (n) >> 16 ); \ + (b)[(i) + 2] = (uint8) ( (n) >> 8 ); \ + (b)[(i) + 3] = (uint8) ( (n) ); \ +} + +/* decryption key schedule tables */ + +int KT_init = 1; + +uint32 KT0[256]; +uint32 KT1[256]; +uint32 KT2[256]; +uint32 KT3[256]; + +/* AES key scheduling routine */ + +int aes_set_key( aes_context *ctx, uint8 *key, int nbits ) +{ + int i; + uint32 *RK, *SK; + + if( do_init ) + { + aes_gen_tables(); + + do_init = 0; + } + + switch( nbits ) + { + case 128: ctx->nr = 10; break; + case 192: ctx->nr = 12; break; + case 256: ctx->nr = 14; break; + default : return( 1 ); + } + + RK = ctx->erk; + + for( i = 0; i < (nbits >> 5); i++ ) + { + GET_UINT32( RK[i], key, i * 4 ); + } + + /* setup encryption round keys */ + + switch( nbits ) + { + case 128: + + for( i = 0; i < 10; i++, RK += 4 ) + { + RK[4] = RK[0] ^ RCON[i] ^ + ( FSb[ (uint8) ( RK[3] >> 16 ) ] << 24 ) ^ + ( FSb[ (uint8) ( RK[3] >> 8 ) ] << 16 ) ^ + ( FSb[ (uint8) ( RK[3] ) ] << 8 ) ^ + ( FSb[ (uint8) ( RK[3] >> 24 ) ] ); + + RK[5] = RK[1] ^ RK[4]; + RK[6] = RK[2] ^ RK[5]; + RK[7] = RK[3] ^ RK[6]; + } + break; + + case 192: + + for( i = 0; i < 8; i++, RK += 6 ) + { + RK[6] = RK[0] ^ RCON[i] ^ + ( FSb[ (uint8) ( RK[5] >> 16 ) ] << 24 ) ^ + ( FSb[ (uint8) ( RK[5] >> 8 ) ] << 16 ) ^ + ( FSb[ (uint8) ( RK[5] ) ] << 8 ) ^ + ( FSb[ (uint8) ( RK[5] >> 24 ) ] ); + + RK[7] = RK[1] ^ RK[6]; + RK[8] = RK[2] ^ RK[7]; + RK[9] = RK[3] ^ RK[8]; + RK[10] = RK[4] ^ RK[9]; + RK[11] = RK[5] ^ RK[10]; + } + break; + + case 256: + + for( i = 0; i < 7; i++, RK += 8 ) + { + RK[8] = RK[0] ^ RCON[i] ^ + ( FSb[ (uint8) ( RK[7] >> 16 ) ] << 24 ) ^ + ( FSb[ (uint8) ( RK[7] >> 8 ) ] << 16 ) ^ + ( FSb[ (uint8) ( RK[7] ) ] << 8 ) ^ + ( FSb[ (uint8) ( RK[7] >> 24 ) ] ); + + RK[9] = RK[1] ^ RK[8]; + RK[10] = RK[2] ^ RK[9]; + RK[11] = RK[3] ^ RK[10]; + + RK[12] = RK[4] ^ + ( FSb[ (uint8) ( RK[11] >> 24 ) ] << 24 ) ^ + ( FSb[ (uint8) ( RK[11] >> 16 ) ] << 16 ) ^ + ( FSb[ (uint8) ( RK[11] >> 8 ) ] << 8 ) ^ + ( FSb[ (uint8) ( RK[11] ) ] ); + + RK[13] = RK[5] ^ RK[12]; + RK[14] = RK[6] ^ RK[13]; + RK[15] = RK[7] ^ RK[14]; + } + break; + } + + /* setup decryption round keys */ + + if( KT_init ) + { + for( i = 0; i < 256; i++ ) + { + KT0[i] = RT0[ FSb[i] ]; + KT1[i] = RT1[ FSb[i] ]; + KT2[i] = RT2[ FSb[i] ]; + KT3[i] = RT3[ FSb[i] ]; + } + + KT_init = 0; + } + + SK = ctx->drk; + + *SK++ = *RK++; + *SK++ = *RK++; + *SK++ = *RK++; + *SK++ = *RK++; + + for( i = 1; i < ctx->nr; i++ ) + { + RK -= 8; + + *SK++ = KT0[ (uint8) ( *RK >> 24 ) ] ^ + KT1[ (uint8) ( *RK >> 16 ) ] ^ + KT2[ (uint8) ( *RK >> 8 ) ] ^ + KT3[ (uint8) ( *RK ) ]; RK++; + + *SK++ = KT0[ (uint8) ( *RK >> 24 ) ] ^ + KT1[ (uint8) ( *RK >> 16 ) ] ^ + KT2[ (uint8) ( *RK >> 8 ) ] ^ + KT3[ (uint8) ( *RK ) ]; RK++; + + *SK++ = KT0[ (uint8) ( *RK >> 24 ) ] ^ + KT1[ (uint8) ( *RK >> 16 ) ] ^ + KT2[ (uint8) ( *RK >> 8 ) ] ^ + KT3[ (uint8) ( *RK ) ]; RK++; + + *SK++ = KT0[ (uint8) ( *RK >> 24 ) ] ^ + KT1[ (uint8) ( *RK >> 16 ) ] ^ + KT2[ (uint8) ( *RK >> 8 ) ] ^ + KT3[ (uint8) ( *RK ) ]; RK++; + } + + RK -= 8; + + *SK++ = *RK++; + *SK++ = *RK++; + *SK++ = *RK++; + *SK++ = *RK++; + + return( 0 ); +} + +/* AES 128-bit block encryption routine */ + +void aes_encrypt( aes_context *ctx, uint8 input[16], uint8 output[16] ) +{ + uint32 *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3; + + RK = ctx->erk; + + GET_UINT32( X0, input, 0 ); X0 ^= RK[0]; + GET_UINT32( X1, input, 4 ); X1 ^= RK[1]; + GET_UINT32( X2, input, 8 ); X2 ^= RK[2]; + GET_UINT32( X3, input, 12 ); X3 ^= RK[3]; + +#define AES_FROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3) \ +{ \ + RK += 4; \ + \ + X0 = RK[0] ^ FT0[ (uint8) ( Y0 >> 24 ) ] ^ \ + FT1[ (uint8) ( Y1 >> 16 ) ] ^ \ + FT2[ (uint8) ( Y2 >> 8 ) ] ^ \ + FT3[ (uint8) ( Y3 ) ]; \ + \ + X1 = RK[1] ^ FT0[ (uint8) ( Y1 >> 24 ) ] ^ \ + FT1[ (uint8) ( Y2 >> 16 ) ] ^ \ + FT2[ (uint8) ( Y3 >> 8 ) ] ^ \ + FT3[ (uint8) ( Y0 ) ]; \ + \ + X2 = RK[2] ^ FT0[ (uint8) ( Y2 >> 24 ) ] ^ \ + FT1[ (uint8) ( Y3 >> 16 ) ] ^ \ + FT2[ (uint8) ( Y0 >> 8 ) ] ^ \ + FT3[ (uint8) ( Y1 ) ]; \ + \ + X3 = RK[3] ^ FT0[ (uint8) ( Y3 >> 24 ) ] ^ \ + FT1[ (uint8) ( Y0 >> 16 ) ] ^ \ + FT2[ (uint8) ( Y1 >> 8 ) ] ^ \ + FT3[ (uint8) ( Y2 ) ]; \ +} + + AES_FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 1 */ + AES_FROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 2 */ + AES_FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 3 */ + AES_FROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 4 */ + AES_FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 5 */ + AES_FROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 6 */ + AES_FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 7 */ + AES_FROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 8 */ + AES_FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 9 */ + + if( ctx->nr > 10 ) + { + AES_FROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 10 */ + AES_FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 11 */ + } + + if( ctx->nr > 12 ) + { + AES_FROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 12 */ + AES_FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 13 */ + } + + /* last round */ + + RK += 4; + + X0 = RK[0] ^ ( FSb[ (uint8) ( Y0 >> 24 ) ] << 24 ) ^ + ( FSb[ (uint8) ( Y1 >> 16 ) ] << 16 ) ^ + ( FSb[ (uint8) ( Y2 >> 8 ) ] << 8 ) ^ + ( FSb[ (uint8) ( Y3 ) ] ); + + X1 = RK[1] ^ ( FSb[ (uint8) ( Y1 >> 24 ) ] << 24 ) ^ + ( FSb[ (uint8) ( Y2 >> 16 ) ] << 16 ) ^ + ( FSb[ (uint8) ( Y3 >> 8 ) ] << 8 ) ^ + ( FSb[ (uint8) ( Y0 ) ] ); + + X2 = RK[2] ^ ( FSb[ (uint8) ( Y2 >> 24 ) ] << 24 ) ^ + ( FSb[ (uint8) ( Y3 >> 16 ) ] << 16 ) ^ + ( FSb[ (uint8) ( Y0 >> 8 ) ] << 8 ) ^ + ( FSb[ (uint8) ( Y1 ) ] ); + + X3 = RK[3] ^ ( FSb[ (uint8) ( Y3 >> 24 ) ] << 24 ) ^ + ( FSb[ (uint8) ( Y0 >> 16 ) ] << 16 ) ^ + ( FSb[ (uint8) ( Y1 >> 8 ) ] << 8 ) ^ + ( FSb[ (uint8) ( Y2 ) ] ); + + PUT_UINT32( X0, output, 0 ); + PUT_UINT32( X1, output, 4 ); + PUT_UINT32( X2, output, 8 ); + PUT_UINT32( X3, output, 12 ); +} + +/* AES 128-bit block decryption routine */ + +void aes_decrypt( aes_context *ctx, uint8 input[16], uint8 output[16] ) +{ + uint32 *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3; + + RK = ctx->drk; + + GET_UINT32( X0, input, 0 ); X0 ^= RK[0]; + GET_UINT32( X1, input, 4 ); X1 ^= RK[1]; + GET_UINT32( X2, input, 8 ); X2 ^= RK[2]; + GET_UINT32( X3, input, 12 ); X3 ^= RK[3]; + +#define AES_RROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3) \ +{ \ + RK += 4; \ + \ + X0 = RK[0] ^ RT0[ (uint8) ( Y0 >> 24 ) ] ^ \ + RT1[ (uint8) ( Y3 >> 16 ) ] ^ \ + RT2[ (uint8) ( Y2 >> 8 ) ] ^ \ + RT3[ (uint8) ( Y1 ) ]; \ + \ + X1 = RK[1] ^ RT0[ (uint8) ( Y1 >> 24 ) ] ^ \ + RT1[ (uint8) ( Y0 >> 16 ) ] ^ \ + RT2[ (uint8) ( Y3 >> 8 ) ] ^ \ + RT3[ (uint8) ( Y2 ) ]; \ + \ + X2 = RK[2] ^ RT0[ (uint8) ( Y2 >> 24 ) ] ^ \ + RT1[ (uint8) ( Y1 >> 16 ) ] ^ \ + RT2[ (uint8) ( Y0 >> 8 ) ] ^ \ + RT3[ (uint8) ( Y3 ) ]; \ + \ + X3 = RK[3] ^ RT0[ (uint8) ( Y3 >> 24 ) ] ^ \ + RT1[ (uint8) ( Y2 >> 16 ) ] ^ \ + RT2[ (uint8) ( Y1 >> 8 ) ] ^ \ + RT3[ (uint8) ( Y0 ) ]; \ +} + + AES_RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 1 */ + AES_RROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 2 */ + AES_RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 3 */ + AES_RROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 4 */ + AES_RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 5 */ + AES_RROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 6 */ + AES_RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 7 */ + AES_RROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 8 */ + AES_RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 9 */ + + if( ctx->nr > 10 ) + { + AES_RROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 10 */ + AES_RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 11 */ + } + + if( ctx->nr > 12 ) + { + AES_RROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); /* round 12 */ + AES_RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); /* round 13 */ + } + + /* last round */ + + RK += 4; + + X0 = RK[0] ^ ( RSb[ (uint8) ( Y0 >> 24 ) ] << 24 ) ^ + ( RSb[ (uint8) ( Y3 >> 16 ) ] << 16 ) ^ + ( RSb[ (uint8) ( Y2 >> 8 ) ] << 8 ) ^ + ( RSb[ (uint8) ( Y1 ) ] ); + + X1 = RK[1] ^ ( RSb[ (uint8) ( Y1 >> 24 ) ] << 24 ) ^ + ( RSb[ (uint8) ( Y0 >> 16 ) ] << 16 ) ^ + ( RSb[ (uint8) ( Y3 >> 8 ) ] << 8 ) ^ + ( RSb[ (uint8) ( Y2 ) ] ); + + X2 = RK[2] ^ ( RSb[ (uint8) ( Y2 >> 24 ) ] << 24 ) ^ + ( RSb[ (uint8) ( Y1 >> 16 ) ] << 16 ) ^ + ( RSb[ (uint8) ( Y0 >> 8 ) ] << 8 ) ^ + ( RSb[ (uint8) ( Y3 ) ] ); + + X3 = RK[3] ^ ( RSb[ (uint8) ( Y3 >> 24 ) ] << 24 ) ^ + ( RSb[ (uint8) ( Y2 >> 16 ) ] << 16 ) ^ + ( RSb[ (uint8) ( Y1 >> 8 ) ] << 8 ) ^ + ( RSb[ (uint8) ( Y0 ) ] ); + + PUT_UINT32( X0, output, 0 ); + PUT_UINT32( X1, output, 4 ); + PUT_UINT32( X2, output, 8 ); + PUT_UINT32( X3, output, 12 ); +} + diff --git a/protocols/Skype/src/skype.cpp b/protocols/Skype/src/skype.cpp index 06376b70c9..9f080883bb 100644 --- a/protocols/Skype/src/skype.cpp +++ b/protocols/Skype/src/skype.cpp @@ -1,9 +1,16 @@ -#include "skype.h" +//#include "skype.h" #include "skype_proto.h" +#include "aes\aes.h" +#include "base64\base64.h" +//#include "skypekit\skype.h" + +#include "..\..\..\skypekit\key.h" + int hLangpack; +CSkype *g_skype; HINSTANCE g_hInstance; -XML_API xi = {0}; + TIME_API tmi = {0}; PLUGININFOEX pluginInfo = @@ -35,10 +42,255 @@ extern "C" __declspec(dllexport) PLUGININFOEX* MirandaPluginInfoEx(DWORD miranda extern "C" __declspec(dllexport) const MUUID MirandaInterfaces[] = {MIID_PROTOCOL, MIID_LAST}; +// --- + +BOOL IsRunAsAdmin() +{ + BOOL fIsRunAsAdmin = FALSE; + DWORD dwError = ERROR_SUCCESS; + PSID pAdministratorsGroup = NULL; + + // Allocate and initialize a SID of the administrators group. + SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY; + if ( !AllocateAndInitializeSid( + &NtAuthority, + 2, + SECURITY_BUILTIN_DOMAIN_RID, + DOMAIN_ALIAS_RID_ADMINS, + 0, 0, 0, 0, 0, 0, + &pAdministratorsGroup)) + { + dwError = GetLastError(); + goto Cleanup; + } + + // Determine whether the SID of administrators group is enabled in + // the primary access token of the process. + if ( !CheckTokenMembership(NULL, pAdministratorsGroup, &fIsRunAsAdmin)) + { + dwError = GetLastError(); + goto Cleanup; + } + +Cleanup: + // Centralized cleanup for all allocated resources. + if (pAdministratorsGroup) + { + FreeSid(pAdministratorsGroup); + pAdministratorsGroup = NULL; + } + + // Throw the error if something failed in the function. + if (ERROR_SUCCESS != dwError) + { + throw dwError; + } + + return fIsRunAsAdmin; +} + +char *LoadKeyPair(HINSTANCE hInstance) +{ + HRSRC hRes = FindResource(hInstance, MAKEINTRESOURCE(IDR_KEY), L"BIN"); + if (hRes) + { + HGLOBAL hResource = LoadResource(hInstance, hRes); + if (hResource) + { + aes_context ctx; + unsigned char key[128]; + + int basedecoded = Base64::Decode(MY_KEY, (char *)key, MAX_PATH); + ::aes_set_key(&ctx, key, 128); + memset(key, 0, sizeof(key)); + + basedecoded = ::SizeofResource(hInstance, hRes); + char *pData = (char *)hResource; + if (!pData) + return NULL; + + unsigned char *bufD = (unsigned char*)::malloc(basedecoded + 1); + unsigned char *tmpD = (unsigned char*)::malloc(basedecoded + 1); + basedecoded = Base64::Decode(pData, (char *)tmpD, basedecoded); + + for (int i = 0; i < basedecoded; i += 16) + aes_decrypt(&ctx, tmpD+i, bufD+i); + + ::free(tmpD); + bufD[basedecoded] = 0; //cert should be null terminated + return (char *)bufD; + } + return NULL; + } + return NULL; +} + +int StartSkypeRuntime(HINSTANCE hInstance, const wchar_t *profileName, int &port/*, const wchar_t *dbPath*/) +{ + STARTUPINFO cif; + PROCESS_INFORMATION pi; + wchar_t param[128]; + + ::ZeroMemory(&cif, sizeof(STARTUPINFO)); + cif.cb = sizeof(STARTUPINFO); + cif.dwFlags = STARTF_USESHOWWINDOW; + cif.wShowWindow = SW_HIDE; + + //HRSRC hRes; + //HGLOBAL hResource; + wchar_t fileName[MAX_PATH]; + + HRSRC hRes = ::FindResource(hInstance, MAKEINTRESOURCE(/*IDR_RUNTIME*/102), L"BIN"); + if (hRes) + { + HGLOBAL hResource = ::LoadResource(hInstance, hRes); + if (hResource) + { + HANDLE hFile; + char *pData = (char *)LockResource(hResource); + DWORD dwSize = SizeofResource(hInstance, hRes), written = 0; + ::GetModuleFileName(hInstance, fileName, MAX_PATH); + + wchar_t *skypeKitPath = ::wcsrchr(fileName, '\\'); + if (skypeKitPath != NULL) + *skypeKitPath = 0; + ::swprintf(fileName, SIZEOF(fileName), L"%s\\%s", fileName, L"SkypeKit.exe"); + if ( !::PathFileExists(fileName)) + { + if ((hFile = ::CreateFile( + fileName, + GENERIC_WRITE, + 0, + NULL, + CREATE_ALWAYS, + FILE_ATTRIBUTE_NORMAL, + 0)) != INVALID_HANDLE_VALUE) + { + ::WriteFile(hFile, (void *)pData, dwSize, &written, NULL); + ::CloseHandle(hFile); + } + else + { + // Check the current process's "run as administrator" status. + // Elevate the process if it is not run as administrator. + if (!IsRunAsAdmin()) + { + wchar_t path[MAX_PATH], cmdLine[100]; + ::GetModuleFileName(NULL, path, ARRAYSIZE(path)); + + if (profileName) + ::swprintf( + cmdLine, + SIZEOF(cmdLine), + L" /restart:%d /profile=%s", + ::GetCurrentProcessId(), + profileName); + else + ::swprintf( + cmdLine, + SIZEOF(cmdLine), + L" /restart:%d", + ::GetCurrentProcessId()); + + // Launch itself as administrator. + SHELLEXECUTEINFO sei = { sizeof(sei) }; + sei.lpVerb = L"runas"; + sei.lpFile = path; + sei.lpParameters = cmdLine; + //sei.hwnd = hDlg; + sei.nShow = SW_NORMAL; + + if ( !::ShellExecuteEx(&sei)) + { + DWORD dwError = ::GetLastError(); + if (dwError == ERROR_CANCELLED) + { + // The user refused to allow privileges elevation. + // Do nothing ... + } + } + //else + //{ + // //DestroyWindow(hDlg); // Quit itself + // ::CallService("CloseAction", 0, 0); + //} + } + return 0; + } + } + } + } + + PROCESSENTRY32 entry; + entry.dwSize = sizeof(PROCESSENTRY32); + + HANDLE snapshot = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL); + if (::Process32First(snapshot, &entry) == TRUE) { + while (::Process32Next(snapshot, &entry) == TRUE) { + if (::wcsicmp(entry.szExeFile, L"SkypeKit.exe") == 0) { + HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID); + port += rand() % 8963 + 1000; + ::CloseHandle(hProcess); + break; + } + } + } + ::CloseHandle(snapshot); + + //::swprintf(param, SIZEOF(param), L"-p -P %d -f %s", port, dbPath); + ::swprintf(param, SIZEOF(param), L"-p -P %d", port); + int startingrt = ::CreateProcess( + fileName, param, + NULL, NULL, FALSE, + CREATE_NEW_CONSOLE, + NULL, NULL, &cif, &pi); + DWORD rterr = GetLastError(); + + //if (startingrt && rterr == ERROR_SUCCESS) + //return 1; + //else + //return 0; + return startingrt; +} + +// --- extern "C" int __declspec(dllexport) Load(void) { - mir_getXI(&xi); + int port = 8963; + VARST profilename( _T("%miranda_profilename%")); + + if ( !StartSkypeRuntime(g_hInstance, (TCHAR*)profilename, port)) + { + ::MessageBoxA(NULL, "", MODULE, MB_OK | MB_ICONERROR); + return 1; + } + + char *keyPair = LoadKeyPair(g_hInstance); + if ( !keyPair) + { + ::MessageBoxA(NULL, "", MODULE, MB_OK | MB_ICONERROR); + return 1; + } + + g_skype = new CSkype(); + TransportInterface::Status status = g_skype->init(keyPair, "127.0.0.1", port); + if (status != TransportInterface::OK) + { + ::MessageBoxA(NULL, "", MODULE, MB_OK | MB_ICONERROR); + return 1; + } + + if ( !g_skype->start()) + { + ::MessageBoxA(NULL, "", MODULE, MB_OK | MB_ICONERROR); + return 1; + } + + free(keyPair); + + // --- + mir_getTMI(&tmi); mir_getLP(&pluginInfo); @@ -61,5 +313,8 @@ extern "C" int __declspec(dllexport) Unload(void) CSkypeProto::UninitMenus(); CSkypeProto::UninitIcons(); + g_skype->stop(); + delete g_skype; + return 0; } \ No newline at end of file diff --git a/protocols/Skype/src/skype.h b/protocols/Skype/src/skype.h index 8615ebf025..0a8d5a9f50 100644 --- a/protocols/Skype/src/skype.h +++ b/protocols/Skype/src/skype.h @@ -23,6 +23,7 @@ #include //#include //#include +#include #include #include #include @@ -48,14 +49,10 @@ #include #include - -#include +#include #include "resource.h" #include "version.h" -#include "skype_subclassing.h" - -#include "..\..\..\skypekit\key.h" #define MODULE "Skype" #define SKYPE_SID_LIMIT 128 @@ -79,7 +76,7 @@ #define CMITEMS_COUNT 3 -extern HINSTANCE g_hInstance; - +class CSkype; -struct CSkypeProto; \ No newline at end of file +extern CSkype *g_skype; +extern HINSTANCE g_hInstance; \ No newline at end of file diff --git a/protocols/Skype/src/skype_account.cpp b/protocols/Skype/src/skype_account.cpp index d5a8db7f13..d28116a6ee 100644 --- a/protocols/Skype/src/skype_account.cpp +++ b/protocols/Skype/src/skype_account.cpp @@ -88,7 +88,7 @@ void __cdecl CSkypeProto::SignInAsync(void*) } else { - ::CallService(MS_DB_CRYPT_ENCODESTRING, ::wcslen(this->password), (LPARAM)this->password); + ::CallService(MS_DB_CRYPT_ENCODESTRING, ::strlen(this->password), (LPARAM)this->password); } this->LoadOwnInfo(this); @@ -126,8 +126,8 @@ bool CSkypeProto::PreparePassword() ::mir_free(this->password); this->password = NULL; } - this->password = ::db_get_wsa(NULL, this->m_szModuleName, SKYPE_SETTINGS_PASSWORD); - if ( !this->password || !::wcslen(this->password)) + this->password = ::db_get_sa(NULL, this->m_szModuleName, SKYPE_SETTINGS_PASSWORD); + if ( !this->password || !::strlen(this->password)) { if (this->password) { @@ -142,12 +142,12 @@ bool CSkypeProto::PreparePassword() } else { - this->password = ::mir_wstrdup(param.password); + this->password = ::mir_strdup(param.password); this->rememberPassword = param.rememberPassword; } } else - ::CallService(MS_DB_CRYPT_DECODESTRING, ::wcslen(this->password), (LPARAM)this->password); + ::CallService(MS_DB_CRYPT_DECODESTRING, ::strlen(this->password), (LPARAM)this->password); } return true; @@ -158,7 +158,7 @@ bool CSkypeProto::SignIn(int status) if ( !this->PrepareLogin()) return false; - if (this->skype->GetAccount(::mir_u2a(this->login), this->account)) + if (g_skype->GetAccount(::mir_u2a(this->login), this->account)) { if ( !this->PreparePassword()) return false; @@ -172,7 +172,7 @@ bool CSkypeProto::SignIn(int status) this->SetAccountSettings(); - this->account->LoginWithPassword(::mir_utf8encodeW(this->password), false, false); + this->account->LoginWithPassword(this->password, false, false); } return true; @@ -181,8 +181,8 @@ bool CSkypeProto::SignIn(int status) void CSkypeProto::SetAccountSettings() { int port = this->GetSettingWord("Port", rand() % 10000 + 10000); - this->skype->SetInt(SETUPKEY_PORT, port); - this->skype->SetInt(SETUPKEY_DISABLE_PORT80, (int)!this->GetSettingByte("UseAlternativePorts", 1)); + g_skype->SetInt(SETUPKEY_PORT, port); + g_skype->SetInt(SETUPKEY_DISABLE_PORT80, (int)!this->GetSettingByte("UseAlternativePorts", 1)); } bool CSkypeProto::IsAvatarChanged(const SEBinary &avatar) diff --git a/protocols/Skype/src/skype_chat.cpp b/protocols/Skype/src/skype_chat.cpp index 3a909fa131..42a19fe190 100644 --- a/protocols/Skype/src/skype_chat.cpp +++ b/protocols/Skype/src/skype_chat.cpp @@ -195,7 +195,7 @@ void CSkypeProto::CreateChatWindow(CConversation::Ref conversation, bool showWin } gcd.iType = GC_EVENT_CONTROL; - ::CallServiceSync(MS_GC_EVENT, ShowWindow ? SESSION_INITDONE : WINDOW_HIDDEN, (LPARAM)&gce); + ::CallServiceSync(MS_GC_EVENT, showWindow ? SESSION_INITDONE : WINDOW_HIDDEN, (LPARAM)&gce); ::CallServiceSync(MS_GC_EVENT, SESSION_ONLINE, (LPARAM)&gce); ::mir_free(cid); @@ -212,16 +212,16 @@ wchar_t *CSkypeProto::StartChat(const wchar_t *cid, const SEStringList &invitedC { if (cid) { - this->skype->GetConversationByIdentity(::mir_utf8encodeW(cid), conversation); + g_skype->GetConversationByIdentity(::mir_utf8encodeW(cid), conversation); conversation->GetJoinBlob(data); - this->skype->GetConversationByBlob(data, conversation, false); + g_skype->GetConversationByBlob(data, conversation, false); conversation->Join(); chatID = ::mir_wstrdup(cid); } else { - this->skype->CreateConference(conversation); + g_skype->CreateConference(conversation); conversation->SetOption(CConversation::P_OPT_JOINING_ENABLED, true); conversation->SetOption(CConversation::P_OPT_ENTRY_LEVEL_RANK, CParticipant::WRITER); conversation->SetOption(CConversation::P_OPT_DISCLOSE_HISTORY, 1); @@ -300,7 +300,7 @@ void CSkypeProto::JoinToChat(CConversation::Ref conversation, bool showWindow) mir_ptr group = ::mir_utf8decodeW(CParticipant::GetRankName(rank)); CContact::Ref contact; - this->skype->GetContact((char *)mir_ptr(::mir_utf8encodeW(sid)), contact); + g_skype->GetContact((char *)mir_ptr(::mir_utf8encodeW(sid)), contact); CContact::AVAILABILITY status; contact->GetPropAvailability(status); @@ -392,9 +392,9 @@ INT_PTR __cdecl CSkypeProto::OnJoinChat(WPARAM wParam, LPARAM) CConversation::Ref conversation; //todo: fixme - this->skype->GetConversationByIdentity(::mir_utf8encodeW(cid), conversation); + g_skype->GetConversationByIdentity(::mir_utf8encodeW(cid), conversation); conversation->GetJoinBlob(data); - this->skype->GetConversationByBlob(data, conversation, false); + g_skype->GetConversationByBlob(data, conversation, false); conversation->Join(); this->JoinToChat(conversation); @@ -436,7 +436,7 @@ int __cdecl CSkypeProto::OnGCEventHook(WPARAM, LPARAM lParam) case GC_SESSION_TERMINATE: { CConversation::Ref conversation; - if (this->skype->GetConversationByIdentity(::mir_utf8encodeW(chatID), conversation, false)) + if (g_skype->GetConversationByIdentity(::mir_utf8encodeW(chatID), conversation, false)) { Participant::Refs participants; conversation->GetParticipants(participants, CConversation::MYSELF); @@ -449,7 +449,7 @@ int __cdecl CSkypeProto::OnGCEventHook(WPARAM, LPARAM lParam) if (gch->ptszText && gch->ptszText[0]) { CConversation::Ref conversation; - if (this->skype->GetConversationByIdentity(::mir_utf8encodeW(chatID), conversation, false)) + if (g_skype->GetConversationByIdentity(::mir_utf8encodeW(chatID), conversation, false)) { CMessage::Ref message; ::mir_ptr text(::mir_utf8encodeW(gch->ptszText)); @@ -588,7 +588,7 @@ void CSkypeProto::UpdateChatUserStatus(CContact::Ref contact) GC_INFO gci = {0}; gci.Flags = BYINDEX | ID; - gci.pszModule = m_szModuleName; + gci.pszModule = this->m_szModuleName; int count = ::CallServiceSync(MS_GC_GETSESSIONCOUNT, 0, (LPARAM)this->m_szModuleName); for (int i = 0; i < count ; i++) diff --git a/protocols/Skype/src/skype_contacts.cpp b/protocols/Skype/src/skype_contacts.cpp index e2addbdc0f..22ef53c8de 100644 --- a/protocols/Skype/src/skype_contacts.cpp +++ b/protocols/Skype/src/skype_contacts.cpp @@ -234,7 +234,7 @@ HANDLE CSkypeProto::AddContact(CContact::Ref contact) void __cdecl CSkypeProto::LoadContactList(void*) { - this->skype->GetHardwiredContactGroup(CContactGroup::ALL_BUDDIES, this->commonList); + g_skype->GetHardwiredContactGroup(CContactGroup::ALL_BUDDIES, this->commonList); this->commonList.fetch(); this->commonList->SetOnContactListChangedCallback( (CContactGroup::OnContactListChanged)&CSkypeProto::OnContactListChanged, @@ -262,7 +262,7 @@ void __cdecl CSkypeProto::LoadContactList(void*) void __cdecl CSkypeProto::LoadChatList(void*) { CConversation::Refs conversations; - this->skype->GetConversationList(conversations); + g_skype->GetConversationList(conversations); for (uint i = 0; i < conversations.size(); i++) { CConversation::TYPE type; @@ -280,7 +280,7 @@ void __cdecl CSkypeProto::LoadChatList(void*) } /*CConversation::Refs conversations; - this->skype->GetConversationList(conversations); + g_skype->GetConversationList(conversations); for (uint i = 0; i < conversations.size(); i++) { conversations[i]->Delete(); @@ -290,7 +290,7 @@ void __cdecl CSkypeProto::LoadChatList(void*) void __cdecl CSkypeProto::LoadAuthWaitList(void*) { CContact::Refs authContacts; - this->skype->GetHardwiredContactGroup(CContactGroup::CONTACTS_WAITING_MY_AUTHORIZATION, this->authWaitList); + g_skype->GetHardwiredContactGroup(CContactGroup::CONTACTS_WAITING_MY_AUTHORIZATION, this->authWaitList); this->authWaitList.fetch(); this->authWaitList->SetOnContactListChangedCallback( (CContactGroup::OnContactListChanged)&CSkypeProto::OnContactListChanged, @@ -334,7 +334,7 @@ void CSkypeProto::OnSearchCompleted(HANDLE hSearch) this->SendBroadcast(ACKTYPE_SEARCH, ACKRESULT_SUCCESS, hSearch, 0); } -void CSkypeProto::OnContactFinded(HANDLE hSearch, CContact::Ref contact) +void CSkypeProto::OnContactFinded(CContact::Ref contact, HANDLE hSearch) { PROTOSEARCHRESULT psr = {0}; psr.cbSize = sizeof(psr); @@ -380,7 +380,7 @@ void __cdecl CSkypeProto::SearchBySidAsync(void* arg) } CContactSearch::Ref search; - this->skype->CreateIdentitySearch(::mir_u2a(sid), search); + g_skype->CreateIdentitySearch(::mir_u2a(sid), search); search.fetch(); search->SetProtoInfo(this, (HANDLE)SKYPE_SEARCH_BYSID); search->SetOnContactFindedCallback( @@ -404,7 +404,7 @@ void __cdecl CSkypeProto::SearchByEmailAsync(void* arg) wchar_t *email = (wchar_t *)arg; CContactSearch::Ref search; - this->skype->CreateContactSearch(search); + g_skype->CreateContactSearch(search); search.fetch(); search->SetProtoInfo(this, (HANDLE)SKYPE_SEARCH_BYEMAIL); search->SetOnContactFindedCallback( diff --git a/protocols/Skype/src/skype_database.cpp b/protocols/Skype/src/skype_database.cpp index c6d2778aa9..2e4bfe4139 100644 --- a/protocols/Skype/src/skype_database.cpp +++ b/protocols/Skype/src/skype_database.cpp @@ -12,16 +12,16 @@ bool CSkypeProto::IsMessageInDB(HANDLE hContact, DWORD timestamp, const char* gu dbei.pBlob = (PBYTE)::mir_alloc(dbei.cbBlob); ::db_event_get(hDbEvent, &dbei); - if (dbei.timestamp < timestamp) + /*if (dbei.timestamp < timestamp) { ::mir_free(dbei.pBlob); break; - } + }*/ int sendFlag = dbei.flags & DBEF_SENT; if (dbei.eventType == EVENTTYPE_MESSAGE && sendFlag == flag) { - if (::memcmp(&dbei.pBlob[dbei.cbBlob - 32], guid, 32) == 0) + if (::memcmp(&dbei.pBlob[dbei.cbBlob], guid, 32) == 0) { ::mir_free(dbei.pBlob); result = true; @@ -100,9 +100,9 @@ void CSkypeProto::RaiseMessageReceivedEvent( const wchar_t *message, bool isNeedCheck) { - if (isNeedCheck) - if (this->IsMessageInDB(hContact, timestamp, guid)) - return; + //if (isNeedCheck) + // if (this->IsMessageInDB(hContact, timestamp, guid)) + // return; PROTORECVEVENT recv; recv.flags = PREF_UTF; @@ -119,8 +119,8 @@ void CSkypeProto::RaiseMessageSendedEvent( const char *guid, const wchar_t *message) { - if (this->IsMessageInDB(hContact, timestamp, guid, DBEF_SENT)) - return; + //if (this->IsMessageInDB(hContact, timestamp, guid, DBEF_SENT)) + // return; char *msg = ::mir_utf8encodeW(message); diff --git a/protocols/Skype/src/skype_dialogs.cpp b/protocols/Skype/src/skype_dialogs.cpp index 009e8c690c..5c31c68dec 100644 --- a/protocols/Skype/src/skype_dialogs.cpp +++ b/protocols/Skype/src/skype_dialogs.cpp @@ -13,8 +13,8 @@ INT_PTR CALLBACK CSkypeProto::SkypeMainOptionsProc(HWND hwnd, UINT message, WPAR proto = (CSkypeProto*)lParam; ::SetWindowLongPtr(hwnd, GWLP_USERDATA, lParam); { - char *sid = ::db_get_sa(NULL, proto->m_szModuleName, SKYPE_SETTINGS_LOGIN); - SetDlgItemTextA(hwnd, IDC_SL, sid); + wchar_t *sid = ::db_get_wsa(NULL, proto->m_szModuleName, SKYPE_SETTINGS_LOGIN); + SetDlgItemText(hwnd, IDC_SL, sid); ::mir_free(sid); } { @@ -65,9 +65,9 @@ INT_PTR CALLBACK CSkypeProto::SkypeMainOptionsProc(HWND hwnd, UINT message, WPAR if (!proto->IsOnline() && proto->GetSettingWord("Status") <= ID_STATUS_OFFLINE) { - char sid[128]; - GetDlgItemTextA(hwnd, IDC_SL, sid, SIZEOF(sid)); - EnableWindow(GetDlgItem(hwnd, IDC_REGISTER), ::strlen(sid)); + wchar_t sid[128]; + GetDlgItemText(hwnd, IDC_SL, sid, SIZEOF(sid)); + EnableWindow(GetDlgItem(hwnd, IDC_REGISTER), ::wcslen(sid)); } SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0); } @@ -98,12 +98,12 @@ INT_PTR CALLBACK CSkypeProto::SkypeMainOptionsProc(HWND hwnd, UINT message, WPAR GetDlgItemTextA(hwnd, IDC_PW, pwd, SIZEOF(pwd)); CSkype::VALIDATERESULT reason; - proto->skype->ValidatePassword(sid, pwd, reason); + g_skype->ValidatePassword(sid, pwd, reason); if (reason == CSkype::VALIDATED_OK) { CAccount::Ref account; - proto->skype->GetAccount(sid, proto->account); + g_skype->GetAccount(sid, proto->account); proto->account->SetStrProperty(CAccount::P_FULLNAME, sid); proto->account->SetOnAccountChangedCallback( (CAccount::OnAccountChanged)&CSkypeProto::OnAccountChanged, proto); @@ -117,14 +117,14 @@ INT_PTR CALLBACK CSkypeProto::SkypeMainOptionsProc(HWND hwnd, UINT message, WPAR case IDC_CHANGE_PWD: { - wchar_t sid[128], pwd[128]; - GetDlgItemText(hwnd, IDC_SL, sid, SIZEOF(sid)); - GetDlgItemText(hwnd, IDC_PW, pwd, SIZEOF(pwd)); + char sid[128], pwd[128]; + GetDlgItemTextA(hwnd, IDC_SL, sid, SIZEOF(sid)); + GetDlgItemTextA(hwnd, IDC_PW, pwd, SIZEOF(pwd)); PasswordChangeBoxParam param; if (proto->ChangePassword(param)) { - proto->account->ChangePassword(::mir_utf8encodeW(param.password), ::mir_utf8encodeW(param.password2)); + proto->account->ChangePassword(param.password, param.password2); } } break; @@ -135,15 +135,16 @@ INT_PTR CALLBACK CSkypeProto::SkypeMainOptionsProc(HWND hwnd, UINT message, WPAR case WM_NOTIFY: if (reinterpret_cast(lParam)->code == PSN_APPLY && !proto->IsOnline()) { - wchar_t data[128]; - GetDlgItemText(hwnd, IDC_SL, data, SIZEOF(data)); - ::db_set_ws(NULL, proto->m_szModuleName, SKYPE_SETTINGS_LOGIN, data); + wchar_t sid[128]; + GetDlgItemText(hwnd, IDC_SL, sid, SIZEOF(sid)); + ::db_set_ws(NULL, proto->m_szModuleName, SKYPE_SETTINGS_LOGIN, sid); ::mir_free(proto->login); - proto->login = ::mir_wstrdup(data); + proto->login = ::mir_wstrdup(sid); - GetDlgItemText(hwnd, IDC_PW, data, sizeof(data)); - ::CallService(MS_DB_CRYPT_ENCODESTRING, wcslen(data), (LPARAM)&data); - ::db_set_ws(NULL, proto->m_szModuleName, SKYPE_SETTINGS_PASSWORD, data); + char pwd[128]; + GetDlgItemTextA(hwnd, IDC_PW, pwd, SIZEOF(pwd)); + ::CallService(MS_DB_CRYPT_ENCODESTRING, ::strlen(pwd), (LPARAM)&pwd); + ::db_set_s(NULL, proto->m_szModuleName, SKYPE_SETTINGS_PASSWORD, pwd); HWND item = GetDlgItem(hwnd, IDC_PORT); if (item) @@ -233,9 +234,9 @@ INT_PTR CALLBACK CSkypeProto::SkypePasswordRequestProc(HWND hwndDlg, UINT msg, W { param->rememberPassword = ::IsDlgButtonChecked(hwndDlg, IDC_SAVEPASSWORD) > 0; - wchar_t password[SKYPE_PASSWORD_LIMIT]; - ::GetDlgItemText(hwndDlg, IDC_PASSWORD, password, SIZEOF(password)); - param->password = ::mir_wstrdup(password); + char password[SKYPE_PASSWORD_LIMIT]; + ::GetDlgItemTextA(hwndDlg, IDC_PASSWORD, password, SIZEOF(password)); + param->password = ::mir_strdup(password); ::EndDialog(hwndDlg, IDOK); } @@ -290,16 +291,16 @@ INT_PTR CALLBACK CSkypeProto::SkypePasswordChangeProc(HWND hwndDlg, UINT msg, WP { case IDOK: { - wchar_t oldPwd[SKYPE_PASSWORD_LIMIT]; - ::GetDlgItemText(hwndDlg, IDC_PASSWORD, oldPwd, SIZEOF(oldPwd)); - param->password = ::mir_wstrdup(oldPwd); + char oldPwd[SKYPE_PASSWORD_LIMIT]; + ::GetDlgItemTextA(hwndDlg, IDC_PASSWORD, oldPwd, SIZEOF(oldPwd)); + param->password = ::mir_strdup(oldPwd); - wchar_t pwd1[SKYPE_PASSWORD_LIMIT]; - ::GetDlgItemText(hwndDlg, IDC_PASSWORD2, pwd1, SIZEOF(pwd1)); - param->password2 = ::mir_wstrdup(pwd1); + char pwd1[SKYPE_PASSWORD_LIMIT]; + ::GetDlgItemTextA(hwndDlg, IDC_PASSWORD2, pwd1, SIZEOF(pwd1)); + param->password2 = ::mir_strdup(pwd1); - wchar_t pwd2[SKYPE_PASSWORD_LIMIT]; - ::GetDlgItemText(hwndDlg, IDC_PASSWORD3, pwd2, SIZEOF(pwd2)); + char pwd2[SKYPE_PASSWORD_LIMIT]; + ::GetDlgItemTextA(hwndDlg, IDC_PASSWORD3, pwd2, SIZEOF(pwd2)); ::EndDialog(hwndDlg, IDOK); } @@ -647,7 +648,7 @@ INT_PTR CALLBACK CSkypeProto::InviteToChatProc(HWND hwndDlg, UINT msg, WPARAM wP { CContact::Ref contact; CContact::AVAILABILITY status; - param->ppro->skype->GetContact(invitedContacts[i], contact); + g_skype->GetContact(invitedContacts[i], contact); contact->GetPropAvailability(status); //todo: fix rank @@ -659,7 +660,7 @@ INT_PTR CALLBACK CSkypeProto::InviteToChatProc(HWND hwndDlg, UINT msg, WPARAM wP } CConversation::Ref conversation; - param->ppro->skype->GetConversationByIdentity(::mir_utf8encodeW(chatID), conversation); + g_skype->GetConversationByIdentity(::mir_utf8encodeW(chatID), conversation); conversation->AddConsumers(invitedContacts); } else @@ -670,7 +671,7 @@ INT_PTR CALLBACK CSkypeProto::InviteToChatProc(HWND hwndDlg, UINT msg, WPARAM wP { CContact::Ref contact; CContact::AVAILABILITY status; - param->ppro->skype->GetContact(invitedContacts[i], contact); + g_skype->GetContact(invitedContacts[i], contact); contact->GetPropAvailability(status); //todo: fix rank diff --git a/protocols/Skype/src/skype_events.cpp b/protocols/Skype/src/skype_events.cpp index 23a3599fcb..b35300a3b8 100644 --- a/protocols/Skype/src/skype_events.cpp +++ b/protocols/Skype/src/skype_events.cpp @@ -2,8 +2,6 @@ int CSkypeProto::OnModulesLoaded(WPARAM, LPARAM) { - this->InitSkype(); - this->InitChat(); this->InitNetLib(); this->InitCustomFolders(); @@ -11,6 +9,10 @@ int CSkypeProto::OnModulesLoaded(WPARAM, LPARAM) this->HookEvent(ME_OPT_INITIALISE, &CSkypeProto::OnOptionsInit); this->HookEvent(ME_USERINFO_INITIALISE, &CSkypeProto::OnUserInfoInit); + g_skype->SetOnMessageCallback( + (CSkype::OnMessaged)&CSkypeProto::OnMessage, + this); + return 0; } @@ -19,7 +21,6 @@ int CSkypeProto::OnPreShutdown(WPARAM, LPARAM) this->SetStatus(ID_STATUS_OFFLINE); this->UninitNetLib(); - this->UninitSkype(); return 0; } @@ -35,7 +36,7 @@ int CSkypeProto::OnContactDeleted(WPARAM wParam, LPARAM lParam) this->LeaveChat(chatID); CConversation::Ref conversation; - this->skype->GetConversationByIdentity(::mir_utf8encodeW(chatID), conversation); + g_skype->GetConversationByIdentity(::mir_utf8encodeW(chatID), conversation); conversation->RetireFrom(); conversation->Delete(); } @@ -55,15 +56,17 @@ int CSkypeProto::OnMessagePreCreate(WPARAM, LPARAM lParam) SEBinary guid; if (message->GetPropGuid(guid)) { - evt->dbei->pBlob = (PBYTE)::mir_realloc(evt->dbei->pBlob, (evt->dbei->cbBlob + 32)); + evt->dbei->pBlob = (PBYTE)::mir_realloc(evt->dbei->pBlob, (evt->dbei->cbBlob + 33)); ::memcpy(&evt->dbei->pBlob[evt->dbei->cbBlob], guid.data(), 32); - evt->dbei->cbBlob += 32; + evt->dbei->cbBlob += 33; } + //delete message; + return 1; } -void CSkypeProto::OnMessageSended(CConversation::Ref conversation, CMessage::Ref message) +void CSkypeProto::OnMessageSended(CConversation::Ref &conversation, CMessage::Ref &message) { SEString data; @@ -88,7 +91,7 @@ void CSkypeProto::OnMessageSended(CConversation::Ref conversation, CMessage::Ref participants[0]->GetPropIdentity(data); CContact::Ref receiver; - this->skype->GetContact(data, receiver); + g_skype->GetContact(data, receiver); HANDLE hContact = this->AddContact(receiver); @@ -130,7 +133,7 @@ void CSkypeProto::OnMessageSended(CConversation::Ref conversation, CMessage::Ref ::mir_free(text); } -void CSkypeProto::OnMessageReceived(CConversation::Ref conversation, CMessage::Ref message) +void CSkypeProto::OnMessageReceived(CConversation::Ref &conversation, CMessage::Ref &message) { SEString data; @@ -150,7 +153,7 @@ void CSkypeProto::OnMessageReceived(CConversation::Ref conversation, CMessage::R message->GetPropAuthor(data); CContact::Ref author; - this->skype->GetContact(data, author); + g_skype->GetContact(data, author); HANDLE hContact = this->AddContact(author); @@ -181,7 +184,7 @@ void CSkypeProto::OnMessageReceived(CConversation::Ref conversation, CMessage::R ::mir_free(text); } -void CSkypeProto::OnTransferChanged(int prop, CTransfer::Ref transfer) +void CSkypeProto::OnTransferChanged(CTransfer::Ref transfer, int prop) { switch (prop) { @@ -191,7 +194,7 @@ void CSkypeProto::OnTransferChanged(int prop, CTransfer::Ref transfer) transfer->GetPropChatmsgGuid(guid); CMessage::Ref message; - this->skype->GetMessageByGuid(guid, message); + g_skype->GetMessageByGuid(guid, message); uint oid = message->getOID(); @@ -239,7 +242,7 @@ void CSkypeProto::OnTransferChanged(int prop, CTransfer::Ref transfer) transfer->GetPropChatmsgGuid(guid); CMessage::Ref message; - this->skype->GetMessageByGuid(guid, message); + g_skype->GetMessageByGuid(guid, message); uint oid = message->getOID(); @@ -259,10 +262,10 @@ void CSkypeProto::OnTransferChanged(int prop, CTransfer::Ref transfer) pfts.pszFiles = &pfts.szCurrentFile; transfer->GetPropFilesize(data); - pfts.totalBytes = pfts.currentFileSize = data.toUInt(); + pfts.totalBytes = pfts.currentFileSize = data.toUInt64(); transfer->GetPropBytestransferred(data); - pfts.totalProgress = pfts.currentFileProgress = data.toUInt(); + pfts.totalProgress = pfts.currentFileProgress = data.toUInt64(); this->SendBroadcast(hContact, ACKTYPE_FILE, ACKRESULT_DATA, (HANDLE)oid, (LPARAM)&pfts); } @@ -270,7 +273,7 @@ void CSkypeProto::OnTransferChanged(int prop, CTransfer::Ref transfer) } } -void CSkypeProto::OnFile(CConversation::Ref conversation, CMessage::Ref message) +void CSkypeProto::OnFile(CConversation::Ref &conversation, CMessage::Ref &message) { CTransfer::Refs transfers; message->GetTransfers(transfers); @@ -375,7 +378,7 @@ void CSkypeProto::OnMessage(CConversation::Ref conversation, CMessage::Ref messa if ( !alreadyInChat.contains(sid)) { CContact::Ref contact; - this->skype->GetContact((char *)mir_ptr(::mir_utf8encodeW(sid)), contact); + g_skype->GetContact((char *)mir_ptr(::mir_utf8encodeW(sid)), contact); CContact::AVAILABILITY status; contact->GetPropAvailability(status); @@ -453,7 +456,7 @@ void CSkypeProto::OnMessage(CConversation::Ref conversation, CMessage::Ref messa message->GetPropAuthor(identity); CContact::Ref author; - this->skype->GetContact(identity, author); + g_skype->GetContact(identity, author); HANDLE hContact = this->AddContact(author); diff --git a/protocols/Skype/src/skype_instances.cpp b/protocols/Skype/src/skype_instances.cpp index a34d99a192..996c588164 100644 --- a/protocols/Skype/src/skype_instances.cpp +++ b/protocols/Skype/src/skype_instances.cpp @@ -1,10 +1,5 @@ #include "skype_proto.h" -int CSkypeProto::SendBroadcast(int type, int result, HANDLE hProcess, LPARAM lParam) -{ - return this->SendBroadcast(NULL, type, result, hProcess, lParam); -} - LIST CSkypeProto::instanceList(1, CSkypeProto::CompareProtos); CSkypeProto* CSkypeProto::InitSkypeProto(const char* protoName, const wchar_t* userName) @@ -18,6 +13,7 @@ CSkypeProto* CSkypeProto::InitSkypeProto(const char* protoName, const wchar_t* u MB_ICONWARNING); return NULL; } + CSkypeProto *ppro = new CSkypeProto(protoName, userName); CSkypeProto::instanceList.insert(ppro); diff --git a/protocols/Skype/src/skype_menus.cpp b/protocols/Skype/src/skype_menus.cpp index 59f9723ef7..fc08f4b814 100644 --- a/protocols/Skype/src/skype_menus.cpp +++ b/protocols/Skype/src/skype_menus.cpp @@ -77,7 +77,7 @@ int CSkypeProto::GrantAuth(WPARAM wParam, LPARAM lParam) CContact::Ref contact; HANDLE hContact = (HANDLE)wParam; SEString sid(::mir_u2a(::mir_ptr(::db_get_wsa(hContact, this->m_szModuleName, SKYPE_SETTINGS_LOGIN)))); - if (this->skype->GetContact(sid, contact)) + if (g_skype->GetContact(sid, contact)) { if (contact->SetBuddyStatus(true)) { @@ -94,7 +94,7 @@ int CSkypeProto::RevokeAuth(WPARAM wParam, LPARAM lParam) CContact::Ref contact; HANDLE hContact = (HANDLE)wParam; SEString sid(::mir_u2a(::mir_ptr(::db_get_wsa(hContact, this->m_szModuleName, SKYPE_SETTINGS_LOGIN)))); - if (this->skype->GetContact(sid, contact)) + if (g_skype->GetContact(sid, contact)) { if (contact->SetBuddyStatus(false)) { diff --git a/protocols/Skype/src/skype_netlib.cpp b/protocols/Skype/src/skype_netlib.cpp index 7401e8c84e..e0782e4a9f 100644 --- a/protocols/Skype/src/skype_netlib.cpp +++ b/protocols/Skype/src/skype_netlib.cpp @@ -38,40 +38,40 @@ void CSkypeProto::InitProxy() { case PROXYTYPE_HTTP: case PROXYTYPE_HTTPS: - this->skype->SetInt(SETUPKEY_HTTPS_PROXY_ENABLE, 1); - this->skype->SetInt(SETUPKEY_SOCKS_PROXY_ENABLE, 0); - this->skype->SetStr(SETUPKEY_HTTPS_PROXY_ADDR, address); + g_skype->SetInt(SETUPKEY_HTTPS_PROXY_ENABLE, 1); + g_skype->SetInt(SETUPKEY_SOCKS_PROXY_ENABLE, 0); + g_skype->SetStr(SETUPKEY_HTTPS_PROXY_ADDR, address); if (nlus.useProxyAuth) { char encodedPass[MAX_PATH]; Base64::Encode(nlus.szProxyAuthPassword, encodedPass, MAX_PATH); - this->skype->SetStr(SETUPKEY_HTTPS_PROXY_USER, nlus.szProxyAuthUser); - this->skype->SetStr(SETUPKEY_HTTPS_PROXY_PWD, encodedPass); + g_skype->SetStr(SETUPKEY_HTTPS_PROXY_USER, nlus.szProxyAuthUser); + g_skype->SetStr(SETUPKEY_HTTPS_PROXY_PWD, encodedPass); } break; case PROXYTYPE_SOCKS4: case PROXYTYPE_SOCKS5: - this->skype->SetInt(SETUPKEY_HTTPS_PROXY_ENABLE, 0); - this->skype->SetInt(SETUPKEY_SOCKS_PROXY_ENABLE, 1); - this->skype->SetStr(SETUPKEY_SOCKS_PROXY_ADDR, address); + g_skype->SetInt(SETUPKEY_HTTPS_PROXY_ENABLE, 0); + g_skype->SetInt(SETUPKEY_SOCKS_PROXY_ENABLE, 1); + g_skype->SetStr(SETUPKEY_SOCKS_PROXY_ADDR, address); if (nlus.useProxyAuth) { - this->skype->SetStr(SETUPKEY_SOCKS_PROXY_USER, nlus.szProxyAuthUser); - this->skype->SetStr(SETUPKEY_SOCKS_PROXY_PWD, nlus.szProxyAuthPassword); + g_skype->SetStr(SETUPKEY_SOCKS_PROXY_USER, nlus.szProxyAuthUser); + g_skype->SetStr(SETUPKEY_SOCKS_PROXY_PWD, nlus.szProxyAuthPassword); } break; default: - this->skype->Delete(SETUPKEY_HTTPS_PROXY_ENABLE); - this->skype->Delete(SETUPKEY_HTTPS_PROXY_ADDR); - this->skype->Delete(SETUPKEY_HTTPS_PROXY_USER); - this->skype->Delete(SETUPKEY_HTTPS_PROXY_PWD); - this->skype->Delete(SETUPKEY_SOCKS_PROXY_ENABLE); - this->skype->Delete(SETUPKEY_SOCKS_PROXY_ADDR); - this->skype->Delete(SETUPKEY_SOCKS_PROXY_USER); - this->skype->Delete(SETUPKEY_SOCKS_PROXY_PWD); + g_skype->Delete(SETUPKEY_HTTPS_PROXY_ENABLE); + g_skype->Delete(SETUPKEY_HTTPS_PROXY_ADDR); + g_skype->Delete(SETUPKEY_HTTPS_PROXY_USER); + g_skype->Delete(SETUPKEY_HTTPS_PROXY_PWD); + g_skype->Delete(SETUPKEY_SOCKS_PROXY_ENABLE); + g_skype->Delete(SETUPKEY_SOCKS_PROXY_ADDR); + g_skype->Delete(SETUPKEY_SOCKS_PROXY_USER); + g_skype->Delete(SETUPKEY_SOCKS_PROXY_PWD); break; } } diff --git a/protocols/Skype/src/skype_proto.cpp b/protocols/Skype/src/skype_proto.cpp index dac3c2dd7d..e3b4edefd2 100644 --- a/protocols/Skype/src/skype_proto.cpp +++ b/protocols/Skype/src/skype_proto.cpp @@ -2,7 +2,7 @@ CSkypeProto::CSkypeProto(const char* protoName, const TCHAR* userName) { - ProtoConstructor(this, protoName, userName); + ::ProtoConstructor(this, protoName, userName); this->rememberPassword = false; @@ -15,7 +15,7 @@ CSkypeProto::CSkypeProto(const char* protoName, const TCHAR* userName) dbEventType.descr = "Call"; ::CallService(MS_DB_EVENT_REGISTERTYPE, 0, (LPARAM)&dbEventType); - this->HookEvent(ME_MSG_PRECREATEEVENT, &CSkypeProto::OnMessagePreCreate); + //this->HookEvent(ME_MSG_PRECREATEEVENT, &CSkypeProto::OnMessagePreCreate); this->CreateServiceObj(PS_CREATEACCMGRUI, &CSkypeProto::OnAccountManagerInit); // Chat API @@ -46,7 +46,7 @@ HANDLE __cdecl CSkypeProto::AddToList(int flags, PROTOSEARCHRESULT* psr) { //fixme CContact::Ref contact; - this->skype->GetContact(::mir_u2a(psr->id), contact); + g_skype->GetContact(::mir_u2a(psr->id), contact); return this->AddContact(contact); return 0; } @@ -128,7 +128,7 @@ int __cdecl CSkypeProto::AuthRequest(HANDLE hContact, const TCHAR* szMessage) { CContact::Ref contact; SEString sid(::mir_u2a(this->GetSettingString(hContact, SKYPE_SETTINGS_LOGIN))); - if (this->skype->GetContact(sid, contact)) + if (g_skype->GetContact(sid, contact)) { contact->SetBuddyStatus(Contact::AUTHORIZED_BY_ME); contact->SendAuthRequest(::mir_utf8encodeW(szMessage)); @@ -146,7 +146,7 @@ HANDLE __cdecl CSkypeProto::FileAllow( HANDLE hContact, HANDLE hTransfer, const { uint oid = (uint)hTransfer; - MessageRef message(oid); + CMessage *message = new CMessage(oid, g_skype); CTransfer::Refs transfers; message->GetTransfers(transfers); @@ -160,9 +160,12 @@ HANDLE __cdecl CSkypeProto::FileAllow( HANDLE hContact, HANDLE hTransfer, const if (!transfers[i]->Accept(::mir_u2a(fullPath), success) || !success) { // todo: write to log! + delete message; return 0; } } + + delete message; return hTransfer; } @@ -225,7 +228,7 @@ DWORD_PTR __cdecl CSkypeProto:: GetCaps(int type, HANDLE hContact) return PF4_FORCEAUTH | PF4_FORCEADDED | PF4_SUPPORTTYPING | PF4_AVATARS | PF4_OFFLINEFILES | PF4_IMSENDUTF | PF4_IMSENDOFFLINE; case PFLAG_UNIQUEIDTEXT: - return (DWORD_PTR)::Translate("Skype Name"); + return (DWORD_PTR)::Translate("g_skype Name"); case PFLAG_UNIQUEIDSETTING: return (DWORD_PTR)SKYPE_SETTINGS_LOGIN; default: @@ -286,9 +289,9 @@ int __cdecl CSkypeProto::RecvMsg( HANDLE hContact, PROTORECVEVENT* pre) ::db_unset(hContact, "CList", "Hidden"); this->UserIsTyping(hContact, PROTOTYPE_SELFTYPING_OFF); - int length = ::strlen(pre->szMessage) + 1; + /*int length = ::strlen(pre->szMessage) + 1; pre->szMessage = (char *)::mir_realloc(pre->szMessage, length + 32); - ::memcpy(&pre->szMessage[length], (char *)pre->lParam, 32); + ::memcpy(&pre->szMessage[length], (char *)pre->lParam, 32);*/ return ::Proto_RecvMessage(hContact, pre); } @@ -305,7 +308,8 @@ HANDLE __cdecl CSkypeProto::SendFile( HANDLE hContact, const TCHAR* szDescriptio mir_ptr sid(::db_get_wsa(hContact, this->m_szModuleName, SKYPE_SETTINGS_LOGIN)); targets.append((char *)mir_ptr(::mir_utf8encodeW(sid))); - CConversation::Ref conversation = CConversation::FindBySid(this->skype, sid); + CConversation::Ref conversation; + g_skype->GetConversationByParticipants(targets, conversation); SEFilenameList fileList; for (int i = 0; ppszFiles[i]; i++) @@ -337,9 +341,13 @@ HANDLE __cdecl CSkypeProto::SendFile( HANDLE hContact, const TCHAR* szDescriptio int __cdecl CSkypeProto::SendMsg(HANDLE hContact, int flags, const char* msg) { - CConversation::Ref conversation = CConversation::FindBySid( - this->skype, - ::mir_ptr(::db_get_wsa(hContact, this->m_szModuleName, SKYPE_SETTINGS_LOGIN))); + SEStringList targets; + wchar_t * sid = ::db_get_wsa(hContact, this->m_szModuleName, SKYPE_SETTINGS_LOGIN); + SEString identity = ::mir_u2a(sid); + targets.append(identity); + + CConversation::Ref conversation; + g_skype->GetConversationByParticipants(targets, conversation); if (conversation) { @@ -428,7 +436,13 @@ int __cdecl CSkypeProto::UserIsTyping( HANDLE hContact, int type ) mir_ptr sid(::db_get_wsa(hContact, this->m_szModuleName, SKYPE_SETTINGS_LOGIN)); if (::wcsicmp(sid, this->login) != 0) { - CConversation::Ref conversation = CConversation::FindBySid(this->skype, sid); + SEStringList targets; + mir_ptr sid(::db_get_wsa(hContact, this->m_szModuleName, SKYPE_SETTINGS_LOGIN)); + targets.append((char *)mir_ptr(::mir_utf8encodeW(sid))); + + CConversation::Ref conversation; + g_skype->GetConversationByParticipants(targets, conversation); + if (conversation) { switch (type) diff --git a/protocols/Skype/src/skype_proto.h b/protocols/Skype/src/skype_proto.h index 017e3b7af9..4f72dc8096 100644 --- a/protocols/Skype/src/skype_proto.h +++ b/protocols/Skype/src/skype_proto.h @@ -1,7 +1,7 @@ #pragma once #include "skype.h" -#include +#include "skypekit\skype.h" typedef void (__cdecl CSkypeProto::* SkypeThreadFunc) (void*); typedef int (__cdecl CSkypeProto::* SkypeEventFunc)(WPARAM, LPARAM); @@ -119,7 +119,7 @@ struct InviteChatParam struct PasswordRequestBoxParam { wchar_t *login; - wchar_t *password; + char *password; bool rememberPassword; bool showRememberPasswordBox; @@ -138,8 +138,8 @@ struct PasswordRequestBoxParam struct PasswordChangeBoxParam { - wchar_t *password; - wchar_t *password2; + char *password; + char *password2; PasswordChangeBoxParam() { } @@ -153,7 +153,7 @@ struct PasswordChangeBoxParam struct CSkypeProto : public PROTO_INTERFACE { public: - CSkypeProto(const char *, const wchar_t *); + CSkypeProto(const char *protoName, const wchar_t *userName); ~CSkypeProto(); // PROTO_INTERFACE @@ -242,7 +242,6 @@ public: bool IsOnline(); protected: - CSkype *skype; CAccount::Ref account; CContact::Refs contactList; CTransfer::Refs transferList; @@ -253,7 +252,7 @@ protected: void OnAccountChanged(int prop); wchar_t *login; - wchar_t *password; + char *password; bool rememberPassword; bool RequestPassword(PasswordRequestBoxParam ¶m); @@ -279,12 +278,12 @@ protected: // messages void OnMessage(CConversation::Ref conversation, CMessage::Ref message); - void OnMessageSended(CConversation::Ref conversation, CMessage::Ref message); - void OnMessageReceived(CConversation::Ref conversation, CMessage::Ref message); + void OnMessageSended(CConversation::Ref &conversation, CMessage::Ref &message); + void OnMessageReceived(CConversation::Ref &conversation, CMessage::Ref &message); // transfer - void OnFile(CConversation::Ref conversation, CMessage::Ref message); - void OnTransferChanged(int prop, CTransfer::Ref transfer); + void OnFile(CConversation::Ref &conversation, CMessage::Ref &message); + void OnTransferChanged(CTransfer::Ref transfer, int prop); // chat static wchar_t* Groups[]; @@ -329,7 +328,7 @@ protected: void UpdateContactLastEventDate(SEObject *obj, HANDLE hContact); void OnSearchCompleted(HANDLE hSearch); - void OnContactFinded(HANDLE hSearch, CContact::Ref contact); + void OnContactFinded(CContact::Ref contact, HANDLE hSearch); void OnContactChanged(CContact::Ref contact, int prop); void OnContactListChanged(const ContactRef& contact); @@ -396,10 +395,6 @@ protected: SEBinary GetAvatarBinary(wchar_t *path); - // runtime - void InitSkype(); - void UninitSkype(); - // instances static LIST instanceList; static int CompareProtos(const CSkypeProto *p1, const CSkypeProto *p2); diff --git a/protocols/Skype/src/skype_runtime.cpp b/protocols/Skype/src/skype_runtime.cpp deleted file mode 100644 index 45c1def4a2..0000000000 --- a/protocols/Skype/src/skype_runtime.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "skype_proto.h" - -void CSkypeProto::InitSkype() -{ - wchar_t *profileName = ::Utils_ReplaceVarsT(L"%miranda_profilename%"); - wchar_t *dbPath = ::Utils_ReplaceVarsT(L"%miranda_userdata%\\SkypeKit\\"); - - this->skype = CSkype::GetInstance(g_hInstance, profileName, dbPath); - this->skype->SetOnMessageCallback( - (CSkype::OnMessaged)&CSkypeProto::OnMessage, - this); -} - -void CSkypeProto::UninitSkype() -{ - this->skype->stop(); - delete this->skype; -} \ No newline at end of file diff --git a/protocols/Skype/src/skype_subclassing.cpp b/protocols/Skype/src/skype_subclassing.cpp deleted file mode 100644 index a055f0782f..0000000000 --- a/protocols/Skype/src/skype_subclassing.cpp +++ /dev/null @@ -1,574 +0,0 @@ -#include "skype.h" - -#include "base64/base64.h" -extern "C" -{ -#include "aes\aes.h" -} - -// CSkype - -CSkype::CSkype(int num_threads) : Skype(num_threads) -{ - this->proto = NULL; - this->onMessagedCallback = NULL; -} - -CAccount* CSkype::newAccount(int oid) -{ - return new CAccount(oid, this); -} - -CContactGroup* CSkype::newContactGroup(int oid) -{ - return new CContactGroup(oid, this); -} - -CContact* CSkype::newContact(int oid) -{ - return new CContact(oid, this); -} - -CConversation* CSkype::newConversation(int oid) -{ - return new CConversation(oid, this); -} - -CParticipant* CSkype::newParticipant(int oid) -{ - return new CParticipant(oid, this); -} - -CMessage* CSkype::newMessage(int oid) -{ - return new CMessage(oid, this); -} - -CTransfer* CSkype::newTransfer(int oid) -{ - return new CTransfer(oid, this); -} - -CContactSearch* CSkype::newContactSearch(int oid) -{ - return new CContactSearch(oid, this); -} - -void CSkype::OnMessage ( - const MessageRef & message, - const bool & changesInboxTimestamp, - const MessageRef & supersedesHistoryMessage, - const ConversationRef & conversation) -{ - /*uint now; - skype->GetUnixTimestamp(now); - conversation->SetConsumedHorizon(now);*/ - - if (this->proto) - (proto->*onMessagedCallback)(conversation->ref(), message->ref()); -} - -void CSkype::SetOnMessageCallback(OnMessaged callback, CSkypeProto* proto) -{ - this->proto = proto; - this->onMessagedCallback = callback; -} - -BOOL CSkype::IsRunAsAdmin() -{ - BOOL fIsRunAsAdmin = FALSE; - DWORD dwError = ERROR_SUCCESS; - PSID pAdministratorsGroup = NULL; - - // Allocate and initialize a SID of the administrators group. - SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY; - if ( !AllocateAndInitializeSid( - &NtAuthority, - 2, - SECURITY_BUILTIN_DOMAIN_RID, - DOMAIN_ALIAS_RID_ADMINS, - 0, 0, 0, 0, 0, 0, - &pAdministratorsGroup)) - { - dwError = GetLastError(); - goto Cleanup; - } - - // Determine whether the SID of administrators group is enabled in - // the primary access token of the process. - if ( !CheckTokenMembership(NULL, pAdministratorsGroup, &fIsRunAsAdmin)) - { - dwError = GetLastError(); - goto Cleanup; - } - -Cleanup: - // Centralized cleanup for all allocated resources. - if (pAdministratorsGroup) - { - FreeSid(pAdministratorsGroup); - pAdministratorsGroup = NULL; - } - - // Throw the error if something failed in the function. - if (ERROR_SUCCESS != dwError) - { - throw dwError; - } - - return fIsRunAsAdmin; -} - -char *CSkype::LoadKeyPair(HINSTANCE hInstance) -{ - HRSRC hRes = FindResource(hInstance, MAKEINTRESOURCE(IDR_KEY), L"BIN"); - if (hRes) - { - HGLOBAL hResource = LoadResource(hInstance, hRes); - if (hResource) - { - aes_context ctx; - unsigned char key[128]; - - int basedecoded = Base64::Decode(MY_KEY, (char *)key, MAX_PATH); - ::aes_set_key(&ctx, key, 128); - memset(key, 0, sizeof(key)); - - basedecoded = ::SizeofResource(hInstance, hRes); - char *pData = (char *)hResource; - if (!pData) - return NULL; - - unsigned char *bufD = (unsigned char*)::malloc(basedecoded + 1); - unsigned char *tmpD = (unsigned char*)::malloc(basedecoded + 1); - basedecoded = Base64::Decode(pData, (char *)tmpD, basedecoded); - - for (int i = 0; i < basedecoded; i += 16) - aes_decrypt(&ctx, tmpD+i, bufD+i); - - ::free(tmpD); - bufD[basedecoded] = 0; //cert should be null terminated - return (char *)bufD; - } - return NULL; - } - return NULL; -} - -int CSkype::StartSkypeRuntime(HINSTANCE hInstance, const wchar_t *profileName, int &port, const wchar_t *dbPath) -{ - STARTUPINFO cif; - PROCESS_INFORMATION pi; - wchar_t param[128]; - - ::ZeroMemory(&cif, sizeof(STARTUPINFO)); - cif.cb = sizeof(STARTUPINFO); - cif.dwFlags = STARTF_USESHOWWINDOW; - cif.wShowWindow = SW_HIDE; - - //HRSRC hRes; - //HGLOBAL hResource; - wchar_t fileName[MAX_PATH]; - - HRSRC hRes = ::FindResource(hInstance, MAKEINTRESOURCE(/*IDR_RUNTIME*/102), L"BIN"); - if (hRes) - { - HGLOBAL hResource = ::LoadResource(hInstance, hRes); - if (hResource) - { - HANDLE hFile; - char *pData = (char *)LockResource(hResource); - DWORD dwSize = SizeofResource(hInstance, hRes), written = 0; - ::GetModuleFileName(hInstance, fileName, MAX_PATH); - - wchar_t *skypeKitPath = ::wcsrchr(fileName, '\\'); - if (skypeKitPath != NULL) - *skypeKitPath = 0; - ::swprintf(fileName, SIZEOF(fileName), L"%s\\%s", fileName, L"SkypeKit.exe"); - if ( !::PathFileExists(fileName)) - { - if ((hFile = ::CreateFile( - fileName, - GENERIC_WRITE, - 0, - NULL, - CREATE_ALWAYS, - FILE_ATTRIBUTE_NORMAL, - 0)) != INVALID_HANDLE_VALUE) - { - ::WriteFile(hFile, (void *)pData, dwSize, &written, NULL); - ::CloseHandle(hFile); - } - else - { - // Check the current process's "run as administrator" status. - // Elevate the process if it is not run as administrator. - if (!CSkype::IsRunAsAdmin()) - { - wchar_t path[MAX_PATH], cmdLine[100]; - ::GetModuleFileName(NULL, path, ARRAYSIZE(path)); - ::swprintf( - cmdLine, - SIZEOF(cmdLine), - L" /restart:%d /profile=%s", - ::GetCurrentProcessId(), - profileName); - - // Launch itself as administrator. - SHELLEXECUTEINFO sei = { sizeof(sei) }; - sei.lpVerb = L"runas"; - sei.lpFile = path; - sei.lpParameters = cmdLine; - //sei.hwnd = hDlg; - sei.nShow = SW_NORMAL; - - if ( !::ShellExecuteEx(&sei)) - { - DWORD dwError = ::GetLastError(); - if (dwError == ERROR_CANCELLED) - { - // The user refused to allow privileges elevation. - // Do nothing ... - } - } - //else - //{ - // //DestroyWindow(hDlg); // Quit itself - // ::CallService("CloseAction", 0, 0); - //} - } - return 0; - } - } - } - } - - PROCESSENTRY32 entry; - entry.dwSize = sizeof(PROCESSENTRY32); - - HANDLE snapshot = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL); - if (::Process32First(snapshot, &entry) == TRUE) { - while (::Process32Next(snapshot, &entry) == TRUE) { - if (::wcsicmp(entry.szExeFile, L"SkypeKit.exe") == 0) { - HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID); - port += rand() % 8963 + 1000; - ::CloseHandle(hProcess); - break; - } - } - } - ::CloseHandle(snapshot); - - //::swprintf(param, SIZEOF(param), L"-p -P %d -f %s", port, dbPath); - ::swprintf(param, SIZEOF(param), L"-p -P %d", port); - int startingrt = ::CreateProcess( - fileName, param, - NULL, NULL, FALSE, - CREATE_NEW_CONSOLE, - NULL, NULL, &cif, &pi); - DWORD rterr = GetLastError(); - - //if (startingrt && rterr == ERROR_SUCCESS) - //return 1; - //else - //return 0; - return startingrt; -} - -CSkype *CSkype::GetInstance(HINSTANCE hInstance, const wchar_t *profileName, const wchar_t *dbPath) -{ - int port = 8963; - if (!CSkype::StartSkypeRuntime(hInstance, profileName, port, dbPath)) return NULL; - - char *keyPair = CSkype::LoadKeyPair(hInstance); - if (!keyPair) - return NULL; - - CSkype *skype = new CSkype(); - TransportInterface::Status status = skype->init(keyPair, "127.0.0.1", port); - if (status != TransportInterface::OK) - return NULL; - skype->start(); - - free(keyPair); - - //this->skype->SetOnMessageCallback((CSkype::OnMessaged)&CSkypeProto::OnMessage, this); - return skype; -} - -// CAccount - -CAccount::CAccount(unsigned int oid, SERootObject* root) : Account(oid, root) -{ - this->proto = NULL; - this->callback == NULL; -} - -void CAccount::SetOnAccountChangedCallback(OnAccountChanged callback, CSkypeProto* proto) -{ - this->skype = (CSkype *)root; - - this->proto = proto; - this->callback = callback; -} - -bool CAccount::SetAvatar(SEBinary avatar, Skype::VALIDATERESULT &result) -{ - int fbl; - if (!this->skype->ValidateAvatar(avatar, result, fbl) || result != Skype::VALIDATED_OK) - return false; - - if (!this->SetBinProperty(Account::P_AVATAR_IMAGE, avatar)) - return false; - - return true; -} - -void CAccount::OnChange(int prop) -{ - if (this->proto) - (proto->*callback)(prop); -} - -// CContactGroup - -CContactGroup::CContactGroup(unsigned int oid, SERootObject* root) : ContactGroup(oid, root) -{ - this->proto = NULL; - this->callback == NULL; -} - -void CContactGroup::SetOnContactListChangedCallback(OnContactListChanged callback, CSkypeProto* proto) -{ - this->proto = proto; - this->callback = callback; -} - -void CContactGroup::OnChange(const ContactRef& contact) -{ - if (this->proto) - (proto->*callback)(contact); -} - -// CContactSearch - -CContactSearch::CContactSearch(unsigned int oid, SERootObject* root) : ContactSearch(oid, root) -{ - this->proto = NULL; - this->SearchCompletedCallback == NULL; - this->ContactFindedCallback == NULL; -} - -void CContactSearch::OnChange(int prop) -{ - if (prop == P_CONTACT_SEARCH_STATUS) - { - CContactSearch::STATUS status; - this->GetPropContactSearchStatus(status); - if (status == FINISHED || status == FAILED) - { - this->isSeachFinished = true; - if (this->proto) - (proto->*SearchCompletedCallback)(this->hSearch); - } - } -} - -void CContactSearch::OnNewResult(const ContactRef& contact, const uint& rankValue) -{ - if (this->proto) - (proto->*ContactFindedCallback)(this->hSearch, contact->ref()); -} - -void CContactSearch::BlockWhileSearch() -{ - this->isSeachFinished = false; - this->isSeachFailed = false; - while (!this->isSeachFinished && !this->isSeachFailed) - Sleep(1); -} - -void CContactSearch::SetProtoInfo(CSkypeProto* proto, HANDLE hSearch) -{ - this->proto = proto; - this->hSearch = hSearch; -} - -void CContactSearch::SetOnSearchCompleatedCallback(OnSearchCompleted callback) -{ - this->SearchCompletedCallback = callback; -} - -void CContactSearch::SetOnContactFindedCallback(OnContactFinded callback) -{ - this->ContactFindedCallback = callback; -} - -// CParticipant - -CParticipant::CParticipant(unsigned int oid, SERootObject* root) : Participant(oid, root) { } - -SEString CParticipant::GetRankName(CParticipant::RANK rank) -{ - char *result = NULL; - switch (rank) - { - case CParticipant::CREATOR: - result = "Creator"; - break; - case CParticipant::ADMIN: - result = "Admin"; - break; - case CParticipant::SPEAKER: - result = "Speaker"; - break; - case CParticipant::WRITER: - result = "Writer"; - break; - case CParticipant::SPECTATOR: - result = "Spectator"; - break; - case CParticipant::RETIRED: - result = "Retried"; - break; - case CParticipant::OUTLAW: - result = "Outlaw"; - break; - } - return result; -} - -// CContact - -CContact::CContact(unsigned int oid, SERootObject* root) : Contact(oid, root) -{ - this->proto = NULL; - this->callback == NULL; -} - -SEString CContact::GetSid() -{ - SEString result; - CContact::AVAILABILITY availability; - this->GetPropAvailability(availability); - if (availability == CContact::SKYPEOUT) - this->GetPropPstnnumber(result); - else - this->GetPropSkypename(result); - return result; -} - -SEString CContact::GetNick() -{ - SEString result; - CContact::AVAILABILITY availability; - this->GetPropAvailability(availability); - if (availability == CContact::SKYPEOUT) - result = this->GetSid(); - else - this->GetPropDisplayname(result); - return result; -} - -bool CContact::GetFullname(SEString &firstName, SEString &lastName) -{ - SEString fullname; - this->GetPropFullname(fullname); - int pos = fullname.find(" "); - if (pos && pos < fullname.length()) - { - firstName = fullname.substr(0, pos); - lastName = fullname.right(pos); - } - - firstName = fullname; - - return true; -} - -void CContact::SetOnContactChangedCallback(OnContactChanged callback, CSkypeProto* proto) -{ - this->proto = proto; - this->callback = callback; -} - -//bool CContact::SentAuthRequest(SEString message) -//{ -// this->SetBuddyStatus(Contact::AUTHORIZED_BY_ME); -// this->SendAuthRequest(message); -//} - -void CContact::OnChange(int prop) -{ - if (this->proto) - (proto->*callback)(this->ref(), prop); -} - -// Conversation - -CConversation::CConversation(unsigned int oid, SERootObject* root) : Conversation(oid, root) -{ - this->proto = NULL; - this->messageReceivedCallback = NULL; -} - -void CConversation::OnMessage(const MessageRef & message) -{ - if (this->proto) - (proto->*messageReceivedCallback)(message->ref()); -} - -void CConversation::OnChange(int prop) -{ - if (prop == Conversation::P_LOCAL_LIVESTATUS) - { - Conversation::LOCAL_LIVESTATUS liveStatus; - this->GetPropLocalLivestatus(liveStatus); - if (liveStatus == Conversation::RINGING_FOR_ME) - { - - } - } -} - -CConversation::Ref CConversation::FindBySid(CSkype *skype, const wchar_t *sid) -{ - SEString identity = ::mir_u2a(sid); - SEStringList participants; - participants.append(identity); - - CConversation::Ref conversation; - skype->GetConversationByParticipants(participants, conversation); - - return conversation; -} - -void CConversation::SetOnMessageReceivedCallback(OnMessageReceived callback, CSkypeProto* proto) -{ - this->proto = proto; - this->messageReceivedCallback = callback; -} - -// CMessage - -CMessage::CMessage(unsigned int oid, SERootObject* root) : Message(oid, root) { } - -// CTransfer - -CTransfer::CTransfer(unsigned int oid, SERootObject* root) : Transfer(oid, root) -{ - this->proto = NULL; - this->transferCallback = NULL; -} - -void CTransfer::SetOnTransferCallback(OnTransfer callback, CSkypeProto* proto) -{ - this->proto = proto; - this->transferCallback = callback; -} - -void CTransfer::OnChange(int prop) -{ - if (this->proto) - (proto->*transferCallback)(prop, this->ref()); -} \ No newline at end of file diff --git a/protocols/Skype/src/skype_subclassing.h b/protocols/Skype/src/skype_subclassing.h deleted file mode 100644 index 13fa95e7c3..0000000000 --- a/protocols/Skype/src/skype_subclassing.h +++ /dev/null @@ -1,201 +0,0 @@ -#pragma once - -#undef OCSP_REQUEST -#undef OCSP_RESPONSE - -#include - -struct CSkypeProto; - -class CSkype; - -class CMessage : public Message -{ -public: - typedef DRef Ref; - typedef DRefs Refs; - - CMessage(unsigned int oid, SERootObject* root); -}; - -class CTransfer : public Transfer -{ -public: - typedef void (CSkypeProto::* OnTransfer)(int prop, CTransfer::Ref transfer); - - typedef DRef Ref; - typedef DRefs Refs; - - CTransfer(unsigned int oid, SERootObject* p_root); - - void SetOnTransferCallback(OnTransfer callback, CSkypeProto* proto); - -private: - CSkypeProto* proto; - OnTransfer transferCallback; - - void OnChange(int prop); -}; - -class CParticipant : public Participant -{ -public: - typedef DRef Ref; - typedef DRefs Refs; - - CParticipant(unsigned int oid, SERootObject* root); - - static SEString GetRankName(CParticipant::RANK rank); -}; - -class CConversation : public Conversation -{ -public: - typedef void (CSkypeProto::* OnMessageReceived)(CMessage::Ref message); - - typedef DRef Ref; - typedef DRefs Refs; - - CConversation(unsigned int oid, SERootObject* root); - - static CConversation::Ref FindBySid(CSkype *skype, const wchar_t *sid); - - void SetOnMessageReceivedCallback(OnMessageReceived callback, CSkypeProto* proto); - -private: - CSkypeProto* proto; - OnMessageReceived messageReceivedCallback; - - void OnMessage(const MessageRef & message); - void OnChange(int prop); -}; - -class CContact : public Contact -{ -public: - typedef void (CSkypeProto::* OnContactChanged)(CContact::Ref contact, int); - - typedef DRef Ref; - typedef DRefs Refs; - - CContact(unsigned int oid, SERootObject* root); - - /*bool SentAuthRequest(SEString message);*/ - - SEString GetSid(); - SEString GetNick(); - bool GetFullname(SEString &firstName, SEString &lastName); - - void SetOnContactChangedCallback(OnContactChanged callback, CSkypeProto* proto); - -private: - CSkypeProto* proto; - OnContactChanged callback; - - void OnChange(int prop); -}; - -class CContactSearch : public ContactSearch -{ -public: - typedef void (CSkypeProto::* OnSearchCompleted)(HANDLE hSearch); - typedef void (CSkypeProto::* OnContactFinded)(HANDLE hSearch, CContact::Ref contact); - - typedef DRef Ref; - typedef DRefs Refs; - - bool isSeachFinished; - bool isSeachFailed; - - CContactSearch(unsigned int oid, SERootObject* root); - - void OnChange(int prop); - void OnNewResult(const ContactRef& contact, const uint& rankValue); - - void SetProtoInfo(CSkypeProto* proto, HANDLE hSearch); - void SetOnSearchCompleatedCallback(OnSearchCompleted callback); - void SetOnContactFindedCallback(OnContactFinded callback); - - void BlockWhileSearch(); -private: - HANDLE hSearch; - CSkypeProto* proto; - OnSearchCompleted SearchCompletedCallback; - OnContactFinded ContactFindedCallback; -}; - -class CContactGroup : public ContactGroup -{ -public: - typedef void (CSkypeProto::* OnContactListChanged)(const ContactRef& contact); - - typedef DRef Ref; - typedef DRefs Refs; - CContactGroup(unsigned int oid, SERootObject* root); - - void SetOnContactListChangedCallback(OnContactListChanged callback, CSkypeProto* proto); - -private: - CSkypeProto* proto; - OnContactListChanged callback; - - void OnChange(const ContactRef& contact); -}; - -class CAccount : public Account -{ -public: - typedef void (CSkypeProto::* OnAccountChanged)(int); - - typedef DRef Ref; - typedef DRefs Refs; - - CAccount(unsigned int oid, SERootObject* root); - - bool SetAvatar(SEBinary avatar, Skype::VALIDATERESULT &result); - - void SetOnAccountChangedCallback(OnAccountChanged callback, CSkypeProto* proto); - -private: - CSkype *skype; - CSkypeProto* proto; - OnAccountChanged callback; - void OnChange(int prop); -}; - -class CSkype : public Skype -{ -public: - typedef void (CSkypeProto::* OnMessaged)(CConversation::Ref conversation, CMessage::Ref message); - - CAccount* newAccount(int oid); - CContactGroup* newContactGroup(int oid); - CConversation* newConversation(int oid); - CContactSearch* newContactSearch(int oid); - CParticipant* newParticipant(int oid); - CContact* newContact(int oid); - CMessage* newMessage(int oid); - CTransfer* newTransfer(int oid); - - CConversation::Refs inbox; - - CSkype(int num_threads = 1); - - void SetOnMessageCallback(OnMessaged callback, CSkypeProto* proto); - - static CSkype *GetInstance(HINSTANCE hInstance, const wchar_t *profileName, const wchar_t *dbPath); - -private: - CSkypeProto* proto; - OnMessaged onMessagedCallback; - - void OnMessage( - const MessageRef & message, - const bool & changesInboxTimestamp, - const MessageRef & supersedesHistoryMessage, - const ConversationRef & conversation); - - static BOOL IsRunAsAdmin(); - static char *LoadKeyPair(HINSTANCE hInstance); - static int StartSkypeRuntime(HINSTANCE hInstance, const wchar_t *profileName, int &port, const wchar_t *dbPath); -}; \ No newline at end of file diff --git a/protocols/Skype/src/skype_utils.cpp b/protocols/Skype/src/skype_utils.cpp index fe8141d83f..199e3b3c8f 100644 --- a/protocols/Skype/src/skype_utils.cpp +++ b/protocols/Skype/src/skype_utils.cpp @@ -281,14 +281,6 @@ LanguagesListEntry CSkypeProto::languages[] = {"Zulu", "zu"} }; - -void CSkypeProto::FakeAsync(void *param) -{ - ::Sleep(100); - ::CallService(MS_PROTO_BROADCASTACK, 0, (LPARAM)param); - ::mir_free(param); -} - int CSkypeProto::DetectAvatarFormatBuffer(const char *pBuffer) { if (!strncmp(pBuffer, "%PNG", 4)) @@ -399,20 +391,23 @@ void CSkypeProto::HookEvent(const char* szEvent, SkypeEventFunc handler) ::HookEventObj(szEvent, (MIRANDAHOOKOBJ)*( void**)&handler, this); } +void CSkypeProto::FakeAsync(void *param) +{ + ::Sleep(100); + ::CallService(MS_PROTO_BROADCASTACK, 0, (LPARAM)param); + ::mir_free(param); +} + int CSkypeProto::SendBroadcast(HANDLE hContact, int type, int result, HANDLE hProcess, LPARAM lParam) { - /*ACKDATA ack = { sizeof(ACKDATA) }; - ack.szModule = this->m_szModuleName; - ack.hContact = hContact; - ack.type = type; - ack.result = result; - ack.hProcess = hProcess; - ack.lParam = lParam; - - return ::CallService(MS_PROTO_BROADCASTACK, 0, (LPARAM)&ack);*/ return ::ProtoBroadcastAck(this->m_szModuleName, hContact, type, result, hProcess, lParam); } +int CSkypeProto::SendBroadcast(int type, int result, HANDLE hProcess, LPARAM lParam) +{ + return this->SendBroadcast(NULL, type, result, hProcess, lParam); +} + DWORD CSkypeProto::SendBroadcastAsync(HANDLE hContact, int type, int hResult, HANDLE hProcess, LPARAM lParam, size_t paramSize) { ACKDATA *ack = (ACKDATA *)::mir_calloc(sizeof(ACKDATA) + paramSize); diff --git a/protocols/Skype/src/skypekit/account.cpp b/protocols/Skype/src/skypekit/account.cpp new file mode 100644 index 0000000000..313b1cf50d --- /dev/null +++ b/protocols/Skype/src/skypekit/account.cpp @@ -0,0 +1,34 @@ +#include "account.h" +#include "skype.h" + +CAccount::CAccount(unsigned int oid, SERootObject* root) : Account(oid, root) +{ + this->proto = NULL; + this->callback == NULL; +} + +void CAccount::SetOnAccountChangedCallback(OnAccountChanged callback, CSkypeProto* proto) +{ + this->skype = (CSkype *)root; + + this->proto = proto; + this->callback = callback; +} + +bool CAccount::SetAvatar(SEBinary avatar, Skype::VALIDATERESULT &result) +{ + int fbl; + if (!this->skype->ValidateAvatar(avatar, result, fbl) || result != Skype::VALIDATED_OK) + return false; + + if (!this->SetBinProperty(Account::P_AVATAR_IMAGE, avatar)) + return false; + + return true; +} + +void CAccount::OnChange(int prop) +{ + if (this->proto) + (proto->*callback)(prop); +} \ No newline at end of file diff --git a/protocols/Skype/src/skypekit/account.h b/protocols/Skype/src/skypekit/account.h new file mode 100644 index 0000000000..b537019698 --- /dev/null +++ b/protocols/Skype/src/skypekit/account.h @@ -0,0 +1,26 @@ +#pragma once + +#include "common.h" + +class CSkype; + +class CAccount : public Account +{ +public: + typedef void (CSkypeProto::* OnAccountChanged)(int); + + typedef DRef Ref; + typedef DRefs Refs; + + CAccount(unsigned int oid, SERootObject* root); + + bool SetAvatar(SEBinary avatar, Skype::VALIDATERESULT &result); + + void SetOnAccountChangedCallback(OnAccountChanged callback, CSkypeProto* proto); + +private: + CSkype *skype; + CSkypeProto* proto; + OnAccountChanged callback; + void OnChange(int prop); +}; \ No newline at end of file diff --git a/protocols/Skype/src/skypekit/common.h b/protocols/Skype/src/skypekit/common.h new file mode 100644 index 0000000000..de00501ba3 --- /dev/null +++ b/protocols/Skype/src/skypekit/common.h @@ -0,0 +1,8 @@ +#pragma once + +#undef OCSP_REQUEST +#undef OCSP_RESPONSE + +#include + +struct CSkypeProto; \ No newline at end of file diff --git a/protocols/Skype/src/skypekit/contact.cpp b/protocols/Skype/src/skypekit/contact.cpp new file mode 100644 index 0000000000..2030eba250 --- /dev/null +++ b/protocols/Skype/src/skypekit/contact.cpp @@ -0,0 +1,59 @@ +#include "contact.h" + +CContact::CContact(unsigned int oid, SERootObject* root) : Contact(oid, root) +{ + this->proto = NULL; + this->callback == NULL; +} + +SEString CContact::GetSid() +{ + SEString result; + CContact::AVAILABILITY availability; + this->GetPropAvailability(availability); + if (availability == CContact::SKYPEOUT) + this->GetPropPstnnumber(result); + else + this->GetPropSkypename(result); + return result; +} + +SEString CContact::GetNick() +{ + SEString result; + CContact::AVAILABILITY availability; + this->GetPropAvailability(availability); + if (availability == CContact::SKYPEOUT) + result = this->GetSid(); + else + this->GetPropDisplayname(result); + return result; +} + +bool CContact::GetFullname(SEString &firstName, SEString &lastName) +{ + SEString fullname; + this->GetPropFullname(fullname); + int pos = fullname.find(" "); + if (pos && pos < fullname.length()) + { + firstName = fullname.substr(0, pos); + lastName = fullname.right(pos); + } + + firstName = fullname; + + return true; +} + +void CContact::SetOnContactChangedCallback(OnContactChanged callback, CSkypeProto* proto) +{ + this->proto = proto; + this->callback = callback; +} + +void CContact::OnChange(int prop) +{ + if (this->proto) + (proto->*callback)(this->ref(), prop); +} \ No newline at end of file diff --git a/protocols/Skype/src/skypekit/contact.h b/protocols/Skype/src/skypekit/contact.h new file mode 100644 index 0000000000..ee36764c50 --- /dev/null +++ b/protocols/Skype/src/skypekit/contact.h @@ -0,0 +1,26 @@ +#pragma once + +#include "common.h" + +class CContact : public Contact +{ +public: + typedef void (CSkypeProto::* OnContactChanged)(CContact::Ref contact, int); + + typedef DRef Ref; + typedef DRefs Refs; + + CContact(unsigned int oid, SERootObject* root); + + SEString GetSid(); + SEString GetNick(); + bool GetFullname(SEString &firstName, SEString &lastName); + + void SetOnContactChangedCallback(OnContactChanged callback, CSkypeProto* proto); + +private: + CSkypeProto* proto; + OnContactChanged callback; + + void OnChange(int prop); +}; \ No newline at end of file diff --git a/protocols/Skype/src/skypekit/conversation.cpp b/protocols/Skype/src/skypekit/conversation.cpp new file mode 100644 index 0000000000..e907c82d75 --- /dev/null +++ b/protocols/Skype/src/skypekit/conversation.cpp @@ -0,0 +1,3 @@ +#include "conversation.h" + +CConversation::CConversation(unsigned int oid, SERootObject* root) : Conversation(oid, root) { } diff --git a/protocols/Skype/src/skypekit/conversation.h b/protocols/Skype/src/skypekit/conversation.h new file mode 100644 index 0000000000..9fdc064962 --- /dev/null +++ b/protocols/Skype/src/skypekit/conversation.h @@ -0,0 +1,15 @@ +#pragma once + +#include "common.h" + +class CConversation : public Conversation +{ +public: + typedef DRef Ref; + typedef DRefs Refs; + + CConversation(unsigned int oid, SERootObject* root); + +private: + CSkypeProto* proto; +}; \ No newline at end of file diff --git a/protocols/Skype/src/skypekit/group.cpp b/protocols/Skype/src/skypekit/group.cpp new file mode 100644 index 0000000000..f547563bcf --- /dev/null +++ b/protocols/Skype/src/skypekit/group.cpp @@ -0,0 +1,19 @@ +#include "group.h" + +CContactGroup::CContactGroup(unsigned int oid, SERootObject* root) : ContactGroup(oid, root) +{ + this->proto = NULL; + this->callback == NULL; +} + +void CContactGroup::SetOnContactListChangedCallback(OnContactListChanged callback, CSkypeProto* proto) +{ + this->proto = proto; + this->callback = callback; +} + +void CContactGroup::OnChange(const ContactRef &contact) +{ + if (this->proto) + (proto->*callback)(contact); +} \ No newline at end of file diff --git a/protocols/Skype/src/skypekit/group.h b/protocols/Skype/src/skypekit/group.h new file mode 100644 index 0000000000..ff395594a5 --- /dev/null +++ b/protocols/Skype/src/skypekit/group.h @@ -0,0 +1,22 @@ +#pragma once + +#include "common.h" +#include "contact.h" + +class CContactGroup : public ContactGroup +{ +public: + typedef void (CSkypeProto::* OnContactListChanged)(CContact::Ref contact); + + typedef DRef Ref; + typedef DRefs Refs; + CContactGroup(unsigned int oid, SERootObject* root); + + void SetOnContactListChangedCallback(OnContactListChanged callback, CSkypeProto* proto); + +private: + CSkypeProto* proto; + OnContactListChanged callback; + + void OnChange(const ContactRef &contact); +}; \ No newline at end of file diff --git a/protocols/Skype/src/skypekit/message.cpp b/protocols/Skype/src/skypekit/message.cpp new file mode 100644 index 0000000000..ce96de0c01 --- /dev/null +++ b/protocols/Skype/src/skypekit/message.cpp @@ -0,0 +1,3 @@ +#include "message.h" + +CMessage::CMessage(unsigned int oid, SERootObject* root) : Message(oid, root) { } diff --git a/protocols/Skype/src/skypekit/message.h b/protocols/Skype/src/skypekit/message.h new file mode 100644 index 0000000000..6bf1eda497 --- /dev/null +++ b/protocols/Skype/src/skypekit/message.h @@ -0,0 +1,12 @@ +#pragma once + +#include "common.h" + +class CMessage : public Message +{ +public: + typedef DRef Ref; + typedef DRefs Refs; + + CMessage(unsigned int oid, SERootObject* root); +}; \ No newline at end of file diff --git a/protocols/Skype/src/skypekit/participant.cpp b/protocols/Skype/src/skypekit/participant.cpp new file mode 100644 index 0000000000..8b25254a0e --- /dev/null +++ b/protocols/Skype/src/skypekit/participant.cpp @@ -0,0 +1,33 @@ +#include "participant.h" + +CParticipant::CParticipant(unsigned int oid, SERootObject* root) : Participant(oid, root) { } + +SEString CParticipant::GetRankName(CParticipant::RANK rank) +{ + char *result = NULL; + switch (rank) + { + case CParticipant::CREATOR: + result = "Creator"; + break; + case CParticipant::ADMIN: + result = "Admin"; + break; + case CParticipant::SPEAKER: + result = "Speaker"; + break; + case CParticipant::WRITER: + result = "Writer"; + break; + case CParticipant::SPECTATOR: + result = "Spectator"; + break; + case CParticipant::RETIRED: + result = "Retried"; + break; + case CParticipant::OUTLAW: + result = "Outlaw"; + break; + } + return result; +} \ No newline at end of file diff --git a/protocols/Skype/src/skypekit/participant.h b/protocols/Skype/src/skypekit/participant.h new file mode 100644 index 0000000000..3a09d28017 --- /dev/null +++ b/protocols/Skype/src/skypekit/participant.h @@ -0,0 +1,14 @@ +#pragma once + +#include "common.h" + +class CParticipant : public Participant +{ +public: + typedef DRef Ref; + typedef DRefs Refs; + + CParticipant(unsigned int oid, SERootObject* root); + + static SEString GetRankName(CParticipant::RANK rank); +}; \ No newline at end of file diff --git a/protocols/Skype/src/skypekit/search.cpp b/protocols/Skype/src/skypekit/search.cpp new file mode 100644 index 0000000000..362e412c86 --- /dev/null +++ b/protocols/Skype/src/skypekit/search.cpp @@ -0,0 +1,53 @@ +#include "search.h" + +CContactSearch::CContactSearch(unsigned int oid, SERootObject* root) : ContactSearch(oid, root) +{ + this->proto = NULL; + this->SearchCompletedCallback == NULL; + this->ContactFindedCallback == NULL; +} + +void CContactSearch::OnChange(int prop) +{ + if (prop == P_CONTACT_SEARCH_STATUS) + { + CContactSearch::STATUS status; + this->GetPropContactSearchStatus(status); + if (status == FINISHED || status == FAILED) + { + this->isSeachFinished = true; + if (this->proto) + (proto->*SearchCompletedCallback)(this->hSearch); + } + } +} + +void CContactSearch::OnNewResult(const ContactRef &contact, const uint &rankValue) +{ + if (this->proto) + (proto->*ContactFindedCallback)(contact, this->hSearch); +} + +void CContactSearch::BlockWhileSearch() +{ + this->isSeachFinished = false; + this->isSeachFailed = false; + while (!this->isSeachFinished && !this->isSeachFailed) + Sleep(1); +} + +void CContactSearch::SetProtoInfo(CSkypeProto* proto, HANDLE hSearch) +{ + this->proto = proto; + this->hSearch = hSearch; +} + +void CContactSearch::SetOnSearchCompleatedCallback(OnSearchCompleted callback) +{ + this->SearchCompletedCallback = callback; +} + +void CContactSearch::SetOnContactFindedCallback(OnContactFinded callback) +{ + this->ContactFindedCallback = callback; +} \ No newline at end of file diff --git a/protocols/Skype/src/skypekit/search.h b/protocols/Skype/src/skypekit/search.h new file mode 100644 index 0000000000..60b920130d --- /dev/null +++ b/protocols/Skype/src/skypekit/search.h @@ -0,0 +1,33 @@ +#pragma once + +#include "common.h" +#include "contact.h" + +class CContactSearch : public ContactSearch +{ +public: + typedef void (CSkypeProto::* OnSearchCompleted)(HANDLE hSearch); + typedef void (CSkypeProto::* OnContactFinded)(CContact::Ref contact, HANDLE hSearch); + + typedef DRef Ref; + typedef DRefs Refs; + + bool isSeachFinished; + bool isSeachFailed; + + CContactSearch(unsigned int oid, SERootObject* root); + + void OnChange(int prop); + void OnNewResult(const ContactRef &contact, const uint &rankValue); + + void SetProtoInfo(CSkypeProto* proto, HANDLE hSearch); + void SetOnSearchCompleatedCallback(OnSearchCompleted callback); + void SetOnContactFindedCallback(OnContactFinded callback); + + void BlockWhileSearch(); +private: + HANDLE hSearch; + CSkypeProto* proto; + OnSearchCompleted SearchCompletedCallback; + OnContactFinded ContactFindedCallback; +}; \ No newline at end of file diff --git a/protocols/Skype/src/skypekit/skype.cpp b/protocols/Skype/src/skypekit/skype.cpp new file mode 100644 index 0000000000..fe22f548c8 --- /dev/null +++ b/protocols/Skype/src/skypekit/skype.cpp @@ -0,0 +1,63 @@ +#include "skype.h" + +CSkype::CSkype(int num_threads) : Skype(num_threads) +{ + this->proto = NULL; + this->onMessagedCallback = NULL; +} + +CAccount* CSkype::newAccount(int oid) +{ + return new CAccount(oid, this); +} + +CContactGroup* CSkype::newContactGroup(int oid) +{ + return new CContactGroup(oid, this); +} + +CContact* CSkype::newContact(int oid) +{ + return new CContact(oid, this); +} + +CConversation* CSkype::newConversation(int oid) +{ + return new CConversation(oid, this); +} + +CParticipant* CSkype::newParticipant(int oid) +{ + return new CParticipant(oid, this); +} + +CMessage* CSkype::newMessage(int oid) +{ + return new CMessage(oid, this); +} + +CTransfer* CSkype::newTransfer(int oid) +{ + return new CTransfer(oid, this); +} + +CContactSearch* CSkype::newContactSearch(int oid) +{ + return new CContactSearch(oid, this); +} + +void CSkype::OnMessage ( + const MessageRef & message, + const bool & changesInboxTimestamp, + const MessageRef & supersedesHistoryMessage, + const ConversationRef & conversation) +{ + if (this->proto) + (proto->*onMessagedCallback)(conversation, message); +} + +void CSkype::SetOnMessageCallback(OnMessaged callback, CSkypeProto* proto) +{ + this->proto = proto; + this->onMessagedCallback = callback; +} \ No newline at end of file diff --git a/protocols/Skype/src/skypekit/skype.h b/protocols/Skype/src/skypekit/skype.h new file mode 100644 index 0000000000..faac4f3d65 --- /dev/null +++ b/protocols/Skype/src/skypekit/skype.h @@ -0,0 +1,41 @@ +#pragma once + +#include "common.h" + +#include "group.h" +#include "search.h" +#include "account.h" +#include "contact.h" +#include "message.h" +#include "transfer.h" +#include "participant.h" +#include "conversation.h" + +class CSkype : public Skype +{ +public: + typedef void (CSkypeProto::* OnMessaged)(CConversation::Ref conversation, CMessage::Ref message); + + CAccount* newAccount(int oid); + CContactGroup* newContactGroup(int oid); + CConversation* newConversation(int oid); + CContactSearch* newContactSearch(int oid); + CParticipant* newParticipant(int oid); + CContact* newContact(int oid); + CMessage* newMessage(int oid); + CTransfer* newTransfer(int oid); + + CSkype(int num_threads = 1); + + void SetOnMessageCallback(OnMessaged callback, CSkypeProto* proto); + +private: + CSkypeProto* proto; + OnMessaged onMessagedCallback; + + void OnMessage( + const MessageRef & message, + const bool & changesInboxTimestamp, + const MessageRef & supersedesHistoryMessage, + const ConversationRef & conversation); +}; \ No newline at end of file diff --git a/protocols/Skype/src/skypekit/transfer.cpp b/protocols/Skype/src/skypekit/transfer.cpp new file mode 100644 index 0000000000..986a5bcbab --- /dev/null +++ b/protocols/Skype/src/skypekit/transfer.cpp @@ -0,0 +1,19 @@ +#include "transfer.h" + +CTransfer::CTransfer(unsigned int oid, SERootObject* root) : Transfer(oid, root) +{ + this->proto = NULL; + this->transferCallback = NULL; +} + +void CTransfer::SetOnTransferCallback(OnTransfer callback, CSkypeProto* proto) +{ + this->proto = proto; + this->transferCallback = callback; +} + +void CTransfer::OnChange(int prop) +{ + if (this->proto) + (proto->*transferCallback)(this->ref(), prop); +} \ No newline at end of file diff --git a/protocols/Skype/src/skypekit/transfer.h b/protocols/Skype/src/skypekit/transfer.h new file mode 100644 index 0000000000..d327c496b0 --- /dev/null +++ b/protocols/Skype/src/skypekit/transfer.h @@ -0,0 +1,22 @@ +#pragma once + +#include "common.h" + +class CTransfer : public Transfer +{ +public: + typedef void (CSkypeProto::* OnTransfer)(CTransfer::Ref transfer, int); + + typedef DRef Ref; + typedef DRefs Refs; + + CTransfer(unsigned int oid, SERootObject* p_root); + + void SetOnTransferCallback(OnTransfer callback, CSkypeProto* proto); + +private: + CSkypeProto* proto; + OnTransfer transferCallback; + + void OnChange(int prop); +}; \ No newline at end of file -- cgit v1.2.3