/src/openssl/crypto/des/str2key.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
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 |
8 | | */ |
9 | | |
10 | | /* |
11 | | * DES low level APIs are deprecated for public use, but still ok for internal |
12 | | * use. |
13 | | */ |
14 | | #include "internal/deprecated.h" |
15 | | |
16 | | #include <openssl/crypto.h> |
17 | | #include "des_local.h" |
18 | | |
19 | | void DES_string_to_key(const char *str, DES_cblock *key) |
20 | 0 | { |
21 | 0 | DES_key_schedule ks; |
22 | 0 | int i, length; |
23 | |
|
24 | 0 | memset(key, 0, 8); |
25 | 0 | length = strlen(str); |
26 | 0 | for (i = 0; i < length; i++) { |
27 | 0 | register unsigned char j = str[i]; |
28 | |
|
29 | 0 | if ((i % 16) < 8) |
30 | 0 | (*key)[i % 8] ^= (j << 1); |
31 | 0 | else { |
32 | | /* Reverse the bit order 05/05/92 eay */ |
33 | 0 | j = ((j << 4) & 0xf0) | ((j >> 4) & 0x0f); |
34 | 0 | j = ((j << 2) & 0xcc) | ((j >> 2) & 0x33); |
35 | 0 | j = ((j << 1) & 0xaa) | ((j >> 1) & 0x55); |
36 | 0 | (*key)[7 - (i % 8)] ^= j; |
37 | 0 | } |
38 | 0 | } |
39 | 0 | DES_set_odd_parity(key); |
40 | 0 | DES_set_key_unchecked(key, &ks); |
41 | 0 | DES_cbc_cksum((const unsigned char *)str, key, length, &ks, key); |
42 | 0 | OPENSSL_cleanse(&ks, sizeof(ks)); |
43 | 0 | DES_set_odd_parity(key); |
44 | 0 | } |
45 | | |
46 | | void DES_string_to_2keys(const char *str, DES_cblock *key1, DES_cblock *key2) |
47 | 0 | { |
48 | 0 | DES_key_schedule ks; |
49 | 0 | int i, length; |
50 | |
|
51 | 0 | memset(key1, 0, 8); |
52 | 0 | memset(key2, 0, 8); |
53 | 0 | length = strlen(str); |
54 | 0 | for (i = 0; i < length; i++) { |
55 | 0 | register unsigned char j = str[i]; |
56 | |
|
57 | 0 | if ((i % 32) < 16) { |
58 | 0 | if ((i % 16) < 8) |
59 | 0 | (*key1)[i % 8] ^= (j << 1); |
60 | 0 | else |
61 | 0 | (*key2)[i % 8] ^= (j << 1); |
62 | 0 | } else { |
63 | 0 | j = ((j << 4) & 0xf0) | ((j >> 4) & 0x0f); |
64 | 0 | j = ((j << 2) & 0xcc) | ((j >> 2) & 0x33); |
65 | 0 | j = ((j << 1) & 0xaa) | ((j >> 1) & 0x55); |
66 | 0 | if ((i % 16) < 8) |
67 | 0 | (*key1)[7 - (i % 8)] ^= j; |
68 | 0 | else |
69 | 0 | (*key2)[7 - (i % 8)] ^= j; |
70 | 0 | } |
71 | 0 | } |
72 | 0 | if (length <= 8) |
73 | 0 | memcpy(key2, key1, 8); |
74 | 0 | DES_set_odd_parity(key1); |
75 | 0 | DES_set_odd_parity(key2); |
76 | 0 | DES_set_key_unchecked(key1, &ks); |
77 | 0 | DES_cbc_cksum((const unsigned char *)str, key1, length, &ks, key1); |
78 | 0 | DES_set_key_unchecked(key2, &ks); |
79 | 0 | DES_cbc_cksum((const unsigned char *)str, key2, length, &ks, key2); |
80 | 0 | OPENSSL_cleanse(&ks, sizeof(ks)); |
81 | 0 | DES_set_odd_parity(key1); |
82 | 0 | DES_set_odd_parity(key2); |
83 | 0 | } |