diff options
Diffstat (limited to 'src/lib/libcrypto/evp/e_des3.c')
-rw-r--r-- | src/lib/libcrypto/evp/e_des3.c | 88 |
1 files changed, 58 insertions, 30 deletions
diff --git a/src/lib/libcrypto/evp/e_des3.c b/src/lib/libcrypto/evp/e_des3.c index a9aba4ae70..077860e7b6 100644 --- a/src/lib/libcrypto/evp/e_des3.c +++ b/src/lib/libcrypto/evp/e_des3.c | |||
@@ -56,12 +56,13 @@ | |||
56 | * [including the GNU Public Licence.] | 56 | * [including the GNU Public Licence.] |
57 | */ | 57 | */ |
58 | 58 | ||
59 | #ifndef NO_DES | 59 | #ifndef OPENSSL_NO_DES |
60 | #include <stdio.h> | 60 | #include <stdio.h> |
61 | #include "cryptlib.h" | 61 | #include "cryptlib.h" |
62 | #include <openssl/evp.h> | 62 | #include <openssl/evp.h> |
63 | #include <openssl/objects.h> | 63 | #include <openssl/objects.h> |
64 | #include "evp_locl.h" | 64 | #include "evp_locl.h" |
65 | #include <openssl/des.h> | ||
65 | 66 | ||
66 | static int des_ede_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | 67 | static int des_ede_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
67 | const unsigned char *iv,int enc); | 68 | const unsigned char *iv,int enc); |
@@ -69,60 +70,78 @@ static int des_ede_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | |||
69 | static int des_ede3_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | 70 | static int des_ede3_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
70 | const unsigned char *iv,int enc); | 71 | const unsigned char *iv,int enc); |
71 | 72 | ||
73 | typedef struct | ||
74 | { | ||
75 | DES_key_schedule ks1;/* key schedule */ | ||
76 | DES_key_schedule ks2;/* key schedule (for ede) */ | ||
77 | DES_key_schedule ks3;/* key schedule (for ede3) */ | ||
78 | } DES_EDE_KEY; | ||
79 | |||
80 | #define data(ctx) ((DES_EDE_KEY *)(ctx)->cipher_data) | ||
81 | |||
72 | /* Because of various casts and different args can't use IMPLEMENT_BLOCK_CIPHER */ | 82 | /* Because of various casts and different args can't use IMPLEMENT_BLOCK_CIPHER */ |
73 | 83 | ||
74 | static int des_ede_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 84 | static int des_ede_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
75 | const unsigned char *in, unsigned int inl) | 85 | const unsigned char *in, unsigned int inl) |
76 | { | 86 | { |
77 | BLOCK_CIPHER_ecb_loop() | 87 | BLOCK_CIPHER_ecb_loop() |
78 | des_ecb3_encrypt((des_cblock *)(in + i), (des_cblock *)(out + i), | 88 | DES_ecb3_encrypt((DES_cblock *)(in + i), (DES_cblock *)(out + i), |
79 | ctx->c.des_ede.ks1, ctx->c.des_ede.ks2, ctx->c.des_ede.ks3, | 89 | &data(ctx)->ks1, &data(ctx)->ks2, |
80 | ctx->encrypt); | 90 | &data(ctx)->ks3, |
91 | ctx->encrypt); | ||
81 | return 1; | 92 | return 1; |
82 | } | 93 | } |
83 | 94 | ||
84 | static int des_ede_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 95 | static int des_ede_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
85 | const unsigned char *in, unsigned int inl) | 96 | const unsigned char *in, unsigned int inl) |
86 | { | 97 | { |
87 | des_ede3_ofb64_encrypt(in, out, (long)inl, | 98 | DES_ede3_ofb64_encrypt(in, out, (long)inl, |
88 | ctx->c.des_ede.ks1, ctx->c.des_ede.ks2, ctx->c.des_ede.ks3, | 99 | &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, |
89 | (des_cblock *)ctx->iv, &ctx->num); | 100 | (DES_cblock *)ctx->iv, &ctx->num); |
90 | return 1; | 101 | return 1; |
91 | } | 102 | } |
92 | 103 | ||
93 | static int des_ede_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 104 | static int des_ede_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
94 | const unsigned char *in, unsigned int inl) | 105 | const unsigned char *in, unsigned int inl) |
95 | { | 106 | { |
96 | des_ede3_cbc_encrypt(in, out, (long)inl, | 107 | #ifdef KSSL_DEBUG |
97 | ctx->c.des_ede.ks1, ctx->c.des_ede.ks2, ctx->c.des_ede.ks3, | 108 | { |
98 | (des_cblock *)ctx->iv, ctx->encrypt); | 109 | int i; |
110 | char *cp; | ||
111 | printf("des_ede_cbc_cipher(ctx=%lx, buflen=%d)\n", ctx, ctx->buf_len); | ||
112 | printf("\t iv= "); | ||
113 | for(i=0;i<8;i++) | ||
114 | printf("%02X",ctx->iv[i]); | ||
115 | printf("\n"); | ||
116 | } | ||
117 | #endif /* KSSL_DEBUG */ | ||
118 | DES_ede3_cbc_encrypt(in, out, (long)inl, | ||
119 | &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, | ||
120 | (DES_cblock *)ctx->iv, ctx->encrypt); | ||
99 | return 1; | 121 | return 1; |
100 | } | 122 | } |
101 | 123 | ||
102 | static int des_ede_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 124 | static int des_ede_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
103 | const unsigned char *in, unsigned int inl) | 125 | const unsigned char *in, unsigned int inl) |
104 | { | 126 | { |
105 | des_ede3_cfb64_encrypt(in, out, (long)inl, | 127 | DES_ede3_cfb64_encrypt(in, out, (long)inl, |
106 | ctx->c.des_ede.ks1, ctx->c.des_ede.ks2, ctx->c.des_ede.ks3, | 128 | &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, |
107 | (des_cblock *)ctx->iv, &ctx->num, ctx->encrypt); | 129 | (DES_cblock *)ctx->iv, &ctx->num, ctx->encrypt); |
108 | return 1; | 130 | return 1; |
109 | } | 131 | } |
110 | 132 | ||
111 | #define NID_des_ede_ecb NID_des_ede | 133 | BLOCK_CIPHER_defs(des_ede, DES_EDE_KEY, NID_des_ede, 8, 16, 8, 64, |
112 | |||
113 | BLOCK_CIPHER_defs(des_ede, des_ede, NID_des_ede, 8, 16, 8, | ||
114 | 0, des_ede_init_key, NULL, | 134 | 0, des_ede_init_key, NULL, |
115 | EVP_CIPHER_set_asn1_iv, | 135 | EVP_CIPHER_set_asn1_iv, |
116 | EVP_CIPHER_get_asn1_iv, | 136 | EVP_CIPHER_get_asn1_iv, |
117 | NULL) | 137 | NULL) |
118 | 138 | ||
119 | #define NID_des_ede3_ecb NID_des_ede3 | ||
120 | #define des_ede3_cfb_cipher des_ede_cfb_cipher | 139 | #define des_ede3_cfb_cipher des_ede_cfb_cipher |
121 | #define des_ede3_ofb_cipher des_ede_ofb_cipher | 140 | #define des_ede3_ofb_cipher des_ede_ofb_cipher |
122 | #define des_ede3_cbc_cipher des_ede_cbc_cipher | 141 | #define des_ede3_cbc_cipher des_ede_cbc_cipher |
123 | #define des_ede3_ecb_cipher des_ede_ecb_cipher | 142 | #define des_ede3_ecb_cipher des_ede_ecb_cipher |
124 | 143 | ||
125 | BLOCK_CIPHER_defs(des_ede3, des_ede, NID_des_ede3, 8, 24, 8, | 144 | BLOCK_CIPHER_defs(des_ede3, DES_EDE_KEY, NID_des_ede3, 8, 24, 8, 64, |
126 | 0, des_ede3_init_key, NULL, | 145 | 0, des_ede3_init_key, NULL, |
127 | EVP_CIPHER_set_asn1_iv, | 146 | EVP_CIPHER_set_asn1_iv, |
128 | EVP_CIPHER_get_asn1_iv, | 147 | EVP_CIPHER_get_asn1_iv, |
@@ -131,34 +150,43 @@ BLOCK_CIPHER_defs(des_ede3, des_ede, NID_des_ede3, 8, 24, 8, | |||
131 | static int des_ede_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | 150 | static int des_ede_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
132 | const unsigned char *iv, int enc) | 151 | const unsigned char *iv, int enc) |
133 | { | 152 | { |
134 | des_cblock *deskey = (des_cblock *)key; | 153 | DES_cblock *deskey = (DES_cblock *)key; |
135 | 154 | ||
136 | des_set_key_unchecked(&deskey[0],ctx->c.des_ede.ks1); | 155 | DES_set_key_unchecked(&deskey[0],&data(ctx)->ks1); |
137 | des_set_key_unchecked(&deskey[1],ctx->c.des_ede.ks2); | 156 | DES_set_key_unchecked(&deskey[1],&data(ctx)->ks2); |
138 | memcpy( (char *)ctx->c.des_ede.ks3, | 157 | memcpy(&data(ctx)->ks3,&data(ctx)->ks1, |
139 | (char *)ctx->c.des_ede.ks1, | 158 | sizeof(data(ctx)->ks1)); |
140 | sizeof(ctx->c.des_ede.ks1)); | ||
141 | return 1; | 159 | return 1; |
142 | } | 160 | } |
143 | 161 | ||
144 | static int des_ede3_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | 162 | static int des_ede3_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
145 | const unsigned char *iv, int enc) | 163 | const unsigned char *iv, int enc) |
146 | { | 164 | { |
147 | des_cblock *deskey = (des_cblock *)key; | 165 | DES_cblock *deskey = (DES_cblock *)key; |
166 | #ifdef KSSL_DEBUG | ||
167 | { | ||
168 | int i; | ||
169 | printf("des_ede3_init_key(ctx=%lx)\n", ctx); | ||
170 | printf("\tKEY= "); | ||
171 | for(i=0;i<24;i++) printf("%02X",key[i]); printf("\n"); | ||
172 | printf("\t IV= "); | ||
173 | for(i=0;i<8;i++) printf("%02X",iv[i]); printf("\n"); | ||
174 | } | ||
175 | #endif /* KSSL_DEBUG */ | ||
148 | 176 | ||
149 | des_set_key_unchecked(&deskey[0],ctx->c.des_ede.ks1); | 177 | DES_set_key_unchecked(&deskey[0],&data(ctx)->ks1); |
150 | des_set_key_unchecked(&deskey[1],ctx->c.des_ede.ks2); | 178 | DES_set_key_unchecked(&deskey[1],&data(ctx)->ks2); |
151 | des_set_key_unchecked(&deskey[2],ctx->c.des_ede.ks3); | 179 | DES_set_key_unchecked(&deskey[2],&data(ctx)->ks3); |
152 | 180 | ||
153 | return 1; | 181 | return 1; |
154 | } | 182 | } |
155 | 183 | ||
156 | EVP_CIPHER *EVP_des_ede(void) | 184 | const EVP_CIPHER *EVP_des_ede(void) |
157 | { | 185 | { |
158 | return &des_ede_ecb; | 186 | return &des_ede_ecb; |
159 | } | 187 | } |
160 | 188 | ||
161 | EVP_CIPHER *EVP_des_ede3(void) | 189 | const EVP_CIPHER *EVP_des_ede3(void) |
162 | { | 190 | { |
163 | return &des_ede3_ecb; | 191 | return &des_ede3_ecb; |
164 | } | 192 | } |