1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
5 * This code was based on:
6 * PC1 Cipher Algorithm ( Pukall Cipher 1 )
7 * By Alexander PUKALL 1991
8 * free code no restriction to use
9 * please include the name of the Author in the final software
11 * http://membres.lycos.fr/pc1/
19 #include <unistd.h> /* for unlink() */
21 #include <getopt.h> /* for getopt() */
34 unsigned short x1a0
[8];
40 unsigned short compte
;
41 unsigned char cle
[17];
45 static void pc1_finish(struct pc1_ctx
*pc1
)
47 /* erase all variables */
48 memset(pc1
, 0, sizeof(struct pc1_ctx
));
51 static void pc1_code(struct pc1_ctx
*pc1
)
53 pc1
->dx
= pc1
->x1a2
+ pc1
->i
;
54 pc1
->ax
= pc1
->x1a0
[pc1
->i
];
67 pc1
->ax
= pc1
->ax
* pc1
->bx
;
75 pc1
->ax
= pc1
->ax
* pc1
->si
;
76 pc1
->cx
= pc1
->ax
+ pc1
->cx
;
82 pc1
->ax
= pc1
->ax
* pc1
->bx
;
83 pc1
->dx
= pc1
->cx
+ pc1
->dx
;
85 pc1
->ax
= pc1
->ax
+ 1;
88 pc1
->x1a0
[pc1
->i
] = pc1
->ax
;
90 pc1
->res
= pc1
->ax
^ pc1
->dx
;
94 static void pc1_assemble(struct pc1_ctx
*pc1
)
96 pc1
->x1a0
[0] = (pc1
->cle
[0] * 256) + pc1
->cle
[1];
99 pc1
->inter
= pc1
->res
;
101 pc1
->x1a0
[1] = pc1
->x1a0
[0] ^ ((pc1
->cle
[2]*256) + pc1
->cle
[3]);
103 pc1
->inter
= pc1
->inter
^ pc1
->res
;
105 pc1
->x1a0
[2] = pc1
->x1a0
[1] ^ ((pc1
->cle
[4]*256) + pc1
->cle
[5]);
107 pc1
->inter
= pc1
->inter
^ pc1
->res
;
109 pc1
->x1a0
[3] = pc1
->x1a0
[2] ^ ((pc1
->cle
[6]*256) + pc1
->cle
[7]);
111 pc1
->inter
= pc1
->inter
^ pc1
->res
;
113 pc1
->x1a0
[4] = pc1
->x1a0
[3] ^ ((pc1
->cle
[8]*256) + pc1
->cle
[9]);
115 pc1
->inter
= pc1
->inter
^ pc1
->res
;
117 pc1
->x1a0
[5] = pc1
->x1a0
[4] ^ ((pc1
->cle
[10]*256) + pc1
->cle
[11]);
119 pc1
->inter
= pc1
->inter
^ pc1
->res
;
121 pc1
->x1a0
[6] = pc1
->x1a0
[5] ^ ((pc1
->cle
[12]*256) + pc1
->cle
[13]);
123 pc1
->inter
= pc1
->inter
^ pc1
->res
;
125 pc1
->x1a0
[7] = pc1
->x1a0
[6] ^ ((pc1
->cle
[14]*256) + pc1
->cle
[15]);
127 pc1
->inter
= pc1
->inter
^ pc1
->res
;
132 static unsigned char pc1_decrypt(struct pc1_ctx
*pc1
, short c
)
135 pc1
->cfc
= pc1
->inter
>> 8;
136 pc1
->cfd
= pc1
->inter
& 255; /* cfc^cfd = random byte */
138 c
= c
^ (pc1
->cfc
^ pc1
->cfd
);
139 for (pc1
->compte
= 0; pc1
->compte
<= 15; pc1
->compte
++) {
140 /* we mix the plaintext byte with the key */
141 pc1
->cle
[pc1
->compte
] = pc1
->cle
[pc1
->compte
] ^ c
;
147 static unsigned char pc1_encrypt(struct pc1_ctx
*pc1
, short c
)
150 pc1
->cfc
= pc1
->inter
>> 8;
151 pc1
->cfd
= pc1
->inter
& 255; /* cfc^cfd = random byte */
153 for (pc1
->compte
= 0; pc1
->compte
<= 15; pc1
->compte
++) {
154 /* we mix the plaintext byte with the key */
155 pc1
->cle
[pc1
->compte
] = pc1
->cle
[pc1
->compte
] ^ c
;
157 c
= c
^ (pc1
->cfc
^ pc1
->cfd
);
162 static void pc1_init(struct pc1_ctx
*pc1
)
164 memset(pc1
, 0, sizeof(struct pc1_ctx
));
166 /* ('Remsaalps!123456') is the key used, you can change it */
167 strcpy(pc1
->cle
, "Remsaalps!123456");
170 static void pc1_decrypt_buf(struct pc1_ctx
*pc1
, void *data
, unsigned len
)
172 unsigned char *buf
= data
;
175 for (i
= 0; i
< len
; i
++)
176 buf
[i
] = pc1_decrypt(pc1
, buf
[i
]);
179 static void pc1_encrypt_buf(struct pc1_ctx
*pc1
, void *data
, unsigned len
)
181 unsigned char *buf
= data
;
184 for (i
= 0; i
< len
; i
++)
185 buf
[i
] = pc1_encrypt(pc1
, buf
[i
]);
192 static char *progname
;
199 #define ERR(fmt, ...) do { \
201 fprintf(stderr, "[%s] *** error: " fmt "\n", \
202 progname, ## __VA_ARGS__ ); \
205 #define ERRS(fmt, ...) do { \
208 fprintf(stderr, "[%s] *** error: " fmt ": %s\n", \
209 progname, ## __VA_ARGS__, strerror(save)); \
212 void usage(int status
)
214 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
216 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
220 " -d decrypt instead of encrypt"
221 " -i <file> read input from the file <file>\n"
222 " -o <file> write output to the file <file>\n"
223 " -h show this screen\n"
229 #define BUFSIZE (64 * 1024)
231 int main(int argc
, char *argv
[])
234 int res
= EXIT_FAILURE
;
240 FILE *outfile
, *infile
;
242 progname
= basename(argv
[0]);
247 c
= getopt(argc
, argv
, "di:o:h");
270 if (ifname
== NULL
) {
271 ERR("no input file specified");
275 if (ofname
== NULL
) {
276 ERR("no output file specified");
280 err
= stat(ifname
, &st
);
282 ERRS("stat failed on %s", ifname
);
287 buf
= malloc(BUFSIZE
);
289 ERR("no memory for buffer\n");
293 infile
= fopen(ifname
, "r");
294 if (infile
== NULL
) {
295 ERRS("could not open \"%s\" for reading", ifname
);
299 outfile
= fopen(ofname
, "w");
300 if (outfile
== NULL
) {
301 ERRS("could not open \"%s\" for writing", ofname
);
315 fread(buf
, datalen
, 1, infile
);
317 ERRS("unable to read from file %s", ifname
);
322 pc1_decrypt_buf(&pc1
, buf
, datalen
);
324 pc1_encrypt_buf(&pc1
, buf
, datalen
);
327 fwrite(buf
, datalen
, 1, outfile
);
329 ERRS("unable to write to file %s", ofname
);
343 if (res
!= EXIT_SUCCESS
) {