| 6 | // |
| 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 | #ifndef OPENSSL_HEADER_AES_H |
| 16 | #define OPENSSL_HEADER_AES_H |
| 17 |
| 18 | #include <openssl/base.h> // IWYU pragma: export |
| 19 |
| 20 | #if defined(__cplusplus) |
| 21 | extern "C" { |
| 22 | #endif |
| 23 |
| 24 |
| 25 | // Raw AES functions. |
| 26 |
| 27 |
| 28 | #define AES_ENCRYPT 1 |
| 29 | #define AES_DECRYPT 0 |
| 30 |
| 31 | // AES_MAXNR is the maximum number of AES rounds. |
| 32 | #define AES_MAXNR 14 |
| 33 |
| 34 | #define AES_BLOCK_SIZE 16 |
| 35 |
| 36 | // aes_key_st should be an opaque type, but EVP requires that the size be |
| 37 | // known. |
| 38 | struct aes_key_st { |
| 39 | uint32_t rd_key[4 * (AES_MAXNR + 1)]; |
| 40 | unsigned rounds; |
| 41 | }; |
| 42 | typedef struct aes_key_st AES_KEY; |
| 43 |
| 44 | // AES_set_encrypt_key configures |aeskey| to encrypt with the |bits|-bit key, |
| 45 | // |key|. |key| must point to |bits|/8 bytes. It returns zero on success and a |
| 46 | // negative number if |bits| is an invalid AES key size. |
| 47 | // |
| 48 | // WARNING: this function breaks the usual return value convention. |
| 49 | OPENSSL_EXPORT int AES_set_encrypt_key(const uint8_t *key, unsigned bits, |
| 50 | AES_KEY *aeskey); |
| 51 |
| 52 | // AES_set_decrypt_key configures |aeskey| to decrypt with the |bits|-bit key, |
| 53 | // |key|. |key| must point to |bits|/8 bytes. It returns zero on success and a |
| 54 | // negative number if |bits| is an invalid AES key size. |
| 55 | // |
| 56 | // WARNING: this function breaks the usual return value convention. |
| 57 | OPENSSL_EXPORT int AES_set_decrypt_key(const uint8_t *key, unsigned bits, |
| 58 | AES_KEY *aeskey); |
| 59 |
| 60 | // AES_encrypt encrypts a single block from |in| to |out| with |key|. The |in| |
| 61 | // and |out| pointers may overlap. |
| 62 | OPENSSL_EXPORT void AES_encrypt(const uint8_t *in, uint8_t *out, |
| 63 | const AES_KEY *key); |
| 64 |
| 65 | // AES_decrypt decrypts a single block from |in| to |out| with |key|. The |in| |
| 66 | // and |out| pointers may overlap. |
| 67 | OPENSSL_EXPORT void AES_decrypt(const uint8_t *in, uint8_t *out, |
| 68 | const AES_KEY *key); |
| 69 |
| 70 |
| 71 | // Block cipher modes. |
| 72 |
| 73 | // AES_ctr128_encrypt encrypts (or decrypts, it's the same in CTR mode) |len| |
| 74 | // bytes from |in| to |out|. The |num| parameter must be set to zero on the |
| 75 | // first call and |ivec| will be incremented. This function may be called |
| 76 | // in-place with |in| equal to |out|, but otherwise the buffers may not |
| 77 | // partially overlap. A partial overlap may overwrite input data before it is |
| 78 | // read. |
| 79 | OPENSSL_EXPORT void AES_ctr128_encrypt(const uint8_t *in, uint8_t *out, |
| 80 | size_t len, const AES_KEY *key, |
| 81 | uint8_t ivec[AES_BLOCK_SIZE], |
| 82 | uint8_t ecount_buf[AES_BLOCK_SIZE], |
| 83 | unsigned int *num); |
| 84 |
| 85 | // AES_ecb_encrypt encrypts (or decrypts, if |enc| == |AES_DECRYPT|) a single, |
| 86 | // 16 byte block from |in| to |out|. This function may be called in-place with |
| 87 | // |in| equal to |out|, but otherwise the buffers may not partially overlap. A |
| 88 | // partial overlap may overwrite input data before it is read. |
| 89 | OPENSSL_EXPORT void AES_ecb_encrypt(const uint8_t *in, uint8_t *out, |
| 90 | const AES_KEY *key, const int enc); |
| 91 |
| 92 | // AES_cbc_encrypt encrypts (or decrypts, if |enc| == |AES_DECRYPT|) |len| |
| 93 | // bytes from |in| to |out|. The length must be a multiple of the block size. |
| 94 | // This function may be called in-place with |in| equal to |out|, but otherwise |
| 95 | // the buffers may not partially overlap. A partial overlap may overwrite input |
| 96 | // data before it is read. |
| 97 | OPENSSL_EXPORT void AES_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t len, |
| 98 | const AES_KEY *key, uint8_t *ivec, |
| 99 | const int enc); |
| 100 |
| 101 | // AES_ofb128_encrypt encrypts (or decrypts, it's the same in OFB mode) |len| |
| 102 | // bytes from |in| to |out|. The |num| parameter must be set to zero on the |
| 103 | // first call. This function may be called in-place with |in| equal to |out|, |
| 104 | // but otherwise the buffers may not partially overlap. A partial overlap may |
| 105 | // overwrite input data before it is read. |
| 106 | OPENSSL_EXPORT void AES_ofb128_encrypt(const uint8_t *in, uint8_t *out, |
| 107 | size_t len, const AES_KEY *key, |
| 108 | uint8_t *ivec, int *num); |
| 109 |
| 110 | // AES_cfb128_encrypt encrypts (or decrypts, if |enc| == |AES_DECRYPT|) |len| |
| 111 | // bytes from |in| to |out|. The |num| parameter must be set to zero on the |
| 112 | // first call. This function may be called in-place with |in| equal to |out|, |
| 113 | // but otherwise the buffers may not partially overlap. A partial overlap may |
| 114 | // overwrite input data before it is read. |
| 115 | OPENSSL_EXPORT void AES_cfb128_encrypt(const uint8_t *in, uint8_t *out, |
| 116 | size_t len, const AES_KEY *key, |
| 117 | uint8_t *ivec, int *num, int enc); |
| 118 |
| 119 |
| 120 | // AES key wrap. |
| 121 | // |
| 122 | // These functions implement AES Key Wrap mode, as defined in RFC 3394. They |
| 123 | // should never be used except to interoperate with existing systems that use |
| 124 | // this mode. |
| 125 |
| 126 | // AES_wrap_key performs AES key wrap on |in| which must be a multiple of 8 |
| 127 | // bytes. |iv| must point to an 8 byte value or be NULL to use the default IV. |
| 128 | // |key| must have been configured for encryption. On success, it writes |
| 129 | // |in_len| + 8 bytes to |out| and returns |in_len| + 8. Otherwise, it returns |
| 130 | // -1. |
| 131 | OPENSSL_EXPORT int AES_wrap_key(const AES_KEY *key, const uint8_t *iv, |
| 132 | uint8_t *out, const uint8_t *in, size_t in_len); |
| 133 |
| 134 | // AES_unwrap_key performs AES key unwrap on |in| which must be a multiple of 8 |
| 135 | // bytes. |iv| must point to an 8 byte value or be NULL to use the default IV. |
| 136 | // |key| must have been configured for decryption. On success, it writes |
| 137 | // |in_len| - 8 bytes to |out| and returns |in_len| - 8. Otherwise, it returns |
| 138 | // -1. |
| 139 | OPENSSL_EXPORT int AES_unwrap_key(const AES_KEY *key, const uint8_t *iv, |
| 140 | uint8_t *out, const uint8_t *in, |
| 141 | size_t in_len); |
| 142 |
| 143 |
| 144 | // AES key wrap with padding. |
| 145 | // |
| 146 | // These functions implement AES Key Wrap with Padding mode, as defined in RFC |
| 147 | // 5649. They should never be used except to interoperate with existing systems |
| 148 | // that use this mode. |
| 149 |
| 150 | // AES_wrap_key_padded performs a padded AES key wrap on |in| which must be |
| 151 | // between 1 and 2^32-1 bytes. |key| must have been configured for encryption. |
| 152 | // On success it writes at most |max_out| bytes of ciphertext to |out|, sets |
| 153 | // |*out_len| to the number of bytes written, and returns one. On failure it |
| 154 | // returns zero. To ensure success, set |max_out| to at least |in_len| + 15. |
| 155 | OPENSSL_EXPORT int AES_wrap_key_padded(const AES_KEY *key, uint8_t *out, |
| 156 | size_t *out_len, size_t max_out, |
| 157 | const uint8_t *in, size_t in_len); |
| 158 |
| 159 | // AES_unwrap_key_padded performs a padded AES key unwrap on |in| which must be |
| 160 | // a multiple of 8 bytes. |key| must have been configured for decryption. On |
| 161 | // success it writes at most |max_out| bytes to |out|, sets |*out_len| to the |
| 162 | // number of bytes written, and returns one. On failure it returns zero. Setting |
| 163 | // |max_out| to |in_len| is a sensible estimate. |
| 164 | OPENSSL_EXPORT int AES_unwrap_key_padded(const AES_KEY *key, uint8_t *out, |
| 165 | size_t *out_len, size_t max_out, |
| 166 | const uint8_t *in, size_t in_len); |
| 167 |
| 168 | OPENSSL_EXPORT void AES_ige_encrypt(const unsigned char *in, unsigned char *out, |
| 169 | size_t length, const AES_KEY *key, |
| 170 | unsigned char *ivec, const int enc); |
| 171 | OPENSSL_EXPORT void AES_bi_ige_encrypt(const unsigned char *in, unsigned char *out, |
| 172 | size_t length, const AES_KEY *key, |
| 173 | const AES_KEY *key2, const unsigned char *ivec, |
| 174 | const int enc); |
| 175 |
| 176 | #if defined(__cplusplus) |
| 177 | } // extern C |
| 178 | #endif |
| 179 |
| 180 | #endif // OPENSSL_HEADER_AES_H |
| 181 |