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