6c328c0deb8fc6575f7216a9e2a7bfde40d655d0
[openssl.git] / crypto / evp / cmeth_lib.c
1 /*
2  * Copyright 2015-2016 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
10 #include <string.h>
11
12 #include <openssl/evp.h>
13 #include "internal/evp_int.h"
14 #include "evp_locl.h"
15
16 EVP_CIPHER *EVP_CIPHER_meth_new(int cipher_type, int block_size, int key_len)
17 {
18     EVP_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_CIPHER));
19
20     if (cipher != NULL) {
21         cipher->nid = cipher_type;
22         cipher->block_size = block_size;
23         cipher->key_len = key_len;
24     }
25     return cipher;
26 }
27
28 EVP_CIPHER *EVP_CIPHER_meth_dup(const EVP_CIPHER *cipher)
29 {
30     EVP_CIPHER *to = EVP_CIPHER_meth_new(cipher->nid, cipher->block_size,
31                                          cipher->key_len);
32
33     if (to != NULL)
34         memcpy(to, cipher, sizeof(*to));
35     return to;
36 }
37
38 void EVP_CIPHER_meth_free(EVP_CIPHER *cipher)
39 {
40     OPENSSL_free(cipher);
41 }
42
43 int EVP_CIPHER_meth_set_iv_length(EVP_CIPHER *cipher, int iv_len)
44 {
45     cipher->iv_len = iv_len;
46     return 1;
47 }
48
49 int EVP_CIPHER_meth_set_flags(EVP_CIPHER *cipher, unsigned long flags)
50 {
51     cipher->flags = flags;
52     return 1;
53 }
54
55 int EVP_CIPHER_meth_set_impl_ctx_size(EVP_CIPHER *cipher, int ctx_size)
56 {
57     cipher->ctx_size = ctx_size;
58     return 1;
59 }
60
61 int EVP_CIPHER_meth_set_init(EVP_CIPHER *cipher,
62                              int (*init) (EVP_CIPHER_CTX *ctx,
63                                           const unsigned char *key,
64                                           const unsigned char *iv,
65                                           int enc))
66 {
67     cipher->init = init;
68     return 1;
69 }
70
71 int EVP_CIPHER_meth_set_do_cipher(EVP_CIPHER *cipher,
72                                   int (*do_cipher) (EVP_CIPHER_CTX *ctx,
73                                                     unsigned char *out,
74                                                     const unsigned char *in,
75                                                     size_t inl))
76 {
77     cipher->do_cipher = do_cipher;
78     return 1;
79 }
80
81 int EVP_CIPHER_meth_set_cleanup(EVP_CIPHER *cipher,
82                                 int (*cleanup) (EVP_CIPHER_CTX *))
83 {
84     cipher->cleanup = cleanup;
85     return 1;
86 }
87
88 int EVP_CIPHER_meth_set_set_asn1_params(EVP_CIPHER *cipher,
89                                         int (*set_asn1_parameters) (EVP_CIPHER_CTX *,
90                                                                     ASN1_TYPE *))
91 {
92     cipher->set_asn1_parameters = set_asn1_parameters;
93     return 1;
94 }
95
96 int EVP_CIPHER_meth_set_get_asn1_params(EVP_CIPHER *cipher,
97                                         int (*get_asn1_parameters) (EVP_CIPHER_CTX *,
98                                                                     ASN1_TYPE *))
99 {
100     cipher->get_asn1_parameters = get_asn1_parameters;
101     return 1;
102 }
103
104 int EVP_CIPHER_meth_set_ctrl(EVP_CIPHER *cipher,
105                              int (*ctrl) (EVP_CIPHER_CTX *, int type,
106                                           int arg, void *ptr))
107 {
108     cipher->ctrl = ctrl;
109     return 1;
110 }
111
112
113 int (*EVP_CIPHER_meth_get_init(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *ctx,
114                                                           const unsigned char *key,
115                                                           const unsigned char *iv,
116                                                           int enc)
117 {
118     return cipher->init;
119 }
120 int (*EVP_CIPHER_meth_get_do_cipher(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *ctx,
121                                                                unsigned char *out,
122                                                                const unsigned char *in,
123                                                                size_t inl)
124 {
125     return cipher->do_cipher;
126 }
127
128 int (*EVP_CIPHER_meth_get_cleanup(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *)
129 {
130     return cipher->cleanup;
131 }
132
133 int (*EVP_CIPHER_meth_get_set_asn1_params(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
134                                                                      ASN1_TYPE *)
135 {
136     return cipher->set_asn1_parameters;
137 }
138
139 int (*EVP_CIPHER_meth_get_get_asn1_params(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
140                                                                ASN1_TYPE *)
141 {
142     return cipher->get_asn1_parameters;
143 }
144
145 int (*EVP_CIPHER_meth_get_ctrl(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
146                                                           int type, int arg,
147                                                           void *ptr)
148 {
149     return cipher->ctrl;
150 }
151