]> andersk Git - openssh.git/blob - cipher-aes.c
- deraadt@cvs.openbsd.org 2006/03/19 18:51:18
[openssh.git] / cipher-aes.c
1 /*
2  * Copyright (c) 2003 Markus Friedl.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #include "includes.h"
26
27 /* compatibility with old or broken OpenSSL versions */
28 #include "openbsd-compat/openssl-compat.h"
29
30 #ifdef USE_BUILTIN_RIJNDAEL
31
32 #include <openssl/evp.h>
33 #include "rijndael.h"
34 #include "xmalloc.h"
35 #include "log.h"
36
37 #define RIJNDAEL_BLOCKSIZE 16
38 struct ssh_rijndael_ctx
39 {
40         rijndael_ctx    r_ctx;
41         u_char          r_iv[RIJNDAEL_BLOCKSIZE];
42 };
43
44 const EVP_CIPHER * evp_rijndael(void);
45 void ssh_rijndael_iv(EVP_CIPHER_CTX *, int, u_char *, u_int);
46
47 static int
48 ssh_rijndael_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
49     int enc)
50 {
51         struct ssh_rijndael_ctx *c;
52
53         if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
54                 c = xmalloc(sizeof(*c));
55                 EVP_CIPHER_CTX_set_app_data(ctx, c);
56         }
57         if (key != NULL) {
58                 if (enc == -1)
59                         enc = ctx->encrypt;
60                 rijndael_set_key(&c->r_ctx, (u_char *)key,
61                     8*EVP_CIPHER_CTX_key_length(ctx), enc);
62         }
63         if (iv != NULL)
64                 memcpy(c->r_iv, iv, RIJNDAEL_BLOCKSIZE);
65         return (1);
66 }
67
68 static int
69 ssh_rijndael_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src,
70     u_int len)
71 {
72         struct ssh_rijndael_ctx *c;
73         u_char buf[RIJNDAEL_BLOCKSIZE];
74         u_char *cprev, *cnow, *plain, *ivp;
75         int i, j, blocks = len / RIJNDAEL_BLOCKSIZE;
76
77         if (len == 0)
78                 return (1);
79         if (len % RIJNDAEL_BLOCKSIZE)
80                 fatal("ssh_rijndael_cbc: bad len %d", len);
81         if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
82                 error("ssh_rijndael_cbc: no context");
83                 return (0);
84         }
85         if (ctx->encrypt) {
86                 cnow  = dest;
87                 plain = (u_char *)src;
88                 cprev = c->r_iv;
89                 for (i = 0; i < blocks; i++, plain+=RIJNDAEL_BLOCKSIZE,
90                     cnow+=RIJNDAEL_BLOCKSIZE) {
91                         for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++)
92                                 buf[j] = plain[j] ^ cprev[j];
93                         rijndael_encrypt(&c->r_ctx, buf, cnow);
94                         cprev = cnow;
95                 }
96                 memcpy(c->r_iv, cprev, RIJNDAEL_BLOCKSIZE);
97         } else {
98                 cnow  = (u_char *) (src+len-RIJNDAEL_BLOCKSIZE);
99                 plain = dest+len-RIJNDAEL_BLOCKSIZE;
100
101                 memcpy(buf, cnow, RIJNDAEL_BLOCKSIZE);
102                 for (i = blocks; i > 0; i--, cnow-=RIJNDAEL_BLOCKSIZE,
103                     plain-=RIJNDAEL_BLOCKSIZE) {
104                         rijndael_decrypt(&c->r_ctx, cnow, plain);
105                         ivp = (i == 1) ? c->r_iv : cnow-RIJNDAEL_BLOCKSIZE;
106                         for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++)
107                                 plain[j] ^= ivp[j];
108                 }
109                 memcpy(c->r_iv, buf, RIJNDAEL_BLOCKSIZE);
110         }
111         return (1);
112 }
113
114 static int
115 ssh_rijndael_cleanup(EVP_CIPHER_CTX *ctx)
116 {
117         struct ssh_rijndael_ctx *c;
118
119         if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
120                 memset(c, 0, sizeof(*c));
121                 xfree(c);
122                 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
123         }
124         return (1);
125 }
126
127 void
128 ssh_rijndael_iv(EVP_CIPHER_CTX *evp, int doset, u_char * iv, u_int len)
129 {
130         struct ssh_rijndael_ctx *c;
131
132         if ((c = EVP_CIPHER_CTX_get_app_data(evp)) == NULL)
133                 fatal("ssh_rijndael_iv: no context");
134         if (doset)
135                 memcpy(c->r_iv, iv, len);
136         else
137                 memcpy(iv, c->r_iv, len);
138 }
139
140 const EVP_CIPHER *
141 evp_rijndael(void)
142 {
143         static EVP_CIPHER rijndal_cbc;
144
145         memset(&rijndal_cbc, 0, sizeof(EVP_CIPHER));
146         rijndal_cbc.nid = NID_undef;
147         rijndal_cbc.block_size = RIJNDAEL_BLOCKSIZE;
148         rijndal_cbc.iv_len = RIJNDAEL_BLOCKSIZE;
149         rijndal_cbc.key_len = 16;
150         rijndal_cbc.init = ssh_rijndael_init;
151         rijndal_cbc.cleanup = ssh_rijndael_cleanup;
152         rijndal_cbc.do_cipher = ssh_rijndael_cbc;
153 #ifndef SSH_OLD_EVP
154         rijndal_cbc.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH |
155             EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV;
156 #endif
157         return (&rijndal_cbc);
158 }
159 #endif /* USE_BUILTIN_RIJNDAEL */
This page took 0.048199 seconds and 5 git commands to generate.