rc5.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* rc5.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. /* rc5.c a C implementation of RC5 for ARM microcontrollers
  17. *
  18. * author: Daniel Otte
  19. * email: daniel.otte@rub.de
  20. * license: GPLv3
  21. *
  22. * this implementation is limited to 64bit blocks and a maximum of 255 rounds
  23. *
  24. */
  25. #include <stdint.h>
  26. #include <stdlib.h> /* malloc() & free() */
  27. #include <string.h> /* memset() & memcpy() */
  28. #include <crypto/rc5.h>
  29. #define A (((uint32_t*)buffer)[0])
  30. #define B (((uint32_t*)buffer)[1])
  31. #define ROTL32(v,n) (((v)<<(n))|((v)>>(32-(n))))
  32. #define ROTR32(v,n) (((v)>>(n))|((v)<<(32-(n))))
  33. void rc5_enc(void* buffer, const rc5_ctx_t* ctx){
  34. uint8_t i;
  35. A += ctx->s[0];
  36. B += ctx->s[1];
  37. for(i=0; i<ctx->rounds; ++i){
  38. A = ROTL32(A^B, B&31) + ctx->s[(i+1)*2+0];
  39. B = ROTL32(A^B, A&31) + ctx->s[(i+1)*2+1];
  40. }
  41. }
  42. void rc5_dec(void* buffer, const rc5_ctx_t* ctx){
  43. uint8_t i;
  44. for(i=ctx->rounds; i>0; --i){
  45. B = ROTR32(B - ctx->s[i*2+1], A&31) ^ A;
  46. A = ROTR32(A - ctx->s[i*2+0], B&31) ^ B;
  47. }
  48. B -= ctx->s[1];
  49. A -= ctx->s[0];
  50. }
  51. /*
  52. P32 = 10110111111000010101000101100011 = b7e15163
  53. Q32 = 10011110001101110111100110111001 = 9e3779b9
  54. */
  55. #define P32 0xb7e15163
  56. #define Q32 0x9e3779b9
  57. void rc5_init(void* key, uint16_t keysize_b, uint8_t rounds, rc5_ctx_t* ctx){
  58. uint16_t c,n,m,j,i,t;
  59. uint32_t a,b,l[(keysize_b+31)/32];
  60. ctx->rounds = rounds;
  61. t=2*(rounds+1);
  62. c=(keysize_b+31)/32;
  63. ctx->s = malloc(t*sizeof(uint32_t));
  64. memset(l, 0, sizeof(uint32_t)*c);
  65. memcpy(l, key, (keysize_b+7)/8);
  66. ctx->s[0] = P32;
  67. for(i=1; i<t; ++i){
  68. ctx->s[i] = ctx->s[i-1] + Q32;
  69. }
  70. m = ((t>c)?t:c)*3;
  71. i=j=0;
  72. a=b=0;
  73. for(n=0; n<m; ++n){
  74. a=ctx->s[i]=ROTL32(ctx->s[i]+a+b, 3);
  75. b=l[j]=ROTL32(l[j]+a+b, (a+b)&31);
  76. i=(i+1)%t;
  77. j=(j+1)%c;
  78. }
  79. }
  80. void rc5_free(rc5_ctx_t* ctx){
  81. if(ctx->s)
  82. free(ctx->s);
  83. }