⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 api.tex

📁 含有多种公开密钥算法、多种块加密、多种数据流加密、多种HASH函数、多种CheckSum校验、多种MAC校验等几十种加密算法的程序
💻 TEX
📖 第 1 页 / 共 5 页
字号:
similar such matters.The constructor of this object takes a string which specifies various options.If more than one is used, they should be separated by a space. The options arelisted by order of danger (ie the caution you should have about using theoption), with safest first.\noindent\textbf{Option ``seed\_rng''}: If this argument is set, then a ``best effort''attempt will be made to initialize the random number generator. Even if thisargument is \emph{not} passed, if \filename{/dev/random} is available, it willbe used to seed the RNG (via the \type{File\_EntropySource}).\noindent\textbf{Option ``egd\_path=PATH''}: Try to connect to an EGD socket at PATH anduse the output to seed the global RNG. If you do not pass this option, but youdo pass ``seed\_rng'', Botan will attempt to read from an EGD socket at\filename{/var/run/egd-pool}, which is a pseudo-standard location for thesystem's EGD socket.Be aware that if the process listening on the other side of the socket isactively hostile, it is possible for it to accept but ignore the connection,blocking your process.\noindent\textbf{Option ``secure\_memory''}: Try to create a more secure allocator type-- one that either locks allocated memory into RAM, or that memory maps a diskfile that it erases after use. If both are available, it will prefer the memorymapping mechanism, because locking memory requires root privileges on manysystems.On systems that don't (currently) have any specialized allocators, likeWindows, this option is ignored.\noindent\textbf{Option ``thread\_safe''}: The library should use mutexes for guardingaccess to shared resources, such as the memory allocation system. If you passthe ``thread\_safe'' option, and the initializer can't find a useful mutexmodule, it will throw an exception. Thread safety in Botan is basicallyuntested and it is fairly likely there are problems. If you're going to trythis, be ready to have to send bug reports to the mailing list.While most things in Botan are already thread-safe at the object level, a fewobjects (notably the global RNG, the allocators, and the lookup tables) areshared between threads for efficiency or security reasons. Access to theseobjects are protected using mutexes, and I've tried to ensure things arelocked when they need to be, but I probably am missing some cases.\noindent\textbf{Option ``fast\_rng''}: If this option is set, the library will use an\type{ANSI\_X917\_RNG} object, instead of a somewhat slower \type{Randpool}object, as the global random number generator. See the section \textit{RandomNumber Generators} for more details about what these objects are. Don't passthis option unless you know what you're doing; there are many cases where theX9.17 generator does not provide sufficient security (mostly because it has afairly small internal state).\noindent\textbf{Option ``no\_timers''}: The library should not try to find a highresolution timer to use. Don't pass this option unless you \emph{really} knowwhat you're doing.If you do not create a \type{LibraryInitializer} object, pretty much any Botanoperation will fail, because it will be unable to do basic things like getmemory. Note too, that you should be careful to only create one such object.\pagebreak\section{Gotchas}There are a few things to watch out for to prevent problems when using Botan.First and primary of these is to \emph{never} allocate any kind of Botan objectglobally. The problem is that the constructor for such an object will be calledbefore the \type{LibraryInitializer} is created, and the constructor willundoubtedly try to call an object which has not been initialized. If you'relucky your program will die with an uncaught exception. If you're less lucky,it will crash from a memory access error. And if you're really unlucky it\emph{won't} crash, and your program will be in an unknown (but very bad)state. Generally, global variables are bad news anyway, and I can't think ofmany cases where this will cause problems for application code. The librarydoes have some global objects in it, and a great deal of hackery is involvedmaking sure everything is created and destroyed at the right time (and in theright order).The same rule applies for making sure the destructors of all your Botan objectsare called before the \type{LibraryInitializer} is destroyed. This implies youcan't have static variables that are Botan objects inside functions orclasses. This is kind of inelegant, but rarely a real problem in practice.Never create a \type{SecureVector} or \type{SecureBuffer} with a type that isnot a basic integer (\type{byte}, \type{u16bit}, \type{u32bit}, \type{u64bit}).More strongly, if you, as a user of the library, are creating any memory bufferobject that's not a \type{SecureVector<byte>}, you're probably doing somethingwrong (I suppose there may be exceptions to this rule, but not many).Don't include headers you don't have to. Past experience with Botan has shownthat headers get renamed fairly regularly as internal design changes are made,but this need not affect you, if you follow the ``proper procedures''. Usingthe lookup interface defined in \filename{lookup.h} and \filename{look\_pk.h}will save you a great deal of pain in this regard, as it insulates you againstmany such changes.Enclose your \function{main} with a try block, and catch any\type{std::exception} throws. This is not strictly required, but if you don't,and Botan throws an exception, your application would die mysteriously andwithout any error message.Botan's multiple precision integer class (\type{BigInt}) has some of it's lowlevel functions linked as C. This is to make it easier to write implementationsof them in assembly (as then the name mangling technique used by the compilerdoesn't come into play). These functions are not visible in application space,but at link time they could cause conflicts, so you should try to avoid namingfunctions in your code any name starting with \texttt{bigint\_}.\pagebreak\section{The Basic Interface}Botan has two different interfaces. The one documented in this section ismeant more for implementing higher-level types (see the section on filters,later in this manual) than for use by applications.\subsection{Basic Algorithm Abilities}There are a small handful of functions implemented by most of Botan'salgorithm objects. Among these are:\noindent\type{std::string} \function{name}():Returns a human-readable string of the name of this algorithm. Examples ofnames returned are ``Blowfish'' and ``HMAC(MD5)''. You can turn names back intoalgorithm objects using the functions in \filename{lookup.h}.\noindent\type{void} \function{clear}():Clear out the algorithm's internal state. A block cipher object will ``forget''it's key, a hash function will ``forget'' any data put into it, etc.\noindent\function{clone}():This function is central to Botan's name-based interface. The \function{clone}has many different return types, such as \type{BlockCipher*} and\type{HashFunction*}, depending on what kind of object it is called on. Notethat unlike Java's clone, this returns a new object in a ``pristine'' state;that is, operations done on the initial object before calling \function{clone}do not affect the initial state of the new clone.Cloned objects can (and should) be deallocated with the C++ \texttt{delete}operator.\subsection{Keys}A \type{SymmetricKey} is an abstraction for a symmetric key, which isrepresented as an arbitrary length byte string. The major functions of thistype are it's constructors:\noindent\function{SymmetricKey}(\type{u32bit} \arg{length}):This constructor takes creates a new random key of size \arg{length}.\noindent\function{SymmetricKey}(\type{std::string} \arg{str}):The argument \arg{str} is assumed to be a hex string; it is converted to binaryand stored as the key. Whitespace is ignored.\noindent\function{SymmetricKey}(\type{const byte} \arg{input}[], \type{u32bit}\arg{length}):This constructor simply copies it's input.Synonyms for this type include \type{BlockCipher\_Key},\type{StreamCipher\_Key}, \type{MAC\_Key}, and \type{BlockCipherModeIV}(they're all exactly the same thing, the different names just makes it clearwhat the bytes are being used for).\subsection{Symmetrically Keyed Algorithms}Block ciphers, stream ciphers, and MACs all handle keys in pretty much the sameway. To make this similarity explicit, all algorithms of those types arederived from the \type{SymmetricAlgorithm} base class. This type has threefunctions:\noindent\type{void} \function{set\_key}(\type{const byte} \arg{key}[], \type{u32bit}\arg{length}):Most algorithms only accept keys of certain lengths. If you attempt to call\function{set\_key} with a key length that is not supported, the exception\type{Invalid\_Key\_Length} will be thrown. There is also another version of\function{set\_key} that takes a \type{SymmetricKey} as an argument.\noindent\type{bool} \function{valid\_keylength}(\type{u32bit} \arg{length}) const:This function returns true if a key of the given length will be accepted bythe cipher.There are also three constant data members of every \type{SymmetricAlgorithm}object, which specify exactly what limits there are on keys which that objectcan accept:MAXIMUM\_KEYLENGTH: The maximum length of a key. This is at most 32 (256 bits),even if the algorithm actually supports more.MINIMUM\_KEYLENGTH: The minimum length of a key. This is at least 1.KEYLENGTH\_MULTIPLE: The length of the key must be a multiple of this value.In all cases, \function{set\_key} must be called on an object before any dataprocessing (encryption, decryption, etc) is done by that object. If this is notdone, the results are undefined -- that is to say, Botan reserves the right inthis situation to do anything from printing a nasty insulting message on thescreen to dumping core.\subsection{Block Ciphers}Block ciphers implement the interface \type{BlockCipher}, found in\filename{base.h}.\noindent\type{void} \function{encrypt}(\type{const byte} \arg{in}[BLOCK\_SIZE],                               \type{byte} \arg{out}[BLOCK\_SIZE]) const\noindent\type{void} \function{encrypt}(\type{byte} \arg{block}[BLOCK\_SIZE]) constThese functions apply the block cipher transformation to \arg{in} and place theresult in \arg{out}, or encrypts \arg{block} in place (\arg{in} may be the sameas \arg{out}). BLOCK\_SIZE is a constant member of each class, which specifieshow much data a block cipher can process at one time. Note that BLOCK\_SIZE isnot a static class member, like the old BLOCKSIZE was.\type{BlockCipher}s have similar functions \function{decrypt}, which performthe inverse operation.Block ciphers implement the \type{SymmetricAlgorithm} interface.\subsection{Stream Ciphers}Stream ciphers are somewhat different from block ciphers, in that encryptingdata results in changing the internal state of the cipher. Also, you mayencrypt any length of data in one go (in byte amounts).\noindent\type{void} \function{encrypt}(\type{const byte} \arg{in}[], \type{byte}\arg{out}[], \type{u32bit} \arg{length})\noindent\type{void} \function{encrypt}(\type{byte} \arg{data}[], \type{u32bit}\arg{length}):These functions encrypt the arbitrary length (well, less than 4 gigabyte long)string \arg{in} and place it into \arg{out}, or encrypts it in place in\arg{data}. The \function{decrypt} functions look just like\function{encrypt}.Stream ciphers implement the \type{SymmetricAlgorithm} interface.Some stream ciphers support random access to any point in their cipher stream(currently, the only like this in in Botan is SEAL). For such ciphers, calling\type{void} \function{seek}(\type{u32bit} \arg{byte}) will change the cipher'sstate so that it as if the cipher had been keyed as normal, then encrypted\arg{byte} -- 1 bytes of data (so the next byte in the cipher stream is bytenumber \arg{byte}).\subsection{Hash Functions / Message Authentication Codes}Hash functions take their input without producing any output, only producinganything when all input has already taken place. MACs are very similar, but areadditionally keyed. Both of these are derived from the base class\type{BufferedComputation}, which has the following functions.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -