2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
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
10 #include <openssl/opensslconf.h>
11 #include <openssl/aes.h>
12 #include <openssl/params.h>
13 #include <openssl/core_numbers.h>
14 #include "internal/cryptlib.h"
15 #include "internal/modes_int.h"
17 #define IV_STATE_UNINITIALISED 0 /* initial state is not initialized */
18 #define IV_STATE_BUFFERED 1 /* iv has been copied to the iv buffer */
19 #define IV_STATE_COPIED 2 /* iv has been copied from the iv buffer */
20 #define IV_STATE_FINISHED 3 /* the iv has been used - so don't reuse it */
22 #define PROV_CIPHER_FUNC(type, name, args) typedef type (* OSSL_##name##_fn)args
24 typedef struct prov_aes_cipher_st PROV_AES_CIPHER;
26 typedef struct prov_aes_key_st {
37 /* Platform specific data */
40 #if defined(OPENSSL_CPUID_OBJ) && defined(__s390__)
45 * KM-AES parameter block - begin
46 * (see z/Architecture Principles of Operation >= SA22-7832-06)
51 /* KM-AES parameter block - end */
53 * KMO-AES/KMF-AES parameter block - begin
54 * (see z/Architecture Principles of Operation >= SA22-7832-08)
60 /* KMO-AES/KMF-AES parameter block - end */
65 #endif /* defined(OPENSSL_CPUID_OBJ) && defined(__s390__) */
68 /* The cipher functions we are going to use */
69 const PROV_AES_CIPHER *ciph;
71 /* The mode that we are using */
74 /* Set to 1 if we are encrypting or 0 otherwise */
77 unsigned char iv[AES_BLOCK_SIZE];
80 * num contains the number of bytes of |iv| which are valid for modes that
81 * manage partial blocks themselves.
85 /* Buffer of partial blocks processed via update calls */
86 unsigned char buf[AES_BLOCK_SIZE];
88 /* Number of bytes in buf */
95 /* Whether padding should be used or not */
99 struct prov_aes_cipher_st {
100 int (*init)(PROV_AES_KEY *dat, const uint8_t *key, size_t keylen);
101 int (*cipher)(PROV_AES_KEY *dat, uint8_t *out, const uint8_t *in,
105 #include "ciphers_gcm.h"
106 #include "ciphers_ccm.h"
108 const PROV_AES_CIPHER *PROV_AES_CIPHER_ecb(size_t keylen);
109 const PROV_AES_CIPHER *PROV_AES_CIPHER_cbc(size_t keylen);
110 const PROV_AES_CIPHER *PROV_AES_CIPHER_ofb(size_t keylen);
111 const PROV_AES_CIPHER *PROV_AES_CIPHER_cfb(size_t keylen);
112 const PROV_AES_CIPHER *PROV_AES_CIPHER_cfb1(size_t keylen);
113 const PROV_AES_CIPHER *PROV_AES_CIPHER_cfb8(size_t keylen);
114 const PROV_AES_CIPHER *PROV_AES_CIPHER_ctr(size_t keylen);
116 size_t fillblock(unsigned char *buf, size_t *buflen, size_t blocksize,
117 const unsigned char **in, size_t *inlen);
118 int trailingdata(unsigned char *buf, size_t *buflen, size_t blocksize,
119 const unsigned char **in, size_t *inlen);
120 void padblock(unsigned char *buf, size_t *buflen, size_t blocksize);
121 int unpadblock(unsigned char *buf, size_t *buflen, size_t blocksize);
123 OSSL_OP_cipher_gettable_params_fn cipher_default_gettable_params;
124 OSSL_OP_cipher_gettable_ctx_params_fn cipher_default_gettable_ctx_params;
125 OSSL_OP_cipher_settable_ctx_params_fn cipher_default_settable_ctx_params;
126 OSSL_OP_cipher_gettable_ctx_params_fn cipher_aead_gettable_ctx_params;
127 OSSL_OP_cipher_settable_ctx_params_fn cipher_aead_settable_ctx_params;
129 int cipher_default_get_params(OSSL_PARAM params[], int md, unsigned long flags,
130 int kbits, int blkbits, int ivbits);
132 #define IMPLEMENT_aead_cipher(alg, lc, UCMODE, flags, kbits, blkbits, ivbits) \
133 static OSSL_OP_cipher_get_params_fn alg##_##kbits##_##lc##_get_params; \
134 static int alg##_##kbits##_##lc##_get_params(OSSL_PARAM params[]) \
136 return cipher_default_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
137 flags, kbits, blkbits, ivbits); \
139 static OSSL_OP_cipher_newctx_fn alg##kbits##lc##_newctx; \
140 static void * alg##kbits##lc##_newctx(void *provctx) \
142 return alg##_##lc##_newctx(provctx, kbits); \
144 const OSSL_DISPATCH alg##kbits##lc##_functions[] = { \
145 { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))alg##kbits##lc##_newctx }, \
146 { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))alg##_##lc##_freectx }, \
147 { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void)) lc##_einit }, \
148 { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void)) lc##_dinit }, \
149 { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void)) lc##_stream_update }, \
150 { OSSL_FUNC_CIPHER_FINAL, (void (*)(void)) lc##_stream_final }, \
151 { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void)) lc##_cipher }, \
152 { OSSL_FUNC_CIPHER_GET_PARAMS, \
153 (void (*)(void)) alg##_##kbits##_##lc##_get_params }, \
154 { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
155 (void (*)(void)) lc##_get_ctx_params }, \
156 { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
157 (void (*)(void)) lc##_set_ctx_params }, \
158 { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
159 (void (*)(void))cipher_default_gettable_params }, \
160 { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
161 (void (*)(void))cipher_aead_gettable_ctx_params }, \
162 { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
163 (void (*)(void))cipher_aead_settable_ctx_params }, \