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