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

com.google.crypto.tink.prf.PrfSetWrapper Maven / Gradle / Ivy

Go to download

Tink is a small cryptographic library that provides a safe, simple, agile and fast way to accomplish some common cryptographic tasks.

There is a newer version: 1.16.0
Show newest version
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////
package com.google.crypto.tink.prf;

import com.google.crypto.tink.PrimitiveSet;
import com.google.crypto.tink.PrimitiveWrapper;
import com.google.crypto.tink.Registry;
import com.google.crypto.tink.proto.OutputPrefixType;
import com.google.errorprone.annotations.Immutable;
import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * PrfSetWrapper is the implementation of PrimitiveWrapper for the PrfSet primitive.
 *
 * 

The returned primitive has instances of {@code Prf} for each key in the KeySet. The individual * Prf instances can then be used to compute psuedo-random sequences from the underlying key. */ @Immutable public class PrfSetWrapper implements PrimitiveWrapper { private static class WrappedPrfSet extends PrfSet { // This map is constructed using Collections.unmodifiableMap @SuppressWarnings("Immutable") private final Map keyIdToPrfMap; private final int primaryKeyId; private WrappedPrfSet(PrimitiveSet primitives) throws GeneralSecurityException { if (primitives.getRawPrimitives().isEmpty()) { throw new GeneralSecurityException("No primitives provided."); } if (primitives.getPrimary() == null) { throw new GeneralSecurityException("Primary key not set."); } primaryKeyId = primitives.getPrimary().getKeyId(); List> entries = primitives.getRawPrimitives(); Map mutablePrfMap = new HashMap<>(); for (PrimitiveSet.Entry entry : entries) { if (!entry.getOutputPrefixType().equals(OutputPrefixType.RAW)) { throw new GeneralSecurityException( "Key " + entry.getKeyId() + " has non raw prefix type"); } // PrfSets pushed into this wrapper should have have only one key with ID '0'. if (entry.getPrimitive().getPrfs().size() > 1) { throw new GeneralSecurityException( "More PRFs than expected in KeyTypeManager for key " + entry.getKeyId()); } // Likewise, the key IDs of the PrfSet passed mutablePrfMap.put( entry.getKeyId(), entry.getPrimitive().getPrfs().get(entry.getPrimitive().getPrimaryId())); } keyIdToPrfMap = Collections.unmodifiableMap(mutablePrfMap); } @Override public int getPrimaryId() { return primaryKeyId; } @Override public Map getPrfs() throws GeneralSecurityException { return keyIdToPrfMap; } } @Override public PrfSet wrap(PrimitiveSet set) throws GeneralSecurityException { return new WrappedPrfSet(set); } @Override public Class getPrimitiveClass() { return PrfSet.class; } public static void register() throws GeneralSecurityException { Registry.registerPrimitiveWrapper(new PrfSetWrapper()); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy