xref: /third_party/openssl/crypto/idea/i_cfb64.c (revision e1051a39)
1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci/*
11e1051a39Sopenharmony_ci * IDEA low level APIs are deprecated for public use, but still ok for internal
12e1051a39Sopenharmony_ci * use where we're using them to implement the higher level EVP interface, as is
13e1051a39Sopenharmony_ci * the case here.
14e1051a39Sopenharmony_ci */
15e1051a39Sopenharmony_ci#include "internal/deprecated.h"
16e1051a39Sopenharmony_ci
17e1051a39Sopenharmony_ci#include <openssl/idea.h>
18e1051a39Sopenharmony_ci#include "idea_local.h"
19e1051a39Sopenharmony_ci
20e1051a39Sopenharmony_ci/*
21e1051a39Sopenharmony_ci * The input and output encrypted as though 64bit cfb mode is being used.
22e1051a39Sopenharmony_ci * The extra state information to record how much of the 64bit block we have
23e1051a39Sopenharmony_ci * used is contained in *num;
24e1051a39Sopenharmony_ci */
25e1051a39Sopenharmony_ci
26e1051a39Sopenharmony_civoid IDEA_cfb64_encrypt(const unsigned char *in, unsigned char *out,
27e1051a39Sopenharmony_ci                        long length, IDEA_KEY_SCHEDULE *schedule,
28e1051a39Sopenharmony_ci                        unsigned char *ivec, int *num, int encrypt)
29e1051a39Sopenharmony_ci{
30e1051a39Sopenharmony_ci    register unsigned long v0, v1, t;
31e1051a39Sopenharmony_ci    register int n = *num;
32e1051a39Sopenharmony_ci    register long l = length;
33e1051a39Sopenharmony_ci    unsigned long ti[2];
34e1051a39Sopenharmony_ci    unsigned char *iv, c, cc;
35e1051a39Sopenharmony_ci
36e1051a39Sopenharmony_ci    if (n < 0) {
37e1051a39Sopenharmony_ci        *num = -1;
38e1051a39Sopenharmony_ci        return;
39e1051a39Sopenharmony_ci    }
40e1051a39Sopenharmony_ci
41e1051a39Sopenharmony_ci    iv = (unsigned char *)ivec;
42e1051a39Sopenharmony_ci    if (encrypt) {
43e1051a39Sopenharmony_ci        while (l--) {
44e1051a39Sopenharmony_ci            if (n == 0) {
45e1051a39Sopenharmony_ci                n2l(iv, v0);
46e1051a39Sopenharmony_ci                ti[0] = v0;
47e1051a39Sopenharmony_ci                n2l(iv, v1);
48e1051a39Sopenharmony_ci                ti[1] = v1;
49e1051a39Sopenharmony_ci                IDEA_encrypt((unsigned long *)ti, schedule);
50e1051a39Sopenharmony_ci                iv = (unsigned char *)ivec;
51e1051a39Sopenharmony_ci                t = ti[0];
52e1051a39Sopenharmony_ci                l2n(t, iv);
53e1051a39Sopenharmony_ci                t = ti[1];
54e1051a39Sopenharmony_ci                l2n(t, iv);
55e1051a39Sopenharmony_ci                iv = (unsigned char *)ivec;
56e1051a39Sopenharmony_ci            }
57e1051a39Sopenharmony_ci            c = *(in++) ^ iv[n];
58e1051a39Sopenharmony_ci            *(out++) = c;
59e1051a39Sopenharmony_ci            iv[n] = c;
60e1051a39Sopenharmony_ci            n = (n + 1) & 0x07;
61e1051a39Sopenharmony_ci        }
62e1051a39Sopenharmony_ci    } else {
63e1051a39Sopenharmony_ci        while (l--) {
64e1051a39Sopenharmony_ci            if (n == 0) {
65e1051a39Sopenharmony_ci                n2l(iv, v0);
66e1051a39Sopenharmony_ci                ti[0] = v0;
67e1051a39Sopenharmony_ci                n2l(iv, v1);
68e1051a39Sopenharmony_ci                ti[1] = v1;
69e1051a39Sopenharmony_ci                IDEA_encrypt((unsigned long *)ti, schedule);
70e1051a39Sopenharmony_ci                iv = (unsigned char *)ivec;
71e1051a39Sopenharmony_ci                t = ti[0];
72e1051a39Sopenharmony_ci                l2n(t, iv);
73e1051a39Sopenharmony_ci                t = ti[1];
74e1051a39Sopenharmony_ci                l2n(t, iv);
75e1051a39Sopenharmony_ci                iv = (unsigned char *)ivec;
76e1051a39Sopenharmony_ci            }
77e1051a39Sopenharmony_ci            cc = *(in++);
78e1051a39Sopenharmony_ci            c = iv[n];
79e1051a39Sopenharmony_ci            iv[n] = cc;
80e1051a39Sopenharmony_ci            *(out++) = c ^ cc;
81e1051a39Sopenharmony_ci            n = (n + 1) & 0x07;
82e1051a39Sopenharmony_ci        }
83e1051a39Sopenharmony_ci    }
84e1051a39Sopenharmony_ci    v0 = v1 = ti[0] = ti[1] = t = c = cc = 0;
85e1051a39Sopenharmony_ci    *num = n;
86e1051a39Sopenharmony_ci}
87