| 1 | #ifndef AWS_CAL_SYMMETRIC_CIPHER_H |
| 2 | #define AWS_CAL_SYMMETRIC_CIPHER_H |
| 3 | /** |
| 4 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 5 | * SPDX-License-Identifier: Apache-2.0. |
| 6 | */ |
| 7 | #include <aws/cal/cal.h> |
| 8 | #include <aws/common/byte_buf.h> |
| 9 | |
| 10 | AWS_PUSH_SANE_WARNING_LEVEL |
| 11 | |
| 12 | #define AWS_AES_256_CIPHER_BLOCK_SIZE 16 |
| 13 | #define AWS_AES_256_KEY_BIT_LEN 256 |
| 14 | #define AWS_AES_256_KEY_BYTE_LEN (AWS_AES_256_KEY_BIT_LEN / 8) |
| 15 | |
| 16 | struct aws_symmetric_cipher; |
| 17 | |
| 18 | typedef struct aws_symmetric_cipher *(aws_aes_cbc_256_new_fn)(struct aws_allocator *allocator, |
| 19 | const struct aws_byte_cursor *key, |
| 20 | const struct aws_byte_cursor *iv); |
| 21 | |
| 22 | typedef struct aws_symmetric_cipher *(aws_aes_ctr_256_new_fn)(struct aws_allocator *allocator, |
| 23 | const struct aws_byte_cursor *key, |
| 24 | const struct aws_byte_cursor *iv); |
| 25 | |
| 26 | typedef struct aws_symmetric_cipher *(aws_aes_gcm_256_new_fn)(struct aws_allocator *allocator, |
| 27 | const struct aws_byte_cursor *key, |
| 28 | const struct aws_byte_cursor *iv, |
| 29 | const struct aws_byte_cursor *aad); |
| 30 | |
| 31 | typedef struct aws_symmetric_cipher *(aws_aes_keywrap_256_new_fn)(struct aws_allocator *allocator, |
| 32 | const struct aws_byte_cursor *key); |
| 33 | |
| 34 | enum aws_symmetric_cipher_state { |
| 35 | AWS_SYMMETRIC_CIPHER_READY, |
| 36 | AWS_SYMMETRIC_CIPHER_FINALIZED, |
| 37 | AWS_SYMMETRIC_CIPHER_ERROR, |
| 38 | }; |
| 39 | |
| 40 | AWS_EXTERN_C_BEGIN |
| 41 | |
| 42 | /** |
| 43 | * Creates an instance of AES CBC with 256-bit key. |
| 44 | * If key and iv are NULL, they will be generated internally. |
| 45 | * You can get the generated key and iv back by calling: |
| 46 | * |
| 47 | * aws_symmetric_cipher_get_key() and |
| 48 | * aws_symmetric_cipher_get_initialization_vector() |
| 49 | * |
| 50 | * respectively. |
| 51 | * |
| 52 | * If they are set, that key and iv will be copied internally and used by the cipher. |
| 53 | * |
| 54 | * Returns NULL on failure. You can check aws_last_error() to get the error code indicating the failure cause. |
| 55 | */ |
| 56 | AWS_CAL_API struct aws_symmetric_cipher *aws_aes_cbc_256_new( |
| 57 | struct aws_allocator *allocator, |
| 58 | const struct aws_byte_cursor *key, |
| 59 | const struct aws_byte_cursor *iv); |
| 60 | |
| 61 | /** |
| 62 | * Creates an instance of AES CTR with 256-bit key. |
| 63 | * If key and iv are NULL, they will be generated internally. |
| 64 | * You can get the generated key and iv back by calling: |
| 65 | * |
| 66 | * aws_symmetric_cipher_get_key() and |
| 67 | * aws_symmetric_cipher_get_initialization_vector() |
| 68 | * |
| 69 | * respectively. |
| 70 | * |
| 71 | * If they are set, that key and iv will be copied internally and used by the cipher. |
| 72 | * |
| 73 | * Returns NULL on failure. You can check aws_last_error() to get the error code indicating the failure cause. |
| 74 | */ |
| 75 | AWS_CAL_API struct aws_symmetric_cipher *aws_aes_ctr_256_new( |
| 76 | struct aws_allocator *allocator, |
| 77 | const struct aws_byte_cursor *key, |
| 78 | const struct aws_byte_cursor *iv); |
| 79 | |
| 80 | /** |
| 81 | * Creates an instance of AES GCM with 256-bit key. |
| 82 | * If key, iv are NULL, they will be generated internally. |
| 83 | * You can get the generated key and iv back by calling: |
| 84 | * |
| 85 | * aws_symmetric_cipher_get_key() and |
| 86 | * aws_symmetric_cipher_get_initialization_vector() |
| 87 | * |
| 88 | * respectively. |
| 89 | * |
| 90 | * If aad is set it will be copied and applied to the cipher. |
| 91 | * |
| 92 | * If they are set, that key and iv will be copied internally and used by the cipher. |
| 93 | * |
| 94 | * For decryption purposes tag can be provided via aws_symmetric_cipher_set_tag method. |
| 95 | * Note: for decrypt operations, tag must be provided before first decrypt is called. |
| 96 | * (this is a windows bcrypt limitations, but for consistency sake same limitation is extended to other platforms) |
| 97 | * Tag generated during encryption can be retrieved using aws_symmetric_cipher_get_tag method |
| 98 | * after finalize is called. |
| 99 | * |
| 100 | * Returns NULL on failure. You can check aws_last_error() to get the error code indicating the failure cause. |
| 101 | */ |
| 102 | AWS_CAL_API struct aws_symmetric_cipher *aws_aes_gcm_256_new( |
| 103 | struct aws_allocator *allocator, |
| 104 | const struct aws_byte_cursor *key, |
| 105 | const struct aws_byte_cursor *iv, |
| 106 | const struct aws_byte_cursor *aad); |
| 107 | |
| 108 | /** |
| 109 | * Creates an instance of AES Keywrap with 256-bit key. |
| 110 | * If key is NULL, it will be generated internally. |
| 111 | * You can get the generated key back by calling: |
| 112 | * |
| 113 | * aws_symmetric_cipher_get_key() |
| 114 | * |
| 115 | * If key is set, that key will be copied internally and used by the cipher. |
| 116 | * |
| 117 | * Returns NULL on failure. You can check aws_last_error() to get the error code indicating the failure cause. |
| 118 | */ |
| 119 | AWS_CAL_API struct aws_symmetric_cipher *aws_aes_keywrap_256_new( |
| 120 | struct aws_allocator *allocator, |
| 121 | const struct aws_byte_cursor *key); |
| 122 | |
| 123 | /** |
| 124 | * Cleans up internal resources and state for cipher and then deallocates it. |
| 125 | */ |
| 126 | AWS_CAL_API void aws_symmetric_cipher_destroy(struct aws_symmetric_cipher *cipher); |
| 127 | |
| 128 | /** |
| 129 | * Encrypts the value in to_encrypt and writes the encrypted data into out. |
| 130 | * If out is dynamic it will be expanded. If it is not, and out is not large enough to handle |
| 131 | * the encrypted output, the call will fail. If you're trying to optimize to use a stack based array |
| 132 | * or something, make sure it's at least as large as the size of to_encrypt + an extra BLOCK to account for |
| 133 | * padding etc... |
| 134 | * |
| 135 | * returns AWS_OP_SUCCESS on success. Call aws_last_error() to determine the failure cause if it returns |
| 136 | * AWS_OP_ERR; |
| 137 | */ |
| 138 | AWS_CAL_API int aws_symmetric_cipher_encrypt( |
| 139 | struct aws_symmetric_cipher *cipher, |
| 140 | struct aws_byte_cursor to_encrypt, |
| 141 | struct aws_byte_buf *out); |
| 142 | |
| 143 | /** |
| 144 | * Decrypts the value in to_decrypt and writes the decrypted data into out. |
| 145 | * If out is dynamic it will be expanded. If it is not, and out is not large enough to handle |
| 146 | * the decrypted output, the call will fail. If you're trying to optimize to use a stack based array |
| 147 | * or something, make sure it's at least as large as the size of to_decrypt + an extra BLOCK to account for |
| 148 | * padding etc... |
| 149 | * |
| 150 | * returns AWS_OP_SUCCESS on success. Call aws_last_error() to determine the failure cause if it returns |
| 151 | * AWS_OP_ERR; |
| 152 | */ |
| 153 | AWS_CAL_API int aws_symmetric_cipher_decrypt( |
| 154 | struct aws_symmetric_cipher *cipher, |
| 155 | struct aws_byte_cursor to_decrypt, |
| 156 | struct aws_byte_buf *out); |
| 157 | |
| 158 | /** |
| 159 | * Encrypts any remaining data that was reserved for final padding, loads GMACs etc... and if there is any |
| 160 | * writes any remaining encrypted data to out. If out is dynamic it will be expanded. If it is not, and |
| 161 | * out is not large enough to handle the decrypted output, the call will fail. If you're trying to optimize |
| 162 | * to use a stack based array or something, make sure it's at least as large as the size of 2 BLOCKs to account for |
| 163 | * padding etc... |
| 164 | * |
| 165 | * After invoking this function, you MUST call aws_symmetric_cipher_reset() before invoking any encrypt/decrypt |
| 166 | * operations on this cipher again. |
| 167 | * |
| 168 | * returns AWS_OP_SUCCESS on success. Call aws_last_error() to determine the failure cause if it returns |
| 169 | * AWS_OP_ERR; |
| 170 | */ |
| 171 | AWS_CAL_API int aws_symmetric_cipher_finalize_encryption(struct aws_symmetric_cipher *cipher, struct aws_byte_buf *out); |
| 172 | |
| 173 | /** |
| 174 | * Decrypts any remaining data that was reserved for final padding, loads GMACs etc... and if there is any |
| 175 | * writes any remaining decrypted data to out. If out is dynamic it will be expanded. If it is not, and |
| 176 | * out is not large enough to handle the decrypted output, the call will fail. If you're trying to optimize |
| 177 | * to use a stack based array or something, make sure it's at least as large as the size of 2 BLOCKs to account for |
| 178 | * padding etc... |
| 179 | * |
| 180 | * After invoking this function, you MUST call aws_symmetric_cipher_reset() before invoking any encrypt/decrypt |
| 181 | * operations on this cipher again. |
| 182 | * |
| 183 | * returns AWS_OP_SUCCESS on success. Call aws_last_error() to determine the failure cause if it returns |
| 184 | * AWS_OP_ERR; |
| 185 | */ |
| 186 | AWS_CAL_API int aws_symmetric_cipher_finalize_decryption(struct aws_symmetric_cipher *cipher, struct aws_byte_buf *out); |
| 187 | |
| 188 | /** |
| 189 | * Resets the cipher state for starting a new encrypt or decrypt operation. Note encrypt/decrypt cannot be mixed on the |
| 190 | * same cipher without a call to reset in between them. However, this leaves the key, iv etc... materials setup for |
| 191 | * immediate reuse. |
| 192 | * Note: GCM tag is not preserved between operations. If you intend to do encrypt followed directly by decrypt, make |
| 193 | * sure to make a copy of tag before reseting the cipher and pass that copy for decryption. |
| 194 | * |
| 195 | * Warning: In most cases it's a really bad idea to reset a cipher and perform another operation using that cipher. |
| 196 | * Key and IV should not be reused for different operations. Instead of reseting the cipher, destroy the cipher |
| 197 | * and create new one with a new key/iv pair. Use reset at your own risk, and only after careful consideration. |
| 198 | * |
| 199 | * returns AWS_OP_SUCCESS on success. Call aws_last_error() to determine the failure cause if it returns |
| 200 | * AWS_OP_ERR; |
| 201 | */ |
| 202 | AWS_CAL_API int aws_symmetric_cipher_reset(struct aws_symmetric_cipher *cipher); |
| 203 | |
| 204 | /** |
| 205 | * Gets the current GMAC tag. If not AES GCM, this function will just return an empty cursor. |
| 206 | * The memory in this cursor is unsafe as it refers to the internal buffer. |
| 207 | * This was done because the use case doesn't require fetching these during an |
| 208 | * encryption or decryption operation and it dramatically simplifies the API. |
| 209 | * Only use this function between other calls to this API as any function call can alter the value of this tag. |
| 210 | * |
| 211 | * If you need to access it in a different pattern, copy the values to your own buffer first. |
| 212 | */ |
| 213 | AWS_CAL_API struct aws_byte_cursor aws_symmetric_cipher_get_tag(const struct aws_symmetric_cipher *cipher); |
| 214 | |
| 215 | /** |
| 216 | * Sets the GMAC tag on the cipher. Does nothing for ciphers that do not support tag. |
| 217 | */ |
| 218 | AWS_CAL_API void aws_symmetric_cipher_set_tag(struct aws_symmetric_cipher *cipher, struct aws_byte_cursor tag); |
| 219 | |
| 220 | /** |
| 221 | * Gets the original initialization vector as a cursor. |
| 222 | * The memory in this cursor is unsafe as it refers to the internal buffer. |
| 223 | * This was done because the use case doesn't require fetching these during an |
| 224 | * encryption or decryption operation and it dramatically simplifies the API. |
| 225 | * |
| 226 | * Unlike some other fields, this value does not change after the inital construction of the cipher. |
| 227 | * |
| 228 | * For some algorithms, such as AES Keywrap, this will return an empty cursor. |
| 229 | */ |
| 230 | AWS_CAL_API struct aws_byte_cursor aws_symmetric_cipher_get_initialization_vector( |
| 231 | const struct aws_symmetric_cipher *cipher); |
| 232 | |
| 233 | /** |
| 234 | * Gets the original key. |
| 235 | * |
| 236 | * The memory in this cursor is unsafe as it refers to the internal buffer. |
| 237 | * This was done because the use case doesn't require fetching these during an |
| 238 | * encryption or decryption operation and it dramatically simplifies the API. |
| 239 | * |
| 240 | * Unlike some other fields, this value does not change after the inital construction of the cipher. |
| 241 | */ |
| 242 | AWS_CAL_API struct aws_byte_cursor aws_symmetric_cipher_get_key(const struct aws_symmetric_cipher *cipher); |
| 243 | |
| 244 | /** |
| 245 | * Returns true if the state of the cipher is good, and otherwise returns false. |
| 246 | * Most operations, other than aws_symmetric_cipher_reset() will fail if this function is returning false. |
| 247 | * aws_symmetric_cipher_reset() will reset the state to a good state if possible. |
| 248 | */ |
| 249 | AWS_CAL_API bool aws_symmetric_cipher_is_good(const struct aws_symmetric_cipher *cipher); |
| 250 | |
| 251 | /** |
| 252 | * Retuns the current state of the cipher. Ther state of the cipher can be ready for use, finalized, or has encountered |
| 253 | * an error. if the cipher is in a finished or error state, it must be reset before further use. |
| 254 | */ |
| 255 | AWS_CAL_API enum aws_symmetric_cipher_state aws_symmetric_cipher_get_state(const struct aws_symmetric_cipher *cipher); |
| 256 | |
| 257 | AWS_EXTERN_C_END |
| 258 | AWS_POP_SANE_WARNING_LEVEL |
| 259 | #endif /* AWS_CAL_SYMMETRIC_CIPHER_H */ |