ClpParameterUtilities.pas 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. { *********************************************************************************** }
  2. { * CryptoLib Library * }
  3. { * Copyright (c) 2018 - 20XX Ugochukwu Mmaduekwe * }
  4. { * Github Repository <https://github.com/Xor-el> * }
  5. { * Distributed under the MIT software license, see the accompanying file LICENSE * }
  6. { * or visit http://www.opensource.org/licenses/mit-license.php. * }
  7. { * Acknowledgements: * }
  8. { * * }
  9. { * Thanks to Sphere 10 Software (http://www.sphere10.com/) for sponsoring * }
  10. { * development of this library * }
  11. { * ******************************************************************************* * }
  12. (* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *)
  13. unit ClpParameterUtilities;
  14. {$I ..\Include\CryptoLib.inc}
  15. interface
  16. uses
  17. SysUtils,
  18. Generics.Collections,
  19. ClpKeyParameter,
  20. ClpIKeyParameter,
  21. ClpIDerObjectIdentifier,
  22. ClpNistObjectIdentifiers,
  23. ClpCryptoLibTypes;
  24. resourcestring
  25. SAlgorithmNil = 'Algorithm Cannot be Nil';
  26. SAlgorithmNotRecognised = 'Algorithm "%s" not Recognised.';
  27. type
  28. TParameterUtilities = class sealed(TObject)
  29. strict private
  30. class var
  31. Falgorithms: TDictionary<String, String>;
  32. FbasicIVSizes: TDictionary<String, Int32>;
  33. class procedure AddAlgorithm(const canonicalName: String;
  34. aliases: array of String); static;
  35. class procedure AddBasicIVSizeEntries(size: Int32;
  36. algorithms: array of String); static;
  37. class constructor CreateParameterUtilities();
  38. class destructor DestroyParameterUtilities();
  39. public
  40. class function GetCanonicalAlgorithmName(const algorithm: String): String;
  41. static; inline;
  42. class function CreateKeyParameter(const algOid: IDerObjectIdentifier;
  43. keyBytes: TCryptoLibByteArray): IKeyParameter; overload; static; inline;
  44. class function CreateKeyParameter(const algorithm: String;
  45. keyBytes: TCryptoLibByteArray): IKeyParameter; overload; static;
  46. class function CreateKeyParameter(const algOid: IDerObjectIdentifier;
  47. keyBytes: TCryptoLibByteArray; offset, length: Int32): IKeyParameter;
  48. overload; static; inline;
  49. class function CreateKeyParameter(const algorithm: String;
  50. keyBytes: TCryptoLibByteArray; offset, length: Int32): IKeyParameter;
  51. overload; static;
  52. class procedure Boot(); static;
  53. end;
  54. implementation
  55. { TParameterUtilities }
  56. class procedure TParameterUtilities.AddAlgorithm(const canonicalName: String;
  57. aliases: array of String);
  58. var
  59. alias: string;
  60. begin
  61. Falgorithms.Add(canonicalName, canonicalName);
  62. for alias in aliases do
  63. begin
  64. Falgorithms.Add(alias, canonicalName);
  65. end;
  66. end;
  67. class procedure TParameterUtilities.AddBasicIVSizeEntries(size: Int32;
  68. algorithms: array of String);
  69. var
  70. algorithm: string;
  71. begin
  72. for algorithm in algorithms do
  73. begin
  74. FbasicIVSizes.Add(algorithm, size);
  75. end;
  76. end;
  77. class procedure TParameterUtilities.Boot;
  78. begin
  79. Falgorithms := TDictionary<String, String>.Create();
  80. FbasicIVSizes := TDictionary<string, Integer>.Create();
  81. TNistObjectIdentifiers.Boot;
  82. AddAlgorithm('AES', []);
  83. AddAlgorithm('AES128', ['2.16.840.1.101.3.4.2',
  84. TNistObjectIdentifiers.IdAes128Cbc.ID,
  85. TNistObjectIdentifiers.IdAes128Cfb.ID,
  86. TNistObjectIdentifiers.IdAes128Ecb.ID,
  87. TNistObjectIdentifiers.IdAes128Ofb.ID]);
  88. AddAlgorithm('AES192', ['2.16.840.1.101.3.4.22',
  89. TNistObjectIdentifiers.IdAes192Cbc.ID,
  90. TNistObjectIdentifiers.IdAes192Cfb.ID,
  91. TNistObjectIdentifiers.IdAes192Ecb.ID,
  92. TNistObjectIdentifiers.IdAes192Ofb.ID]);
  93. AddAlgorithm('AES256', ['2.16.840.1.101.3.4.42',
  94. TNistObjectIdentifiers.IdAes256Cbc.ID,
  95. TNistObjectIdentifiers.IdAes256Cfb.ID,
  96. TNistObjectIdentifiers.IdAes256Ecb.ID,
  97. TNistObjectIdentifiers.IdAes256Ofb.ID]);
  98. AddBasicIVSizeEntries(16, ['AES', 'AES128', 'AES192', 'AES256']);
  99. end;
  100. class function TParameterUtilities.GetCanonicalAlgorithmName(const algorithm
  101. : String): String;
  102. begin
  103. Falgorithms.TryGetValue(UpperCase(algorithm), result);
  104. end;
  105. class function TParameterUtilities.CreateKeyParameter(const algorithm: String;
  106. keyBytes: TCryptoLibByteArray): IKeyParameter;
  107. begin
  108. result := CreateKeyParameter(algorithm, keyBytes, 0, System.length(keyBytes));
  109. end;
  110. class function TParameterUtilities.CreateKeyParameter
  111. (const algOid: IDerObjectIdentifier; keyBytes: TCryptoLibByteArray)
  112. : IKeyParameter;
  113. begin
  114. result := CreateKeyParameter(algOid.ID, keyBytes, 0, System.length(keyBytes));
  115. end;
  116. class function TParameterUtilities.CreateKeyParameter(const algorithm: String;
  117. keyBytes: TCryptoLibByteArray; offset, length: Int32): IKeyParameter;
  118. var
  119. canonical: string;
  120. begin
  121. if (algorithm = '') then
  122. begin
  123. raise EArgumentNilCryptoLibException.CreateRes(@SAlgorithmNil);
  124. end;
  125. canonical := GetCanonicalAlgorithmName(algorithm);
  126. if (canonical = '') then
  127. begin
  128. raise ESecurityUtilityCryptoLibException.CreateResFmt
  129. (@SAlgorithmNotRecognised, [algorithm]);
  130. end;
  131. result := TKeyParameter.Create(keyBytes, offset, length) as IKeyParameter;
  132. end;
  133. class function TParameterUtilities.CreateKeyParameter
  134. (const algOid: IDerObjectIdentifier; keyBytes: TCryptoLibByteArray;
  135. offset, length: Int32): IKeyParameter;
  136. begin
  137. result := CreateKeyParameter(algOid.ID, keyBytes, offset, length);
  138. end;
  139. class constructor TParameterUtilities.CreateParameterUtilities;
  140. begin
  141. TParameterUtilities.Boot;
  142. end;
  143. class destructor TParameterUtilities.DestroyParameterUtilities;
  144. begin
  145. Falgorithms.Free;
  146. FbasicIVSizes.Free;
  147. end;
  148. end.