s_ocb_done.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. * Tom St Denis, [email protected], http://libtomcrypt.org
  10. */
  11. /* OCB Implementation by Tom St Denis */
  12. #include "mycrypt.h"
  13. #ifdef OCB_MODE
  14. /* Since the last block is encrypted in CTR mode the same code can
  15. * be used to finish a decrypt or encrypt stream. The only difference
  16. * is we XOR the final ciphertext into the checksum so we have to xor it
  17. * before we CTR [decrypt] or after [encrypt]
  18. *
  19. * the names pt/ptlen/ct really just mean in/inlen/out but this is the way I wrote it...
  20. */
  21. int __ocb_done(ocb_state *ocb, const unsigned char *pt, unsigned long ptlen,
  22. unsigned char *ct, unsigned char *tag, unsigned long *taglen, int mode)
  23. {
  24. unsigned char *Z, *Y, *X;
  25. int err, x;
  26. _ARGCHK(ocb != NULL);
  27. _ARGCHK(pt != NULL);
  28. _ARGCHK(ct != NULL);
  29. _ARGCHK(tag != NULL);
  30. _ARGCHK(taglen != NULL);
  31. if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) {
  32. return err;
  33. }
  34. if (ocb->block_len != cipher_descriptor[ocb->cipher].block_length ||
  35. (int)ptlen > ocb->block_len || (int)ptlen < 0) {
  36. return CRYPT_INVALID_ARG;
  37. }
  38. /* allocate ram */
  39. Z = XMALLOC(MAXBLOCKSIZE);
  40. Y = XMALLOC(MAXBLOCKSIZE);
  41. X = XMALLOC(MAXBLOCKSIZE);
  42. if (X == NULL || Y == NULL || Z == NULL) {
  43. if (X != NULL) {
  44. XFREE(X);
  45. }
  46. if (Y != NULL) {
  47. XFREE(Y);
  48. }
  49. if (Z != NULL) {
  50. XFREE(Z);
  51. }
  52. return CRYPT_MEM;
  53. }
  54. /* compute X[m] = len(pt[m]) XOR Lr XOR Z[m] */
  55. ocb_shift_xor(ocb, X);
  56. XMEMCPY(Z, X, ocb->block_len);
  57. X[ocb->block_len-1] ^= (ptlen*8)&255;
  58. X[ocb->block_len-2] ^= ((ptlen*8)>>8)&255;
  59. for (x = 0; x < ocb->block_len; x++) {
  60. X[x] ^= ocb->Lr[x];
  61. }
  62. /* Y[m] = E(X[m])) */
  63. cipher_descriptor[ocb->cipher].ecb_encrypt(X, Y, &ocb->key);
  64. if (mode == 1) {
  65. /* decrypt mode, so let's xor it first */
  66. /* xor C[m] into checksum */
  67. for (x = 0; x < (int)ptlen; x++) {
  68. ocb->checksum[x] ^= ct[x];
  69. }
  70. }
  71. /* C[m] = P[m] xor Y[m] */
  72. for (x = 0; x < (int)ptlen; x++) {
  73. ct[x] = pt[x] ^ Y[x];
  74. }
  75. if (mode == 0) {
  76. /* encrypt mode */
  77. /* xor C[m] into checksum */
  78. for (x = 0; x < (int)ptlen; x++) {
  79. ocb->checksum[x] ^= ct[x];
  80. }
  81. }
  82. /* xor Y[m] and Z[m] into checksum */
  83. for (x = 0; x < ocb->block_len; x++) {
  84. ocb->checksum[x] ^= Y[x] ^ Z[x];
  85. }
  86. /* encrypt checksum, er... tag!! */
  87. cipher_descriptor[ocb->cipher].ecb_encrypt(ocb->checksum, X, &ocb->key);
  88. /* now store it */
  89. for (x = 0; x < ocb->block_len && x < (int)*taglen; x++) {
  90. tag[x] = X[x];
  91. }
  92. *taglen = x;
  93. #ifdef CLEAN_STACK
  94. zeromem(X, MAXBLOCKSIZE);
  95. zeromem(Y, MAXBLOCKSIZE);
  96. zeromem(Z, MAXBLOCKSIZE);
  97. zeromem(ocb, sizeof(*ocb));
  98. #endif
  99. XFREE(X);
  100. XFREE(Y);
  101. XFREE(Z);
  102. return CRYPT_OK;
  103. }
  104. #endif