]>
Commit | Line | Data |
---|---|---|
aab26e6f MC |
1 | /* |
2 | * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved. | |
3 | * | |
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 | |
8 | */ | |
9 | ||
a672a02a | 10 | #include <openssl/opensslconf.h> |
aab26e6f | 11 | #include <openssl/aes.h> |
a672a02a | 12 | #include <openssl/params.h> |
784883fc | 13 | #include <openssl/core_numbers.h> |
39147079 | 14 | #include "internal/cryptlib.h" |
a672a02a SL |
15 | #include "internal/modes_int.h" |
16 | ||
a672a02a SL |
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 */ | |
21 | ||
3bfe9005 SL |
22 | #define PROV_CIPHER_FUNC(type, name, args) typedef type (* OSSL_##name##_fn)args |
23 | ||
24 | typedef struct prov_aes_cipher_st PROV_AES_CIPHER; | |
25 | ||
aab26e6f MC |
26 | typedef struct prov_aes_key_st { |
27 | union { | |
39147079 | 28 | OSSL_UNION_ALIGN; |
aab26e6f MC |
29 | AES_KEY ks; |
30 | } ks; | |
31 | block128_f block; | |
32 | union { | |
33 | cbc128_f cbc; | |
34 | ctr128_f ctr; | |
35 | } stream; | |
36 | ||
64adf9aa MC |
37 | /* Platform specific data */ |
38 | union { | |
39 | int dummy; | |
40 | #if defined(OPENSSL_CPUID_OBJ) && defined(__s390__) | |
41 | struct { | |
42 | union { | |
39147079 | 43 | OSSL_UNION_ALIGN; |
64adf9aa MC |
44 | /*- |
45 | * KM-AES parameter block - begin | |
46 | * (see z/Architecture Principles of Operation >= SA22-7832-06) | |
47 | */ | |
48 | struct { | |
49 | unsigned char k[32]; | |
50 | } km; | |
51 | /* KM-AES parameter block - end */ | |
52 | /*- | |
53 | * KMO-AES/KMF-AES parameter block - begin | |
54 | * (see z/Architecture Principles of Operation >= SA22-7832-08) | |
55 | */ | |
56 | struct { | |
57 | unsigned char cv[16]; | |
58 | unsigned char k[32]; | |
59 | } kmo_kmf; | |
60 | /* KMO-AES/KMF-AES parameter block - end */ | |
61 | } param; | |
62 | unsigned int fc; | |
63 | int res; | |
64 | } s390x; | |
65 | #endif /* defined(OPENSSL_CPUID_OBJ) && defined(__s390__) */ | |
66 | } plat; | |
67 | ||
aab26e6f MC |
68 | /* The cipher functions we are going to use */ |
69 | const PROV_AES_CIPHER *ciph; | |
70 | ||
71 | /* The mode that we are using */ | |
72 | int mode; | |
73 | ||
74 | /* Set to 1 if we are encrypting or 0 otherwise */ | |
75 | int enc; | |
76 | ||
77 | unsigned char iv[AES_BLOCK_SIZE]; | |
78 | ||
79 | /* | |
80 | * num contains the number of bytes of |iv| which are valid for modes that | |
81 | * manage partial blocks themselves. | |
82 | */ | |
83 | size_t num; | |
84 | ||
85 | /* Buffer of partial blocks processed via update calls */ | |
86 | unsigned char buf[AES_BLOCK_SIZE]; | |
87 | ||
88 | /* Number of bytes in buf */ | |
89 | size_t bufsz; | |
90 | ||
91 | uint64_t flags; | |
92 | ||
93 | size_t keylen; | |
94 | ||
95 | /* Whether padding should be used or not */ | |
96 | unsigned int pad : 1; | |
97 | } PROV_AES_KEY; | |
98 | ||
99 | struct prov_aes_cipher_st { | |
3bfe9005 SL |
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, | |
aab26e6f MC |
102 | size_t inl); |
103 | }; | |
104 | ||
a672a02a | 105 | #include "ciphers_gcm.h" |
3bfe9005 | 106 | #include "ciphers_ccm.h" |
a672a02a | 107 | |
64adf9aa MC |
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); | |
aab26e6f MC |
115 | |
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); | |
784883fc SL |
122 | |
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; | |
128 | ||
129 | int cipher_default_get_params(OSSL_PARAM params[], int md, unsigned long flags, | |
130 | int kbits, int blkbits, int ivbits); | |
131 | ||
3bfe9005 SL |
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[]) \ | |
135 | { \ | |
136 | return cipher_default_get_params(params, EVP_CIPH_##UCMODE##_MODE, \ | |
137 | flags, kbits, blkbits, ivbits); \ | |
138 | } \ | |
139 | static OSSL_OP_cipher_newctx_fn alg##kbits##lc##_newctx; \ | |
140 | static void * alg##kbits##lc##_newctx(void *provctx) \ | |
141 | { \ | |
142 | return alg##_##lc##_newctx(provctx, kbits); \ | |
143 | } \ | |
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 }, \ | |
164 | { 0, NULL } \ | |
165 | } |