lrw_process.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis
  2. *
  3. * LibTomCrypt is a library that provides various cryptographic
  4. * algorithms in a highly modular and flexible manner.
  5. *
  6. * The library is free for all purposes without any express
  7. * guarantee it works.
  8. */
  9. #include "tomcrypt_private.h"
  10. /**
  11. @file lrw_process.c
  12. LRW_MODE implementation, Encrypt/decrypt blocks, Tom St Denis
  13. */
  14. #ifdef LTC_LRW_MODE
  15. /**
  16. Process blocks with LRW, since decrypt/encrypt are largely the same they share this code.
  17. @param pt The "input" data
  18. @param ct [out] The "output" data
  19. @param len The length of the input, must be a multiple of 128-bits (16 octets)
  20. @param mode LRW_ENCRYPT or LRW_DECRYPT
  21. @param lrw The LRW state
  22. @return CRYPT_OK if successful
  23. */
  24. int lrw_process(const unsigned char *pt, unsigned char *ct, unsigned long len, int mode, symmetric_LRW *lrw)
  25. {
  26. unsigned char prod[16];
  27. int x, err;
  28. #ifdef LTC_LRW_TABLES
  29. int y;
  30. #endif
  31. LTC_ARGCHK(pt != NULL);
  32. LTC_ARGCHK(ct != NULL);
  33. LTC_ARGCHK(lrw != NULL);
  34. if (len & 15) {
  35. return CRYPT_INVALID_ARG;
  36. }
  37. while (len) {
  38. /* copy pad */
  39. XMEMCPY(prod, lrw->pad, 16);
  40. /* increment IV */
  41. for (x = 15; x >= 0; x--) {
  42. lrw->IV[x] = (lrw->IV[x] + 1) & 255;
  43. if (lrw->IV[x]) {
  44. break;
  45. }
  46. }
  47. /* update pad */
  48. #ifdef LTC_LRW_TABLES
  49. /* for each byte changed we undo it's affect on the pad then add the new product */
  50. for (; x < 16; x++) {
  51. #ifdef LTC_FAST
  52. for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) {
  53. *(LTC_FAST_TYPE_PTR_CAST(lrw->pad + y)) ^= *(LTC_FAST_TYPE_PTR_CAST(&lrw->PC[x][lrw->IV[x]][y])) ^ *(LTC_FAST_TYPE_PTR_CAST(&lrw->PC[x][(lrw->IV[x]-1)&255][y]));
  54. }
  55. #else
  56. for (y = 0; y < 16; y++) {
  57. lrw->pad[y] ^= lrw->PC[x][lrw->IV[x]][y] ^ lrw->PC[x][(lrw->IV[x]-1)&255][y];
  58. }
  59. #endif
  60. }
  61. #else
  62. gcm_gf_mult(lrw->tweak, lrw->IV, lrw->pad);
  63. #endif
  64. /* xor prod */
  65. #ifdef LTC_FAST
  66. for (x = 0; x < 16; x += sizeof(LTC_FAST_TYPE)) {
  67. *(LTC_FAST_TYPE_PTR_CAST(ct + x)) = *(LTC_FAST_TYPE_PTR_CAST(pt + x)) ^ *(LTC_FAST_TYPE_PTR_CAST(prod + x));
  68. }
  69. #else
  70. for (x = 0; x < 16; x++) {
  71. ct[x] = pt[x] ^ prod[x];
  72. }
  73. #endif
  74. /* send through cipher */
  75. if (mode == LRW_ENCRYPT) {
  76. if ((err = cipher_descriptor[lrw->cipher].ecb_encrypt(ct, ct, &lrw->key)) != CRYPT_OK) {
  77. return err;
  78. }
  79. } else {
  80. if ((err = cipher_descriptor[lrw->cipher].ecb_decrypt(ct, ct, &lrw->key)) != CRYPT_OK) {
  81. return err;
  82. }
  83. }
  84. /* xor prod */
  85. #ifdef LTC_FAST
  86. for (x = 0; x < 16; x += sizeof(LTC_FAST_TYPE)) {
  87. *(LTC_FAST_TYPE_PTR_CAST(ct + x)) = *(LTC_FAST_TYPE_PTR_CAST(ct + x)) ^ *(LTC_FAST_TYPE_PTR_CAST(prod + x));
  88. }
  89. #else
  90. for (x = 0; x < 16; x++) {
  91. ct[x] = ct[x] ^ prod[x];
  92. }
  93. #endif
  94. /* move to next */
  95. pt += 16;
  96. ct += 16;
  97. len -= 16;
  98. }
  99. return CRYPT_OK;
  100. }
  101. #endif
  102. /* ref: $Format:%D$ */
  103. /* git commit: $Format:%H$ */
  104. /* commit time: $Format:%ai$ */