| 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 | #include <openssl/aead.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 |
| 19 | #include <openssl/cipher.h> |
| 20 | #include <openssl/crypto.h> |
| 21 | #include <openssl/err.h> |
| 22 | #include <openssl/sha.h> |
| 23 |
| 24 | #include "../fipsmodule/aes/internal.h" |
| 25 | #include "../fipsmodule/cipher/internal.h" |
| 26 |
| 27 |
| 28 | #define EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN SHA256_DIGEST_LENGTH |
| 29 | #define EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN 12 |
| 30 |
| 31 | struct aead_aes_ctr_hmac_sha256_ctx { |
| 32 | union { |
| 33 | double align; |
| 34 | AES_KEY ks; |
| 35 | } ks; |
| 36 | ctr128_f ctr; |
| 37 | block128_f block; |
| 38 | SHA256_CTX inner_init_state; |
| 39 | SHA256_CTX outer_init_state; |
| 40 | }; |
| 41 |
| 42 | static_assert(sizeof(((EVP_AEAD_CTX *)NULL)->state) >= |
| 43 | sizeof(struct aead_aes_ctr_hmac_sha256_ctx), |
| 44 | "AEAD state is too small"); |
| 45 | static_assert(alignof(union evp_aead_ctx_st_state) >= |
| 46 | alignof(struct aead_aes_ctr_hmac_sha256_ctx), |
| 47 | "AEAD state has insufficient alignment"); |
| 48 |
| 49 | static void hmac_init(SHA256_CTX *out_inner, SHA256_CTX *out_outer, |
| 50 | const uint8_t hmac_key[32]) { |
| 51 | static const size_t hmac_key_len = 32; |
| 52 | uint8_t block[SHA256_CBLOCK]; |
| 53 | OPENSSL_memcpy(block, hmac_key, hmac_key_len); |
| 54 | OPENSSL_memset(block + hmac_key_len, 0x36, sizeof(block) - hmac_key_len); |
| 55 |
| 56 | unsigned i; |
| 57 | for (i = 0; i < hmac_key_len; i++) { |
| 58 | block[i] ^= 0x36; |
| 59 | } |
| 60 |
| 61 | SHA256_Init(out_inner); |
| 62 | SHA256_Update(out_inner, block, sizeof(block)); |
| 63 |
| 64 | OPENSSL_memset(block + hmac_key_len, 0x5c, sizeof(block) - hmac_key_len); |
| 65 | for (i = 0; i < hmac_key_len; i++) { |
| 66 | block[i] ^= (0x36 ^ 0x5c); |
| 67 | } |
| 68 |
| 69 | SHA256_Init(out_outer); |
| 70 | SHA256_Update(out_outer, block, sizeof(block)); |
| 71 | } |
| 72 |
| 73 | static int aead_aes_ctr_hmac_sha256_init(EVP_AEAD_CTX *ctx, const uint8_t *key, |
| 74 | size_t key_len, size_t tag_len) { |
| 75 | struct aead_aes_ctr_hmac_sha256_ctx *aes_ctx = |
| 76 | (struct aead_aes_ctr_hmac_sha256_ctx *)&ctx->state; |
| 77 | static const size_t hmac_key_len = 32; |
| 78 |
| 79 | if (key_len < hmac_key_len) { |
| 80 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH); |
| 81 | return 0; // EVP_AEAD_CTX_init should catch this. |
| 82 | } |
| 83 |
| 84 | const size_t aes_key_len = key_len - hmac_key_len; |
| 85 | if (aes_key_len != 16 && aes_key_len != 32) { |
| 86 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH); |
| 87 | return 0; // EVP_AEAD_CTX_init should catch this. |
| 88 | } |
| 89 |
| 90 | if (tag_len == EVP_AEAD_DEFAULT_TAG_LENGTH) { |
| 91 | tag_len = EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN; |
| 92 | } |
| 93 |
| 94 | if (tag_len > EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN) { |
| 95 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TAG_TOO_LARGE); |
| 96 | return 0; |
| 97 | } |
| 98 |
| 99 | aes_ctx->ctr = |
| 100 | aes_ctr_set_key(&aes_ctx->ks.ks, NULL, &aes_ctx->block, key, aes_key_len); |
| 101 | ctx->tag_len = tag_len; |
| 102 | hmac_init(&aes_ctx->inner_init_state, &aes_ctx->outer_init_state, |
| 103 | key + aes_key_len); |
| 104 |
| 105 | return 1; |
| 106 | } |
| 107 |
| 108 | static void aead_aes_ctr_hmac_sha256_cleanup(EVP_AEAD_CTX *ctx) {} |
| 109 |
| 110 | static void hmac_update_uint64(SHA256_CTX *sha256, uint64_t value) { |
| 111 | unsigned i; |
| 112 | uint8_t bytes[8]; |
| 113 |
| 114 | for (i = 0; i < sizeof(bytes); i++) { |
| 115 | bytes[i] = value & 0xff; |
| 116 | value >>= 8; |
| 117 | } |
| 118 | SHA256_Update(sha256, bytes, sizeof(bytes)); |
| 119 | } |
| 120 |
| 121 | static void hmac_calculate(uint8_t out[SHA256_DIGEST_LENGTH], |
| 122 | const SHA256_CTX *inner_init_state, |
| 123 | const SHA256_CTX *outer_init_state, |
| 124 | const uint8_t *ad, size_t ad_len, |
| 125 | const uint8_t *nonce, const uint8_t *ciphertext, |
| 126 | size_t ciphertext_len) { |
| 127 | SHA256_CTX sha256; |
| 128 | OPENSSL_memcpy(&sha256, inner_init_state, sizeof(sha256)); |
| 129 | hmac_update_uint64(&sha256, ad_len); |
| 130 | hmac_update_uint64(&sha256, ciphertext_len); |
| 131 | SHA256_Update(&sha256, nonce, EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN); |
| 132 | SHA256_Update(&sha256, ad, ad_len); |
| 133 |
| 134 | // Pad with zeros to the end of the SHA-256 block. |
| 135 | const unsigned num_padding = |
| 136 | (SHA256_CBLOCK - ((sizeof(uint64_t)*2 + |
| 137 | EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN + ad_len) % |
| 138 | SHA256_CBLOCK)) % |
| 139 | SHA256_CBLOCK; |
| 140 | uint8_t padding[SHA256_CBLOCK]; |
| 141 | OPENSSL_memset(padding, 0, num_padding); |
| 142 | SHA256_Update(&sha256, padding, num_padding); |
| 143 |
| 144 | SHA256_Update(&sha256, ciphertext, ciphertext_len); |
| 145 |
| 146 | uint8_t inner_digest[SHA256_DIGEST_LENGTH]; |
| 147 | SHA256_Final(inner_digest, &sha256); |
| 148 |
| 149 | OPENSSL_memcpy(&sha256, outer_init_state, sizeof(sha256)); |
| 150 | SHA256_Update(&sha256, inner_digest, sizeof(inner_digest)); |
| 151 | SHA256_Final(out, &sha256); |
| 152 | } |
| 153 |
| 154 | static void aead_aes_ctr_hmac_sha256_crypt( |
| 155 | const struct aead_aes_ctr_hmac_sha256_ctx *aes_ctx, uint8_t *out, |
| 156 | const uint8_t *in, size_t len, const uint8_t *nonce) { |
| 157 | // Since the AEAD operation is one-shot, keeping a buffer of unused keystream |
| 158 | // bytes is pointless. However, |CRYPTO_ctr128_encrypt_ctr32| requires it. |
| 159 | uint8_t partial_block_buffer[AES_BLOCK_SIZE]; |
| 160 | unsigned partial_block_offset = 0; |
| 161 | OPENSSL_memset(partial_block_buffer, 0, sizeof(partial_block_buffer)); |
| 162 |
| 163 | uint8_t counter[AES_BLOCK_SIZE]; |
| 164 | OPENSSL_memcpy(counter, nonce, EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN); |
| 165 | OPENSSL_memset(counter + EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN, 0, 4); |
| 166 |
| 167 | CRYPTO_ctr128_encrypt_ctr32(in, out, len, &aes_ctx->ks.ks, counter, |
| 168 | partial_block_buffer, &partial_block_offset, |
| 169 | aes_ctx->ctr); |
| 170 | } |
| 171 |
| 172 | static int aead_aes_ctr_hmac_sha256_seal_scatter( |
| 173 | const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag, |
| 174 | size_t *out_tag_len, size_t max_out_tag_len, const uint8_t *nonce, |
| 175 | size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *extra_in, |
| 176 | size_t extra_in_len, const uint8_t *ad, size_t ad_len) { |
| 177 | const struct aead_aes_ctr_hmac_sha256_ctx *aes_ctx = |
| 178 | (struct aead_aes_ctr_hmac_sha256_ctx *) &ctx->state; |
| 179 | const uint64_t in_len_64 = in_len; |
| 180 |
| 181 | if (in_len_64 >= (UINT64_C(1) << 32) * AES_BLOCK_SIZE) { |
| 182 | // This input is so large it would overflow the 32-bit block counter. |
| 183 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
| 184 | return 0; |
| 185 | } |
| 186 |
| 187 | if (max_out_tag_len < ctx->tag_len) { |
| 188 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
| 189 | return 0; |
| 190 | } |
| 191 |
| 192 | if (nonce_len != EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN) { |
| 193 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE); |
| 194 | return 0; |
| 195 | } |
| 196 |
| 197 | aead_aes_ctr_hmac_sha256_crypt(aes_ctx, out, in, in_len, nonce); |
| 198 |
| 199 | uint8_t hmac_result[SHA256_DIGEST_LENGTH]; |
| 200 | hmac_calculate(hmac_result, &aes_ctx->inner_init_state, |
| 201 | &aes_ctx->outer_init_state, ad, ad_len, nonce, out, in_len); |
| 202 | OPENSSL_memcpy(out_tag, hmac_result, ctx->tag_len); |
| 203 | *out_tag_len = ctx->tag_len; |
| 204 |
| 205 | return 1; |
| 206 | } |
| 207 |
| 208 | static int aead_aes_ctr_hmac_sha256_open_gather( |
| 209 | const EVP_AEAD_CTX *ctx, uint8_t *out, const uint8_t *nonce, |
| 210 | size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *in_tag, |
| 211 | size_t in_tag_len, const uint8_t *ad, size_t ad_len) { |
| 212 | const struct aead_aes_ctr_hmac_sha256_ctx *aes_ctx = |
| 213 | (struct aead_aes_ctr_hmac_sha256_ctx *) &ctx->state; |
| 214 |
| 215 | if (in_tag_len != ctx->tag_len) { |
| 216 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
| 217 | return 0; |
| 218 | } |
| 219 |
| 220 | if (nonce_len != EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN) { |
| 221 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE); |
| 222 | return 0; |
| 223 | } |
| 224 |
| 225 | uint8_t hmac_result[SHA256_DIGEST_LENGTH]; |
| 226 | hmac_calculate(hmac_result, &aes_ctx->inner_init_state, |
| 227 | &aes_ctx->outer_init_state, ad, ad_len, nonce, in, |
| 228 | in_len); |
| 229 | if (CRYPTO_memcmp(hmac_result, in_tag, ctx->tag_len) != 0) { |
| 230 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
| 231 | return 0; |
| 232 | } |
| 233 |
| 234 | aead_aes_ctr_hmac_sha256_crypt(aes_ctx, out, in, in_len, nonce); |
| 235 |
| 236 | return 1; |
| 237 | } |
| 238 |
| 239 | static const EVP_AEAD aead_aes_128_ctr_hmac_sha256 = { |
| 240 | 16 /* AES key */ + 32 /* HMAC key */, |
| 241 | 12, // nonce length |
| 242 | EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN, // overhead |
| 243 | EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN, // max tag length |
| 244 | 0, // seal_scatter_supports_extra_in |
| 245 |
| 246 | aead_aes_ctr_hmac_sha256_init, |
| 247 | NULL /* init_with_direction */, |
| 248 | aead_aes_ctr_hmac_sha256_cleanup, |
| 249 | NULL /* open */, |
| 250 | aead_aes_ctr_hmac_sha256_seal_scatter, |
| 251 | aead_aes_ctr_hmac_sha256_open_gather, |
| 252 | NULL /* get_iv */, |
| 253 | NULL /* tag_len */, |
| 254 | }; |
| 255 |
| 256 | static const EVP_AEAD aead_aes_256_ctr_hmac_sha256 = { |
| 257 | 32 /* AES key */ + 32 /* HMAC key */, |
| 258 | 12, // nonce length |
| 259 | EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN, // overhead |
| 260 | EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN, // max tag length |
| 261 | 0, // seal_scatter_supports_extra_in |
| 262 |
| 263 | aead_aes_ctr_hmac_sha256_init, |
| 264 | NULL /* init_with_direction */, |
| 265 | aead_aes_ctr_hmac_sha256_cleanup, |
| 266 | NULL /* open */, |
| 267 | aead_aes_ctr_hmac_sha256_seal_scatter, |
| 268 | aead_aes_ctr_hmac_sha256_open_gather, |
| 269 | NULL /* get_iv */, |
| 270 | NULL /* tag_len */, |
| 271 | }; |
| 272 |
| 273 | const EVP_AEAD *EVP_aead_aes_128_ctr_hmac_sha256(void) { |
| 274 | return &aead_aes_128_ctr_hmac_sha256; |
| 275 | } |
| 276 |
| 277 | const EVP_AEAD *EVP_aead_aes_256_ctr_hmac_sha256(void) { |
| 278 | return &aead_aes_256_ctr_hmac_sha256; |
| 279 | } |
| 280 |