Poly1305KeyGenerator.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Crypto;
  4. using Org.BouncyCastle.Crypto.Macs;
  5. using Org.BouncyCastle.Crypto.Parameters;
  6. using Org.BouncyCastle.Math;
  7. namespace Org.BouncyCastle.Crypto.Generators
  8. {
  9. /// <summary>
  10. /// Generates keys for the Poly1305 MAC.
  11. /// </summary>
  12. /// <remarks>
  13. /// Poly1305 keys are 256 bit keys consisting of a 128 bit secret key used for the underlying block
  14. /// cipher followed by a 128 bit {@code r} value used for the polynomial portion of the Mac. <br/>
  15. /// The {@code r} value has a specific format with some bits required to be cleared, resulting in an
  16. /// effective 106 bit key. <br/>
  17. /// A separately generated 256 bit key can be modified to fit the Poly1305 key format by using the
  18. /// {@link #clamp(byte[])} method to clear the required bits.
  19. /// </remarks>
  20. /// <seealso cref="Poly1305"/>
  21. public class Poly1305KeyGenerator
  22. : CipherKeyGenerator
  23. {
  24. private const byte R_MASK_LOW_2 = (byte)0xFC;
  25. private const byte R_MASK_HIGH_4 = (byte)0x0F;
  26. /// <summary>
  27. /// Initialises the key generator.
  28. /// </summary>
  29. /// <remarks>
  30. /// Poly1305 keys are always 256 bits, so the key length in the provided parameters is ignored.
  31. /// </remarks>
  32. protected override void engineInit(KeyGenerationParameters param)
  33. {
  34. // Poly1305 keys are always 256 bits
  35. this.random = param.Random;
  36. this.strength = 32;
  37. }
  38. /// <summary>
  39. /// Generates a 256 bit key in the format required for Poly1305 - e.g.
  40. /// <code>k[0] ... k[15], r[0] ... r[15]</code> with the required bits in <code>r</code> cleared
  41. /// as per <see cref="Clamp(byte[])"/>.
  42. /// </summary>
  43. protected override byte[] engineGenerateKey()
  44. {
  45. byte[] key = base.engineGenerateKey();
  46. Clamp(key);
  47. return key;
  48. }
  49. /// <summary>
  50. /// Modifies an existing 32 byte key value to comply with the requirements of the Poly1305 key by
  51. /// clearing required bits in the <code>r</code> (second 16 bytes) portion of the key.<br/>
  52. /// Specifically:
  53. /// <ul>
  54. /// <li>r[3], r[7], r[11], r[15] have top four bits clear (i.e., are {0, 1, . . . , 15})</li>
  55. /// <li>r[4], r[8], r[12] have bottom two bits clear (i.e., are in {0, 4, 8, . . . , 252})</li>
  56. /// </ul>
  57. /// </summary>
  58. /// <param name="key">a 32 byte key value <code>k[0] ... k[15], r[0] ... r[15]</code></param>
  59. public static void Clamp(byte[] key)
  60. {
  61. /*
  62. * Key is k[0] ... k[15], r[0] ... r[15] as per poly1305_aes_clamp in ref impl.
  63. */
  64. if (key.Length != 32)
  65. throw new ArgumentException("Poly1305 key must be 256 bits.");
  66. /*
  67. * r[3], r[7], r[11], r[15] have top four bits clear (i.e., are {0, 1, . . . , 15})
  68. */
  69. key[3] &= R_MASK_HIGH_4;
  70. key[7] &= R_MASK_HIGH_4;
  71. key[11] &= R_MASK_HIGH_4;
  72. key[15] &= R_MASK_HIGH_4;
  73. /*
  74. * r[4], r[8], r[12] have bottom two bits clear (i.e., are in {0, 4, 8, . . . , 252}).
  75. */
  76. key[4] &= R_MASK_LOW_2;
  77. key[8] &= R_MASK_LOW_2;
  78. key[12] &= R_MASK_LOW_2;
  79. }
  80. /// <summary>
  81. /// Checks a 32 byte key for compliance with the Poly1305 key requirements, e.g.
  82. /// <code>k[0] ... k[15], r[0] ... r[15]</code> with the required bits in <code>r</code> cleared
  83. /// as per <see cref="Clamp(byte[])"/>.
  84. /// </summary>
  85. /// <param name="key">Key.</param>
  86. /// <exception cref="System.ArgumentException">if the key is of the wrong length, or has invalid bits set
  87. /// in the <code>r</code> portion of the key.</exception>
  88. public static void CheckKey(byte[] key)
  89. {
  90. if (key.Length != 32)
  91. throw new ArgumentException("Poly1305 key must be 256 bits.");
  92. CheckMask(key[3], R_MASK_HIGH_4);
  93. CheckMask(key[7], R_MASK_HIGH_4);
  94. CheckMask(key[11], R_MASK_HIGH_4);
  95. CheckMask(key[15], R_MASK_HIGH_4);
  96. CheckMask(key[4], R_MASK_LOW_2);
  97. CheckMask(key[8], R_MASK_LOW_2);
  98. CheckMask(key[12], R_MASK_LOW_2);
  99. }
  100. private static void CheckMask(byte b, byte mask)
  101. {
  102. if ((b & (~mask)) != 0)
  103. throw new ArgumentException("Invalid format for r portion of Poly1305 key.");
  104. }
  105. }
  106. }
  107. #endif