diff options
author | Damien Miller <djm@cvs.openbsd.org> | 2007-09-11 12:07:06 +0000 |
---|---|---|
committer | Damien Miller <djm@cvs.openbsd.org> | 2007-09-11 12:07:06 +0000 |
commit | 023f148f597a0a1455b3cd62cf30553f5dbc28b5 (patch) | |
tree | a32e658a66b6c66c91dafff6fa75a5662610439d | |
parent | a1d52e76944f7481dc25cbc459f4264c83dda362 (diff) |
Add __bounded__ attributes to prototypes.
Use (x & 0xff) rather than (x % 256) - compilers generate faster code.
Add arc4_getbytes function to extract raw keystream
ok markus@ tom@
-rw-r--r-- | sys/crypto/arc4.c | 28 | ||||
-rw-r--r-- | sys/crypto/arc4.h | 11 |
2 files changed, 29 insertions, 10 deletions
diff --git a/sys/crypto/arc4.c b/sys/crypto/arc4.c index 3bb3be4d78a..06fc22f6aed 100644 --- a/sys/crypto/arc4.c +++ b/sys/crypto/arc4.c @@ -1,4 +1,4 @@ -/* $OpenBSD: arc4.c,v 1.2 2007/07/24 19:35:20 damien Exp $ */ +/* $OpenBSD: arc4.c,v 1.3 2007/09/11 12:07:05 djm Exp $ */ /* * Copyright (c) 2003 Markus Friedl <markus@openbsd.org> * @@ -36,7 +36,7 @@ rc4_keysetup(struct rc4_ctx *ctx, u_char *key, u_int32_t klen) for (i = 0; i < RC4STATE; i++) ctx->state[i] = i; for (i = 0; i < RC4STATE; i++) { - y = (key[x] + ctx->state[i] + y) % RC4STATE; + y = (key[x] + ctx->state[i] + y) & (RC4STATE - 1); RC4SWAP(i, y); x = (x + 1) % klen; } @@ -50,11 +50,25 @@ rc4_crypt(struct rc4_ctx *ctx, u_char *src, u_char *dst, u_int32_t i; for (i = 0; i < len; i++) { - ctx->x = (ctx->x + 1) % RC4STATE; - ctx->y = (ctx->state[ctx->x] + ctx->y) % RC4STATE; + ctx->x = (ctx->x + 1) & (RC4STATE - 1); + ctx->y = (ctx->state[ctx->x] + ctx->y) & (RC4STATE - 1); RC4SWAP(ctx->x, ctx->y); dst[i] = src[i] ^ ctx->state[ - (ctx->state[ctx->x] + ctx->state[ctx->y]) % RC4STATE]; + (ctx->state[ctx->x] + ctx->state[ctx->y]) & (RC4STATE - 1)]; + } +} + +void +rc4_getbytes(struct rc4_ctx *ctx, u_char *dst, u_int32_t len) +{ + u_int32_t i; + + for (i = 0; i < len; i++) { + ctx->x = (ctx->x + 1) & (RC4STATE - 1); + ctx->y = (ctx->state[ctx->x] + ctx->y) & (RC4STATE - 1); + RC4SWAP(ctx->x, ctx->y); + dst[i] = ctx->state[ + (ctx->state[ctx->x] + ctx->state[ctx->y]) & (RC4STATE - 1)]; } } @@ -62,8 +76,8 @@ void rc4_skip(struct rc4_ctx *ctx, u_int32_t len) { for (; len > 0; len--) { - ctx->x = (ctx->x + 1) % RC4STATE; - ctx->y = (ctx->state[ctx->x] + ctx->y) % RC4STATE; + ctx->x = (ctx->x + 1) & (RC4STATE - 1); + ctx->y = (ctx->state[ctx->x] + ctx->y) & (RC4STATE - 1); RC4SWAP(ctx->x, ctx->y); } } diff --git a/sys/crypto/arc4.h b/sys/crypto/arc4.h index b4e96ea44b4..edb73e5e173 100644 --- a/sys/crypto/arc4.h +++ b/sys/crypto/arc4.h @@ -1,4 +1,4 @@ -/* $OpenBSD: arc4.h,v 1.2 2007/07/24 19:35:20 damien Exp $ */ +/* $OpenBSD: arc4.h,v 1.3 2007/09/11 12:07:05 djm Exp $ */ /* * Copyright (c) 2003 Markus Friedl <markus@openbsd.org> * @@ -23,6 +23,11 @@ struct rc4_ctx { u_int8_t state[RC4STATE]; }; -void rc4_keysetup(struct rc4_ctx *, u_char *, u_int32_t); -void rc4_crypt(struct rc4_ctx *, u_char *, u_char *, u_int32_t); +void rc4_keysetup(struct rc4_ctx *, u_char *, u_int32_t) + __attribute__((__bounded__(__buffer__,2,3))); +void rc4_crypt(struct rc4_ctx *, u_char *, u_char *, u_int32_t) + __attribute__((__bounded__(__buffer__,2,4))) + __attribute__((__bounded__(__buffer__,3,4))); +void rc4_getbytes(struct rc4_ctx *, u_char *, u_int32_t) + __attribute__((__bounded__(__buffer__,2,3))); void rc4_skip(struct rc4_ctx *, u_int32_t); |