present_common.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* present_common.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. * present_common.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. uint8_t present_sbox(uint8_t b){
  28. static const uint8_t sb[] = {
  29. 0xC, 0x5, 0x6, 0xB,
  30. 0x9, 0x0, 0xA, 0xD,
  31. 0x3, 0xE, 0xF, 0x8,
  32. 0x4, 0x7, 0x1, 0x2
  33. };
  34. return ((sb[b >> 4]) << 4) | (sb[b & 0xf]);
  35. }
  36. uint8_t present_sbox_inv(uint8_t b){
  37. static const uint8_t sb[] = {
  38. 0x5, 0xE, 0xF, 0x8,
  39. 0xC, 0x1, 0x2, 0xD,
  40. 0xB, 0x4, 0x6, 0x3,
  41. 0x0, 0x7, 0x9, 0xA
  42. };
  43. return ((sb[b >> 4]) << 4) | (sb[b & 0xf]);
  44. }
  45. void present_p(uint8_t* o, uint8_t* i){
  46. uint8_t m,n=0,idx=0;
  47. for(m=0; m<64; ++m){
  48. o[idx] <<= 1;
  49. o[idx] |= i[n] >> 7;
  50. i[n] <<= 1;
  51. idx = (idx + 2) & 7;
  52. if((m & 7) == 7){
  53. ++n;
  54. }
  55. if(m == 31){
  56. idx += 1;
  57. }
  58. }
  59. }
  60. void present_generic_enc(void* buffer, uint8_t* ctx, uint8_t ksize_B,
  61. void(*update)(uint8_t*, uint8_t)){
  62. uint8_t i,j,tmp[8], k[ksize_B];
  63. memcpy(k, ctx, ksize_B);
  64. memxor(buffer, k, 8);
  65. for(i=1; i<32; ++i){
  66. j = 7;
  67. do{
  68. tmp[j] = present_sbox(((uint8_t*)buffer)[j]);
  69. }while(j--);
  70. present_p(buffer, tmp);
  71. update(k, i);
  72. memxor(buffer, k, 8);
  73. }
  74. }
  75. void present_generic_dec(void* buffer, uint8_t* ctx, uint8_t ksize_B,
  76. void(*update)(uint8_t*, uint8_t)){
  77. uint8_t j,tmp[8], k[ksize_B];
  78. uint8_t i;
  79. memcpy(k, ctx + ksize_B, ksize_B);
  80. memxor(buffer, k, 8);
  81. i = 31;
  82. do{
  83. present_p(tmp, buffer);
  84. present_p(buffer, tmp);
  85. j = 7;
  86. do{
  87. ((uint8_t*)buffer)[j] = present_sbox_inv(((uint8_t*)buffer)[j]);
  88. }while(j--);
  89. update(k, i);
  90. memxor(buffer, k, 8);
  91. }while(--i);
  92. }