1/*
2 * Copyright 2019-2020 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/*
11 * IDEA low level APIs are deprecated for public use, but still ok for internal
12 * use where we're using them to implement the higher level EVP interface, as is
13 * the case here.
14 */
15#include "internal/deprecated.h"
16
17#include "cipher_idea.h"
18
19static int cipher_hw_idea_initkey(PROV_CIPHER_CTX *ctx,
20                                  const unsigned char *key, size_t keylen)
21{
22    PROV_IDEA_CTX *ictx =  (PROV_IDEA_CTX *)ctx;
23    IDEA_KEY_SCHEDULE *ks = &(ictx->ks.ks);
24
25    if (ctx->enc
26            || ctx->mode == EVP_CIPH_OFB_MODE
27            || ctx->mode == EVP_CIPH_CFB_MODE) {
28        IDEA_set_encrypt_key(key, ks);
29    } else {
30        IDEA_KEY_SCHEDULE tmp;
31
32        IDEA_set_encrypt_key(key, &tmp);
33        IDEA_set_decrypt_key(&tmp, ks);
34        OPENSSL_cleanse((unsigned char *)&tmp, sizeof(IDEA_KEY_SCHEDULE));
35    }
36    return 1;
37}
38
39# define PROV_CIPHER_HW_idea_mode_ex(mode, UCMODE, fname)                      \
40IMPLEMENT_CIPHER_HW_##UCMODE(mode, idea, PROV_IDEA_CTX, IDEA_KEY_SCHEDULE,     \
41                             fname)                                            \
42static const PROV_CIPHER_HW idea_##mode = {                                    \
43    cipher_hw_idea_initkey,                                                    \
44    cipher_hw_idea_##mode##_cipher                                             \
45};                                                                             \
46const PROV_CIPHER_HW *ossl_prov_cipher_hw_idea_##mode(size_t keybits)          \
47{                                                                              \
48    return &idea_##mode;                                                       \
49}
50
51# define PROV_CIPHER_HW_idea_mode(mode, UCMODE)                                \
52    PROV_CIPHER_HW_idea_mode_ex(mode, UCMODE, IDEA_##mode)
53
54PROV_CIPHER_HW_idea_mode(cbc, CBC)
55PROV_CIPHER_HW_idea_mode(ofb64, OFB)
56PROV_CIPHER_HW_idea_mode(cfb64, CFB)
57/*
58 * IDEA_ecb_encrypt() does not have a enc parameter  - so we create a macro
59 * that ignores this parameter when IMPLEMENT_CIPHER_HW_ecb() is called.
60 */
61#define IDEA2_ecb_encrypt(in, out, ks, enc) IDEA_ecb_encrypt(in, out, ks)
62
63PROV_CIPHER_HW_idea_mode_ex(ecb, ECB, IDEA2_ecb)
64