raw_ofb.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* ===================================================================
  2. *
  3. * Copyright (c) 2014, Legrandin <helderijs@gmail.com>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. * ===================================================================
  30. */
  31. #include "common.h"
  32. FAKE_INIT(raw_ofb)
  33. #include "block_base.h"
  34. #define ERR_OFB_IV_LEN ((3 << 16) | 1)
  35. #define MAX_BLOCK_LEN 16
  36. typedef struct {
  37. BlockBase *cipher;
  38. /** How many bytes at the beginning of the key stream
  39. * have already been used.
  40. */
  41. size_t usedKeyStream;
  42. uint8_t keyStream[MAX_BLOCK_LEN];
  43. } OfbModeState;
  44. EXPORT_SYM int OFB_start_operation(BlockBase *cipher,
  45. const uint8_t iv[],
  46. size_t iv_len,
  47. OfbModeState **pResult)
  48. {
  49. if ((NULL == cipher) || (NULL == iv) || (NULL == pResult))
  50. return ERR_NULL;
  51. if (cipher->block_len > MAX_BLOCK_LEN)
  52. return ERR_BLOCK_SIZE;
  53. if (cipher->block_len != iv_len)
  54. return ERR_OFB_IV_LEN;
  55. *pResult = calloc(1, sizeof(OfbModeState));
  56. if (NULL == *pResult)
  57. return ERR_MEMORY;
  58. (*pResult)->cipher = cipher;
  59. (*pResult)->usedKeyStream = cipher->block_len;
  60. memcpy((*pResult)->keyStream, iv, iv_len);
  61. return 0;
  62. }
  63. EXPORT_SYM int OFB_encrypt(OfbModeState *ofbState,
  64. const uint8_t *in,
  65. uint8_t *out,
  66. size_t data_len)
  67. {
  68. size_t block_len;
  69. uint8_t oldKeyStream[MAX_BLOCK_LEN];
  70. if ((NULL == ofbState) || (NULL == in) || (NULL == out))
  71. return ERR_NULL;
  72. block_len = ofbState->cipher->block_len;
  73. if (block_len > MAX_BLOCK_LEN)
  74. return ERR_BLOCK_SIZE;
  75. while (data_len > 0) {
  76. size_t i;
  77. size_t keyStreamToUse;
  78. if (ofbState->usedKeyStream == block_len) {
  79. int result;
  80. memcpy(oldKeyStream, ofbState->keyStream, block_len);
  81. result = ofbState->cipher->encrypt(ofbState->cipher,
  82. oldKeyStream,
  83. ofbState->keyStream,
  84. block_len);
  85. if (0 != result)
  86. return result;
  87. ofbState->usedKeyStream = 0;
  88. }
  89. keyStreamToUse = MIN(data_len, block_len - ofbState->usedKeyStream);
  90. for (i=0; i<keyStreamToUse; i++)
  91. *out++ = *in++ ^ ofbState->keyStream[i + ofbState->usedKeyStream];
  92. data_len -= keyStreamToUse;
  93. ofbState->usedKeyStream += keyStreamToUse;
  94. }
  95. return 0;
  96. }
  97. EXPORT_SYM int OFB_decrypt(OfbModeState *ofbState,
  98. const uint8_t *in,
  99. uint8_t *out,
  100. size_t data_len)
  101. {
  102. return OFB_encrypt(ofbState, in, out, data_len);
  103. }
  104. EXPORT_SYM int OFB_stop_operation(OfbModeState *state)
  105. {
  106. if (NULL == state)
  107. return ERR_NULL;
  108. state->cipher->destructor(state->cipher);
  109. free(state);
  110. return 0;
  111. }