Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /trunk/ttssh2/ttxssh/cipher-3des1.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9210 - (hide annotations) (download) (as text)
Sat Apr 17 08:36:59 2021 UTC (2 years, 11 months ago) by nmaya
Original Path: branches/ssh_chacha20poly1305/ttssh2/ttxssh/cipher-3des1.c
File MIME type: text/x-csrc
File size: 5163 byte(s)
ファイルを分割・コードを移動・関数名を整理・新しい OpenSSH からインポート

- OpenSSH からインポート
  cipher-3des1.c from OpenSSH-7.5p1
  ssherr.c from OpenSSH-8.5p1
  ssherr.h from OpenSSH-8.5p1
1 nmaya 9210 /* Imported from OpenSSH-7.5p1, TeraTerm Project */
2    
3     /* $OpenBSD: cipher-3des1.c,v 1.12 2015/01/14 10:24:42 markus Exp $ */
4     /*
5     * Copyright (c) 2003 Markus Friedl. All rights reserved.
6     *
7     * Permission to use, copy, modify, and distribute this software for any
8     * purpose with or without fee is hereby granted, provided that the above
9     * copyright notice and this permission notice appear in all copies.
10     *
11     * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
12     * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
13     * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
14     * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
15     * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
16     * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
17     * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
18     * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
19     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
20     * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21     */
22    
23     // #include "includes.h"
24    
25     #include <sys/types.h>
26     #include <string.h>
27     #include <openssl/evp.h>
28     #include <windows.h>
29    
30     typedef unsigned int u_int;
31     typedef unsigned char u_char;
32    
33     #include "ssherr.h"
34    
35     /*
36     * This is used by SSH1:
37     *
38     * What kind of triple DES are these 2 routines?
39     *
40     * Why is there a redundant initialization vector?
41     *
42     * If only iv3 was used, then, this would till effect have been
43     * outer-cbc. However, there is also a private iv1 == iv2 which
44     * perhaps makes differential analysis easier. On the other hand, the
45     * private iv1 probably makes the CRC-32 attack ineffective. This is a
46     * result of that there is no longer any known iv1 to use when
47     * choosing the X block.
48     */
49     struct ssh1_3des_ctx
50     {
51     EVP_CIPHER_CTX *k1, *k2, *k3;
52     };
53    
54     const EVP_CIPHER * evp_ssh1_3des(void);
55     int ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int);
56    
57     static int ssh1_3des_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv, int enc)
58     {
59     struct ssh1_3des_ctx *c;
60     u_char *k1, *k2, *k3;
61    
62     if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
63     if ((c = calloc(1, sizeof(*c))) == NULL)
64     return 0;
65     EVP_CIPHER_CTX_set_app_data(ctx, c);
66     }
67     if (key == NULL)
68     return 1;
69     if (enc == -1)
70     enc = EVP_CIPHER_CTX_encrypting(ctx); // ctx->encrypt
71     k1 = k2 = k3 = (u_char *) key;
72     k2 += 8;
73     if (EVP_CIPHER_CTX_key_length(ctx) >= 16+8) {
74     if (enc)
75     k3 += 16;
76     else
77     k1 += 16;
78     }
79     c->k1 = EVP_CIPHER_CTX_new();
80     c->k2 = EVP_CIPHER_CTX_new();
81     c->k3 = EVP_CIPHER_CTX_new();
82     /*** TODO: OPENSSL1.1.1 ERROR CHECK(ticket#39335�����u�\��) ***/
83     if (EVP_CipherInit(c->k1, EVP_des_cbc(), k1, NULL, enc) == 0 ||
84     EVP_CipherInit(c->k2, EVP_des_cbc(), k2, NULL, !enc) == 0 ||
85     EVP_CipherInit(c->k3, EVP_des_cbc(), k3, NULL, enc) == 0) {
86     EVP_CIPHER_CTX_free(c->k1);
87     EVP_CIPHER_CTX_free(c->k2);
88     EVP_CIPHER_CTX_free(c->k3);
89     SecureZeroMemory(c, sizeof(*c));
90     free(c);
91     EVP_CIPHER_CTX_set_app_data(ctx, NULL);
92     return 0;
93     }
94     return 1;
95     }
96    
97     static int ssh1_3des_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src, u_int len)
98     {
99     struct ssh1_3des_ctx *c;
100    
101     if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
102     //error("ssh1_3des_cbc: no context");
103     return 0;
104     }
105     if (EVP_Cipher(c->k1, dest, (u_char *)src, len) == 0 ||
106     EVP_Cipher(c->k2, dest, dest, len) == 0 ||
107     EVP_Cipher(c->k3, dest, dest, len) == 0)
108     return 0;
109     return 1;
110     }
111    
112     static int ssh1_3des_cleanup(EVP_CIPHER_CTX *ctx)
113     {
114     struct ssh1_3des_ctx *c;
115    
116     if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
117     EVP_CIPHER_CTX_free(c->k1);
118     EVP_CIPHER_CTX_free(c->k2);
119     EVP_CIPHER_CTX_free(c->k3);
120     SecureZeroMemory(c, sizeof(*c));
121     free(c);
122     EVP_CIPHER_CTX_set_app_data(ctx, NULL);
123     }
124     return 1;
125     }
126    
127     // ssh1_3des_iv �����g�p�B
128     int ssh1_3des_iv(EVP_CIPHER_CTX *evp, int doset, u_char *iv, int len)
129     {
130     struct ssh1_3des_ctx *c;
131    
132     if (len != 24) {
133     //fatal("%s: bad 3des iv length: %d", __func__, len);
134     return SSH_ERR_INVALID_ARGUMENT;
135     }
136    
137     if ((c = EVP_CIPHER_CTX_get_app_data(evp)) == NULL) {
138     //fatal("%s: no 3des context", __func__);
139     return SSH_ERR_INTERNAL_ERROR;
140     }
141    
142     if (doset) {
143     //debug3("%s: Installed 3DES IV", __func__);
144     memcpy(EVP_CIPHER_CTX_iv_noconst(c->k1), iv, 8);
145     memcpy(EVP_CIPHER_CTX_iv_noconst(c->k2), iv + 8, 8);
146     memcpy(EVP_CIPHER_CTX_iv_noconst(c->k3), iv + 16, 8);
147     } else {
148     //debug3("%s: Copying 3DES IV", __func__);
149     memcpy(iv, EVP_CIPHER_CTX_iv(c->k1), 8);
150     memcpy(iv + 8, EVP_CIPHER_CTX_iv(c->k2), 8);
151     memcpy(iv + 16, EVP_CIPHER_CTX_iv(c->k3), 8);
152     }
153     return 0;
154     }
155    
156     const EVP_CIPHER *evp_ssh1_3des(void)
157     {
158     static EVP_CIPHER *p = NULL;
159    
160     if (p == NULL) {
161     p = EVP_CIPHER_meth_new(NID_undef, /*block_size*/8, /*key_len*/16);
162     /*** TODO: OPENSSL1.1.1 ERROR CHECK(ticket#39335�����u�\��) ***/
163     }
164     if (p) {
165     EVP_CIPHER_meth_set_iv_length(p, 0);
166     EVP_CIPHER_meth_set_init(p, ssh1_3des_init);
167     EVP_CIPHER_meth_set_cleanup(p, ssh1_3des_cleanup);
168     EVP_CIPHER_meth_set_do_cipher(p, ssh1_3des_cbc);
169     EVP_CIPHER_meth_set_flags(p, EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH);
170     }
171     return (p);
172     }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26