oscam-emu.11384.patch

Joe User, 2017-06-15 00:51

Download (325 KB)

View differences:

CMakeLists.txt (working copy)
101 101
    ${CMAKE_CURRENT_SOURCE_DIR}/csctapi
102 102
    ${CMAKE_CURRENT_SOURCE_DIR}/cscrypt
103 103
    ${CMAKE_CURRENT_SOURCE_DIR}/minilzo
104
    ${CMAKE_CURRENT_SOURCE_DIR}/ffdecsa
104 105
    ${CMAKE_CURRENT_SOURCE_DIR}/extapi/cygwin
105 106
    /usr/include/w32api
106 107
    ${OPTIONAL_INCLUDE_DIR}
......
110 111
    ${CMAKE_CURRENT_SOURCE_DIR}/csctapi
111 112
    ${CMAKE_CURRENT_SOURCE_DIR}/cscrypt
112 113
    ${CMAKE_CURRENT_SOURCE_DIR}/minilzo
114
    ${CMAKE_CURRENT_SOURCE_DIR}/ffdecsa
113 115
    ${OPTIONAL_INCLUDE_DIR}
114 116
    )
115 117
endif (OSCamOperatingSystem MATCHES "Windows/Cygwin")
......
420 422
# Manage config.h based on command line parameters
421 423
# Manipulate config file based on given parameters and read unset parameters
422 424

  
425
execute_process (COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/config.sh --enabled WITH_EMU OUTPUT_VARIABLE CONFIG_WITH_EMU OUTPUT_STRIP_TRAILING_WHITESPACE)
426
if (CONFIG_WITH_EMU MATCHES "Y" AND NOT WITH_EMU EQUAL 1)
427
	add_definitions ("-DWITH_EMU")
428
	set (WITH_EMU "1")
429
	message(STATUS "  EMU is added by config compiling with EMU")
430
endif(CONFIG_WITH_EMU MATCHES "Y" AND NOT WITH_EMU EQUAL 1)
431

  
423 432
execute_process (COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/config.sh --show-valid OUTPUT_VARIABLE config_vars_string OUTPUT_STRIP_TRAILING_WHITESPACE)
424 433
string(REGEX MATCHALL "[A-Z0-9_]+" config_vars ${config_vars_string})
425 434

  
......
449 458
add_subdirectory (csctapi)
450 459
add_subdirectory (minilzo)
451 460
add_subdirectory (cscrypt)
461
add_subdirectory (ffdecsa)
452 462

  
453 463
#----------------------- file groups ------------------------------
454 464
execute_process (COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/config.sh --enabled MODULE_CAMD33 OUTPUT_VARIABLE CAMD33 OUTPUT_STRIP_TRAILING_WHITESPACE)
......
498 508

  
499 509
set (exe_name "oscam")
500 510
add_executable (${exe_name} ${exe_srcs} ${exe_hdrs})
501
target_link_libraries (${exe_name} ${csoscam} ${csmodules} ${csreaders} csctapi cscrypt minilzo)
511
target_link_libraries (${exe_name} ${csoscam} ${csmodules} ${csreaders} csctapi cscrypt minilzo ffdecsa)
502 512
if(HAVE_LIBRT AND HAVE_LIBUSB)
503 513
   if (LIBUSBDIR)
504 514
	set (libusb_link "imp_libusb")
......
647 657
execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpmachine COMMAND tr -d '\n' OUTPUT_VARIABLE CS_TARGET)
648 658
add_definitions ("-D'CS_TARGET=\"${CS_TARGET}\"'")
649 659
#----------------------- global compile and link options ------------------------------
660
#enable sse2 on x86
661
if (CMAKE_SYSTEM_PROCESSOR MATCHES "(x86)|(X86)|(amd64)|(AMD64)")
662
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse -msse2 -msse3")
663
endif (CMAKE_SYSTEM_PROCESSOR MATCHES "(x86)|(X86)|(amd64)|(AMD64)")
664

  
650 665
# disable warning about unused but set variables in gcc 4.6+
651 666
if (CMAKE_COMPILER_IS_GNUCC)
652 667
    execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION)
......
731 746

  
732 747
#--------------------------------------------------------------------------------
733 748

  
749
if (NOT OSCamOperatingSystem MATCHES "Mac OS X")
750
if (NOT DEFINED ENV{ANDROID_NDK})
751
if (NOT DEFINED ENV{ANDROID_STANDALONE_TOOLCHAIN})
752
  if(WITH_EMU)
753
	if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/SoftCam.Key)
754
 		execute_process(COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/SoftCam.Key ${CMAKE_CURRENT_BINARY_DIR}/SoftCam.Key)
755
	else(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/SoftCam.Key)
756
		execute_process(COMMAND touch ${CMAKE_CURRENT_BINARY_DIR}/SoftCam.Key)
757
	endif(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/SoftCam.Key)
758
	execute_process(COMMAND touch ${CMAKE_CURRENT_BINARY_DIR}/utils/SoftCam.Key)
759
	set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--format=binary -Wl,SoftCam.Key -Wl,--format=default" ) 
760
 endif(WITH_EMU)
761
endif (NOT DEFINED ENV{ANDROID_STANDALONE_TOOLCHAIN})
762
endif (NOT DEFINED ENV{ANDROID_NDK})
763
endif (NOT OSCamOperatingSystem MATCHES "Mac OS X")
764

  
734 765
#----------------------- installation -----------------------------
735 766

  
736 767
file (GLOB config_files "${CMAKE_CURRENT_SOURCE_DIR}/Distribution/oscam.*")
......
819 850
 endif(STATICLIBUSB AND NOT LIBUSBDIR)
820 851
endif (HAVE_LIBUSB)
821 852

  
853
if (WITH_EMU)
854
	message(STATUS "  Compile with EMU support")
855
endif (WITH_EMU)
856

  
822 857
message (STATUS "")
Makefile (working copy)
65 65

  
66 66
LDFLAGS = -Wl,--gc-sections
67 67

  
68
TARGETHELP := $(shell $(CC) --target-help 2>&1)
69
ifneq (,$(findstring sse2,$(TARGETHELP)))
70
override CFLAGS += -fexpensive-optimizations -mmmx -msse -msse2 -msse3
71
else
72
override CFLAGS += -fexpensive-optimizations
73
endif
74

  
68 75
# The linker for powerpc have bug that prevents --gc-sections from working
69 76
# Check for the linker version and if it matches disable --gc-sections
70 77
# For more information about the bug see:
......
268 275
SRC-$(CONFIG_MODULE_CCCAM) += module-cccam.c
269 276
SRC-$(CONFIG_MODULE_CCCSHARE) += module-cccshare.c
270 277
SRC-$(CONFIG_MODULE_CONSTCW) += module-constcw.c
278
SRC-$(CONFIG_WITH_EMU) += module-emulator.c
279
SRC-$(CONFIG_WITH_EMU) += module-emulator-osemu.c
280
SRC-$(CONFIG_WITH_EMU) += module-emulator-stream.c
281
SRC-$(CONFIG_WITH_EMU) += ffdecsa/ffdecsa.c
282
UNAME := $(shell uname -s)
283
ifneq ($(UNAME),Darwin)
284
ifndef ANDROID_NDK
285
ifndef ANDROID_STANDALONE_TOOLCHAIN
286
ifeq "$(CONFIG_WITH_EMU)" "y"
287
TOUCH_SK := $(shell touch SoftCam.Key)
288
override LDFLAGS += -Wl,--format=binary -Wl,SoftCam.Key -Wl,--format=default
289
endif
290
endif
291
endif
292
endif
271 293
SRC-$(CONFIG_CS_CACHEEX) += module-csp.c
272 294
SRC-$(CONFIG_CW_CYCLE_CHECK) += module-cw-cycle-check.c
273 295
SRC-$(CONFIG_WITH_AZBOX) += module-dvbapi-azbox.c
......
365 387
# starts the compilation.
366 388
all:
367 389
	@./config.sh --use-flags "$(USE_FLAGS)" --objdir "$(OBJDIR)" --make-config.mak
368
	@-mkdir -p $(OBJDIR)/cscrypt $(OBJDIR)/csctapi $(OBJDIR)/minilzo $(OBJDIR)/webif
390
	@-mkdir -p $(OBJDIR)/cscrypt $(OBJDIR)/csctapi $(OBJDIR)/minilzo $(OBJDIR)/ffdecsa $(OBJDIR)/webif
369 391
	@-printf "\
370 392
+-------------------------------------------------------------------------------\n\
371 393
| OSCam ver: $(VER) rev: $(SVN_REV) target: $(TARGET)\n\
config.h (working copy)
1 1
#ifndef CONFIG_H_
2 2
#define CONFIG_H_
3 3

  
4
#define WITH_EMU 1
4 5
#define WEBIF 1
5 6
#define WEBIF_LIVELOG 1
6 7
#define WEBIF_JQUERY 1
config.sh (working copy)
1 1
#!/bin/sh
2 2

  
3
addons="WEBIF WEBIF_LIVELOG WEBIF_JQUERY TOUCH WITH_SSL HAVE_DVBAPI READ_SDT_CHARSETS IRDETO_GUESSING CS_ANTICASC WITH_DEBUG MODULE_MONITOR WITH_LB CS_CACHEEX CW_CYCLE_CHECK LCDSUPPORT LEDSUPPORT CLOCKFIX IPV6SUPPORT"
3
addons="WEBIF WEBIF_LIVELOG WEBIF_JQUERY TOUCH WITH_SSL HAVE_DVBAPI READ_SDT_CHARSETS IRDETO_GUESSING CS_ANTICASC WITH_DEBUG MODULE_MONITOR WITH_LB CS_CACHEEX CW_CYCLE_CHECK LCDSUPPORT LEDSUPPORT CLOCKFIX IPV6SUPPORT WITH_EMU"
4 4
protocols="MODULE_CAMD33 MODULE_CAMD35 MODULE_CAMD35_TCP MODULE_NEWCAMD MODULE_CCCAM MODULE_CCCSHARE MODULE_GBOX MODULE_RADEGAST MODULE_SCAM MODULE_SERIAL MODULE_CONSTCW MODULE_PANDORA MODULE_GHTTP"
5 5
readers="READER_NAGRA READER_IRDETO READER_CONAX READER_CRYPTOWORKS READER_SECA READER_VIACCESS READER_VIDEOGUARD READER_DRE READER_TONGFANG READER_BULCRYPT READER_GRIFFIN READER_DGCRYPT"
6 6
card_readers="CARDREADER_PHOENIX CARDREADER_INTERNAL CARDREADER_SC8IN1 CARDREADER_MP35 CARDREADER_SMARGO CARDREADER_DB2COM CARDREADER_STAPI CARDREADER_STAPI5 CARDREADER_STINGER CARDREADER_DRECAS"
......
24 24
# CONFIG_LEDSUPPORT=n
25 25
CONFIG_CLOCKFIX=y
26 26
# CONFIG_IPV6SUPPORT=n
27
CONFIG_WITH_EMU=y
27 28
# CONFIG_MODULE_CAMD33=n
28 29
CONFIG_MODULE_CAMD35=y
29 30
CONFIG_MODULE_CAMD35_TCP=y
......
289 290

  
290 291
update_deps() {
291 292
	# Calculate dependencies
292
	enabled_any $(get_opts readers) $(get_opts card_readers) && enable_opt WITH_CARDREADER >/dev/null
293
	disabled_all $(get_opts readers) $(get_opts card_readers) && disable_opt WITH_CARDREADER >/dev/null
293
	enabled_any $(get_opts readers) $(get_opts card_readers) WITH_EMU && enable_opt WITH_CARDREADER >/dev/null
294
	disabled_all $(get_opts readers) $(get_opts card_readers) WITH_EMU && disable_opt WITH_CARDREADER >/dev/null
294 295
	disabled WEBIF && disable_opt WEBIF_LIVELOG >/dev/null
295 296
	disabled WEBIF && disable_opt WEBIF_JQUERY >/dev/null
296 297
	enabled MODULE_CCCSHARE && enable_opt MODULE_CCCAM >/dev/null
297 298
	enabled_any CARDREADER_DB2COM CARDREADER_MP35 CARDREADER_SC8IN1 CARDREADER_STINGER && enable_opt CARDREADER_PHOENIX >/dev/null
299
	enabled WITH_EMU && enable_opt READER_VIACCESS >/dev/null
300
	enabled WITH_EMU && enable_opt READER_DRE >/dev/null
301
	enabled WITH_EMU && enable_opt MODULE_NEWCAMD >/dev/null
298 302
}
299 303

  
300 304
list_config() {
......
344 348
	not_have_flag USE_LIBCRYPTO && echo "CONFIG_LIB_AES=y" || echo "# CONFIG_LIB_AES=n"
345 349
	enabled MODULE_CCCAM && echo "CONFIG_LIB_RC6=y" || echo "# CONFIG_LIB_RC6=n"
346 350
	not_have_flag USE_LIBCRYPTO && enabled MODULE_CCCAM && echo "CONFIG_LIB_SHA1=y" || echo "# CONFIG_LIB_SHA1=n"
347
	enabled_any READER_DRE MODULE_SCAM READER_VIACCESS && echo "CONFIG_LIB_DES=y" || echo "# CONFIG_LIB_DES=n"
348
	enabled_any MODULE_CCCAM READER_NAGRA READER_SECA && echo "CONFIG_LIB_IDEA=y" || echo "# CONFIG_LIB_IDEA=n"
349
	not_have_flag USE_LIBCRYPTO && enabled_any READER_CONAX READER_CRYPTOWORKS READER_NAGRA && echo "CONFIG_LIB_BIGNUM=y" || echo "# CONFIG_LIB_BIGNUM=n"
351
	enabled_any READER_DRE MODULE_SCAM READER_VIACCESS WITH_EMU && echo "CONFIG_LIB_DES=y" || echo "# CONFIG_LIB_DES=n"
352
	enabled_any MODULE_CCCAM READER_NAGRA READER_SECA WITH_EMU && echo "CONFIG_LIB_IDEA=y" || echo "# CONFIG_LIB_IDEA=n"
353
	not_have_flag USE_LIBCRYPTO && enabled_any READER_CONAX READER_CRYPTOWORKS READER_NAGRA WITH_EMU && echo "CONFIG_LIB_BIGNUM=y" || echo "# CONFIG_LIB_BIGNUM=n"
350 354
}
351 355

  
352 356
make_config_c() {
......
457 461
		LEDSUPPORT			"LED support"							$(check_test "LEDSUPPORT") \
458 462
		CLOCKFIX			"Clockfix (disable on old systems!)"	$(check_test "CLOCKFIX") \
459 463
		IPV6SUPPORT			"IPv6 support (experimental)"			$(check_test "IPV6SUPPORT") \
464
		WITH_EMU			"Emulator support"						$(check_test "WITH_EMU") \
460 465
		2> ${tempfile}
461 466

  
462 467
	opt=${?}
cscrypt/md5.c (working copy)
25 25

  
26 26
#if !defined(WITH_SSL) && !defined(WITH_LIBCRYPTO)
27 27

  
28
typedef struct MD5Context
29
{
30
	uint32_t buf[4];
31
	uint32_t bits[2];
32
	uint32_t in[16];
33
} MD5_CTX;
34

  
35 28
#ifdef __i386__
36 29
#define byteReverse(a, b)
37 30
#else
......
155 148
 * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
156 149
 * initialization constants.
157 150
 */
158
static void MD5_Init(MD5_CTX *ctx)
151
void MD5_Init(MD5_CTX *ctx)
159 152
{
160 153
	ctx->buf[0] = 0x67452301;
161 154
	ctx->buf[1] = 0xefcdab89;
......
170 163
 * Update context to reflect the concatenation of another buffer full
171 164
 * of bytes.
172 165
 */
173
static void MD5_Update(MD5_CTX *ctx, const unsigned char *buf, unsigned int len)
166
void MD5_Update(MD5_CTX *ctx, const unsigned char *buf, unsigned int len)
174 167
{
175 168
	uint32_t t;
176 169

  
......
219 212
 * Final wrapup - pad to 64-byte boundary with the bit pattern
220 213
 * 1 0* (64-bit count of bits processed, MSB-first)
221 214
 */
222
static void MD5_Final(unsigned char digest[MD5_DIGEST_LENGTH], MD5_CTX *ctx)
215
void MD5_Final(unsigned char digest[MD5_DIGEST_LENGTH], MD5_CTX *ctx)
223 216
{
224 217
	unsigned count;
225 218
	unsigned char *p;
cscrypt/md5.h (working copy)
7 7
#define MD5_DIGEST_LENGTH 16
8 8

  
9 9
unsigned char *MD5(const unsigned char *input, unsigned long len, unsigned char *output_hash);
10
#endif
11 10

  
12
char *__md5_crypt(const char *text_pass, const char *salt, char *crypted_passwd);
11
typedef struct MD5Context {
12
	uint32_t buf[4];
13
	uint32_t bits[2];
14
	uint32_t in[16];
15
} MD5_CTX;
13 16

  
17
void MD5_Init(MD5_CTX *ctx);
18
void MD5_Update(MD5_CTX *ctx, const unsigned char *buf, unsigned int len);
19
void MD5_Final(unsigned char digest[MD5_DIGEST_LENGTH], MD5_CTX *ctx);
14 20
#endif
21
char *__md5_crypt(const char *text_pass, const char *salt, char *crypted_passwd);
22
#endif
csctapi/cardreaders.h (working copy)
14 14
extern const struct s_cardreader cardreader_stapi;
15 15
extern const struct s_cardreader cardreader_stinger;
16 16
extern const struct s_cardreader cardreader_drecas;
17
extern const struct s_cardreader cardreader_emu;
17 18

  
18 19
#endif
ffdecsa/CMakeLists.txt (working copy)
1
project (ffdecsa)
2

  
3
file (GLOB ffdecsa_srcs "ffdecsa.c")
4
file (GLOB ffdecsa_hdrs "*.h")
5

  
6
set (lib_name "ffdecsa")
7

  
8
add_library (${lib_name} STATIC ${ffdecsa_srcs} ${ffdecsa_hdrs})
ffdecsa/COPYING (working copy)
1
		    GNU GENERAL PUBLIC LICENSE
2
		       Version 2, June 1991
3

  
4
 Copyright (C) 1989, 1991 Free Software Foundation, Inc.
5
                          675 Mass Ave, Cambridge, MA 02139, USA
6
 Everyone is permitted to copy and distribute verbatim copies
7
 of this license document, but changing it is not allowed.
8

  
9
			    Preamble
10

  
11
  The licenses for most software are designed to take away your
12
freedom to share and change it.  By contrast, the GNU General Public
13
License is intended to guarantee your freedom to share and change free
14
software--to make sure the software is free for all its users.  This
15
General Public License applies to most of the Free Software
16
Foundation's software and to any other program whose authors commit to
17
using it.  (Some other Free Software Foundation software is covered by
18
the GNU Library General Public License instead.)  You can apply it to
19
your programs, too.
20

  
21
  When we speak of free software, we are referring to freedom, not
22
price.  Our General Public Licenses are designed to make sure that you
23
have the freedom to distribute copies of free software (and charge for
24
this service if you wish), that you receive source code or can get it
25
if you want it, that you can change the software or use pieces of it
26
in new free programs; and that you know you can do these things.
27

  
28
  To protect your rights, we need to make restrictions that forbid
29
anyone to deny you these rights or to ask you to surrender the rights.
30
These restrictions translate to certain responsibilities for you if you
31
distribute copies of the software, or if you modify it.
32

  
33
  For example, if you distribute copies of such a program, whether
34
gratis or for a fee, you must give the recipients all the rights that
35
you have.  You must make sure that they, too, receive or can get the
36
source code.  And you must show them these terms so they know their
37
rights.
38

  
39
  We protect your rights with two steps: (1) copyright the software, and
40
(2) offer you this license which gives you legal permission to copy,
41
distribute and/or modify the software.
42

  
43
  Also, for each author's protection and ours, we want to make certain
44
that everyone understands that there is no warranty for this free
45
software.  If the software is modified by someone else and passed on, we
46
want its recipients to know that what they have is not the original, so
47
that any problems introduced by others will not reflect on the original
48
authors' reputations.
49

  
50
  Finally, any free program is threatened constantly by software
51
patents.  We wish to avoid the danger that redistributors of a free
52
program will individually obtain patent licenses, in effect making the
53
program proprietary.  To prevent this, we have made it clear that any
54
patent must be licensed for everyone's free use or not licensed at all.
55

  
56
  The precise terms and conditions for copying, distribution and
57
modification follow.
58

59
		    GNU GENERAL PUBLIC LICENSE
60
   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61

  
62
  0. This License applies to any program or other work which contains
63
a notice placed by the copyright holder saying it may be distributed
64
under the terms of this General Public License.  The "Program", below,
65
refers to any such program or work, and a "work based on the Program"
66
means either the Program or any derivative work under copyright law:
67
that is to say, a work containing the Program or a portion of it,
68
either verbatim or with modifications and/or translated into another
69
language.  (Hereinafter, translation is included without limitation in
70
the term "modification".)  Each licensee is addressed as "you".
71

  
72
Activities other than copying, distribution and modification are not
73
covered by this License; they are outside its scope.  The act of
74
running the Program is not restricted, and the output from the Program
75
is covered only if its contents constitute a work based on the
76
Program (independent of having been made by running the Program).
77
Whether that is true depends on what the Program does.
78

  
79
  1. You may copy and distribute verbatim copies of the Program's
80
source code as you receive it, in any medium, provided that you
81
conspicuously and appropriately publish on each copy an appropriate
82
copyright notice and disclaimer of warranty; keep intact all the
83
notices that refer to this License and to the absence of any warranty;
84
and give any other recipients of the Program a copy of this License
85
along with the Program.
86

  
87
You may charge a fee for the physical act of transferring a copy, and
88
you may at your option offer warranty protection in exchange for a fee.
89

  
90
  2. You may modify your copy or copies of the Program or any portion
91
of it, thus forming a work based on the Program, and copy and
92
distribute such modifications or work under the terms of Section 1
93
above, provided that you also meet all of these conditions:
94

  
95
    a) You must cause the modified files to carry prominent notices
96
    stating that you changed the files and the date of any change.
97

  
98
    b) You must cause any work that you distribute or publish, that in
99
    whole or in part contains or is derived from the Program or any
100
    part thereof, to be licensed as a whole at no charge to all third
101
    parties under the terms of this License.
102

  
103
    c) If the modified program normally reads commands interactively
104
    when run, you must cause it, when started running for such
105
    interactive use in the most ordinary way, to print or display an
106
    announcement including an appropriate copyright notice and a
107
    notice that there is no warranty (or else, saying that you provide
108
    a warranty) and that users may redistribute the program under
109
    these conditions, and telling the user how to view a copy of this
110
    License.  (Exception: if the Program itself is interactive but
111
    does not normally print such an announcement, your work based on
112
    the Program is not required to print an announcement.)
113

114
These requirements apply to the modified work as a whole.  If
115
identifiable sections of that work are not derived from the Program,
116
and can be reasonably considered independent and separate works in
117
themselves, then this License, and its terms, do not apply to those
118
sections when you distribute them as separate works.  But when you
119
distribute the same sections as part of a whole which is a work based
120
on the Program, the distribution of the whole must be on the terms of
121
this License, whose permissions for other licensees extend to the
122
entire whole, and thus to each and every part regardless of who wrote it.
123

  
124
Thus, it is not the intent of this section to claim rights or contest
125
your rights to work written entirely by you; rather, the intent is to
126
exercise the right to control the distribution of derivative or
127
collective works based on the Program.
128

  
129
In addition, mere aggregation of another work not based on the Program
130
with the Program (or with a work based on the Program) on a volume of
131
a storage or distribution medium does not bring the other work under
132
the scope of this License.
133

  
134
  3. You may copy and distribute the Program (or a work based on it,
135
under Section 2) in object code or executable form under the terms of
136
Sections 1 and 2 above provided that you also do one of the following:
137

  
138
    a) Accompany it with the complete corresponding machine-readable
139
    source code, which must be distributed under the terms of Sections
140
    1 and 2 above on a medium customarily used for software interchange; or,
141

  
142
    b) Accompany it with a written offer, valid for at least three
143
    years, to give any third party, for a charge no more than your
144
    cost of physically performing source distribution, a complete
145
    machine-readable copy of the corresponding source code, to be
146
    distributed under the terms of Sections 1 and 2 above on a medium
147
    customarily used for software interchange; or,
148

  
149
    c) Accompany it with the information you received as to the offer
150
    to distribute corresponding source code.  (This alternative is
151
    allowed only for noncommercial distribution and only if you
152
    received the program in object code or executable form with such
153
    an offer, in accord with Subsection b above.)
154

  
155
The source code for a work means the preferred form of the work for
156
making modifications to it.  For an executable work, complete source
157
code means all the source code for all modules it contains, plus any
158
associated interface definition files, plus the scripts used to
159
control compilation and installation of the executable.  However, as a
160
special exception, the source code distributed need not include
161
anything that is normally distributed (in either source or binary
162
form) with the major components (compiler, kernel, and so on) of the
163
operating system on which the executable runs, unless that component
164
itself accompanies the executable.
165

  
166
If distribution of executable or object code is made by offering
167
access to copy from a designated place, then offering equivalent
168
access to copy the source code from the same place counts as
169
distribution of the source code, even though third parties are not
170
compelled to copy the source along with the object code.
171

172
  4. You may not copy, modify, sublicense, or distribute the Program
173
except as expressly provided under this License.  Any attempt
174
otherwise to copy, modify, sublicense or distribute the Program is
175
void, and will automatically terminate your rights under this License.
176
However, parties who have received copies, or rights, from you under
177
this License will not have their licenses terminated so long as such
178
parties remain in full compliance.
179

  
180
  5. You are not required to accept this License, since you have not
181
signed it.  However, nothing else grants you permission to modify or
182
distribute the Program or its derivative works.  These actions are
183
prohibited by law if you do not accept this License.  Therefore, by
184
modifying or distributing the Program (or any work based on the
185
Program), you indicate your acceptance of this License to do so, and
186
all its terms and conditions for copying, distributing or modifying
187
the Program or works based on it.
188

  
189
  6. Each time you redistribute the Program (or any work based on the
190
Program), the recipient automatically receives a license from the
191
original licensor to copy, distribute or modify the Program subject to
192
these terms and conditions.  You may not impose any further
193
restrictions on the recipients' exercise of the rights granted herein.
194
You are not responsible for enforcing compliance by third parties to
195
this License.
196

  
197
  7. If, as a consequence of a court judgment or allegation of patent
198
infringement or for any other reason (not limited to patent issues),
199
conditions are imposed on you (whether by court order, agreement or
200
otherwise) that contradict the conditions of this License, they do not
201
excuse you from the conditions of this License.  If you cannot
202
distribute so as to satisfy simultaneously your obligations under this
203
License and any other pertinent obligations, then as a consequence you
204
may not distribute the Program at all.  For example, if a patent
205
license would not permit royalty-free redistribution of the Program by
206
all those who receive copies directly or indirectly through you, then
207
the only way you could satisfy both it and this License would be to
208
refrain entirely from distribution of the Program.
209

  
210
If any portion of this section is held invalid or unenforceable under
211
any particular circumstance, the balance of the section is intended to
212
apply and the section as a whole is intended to apply in other
213
circumstances.
214

  
215
It is not the purpose of this section to induce you to infringe any
216
patents or other property right claims or to contest validity of any
217
such claims; this section has the sole purpose of protecting the
218
integrity of the free software distribution system, which is
219
implemented by public license practices.  Many people have made
220
generous contributions to the wide range of software distributed
221
through that system in reliance on consistent application of that
222
system; it is up to the author/donor to decide if he or she is willing
223
to distribute software through any other system and a licensee cannot
224
impose that choice.
225

  
226
This section is intended to make thoroughly clear what is believed to
227
be a consequence of the rest of this License.
228

229
  8. If the distribution and/or use of the Program is restricted in
230
certain countries either by patents or by copyrighted interfaces, the
231
original copyright holder who places the Program under this License
232
may add an explicit geographical distribution limitation excluding
233
those countries, so that distribution is permitted only in or among
234
countries not thus excluded.  In such case, this License incorporates
235
the limitation as if written in the body of this License.
236

  
237
  9. The Free Software Foundation may publish revised and/or new versions
238
of the General Public License from time to time.  Such new versions will
239
be similar in spirit to the present version, but may differ in detail to
240
address new problems or concerns.
241

  
242
Each version is given a distinguishing version number.  If the Program
243
specifies a version number of this License which applies to it and "any
244
later version", you have the option of following the terms and conditions
245
either of that version or of any later version published by the Free
246
Software Foundation.  If the Program does not specify a version number of
247
this License, you may choose any version ever published by the Free Software
248
Foundation.
249

  
250
  10. If you wish to incorporate parts of the Program into other free
251
programs whose distribution conditions are different, write to the author
252
to ask for permission.  For software which is copyrighted by the Free
253
Software Foundation, write to the Free Software Foundation; we sometimes
254
make exceptions for this.  Our decision will be guided by the two goals
255
of preserving the free status of all derivatives of our free software and
256
of promoting the sharing and reuse of software generally.
257

  
258
			    NO WARRANTY
259

  
260
  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
262
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
266
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
267
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268
REPAIR OR CORRECTION.
269

  
270
  12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278
POSSIBILITY OF SUCH DAMAGES.
279

  
280
		     END OF TERMS AND CONDITIONS
281

282
	Appendix: How to Apply These Terms to Your New Programs
283

  
284
  If you develop a new program, and you want it to be of the greatest
285
possible use to the public, the best way to achieve this is to make it
286
free software which everyone can redistribute and change under these terms.
287

  
288
  To do so, attach the following notices to the program.  It is safest
289
to attach them to the start of each source file to most effectively
290
convey the exclusion of warranty; and each file should have at least
291
the "copyright" line and a pointer to where the full notice is found.
292

  
293
    <one line to give the program's name and a brief idea of what it does.>
294
    Copyright (C) 19yy  <name of author>
295

  
296
    This program is free software; you can redistribute it and/or modify
297
    it under the terms of the GNU General Public License as published by
298
    the Free Software Foundation; either version 2 of the License, or
299
    (at your option) any later version.
300

  
301
    This program is distributed in the hope that it will be useful,
302
    but WITHOUT ANY WARRANTY; without even the implied warranty of
303
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
304
    GNU General Public License for more details.
305

  
306
    You should have received a copy of the GNU General Public License
307
    along with this program; if not, write to the Free Software
308
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
309

  
310
Also add information on how to contact you by electronic and paper mail.
311

  
312
If the program is interactive, make it output a short notice like this
313
when it starts in an interactive mode:
314

  
315
    Gnomovision version 69, Copyright (C) 19yy name of author
316
    Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
317
    This is free software, and you are welcome to redistribute it
318
    under certain conditions; type `show c' for details.
319

  
320
The hypothetical commands `show w' and `show c' should show the appropriate
321
parts of the General Public License.  Of course, the commands you use may
322
be called something other than `show w' and `show c'; they could even be
323
mouse-clicks or menu items--whatever suits your program.
324

  
325
You should also get your employer (if you work as a programmer) or your
326
school, if any, to sign a "copyright disclaimer" for the program, if
327
necessary.  Here is a sample; alter the names:
328

  
329
  Yoyodyne, Inc., hereby disclaims all copyright interest in the program
330
  `Gnomovision' (which makes passes at compilers) written by James Hacker.
331

  
332
  <signature of Ty Coon>, 1 April 1989
333
  Ty Coon, President of Vice
334

  
335
This General Public License does not permit incorporating your program into
336
proprietary programs.  If your program is a subroutine library, you may
337
consider it more useful to permit linking proprietary applications with the
338
library.  If this is what you want to do, use the GNU Library General
339
Public License instead of this License.
ffdecsa/Makefile (working copy)
1
parent:
2
	@$(MAKE) --no-print-directory -C ..
ffdecsa/ffdecsa.c (working copy)
1
/* FFdecsa -- fast decsa algorithm
2
 *
3
 * Copyright (C) 2003-2004  fatih89r
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
 */
19

  
20

  
21
#include <sys/types.h>
22
#include <string.h>
23
#include <stdio.h>
24
#include <stdlib.h>
25

  
26
#include "ffdecsa.h"
27

  
28
#ifndef NULL
29
#define NULL 0
30
#endif
31

  
32
//#define DEBUG
33
#ifdef DEBUG
34
#define DBG(a) a
35
#else
36
#define DBG(a)
37
#endif
38

  
39
//// parallelization stuff, large speed differences are possible
40
// possible choices
41
#define PARALLEL_32_4CHAR     320
42
#define PARALLEL_32_4CHARA    321
43
#define PARALLEL_32_INT       322
44
#define PARALLEL_64_8CHAR     640
45
#define PARALLEL_64_8CHARA    641
46
#define PARALLEL_64_2INT      642
47
#define PARALLEL_64_LONG      643
48
#define PARALLEL_64_MMX       644
49
#define PARALLEL_128_16CHAR  1280
50
#define PARALLEL_128_16CHARA 1281
51
#define PARALLEL_128_4INT    1282
52
#define PARALLEL_128_2LONG   1283
53
#define PARALLEL_128_2MMX    1284
54
#define PARALLEL_128_SSE     1285
55
#define PARALLEL_128_SSE2    1286
56

  
57
//////// our choice //////////////// our choice //////////////// our choice //////////////// our choice ////////
58
#ifndef PARALLEL_MODE
59

  
60
#if defined(__x86_64__) || defined(_M_X64)
61
#define PARALLEL_MODE PARALLEL_128_SSE2
62

  
63
#elif defined(__mips__) || defined(__mips) || defined(__MIPS__)
64
#define PARALLEL_MODE PARALLEL_64_LONG
65

  
66
#elif defined(__sh__) || defined(__SH4__)
67
#define PARALLEL_MODE PARALLEL_32_INT
68
#define COPY_UNALIGNED_PKT
69
#define MEMALIGN_VAL 4
70

  
71
#else
72
#define PARALLEL_MODE PARALLEL_32_INT
73
#endif
74

  
75
#endif
76
//////// our choice //////////////// our choice //////////////// our choice //////////////// our choice ////////
77

  
78
#include "parallel_generic.h"
79
//// conditionals
80
#if PARALLEL_MODE==PARALLEL_32_4CHAR
81
#include "parallel_032_4char.h"
82
#elif PARALLEL_MODE==PARALLEL_32_4CHARA
83
#include "parallel_032_4charA.h"
84
#elif PARALLEL_MODE==PARALLEL_32_INT
85
#include "parallel_032_int.h"
86
#elif PARALLEL_MODE==PARALLEL_64_8CHAR
87
#include "parallel_064_8char.h"
88
#elif PARALLEL_MODE==PARALLEL_64_8CHARA
89
#include "parallel_064_8charA.h"
90
#elif PARALLEL_MODE==PARALLEL_64_2INT
91
#include "parallel_064_2int.h"
92
#elif PARALLEL_MODE==PARALLEL_64_LONG
93
#include "parallel_064_long.h"
94
#elif PARALLEL_MODE==PARALLEL_64_MMX
95
#include "parallel_064_mmx.h"
96
#elif PARALLEL_MODE==PARALLEL_128_16CHAR
97
#include "parallel_128_16char.h"
98
#elif PARALLEL_MODE==PARALLEL_128_16CHARA
99
#include "parallel_128_16charA.h"
100
#elif PARALLEL_MODE==PARALLEL_128_4INT
101
#include "parallel_128_4int.h"
102
#elif PARALLEL_MODE==PARALLEL_128_2LONG
103
#include "parallel_128_2long.h"
104
#elif PARALLEL_MODE==PARALLEL_128_2MMX
105
#include "parallel_128_2mmx.h"
106
#elif PARALLEL_MODE==PARALLEL_128_SSE
107
#include "parallel_128_sse.h"
108
#elif PARALLEL_MODE==PARALLEL_128_SSE2
109
#include "parallel_128_sse2.h"
110
#else
111
#error "unknown/undefined parallel mode"
112
#endif
113

  
114
// stuff depending on conditionals
115

  
116
#define BYTES_PER_GROUP (GROUP_PARALLELISM/8)
117
#define BYPG BYTES_PER_GROUP
118
#define BITS_PER_GROUP GROUP_PARALLELISM
119
#define BIPG BITS_PER_GROUP
120

  
121
// platform specific
122

  
123
#ifdef __arm__
124
#if !defined(MEMALIGN_VAL) || MEMALIGN_VAL<4
125
#undef MEMALIGN_VAL
126
#define MEMALIGN_VAL 4
127
#endif
128
#define COPY_UNALIGNED_PKT
129
#endif
130

  
131
//
132

  
133
#ifndef MALLOC
134
#define MALLOC(X) malloc(X)
135
#endif
136
#ifndef FREE
137
#define FREE(X) free(X)
138
#endif
139
#ifdef MEMALIGN_VAL
140
#define MEMALIGN __attribute__((aligned(MEMALIGN_VAL)))
141
#else
142
#define MEMALIGN
143
#endif
144

  
145
//// debug tool
146

  
147
#ifdef DEBUG
148
static void dump_mem(const char *string, const unsigned char *p, int len, int linelen){
149
  int i;
150
  for(i=0;i<len;i++){
151
    if(i%linelen==0&&i) fprintf(stderr,"\n");
152
    if(i%linelen==0) fprintf(stderr,"%s %08x:",string,i);
153
    else{
154
      if(i%8==0) fprintf(stderr," ");
155
      if(i%4==0) fprintf(stderr," ");
156
    }
157
    fprintf(stderr," %02x",p[i]);
158
  }
159
  if(i%linelen==0) fprintf(stderr,"\n");
160
}
161
#endif
162

  
163
//////////////////////////////////////////////////////////////////////////////////
164

  
165
struct csa_key_t{
166
	unsigned char ck[8];
167
// used by stream
168
        int iA[8];  // iA[0] is for A1, iA[7] is for A8
169
        int iB[8];  // iB[0] is for B1, iB[7] is for B8
170
// used by stream (group)
171
        MEMALIGN group ck_g[8][8]; // [byte][bit:0=LSB,7=MSB]
172
        MEMALIGN group iA_g[8][4]; // [0 for A1][0 for LSB]
173
        MEMALIGN group iB_g[8][4]; // [0 for B1][0 for LSB]
174
// used by block
175
	unsigned char kk[56];
176
// used by block (group)
177
	MEMALIGN batch kkmulti[56]; // many times the same byte in every batch
178
};
179

  
180
struct csa_keys_t{
181
  struct csa_key_t even;
182
  struct csa_key_t odd;
183
};
184

  
185
//-----stream cypher
186

  
187
//-----key schedule for stream decypher
188
static void key_schedule_stream(
189
  unsigned char *ck,    // [In]  ck[0]-ck[7]   8 bytes   | Key.
190
  int *iA,              // [Out] iA[0]-iA[7]   8 nibbles | Key schedule.
191
  int *iB)              // [Out] iB[0]-iB[7]   8 nibbles | Key schedule.
192
{
193
    iA[0]=(ck[0]>>4)&0xf;
194
    iA[1]=(ck[0]   )&0xf;
195
    iA[2]=(ck[1]>>4)&0xf;
196
    iA[3]=(ck[1]   )&0xf;
197
    iA[4]=(ck[2]>>4)&0xf;
198
    iA[5]=(ck[2]   )&0xf;
199
    iA[6]=(ck[3]>>4)&0xf;
200
    iA[7]=(ck[3]   )&0xf;
201
    iB[0]=(ck[4]>>4)&0xf;
202
    iB[1]=(ck[4]   )&0xf;
203
    iB[2]=(ck[5]>>4)&0xf;
204
    iB[3]=(ck[5]   )&0xf;
205
    iB[4]=(ck[6]>>4)&0xf;
206
    iB[5]=(ck[6]   )&0xf;
207
    iB[6]=(ck[7]>>4)&0xf;
208
    iB[7]=(ck[7]   )&0xf;
209
}
210

  
211
//----- stream main function
212

  
213
#define STREAM_INIT
214
#include "stream.c"
215
#undef STREAM_INIT
216

  
217
#define STREAM_NORMAL
218
#include "stream.c"
219
#undef STREAM_NORMAL
220

  
221

  
222
//-----block decypher
223

  
224
//-----key schedule for block decypher
225

  
226
static void key_schedule_block(
227
  unsigned char *ck,    // [In]  ck[0]-ck[7]   8 bytes | Key.
228
  unsigned char *kk)    // [Out] kk[0]-kk[55] 56 bytes | Key schedule.
229
{
230
  static const unsigned char key_perm[0x40] = {
231
    0x12,0x24,0x09,0x07,0x2A,0x31,0x1D,0x15, 0x1C,0x36,0x3E,0x32,0x13,0x21,0x3B,0x40,
232
    0x18,0x14,0x25,0x27,0x02,0x35,0x1B,0x01, 0x22,0x04,0x0D,0x0E,0x39,0x28,0x1A,0x29,
233
    0x33,0x23,0x34,0x0C,0x16,0x30,0x1E,0x3A, 0x2D,0x1F,0x08,0x19,0x17,0x2F,0x3D,0x11,
234
    0x3C,0x05,0x38,0x2B,0x0B,0x06,0x0A,0x2C, 0x20,0x3F,0x2E,0x0F,0x03,0x26,0x10,0x37,
235
  };
236

  
237
  int i,j,k;
238
  int bit[64];
239
  int newbit[64];
240
  int kb[7][8];
241

  
242
  // 56 steps
243
  // 56 key bytes kk(55)..kk(0) by key schedule from ck
244

  
245
  // kb(6,0) .. kb(6,7) = ck(0) .. ck(7)
246
  kb[6][0] = ck[0];
247
  kb[6][1] = ck[1];
248
  kb[6][2] = ck[2];
249
  kb[6][3] = ck[3];
250
  kb[6][4] = ck[4];
251
  kb[6][5] = ck[5];
252
  kb[6][6] = ck[6];
253
  kb[6][7] = ck[7];
254

  
255
  // calculate kb[5] .. kb[0]
256
  for(i=5; i>=0; i--){
257
    // 64 bit perm on kb
258
    for(j=0; j<8; j++){
259
      for(k=0; k<8; k++){
260
        bit[j*8+k] = (kb[i+1][j] >> (7-k)) & 1;
261
        newbit[key_perm[j*8+k]-1] = bit[j*8+k];
262
      }
263
    }
264
    for(j=0; j<8; j++){
265
      kb[i][j] = 0;
266
      for(k=0; k<8; k++){
267
        kb[i][j] |= newbit[j*8+k] << (7-k);
268
      }
269
    }
270
  }
271

  
272
  // xor to give kk
273
  for(i=0; i<7; i++){
274
    for(j=0; j<8; j++){
275
      kk[i*8+j] = kb[i][j] ^ i;
276
    }
277
  }
278

  
279
}
280

  
281
//-----block utils
282

  
283
static inline __attribute__((always_inline)) void trasp_N_8 (unsigned char *in,unsigned char* out,int count){
284
  int *ri=(int *)in;
285
  int *ibi=(int *)out;
286
  int j,i,k,g;
287
  // copy and first step
288
  for(g=0;g<count;g++){
289
    ri[g]=ibi[2*g];
290
    ri[GROUP_PARALLELISM+g]=ibi[2*g+1];
291
  }
292
//dump_mem("NE1 r[roff]",&r[roff],GROUP_PARALLELISM*8,GROUP_PARALLELISM);
293
// now 01230123
294
#define INTS_PER_ROW (GROUP_PARALLELISM/8*2)
295
  for(j=0;j<8;j+=4){
296
    for(i=0;i<2;i++){
297
      for(k=0;k<INTS_PER_ROW;k++){
298
        unsigned int t,b;
299
        t=ri[INTS_PER_ROW*(j+i)+k];
300
        b=ri[INTS_PER_ROW*(j+i+2)+k];
301
        ri[INTS_PER_ROW*(j+i)+k]=     (t&0x0000ffff)      | ((b           )<<16);
302
        ri[INTS_PER_ROW*(j+i+2)+k]=  ((t           )>>16) |  (b&0xffff0000) ;
303
      }
304
    }
305
  }
306
//dump_mem("NE2 r[roff]",&r[roff],GROUP_PARALLELISM*8,GROUP_PARALLELISM);
307
// now 01010101
308
  for(j=0;j<8;j+=2){
309
    for(i=0;i<1;i++){
310
      for(k=0;k<INTS_PER_ROW;k++){
311
        unsigned int t,b;
312
        t=ri[INTS_PER_ROW*(j+i)+k];
313
        b=ri[INTS_PER_ROW*(j+i+1)+k];
314
        ri[INTS_PER_ROW*(j+i)+k]=     (t&0x00ff00ff)     | ((b&0x00ff00ff)<<8);
315
        ri[INTS_PER_ROW*(j+i+1)+k]=  ((t&0xff00ff00)>>8) |  (b&0xff00ff00);
316
      }
317
    }
318
  }
319
//dump_mem("NE3 r[roff]",&r[roff],GROUP_PARALLELISM*8,GROUP_PARALLELISM);
320
// now 00000000
321
}
322

  
323
static inline __attribute__((always_inline)) void trasp_8_N (unsigned char *in,unsigned char* out,int count){
324
  int *ri=(int *)in;
325
  int *bdi=(int *)out;
326
  int j,i,k,g;
327
#define INTS_PER_ROW (GROUP_PARALLELISM/8*2)
328
//dump_mem("NE1 r[roff]",&r[roff],GROUP_PARALLELISM*8,GROUP_PARALLELISM);
329
// now 00000000
330
  for(j=0;j<8;j+=2){
331
    for(i=0;i<1;i++){
332
      for(k=0;k<INTS_PER_ROW;k++){
333
        unsigned int t,b;
334
        t=ri[INTS_PER_ROW*(j+i)+k];
335
        b=ri[INTS_PER_ROW*(j+i+1)+k];
336
        ri[INTS_PER_ROW*(j+i)+k]=     (t&0x00ff00ff)     | ((b&0x00ff00ff)<<8);
337
        ri[INTS_PER_ROW*(j+i+1)+k]=  ((t&0xff00ff00)>>8) |  (b&0xff00ff00);
338
      }
339
    }
340
  }
341
//dump_mem("NE2 r[roff]",&r[roff],GROUP_PARALLELISM*8,GROUP_PARALLELISM);
342
// now 01010101
343
  for(j=0;j<8;j+=4){
344
    for(i=0;i<2;i++){
345
      for(k=0;k<INTS_PER_ROW;k++){
346
        unsigned int t,b;
347
        t=ri[INTS_PER_ROW*(j+i)+k];
348
        b=ri[INTS_PER_ROW*(j+i+2)+k];
349
        ri[INTS_PER_ROW*(j+i)+k]=     (t&0x0000ffff)      | ((b           )<<16);
350
        ri[INTS_PER_ROW*(j+i+2)+k]=  ((t           )>>16) |  (b&0xffff0000) ;
351
      }
352
    }
353
  }
354
//dump_mem("NE3 r[roff]",&r[roff],GROUP_PARALLELISM*8,GROUP_PARALLELISM);
355
// now 01230123
356
  for(g=0;g<count;g++){
357
    bdi[2*g]=ri[g];
358
    bdi[2*g+1]=ri[GROUP_PARALLELISM+g];
359
  }
360
}
361

  
362
//-----block main function
363

  
364
// block group
365
static void block_decypher_group(
366
  batch *kkmulti,       // [In]  kkmulti[0]-kkmulti[55] 56 batches | Key schedule (each batch has repeated equal bytes).
367
  unsigned char *ib,    // [In]  (ib0,ib1,...ib7)...x32 32*8 bytes | Initialization vector.
368
  unsigned char *bd,    // [Out] (bd0,bd1,...bd7)...x32 32*8 bytes | Block decipher.
369
  int count)
370
{
371
  // int is faster than unsigned char. apparently not
372
  static const unsigned char block_sbox[0x100] = {
373
    0x3A,0xEA,0x68,0xFE,0x33,0xE9,0x88,0x1A, 0x83,0xCF,0xE1,0x7F,0xBA,0xE2,0x38,0x12,
374
    0xE8,0x27,0x61,0x95,0x0C,0x36,0xE5,0x70, 0xA2,0x06,0x82,0x7C,0x17,0xA3,0x26,0x49,
375
    0xBE,0x7A,0x6D,0x47,0xC1,0x51,0x8F,0xF3, 0xCC,0x5B,0x67,0xBD,0xCD,0x18,0x08,0xC9,
376
    0xFF,0x69,0xEF,0x03,0x4E,0x48,0x4A,0x84, 0x3F,0xB4,0x10,0x04,0xDC,0xF5,0x5C,0xC6,
377
    0x16,0xAB,0xAC,0x4C,0xF1,0x6A,0x2F,0x3C, 0x3B,0xD4,0xD5,0x94,0xD0,0xC4,0x63,0x62,
378
    0x71,0xA1,0xF9,0x4F,0x2E,0xAA,0xC5,0x56, 0xE3,0x39,0x93,0xCE,0x65,0x64,0xE4,0x58,
379
    0x6C,0x19,0x42,0x79,0xDD,0xEE,0x96,0xF6, 0x8A,0xEC,0x1E,0x85,0x53,0x45,0xDE,0xBB,
380
    0x7E,0x0A,0x9A,0x13,0x2A,0x9D,0xC2,0x5E, 0x5A,0x1F,0x32,0x35,0x9C,0xA8,0x73,0x30,
381

  
382
    0x29,0x3D,0xE7,0x92,0x87,0x1B,0x2B,0x4B, 0xA5,0x57,0x97,0x40,0x15,0xE6,0xBC,0x0E,
383
    0xEB,0xC3,0x34,0x2D,0xB8,0x44,0x25,0xA4, 0x1C,0xC7,0x23,0xED,0x90,0x6E,0x50,0x00,
384
    0x99,0x9E,0x4D,0xD9,0xDA,0x8D,0x6F,0x5F, 0x3E,0xD7,0x21,0x74,0x86,0xDF,0x6B,0x05,
385
    0x8E,0x5D,0x37,0x11,0xD2,0x28,0x75,0xD6, 0xA7,0x77,0x24,0xBF,0xF0,0xB0,0x02,0xB7,
386
    0xF8,0xFC,0x81,0x09,0xB1,0x01,0x76,0x91, 0x7D,0x0F,0xC8,0xA0,0xF2,0xCB,0x78,0x60,
387
    0xD1,0xF7,0xE0,0xB5,0x98,0x22,0xB3,0x20, 0x1D,0xA6,0xDB,0x7B,0x59,0x9F,0xAE,0x31,
388
    0xFB,0xD3,0xB6,0xCA,0x43,0x72,0x07,0xF4, 0xD8,0x41,0x14,0x55,0x0D,0x54,0x8B,0xB9,
389
    0xAD,0x46,0x0B,0xAF,0x80,0x52,0x2C,0xFA, 0x8C,0x89,0x66,0xFD,0xB2,0xA9,0x9B,0xC0,
390
  };
391
  MEMALIGN unsigned char r[GROUP_PARALLELISM*(8+56)];  /* 56 because we will move back in memory while looping */
392
  MEMALIGN unsigned char sbox_in[GROUP_PARALLELISM],sbox_out[GROUP_PARALLELISM],perm_out[GROUP_PARALLELISM];
393
  int roff;
394
  int i,g,count_all=GROUP_PARALLELISM;
395

  
396
  roff=GROUP_PARALLELISM*56;
397

  
398
#define FASTTRASP1
399
#ifndef FASTTRASP1
400
  for(g=0;g<count;g++){
401
    // Init registers 
402
    int j;
403
    for(j=0;j<8;j++){
404
      r[roff+GROUP_PARALLELISM*j+g]=ib[8*g+j];
405
    }
406
  }
407
#else
408
  trasp_N_8((unsigned char *)&r[roff],(unsigned char *)ib,count);
409
#endif
410
//dump_mem("OLD r[roff]",&r[roff],GROUP_PARALLELISM*8,GROUP_PARALLELISM);
411

  
412
  // loop over kk[55]..kk[0]
413
  for(i=55;i>=0;i--){
414
    {
415
      MEMALIGN batch tkkmulti=kkmulti[i];
416
      batch *si=(batch *)sbox_in;
417
      batch *r6_N=(batch *)(r+roff+GROUP_PARALLELISM*6);
418
      for(g=0;g<count_all/BYTES_PER_BATCH;g++){
419
        si[g]=B_FFXOR(tkkmulti,r6_N[g]);              //FIXME: introduce FASTBATCH?
420
      }
421
    }
422

  
423
    // table lookup, this works on only one byte at a time
424
    // most difficult part of all
425
    // - can't be parallelized
426
    // - can't be synthetized through boolean terms (8 input bits are too many)
427
    for(g=0;g<count_all;g++){
428
      sbox_out[g]=block_sbox[sbox_in[g]];
429
    }
430

  
431
    // bit permutation
432
    {
433
      unsigned char *po=(unsigned char *)perm_out;
434
      unsigned char *so=(unsigned char *)sbox_out;
435
//dump_mem("pre perm ",(unsigned char *)so,GROUP_PARALLELISM,GROUP_PARALLELISM);
436
      for(g=0;g<count_all;g+=BYTES_PER_BATCH){
437
        MEMALIGN batch in,out;
438
        in=*(batch *)&so[g];
439

  
440
        out=B_FFOR(
441
	    B_FFOR(
442
	    B_FFOR(
443
	    B_FFOR(
444
	    B_FFOR(
445
	           B_FFSH8L(B_FFAND(in,B_FFN_ALL_29()),1),
446
	           B_FFSH8L(B_FFAND(in,B_FFN_ALL_02()),6)),
447
	           B_FFSH8L(B_FFAND(in,B_FFN_ALL_04()),3)),
448
	           B_FFSH8R(B_FFAND(in,B_FFN_ALL_10()),2)),
449
	           B_FFSH8R(B_FFAND(in,B_FFN_ALL_40()),6)),
450
	           B_FFSH8R(B_FFAND(in,B_FFN_ALL_80()),4));
451

  
452
        *(batch *)&po[g]=out;
453
      }
454
//dump_mem("post perm",(unsigned char *)po,GROUP_PARALLELISM,GROUP_PARALLELISM);
455
    }
456

  
457
    roff-=GROUP_PARALLELISM; /* virtual shift of registers */
458

  
459
#if 0
460
/* one by one */
461
    for(g=0;g<count_all;g++){
462
      r[roff+GROUP_PARALLELISM*0+g]=r[roff+GROUP_PARALLELISM*8+g]^sbox_out[g];
463
      r[roff+GROUP_PARALLELISM*6+g]^=perm_out[g];
464
      r[roff+GROUP_PARALLELISM*4+g]^=r[roff+GROUP_PARALLELISM*0+g];
465
      r[roff+GROUP_PARALLELISM*3+g]^=r[roff+GROUP_PARALLELISM*0+g];
466
      r[roff+GROUP_PARALLELISM*2+g]^=r[roff+GROUP_PARALLELISM*0+g];
467
    }
468
#else
469
    for(g=0;g<count_all;g+=BEST_SPAN){
470
      XOR_BEST_BY(&r[roff+GROUP_PARALLELISM*0+g],&r[roff+GROUP_PARALLELISM*8+g],&sbox_out[g]);
471
      XOREQ_BEST_BY(&r[roff+GROUP_PARALLELISM*6+g],&perm_out[g]);
472
      XOREQ_BEST_BY(&r[roff+GROUP_PARALLELISM*4+g],&r[roff+GROUP_PARALLELISM*0+g]);
473
      XOREQ_BEST_BY(&r[roff+GROUP_PARALLELISM*3+g],&r[roff+GROUP_PARALLELISM*0+g]);
474
      XOREQ_BEST_BY(&r[roff+GROUP_PARALLELISM*2+g],&r[roff+GROUP_PARALLELISM*0+g]);
475
    }
476
#endif
477
  }
478

  
479
#define FASTTRASP2
480
#ifndef FASTTRASP2
481
  for(g=0;g<count;g++){
482
    // Copy results
483
    int j;
484
    for(j=0;j<8;j++){
485
      bd[8*g+j]=r[roff+GROUP_PARALLELISM*j+g];
486
    }
487
  }
488
#else
489
  trasp_8_N((unsigned char *)&r[roff],(unsigned char *)bd,count);
490
#endif
491
}
492

  
493
//-----------------------------------EXTERNAL INTERFACE
494

  
495
//-----get internal parallelism
496

  
497
int get_internal_parallelism(void){
498
  return GROUP_PARALLELISM;
499
}
500

  
501
//-----get suggested cluster size
502

  
503
int get_suggested_cluster_size(void){
504
  int r;
505
  r=GROUP_PARALLELISM+GROUP_PARALLELISM/10;
506
  if(r<GROUP_PARALLELISM+5) r=GROUP_PARALLELISM+5;
507
  return r;
508
}
509

  
510
//-----key structure
511

  
512
void *get_key_struct(void){
513
  struct csa_keys_t *keys=(struct csa_keys_t *)MALLOC(sizeof(struct csa_keys_t));
514
  if(keys) {
515
    static const unsigned char pk[8] = { 0,0,0,0,0,0,0,0 };
516
    set_control_words(keys,pk,pk);
517
    }
518
  return keys;
519
}
520

  
521
void free_key_struct(void *keys){
522
  return FREE(keys);
523
}
524

  
525
//-----set control words
526

  
527
static void schedule_key(struct csa_key_t *key, const unsigned char *pk){
528
  // could be made faster, but is not run often
529
  int bi,by;
530
  int i,j;
531
// key
532
  memcpy(key->ck,pk,8);
533
// precalculations for stream
534
  key_schedule_stream(key->ck,key->iA,key->iB);
535
  for(by=0;by<8;by++){
536
    for(bi=0;bi<8;bi++){
537
      key->ck_g[by][bi]=(key->ck[by]&(1<<bi))?FF1():FF0();
538
    }
539
  }
540
  for(by=0;by<8;by++){
541
    for(bi=0;bi<4;bi++){
542
      key->iA_g[by][bi]=(key->iA[by]&(1<<bi))?FF1():FF0();
543
      key->iB_g[by][bi]=(key->iB[by]&(1<<bi))?FF1():FF0();
544
    }
545
  }
546
// precalculations for block
547
  key_schedule_block(key->ck,key->kk);
548
  for(i=0;i<56;i++){
549
    for(j=0;j<BYTES_PER_BATCH;j++){
550
      *(((unsigned char *)&key->kkmulti[i])+j)=key->kk[i];
551
    }
552
  }
553
}
554

  
555
void set_control_words(void *keys, const unsigned char *ev, const unsigned char *od){
556
  schedule_key(&((struct csa_keys_t *)keys)->even,ev);
557
  schedule_key(&((struct csa_keys_t *)keys)->odd,od);
558
}
559

  
560
void set_even_control_word(void *keys, const unsigned char *pk){
561
  schedule_key(&((struct csa_keys_t *)keys)->even,pk);
562
}
563

  
564
void set_odd_control_word(void *keys, const unsigned char *pk){
565
  schedule_key(&((struct csa_keys_t *)keys)->odd,pk);
566
}
567

  
568
//-----get control words
569

  
570
void get_control_words(void *keys, unsigned char *even, unsigned char *odd){
571
  memcpy(even,&((struct csa_keys_t *)keys)->even.ck,8);
572
  memcpy(odd,&((struct csa_keys_t *)keys)->odd.ck,8);
573
}
574

  
575
//----- decrypt
576

  
577
int decrypt_packets(void *keys, unsigned char **cluster){
578
  // statistics, currently unused
579
  int stat_no_scramble=0;
580
  int stat_reserved=0;
581
  int stat_decrypted[2]={0,0};
582
  int stat_decrypted_mini=0;
583
  unsigned char **clst;
584
  unsigned char **clst2;
585
  int grouped;
586
  int group_ev_od;
587
  int advanced;
588
  int can_advance;
589
  unsigned char *g_pkt[GROUP_PARALLELISM];
590
  int g_len[GROUP_PARALLELISM];
591
  int g_offset[GROUP_PARALLELISM];
592
  int g_n[GROUP_PARALLELISM];
593
  int g_residue[GROUP_PARALLELISM];
594
  unsigned char *pkt;
595
  int xc0,ev_od,len,offset,n,residue;
596
  struct csa_key_t* k;
597
  int i,j,iter,g;
598
  int t23,tsmall;
599
  int alive[24];
600
//icc craziness  int pad1=0; //////////align! FIXME
601
  unsigned char *encp[GROUP_PARALLELISM];
602
  MEMALIGN unsigned char stream_in[GROUP_PARALLELISM*8];
603
  MEMALIGN unsigned char stream_out[GROUP_PARALLELISM*8];
604
  MEMALIGN unsigned char ib[GROUP_PARALLELISM*8];
605
  MEMALIGN unsigned char block_out[GROUP_PARALLELISM*8];
606
#ifdef COPY_UNALIGNED_PKT
607
  unsigned char *unaligned[GROUP_PARALLELISM];
608
  MEMALIGN unsigned char alignedBuff[GROUP_PARALLELISM][188];
609
#endif
610
  struct stream_regs regs;
611

  
612
//icc craziness  i=(int)&pad1;//////////align!!! FIXME
613

  
614
  // build a list of packets to be processed
615
  clst=cluster;
616
  grouped=0;
617
  advanced=0;
618
  can_advance=1;
619
  group_ev_od=-1; // silence incorrect compiler warning
620
  pkt=*clst;
621
  do{ // find a new packet
622
    if(grouped==GROUP_PARALLELISM){
623
      // full
624
      break;
625
    }
626
    if(pkt==NULL){
627
      // no more ranges
628
      break;
629
    }
630
    if(pkt>=*(clst+1)){
631
      // out of this range, try next
632
      clst++;clst++;
633
      pkt=*clst;
634
      continue;
635
    }
636

  
637
    do{ // handle this packet
638
      xc0=pkt[3]&0xc0;
639
      DBG(fprintf(stderr,"   exam pkt=%p, xc0=%02x, can_adv=%i\n",pkt,xc0,can_advance));
640
      if(xc0==0x00){
641
        DBG(fprintf(stderr,"skip clear pkt %p (can_advance is %i)\n",pkt,can_advance));
642
        advanced+=can_advance;
643
        stat_no_scramble++;
644
        break;
645
      }
646
      if(xc0==0x40){
647
        DBG(fprintf(stderr,"skip reserved pkt %p (can_advance is %i)\n",pkt,can_advance));
648
        advanced+=can_advance;
649
        stat_reserved++;
650
        break;
651
      }
652
      if(xc0==0x80||xc0==0xc0){ // encrypted
653
        ev_od=(xc0&0x40)>>6; // 0 even, 1 odd
654
        if(grouped==0) group_ev_od=ev_od; // this group will be all even (or odd)
655
        if(group_ev_od==ev_od){ // could be added to group
656
          pkt[3]&=0x3f;  // consider it decrypted now
657
          if(pkt[3]&0x20){ // incomplete packet
658
            offset=4+pkt[4]+1;
659
            len=188-offset;
660
            n=len>>3;
661
            residue=len-(n<<3);
662
            if(n==0){ // decrypted==encrypted!
663
              DBG(fprintf(stderr,"DECRYPTED MINI! (can_advance is %i)\n",can_advance));
664
              advanced+=can_advance;
665
              stat_decrypted_mini++;
666
              break; // this doesn't need more processing
667
            }
668
          }else{
669
            len=184;
670
            offset=4;
671
            n=23;
672
            residue=0;
673
          }
674
          g_pkt[grouped]=pkt;
675
          g_len[grouped]=len;
676
          g_offset[grouped]=offset;
677
          g_n[grouped]=n;
678
          g_residue[grouped]=residue;
679
          DBG(fprintf(stderr,"%2i: eo=%i pkt=%p len=%03i n=%2i residue=%i\n",grouped,ev_od,pkt,len,n,residue));
680
          grouped++;
681
          advanced+=can_advance;
682
          stat_decrypted[ev_od]++;
683
        }
684
        else{
685
          can_advance=0;
686
          DBG(fprintf(stderr,"skip pkt %p and can_advance set to 0\n",pkt));
687
          break; // skip and go on
688
        }
689
      }
690
    } while(0);
691

  
692
    if(can_advance){
693
      // move range start forward
694
      *clst+=188;
695
    }
696
    // next packet, if there is one
697
    pkt+=188;
698
  } while(1);
699
  DBG(fprintf(stderr,"-- result: grouped %i pkts, advanced %i pkts\n",grouped,advanced));
700

  
701
  // delete empty ranges and compact list
702
  clst2=cluster;
703
  for(clst=cluster;*clst!=NULL;clst+=2){
704
    // if not empty
705
    if(*clst<*(clst+1)){
706
      // it will remain 
707
      *clst2=*clst;
708
      *(clst2+1)=*(clst+1);
709
      clst2+=2;
710
    }
711
  }
712
  *clst2=NULL;
713

  
714
  if(grouped==0){
715
    // no processing needed
716
    return advanced;
717
  }
718

  
719
  //  sort them, longest payload first
720
  //  we expect many n=23 packets and a few n<23
721
  DBG(fprintf(stderr,"PRESORTING\n"));
722
  for(i=0;i<grouped;i++){
723
    DBG(fprintf(stderr,"%2i of %2i: pkt=%p len=%03i n=%2i residue=%i\n",i,grouped,g_pkt[i],g_len[i],g_n[i],g_residue[i]));
724
    }
725
  // grouped is always <= GROUP_PARALLELISM
726

  
727
#define g_swap(a,b) \
728
    pkt=g_pkt[a]; \
729
    g_pkt[a]=g_pkt[b]; \
730
    g_pkt[b]=pkt; \
731
\
732
    len=g_len[a]; \
733
    g_len[a]=g_len[b]; \
734
    g_len[b]=len; \
735
\
736
    offset=g_offset[a]; \
737
    g_offset[a]=g_offset[b]; \
738
    g_offset[b]=offset; \
739
\
740
    n=g_n[a]; \
741
    g_n[a]=g_n[b]; \
742
    g_n[b]=n; \
743
\
744
    residue=g_residue[a]; \
745
    g_residue[a]=g_residue[b]; \
746
    g_residue[b]=residue;
747

  
748
  // step 1: move n=23 packets before small packets
749
  t23=0;
750
  tsmall=grouped-1;
751
  for(;;){
752
    for(;t23<grouped;t23++){
753
      if(g_n[t23]!=23) break;
754
    }
755
DBG(fprintf(stderr,"t23 after for =%i\n",t23));
756
    
757
    for(;tsmall>=0;tsmall--){
758
      if(g_n[tsmall]==23) break;
759
    }
760
DBG(fprintf(stderr,"tsmall after for =%i\n",tsmall));
761
    
762
    if(tsmall-t23<1) break;
763
    
764
DBG(fprintf(stderr,"swap t23=%i,tsmall=%i\n",t23,tsmall));
765

  
766
    g_swap(t23,tsmall);
767

  
768
    t23++;
769
    tsmall--;
770
DBG(fprintf(stderr,"new t23=%i,tsmall=%i\n\n",t23,tsmall));
771
  }
772
  DBG(fprintf(stderr,"packets with n=23, t23=%i   grouped=%i\n",t23,grouped));
773
  DBG(fprintf(stderr,"MIDSORTING\n"));
774
  for(i=0;i<grouped;i++){
775
    DBG(fprintf(stderr,"%2i of %2i: pkt=%p len=%03i n=%2i residue=%i\n",i,grouped,g_pkt[i],g_len[i],g_n[i],g_residue[i]));
776
    }
777

  
778
  // step 2: sort small packets in decreasing order of n (bubble sort is enough)
779
  for(i=t23;i<grouped;i++){
780
    for(j=i+1;j<grouped;j++){
781
      if(g_n[j]>g_n[i]){
782
        g_swap(i,j);
783
      }
784
    }
785
  }
786
  DBG(fprintf(stderr,"POSTSORTING\n"));
787
  for(i=0;i<grouped;i++){
788
    DBG(fprintf(stderr,"%2i of %2i: pkt=%p len=%03i n=%2i residue=%i\n",i,grouped,g_pkt[i],g_len[i],g_n[i],g_residue[i]));
789
    }
790

  
791
  // we need to know how many packets need 23 iterations, how many 22...
792
  for(i=0;i<=23;i++){
793
    alive[i]=0;
794
  }
795
  // count
796
  alive[23-1]=t23;
797
  for(i=t23;i<grouped;i++){
798
    alive[g_n[i]-1]++;
799
  }
800
  // integrate
801
  for(i=22;i>=0;i--){
802
    alive[i]+=alive[i+1];
803
  }
804
  DBG(fprintf(stderr,"ALIVE\n"));
805
  for(i=0;i<=23;i++){
806
    DBG(fprintf(stderr,"alive%2i=%i\n",i,alive[i]));
807
    }
808

  
809
  // choose key
810
  if(group_ev_od==0){
811
    k=&((struct csa_keys_t *)keys)->even;
812
  }
813
  else{
814
    k=&((struct csa_keys_t *)keys)->odd;
815
  }
816

  
817
  //INIT
818
//#define INITIALIZE_UNUSED_INPUT
819
#ifdef INITIALIZE_UNUSED_INPUT
820
// unnecessary zeroing.
821
// without this, we operate on uninitialized memory
822
// when grouped<GROUP_PARALLELISM, but it's not a problem,
823
// as final results will be discarded.
824
// random data makes debugging sessions difficult.
825
  for(j=0;j<GROUP_PARALLELISM*8;j++) stream_in[j]=0;
826
DBG(fprintf(stderr,"--- WARNING: you could gain speed by not initializing unused memory ---\n"));
827
#else
828
DBG(fprintf(stderr,"--- WARNING: DEBUGGING IS MORE DIFFICULT WHEN PROCESSING RANDOM DATA CHANGING AT EVERY RUN! ---\n"));
829
#endif
830

  
831
  for(g=0;g<grouped;g++){
832
    encp[g]=g_pkt[g];
833
    DBG(fprintf(stderr,"header[%i]=%p (%02x)\n",g,encp[g],*(encp[g])));
834
    encp[g]+=g_offset[g]; // skip header
835
#ifdef COPY_UNALIGNED_PKT
836
    if(((int)encp[g])&0x03) {
837
      memcpy(alignedBuff[g],encp[g],g_len[g]);
838
      unaligned[g]=encp[g];
... This diff was truncated because it exceeds the maximum size that can be displayed.