1 // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
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 #ifndef OPENSSL_HEADER_CRYPTO_FIPSMODULE_CIPHER_INTERNAL_H
16 #define OPENSSL_HEADER_CRYPTO_FIPSMODULE_CIPHER_INTERNAL_H
17 
18 #include <openssl/base.h>
19 
20 #include <openssl/aead.h>
21 #include <openssl/aes.h>
22 
23 #include "../../internal.h"
24 #include "../aes/internal.h"
25 
26 #if defined(__cplusplus)
27 extern "C" {
28 #endif
29 
30 
31 // EVP_CIPH_MODE_MASK contains the bits of |flags| that represent the mode.
32 #define EVP_CIPH_MODE_MASK 0x3f
33 
34 // EVP_AEAD represents a specific AEAD algorithm.
35 struct evp_aead_st {
36   uint8_t key_len;
37   uint8_t nonce_len;
38   uint8_t overhead;
39   uint8_t max_tag_len;
40   int seal_scatter_supports_extra_in;
41 
42   // init initialises an |EVP_AEAD_CTX|. If this call returns zero then
43   // |cleanup| will not be called for that context.
44   int (*init)(EVP_AEAD_CTX *, const uint8_t *key, size_t key_len,
45               size_t tag_len);
46   int (*init_with_direction)(EVP_AEAD_CTX *, const uint8_t *key, size_t key_len,
47                              size_t tag_len, enum evp_aead_direction_t dir);
48   void (*cleanup)(EVP_AEAD_CTX *);
49 
50   int (*open)(const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len,
51               size_t max_out_len, const uint8_t *nonce, size_t nonce_len,
52               const uint8_t *in, size_t in_len, const uint8_t *ad,
53               size_t ad_len);
54 
55   int (*seal_scatter)(const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag,
56                       size_t *out_tag_len, size_t max_out_tag_len,
57                       const uint8_t *nonce, size_t nonce_len, const uint8_t *in,
58                       size_t in_len, const uint8_t *extra_in,
59                       size_t extra_in_len, const uint8_t *ad, size_t ad_len);
60 
61   int (*open_gather)(const EVP_AEAD_CTX *ctx, uint8_t *out,
62                      const uint8_t *nonce, size_t nonce_len, const uint8_t *in,
63                      size_t in_len, const uint8_t *in_tag, size_t in_tag_len,
64                      const uint8_t *ad, size_t ad_len);
65 
66   int (*get_iv)(const EVP_AEAD_CTX *ctx, const uint8_t **out_iv,
67                 size_t *out_len);
68 
69   size_t (*tag_len)(const EVP_AEAD_CTX *ctx, size_t in_Len,
70                     size_t extra_in_len);
71 };
72 
73 struct evp_cipher_st {
74   // type contains a NID identifying the cipher. (e.g. NID_aes_128_gcm.)
75   int nid;
76 
77   // block_size contains the block size, in bytes, of the cipher, or 1 for a
78   // stream cipher.
79   unsigned block_size;
80 
81   // key_len contains the key size, in bytes, for the cipher. If the cipher
82   // takes a variable key size then this contains the default size.
83   unsigned key_len;
84 
85   // iv_len contains the IV size, in bytes, or zero if inapplicable.
86   unsigned iv_len;
87 
88   // ctx_size contains the size, in bytes, of the per-key context for this
89   // cipher.
90   unsigned ctx_size;
91 
92   // flags contains the OR of a number of flags. See |EVP_CIPH_*|.
93   uint32_t flags;
94 
95   int (*init)(EVP_CIPHER_CTX *ctx, const uint8_t *key, const uint8_t *iv,
96               int enc);
97 
98   int (*cipher)(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
99                 size_t inl);
100 
101   // cleanup, if non-NULL, releases memory associated with the context. It is
102   // called if |EVP_CTRL_INIT| succeeds. Note that |init| may not have been
103   // called at this point.
104   void (*cleanup)(EVP_CIPHER_CTX *);
105 
106   int (*ctrl)(EVP_CIPHER_CTX *, int type, int arg, void *ptr);
107 };
108 
109 #if defined(__cplusplus)
110 }  // extern C
111 #endif
112 
113 #endif  // OPENSSL_HEADER_CRYPTO_FIPSMODULE_CIPHER_INTERNAL_H
114