1 /* $NetBSD: cipher-ctr.c,v 1.2 2009/06/07 22:38:46 christos Exp $ */
2 /* $OpenBSD: cipher-ctr.c,v 1.10 2006/08/03 03:34:42 deraadt Exp $ */
4 * Copyright (c) 2003 Markus Friedl <markus@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 __RCSID("$NetBSD: cipher-ctr.c,v 1.2 2009/06/07 22:38:46 christos Exp $");
21 #include <sys/types.h>
25 #include <openssl/evp.h>
26 #include <openssl/aes.h>
31 const EVP_CIPHER
*evp_aes_128_ctr(void);
32 void ssh_aes_ctr_iv(EVP_CIPHER_CTX
*, int, u_char
*, size_t);
34 struct ssh_aes_ctr_ctx
37 u_char aes_counter
[AES_BLOCK_SIZE
];
41 * increment counter 'ctr',
42 * the counter is of size 'len' bytes and stored in network-byte-order.
43 * (LSB at ctr[len-1], MSB at ctr[0])
46 ssh_ctr_inc(u_char
*ctr
, size_t len
)
50 for (i
= len
- 1; i
>= 0; i
--)
51 if (++ctr
[i
]) /* continue on overflow */
56 ssh_aes_ctr(EVP_CIPHER_CTX
*ctx
, u_char
*dest
, const u_char
*src
,
59 struct ssh_aes_ctr_ctx
*c
;
61 u_char buf
[AES_BLOCK_SIZE
];
65 if ((c
= EVP_CIPHER_CTX_get_app_data(ctx
)) == NULL
)
70 AES_encrypt(c
->aes_counter
, buf
, &c
->aes_ctx
);
71 ssh_ctr_inc(c
->aes_counter
, AES_BLOCK_SIZE
);
73 *(dest
++) = *(src
++) ^ buf
[n
];
74 n
= (n
+ 1) % AES_BLOCK_SIZE
;
80 ssh_aes_ctr_init(EVP_CIPHER_CTX
*ctx
, const u_char
*key
, const u_char
*iv
,
83 struct ssh_aes_ctr_ctx
*c
;
85 if ((c
= EVP_CIPHER_CTX_get_app_data(ctx
)) == NULL
) {
86 c
= xmalloc(sizeof(*c
));
87 EVP_CIPHER_CTX_set_app_data(ctx
, c
);
90 AES_set_encrypt_key(key
, EVP_CIPHER_CTX_key_length(ctx
) * 8,
93 memcpy(c
->aes_counter
, iv
, AES_BLOCK_SIZE
);
98 ssh_aes_ctr_cleanup(EVP_CIPHER_CTX
*ctx
)
100 struct ssh_aes_ctr_ctx
*c
;
102 if ((c
= EVP_CIPHER_CTX_get_app_data(ctx
)) != NULL
) {
103 memset(c
, 0, sizeof(*c
));
105 EVP_CIPHER_CTX_set_app_data(ctx
, NULL
);
111 ssh_aes_ctr_iv(EVP_CIPHER_CTX
*evp
, int doset
, u_char
* iv
, size_t len
)
113 struct ssh_aes_ctr_ctx
*c
;
115 if ((c
= EVP_CIPHER_CTX_get_app_data(evp
)) == NULL
)
116 fatal("ssh_aes_ctr_iv: no context");
118 memcpy(c
->aes_counter
, iv
, len
);
120 memcpy(iv
, c
->aes_counter
, len
);
124 evp_aes_128_ctr(void)
126 static EVP_CIPHER aes_ctr
;
128 memset(&aes_ctr
, 0, sizeof(EVP_CIPHER
));
129 aes_ctr
.nid
= NID_undef
;
130 aes_ctr
.block_size
= AES_BLOCK_SIZE
;
131 aes_ctr
.iv_len
= AES_BLOCK_SIZE
;
132 aes_ctr
.key_len
= 16;
133 aes_ctr
.init
= ssh_aes_ctr_init
;
134 aes_ctr
.cleanup
= ssh_aes_ctr_cleanup
;
135 aes_ctr
.do_cipher
= ssh_aes_ctr
;
136 aes_ctr
.flags
= EVP_CIPH_CBC_MODE
| EVP_CIPH_VARIABLE_LENGTH
|
137 EVP_CIPH_ALWAYS_CALL_INIT
| EVP_CIPH_CUSTOM_IV
;