xref: /netbsd-src/crypto/external/bsd/openssl/dist/crypto/idea/i_cfb64.c (revision 8fbed61efdd901c0e09614c9f45356aeeab23fe3)
15af53050Schristos /*
2*8fbed61eSchristos  * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3a89c9211Schristos  *
4*8fbed61eSchristos  * Licensed under the Apache License 2.0 (the "License").  You may not use
55af53050Schristos  * this file except in compliance with the License.  You can obtain a copy
65af53050Schristos  * in the file LICENSE in the source distribution or at
75af53050Schristos  * https://www.openssl.org/source/license.html
8a89c9211Schristos  */
9a89c9211Schristos 
10*8fbed61eSchristos /*
11*8fbed61eSchristos  * IDEA low level APIs are deprecated for public use, but still ok for internal
12*8fbed61eSchristos  * use where we're using them to implement the higher level EVP interface, as is
13*8fbed61eSchristos  * the case here.
14*8fbed61eSchristos  */
15*8fbed61eSchristos #include "internal/deprecated.h"
16*8fbed61eSchristos 
17a89c9211Schristos #include <openssl/idea.h>
1852629741Schristos #include "idea_local.h"
19a89c9211Schristos 
209cef71b6Sspz /*
219cef71b6Sspz  * The input and output encrypted as though 64bit cfb mode is being used.
229cef71b6Sspz  * The extra state information to record how much of the 64bit block we have
239cef71b6Sspz  * used is contained in *num;
24a89c9211Schristos  */
25a89c9211Schristos 
IDEA_cfb64_encrypt(const unsigned char * in,unsigned char * out,long length,IDEA_KEY_SCHEDULE * schedule,unsigned char * ivec,int * num,int encrypt)265af53050Schristos void IDEA_cfb64_encrypt(const unsigned char *in, unsigned char *out,
27a89c9211Schristos                         long length, IDEA_KEY_SCHEDULE *schedule,
28a89c9211Schristos                         unsigned char *ivec, int *num, int encrypt)
29a89c9211Schristos {
3049d46fa3Schristos     register IDEA_INT v0, v1, t;
31a89c9211Schristos     register int n = *num;
32a89c9211Schristos     register long l = length;
3349d46fa3Schristos     IDEA_INT ti[2];
34a89c9211Schristos     unsigned char *iv, c, cc;
35a89c9211Schristos 
36*8fbed61eSchristos     if (n < 0) {
37*8fbed61eSchristos         *num = -1;
38*8fbed61eSchristos         return;
39*8fbed61eSchristos     }
40*8fbed61eSchristos 
41a89c9211Schristos     iv = (unsigned char *)ivec;
429cef71b6Sspz     if (encrypt) {
439cef71b6Sspz         while (l--) {
449cef71b6Sspz             if (n == 0) {
459cef71b6Sspz                 n2l(iv, v0);
469cef71b6Sspz                 ti[0] = v0;
479cef71b6Sspz                 n2l(iv, v1);
489cef71b6Sspz                 ti[1] = v1;
495af53050Schristos                 IDEA_encrypt(ti, schedule);
50a89c9211Schristos                 iv = (unsigned char *)ivec;
519cef71b6Sspz                 t = ti[0];
529cef71b6Sspz                 l2n(t, iv);
539cef71b6Sspz                 t = ti[1];
549cef71b6Sspz                 l2n(t, iv);
55a89c9211Schristos                 iv = (unsigned char *)ivec;
56a89c9211Schristos             }
57a89c9211Schristos             c = *(in++) ^ iv[n];
58a89c9211Schristos             *(out++) = c;
59a89c9211Schristos             iv[n] = c;
60a89c9211Schristos             n = (n + 1) & 0x07;
61a89c9211Schristos         }
629cef71b6Sspz     } else {
639cef71b6Sspz         while (l--) {
649cef71b6Sspz             if (n == 0) {
659cef71b6Sspz                 n2l(iv, v0);
669cef71b6Sspz                 ti[0] = v0;
679cef71b6Sspz                 n2l(iv, v1);
689cef71b6Sspz                 ti[1] = v1;
695af53050Schristos                 IDEA_encrypt(ti, schedule);
70a89c9211Schristos                 iv = (unsigned char *)ivec;
719cef71b6Sspz                 t = ti[0];
729cef71b6Sspz                 l2n(t, iv);
739cef71b6Sspz                 t = ti[1];
749cef71b6Sspz                 l2n(t, iv);
75a89c9211Schristos                 iv = (unsigned char *)ivec;
76a89c9211Schristos             }
77a89c9211Schristos             cc = *(in++);
78a89c9211Schristos             c = iv[n];
79a89c9211Schristos             iv[n] = cc;
80a89c9211Schristos             *(out++) = c ^ cc;
81a89c9211Schristos             n = (n + 1) & 0x07;
82a89c9211Schristos         }
83a89c9211Schristos     }
84a89c9211Schristos     v0 = v1 = ti[0] = ti[1] = t = c = cc = 0;
85a89c9211Schristos     *num = n;
86a89c9211Schristos }
87