present128.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* present128.c */
  2. /*
  3. This file is part of the AVR-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. * present128.c
  18. * a implementation of the PRESENT block-cipher
  19. * author: Daniel Otte
  20. * email: daniel.otte@rub.de
  21. * license: GPLv3
  22. *
  23. * */
  24. #include <string.h>
  25. #include <stdint.h>
  26. #include <crypto/memxor.h>
  27. #include "present_common.h"
  28. #include <crypto/present128.h>
  29. static
  30. void key_update_128(uint8_t* buffer, uint8_t round){
  31. uint8_t j;
  32. uint8_t t8;
  33. union __attribute__((packed)){
  34. uint8_t v8[2];
  35. uint16_t v16;
  36. } tmp;
  37. /* rotate buffer 67 right */
  38. for(j=0; j<8; ++j){
  39. tmp.v8[0] = buffer[j];
  40. buffer[j] = buffer[j + 8];
  41. buffer[j + 8] = tmp.v8[0];
  42. }
  43. j=0;
  44. t8 = (uint16_t)buffer[15] << (5);
  45. do{
  46. tmp.v8[1] = buffer[j];
  47. tmp.v16 >>= 3;
  48. buffer[j] = tmp.v8[1] | t8;
  49. t8 = tmp.v8[0] & 0xe0;
  50. }while(++j<16);
  51. /* rotating done now substitution */
  52. buffer[0] = present_sbox(buffer[0]);
  53. /* xor with round counter */
  54. buffer[8] ^= round << 6;
  55. buffer[7] ^= round >> 2;
  56. }
  57. static
  58. void key_update_128_inv(uint8_t* buffer, uint8_t round){
  59. uint8_t j;
  60. uint8_t t8;
  61. union __attribute__((packed)){
  62. uint8_t v8[2];
  63. uint16_t v16;
  64. } tmp;
  65. /* xor with round counter */
  66. buffer[8] ^= round << 6;
  67. buffer[7] ^= round >> 2;
  68. /* rotating done now substitution */
  69. buffer[0] = present_sbox_inv(buffer[0]);
  70. /* rotate buffer 67 left */
  71. for(j=0; j<8; ++j){
  72. tmp.v8[0] = buffer[j];
  73. buffer[j] = buffer[j + 8];
  74. buffer[j + 8] = tmp.v8[0];
  75. }
  76. j=15;
  77. t8 = (uint16_t)buffer[0] >> (5);
  78. do{
  79. tmp.v8[0] = buffer[j];
  80. tmp.v16 <<= 3;
  81. buffer[j] = tmp.v8[0] | t8;
  82. t8 = tmp.v8[1] & 0x07;
  83. }while(j--);
  84. }
  85. void present128_init(const uint8_t* key, uint8_t keysize_b, present128_ctx_t* ctx){
  86. uint8_t i;
  87. memcpy(ctx->fwd_key, key, 16);
  88. memcpy(ctx->rev_key, key, 16);
  89. for(i=1; i<32; ++i){
  90. key_update_128(ctx->rev_key, i);
  91. }
  92. }
  93. void present128_enc(void* buffer, present128_ctx_t* ctx){
  94. present_generic_enc(buffer, (uint8_t*)ctx, 16, key_update_128);
  95. }
  96. void present128_dec(void* buffer, present128_ctx_t* ctx){
  97. present_generic_dec(buffer, (uint8_t*)ctx, 16, key_update_128_inv);
  98. }
  99. /*
  100. void present128_enc(void* buffer, present128_ctx_t* ctx){
  101. uint8_t i,j,tmp[8], k[16];
  102. memcpy(k, ctx->fwd_key, 16);
  103. memxor(buffer, k, 8);
  104. for(i=1; i<32; ++i){
  105. j = 7;
  106. do{
  107. tmp[j] = present_sbox(((uint8_t*)buffer)[j]);
  108. }while(j--);
  109. present_p(buffer, tmp);
  110. key_update_128(k, i);
  111. memxor(buffer, k, 8);
  112. }
  113. }
  114. void present128_dec(void* buffer, present128_ctx_t* ctx){
  115. uint8_t j,tmp[8], k[16];
  116. uint8_t i;
  117. memcpy(k, ctx->rev_key, 16);
  118. memxor(buffer, k, 8);
  119. i = 31;
  120. do{
  121. present_p(tmp, buffer);
  122. present_p(buffer, tmp);
  123. j = 7;
  124. do{
  125. ((uint8_t*)buffer)[j] = present_sbox_inv(((uint8_t*)buffer)[j]);
  126. }while(j--);
  127. key_update_128_inv(k, i);
  128. memxor(buffer, k, 8);
  129. }while(--i);
  130. }
  131. */