public static byte[] ToUInt16BigEndianBytes(this TpmAlg alg)
        {
            var bytes = new byte[2];

            BinaryPrimitives.WriteUInt16BigEndian(bytes, (ushort)alg);

            return(bytes);
        }
Esempio n. 2
0
        public static (UInt16 size, byte[] name) NameFromTPM2BName(Memory <byte> ab, ref int offset)
        {
            // TCG TPM Rev 2.0, part 2, structures, section 10.5.3, TPM2B_NAME
            // This buffer holds a Name for any entity type.
            // The type of Name in the structure is determined by context and the size parameter.
            var    totalBytes = AuthDataHelper.GetSizedByteArray(ab, ref offset, 2);
            UInt16 totalSize  = 0;

            if (null != totalBytes)
            {
                totalSize = BitConverter.ToUInt16(totalBytes.ToArray().Reverse().ToArray(), 0);
            }
            UInt16 size  = 0;
            var    bytes = AuthDataHelper.GetSizedByteArray(ab, ref offset, 2);

            if (null != bytes)
            {
                size = BitConverter.ToUInt16(bytes.ToArray().Reverse().ToArray(), 0);
            }
            // If size is four, then the Name is a handle.
            if (4 == size)
            {
                throw new Fido2VerificationException("Unexpected handle in TPM2B_NAME");
            }
            // If size is zero, then no Name is present.
            if (0 == size)
            {
                throw new Fido2VerificationException("Unexpected no name found in TPM2B_NAME");
            }
            // Otherwise, the size shall be the size of a TPM_ALG_ID plus the size of the digest produced by the indicated hash algorithm.
            TpmAlg tpmalg = TpmAlg.TPM_ALG_ERROR;

            byte[] name = null;
            if (Enum.IsDefined(typeof(TpmAlg), size))
            {
                tpmalg = (TpmAlg)size;
                if (tpmAlgToDigestSizeMap.ContainsKey(tpmalg))
                {
                    name = AuthDataHelper.GetSizedByteArray(ab, ref offset, tpmAlgToDigestSizeMap[tpmalg]);
                }
            }
            if (totalSize != bytes.Length + name.Length)
            {
                throw new Fido2VerificationException("Unexpected no name found in TPM2B_NAME");
            }
            return(size, name);
        }