All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ch.bfh.unicrypt.crypto.schemes.padding.abstracts.AbstractReversibleByteArrayPaddingScheme Maven / Gradle / Ivy

/*
 * UniCrypt
 *
 *  UniCrypt(tm): Cryptographical framework allowing the implementation of cryptographic protocols e.g. e-voting
 *  Copyright (c) 2016 Bern University of Applied Sciences (BFH), Research Institute for
 *  Security in the Information Society (RISIS), E-Voting Group (EVG)
 *  Quellgasse 21, CH-2501 Biel, Switzerland
 *
 *  Licensed under Dual License consisting of:
 *  1. GNU Affero General Public License (AGPL) v3
 *  and
 *  2. Commercial license
 *
 *
 *  1. This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see .
 *
 *
 *  2. Licensees holding valid commercial licenses for UniCrypt may use this file in
 *   accordance with the commercial license agreement provided with the
 *   Software or, alternatively, in accordance with the terms contained in
 *   a written agreement between you and Bern University of Applied Sciences (BFH), Research Institute for
 *   Security in the Information Society (RISIS), E-Voting Group (EVG)
 *   Quellgasse 21, CH-2501 Biel, Switzerland.
 *
 *
 *   For further information contact 
 *
 *
 * Redistributions of files must retain the above copyright notice.
 */
package ch.bfh.unicrypt.crypto.schemes.padding.abstracts;

import ch.bfh.unicrypt.crypto.schemes.padding.interfaces.ReversiblePaddingScheme;
import ch.bfh.unicrypt.helper.array.classes.ByteArray;
import ch.bfh.unicrypt.helper.random.RandomByteSequence;
import ch.bfh.unicrypt.math.algebra.concatenative.classes.ByteArrayElement;
import ch.bfh.unicrypt.math.algebra.concatenative.classes.ByteArrayMonoid;
import ch.bfh.unicrypt.math.algebra.general.interfaces.Element;
import ch.bfh.unicrypt.math.function.abstracts.AbstractFunction;
import ch.bfh.unicrypt.math.function.interfaces.Function;

/**
 *
 * @author R. Haenni 
 */
public abstract class AbstractReversibleByteArrayPaddingScheme
	   extends AbstractByteArrayPaddingScheme
	   implements ReversiblePaddingScheme {

	private static final long serialVersionUID = 1L;

	private Function unpaddingFunction;

	protected AbstractReversibleByteArrayPaddingScheme(ByteArrayMonoid paddingSpace) {
		super(paddingSpace);
	}

	@Override
	public Function getUnpaddingFunction() {
		if (this.unpaddingFunction == null) {
			this.unpaddingFunction = this.abstractGetUnpaddingFunction();
		}
		return this.unpaddingFunction;
	}

	@Override
	public ByteArrayElement unpad(final Element element) {
		return (ByteArrayElement) this.getUnpaddingFunction().apply(element);
	}

	protected Function abstractGetUnpaddingFunction() {
		return new AbstractFunction(
			   this.paddingSpace, this.messageSpace) {
			@Override
			protected ByteArrayElement abstractApply(ByteArrayElement element, RandomByteSequence randomByteSequence) {
				ByteArray byteArray = element.getValue();
				ByteArray result = byteArray.extractPrefix(byteArray.getLength() - getUnpaddingLength(byteArray));
				return ByteArrayMonoid.getInstance().getElement(result);
			}
		};
	}

	private int getUnpaddingLength(ByteArray byteArray) {
		if (this.abstractEndsWithLength()) {
			return byteArray.getByteAt(byteArray.getLength() - 1);
		}
		Byte separator = this.abstractGetSeparator();
		if (separator != null) {
			int i = byteArray.getLength() - 1;
			while (byteArray.getByteAt(i) != separator) {
				i--;
			}
			return byteArray.getLength() - i;
		}
		throw new IllegalArgumentException();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy