| // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <assert.h> |
| 16 | #include <string.h> |
| 17 |
| 18 | #include "internal.h" |
| 19 |
| 20 |
| 21 | static_assert(16 % sizeof(size_t) == 0, "block cannot be divided into size_t"); |
| 22 |
| 23 | void CRYPTO_ofb128_encrypt(const uint8_t *in, uint8_t *out, size_t len, |
| 24 | const AES_KEY *key, uint8_t ivec[16], unsigned *num, |
| 25 | block128_f block) { |
| 26 | assert(key != NULL && ivec != NULL && num != NULL); |
| 27 | assert(len == 0 || (in != NULL && out != NULL)); |
| 28 |
| 29 | unsigned n = *num; |
| 30 |
| 31 | while (n && len) { |
| 32 | *(out++) = *(in++) ^ ivec[n]; |
| 33 | --len; |
| 34 | n = (n + 1) % 16; |
| 35 | } |
| 36 |
| 37 | while (len >= 16) { |
| 38 | (*block)(ivec, ivec, key); |
| 39 | CRYPTO_xor16(out, in, ivec); |
| 40 | len -= 16; |
| 41 | out += 16; |
| 42 | in += 16; |
| 43 | n = 0; |
| 44 | } |
| 45 | if (len) { |
| 46 | (*block)(ivec, ivec, key); |
| 47 | while (len--) { |
| 48 | out[n] = in[n] ^ ivec[n]; |
| 49 | ++n; |
| 50 | } |
| 51 | } |
| 52 | *num = n; |
| 53 | } |
| 54 |