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