Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Serpent (cipher)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Key schedule == The Serpent key schedule consists of 3 main stages. In the first stage the key is initialized by adding padding if necessary. This is done in order to make short keys map to long keys of 256-bits, one "1" bit is appended to the end of the short key followed by "0" bits until the short key is mapped to a long key length.<ref name=":0" /> In the next phase, the "prekeys" are derived using the previously initialized key. 32-bit key parts XORed, the ''FRAC'' which is the fraction of the [[Golden ratio]] and the round index is XORed with the key parts, the result of the [[Exclusive or|XOR]] operation is rotated to left by 11. The ''FRAC'' and round index were added to achieve an even distribution of the keys bits during the rounds.<ref name=":0" /> Finally the "subkeys" are derived from the previously generated "prekeys". This results in a total of 33 128-bit "subkeys".<ref name=":0" /> At the end the round key or "subkey" are placed in the "initial permutation IP" to place the key bits in the correct column.<ref name=":0" /> === Key schedule in C++ === <syntaxhighlight lang="cpp"> #define FRAC 0x9e3779b9 // fractional part of the golden ratio #define ROTL(A, n) ((A) << n | (A) >> 32-n) uint32_t key[8]; // key provided by user uint32_t subkey[33][4]; // roundkeys const uint8_t S[8][16] = {}; // S-boxes /* key schedule: get prekeys */ void get_pre(uint32_t w[4*33], const uint32_t k[8]) { uint32_t x[4*33+8]; for (int i = 0; i < 8; i++) x[i] = k[i]; for (int i = 8; i < 140; i++) { x[i] = ROTL(x[i-8] ^ x[i-5] ^ x[i-3] ^ x[i-1] ^ FRAC ^ (i-8), 11); w[i-8] = x[i]; } } /* key schedule: get subkeys */ void get_sk(const uint32_t w[4*33], uint32_t (*sk)[4]) { uint8_t i, p, j, s, k; for (i = 0; i < 33; i++) { p = 32 + 3 - i; for (j = 0; j < 4; j++) sk[i][j] = 0; for (k = 0; k < 32; k++) { s = S[p % 8][((w[4 * i + 0] >> k) & 0x1) << 0 | ((w[4 * i + 1] >> k) & 0x1) << 1 | ((w[4 * i + 2] >> k) & 0x1) << 2 | ((w[4 * i + 3] >> k) & 0x1) << 3 ]; for (j = 0; j < 4; j++) { sk[i][j] |= ((s >> j) & 0x1) << k; } } } } void key_schedule() { uint32_t w[4*33]; get_pre(w, key); get_sk(w, subkey); } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)