forked from wmcbrine/tivodecode-ng
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTuring.hxx
107 lines (88 loc) · 3.97 KB
/
Turing.hxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* Interface definition of Turing
*
* Copyright C 2002, Qualcomm Inc. Written by Greg Rose
*/
/*
This software is free for commercial and non-commercial use subject to
the following conditions:
1. Copyright remains vested in QUALCOMM Incorporated, and Copyright
notices in the code are not to be removed. If this package is used in
a product, QUALCOMM should be given attribution as the author of the
Turing encryption algorithm. This can be in the form of a textual
message at program startup or in documentation (online or textual)
provided with the package.
2. Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
a. Redistributions of source code must retain the copyright notice,
this list of conditions and the following disclaimer.
b. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the
distribution.
c. All advertising materials mentioning features or use of this
software must display the following acknowledgement: This product
includes software developed by QUALCOMM Incorporated.
3. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AND AGAINST
INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4. The license and distribution terms for any publically available version
or derivative of this code cannot be changed, that is, this code cannot
simply be copied and put under another distribution license including
the GNU Public License.
5. The Turing family of encryption algorithms are covered by patents in
the United States of America and other countries. A free and
irrevocable license is hereby granted for the use of such patents to
the extent required to utilize the Turing family of encryption
algorithms for any purpose, subject to the condition that any
commercial product utilising any of the Turing family of encryption
algorithms should show the words "Encryption by QUALCOMM" either on the
product or in the associated documentation.
*/
#ifndef TURING_H
#define TURING_H 1
#define MAXKEY 32 /* bytes*/
#define MAXKIV 48 /* bytes*/
#define MAXSTREAM 340 /* bytes, maximum stream generated by one call */
#define LFSRLEN 17 /* words*/
#define OUTLEN 20 /* bytes*/
#define POLY 0x4D /* x^8 + x^6 + x^3 + x^2 + 1 */
#include <stdint.h>
class Turing
{
private:
int keylen; /* adjusted to count WORDs */
uint32_t K[MAXKEY / 4]; /* storage for mixed key */
uint32_t R[LFSRLEN]; /* the shift register */
/* precalculated S-boxes */
uint32_t S0[256], S1[256], S2[256], S3[256];
public:
void key(const uint8_t key[], const int keylength);
void IV(const uint8_t iv[], const int ivlength);
int gen(uint8_t *buf); /* returns number of bytes of mask generated */
};
/* some useful macros -- big-endian */
#define B(x,i) ((uint8_t)(((x) >> (24 - 8 * i)) & 0xFF))
#define WORD2BYTE(w, b) { \
(b)[3] = B(w, 3); \
(b)[2] = B(w, 2); \
(b)[1] = B(w, 1); \
(b)[0] = B(w, 0); \
}
#define BYTE2WORD(b) ( \
(((uint32_t)(b)[0] & 0xFF) << 24) | \
(((uint32_t)(b)[1] & 0xFF) << 16) | \
(((uint32_t)(b)[2] & 0xFF) << 8) | \
(((uint32_t)(b)[3] & 0xFF)) \
)
#define ROTL(w,x) (((w) << (x))|((w) >> (32 - (x))))
#endif