KMACwithSHAKE128_params.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  4. using System;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Nist
  6. {
  7. /// <summary>
  8. /// KMACwithSHAKE128-params ::= SEQUENCE {
  9. /// kMACOutputLength INTEGER DEFAULT 256, -- Output length in bits
  10. /// customizationString OCTET STRING DEFAULT ''H
  11. /// }
  12. /// </summary>
  13. public class KMacWithShake128Params : Asn1Encodable
  14. {
  15. private static readonly byte[] EMPTY_STRING = new byte[0];
  16. private static readonly int DEF_LENGTH = 256;
  17. private readonly int outputLength;
  18. private readonly byte[] customizationString;
  19. public KMacWithShake128Params(int outputLength)
  20. {
  21. this.outputLength = outputLength;
  22. this.customizationString = EMPTY_STRING;
  23. }
  24. public KMacWithShake128Params(int outputLength, byte[] customizationString)
  25. {
  26. this.outputLength = outputLength;
  27. this.customizationString = Arrays.Clone(customizationString);
  28. }
  29. public static KMacWithShake128Params GetInstance(object o)
  30. {
  31. if (o is KMacWithShake128Params)
  32. {
  33. return (KMacWithShake128Params)o;
  34. }
  35. else if (o != null)
  36. {
  37. return new KMacWithShake128Params(Asn1Sequence.GetInstance(o));
  38. }
  39. return null;
  40. }
  41. private KMacWithShake128Params(Asn1Sequence seq)
  42. {
  43. if (seq.Count > 2)
  44. throw new InvalidOperationException("sequence size greater than 2");
  45. if (seq.Count == 2)
  46. {
  47. this.outputLength = DerInteger.GetInstance(seq[0]).IntValueExact;
  48. this.customizationString = Arrays.Clone(Asn1OctetString.GetInstance(seq[1]).GetOctets());
  49. }
  50. else if (seq.Count == 1)
  51. {
  52. if (seq[0] is DerInteger)
  53. {
  54. this.outputLength = DerInteger.GetInstance(seq[0]).IntValueExact;
  55. this.customizationString = EMPTY_STRING;
  56. }
  57. else
  58. {
  59. this.outputLength = DEF_LENGTH;
  60. this.customizationString = Arrays.Clone(Asn1OctetString.GetInstance(seq[0]).GetOctets());
  61. }
  62. }
  63. else
  64. {
  65. this.outputLength = DEF_LENGTH;
  66. this.customizationString = EMPTY_STRING;
  67. }
  68. }
  69. public int OutputLength
  70. {
  71. get { return outputLength; }
  72. }
  73. public byte[] CustomizationString
  74. {
  75. get { return Arrays.Clone(customizationString); }
  76. }
  77. public override Asn1Object ToAsn1Object()
  78. {
  79. Asn1EncodableVector v = new Asn1EncodableVector();
  80. if (outputLength != DEF_LENGTH)
  81. {
  82. v.Add(new DerInteger(outputLength));
  83. }
  84. if (customizationString.Length != 0)
  85. {
  86. v.Add(new DerOctetString(CustomizationString));
  87. }
  88. return new DerSequence(v);
  89. }
  90. }
  91. }
  92. #pragma warning restore
  93. #endif