Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Cryptographic API. |
| 4 | * |
| 5 | * Serpent Cipher Algorithm. |
| 6 | * |
| 7 | * Copyright (C) 2002 Dag Arne Osvik <osvik@ii.uib.no> |
| 8 | * 2003 Herbert Valerio Riedel <hvr@gnu.org> |
| 9 | * |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 10 | * Added tnepres support: |
| 11 | * Ruben Jesus Garcia Hernandez <ruben@ugr.es>, 18.10.2004 |
| 12 | * Based on code by hvr |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/errno.h> |
| 18 | #include <asm/byteorder.h> |
| 19 | #include <linux/crypto.h> |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 20 | #include <linux/types.h> |
Jussi Kivilinna | bc83b82 | 2011-10-18 00:03:08 +0300 | [diff] [blame] | 21 | #include <crypto/serpent.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | /* Key is padded to the maximum of 256 bits before round key generation. |
| 24 | * Any key length <= 256 bits (32 bytes) is allowed by the algorithm. |
| 25 | */ |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #define PHI 0x9e3779b9UL |
| 28 | |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 29 | #define keyiter(a, b, c, d, i, j) \ |
| 30 | ({ b ^= d; b ^= c; b ^= a; b ^= PHI ^ i; b = rol32(b, 11); k[j] = b; }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 32 | #define loadkeys(x0, x1, x2, x3, i) \ |
| 33 | ({ x0 = k[i]; x1 = k[i+1]; x2 = k[i+2]; x3 = k[i+3]; }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 35 | #define storekeys(x0, x1, x2, x3, i) \ |
| 36 | ({ k[i] = x0; k[i+1] = x1; k[i+2] = x2; k[i+3] = x3; }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 38 | #define store_and_load_keys(x0, x1, x2, x3, s, l) \ |
| 39 | ({ storekeys(x0, x1, x2, x3, s); loadkeys(x0, x1, x2, x3, l); }) |
| 40 | |
| 41 | #define K(x0, x1, x2, x3, i) ({ \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | x3 ^= k[4*(i)+3]; x2 ^= k[4*(i)+2]; \ |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 43 | x1 ^= k[4*(i)+1]; x0 ^= k[4*(i)+0]; \ |
| 44 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 46 | #define LK(x0, x1, x2, x3, x4, i) ({ \ |
| 47 | x0 = rol32(x0, 13);\ |
| 48 | x2 = rol32(x2, 3); x1 ^= x0; x4 = x0 << 3; \ |
| 49 | x3 ^= x2; x1 ^= x2; \ |
| 50 | x1 = rol32(x1, 1); x3 ^= x4; \ |
| 51 | x3 = rol32(x3, 7); x4 = x1; \ |
| 52 | x0 ^= x1; x4 <<= 7; x2 ^= x3; \ |
| 53 | x0 ^= x3; x2 ^= x4; x3 ^= k[4*i+3]; \ |
| 54 | x1 ^= k[4*i+1]; x0 = rol32(x0, 5); x2 = rol32(x2, 22);\ |
| 55 | x0 ^= k[4*i+0]; x2 ^= k[4*i+2]; \ |
| 56 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 58 | #define KL(x0, x1, x2, x3, x4, i) ({ \ |
| 59 | x0 ^= k[4*i+0]; x1 ^= k[4*i+1]; x2 ^= k[4*i+2]; \ |
| 60 | x3 ^= k[4*i+3]; x0 = ror32(x0, 5); x2 = ror32(x2, 22);\ |
| 61 | x4 = x1; x2 ^= x3; x0 ^= x3; \ |
| 62 | x4 <<= 7; x0 ^= x1; x1 = ror32(x1, 1); \ |
| 63 | x2 ^= x4; x3 = ror32(x3, 7); x4 = x0 << 3; \ |
| 64 | x1 ^= x0; x3 ^= x4; x0 = ror32(x0, 13);\ |
| 65 | x1 ^= x2; x3 ^= x2; x2 = ror32(x2, 3); \ |
| 66 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 68 | #define S0(x0, x1, x2, x3, x4) ({ \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | x4 = x3; \ |
| 70 | x3 |= x0; x0 ^= x4; x4 ^= x2; \ |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 71 | x4 = ~x4; x3 ^= x1; x1 &= x0; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | x1 ^= x4; x2 ^= x0; x0 ^= x3; \ |
| 73 | x4 |= x0; x0 ^= x2; x2 &= x1; \ |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 74 | x3 ^= x2; x1 = ~x1; x2 ^= x4; \ |
| 75 | x1 ^= x2; \ |
| 76 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 78 | #define S1(x0, x1, x2, x3, x4) ({ \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | x4 = x1; \ |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 80 | x1 ^= x0; x0 ^= x3; x3 = ~x3; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | x4 &= x1; x0 |= x1; x3 ^= x2; \ |
| 82 | x0 ^= x3; x1 ^= x3; x3 ^= x4; \ |
| 83 | x1 |= x4; x4 ^= x2; x2 &= x0; \ |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 84 | x2 ^= x1; x1 |= x0; x0 = ~x0; \ |
| 85 | x0 ^= x2; x4 ^= x1; \ |
| 86 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 88 | #define S2(x0, x1, x2, x3, x4) ({ \ |
| 89 | x3 = ~x3; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | x1 ^= x0; x4 = x0; x0 &= x2; \ |
| 91 | x0 ^= x3; x3 |= x4; x2 ^= x1; \ |
| 92 | x3 ^= x1; x1 &= x0; x0 ^= x2; \ |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 93 | x2 &= x3; x3 |= x1; x0 = ~x0; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | x3 ^= x0; x4 ^= x0; x0 ^= x2; \ |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 95 | x1 |= x2; \ |
| 96 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 98 | #define S3(x0, x1, x2, x3, x4) ({ \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | x4 = x1; \ |
| 100 | x1 ^= x3; x3 |= x0; x4 &= x0; \ |
| 101 | x0 ^= x2; x2 ^= x1; x1 &= x3; \ |
| 102 | x2 ^= x3; x0 |= x4; x4 ^= x3; \ |
| 103 | x1 ^= x0; x0 &= x3; x3 &= x4; \ |
| 104 | x3 ^= x2; x4 |= x1; x2 &= x1; \ |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 105 | x4 ^= x3; x0 ^= x3; x3 ^= x2; \ |
| 106 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 108 | #define S4(x0, x1, x2, x3, x4) ({ \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | x4 = x3; \ |
| 110 | x3 &= x0; x0 ^= x4; \ |
| 111 | x3 ^= x2; x2 |= x4; x0 ^= x1; \ |
| 112 | x4 ^= x3; x2 |= x0; \ |
| 113 | x2 ^= x1; x1 &= x0; \ |
| 114 | x1 ^= x4; x4 &= x2; x2 ^= x3; \ |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 115 | x4 ^= x0; x3 |= x1; x1 = ~x1; \ |
| 116 | x3 ^= x0; \ |
| 117 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 119 | #define S5(x0, x1, x2, x3, x4) ({ \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | x4 = x1; x1 |= x0; \ |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 121 | x2 ^= x1; x3 = ~x3; x4 ^= x0; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | x0 ^= x2; x1 &= x4; x4 |= x3; \ |
| 123 | x4 ^= x0; x0 &= x3; x1 ^= x3; \ |
| 124 | x3 ^= x2; x0 ^= x1; x2 &= x4; \ |
| 125 | x1 ^= x2; x2 &= x0; \ |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 126 | x3 ^= x2; \ |
| 127 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 129 | #define S6(x0, x1, x2, x3, x4) ({ \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | x4 = x1; \ |
| 131 | x3 ^= x0; x1 ^= x2; x2 ^= x0; \ |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 132 | x0 &= x3; x1 |= x3; x4 = ~x4; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | x0 ^= x1; x1 ^= x2; \ |
| 134 | x3 ^= x4; x4 ^= x0; x2 &= x0; \ |
| 135 | x4 ^= x1; x2 ^= x3; x3 &= x1; \ |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 136 | x3 ^= x0; x1 ^= x2; \ |
| 137 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 139 | #define S7(x0, x1, x2, x3, x4) ({ \ |
| 140 | x1 = ~x1; \ |
| 141 | x4 = x1; x0 = ~x0; x1 &= x2; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | x1 ^= x3; x3 |= x4; x4 ^= x2; \ |
| 143 | x2 ^= x3; x3 ^= x0; x0 |= x1; \ |
| 144 | x2 &= x0; x0 ^= x4; x4 ^= x3; \ |
| 145 | x3 &= x0; x4 ^= x1; \ |
| 146 | x2 ^= x4; x3 ^= x1; x4 |= x0; \ |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 147 | x4 ^= x1; \ |
| 148 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 150 | #define SI0(x0, x1, x2, x3, x4) ({ \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | x4 = x3; x1 ^= x0; \ |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 152 | x3 |= x1; x4 ^= x1; x0 = ~x0; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | x2 ^= x3; x3 ^= x0; x0 &= x1; \ |
| 154 | x0 ^= x2; x2 &= x3; x3 ^= x4; \ |
| 155 | x2 ^= x3; x1 ^= x3; x3 &= x0; \ |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 156 | x1 ^= x0; x0 ^= x2; x4 ^= x3; \ |
| 157 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 159 | #define SI1(x0, x1, x2, x3, x4) ({ \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | x1 ^= x3; x4 = x0; \ |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 161 | x0 ^= x2; x2 = ~x2; x4 |= x1; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | x4 ^= x3; x3 &= x1; x1 ^= x2; \ |
| 163 | x2 &= x4; x4 ^= x1; x1 |= x3; \ |
| 164 | x3 ^= x0; x2 ^= x0; x0 |= x4; \ |
| 165 | x2 ^= x4; x1 ^= x0; \ |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 166 | x4 ^= x1; \ |
| 167 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 169 | #define SI2(x0, x1, x2, x3, x4) ({ \ |
| 170 | x2 ^= x1; x4 = x3; x3 = ~x3; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | x3 |= x2; x2 ^= x4; x4 ^= x0; \ |
| 172 | x3 ^= x1; x1 |= x2; x2 ^= x0; \ |
| 173 | x1 ^= x4; x4 |= x3; x2 ^= x3; \ |
| 174 | x4 ^= x2; x2 &= x1; \ |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 175 | x2 ^= x3; x3 ^= x4; x4 ^= x0; \ |
| 176 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 178 | #define SI3(x0, x1, x2, x3, x4) ({ \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | x2 ^= x1; \ |
| 180 | x4 = x1; x1 &= x2; \ |
| 181 | x1 ^= x0; x0 |= x4; x4 ^= x3; \ |
| 182 | x0 ^= x3; x3 |= x1; x1 ^= x2; \ |
| 183 | x1 ^= x3; x0 ^= x2; x2 ^= x3; \ |
| 184 | x3 &= x1; x1 ^= x0; x0 &= x2; \ |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 185 | x4 ^= x3; x3 ^= x0; x0 ^= x1; \ |
| 186 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 188 | #define SI4(x0, x1, x2, x3, x4) ({ \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | x2 ^= x3; x4 = x0; x0 &= x1; \ |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 190 | x0 ^= x2; x2 |= x3; x4 = ~x4; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | x1 ^= x0; x0 ^= x2; x2 &= x4; \ |
| 192 | x2 ^= x0; x0 |= x4; \ |
| 193 | x0 ^= x3; x3 &= x2; \ |
| 194 | x4 ^= x3; x3 ^= x1; x1 &= x0; \ |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 195 | x4 ^= x1; x0 ^= x3; \ |
| 196 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 198 | #define SI5(x0, x1, x2, x3, x4) ({ \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | x4 = x1; x1 |= x2; \ |
| 200 | x2 ^= x4; x1 ^= x3; x3 &= x4; \ |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 201 | x2 ^= x3; x3 |= x0; x0 = ~x0; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | x3 ^= x2; x2 |= x0; x4 ^= x1; \ |
| 203 | x2 ^= x4; x4 &= x0; x0 ^= x1; \ |
| 204 | x1 ^= x3; x0 &= x2; x2 ^= x3; \ |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 205 | x0 ^= x2; x2 ^= x4; x4 ^= x3; \ |
| 206 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 208 | #define SI6(x0, x1, x2, x3, x4) ({ \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | x0 ^= x2; \ |
| 210 | x4 = x0; x0 &= x3; x2 ^= x3; \ |
| 211 | x0 ^= x2; x3 ^= x1; x2 |= x4; \ |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 212 | x2 ^= x3; x3 &= x0; x0 = ~x0; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | x3 ^= x1; x1 &= x2; x4 ^= x0; \ |
| 214 | x3 ^= x4; x4 ^= x2; x0 ^= x1; \ |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 215 | x2 ^= x0; \ |
| 216 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 218 | #define SI7(x0, x1, x2, x3, x4) ({ \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | x4 = x3; x3 &= x0; x0 ^= x2; \ |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 220 | x2 |= x4; x4 ^= x1; x0 = ~x0; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | x1 |= x3; x4 ^= x0; x0 &= x2; \ |
| 222 | x0 ^= x1; x1 &= x2; x3 ^= x2; \ |
| 223 | x4 ^= x3; x2 &= x3; x3 |= x0; \ |
| 224 | x1 ^= x4; x3 ^= x4; x4 &= x0; \ |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 225 | x4 ^= x2; \ |
| 226 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | |
Arnd Bergmann | 4739711 | 2019-06-18 13:19:42 +0200 | [diff] [blame] | 228 | /* |
| 229 | * both gcc and clang have misoptimized this function in the past, |
| 230 | * producing horrible object code from spilling temporary variables |
| 231 | * on the stack. Forcing this part out of line avoids that. |
| 232 | */ |
| 233 | static noinline void __serpent_setkey_sbox(u32 r0, u32 r1, u32 r2, |
| 234 | u32 r3, u32 r4, u32 *k) |
Arnd Bergmann | c871c10 | 2017-07-31 22:43:55 +0200 | [diff] [blame] | 235 | { |
| 236 | k += 100; |
| 237 | S3(r3, r4, r0, r1, r2); store_and_load_keys(r1, r2, r4, r3, 28, 24); |
| 238 | S4(r1, r2, r4, r3, r0); store_and_load_keys(r2, r4, r3, r0, 24, 20); |
| 239 | S5(r2, r4, r3, r0, r1); store_and_load_keys(r1, r2, r4, r0, 20, 16); |
| 240 | S6(r1, r2, r4, r0, r3); store_and_load_keys(r4, r3, r2, r0, 16, 12); |
| 241 | S7(r4, r3, r2, r0, r1); store_and_load_keys(r1, r2, r0, r4, 12, 8); |
| 242 | S0(r1, r2, r0, r4, r3); store_and_load_keys(r0, r2, r4, r1, 8, 4); |
| 243 | S1(r0, r2, r4, r1, r3); store_and_load_keys(r3, r4, r1, r0, 4, 0); |
| 244 | S2(r3, r4, r1, r0, r2); store_and_load_keys(r2, r4, r3, r0, 0, -4); |
| 245 | S3(r2, r4, r3, r0, r1); store_and_load_keys(r0, r1, r4, r2, -4, -8); |
| 246 | S4(r0, r1, r4, r2, r3); store_and_load_keys(r1, r4, r2, r3, -8, -12); |
| 247 | S5(r1, r4, r2, r3, r0); store_and_load_keys(r0, r1, r4, r3, -12, -16); |
| 248 | S6(r0, r1, r4, r3, r2); store_and_load_keys(r4, r2, r1, r3, -16, -20); |
| 249 | S7(r4, r2, r1, r3, r0); store_and_load_keys(r0, r1, r3, r4, -20, -24); |
| 250 | S0(r0, r1, r3, r4, r2); store_and_load_keys(r3, r1, r4, r0, -24, -28); |
| 251 | k -= 50; |
| 252 | S1(r3, r1, r4, r0, r2); store_and_load_keys(r2, r4, r0, r3, 22, 18); |
| 253 | S2(r2, r4, r0, r3, r1); store_and_load_keys(r1, r4, r2, r3, 18, 14); |
| 254 | S3(r1, r4, r2, r3, r0); store_and_load_keys(r3, r0, r4, r1, 14, 10); |
| 255 | S4(r3, r0, r4, r1, r2); store_and_load_keys(r0, r4, r1, r2, 10, 6); |
| 256 | S5(r0, r4, r1, r2, r3); store_and_load_keys(r3, r0, r4, r2, 6, 2); |
| 257 | S6(r3, r0, r4, r2, r1); store_and_load_keys(r4, r1, r0, r2, 2, -2); |
| 258 | S7(r4, r1, r0, r2, r3); store_and_load_keys(r3, r0, r2, r4, -2, -6); |
| 259 | S0(r3, r0, r2, r4, r1); store_and_load_keys(r2, r0, r4, r3, -6, -10); |
| 260 | S1(r2, r0, r4, r3, r1); store_and_load_keys(r1, r4, r3, r2, -10, -14); |
| 261 | S2(r1, r4, r3, r2, r0); store_and_load_keys(r0, r4, r1, r2, -14, -18); |
| 262 | S3(r0, r4, r1, r2, r3); store_and_load_keys(r2, r3, r4, r0, -18, -22); |
| 263 | k -= 50; |
| 264 | S4(r2, r3, r4, r0, r1); store_and_load_keys(r3, r4, r0, r1, 28, 24); |
| 265 | S5(r3, r4, r0, r1, r2); store_and_load_keys(r2, r3, r4, r1, 24, 20); |
| 266 | S6(r2, r3, r4, r1, r0); store_and_load_keys(r4, r0, r3, r1, 20, 16); |
| 267 | S7(r4, r0, r3, r1, r2); store_and_load_keys(r2, r3, r1, r4, 16, 12); |
| 268 | S0(r2, r3, r1, r4, r0); store_and_load_keys(r1, r3, r4, r2, 12, 8); |
| 269 | S1(r1, r3, r4, r2, r0); store_and_load_keys(r0, r4, r2, r1, 8, 4); |
| 270 | S2(r0, r4, r2, r1, r3); store_and_load_keys(r3, r4, r0, r1, 4, 0); |
| 271 | S3(r3, r4, r0, r1, r2); storekeys(r1, r2, r4, r3, 0); |
| 272 | } |
| 273 | |
Jussi Kivilinna | 1848205 | 2011-11-09 16:26:36 +0200 | [diff] [blame] | 274 | int __serpent_setkey(struct serpent_ctx *ctx, const u8 *key, |
| 275 | unsigned int keylen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 277 | u32 *k = ctx->expkey; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | u8 *k8 = (u8 *)k; |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 279 | u32 r0, r1, r2, r3, r4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | int i; |
| 281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | /* Copy key, add padding */ |
| 283 | |
| 284 | for (i = 0; i < keylen; ++i) |
| 285 | k8[i] = key[i]; |
| 286 | if (i < SERPENT_MAX_KEY_SIZE) |
| 287 | k8[i++] = 1; |
| 288 | while (i < SERPENT_MAX_KEY_SIZE) |
| 289 | k8[i++] = 0; |
| 290 | |
| 291 | /* Expand key using polynomial */ |
| 292 | |
| 293 | r0 = le32_to_cpu(k[3]); |
| 294 | r1 = le32_to_cpu(k[4]); |
| 295 | r2 = le32_to_cpu(k[5]); |
| 296 | r3 = le32_to_cpu(k[6]); |
| 297 | r4 = le32_to_cpu(k[7]); |
| 298 | |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 299 | keyiter(le32_to_cpu(k[0]), r0, r4, r2, 0, 0); |
| 300 | keyiter(le32_to_cpu(k[1]), r1, r0, r3, 1, 1); |
| 301 | keyiter(le32_to_cpu(k[2]), r2, r1, r4, 2, 2); |
| 302 | keyiter(le32_to_cpu(k[3]), r3, r2, r0, 3, 3); |
| 303 | keyiter(le32_to_cpu(k[4]), r4, r3, r1, 4, 4); |
| 304 | keyiter(le32_to_cpu(k[5]), r0, r4, r2, 5, 5); |
| 305 | keyiter(le32_to_cpu(k[6]), r1, r0, r3, 6, 6); |
| 306 | keyiter(le32_to_cpu(k[7]), r2, r1, r4, 7, 7); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 308 | keyiter(k[0], r3, r2, r0, 8, 8); |
| 309 | keyiter(k[1], r4, r3, r1, 9, 9); |
| 310 | keyiter(k[2], r0, r4, r2, 10, 10); |
| 311 | keyiter(k[3], r1, r0, r3, 11, 11); |
| 312 | keyiter(k[4], r2, r1, r4, 12, 12); |
| 313 | keyiter(k[5], r3, r2, r0, 13, 13); |
| 314 | keyiter(k[6], r4, r3, r1, 14, 14); |
| 315 | keyiter(k[7], r0, r4, r2, 15, 15); |
| 316 | keyiter(k[8], r1, r0, r3, 16, 16); |
| 317 | keyiter(k[9], r2, r1, r4, 17, 17); |
| 318 | keyiter(k[10], r3, r2, r0, 18, 18); |
| 319 | keyiter(k[11], r4, r3, r1, 19, 19); |
| 320 | keyiter(k[12], r0, r4, r2, 20, 20); |
| 321 | keyiter(k[13], r1, r0, r3, 21, 21); |
| 322 | keyiter(k[14], r2, r1, r4, 22, 22); |
| 323 | keyiter(k[15], r3, r2, r0, 23, 23); |
| 324 | keyiter(k[16], r4, r3, r1, 24, 24); |
| 325 | keyiter(k[17], r0, r4, r2, 25, 25); |
| 326 | keyiter(k[18], r1, r0, r3, 26, 26); |
| 327 | keyiter(k[19], r2, r1, r4, 27, 27); |
| 328 | keyiter(k[20], r3, r2, r0, 28, 28); |
| 329 | keyiter(k[21], r4, r3, r1, 29, 29); |
| 330 | keyiter(k[22], r0, r4, r2, 30, 30); |
| 331 | keyiter(k[23], r1, r0, r3, 31, 31); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
| 333 | k += 50; |
| 334 | |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 335 | keyiter(k[-26], r2, r1, r4, 32, -18); |
| 336 | keyiter(k[-25], r3, r2, r0, 33, -17); |
| 337 | keyiter(k[-24], r4, r3, r1, 34, -16); |
| 338 | keyiter(k[-23], r0, r4, r2, 35, -15); |
| 339 | keyiter(k[-22], r1, r0, r3, 36, -14); |
| 340 | keyiter(k[-21], r2, r1, r4, 37, -13); |
| 341 | keyiter(k[-20], r3, r2, r0, 38, -12); |
| 342 | keyiter(k[-19], r4, r3, r1, 39, -11); |
| 343 | keyiter(k[-18], r0, r4, r2, 40, -10); |
| 344 | keyiter(k[-17], r1, r0, r3, 41, -9); |
| 345 | keyiter(k[-16], r2, r1, r4, 42, -8); |
| 346 | keyiter(k[-15], r3, r2, r0, 43, -7); |
| 347 | keyiter(k[-14], r4, r3, r1, 44, -6); |
| 348 | keyiter(k[-13], r0, r4, r2, 45, -5); |
| 349 | keyiter(k[-12], r1, r0, r3, 46, -4); |
| 350 | keyiter(k[-11], r2, r1, r4, 47, -3); |
| 351 | keyiter(k[-10], r3, r2, r0, 48, -2); |
| 352 | keyiter(k[-9], r4, r3, r1, 49, -1); |
| 353 | keyiter(k[-8], r0, r4, r2, 50, 0); |
| 354 | keyiter(k[-7], r1, r0, r3, 51, 1); |
| 355 | keyiter(k[-6], r2, r1, r4, 52, 2); |
| 356 | keyiter(k[-5], r3, r2, r0, 53, 3); |
| 357 | keyiter(k[-4], r4, r3, r1, 54, 4); |
| 358 | keyiter(k[-3], r0, r4, r2, 55, 5); |
| 359 | keyiter(k[-2], r1, r0, r3, 56, 6); |
| 360 | keyiter(k[-1], r2, r1, r4, 57, 7); |
| 361 | keyiter(k[0], r3, r2, r0, 58, 8); |
| 362 | keyiter(k[1], r4, r3, r1, 59, 9); |
| 363 | keyiter(k[2], r0, r4, r2, 60, 10); |
| 364 | keyiter(k[3], r1, r0, r3, 61, 11); |
| 365 | keyiter(k[4], r2, r1, r4, 62, 12); |
| 366 | keyiter(k[5], r3, r2, r0, 63, 13); |
| 367 | keyiter(k[6], r4, r3, r1, 64, 14); |
| 368 | keyiter(k[7], r0, r4, r2, 65, 15); |
| 369 | keyiter(k[8], r1, r0, r3, 66, 16); |
| 370 | keyiter(k[9], r2, r1, r4, 67, 17); |
| 371 | keyiter(k[10], r3, r2, r0, 68, 18); |
| 372 | keyiter(k[11], r4, r3, r1, 69, 19); |
| 373 | keyiter(k[12], r0, r4, r2, 70, 20); |
| 374 | keyiter(k[13], r1, r0, r3, 71, 21); |
| 375 | keyiter(k[14], r2, r1, r4, 72, 22); |
| 376 | keyiter(k[15], r3, r2, r0, 73, 23); |
| 377 | keyiter(k[16], r4, r3, r1, 74, 24); |
| 378 | keyiter(k[17], r0, r4, r2, 75, 25); |
| 379 | keyiter(k[18], r1, r0, r3, 76, 26); |
| 380 | keyiter(k[19], r2, r1, r4, 77, 27); |
| 381 | keyiter(k[20], r3, r2, r0, 78, 28); |
| 382 | keyiter(k[21], r4, r3, r1, 79, 29); |
| 383 | keyiter(k[22], r0, r4, r2, 80, 30); |
| 384 | keyiter(k[23], r1, r0, r3, 81, 31); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | |
| 386 | k += 50; |
| 387 | |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 388 | keyiter(k[-26], r2, r1, r4, 82, -18); |
| 389 | keyiter(k[-25], r3, r2, r0, 83, -17); |
| 390 | keyiter(k[-24], r4, r3, r1, 84, -16); |
| 391 | keyiter(k[-23], r0, r4, r2, 85, -15); |
| 392 | keyiter(k[-22], r1, r0, r3, 86, -14); |
| 393 | keyiter(k[-21], r2, r1, r4, 87, -13); |
| 394 | keyiter(k[-20], r3, r2, r0, 88, -12); |
| 395 | keyiter(k[-19], r4, r3, r1, 89, -11); |
| 396 | keyiter(k[-18], r0, r4, r2, 90, -10); |
| 397 | keyiter(k[-17], r1, r0, r3, 91, -9); |
| 398 | keyiter(k[-16], r2, r1, r4, 92, -8); |
| 399 | keyiter(k[-15], r3, r2, r0, 93, -7); |
| 400 | keyiter(k[-14], r4, r3, r1, 94, -6); |
| 401 | keyiter(k[-13], r0, r4, r2, 95, -5); |
| 402 | keyiter(k[-12], r1, r0, r3, 96, -4); |
| 403 | keyiter(k[-11], r2, r1, r4, 97, -3); |
| 404 | keyiter(k[-10], r3, r2, r0, 98, -2); |
| 405 | keyiter(k[-9], r4, r3, r1, 99, -1); |
| 406 | keyiter(k[-8], r0, r4, r2, 100, 0); |
| 407 | keyiter(k[-7], r1, r0, r3, 101, 1); |
| 408 | keyiter(k[-6], r2, r1, r4, 102, 2); |
| 409 | keyiter(k[-5], r3, r2, r0, 103, 3); |
| 410 | keyiter(k[-4], r4, r3, r1, 104, 4); |
| 411 | keyiter(k[-3], r0, r4, r2, 105, 5); |
| 412 | keyiter(k[-2], r1, r0, r3, 106, 6); |
| 413 | keyiter(k[-1], r2, r1, r4, 107, 7); |
| 414 | keyiter(k[0], r3, r2, r0, 108, 8); |
| 415 | keyiter(k[1], r4, r3, r1, 109, 9); |
| 416 | keyiter(k[2], r0, r4, r2, 110, 10); |
| 417 | keyiter(k[3], r1, r0, r3, 111, 11); |
| 418 | keyiter(k[4], r2, r1, r4, 112, 12); |
| 419 | keyiter(k[5], r3, r2, r0, 113, 13); |
| 420 | keyiter(k[6], r4, r3, r1, 114, 14); |
| 421 | keyiter(k[7], r0, r4, r2, 115, 15); |
| 422 | keyiter(k[8], r1, r0, r3, 116, 16); |
| 423 | keyiter(k[9], r2, r1, r4, 117, 17); |
| 424 | keyiter(k[10], r3, r2, r0, 118, 18); |
| 425 | keyiter(k[11], r4, r3, r1, 119, 19); |
| 426 | keyiter(k[12], r0, r4, r2, 120, 20); |
| 427 | keyiter(k[13], r1, r0, r3, 121, 21); |
| 428 | keyiter(k[14], r2, r1, r4, 122, 22); |
| 429 | keyiter(k[15], r3, r2, r0, 123, 23); |
| 430 | keyiter(k[16], r4, r3, r1, 124, 24); |
| 431 | keyiter(k[17], r0, r4, r2, 125, 25); |
| 432 | keyiter(k[18], r1, r0, r3, 126, 26); |
| 433 | keyiter(k[19], r2, r1, r4, 127, 27); |
| 434 | keyiter(k[20], r3, r2, r0, 128, 28); |
| 435 | keyiter(k[21], r4, r3, r1, 129, 29); |
| 436 | keyiter(k[22], r0, r4, r2, 130, 30); |
| 437 | keyiter(k[23], r1, r0, r3, 131, 31); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | |
| 439 | /* Apply S-boxes */ |
Arnd Bergmann | c871c10 | 2017-07-31 22:43:55 +0200 | [diff] [blame] | 440 | __serpent_setkey_sbox(r0, r1, r2, r3, r4, ctx->expkey); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
| 442 | return 0; |
| 443 | } |
Jussi Kivilinna | 1848205 | 2011-11-09 16:26:36 +0200 | [diff] [blame] | 444 | EXPORT_SYMBOL_GPL(__serpent_setkey); |
| 445 | |
| 446 | int serpent_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen) |
| 447 | { |
| 448 | return __serpent_setkey(crypto_tfm_ctx(tfm), key, keylen); |
| 449 | } |
Jussi Kivilinna | bc83b82 | 2011-10-18 00:03:08 +0300 | [diff] [blame] | 450 | EXPORT_SYMBOL_GPL(serpent_setkey); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
Jussi Kivilinna | bc83b82 | 2011-10-18 00:03:08 +0300 | [diff] [blame] | 452 | void __serpent_encrypt(struct serpent_ctx *ctx, u8 *dst, const u8 *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | { |
Jussi Kivilinna | bc83b82 | 2011-10-18 00:03:08 +0300 | [diff] [blame] | 454 | const u32 *k = ctx->expkey; |
Alexey Dobriyan | d08f74e | 2005-12-23 01:12:25 +0300 | [diff] [blame] | 455 | const __le32 *s = (const __le32 *)src; |
| 456 | __le32 *d = (__le32 *)dst; |
| 457 | u32 r0, r1, r2, r3, r4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | |
| 459 | /* |
| 460 | * Note: The conversions between u8* and u32* might cause trouble |
| 461 | * on architectures with stricter alignment rules than x86 |
| 462 | */ |
| 463 | |
| 464 | r0 = le32_to_cpu(s[0]); |
| 465 | r1 = le32_to_cpu(s[1]); |
| 466 | r2 = le32_to_cpu(s[2]); |
| 467 | r3 = le32_to_cpu(s[3]); |
| 468 | |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 469 | K(r0, r1, r2, r3, 0); |
| 470 | S0(r0, r1, r2, r3, r4); LK(r2, r1, r3, r0, r4, 1); |
| 471 | S1(r2, r1, r3, r0, r4); LK(r4, r3, r0, r2, r1, 2); |
| 472 | S2(r4, r3, r0, r2, r1); LK(r1, r3, r4, r2, r0, 3); |
| 473 | S3(r1, r3, r4, r2, r0); LK(r2, r0, r3, r1, r4, 4); |
| 474 | S4(r2, r0, r3, r1, r4); LK(r0, r3, r1, r4, r2, 5); |
| 475 | S5(r0, r3, r1, r4, r2); LK(r2, r0, r3, r4, r1, 6); |
| 476 | S6(r2, r0, r3, r4, r1); LK(r3, r1, r0, r4, r2, 7); |
| 477 | S7(r3, r1, r0, r4, r2); LK(r2, r0, r4, r3, r1, 8); |
| 478 | S0(r2, r0, r4, r3, r1); LK(r4, r0, r3, r2, r1, 9); |
| 479 | S1(r4, r0, r3, r2, r1); LK(r1, r3, r2, r4, r0, 10); |
| 480 | S2(r1, r3, r2, r4, r0); LK(r0, r3, r1, r4, r2, 11); |
| 481 | S3(r0, r3, r1, r4, r2); LK(r4, r2, r3, r0, r1, 12); |
| 482 | S4(r4, r2, r3, r0, r1); LK(r2, r3, r0, r1, r4, 13); |
| 483 | S5(r2, r3, r0, r1, r4); LK(r4, r2, r3, r1, r0, 14); |
| 484 | S6(r4, r2, r3, r1, r0); LK(r3, r0, r2, r1, r4, 15); |
| 485 | S7(r3, r0, r2, r1, r4); LK(r4, r2, r1, r3, r0, 16); |
| 486 | S0(r4, r2, r1, r3, r0); LK(r1, r2, r3, r4, r0, 17); |
| 487 | S1(r1, r2, r3, r4, r0); LK(r0, r3, r4, r1, r2, 18); |
| 488 | S2(r0, r3, r4, r1, r2); LK(r2, r3, r0, r1, r4, 19); |
| 489 | S3(r2, r3, r0, r1, r4); LK(r1, r4, r3, r2, r0, 20); |
| 490 | S4(r1, r4, r3, r2, r0); LK(r4, r3, r2, r0, r1, 21); |
| 491 | S5(r4, r3, r2, r0, r1); LK(r1, r4, r3, r0, r2, 22); |
| 492 | S6(r1, r4, r3, r0, r2); LK(r3, r2, r4, r0, r1, 23); |
| 493 | S7(r3, r2, r4, r0, r1); LK(r1, r4, r0, r3, r2, 24); |
| 494 | S0(r1, r4, r0, r3, r2); LK(r0, r4, r3, r1, r2, 25); |
| 495 | S1(r0, r4, r3, r1, r2); LK(r2, r3, r1, r0, r4, 26); |
| 496 | S2(r2, r3, r1, r0, r4); LK(r4, r3, r2, r0, r1, 27); |
| 497 | S3(r4, r3, r2, r0, r1); LK(r0, r1, r3, r4, r2, 28); |
| 498 | S4(r0, r1, r3, r4, r2); LK(r1, r3, r4, r2, r0, 29); |
| 499 | S5(r1, r3, r4, r2, r0); LK(r0, r1, r3, r2, r4, 30); |
| 500 | S6(r0, r1, r3, r2, r4); LK(r3, r4, r1, r2, r0, 31); |
| 501 | S7(r3, r4, r1, r2, r0); K(r0, r1, r2, r3, 32); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | |
| 503 | d[0] = cpu_to_le32(r0); |
| 504 | d[1] = cpu_to_le32(r1); |
| 505 | d[2] = cpu_to_le32(r2); |
| 506 | d[3] = cpu_to_le32(r3); |
| 507 | } |
Jussi Kivilinna | bc83b82 | 2011-10-18 00:03:08 +0300 | [diff] [blame] | 508 | EXPORT_SYMBOL_GPL(__serpent_encrypt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | |
Jussi Kivilinna | bc83b82 | 2011-10-18 00:03:08 +0300 | [diff] [blame] | 510 | static void serpent_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 512 | struct serpent_ctx *ctx = crypto_tfm_ctx(tfm); |
Jussi Kivilinna | bc83b82 | 2011-10-18 00:03:08 +0300 | [diff] [blame] | 513 | |
| 514 | __serpent_encrypt(ctx, dst, src); |
| 515 | } |
| 516 | |
| 517 | void __serpent_decrypt(struct serpent_ctx *ctx, u8 *dst, const u8 *src) |
| 518 | { |
| 519 | const u32 *k = ctx->expkey; |
Alexey Dobriyan | d08f74e | 2005-12-23 01:12:25 +0300 | [diff] [blame] | 520 | const __le32 *s = (const __le32 *)src; |
| 521 | __le32 *d = (__le32 *)dst; |
| 522 | u32 r0, r1, r2, r3, r4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | |
| 524 | r0 = le32_to_cpu(s[0]); |
| 525 | r1 = le32_to_cpu(s[1]); |
| 526 | r2 = le32_to_cpu(s[2]); |
| 527 | r3 = le32_to_cpu(s[3]); |
| 528 | |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 529 | K(r0, r1, r2, r3, 32); |
| 530 | SI7(r0, r1, r2, r3, r4); KL(r1, r3, r0, r4, r2, 31); |
| 531 | SI6(r1, r3, r0, r4, r2); KL(r0, r2, r4, r1, r3, 30); |
| 532 | SI5(r0, r2, r4, r1, r3); KL(r2, r3, r0, r4, r1, 29); |
| 533 | SI4(r2, r3, r0, r4, r1); KL(r2, r0, r1, r4, r3, 28); |
| 534 | SI3(r2, r0, r1, r4, r3); KL(r1, r2, r3, r4, r0, 27); |
| 535 | SI2(r1, r2, r3, r4, r0); KL(r2, r0, r4, r3, r1, 26); |
| 536 | SI1(r2, r0, r4, r3, r1); KL(r1, r0, r4, r3, r2, 25); |
| 537 | SI0(r1, r0, r4, r3, r2); KL(r4, r2, r0, r1, r3, 24); |
| 538 | SI7(r4, r2, r0, r1, r3); KL(r2, r1, r4, r3, r0, 23); |
| 539 | SI6(r2, r1, r4, r3, r0); KL(r4, r0, r3, r2, r1, 22); |
| 540 | SI5(r4, r0, r3, r2, r1); KL(r0, r1, r4, r3, r2, 21); |
| 541 | SI4(r0, r1, r4, r3, r2); KL(r0, r4, r2, r3, r1, 20); |
| 542 | SI3(r0, r4, r2, r3, r1); KL(r2, r0, r1, r3, r4, 19); |
| 543 | SI2(r2, r0, r1, r3, r4); KL(r0, r4, r3, r1, r2, 18); |
| 544 | SI1(r0, r4, r3, r1, r2); KL(r2, r4, r3, r1, r0, 17); |
| 545 | SI0(r2, r4, r3, r1, r0); KL(r3, r0, r4, r2, r1, 16); |
| 546 | SI7(r3, r0, r4, r2, r1); KL(r0, r2, r3, r1, r4, 15); |
| 547 | SI6(r0, r2, r3, r1, r4); KL(r3, r4, r1, r0, r2, 14); |
| 548 | SI5(r3, r4, r1, r0, r2); KL(r4, r2, r3, r1, r0, 13); |
| 549 | SI4(r4, r2, r3, r1, r0); KL(r4, r3, r0, r1, r2, 12); |
| 550 | SI3(r4, r3, r0, r1, r2); KL(r0, r4, r2, r1, r3, 11); |
| 551 | SI2(r0, r4, r2, r1, r3); KL(r4, r3, r1, r2, r0, 10); |
| 552 | SI1(r4, r3, r1, r2, r0); KL(r0, r3, r1, r2, r4, 9); |
| 553 | SI0(r0, r3, r1, r2, r4); KL(r1, r4, r3, r0, r2, 8); |
| 554 | SI7(r1, r4, r3, r0, r2); KL(r4, r0, r1, r2, r3, 7); |
| 555 | SI6(r4, r0, r1, r2, r3); KL(r1, r3, r2, r4, r0, 6); |
| 556 | SI5(r1, r3, r2, r4, r0); KL(r3, r0, r1, r2, r4, 5); |
| 557 | SI4(r3, r0, r1, r2, r4); KL(r3, r1, r4, r2, r0, 4); |
| 558 | SI3(r3, r1, r4, r2, r0); KL(r4, r3, r0, r2, r1, 3); |
| 559 | SI2(r4, r3, r0, r2, r1); KL(r3, r1, r2, r0, r4, 2); |
| 560 | SI1(r3, r1, r2, r0, r4); KL(r4, r1, r2, r0, r3, 1); |
| 561 | SI0(r4, r1, r2, r0, r3); K(r2, r3, r1, r4, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | |
| 563 | d[0] = cpu_to_le32(r2); |
| 564 | d[1] = cpu_to_le32(r3); |
| 565 | d[2] = cpu_to_le32(r1); |
| 566 | d[3] = cpu_to_le32(r4); |
| 567 | } |
Jussi Kivilinna | bc83b82 | 2011-10-18 00:03:08 +0300 | [diff] [blame] | 568 | EXPORT_SYMBOL_GPL(__serpent_decrypt); |
| 569 | |
| 570 | static void serpent_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) |
| 571 | { |
| 572 | struct serpent_ctx *ctx = crypto_tfm_ctx(tfm); |
| 573 | |
| 574 | __serpent_decrypt(ctx, dst, src); |
| 575 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 577 | static int tnepres_setkey(struct crypto_tfm *tfm, const u8 *key, |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 578 | unsigned int keylen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | { |
| 580 | u8 rev_key[SERPENT_MAX_KEY_SIZE]; |
| 581 | int i; |
| 582 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | for (i = 0; i < keylen; ++i) |
| 584 | rev_key[keylen - i - 1] = key[i]; |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 585 | |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 586 | return serpent_setkey(tfm, rev_key, keylen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | } |
| 588 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 589 | static void tnepres_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | { |
| 591 | const u32 * const s = (const u32 * const)src; |
| 592 | u32 * const d = (u32 * const)dst; |
| 593 | |
| 594 | u32 rs[4], rd[4]; |
| 595 | |
| 596 | rs[0] = swab32(s[3]); |
| 597 | rs[1] = swab32(s[2]); |
| 598 | rs[2] = swab32(s[1]); |
| 599 | rs[3] = swab32(s[0]); |
| 600 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 601 | serpent_encrypt(tfm, (u8 *)rd, (u8 *)rs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | |
| 603 | d[0] = swab32(rd[3]); |
| 604 | d[1] = swab32(rd[2]); |
| 605 | d[2] = swab32(rd[1]); |
| 606 | d[3] = swab32(rd[0]); |
| 607 | } |
| 608 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 609 | static void tnepres_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | { |
| 611 | const u32 * const s = (const u32 * const)src; |
| 612 | u32 * const d = (u32 * const)dst; |
| 613 | |
| 614 | u32 rs[4], rd[4]; |
| 615 | |
| 616 | rs[0] = swab32(s[3]); |
| 617 | rs[1] = swab32(s[2]); |
| 618 | rs[2] = swab32(s[1]); |
| 619 | rs[3] = swab32(s[0]); |
| 620 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 621 | serpent_decrypt(tfm, (u8 *)rd, (u8 *)rs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | |
| 623 | d[0] = swab32(rd[3]); |
| 624 | d[1] = swab32(rd[2]); |
| 625 | d[2] = swab32(rd[1]); |
| 626 | d[3] = swab32(rd[0]); |
| 627 | } |
| 628 | |
Jussi Kivilinna | bbc406b | 2012-07-11 14:20:10 +0300 | [diff] [blame] | 629 | static struct crypto_alg srp_algs[2] = { { |
| 630 | .cra_name = "serpent", |
| 631 | .cra_driver_name = "serpent-generic", |
| 632 | .cra_priority = 100, |
| 633 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER, |
| 634 | .cra_blocksize = SERPENT_BLOCK_SIZE, |
| 635 | .cra_ctxsize = sizeof(struct serpent_ctx), |
| 636 | .cra_alignmask = 3, |
| 637 | .cra_module = THIS_MODULE, |
| 638 | .cra_u = { .cipher = { |
| 639 | .cia_min_keysize = SERPENT_MIN_KEY_SIZE, |
| 640 | .cia_max_keysize = SERPENT_MAX_KEY_SIZE, |
| 641 | .cia_setkey = serpent_setkey, |
| 642 | .cia_encrypt = serpent_encrypt, |
| 643 | .cia_decrypt = serpent_decrypt } } |
| 644 | }, { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | .cra_name = "tnepres", |
Eric Biggers | d6ebf52 | 2019-06-02 22:40:57 -0700 | [diff] [blame] | 646 | .cra_driver_name = "tnepres-generic", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER, |
| 648 | .cra_blocksize = SERPENT_BLOCK_SIZE, |
| 649 | .cra_ctxsize = sizeof(struct serpent_ctx), |
Herbert Xu | a429d26 | 2006-01-07 16:38:15 +1100 | [diff] [blame] | 650 | .cra_alignmask = 3, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | .cra_module = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | .cra_u = { .cipher = { |
| 653 | .cia_min_keysize = SERPENT_MIN_KEY_SIZE, |
| 654 | .cia_max_keysize = SERPENT_MAX_KEY_SIZE, |
Jussi Kivilinna | 7f4e3e3 | 2011-11-23 12:21:00 +0200 | [diff] [blame] | 655 | .cia_setkey = tnepres_setkey, |
| 656 | .cia_encrypt = tnepres_encrypt, |
| 657 | .cia_decrypt = tnepres_decrypt } } |
Jussi Kivilinna | bbc406b | 2012-07-11 14:20:10 +0300 | [diff] [blame] | 658 | } }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame] | 660 | static int __init serpent_mod_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | { |
Jussi Kivilinna | bbc406b | 2012-07-11 14:20:10 +0300 | [diff] [blame] | 662 | return crypto_register_algs(srp_algs, ARRAY_SIZE(srp_algs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame] | 665 | static void __exit serpent_mod_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | { |
Jussi Kivilinna | bbc406b | 2012-07-11 14:20:10 +0300 | [diff] [blame] | 667 | crypto_unregister_algs(srp_algs, ARRAY_SIZE(srp_algs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | } |
| 669 | |
Eric Biggers | c4741b2 | 2019-04-11 21:57:42 -0700 | [diff] [blame] | 670 | subsys_initcall(serpent_mod_init); |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame] | 671 | module_exit(serpent_mod_fini); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | |
| 673 | MODULE_LICENSE("GPL"); |
| 674 | MODULE_DESCRIPTION("Serpent and tnepres (kerneli compatible serpent reversed) Cipher Algorithm"); |
| 675 | MODULE_AUTHOR("Dag Arne Osvik <osvik@ii.uib.no>"); |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 676 | MODULE_ALIAS_CRYPTO("tnepres"); |
| 677 | MODULE_ALIAS_CRYPTO("serpent"); |
Mathias Krause | 3e14dcf | 2015-01-11 18:17:42 +0100 | [diff] [blame] | 678 | MODULE_ALIAS_CRYPTO("serpent-generic"); |