Home | History | Annotate | Line # | Download | only in evp
      1      1.1  christos /*
      2      1.1  christos  * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
      3      1.1  christos  *
      4      1.1  christos  * Licensed under the Apache License 2.0 (the "License").  You may not use
      5      1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      6      1.1  christos  * in the file LICENSE in the source distribution or at
      7      1.1  christos  * https://www.openssl.org/source/license.html
      8      1.1  christos  */
      9      1.1  christos 
     10      1.1  christos /*
     11      1.1  christos  * IDEA low level APIs are deprecated for public use, but still ok for internal
     12      1.1  christos  * use where we're using them to implement the higher level EVP interface, as is
     13      1.1  christos  * the case here.
     14      1.1  christos  */
     15      1.1  christos #include "internal/deprecated.h"
     16      1.1  christos 
     17      1.1  christos #include <stdio.h>
     18      1.1  christos #include "internal/cryptlib.h"
     19      1.1  christos 
     20      1.1  christos #ifndef OPENSSL_NO_IDEA
     21  1.1.1.2  christos #include <openssl/evp.h>
     22  1.1.1.2  christos #include <openssl/objects.h>
     23  1.1.1.2  christos #include "crypto/evp.h"
     24  1.1.1.2  christos #include <openssl/idea.h>
     25  1.1.1.2  christos #include "evp_local.h"
     26      1.1  christos 
     27      1.1  christos /* Can't use IMPLEMENT_BLOCK_CIPHER because IDEA_ecb_encrypt is different */
     28      1.1  christos 
     29      1.1  christos typedef struct {
     30      1.1  christos     IDEA_KEY_SCHEDULE ks;
     31      1.1  christos } EVP_IDEA_KEY;
     32      1.1  christos 
     33      1.1  christos static int idea_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
     34  1.1.1.2  christos     const unsigned char *iv, int enc);
     35      1.1  christos 
     36      1.1  christos /*
     37      1.1  christos  * NB IDEA_ecb_encrypt doesn't take an 'encrypt' argument so we treat it as a
     38      1.1  christos  * special case
     39      1.1  christos  */
     40      1.1  christos 
     41      1.1  christos static int idea_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
     42  1.1.1.2  christos     const unsigned char *in, size_t inl)
     43      1.1  christos {
     44      1.1  christos     BLOCK_CIPHER_ecb_loop()
     45      1.1  christos         IDEA_ecb_encrypt(in + i, out + i, &EVP_C_DATA(EVP_IDEA_KEY, ctx)->ks);
     46      1.1  christos     return 1;
     47      1.1  christos }
     48      1.1  christos 
     49      1.1  christos BLOCK_CIPHER_func_cbc(idea, IDEA, EVP_IDEA_KEY, ks)
     50  1.1.1.2  christos     BLOCK_CIPHER_func_ofb(idea, IDEA, 64, EVP_IDEA_KEY, ks)
     51  1.1.1.2  christos         BLOCK_CIPHER_func_cfb(idea, IDEA, 64, EVP_IDEA_KEY, ks)
     52      1.1  christos 
     53  1.1.1.2  christos             BLOCK_CIPHER_defs(idea, IDEA_KEY_SCHEDULE, NID_idea, 8, 16, 8, 64,
     54  1.1.1.2  christos                 0, idea_init_key, NULL,
     55  1.1.1.2  christos                 EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv, NULL)
     56      1.1  christos 
     57  1.1.1.2  christos                 static int idea_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
     58  1.1.1.2  christos                     const unsigned char *iv, int enc)
     59      1.1  christos {
     60      1.1  christos     if (!enc) {
     61      1.1  christos         if (EVP_CIPHER_CTX_get_mode(ctx) == EVP_CIPH_OFB_MODE)
     62      1.1  christos             enc = 1;
     63      1.1  christos         else if (EVP_CIPHER_CTX_get_mode(ctx) == EVP_CIPH_CFB_MODE)
     64      1.1  christos             enc = 1;
     65      1.1  christos     }
     66      1.1  christos     if (enc)
     67      1.1  christos         IDEA_set_encrypt_key(key, &EVP_C_DATA(EVP_IDEA_KEY, ctx)->ks);
     68      1.1  christos     else {
     69      1.1  christos         IDEA_KEY_SCHEDULE tmp;
     70      1.1  christos 
     71      1.1  christos         IDEA_set_encrypt_key(key, &tmp);
     72      1.1  christos         IDEA_set_decrypt_key(&tmp, &EVP_C_DATA(EVP_IDEA_KEY, ctx)->ks);
     73      1.1  christos         OPENSSL_cleanse((unsigned char *)&tmp, sizeof(IDEA_KEY_SCHEDULE));
     74      1.1  christos     }
     75      1.1  christos     return 1;
     76      1.1  christos }
     77      1.1  christos 
     78      1.1  christos #endif
     79