]>
Commit | Line | Data |
---|---|---|
d2e9e320 RS |
1 | /* |
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. | |
d02b48c6 | 3 | * |
2d48d5dd | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
d2e9e320 RS |
5 | * this file except in compliance with the License. You can obtain a copy |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
d02b48c6 RE |
8 | */ |
9 | ||
298a2f9e | 10 | #include <openssl/crypto.h> |
706457b7 | 11 | #include "des_local.h" |
d02b48c6 | 12 | |
c2e4f17c | 13 | void DES_string_to_key(const char *str, DES_cblock *key) |
0f113f3e MC |
14 | { |
15 | DES_key_schedule ks; | |
16 | int i, length; | |
d02b48c6 | 17 | |
0f113f3e MC |
18 | memset(key, 0, 8); |
19 | length = strlen(str); | |
0f113f3e | 20 | for (i = 0; i < length; i++) { |
56989dcd RS |
21 | register unsigned char j = str[i]; |
22 | ||
0f113f3e MC |
23 | if ((i % 16) < 8) |
24 | (*key)[i % 8] ^= (j << 1); | |
25 | else { | |
26 | /* Reverse the bit order 05/05/92 eay */ | |
27 | j = ((j << 4) & 0xf0) | ((j >> 4) & 0x0f); | |
28 | j = ((j << 2) & 0xcc) | ((j >> 2) & 0x33); | |
29 | j = ((j << 1) & 0xaa) | ((j >> 1) & 0x55); | |
30 | (*key)[7 - (i % 8)] ^= j; | |
31 | } | |
32 | } | |
0f113f3e | 33 | DES_set_odd_parity(key); |
0f113f3e | 34 | DES_set_key_unchecked(key, &ks); |
0f113f3e MC |
35 | DES_cbc_cksum((const unsigned char *)str, key, length, &ks, key); |
36 | OPENSSL_cleanse(&ks, sizeof(ks)); | |
37 | DES_set_odd_parity(key); | |
38 | } | |
d02b48c6 | 39 | |
c2e4f17c | 40 | void DES_string_to_2keys(const char *str, DES_cblock *key1, DES_cblock *key2) |
0f113f3e MC |
41 | { |
42 | DES_key_schedule ks; | |
43 | int i, length; | |
d02b48c6 | 44 | |
0f113f3e MC |
45 | memset(key1, 0, 8); |
46 | memset(key2, 0, 8); | |
47 | length = strlen(str); | |
0f113f3e | 48 | for (i = 0; i < length; i++) { |
56989dcd RS |
49 | register unsigned char j = str[i]; |
50 | ||
0f113f3e MC |
51 | if ((i % 32) < 16) { |
52 | if ((i % 16) < 8) | |
53 | (*key1)[i % 8] ^= (j << 1); | |
54 | else | |
55 | (*key2)[i % 8] ^= (j << 1); | |
56 | } else { | |
57 | j = ((j << 4) & 0xf0) | ((j >> 4) & 0x0f); | |
58 | j = ((j << 2) & 0xcc) | ((j >> 2) & 0x33); | |
59 | j = ((j << 1) & 0xaa) | ((j >> 1) & 0x55); | |
60 | if ((i % 16) < 8) | |
61 | (*key1)[7 - (i % 8)] ^= j; | |
62 | else | |
63 | (*key2)[7 - (i % 8)] ^= j; | |
64 | } | |
65 | } | |
66 | if (length <= 8) | |
67 | memcpy(key2, key1, 8); | |
0f113f3e MC |
68 | DES_set_odd_parity(key1); |
69 | DES_set_odd_parity(key2); | |
0f113f3e | 70 | DES_set_key_unchecked(key1, &ks); |
0f113f3e | 71 | DES_cbc_cksum((const unsigned char *)str, key1, length, &ks, key1); |
0f113f3e | 72 | DES_set_key_unchecked(key2, &ks); |
0f113f3e MC |
73 | DES_cbc_cksum((const unsigned char *)str, key2, length, &ks, key2); |
74 | OPENSSL_cleanse(&ks, sizeof(ks)); | |
75 | DES_set_odd_parity(key1); | |
76 | DES_set_odd_parity(key2); | |
77 | } |