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