1*f10e73dfSEric Biggers /* FCrypt encryption algorithm 2*f10e73dfSEric Biggers * 3*f10e73dfSEric Biggers * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved. 4*f10e73dfSEric Biggers * Written by David Howells (dhowells@redhat.com) 5*f10e73dfSEric Biggers * 6*f10e73dfSEric Biggers * This program is free software; you can redistribute it and/or 7*f10e73dfSEric Biggers * modify it under the terms of the GNU General Public License 8*f10e73dfSEric Biggers * as published by the Free Software Foundation; either version 9*f10e73dfSEric Biggers * 2 of the License, or (at your option) any later version. 10*f10e73dfSEric Biggers * 11*f10e73dfSEric Biggers * Based on code: 12*f10e73dfSEric Biggers * 13*f10e73dfSEric Biggers * Copyright (c) 1995 - 2000 Kungliga Tekniska Högskolan 14*f10e73dfSEric Biggers * (Royal Institute of Technology, Stockholm, Sweden). 15*f10e73dfSEric Biggers * All rights reserved. 16*f10e73dfSEric Biggers * 17*f10e73dfSEric Biggers * Redistribution and use in source and binary forms, with or without 18*f10e73dfSEric Biggers * modification, are permitted provided that the following conditions 19*f10e73dfSEric Biggers * are met: 20*f10e73dfSEric Biggers * 21*f10e73dfSEric Biggers * 1. Redistributions of source code must retain the above copyright 22*f10e73dfSEric Biggers * notice, this list of conditions and the following disclaimer. 23*f10e73dfSEric Biggers * 24*f10e73dfSEric Biggers * 2. Redistributions in binary form must reproduce the above copyright 25*f10e73dfSEric Biggers * notice, this list of conditions and the following disclaimer in the 26*f10e73dfSEric Biggers * documentation and/or other materials provided with the distribution. 27*f10e73dfSEric Biggers * 28*f10e73dfSEric Biggers * 3. Neither the name of the Institute nor the names of its contributors 29*f10e73dfSEric Biggers * may be used to endorse or promote products derived from this software 30*f10e73dfSEric Biggers * without specific prior written permission. 31*f10e73dfSEric Biggers * 32*f10e73dfSEric Biggers * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 33*f10e73dfSEric Biggers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 34*f10e73dfSEric Biggers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 35*f10e73dfSEric Biggers * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 36*f10e73dfSEric Biggers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 37*f10e73dfSEric Biggers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 38*f10e73dfSEric Biggers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 39*f10e73dfSEric Biggers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 40*f10e73dfSEric Biggers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 41*f10e73dfSEric Biggers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 42*f10e73dfSEric Biggers * SUCH DAMAGE. 43*f10e73dfSEric Biggers */ 44*f10e73dfSEric Biggers 45*f10e73dfSEric Biggers #include <asm/byteorder.h> 46*f10e73dfSEric Biggers #include <kunit/visibility.h> 47*f10e73dfSEric Biggers #include <linux/export.h> 48*f10e73dfSEric Biggers #include <linux/unaligned.h> 49*f10e73dfSEric Biggers #include "ar-internal.h" 50*f10e73dfSEric Biggers 51*f10e73dfSEric Biggers /* 52*f10e73dfSEric Biggers * Sboxes for Feistel network derived from 53*f10e73dfSEric Biggers * /afs/transarc.com/public/afsps/afs.rel31b.export-src/rxkad/sboxes.h 54*f10e73dfSEric Biggers */ 55*f10e73dfSEric Biggers #undef Z 56*f10e73dfSEric Biggers #define Z(x) cpu_to_be32(x << 3) 57*f10e73dfSEric Biggers static const __be32 sbox0[256] = { 58*f10e73dfSEric Biggers Z(0xea), Z(0x7f), Z(0xb2), Z(0x64), Z(0x9d), Z(0xb0), Z(0xd9), Z(0x11), 59*f10e73dfSEric Biggers Z(0xcd), Z(0x86), Z(0x86), Z(0x91), Z(0x0a), Z(0xb2), Z(0x93), Z(0x06), 60*f10e73dfSEric Biggers Z(0x0e), Z(0x06), Z(0xd2), Z(0x65), Z(0x73), Z(0xc5), Z(0x28), Z(0x60), 61*f10e73dfSEric Biggers Z(0xf2), Z(0x20), Z(0xb5), Z(0x38), Z(0x7e), Z(0xda), Z(0x9f), Z(0xe3), 62*f10e73dfSEric Biggers Z(0xd2), Z(0xcf), Z(0xc4), Z(0x3c), Z(0x61), Z(0xff), Z(0x4a), Z(0x4a), 63*f10e73dfSEric Biggers Z(0x35), Z(0xac), Z(0xaa), Z(0x5f), Z(0x2b), Z(0xbb), Z(0xbc), Z(0x53), 64*f10e73dfSEric Biggers Z(0x4e), Z(0x9d), Z(0x78), Z(0xa3), Z(0xdc), Z(0x09), Z(0x32), Z(0x10), 65*f10e73dfSEric Biggers Z(0xc6), Z(0x6f), Z(0x66), Z(0xd6), Z(0xab), Z(0xa9), Z(0xaf), Z(0xfd), 66*f10e73dfSEric Biggers Z(0x3b), Z(0x95), Z(0xe8), Z(0x34), Z(0x9a), Z(0x81), Z(0x72), Z(0x80), 67*f10e73dfSEric Biggers Z(0x9c), Z(0xf3), Z(0xec), Z(0xda), Z(0x9f), Z(0x26), Z(0x76), Z(0x15), 68*f10e73dfSEric Biggers Z(0x3e), Z(0x55), Z(0x4d), Z(0xde), Z(0x84), Z(0xee), Z(0xad), Z(0xc7), 69*f10e73dfSEric Biggers Z(0xf1), Z(0x6b), Z(0x3d), Z(0xd3), Z(0x04), Z(0x49), Z(0xaa), Z(0x24), 70*f10e73dfSEric Biggers Z(0x0b), Z(0x8a), Z(0x83), Z(0xba), Z(0xfa), Z(0x85), Z(0xa0), Z(0xa8), 71*f10e73dfSEric Biggers Z(0xb1), Z(0xd4), Z(0x01), Z(0xd8), Z(0x70), Z(0x64), Z(0xf0), Z(0x51), 72*f10e73dfSEric Biggers Z(0xd2), Z(0xc3), Z(0xa7), Z(0x75), Z(0x8c), Z(0xa5), Z(0x64), Z(0xef), 73*f10e73dfSEric Biggers Z(0x10), Z(0x4e), Z(0xb7), Z(0xc6), Z(0x61), Z(0x03), Z(0xeb), Z(0x44), 74*f10e73dfSEric Biggers Z(0x3d), Z(0xe5), Z(0xb3), Z(0x5b), Z(0xae), Z(0xd5), Z(0xad), Z(0x1d), 75*f10e73dfSEric Biggers Z(0xfa), Z(0x5a), Z(0x1e), Z(0x33), Z(0xab), Z(0x93), Z(0xa2), Z(0xb7), 76*f10e73dfSEric Biggers Z(0xe7), Z(0xa8), Z(0x45), Z(0xa4), Z(0xcd), Z(0x29), Z(0x63), Z(0x44), 77*f10e73dfSEric Biggers Z(0xb6), Z(0x69), Z(0x7e), Z(0x2e), Z(0x62), Z(0x03), Z(0xc8), Z(0xe0), 78*f10e73dfSEric Biggers Z(0x17), Z(0xbb), Z(0xc7), Z(0xf3), Z(0x3f), Z(0x36), Z(0xba), Z(0x71), 79*f10e73dfSEric Biggers Z(0x8e), Z(0x97), Z(0x65), Z(0x60), Z(0x69), Z(0xb6), Z(0xf6), Z(0xe6), 80*f10e73dfSEric Biggers Z(0x6e), Z(0xe0), Z(0x81), Z(0x59), Z(0xe8), Z(0xaf), Z(0xdd), Z(0x95), 81*f10e73dfSEric Biggers Z(0x22), Z(0x99), Z(0xfd), Z(0x63), Z(0x19), Z(0x74), Z(0x61), Z(0xb1), 82*f10e73dfSEric Biggers Z(0xb6), Z(0x5b), Z(0xae), Z(0x54), Z(0xb3), Z(0x70), Z(0xff), Z(0xc6), 83*f10e73dfSEric Biggers Z(0x3b), Z(0x3e), Z(0xc1), Z(0xd7), Z(0xe1), Z(0x0e), Z(0x76), Z(0xe5), 84*f10e73dfSEric Biggers Z(0x36), Z(0x4f), Z(0x59), Z(0xc7), Z(0x08), Z(0x6e), Z(0x82), Z(0xa6), 85*f10e73dfSEric Biggers Z(0x93), Z(0xc4), Z(0xaa), Z(0x26), Z(0x49), Z(0xe0), Z(0x21), Z(0x64), 86*f10e73dfSEric Biggers Z(0x07), Z(0x9f), Z(0x64), Z(0x81), Z(0x9c), Z(0xbf), Z(0xf9), Z(0xd1), 87*f10e73dfSEric Biggers Z(0x43), Z(0xf8), Z(0xb6), Z(0xb9), Z(0xf1), Z(0x24), Z(0x75), Z(0x03), 88*f10e73dfSEric Biggers Z(0xe4), Z(0xb0), Z(0x99), Z(0x46), Z(0x3d), Z(0xf5), Z(0xd1), Z(0x39), 89*f10e73dfSEric Biggers Z(0x72), Z(0x12), Z(0xf6), Z(0xba), Z(0x0c), Z(0x0d), Z(0x42), Z(0x2e) 90*f10e73dfSEric Biggers }; 91*f10e73dfSEric Biggers 92*f10e73dfSEric Biggers #undef Z 93*f10e73dfSEric Biggers #define Z(x) cpu_to_be32(((x & 0x1f) << 27) | (x >> 5)) 94*f10e73dfSEric Biggers static const __be32 sbox1[256] = { 95*f10e73dfSEric Biggers Z(0x77), Z(0x14), Z(0xa6), Z(0xfe), Z(0xb2), Z(0x5e), Z(0x8c), Z(0x3e), 96*f10e73dfSEric Biggers Z(0x67), Z(0x6c), Z(0xa1), Z(0x0d), Z(0xc2), Z(0xa2), Z(0xc1), Z(0x85), 97*f10e73dfSEric Biggers Z(0x6c), Z(0x7b), Z(0x67), Z(0xc6), Z(0x23), Z(0xe3), Z(0xf2), Z(0x89), 98*f10e73dfSEric Biggers Z(0x50), Z(0x9c), Z(0x03), Z(0xb7), Z(0x73), Z(0xe6), Z(0xe1), Z(0x39), 99*f10e73dfSEric Biggers Z(0x31), Z(0x2c), Z(0x27), Z(0x9f), Z(0xa5), Z(0x69), Z(0x44), Z(0xd6), 100*f10e73dfSEric Biggers Z(0x23), Z(0x83), Z(0x98), Z(0x7d), Z(0x3c), Z(0xb4), Z(0x2d), Z(0x99), 101*f10e73dfSEric Biggers Z(0x1c), Z(0x1f), Z(0x8c), Z(0x20), Z(0x03), Z(0x7c), Z(0x5f), Z(0xad), 102*f10e73dfSEric Biggers Z(0xf4), Z(0xfa), Z(0x95), Z(0xca), Z(0x76), Z(0x44), Z(0xcd), Z(0xb6), 103*f10e73dfSEric Biggers Z(0xb8), Z(0xa1), Z(0xa1), Z(0xbe), Z(0x9e), Z(0x54), Z(0x8f), Z(0x0b), 104*f10e73dfSEric Biggers Z(0x16), Z(0x74), Z(0x31), Z(0x8a), Z(0x23), Z(0x17), Z(0x04), Z(0xfa), 105*f10e73dfSEric Biggers Z(0x79), Z(0x84), Z(0xb1), Z(0xf5), Z(0x13), Z(0xab), Z(0xb5), Z(0x2e), 106*f10e73dfSEric Biggers Z(0xaa), Z(0x0c), Z(0x60), Z(0x6b), Z(0x5b), Z(0xc4), Z(0x4b), Z(0xbc), 107*f10e73dfSEric Biggers Z(0xe2), Z(0xaf), Z(0x45), Z(0x73), Z(0xfa), Z(0xc9), Z(0x49), Z(0xcd), 108*f10e73dfSEric Biggers Z(0x00), Z(0x92), Z(0x7d), Z(0x97), Z(0x7a), Z(0x18), Z(0x60), Z(0x3d), 109*f10e73dfSEric Biggers Z(0xcf), Z(0x5b), Z(0xde), Z(0xc6), Z(0xe2), Z(0xe6), Z(0xbb), Z(0x8b), 110*f10e73dfSEric Biggers Z(0x06), Z(0xda), Z(0x08), Z(0x15), Z(0x1b), Z(0x88), Z(0x6a), Z(0x17), 111*f10e73dfSEric Biggers Z(0x89), Z(0xd0), Z(0xa9), Z(0xc1), Z(0xc9), Z(0x70), Z(0x6b), Z(0xe5), 112*f10e73dfSEric Biggers Z(0x43), Z(0xf4), Z(0x68), Z(0xc8), Z(0xd3), Z(0x84), Z(0x28), Z(0x0a), 113*f10e73dfSEric Biggers Z(0x52), Z(0x66), Z(0xa3), Z(0xca), Z(0xf2), Z(0xe3), Z(0x7f), Z(0x7a), 114*f10e73dfSEric Biggers Z(0x31), Z(0xf7), Z(0x88), Z(0x94), Z(0x5e), Z(0x9c), Z(0x63), Z(0xd5), 115*f10e73dfSEric Biggers Z(0x24), Z(0x66), Z(0xfc), Z(0xb3), Z(0x57), Z(0x25), Z(0xbe), Z(0x89), 116*f10e73dfSEric Biggers Z(0x44), Z(0xc4), Z(0xe0), Z(0x8f), Z(0x23), Z(0x3c), Z(0x12), Z(0x52), 117*f10e73dfSEric Biggers Z(0xf5), Z(0x1e), Z(0xf4), Z(0xcb), Z(0x18), Z(0x33), Z(0x1f), Z(0xf8), 118*f10e73dfSEric Biggers Z(0x69), Z(0x10), Z(0x9d), Z(0xd3), Z(0xf7), Z(0x28), Z(0xf8), Z(0x30), 119*f10e73dfSEric Biggers Z(0x05), Z(0x5e), Z(0x32), Z(0xc0), Z(0xd5), Z(0x19), Z(0xbd), Z(0x45), 120*f10e73dfSEric Biggers Z(0x8b), Z(0x5b), Z(0xfd), Z(0xbc), Z(0xe2), Z(0x5c), Z(0xa9), Z(0x96), 121*f10e73dfSEric Biggers Z(0xef), Z(0x70), Z(0xcf), Z(0xc2), Z(0x2a), Z(0xb3), Z(0x61), Z(0xad), 122*f10e73dfSEric Biggers Z(0x80), Z(0x48), Z(0x81), Z(0xb7), Z(0x1d), Z(0x43), Z(0xd9), Z(0xd7), 123*f10e73dfSEric Biggers Z(0x45), Z(0xf0), Z(0xd8), Z(0x8a), Z(0x59), Z(0x7c), Z(0x57), Z(0xc1), 124*f10e73dfSEric Biggers Z(0x79), Z(0xc7), Z(0x34), Z(0xd6), Z(0x43), Z(0xdf), Z(0xe4), Z(0x78), 125*f10e73dfSEric Biggers Z(0x16), Z(0x06), Z(0xda), Z(0x92), Z(0x76), Z(0x51), Z(0xe1), Z(0xd4), 126*f10e73dfSEric Biggers Z(0x70), Z(0x03), Z(0xe0), Z(0x2f), Z(0x96), Z(0x91), Z(0x82), Z(0x80) 127*f10e73dfSEric Biggers }; 128*f10e73dfSEric Biggers 129*f10e73dfSEric Biggers #undef Z 130*f10e73dfSEric Biggers #define Z(x) cpu_to_be32(x << 11) 131*f10e73dfSEric Biggers static const __be32 sbox2[256] = { 132*f10e73dfSEric Biggers Z(0xf0), Z(0x37), Z(0x24), Z(0x53), Z(0x2a), Z(0x03), Z(0x83), Z(0x86), 133*f10e73dfSEric Biggers Z(0xd1), Z(0xec), Z(0x50), Z(0xf0), Z(0x42), Z(0x78), Z(0x2f), Z(0x6d), 134*f10e73dfSEric Biggers Z(0xbf), Z(0x80), Z(0x87), Z(0x27), Z(0x95), Z(0xe2), Z(0xc5), Z(0x5d), 135*f10e73dfSEric Biggers Z(0xf9), Z(0x6f), Z(0xdb), Z(0xb4), Z(0x65), Z(0x6e), Z(0xe7), Z(0x24), 136*f10e73dfSEric Biggers Z(0xc8), Z(0x1a), Z(0xbb), Z(0x49), Z(0xb5), Z(0x0a), Z(0x7d), Z(0xb9), 137*f10e73dfSEric Biggers Z(0xe8), Z(0xdc), Z(0xb7), Z(0xd9), Z(0x45), Z(0x20), Z(0x1b), Z(0xce), 138*f10e73dfSEric Biggers Z(0x59), Z(0x9d), Z(0x6b), Z(0xbd), Z(0x0e), Z(0x8f), Z(0xa3), Z(0xa9), 139*f10e73dfSEric Biggers Z(0xbc), Z(0x74), Z(0xa6), Z(0xf6), Z(0x7f), Z(0x5f), Z(0xb1), Z(0x68), 140*f10e73dfSEric Biggers Z(0x84), Z(0xbc), Z(0xa9), Z(0xfd), Z(0x55), Z(0x50), Z(0xe9), Z(0xb6), 141*f10e73dfSEric Biggers Z(0x13), Z(0x5e), Z(0x07), Z(0xb8), Z(0x95), Z(0x02), Z(0xc0), Z(0xd0), 142*f10e73dfSEric Biggers Z(0x6a), Z(0x1a), Z(0x85), Z(0xbd), Z(0xb6), Z(0xfd), Z(0xfe), Z(0x17), 143*f10e73dfSEric Biggers Z(0x3f), Z(0x09), Z(0xa3), Z(0x8d), Z(0xfb), Z(0xed), Z(0xda), Z(0x1d), 144*f10e73dfSEric Biggers Z(0x6d), Z(0x1c), Z(0x6c), Z(0x01), Z(0x5a), Z(0xe5), Z(0x71), Z(0x3e), 145*f10e73dfSEric Biggers Z(0x8b), Z(0x6b), Z(0xbe), Z(0x29), Z(0xeb), Z(0x12), Z(0x19), Z(0x34), 146*f10e73dfSEric Biggers Z(0xcd), Z(0xb3), Z(0xbd), Z(0x35), Z(0xea), Z(0x4b), Z(0xd5), Z(0xae), 147*f10e73dfSEric Biggers Z(0x2a), Z(0x79), Z(0x5a), Z(0xa5), Z(0x32), Z(0x12), Z(0x7b), Z(0xdc), 148*f10e73dfSEric Biggers Z(0x2c), Z(0xd0), Z(0x22), Z(0x4b), Z(0xb1), Z(0x85), Z(0x59), Z(0x80), 149*f10e73dfSEric Biggers Z(0xc0), Z(0x30), Z(0x9f), Z(0x73), Z(0xd3), Z(0x14), Z(0x48), Z(0x40), 150*f10e73dfSEric Biggers Z(0x07), Z(0x2d), Z(0x8f), Z(0x80), Z(0x0f), Z(0xce), Z(0x0b), Z(0x5e), 151*f10e73dfSEric Biggers Z(0xb7), Z(0x5e), Z(0xac), Z(0x24), Z(0x94), Z(0x4a), Z(0x18), Z(0x15), 152*f10e73dfSEric Biggers Z(0x05), Z(0xe8), Z(0x02), Z(0x77), Z(0xa9), Z(0xc7), Z(0x40), Z(0x45), 153*f10e73dfSEric Biggers Z(0x89), Z(0xd1), Z(0xea), Z(0xde), Z(0x0c), Z(0x79), Z(0x2a), Z(0x99), 154*f10e73dfSEric Biggers Z(0x6c), Z(0x3e), Z(0x95), Z(0xdd), Z(0x8c), Z(0x7d), Z(0xad), Z(0x6f), 155*f10e73dfSEric Biggers Z(0xdc), Z(0xff), Z(0xfd), Z(0x62), Z(0x47), Z(0xb3), Z(0x21), Z(0x8a), 156*f10e73dfSEric Biggers Z(0xec), Z(0x8e), Z(0x19), Z(0x18), Z(0xb4), Z(0x6e), Z(0x3d), Z(0xfd), 157*f10e73dfSEric Biggers Z(0x74), Z(0x54), Z(0x1e), Z(0x04), Z(0x85), Z(0xd8), Z(0xbc), Z(0x1f), 158*f10e73dfSEric Biggers Z(0x56), Z(0xe7), Z(0x3a), Z(0x56), Z(0x67), Z(0xd6), Z(0xc8), Z(0xa5), 159*f10e73dfSEric Biggers Z(0xf3), Z(0x8e), Z(0xde), Z(0xae), Z(0x37), Z(0x49), Z(0xb7), Z(0xfa), 160*f10e73dfSEric Biggers Z(0xc8), Z(0xf4), Z(0x1f), Z(0xe0), Z(0x2a), Z(0x9b), Z(0x15), Z(0xd1), 161*f10e73dfSEric Biggers Z(0x34), Z(0x0e), Z(0xb5), Z(0xe0), Z(0x44), Z(0x78), Z(0x84), Z(0x59), 162*f10e73dfSEric Biggers Z(0x56), Z(0x68), Z(0x77), Z(0xa5), Z(0x14), Z(0x06), Z(0xf5), Z(0x2f), 163*f10e73dfSEric Biggers Z(0x8c), Z(0x8a), Z(0x73), Z(0x80), Z(0x76), Z(0xb4), Z(0x10), Z(0x86) 164*f10e73dfSEric Biggers }; 165*f10e73dfSEric Biggers 166*f10e73dfSEric Biggers #undef Z 167*f10e73dfSEric Biggers #define Z(x) cpu_to_be32(x << 19) 168*f10e73dfSEric Biggers static const __be32 sbox3[256] = { 169*f10e73dfSEric Biggers Z(0xa9), Z(0x2a), Z(0x48), Z(0x51), Z(0x84), Z(0x7e), Z(0x49), Z(0xe2), 170*f10e73dfSEric Biggers Z(0xb5), Z(0xb7), Z(0x42), Z(0x33), Z(0x7d), Z(0x5d), Z(0xa6), Z(0x12), 171*f10e73dfSEric Biggers Z(0x44), Z(0x48), Z(0x6d), Z(0x28), Z(0xaa), Z(0x20), Z(0x6d), Z(0x57), 172*f10e73dfSEric Biggers Z(0xd6), Z(0x6b), Z(0x5d), Z(0x72), Z(0xf0), Z(0x92), Z(0x5a), Z(0x1b), 173*f10e73dfSEric Biggers Z(0x53), Z(0x80), Z(0x24), Z(0x70), Z(0x9a), Z(0xcc), Z(0xa7), Z(0x66), 174*f10e73dfSEric Biggers Z(0xa1), Z(0x01), Z(0xa5), Z(0x41), Z(0x97), Z(0x41), Z(0x31), Z(0x82), 175*f10e73dfSEric Biggers Z(0xf1), Z(0x14), Z(0xcf), Z(0x53), Z(0x0d), Z(0xa0), Z(0x10), Z(0xcc), 176*f10e73dfSEric Biggers Z(0x2a), Z(0x7d), Z(0xd2), Z(0xbf), Z(0x4b), Z(0x1a), Z(0xdb), Z(0x16), 177*f10e73dfSEric Biggers Z(0x47), Z(0xf6), Z(0x51), Z(0x36), Z(0xed), Z(0xf3), Z(0xb9), Z(0x1a), 178*f10e73dfSEric Biggers Z(0xa7), Z(0xdf), Z(0x29), Z(0x43), Z(0x01), Z(0x54), Z(0x70), Z(0xa4), 179*f10e73dfSEric Biggers Z(0xbf), Z(0xd4), Z(0x0b), Z(0x53), Z(0x44), Z(0x60), Z(0x9e), Z(0x23), 180*f10e73dfSEric Biggers Z(0xa1), Z(0x18), Z(0x68), Z(0x4f), Z(0xf0), Z(0x2f), Z(0x82), Z(0xc2), 181*f10e73dfSEric Biggers Z(0x2a), Z(0x41), Z(0xb2), Z(0x42), Z(0x0c), Z(0xed), Z(0x0c), Z(0x1d), 182*f10e73dfSEric Biggers Z(0x13), Z(0x3a), Z(0x3c), Z(0x6e), Z(0x35), Z(0xdc), Z(0x60), Z(0x65), 183*f10e73dfSEric Biggers Z(0x85), Z(0xe9), Z(0x64), Z(0x02), Z(0x9a), Z(0x3f), Z(0x9f), Z(0x87), 184*f10e73dfSEric Biggers Z(0x96), Z(0xdf), Z(0xbe), Z(0xf2), Z(0xcb), Z(0xe5), Z(0x6c), Z(0xd4), 185*f10e73dfSEric Biggers Z(0x5a), Z(0x83), Z(0xbf), Z(0x92), Z(0x1b), Z(0x94), Z(0x00), Z(0x42), 186*f10e73dfSEric Biggers Z(0xcf), Z(0x4b), Z(0x00), Z(0x75), Z(0xba), Z(0x8f), Z(0x76), Z(0x5f), 187*f10e73dfSEric Biggers Z(0x5d), Z(0x3a), Z(0x4d), Z(0x09), Z(0x12), Z(0x08), Z(0x38), Z(0x95), 188*f10e73dfSEric Biggers Z(0x17), Z(0xe4), Z(0x01), Z(0x1d), Z(0x4c), Z(0xa9), Z(0xcc), Z(0x85), 189*f10e73dfSEric Biggers Z(0x82), Z(0x4c), Z(0x9d), Z(0x2f), Z(0x3b), Z(0x66), Z(0xa1), Z(0x34), 190*f10e73dfSEric Biggers Z(0x10), Z(0xcd), Z(0x59), Z(0x89), Z(0xa5), Z(0x31), Z(0xcf), Z(0x05), 191*f10e73dfSEric Biggers Z(0xc8), Z(0x84), Z(0xfa), Z(0xc7), Z(0xba), Z(0x4e), Z(0x8b), Z(0x1a), 192*f10e73dfSEric Biggers Z(0x19), Z(0xf1), Z(0xa1), Z(0x3b), Z(0x18), Z(0x12), Z(0x17), Z(0xb0), 193*f10e73dfSEric Biggers Z(0x98), Z(0x8d), Z(0x0b), Z(0x23), Z(0xc3), Z(0x3a), Z(0x2d), Z(0x20), 194*f10e73dfSEric Biggers Z(0xdf), Z(0x13), Z(0xa0), Z(0xa8), Z(0x4c), Z(0x0d), Z(0x6c), Z(0x2f), 195*f10e73dfSEric Biggers Z(0x47), Z(0x13), Z(0x13), Z(0x52), Z(0x1f), Z(0x2d), Z(0xf5), Z(0x79), 196*f10e73dfSEric Biggers Z(0x3d), Z(0xa2), Z(0x54), Z(0xbd), Z(0x69), Z(0xc8), Z(0x6b), Z(0xf3), 197*f10e73dfSEric Biggers Z(0x05), Z(0x28), Z(0xf1), Z(0x16), Z(0x46), Z(0x40), Z(0xb0), Z(0x11), 198*f10e73dfSEric Biggers Z(0xd3), Z(0xb7), Z(0x95), Z(0x49), Z(0xcf), Z(0xc3), Z(0x1d), Z(0x8f), 199*f10e73dfSEric Biggers Z(0xd8), Z(0xe1), Z(0x73), Z(0xdb), Z(0xad), Z(0xc8), Z(0xc9), Z(0xa9), 200*f10e73dfSEric Biggers Z(0xa1), Z(0xc2), Z(0xc5), Z(0xe3), Z(0xba), Z(0xfc), Z(0x0e), Z(0x25) 201*f10e73dfSEric Biggers }; 202*f10e73dfSEric Biggers 203*f10e73dfSEric Biggers union fcrypt_block { 204*f10e73dfSEric Biggers __be64 a; 205*f10e73dfSEric Biggers struct { 206*f10e73dfSEric Biggers __be32 l, r; 207*f10e73dfSEric Biggers }; 208*f10e73dfSEric Biggers }; 209*f10e73dfSEric Biggers 210*f10e73dfSEric Biggers #define F_ENCRYPT(R, L, sched) \ 211*f10e73dfSEric Biggers do { \ 212*f10e73dfSEric Biggers union lc4 { \ 213*f10e73dfSEric Biggers __be32 l; \ 214*f10e73dfSEric Biggers u8 c[4]; \ 215*f10e73dfSEric Biggers } u; \ 216*f10e73dfSEric Biggers u.l = sched ^ R; \ 217*f10e73dfSEric Biggers L ^= sbox0[u.c[0]] ^ sbox1[u.c[1]] ^ sbox2[u.c[2]] ^ \ 218*f10e73dfSEric Biggers sbox3[u.c[3]]; \ 219*f10e73dfSEric Biggers } while (0) 220*f10e73dfSEric Biggers 221*f10e73dfSEric Biggers /* Encrypt one block using FCrypt. */ 222*f10e73dfSEric Biggers static __be64 fcrypt_encrypt(const struct fcrypt_key *key, __be64 ptext) 223*f10e73dfSEric Biggers { 224*f10e73dfSEric Biggers union fcrypt_block X = { .a = ptext }; 225*f10e73dfSEric Biggers 226*f10e73dfSEric Biggers /* This is a 16 round Feistel network with permutation F_ENCRYPT. */ 227*f10e73dfSEric Biggers F_ENCRYPT(X.r, X.l, key->sched[0x0]); 228*f10e73dfSEric Biggers F_ENCRYPT(X.l, X.r, key->sched[0x1]); 229*f10e73dfSEric Biggers F_ENCRYPT(X.r, X.l, key->sched[0x2]); 230*f10e73dfSEric Biggers F_ENCRYPT(X.l, X.r, key->sched[0x3]); 231*f10e73dfSEric Biggers F_ENCRYPT(X.r, X.l, key->sched[0x4]); 232*f10e73dfSEric Biggers F_ENCRYPT(X.l, X.r, key->sched[0x5]); 233*f10e73dfSEric Biggers F_ENCRYPT(X.r, X.l, key->sched[0x6]); 234*f10e73dfSEric Biggers F_ENCRYPT(X.l, X.r, key->sched[0x7]); 235*f10e73dfSEric Biggers F_ENCRYPT(X.r, X.l, key->sched[0x8]); 236*f10e73dfSEric Biggers F_ENCRYPT(X.l, X.r, key->sched[0x9]); 237*f10e73dfSEric Biggers F_ENCRYPT(X.r, X.l, key->sched[0xa]); 238*f10e73dfSEric Biggers F_ENCRYPT(X.l, X.r, key->sched[0xb]); 239*f10e73dfSEric Biggers F_ENCRYPT(X.r, X.l, key->sched[0xc]); 240*f10e73dfSEric Biggers F_ENCRYPT(X.l, X.r, key->sched[0xd]); 241*f10e73dfSEric Biggers F_ENCRYPT(X.r, X.l, key->sched[0xe]); 242*f10e73dfSEric Biggers F_ENCRYPT(X.l, X.r, key->sched[0xf]); 243*f10e73dfSEric Biggers return X.a; 244*f10e73dfSEric Biggers } 245*f10e73dfSEric Biggers 246*f10e73dfSEric Biggers /* Decrypt one block using FCrypt. */ 247*f10e73dfSEric Biggers static __be64 fcrypt_decrypt(const struct fcrypt_key *key, __be64 ctext) 248*f10e73dfSEric Biggers { 249*f10e73dfSEric Biggers union fcrypt_block X = { .a = ctext }; 250*f10e73dfSEric Biggers 251*f10e73dfSEric Biggers /* This is a 16 round Feistel network with permutation F_ENCRYPT. */ 252*f10e73dfSEric Biggers F_ENCRYPT(X.l, X.r, key->sched[0xf]); 253*f10e73dfSEric Biggers F_ENCRYPT(X.r, X.l, key->sched[0xe]); 254*f10e73dfSEric Biggers F_ENCRYPT(X.l, X.r, key->sched[0xd]); 255*f10e73dfSEric Biggers F_ENCRYPT(X.r, X.l, key->sched[0xc]); 256*f10e73dfSEric Biggers F_ENCRYPT(X.l, X.r, key->sched[0xb]); 257*f10e73dfSEric Biggers F_ENCRYPT(X.r, X.l, key->sched[0xa]); 258*f10e73dfSEric Biggers F_ENCRYPT(X.l, X.r, key->sched[0x9]); 259*f10e73dfSEric Biggers F_ENCRYPT(X.r, X.l, key->sched[0x8]); 260*f10e73dfSEric Biggers F_ENCRYPT(X.l, X.r, key->sched[0x7]); 261*f10e73dfSEric Biggers F_ENCRYPT(X.r, X.l, key->sched[0x6]); 262*f10e73dfSEric Biggers F_ENCRYPT(X.l, X.r, key->sched[0x5]); 263*f10e73dfSEric Biggers F_ENCRYPT(X.r, X.l, key->sched[0x4]); 264*f10e73dfSEric Biggers F_ENCRYPT(X.l, X.r, key->sched[0x3]); 265*f10e73dfSEric Biggers F_ENCRYPT(X.r, X.l, key->sched[0x2]); 266*f10e73dfSEric Biggers F_ENCRYPT(X.l, X.r, key->sched[0x1]); 267*f10e73dfSEric Biggers F_ENCRYPT(X.r, X.l, key->sched[0x0]); 268*f10e73dfSEric Biggers return X.a; 269*f10e73dfSEric Biggers } 270*f10e73dfSEric Biggers 271*f10e73dfSEric Biggers /** 272*f10e73dfSEric Biggers * fcrypt_preparekey - Prepare a key for FCrypt encryption and decryption 273*f10e73dfSEric Biggers * @key: (out) The prepared key 274*f10e73dfSEric Biggers * @raw_key: The raw key as an 8-byte array 275*f10e73dfSEric Biggers * 276*f10e73dfSEric Biggers * This computes the FCrypt key schedule. 277*f10e73dfSEric Biggers */ 278*f10e73dfSEric Biggers void fcrypt_preparekey(struct fcrypt_key *key, const u8 raw_key[FCRYPT_BSIZE]) 279*f10e73dfSEric Biggers { 280*f10e73dfSEric Biggers u64 k = 0; 281*f10e73dfSEric Biggers 282*f10e73dfSEric Biggers /* Load the 56 non-parity bits of the key. Discard the parity bits. */ 283*f10e73dfSEric Biggers for (int i = 0; i < 8; i++) 284*f10e73dfSEric Biggers k = (k << 7) | (raw_key[i] >> 1); 285*f10e73dfSEric Biggers 286*f10e73dfSEric Biggers /* Generate the key schedule word for each round. */ 287*f10e73dfSEric Biggers for (int i = 0; i < FCRYPT_ROUNDS; i++) { 288*f10e73dfSEric Biggers key->sched[i] = cpu_to_be32(k); 289*f10e73dfSEric Biggers /* Rotate the low 56 bits of 'k' right by 11 bits. */ 290*f10e73dfSEric Biggers k = (k >> 11) | ((k & ((1 << 11) - 1)) << (56 - 11)); 291*f10e73dfSEric Biggers } 292*f10e73dfSEric Biggers } 293*f10e73dfSEric Biggers EXPORT_SYMBOL_IF_KUNIT(fcrypt_preparekey); 294*f10e73dfSEric Biggers 295*f10e73dfSEric Biggers /** 296*f10e73dfSEric Biggers * fcrypt_pcbc_encrypt - Encrypt data using FCrypt cipher in PCBC mode 297*f10e73dfSEric Biggers * @key: The key 298*f10e73dfSEric Biggers * @iv: The 8-byte initialization vector 299*f10e73dfSEric Biggers * @src: The source data 300*f10e73dfSEric Biggers * @dst: The destination data. Both in-place and out-of-place are supported. 301*f10e73dfSEric Biggers * @nblocks: The number of 8-byte blocks to encrypt 302*f10e73dfSEric Biggers * 303*f10e73dfSEric Biggers * WARNING: This cipher is insecure. Not only is the 56-bit key easily 304*f10e73dfSEric Biggers * brute-forced, the cipher itself is cryptographically weak and doesn't even 305*f10e73dfSEric Biggers * provide the intended 56-bit security level. It effectively just acts as an 306*f10e73dfSEric Biggers * obfuscation algorithm. It is supported only for backwards compatibility. 307*f10e73dfSEric Biggers */ 308*f10e73dfSEric Biggers void fcrypt_pcbc_encrypt(const struct fcrypt_key *key, 309*f10e73dfSEric Biggers const u8 iv[FCRYPT_BSIZE], const void *src, void *dst, 310*f10e73dfSEric Biggers size_t nblocks) 311*f10e73dfSEric Biggers { 312*f10e73dfSEric Biggers __be64 prev = get_unaligned((const __be64 *)iv); 313*f10e73dfSEric Biggers const __be64 *src_blocks = src; 314*f10e73dfSEric Biggers __be64 *dst_blocks = dst; 315*f10e73dfSEric Biggers 316*f10e73dfSEric Biggers while (nblocks--) { 317*f10e73dfSEric Biggers __be64 ptext, ctext; 318*f10e73dfSEric Biggers 319*f10e73dfSEric Biggers ptext = get_unaligned(src_blocks++); 320*f10e73dfSEric Biggers ctext = fcrypt_encrypt(key, prev ^ ptext); 321*f10e73dfSEric Biggers put_unaligned(ctext, dst_blocks++); 322*f10e73dfSEric Biggers prev = ptext ^ ctext; 323*f10e73dfSEric Biggers } 324*f10e73dfSEric Biggers } 325*f10e73dfSEric Biggers EXPORT_SYMBOL_IF_KUNIT(fcrypt_pcbc_encrypt); 326*f10e73dfSEric Biggers 327*f10e73dfSEric Biggers /** 328*f10e73dfSEric Biggers * fcrypt_pcbc_decrypt - Decrypt data using FCrypt cipher in PCBC mode 329*f10e73dfSEric Biggers * @key: The key 330*f10e73dfSEric Biggers * @iv: The 8-byte initialization vector 331*f10e73dfSEric Biggers * @src: The source data 332*f10e73dfSEric Biggers * @dst: The destination data. Both in-place and out-of-place are supported. 333*f10e73dfSEric Biggers * @nblocks: The number of 8-byte blocks to decrypt 334*f10e73dfSEric Biggers */ 335*f10e73dfSEric Biggers void fcrypt_pcbc_decrypt(const struct fcrypt_key *key, 336*f10e73dfSEric Biggers const u8 iv[FCRYPT_BSIZE], const void *src, void *dst, 337*f10e73dfSEric Biggers size_t nblocks) 338*f10e73dfSEric Biggers { 339*f10e73dfSEric Biggers __be64 prev = get_unaligned((const __be64 *)iv); 340*f10e73dfSEric Biggers const __be64 *src_blocks = src; 341*f10e73dfSEric Biggers __be64 *dst_blocks = dst; 342*f10e73dfSEric Biggers 343*f10e73dfSEric Biggers while (nblocks--) { 344*f10e73dfSEric Biggers __be64 ptext, ctext; 345*f10e73dfSEric Biggers 346*f10e73dfSEric Biggers ctext = get_unaligned(src_blocks++); 347*f10e73dfSEric Biggers ptext = prev ^ fcrypt_decrypt(key, ctext); 348*f10e73dfSEric Biggers put_unaligned(ptext, dst_blocks++); 349*f10e73dfSEric Biggers prev = ptext ^ ctext; 350*f10e73dfSEric Biggers } 351*f10e73dfSEric Biggers } 352*f10e73dfSEric Biggers EXPORT_SYMBOL_IF_KUNIT(fcrypt_pcbc_decrypt); 353