2 * Copyright (c) 2006 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/types.h>
45 #include <evp-hcrypto.h>
47 #include <krb5-types.h>
66 aes_init(EVP_CIPHER_CTX
*ctx
,
67 const unsigned char * key
,
68 const unsigned char * iv
,
71 AES_KEY
*k
= ctx
->cipher_data
;
73 AES_set_encrypt_key(key
, ctx
->cipher
->key_len
* 8, k
);
75 AES_set_decrypt_key(key
, ctx
->cipher
->key_len
* 8, k
);
80 aes_do_cipher(EVP_CIPHER_CTX
*ctx
,
82 const unsigned char *in
,
85 AES_KEY
*k
= ctx
->cipher_data
;
86 AES_cbc_encrypt(in
, out
, size
, k
, ctx
->iv
, ctx
->encrypt
);
91 * The AES-128 cipher type (hcrypto)
93 * @return the AES-128 EVP_CIPHER pointer.
95 * @ingroup hcrypto_evp
99 EVP_hcrypto_aes_128_cbc(void)
101 static const EVP_CIPHER aes_128_cbc
= {
121 * The AES-192 cipher type (hcrypto)
123 * @return the AES-192 EVP_CIPHER pointer.
125 * @ingroup hcrypto_evp
129 EVP_hcrypto_aes_192_cbc(void)
131 static const EVP_CIPHER aes_192_cbc
= {
150 * The AES-256 cipher type (hcrypto)
152 * @return the AES-256 EVP_CIPHER pointer.
154 * @ingroup hcrypto_evp
158 EVP_hcrypto_aes_256_cbc(void)
160 static const EVP_CIPHER aes_256_cbc
= {
179 * The message digest SHA256 - hcrypto
181 * @return the message digest type.
183 * @ingroup hcrypto_evp
187 EVP_hcrypto_sha256(void)
189 static const struct hc_evp_md sha256
= {
193 (hc_evp_md_init
)SHA256_Init
,
194 (hc_evp_md_update
)SHA256_Update
,
195 (hc_evp_md_final
)SHA256_Final
,
202 * The message digest SHA1 - hcrypto
204 * @return the message digest type.
206 * @ingroup hcrypto_evp
210 EVP_hcrypto_sha1(void)
212 static const struct hc_evp_md sha1
= {
216 (hc_evp_md_init
)SHA1_Init
,
217 (hc_evp_md_update
)SHA1_Update
,
218 (hc_evp_md_final
)SHA1_Final
,
225 * The message digest MD5 - hcrypto
227 * @return the message digest type.
229 * @ingroup hcrypto_evp
233 EVP_hcrypto_md5(void)
235 static const struct hc_evp_md md5
= {
239 (hc_evp_md_init
)MD5_Init
,
240 (hc_evp_md_update
)MD5_Update
,
241 (hc_evp_md_final
)MD5_Final
,
248 * The message digest MD4 - hcrypto
250 * @return the message digest type.
252 * @ingroup hcrypto_evp
256 EVP_hcrypto_md4(void)
258 static const struct hc_evp_md md4
= {
262 (hc_evp_md_init
)MD4_Init
,
263 (hc_evp_md_update
)MD4_Update
,
264 (hc_evp_md_final
)MD4_Final
,
271 * The message digest MD2 - hcrypto
273 * @return the message digest type.
275 * @ingroup hcrypto_evp
279 EVP_hcrypto_md2(void)
281 static const struct hc_evp_md md2
= {
285 (hc_evp_md_init
)MD2_Init
,
286 (hc_evp_md_update
)MD2_Update
,
287 (hc_evp_md_final
)MD2_Final
,
298 des_cbc_init(EVP_CIPHER_CTX
*ctx
,
299 const unsigned char * key
,
300 const unsigned char * iv
,
303 DES_key_schedule
*k
= ctx
->cipher_data
;
305 memcpy(&deskey
, key
, sizeof(deskey
));
306 DES_set_key_unchecked(&deskey
, k
);
311 des_cbc_do_cipher(EVP_CIPHER_CTX
*ctx
,
313 const unsigned char *in
,
316 DES_key_schedule
*k
= ctx
->cipher_data
;
317 DES_cbc_encrypt(in
, out
, size
,
318 k
, (DES_cblock
*)ctx
->iv
, ctx
->encrypt
);
323 * The DES cipher type
325 * @return the DES-CBC EVP_CIPHER pointer.
327 * @ingroup hcrypto_evp
331 EVP_hcrypto_des_cbc(void)
333 static const EVP_CIPHER des_cbc
= {
342 sizeof(DES_key_schedule
),
355 struct des_ede3_cbc
{
356 DES_key_schedule ks
[3];
360 des_ede3_cbc_init(EVP_CIPHER_CTX
*ctx
,
361 const unsigned char * key
,
362 const unsigned char * iv
,
365 struct des_ede3_cbc
*k
= ctx
->cipher_data
;
368 memcpy(&deskey
, key
, sizeof(deskey
));
369 DES_set_odd_parity(&deskey
);
370 DES_set_key_unchecked(&deskey
, &k
->ks
[0]);
372 memcpy(&deskey
, key
+ 8, sizeof(deskey
));
373 DES_set_odd_parity(&deskey
);
374 DES_set_key_unchecked(&deskey
, &k
->ks
[1]);
376 memcpy(&deskey
, key
+ 16, sizeof(deskey
));
377 DES_set_odd_parity(&deskey
);
378 DES_set_key_unchecked(&deskey
, &k
->ks
[2]);
384 des_ede3_cbc_do_cipher(EVP_CIPHER_CTX
*ctx
,
386 const unsigned char *in
,
389 struct des_ede3_cbc
*k
= ctx
->cipher_data
;
390 DES_ede3_cbc_encrypt(in
, out
, size
,
391 &k
->ks
[0], &k
->ks
[1], &k
->ks
[2],
392 (DES_cblock
*)ctx
->iv
, ctx
->encrypt
);
397 * The tripple DES cipher type - hcrypto
399 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
401 * @ingroup hcrypto_evp
405 EVP_hcrypto_des_ede3_cbc(void)
407 static const EVP_CIPHER des_ede3_cbc
= {
414 des_ede3_cbc_do_cipher
,
416 sizeof(struct des_ede3_cbc
),
422 return &des_ede3_cbc
;
430 unsigned int maximum_effective_key
;
435 rc2_init(EVP_CIPHER_CTX
*ctx
,
436 const unsigned char * key
,
437 const unsigned char * iv
,
440 struct rc2_cbc
*k
= ctx
->cipher_data
;
441 k
->maximum_effective_key
= EVP_CIPHER_CTX_key_length(ctx
) * 8;
443 EVP_CIPHER_CTX_key_length(ctx
),
445 k
->maximum_effective_key
);
450 rc2_do_cipher(EVP_CIPHER_CTX
*ctx
,
452 const unsigned char *in
,
455 struct rc2_cbc
*k
= ctx
->cipher_data
;
456 RC2_cbc_encrypt(in
, out
, size
, &k
->key
, ctx
->iv
, ctx
->encrypt
);
461 * The RC2 cipher type - hcrypto
463 * @return the RC2 EVP_CIPHER pointer.
465 * @ingroup hcrypto_evp
469 EVP_hcrypto_rc2_cbc(void)
471 static const EVP_CIPHER rc2_cbc
= {
476 EVP_CIPH_CBC_MODE
|EVP_CIPH_VARIABLE_LENGTH
,
480 sizeof(struct rc2_cbc
),
490 * The RC2-40 cipher type
492 * @return the RC2-40 EVP_CIPHER pointer.
494 * @ingroup hcrypto_evp
498 EVP_hcrypto_rc2_40_cbc(void)
500 static const EVP_CIPHER rc2_40_cbc
= {
509 sizeof(struct rc2_cbc
),
519 * The RC2-64 cipher type
521 * @return the RC2-64 EVP_CIPHER pointer.
523 * @ingroup hcrypto_evp
527 EVP_hcrypto_rc2_64_cbc(void)
529 static const EVP_CIPHER rc2_64_cbc
= {
538 sizeof(struct rc2_cbc
),
548 camellia_init(EVP_CIPHER_CTX
*ctx
,
549 const unsigned char * key
,
550 const unsigned char * iv
,
553 CAMELLIA_KEY
*k
= ctx
->cipher_data
;
554 k
->bits
= ctx
->cipher
->key_len
* 8;
555 CAMELLIA_set_key(key
, ctx
->cipher
->key_len
* 8, k
);
560 camellia_do_cipher(EVP_CIPHER_CTX
*ctx
,
562 const unsigned char *in
,
565 CAMELLIA_KEY
*k
= ctx
->cipher_data
;
566 CAMELLIA_cbc_encrypt(in
, out
, size
, k
, ctx
->iv
, ctx
->encrypt
);
571 * The Camellia-128 cipher type - hcrypto
573 * @return the Camellia-128 EVP_CIPHER pointer.
575 * @ingroup hcrypto_evp
579 EVP_hcrypto_camellia_128_cbc(void)
581 static const EVP_CIPHER cipher
= {
590 sizeof(CAMELLIA_KEY
),
600 * The Camellia-198 cipher type - hcrypto
602 * @return the Camellia-198 EVP_CIPHER pointer.
604 * @ingroup hcrypto_evp
608 EVP_hcrypto_camellia_192_cbc(void)
610 static const EVP_CIPHER cipher
= {
619 sizeof(CAMELLIA_KEY
),
629 * The Camellia-256 cipher type - hcrypto
631 * @return the Camellia-256 EVP_CIPHER pointer.
633 * @ingroup hcrypto_evp
637 EVP_hcrypto_camellia_256_cbc(void)
639 static const EVP_CIPHER cipher
= {
648 sizeof(CAMELLIA_KEY
),
658 rc4_init(EVP_CIPHER_CTX
*ctx
,
659 const unsigned char *key
,
660 const unsigned char *iv
,
663 RC4_KEY
*k
= ctx
->cipher_data
;
664 RC4_set_key(k
, ctx
->key_len
, key
);
669 rc4_do_cipher(EVP_CIPHER_CTX
*ctx
,
671 const unsigned char *in
,
674 RC4_KEY
*k
= ctx
->cipher_data
;
675 RC4(k
, size
, in
, out
);
680 EVP_hcrypto_rc4(void)
682 static const EVP_CIPHER rc4
= {
687 EVP_CIPH_STREAM_CIPHER
|EVP_CIPH_VARIABLE_LENGTH
,
702 EVP_hcrypto_rc4_40(void)
704 static const EVP_CIPHER rc4_40
= {
709 EVP_CIPH_STREAM_CIPHER
|EVP_CIPH_VARIABLE_LENGTH
,