cscipher_small.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* cscipher_small_core.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. #include <stdint.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <crypto/memxor.h>
  20. #include <crypto/cscipher.h>
  21. #define DEBUG 0
  22. #if DEBUG
  23. #include <crypto/cli.h>
  24. #endif
  25. #define ROTL(a) (((a)<<1)|((a)>>7))
  26. #ifndef SBOX_PROG
  27. #define SBOX_PROG 0
  28. #endif
  29. #if SBOX_PROG
  30. static const uint8_t fg_table[] = {
  31. 0xfa, 0xd6, 0xb0, 0xb2, 0x7b, 0x5e, 0x71, 0x78,
  32. 0xed, 0xd4, 0xa5, 0xb3, 0xef, 0xdc, 0xe7, 0xf9
  33. };
  34. static
  35. uint8_t p(uint8_t a){
  36. a ^= fg_table[a&0xf]&0xf0;
  37. a ^= fg_table[a>>4] &0x0f;
  38. a ^= fg_table[a&0xf]&0xf0;
  39. return a;
  40. }
  41. #define P(a) p(a)
  42. #else
  43. #include <crypto/cscipher_sbox.h>
  44. #define P(a) (cscipher_sbox[(a)])
  45. #endif
  46. static const uint8_t round_const[] = {
  47. 0xb7, 0xe1, 0x51, 0x62, 0x8a, 0xed, 0x2a, 0x6a,
  48. 0xbf, 0x71, 0x58, 0x80, 0x9c, 0xf4, 0xf3, 0xc7 };
  49. static const uint8_t ks_const[] = {
  50. 0x29,0x0d,0x61,0x40,0x9c,0xeb,0x9e,0x8f,
  51. 0x1f,0x85,0x5f,0x58,0x5b,0x01,0x39,0x86,
  52. 0x97,0x2e,0xd7,0xd6,0x35,0xae,0x17,0x16,
  53. 0x21,0xb6,0x69,0x4e,0xa5,0x72,0x87,0x08,
  54. 0x3c,0x18,0xe6,0xe7,0xfa,0xad,0xb8,0x89,
  55. 0xb7,0x00,0xf7,0x6f,0x73,0x84,0x11,0x63,
  56. 0x3f,0x96,0x7f,0x6e,0xbf,0x14,0x9d,0xac,
  57. 0xa4,0x0e,0x7e,0xf6,0x20,0x4a,0x62,0x30,
  58. 0x03,0xc5,0x4b,0x5a,0x46,0xa3,0x44,0x65
  59. };
  60. static uint16_t m(uint16_t a){
  61. uint8_t xl, xr, yl, yr;
  62. uint16_t ret;
  63. xr = a>>8;
  64. xl = a&0xff;
  65. yl = (ROTL(xl)&0x55)^xl^xr;
  66. yr = ROTL(xl)^xr;
  67. ret = (P(yr)<<8)|P(yl);
  68. return ret;
  69. }
  70. static uint16_t m_inv(uint16_t a){
  71. uint8_t xl, xr;
  72. xr = P(a>>8);
  73. xl = P(a&0xff);
  74. xl ^= xr;
  75. xl ^= (ROTL(xl)&0xaa);
  76. xr ^= ROTL(xl);
  77. return (xr<<8)|xl;
  78. }
  79. void cscipher_enc(void* buffer, const cscipher_ctx_t* ctx){
  80. uint8_t i,j,k;
  81. uint8_t tmp[8];
  82. for(i=0; i<8; ++i){
  83. #if DEBUG
  84. cli_putstr("\r\nDBG: round ");
  85. cli_hexdump(&i, 1);
  86. cli_putstr(" buffer:");
  87. cli_hexdump(buffer, 8);
  88. #endif
  89. for(j=0; j<3; ++j){
  90. if(j==0){
  91. memxor(buffer, ctx->keys[i], 8);
  92. }else{
  93. memxor(buffer, round_const+((j==1)?0:8), 8);
  94. }
  95. for(k=0; k<4; ++k){
  96. ((uint16_t*)tmp)[k] = m(((uint16_t*)buffer)[k]);
  97. }
  98. for(k=0; k<4; ++k){
  99. ((uint8_t*)buffer)[k] = tmp[2*k];
  100. ((uint8_t*)buffer)[k+4] = tmp[2*k+1];
  101. }
  102. }
  103. }
  104. memxor(buffer, ctx->keys[8], 8);
  105. }
  106. void cscipher_dec(void* buffer, const cscipher_ctx_t* ctx){
  107. uint8_t i=7,j,k;
  108. uint8_t tmp[8];
  109. memxor(buffer, ctx->keys[8], 8);
  110. do{
  111. for(j=0; j<3; ++j){
  112. for(k=0; k<4; ++k){
  113. tmp[2*k] = ((uint8_t*)buffer)[k];
  114. tmp[2*k+1] = ((uint8_t*)buffer)[4+k];
  115. }
  116. for(k=0; k<4; ++k){
  117. ((uint16_t*)buffer)[k] = m_inv(((uint16_t*)tmp)[k]);
  118. }
  119. if(j==2){
  120. memxor(buffer, ctx->keys[i], 8);
  121. }else{
  122. memxor(buffer, round_const+((j==1)?0:8), 8);
  123. }
  124. }
  125. }while(i--);
  126. }
  127. void cscipher_init(const void* key, cscipher_ctx_t* ctx){
  128. uint8_t tmp_key[16], tmp[8];
  129. uint8_t i,j,k,t=0;
  130. memcpy(tmp_key, key, 16);
  131. for(i=0; i<9; ++i){
  132. #if DEBUG
  133. cli_putstr("\r\nDBG: round ");
  134. cli_hexdump(&i, 1);
  135. cli_putstr(" key state:");
  136. cli_hexdump(tmp_key, 16);
  137. #endif
  138. memcpy(tmp, tmp_key+(((i&1)==0)?0:8), 8);
  139. memxor(tmp, ks_const+8*i, 8);
  140. for(j=0; j<8; ++j){
  141. tmp[j] = P(tmp[j]);
  142. }
  143. for(j=0; j<8; ++j){
  144. for(k=0; k<8; ++k){
  145. t<<=1;
  146. t |= tmp[k]>>7;
  147. tmp[k]<<=1;
  148. }
  149. tmp_key[j+(((i&1)==0)?8:0)] ^= t;
  150. }
  151. memcpy(ctx->keys[i], tmp_key+(((i&1)==0)?8:0), 8);
  152. }
  153. }