58 #include <linux/types.h>
64 # define RPCDBG_FACILITY RPCDBG_AUTH
72 static void krb5_nfold(
u32 inbits,
const u8 *
in,
95 lcm = outbits*inbits/
a;
104 for (i = lcm-1; i >= 0; i--) {
112 + (((inbits << 3) + 13) * (i/inbits))
115 + ((inbits - (i % inbits)) << 3)
119 byte += (((in[((inbits - 1) - (msbit >> 3)) % inbits] << 8)|
120 (in[((inbits) - (msbit >> 3)) % inbits]))
121 >> ((msbit & 7) + 1)) & 0xff;
124 byte += out[i % outbits];
125 out[i % outbits] = byte & 0xff;
134 for (i = outbits - 1; i >= 0; i--) {
137 out[
i] = byte & 0xff;
151 const struct xdr_netobj *inkey,
152 struct xdr_netobj *outkey,
153 const struct xdr_netobj *in_constant,
156 size_t blocksize, keybytes, keylength,
n;
157 unsigned char *inblockdata, *outblockdata, *rawkey;
158 struct xdr_netobj inblock, outblock;
166 if ((inkey->len != keylength) || (outkey->len != keylength))
173 if (crypto_blkcipher_setkey(cipher, inkey->data, inkey->len))
179 inblockdata =
kmalloc(blocksize, gfp_mask);
180 if (inblockdata ==
NULL)
181 goto err_free_cipher;
183 outblockdata =
kmalloc(blocksize, gfp_mask);
184 if (outblockdata ==
NULL)
187 rawkey =
kmalloc(keybytes, gfp_mask);
191 inblock.data = (
char *) inblockdata;
192 inblock.len = blocksize;
194 outblock.data = (
char *) outblockdata;
195 outblock.len = blocksize;
199 if (in_constant->len == inblock.len) {
200 memcpy(inblock.data, in_constant->data, inblock.len);
202 krb5_nfold(in_constant->len * 8, in_constant->data,
203 inblock.len * 8, inblock.data);
209 while (n < keybytes) {
211 outblock.data, inblock.len);
213 if ((keybytes - n) <= outblock.len) {
214 memcpy(rawkey + n, outblock.data, (keybytes - n));
218 memcpy(rawkey + n, outblock.data, outblock.len);
219 memcpy(inblock.data, outblock.data, outblock.len);
225 inblock.data = (
char *) rawkey;
226 inblock.len = keybytes;
229 ret = (*(gk5e->
mk_key))(gk5e, &inblock, outkey);
231 dprintk(
"%s: got %d from mk_key function for '%s'\n",
241 memset(rawkey, 0, keybytes);
244 memset(outblockdata, 0, blocksize);
247 memset(inblockdata, 0, blocksize);
250 crypto_free_blkcipher(cipher);
255 #define smask(step) ((1<<step)-1)
256 #define pstep(x, step) (((x)&smask(step))^(((x)>>step)&smask(step)))
257 #define parity_char(x) pstep(pstep(pstep((x), 4), 2), 1)
259 static void mit_des_fixup_key_parity(
u8 key[8])
262 for (i = 0; i < 8; i++) {
272 struct xdr_netobj *randombits,
273 struct xdr_netobj *
key)
278 if (key->len != 24) {
279 dprintk(
"%s: key->len is %d\n", __func__, key->len);
282 if (randombits->len != 21) {
283 dprintk(
"%s: randombits->len is %d\n",
284 __func__, randombits->len);
291 for (i = 0; i < 3; i++) {
292 memcpy(key->data + i*8, randombits->data + i*7, 7);
293 key->data[i*8+7] = (((key->data[i*8]&1)<<1) |
294 ((key->data[i*8+1]&1)<<2) |
295 ((key->data[i*8+2]&1)<<3) |
296 ((key->data[i*8+3]&1)<<4) |
297 ((key->data[i*8+4]&1)<<5) |
298 ((key->data[i*8+5]&1)<<6) |
299 ((key->data[i*8+6]&1)<<7));
301 mit_des_fixup_key_parity(key->data + i*8);
312 struct xdr_netobj *randombits,
313 struct xdr_netobj *
key)
317 if (key->len != 16 && key->len != 32) {
318 dprintk(
"%s: key->len is %d\n", __func__, key->len);
321 if (randombits->len != 16 && randombits->len != 32) {
322 dprintk(
"%s: randombits->len is %d\n",
323 __func__, randombits->len);
326 if (randombits->len != key->len) {
327 dprintk(
"%s: randombits->len is %d, key->len is %d\n",
328 __func__, randombits->len, key->len);
331 memcpy(key->data, randombits->data, key->len);