serpent.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* serpent.c */
  2. /*
  3. This file is part of the ARM-Crypto-Lib.
  4. Copyright (C) 2006-2010 Daniel Otte (daniel.otte@rub.de)
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /* serpent.c
  17. * a bitsliced implementation of the serpent cipher for avr microcontrollers
  18. * author: Daniel Otte
  19. * email: daniel.otte@rub.de
  20. * license: GPLv3
  21. */
  22. #include <stdint.h>
  23. #include <string.h> /* memset() */
  24. #include <crypto/memxor.h>
  25. #include <crypto/serpent.h>
  26. #include <crypto/serpent-sboxes.h>
  27. /******************************************************************************/
  28. uint32_t rotl32(uint32_t a, uint8_t n){
  29. return ((a<<n) | (a>>(32-n)));
  30. }
  31. uint32_t rotr32(uint32_t a, uint8_t n){
  32. return ((a>>n) | (a<<(32-n)));
  33. }
  34. #define X0 (((uint32_t*)b)[0])
  35. #define X1 (((uint32_t*)b)[1])
  36. #define X2 (((uint32_t*)b)[2])
  37. #define X3 (((uint32_t*)b)[3])
  38. static void serpent_lt(uint8_t *b){
  39. X0 = rotl32(X0, 13);
  40. X2 = rotl32(X2, 3);
  41. X1 ^= X0 ^ X2;
  42. X3 ^= X2 ^ (X0 << 3);
  43. X1 = rotl32(X1, 1);
  44. X3 = rotl32(X3, 7);
  45. X0 ^= X1 ^ X3;
  46. X2 ^= X3 ^ (X1 << 7);
  47. X0 = rotl32(X0, 5);
  48. X2 = rotr32(X2, 10);
  49. }
  50. static void serpent_inv_lt(uint8_t *b){
  51. X2 = rotl32(X2, 10);
  52. X0 = rotr32(X0, 5);
  53. X2 ^= X3 ^ (X1 << 7);
  54. X0 ^= X1 ^ X3;
  55. X3 = rotr32(X3, 7);
  56. X1 = rotr32(X1, 1);
  57. X3 ^= X2 ^ (X0 << 3);
  58. X1 ^= X0 ^ X2;
  59. X2 = rotr32(X2, 3);
  60. X0 = rotr32(X0, 13);
  61. }
  62. #define GOLDEN_RATIO 0x9e3779b9l
  63. static uint32_t serpent_gen_w(uint32_t * b, uint8_t i){
  64. uint32_t ret;
  65. ret = b[0] ^ b[3] ^ b[5] ^ b[7] ^ GOLDEN_RATIO ^ (uint32_t)i;
  66. ret = rotl32(ret, 11);
  67. return ret;
  68. }
  69. void serpent_init(const void* key, uint16_t keysize_b, serpent_ctx_t* ctx){
  70. uint32_t buffer[8];
  71. uint8_t i,j;
  72. if(keysize_b<256){
  73. /* keysize is less than 256 bit, padding needed */
  74. memset(buffer, 0, 32);
  75. memcpy(buffer, key, (keysize_b+7)/8);
  76. ((uint8_t*)buffer)[keysize_b/8] |= 1<<(keysize_b%8);
  77. } else {
  78. /* keysize is 256 bit */
  79. memcpy((void*)buffer, key, 32);
  80. }
  81. for(i=0; i<33; ++i){
  82. for(j=0; j<4; ++j){
  83. ctx->k[i][j] = serpent_gen_w(buffer, i*4+j);
  84. memmove(buffer, &(buffer[1]), 7*4); /* shift buffer one to the "left" */
  85. buffer[7] = ctx->k[i][j];
  86. }
  87. }
  88. for(i=0; i<33; ++i){
  89. sbox128(ctx->k[i],3-i);
  90. }
  91. }
  92. void serpent_enc(void* buffer, const serpent_ctx_t* ctx){
  93. uint8_t i;
  94. for(i=0; i<31; ++i){
  95. memxor(buffer, ctx->k[i], 16);
  96. sbox128(buffer, i);
  97. serpent_lt((uint8_t*)buffer);
  98. }
  99. memxor(buffer, ctx->k[i], 16);
  100. sbox128(buffer, i);
  101. ++i;
  102. memxor(buffer, ctx->k[i], 16);
  103. }
  104. void serpent_dec(void* buffer, const serpent_ctx_t* ctx){
  105. int8_t i=32;
  106. memxor(buffer, ctx->k[i], 16);
  107. --i;
  108. inv_sbox128(buffer, i);
  109. memxor((uint8_t*)buffer, ctx->k[i], 16);
  110. --i;
  111. for(; i>=0; --i){
  112. serpent_inv_lt(buffer);
  113. inv_sbox128(buffer, i);
  114. memxor(buffer, ctx->k[i], 16);
  115. }
  116. }