2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "internal/cryptlib.h"
12 #include <openssl/evp.h>
13 #include <openssl/objects.h>
14 #include "internal/evp_int.h"
17 int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
)
21 if (c
->cipher
->set_asn1_parameters
!= NULL
)
22 ret
= c
->cipher
->set_asn1_parameters(c
, type
);
23 else if (c
->cipher
->flags
& EVP_CIPH_FLAG_DEFAULT_ASN1
) {
24 switch (EVP_CIPHER_CTX_mode(c
)) {
25 case EVP_CIPH_WRAP_MODE
:
26 if (EVP_CIPHER_CTX_nid(c
) == NID_id_smime_alg_CMS3DESwrap
)
27 ASN1_TYPE_set(type
, V_ASN1_NULL
, NULL
);
31 case EVP_CIPH_GCM_MODE
:
32 case EVP_CIPH_CCM_MODE
:
33 case EVP_CIPH_XTS_MODE
:
34 case EVP_CIPH_OCB_MODE
:
39 ret
= EVP_CIPHER_set_asn1_iv(c
, type
);
46 int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
)
50 if (c
->cipher
->get_asn1_parameters
!= NULL
)
51 ret
= c
->cipher
->get_asn1_parameters(c
, type
);
52 else if (c
->cipher
->flags
& EVP_CIPH_FLAG_DEFAULT_ASN1
) {
53 switch (EVP_CIPHER_CTX_mode(c
)) {
55 case EVP_CIPH_WRAP_MODE
:
59 case EVP_CIPH_GCM_MODE
:
60 case EVP_CIPH_CCM_MODE
:
61 case EVP_CIPH_XTS_MODE
:
62 case EVP_CIPH_OCB_MODE
:
67 ret
= EVP_CIPHER_get_asn1_iv(c
, type
);
75 int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
)
81 l
= EVP_CIPHER_CTX_iv_length(c
);
82 OPENSSL_assert(l
<= sizeof(c
->iv
));
83 i
= ASN1_TYPE_get_octetstring(type
, c
->oiv
, l
);
87 memcpy(c
->iv
, c
->oiv
, l
);
92 int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
)
98 j
= EVP_CIPHER_CTX_iv_length(c
);
99 OPENSSL_assert(j
<= sizeof(c
->iv
));
100 i
= ASN1_TYPE_set_octetstring(type
, c
->oiv
, j
);
105 /* Convert the various cipher NIDs and dummies to a proper OID NID */
106 int EVP_CIPHER_type(const EVP_CIPHER
*ctx
)
110 nid
= EVP_CIPHER_nid(ctx
);
125 case NID_aes_128_cfb128
:
126 case NID_aes_128_cfb8
:
127 case NID_aes_128_cfb1
:
129 return NID_aes_128_cfb128
;
131 case NID_aes_192_cfb128
:
132 case NID_aes_192_cfb8
:
133 case NID_aes_192_cfb1
:
135 return NID_aes_192_cfb128
;
137 case NID_aes_256_cfb128
:
138 case NID_aes_256_cfb8
:
139 case NID_aes_256_cfb1
:
141 return NID_aes_256_cfb128
;
147 return NID_des_cfb64
;
149 case NID_des_ede3_cfb64
:
150 case NID_des_ede3_cfb8
:
151 case NID_des_ede3_cfb1
:
153 return NID_des_cfb64
;
156 /* Check it has an OID and it is valid */
157 otmp
= OBJ_nid2obj(nid
);
158 if (OBJ_get0_data(otmp
) == NULL
)
160 ASN1_OBJECT_free(otmp
);
165 int EVP_CIPHER_block_size(const EVP_CIPHER
*e
)
167 return e
->block_size
;
170 int EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX
*ctx
)
172 return ctx
->cipher
->block_size
;
175 int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER
*e
)
180 int EVP_Cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
181 const unsigned char *in
, unsigned int inl
)
183 return ctx
->cipher
->do_cipher(ctx
, out
, in
, inl
);
186 const EVP_CIPHER
*EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX
*ctx
)
191 int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX
*ctx
)
196 unsigned long EVP_CIPHER_flags(const EVP_CIPHER
*cipher
)
198 return cipher
->flags
;
201 void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX
*ctx
)
203 return ctx
->app_data
;
206 void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX
*ctx
, void *data
)
208 ctx
->app_data
= data
;
211 void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX
*ctx
)
213 return ctx
->cipher_data
;
216 void *EVP_CIPHER_CTX_set_cipher_data(EVP_CIPHER_CTX
*ctx
, void *cipher_data
)
218 void *old_cipher_data
;
220 old_cipher_data
= ctx
->cipher_data
;
221 ctx
->cipher_data
= cipher_data
;
223 return old_cipher_data
;
226 int EVP_CIPHER_iv_length(const EVP_CIPHER
*cipher
)
228 return cipher
->iv_len
;
231 int EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX
*ctx
)
233 return ctx
->cipher
->iv_len
;
236 const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX
*ctx
)
241 const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX
*ctx
)
246 unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX
*ctx
)
251 unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX
*ctx
)
256 int EVP_CIPHER_CTX_num(const EVP_CIPHER_CTX
*ctx
)
261 void EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX
*ctx
, int num
)
266 int EVP_CIPHER_key_length(const EVP_CIPHER
*cipher
)
268 return cipher
->key_len
;
271 int EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX
*ctx
)
276 int EVP_CIPHER_nid(const EVP_CIPHER
*cipher
)
281 int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX
*ctx
)
283 return ctx
->cipher
->nid
;
286 int EVP_MD_block_size(const EVP_MD
*md
)
288 return md
->block_size
;
291 int EVP_MD_type(const EVP_MD
*md
)
296 int EVP_MD_pkey_type(const EVP_MD
*md
)
298 return md
->pkey_type
;
301 int EVP_MD_size(const EVP_MD
*md
)
304 EVPerr(EVP_F_EVP_MD_SIZE
, EVP_R_MESSAGE_DIGEST_IS_NULL
);
310 unsigned long EVP_MD_flags(const EVP_MD
*md
)
315 EVP_MD
*EVP_MD_meth_new(int md_type
, int pkey_type
)
317 EVP_MD
*md
= OPENSSL_zalloc(sizeof(*md
));
321 md
->pkey_type
= pkey_type
;
325 EVP_MD
*EVP_MD_meth_dup(const EVP_MD
*md
)
327 EVP_MD
*to
= EVP_MD_meth_new(md
->type
, md
->pkey_type
);
330 memcpy(to
, md
, sizeof(*to
));
333 void EVP_MD_meth_free(EVP_MD
*md
)
337 int EVP_MD_meth_set_input_blocksize(EVP_MD
*md
, int blocksize
)
339 md
->block_size
= blocksize
;
342 int EVP_MD_meth_set_result_size(EVP_MD
*md
, int resultsize
)
344 md
->md_size
= resultsize
;
347 int EVP_MD_meth_set_app_datasize(EVP_MD
*md
, int datasize
)
349 md
->ctx_size
= datasize
;
352 int EVP_MD_meth_set_flags(EVP_MD
*md
, unsigned long flags
)
357 int EVP_MD_meth_set_init(EVP_MD
*md
, int (*init
)(EVP_MD_CTX
*ctx
))
362 int EVP_MD_meth_set_update(EVP_MD
*md
, int (*update
)(EVP_MD_CTX
*ctx
,
369 int EVP_MD_meth_set_final(EVP_MD
*md
, int (*final
)(EVP_MD_CTX
*ctx
,
375 int EVP_MD_meth_set_copy(EVP_MD
*md
, int (*copy
)(EVP_MD_CTX
*to
,
376 const EVP_MD_CTX
*from
))
381 int EVP_MD_meth_set_cleanup(EVP_MD
*md
, int (*cleanup
)(EVP_MD_CTX
*ctx
))
383 md
->cleanup
= cleanup
;
386 int EVP_MD_meth_set_ctrl(EVP_MD
*md
, int (*ctrl
)(EVP_MD_CTX
*ctx
, int cmd
,
393 int EVP_MD_meth_get_input_blocksize(const EVP_MD
*md
)
395 return md
->block_size
;
397 int EVP_MD_meth_get_result_size(const EVP_MD
*md
)
401 int EVP_MD_meth_get_app_datasize(const EVP_MD
*md
)
405 unsigned long EVP_MD_meth_get_flags(const EVP_MD
*md
)
407 return md
->block_size
;
409 int (*EVP_MD_meth_get_init(const EVP_MD
*md
))(EVP_MD_CTX
*ctx
)
413 int (*EVP_MD_meth_get_update(const EVP_MD
*md
))(EVP_MD_CTX
*ctx
,
419 int (*EVP_MD_meth_get_final(const EVP_MD
*md
))(EVP_MD_CTX
*ctx
,
424 int (*EVP_MD_meth_get_copy(const EVP_MD
*md
))(EVP_MD_CTX
*to
,
425 const EVP_MD_CTX
*from
)
429 int (*EVP_MD_meth_get_cleanup(const EVP_MD
*md
))(EVP_MD_CTX
*ctx
)
433 int (*EVP_MD_meth_get_ctrl(const EVP_MD
*md
))(EVP_MD_CTX
*ctx
, int cmd
,
439 const EVP_MD
*EVP_MD_CTX_md(const EVP_MD_CTX
*ctx
)
446 EVP_PKEY_CTX
*EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX
*ctx
)
451 void *EVP_MD_CTX_md_data(const EVP_MD_CTX
*ctx
)
456 int (*EVP_MD_CTX_update_fn(EVP_MD_CTX
*ctx
))(EVP_MD_CTX
*ctx
,
457 const void *data
, size_t count
)
462 void EVP_MD_CTX_set_update_fn(EVP_MD_CTX
*ctx
,
463 int (*update
) (EVP_MD_CTX
*ctx
,
464 const void *data
, size_t count
))
466 ctx
->update
= update
;
469 void EVP_MD_CTX_set_flags(EVP_MD_CTX
*ctx
, int flags
)
474 void EVP_MD_CTX_clear_flags(EVP_MD_CTX
*ctx
, int flags
)
476 ctx
->flags
&= ~flags
;
479 int EVP_MD_CTX_test_flags(const EVP_MD_CTX
*ctx
, int flags
)
481 return (ctx
->flags
& flags
);
484 void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX
*ctx
, int flags
)
489 void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX
*ctx
, int flags
)
491 ctx
->flags
&= ~flags
;
494 int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX
*ctx
, int flags
)
496 return (ctx
->flags
& flags
);