ofb128.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/crypto.h>
  10. #include "modes_lcl.h"
  11. #include <string.h>
  12. /*
  13. * The input and output encrypted as though 128bit ofb mode is being used.
  14. * The extra state information to record how much of the 128bit block we have
  15. * used is contained in *num;
  16. */
  17. void CRYPTO_ofb128_encrypt(const unsigned char *in, unsigned char *out,
  18. size_t len, const void *key,
  19. unsigned char ivec[16], int *num, block128_f block)
  20. {
  21. unsigned int n;
  22. size_t l = 0;
  23. n = *num;
  24. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  25. if (16 % sizeof(size_t) == 0) { /* always true actually */
  26. do {
  27. while (n && len) {
  28. *(out++) = *(in++) ^ ivec[n];
  29. --len;
  30. n = (n + 1) % 16;
  31. }
  32. # if defined(STRICT_ALIGNMENT)
  33. if (((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) !=
  34. 0)
  35. break;
  36. # endif
  37. while (len >= 16) {
  38. (*block) (ivec, ivec, key);
  39. for (; n < 16; n += sizeof(size_t))
  40. *(size_t *)(out + n) =
  41. *(size_t *)(in + n) ^ *(size_t *)(ivec + n);
  42. len -= 16;
  43. out += 16;
  44. in += 16;
  45. n = 0;
  46. }
  47. if (len) {
  48. (*block) (ivec, ivec, key);
  49. while (len--) {
  50. out[n] = in[n] ^ ivec[n];
  51. ++n;
  52. }
  53. }
  54. *num = n;
  55. return;
  56. } while (0);
  57. }
  58. /* the rest would be commonly eliminated by x86* compiler */
  59. #endif
  60. while (l < len) {
  61. if (n == 0) {
  62. (*block) (ivec, ivec, key);
  63. }
  64. out[l] = in[l] ^ ivec[n];
  65. ++l;
  66. n = (n + 1) % 16;
  67. }
  68. *num = n;
  69. }