com.google.crypto.tink.signature.internal.SigUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tink-android Show documentation
Show all versions of tink-android Show documentation
Tink is a small cryptographic library that provides a safe, simple, agile and fast way to accomplish some common cryptographic tasks.
The newest version!
// Copyright 2017 Google LLC
//
// 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.signature.internal;
import com.google.crypto.tink.proto.EcdsaSignatureEncoding;
import com.google.crypto.tink.proto.EllipticCurveType;
import com.google.crypto.tink.proto.HashType;
import com.google.crypto.tink.subtle.EllipticCurves;
import com.google.crypto.tink.subtle.Enums;
import com.google.protobuf.ByteString;
import java.math.BigInteger;
import java.security.GeneralSecurityException;
/** Utility functions to convert to and from signature-related proto. */
public final class SigUtil {
static final String INVALID_PARAMS = "Invalid ECDSA parameters";
/**
* Converts protobuf enum {@code HashType} to raw Java enum {@code Enums.HashType}.
*
* @throws GeneralSecurityException if the HashType is not SHA256, SHA384, or SHA512.
*/
public static Enums.HashType toHashType(HashType hash) throws GeneralSecurityException {
switch (hash) {
case SHA256:
return Enums.HashType.SHA256;
case SHA384:
return Enums.HashType.SHA384;
case SHA512:
return Enums.HashType.SHA512;
default:
break;
}
throw new GeneralSecurityException("unsupported hash type: " + hash.name());
}
/** Converts protobuf enum {@code EllipticCurveType} to raw Java enum {code CurveType}. */
public static EllipticCurves.CurveType toCurveType(EllipticCurveType type)
throws GeneralSecurityException {
switch (type) {
case NIST_P256:
return EllipticCurves.CurveType.NIST_P256;
case NIST_P384:
return EllipticCurves.CurveType.NIST_P384;
case NIST_P521:
return EllipticCurves.CurveType.NIST_P521;
default:
throw new GeneralSecurityException("unknown curve type: " + type.name());
}
}
/**
* Converts protobuf enum {@code EcdsaSignatureEncoding} to raw Java enum {code
* EllipticCurves.EcdsaEncoding}.
*/
public static EllipticCurves.EcdsaEncoding toEcdsaEncoding(EcdsaSignatureEncoding encoding)
throws GeneralSecurityException {
switch (encoding) {
case IEEE_P1363:
return EllipticCurves.EcdsaEncoding.IEEE_P1363;
case DER:
return EllipticCurves.EcdsaEncoding.DER;
default:
throw new GeneralSecurityException("unknown ECDSA encoding: " + encoding.name());
}
}
/**
* Returns the unsigned byte representation of the input BigInteger. BigInteger's toByteArray
* returns a two's complement representation of non-negative integers, which might include an
* extra zero byte at position 0 (in big endian).
*/
public static ByteString toUnsignedIntByteString(BigInteger i) {
byte[] twosComplement = i.toByteArray();
if (twosComplement[0] == 0x00) {
return ByteString.copyFrom(twosComplement, 1, twosComplement.length - 1);
}
return ByteString.copyFrom(twosComplement);
}
private SigUtil() {}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy