d722cc7a271bf060fd4500f0cce29640a2275900
[oweals/openssl.git] / providers / default / ciphers / cipher_idea_hw.c
1 /*
2  * Copyright 2019 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 "cipher_idea.h"
11
12 static int cipher_hw_idea_initkey(PROV_CIPHER_CTX *ctx,
13                                   const unsigned char *key, size_t keylen)
14 {
15     PROV_IDEA_CTX *ictx =  (PROV_IDEA_CTX *)ctx;
16     IDEA_KEY_SCHEDULE *ks = &(ictx->ks.ks);
17
18     if (ctx->enc
19             || ctx->mode == EVP_CIPH_OFB_MODE
20             || ctx->mode == EVP_CIPH_CFB_MODE) {
21         IDEA_set_encrypt_key(key, ks);
22     } else {
23         IDEA_KEY_SCHEDULE tmp;
24
25         IDEA_set_encrypt_key(key, &tmp);
26         IDEA_set_decrypt_key(&tmp, ks);
27         OPENSSL_cleanse((unsigned char *)&tmp, sizeof(IDEA_KEY_SCHEDULE));
28     }
29     return 1;
30 }
31
32 # define PROV_CIPHER_HW_idea_mode_ex(mode, UCMODE, fname)                      \
33 IMPLEMENT_CIPHER_HW_##UCMODE(mode, idea, PROV_IDEA_CTX, IDEA_KEY_SCHEDULE,     \
34                              fname)                                            \
35 static const PROV_CIPHER_HW idea_##mode = {                                    \
36     cipher_hw_idea_initkey,                                                    \
37     cipher_hw_idea_##mode##_cipher                                             \
38 };                                                                             \
39 const PROV_CIPHER_HW *PROV_CIPHER_HW_idea_##mode(size_t keybits)               \
40 {                                                                              \
41     return &idea_##mode;                                                       \
42 }
43
44 # define PROV_CIPHER_HW_idea_mode(mode, UCMODE)                                \
45     PROV_CIPHER_HW_idea_mode_ex(mode, UCMODE, IDEA_##mode)
46
47 PROV_CIPHER_HW_idea_mode(cbc, CBC)
48 PROV_CIPHER_HW_idea_mode(ofb64, OFB)
49 PROV_CIPHER_HW_idea_mode(cfb64, CFB)
50 /*
51  * IDEA_ecb_encrypt() does not have a enc parameter  - so we create a macro
52  * that ignores this parameter when IMPLEMENT_CIPHER_HW_ecb() is called.
53  */
54 #define IDEA2_ecb_encrypt(in, out, ks, enc) IDEA_ecb_encrypt(in, out, ks)
55
56 PROV_CIPHER_HW_idea_mode_ex(ecb, ECB, IDEA2_ecb)