]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/rc2/rc2_cbc.c
2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 * RC2 low level APIs are deprecated for public use, but still ok for internal
14 #include "internal/deprecated.h"
16 #include <openssl/rc2.h>
17 #include "rc2_local.h"
19 void RC2_cbc_encrypt(const unsigned char *in
, unsigned char *out
, long length
,
20 RC2_KEY
*ks
, unsigned char *iv
, int encrypt
)
22 register unsigned long tin0
, tin1
;
23 register unsigned long tout0
, tout1
, xor0
, xor1
;
24 register long l
= length
;
31 for (l
-= 8; l
>= 0; l
-= 8) {
45 c2ln(in
, tin0
, tin1
, l
+ 8);
62 for (l
-= 8; l
>= 0; l
-= 8) {
68 tout0
= tin
[0] ^ xor0
;
69 tout1
= tin
[1] ^ xor1
;
81 tout0
= tin
[0] ^ xor0
;
82 tout1
= tin
[1] ^ xor1
;
83 l2cn(tout0
, tout1
, out
, l
+ 8);
90 tin0
= tin1
= tout0
= tout1
= xor0
= xor1
= 0;
94 void RC2_encrypt(unsigned long *d
, RC2_KEY
*key
)
97 register RC2_INT
*p0
, *p1
;
98 register RC2_INT x0
, x1
, x2
, x3
, t
;
102 x0
= (RC2_INT
) l
& 0xffff;
103 x1
= (RC2_INT
) (l
>> 16L);
105 x2
= (RC2_INT
) l
& 0xffff;
106 x3
= (RC2_INT
) (l
>> 16L);
111 p0
= p1
= &(key
->data
[0]);
113 t
= (x0
+ (x1
& ~x3
) + (x2
& x3
) + *(p0
++)) & 0xffff;
114 x0
= (t
<< 1) | (t
>> 15);
115 t
= (x1
+ (x2
& ~x0
) + (x3
& x0
) + *(p0
++)) & 0xffff;
116 x1
= (t
<< 2) | (t
>> 14);
117 t
= (x2
+ (x3
& ~x1
) + (x0
& x1
) + *(p0
++)) & 0xffff;
118 x2
= (t
<< 3) | (t
>> 13);
119 t
= (x3
+ (x0
& ~x2
) + (x1
& x2
) + *(p0
++)) & 0xffff;
120 x3
= (t
<< 5) | (t
>> 11);
125 i
= (n
== 2) ? 6 : 5;
135 (unsigned long)(x0
& 0xffff) | ((unsigned long)(x1
& 0xffff) << 16L);
137 (unsigned long)(x2
& 0xffff) | ((unsigned long)(x3
& 0xffff) << 16L);
140 void RC2_decrypt(unsigned long *d
, RC2_KEY
*key
)
143 register RC2_INT
*p0
, *p1
;
144 register RC2_INT x0
, x1
, x2
, x3
, t
;
148 x0
= (RC2_INT
) l
& 0xffff;
149 x1
= (RC2_INT
) (l
>> 16L);
151 x2
= (RC2_INT
) l
& 0xffff;
152 x3
= (RC2_INT
) (l
>> 16L);
157 p0
= &(key
->data
[63]);
158 p1
= &(key
->data
[0]);
160 t
= ((x3
<< 11) | (x3
>> 5)) & 0xffff;
161 x3
= (t
- (x0
& ~x2
) - (x1
& x2
) - *(p0
--)) & 0xffff;
162 t
= ((x2
<< 13) | (x2
>> 3)) & 0xffff;
163 x2
= (t
- (x3
& ~x1
) - (x0
& x1
) - *(p0
--)) & 0xffff;
164 t
= ((x1
<< 14) | (x1
>> 2)) & 0xffff;
165 x1
= (t
- (x2
& ~x0
) - (x3
& x0
) - *(p0
--)) & 0xffff;
166 t
= ((x0
<< 15) | (x0
>> 1)) & 0xffff;
167 x0
= (t
- (x1
& ~x3
) - (x2
& x3
) - *(p0
--)) & 0xffff;
172 i
= (n
== 2) ? 6 : 5;
174 x3
= (x3
- p1
[x2
& 0x3f]) & 0xffff;
175 x2
= (x2
- p1
[x1
& 0x3f]) & 0xffff;
176 x1
= (x1
- p1
[x0
& 0x3f]) & 0xffff;
177 x0
= (x0
- p1
[x3
& 0x3f]) & 0xffff;
182 (unsigned long)(x0
& 0xffff) | ((unsigned long)(x1
& 0xffff) << 16L);
184 (unsigned long)(x2
& 0xffff) | ((unsigned long)(x3
& 0xffff) << 16L);