From be9181698a3f3f84accb6dc8ae5e1b4fd81a2b22 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Sat, 27 Aug 2016 07:49:43 +0200 Subject: [PATCH] Add remove padding --- source/tanya/crypto/{padding.d => cipher.d} | 100 ++++++++++++++++++-- 1 file changed, 94 insertions(+), 6 deletions(-) rename source/tanya/crypto/{padding.d => cipher.d} (63%) diff --git a/source/tanya/crypto/padding.d b/source/tanya/crypto/cipher.d similarity index 63% rename from source/tanya/crypto/padding.d rename to source/tanya/crypto/cipher.d index fbf7ec5..60d1587 100644 --- a/source/tanya/crypto/padding.d +++ b/source/tanya/crypto/cipher.d @@ -8,14 +8,12 @@ * Mozilla Public License, v. 2.0). * Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner) */ -module tanya.crypto.padding; +module tanya.crypto.cipher; import tanya.memory; import std.algorithm.iteration; import std.typecons; -@nogc: - /** * Supported padding mode. * @@ -31,13 +29,13 @@ enum Mode /** * Params: - * allocator = Allocator that should be used if the block should be extended - * or a new block should be added. * input = Sequence that should be padded. * mode = Padding mode. * blockSize = Block size. + * allocator = Allocator that should be used if the block should be extended + * or a new block should be added. * - * Returns: The functions modifies the initial array and returns it. + * Returns: The function modifies the initial array and returns it. * * See_Also: * $(D_PSYMBOL Mode) @@ -189,3 +187,93 @@ unittest defaultAllocator.finalize(input); } } + +/** + * Params: + * input = Sequence that should be padded. + * mode = Padding mode. + * blockSize = Block size. + * allocator = Allocator that should be used for freeing the space allocated + * for the padding. + * + * Returns: The function modifies the initial array and returns it. + * + * See_Also: + * $(D_PSYMBOL applyPadding) + */ +ref ubyte[] removePadding(ref ubyte[] input, + in Mode mode, + in ushort blockSize, + Allocator allocator = defaultAllocator) +in +{ + assert(input.length != 0); + assert(input.length % 64 == 0); +} +body +{ + final switch (mode) with (Mode) + { + case zero: + break; + case pkcs7: + case ansiX923: + immutable last = input[$ - 1]; + + allocator.shrinkArray(input, last ? last : blockSize); + break; + } + + return input; +} + +/// +unittest +{ + { // Zeros + auto input = defaultAllocator.makeArray!ubyte(50); + auto inputDup = defaultAllocator.makeArray!ubyte(50); + + applyPadding(input, Mode.zero, 64); + applyPadding(inputDup, Mode.zero, 64); + + removePadding(input, Mode.zero, 64); + assert(input == inputDup); + + defaultAllocator.finalize(input); + defaultAllocator.finalize(inputDup); + + } + { // PKCS#7 + auto input = defaultAllocator.makeArray!ubyte(50); + auto inputDup = defaultAllocator.makeArray!ubyte(50); + for (ubyte i; i < 40; ++i) + { + input[i] = i; + inputDup[i] = i; + } + + applyPadding(input, Mode.pkcs7, 64); + removePadding(input, Mode.pkcs7, 64); + assert(input == inputDup); + + defaultAllocator.finalize(input); + defaultAllocator.finalize(inputDup); + } + { // ANSI X.923 + auto input = defaultAllocator.makeArray!ubyte(50); + auto inputDup = defaultAllocator.makeArray!ubyte(50); + for (ubyte i; i < 40; ++i) + { + input[i] = i; + inputDup[i] = i; + } + + applyPadding(input, Mode.pkcs7, 64); + removePadding(input, Mode.pkcs7, 64); + assert(input == inputDup); + + defaultAllocator.finalize(input); + defaultAllocator.finalize(inputDup); + } +}