From 13dc05b6b81d6f98a5a1610ebcbc8deb74034df6 Mon Sep 17 00:00:00 2001 From: Gregor Pintar Date: Fri, 16 Aug 2013 15:48:21 +0200 Subject: [PATCH] modes cleaned and CFB mode added (not tested) --- build.sh | 4 +- include/kripto/mode/cfb.h | 8 +++ lib/mode/cbc.c | 46 +++--------- lib/mode/cfb.c | 173 ++++++++++++++++++++++++++++++++++++++++++++++ lib/mode/ctr.c | 23 ++---- lib/mode/ecb.c | 8 +-- lib/mode/ofb.c | 11 +-- 7 files changed, 200 insertions(+), 73 deletions(-) create mode 100644 include/kripto/mode/cfb.h diff --git a/build.sh b/build.sh index df57703..a1be7b4 100755 --- a/build.sh +++ b/build.sh @@ -20,8 +20,8 @@ CFLAGS="-std=c99 -pedantic -Wall -Wextra -Wstrict-prototypes -Wmissing-prototype OPTIM="-O2 -D_FORTIFY_SOURCE=2 -flto -DNDEBUG $OPTIM" LDFLAGS="-Wall $LDFLAGS" -SRC="lib/version.c lib/authstream.c lib/authmode.c lib/mac.c lib/mac/hmac.c lib/stream/salsa20.c lib/mode.c lib/hash/blake256.c lib/hash/blake512.c lib/hash/blake2s.c lib/hash/blake2b.c lib/hash/keccak1600.c lib/hash/keccak800.c lib/block/xtea.c lib/block/threefish256.c lib/block/threefish512.c lib/block/threefish1024.c lib/mode/ecb.c lib/mode/ctr.c lib/mode/cbc.c lib/mode/ofb.c lib/stream/rc4.c lib/stream/chacha.c lib/block/rijndael.c lib/block/serpent.c lib/block/rc6.c lib/block/twofish.c lib/block/blowfish.c lib/block/anubis.c lib/block/noekeon.c lib/block/aria.c lib/block/seed.c lib/block/camellia.c lib/block/gost.c lib/hash.c lib/hash/sha1.c lib/hash/sha2_256.c lib/hash/sha2_512.c lib/memwipe.c lib/random.c lib/pkcs7.c lib/block.c lib/stream.c lib/pbkdf2.c lib/scrypt.c" -OBJ="version.o authstream.o authmode.o mac.o hmac.o salsa20.o mode.o blake256.o blake512.o blake2s.o blake2b.o keccak1600.o keccak800.o xtea.o threefish256.o threefish512.o threefish1024.o ecb.o ctr.o cbc.o ofb.o rc4.o chacha.o rijndael.o serpent.o rc6.o twofish.o blowfish.o anubis.o noekeon.o aria.o seed.o camellia.o gost.o hash.o sha1.o sha2_256.o sha2_512.o memwipe.o random.o pkcs7.o block.o stream.o pbkdf2.o scrypt.o" +SRC="lib/version.c lib/authstream.c lib/authmode.c lib/mac.c lib/mac/hmac.c lib/stream/salsa20.c lib/mode.c lib/hash/blake256.c lib/hash/blake512.c lib/hash/blake2s.c lib/hash/blake2b.c lib/hash/keccak1600.c lib/hash/keccak800.c lib/block/xtea.c lib/block/threefish256.c lib/block/threefish512.c lib/block/threefish1024.c lib/mode/ecb.c lib/mode/ctr.c lib/mode/cbc.c lib/mode/ofb.c lib/stream/rc4.c lib/stream/chacha.c lib/block/rijndael.c lib/block/serpent.c lib/block/rc6.c lib/block/twofish.c lib/block/blowfish.c lib/block/anubis.c lib/block/noekeon.c lib/block/aria.c lib/block/seed.c lib/block/camellia.c lib/block/gost.c lib/hash.c lib/hash/sha1.c lib/hash/sha2_256.c lib/hash/sha2_512.c lib/memwipe.c lib/random.c lib/pkcs7.c lib/block.c lib/stream.c lib/pbkdf2.c lib/scrypt.c lib/mode/cfb.c" +OBJ="version.o authstream.o authmode.o mac.o hmac.o salsa20.o mode.o blake256.o blake512.o blake2s.o blake2b.o keccak1600.o keccak800.o xtea.o threefish256.o threefish512.o threefish1024.o ecb.o ctr.o cbc.o ofb.o rc4.o chacha.o rijndael.o serpent.o rc6.o twofish.o blowfish.o anubis.o noekeon.o aria.o seed.o camellia.o gost.o hash.o sha1.o sha2_256.o sha2_512.o memwipe.o random.o pkcs7.o block.o stream.o pbkdf2.o scrypt.o cfb.o" i=1 while [ $i -le $# ]; do diff --git a/include/kripto/mode/cfb.h b/include/kripto/mode/cfb.h new file mode 100644 index 0000000..2d75bd3 --- /dev/null +++ b/include/kripto/mode/cfb.h @@ -0,0 +1,8 @@ +#ifndef KRIPTO_MODE_CFB_H +#define KRIPTO_MODE_CFB_H + +#include + +extern kripto_mode_desc *const kripto_mode_cfb; + +#endif diff --git a/lib/mode/cbc.c b/lib/mode/cbc.c index 91527ed..bb2e3b3 100644 --- a/lib/mode/cbc.c +++ b/lib/mode/cbc.c @@ -15,7 +15,6 @@ #include #include #include -#include #include #include @@ -47,11 +46,6 @@ static size_t cbc_encrypt size_t i; unsigned int n; - assert(pt); - assert(ct); - - if(len & (s->block_size - 1)) return 0; - for(i = 0; i < len; i += n) { for(n = 0; n < s->block_size; n++) @@ -80,11 +74,6 @@ static size_t cbc_decrypt size_t i; unsigned int n; - assert(ct); - assert(pt); - - if(len & (s->block_size - 1)) return 0; - for(i = 0; i < len; i += n) { for(n = 0; n < s->block_size; n++) @@ -105,28 +94,13 @@ static size_t cbc_decrypt return i; } -static size_t cbc_prng -( - kripto_stream *s, - void *out, - const size_t len -) -{ - (void)s; - (void)out; - (void)len; - - assert(1); - - return 0; -} - static void cbc_destroy(kripto_stream *s) { - kripto_memwipe(s, sizeof(struct kripto_stream) + kripto_memwipe(s, sizeof(kripto_stream) + (s->block_size << 1) - + sizeof(struct kripto_stream_desc) + + sizeof(kripto_stream_desc) ); + free(s); } @@ -141,29 +115,25 @@ static kripto_stream *cbc_create kripto_block_desc *b; struct kripto_stream_desc *stream; - assert(block); - b = kripto_block_get_desc(block); - assert(iv_len > kripto_block_size(b)); - - s = malloc(sizeof(struct kripto_stream) + s = malloc(sizeof(kripto_stream) + (kripto_block_size(b) << 1) - + sizeof(struct kripto_stream_desc) + + sizeof(kripto_stream_desc) ); if(!s) return 0; s->block_size = kripto_block_size(b); stream = (struct kripto_stream_desc *) - ((uint8_t *)s + sizeof(struct kripto_stream)); + ((uint8_t *)s + sizeof(kripto_stream)); - s->iv = (uint8_t *)stream + sizeof(struct kripto_stream_desc); + s->iv = (uint8_t *)stream + sizeof(kripto_stream_desc); s->buf = s->iv + s->block_size; stream->encrypt = &cbc_encrypt; stream->decrypt = &cbc_decrypt; - stream->prng = &cbc_prng; + stream->prng = 0; stream->create = 0; stream->destroy = &cbc_destroy; stream->max_key = kripto_block_max_key(b); diff --git a/lib/mode/cfb.c b/lib/mode/cfb.c index e69de29..67a44cb 100644 --- a/lib/mode/cfb.c +++ b/lib/mode/cfb.c @@ -0,0 +1,173 @@ +/* + * Copyright (C) 2013 Gregor Pintar + * + * Permission is granted to deal in this work without any restriction, + * including unlimited rights to use, publicly perform, publish, + * reproduce, relicence, modify, merge, and/or distribute in any form, + * for any purpose, with or without fee, and by any means. + * + * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, + * to the utmost extent permitted by applicable law. In no event + * shall a licensor, author or contributor be held liable for any + * issues arising in any way out of dealing in the work. + */ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include + +struct kripto_stream +{ + kripto_stream_desc *desc; + const kripto_block *block; + unsigned int block_size; + uint8_t *iv; +}; + +static size_t cfb_encrypt +( + kripto_stream *s, + const void *pt, + void *ct, + const size_t len +) +{ + size_t i; + unsigned int n; + + for(i = 0; i < len; i += n) + { + kripto_block_encrypt(s->block, s->iv, ct); + + for(n = 0; n < s->block_size; n++) + s->iv[n] = U8(ct)[n] ^= CU8(pt)[n]; + + CPTR_INC(pt, n); + PTR_INC(ct, n); + } + + return i; +} + +static size_t cfb_decrypt +( + kripto_stream *s, + const void *ct, + void *pt, + const size_t len +) +{ + size_t i; + unsigned int n; + + for(i = 0; i < len; i += n) + { + kripto_block_encrypt(s->block, s->iv, pt); + + for(n = 0; n < s->block_size; n++) + { + U8(pt)[n] ^= CU8(ct)[n]; + s->iv[n] = CU8(ct)[n]; + } + + CPTR_INC(ct, n); + PTR_INC(pt, n); + } + + return i; +} + +static size_t cfb_prng +( + kripto_stream *s, + void *out, + const size_t len +) +{ + size_t i; + unsigned int n; + + for(i = 0; i < len; i += n) + { + kripto_block_encrypt(s->block, s->iv, out); + + for(n = 0; n < s->block_size; n++) + s->iv[n] = U8(out)[n]; + + PTR_INC(out, n); + } + + return i; +} + +static void cfb_destroy(kripto_stream *s) +{ + kripto_memwipe(s, sizeof(kripto_stream) + + s->block_size + + sizeof(kripto_stream_desc) + ); + + free(s); +} + +static kripto_stream *cfb_create +( + const kripto_block *block, + const void *iv, + const unsigned int iv_len +) +{ + kripto_stream *s; + kripto_block_desc *b; + struct kripto_stream_desc *stream; + + b = kripto_block_get_desc(block); + + s = malloc(sizeof(kripto_stream) + + kripto_block_size(b) + + sizeof(kripto_stream_desc) + ); + if(!s) return 0; + + s->block_size = kripto_block_size(b); + + stream = (struct kripto_stream_desc *) + ((uint8_t *)s + sizeof(kripto_stream)); + + s->iv = (uint8_t *)stream + sizeof(kripto_stream_desc); + + stream->encrypt = &cfb_encrypt; + stream->decrypt = &cfb_decrypt; + stream->prng = &cfb_prng; + stream->create = 0; + stream->destroy = &cfb_destroy; + stream->max_key = kripto_block_max_key(b); + stream->max_iv = s->block_size; + + s->desc = stream; + + if(iv_len) memcpy(s->iv, iv, iv_len); + memset(s->iv + iv_len, 0, s->block_size - iv_len); + + s->block = block; + + return s; +} + +static const struct kripto_mode_desc cfb = +{ + &cfb_create, + &kripto_block_size +}; + +kripto_mode_desc *const kripto_mode_cfb = &cfb; diff --git a/lib/mode/ctr.c b/lib/mode/ctr.c index 6d3bf8c..abe825f 100644 --- a/lib/mode/ctr.c +++ b/lib/mode/ctr.c @@ -15,7 +15,6 @@ #include #include #include -#include #include #include @@ -48,9 +47,6 @@ static size_t ctr_crypt size_t i; unsigned int n; - assert(in); - assert(out); - for(i = 0; i < len; i++) { if(s->used == s->block_size) @@ -78,8 +74,6 @@ static size_t ctr_prng size_t i; unsigned int n; - assert(out); - for(i = 0; i < len; i++) { if(s->used == s->block_size) @@ -99,10 +93,11 @@ static size_t ctr_prng static void ctr_destroy(kripto_stream *s) { - kripto_memwipe(s, sizeof(struct kripto_stream) + kripto_memwipe(s, sizeof(kripto_stream) + (s->block_size << 1) - + sizeof(struct kripto_stream_desc) + + sizeof(kripto_stream_desc) ); + free(s); } @@ -117,24 +112,20 @@ static kripto_stream *ctr_create kripto_block_desc *b; struct kripto_stream_desc *stream; - assert(block); - b = kripto_block_get_desc(block); - assert(iv_len > kripto_block_size(b)); - - s = malloc(sizeof(struct kripto_stream) + s = malloc(sizeof(kripto_stream) + (kripto_block_size(b) << 1) - + sizeof(struct kripto_stream_desc) + + sizeof(kripto_stream_desc) ); if(!s) return 0; s->block_size = kripto_block_size(b); stream = (struct kripto_stream_desc *) - ((uint8_t *)s + sizeof(struct kripto_stream)); + ((uint8_t *)s + sizeof(kripto_stream)); - s->x = (uint8_t *)stream + sizeof(struct kripto_stream_desc); + s->x = (uint8_t *)stream + sizeof(kripto_stream_desc); s->buf = s->x + s->block_size; stream->encrypt = &ctr_crypt; diff --git a/lib/mode/ecb.c b/lib/mode/ecb.c index c1c05d3..39b2acb 100644 --- a/lib/mode/ecb.c +++ b/lib/mode/ecb.c @@ -15,7 +15,6 @@ #include #include #include -#include #include #include @@ -44,8 +43,6 @@ static size_t ecb_encrypt { size_t i; - if(len & (s->block_size - 1)) return 0; - for(i = 0; i < len; i += s->block_size) kripto_block_encrypt(s->block, CU8(pt) + i, U8(ct) + i); @@ -62,8 +59,6 @@ static size_t ecb_decrypt { size_t i; - if(len & (s->block_size - 1)) return 0; - for(i = 0; i < len; i += s->block_size) kripto_block_decrypt(s->block, CU8(ct) + i, U8(pt) + i); @@ -76,6 +71,7 @@ static void ecb_destroy(kripto_stream *s) + s->block_size + sizeof(kripto_stream_desc) ); + free(s); } @@ -99,8 +95,6 @@ static kripto_stream *ecb_create (void)iv; (void)iv_len; - assert(block); - assert(!iv_len); b = kripto_block_get_desc(block); diff --git a/lib/mode/ofb.c b/lib/mode/ofb.c index c2b5f9d..67a8cf8 100644 --- a/lib/mode/ofb.c +++ b/lib/mode/ofb.c @@ -15,7 +15,6 @@ #include #include #include -#include #include #include @@ -46,9 +45,6 @@ static size_t ofb_crypt { size_t i; - assert(in); - assert(out); - for(i = 0; i < len; i++) { if(s->used == s->block_size) @@ -72,8 +68,6 @@ static size_t ofb_prng { size_t i; - assert(out); - for(i = 0; i < len; i++) { if(s->used == s->block_size) @@ -94,6 +88,7 @@ static void ofb_destroy(kripto_stream *s) + sizeof(struct kripto_stream_desc) + s->block_size ); + free(s); } @@ -108,12 +103,8 @@ static kripto_stream *ofb_create kripto_block_desc *b; struct kripto_stream_desc *stream; - assert(block); - b = kripto_block_get_desc(block); - assert(iv_len > kripto_block_size(b)); - s = malloc(sizeof(struct kripto_stream) + sizeof(struct kripto_stream_desc) + kripto_block_size(b) -- 2.11.4.GIT