summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/des/set_key.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/des/set_key.c')
-rw-r--r--src/lib/libcrypto/des/set_key.c65
1 files changed, 33 insertions, 32 deletions
diff --git a/src/lib/libcrypto/des/set_key.c b/src/lib/libcrypto/des/set_key.c
index 07b8d03da1..91116c4d16 100644
--- a/src/lib/libcrypto/des/set_key.c
+++ b/src/lib/libcrypto/des/set_key.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: set_key.c,v 1.22 2023/07/08 07:11:07 beck Exp $ */ 1/* $OpenBSD: set_key.c,v 1.23 2023/07/08 07:34:34 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -84,7 +84,8 @@ static const unsigned char odd_parity[256] = {
84 193, 193, 194, 194, 196, 196, 199, 199, 200, 200, 203, 203, 205, 205, 206, 206, 84 193, 193, 194, 194, 196, 196, 199, 199, 200, 200, 203, 203, 205, 205, 206, 206,
85 208, 208, 211, 211, 213, 213, 214, 214, 217, 217, 218, 218, 220, 220, 223, 223, 85 208, 208, 211, 211, 213, 213, 214, 214, 217, 217, 218, 218, 220, 220, 223, 223,
86 224, 224, 227, 227, 229, 229, 230, 230, 233, 233, 234, 234, 236, 236, 239, 239, 86 224, 224, 227, 227, 229, 229, 230, 230, 233, 233, 234, 234, 236, 236, 239, 239,
87 241, 241, 242, 242, 244, 244, 247, 247, 248, 248, 251, 251, 253, 253, 254, 254}; 87 241, 241, 242, 242, 244, 244, 247, 247, 248, 248, 251, 251, 253, 253, 254, 254,
88};
88 89
89void 90void
90DES_set_odd_parity(DES_cblock *key) 91DES_set_odd_parity(DES_cblock *key)
@@ -100,8 +101,7 @@ DES_check_key_parity(const_DES_cblock *key)
100{ 101{
101 unsigned int i; 102 unsigned int i;
102 103
103 for (i = 0; i < DES_KEY_SZ; i++) 104 for (i = 0; i < DES_KEY_SZ; i++) {
104 {
105 if ((*key)[i] != odd_parity[(*key)[i]]) 105 if ((*key)[i] != odd_parity[(*key)[i]])
106 return (0); 106 return (0);
107 } 107 }
@@ -120,23 +120,24 @@ DES_check_key_parity(const_DES_cblock *key)
120#define NUM_WEAK_KEY 16 120#define NUM_WEAK_KEY 16
121static const DES_cblock weak_keys[NUM_WEAK_KEY] = { 121static const DES_cblock weak_keys[NUM_WEAK_KEY] = {
122 /* weak keys */ 122 /* weak keys */
123 {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01}, 123 {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
124 {0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE}, 124 {0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE},
125 {0x1F,0x1F,0x1F,0x1F,0x0E,0x0E,0x0E,0x0E}, 125 {0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E},
126 {0xE0,0xE0,0xE0,0xE0,0xF1,0xF1,0xF1,0xF1}, 126 {0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1},
127 /* semi-weak keys */ 127 /* semi-weak keys */
128 {0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE}, 128 {0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE},
129 {0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01}, 129 {0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01},
130 {0x1F,0xE0,0x1F,0xE0,0x0E,0xF1,0x0E,0xF1}, 130 {0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1},
131 {0xE0,0x1F,0xE0,0x1F,0xF1,0x0E,0xF1,0x0E}, 131 {0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E},
132 {0x01,0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1}, 132 {0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1},
133 {0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1,0x01}, 133 {0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01},
134 {0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E,0xFE}, 134 {0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE},
135 {0xFE,0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E}, 135 {0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E},
136 {0x01,0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E}, 136 {0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E},
137 {0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E,0x01}, 137 {0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01},
138 {0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE}, 138 {0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE},
139 {0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}}; 139 {0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1},
140};
140 141
141int 142int
142DES_is_weak_key(const_DES_cblock *key) 143DES_is_weak_key(const_DES_cblock *key)
@@ -161,7 +162,7 @@ DES_is_weak_key(const_DES_cblock *key)
161 162
162static const DES_LONG des_skb[8][64] = { 163static const DES_LONG des_skb[8][64] = {
163 { 164 {
164 /* for C bits (numbered as per FIPS 46) 1 2 3 4 5 6 */ 165 /* for C bits (numbered as per FIPS 46) 1 2 3 4 5 6 */
165 0x00000000L, 0x00000010L, 0x20000000L, 0x20000010L, 166 0x00000000L, 0x00000010L, 0x20000000L, 0x20000010L,
166 0x00010000L, 0x00010010L, 0x20010000L, 0x20010010L, 167 0x00010000L, 0x00010010L, 0x20010000L, 0x20010010L,
167 0x00000800L, 0x00000810L, 0x20000800L, 0x20000810L, 168 0x00000800L, 0x00000810L, 0x20000800L, 0x20000810L,
@@ -179,7 +180,7 @@ static const DES_LONG des_skb[8][64] = {
179 0x00080820L, 0x00080830L, 0x20080820L, 0x20080830L, 180 0x00080820L, 0x00080830L, 0x20080820L, 0x20080830L,
180 0x00090820L, 0x00090830L, 0x20090820L, 0x20090830L, 181 0x00090820L, 0x00090830L, 0x20090820L, 0x20090830L,
181 }, { 182 }, {
182 /* for C bits (numbered as per FIPS 46) 7 8 10 11 12 13 */ 183 /* for C bits (numbered as per FIPS 46) 7 8 10 11 12 13 */
183 0x00000000L, 0x02000000L, 0x00002000L, 0x02002000L, 184 0x00000000L, 0x02000000L, 0x00002000L, 0x02002000L,
184 0x00200000L, 0x02200000L, 0x00202000L, 0x02202000L, 185 0x00200000L, 0x02200000L, 0x00202000L, 0x02202000L,
185 0x00000004L, 0x02000004L, 0x00002004L, 0x02002004L, 186 0x00000004L, 0x02000004L, 0x00002004L, 0x02002004L,
@@ -197,7 +198,7 @@ static const DES_LONG des_skb[8][64] = {
197 0x10000404L, 0x12000404L, 0x10002404L, 0x12002404L, 198 0x10000404L, 0x12000404L, 0x10002404L, 0x12002404L,
198 0x10200404L, 0x12200404L, 0x10202404L, 0x12202404L, 199 0x10200404L, 0x12200404L, 0x10202404L, 0x12202404L,
199 }, { 200 }, {
200 /* for C bits (numbered as per FIPS 46) 14 15 16 17 19 20 */ 201 /* for C bits (numbered as per FIPS 46) 14 15 16 17 19 20 */
201 0x00000000L, 0x00000001L, 0x00040000L, 0x00040001L, 202 0x00000000L, 0x00000001L, 0x00040000L, 0x00040001L,
202 0x01000000L, 0x01000001L, 0x01040000L, 0x01040001L, 203 0x01000000L, 0x01000001L, 0x01040000L, 0x01040001L,
203 0x00000002L, 0x00000003L, 0x00040002L, 0x00040003L, 204 0x00000002L, 0x00000003L, 0x00040002L, 0x00040003L,
@@ -215,7 +216,7 @@ static const DES_LONG des_skb[8][64] = {
215 0x08000202L, 0x08000203L, 0x08040202L, 0x08040203L, 216 0x08000202L, 0x08000203L, 0x08040202L, 0x08040203L,
216 0x09000202L, 0x09000203L, 0x09040202L, 0x09040203L, 217 0x09000202L, 0x09000203L, 0x09040202L, 0x09040203L,
217 }, { 218 }, {
218 /* for C bits (numbered as per FIPS 46) 21 23 24 26 27 28 */ 219 /* for C bits (numbered as per FIPS 46) 21 23 24 26 27 28 */
219 0x00000000L, 0x00100000L, 0x00000100L, 0x00100100L, 220 0x00000000L, 0x00100000L, 0x00000100L, 0x00100100L,
220 0x00000008L, 0x00100008L, 0x00000108L, 0x00100108L, 221 0x00000008L, 0x00100008L, 0x00000108L, 0x00100108L,
221 0x00001000L, 0x00101000L, 0x00001100L, 0x00101100L, 222 0x00001000L, 0x00101000L, 0x00001100L, 0x00101100L,
@@ -233,7 +234,7 @@ static const DES_LONG des_skb[8][64] = {
233 0x04021000L, 0x04121000L, 0x04021100L, 0x04121100L, 234 0x04021000L, 0x04121000L, 0x04021100L, 0x04121100L,
234 0x04021008L, 0x04121008L, 0x04021108L, 0x04121108L, 235 0x04021008L, 0x04121008L, 0x04021108L, 0x04121108L,
235 }, { 236 }, {
236 /* for D bits (numbered as per FIPS 46) 1 2 3 4 5 6 */ 237 /* for D bits (numbered as per FIPS 46) 1 2 3 4 5 6 */
237 0x00000000L, 0x10000000L, 0x00010000L, 0x10010000L, 238 0x00000000L, 0x10000000L, 0x00010000L, 0x10010000L,
238 0x00000004L, 0x10000004L, 0x00010004L, 0x10010004L, 239 0x00000004L, 0x10000004L, 0x00010004L, 0x10010004L,
239 0x20000000L, 0x30000000L, 0x20010000L, 0x30010000L, 240 0x20000000L, 0x30000000L, 0x20010000L, 0x30010000L,
@@ -251,7 +252,7 @@ static const DES_LONG des_skb[8][64] = {
251 0x20101000L, 0x30101000L, 0x20111000L, 0x30111000L, 252 0x20101000L, 0x30101000L, 0x20111000L, 0x30111000L,
252 0x20101004L, 0x30101004L, 0x20111004L, 0x30111004L, 253 0x20101004L, 0x30101004L, 0x20111004L, 0x30111004L,
253 }, { 254 }, {
254 /* for D bits (numbered as per FIPS 46) 8 9 11 12 13 14 */ 255 /* for D bits (numbered as per FIPS 46) 8 9 11 12 13 14 */
255 0x00000000L, 0x08000000L, 0x00000008L, 0x08000008L, 256 0x00000000L, 0x08000000L, 0x00000008L, 0x08000008L,
256 0x00000400L, 0x08000400L, 0x00000408L, 0x08000408L, 257 0x00000400L, 0x08000400L, 0x00000408L, 0x08000408L,
257 0x00020000L, 0x08020000L, 0x00020008L, 0x08020008L, 258 0x00020000L, 0x08020000L, 0x00020008L, 0x08020008L,
@@ -269,7 +270,7 @@ static const DES_LONG des_skb[8][64] = {
269 0x02020001L, 0x0A020001L, 0x02020009L, 0x0A020009L, 270 0x02020001L, 0x0A020001L, 0x02020009L, 0x0A020009L,
270 0x02020401L, 0x0A020401L, 0x02020409L, 0x0A020409L, 271 0x02020401L, 0x0A020401L, 0x02020409L, 0x0A020409L,
271 }, { 272 }, {
272 /* for D bits (numbered as per FIPS 46) 16 17 18 19 20 21 */ 273 /* for D bits (numbered as per FIPS 46) 16 17 18 19 20 21 */
273 0x00000000L, 0x00000100L, 0x00080000L, 0x00080100L, 274 0x00000000L, 0x00000100L, 0x00080000L, 0x00080100L,
274 0x01000000L, 0x01000100L, 0x01080000L, 0x01080100L, 275 0x01000000L, 0x01000100L, 0x01080000L, 0x01080100L,
275 0x00000010L, 0x00000110L, 0x00080010L, 0x00080110L, 276 0x00000010L, 0x00000110L, 0x00080010L, 0x00080110L,
@@ -287,7 +288,7 @@ static const DES_LONG des_skb[8][64] = {
287 0x00200210L, 0x00200310L, 0x00280210L, 0x00280310L, 288 0x00200210L, 0x00200310L, 0x00280210L, 0x00280310L,
288 0x01200210L, 0x01200310L, 0x01280210L, 0x01280310L, 289 0x01200210L, 0x01200310L, 0x01280210L, 0x01280310L,
289 }, { 290 }, {
290 /* for D bits (numbered as per FIPS 46) 22 23 24 25 27 28 */ 291 /* for D bits (numbered as per FIPS 46) 22 23 24 25 27 28 */
291 0x00000000L, 0x04000000L, 0x00040000L, 0x04040000L, 292 0x00000000L, 0x04000000L, 0x00040000L, 0x04040000L,
292 0x00000002L, 0x04000002L, 0x00040002L, 0x04040002L, 293 0x00000002L, 0x04000002L, 0x00040002L, 0x04040002L,
293 0x00002000L, 0x04002000L, 0x00042000L, 0x04042000L, 294 0x00002000L, 0x04002000L, 0x00042000L, 0x04042000L,
@@ -304,7 +305,8 @@ static const DES_LONG des_skb[8][64] = {
304 0x00000822L, 0x04000822L, 0x00040822L, 0x04040822L, 305 0x00000822L, 0x04000822L, 0x00040822L, 0x04040822L,
305 0x00002820L, 0x04002820L, 0x00042820L, 0x04042820L, 306 0x00002820L, 0x04002820L, 0x00042820L, 0x04042820L,
306 0x00002822L, 0x04002822L, 0x00042822L, 0x04042822L, 307 0x00002822L, 0x04002822L, 0x00042822L, 0x04042822L,
307 }}; 308 },
309};
308 310
309int 311int
310DES_set_key(const_DES_cblock *key, DES_key_schedule *schedule) 312DES_set_key(const_DES_cblock *key, DES_key_schedule *schedule)
@@ -335,7 +337,7 @@ DES_set_key_checked(const_DES_cblock *key, DES_key_schedule *schedule)
335void 337void
336DES_set_key_unchecked(const_DES_cblock *key, DES_key_schedule *schedule) 338DES_set_key_unchecked(const_DES_cblock *key, DES_key_schedule *schedule)
337{ 339{
338 static const int shifts2[16] = {0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0}; 340 static const int shifts2[16] = {0, 0,1, 1,1, 1,1, 1,0, 1,1, 1,1, 1,1, 0};
339 DES_LONG c, d, t, s, t2; 341 DES_LONG c, d, t, s, t2;
340 const unsigned char *in; 342 const unsigned char *in;
341 DES_LONG *k; 343 DES_LONG *k;
@@ -360,8 +362,7 @@ DES_set_key_unchecked(const_DES_cblock *key, DES_key_schedule *schedule)
360 ((d & 0x00ff0000L) >> 16L)|((c & 0xf0000000L) >> 4L)); 362 ((d & 0x00ff0000L) >> 16L)|((c & 0xf0000000L) >> 4L));
361 c &= 0x0fffffffL; 363 c &= 0x0fffffffL;
362 364
363 for (i = 0; i < ITERATIONS; i++) 365 for (i = 0; i < ITERATIONS; i++) {
364 {
365 if (shifts2[i]) { 366 if (shifts2[i]) {
366 c = ((c >> 2L)|(c << 26L)); 367 c = ((c >> 2L)|(c << 26L));
367 d = ((d >> 2L)|(d << 26L)); 368 d = ((d >> 2L)|(d << 26L));