This repository has been archived by the owner on Jan 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
snak.c
183 lines (150 loc) · 3.67 KB
/
snak.c
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <ctype.h>
#include <inttypes.h>
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "h.h"
static bool
overflow15(uint32_t a, uint32_t b)
{
return (a + b > 0x7FFF);
}
static char *
mnsnc(const char *s)
{
static char buf[3];
uint16_t a;
uint16_t flag;
int c = s[8] % 16;
for (a = 0; *s != '\0'; ++s) {
if (overflow15(a, *s))
flag = 1;
else
flag = 0;
a = flag | (2 * (a + *s));
}
for (; c > 0; --c)
a = ((a & 0x8000U) >> 15) | (uint16_t)(2 * a);
buf[2] = 0;
buf[1] = a % 26 + 'a';
a /= 26;
buf[0] = a % 26 + 'a';
return buf;
}
static void
strbn(char *out, unsigned int in)
{
static const char *alphabet = "abcdefghijklmnopqrstuvwxyz";
size_t i;
for (i = 3; i > 0; --i) {
out[i - 1] = alphabet[in % 26];
in /= 26;
}
}
static void
decfrp(char *s)
{
char *p;
unsigned char a, b;
a = b = 0;
for (p = s + strlen(s) - 1; p >= s; --p) {
a = (*p - 'a' + b) % 26;
a = (a + 'a') & 0xFF;
b += a;
b %= 26;
*p = a;
}
}
/* Implementation of xorshift* without retaining seed state. */
static uint64_t
rnd(uint64_t seed)
{
seed ^= seed >> 12;
seed ^= seed << 25;
seed ^= seed >> 27;
return seed * 0x2545F4914F6CDD1D;
}
static unsigned int
mkver(unsigned int lictype, unsigned int major, unsigned int minor)
{
return ((lictype << 12) | (major * 10 + minor));
}
static void
mksnak(bool has_snakext, uint16_t product_id, uint16_t major, uint16_t minor,
char *serno, char *actkey)
{
const char *cksum;
uint64_t serial;
unsigned int version = mkver((has_snakext ? 3 : 2), major, minor);
char merged[18];
memset(serno, 0, 10);
memset(actkey, 0, 9);
#ifdef DBG
printf("has_snakext: %d, major: %u, minor: %u, version: %u\n",
!!has_snakext, major, minor, version);
#endif
/* bitmask to ensure at most six digits */
serial = rnd((uintptr_t)serno * time(NULL)) & 0xEFFFF;
snprintf(serno, 10, "SCO%06" PRIu64, serial);
strbn(actkey, product_id);
strbn(actkey + 3, version);
snprintf(merged, sizeof(merged), "%s%s", serno, actkey);
cksum = mnsnc(merged);
actkey[6] = cksum[0];
actkey[7] = cksum[1];
decfrp(actkey);
}
static uint16_t
strtou16lim(const char *in, unsigned long limit)
{
char *end;
unsigned long i;
if (limit > UINT16_MAX)
die("internal: limit > UINT16_MAX");
i = strtoul(in, &end, 10);
if (*in == '\0' || *end != '\0')
die("%s not a number", in);
if (i > limit)
die("%s out of range (max %"PRIu16")", in, limit);
return i;
}
int
gen_snak(int argc, char *argv[])
{
char *snakext = NULL;
uint16_t product_id, major, minor;
char serno[10], actkey[9], snakextmd[7];
if (argc < 3) {
usage(true);
return EXIT_FAILURE;
}
/* "zzz" is the maximum possible product ID encoded value.
* This decodes to a value of 17575.
*/
product_id = strtou16lim(argv[0], 17575);
/* License type (whether snakext is to be read), version major and
* version minor share an integer, max encoded as "zzz".
* The license type is shifted up by 12, leaving 0xFFF (4095) for the
* version major and minor.
* Of that, the version major is all the upper digits, and the minor is
* the bottom digit (i.e. version/10 => major, version%10 => minor).
*
* Without doing too much checking, the maximum major version is 409 and
* the maximum minor version is 9.
*/
major = strtou16lim(argv[1], 409);
minor = strtou16lim(argv[2], 9);
if (argc >= 4)
snakext = argv[3];
mksnak(snakext != NULL, product_id, major, minor, serno, actkey);
printf("Serial number: %s\n"
"Activation key: %s\n", serno, actkey);
if (snakext != NULL) {
mdsnakext(serno, actkey, snakext, snakextmd);
printf("License data: %s;m%s\n", snakext, snakextmd);
}
return EXIT_SUCCESS;
}