block.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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 <assert.h>
  10. #include "cipher_local.h"
  11. #include "internal/providercommonerr.h"
  12. /*
  13. * Fills a single block of buffered data from the input, and returns the amount
  14. * of data remaining in the input that is a multiple of the blocksize. The buffer
  15. * is only filled if it already has some data in it, isn't full already or we
  16. * don't have at least one block in the input.
  17. *
  18. * buf: a buffer of blocksize bytes
  19. * buflen: contains the amount of data already in buf on entry. Updated with the
  20. * amount of data in buf at the end. On entry *buflen must always be
  21. * less than the blocksize
  22. * blocksize: size of a block. Must be greater than 0 and a power of 2
  23. * in: pointer to a pointer containing the input data
  24. * inlen: amount of input data available
  25. *
  26. * On return buf is filled with as much data as possible up to a full block,
  27. * *buflen is updated containing the amount of data in buf. *in is updated to
  28. * the new location where input data should be read from, *inlen is updated with
  29. * the remaining amount of data in *in. Returns the largest value <= *inlen
  30. * which is a multiple of the blocksize.
  31. */
  32. size_t fillblock(unsigned char *buf, size_t *buflen, size_t blocksize,
  33. const unsigned char **in, size_t *inlen)
  34. {
  35. size_t blockmask = ~(blocksize - 1);
  36. assert(*buflen <= blocksize);
  37. assert(blocksize > 0 && (blocksize & (blocksize - 1)) == 0);
  38. if (*buflen != blocksize && (*buflen != 0 || *inlen < blocksize)) {
  39. size_t bufremain = blocksize - *buflen;
  40. if (*inlen < bufremain)
  41. bufremain = *inlen;
  42. memcpy(buf + *buflen, *in, bufremain);
  43. *in += bufremain;
  44. *inlen -= bufremain;
  45. *buflen += bufremain;
  46. }
  47. return *inlen & blockmask;
  48. }
  49. /*
  50. * Fills the buffer with trailing data from an encryption/decryption that didn't
  51. * fit into a full block.
  52. */
  53. int trailingdata(unsigned char *buf, size_t *buflen, size_t blocksize,
  54. const unsigned char **in, size_t *inlen)
  55. {
  56. if (*inlen == 0)
  57. return 1;
  58. if (*buflen + *inlen > blocksize) {
  59. ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
  60. return 0;
  61. }
  62. memcpy(buf + *buflen, *in, *inlen);
  63. *buflen += *inlen;
  64. *inlen = 0;
  65. return 1;
  66. }
  67. /* Pad the final block for encryption */
  68. void padblock(unsigned char *buf, size_t *buflen, size_t blocksize)
  69. {
  70. size_t i;
  71. unsigned char pad = (unsigned char)(blocksize - *buflen);
  72. for (i = *buflen; i < blocksize; i++)
  73. buf[i] = pad;
  74. }
  75. int unpadblock(unsigned char *buf, size_t *buflen, size_t blocksize)
  76. {
  77. size_t pad, i;
  78. size_t len = *buflen;
  79. if(len != blocksize) {
  80. ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
  81. return 0;
  82. }
  83. /*
  84. * The following assumes that the ciphertext has been authenticated.
  85. * Otherwise it provides a padding oracle.
  86. */
  87. pad = buf[blocksize - 1];
  88. if (pad == 0 || pad > blocksize) {
  89. ERR_raise(ERR_LIB_PROV, PROV_R_BAD_DECRYPT);
  90. return 0;
  91. }
  92. for (i = 0; i < pad; i++) {
  93. if (buf[--len] != pad) {
  94. ERR_raise(ERR_LIB_PROV, PROV_R_BAD_DECRYPT);
  95. return 0;
  96. }
  97. }
  98. *buflen = len;
  99. return 1;
  100. }