]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/intel-ipsec-mb/des_key.c
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / spdk / intel-ipsec-mb / des_key.c
1 /*******************************************************************************
2 Copyright (c) 2017-2018, Intel Corporation
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6
7 * Redistributions of source code must retain the above copyright notice,
8 this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of Intel Corporation nor the names of its contributors
13 may be used to endorse or promote products derived from this software
14 without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *******************************************************************************/
27
28 #include <stdlib.h>
29 #include <stdint.h>
30
31 #include "intel-ipsec-mb.h"
32 #include "des.h"
33 #include "des_utils.h"
34
35 /**
36 * @brief Rotates 28-bit word
37 *
38 * Roll right of 28-bit word - used in 28-bit subkey operations
39 *
40 * @param val 28-bit word to be rotated
41 * @param nshift number of bits to rotate by
42 *
43 * @return val rotated by nshift bits
44 */
45 __forceinline
46 uint32_t rotate28(const uint32_t val, const unsigned nshift)
47 {
48 const uint32_t mask = (UINT32_C(1) << 28) - UINT32_C(1);
49
50 IMB_ASSERT(nshift <= 28);
51 return ((val >> nshift) & mask) |
52 ((val << (28 - nshift)) & mask);
53 }
54
55 /**
56 * @brief Expands 8 groups of 6bits into 8 groups of 8bits
57 *
58 * @param in a 48-bit word including 8 groups of 6bits
59 *
60 * @return 64-bit word with 8 groups of 8bits
61 */
62 __forceinline
63 uint64_t expand_8x6_to_8x8(const uint64_t in)
64 {
65 return (((in >> (6 * 0)) & UINT64_C(63)) << (8 * 0)) |
66 (((in >> (6 * 1)) & UINT64_C(63)) << (8 * 1)) |
67 (((in >> (6 * 2)) & UINT64_C(63)) << (8 * 2)) |
68 (((in >> (6 * 3)) & UINT64_C(63)) << (8 * 3)) |
69 (((in >> (6 * 4)) & UINT64_C(63)) << (8 * 4)) |
70 (((in >> (6 * 5)) & UINT64_C(63)) << (8 * 5)) |
71 (((in >> (6 * 6)) & UINT64_C(63)) << (8 * 6)) |
72 (((in >> (6 * 7)) & UINT64_C(63)) << (8 * 7));
73 }
74
75 static const uint8_t pc1c_table_fips46_3[28] = {
76 57, 49, 41, 33, 25, 17, 9,
77 1, 58, 50, 42, 34, 26, 18,
78 10, 2, 59, 51, 43, 35, 27,
79 19, 11, 3, 60, 52, 44, 36
80 };
81
82 static const uint8_t pc1d_table_fips46_3[28] = {
83 63, 55, 47, 39, 31, 23, 15,
84 7, 62, 54, 46, 38, 30, 22,
85 14, 6, 61, 53, 45, 37, 29,
86 21, 13, 5, 28, 20, 12, 4
87 };
88
89 static const uint8_t pc2_table_fips46_3[48] = {
90 14, 17, 11, 24, 1, 5,
91 3, 28, 15, 6, 21, 10,
92 23, 19, 12, 4, 26, 8,
93 16, 7, 27, 20, 13, 2,
94 41, 52, 31, 37, 47, 55,
95 30, 40, 51, 45, 33, 48,
96 44, 49, 39, 56, 34, 53,
97 46, 42, 50, 36, 29, 32
98 };
99
100 static const uint8_t shift_tab_fips46_3[16] = {
101 1, 1, 2, 2, 2, 2, 2, 2,
102 1, 2, 2, 2, 2, 2, 2, 1
103 };
104
105 int des_key_schedule(uint64_t *ks, const void *key)
106 {
107 uint64_t c, d;
108 uint64_t t = 0;
109 int n;
110
111 if (key == NULL || ks == NULL)
112 return -1;
113
114 /* KEY: 56 bits but spread across 64 bits
115 * - MSB per byte used for parity
116 * - load_and_convert loads the key and swaps bits in bytes
117 * so that bit numbers are more suitable for LE machine and
118 * FIPS46-3 DES tables
119 */
120 t = load64_reflect(key);
121
122 /* PC1
123 * - built from the KEY, PC1 permute tables skip KEY parity bits
124 * - c & d are both 28 bits
125 */
126 c = permute_64b(t, pc1c_table_fips46_3, IMB_DIM(pc1c_table_fips46_3));
127 d = permute_64b(t, pc1d_table_fips46_3, IMB_DIM(pc1d_table_fips46_3));
128
129 /* KS rounds */
130 for (n = 0; n < 16; n++) {
131 c = rotate28((uint32_t)c, (unsigned) shift_tab_fips46_3[n]);
132 d = rotate28((uint32_t)d, (unsigned) shift_tab_fips46_3[n]);
133
134 /* PC2 */
135 t = permute_64b(c | (d << 28), pc2_table_fips46_3,
136 IMB_DIM(pc2_table_fips46_3));
137
138 /* store KS as 6 bits per byte and keep LE */
139 ks[n] = expand_8x6_to_8x8(t);
140 }
141
142 return 0;
143 }