]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/e_rc2.c
Update copyright year
[thirdparty/openssl.git] / crypto / evp / e_rc2.c
CommitLineData
62867571 1/*
c486283c 2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
62867571
RS
4 * Licensed under the OpenSSL license (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
d02b48c6
RE
8 */
9
d02b48c6 10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
c7e7fc3e
RL
12
13#ifndef OPENSSL_NO_RC2
14
0f113f3e
MC
15# include <openssl/evp.h>
16# include <openssl/objects.h>
6435f0f6 17# include "internal/evp_int.h"
0f113f3e 18# include <openssl/rc2.h>
d02b48c6 19
1921eaad 20static int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
0f113f3e 21 const unsigned char *iv, int enc);
49528751
DSH
22static int rc2_meth_to_magic(EVP_CIPHER_CTX *ctx);
23static int rc2_magic_to_meth(int i);
dfeab068
RE
24static int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
25static int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
49528751 26static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr);
dfeab068 27
0f113f3e
MC
28typedef struct {
29 int key_bits; /* effective key bits */
30 RC2_KEY ks; /* key schedule */
31} EVP_RC2_KEY;
dbad1690 32
6435f0f6 33# define data(ctx) EVP_C_DATA(EVP_RC2_KEY,ctx)
dbad1690
BL
34
35IMPLEMENT_BLOCK_CIPHER(rc2, ks, RC2, EVP_RC2_KEY, NID_rc2,
0f113f3e
MC
36 8,
37 RC2_KEY_LENGTH, 8, 64,
38 EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
39 rc2_init_key, NULL,
40 rc2_set_asn1_type_and_iv, rc2_get_asn1_type_and_iv,
41 rc2_ctrl)
42# define RC2_40_MAGIC 0xa0
43# define RC2_64_MAGIC 0x78
44# define RC2_128_MAGIC 0x3a
45static const EVP_CIPHER r2_64_cbc_cipher = {
46 NID_rc2_64_cbc,
47 8, 8 /* 64 bit */ , 8,
48 EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
49 rc2_init_key,
50 rc2_cbc_cipher,
51 NULL,
52 sizeof(EVP_RC2_KEY),
53 rc2_set_asn1_type_and_iv,
54 rc2_get_asn1_type_and_iv,
55 rc2_ctrl,
56 NULL
57};
58
59static const EVP_CIPHER r2_40_cbc_cipher = {
60 NID_rc2_40_cbc,
61 8, 5 /* 40 bit */ , 8,
62 EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
63 rc2_init_key,
64 rc2_cbc_cipher,
65 NULL,
66 sizeof(EVP_RC2_KEY),
67 rc2_set_asn1_type_and_iv,
68 rc2_get_asn1_type_and_iv,
69 rc2_ctrl,
70 NULL
71};
d02b48c6 72
13588350 73const EVP_CIPHER *EVP_rc2_64_cbc(void)
0f113f3e 74{
26a7d938 75 return &r2_64_cbc_cipher;
0f113f3e 76}
dfeab068 77
13588350 78const EVP_CIPHER *EVP_rc2_40_cbc(void)
0f113f3e 79{
26a7d938 80 return &r2_40_cbc_cipher;
0f113f3e
MC
81}
82
1921eaad 83static int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
0f113f3e
MC
84 const unsigned char *iv, int enc)
85{
86 RC2_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx),
87 key, data(ctx)->key_bits);
88 return 1;
89}
d02b48c6 90
49528751 91static int rc2_meth_to_magic(EVP_CIPHER_CTX *e)
0f113f3e
MC
92{
93 int i;
94
434893af
MC
95 if (EVP_CIPHER_CTX_ctrl(e, EVP_CTRL_GET_RC2_KEY_BITS, 0, &i) <= 0)
96 return 0;
0f113f3e 97 if (i == 128)
26a7d938 98 return RC2_128_MAGIC;
0f113f3e 99 else if (i == 64)
26a7d938 100 return RC2_64_MAGIC;
0f113f3e 101 else if (i == 40)
26a7d938 102 return RC2_40_MAGIC;
0f113f3e 103 else
26a7d938 104 return 0;
0f113f3e 105}
dfeab068 106
49528751 107static int rc2_magic_to_meth(int i)
0f113f3e
MC
108{
109 if (i == RC2_128_MAGIC)
110 return 128;
111 else if (i == RC2_64_MAGIC)
112 return 64;
113 else if (i == RC2_40_MAGIC)
114 return 40;
115 else {
116 EVPerr(EVP_F_RC2_MAGIC_TO_METH, EVP_R_UNSUPPORTED_KEY_SIZE);
26a7d938 117 return 0;
0f113f3e
MC
118 }
119}
dfeab068 120
6b691a5c 121static int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
0f113f3e
MC
122{
123 long num = 0;
124 int i = 0;
125 int key_bits;
126 unsigned int l;
127 unsigned char iv[EVP_MAX_IV_LENGTH];
128
129 if (type != NULL) {
130 l = EVP_CIPHER_CTX_iv_length(c);
131 OPENSSL_assert(l <= sizeof(iv));
132 i = ASN1_TYPE_get_int_octetstring(type, &num, iv, l);
133 if (i != (int)l)
d356dc56 134 return -1;
0f113f3e
MC
135 key_bits = rc2_magic_to_meth((int)num);
136 if (!key_bits)
d356dc56 137 return -1;
0f113f3e
MC
138 if (i > 0 && !EVP_CipherInit_ex(c, NULL, NULL, NULL, iv, -1))
139 return -1;
434893af
MC
140 if (EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_RC2_KEY_BITS, key_bits,
141 NULL) <= 0
142 || EVP_CIPHER_CTX_set_key_length(c, key_bits / 8) <= 0)
d356dc56 143 return -1;
0f113f3e 144 }
d356dc56 145 return i;
0f113f3e 146}
dfeab068 147
6b691a5c 148static int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
0f113f3e
MC
149{
150 long num;
151 int i = 0, j;
152
153 if (type != NULL) {
154 num = rc2_meth_to_magic(c);
155 j = EVP_CIPHER_CTX_iv_length(c);
936166af
RL
156 i = ASN1_TYPE_set_int_octetstring(type, num,
157 (unsigned char *)EVP_CIPHER_CTX_original_iv(c),
158 j);
0f113f3e 159 }
26a7d938 160 return i;
0f113f3e 161}
dfeab068 162
49528751 163static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
0f113f3e
MC
164{
165 switch (type) {
166 case EVP_CTRL_INIT:
167 data(c)->key_bits = EVP_CIPHER_CTX_key_length(c) * 8;
168 return 1;
169
170 case EVP_CTRL_GET_RC2_KEY_BITS:
171 *(int *)ptr = data(c)->key_bits;
172 return 1;
173
174 case EVP_CTRL_SET_RC2_KEY_BITS:
175 if (arg > 0) {
176 data(c)->key_bits = arg;
177 return 1;
178 }
179 return 0;
180# ifdef PBE_PRF_TEST
181 case EVP_CTRL_PBE_PRF_NID:
182 *(int *)ptr = NID_hmacWithMD5;
183 return 1;
184# endif
185
186 default:
187 return -1;
188 }
189}
49528751 190
d02b48c6 191#endif