aboutsummaryrefslogtreecommitdiffstats
path: root/main/musl/1002-add-legacy-functions-setkey-and-encrypt.patch
blob: a65049a56aebbb0aadf49214ab0e6100de41df24 (plain)
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
From ea610e10b6ea99dbb774b7231884002324ffc478 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timo=20Ter=C3=A4s?= <timo.teras@iki.fi>
Date: Mon, 13 Jan 2014 13:05:14 +0200
Subject: [PATCH] add legacy functions setkey() and encrypt()

---
 src/crypt/crypt_des.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 63 insertions(+), 5 deletions(-)

diff --git a/src/crypt/crypt_des.c b/src/crypt/crypt_des.c
index dc95dca..05f69e7 100644
--- a/src/crypt/crypt_des.c
+++ b/src/crypt/crypt_des.c
@@ -55,6 +55,8 @@
 
 #include <stdint.h>
 #include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
 
 struct expanded_key {
 	uint32_t l[16], r[16];
@@ -755,8 +757,10 @@ static void des_setkey(const unsigned char *key, struct expanded_key *ekey)
  */
 static void do_des(uint32_t l_in, uint32_t r_in,
     uint32_t *l_out, uint32_t *r_out,
-    uint32_t count, uint32_t saltbits, const struct expanded_key *ekey)
+    int32_t count, uint32_t saltbits, const struct expanded_key *ekey)
 {
+	const uint32_t *rkl, *rkr;
+	int klradd;
 	uint32_t l, r;
 
 	/*
@@ -773,13 +777,24 @@ static void do_des(uint32_t l_in, uint32_t r_in,
 		}
 	}
 
+	if (count < 0) {
+		count = -count;
+		rkl = &ekey->l[15];
+		rkr = &ekey->r[15];
+		klradd = -1;
+	} else {
+		rkl = &ekey->l[0];
+		rkr = &ekey->r[0];
+		klradd = 1;
+	}
+
 	while (count--) {
 		/*
 		 * Do each round.
 		 */
 		unsigned int round = 16;
-		const uint32_t *kl = ekey->l;
-		const uint32_t *kr = ekey->r;
+		const uint32_t *kl = rkl;
+		const uint32_t *kr = rkr;
 		uint32_t f;
 		while (round--) {
 			uint32_t r48l, r48r;
@@ -802,8 +817,10 @@ static void do_des(uint32_t l_in, uint32_t r_in,
 			 * XOR with the permuted key.
 			 */
 			f = (r48l ^ r48r) & saltbits;
-			r48l ^= f ^ *kl++;
-			r48r ^= f ^ *kr++;
+			r48l ^= f ^ *kl;
+			r48r ^= f ^ *kr;
+			kl += klradd;
+			kr += klradd;
 			/*
 			 * Do S-box lookups (which shrink it back to 32 bits)
 			 * and do the P-box permutation at the same time.
@@ -1016,3 +1033,44 @@ char *__crypt_des(const char *key, const char *setting, char *output)
 
 	return (setting[0]=='*') ? "x" : "*";
 }
+
+static struct expanded_key __encrypt_key;
+
+void setkey(const char *key)
+{
+	unsigned char bkey[8];
+	const char *pkey = key;
+	int i, j;
+
+	for (i = 0; i < 8; i++) {
+		bkey[i] = 0;
+		for (j = 7; j >= 0; j--, pkey++) {
+			if (*pkey & 1)
+				bkey[i] |= 1 << j;
+		}
+	}
+
+	des_setkey(bkey, &__encrypt_key);
+}
+
+void encrypt(char *block, int edflag)
+{
+	unsigned char *p;
+	uint32_t b[2];
+	int i, j;
+
+	p = (unsigned char*) block;
+	for (i = 0; i < 2; i++) {
+		b[i] = 0;
+		for (j = 31; j >= 0; j--, p++)
+			if (*p & 1)
+				b[i] |= 1 << j;
+	}
+
+	do_des(b[0], b[1], b, b + 1, edflag ? -1 : 1, 0, &__encrypt_key);
+
+	p = (unsigned char*) block;
+	for (i = 0; i < 2; i++)
+		for (j = 31; j >= 0; j--)
+			*p++ = (b[i] & (1 << j)) ? 1 : 0;
+}
-- 
1.8.5.2