]> andersk Git - openssh.git/blame - cipher-aes.c
- (dtucker) [configure.ac] Add -lcrypt to let DragonFly build OOTB.
[openssh.git] / cipher-aes.c
CommitLineData
3b6e3da9 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
3b6e3da9 25#include "includes.h"
e5146707 26
27/* compatibility with old or broken OpenSSL versions */
28#include "openbsd-compat/openssl-compat.h"
29
30#ifdef USE_BUILTIN_RIJNDAEL
24436b92 31#include <sys/types.h>
3b6e3da9 32
33#include <openssl/evp.h>
24436b92 34
35#include <stdarg.h>
36
3b6e3da9 37#include "rijndael.h"
38#include "xmalloc.h"
39#include "log.h"
40
41#define RIJNDAEL_BLOCKSIZE 16
42struct ssh_rijndael_ctx
43{
44 rijndael_ctx r_ctx;
45 u_char r_iv[RIJNDAEL_BLOCKSIZE];
46};
47
48const EVP_CIPHER * evp_rijndael(void);
49void ssh_rijndael_iv(EVP_CIPHER_CTX *, int, u_char *, u_int);
50
51static int
52ssh_rijndael_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
53 int enc)
54{
55 struct ssh_rijndael_ctx *c;
56
57 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
58 c = xmalloc(sizeof(*c));
59 EVP_CIPHER_CTX_set_app_data(ctx, c);
60 }
61 if (key != NULL) {
62 if (enc == -1)
63 enc = ctx->encrypt;
64 rijndael_set_key(&c->r_ctx, (u_char *)key,
65 8*EVP_CIPHER_CTX_key_length(ctx), enc);
66 }
67 if (iv != NULL)
68 memcpy(c->r_iv, iv, RIJNDAEL_BLOCKSIZE);
69 return (1);
70}
71
72static int
73ssh_rijndael_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src,
74 u_int len)
75{
76 struct ssh_rijndael_ctx *c;
77 u_char buf[RIJNDAEL_BLOCKSIZE];
78 u_char *cprev, *cnow, *plain, *ivp;
79 int i, j, blocks = len / RIJNDAEL_BLOCKSIZE;
80
81 if (len == 0)
82 return (1);
83 if (len % RIJNDAEL_BLOCKSIZE)
84 fatal("ssh_rijndael_cbc: bad len %d", len);
85 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
86 error("ssh_rijndael_cbc: no context");
87 return (0);
88 }
89 if (ctx->encrypt) {
90 cnow = dest;
91 plain = (u_char *)src;
92 cprev = c->r_iv;
93 for (i = 0; i < blocks; i++, plain+=RIJNDAEL_BLOCKSIZE,
94 cnow+=RIJNDAEL_BLOCKSIZE) {
95 for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++)
96 buf[j] = plain[j] ^ cprev[j];
97 rijndael_encrypt(&c->r_ctx, buf, cnow);
98 cprev = cnow;
99 }
100 memcpy(c->r_iv, cprev, RIJNDAEL_BLOCKSIZE);
101 } else {
102 cnow = (u_char *) (src+len-RIJNDAEL_BLOCKSIZE);
103 plain = dest+len-RIJNDAEL_BLOCKSIZE;
104
105 memcpy(buf, cnow, RIJNDAEL_BLOCKSIZE);
106 for (i = blocks; i > 0; i--, cnow-=RIJNDAEL_BLOCKSIZE,
107 plain-=RIJNDAEL_BLOCKSIZE) {
108 rijndael_decrypt(&c->r_ctx, cnow, plain);
109 ivp = (i == 1) ? c->r_iv : cnow-RIJNDAEL_BLOCKSIZE;
110 for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++)
111 plain[j] ^= ivp[j];
112 }
113 memcpy(c->r_iv, buf, RIJNDAEL_BLOCKSIZE);
114 }
115 return (1);
116}
117
118static int
119ssh_rijndael_cleanup(EVP_CIPHER_CTX *ctx)
120{
121 struct ssh_rijndael_ctx *c;
122
123 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
124 memset(c, 0, sizeof(*c));
125 xfree(c);
126 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
127 }
128 return (1);
129}
130
131void
132ssh_rijndael_iv(EVP_CIPHER_CTX *evp, int doset, u_char * iv, u_int len)
133{
134 struct ssh_rijndael_ctx *c;
135
136 if ((c = EVP_CIPHER_CTX_get_app_data(evp)) == NULL)
137 fatal("ssh_rijndael_iv: no context");
138 if (doset)
139 memcpy(c->r_iv, iv, len);
140 else
141 memcpy(iv, c->r_iv, len);
142}
143
144const EVP_CIPHER *
145evp_rijndael(void)
146{
147 static EVP_CIPHER rijndal_cbc;
148
149 memset(&rijndal_cbc, 0, sizeof(EVP_CIPHER));
150 rijndal_cbc.nid = NID_undef;
151 rijndal_cbc.block_size = RIJNDAEL_BLOCKSIZE;
152 rijndal_cbc.iv_len = RIJNDAEL_BLOCKSIZE;
153 rijndal_cbc.key_len = 16;
154 rijndal_cbc.init = ssh_rijndael_init;
155 rijndal_cbc.cleanup = ssh_rijndael_cleanup;
156 rijndal_cbc.do_cipher = ssh_rijndael_cbc;
157#ifndef SSH_OLD_EVP
158 rijndal_cbc.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH |
159 EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV;
160#endif
161 return (&rijndal_cbc);
162}
e5146707 163#endif /* USE_BUILTIN_RIJNDAEL */
This page took 0.12506 seconds and 5 git commands to generate.