]> andersk Git - gssapi-openssh.git/blame - openssh/cipher-ctr.c
Initial revision
[gssapi-openssh.git] / openssh / cipher-ctr.c
CommitLineData
30460aeb 1/* $OpenBSD: cipher-ctr.c,v 1.10 2006/08/03 03:34:42 deraadt Exp $ */
c9307018 2/*
3 * Copyright (c) 2003 Markus Friedl <markus@openbsd.org>
4 *
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.
8 *
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.
16 */
17#include "includes.h"
30460aeb 18
19#include <sys/types.h>
20
21#include <stdarg.h>
22#include <string.h>
c9307018 23
24#include <openssl/evp.h>
25
c9307018 26#include "xmalloc.h"
30460aeb 27#include "log.h"
c9307018 28
08822d99 29/* compatibility with old or broken OpenSSL versions */
30#include "openbsd-compat/openssl-compat.h"
ebcd87da 31
08822d99 32#ifdef USE_BUILTIN_RIJNDAEL
c9307018 33#include "rijndael.h"
34#define AES_KEY rijndael_ctx
35#define AES_BLOCK_SIZE 16
36#define AES_encrypt(a, b, c) rijndael_encrypt(c, a, b)
37#define AES_set_encrypt_key(a, b, c) rijndael_set_key(c, (char *)a, b, 1)
38#else
39#include <openssl/aes.h>
40#endif
41
42const EVP_CIPHER *evp_aes_128_ctr(void);
43void ssh_aes_ctr_iv(EVP_CIPHER_CTX *, int, u_char *, u_int);
44
45struct ssh_aes_ctr_ctx
46{
47 AES_KEY aes_ctx;
48 u_char aes_counter[AES_BLOCK_SIZE];
49};
50
51/*
52 * increment counter 'ctr',
53 * the counter is of size 'len' bytes and stored in network-byte-order.
54 * (LSB at ctr[len-1], MSB at ctr[0])
55 */
56static void
57ssh_ctr_inc(u_char *ctr, u_int len)
58{
59 int i;
60
61 for (i = len - 1; i >= 0; i--)
62 if (++ctr[i]) /* continue on overflow */
63 return;
64}
65
66static int
67ssh_aes_ctr(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src,
68 u_int len)
69{
70 struct ssh_aes_ctr_ctx *c;
71 u_int n = 0;
72 u_char buf[AES_BLOCK_SIZE];
73
74 if (len == 0)
75 return (1);
76 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL)
77 return (0);
78
79 while ((len--) > 0) {
80 if (n == 0) {
81 AES_encrypt(c->aes_counter, buf, &c->aes_ctx);
82 ssh_ctr_inc(c->aes_counter, AES_BLOCK_SIZE);
83 }
84 *(dest++) = *(src++) ^ buf[n];
85 n = (n + 1) % AES_BLOCK_SIZE;
86 }
87 return (1);
88}
89
90static int
91ssh_aes_ctr_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
92 int enc)
93{
94 struct ssh_aes_ctr_ctx *c;
95
96 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
97 c = xmalloc(sizeof(*c));
98 EVP_CIPHER_CTX_set_app_data(ctx, c);
99 }
100 if (key != NULL)
540d72c3 101 AES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
2ce0bfe4 102 &c->aes_ctx);
c9307018 103 if (iv != NULL)
104 memcpy(c->aes_counter, iv, AES_BLOCK_SIZE);
105 return (1);
106}
107
108static int
109ssh_aes_ctr_cleanup(EVP_CIPHER_CTX *ctx)
110{
111 struct ssh_aes_ctr_ctx *c;
112
113 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
114 memset(c, 0, sizeof(*c));
115 xfree(c);
116 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
117 }
118 return (1);
119}
120
121void
122ssh_aes_ctr_iv(EVP_CIPHER_CTX *evp, int doset, u_char * iv, u_int len)
123{
124 struct ssh_aes_ctr_ctx *c;
125
126 if ((c = EVP_CIPHER_CTX_get_app_data(evp)) == NULL)
127 fatal("ssh_aes_ctr_iv: no context");
128 if (doset)
129 memcpy(c->aes_counter, iv, len);
130 else
131 memcpy(iv, c->aes_counter, len);
132}
133
134const EVP_CIPHER *
135evp_aes_128_ctr(void)
136{
137 static EVP_CIPHER aes_ctr;
138
139 memset(&aes_ctr, 0, sizeof(EVP_CIPHER));
140 aes_ctr.nid = NID_undef;
141 aes_ctr.block_size = AES_BLOCK_SIZE;
142 aes_ctr.iv_len = AES_BLOCK_SIZE;
143 aes_ctr.key_len = 16;
144 aes_ctr.init = ssh_aes_ctr_init;
145 aes_ctr.cleanup = ssh_aes_ctr_cleanup;
146 aes_ctr.do_cipher = ssh_aes_ctr;
ebcd87da 147#ifndef SSH_OLD_EVP
c9307018 148 aes_ctr.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH |
149 EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV;
ebcd87da 150#endif
c9307018 151 return (&aes_ctr);
152}
This page took 0.262703 seconds and 5 git commands to generate.