00001 /***************************************************************************** 00002 * Copyright (C) 2012 by Benjamin Hadorn (b_hadorn@bluewin.ch) 00003 ***************************************************************************** 00004 * Project : Zeus Base Library 00005 * Module : RSACrypter 00006 * Package : Zeus.ZeusBase.Security 00007 * Author : Benjamin Hadorn 00008 * Date : 15.01.2012 00009 * System : Zeus-Framework 00010 ***************************************************************************** 00011 * Licence: * 00012 * This library is free software; you can redistribute it and/or modify * 00013 * it under the terms of the GNU Lesser General Public License as * 00014 * published by the Free Software Foundation; either version * 00015 * 2.1 of the License, or (at your option) any later version. * 00016 * * 00017 * This library is distributed in the hope that it will be useful, * 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00020 * GNU Lesser General Public License for more details. * 00021 * * 00022 * You should have received a copy of the GNU Lesser General Public * 00023 * License along with this library; if not, write to the Free Software * 00024 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA * 00025 *****************************************************************************/ 00026 00027 /***************************************************************************** 00028 * Changes: 00029 *****************************************************************************/ 00030 00031 #ifndef RSACrypterH 00032 #define RSACrypterH 00033 00034 #include <zeusbase/System/BigInteger.hpp> 00035 #include <zeusbase/System/ZObject.h> 00036 00037 BEGIN_NAMESPACE_Zeus 00038 00039 /*************************************************************************/ 00042 /*************************************************************************/ 00043 struct TypeRSAKey 00044 { 00046 Int1024 i1024_N; 00048 Int1024 i1024_Exp; 00049 }; 00050 00051 /*************************************************************************/ 00054 /*************************************************************************/ 00055 typedef TypeRSAKey TypePublicRSAKey; 00056 00057 /*************************************************************************/ 00060 /*************************************************************************/ 00061 struct TypePrivateRSAKey : TypeRSAKey 00062 { 00064 //Int1024 i1024_N; 00066 //Int1024 i1024_Exp; 00067 //First prime 00068 Int1024 i1024_P; 00069 //2nd prime 00070 Int1024 i1024_Q; 00071 //First prime - 1 00072 Int1024 i1024_Pm1; 00073 //2nd prime - 1 00074 Int1024 i1024_Qm1; 00075 //First component D1 00076 Int1024 i1024_D1; 00077 //First component D2 00078 Int1024 i1024_D2; 00079 }; 00080 00081 /*****************************************************************************/ 00085 /*****************************************************************************/ 00086 zeusbase_class TRSACrypter : public TZObject 00087 { 00088 public: 00089 TRSACrypter(); 00090 00091 static void generateComponents(const Int1024& ri1024P, 00092 const Int1024& ri1024Q, 00093 const Int1024& ri1024E, 00094 TypePublicRSAKey& rPublicKey, 00095 TypePrivateRSAKey& rPrivateKey); 00096 00097 Retval decode(const TypePrivateRSAKey& rKey, const IByteArray& rInput, IByteArray& rOutput); 00098 Retval encode(const TypePublicRSAKey& rKey, const IByteArray& rInput, IByteArray& rOutput); 00099 00100 Retval sign(const TypePrivateRSAKey& rKey, const IByteArray& rInput, IByteArray& rOutput); 00101 Retval verify(const TypePublicRSAKey& rKey, const IByteArray& rInput, IByteArray& rOutput); 00102 00103 protected: 00104 /*************************************************************************/ 00107 /*************************************************************************/ 00108 enum EPaddingMethod 00109 { 00110 etNone = 0, 00111 etPKCS7 = 1, 00112 etZeros = 2, 00113 etANSIX923 = 3, 00114 etISO10126 = 4 00115 }; 00116 00117 Int1024 encode_CRT(const TypePrivateRSAKey& rKey, 00118 const Int1024& ri1024Base) const; 00119 00120 private: 00122 EPaddingMethod m_ePaddingMethod; 00123 00124 void doPadding(const TypeRSAKey& rKey, 00125 const Uint8* pui8Data, 00126 Int iRemBlock, 00127 IByteArray& rOutput); 00128 }; 00129 00130 END_NAMESPACE_Zeus 00131 00132 #endif