2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 br_des_do_IP(uint32_t *xl, uint32_t *xr)
32 * Permutation algorithm is initially from Richard Outerbridge;
33 * implementation here is adapted from Crypto++ "des.cpp" file
34 * (which is in public domain).
40 t = ((l >> 4) ^ r) & (uint32_t)0x0F0F0F0F;
43 t = ((l >> 16) ^ r) & (uint32_t)0x0000FFFF;
46 t = ((r >> 2) ^ l) & (uint32_t)0x33333333;
49 t = ((r >> 8) ^ l) & (uint32_t)0x00FF00FF;
52 t = ((l >> 1) ^ r) & (uint32_t)0x55555555;
61 br_des_do_invIP(uint32_t *xl, uint32_t *xr)
70 t = ((l >> 1) ^ r) & 0x55555555;
73 t = ((r >> 8) ^ l) & 0x00FF00FF;
76 t = ((r >> 2) ^ l) & 0x33333333;
79 t = ((l >> 16) ^ r) & 0x0000FFFF;
82 t = ((l >> 4) ^ r) & 0x0F0F0F0F;
91 br_des_keysched_unit(uint32_t *skey, const void *key)
93 uint32_t xl, xr, kl, kr;
97 xr = br_dec32be((const unsigned char *)key + 4);
100 * Permutation PC-1 is quite similar to the IP permutation.
101 * Definition of IP (in FIPS 46-3 notations) is:
102 * 58 50 42 34 26 18 10 2
103 * 60 52 44 36 28 20 12 4
104 * 62 54 46 38 30 22 14 6
105 * 64 56 48 40 32 24 16 8
106 * 57 49 41 33 25 17 9 1
107 * 59 51 43 35 27 19 11 3
108 * 61 53 45 37 29 21 13 5
109 * 63 55 47 39 31 23 15 7
111 * Definition of PC-1 is:
112 * 57 49 41 33 25 17 9 1
113 * 58 50 42 34 26 18 10 2
114 * 59 51 43 35 27 19 11 3
116 * 63 55 47 39 31 23 15 7
117 * 62 54 46 38 30 22 14 6
118 * 61 53 45 37 29 21 13 5
121 br_des_do_IP(&xl, &xr);
122 kl = ((xr & (uint32_t)0xFF000000) >> 4)
123 | ((xl & (uint32_t)0xFF000000) >> 12)
124 | ((xr & (uint32_t)0x00FF0000) >> 12)
125 | ((xl & (uint32_t)0x00FF0000) >> 20);
126 kr = ((xr & (uint32_t)0x000000FF) << 20)
127 | ((xl & (uint32_t)0x0000FF00) << 4)
128 | ((xr & (uint32_t)0x0000FF00) >> 4)
129 | ((xl & (uint32_t)0x000F0000) >> 16);
132 * For each round, rotate the two 28-bit words kl and kr.
133 * The extraction of the 48-bit subkey (PC-2) is not done yet.
135 for (i = 0; i < 16; i ++) {
136 if ((1 << i) & 0x8103) {
137 kl = (kl << 1) | (kl >> 27);
138 kr = (kr << 1) | (kr >> 27);
140 kl = (kl << 2) | (kl >> 26);
141 kr = (kr << 2) | (kr >> 26);
143 kl &= (uint32_t)0x0FFFFFFF;
144 kr &= (uint32_t)0x0FFFFFFF;
145 skey[(i << 1) + 0] = kl;
146 skey[(i << 1) + 1] = kr;
152 br_des_rev_skey(uint32_t *skey)
156 for (i = 0; i < 16; i += 2) {
160 skey[i + 0] = skey[30 - i];
163 skey[i + 1] = skey[31 - i];