trivium.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* trivium.c */
  2. /*
  3. This file is part of the ARM-Crypto-Lib.
  4. Copyright (C) 2008 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. /**
  17. *
  18. * author: Daniel Otte
  19. * email: daniel.otte@rub.de
  20. * license: GPLv3
  21. *
  22. */
  23. #include <stdint.h>
  24. #include <string.h>
  25. #include <crypto/trivium.h>
  26. #define G(i) ((((*ctx)[(i)/8])>>(((i)%8)))&1)
  27. #define S(i,v) ((*ctx)[(i)/8] = (((*ctx)[(i)/8]) & (uint8_t)~(1<<((i)%8))) | ((v)<<((i)%8)))
  28. uint8_t trivium_enc(trivium_ctx_t* ctx){
  29. uint8_t t1,t2,t3,z;
  30. t1 = G(65) ^ G(92);
  31. t2 = G(161) ^ G(176);
  32. t3 = G(242) ^ G(287);
  33. z = t1^t2^t3;
  34. t1 ^= (G(90) & G(91)) ^ G(170);
  35. t2 ^= (G(174) & G(175)) ^ G(263);
  36. t3 ^= (G(285) & G(286)) ^ G(68);
  37. /* shift whole state and insert ts later */
  38. uint8_t i,c1=0,c2;
  39. for(i=0; i<36; ++i){
  40. c2=(((*ctx)[i])>>7);
  41. (*ctx)[i] = (((*ctx)[i])<<1)|c1;
  42. c1=c2;
  43. }
  44. /* insert ts */
  45. S(0, t3);
  46. S(93, t1);
  47. S(177, t2);
  48. return z?0x080:0x00;
  49. }
  50. uint8_t trivium_getbyte(trivium_ctx_t *ctx){
  51. uint8_t r=0, i=0;
  52. do{
  53. r>>=1;
  54. r |= trivium_enc(ctx);
  55. }while(++i<8);
  56. return r;
  57. }
  58. #define KEYSIZE_B ((keysize_b+7)/8)
  59. #define IVSIZE_B ((ivsize_b +7)/8)
  60. static const uint8_t rev_table[16] = {
  61. 0x00, 0x08, 0x04, 0x0C, /* 0000 1000 0100 1100 */
  62. 0x02, 0x0A, 0x06, 0x0E, /* 0010 1010 0110 1110 */
  63. 0x01, 0x09, 0x05, 0x0D, /* 0001 1001 0101 1101 */
  64. 0x03, 0x0B, 0x07, 0x0F /* 0011 1011 0111 1111 */
  65. };
  66. void trivium_init(const void* key, uint16_t keysize_b,
  67. const void* iv, uint16_t ivsize_b,
  68. trivium_ctx_t* ctx){
  69. uint16_t i;
  70. uint8_t c1,c2;
  71. uint8_t t1,t2;
  72. memset((*ctx)+KEYSIZE_B, 0, 35-KEYSIZE_B);
  73. c2=0;
  74. c1=KEYSIZE_B;
  75. do{
  76. t1 = ((uint8_t*)key)[--c1];
  77. t2 = (rev_table[t1&0x0f]<<4)|(rev_table[t1>>4]);
  78. (*ctx)[c2++] = t2;
  79. }while(c1!=0);
  80. c2=12;
  81. c1=IVSIZE_B;
  82. do{
  83. t1 = ((uint8_t*)iv)[--c1];
  84. t2 = (rev_table[t1&0x0f]<<4)|(rev_table[t1>>4]);
  85. (*ctx)[c2++] = t2;
  86. }while(c1!=0);
  87. for(i=12+IVSIZE_B; i>10; --i){
  88. c2=(((*ctx)[i])<<5);
  89. (*ctx)[i] = (((*ctx)[i])>>3)|c1;
  90. c1=c2;
  91. }
  92. (*ctx)[35] = 0xE0;
  93. for(i=0; i<4*288; ++i){
  94. trivium_enc(ctx);
  95. }
  96. }