Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 1 | /* $OpenBSD: cipher-ctr.c,v 1.10 2006/08/03 03:34:42 deraadt Exp $ */ |
Damien Miller | f5399c2 | 2003-05-18 20:53:59 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | 0275b52 | 2003-06-18 20:29:35 +1000 | [diff] [blame] | 3 | * Copyright (c) 2003 Markus Friedl <markus@openbsd.org> |
Damien Miller | f5399c2 | 2003-05-18 20:53:59 +1000 | [diff] [blame] | 4 | * |
Damien Miller | 0275b52 | 2003-06-18 20:29:35 +1000 | [diff] [blame] | 5 | * Permission to use, copy, modify, and distribute this software for any |
| 6 | * purpose with or without fee is hereby granted, provided that the above |
| 7 | * copyright notice and this permission notice appear in all copies. |
Damien Miller | f5399c2 | 2003-05-18 20:53:59 +1000 | [diff] [blame] | 8 | * |
Damien Miller | 0275b52 | 2003-06-18 20:29:35 +1000 | [diff] [blame] | 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
Damien Miller | f5399c2 | 2003-05-18 20:53:59 +1000 | [diff] [blame] | 16 | */ |
| 17 | #include "includes.h" |
Damien Miller | f5399c2 | 2003-05-18 20:53:59 +1000 | [diff] [blame] | 18 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 19 | #include <sys/types.h> |
| 20 | |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 21 | #include <string.h> |
| 22 | |
Damien Miller | f5399c2 | 2003-05-18 20:53:59 +1000 | [diff] [blame] | 23 | #include <openssl/evp.h> |
| 24 | |
Damien Miller | f5399c2 | 2003-05-18 20:53:59 +1000 | [diff] [blame] | 25 | #include "xmalloc.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 26 | #include "log.h" |
Damien Miller | f5399c2 | 2003-05-18 20:53:59 +1000 | [diff] [blame] | 27 | |
Darren Tucker | 129d0bb | 2005-12-19 17:40:40 +1100 | [diff] [blame] | 28 | /* compatibility with old or broken OpenSSL versions */ |
| 29 | #include "openbsd-compat/openssl-compat.h" |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 30 | |
Darren Tucker | 129d0bb | 2005-12-19 17:40:40 +1100 | [diff] [blame] | 31 | #ifdef USE_BUILTIN_RIJNDAEL |
Damien Miller | f5399c2 | 2003-05-18 20:53:59 +1000 | [diff] [blame] | 32 | #include "rijndael.h" |
| 33 | #define AES_KEY rijndael_ctx |
| 34 | #define AES_BLOCK_SIZE 16 |
| 35 | #define AES_encrypt(a, b, c) rijndael_encrypt(c, a, b) |
| 36 | #define AES_set_encrypt_key(a, b, c) rijndael_set_key(c, (char *)a, b, 1) |
| 37 | #else |
| 38 | #include <openssl/aes.h> |
| 39 | #endif |
| 40 | |
| 41 | const EVP_CIPHER *evp_aes_128_ctr(void); |
| 42 | void ssh_aes_ctr_iv(EVP_CIPHER_CTX *, int, u_char *, u_int); |
| 43 | |
| 44 | struct ssh_aes_ctr_ctx |
| 45 | { |
| 46 | AES_KEY aes_ctx; |
| 47 | u_char aes_counter[AES_BLOCK_SIZE]; |
| 48 | }; |
| 49 | |
| 50 | /* |
| 51 | * increment counter 'ctr', |
| 52 | * the counter is of size 'len' bytes and stored in network-byte-order. |
| 53 | * (LSB at ctr[len-1], MSB at ctr[0]) |
| 54 | */ |
| 55 | static void |
| 56 | ssh_ctr_inc(u_char *ctr, u_int len) |
| 57 | { |
| 58 | int i; |
| 59 | |
| 60 | for (i = len - 1; i >= 0; i--) |
| 61 | if (++ctr[i]) /* continue on overflow */ |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | static int |
| 66 | ssh_aes_ctr(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src, |
| 67 | u_int len) |
| 68 | { |
| 69 | struct ssh_aes_ctr_ctx *c; |
| 70 | u_int n = 0; |
| 71 | u_char buf[AES_BLOCK_SIZE]; |
| 72 | |
| 73 | if (len == 0) |
| 74 | return (1); |
| 75 | if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) |
| 76 | return (0); |
| 77 | |
| 78 | while ((len--) > 0) { |
| 79 | if (n == 0) { |
| 80 | AES_encrypt(c->aes_counter, buf, &c->aes_ctx); |
| 81 | ssh_ctr_inc(c->aes_counter, AES_BLOCK_SIZE); |
| 82 | } |
| 83 | *(dest++) = *(src++) ^ buf[n]; |
| 84 | n = (n + 1) % AES_BLOCK_SIZE; |
| 85 | } |
| 86 | return (1); |
| 87 | } |
| 88 | |
| 89 | static int |
| 90 | ssh_aes_ctr_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv, |
| 91 | int enc) |
| 92 | { |
| 93 | struct ssh_aes_ctr_ctx *c; |
| 94 | |
| 95 | if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) { |
| 96 | c = xmalloc(sizeof(*c)); |
| 97 | EVP_CIPHER_CTX_set_app_data(ctx, c); |
| 98 | } |
| 99 | if (key != NULL) |
Darren Tucker | fc57f71 | 2004-02-07 10:41:48 +1100 | [diff] [blame] | 100 | AES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, |
Damien Miller | 0dc1bef | 2005-07-17 17:22:45 +1000 | [diff] [blame] | 101 | &c->aes_ctx); |
Damien Miller | f5399c2 | 2003-05-18 20:53:59 +1000 | [diff] [blame] | 102 | if (iv != NULL) |
| 103 | memcpy(c->aes_counter, iv, AES_BLOCK_SIZE); |
| 104 | return (1); |
| 105 | } |
| 106 | |
| 107 | static int |
| 108 | ssh_aes_ctr_cleanup(EVP_CIPHER_CTX *ctx) |
| 109 | { |
| 110 | struct ssh_aes_ctr_ctx *c; |
| 111 | |
| 112 | if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) { |
| 113 | memset(c, 0, sizeof(*c)); |
| 114 | xfree(c); |
| 115 | EVP_CIPHER_CTX_set_app_data(ctx, NULL); |
| 116 | } |
| 117 | return (1); |
| 118 | } |
| 119 | |
| 120 | void |
| 121 | ssh_aes_ctr_iv(EVP_CIPHER_CTX *evp, int doset, u_char * iv, u_int len) |
| 122 | { |
| 123 | struct ssh_aes_ctr_ctx *c; |
| 124 | |
| 125 | if ((c = EVP_CIPHER_CTX_get_app_data(evp)) == NULL) |
| 126 | fatal("ssh_aes_ctr_iv: no context"); |
| 127 | if (doset) |
| 128 | memcpy(c->aes_counter, iv, len); |
| 129 | else |
| 130 | memcpy(iv, c->aes_counter, len); |
| 131 | } |
| 132 | |
| 133 | const EVP_CIPHER * |
| 134 | evp_aes_128_ctr(void) |
| 135 | { |
| 136 | static EVP_CIPHER aes_ctr; |
| 137 | |
| 138 | memset(&aes_ctr, 0, sizeof(EVP_CIPHER)); |
| 139 | aes_ctr.nid = NID_undef; |
| 140 | aes_ctr.block_size = AES_BLOCK_SIZE; |
| 141 | aes_ctr.iv_len = AES_BLOCK_SIZE; |
| 142 | aes_ctr.key_len = 16; |
| 143 | aes_ctr.init = ssh_aes_ctr_init; |
| 144 | aes_ctr.cleanup = ssh_aes_ctr_cleanup; |
| 145 | aes_ctr.do_cipher = ssh_aes_ctr; |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 146 | #ifndef SSH_OLD_EVP |
Damien Miller | f5399c2 | 2003-05-18 20:53:59 +1000 | [diff] [blame] | 147 | aes_ctr.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | |
| 148 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV; |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 149 | #endif |
Damien Miller | f5399c2 | 2003-05-18 20:53:59 +1000 | [diff] [blame] | 150 | return (&aes_ctr); |
| 151 | } |