]>
Commit | Line | Data |
---|---|---|
4f22f405 | 1 | /* |
3c2bdd7d | 2 | * Copyright 2008-2021 The OpenSSL Project Authors. All Rights Reserved. |
85b2c0ce | 3 | * |
81cae8ce | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
4f22f405 RS |
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 | |
85b2c0ce AP |
8 | */ |
9 | ||
85b2c0ce | 10 | #include <string.h> |
459b15d4 | 11 | #include <openssl/crypto.h> |
25f2138b | 12 | #include "crypto/modes.h" |
85b2c0ce | 13 | |
77286fe3 BE |
14 | #if defined(__GNUC__) && !defined(STRICT_ALIGNMENT) |
15 | typedef size_t size_t_aX __attribute((__aligned__(1))); | |
16 | #else | |
17 | typedef size_t size_t_aX; | |
18 | #endif | |
19 | ||
0f113f3e MC |
20 | /* |
21 | * The input and output encrypted as though 128bit ofb mode is being used. | |
22 | * The extra state information to record how much of the 128bit block we have | |
23 | * used is contained in *num; | |
85b2c0ce AP |
24 | */ |
25 | void CRYPTO_ofb128_encrypt(const unsigned char *in, unsigned char *out, | |
0f113f3e MC |
26 | size_t len, const void *key, |
27 | unsigned char ivec[16], int *num, block128_f block) | |
85b2c0ce | 28 | { |
0f113f3e MC |
29 | unsigned int n; |
30 | size_t l = 0; | |
85b2c0ce | 31 | |
1634b2df P |
32 | if (*num < 0) { |
33 | /* There is no good way to signal an error return from here */ | |
34 | *num = -1; | |
35 | return; | |
36 | } | |
0f113f3e | 37 | n = *num; |
85b2c0ce AP |
38 | |
39 | #if !defined(OPENSSL_SMALL_FOOTPRINT) | |
0f113f3e MC |
40 | if (16 % sizeof(size_t) == 0) { /* always true actually */ |
41 | do { | |
42 | while (n && len) { | |
43 | *(out++) = *(in++) ^ ivec[n]; | |
44 | --len; | |
45 | n = (n + 1) % 16; | |
46 | } | |
47 | # if defined(STRICT_ALIGNMENT) | |
48 | if (((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != | |
49 | 0) | |
50 | break; | |
51 | # endif | |
52 | while (len >= 16) { | |
53 | (*block) (ivec, ivec, key); | |
54 | for (; n < 16; n += sizeof(size_t)) | |
77286fe3 BE |
55 | *(size_t_aX *)(out + n) = |
56 | *(size_t_aX *)(in + n) | |
57 | ^ *(size_t_aX *)(ivec + n); | |
0f113f3e MC |
58 | len -= 16; |
59 | out += 16; | |
60 | in += 16; | |
61 | n = 0; | |
62 | } | |
63 | if (len) { | |
64 | (*block) (ivec, ivec, key); | |
65 | while (len--) { | |
66 | out[n] = in[n] ^ ivec[n]; | |
67 | ++n; | |
68 | } | |
69 | } | |
70 | *num = n; | |
71 | return; | |
72 | } while (0); | |
73 | } | |
74 | /* the rest would be commonly eliminated by x86* compiler */ | |
85b2c0ce | 75 | #endif |
0f113f3e MC |
76 | while (l < len) { |
77 | if (n == 0) { | |
78 | (*block) (ivec, ivec, key); | |
79 | } | |
80 | out[l] = in[l] ^ ivec[n]; | |
81 | ++l; | |
82 | n = (n + 1) % 16; | |
83 | } | |
85b2c0ce | 84 | |
0f113f3e | 85 | *num = n; |
85b2c0ce | 86 | } |