2N/A/* This file was automatically imported with 2N/A import_gcry.py. Please don't modify it */ 2N/A/* serpent.c - Implementation of the Serpent encryption algorithm. 2N/A * Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc. 2N/A * This file is part of Libgcrypt. 2N/A * Libgcrypt is free software; you can redistribute it and/or modify 2N/A * it under the terms of the GNU Lesser general Public License as 2N/A * published by the Free Software Foundation; either version 2.1 of 2N/A * the License, or (at your option) any later version. 2N/A * Libgcrypt is distributed in the hope that it will be useful, 2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of 2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 2N/A * GNU Lesser General Public License for more details. 2N/A * You should have received a copy of the GNU Lesser General Public 2N/A * License along with this program; if not, write to the Free Software 2N/A * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 2N/A/* Magic number, used during generating of the subkeys. */ 2N/A/* Serpent works on 128 bit blocks. */ 2N/A/* Serpent key, provided by the user. If the original key is shorter 2N/A than 256 bits, it is padded. */ 2N/A/* The key schedule consists of 33 128 bit subkeys. */ 2N/A/* A Serpent context. */ 2N/A | (((x) &
0xff000000) >>
24) | (((x) &
0x00ff0000) >>
8) \
2N/A | (((x) &
0x0000ff00) <<
8) | (((x) &
0x000000ff) <<
24))
2N/A/* These are the S-Boxes of Serpent. They are copied from Serpents 2N/A reference implementation (the optimized one, contained in 2N/A `floppy2') and are therefore: 2N/A Copyright (C) 1998 Ross Anderson, Eli Biham, Lars Knudsen. 2N/A To quote the Serpent homepage 2N/A "Serpent is now completely in the public domain, and we impose no 2N/A restrictions on its use. This was announced on the 21st August at 2N/A the First AES Candidate Conference. The optimised implementations 2N/A in the submission package are now under the GNU PUBLIC LICENSE 2N/A (GPL), although some comments in the code still say otherwise. You 2N/A are welcome to use Serpent for any application." */ 2N/A/* XOR BLOCK1 into BLOCK0. */ 2N/A/* Copy BLOCK_SRC to BLOCK_DST. */ 2N/A/* Apply SBOX number WHICH to to the block found in ARRAY0 at index 2N/A INDEX, writing the output to the block found in ARRAY1 at index 2N/A/* Apply inverse SBOX number WHICH to to the block found in ARRAY0 at 2N/A index INDEX, writing the output to the block found in ARRAY1 at 2N/A/* Apply the linear transformation to BLOCK. */ 2N/A/* Apply the inverse linear transformation to BLOCK. */ 2N/A/* Apply a Serpent round to BLOCK, using the SBOX number WHICH and the 2N/A subkeys contained in SUBKEYS. Use BLOCK_TMP as temporary storage. 2N/A This macro increments `round'. */ 2N/A/* Apply the last Serpent round to BLOCK, using the SBOX number WHICH 2N/A and the subkeys contained in SUBKEYS. Use BLOCK_TMP as temporary 2N/A storage. The result will be stored in BLOCK_TMP. This macro 2N/A increments `round'. */ 2N/A/* Apply an inverse Serpent round to BLOCK, using the SBOX number 2N/A WHICH and the subkeys contained in SUBKEYS. Use BLOCK_TMP as 2N/A temporary storage. This macro increments `round'. */ 2N/A/* Apply the first Serpent round to BLOCK, using the SBOX number WHICH 2N/A and the subkeys contained in SUBKEYS. Use BLOCK_TMP as temporary 2N/A storage. The result will be stored in BLOCK_TMP. This macro 2N/A increments `round'. */ 2N/A/* Convert the user provided key KEY of KEY_LENGTH bytes into the 2N/A internally used format. */ 2N/A /* Key must be padded according to the Serpent 2N/A/* Derive the 33 subkeys from KEY and store them in SUBKEYS. */ 2N/A /* Initialize with key values. */ 2N/A for (i = 0; i <
8; i++)
2N/A /* Expand to intermediate key using the affine recurrence. */ 2N/A for (i = 0; i <
132; i++)
2N/A w[i] =
rol (w[i -
8] ^ w[i -
5] ^ w[i -
3] ^ w[i -
1] ^
PHI ^ i,
11);
2N/A /* Calculate subkeys via S-Boxes, in bitslice mode. */ 2N/A /* Renumber subkeys. */ 2N/A for (j = 0; j <
4; j++)
2N/A/* Initialize CONTEXT with the key KEY of KEY_LENGTH bits. */ 2N/A/* Initialize CTX with the key KEY of KEY_LENGTH bytes. */ 2N/A /* Execute a self-test the first time, Serpent is used. */ 2N/A/* "SERPENT" is an alias for "SERPENT128". */