blob_id
stringlengths
40
40
directory_id
stringlengths
40
40
path
stringlengths
3
264
content_id
stringlengths
40
40
detected_licenses
sequencelengths
0
85
license_type
stringclasses
2 values
repo_name
stringlengths
5
140
snapshot_id
stringlengths
40
40
revision_id
stringlengths
40
40
branch_name
stringclasses
905 values
visit_date
timestamp[us]date
2015-08-09 11:21:18
2023-09-06 10:45:07
revision_date
timestamp[us]date
1997-09-14 05:04:47
2023-09-17 19:19:19
committer_date
timestamp[us]date
1997-09-14 05:04:47
2023-09-06 06:22:19
github_id
int64
3.89k
681M
star_events_count
int64
0
209k
fork_events_count
int64
0
110k
gha_license_id
stringclasses
22 values
gha_event_created_at
timestamp[us]date
2012-06-07 00:51:45
2023-09-14 21:58:39
gha_created_at
timestamp[us]date
2008-03-27 23:40:48
2023-08-21 23:17:38
gha_language
stringclasses
141 values
src_encoding
stringclasses
34 values
language
stringclasses
1 value
is_vendor
bool
1 class
is_generated
bool
2 classes
length_bytes
int64
3
10.4M
extension
stringclasses
115 values
content
stringlengths
3
10.4M
authors
sequencelengths
1
1
author_id
stringlengths
0
158
4929c4477d9b3a1bcd654dead7f10eba8801328f
253be818ab061b4a00d06d2e7046b787d167308d
/src/ripple/nodestore/impl/DatabaseNodeImp.h
4e62f9ace43866c82030119619da0a4051533a02
[ "MIT-Wu", "MIT", "ISC", "BSL-1.0" ]
permissive
zhaowen28/xag-core
357c12bddbe6f8311d1a46858f0ed7e0e604a063
1ec19ad71dcb4f31c394915d7ad99155e53653d3
refs/heads/master
2021-05-26T15:50:41.692232
2019-11-15T03:52:11
2019-11-15T03:52:11
254,128,733
3
0
NOASSERTION
2020-04-08T15:35:27
2020-04-08T15:35:26
null
UTF-8
C++
false
false
4,135
h
//------------------------------------------------------------------------------ /* This file is part of rippled: https://github.com/ripple/rippled Copyright (c) 2012, 2013 Ripple Labs Inc. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ //============================================================================== #ifndef RIPPLE_NODESTORE_DATABASENODEIMP_H_INCLUDED #define RIPPLE_NODESTORE_DATABASENODEIMP_H_INCLUDED #include <ripple/nodestore/Database.h> #include <ripple/basics/chrono.h> namespace ripple { namespace NodeStore { class DatabaseNodeImp : public Database { public: DatabaseNodeImp() = delete; DatabaseNodeImp(DatabaseNodeImp const&) = delete; DatabaseNodeImp& operator=(DatabaseNodeImp const&) = delete; DatabaseNodeImp( std::string const& name, Scheduler& scheduler, int readThreads, Stoppable& parent, std::unique_ptr<Backend> backend, Section const& config, beast::Journal j) : Database(name, parent, scheduler, readThreads, config, j) , pCache_(std::make_shared<TaggedCache<uint256, NodeObject>>( name, cacheTargetSize, cacheTargetAge, stopwatch(), j)) , nCache_(std::make_shared<KeyCache<uint256>>( name, stopwatch(), cacheTargetSize, cacheTargetAge)) , backend_(std::move(backend)) { assert(backend_); setParent(parent); } ~DatabaseNodeImp() override { // Stop threads before data members are destroyed. stopThreads(); } std::string getName() const override { return backend_->getName(); } std::int32_t getWriteLoad() const override { return backend_->getWriteLoad(); } void import(Database& source) override { importInternal(*backend_.get(), source); } void store(NodeObjectType type, Blob&& data, uint256 const& hash, std::uint32_t seq) override; std::shared_ptr<NodeObject> fetch(uint256 const& hash, std::uint32_t seq) override { return doFetch(hash, seq, *pCache_, *nCache_, false); } bool asyncFetch(uint256 const& hash, std::uint32_t seq, std::shared_ptr<NodeObject>& object) override; bool copyLedger(std::shared_ptr<Ledger const> const& ledger) override { return Database::copyLedger( *backend_, *ledger, pCache_, nCache_, nullptr); } int getDesiredAsyncReadCount(std::uint32_t seq) override { // We prefer a client not fill our cache // We don't want to push data out of the cache // before it's retrieved return pCache_->getTargetSize() / asyncDivider; } float getCacheHitRate() override {return pCache_->getHitRate();} void tune(int size, std::chrono::seconds age) override; void sweep() override; private: // Positive cache std::shared_ptr<TaggedCache<uint256, NodeObject>> pCache_; // Negative cache std::shared_ptr<KeyCache<uint256>> nCache_; // Persistent key/value storage std::unique_ptr<Backend> backend_; std::shared_ptr<NodeObject> fetchFrom(uint256 const& hash, std::uint32_t seq) override { return fetchInternal(hash, *backend_); } void for_each(std::function<void(std::shared_ptr<NodeObject>)> f) override { backend_->for_each(f); } }; } } #endif
98284cf276e8e0c0caa95f4b7bc5f99059e32f1a
036ab5f1101a8fd2647a5c702ad0f85a57bd2eec
/buscasOrdenacoes.cpp
fea50077ac082245aa275bfeef4c1e25115a7816
[]
no_license
arielgoes/routing
5bdfd48e95e493dd7c96838678f9af3ea923ae0b
de0fadff8b8200e9637f73c1b90509a654a1826a
refs/heads/master
2021-09-17T19:42:22.107349
2018-07-04T17:28:58
2018-07-04T17:28:58
null
0
0
null
null
null
null
UTF-8
C++
false
false
5,554
cpp
//PRIMEIRA PARTE DO CÓDIGO DA AULA DO DIA 22/01/2018 - BFS(BUSCA EM LARGURA) int bsf(Grafo *g, int s, int t){ //ONDE S EH O NO INICIAL deque<Node*> fila; Node *aux; int i; for(i=0; i < g->vertices.size(); i++){ g->vertices.at(i)->visited = 0; } aux = buscaNode(g, s); if(aux == NULL) return -1; fila.push_back(aux);//PEGA O ULTIMO ELEMENTO DA FILA while(!fila.empty()){ aux = fila.front();//PEGA O PRIMEIRO ELEMENTO DA FILA fila.pop_front(); aux->visited = 1; if (aux->index == t)return 1; //ITERANDO SOBRE OS ADJ E ADICIONANDO NA FILA for(i=0; i< aux->adj->size(); i++){ if(aux->adj->at(i)->visited == 0){ fila.push_back(aux->adj->at(i)); } } } return 0; } //FAZENDO A BUSCA POR PROFUNDIDADE int dfs(Grafo *g, int s, int t){ //ONDE S EH O NO INICIAL stack<Node*> stack; Node *aux; int i; for(i=0; i < g->vertices.size(); i++){ g->vertices.at(i)->visited = 0; } aux = buscaNode(g, s); if(aux == NULL) return -1; stack.push(aux); while(!stack.empty()){ aux = stack.top(); stack.pop(); aux->visited = 1; if (aux->index == t)return 1; //ITERANDO SOBRE OS ADJ E ADICIONANDO NA FILA for(i=0; i< aux->adj->size(); i++){ if(aux->adj->at(i)->visited == 0){ stack.push(aux->adj->at(i)); } } } return 0; } //PROFUNDIDADE - VERSÃO RECURSIVA void dfsRec(Grafo* g, int s, int t){ int i; Node *aux = buscaNode(g,s); aux->visited = 1; if(aux->index != t){ for(i=0; i < aux->adj->size(); i++){ if(aux->adj->at(i)->visited == 0){ dfsRec(g, aux->adj->at(i)->index, t); } } } } //PROFUNDIDADE - VERSÃO RECURSIVA - Aula 23/01 int temCicloUtil(Grafo* g, Node *aux){ int i; aux->recur = 1; //VERTICE AINDA NAO EXPLOROU TODOS OS FILHOS aux->visited = 1; for(i=0; i < aux->adj->size(); i++){ if(aux->adj->at(i)->recur == 1){ return 1; }else{ temCicloUtil(g, aux->adj->at(i)); } } aux->recur = 0; //VERTICE EXPLOROU TODOS OS FILHOS return 0; } int temCiclo(Grafo *g){ int i; //ZERANDO A ESTRUTURA AUXILIAR (QUE VERIFICA SE O VÉRTICE ESTÁ OU NÃO NA RECURSÃO) for(i=0; i < g->vertices.size();i++){ g->vertices.at(i)->recur = 0; } //CHAMA A FUNÇÃO PARA VERIFICAR CICLIO A PARTIR DE CADA VÉRTICE DO GRAFO. for(i=0; i < g->vertices.size();i++){ if(temCicloUtil(g, g->vertices.at(i))) return 1; } return 0; } void caminhoMinimo(Grafo *g, int s){ //ONDE S EH O NO INICIAL deque<Node*> fila; Node *aux; int i; for(i=0; i < g->vertices.size(); i++){ g->vertices.at(i)->visited = 0; g->vertices.at(i)->prev = -1; g->vertices.at(i)->dist = 0; } aux = buscaNode(g, s); if(aux == NULL) return; fila.push_back(aux);//PEGA O ULTIMO ELEMENTO DA FILA while(!fila.empty()){ aux = fila.front();//PEGA O PRIMEIRO ELEMENTO DA FILA fila.pop_front(); aux->visited = 1; //ITERANDO SOBRE OS ADJ E ADICIONANDO NA FILA for(i=0; i< aux->adj->size(); i++){ if(aux->adj->at(i)->visited == 0){ aux->adj->at(i)->dist = aux->dist + 1; aux->adj->at(i)->prev = aux->prev; fila.push_back(aux->adj->at(i)); } } } } void posOrdemGrafo(Grafo *g, int s, stack<int> *stack){ //VERTICE S = 0; int i; Node *aux = buscaNode(g, s); aux->visited = 1; for(i=0; i< aux->adj->size(); i++){ if(aux->adj->at(i)->visited == 0){ posOrdemGrafo(g, aux->adj->at(i)->index, stack); } } stack->push(s); } Grafo* transporGrafo(Grafo *g){ int i, j; Grafo *grafoTranposto = new Grafo; for(i=0; i < g->vertices.size(); i++){ for(j=0; j < g->vertices.at(i)->adj->size(); j++){ //CRIA OS VÉRTICES INVERTIDOS addArco(grafoTranposto, g->vertices.at(i)->adj->at(j)->index, g->vertices.at(i)->index); } } return grafoTranposto; } void dfsRec2(Grafo* g, int s, vector<int> *vertices){ int i; Node *aux = buscaNode(g,s); aux->visited = 1; vertices->push_back(s); for(i=0; i < aux->adj->size(); i++){ if(aux->adj->at(i)->visited == 0){ dfsRec2(g, aux->adj->at(i)->index, vertices); } } } void componentesConexos(Grafo *g, int s){ Grafo *grafoTransposto; int indiceNo, i, j; Node *nodeAtual; vector<vector<int>*> componentesConexos; //ORDEM EM QUE EH PRECISO APLICAR A BUSCA EM PROFUNDIDADE stack<int> *posOrdemPilha = new stack<int>; posOrdemGrafo (g, s, posOrdemPilha); //CRIA O GRAFO TRANSPOSTO grafoTransposto = transporGrafo(g); while(!posOrdemPilha->empty()){ indiceNo = posOrdemPilha->top(); posOrdemPilha->pop(); nodeAtual = buscaNode(grafoTransposto, indiceNo); if(nodeAtual->visited == 0){ vector<int> *vertices = new vector<int>; dfsRec2(grafoTransposto, indiceNo, vertices); componentesConexos.push_back(vertices); } } for(i=0; i<componentesConexos.size(); i++){ printf("Componente %d: \n", i); for(j=0; j<componentesConexos.at(i)->size(); j++){ printf(" %d", componentesConexos.at(i)->at(j)); } printf("\n"); } } void ordenacaoTopologicaUtil(Grafo *g, Node *aux, stack<int> *pilha){ int i; if(aux->visited == 0){ aux->visited = 1; for(i=0; i< aux->adj->size(); i++){ if(aux->adj->at(i)->visited == 0){ ordenacaoTopologicaUtil(g, aux->adj->at(i), pilha); } } pilha->push(aux->index); } } void ordenacaoTopologica(Grafo *g){ int i; stack<int> *pilha = new stack<int>; //ZERA A ESTRUTURA for(i=0; i<g->vertices.size(); i++){ g->vertices.at(i)->visited = 0; } //CHAMA A FUNCAO PARA TODOS OS VERTICES DO GRAFO for(i=0; i< g->vertices.size(); i++){ ordenacaoTopologicaUtil(g, g->vertices.at(i), pilha); } while(!pilha->empty()){ printf("%d \n", pilha->top()); pilha->pop(); } }
59dafaa0099cf891a0f4d7a8dd4b8771b41730f5
afc64dc90ce7b29a6e3a5300782e51dc7f8da495
/oscpu_chisel/chisel-template/difftest/src/test/csrc/common/sdcard.cpp
8ed74c771af98b1e086683acf6269073ec04dbcd
[ "MulanPSL-2.0", "LicenseRef-scancode-mulanpsl-2.0-en", "LicenseRef-scancode-unknown-license-reference" ]
permissive
liuhangr/riscv1
c28f083b506b921f200b5bc5fb727d0c43ba070b
67606dfaf09949169d06143f62379f6dd8aa7ccf
refs/heads/main
2023-07-10T13:40:08.159020
2021-08-10T09:59:44
2021-08-10T09:59:44
394,829,676
1
0
null
null
null
null
UTF-8
C++
false
false
1,237
cpp
/*************************************************************************************** * Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences * * XiangShan is licensed under Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * * See the Mulan PSL v2 for more details. ***************************************************************************************/ #include "common.h" #include "sdcard.h" FILE *fp = NULL; extern "C" { void sd_setaddr(uint32_t addr) { fseek(fp, addr, SEEK_SET); //printf("set addr to 0x%08x\n", addr); //assert(0); } void sd_read(uint32_t *data) { fread(data, 4, 1, fp); //printf("read data = 0x%08x\n", *data); //assert(0); } void init_sd(void) { fp = fopen("/home/xyn/workloads/debian/riscv-debian.img", "r"); if(!fp) { eprintf(ANSI_COLOR_MAGENTA "[warning] sdcard img not found\n"); } } }
714961d18a7b8a181f51cc94543c780b70d519cd
5838cf8f133a62df151ed12a5f928a43c11772ed
/NT/enduser/netmeeting/av/h323/capsctl.cpp
94ed936c308b8328cb28f5e02b0982ed9b93757b
[]
no_license
proaholic/Win2K3
e5e17b2262f8a2e9590d3fd7a201da19771eb132
572f0250d5825e7b80920b6610c22c5b9baaa3aa
refs/heads/master
2023-07-09T06:15:54.474432
2021-08-11T09:09:14
2021-08-11T09:09:14
null
0
0
null
null
null
null
UTF-8
C++
false
false
52,063
cpp
/* * File: capsctl.cpp * * capability control object implementations * * * Revision History: * * 10/10/96 mikeg created * 06/24/97 mikev - Added T.120 capability to serialized caps and simcaps (interim hack until a * T120 resolver is implemented) * - Retired ResolveEncodeFormat(Audio,Video) and implemented a data-independent * resolution algorithm and exposed method ResolveFormats(). Added support * routines ResolvePermutations(), TestSimultaneousCaps() and * AreSimcaps(). */ #include "precomp.h" UINT g_AudioPacketDurationMs = AUDIO_PACKET_DURATION_LONG; // preferred packet duration BOOL g_fRegAudioPacketDuration = FALSE; // AudioPacketDurationMs from registry PCC_TERMCAPDESCRIPTORS CapsCtl::pAdvertisedSets=NULL; DWORD CapsCtl::dwConSpeed = 0; UINT CapsCtl::uStaticGlobalRefCount=0; UINT CapsCtl::uAdvertizedSize=0; extern HRESULT WINAPI CreateMediaCapability(REFGUID, LPIH323MediaCap *); LPIH323MediaCap CapsCtl::FindHostForID(MEDIA_FORMAT_ID id) { if(pAudCaps && pAudCaps->IsHostForCapID(id)) { return (pAudCaps); } else if (pVidCaps && pVidCaps->IsHostForCapID(id)) { return (pVidCaps); } return NULL; } LPIH323MediaCap CapsCtl::FindHostForMediaType(PCC_TERMCAP pCapability) { if(pCapability->DataType == H245_DATA_AUDIO) { return (pAudCaps); } else if(pCapability->DataType == H245_DATA_VIDEO) { return (pVidCaps); } return NULL; } LPIH323MediaCap CapsCtl::FindHostForMediaGuid(LPGUID pMediaGuid) { if(MEDIA_TYPE_H323VIDEO == *pMediaGuid) { return (pVidCaps); } else if(MEDIA_TYPE_H323AUDIO == *pMediaGuid) { return (pAudCaps); } else return NULL; } ULONG CapsCtl::AddRef() { uRef++; return uRef; } ULONG CapsCtl::Release() { uRef--; if(uRef == 0) { delete this; return 0; } return uRef; } STDMETHODIMP CapsCtl::QueryInterface( REFIID iid, void ** ppvObject) { // this breaks the rules for the official COM QueryInterface because // the interfaces that are queried for are not necessarily real COM // interfaces. The reflexive property of QueryInterface would be broken in // that case. HRESULT hr = E_NOINTERFACE; if(!ppvObject) return hr; *ppvObject = 0; if(iid == IID_IDualPubCap)// satisfy symmetric property of QI { *ppvObject = (IDualPubCap *)this; hr = hrSuccess; AddRef(); } else if(iid == IID_IAppAudioCap ) { if(pAudCaps) { return pAudCaps->QueryInterface(iid, ppvObject); } } else if(iid == IID_IAppVidCap ) { if(pVidCaps) { return pVidCaps->QueryInterface(iid, ppvObject); } } return hr; } CapsCtl::CapsCtl () : uRef(1), pVidCaps(NULL), pAudCaps(NULL), pACapsBuf(NULL), pVCapsBuf(NULL), dwNumInUse(0), bAudioPublicize(TRUE), bVideoPublicize(TRUE), bT120Publicize(TRUE), m_localT120cap(INVALID_MEDIA_FORMAT), m_remoteT120cap(INVALID_MEDIA_FORMAT), m_remoteT120bitrate(0), m_pAudTermCaps(NULL), m_pVidTermCaps(NULL), pSetIDs(NULL), pRemAdvSets(NULL) { uStaticGlobalRefCount++; } CapsCtl::~CapsCtl () { if (pACapsBuf) { MemFree (pACapsBuf); } if (pVCapsBuf) { MemFree (pVCapsBuf); } if (pAudCaps) { pAudCaps->Release(); } if (pVidCaps) { pVidCaps->Release(); } uStaticGlobalRefCount--; if (uStaticGlobalRefCount == 0) { //Free up the sim. caps array if (pAdvertisedSets) { while (pAdvertisedSets->wLength) { //wLength is Zero based MemFree ((VOID *)pAdvertisedSets->pTermCapDescriptorArray[--pAdvertisedSets->wLength]); } MemFree ((VOID *)pAdvertisedSets->pTermCapDescriptorArray); pAdvertisedSets->pTermCapDescriptorArray = NULL; MemFree ((void *) pAdvertisedSets); pAdvertisedSets=NULL; dwNumInUse=0; } } //And the remote array if (pRemAdvSets) { while (pRemAdvSets->wLength) { MemFree ((VOID *)pRemAdvSets->pTermCapDescriptorArray[--pRemAdvSets->wLength]); } MemFree ((void *) pRemAdvSets->pTermCapDescriptorArray); pRemAdvSets->pTermCapDescriptorArray = NULL; MemFree ((void *) pRemAdvSets); pRemAdvSets=NULL; } MemFree (pSetIDs); pSetIDs=NULL; } BOOL CapsCtl::Init() { HRESULT hrLast; int iBase = 1; if (g_capFlags & CAPFLAGS_AV_STREAMS) { hrLast = ::CreateMediaCapability(MEDIA_TYPE_H323AUDIO, &pAudCaps); if(!HR_SUCCEEDED(hrLast)) { goto InitDone; } } if (g_capFlags & CAPFLAGS_AV_STREAMS) { hrLast = ::CreateMediaCapability(MEDIA_TYPE_H323VIDEO, &pVidCaps); if(!HR_SUCCEEDED(hrLast)) { goto InitDone; } } if (pAudCaps) { // Base the capability IDs beginning at 1 (zero is an invalid capability ID!) pAudCaps->SetCapIDBase(iBase); iBase += pAudCaps->GetNumCaps(); } if (pVidCaps) { pVidCaps->SetCapIDBase(iBase); iBase += pVidCaps->GetNumCaps(); } InitDone: m_localT120cap = iBase; return TRUE; } HRESULT CapsCtl::ReInitialize() { HRESULT hr = hrSuccess; int iBase = 1; if (pAudCaps && !pAudCaps->ReInit()) { hr = CAPS_E_SYSTEM_ERROR; goto EXIT; } if (pVidCaps && !pVidCaps->ReInit()) { hr = CAPS_E_SYSTEM_ERROR; goto EXIT; } // Base the capability IDs beginning at 1 (zero is an invalid capability ID!) if (pAudCaps) { pAudCaps->SetCapIDBase(iBase); iBase += pAudCaps->GetNumCaps(); } if (pVidCaps) { pVidCaps->SetCapIDBase(iBase); iBase += pVidCaps->GetNumCaps(); } m_localT120cap = iBase; EXIT: return hr; } const char szNMProdNum[] = "Microsoft\256 NetMeeting(TM)\0"; const char szNM20VerNum[] = "Version 2.0\0"; HRESULT CapsCtl::AddRemoteDecodeCaps(PCC_TERMCAPLIST pTermCapList,PCC_TERMCAPDESCRIPTORS pTermCapDescriptors, PCC_VENDORINFO pVendorInfo) { FX_ENTRY("CapsCtl::AddRemoteDecodeCaps"); HRESULT hr; void *pData=NULL; UINT uSize,x,y,z; //WLength is # of capabilities, not structure length WORD wNDesc; LPIH323MediaCap pMediaCap; if(!pTermCapList && !pTermCapDescriptors) // additional capability descriptors may be added { // at any time return CAPS_E_INVALID_PARAM; } // Check for NM version 2.0 m_fNM20 = FALSE; ASSERT(pVendorInfo); if (pVendorInfo->bCountryCode == USA_H221_COUNTRY_CODE && pVendorInfo->wManufacturerCode == MICROSOFT_H_221_MFG_CODE && pVendorInfo->pProductNumber && pVendorInfo->pVersionNumber && pVendorInfo->pProductNumber->wOctetStringLength == sizeof(szNMProdNum) && pVendorInfo->pVersionNumber->wOctetStringLength == sizeof(szNM20VerNum) && memcmp(pVendorInfo->pProductNumber->pOctetString, szNMProdNum, sizeof(szNMProdNum)) == 0 && memcmp(pVendorInfo->pVersionNumber->pOctetString, szNM20VerNum, sizeof(szNM20VerNum)) == 0 ) { m_fNM20 = TRUE; } // cleanup old term caps if term caps are being added and old caps exist if (pAudCaps) pAudCaps->FlushRemoteCaps(); if (pVidCaps) pVidCaps->FlushRemoteCaps(); m_remoteT120cap = INVALID_MEDIA_FORMAT; // note there is no T120 cap resolver and // this CapsCtl holds exactly one local and remote T120 cap // Copy pTermcapDescriptors to a local copy, (and free any old one) if (pRemAdvSets) { while (pRemAdvSets->wLength) { //0 based MemFree ((VOID *)pRemAdvSets->pTermCapDescriptorArray[--pRemAdvSets->wLength]); } MemFree ((VOID *)pRemAdvSets->pTermCapDescriptorArray); pRemAdvSets->pTermCapDescriptorArray = NULL; MemFree ((VOID *)pRemAdvSets); pRemAdvSets=NULL; } //Ok, walk through the PCC_TERMCAPDESCRIPTORS list, first, allocate memory for the Master PCC_TERMCAPDESCRIPTORS //structure, then each simcap, and the altcaps therin, then copy the data. if (!(pRemAdvSets=(PCC_TERMCAPDESCRIPTORS) MemAlloc (sizeof (CC_TERMCAPDESCRIPTORS) ))){ return CAPS_E_SYSTEM_ERROR; } //How many Descriptors? pRemAdvSets->wLength=pTermCapDescriptors->wLength; if (!(pRemAdvSets->pTermCapDescriptorArray=((H245_TOTCAPDESC_T **)MemAlloc (sizeof (H245_TOTCAPDESC_T*)*pTermCapDescriptors->wLength))) ) { return CAPS_E_SYSTEM_ERROR; } //Once per descriptor... for (x=0;x < pTermCapDescriptors->wLength;x++) { //Allocate memory for the descriptor entry if (!(pRemAdvSets->pTermCapDescriptorArray[x]=(H245_TOTCAPDESC_T *)MemAlloc (sizeof (H245_TOTCAPDESC_T)))) { return CAPS_E_SYSTEM_ERROR; } //BUGBUG for beta 2 Copy en masse. memcpy (pRemAdvSets->pTermCapDescriptorArray[x],pTermCapDescriptors->pTermCapDescriptorArray[x],sizeof (H245_TOTCAPDESC_T)); /* post beta 2? //Copy the capability ID pRemAdvSets->pTermCapDescriptorArray[x].CapID=pTermCapDescriptors[x].CapID //Walk the simcaps, then altcaps and copy entries */ } for (wNDesc=0;wNDesc <pTermCapList->wLength;wNDesc++) { pData=NULL; pMediaCap = FindHostForMediaType(pTermCapList->pTermCapArray[wNDesc]); if(!pMediaCap) { // special case: there is no T120 resolver. THIS IS A TEMPORARY // SITUATION. We cannot track bitrate limits on multiple T120 capability // instances because of this. As of now, we (NetMeeting) do not advertise // more than one T.120 capability. //This code will keep the last T.120 capability encountered. if(((pTermCapList->pTermCapArray[wNDesc])->DataType == H245_DATA_DATA) && ((pTermCapList->pTermCapArray[wNDesc])->Cap.H245Dat_T120.application.choice == DACy_applctn_t120_chosen) && ((pTermCapList->pTermCapArray[wNDesc])->Cap.H245Dat_T120.application.u.DACy_applctn_t120.choice == separateLANStack_chosen)) { // it's data data m_remoteT120cap = (pTermCapList->pTermCapArray[wNDesc])->CapId; m_remoteT120bitrate = (pTermCapList->pTermCapArray[wNDesc])->Cap.H245Dat_T120.maxBitRate; } // continue; // handled it in-line } else if(pMediaCap->IsCapabilityRecognized(pTermCapList->pTermCapArray[wNDesc])) { hr = pMediaCap->AddRemoteDecodeFormat(pTermCapList->pTermCapArray[wNDesc]); #ifdef DEBUG if(!HR_SUCCEEDED(hr)) { ERRORMESSAGE(("%s:AddRemoteDecodeFormat returned 0x%08lx\r\n",_fx_, hr)); } #endif // DEBUG } } return (hrSuccess); } HRESULT CapsCtl::CreateCapList(PCC_TERMCAPLIST *ppCapBuf, PCC_TERMCAPDESCRIPTORS *ppCombinations) { PCC_TERMCAPLIST pTermCapList = NULL, pTermListAud=NULL, pTermListVid=NULL; PCC_TERMCAPDESCRIPTORS pCombinations = NULL; UINT uCount = 0, uSize = 0, uT120Size = 0; HRESULT hr; WORD wc; UINT x=0,y=0,z=0,uNumAud=0,uNumVid=0; H245_TOTCAPDESC_T *pTotCaps, **ppThisDescriptor; PPCC_TERMCAP ppCCThisTermCap; PCC_TERMCAP pCCT120Cap = NULL; uCount = GetNumCaps(TRUE); ASSERT((NULL == m_pAudTermCaps) && (NULL == m_pVidTermCaps)); // calc size of CC_TERMCAPLIST header + CC_TERMCAPDESCRIPTORS + array of PCC_TERMCAP // allocate mem for the master CC_TERMCAPLIST, including the array of pointers to all CC_TERMCAPs uSize = sizeof(CC_TERMCAPLIST) + sizeof (CC_TERMCAPDESCRIPTORS) + (uCount * sizeof(PCC_TERMCAP)); if((m_localT120cap != INVALID_MEDIA_FORMAT) && bT120Publicize) { uSize += sizeof(CC_TERMCAP); } pTermCapList = (PCC_TERMCAPLIST)MemAlloc(uSize); if(pTermCapList == NULL) { hr = CAPS_E_NOMEM; goto ERROR_EXIT; } // divide up the buffer, CC_TERMCAPLIST first, followed by array of PCC_TERMCAP. // The array of PCC_TERMCAP follows fixed size CC_TERMCAPLIST structure and the fixed size // CC_TERMCAP structure that holds the one T.120 cap. if((m_localT120cap != INVALID_MEDIA_FORMAT) && bT120Publicize) { pCCT120Cap = (PCC_TERMCAP)(((BYTE *)pTermCapList) + sizeof(CC_TERMCAPLIST)); ppCCThisTermCap = (PPCC_TERMCAP) (((BYTE *)pTermCapList) + sizeof(CC_TERMCAPLIST) + sizeof(CC_TERMCAP)); } else ppCCThisTermCap = (PPCC_TERMCAP) (((BYTE *)pTermCapList) + sizeof(CC_TERMCAPLIST)); // allocate mem for the simultaneous caps // get size of cached advertised sets if it exists and more than one media // type is enabled for publication if(bAudioPublicize && bVideoPublicize && pAdvertisedSets) { // use size of cached buffer uSize = uAdvertizedSize; } else if (pAdvertisedSets) { // This case needs to be fixed. If media types are disabled, the simultaneous capability // descriptors in pAdvertisedSets should be rebuilt at that time. There should be no need to test // if(bAudioPublicize && bVideoPublicize && pAdvertisedSets) // calculate size of capability descriptors and simultaneous capability structures. #pragma message ("Figure out the size this needs to be...") #define NUMBER_TERMCAP_DESCRIPTORS 1 uSize = sizeof(H245_TOTCAPDESC_T) * NUMBER_TERMCAP_DESCRIPTORS+ sizeof (CC_TERMCAPDESCRIPTORS)+NUMBER_TERMCAP_DESCRIPTORS* sizeof (H245_TOTCAPDESC_T *); } else { uSize = 0; } if (uSize) { pCombinations = (PCC_TERMCAPDESCRIPTORS)MemAlloc(uSize); // skip the CC_TERMCAPDESCRIPTORS, which has a variable length array of (H245_TOTCAPDESC_T *) following it // the total size of that glob is uSimCapsSize // The actual array of [H245_TOTCAPDESC_T *] follows the CC_TERMCAPDESCRIPTORS structure // anchor the pCombinations->pTermCapDescriptorArray to this point. if(pCombinations == NULL) { hr = CAPS_E_NOMEM; goto ERROR_EXIT; } ppThisDescriptor = pCombinations->pTermCapDescriptorArray = (H245_TOTCAPDESC_T **)((BYTE *)pCombinations + sizeof(CC_TERMCAPDESCRIPTORS)); // the first H245_TOTCAPDESC_T follows the array of [H245_TOTCAPDESC_T *] pTotCaps = (H245_TOTCAPDESC_T *)((BYTE *)ppThisDescriptor + pCombinations->wLength*sizeof(H245_TOTCAPDESC_T **)); if(pAudCaps && bAudioPublicize) { hr=pAudCaps->CreateCapList((LPVOID *)&pTermListAud); if(!HR_SUCCEEDED(hr)) goto ERROR_EXIT; ASSERT(pTermListAud != NULL); } if(pVidCaps && bVideoPublicize) { hr=pVidCaps->CreateCapList((LPVOID *)&pTermListVid); if(!HR_SUCCEEDED(hr)) goto ERROR_EXIT; ASSERT(pTermListVid != NULL); } } else { pCombinations = NULL; } // fix pointers in the master caps list // Now need to fixup the CC_TERMCAPLIST to refer to the individual capabilities // Anchor the CC_TERMCAPLIST member pTermCapArray at the array of PCC_TERMCAP, and // start partying on the array. pTermCapList->wLength =0; pTermCapList->pTermCapArray = ppCCThisTermCap; if(pCCT120Cap) { *ppCCThisTermCap++ = pCCT120Cap; // set T120 capability parameters pCCT120Cap->DataType = H245_DATA_DATA; pCCT120Cap->ClientType = H245_CLIENT_DAT_T120; pCCT120Cap->Dir = H245_CAPDIR_LCLRXTX; pCCT120Cap->Cap.H245Dat_T120.application.choice = DACy_applctn_t120_chosen; pCCT120Cap->Cap.H245Dat_T120.application.u.DACy_applctn_t120.choice= separateLANStack_chosen; pCCT120Cap->Cap.H245Dat_T120.maxBitRate = dwConSpeed; pCCT120Cap->CapId = (H245_CAPID_T)m_localT120cap; pTermCapList->wLength++; } if(pAudCaps && pTermListAud) { for(wc = 0; wc < pTermListAud->wLength; wc++) { // copy the array of "pointers to CC_TERMCAP" *ppCCThisTermCap++ = pTermListAud->pTermCapArray[wc]; pTermCapList->wLength++; } } if(pVidCaps && pTermListVid) { for(wc = 0; wc < pTermListVid->wLength; wc++) { // copy the array of "pointers to CC_TERMCAP" *ppCCThisTermCap++ = pTermListVid->pTermCapArray[wc]; pTermCapList->wLength++; } } // fixup the simultaneous capability descriptors // Create a default set if necessary // if(bAudioPublicize && bVideoPublicize && pAdvertisedSets) { pCombinations->wLength = pAdvertisedSets->wLength; // point pCombinations->pTermCapDescriptorArray past the header (CC_TERMCAPDESCRIPTORS) pCombinations->pTermCapDescriptorArray = (H245_TOTCAPDESC_T **)((BYTE *)pCombinations + sizeof(CC_TERMCAPDESCRIPTORS)); // the first H245_TOTCAPDESC_T follows the array of [H245_TOTCAPDESC_T *] pTotCaps = (H245_TOTCAPDESC_T *)((BYTE *)pCombinations->pTermCapDescriptorArray + pAdvertisedSets->wLength*sizeof(H245_TOTCAPDESC_T **)); for(x = 0; x < pAdvertisedSets->wLength; x++) { // write into the array of descriptor pointers. pointer[x] = this one pCombinations->pTermCapDescriptorArray[x] = pTotCaps; pTotCaps->CapDescId= pAdvertisedSets->pTermCapDescriptorArray[x]->CapDescId; pTotCaps->CapDesc.Length=pAdvertisedSets->pTermCapDescriptorArray[x]->CapDesc.Length; for(y = 0; y < pTotCaps->CapDesc.Length;y++) { //Copy the length field. pTotCaps->CapDesc.SimCapArray[y].Length= pAdvertisedSets->pTermCapDescriptorArray[x]->CapDesc.SimCapArray[y].Length; for(z=0; z < pAdvertisedSets->pTermCapDescriptorArray[x]->CapDesc.SimCapArray[y].Length; z++) { pTotCaps->CapDesc.SimCapArray[y].AltCaps[z] = pAdvertisedSets->pTermCapDescriptorArray[x]->CapDesc.SimCapArray[y].AltCaps[z]; } } pTotCaps++; } } else if (pAdvertisedSets) { // descriptors in pAdvertisedSets should be rebuilt at that time. There should be no need to test // if(bAudioPublicize && bVideoPublicize && pAdvertisedSets) // HACK - put all audio or video caps in one AltCaps[], the T.120 cap in another AltCaps[] // and put both of those in one single capability descriptor (H245_TOTCAPDESC_T) // This hack will not extend past the assumption of one audio channel, one video channel, and // one T.120 channel. If arbitrary media is supported, or multiple audio channels are supported, // this code will be wrong pCombinations->wLength=1; // point pCombinations->pTermCapDescriptorArray past the header (CC_TERMCAPDESCRIPTORS) pCombinations->pTermCapDescriptorArray = (H245_TOTCAPDESC_T **)((BYTE *)pCombinations + sizeof(CC_TERMCAPDESCRIPTORS)); // the first H245_TOTCAPDESC_T follows the array of [H245_TOTCAPDESC_T *] pTotCaps = (H245_TOTCAPDESC_T *)((BYTE *)pCombinations->pTermCapDescriptorArray + pAdvertisedSets->wLength*sizeof(H245_TOTCAPDESC_T **)); pTotCaps->CapDescId=(H245_CAPDESCID_T)x; pTotCaps->CapDesc.Length=0; if(pTermListAud) { uNumAud = min(pTermListAud->wLength, H245_MAX_ALTCAPS); pTotCaps->CapDesc.SimCapArray[x].Length=(unsigned short)uNumAud; for(y = 0; y<uNumAud;y++) { pTotCaps->CapDesc.SimCapArray[x].AltCaps[y] = pTermListAud->pTermCapArray[y]->CapId; } x++; pTotCaps->CapDesc.Length++; } if(pTermListVid && pTermListVid->wLength) { uNumVid = min(pTermListVid->wLength, H245_MAX_ALTCAPS); pTotCaps->CapDesc.SimCapArray[x].Length=(unsigned short)uNumVid; for(y = 0; y<uNumVid;y++) { pTotCaps->CapDesc.SimCapArray[x].AltCaps[y] = pTermListVid->pTermCapArray[y]->CapId; } x++; pTotCaps->CapDesc.Length++; } // the T.120 cap if((m_localT120cap != INVALID_MEDIA_FORMAT) && bT120Publicize) { pTotCaps->CapDesc.SimCapArray[x].Length=1; pTotCaps->CapDesc.SimCapArray[x].AltCaps[0] = (H245_CAPID_T)m_localT120cap; pTotCaps->CapDesc.Length++; } // write into the array of descriptor pointers. pointer[x] = this one *ppThisDescriptor = pTotCaps; } m_pVidTermCaps = pTermListVid; m_pAudTermCaps = pTermListAud; *ppCapBuf = pTermCapList; *ppCombinations = pCombinations; return hrSuccess; ERROR_EXIT: m_pAudTermCaps = NULL; m_pVidTermCaps = NULL; if(pTermCapList) MemFree(pTermCapList); if(pCombinations) MemFree(pCombinations); if(pAudCaps && pTermListAud) { hr=pAudCaps->DeleteCapList(pTermListAud); } if(pVidCaps && pTermListVid) { hr=pVidCaps->DeleteCapList(pTermListVid); } return hr; } HRESULT CapsCtl::DeleteCapList(PCC_TERMCAPLIST pCapBuf, PCC_TERMCAPDESCRIPTORS pCombinations) { MemFree(pCapBuf); MemFree(pCombinations); if(m_pAudTermCaps && pAudCaps) { pAudCaps->DeleteCapList(m_pAudTermCaps); } if(m_pVidTermCaps) { pVidCaps->DeleteCapList(m_pVidTermCaps); } m_pAudTermCaps = NULL; m_pVidTermCaps = NULL; return hrSuccess; } HRESULT CapsCtl::GetEncodeParams(LPVOID pBufOut, UINT uBufSize,LPVOID pLocalParams, UINT uLocalSize,DWORD idRemote, DWORD idLocal) { LPIH323MediaCap pMediaCap = FindHostForID(idLocal); if(!pMediaCap) return CAPS_E_INVALID_PARAM; // HACK // Adjust audio packetization depending on call scenario // unless there is an overriding registry setting if (pMediaCap == pAudCaps) { VIDEO_FORMAT_ID vidLocal=INVALID_MEDIA_FORMAT, vidRemote=INVALID_MEDIA_FORMAT; VIDEO_CHANNEL_PARAMETERS vidParams; CC_TERMCAP vidCaps; UINT audioPacketLength; // modify the audio packetization parameters based on local bandwidth // and presence of video audioPacketLength = AUDIO_PACKET_DURATION_LONG; // the registry setting overrides, if it is present if (g_fRegAudioPacketDuration) audioPacketLength = g_AudioPacketDurationMs; else if (!m_fNM20) // dont try smaller packets for NM20 because it cant handle them { if (pVidCaps && pVidCaps->ResolveEncodeFormat(&vidLocal,&vidRemote) == S_OK && (pVidCaps->GetEncodeParams(&vidCaps,sizeof(vidCaps), &vidParams, sizeof(vidParams), vidRemote, vidLocal) == S_OK)) { // we may potentially send video if (vidParams.ns_params.maxBitRate*100 > BW_ISDN_BITS) audioPacketLength = AUDIO_PACKET_DURATION_SHORT; } else { // no video // since we dont know the actual connection bandwidth we use // the local user setting. // Note: if the remote is on a slow-speed net and the local is on a LAN // we may end up with an inappropriate setting. if (dwConSpeed > BW_288KBS_BITS) audioPacketLength = AUDIO_PACKET_DURATION_SHORT; else if (dwConSpeed > BW_144KBS_BITS) audioPacketLength = AUDIO_PACKET_DURATION_MEDIUM; } } // Setting the AudioPacketDurationMs affects the subsequent GetEncodeParams call pMediaCap->SetAudioPacketDuration(audioPacketLength); } return pMediaCap->GetEncodeParams (pBufOut,uBufSize, pLocalParams, uLocalSize,idRemote,idLocal); } HRESULT CapsCtl::GetPublicDecodeParams(LPVOID pBufOut, UINT uBufSize, VIDEO_FORMAT_ID id) { LPIH323MediaCap pMediaCap = FindHostForID(id); if(!pMediaCap) return CAPS_E_INVALID_PARAM; return pMediaCap->GetPublicDecodeParams (pBufOut,uBufSize,id); } HRESULT CapsCtl::GetDecodeParams(PCC_RX_CHANNEL_REQUEST_CALLBACK_PARAMS pChannelParams,DWORD * pFormatID, LPVOID lpvBuf, UINT uBufSize) { LPIH323MediaCap pMediaCap = FindHostForMediaType(pChannelParams->pChannelCapability); if(!pMediaCap) return CAPS_E_INVALID_PARAM; return pMediaCap->GetDecodeParams (pChannelParams,pFormatID,lpvBuf,uBufSize); } HRESULT CapsCtl::ResolveToLocalFormat(MEDIA_FORMAT_ID FormatIDLocal, MEDIA_FORMAT_ID * pFormatIDRemote) { LPIH323MediaCap pMediaCap = FindHostForID(FormatIDLocal); if(!pMediaCap) return CAPS_E_INVALID_PARAM; return pMediaCap->ResolveToLocalFormat (FormatIDLocal,pFormatIDRemote); } UINT CapsCtl::GetSimCapBufSize (BOOL bRxCaps) { UINT uSize; // get size of cached advertised sets if it exists and more than one media // type is enabled for publication if(bAudioPublicize && bVideoPublicize && pAdvertisedSets) { // use size of cached buffer uSize = uAdvertizedSize; } else { // calculate size of capability descriptors and simultaneous capability structures. #pragma message ("Figure out the size this needs to be...") #define NUMBER_TERMCAP_DESCRIPTORS 1 uSize = sizeof(H245_TOTCAPDESC_T) * NUMBER_TERMCAP_DESCRIPTORS+ sizeof (CC_TERMCAPDESCRIPTORS)+NUMBER_TERMCAP_DESCRIPTORS* sizeof (H245_TOTCAPDESC_T *); } return uSize; } UINT CapsCtl::GetNumCaps(BOOL bRXCaps) { UINT u=0; if(pAudCaps && bAudioPublicize) { u = pAudCaps->GetNumCaps(bRXCaps); } if(pVidCaps && bVideoPublicize) { u += pVidCaps->GetNumCaps(bRXCaps); } if(bT120Publicize) u++; return u; } UINT CapsCtl::GetLocalSendParamSize(MEDIA_FORMAT_ID dwID) { LPIH323MediaCap pMediaCap = FindHostForID(dwID); if(!pMediaCap) return 0; return (pMediaCap->GetLocalSendParamSize(dwID)); } UINT CapsCtl::GetLocalRecvParamSize(PCC_TERMCAP pCapability) { LPIH323MediaCap pMediaCap = FindHostForMediaType(pCapability); if(!pMediaCap) return 0; return (pMediaCap->GetLocalRecvParamSize(pCapability)); } STDMETHODIMP CapsCtl::GetEncodeFormatDetails(MEDIA_FORMAT_ID FormatID, VOID **ppFormat, UINT *puSize) { LPIH323MediaCap pMediaCap = FindHostForID(FormatID); if(!pMediaCap) { *ppFormat = NULL; *puSize = 0; return E_INVALIDARG; } return pMediaCap->GetEncodeFormatDetails (FormatID, ppFormat, puSize); } STDMETHODIMP CapsCtl::GetDecodeFormatDetails(MEDIA_FORMAT_ID FormatID, VOID **ppFormat, UINT *puSize) { LPIH323MediaCap pMediaCap = FindHostForID(FormatID); if(!pMediaCap) { *ppFormat = NULL; *puSize = 0; return E_INVALIDARG; } return pMediaCap->GetDecodeFormatDetails (FormatID, ppFormat, puSize); } // // EnableMediaType controls whether or not capabilities for that media type // are publicized. In a general implementation (next version?) w/ arbitrary // number of media types, each of the media capability objects would keep // track of their own state. This version of Capsctl tracks h323 audio and // video only // HRESULT CapsCtl::EnableMediaType(BOOL bEnable, LPGUID pGuid) { if(!pGuid) return CAPS_E_INVALID_PARAM; if(*pGuid == MEDIA_TYPE_H323AUDIO) { bAudioPublicize = bEnable; } else if (*pGuid == MEDIA_TYPE_H323VIDEO) { bVideoPublicize = bEnable; } else { return CAPS_E_INVALID_PARAM; } return hrSuccess; } // // Build the PCC_TERMCAPDESCRIPTORS list that we will advertise. // // puAudioFormatList/puVideoFormatList MUST BE sorted by preference! // // HRESULT CapsCtl::AddCombinedEntry (MEDIA_FORMAT_ID *puAudioFormatList,UINT uAudNumEntries,MEDIA_FORMAT_ID *puVideoFormatList, UINT uVidNumEntries,DWORD *pIDOut) { static USHORT dwLastIDUsed; DWORD x,y; BOOL bAllEnabled=TRUE,bRecv,bSend; unsigned short Length =0; *pIDOut= (ULONG )CCO_E_SYSTEM_ERROR; //Validate the Input if ((!puAudioFormatList && uAudNumEntries > 0 ) || (!puVideoFormatList && uVidNumEntries > 0 ) || (uVidNumEntries == 0 && uAudNumEntries == 0 )) { //What error code should we return here? return CCO_E_SYSTEM_ERROR; } for (x=0;x<uAudNumEntries;x++) { ASSERT(pAudCaps); pAudCaps->IsFormatEnabled (puAudioFormatList[x],&bRecv,&bSend); bAllEnabled &= bRecv; } for (x=0;x<uVidNumEntries;x++) { ASSERT(pVidCaps); pVidCaps->IsFormatEnabled (puAudioFormatList[x],&bRecv,&bSend); bAllEnabled &= bRecv; } if (!bAllEnabled) { return CCO_E_INVALID_PARAM; } if (uAudNumEntries > H245_MAX_ALTCAPS || uVidNumEntries > H245_MAX_ALTCAPS) { DEBUGMSG (1,("WARNING: Exceeding callcontrol limits!! \r\n")); return CCO_E_INVALID_PARAM; } //If this is the first call, allocate space if (!pAdvertisedSets){ pAdvertisedSets=(PCC_TERMCAPDESCRIPTORS)MemAlloc (sizeof (CC_TERMCAPDESCRIPTORS)); if (!pAdvertisedSets){ //Error code? return CCO_E_SYSTEM_ERROR; } uAdvertizedSize = sizeof (CC_TERMCAPDESCRIPTORS); //Allocate space of NUM_SIMCAP_SETS pAdvertisedSets->pTermCapDescriptorArray=(H245_TOTCAPDESC_T **) MemAlloc (sizeof (H245_TOTCAPDESC_T *)*NUM_SIMCAP_SETS); if (!pAdvertisedSets->pTermCapDescriptorArray) { //Error code? return CCO_E_SYSTEM_ERROR; } //Update the indicies uAdvertizedSize += sizeof (H245_TOTCAPDESC_T *)*NUM_SIMCAP_SETS; dwNumInUse=NUM_SIMCAP_SETS; pAdvertisedSets->wLength=0; } //Find an Index to use. for (x=0;x<pAdvertisedSets->wLength;x++){ if (pAdvertisedSets->pTermCapDescriptorArray[x] == NULL){ break; } } //Did we find space, or do we need a new one? if (x >= dwNumInUse) { //Increment the number in use dwNumInUse++; PVOID pTempTermCapDescriptorArray = NULL; pTempTermCapDescriptorArray = MemReAlloc (pAdvertisedSets->pTermCapDescriptorArray,sizeof (H245_TOTCAPDESC_T *)*(dwNumInUse)); if(pTempTermCapDescriptorArray) { pAdvertisedSets->pTermCapDescriptorArray = (H245_TOTCAPDESC_T **)pTempTermCapDescriptorArray; } else { return CCO_E_SYSTEM_ERROR; } uAdvertizedSize += (sizeof (H245_TOTCAPDESC_T *)*(dwNumInUse))+sizeof (CC_TERMCAPDESCRIPTORS); //Index is 0 based, point at the new entry x=dwNumInUse-1; } //x is now the element we are using. Allocate space for a TermCapDescriptorArray pAdvertisedSets->pTermCapDescriptorArray[x]=(H245_TOTCAPDESC_T *)MemAlloc (sizeof (H245_TOTCAPDESC_T)); if (!pAdvertisedSets->pTermCapDescriptorArray[x]){ return CCO_E_SYSTEM_ERROR; } uAdvertizedSize += sizeof (H245_TOTCAPDESC_T); //Need to update the SetID. (start at 1)... pAdvertisedSets->pTermCapDescriptorArray[x]->CapDescId=++dwLastIDUsed; //Set the # of sets if((m_localT120cap != INVALID_MEDIA_FORMAT) && bT120Publicize) Length++; if(uVidNumEntries) Length++; if(uAudNumEntries) Length++; pAdvertisedSets->pTermCapDescriptorArray[x]->CapDesc.Length= Length; //Copy the Audio into SimCapArray[0], Video into SimCapArray[1] (if both) if ((uVidNumEntries > 0 && uAudNumEntries > 0)) { pAdvertisedSets->pTermCapDescriptorArray[x]->CapDesc.SimCapArray[0].Length=(unsigned short)uAudNumEntries; pAdvertisedSets->pTermCapDescriptorArray[x]->CapDesc.SimCapArray[1].Length=(unsigned short)uVidNumEntries; if((m_localT120cap != INVALID_MEDIA_FORMAT) && bT120Publicize) pAdvertisedSets->pTermCapDescriptorArray[x]->CapDesc.SimCapArray[2].Length=1; //Copy the format IDs for (y=0;y<uAudNumEntries;y++) { pAdvertisedSets->pTermCapDescriptorArray[x]->CapDesc.SimCapArray[0].AltCaps[y]=(USHORT)puAudioFormatList[y]; } for (y=0;y<uVidNumEntries;y++) { pAdvertisedSets->pTermCapDescriptorArray[x]->CapDesc.SimCapArray[1].AltCaps[y]=(USHORT)puVideoFormatList[y]; } if((m_localT120cap != INVALID_MEDIA_FORMAT) && bT120Publicize) pAdvertisedSets->pTermCapDescriptorArray[x]->CapDesc.SimCapArray[2].AltCaps[0]= (H245_CAPID_T)m_localT120cap; } else { if (uAudNumEntries > 0) { pAdvertisedSets->pTermCapDescriptorArray[x]->CapDesc.SimCapArray[0].Length=(unsigned short)uAudNumEntries; if((m_localT120cap != INVALID_MEDIA_FORMAT) && bT120Publicize) pAdvertisedSets->pTermCapDescriptorArray[x]->CapDesc.SimCapArray[1].Length=1; //Copy Audio only for (y=0;y<uAudNumEntries;y++) { pAdvertisedSets->pTermCapDescriptorArray[x]->CapDesc.SimCapArray[0].AltCaps[y]=(USHORT)puAudioFormatList[y]; } if((m_localT120cap != INVALID_MEDIA_FORMAT) && bT120Publicize) pAdvertisedSets->pTermCapDescriptorArray[x]->CapDesc.SimCapArray[1].AltCaps[0]= (H245_CAPID_T)m_localT120cap; } else { pAdvertisedSets->pTermCapDescriptorArray[x]->CapDesc.SimCapArray[0].Length=(unsigned short)uVidNumEntries; if((m_localT120cap != INVALID_MEDIA_FORMAT) && bT120Publicize) pAdvertisedSets->pTermCapDescriptorArray[x]->CapDesc.SimCapArray[1].Length=1; //copy video entries for (y=0;y<uVidNumEntries;y++) { pAdvertisedSets->pTermCapDescriptorArray[x]->CapDesc.SimCapArray[0].AltCaps[y]=(USHORT)puVideoFormatList[y]; } if((m_localT120cap != INVALID_MEDIA_FORMAT) && bT120Publicize) pAdvertisedSets->pTermCapDescriptorArray[x]->CapDesc.SimCapArray[1].AltCaps[0]= (H245_CAPID_T)m_localT120cap; } } //Need to update the wLength pAdvertisedSets->wLength++; *pIDOut=dwLastIDUsed; return hrSuccess; } HRESULT CapsCtl::RemoveCombinedEntry (DWORD ID) { DWORD x; if (!pAdvertisedSets) { return CAPS_E_INVALID_PARAM; } for (x=0;x<dwNumInUse;x++) { if (pAdvertisedSets->pTermCapDescriptorArray[x]) { if (pAdvertisedSets->pTermCapDescriptorArray[x]->CapDescId == ID) { //Found the one to remove MemFree ((VOID *)pAdvertisedSets->pTermCapDescriptorArray[x]); uAdvertizedSize -= sizeof (H245_TOTCAPDESC_T *); if (x != (dwNumInUse -1)) { //Not the last one, swap the two pointers pAdvertisedSets->pTermCapDescriptorArray[x]=pAdvertisedSets->pTermCapDescriptorArray[dwNumInUse-1]; pAdvertisedSets->pTermCapDescriptorArray[dwNumInUse-1]=NULL; } //Decrement the number in use, and set the wLengthField dwNumInUse--; pAdvertisedSets->wLength--; return hrSuccess; } } } //Shouldn't get here, unless it was not found. return CAPS_E_NOCAPS; } // Given a sized list of capability IDs (pointer to array of H245_CAPID_T) // and a sized list of alternate capabilities (AltCaps) within a single simultaneous // capability set, (pointer to an array of pointers to H245_SIMCAP_T) // Determine if the entire list of capability IDs can simultaneously coexist // with respect to the given set of AltCaps. BOOL CapsCtl::AreSimCaps( H245_CAPID_T* pIDArray, UINT uIDArraySize, H245_SIMCAP_T **ppAltCapArray,UINT uAltCapArraySize) { UINT i, u; SHORT j; BOOL bSim; H245_SIMCAP_T *pAltCapEntry, *pFirstAltCapEntry; // If there are fewer AltCaps than capabilities, doom is obvious. Don't bother searching. if(uAltCapArraySize < uIDArraySize) return FALSE; // find an altcaps entry containing the first ID in the list for (i=0;i<uAltCapArraySize;i++) { pAltCapEntry = *(ppAltCapArray+i); // scan this altcaps entry for a matching ID for(j=0;j<pAltCapEntry->Length;j++) { if(*pIDArray == pAltCapEntry->AltCaps[j]) { // found a spot for this capability! if(uIDArraySize ==1) return TRUE; // Done! all the capabilities have been found to coexist // Otherwise, look for the next capability in the *remaining* AltCaps // *This* AltCaps contains the capability we were looking for // So, we "used up" this AltCaps and can't select from it anymore // Pack the array of H245_SIMCAP_T pointers in place so that // "used" entries are at the beginning and "unused" at the end // (a la shell sort swap pointers) if(i != 0) // if not already the same, swap { pFirstAltCapEntry = *ppAltCapArray; *ppAltCapArray = pAltCapEntry; *(ppAltCapArray+i) = pFirstAltCapEntry; } // continue the quest using the remaining capabilities // and the remaining AltCaps bSim = AreSimCaps(pIDArray + 1, uIDArraySize - 1, ppAltCapArray + 1, uAltCapArraySize - 1); if(bSim) { return bSim;// success } else // why not? Either a fit does not exist (common), or the altcaps contain // an odd pattern of multiple instances of some capability IDs, and another // search order *might* fit. Do not blindly try all permutations of search // order. { // If it failed simply because the recently grabbed slot in the altcaps // (the one in *(ppAltCapArray+i)) could have been needed by subsequent // capability IDs, give this one up and look for another instance. // If not, we know for sure that the n! approach will not yield // fruit and can be avoided. for(u=1;(bSim == FALSE)&&(u<uAltCapArraySize);u++) { for(j=0;(bSim == FALSE)&&(j<pAltCapEntry->Length);j++) { // another capability needed the altcaps we grabbed ? if(*(pIDArray+u) == pAltCapEntry->AltCaps[j]) { bSim=TRUE; break; // look no more here, bail to try again because a fit *might* exist } } } if(bSim) // going to continue searching - Swap pointers back if they were swapped above { if(i != 0) // if not the same, swap back { *ppAltCapArray = *(ppAltCapArray+i); *(ppAltCapArray+i) = pAltCapEntry; } break; // next i } else // don't waste CPU - a fit does not exist { return bSim; } } } } } return FALSE; } // Given a sized list of capability IDs (pointer to array of H245_CAPID_T) // and a list of simultaneous capabilities, try each simultaneous capability // and determine if the entire list of capability IDs can simultaneously coexist. BOOL CapsCtl::TestSimultaneousCaps(H245_CAPID_T* pIDArray, UINT uIDArraySize, PCC_TERMCAPDESCRIPTORS pTermCaps) { int iSimSet, iAltSet; BOOL bResolved = FALSE; H245_SIMCAP_T * pAltCapArray[H245_MAX_SIMCAPS]; if (!pAdvertisedSets) return(TRUE); // try each independent local SimCaps set (each descriptor) until success for (iSimSet=0; (bResolved == FALSE) && (iSimSet < pTermCaps->wLength);iSimSet++) { // EXTRA STEP: // Build a sortable representation of the AltCaps set. This step will not be necessary if // and when we change the native representation of a capability descriptor to a variable // length list of pointers to AltCaps. In the meantime, we know that there are no more // than H245_MAX_SIMCAPS AltCaps in this SimCaps. This is imposed by the 2 dimensional // arrays of hardcoded size forced upon us by CALLCONT.DLL. for (iAltSet=0;iAltSet < pTermCaps->pTermCapDescriptorArray[iSimSet]->CapDesc.Length;iAltSet++) { pAltCapArray[iAltSet] = &pTermCaps->pTermCapDescriptorArray[iSimSet]->CapDesc.SimCapArray[iAltSet]; } // do the work bResolved = AreSimCaps(pIDArray, uIDArraySize, (H245_SIMCAP_T **)pAltCapArray, MAKELONG(pTermCaps->pTermCapDescriptorArray[iSimSet]->CapDesc.Length, 0)); } return bResolved; } // Function: CapsCtl::ResolvePermutations(PRES_CONTEXT pResContext, UINT uNumFixedColumns) // // This functions as both a combination generator and a validation mechanism for the // combinations it generates. // // Given a pointer to a resolution context and the number of fixed (i.e. not permutable, // if "permutable" is even a real word) columns, generate one combination at a time. // Try each combination until a working combination is found or until all combinations // have been tried. // // The resolution context structure contains a variable number of columns of variable // length media format ID lists. Each column tracks its current index. When this // function returns TRUE, the winning combination is indicated by the current column // indices. // // The caller can control which combinations are tried first by arranging the columns // in descending importance. // // Incremental searches can be performed without redundant comparisons by adding 1 format // at a time to a column, arranging the column order so that the appended column is // first, and "fixing" that one column at the newly added format. For example, // some calling function could force evaluations on a round-robin column basis by // calling this function inside a loop which does the following: // 1 - adds one format at a time to the rightmost column and sets the current index // of that column to the new entry // 2 - rotates the column order so that the rightmost column is now the leftmost // 3 - fixing the new leftmost column before calling this function again // The result will be that only the permutations which contain the newly added format // will be generated. BOOL CapsCtl::ResolvePermutations(PRES_CONTEXT pResContext, UINT uNumFixedColumns) { RES_PAIR *pResolvedPair; BOOL bResolved = FALSE; UINT i, uColumns; UINT uPairIndex; // converge on one combination in the permutation if(uNumFixedColumns != pResContext->uColumns) { RES_PAIR_LIST *pThisColumn; // take the first non-fixed column, make that column fixed and // iterate on it (loop through indices), and try each sub-permutation // of remaining columns. (until success or all permutations tried) pThisColumn = *(pResContext->ppPairLists+uNumFixedColumns); for (i=0; (bResolved == FALSE) && (i<pThisColumn->uSize); i++) { pThisColumn->uCurrentIndex = i; bResolved = ResolvePermutations(pResContext, uNumFixedColumns+1); } return bResolved; } else { // Bottomed out on the final column. Test the viability of this combination // Build array of local IDs that contians the combination and test the // combination against local simultaneous capabilities, then against // remote simultaneous capabilities // NOTE: be sure to skip empty columns (which represent unresolvable // or unsupported/nonexistent media types or unsupported additional // instances of media types) for(i=0, uColumns=0;i<pResContext->uColumns;i++) { if(((*pResContext->ppPairLists)+i)->uSize) { // get index (row #) for this column uPairIndex = ((*pResContext->ppPairLists)+i)->uCurrentIndex; // get the row pResolvedPair = ((*pResContext->ppPairLists)+i)->pResolvedPairs+uPairIndex; // add the ID to the array *(pResContext->pIDScratch+uColumns) = (H245_CAPID_T)pResolvedPair->idPublicLocal; uColumns++; } // else empty column } // Determine if this combination can exist simultaneously if(TestSimultaneousCaps(pResContext->pIDScratch, uColumns, pResContext->pTermCapsLocal)) { // now test remote // build array of remote IDs and test those against remote // simultaneous capabilities for(i=0, uColumns=0;i<pResContext->uColumns;i++) { if(((*pResContext->ppPairLists)+i)->uSize) { // get index (row #) for this column uPairIndex = ((*pResContext->ppPairLists)+i)->uCurrentIndex; // get the row pResolvedPair = ((*pResContext->ppPairLists)+i)->pResolvedPairs+uPairIndex; // add the ID to the array *(pResContext->pIDScratch+uColumns) =(H245_CAPID_T) pResolvedPair->idRemote; uColumns++; } // else empty column } bResolved = TestSimultaneousCaps(pResContext->pIDScratch, uColumns, pResContext->pTermCapsRemote); } return bResolved; // if(bResolved == TRUE) // The resolved combination of pairs is indicated by the current indices // of **ppPairList; } } // // Given a counted list of desired instances of media, produce an output array of // resolved media format IDs which correspond to the input media type IDs. // This function returns success if at least one media instance is resolved. // When an instance of media is unresolveable, the output corresponding to that // instance contains the value INVALID_MEDIA_FORMAT for local and remote media // format IDs. // // The input is treated as being in preferential order: permutations of the latter // media type instance are varied first. If all permutations do not yield success, // then one media type instance at a time is removed from the end. // HRESULT CapsCtl::ResolveFormats (LPGUID pMediaGuidArray, UINT uNumMedia, PRES_PAIR pResOutput) { HRESULT hr = hrSuccess; PRES_PAIR_LIST pResColumnArray = NULL; PRES_PAIR_LIST *ppPairLists; RES_PAIR *pResPair; PRES_CONTEXT pResContext; LPIH323MediaCap pMediaResolver; UINT i; UINT uMaxFormats = 0; UINT uFixedColumns =0; UINT uFailedMediaCount = 0; BOOL bResolved = FALSE; RES_PAIR UnresolvedPair = {INVALID_MEDIA_FORMAT, INVALID_MEDIA_FORMAT, INVALID_MEDIA_FORMAT}; // create a context structure for the resolution pResContext = (PRES_CONTEXT)MemAlloc(sizeof(RES_CONTEXT)+ (uNumMedia*sizeof(H245_CAPID_T))); if(!pResContext) { hr = CAPS_E_NOMEM; goto ERROR_OUT; } // initialize resolution context pResContext->uColumns = 0; pResContext->pIDScratch = (H245_CAPID_T*)(pResContext+1); pResContext->pTermCapsLocal = pAdvertisedSets; pResContext->pTermCapsRemote = pRemAdvSets; // allocate array of RES_PAIR_LIST (one per column/media type) and // array of pointers to same pResColumnArray = (PRES_PAIR_LIST)MemAlloc((sizeof(RES_PAIR_LIST) * uNumMedia) + (sizeof(PRES_PAIR_LIST) * uNumMedia)); if(!pResColumnArray) { hr = CAPS_E_NOMEM; goto ERROR_OUT; } pResContext->ppPairLists = ppPairLists = (PRES_PAIR_LIST*)(pResColumnArray+uNumMedia); // build columns of media capabilities for(i=0;i<uNumMedia;i++) { // build array of pointers to RES_PAIR_LIST *(ppPairLists+i) = pResColumnArray+i; // initialize RES_PAIR_LIST members (pResColumnArray+i)->pResolvedPairs = NULL; (pResColumnArray+i)->uSize =0; (pResColumnArray+i)->uCurrentIndex = 0; // Get resolver for this media. Special case: there is no T120 resolver. // T120 caps are handled right here in this object if(MEDIA_TYPE_H323_T120 == *(pMediaGuidArray+i)) { pMediaResolver = NULL; if((m_localT120cap != INVALID_MEDIA_FORMAT) &&(m_remoteT120cap != INVALID_MEDIA_FORMAT) ) { (pResColumnArray+i)->uSize =1; uMaxFormats = 1; // only one T.120 cap pResPair = (pResColumnArray+i)->pResolvedPairs = (RES_PAIR *)MemAlloc(uMaxFormats * sizeof(RES_PAIR)); if(!pResPair) { hr = CAPS_E_NOMEM; goto ERROR_OUT; } // pResPair->idLocal = m_localT120cap; pResPair->idRemote = m_remoteT120cap; pResPair->idPublicLocal = pResPair->idLocal; } } else { pMediaResolver = FindHostForMediaGuid(pMediaGuidArray+i); } pResContext->uColumns++; (pResColumnArray+i)->pMediaResolver = pMediaResolver; if(pMediaResolver) { uMaxFormats = pMediaResolver->GetNumCaps(FALSE); // get transmit format count if(uMaxFormats) { pResPair = (pResColumnArray+i)->pResolvedPairs = (RES_PAIR *)MemAlloc(uMaxFormats * sizeof(RES_PAIR)); if(!pResPair) { hr = CAPS_E_NOMEM; goto ERROR_OUT; } // resolve the best choice for each media type (gotta start somewhere) pResPair->idLocal = INVALID_MEDIA_FORMAT; pResPair->idRemote = INVALID_MEDIA_FORMAT; hr=pMediaResolver->ResolveEncodeFormat (&pResPair->idLocal,&pResPair->idRemote); if(!HR_SUCCEEDED(hr)) { if((hr == CAPS_W_NO_MORE_FORMATS) || (hr == CAPS_E_NOMATCH) || (hr == CAPS_E_NOCAPS)) { // No resolved format for this media type. Remove this "column" (pResColumnArray+i)->pResolvedPairs = NULL; MemFree(pResPair); (pResColumnArray+i)->uSize =0; hr = hrSuccess; } else { goto ERROR_OUT; } } else { // this column has one resolved format pResPair->idPublicLocal = pMediaResolver->GetPublicID(pResPair->idLocal); (pResColumnArray+i)->uSize =1; } } // else // No formats exist for this media type. this "column" has zero size } } // Special case test simultaneous caps for the most preferred combination: uFixedColumns = pResContext->uColumns; // << make all columns fixed bResolved = ResolvePermutations(pResContext, uFixedColumns); // if the single most preferred combination can't be used, need to handle // the general case and try permutations until a workable combination is found while(!bResolved) { // make one column at a time permutable, starting with the least-critical media // type. (e.g. it would be typical for the last column to be video because // audio+data are more important. Then we try less and less // preferable video formats before doing anything that would degrade the audio) if(uFixedColumns > 0) // if not already at the end of the rope... { uFixedColumns--; // make another column permutable } else { // wow - tried all permutations and still no luck ...... // nuke the least important remaining media type (e.g. try it w/o video) if(pResContext->uColumns <= 1) // already down to one media type? { hr = CAPS_E_NOMATCH; goto ERROR_OUT; } // Remove the end column (representing the least important media type) // and try it with the remaining columns uFixedColumns = --pResContext->uColumns; // one less column // set the formats of the nuked column to the unresolved state (pResColumnArray+uFixedColumns)->uSize =0; (pResColumnArray+uFixedColumns)->uCurrentIndex =0; pResPair = (pResColumnArray+uFixedColumns)->pResolvedPairs; if (NULL != pResPair) { pResPair->idLocal = INVALID_MEDIA_FORMAT; pResPair->idRemote = INVALID_MEDIA_FORMAT; pResPair->idPublicLocal = INVALID_MEDIA_FORMAT; } uFailedMediaCount++; // track the nuking of a column to avoid // redundantly grabbing all the formats again // ... would not be here if all permutations // had not been tried! // reset the combination indices for(i=0;i<uFixedColumns;i++) { (pResColumnArray+i)->uCurrentIndex = 0; } } // get the rest of the formats for the last known fixed column, make that column // permutable, etc. pMediaResolver = (pResColumnArray+uFixedColumns)->pMediaResolver; if(!pMediaResolver || ((pResColumnArray+uFixedColumns)->uSize ==0)) { continue; // this media type has no further possibility } if(uFailedMediaCount ==0) // If all of the possible resolved pairs // have not yet been obtained, get them! { // get resolved pair IDs for every mutual format of this media type // first: get pointer to array of pair IDs, then use ResolveEncodeFormat() // to fill up the array pResPair = (pResColumnArray+uFixedColumns)->pResolvedPairs; // Get total # of formats less the one that was already obtained uMaxFormats = pMediaResolver->GetNumCaps(FALSE) -1; while(uMaxFormats--) // never exceed the # of remaining local formats... { RES_PAIR *pResPairNext; // recall that ResolveEncodeFormat parameters are I/O - the input // is the local ID of the last resolved mutual format. (remote id // is ignored as input). Fixup the input. pResPairNext = pResPair+1; // start where the previous resolve stopped pResPairNext->idLocal = pResPair->idLocal; // not necessary, ignored ->>> pResPairNext->idRemote = pResPair->idRemote pResPair = pResPairNext; hr=pMediaResolver->ResolveEncodeFormat (&pResPair->idLocal,&pResPair->idRemote); if((hr == CAPS_W_NO_MORE_FORMATS) || (hr == CAPS_E_NOMATCH)) // got all of the formats, but not an error { // this is likely when less than 100% of local formats have a remote match hr = hrSuccess; break; } if(!HR_SUCCEEDED(hr)) goto ERROR_OUT; // get the public ID of the local format (it's *usually* the same, but not always) pResPair->idPublicLocal = pMediaResolver->GetPublicID(pResPair->idLocal); // this column has another format - count it! (pResColumnArray+uFixedColumns)->uSize++; } } // now try the new permutations bResolved = ResolvePermutations(pResContext, uFixedColumns); } if(bResolved) { // spew the output for(i=0;i<uNumMedia;i++) { if((pResColumnArray+i)->uSize) { pResPair = (pResColumnArray+i)->pResolvedPairs + (pResColumnArray+i)->uCurrentIndex; } else { pResPair = &UnresolvedPair; } *(pResOutput+i) = *pResPair; } } else { // if there was some error, preserve that error code, if(HR_SUCCEEDED(hr)) // otherwise the error is.... hr = CAPS_E_NOMATCH; } ERROR_OUT: // well, the success case falls out here too if(pResColumnArray) { for(i=0;i<uNumMedia;i++) { if((pResColumnArray+i)->pResolvedPairs) MemFree((pResColumnArray+i)->pResolvedPairs); } MemFree(pResColumnArray); } if(pResContext) { MemFree(pResContext); } return hr; } HRESULT CapsCtl::ResetCombinedEntries (void) { DWORD x; if (pAdvertisedSets) { for (x = 0; x < pAdvertisedSets->wLength; x++) { if (pAdvertisedSets->pTermCapDescriptorArray[x]) { MemFree (pAdvertisedSets->pTermCapDescriptorArray[x]); } } MemFree (pAdvertisedSets->pTermCapDescriptorArray); pAdvertisedSets->wLength=0; MemFree (pAdvertisedSets); pAdvertisedSets = NULL; } if (pSetIDs) { MemFree(pSetIDs); pSetIDs = NULL; } dwNumInUse=0; uAdvertizedSize=0; return hrSuccess; } 
f6708023de603f80181183658e7df601332881a0
cd655a4cacbde3e99e09279c25e36a253edb6609
/Game/Motor2D/j1Player.cpp
11e0bfdaf2ba8233db3f51cfc013999a309bf23a
[]
no_license
Pletenica/Between-Worlds
a11bcbbf15a8b967e21e13281b4464be7a1c9b57
be98f3fd74e6b87c56b84e9f8c65c88478362624
refs/heads/master
2022-03-26T11:20:20.857814
2019-12-29T22:42:23
2019-12-29T22:42:23
209,526,894
0
0
null
null
null
null
UTF-8
C++
false
false
18,741
cpp
#include "j1App.h" #include "j1Textures.h" #include "j1Input.h" #include "j1Render.h" #include "j1Audio.h" #include "j1Collision.h" #include "j1Player.h" #include "j1Scene.h" #include "j1Map.h" #include "SDL/include/SDL_timer.h" #include "../Game/Brofiler/Brofiler.h" j1Player::j1Player() { /////IDLE ANIMATION////// idle.PushBack({ 0, 0, 32, 32 }); idle.PushBack({ 32, 0, 32, 32 }); idle.PushBack({ 64, 0, 32, 32 }); idle.PushBack({ 32, 0, 32, 32 }); idle.speed = 0.07f; /////JUMP ANIMATION////// //jump.PushBack({ 0, 32, 32, 32 }); //jump.PushBack({ 32, 32, 32, 32 }); jump.PushBack({ 64, 32, 32, 32 }); jump.speed = 0.007f; /////WALK ANIMATION////// walk.PushBack({ 0, 64, 32, 32 }); walk.PushBack({ 32, 64, 32, 32 }); walk.PushBack({ 64, 64, 32, 32 }); walk.PushBack({ 96, 64, 32, 32 }); walk.speed = 0.07f; /////LIANA ANIMATION////// liana.PushBack({ 0, 96, 32, 32 }); liana.PushBack({ 32, 96, 32, 32 }); liana.PushBack({ 64, 96, 32, 32 }); liana.speed = 0.007f; /////DEAD ANIMATION////// dead.PushBack({ 96, 96, 32, 32 }); dead.PushBack({ 96, 32, 32, 32 }); dead.PushBack({ 96, 0, 32, 32 }); dead.PushBack({ 96, 0, 32, 32 }); dead.PushBack({ 96, 0, 32, 32 }); dead.PushBack({ 96, 0, 32, 32 }); dead.PushBack({ 96, 0, 32, 32 }); dead.speed = 0.1f; } j1Player::~j1Player() {} bool j1Player::Awake(pugi::xml_node& config) { /////// SCENE 1 PORTALS /////// Ginit = config.child("gravity").attribute("Ginit").as_int(1); jump_vel = config.child("gravity").attribute("JumpVel").as_int(12); G_max = config.child("gravity").attribute("Gmax").as_int(17); speed_player = config.child("speed").attribute("movement").as_int(2); speed_player_ice = config.child("speed").attribute("iceinercy").as_int(1); speed_player_jump = config.child("speed").attribute("movementinair").as_int(2); return true; } // Load assets bool j1Player::Start() { //// Load All CONDITIONS //// Current_Animation =idle; //// Load All Graphics //// normal_graphics = App->tex->Load("textures/PlayerNormal.png"); fire_graphics = App->tex->Load("textures/PlayerFire.png"); plant_graphics = App->tex->Load("textures/PlayerPlant.png"); ice_graphics = App->tex->Load("textures/PlayerSnow.png"); watter_graphics = App->tex->Load("textures/PlayerWatter.png"); current_graphics = normal_graphics; //// Load All SOUNDS & COLLISIONS //// jumpingsound = App->audio->LoadFx("audio/fx/jump.wav"); deathsound = App->audio->LoadFx("audio/fx/death.wav"); walkingsound = App->audio->LoadFx("audio/fx/walk.wav"); portalsound = App->audio->LoadFx("audio/fx/portal.wav"); body = App->collision->AddCollider({ position.x,position.y,20,32 }, COLLIDER_PLAYER, this); return true; } //Clean Up bool j1Player::CleanUp() { App->audio->StopFx(); //App->audio->UnLoadChunk(jumpingsound); //App->audio->UnLoadChunk(deathsound); //App->audio->UnLoadChunk(walkingsound); //App->audio->UnLoadChunk(portalsound); App->tex->UnLoad(current_graphics); App->tex->UnLoad(fire_graphics); App->tex->UnLoad(plant_graphics); App->tex->UnLoad(ice_graphics); App->tex->UnLoad(watter_graphics); App->tex->UnLoad(normal_graphics); return true; } bool j1Player::PreUpdate() { BROFILER_CATEGORY("PlayerPreUpdate", Profiler::Color::Chartreuse) right = false; left = false; if (deadbool == false && isinair == false && isjumping == false && isdoublejumping==false) { Current_Animation.GetCurrentFrame() = idle.GetCurrentFrame(); } if (stop_left == true) { ice_left = false; } if (stop_right == true) { ice_right = false; } //////// FLIP //////// if (App->input->GetKey(SDL_SCANCODE_A) == KEY_REPEAT) { flip = SDL_FLIP_HORIZONTAL; } if (App->input->GetKey(SDL_SCANCODE_D) == KEY_REPEAT) { flip = SDL_FLIP_NONE; } //////// GOD MODE //////// if (App->input->GetKey(SDL_SCANCODE_F10) == KEY_DOWN) { if (App->player->godmode == true) { //body = App->collision->AddCollider({ position.x,position.y,20,32 }, COLLIDER_PLAYER, this); godmode = false; } else { isinair = false; App->player->godmode = true; } } if (godmode == true) { isinair = false; isjumping = false; isdoublejumping = false; if (App->input->GetKey(SDL_SCANCODE_S) == KEY_REPEAT) { if (position.y < 288) { position.y += speed_player; } } if (App->input->GetKey(SDL_SCANCODE_W) == KEY_REPEAT) { if (position.y > 0) { position.y -= speed_player; } } if (App->input->GetKey(SDL_SCANCODE_A) == KEY_REPEAT && !stop_left) { position.x -= speed_player; flip = SDL_FLIP_HORIZONTAL; } if (App->input->GetKey(SDL_SCANCODE_D) == KEY_REPEAT && !stop_right) { position.x += speed_player; flip = SDL_FLIP_NONE; } } //////// INPUTS WHEN TOUCHES GROUND //////// if (isinair ==false && !isinliana && !godmode && !dimensionagua) { if ((App->input->GetKey(SDL_SCANCODE_A) == KEY_REPEAT)&&!stop_left) { position.x -= speed_player; App->audio->PlayFx(walkingsound, 0); if (dimensionhielo == true) { ice_left = true; ice_right = false; } left = true; Current_Animation.GetCurrentFrame() = walk.GetCurrentFrame(); } if ((App->input->GetKey(SDL_SCANCODE_D) == KEY_REPEAT)&&!stop_right) { position.x += speed_player; App->audio->PlayFx(walkingsound,0); if (dimensionhielo == true) { ice_right = true; ice_left = false; } right = true; Current_Animation.GetCurrentFrame() = walk.GetCurrentFrame(); } if (App->input->GetKey(SDL_SCANCODE_W) == KEY_REPEAT && !stop_jump) { isjumping = true; if (App->audio->PlayFx(walkingsound, 0) == true || App->audio->PlayFx(walkingsound, 0) == false) { App->audio->PlayFx(jumpingsound, 0); } } } //////// WATTER //////// if (dimensionagua == true) { isjumping = false; isdoublejumping = false; Current_Animation.GetCurrentFrame() = idle.GetCurrentFrame(); if (godmode == false) { if (isinair) { if (limit_watter == 0) { position.y++; } limit_watter++; if (limit_watter == 3) { limit_watter = 0; } } if ((App->input->GetKey(SDL_SCANCODE_A) == KEY_REPEAT) && !stop_left) { position.x -= speed_player; left = true; Current_Animation.GetCurrentFrame() = walk.GetCurrentFrame(); } if ((App->input->GetKey(SDL_SCANCODE_D) == KEY_REPEAT) && !stop_right) { position.x += speed_player; right = true; Current_Animation.GetCurrentFrame() = walk.GetCurrentFrame(); } if ((App->input->GetKey(SDL_SCANCODE_W) == KEY_REPEAT) && (position.y > 20) && (!stop_up)) { position.y -= speed_player; } if (App->input->GetKey(SDL_SCANCODE_S) == KEY_REPEAT) { position.y += speed_player; } isinair = false; } } //////// LIANA //////// if (isinliana == true) { isjumping = false; isdoublejumping = false; Current_Animation.GetCurrentFrame() = liana.GetCurrentFrame(); if (isinair == false) { if (limit_liana == 0) { position.y++; } limit_liana++; if (limit_liana == 3) { limit_liana = 0; } } if ((App->input->GetKey(SDL_SCANCODE_A) == KEY_REPEAT) && (stop_left == false)) { position.x -= speed_player; flip = SDL_FLIP_HORIZONTAL; } if ((App->input->GetKey(SDL_SCANCODE_D) == KEY_REPEAT) && (stop_right == false)) { position.x += speed_player; flip = SDL_FLIP_NONE; } if (App->input->GetKey(SDL_SCANCODE_W) == KEY_DOWN) { position.y -= 10; } isinair = false; } //////// JUMPING //////// if (isjumping == true) { Current_Animation.GetCurrentFrame() = jump.GetCurrentFrame(); if (position.y > 0) { if (isdoublejumping == false) { if (App->input->GetKey(SDL_SCANCODE_W) == KEY_DOWN) { isdoublejumping = true; if (App->audio->PlayFx(walkingsound, 0) == true || App->audio->PlayFx(walkingsound, 0) == false) { App->audio->PlayFx(jumpingsound, 0); } G = Ginit; } isinair = false; ice_left = false; ice_right = false; if (G < G_max) { G++; } position.y += G - jump_vel; if ((App->input->GetKey(SDL_SCANCODE_A) == KEY_REPEAT) && !stop_left) { position.x -= speed_player_jump; } if ((App->input->GetKey(SDL_SCANCODE_D) == KEY_REPEAT) && !stop_right) { position.x += speed_player_jump; } } } else { G = 1; isinair = true; isjumping = false; isdoublejumping = false; } } //////// DOUBLE JUMPING //////// if (isdoublejumping == true) { Current_Animation.GetCurrentFrame() = jump.GetCurrentFrame(); if (position.y > 0) { isinair = false; ice_left = false; ice_right = false; if (G < G_max) { G++; } position.y += G - jump_vel; if ((App->input->GetKey(SDL_SCANCODE_A) == KEY_REPEAT) && !stop_left) { position.x -= speed_player_jump; } if ((App->input->GetKey(SDL_SCANCODE_D) == KEY_REPEAT) && !stop_right) { position.x += speed_player_jump; } } else { G = 1; isinair = true; isjumping = false; isdoublejumping = false; } } //////// AIR //////// if (isinair==true && isinliana==false) { isjumping = false; isdoublejumping = false; ice_left = false; ice_right = false; Current_Animation.GetCurrentFrame() = jump.GetCurrentFrame(); if (App->input->GetKey(SDL_SCANCODE_A) == KEY_REPEAT && !stop_left) { position.x -= speed_player; Current_Animation.GetCurrentFrame() = walk.GetCurrentFrame(); flip = SDL_FLIP_HORIZONTAL; } if (App->input->GetKey(SDL_SCANCODE_D) == KEY_REPEAT && !stop_right) { position.x += speed_player; Current_Animation.GetCurrentFrame() = walk.GetCurrentFrame(); flip = SDL_FLIP_NONE; } if (G < G_max) { G++; } position.y += G; } //////// INPUT FAILS //////// if ((App->input->GetKey(SDL_SCANCODE_A) == KEY_REPEAT) && (App->input->GetKey(SDL_SCANCODE_D) == KEY_REPEAT)) { Current_Animation.GetCurrentFrame() = idle.GetCurrentFrame(); } //////// DEATH //////// if (deadbool == true) { if (App->audio->PlayFx(deathsound, 0) == false) { App->audio->PlayFx(deathsound, 0); } Current_Animation.GetCurrentFrame() = dead.GetCurrentFrame(); stop_left = true; stop_jump = true; stop_right = true; if (dead.finished == 1) { if (App->scene->changelevel == false) { ChangeToLevel1(); } else { ChangeToLevel2(); } } } //////// CHANGE PLAYER COLLIDER IF FLIP //////// if (flip == SDL_FLIP_HORIZONTAL) { body->rect.x = App->player->position.x +7; body->rect.y = position.y; } if (flip == SDL_FLIP_NONE) { body->rect.x = App->player->position.x +5; body->rect.y = position.y; } return true; } // Update: draw background bool j1Player::Update(float dt) { BROFILER_CATEGORY("PlayerUpdate", Profiler::Color::DarkSeaGreen) if (dimensionfuego == true) { current_graphics = fire_graphics; } if (dimensionplanta == true) { current_graphics = plant_graphics; } if (dimensionagua == true) { current_graphics = watter_graphics; } if (dimensionhielo == true) { current_graphics = ice_graphics; } if (dimensionnormal == true) { current_graphics = normal_graphics; } return true; } bool j1Player::PostUpdate() { BROFILER_CATEGORY("PlayerPostUpdate", Profiler::Color::YellowGreen) isinair = true; isinliana = false; stop_right = false; stop_left = false; stop_jump = false; stop_up = false; if (body->CheckCollision(App->scene->cameralimit01->rect) == true) { stop_left = true; } if (body->CheckCollision(App->scene->cameralimit02->rect) == true) { stop_right = true; } App->render->Blit(current_graphics, position.x, position.y, &(Current_Animation.GetCurrentFrame()), 1.0f, 0, 0, 0, flip); return exitgame; } void j1Player::OnCollision(Collider* player, Collider* other) { BROFILER_CATEGORY("PlayerOnCollision", Profiler::Color::MediumAquaMarine) if (player->type == COLLIDER_PLAYER) { if (other->type == COLLIDER_CORRIENTE_AGUA) { if (godmode == false) { if (limit_wave_watter == 0) { position.x--; } limit_wave_watter++; if (limit_wave_watter == 3) { limit_wave_watter = 0; } } } if (other->type == COLLIDER_PORTAL_AGUA) { App->audio->PlayFx(portalsound, 0); ice_right = false; ice_left = false; dimensionnormal = false; dimensionagua = true; dimensionfuego = false; dimensionhielo = false; dimensionplanta = false; current_graphics = watter_graphics; if (position.x > other->rect.x - 32) { stop_left = true; stop_jump = true; } } if (other->type == COLLIDER_PORTAL_FUEGO) { App->audio->PlayFx(portalsound, 0); ice_right = false; ice_left = false; dimensionnormal = false; dimensionagua = false; dimensionfuego = true; dimensionhielo = false; dimensionplanta = false; dimensionplanta = false; current_graphics = fire_graphics; if (position.x > other->rect.x - 32) { stop_left = true; stop_jump = true; } } if (other->type == COLLIDER_PORTAL_HIELO) { App->audio->PlayFx(portalsound, 0); ice_right = false; ice_left = false; dimensionnormal = false; dimensionagua = false; dimensionfuego = false; dimensionhielo = true; dimensionplanta = false; current_graphics = ice_graphics; if (position.x > other->rect.x - 32) { stop_left = true; stop_jump = true; } } if (other->type == COLLIDER_PORTAL_PLANTA) { App->audio->PlayFx(portalsound, 0); ice_right = false; ice_left = false; dimensionnormal = false; dimensionagua = false; dimensionfuego = false; dimensionhielo = false; dimensionplanta = true; current_graphics = plant_graphics; if (position.x > other->rect.x-32) { stop_left = true; stop_jump = true; } } if (other->type == COLLIDER_PORTAL_NORMAL1) { App->audio->PlayFx(portalsound, 0); ice_right = false; ice_left = false; dimensionnormal = true; dimensionagua = false; dimensionfuego = false; dimensionhielo = false; dimensionplanta = false; current_graphics = normal_graphics; if (position.x > other->rect.x - 32) { stop_left = true; stop_jump = true; } } if (other->type == COLLIDER_PORTAL_NORMAL2) { App->audio->PlayFx(portalsound, 0); ice_right = false; ice_left = false; dimensionnormal = true; dimensionagua = false; dimensionfuego = false; dimensionhielo = false; dimensionplanta = false; current_graphics = normal_graphics; if (position.x > other->rect.x - 32) { stop_left = true; stop_jump = true; } } if (other->type == COLLIDER_PORTAL_CHANGESCENE1) { App->audio->PlayFx(portalsound, 0); App->player->ChangeToLevel2(); } if (other->type == COLLIDER_SUELO) { isinliana = false; isinair = false; isinice = false; isjumping = false; isdoublejumping = false; if (godmode == false) { if (position.y >= other->rect.y) { isinair = true; if (position.x < other->rect.x) { stop_right = true; } if (position.x > other->rect.x) { stop_left = true; } } if (position.y < other->rect.y) { position.y = other->rect.y - 32; } G = Ginit; if ((other->rect.y <= position.y) && (SDL_SCANCODE_W)) { stop_up = true; Current_Animation.GetCurrentFrame() = idle.GetCurrentFrame(); } if (((other->rect.y <= position.y) && position.x < other->rect.x) && (SDL_SCANCODE_D)) { stop_right = true; Current_Animation.GetCurrentFrame() = idle.GetCurrentFrame(); } if (((other->rect.y <= position.y) && position.x > other->rect.x) && (SDL_SCANCODE_A)) { stop_left = true; Current_Animation.GetCurrentFrame() = idle.GetCurrentFrame(); } if (dimensionhielo == true) { if (limit_ice == 0) { if (ice_left == true) { position.x -= speed_player_ice; } if (ice_right == true) { position.x += speed_player_ice; } } limit_ice++; if (limit_ice >= 2) { limit_ice = 0; } } } } if (other->type == COLLIDER_LIANA) { if (godmode == false) { isinliana = true; isinair = false; } } if (other->type == COLLIDER_DEATH) { if (godmode == false) { isinair = false; isjumping = false; isdoublejumping = false; deadbool = true; stop_right = true; stop_left = true; stop_jump = true; position.y = other->rect.y - 10; } } if (other->type == COLLIDER_DEATH_ENEMY) { if (godmode == false) { isinair = false; isjumping = false; isdoublejumping = false; deadbool = true; stop_right = true; stop_left = true; stop_jump = true; position.y = other->rect.y - 10; } } if (other->type == COLLIDER_CAMERA) { stop_left = true; } if (other->type == COLLIDER_PORTAL_CHANGESCENEFINAL) { exitgame = false; } } } void j1Player::ChangeToLevel1() { ice_right = false; ice_left = false; dimensionnormal = true; dimensionagua = false; dimensionfuego = false; dimensionhielo = false; dimensionplanta = false; current_graphics = normal_graphics; deadbool = false; App->scene->cameraxinvert = 0; App->render->camera.x = 0; App->map->CleanUp(); App->map->collidersdone = false; App->map->Load("Scene01.tmx"); App->scene->cameralimit01->rect.x = App->render->camera.x; App->scene->cameralimit02->rect.x = App->render->camera.x + 380; position.x = App->scene->positionplayerinitx; position.y = App->scene->positionplayerinity; App->scene->donecollidersscene2 = true; App->scene->donecollidersscene1 = false; App->scene->changelevel = false; } void j1Player::ChangeToLevel2() { ice_right = false; ice_left = false; dimensionnormal = true; dimensionagua = false; dimensionfuego = false; dimensionhielo = false; dimensionplanta = false; current_graphics = normal_graphics; deadbool = false; App->scene->cameraxinvert = 0; App->render->camera.x = 0; App->scene->changelevel = true; App->map->CleanUp(); App->map->collidersdone = false; App->map->Load("Scene02.tmx"); //App->tex->UnLoad(App->scene->objects_graphics); //App->scene->objects_graphics = App->tex->Load("textures/Objects.png"); App->scene->cameralimit01->rect.x = App->render->camera.x; App->scene->cameralimit02->rect.x = App->render->camera.x + 380; position.x = App->scene->positionplayerinitx; position.y = App->scene->positionplayerinity; App->scene->donecollidersscene2 = false; App->scene->donecollidersscene1 = true; App->scene->changelevel = true; } bool j1Player::Save(pugi::xml_node& data) const { //data.append_child("player"); //data.child("player").append_attribute("x") = position.x; //data.child("player").append_attribute("y") = position.y; return true; } bool j1Player::Load(pugi::xml_node& data) { //position.x = data.child("player").attribute("x").as_int(); //position.y = data.child("player").attribute("y").as_int(); return true; }
60ec04aedc054c9cafdc61b7de52032a11342e6b
59122303ed8071afcb5e4ea02b6ad636c75c3905
/qtaskbarcontrol_p.cpp
663269d45626961f74d114e6470bdf5a02c43950
[ "BSD-3-Clause" ]
permissive
scaramancha/QTaskbarControl
765541c3a04de4200354ba46ecb38819a90045fb
ef9d627cada83532d11a80ff6d71d27fb96d077a
refs/heads/master
2023-03-17T17:27:32.326823
2020-12-26T09:37:11
2020-12-26T09:37:11
null
0
0
null
null
null
null
UTF-8
C++
false
false
705
cpp
#include "qtaskbarcontrol_p.h" #include <QIcon> void QTaskbarControlPrivate::setWindow(QWindow *window) { Q_UNUSED(window) } void QTaskbarControlPrivate::setWindowsProgressState(QTaskbarControl::WinProgressState state) { Q_UNUSED(state) } QTaskbarControl::WinProgressState QTaskbarControlPrivate::windowsProgressState() const { return QTaskbarControl::Stopped; } void QTaskbarControlPrivate::setWindowsBadgeIcon(const QIcon &icon) { Q_UNUSED(icon) } QIcon QTaskbarControlPrivate::windowsBadgeIcon() const { return {}; } void QTaskbarControlPrivate::setWindowsBadgeTextColor(const QColor &color) { Q_UNUSED(color) } QColor QTaskbarControlPrivate::windowsBadgeTextColor() const { return {}; }
c5ff269f38e6cc490dbc3d796a8c1250f91867d1
624e2ee2fc011c095508c6b10f55e79b880b0685
/opt/ros/kinetic/include/mavros_msgs/HilSensor.h
8c019afa90e566eb3459b8ce07945d5288e89acb
[]
no_license
sergioma295/Drone_Ros
ca9bc1d856fab69d0f9e122d0e7d6ce6c0a3e366
349cce9ab745116e4e7825e79de4127536609210
refs/heads/master
2020-06-01T08:32:43.708171
2019-06-07T19:12:47
2019-06-07T19:12:47
190,713,510
1
0
null
2019-06-07T19:39:31
2019-06-07T08:45:19
C++
UTF-8
C++
false
false
9,121
h
// Generated by gencpp from file mavros_msgs/HilSensor.msg // DO NOT EDIT! #ifndef MAVROS_MSGS_MESSAGE_HILSENSOR_H #define MAVROS_MSGS_MESSAGE_HILSENSOR_H #include <string> #include <vector> #include <map> #include <ros/types.h> #include <ros/serialization.h> #include <ros/builtin_message_traits.h> #include <ros/message_operations.h> #include <std_msgs/Header.h> #include <geometry_msgs/Vector3.h> #include <geometry_msgs/Vector3.h> #include <geometry_msgs/Vector3.h> namespace mavros_msgs { template <class ContainerAllocator> struct HilSensor_ { typedef HilSensor_<ContainerAllocator> Type; HilSensor_() : header() , acc() , gyro() , mag() , abs_pressure(0.0) , diff_pressure(0.0) , pressure_alt(0.0) , temperature(0.0) , fields_updated(0) { } HilSensor_(const ContainerAllocator& _alloc) : header(_alloc) , acc(_alloc) , gyro(_alloc) , mag(_alloc) , abs_pressure(0.0) , diff_pressure(0.0) , pressure_alt(0.0) , temperature(0.0) , fields_updated(0) { (void)_alloc; } typedef ::std_msgs::Header_<ContainerAllocator> _header_type; _header_type header; typedef ::geometry_msgs::Vector3_<ContainerAllocator> _acc_type; _acc_type acc; typedef ::geometry_msgs::Vector3_<ContainerAllocator> _gyro_type; _gyro_type gyro; typedef ::geometry_msgs::Vector3_<ContainerAllocator> _mag_type; _mag_type mag; typedef float _abs_pressure_type; _abs_pressure_type abs_pressure; typedef float _diff_pressure_type; _diff_pressure_type diff_pressure; typedef float _pressure_alt_type; _pressure_alt_type pressure_alt; typedef float _temperature_type; _temperature_type temperature; typedef uint32_t _fields_updated_type; _fields_updated_type fields_updated; typedef boost::shared_ptr< ::mavros_msgs::HilSensor_<ContainerAllocator> > Ptr; typedef boost::shared_ptr< ::mavros_msgs::HilSensor_<ContainerAllocator> const> ConstPtr; }; // struct HilSensor_ typedef ::mavros_msgs::HilSensor_<std::allocator<void> > HilSensor; typedef boost::shared_ptr< ::mavros_msgs::HilSensor > HilSensorPtr; typedef boost::shared_ptr< ::mavros_msgs::HilSensor const> HilSensorConstPtr; // constants requiring out of line definition template<typename ContainerAllocator> std::ostream& operator<<(std::ostream& s, const ::mavros_msgs::HilSensor_<ContainerAllocator> & v) { ros::message_operations::Printer< ::mavros_msgs::HilSensor_<ContainerAllocator> >::stream(s, "", v); return s; } } // namespace mavros_msgs namespace ros { namespace message_traits { // BOOLTRAITS {'IsFixedSize': False, 'IsMessage': True, 'HasHeader': True} // {'geographic_msgs': ['/opt/ros/kinetic/share/geographic_msgs/cmake/../msg'], 'std_msgs': ['/opt/ros/kinetic/share/std_msgs/cmake/../msg'], 'sensor_msgs': ['/opt/ros/kinetic/share/sensor_msgs/cmake/../msg'], 'mavros_msgs': ['/tmp/binarydeb/ros-kinetic-mavros-msgs-0.29.2/msg'], 'geometry_msgs': ['/opt/ros/kinetic/share/geometry_msgs/cmake/../msg'], 'uuid_msgs': ['/opt/ros/kinetic/share/uuid_msgs/cmake/../msg']} // !!!!!!!!!!! ['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_parsed_fields', 'constants', 'fields', 'full_name', 'has_header', 'header_present', 'names', 'package', 'parsed_fields', 'short_name', 'text', 'types'] template <class ContainerAllocator> struct IsFixedSize< ::mavros_msgs::HilSensor_<ContainerAllocator> > : FalseType { }; template <class ContainerAllocator> struct IsFixedSize< ::mavros_msgs::HilSensor_<ContainerAllocator> const> : FalseType { }; template <class ContainerAllocator> struct IsMessage< ::mavros_msgs::HilSensor_<ContainerAllocator> > : TrueType { }; template <class ContainerAllocator> struct IsMessage< ::mavros_msgs::HilSensor_<ContainerAllocator> const> : TrueType { }; template <class ContainerAllocator> struct HasHeader< ::mavros_msgs::HilSensor_<ContainerAllocator> > : TrueType { }; template <class ContainerAllocator> struct HasHeader< ::mavros_msgs::HilSensor_<ContainerAllocator> const> : TrueType { }; template<class ContainerAllocator> struct MD5Sum< ::mavros_msgs::HilSensor_<ContainerAllocator> > { static const char* value() { return "2a892891e5c40d6dd1066bf1f394b5dc"; } static const char* value(const ::mavros_msgs::HilSensor_<ContainerAllocator>&) { return value(); } static const uint64_t static_value1 = 0x2a892891e5c40d6dULL; static const uint64_t static_value2 = 0xd1066bf1f394b5dcULL; }; template<class ContainerAllocator> struct DataType< ::mavros_msgs::HilSensor_<ContainerAllocator> > { static const char* value() { return "mavros_msgs/HilSensor"; } static const char* value(const ::mavros_msgs::HilSensor_<ContainerAllocator>&) { return value(); } }; template<class ContainerAllocator> struct Definition< ::mavros_msgs::HilSensor_<ContainerAllocator> > { static const char* value() { return "# HilSensor.msg\n\ #\n\ # ROS representation of MAVLink HIL_SENSOR\n\ # See mavlink message documentation here:\n\ # https://mavlink.io/en/messages/common.html#HIL_SENSOR\n\ \n\ std_msgs/Header header\n\ \n\ geometry_msgs/Vector3 acc\n\ geometry_msgs/Vector3 gyro\n\ geometry_msgs/Vector3 mag\n\ float32 abs_pressure\n\ float32 diff_pressure\n\ float32 pressure_alt\n\ float32 temperature\n\ uint32 fields_updated\n\ \n\ ================================================================================\n\ MSG: std_msgs/Header\n\ # Standard metadata for higher-level stamped data types.\n\ # This is generally used to communicate timestamped data \n\ # in a particular coordinate frame.\n\ # \n\ # sequence ID: consecutively increasing ID \n\ uint32 seq\n\ #Two-integer timestamp that is expressed as:\n\ # * stamp.sec: seconds (stamp_secs) since epoch (in Python the variable is called 'secs')\n\ # * stamp.nsec: nanoseconds since stamp_secs (in Python the variable is called 'nsecs')\n\ # time-handling sugar is provided by the client library\n\ time stamp\n\ #Frame this data is associated with\n\ # 0: no frame\n\ # 1: global frame\n\ string frame_id\n\ \n\ ================================================================================\n\ MSG: geometry_msgs/Vector3\n\ # This represents a vector in free space. \n\ # It is only meant to represent a direction. Therefore, it does not\n\ # make sense to apply a translation to it (e.g., when applying a \n\ # generic rigid transformation to a Vector3, tf2 will only apply the\n\ # rotation). If you want your data to be translatable too, use the\n\ # geometry_msgs/Point message instead.\n\ \n\ float64 x\n\ float64 y\n\ float64 z\n\ "; } static const char* value(const ::mavros_msgs::HilSensor_<ContainerAllocator>&) { return value(); } }; } // namespace message_traits } // namespace ros namespace ros { namespace serialization { template<class ContainerAllocator> struct Serializer< ::mavros_msgs::HilSensor_<ContainerAllocator> > { template<typename Stream, typename T> inline static void allInOne(Stream& stream, T m) { stream.next(m.header); stream.next(m.acc); stream.next(m.gyro); stream.next(m.mag); stream.next(m.abs_pressure); stream.next(m.diff_pressure); stream.next(m.pressure_alt); stream.next(m.temperature); stream.next(m.fields_updated); } ROS_DECLARE_ALLINONE_SERIALIZER }; // struct HilSensor_ } // namespace serialization } // namespace ros namespace ros { namespace message_operations { template<class ContainerAllocator> struct Printer< ::mavros_msgs::HilSensor_<ContainerAllocator> > { template<typename Stream> static void stream(Stream& s, const std::string& indent, const ::mavros_msgs::HilSensor_<ContainerAllocator>& v) { s << indent << "header: "; s << std::endl; Printer< ::std_msgs::Header_<ContainerAllocator> >::stream(s, indent + " ", v.header); s << indent << "acc: "; s << std::endl; Printer< ::geometry_msgs::Vector3_<ContainerAllocator> >::stream(s, indent + " ", v.acc); s << indent << "gyro: "; s << std::endl; Printer< ::geometry_msgs::Vector3_<ContainerAllocator> >::stream(s, indent + " ", v.gyro); s << indent << "mag: "; s << std::endl; Printer< ::geometry_msgs::Vector3_<ContainerAllocator> >::stream(s, indent + " ", v.mag); s << indent << "abs_pressure: "; Printer<float>::stream(s, indent + " ", v.abs_pressure); s << indent << "diff_pressure: "; Printer<float>::stream(s, indent + " ", v.diff_pressure); s << indent << "pressure_alt: "; Printer<float>::stream(s, indent + " ", v.pressure_alt); s << indent << "temperature: "; Printer<float>::stream(s, indent + " ", v.temperature); s << indent << "fields_updated: "; Printer<uint32_t>::stream(s, indent + " ", v.fields_updated); } }; } // namespace message_operations } // namespace ros #endif // MAVROS_MSGS_MESSAGE_HILSENSOR_H
54e985f6b9d8b64f7eb05308ab70f682a20e2ba3
d4e96aa48ddff651558a3fe2212ebb3a3afe5ac3
/Modules/Registration/Common/include/itkMeanSquaresPointSetToImageMetric.h
65335353c95d8b1d3f61089c876de9bc4f9f3567
[ "SMLNJ", "BSD-3-Clause", "LicenseRef-scancode-free-unknown", "LicenseRef-scancode-mit-old-style", "LicenseRef-scancode-other-permissive", "LicenseRef-scancode-unknown-license-reference", "LicenseRef-scancode-warranty-disclaimer", "NTP", "IJG", "GPL-1.0-or-later", "libtiff", "BSD-4.3TAHOE", "Zlib", "MIT", "LicenseRef-scancode-proprietary-license", "Spencer-86", "Apache-2.0", "FSFUL", "LicenseRef-scancode-public-domain", "Libpng", "BSD-2-Clause" ]
permissive
nalinimsingh/ITK_4D
18e8929672df64df58a6446f047e6ec04d3c2616
95a2eacaeaffe572889832ef0894239f89e3f303
refs/heads/master
2020-03-17T18:58:50.953317
2018-10-01T20:46:43
2018-10-01T21:21:01
133,841,430
0
0
Apache-2.0
2018-05-17T16:34:54
2018-05-17T16:34:53
null
UTF-8
C++
false
false
4,332
h
/*========================================================================= * * Copyright Insight Software Consortium * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0.txt * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * *=========================================================================*/ #ifndef itkMeanSquaresPointSetToImageMetric_h #define itkMeanSquaresPointSetToImageMetric_h #include "itkPointSetToImageMetric.h" #include "itkCovariantVector.h" #include "itkPoint.h" namespace itk { /** \class MeanSquaresPointSetToImageMetric * \brief Computes similarity between pixel values of a point set and * intensity values of an image. * * This metric computes the average squared differences between pixels * in the point set and the transformed set of pixels. * * Spatial correspondance between both images is established through a * Transform. * * \ingroup RegistrationMetrics * \ingroup ITKRegistrationCommon */ template< typename TFixedPointSet, typename TMovingImage > class ITK_TEMPLATE_EXPORT MeanSquaresPointSetToImageMetric: public PointSetToImageMetric< TFixedPointSet, TMovingImage > { public: /** Standard class typedefs. */ typedef MeanSquaresPointSetToImageMetric Self; typedef PointSetToImageMetric< TFixedPointSet, TMovingImage > Superclass; typedef SmartPointer< Self > Pointer; typedef SmartPointer< const Self > ConstPointer; /** Method for creation through the object factory. */ itkNewMacro(Self); /** Run-time type information (and related methods). */ itkTypeMacro(MeanSquaresPointSetToImageMetric, Object); /** Types transferred from the base class */ typedef typename Superclass::RealType RealType; typedef typename Superclass::TransformType TransformType; typedef typename Superclass::TransformPointer TransformPointer; typedef typename Superclass::TransformParametersType TransformParametersType; typedef typename Superclass::TransformJacobianType TransformJacobianType; typedef typename Superclass::GradientPixelType GradientPixelType; typedef typename Superclass::InputPointType InputPointType; typedef typename Superclass::OutputPointType OutputPointType; typedef typename Superclass::MeasureType MeasureType; typedef typename Superclass::DerivativeType DerivativeType; typedef typename Superclass::FixedPointSetType FixedPointSetType; typedef typename Superclass::MovingImageType MovingImageType; typedef typename Superclass::FixedPointSetConstPointer FixedPointSetConstPointer; typedef typename Superclass::MovingImageConstPointer MovingImageConstPointer; typedef typename Superclass::PointIterator PointIterator; typedef typename Superclass::PointDataIterator PointDataIterator; /** Get the derivatives of the match measure. */ void GetDerivative(const TransformParametersType & parameters, DerivativeType & Derivative) const ITK_OVERRIDE; /** Get the value for single valued optimizers. */ MeasureType GetValue(const TransformParametersType & parameters) const ITK_OVERRIDE; /** Get value and derivatives for multiple valued optimizers. */ void GetValueAndDerivative(const TransformParametersType & parameters, MeasureType & Value, DerivativeType & Derivative) const ITK_OVERRIDE; protected: MeanSquaresPointSetToImageMetric(); virtual ~MeanSquaresPointSetToImageMetric() {} private: ITK_DISALLOW_COPY_AND_ASSIGN(MeanSquaresPointSetToImageMetric); }; } // end namespace itk #ifndef ITK_MANUAL_INSTANTIATION #include "itkMeanSquaresPointSetToImageMetric.hxx" #endif #endif
708f76d6c574868dd8e11f1ec1213ba17be9a875
4b08f6ab3a328a33302d82177823596b20a48a5e
/tests/triangle_tests.cpp
8fd66ed883e8a8eb30870083b40689735beb7ff3
[ "MIT" ]
permissive
gitbetter/Lightspace
dd741bdee24bed6e32787fcb6d240b1dc4b111cb
b00a29e2aac64cfac87ca30d2d053559d84289df
refs/heads/master
2022-03-28T10:58:22.585848
2020-01-12T00:17:31
2020-01-12T23:10:42
213,056,631
0
0
null
null
null
null
UTF-8
C++
false
false
2,562
cpp
#include "catch.hpp" #include "shapes.hpp" using namespace ls; TEST_CASE( "Triangle processing", "[triangles]" ) { SECTION( "Constructing a triangle" ) { auto p1 = f_point( 0, 1, 0 ); auto p2 = f_point( -1, 0, 0 ); auto p3 = f_point( 1, 0, 0 ); auto t = triangle::create( p1, p2, p3 ); REQUIRE( t->p1() == p1 ); REQUIRE( t->p2() == p2 ); REQUIRE( t->p3() == p3 ); REQUIRE( t->e1() == f_vector( -1, -1, 0 ) ); REQUIRE( t->e2() == f_vector( 1, -1, 0 ) ); REQUIRE( t->normal() == f_vector( 0, 0, -1 ) ); } SECTION( "Finding the normal on a triangle" ) { auto t = triangle::create( f_point( 0, 1, 0 ), f_point( -1, 0, 0 ), f_point( 1, 0, 0 ) ); auto n1 = t->normal( 0, 0.5f, 0 ); auto n2 = t->normal( -0.5f, 0.75f, 0 ); auto n3 = t->normal( 0.5f, 0.25f, 0 ); REQUIRE( n1 == t->normal() ); REQUIRE( n2 == t->normal() ); REQUIRE( n3 == t->normal() ); } SECTION( "Intersecting a ray parallel to the triangle" ) { auto t = triangle::create( f_point( 0, 1, 0 ), f_point( -1, 0, 0 ), f_point( 1, 0, 0 ) ); auto r = ray( f_point( 0, -1, -2 ), f_vector( 0, 1, 0 ) ); auto itrs = intersect( t, r ); REQUIRE( itrs.empty() ); } SECTION( "A ray misses the p1-p3 edge" ) { auto t = triangle::create( f_point( 0, 1, 0 ), f_point( -1, 0, 0 ), f_point( 1, 0, 0 ) ); auto r = ray( f_point( 1, 1, -2 ), f_vector( 0, 0, 1 ) ); auto itrs = intersect( t, r ); REQUIRE( itrs.empty() ); } SECTION( "A ray misses the p1-p2 edge" ) { auto t = triangle::create( f_point( 0, 1, 0 ), f_point( -1, 0, 0 ), f_point( 1, 0, 0 ) ); auto r = ray( f_point( -1, 1, -2 ), f_vector( 0, 0, 1 ) ); auto itrs = intersect( t, r ); REQUIRE( itrs.empty() ); } SECTION( "A ray misses the p2-p3 edge" ) { auto t = triangle::create( f_point( 0, 1, 0 ), f_point( -1, 0, 0 ), f_point( 1, 0, 0 ) ); auto r = ray( f_point( 0, -1, -2 ), f_vector( 0, 0, 1 ) ); auto itrs = intersect( t, r ); REQUIRE( itrs.empty() ); } SECTION( "A ray strikes a triangle" ) { auto t = triangle::create( f_point( 0, 1, 0 ), f_point( -1, 0, 0 ), f_point( 1, 0, 0 ) ); auto r = ray( f_point( 0, 0.5f, -2 ), f_vector( 0, 0, 1 ) ); auto itrs = intersect( t, r ); REQUIRE( itrs.size() == 1 ); REQUIRE( approx( itrs[0].time(), 2.f ) ); } };
0008d3a3207301cb7a98e181099d0f31a496a52c
d54259e11b684c4124723376c1485b99e387a5e0
/oprator_loading/IncrementDecrementOverloading.cpp
e24953fac0020e5be1191cc93d746970a3e93f60
[]
no_license
Saurabh-12/learn_cpp
754e5f1efdfe021beaf7425f9b1592cae58fd19e
805c0b2131481ac14bd1510868a638040ae1e605
refs/heads/master
2022-03-06T12:39:44.456471
2022-02-28T17:16:58
2022-02-28T17:16:58
116,156,269
0
0
null
null
null
null
UTF-8
C++
false
false
1,961
cpp
#include <iostream> class Digit { private: int m_digit; public: Digit(int digit = 0) : m_digit(digit) { } Digit &operator++(); // prefix Digit &operator--(); // prefix Digit operator++(int); // postfix Digit operator--(int); // postfix friend std::ostream &operator<<(std::ostream &out, const Digit &d); }; Digit &Digit::operator++() { // If our number is already at 9, wrap around to 0 if (m_digit == 9) m_digit = 0; // otherwise just increment to next number else ++m_digit; return *this; } Digit Digit::operator++(int) { // Create a temporary variable with our current digit Digit temp(m_digit); // Use prefix operator to increment this digit ++(*this); // apply operator // return temporary result return temp; // return saved state } Digit &Digit::operator--() { // If our number is already at 0, wrap around to 9 if (m_digit == 0) m_digit = 9; // otherwise just decrement to next number else --m_digit; return *this; } Digit Digit::operator--(int) { // Create a temporary variable with our current digit Digit temp(m_digit); // Use prefix operator to decrement this digit --(*this); // apply operator // return temporary result return temp; // return saved state } std::ostream &operator<<(std::ostream &out, const Digit &d) { out << d.m_digit; return out; } int main() { Digit digit(5); std::cout <<"Digit "<< digit<<"\n"; std::cout << "Pre Increment "<< ++digit <<"\n"; // calls Digit::operator++(); std::cout <<"Post Increment"<< digit++ <<"\n"; // calls Digit::operator++(int); std::cout << "Digit: "<< digit <<"\n"; std::cout << "Pre Decrement "<< --digit <<"\n"; // calls Digit::operator--(); std::cout << "Post Decrement "<< digit--<<"\n"; // calls Digit::operator--(int); std::cout << "Digit "<< digit<<"\n"; return 0; }
712d796a63923b63b999ac2f1f521d3abcfe1355
8e8dba16703c723dbc4b387b7ba51c472a0b53c9
/100SameTree.cpp
f3ecb10b73195c96f301fd3f2b49ca5d24f0e4c5
[]
no_license
Carl-LQ/leetcodeprac
18cfed3825a1c7add650540eed771022a95da0d8
758d414f0d3ddf440d1207dd4cb78e1038b88eab
refs/heads/master
2020-06-01T17:27:41.167241
2019-10-14T01:23:47
2019-10-14T01:23:47
190,865,363
1
0
null
null
null
null
UTF-8
C++
false
false
1,088
cpp
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { string st1 = serializedTreeByPreOrder(p); string st2 = serializedTreeByPreOrder(q); return st1 == st2; } string serializedTreeByPreOrder(TreeNode* t) { if (!t) return "#!"; string res = to_string(t->val)+"!"; res += serializedTreeByPreOrder(t->left); res += serializedTreeByPreOrder(t->right); return res; } }; // 因为看过左神的序列化一棵树的操作,我先入为主写了这个方法,还有一些比较简洁的递归的方法,树的题感觉基本用递归都非常好写。。。 bool isSameTree(TreeNode* p, TreeNode* q) { if (!p && !q) return true; if (!p || !q) return false; return p->val == q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right); }
9fff88cb8bccce52657e11ba804eaf16f4f93e50
ae55b48aebec623cf6f6cb47ab4c39b153d7b4d1
/Ugine/src/isometricsprite.cpp
8d4dcc24fda8a713a9018303adb3be8a6d633eeb
[]
no_license
AlvaroAbad/Star-Control
fed2d7c935debac4b83cfb7083be948d9e4fb2bf
a693aa4603d0eff65ec56d81eb774ad8e7fd7b21
refs/heads/master
2021-01-12T06:05:07.870793
2016-12-24T18:34:00
2016-12-24T18:34:00
77,295,571
0
0
null
null
null
null
UTF-8
C++
false
false
934
cpp
#include "..\include\isometricsprite.h" #include "../include/math.h" #include "../include/image.h" #include <math.h> void IsometricSprite::SetPosition(double x, double y, double z) { Sprite::SetPosition(x, y, z); } void IsometricSprite::SetCollision(CollisionMode mode) { (mode != CollisionMode::COLLISION_PIXEL ? Sprite::SetCollision(mode) : Sprite::SetCollision(CollisionMode::COLLISION_RECT)); } void IsometricSprite::Update(double elapsed, const Map * map) { Sprite::Update(elapsed, map); TransformIsoCoords(GetX(), GetY(), GetZ(), &screenX, &screenY); } void IsometricSprite::UpdateCollisionBox() { double x, y, w, h,cx,cy; x = GetX() - GetImage()->GetHandleX() *fabs(GetScaleX()); y = GetY() - GetImage()->GetHandleX() *fabs(GetScaleY()); w = GetImage()->GetWidth()*fabs(GetScaleX()); h = GetImage()->GetWidth()*fabs(GetScaleX()); cx = x + w / 2; cy = y + h / 2; Sprite::UpdateCollisionBox(x, y, w, h, cx, cy); }
d2a57a93db69723e72a5a55369705a9f6e76e624
f1d9f7801723699226aa421901c87b667ef1cbcb
/src/inet/routing/bgpv4/BgpConfigReader.h
057362d853d976d0973d33b17e437d9981f909b7
[]
no_license
screw/inet
0466bb6ba8f4503c63f4b8d46185536c2d5124e3
bfb2657cdbc7e776ed891dbc52dd9b1f411bf23e
refs/heads/master
2021-06-03T05:27:20.514930
2019-01-11T18:27:28
2019-01-11T18:27:28
137,349,174
0
0
null
2018-06-14T11:27:54
2018-06-14T11:27:53
null
UTF-8
C++
false
false
2,507
h
// // Copyright (C) 2010 Helene Lageber // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License // along with this program; if not, see <http://www.gnu.org/licenses/>. // #ifndef __INET_BGPCONFIGREADER #define __INET_BGPCONFIGREADER #include "inet/common/INETDefs.h" #include "inet/routing/bgpv4/BgpCommon.h" #include "inet/routing/bgpv4/BgpRouter.h" #include "inet/networklayer/ipv4/Ipv4InterfaceData.h" #include "inet/networklayer/contract/ipv4/Ipv4Address.h" #include "inet/routing/bgpv4/bgpmessage/BgpHeader_m.h" #include "inet/routing/bgpv4/bgpmessage/BgpUpdate.h" #include "inet/common/lifecycle/ILifecycle.h" namespace inet { namespace bgp { class INET_API BgpConfigReader { private: cModule *bgpModule = nullptr; IInterfaceTable *ift = nullptr; BgpRouter *bgpRouter = nullptr; public: BgpConfigReader(cModule *bgpModule, IInterfaceTable *ift); virtual ~BgpConfigReader() {}; void loadConfigFromXML(cXMLElement *bgpConfig, BgpRouter *bgpRouter); private: std::vector<const char *> findInternalPeers(cXMLElementList& ASConfig); void loadASConfig(cXMLElementList& ASConfig); void loadEbgpSessionConfig(cXMLElementList& ASConfig, cXMLElementList& sessionList, simtime_t *delayTab); AsId findMyAS(cXMLElementList& ASList, int& outRouterPosition); void loadTimerConfig(cXMLElementList& timerConfig, simtime_t *delayTab); int isInInterfaceTable(IInterfaceTable *ifTable, Ipv4Address addr); int isInInterfaceTable(IInterfaceTable *ifTable, std::string ifName); unsigned int calculateStartDelay(int rtListSize, unsigned char rtPosition, unsigned char rtPeerPosition); bool getBoolAttrOrPar(const cXMLElement& ifConfig, const char *name) const; int getIntAttrOrPar(const cXMLElement& ifConfig, const char *name) const; const char *getStrAttrOrPar(const cXMLElement& ifConfig, const char *name) const; }; } // namespace bgp } // namespace inet #endif // ifndef __INET_BGPCONFIGREADER
bf7c31b98eb7040d86105ffea01aaf88b4caa900
929bb2cbaad96935f1e7bb34c86aa2a254cc6cdc
/src/manifold/models/iris/iris_srcs/topology/mesh.h
da1cd9998842cd444390af5f966f007bd558a1bb
[]
no_license
gthparch/macsim
744e137e40ff2ab3ee9eecd459dae80c39674555
96a7da4dd98401b2468e3cf1ff783fc5f56fbad0
refs/heads/master
2023-09-01T04:32:11.652522
2023-03-27T18:31:16
2023-03-27T18:31:16
44,985,778
97
46
null
2023-04-21T18:56:26
2015-10-26T17:35:08
C++
UTF-8
C++
false
false
3,337
h
/* * ===================================================================================== * *! \brief Filename: Mesh.h * * Description: The class defines functions for a generic k-ary 2D Mesh * with the network being a direct network of size n=k^2. * * In order to understand connections the port assigned is as follows * * Router ports * ************ SEND DATA ********* * port 0: Connects to interface * port 1: Connects to WEST going * port 2: Connects to EAST going * port 3: Connects to NORTH going * port 4: Connects to SOUTH going * ************ SEND SIGNAL ********* * port p: Connects to interface * port p+1: Connects to WEST going * port p+2: Connects to EAST going * port p+3: Connects to NORTH going * port p+4: Connects to SOUTH going * ************ INCOMING DATA ********* * port 2p: Connects to interface * port 2p+1: Connects to WEST going * port 2p+2: Connects to EAST going * port 2p+3: Connects to NORTH going * port 2p+4: Connects to SOUTH going * ************ INCOMING SIGBAL ********* * port 3p: Connects to interface * port 3p+1: Connects to WEST going * port 3p+2: Connects to EAST going * port 3p+3: Connects to NORTH going * port 3p+4: Connects to SOUTH going * * a links * ----> R0 ----> R1 ----> * <--- |^ <--- |^ <--- * blinks * || || * v| v| * ----> R2 ----> R3 ----> * a |^ b * || * l || l * i V| i * n n * k k * s s * * Version: 1.0 * Created: 05/05/2010 12:01:12 AM * Revision: none * Compiler: gcc * * Author: Mitchelle Rasquinha * Company: Georgia Institute of Technology * * ===================================================================================== */ #ifndef _Mesh_h_INC #define _Mesh_h_INC #include "topology.h" extern std::vector<ManifoldProcessor*> g_macsim_terminals; class Mesh : public Topology { private: Mesh (); //DO NOT USE (use the public one) public: Mesh (macsim_c* simBase); ~Mesh (); void parse_config(std::map<std::string,std::string>& p); void connect_interface_terminal(void); void connect_interface_routers(void); void connect_routers(void); void assign_node_ids( component_type t); std::string print_stats(void); std::string print_stats(component_type t); void set_router_outports( uint n); protected: vector<uint> &split(const string &s, char delim, vector<uint> &elems); vector<uint> split(const std::string &s, char delim); public: uint ports; uint vcs; uint credits; uint buffer_size; uint links; uint no_nodes; uint dim; uint grid_size; vector<uint> mapping; macsim_c* m_simBase; /**< macsim_c base class for simulation globals */ }; /* ----- end of class Mesh ----- */ #endif /* ----- #ifndef _Mesh_h_INC ----- */
64934f8e4fc7fd913ab5a7cb4704168cd408fe63
b1fba1dc4fc20524d1175674d0fa82fed4d521e2
/Source/1512682/Item.cpp
aff99f0cea35f318ae4171260e96801b3962588c
[]
no_license
elhoangvu/ExpenditureManagement-Win32
c44db390b9fbf938240ace529179de22ccb03d5e
70f4268b5937564db5c5747bfcc421f851faaf75
refs/heads/master
2020-04-10T09:09:03.037035
2019-01-26T01:54:24
2019-01-26T01:54:24
160,928,059
5
2
null
null
null
null
UTF-8
C++
false
false
1,301
cpp
#include "stdafx.h" #include "Item.h" WCHAR* ItemName[] = { L"Ăn uống", L"Di chuyển", L"Nhà cửa", L"Xe cộ", L"Nhu yếu phẩm",L"Dịch vụ" }; Item * Item::create(ItemType itemType, WCHAR * description, WCHAR* date, unsigned __int64 cost) { Item* item = new (std::nothrow) Item; item->_itemType = itemType; int len = wcslen(description) + 1; item->_description = new WCHAR[len]; wcscpy(item->_description, description); item->_description[len] = NULL; len = wcslen(date) + 1; item->_date = new WCHAR[len]; wcscpy(item->_date, date); item->_date[len] = NULL; item->_cost = cost; return item; } ItemType Item::getItemType() { return _itemType; } unsigned __int64 Item::getCost() { return _cost; } WCHAR * Item::getName() { return ITEMNAME[_itemType]; /*switch (_itemType) { case AN_UONG: return L"Ăn uống"; case DI_CHUYEN: return L"Di chuyển"; case NHA_CUA: return L"Nhà cửa"; case XE_CO: return L"Xe cộ"; case NHU_YEU_PHAM: return L"Nhu yếu phẩm"; }*/ return L"Dịch vụ"; } WCHAR * Item::getDescription() { return _description; } WCHAR * Item::getDate() { return _date; } Item::Item() { _itemType = ItemType::AN_UONG; _cost = 0; _description = nullptr; } Item::~Item() { if (_description) delete _description; }
eebbb62c7a754ae466f56ecb63a443fe775609a1
3189793506a679d8c9f077359eb5dd010de92a22
/DueTimerLite.h
b3542c9139314f3f745d0cf8442bcda9aa30e8a4
[ "Apache-2.0" ]
permissive
petmo338/ignitioncontrol
8d645b4128c7f2397f98926a84341b82c967ffdf
7b8a83c9f30aa5a69e04f23cb190a61d3002656b
refs/heads/master
2021-01-17T15:35:24.258518
2016-03-31T19:07:26
2016-03-31T19:07:26
39,463,351
1
2
null
null
null
null
UTF-8
C++
false
false
1,941
h
/* DueTimerLite.h - DueTimerLite header file, definition of methods and attributes... For instructions, go to https://github.com/ivanseidel/DueTimer Created by Ivan Seidel Gomes, March, 2013. Modified by Philipp Klaus, June 2013. Released into the public domain. */ #ifdef __SAM3X8E__ #ifndef DueTimerLite_h #define DueTimerLite_h #define NR_OF_TIMERS 1 #include "Arduino.h" #include <inttypes.h> class DueTimerLite { protected: // Represents the timer id (index for the array of Timer structs) int timer; uint8_t selected_clock; // Stores the object timer frequency // (allows to access current timer period and frequency): static double _frequency[NR_OF_TIMERS]; // Picks the best clock to lower the error static uint8_t bestClock(double frequency, uint32_t& retRC); public: struct Timer { Tc *tc; uint32_t channel; IRQn_Type irq; }; //static DueTimerLite getAvailable(); // Store timer configuration (static, as it's fix for every object) static const Timer Timers[1]; // Needs to be public, because the handlers are outside class: static void (*callbacks[1])(); DueTimerLite(int _timer); DueTimerLite attachInterrupt(void (*isr)()); DueTimerLite detachInterrupt(); DueTimerLite start(double frequency = 50.0); DueTimerLite stop(); DueTimerLite setFrequency(double frequency); DueTimerLite updateFrequency(double frequency); // DueTimerLite setPeriod(long microseconds); //double getFrequency(); //long getPeriod(); }; // Just to call Timer.getAvailable instead of Timer::getAvailable() : //extern DueTimerLite Timer; // //extern DueTimerLite Timer0; //extern DueTimerLite Timer1; //extern DueTimerLite Timer2; //extern DueTimerLite Timer3; //extern DueTimerLite Timer4; //extern DueTimerLite Timer5; //extern DueTimerLite Timer6; //extern DueTimerLite Timer7; //extern DueTimerLite Timer8; #endif #else #error Oops! Trying to include DueTimerLite on another device? #endif
daf18eebd7341e23e2b6fba01d950ecfc86eed9b
c1f84922db5bb83580294dc0f62e5649294c62c3
/private-gravity/ext_lib/CoinIpopt/build/Ipopt/tutorial/CodingExercise/Cpp/1-skeleton/TutorialCpp_nlp.cpp
25c2fd37bbfa5ca1479f071da938a90198f26e71
[ "BSD-3-Clause", "BSD-3-Clause-Clear" ]
permissive
arunveeramany/GridOpt
9c13428134de0e0d4cecc9b9b367997e5a3b3b7d
79e6762cec29b807a5424b39be30daba97d65bc1
refs/heads/master
2020-03-17T22:00:22.848308
2018-05-18T00:58:10
2018-05-18T00:58:10
133,976,617
0
0
null
null
null
null
UTF-8
C++
false
false
108
cpp
/home/hlh/private-gravity/ext_lib/CoinIpopt/Ipopt/tutorial/CodingExercise/Cpp/1-skeleton/TutorialCpp_nlp.cpp
998589b2a86c22259fbb82950d89a2a641289510
15b9ddab4b911f0f1fbf5483dc31a89eb4f58e79
/codeforces/117/A.cpp
7e272baa88c9b020304aaaee4f82a23af24d1945
[]
no_license
draftcode/programming_contest
7de837696f88e32fe118d1c87a56d3ed6e7a7c4f
5bbd0214405e2187f6c1707c4b9981f0982b7e0e
refs/heads/master
2016-09-06T00:50:37.500448
2012-07-10T09:32:00
2012-10-27T10:09:43
1,948,203
0
0
null
null
null
null
UTF-8
C++
false
false
1,111
cpp
#include <algorithm> #include <functional> #include <iostream> #include <limits> #include <numeric> #include <queue> #include <vector> #include <cstring> #include <cstdio> #include <cmath> using namespace std; typedef long long ll; typedef pair<int, int> P; #define rep(i,n) for(int i = 0; i < n; i++) #define each(it,c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it) ll n, m; bool is_up(ll t) { return (t/(m-1) % 2) == 0; } ll floor(ll t) { ll q = t%(m-1); if (is_up(t)) { return q+1; } else { return m-q; } } ll how_long(ll s, ll f, ll t) { if (s == f) { return 0; } else if (s > f) { if (is_up(t)) { return (m-s)+(m-f); } else { return s-f; } } else { if (is_up(t)) { return f-s; } else { return f+s-2; } } } int main(void) { ios::sync_with_stdio(false); cin >> n >> m; rep (i, n) { ll s, f, t; cin >> s >> f >> t; if (s == f) { cout << t << endl; } else { t += how_long(floor(t), s, t); t += how_long(s, f, t); cout << t << endl; } } return 0; }
bfbcd0a10fcb51e863dedbf6110f67c268c7b84b
5f231ebdb98e6c83e2e3586eec2820b36bd4dec8
/graphics/niwa/graphics/Image.cpp
fe3b63364d6446232f994642af898e0df68ba83f
[]
no_license
uttumuttu/niwa
926583b4cc9b420b827d3cb991227f2b34486778
9e4fc79c469cf7db8b081e489df6e7d04140f336
refs/heads/master
2016-09-03T06:22:19.772697
2015-09-02T15:26:15
2015-09-02T15:26:15
40,969,355
0
0
null
null
null
null
UTF-8
C++
false
false
5,974
cpp
/** * @file * @author Mikko Kauppila * * Copyright (C) Mikko Kauppila 2009. */ #include "Image.h" #include "jpeglib.h" #include "jerror.h" #include <setjmp.h> #include "niwa/logging/Logger.h" namespace { static niwa::logging::Logger Log(typeid(niwa::graphics::Image)); } namespace { struct JpegErrorManager { /** * The first field must be the base * jpeg_error_mgr object; the latter * fields are used for manual extensions. */ struct jpeg_error_mgr pub; jmp_buf setjmp_buffer; }; /** * @brief Receives control for a fatal error. * * (Verbatim from libjpeg documentation.) * * Information sufficient to generate the error message has been * stored in cinfo->err; call output_message to display it. * Control must NOT return to the caller; generally this routine * will exit() or longjmp() somewhere. Typically you would override * this routine to get rid of the exit() default behavior. Note that * if you continue processing, you should clean up the JPEG object * with jpeg_abort() or jpeg_destroy(). */ static void jpegErrorExit(j_common_ptr cinfo) { // Since interaction between jumping and // object destruction is non-portable, it's // best to use a distinct scope for the non-jumping portion. { char buffer[JMSG_LENGTH_MAX]; (*cinfo->err->format_message) (cinfo, buffer); Log.error() << buffer; } JpegErrorManager* mgr = reinterpret_cast<JpegErrorManager*>(cinfo->err); longjmp(mgr->setjmp_buffer, 1); } /** * @brief Decide whether or not to emit a warning or trace message; if so, calls output_message. * * (Verbatim from libjpeg documentation.) * * The main reason for overriding this method would be to abort on warnings. * * @param msg_level -1 for warnings, 0 and up for trace messages */ static void jpegEmitMessage(j_common_ptr cinfo, int msg_level) { char buffer[JMSG_LENGTH_MAX]; (*cinfo->err->format_message) (cinfo, buffer); if(msg_level > 0) { // Do nothing on debug messages; we assume // libjpeg is bug-free. } else if(msg_level == 0) { Log.info() << buffer; } else { Log.warn() << buffer; } } } namespace { /** * Adds a fully opaque alpha channel to RGB source data. */ static boost::shared_array<unsigned char> addAlphaChannel( size_t nPixels, boost::shared_array<unsigned char> source) { boost::shared_array<unsigned char> result(new unsigned char[4*nPixels]); for(size_t i=0; i<nPixels; ++i) { for(size_t j=0; j<3; ++j) { result[4*i+j] = source[3*i+j]; } result[4*i+3] = 255; } return result; } } namespace niwa { namespace graphics { Image* Image::create(std::string const& filename, Format format) { unsigned char* data = 0; FILE* infile = 0; if(fopen_s(&infile, filename.c_str(), "rb")) { return 0; } struct jpeg_decompress_struct cinfo; struct JpegErrorManager jerr; cinfo.err = jpeg_std_error(&jerr.pub); // register manual callbacks jerr.pub.error_exit = jpegErrorExit; jerr.pub.emit_message = jpegEmitMessage; #pragma warning(disable:4611) if(setjmp(jerr.setjmp_buffer)) { // This is where we jump on errors. // Notice that interaction between // jumping and C++ object destruction // is non-portable, so we must not // use managed objects during image loading. delete[] data; data = 0; jpeg_destroy_decompress(&cinfo); fclose(infile); return 0; } jpeg_create_decompress(&cinfo); jpeg_stdio_src(&cinfo, infile); jpeg_read_header(&cinfo, TRUE); jpeg_start_decompress(&cinfo); const int w = cinfo.output_width; const int h = cinfo.output_height; data = new unsigned char[w*h*3]; for(int i=0; i<h; ++i) { unsigned char* ptr = &data[i*w*3]; jpeg_read_scanlines(&cinfo, &ptr, 1); } jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); fclose(infile); boost::shared_array<unsigned char> result(data); if(format == RGBA) { result = addAlphaChannel(w*h, result); } return new Image(w, h, format, result); } Image::~Image() { // ignored } size_t Image::getWidth() const { return width_; } size_t Image::getHeight() const { return height_; } std::pair<size_t, size_t> Image::getDimensions() const { return std::pair<size_t, size_t>(width_, height_); } boost::shared_array<unsigned char> const Image::getData() const { return data_; } boost::shared_array<unsigned char> Image::getData() { return data_; } Image::Format Image::getFormat() const { return format_; } Image::Image(size_t width, size_t height, Image::Format format, boost::shared_array<unsigned char> data) : width_(width), height_(height), format_(format), data_(data) { // ignored } } }
f5f187297a1c1c95ff60bf5d588a5c2bbb592569
e7a7aa4baebc29d9bc797abc32e3ea7ab5484f93
/src/main.cpp
d0264da9cf2e1c903d3bb358ebf04a6656a7b41c
[]
no_license
widon1104/Calculator
97c21f9518a765a2927075007eea1473c7f4d89b
5535563104b98f250d164ad954cd62aa21ca78e8
refs/heads/master
2021-01-10T02:19:39.757975
2015-05-28T13:26:02
2015-05-28T13:26:02
36,435,687
0
0
null
null
null
null
UTF-8
C++
false
false
1,685
cpp
#include <iostream> #include <string> #include "scanner.h" #include "parser.h" #include "calc.h" #include "exception.h" #include "commandparser.h" int main() { STATUS status = STATUS_OK; Calc calc; do { std::cout <<"> "; Scanner scanner(std::cin); if (!scanner.IsEmpty()) { if (scanner.IsCommand()) { //parse command CommandParser cParser(scanner, calc); status = cParser.Excute(); } else { Parser parser(scanner, calc); try { status = parser.Parse(); if (status == STATUS_OK) { std::cout << parser.Calculate() << std::endl; } else { std::cout << "Syntax Error" << std::endl; } } catch (SyntaxError &se) { std::cout << se.what() << std::endl; std::cout << se.StackTrace() << std::endl; } catch (Exception& e) { std::cout << e.what() << std::endl; } catch (std::bad_alloc& e) { std::cout << e.what() << std::endl; } catch (...) { std::cout << "Internal Error" << std::endl; } //clear the input buffer while (!scanner.IsDone()) { scanner.Accept(); } } } else { std::cout << "Expression is empty" << std::endl; } } while (status != STATUS_QUIT); return 0; }
a0f4c859cafda248acd1bfdfa52fdab3ee55f74c
c7850d478e1a62fc8b016225d7a748cf1b0cb81f
/tp1/notebooks/traductor/stock_to_capacity/src/AuxeffectofRatioofStocktoCapacityonGrowthCoefficient.h
d6e2fe9d8699a0257030fb85c9d243ce4b150c17
[]
no_license
dioh/sed_2017_tps
8bac2bd824335581a33ad9b010747ea4af273130
c17d1d2d4b1d80bafe33053f7f2b58661b9bcc65
refs/heads/master
2021-09-14T07:35:57.009752
2018-05-09T19:16:33
2018-05-09T19:16:33
103,854,748
0
0
null
null
null
null
UTF-8
C++
false
false
1,113
h
#ifndef _AuxeffectofRatioofStocktoCapacityonGrowthCoefficient_H_ #define _AuxeffectofRatioofStocktoCapacityonGrowthCoefficient_H_ #include <random> #include "atomic.h" #include "VTime.h" #define AUXEFFECTOFRATIOOFSTOCKTOCAPACITYONGROWTHCOEFFICIENT "AuxeffectofRatioofStocktoCapacityonGrowthCoefficient" class AuxeffectofRatioofStocktoCapacityonGrowthCoefficient : public Atomic { public: AuxeffectofRatioofStocktoCapacityonGrowthCoefficient(const string &name = AUXEFFECTOFRATIOOFSTOCKTOCAPACITYONGROWTHCOEFFICIENT ); virtual string className() const { return AUXEFFECTOFRATIOOFSTOCKTOCAPACITYONGROWTHCOEFFICIENT ;} protected: Model &initFunction(); Model &externalFunction( const ExternalMessage & ); Model &internalFunction( const InternalMessage & ); Model &outputFunction( const CollectMessage & ); private: // Input ports const Port &in_ratioofStocktoCapacity; // Output ports Port &out; // State variables double ratioofStocktoCapacity; // // Check set of state variables bool isSet_ratioofStocktoCapacity; // }; #endif
43d8c2379eacf74d161a27e1a3dd68cc365872cf
9e4eb8483ba93ffc5dacf866780602f744e9e09b
/F/src/renderable.hxx
88735b143c191976d386a88e61b3cbdd4633a324
[ "Apache-2.0" ]
permissive
xiovlad/Ctrl-Alt-Test
929df689cf8fbb95e9f22575dc6ed6d2c69cbe84
bb40ab654f0088c367370a913e43fcdb39b19a3e
refs/heads/master
2023-03-26T05:12:55.094084
2019-05-12T21:50:35
2019-05-12T21:50:35
null
0
0
null
null
null
null
UTF-8
C++
false
false
1,805
hxx
// // Renderable thing // #ifndef RENDERABLE_HXX # define RENDERABLE_HXX #include "sys/msys.h" #define INLINE_ACCESSOR(t, x) inline t Renderable::x() const {return _##x;} inline int Renderable::sortHint() const { // On prend ce bloc de 32 bits et on le considére comme un int // On est sur du little endian, donc on veut : VBO, texture, shader return *((int*)&_vboId); } // // identifiants de l'objet, de textures, de shader, de VBO // INLINE_ACCESSOR(int, id); inline void Renderable::setId(int id) { _id = id; } INLINE_ACCESSOR(Renderable::CustomUniformFct, customSetUniform); inline void Renderable::setCustomSetUniform(Renderable::CustomUniformFct fct) { _customSetUniform = fct; } INLINE_ACCESSOR(Texture::id, textureId); INLINE_ACCESSOR(Texture::id, texture2Id); INLINE_ACCESSOR(Texture::id, bumpTextureId); INLINE_ACCESSOR(Texture::id, specularTextureId); INLINE_ACCESSOR(Shader::id, shaderId); INLINE_ACCESSOR(VBO::id, vboId); /* inline void Renderable::setTexture(Texture::id textureId) { _textureId = textureId; } inline void Renderable::setBumpTexture(Texture::id bumpTextureId) { _bumpTextureId = bumpTextureId; } inline void Renderable::setSpecularTexture(Texture::id specularTextureId) { _specularTextureId = specularTextureId; } */ inline void Renderable::setTexture2(Texture::id texture2Id) { _texture2Id = texture2Id; } inline float Renderable::shininess() const { return 0.5f * float(_shininess); } inline void Renderable::setShininess(unsigned char shininess) { _shininess = shininess; } INLINE_ACCESSOR(unsigned int, numberOfVertices); INLINE_ACCESSOR(unsigned int, primitiveType); inline void Renderable::setPrimitiveType(unsigned int primitiveType) { _primitiveType = primitiveType; } #undef INLINE_ACCESSOR #endif // RENDERABLE_HXX
b3f4ec3a666801566af6cfc184e95534d8c67ad1
ab1c643f224197ca8c44ebd562953f0984df321e
/pchealth/client/datacoll/wmiprovn/pch_drive.h
0de80ad1a8cf5193727cd94c1d16aeda188a2e92
[]
no_license
KernelPanic-OpenSource/Win2K3_NT_admin
e162e0452fb2067f0675745f2273d5c569798709
d36e522f16bd866384bec440517f954a1a5c4a4d
refs/heads/master
2023-04-12T13:25:45.807158
2021-04-13T16:33:59
2021-04-13T16:33:59
357,613,696
0
1
null
null
null
null
UTF-8
C++
false
false
2,070
h
/******************************************************************** Copyright (c) 1999 Microsoft Corporation Module Name: PCH_Drive.H Abstract: WBEM provider class definition for PCH_Drive class Revision History: Ghim-Sim Chua (gschua) 04/27/99 - Created ********************************************************************/ // Property set identification //============================ #ifndef _PCH_Drive_H_ #define _PCH_Drive_H_ #define PROVIDER_NAME_PCH_DRIVE "PCH_Drive" // Property name externs -- defined in PCH_Drive.cpp //================================================= extern const WCHAR* pAvailable ; extern const WCHAR* pTimeStamp ; extern const WCHAR* pChange ; extern const WCHAR* pDriveLetter ; extern const WCHAR* pFilesystemType ; extern const WCHAR* pFree ; extern const WCHAR* pDescription; extern const WCHAR* pMediaType; class CPCH_Drive : public Provider { public: // Constructor/destructor //======================= CPCH_Drive(const CHString& chsClassName, LPCWSTR lpszNameSpace) : Provider(chsClassName, lpszNameSpace) {}; virtual ~CPCH_Drive() {}; protected: // Reading Functions //============================ virtual HRESULT EnumerateInstances(MethodContext* pMethodContext, long lFlags = 0L); virtual HRESULT GetObject(CInstance* pInstance, long lFlags = 0L) { return (WBEM_E_PROVIDER_NOT_CAPABLE); }; virtual HRESULT ExecQuery(MethodContext *pMethodContext, CFrameworkQuery& Query, long lFlags = 0L) { return (WBEM_E_PROVIDER_NOT_CAPABLE); }; // Writing Functions //============================ virtual HRESULT PutInstance(const CInstance& Instance, long lFlags = 0L) { return (WBEM_E_PROVIDER_NOT_CAPABLE); }; virtual HRESULT DeleteInstance(const CInstance& Instance, long lFlags = 0L) { return (WBEM_E_PROVIDER_NOT_CAPABLE); }; // Other Functions virtual HRESULT ExecMethod( const CInstance& Instance, const BSTR bstrMethodName, CInstance *pInParams, CInstance *pOutParams, long lFlags = 0L ) { return (WBEM_E_PROVIDER_NOT_CAPABLE); }; } ; #endif
8121aab6421482cae2e5891c2ee8762e05303626
b44e1008ccbd87f50df13e92cb40513372e10d73
/Source/Usul/MPL/StaticAssert.h
433f283598f08412ec8da6dfa5c0b2f6defbca1f
[]
no_license
perryiv/haf
d76bc205604b14c407a9883a44b7e2ea89e1cf2d
12e1df5fc67fcad3462f335072e025ef866f7c86
refs/heads/master
2020-04-06T07:44:05.044643
2018-12-04T20:06:02
2018-12-04T20:06:02
157,283,369
0
0
null
null
null
null
UTF-8
C++
false
false
1,288
h
/////////////////////////////////////////////////////////////////////////////// // // Copyright (c) 2009, Perry L Miller IV // All rights reserved. // BSD License: http://www.opensource.org/licenses/bsd-license.html // /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// // // Compile-time assertion. // /////////////////////////////////////////////////////////////////////////////// #ifndef _USUL_MPL_STATIC_ASSERT_H_ #define _USUL_MPL_STATIC_ASSERT_H_ namespace Usul { namespace MPL { /////////////////////////////////////////////////////////////////////////////// // // Generic declaration but no definition. Specialization for true. // /////////////////////////////////////////////////////////////////////////////// template < bool T > struct StaticAssert; template <> struct StaticAssert < true >{}; } // namespace MPL } // namespace Usul /////////////////////////////////////////////////////////////////////////////// // // Handy macro for using static assertions. // /////////////////////////////////////////////////////////////////////////////// #define USUL_STATIC_ASSERT(exp)\ Usul::MPL::StaticAssert<(exp)>() #endif // _USUL_MPL_STATIC_ASSERT_H_
1219640d0c3d2fd8d3b06635cd233cffc705a2c4
da2d1435dc13318743ec02fbb6c486db6fa3fa04
/c++/3/stack.cc
02e3ff2ea1ec4876e0235e7f0eb54f0535ce499f
[]
no_license
lileismall/project
0ffe0d87db5dec6f52a0f40094ab2d54d3063888
8f026bcdbe320667c98fb0e38a55250ca7591235
refs/heads/master
2020-06-23T05:53:52.242300
2019-08-08T07:26:18
2019-08-08T07:26:18
198,536,395
1
0
null
null
null
null
UTF-8
C++
false
false
218
cc
#include "stack.h" //using namespace std; int main() { Stack s; s.setSize(5); //默认是10 s.push(10); s.push(12); s.push(14); s.printTop(); s.pop(); s.printTop(); return 0; }
ec65fb0cd3a8a535e80aa3b47ad01fd64cafcdde
28c000caf6617ba2074e0f2a8fc936ccb8c01fb3
/poj/3072/6026969_WA.cc
2c55a27dae6a67a56232076158a65b90d7f324bd
[]
no_license
ATM006/acm_problem_code
f597fa31033fd663b14d74ad94cae3f7c1629b99
ac40d230cd450bcce60df801eb3b8ce9409dfaac
refs/heads/master
2020-08-31T21:34:00.707529
2014-01-23T05:30:42
2014-01-23T05:30:42
null
0
0
null
null
null
null
UTF-8
C++
false
false
2,263
cc
#include <cmath> #include <queue> #include <cstdio> #include <cstring> using namespace std; int n,R,xx[25],yy[25],mark[25]; double map[25][25],dis[25],pi; struct QN{ int u; double d,deg;//d表示距离,deg表示当前角度 bool operator<(const QN a)const { return a.d<d; } }; const double inf = 1000000000; double ABS(double x){return x>0?x:-x;} double Distance(int i,int j) { double ax=xx[i]-xx[j]; double by=yy[i]-yy[j]; return sqrt(ax*ax+by*by); } void Dij_BFS() { QN now,next; priority_queue<QN>Q; for(int i=1;i<=n;i++){dis[i]=inf;mark[i]=0;} now.u=1; now.d=0; dis[1]=0; now.deg=atan((yy[n]-yy[1])*1.0/(xx[n]-xx[1])*1.0); Q.push(now); for(int k,i=1;i<n;i++) { do{ if(Q.size()==0)return ; now=Q.top();Q.pop(); k=now.u; }while(mark[k]); mark[k]=1; for(int i=1;i<=n;i++) { if(!mark[i]&&map[k][i]>0) { double ang1=now.deg; double ang2=atan((yy[i]-yy[k])*1.0/(xx[i]-xx[k])*1.0); double ang=min(fabs(ang1-ang2),2*pi-fabs(ang1-ang2)); ang=180*ang/pi; if(dis[i]>dis[k]+map[k][i]+ang) { dis[i]=dis[k]+map[k][i]+ang; next.u=i; next.d=dis[i]; next.deg=ang2; Q.push(next); } } } } } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); pi=acos(-1.0); while(scanf("%d%d",&R,&n),n!=-1||R!=-1) { for(int i=1;i<=n;i++){ map[i][i]=0; scanf("%d%d",xx+i,yy+i); } for(int i=1;i<=n;i++) { for(int j=i+1;j<=n;j++){ double tem=Distance(i,j); if(tem>R){ map[i][j]=map[j][i]=-1; continue; } map[i][j]=map[j][i]=tem; } } Dij_BFS(); if(dis[n]==inf)puts("impossible"); else printf("%.0lf\n",dis[n]); } return 0; }
c8b6c09737f61ee233f67124ec1cf76de9113cce
64e4fabf9b43b6b02b14b9df7e1751732b30ad38
/src/chromium/gen/gen_combined/services/service_manager/public/mojom/service_manager.mojom.h
f21cf64d61144733998e6dcdfe9a826c2256b618
[ "BSD-3-Clause" ]
permissive
ivan-kits/skia-opengl-emscripten
8a5ee0eab0214c84df3cd7eef37c8ba54acb045e
79573e1ee794061bdcfd88cacdb75243eff5f6f0
refs/heads/master
2023-02-03T16:39:20.556706
2020-12-25T14:00:49
2020-12-25T14:00:49
null
0
0
null
null
null
null
UTF-8
C++
false
false
14,405
h
// Copyright 2013 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef SERVICES_SERVICE_MANAGER_PUBLIC_MOJOM_SERVICE_MANAGER_MOJOM_H_ #define SERVICES_SERVICE_MANAGER_PUBLIC_MOJOM_SERVICE_MANAGER_MOJOM_H_ #include <stdint.h> #include <limits> #include <type_traits> #include <utility> #include "base/callback.h" #include "base/macros.h" #include "base/optional.h" #include "mojo/public/cpp/bindings/mojo_buildflags.h" #if BUILDFLAG(MOJO_TRACE_ENABLED) #include "base/trace_event/trace_event.h" #endif #include "mojo/public/cpp/bindings/clone_traits.h" #include "mojo/public/cpp/bindings/equals_traits.h" #include "mojo/public/cpp/bindings/lib/serialization.h" #include "mojo/public/cpp/bindings/struct_ptr.h" #include "mojo/public/cpp/bindings/struct_traits.h" #include "mojo/public/cpp/bindings/union_traits.h" #include "services/service_manager/public/mojom/service_manager.mojom-shared.h" #include "services/service_manager/public/mojom/service_manager.mojom-forward.h" #include "services/service_manager/public/mojom/connector.mojom.h" #include <string> #include <vector> #include "mojo/public/cpp/bindings/associated_interface_ptr.h" #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h" #include "mojo/public/cpp/bindings/associated_interface_request.h" #include "mojo/public/cpp/bindings/interface_ptr.h" #include "mojo/public/cpp/bindings/interface_request.h" #include "mojo/public/cpp/bindings/lib/control_message_handler.h" #include "mojo/public/cpp/bindings/raw_ptr_impl_ref_traits.h" #include "mojo/public/cpp/bindings/thread_safe_interface_ptr.h" #include "mojo/public/cpp/bindings/lib/native_enum_serialization.h" #include "mojo/public/cpp/bindings/lib/native_struct_serialization.h" #include "base/component_export.h" namespace service_manager { namespace mojom { class ServiceManagerListenerProxy; template <typename ImplRefTraits> class ServiceManagerListenerStub; class ServiceManagerListenerRequestValidator; class COMPONENT_EXPORT(SERVICE_MANAGER_MOJOM) ServiceManagerListener : public ServiceManagerListenerInterfaceBase { public: static const char Name_[]; static constexpr uint32_t Version_ = 0; static constexpr bool PassesAssociatedKinds_ = false; static constexpr bool HasSyncMethods_ = false; using Base_ = ServiceManagerListenerInterfaceBase; using Proxy_ = ServiceManagerListenerProxy; template <typename ImplRefTraits> using Stub_ = ServiceManagerListenerStub<ImplRefTraits>; using RequestValidator_ = ServiceManagerListenerRequestValidator; using ResponseValidator_ = mojo::PassThroughFilter; enum MethodMinVersions : uint32_t { kOnInitMinVersion = 0, kOnServiceCreatedMinVersion = 0, kOnServiceStartedMinVersion = 0, kOnServicePIDReceivedMinVersion = 0, kOnServiceFailedToStartMinVersion = 0, kOnServiceStoppedMinVersion = 0, }; virtual ~ServiceManagerListener() {} virtual void OnInit(std::vector<RunningServiceInfoPtr> running_services) = 0; virtual void OnServiceCreated(RunningServiceInfoPtr service) = 0; virtual void OnServiceStarted(const ::service_manager::Identity& identity, uint32_t pid_deprecated) = 0; virtual void OnServicePIDReceived(const ::service_manager::Identity& identity, uint32_t pid) = 0; virtual void OnServiceFailedToStart(const ::service_manager::Identity& identity) = 0; virtual void OnServiceStopped(const ::service_manager::Identity& identity) = 0; }; class ServiceManagerProxy; template <typename ImplRefTraits> class ServiceManagerStub; class ServiceManagerRequestValidator; class COMPONENT_EXPORT(SERVICE_MANAGER_MOJOM) ServiceManager : public ServiceManagerInterfaceBase { public: static const char Name_[]; static constexpr uint32_t Version_ = 0; static constexpr bool PassesAssociatedKinds_ = false; static constexpr bool HasSyncMethods_ = false; using Base_ = ServiceManagerInterfaceBase; using Proxy_ = ServiceManagerProxy; template <typename ImplRefTraits> using Stub_ = ServiceManagerStub<ImplRefTraits>; using RequestValidator_ = ServiceManagerRequestValidator; using ResponseValidator_ = mojo::PassThroughFilter; enum MethodMinVersions : uint32_t { kAddListenerMinVersion = 0, }; virtual ~ServiceManager() {} virtual void AddListener(ServiceManagerListenerPtr listener) = 0; }; class COMPONENT_EXPORT(SERVICE_MANAGER_MOJOM) ServiceManagerListenerProxy : public ServiceManagerListener { public: using InterfaceType = ServiceManagerListener; explicit ServiceManagerListenerProxy(mojo::MessageReceiverWithResponder* receiver); void OnInit(std::vector<RunningServiceInfoPtr> running_services) final; void OnServiceCreated(RunningServiceInfoPtr service) final; void OnServiceStarted(const ::service_manager::Identity& identity, uint32_t pid_deprecated) final; void OnServicePIDReceived(const ::service_manager::Identity& identity, uint32_t pid) final; void OnServiceFailedToStart(const ::service_manager::Identity& identity) final; void OnServiceStopped(const ::service_manager::Identity& identity) final; private: mojo::MessageReceiverWithResponder* receiver_; }; class COMPONENT_EXPORT(SERVICE_MANAGER_MOJOM) ServiceManagerProxy : public ServiceManager { public: using InterfaceType = ServiceManager; explicit ServiceManagerProxy(mojo::MessageReceiverWithResponder* receiver); void AddListener(ServiceManagerListenerPtr listener) final; private: mojo::MessageReceiverWithResponder* receiver_; }; class COMPONENT_EXPORT(SERVICE_MANAGER_MOJOM) ServiceManagerListenerStubDispatch { public: static bool Accept(ServiceManagerListener* impl, mojo::Message* message); static bool AcceptWithResponder( ServiceManagerListener* impl, mojo::Message* message, std::unique_ptr<mojo::MessageReceiverWithStatus> responder); }; template <typename ImplRefTraits = mojo::RawPtrImplRefTraits<ServiceManagerListener>> class ServiceManagerListenerStub : public mojo::MessageReceiverWithResponderStatus { public: using ImplPointerType = typename ImplRefTraits::PointerType; ServiceManagerListenerStub() {} ~ServiceManagerListenerStub() override {} void set_sink(ImplPointerType sink) { sink_ = std::move(sink); } ImplPointerType& sink() { return sink_; } bool Accept(mojo::Message* message) override { if (ImplRefTraits::IsNull(sink_)) return false; return ServiceManagerListenerStubDispatch::Accept( ImplRefTraits::GetRawPointer(&sink_), message); } bool AcceptWithResponder( mojo::Message* message, std::unique_ptr<mojo::MessageReceiverWithStatus> responder) override { if (ImplRefTraits::IsNull(sink_)) return false; return ServiceManagerListenerStubDispatch::AcceptWithResponder( ImplRefTraits::GetRawPointer(&sink_), message, std::move(responder)); } private: ImplPointerType sink_; }; class COMPONENT_EXPORT(SERVICE_MANAGER_MOJOM) ServiceManagerStubDispatch { public: static bool Accept(ServiceManager* impl, mojo::Message* message); static bool AcceptWithResponder( ServiceManager* impl, mojo::Message* message, std::unique_ptr<mojo::MessageReceiverWithStatus> responder); }; template <typename ImplRefTraits = mojo::RawPtrImplRefTraits<ServiceManager>> class ServiceManagerStub : public mojo::MessageReceiverWithResponderStatus { public: using ImplPointerType = typename ImplRefTraits::PointerType; ServiceManagerStub() {} ~ServiceManagerStub() override {} void set_sink(ImplPointerType sink) { sink_ = std::move(sink); } ImplPointerType& sink() { return sink_; } bool Accept(mojo::Message* message) override { if (ImplRefTraits::IsNull(sink_)) return false; return ServiceManagerStubDispatch::Accept( ImplRefTraits::GetRawPointer(&sink_), message); } bool AcceptWithResponder( mojo::Message* message, std::unique_ptr<mojo::MessageReceiverWithStatus> responder) override { if (ImplRefTraits::IsNull(sink_)) return false; return ServiceManagerStubDispatch::AcceptWithResponder( ImplRefTraits::GetRawPointer(&sink_), message, std::move(responder)); } private: ImplPointerType sink_; }; class COMPONENT_EXPORT(SERVICE_MANAGER_MOJOM) ServiceManagerListenerRequestValidator : public mojo::MessageReceiver { public: bool Accept(mojo::Message* message) override; }; class COMPONENT_EXPORT(SERVICE_MANAGER_MOJOM) ServiceManagerRequestValidator : public mojo::MessageReceiver { public: bool Accept(mojo::Message* message) override; }; class COMPONENT_EXPORT(SERVICE_MANAGER_MOJOM) RunningServiceInfo { public: using DataView = RunningServiceInfoDataView; using Data_ = internal::RunningServiceInfo_Data; template <typename... Args> static RunningServiceInfoPtr New(Args&&... args) { return RunningServiceInfoPtr( base::in_place, std::forward<Args>(args)...); } template <typename U> static RunningServiceInfoPtr From(const U& u) { return mojo::TypeConverter<RunningServiceInfoPtr, U>::Convert(u); } template <typename U> U To() const { return mojo::TypeConverter<U, RunningServiceInfo>::Convert(*this); } RunningServiceInfo(); RunningServiceInfo( const ::service_manager::Identity& identity, uint32_t pid, InstanceState state); ~RunningServiceInfo(); // Clone() is a template so it is only instantiated if it is used. Thus, the // bindings generator does not need to know whether Clone() or copy // constructor/assignment are available for members. template <typename StructPtrType = RunningServiceInfoPtr> RunningServiceInfoPtr Clone() const; // Equals() is a template so it is only instantiated if it is used. Thus, the // bindings generator does not need to know whether Equals() or == operator // are available for members. template <typename T, typename std::enable_if<std::is_same< T, RunningServiceInfo>::value>::type* = nullptr> bool Equals(const T& other) const; template <typename UserType> static std::vector<uint8_t> Serialize(UserType* input) { return mojo::internal::SerializeImpl< RunningServiceInfo::DataView, std::vector<uint8_t>>(input); } template <typename UserType> static mojo::Message SerializeAsMessage(UserType* input) { return mojo::internal::SerializeAsMessageImpl< RunningServiceInfo::DataView>(input); } // The returned Message is serialized only if the message is moved // cross-process or cross-language. Otherwise if the message is Deserialized // as the same UserType |input| will just be moved to |output| in // DeserializeFromMessage. template <typename UserType> static mojo::Message WrapAsMessage(UserType input) { return mojo::Message(std::make_unique< internal::RunningServiceInfo_UnserializedMessageContext< UserType, RunningServiceInfo::DataView>>(0, 0, std::move(input))); } template <typename UserType> static bool Deserialize(const void* data, size_t data_num_bytes, UserType* output) { return mojo::internal::DeserializeImpl<RunningServiceInfo::DataView>( data, data_num_bytes, std::vector<mojo::ScopedHandle>(), output, Validate); } template <typename UserType> static bool Deserialize(const std::vector<uint8_t>& input, UserType* output) { return RunningServiceInfo::Deserialize( input.size() == 0 ? nullptr : &input.front(), input.size(), output); } template <typename UserType> static bool DeserializeFromMessage(mojo::Message input, UserType* output) { auto context = input.TakeUnserializedContext< internal::RunningServiceInfo_UnserializedMessageContext< UserType, RunningServiceInfo::DataView>>(); if (context) { *output = std::move(context->TakeData()); return true; } input.SerializeIfNecessary(); return mojo::internal::DeserializeImpl<RunningServiceInfo::DataView>( input.payload(), input.payload_num_bytes(), std::move(*input.mutable_handles()), output, Validate); } ::service_manager::Identity identity; uint32_t pid; InstanceState state; private: static bool Validate(const void* data, mojo::internal::ValidationContext* validation_context); }; template <typename StructPtrType> RunningServiceInfoPtr RunningServiceInfo::Clone() const { return New( mojo::Clone(identity), mojo::Clone(pid), mojo::Clone(state) ); } template <typename T, typename std::enable_if<std::is_same< T, RunningServiceInfo>::value>::type*> bool RunningServiceInfo::Equals(const T& other_struct) const { if (!mojo::Equals(this->identity, other_struct.identity)) return false; if (!mojo::Equals(this->pid, other_struct.pid)) return false; if (!mojo::Equals(this->state, other_struct.state)) return false; return true; } } // namespace mojom } // namespace service_manager namespace mojo { template <> struct COMPONENT_EXPORT(SERVICE_MANAGER_MOJOM) StructTraits<::service_manager::mojom::RunningServiceInfo::DataView, ::service_manager::mojom::RunningServiceInfoPtr> { static bool IsNull(const ::service_manager::mojom::RunningServiceInfoPtr& input) { return !input; } static void SetToNull(::service_manager::mojom::RunningServiceInfoPtr* output) { output->reset(); } static const decltype(::service_manager::mojom::RunningServiceInfo::identity)& identity( const ::service_manager::mojom::RunningServiceInfoPtr& input) { return input->identity; } static decltype(::service_manager::mojom::RunningServiceInfo::pid) pid( const ::service_manager::mojom::RunningServiceInfoPtr& input) { return input->pid; } static decltype(::service_manager::mojom::RunningServiceInfo::state) state( const ::service_manager::mojom::RunningServiceInfoPtr& input) { return input->state; } static bool Read(::service_manager::mojom::RunningServiceInfo::DataView input, ::service_manager::mojom::RunningServiceInfoPtr* output); }; } // namespace mojo #endif // SERVICES_SERVICE_MANAGER_PUBLIC_MOJOM_SERVICE_MANAGER_MOJOM_H_
ac972cbd7e2a488e1348cddbbd803b28b228f084
6419389be9c1110c77830a76d8f4fdb355f5dfe7
/include/SimpleGeometrySvc.hh
40a25c75c81ff43641fa4227b3f31b95b45bcfc1
[]
no_license
yfujii/g4sim
8bdb3f87a9c748da8fed9816284e8d7d5240ede7
b6d47d677d8c61c4b5e76a6f2c696fb3a863404a
refs/heads/master
2021-01-21T16:54:38.792077
2014-04-11T05:57:41
2014-04-11T05:57:41
null
0
0
null
null
null
null
UTF-8
C++
false
false
1,572
hh
//---------------------------------------------------------------------------// //Description: 1. Setup SimpleGeometry geometry and manage associated sensitive detectors // 2. Communicate with associated sensitive detectors //Author: Wu Chen([email protected]) //Created: 17 Oct, 2012 //Modified: 11, Jan, 2013 // Support G4Hype, G4TwistedTube //Comment: about fVerboseLevel: // You can change it by calling SetVerbose(int) somewhere // or modifying input card before starting a run. // Either way would set verbose of selected sensitive detectors to the same level // 0: Only Warnings and Errors // 1: Important information // >= 5: All information //---------------------------------------------------------------------------// #ifndef SimpleGeometrySvc_h #define SimpleGeometrySvc_h 1 #include "globals.hh" #include "MyVGeometrySvc.hh" class G4VPhysicalVolume; class SimpleGeometryParameter; class SimpleGeometrySvc : public MyVGeometrySvc { public: SimpleGeometrySvc(G4String name, G4String opt = ""); virtual ~SimpleGeometrySvc(); virtual G4VPhysicalVolume* SetGeometry();//Setup geometry. Should be called before the begin of a run //Special functions void ConstructVolumes(); G4VPhysicalVolume* PlaceVolumes(); //=>Access SimpleGeometryParameter* get_GeometryParameter(){ return m_GeometryParameter; } //=>Modify void set_GeometryParameter( SimpleGeometryParameter* val ); private: SimpleGeometryParameter* m_GeometryParameter; }; #endif
7487a7b50469df36a9627638d94a37aa1b1b6ad8
0103ce0fa860abd9f76cfda4979d304d450aba25
/Source/bmalloc/bmalloc/Heap.cpp
4aceb26fb937c104b45286b537a9b295e0d90f60
[]
no_license
1C-Company-third-party/webkit
2dedcae3b5424dd5de1fee6151cd33b35d08567f
f7eec5c68105bea4d4a1dca91734170bdfd537b6
refs/heads/master
2022-11-08T12:23:55.380720
2019-04-18T12:13:32
2019-04-18T12:13:32
182,072,953
5
2
null
2022-10-18T10:56:03
2019-04-18T11:10:56
C++
UTF-8
C++
false
false
17,110
cpp
/* * Copyright (C) 2014-2017 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "Heap.h" #include "AvailableMemory.h" #include "BumpAllocator.h" #include "Chunk.h" #include "Environment.h" #include "Gigacage.h" #include "DebugHeap.h" #include "PerProcess.h" #include "Scavenger.h" #include "SmallLine.h" #include "SmallPage.h" #include "VMHeap.h" #include "bmalloc.h" #include <thread> namespace bmalloc { Heap::Heap(HeapKind kind, std::lock_guard<StaticMutex>&) : m_kind(kind) , m_vmPageSizePhysical(vmPageSizePhysical()) , m_debugHeap(nullptr) { RELEASE_BASSERT(vmPageSizePhysical() >= smallPageSize); RELEASE_BASSERT(vmPageSize() >= vmPageSizePhysical()); initializeLineMetadata(); initializePageMetadata(); if (PerProcess<Environment>::get()->isDebugHeapEnabled()) m_debugHeap = PerProcess<DebugHeap>::get(); else { Gigacage::ensureGigacage(); #if GIGACAGE_ENABLED if (usingGigacage()) { RELEASE_BASSERT(gigacageBasePtr()); m_largeFree.add(LargeRange(gigacageBasePtr(), gigacageSize(), 0)); } #endif } m_scavenger = PerProcess<Scavenger>::get(); } bool Heap::usingGigacage() { return isGigacage(m_kind) && gigacageBasePtr(); } void* Heap::gigacageBasePtr() { return Gigacage::basePtr(gigacageKind(m_kind)); } size_t Heap::gigacageSize() { return Gigacage::size(gigacageKind(m_kind)); } void Heap::initializeLineMetadata() { size_t sizeClassCount = bmalloc::sizeClass(smallLineSize); size_t smallLineCount = m_vmPageSizePhysical / smallLineSize; m_smallLineMetadata.grow(sizeClassCount * smallLineCount); for (size_t sizeClass = 0; sizeClass < sizeClassCount; ++sizeClass) { size_t size = objectSize(sizeClass); LineMetadata* pageMetadata = &m_smallLineMetadata[sizeClass * smallLineCount]; size_t object = 0; size_t line = 0; while (object < m_vmPageSizePhysical) { line = object / smallLineSize; size_t leftover = object % smallLineSize; size_t objectCount; size_t remainder; divideRoundingUp(smallLineSize - leftover, size, objectCount, remainder); pageMetadata[line] = { static_cast<unsigned char>(leftover), static_cast<unsigned char>(objectCount) }; object += objectCount * size; } // Don't allow the last object in a page to escape the page. if (object > m_vmPageSizePhysical) { BASSERT(pageMetadata[line].objectCount); --pageMetadata[line].objectCount; } } } void Heap::initializePageMetadata() { auto computePageSize = [&](size_t sizeClass) { size_t size = objectSize(sizeClass); if (sizeClass < bmalloc::sizeClass(smallLineSize)) return m_vmPageSizePhysical; for (size_t pageSize = m_vmPageSizePhysical; pageSize < pageSizeMax; pageSize += m_vmPageSizePhysical) { RELEASE_BASSERT(pageSize <= chunkSize / 2); size_t waste = pageSize % size; if (waste <= pageSize / pageSizeWasteFactor) return pageSize; } return pageSizeMax; }; for (size_t i = 0; i < sizeClassCount; ++i) m_pageClasses[i] = (computePageSize(i) - 1) / smallPageSize; } void Heap::scavenge(std::lock_guard<StaticMutex>&) { for (auto& list : m_freePages) { for (auto* chunk : list) { for (auto* page : chunk->freePages()) { if (!page->hasPhysicalPages()) continue; vmDeallocatePhysicalPagesSloppy(page->begin()->begin(), pageSize(&list - &m_freePages[0])); page->setHasPhysicalPages(false); } } } for (auto& list : m_chunkCache) { while (!list.isEmpty()) deallocateSmallChunk(list.pop(), &list - &m_chunkCache[0]); } for (auto& range : m_largeFree) { vmDeallocatePhysicalPagesSloppy(range.begin(), range.size()); range.setPhysicalSize(0); } } void Heap::deallocateLineCache(std::lock_guard<StaticMutex>&, LineCache& lineCache) { for (auto& list : lineCache) { while (!list.isEmpty()) { size_t sizeClass = &list - &lineCache[0]; m_lineCache[sizeClass].push(list.popFront()); } } } void Heap::allocateSmallChunk(std::lock_guard<StaticMutex>& lock, size_t pageClass) { RELEASE_BASSERT(isActiveHeapKind(m_kind)); size_t pageSize = bmalloc::pageSize(pageClass); Chunk* chunk = [&]() { if (!m_chunkCache[pageClass].isEmpty()) return m_chunkCache[pageClass].pop(); void* memory = allocateLarge(lock, chunkSize, chunkSize); Chunk* chunk = new (memory) Chunk(pageSize); m_objectTypes.set(chunk, ObjectType::Small); forEachPage(chunk, pageSize, [&](SmallPage* page) { page->setHasPhysicalPages(true); page->setHasFreeLines(lock, true); chunk->freePages().push(page); }); m_scavenger->schedule(0); return chunk; }(); m_freePages[pageClass].push(chunk); } void Heap::deallocateSmallChunk(Chunk* chunk, size_t pageClass) { m_objectTypes.set(chunk, ObjectType::Large); size_t size = m_largeAllocated.remove(chunk); bool hasPhysicalPages = true; forEachPage(chunk, pageSize(pageClass), [&](SmallPage* page) { if (!page->hasPhysicalPages()) hasPhysicalPages = false; }); size_t physicalSize = hasPhysicalPages ? size : 0; m_largeFree.add(LargeRange(chunk, size, physicalSize)); } SmallPage* Heap::allocateSmallPage(std::lock_guard<StaticMutex>& lock, size_t sizeClass, LineCache& lineCache) { RELEASE_BASSERT(isActiveHeapKind(m_kind)); if (!lineCache[sizeClass].isEmpty()) return lineCache[sizeClass].popFront(); if (!m_lineCache[sizeClass].isEmpty()) return m_lineCache[sizeClass].popFront(); m_scavenger->didStartGrowing(); SmallPage* page = [&]() { size_t pageClass = m_pageClasses[sizeClass]; if (m_freePages[pageClass].isEmpty()) allocateSmallChunk(lock, pageClass); Chunk* chunk = m_freePages[pageClass].tail(); chunk->ref(); SmallPage* page = chunk->freePages().pop(); if (chunk->freePages().isEmpty()) m_freePages[pageClass].remove(chunk); if (!page->hasPhysicalPages()) { m_scavenger->scheduleIfUnderMemoryPressure(pageSize(pageClass)); vmAllocatePhysicalPagesSloppy(page->begin()->begin(), pageSize(pageClass)); page->setHasPhysicalPages(true); } return page; }(); page->setSizeClass(sizeClass); return page; } void Heap::deallocateSmallLine(std::lock_guard<StaticMutex>& lock, Object object, LineCache& lineCache) { BASSERT(!object.line()->refCount(lock)); SmallPage* page = object.page(); page->deref(lock); if (!page->hasFreeLines(lock)) { page->setHasFreeLines(lock, true); lineCache[page->sizeClass()].push(page); } if (page->refCount(lock)) return; size_t sizeClass = page->sizeClass(); size_t pageClass = m_pageClasses[sizeClass]; List<SmallPage>::remove(page); // 'page' may be in any thread's line cache. Chunk* chunk = Chunk::get(page); if (chunk->freePages().isEmpty()) m_freePages[pageClass].push(chunk); chunk->freePages().push(page); chunk->deref(); if (!chunk->refCount()) { m_freePages[pageClass].remove(chunk); if (!m_chunkCache[pageClass].isEmpty()) deallocateSmallChunk(m_chunkCache[pageClass].pop(), pageClass); m_chunkCache[pageClass].push(chunk); } m_scavenger->schedule(pageSize(pageClass)); } void Heap::allocateSmallBumpRangesByMetadata( std::lock_guard<StaticMutex>& lock, size_t sizeClass, BumpAllocator& allocator, BumpRangeCache& rangeCache, LineCache& lineCache) { RELEASE_BASSERT(isActiveHeapKind(m_kind)); SmallPage* page = allocateSmallPage(lock, sizeClass, lineCache); SmallLine* lines = page->begin(); BASSERT(page->hasFreeLines(lock)); size_t smallLineCount = m_vmPageSizePhysical / smallLineSize; LineMetadata* pageMetadata = &m_smallLineMetadata[sizeClass * smallLineCount]; auto findSmallBumpRange = [&](size_t& lineNumber) { for ( ; lineNumber < smallLineCount; ++lineNumber) { if (!lines[lineNumber].refCount(lock)) { if (pageMetadata[lineNumber].objectCount) return true; } } return false; }; auto allocateSmallBumpRange = [&](size_t& lineNumber) -> BumpRange { char* begin = lines[lineNumber].begin() + pageMetadata[lineNumber].startOffset; unsigned short objectCount = 0; for ( ; lineNumber < smallLineCount; ++lineNumber) { if (lines[lineNumber].refCount(lock)) break; if (!pageMetadata[lineNumber].objectCount) continue; objectCount += pageMetadata[lineNumber].objectCount; lines[lineNumber].ref(lock, pageMetadata[lineNumber].objectCount); page->ref(lock); } return { begin, objectCount }; }; size_t lineNumber = 0; for (;;) { if (!findSmallBumpRange(lineNumber)) { page->setHasFreeLines(lock, false); BASSERT(allocator.canAllocate()); return; } // In a fragmented page, some free ranges might not fit in the cache. if (rangeCache.size() == rangeCache.capacity()) { lineCache[sizeClass].push(page); BASSERT(allocator.canAllocate()); return; } BumpRange bumpRange = allocateSmallBumpRange(lineNumber); if (allocator.canAllocate()) rangeCache.push(bumpRange); else allocator.refill(bumpRange); } } void Heap::allocateSmallBumpRangesByObject( std::lock_guard<StaticMutex>& lock, size_t sizeClass, BumpAllocator& allocator, BumpRangeCache& rangeCache, LineCache& lineCache) { RELEASE_BASSERT(isActiveHeapKind(m_kind)); size_t size = allocator.size(); SmallPage* page = allocateSmallPage(lock, sizeClass, lineCache); BASSERT(page->hasFreeLines(lock)); auto findSmallBumpRange = [&](Object& it, Object& end) { for ( ; it + size <= end; it = it + size) { if (!it.line()->refCount(lock)) return true; } return false; }; auto allocateSmallBumpRange = [&](Object& it, Object& end) -> BumpRange { char* begin = it.address(); unsigned short objectCount = 0; for ( ; it + size <= end; it = it + size) { if (it.line()->refCount(lock)) break; ++objectCount; it.line()->ref(lock); it.page()->ref(lock); } return { begin, objectCount }; }; Object it(page->begin()->begin()); Object end(it + pageSize(m_pageClasses[sizeClass])); for (;;) { if (!findSmallBumpRange(it, end)) { page->setHasFreeLines(lock, false); BASSERT(allocator.canAllocate()); return; } // In a fragmented page, some free ranges might not fit in the cache. if (rangeCache.size() == rangeCache.capacity()) { lineCache[sizeClass].push(page); BASSERT(allocator.canAllocate()); return; } BumpRange bumpRange = allocateSmallBumpRange(it, end); if (allocator.canAllocate()) rangeCache.push(bumpRange); else allocator.refill(bumpRange); } } LargeRange Heap::splitAndAllocate(LargeRange& range, size_t alignment, size_t size, AllocationKind allocationKind) { RELEASE_BASSERT(isActiveHeapKind(m_kind)); LargeRange prev; LargeRange next; size_t alignmentMask = alignment - 1; if (test(range.begin(), alignmentMask)) { size_t prefixSize = roundUpToMultipleOf(alignment, range.begin()) - range.begin(); std::pair<LargeRange, LargeRange> pair = range.split(prefixSize); prev = pair.first; range = pair.second; } if (range.size() - size > size / pageSizeWasteFactor) { std::pair<LargeRange, LargeRange> pair = range.split(size); range = pair.first; next = pair.second; } switch (allocationKind) { case AllocationKind::Virtual: if (range.physicalSize()) vmDeallocatePhysicalPagesSloppy(range.begin(), range.size()); break; case AllocationKind::Physical: if (range.physicalSize() < range.size()) { m_scavenger->scheduleIfUnderMemoryPressure(range.size()); vmAllocatePhysicalPagesSloppy(range.begin() + range.physicalSize(), range.size() - range.physicalSize()); range.setPhysicalSize(range.size()); } break; } if (prev) m_largeFree.add(prev); if (next) m_largeFree.add(next); m_objectTypes.set(Chunk::get(range.begin()), ObjectType::Large); m_largeAllocated.set(range.begin(), range.size()); return range; } void* Heap::tryAllocateLarge(std::lock_guard<StaticMutex>&, size_t alignment, size_t size, AllocationKind allocationKind) { RELEASE_BASSERT(isActiveHeapKind(m_kind)); BASSERT(isPowerOfTwo(alignment)); if (m_debugHeap) return m_debugHeap->memalignLarge(alignment, size, allocationKind); m_scavenger->didStartGrowing(); size_t roundedSize = size ? roundUpToMultipleOf(largeAlignment, size) : largeAlignment; if (roundedSize < size) // Check for overflow return nullptr; size = roundedSize; size_t roundedAlignment = roundUpToMultipleOf<largeAlignment>(alignment); if (roundedAlignment < alignment) // Check for overflow return nullptr; alignment = roundedAlignment; LargeRange range = m_largeFree.remove(alignment, size); if (!range) { if (usingGigacage()) return nullptr; range = PerProcess<VMHeap>::get()->tryAllocateLargeChunk(alignment, size, allocationKind); if (!range) return nullptr; m_largeFree.add(range); range = m_largeFree.remove(alignment, size); } return splitAndAllocate(range, alignment, size, allocationKind).begin(); } void* Heap::allocateLarge(std::lock_guard<StaticMutex>& lock, size_t alignment, size_t size, AllocationKind allocationKind) { void* result = tryAllocateLarge(lock, alignment, size, allocationKind); RELEASE_BASSERT(result); return result; } bool Heap::isLarge(std::lock_guard<StaticMutex>&, void* object) { return m_objectTypes.get(Object(object).chunk()) == ObjectType::Large; } size_t Heap::largeSize(std::lock_guard<StaticMutex>&, void* object) { return m_largeAllocated.get(object); } void Heap::shrinkLarge(std::lock_guard<StaticMutex>&, const Range& object, size_t newSize) { BASSERT(object.size() > newSize); size_t size = m_largeAllocated.remove(object.begin()); LargeRange range = LargeRange(object, size); splitAndAllocate(range, alignment, newSize, AllocationKind::Physical); m_scavenger->schedule(size); } void Heap::deallocateLarge(std::lock_guard<StaticMutex>&, void* object, AllocationKind allocationKind) { if (m_debugHeap) return m_debugHeap->freeLarge(object, allocationKind); size_t size = m_largeAllocated.remove(object); m_largeFree.add(LargeRange(object, size, allocationKind == AllocationKind::Physical ? size : 0)); m_scavenger->schedule(size); } } // namespace bmalloc
1ed23b9e1fc2be46023d01121bee7951898cc960
be59c390795966476e777e8b75e27b5999640ee0
/contrib/epee/include/storages/parserse_base_utils.h
02afb72a833c82bf5222d67a18ff5891151034e1
[ "BSD-3-Clause", "MIT", "LicenseRef-scancode-unknown-license-reference" ]
permissive
BRTChain/BRT-Chain
cdefbb1ab54c186b1f66e01c4bc5cf97b830f45a
e4bfc027bc82eb79940331f9d334ab31708d4202
refs/heads/master
2022-12-21T15:57:45.359810
2020-09-26T08:02:12
2020-09-26T08:02:12
298,766,400
1
0
null
null
null
null
UTF-8
C++
false
false
13,802
h
// Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above copyright // notice, this list of conditions and the following disclaimer in the // documentation and/or other materials provided with the distribution. // * Neither the name of the Andrey N. Sabelnikov nor the // names of its contributors may be used to endorse or promote products // derived from this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // #pragma once #include <algorithm> #include <boost/utility/string_ref.hpp> #include "misc_log_ex.h" #undef BRT_DEFAULT_LOG_CATEGORY #define BRT_DEFAULT_LOG_CATEGORY "serialization" namespace epee { namespace misc_utils { namespace parse { // 1: digit // 2: .eE (floating point) // 4: alpha // 8: whitespace // 16: allowed in float but doesn't necessarily mean it's a float // 32: \ and " (end of verbatim string) static const constexpr uint8_t lut[256]={ 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8, 8, 0, 0, // 16 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 32 8, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 16, 18, 0, // 48 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 0, 0, 0, 0, 0, 0, // 64 0, 4, 4, 4, 4, 22, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // 80 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 32, 0, 0, 0, // 96 0, 4, 4, 4, 4, 22, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // 112 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, // 128 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; static const constexpr unsigned char isx[256] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 10, 11, 12, 13, 14, 15, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 10, 11, 12, 13, 14, 15, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, }; inline bool isspace(char c) { return lut[(uint8_t)c] & 8; } inline bool isdigit(char c) { return lut[(uint8_t)c] & 1; } inline std::string transform_to_escape_sequence(const std::string& src) { static const char escaped[] = "\b\f\n\r\t\v\"\\/"; std::string::const_iterator it = std::find_first_of(src.begin(), src.end(), escaped, escaped + sizeof(escaped)); if (it == src.end()) return src; std::string res; res.reserve(2 * src.size()); res.assign(src.begin(), it); for(; it!=src.end(); ++it) { switch(*it) { case '\b': //Backspace (ascii code 08) res+="\\b"; break; case '\f': //Form feed (ascii code 0C) res+="\\f"; break; case '\n': //New line res+="\\n"; break; case '\r': //Carriage return res+="\\r"; break; case '\t': //Tab res+="\\t"; break; case '\v': //Vertical tab res+="\\v"; break; //case '\'': //Apostrophe or single quote // res+="\\'"; break; case '"': //Double quote res+="\\\""; break; case '\\': //Backslash caracter res+="\\\\"; break; case '/': //Backslash caracter res+="\\/"; break; default: res.push_back(*it); } } return res; } /* \b Backspace (ascii code 08) \f Form feed (ascii code 0C) \n New line \r Carriage return \t Tab \v Vertical tab \' Apostrophe or single quote \" Double quote \\ Backslash character */ inline void match_string2(std::string::const_iterator& star_end_string, std::string::const_iterator buf_end, std::string& val) { bool escape_mode = false; std::string::const_iterator it = star_end_string; ++it; std::string::const_iterator fi = it; while (fi != buf_end && ((lut[(uint8_t)*fi] & 32)) == 0) ++fi; val.assign(it, fi); it = fi; for(;it != buf_end;it++) { if(escape_mode/*prev_ch == '\\'*/) { switch(*it) { case 'b': //Backspace (ascii code 08) val.push_back(0x08);break; case 'f': //Form feed (ascii code 0C) val.push_back(0x0C);break; case 'n': //New line val.push_back('\n');break; case 'r': //Carriage return val.push_back('\r');break; case 't': //Tab val.push_back('\t');break; case 'v': //Vertical tab val.push_back('\v');break; case '\'': //Apostrophe or single quote val.push_back('\'');break; case '"': //Double quote val.push_back('"');break; case '\\': //Backslash character val.push_back('\\');break; case '/': //Slash character val.push_back('/');break; case 'u': //Unicode code point if (buf_end - it < 4) { ASSERT_MES_AND_THROW("Invalid Unicode escape sequence"); } else { uint32_t dst = 0; for (int i = 0; i < 4; ++i) { const unsigned char tmp = isx[(unsigned char)*++it]; CHECK_AND_ASSERT_THROW_MES(tmp != 0xff, "Bad Unicode encoding"); dst = dst << 4 | tmp; } // encode as UTF-8 if (dst <= 0x7f) { val.push_back(dst); } else if (dst <= 0x7ff) { val.push_back(0xc0 | (dst >> 6)); val.push_back(0x80 | (dst & 0x3f)); } else if (dst <= 0xffff) { val.push_back(0xe0 | (dst >> 12)); val.push_back(0x80 | ((dst >> 6) & 0x3f)); val.push_back(0x80 | (dst & 0x3f)); } else { ASSERT_MES_AND_THROW("Unicode code point is out or range"); } } break; default: val.push_back(*it); LOG_PRINT_L0("Unknown escape sequence :\"\\" << *it << "\""); } escape_mode = false; }else if(*it == '"') { star_end_string = it; return; }else if(*it == '\\') { escape_mode = true; } else { val.push_back(*it); } } ASSERT_MES_AND_THROW("Failed to match string in json entry: " << std::string(star_end_string, buf_end)); } inline bool match_string(std::string::const_iterator& star_end_string, std::string::const_iterator buf_end, std::string& val) { try { match_string2(star_end_string, buf_end, val); return true; } catch(...) { return false; } } inline void match_number2(std::string::const_iterator& star_end_string, std::string::const_iterator buf_end, boost::string_ref& val, bool& is_float_val, bool& is_signed_val) { val.clear(); uint8_t float_flag = 0; is_signed_val = false; size_t chars = 0; std::string::const_iterator it = star_end_string; if (it != buf_end && *it == '-') { is_signed_val = true; ++chars; ++it; } for(;it != buf_end;it++) { const uint8_t flags = lut[(uint8_t)*it]; if (flags & 16) { float_flag |= flags; ++chars; } else { val = boost::string_ref(&*star_end_string, chars); if(val.size()) { star_end_string = --it; is_float_val = !!(float_flag & 2); return; } else ASSERT_MES_AND_THROW("wrong number in json entry: " << std::string(star_end_string, buf_end)); } } ASSERT_MES_AND_THROW("wrong number in json entry: " << std::string(star_end_string, buf_end)); } inline bool match_number(std::string::const_iterator& star_end_string, std::string::const_iterator buf_end, boost::string_ref& val) { try { bool is_v_float = false;bool is_signed_val = false; match_number2(star_end_string, buf_end, val, is_v_float, is_signed_val); return !is_v_float; } catch(...) { return false; } } inline void match_word2(std::string::const_iterator& star_end_string, std::string::const_iterator buf_end, boost::string_ref& val) { val.clear(); for(std::string::const_iterator it = star_end_string;it != buf_end;it++) { if (!(lut[(uint8_t)*it] & 4)) { val = boost::string_ref(&*star_end_string, std::distance(star_end_string, it)); if(val.size()) { star_end_string = --it; return; }else ASSERT_MES_AND_THROW("failed to match word number in json entry: " << std::string(star_end_string, buf_end)); } } ASSERT_MES_AND_THROW("failed to match word number in json entry: " << std::string(star_end_string, buf_end)); } inline bool match_word(std::string::const_iterator& star_end_string, std::string::const_iterator buf_end, boost::string_ref& val) { try { match_word2(star_end_string, buf_end, val); return true; } catch(...) { return false; } } inline bool match_word_with_extrasymb(std::string::const_iterator& star_end_string, std::string::const_iterator buf_end, std::string& val) { val.clear(); for(std::string::const_iterator it = star_end_string;it != buf_end;it++) { if(!isalnum(*it) && *it != '-' && *it != '_') { val.assign(star_end_string, it); if(val.size()) { star_end_string = --it; return true; }else return false; } } return false; } inline bool match_word_til_equal_mark(std::string::const_iterator& star_end_string, std::string::const_iterator buf_end, std::string::const_iterator& word_end) { word_end = star_end_string; for(std::string::const_iterator it = star_end_string;it != buf_end;it++) { if(isspace(*it)) { continue; }else if( *it == '=' ) { star_end_string = it; word_end = it; return true; } } return false; } } } }
7907db3d1baadd40057cbd8b40374e22142c854c
f8aa6e689d132d660e71335552b37cc1a5cb0786
/HaveDone/POJ/2406暴力求子串.cpp
6b700a70ea9cbe13a59e1be6ed949f1a6f932918
[]
no_license
VarickQ/ACM
91171f381fa57b6644f10b1a4a5a9aeaeeff12b2
5d4f2c14272b28c7ff2cb7ebcd4e91400345a096
refs/heads/master
2021-01-10T14:34:07.812097
2017-05-14T09:58:54
2017-05-14T09:58:54
8,500,751
4
1
null
null
null
null
UTF-8
C++
false
false
449
cpp
#include <iostream> #include <string> using namespace std; char str[1000001]; int main() { while(gets(str)) { string s; s = str; if(s == ".")break; string c=""; for(int i=0 ; i < s.length() ; i++ ) { c += s[i]; if(s.length()%c.length() != 0) continue; int numS = s.length()/c.length(); string f=""; for(int j=0 ; j < numS ; j++ ) f += c; if(f==s) { printf("%d\n", numS); break; } } } return 0; }
e44b1e5fa27d21c02a1ca8433aa8ea499ea8d627
4d0a3d58bd66e588a50b834a8b8a65a5dac81170
/Exam/Piscine_cpp/d02/ex01/Fixed.hpp
9731be21387e2aa5a69e1caa3797c1da94280b6e
[]
no_license
fredatgithub/42
0384b35529a42cf09bbe6dc9362b6ad5c5b3e044
7c1352ab9c8dda4a381ce5a11cd3bbbfad078841
refs/heads/master
2023-03-17T09:47:48.850239
2019-12-15T18:13:13
2019-12-15T18:13:13
null
0
0
null
null
null
null
UTF-8
C++
false
false
564
hpp
#ifndef FIXED_HPP # define FIXED_HPP #include <iostream> class Fixed { private: int m_nFixedPointValue; int const m_nFractionalBit; public: Fixed(void); Fixed(int const p_nValue); Fixed(float const p_fValue); Fixed(Fixed const & p_Fixed); ~Fixed(void); Fixed & operator=(Fixed const & p_Fixed); int getRawBits(void) const; void setRawBits(int const p_nRaw); void setRawBits(float const p_fRaw); float toFloat(void) const; int toInt(void) const; }; std::ostream & operator<<(std::ostream & o, Fixed const & p_Fixed); #endif /* !FIXED_HPP */
36175b7d2e5e5dbc6c650d7f6ae0a077e2adbe6f
4244b9a13ba618f8c47a7b39ffad43fb9498d8c8
/LeetCode/compare-version-numbers.cpp
5b1d4faea8cc9ee24423755311c819cdc1c43e63
[]
no_license
bluesilence/cpp
6cfa96ece419d55e0f422632bb25f480b7c3c75c
ebad4c42d56e05a41ba65800e9ffde3e5752946d
refs/heads/master
2016-09-05T13:21:24.081849
2015-08-03T13:17:31
2015-08-03T13:17:31
19,880,989
0
0
null
null
null
null
UTF-8
C++
false
false
1,016
cpp
class Solution { public: int compareVersion(string version1, string version2) { const int LEN1 = version1.size(); const int LEN2 = version2.size(); int index1 = 0; int index2 = 0; int num1 = 0; int num2 = 0; do { num1 = getNextLevel(version1, index1); num2 = getNextLevel(version2, index2); if (num1 != num2) { return num1 > num2 ? 1 : -1; } } while (index1 <= LEN1 || index2 <= LEN2); return 0; } private: //If the substring is like "..", return 0 as the current level of version int getNextLevel(string s, int &begin) { if (begin >= s.size()) return 0; int result = 0; while (begin < s.size() && s[begin] != '.') { result *= 10; result += s[begin++] - '0'; } begin++; return result; } };
0baf270fe0b780fad3403d43100975d3625e567d
414d720944511da242938e4f3ba0f61db41bd9ef
/point_cloud/package/transform/src/point_to_base_link.cpp
b183067a341ff42e59335b201258f2af3707ed96
[]
no_license
yangdaiyu123/localization
1e4c493dde244f366b64cb3ef708d6a4cd45c549
eea5d5f68145576bea86649bcd56ec55dc5ab9ce
refs/heads/master
2020-05-02T18:09:20.428017
2017-03-25T02:08:34
2017-03-25T02:08:34
null
0
0
null
null
null
null
UTF-8
C++
false
false
5,001
cpp
#include "ros/ros.h" #include "std_msgs/String.h" #include "std_msgs/Float64.h" #include "std_msgs/Int8.h" #include "std_msgs/Int32.h" #include "std_msgs/Int8MultiArray.h" #include "std_msgs/Int32MultiArray.h" #include "std_msgs/Float64MultiArray.h" #include <pcl/common/common_headers.h> #include <pcl/point_types.h> #include <pcl/visualization/cloud_viewer.h> #include <pcl/visualization/pcl_visualizer.h> #include <pcl/io/io.h> #include <pcl/common/transforms.h> #include <pcl_conversions/pcl_conversions.h> #include <pcl/filters/passthrough.h> #include <sensor_msgs/PointCloud2.h> #include <sensor_msgs/point_cloud_conversion.h> #include <sensor_msgs/NavSatFix.h> #include <message_filters/subscriber.h> #include <message_filters/time_synchronizer.h> #include <message_filters/sync_policies/approximate_time.h> using namespace std; using namespace message_filters; pcl::PointCloud<pcl::PointXYZI>::Ptr cloud_front(new pcl::PointCloud<pcl::PointXYZI>); pcl::PointCloud<pcl::PointXYZI>::Ptr cloud_rear(new pcl::PointCloud<pcl::PointXYZI>); pcl::PointCloud<pcl::PointXYZI>::Ptr cloud_top(new pcl::PointCloud<pcl::PointXYZI>); pcl::PointCloud<pcl::PointXYZI>::Ptr cloud_sum(new pcl::PointCloud<pcl::PointXYZI>); ros::Publisher pub_front; ros::Publisher pub_rear; ros::Publisher pub_top; double front_pitch = 0;//degree double rear_roll = 0;//degree double rear_pitch = 0;//degree double rear_yaw = 0;//degree double front_tx = 0; double front_ty = 0; double front_tz = 0; double rear_tx = 0; double rear_ty = 0; double rear_tz = 0; sensor_msgs::NavSatFix gps_msg; void synCurbCallback(const sensor_msgs::PointCloud2::ConstPtr& front_cloud_in, const sensor_msgs::PointCloud2::ConstPtr& rear_cloud_in) { pcl::fromROSMsg(*front_cloud_in, *cloud_front); pcl::fromROSMsg(*rear_cloud_in, *cloud_rear); Eigen::Affine3f transform = Eigen::Affine3f::Identity(); transform.translation() << front_tx, front_ty, front_tz; transform.rotate(Eigen::AngleAxisf(front_pitch*M_PI/180, Eigen::Vector3f::UnitY())); pcl::transformPointCloud(*cloud_front, *cloud_front, transform); Eigen::Affine3f transform2 = Eigen::Affine3f::Identity(); transform2.translation() << rear_tx, rear_ty, rear_tz; transform2.rotate(Eigen::AngleAxisf(M_PI/2.0, Eigen::Vector3f::UnitZ())); transform2.rotate(Eigen::AngleAxisf(rear_pitch*M_PI/180, Eigen::Vector3f::UnitY())); transform2.rotate(Eigen::AngleAxisf(rear_roll*M_PI/180, Eigen::Vector3f::UnitX())); pcl::transformPointCloud(*cloud_rear, *cloud_rear, transform2); *cloud_top = *cloud_front + *cloud_rear; pcl::PassThrough<pcl::PointXYZI> pass; pass.setInputCloud(cloud_front); pass.setFilterFieldName("y"); pass.setFilterLimits(-10,10); pass.filter(*cloud_front); pass.setFilterFieldName("z"); pass.setFilterLimits(-0.5,0.6); pass.filter(*cloud_front); pass.setInputCloud(cloud_rear); pass.setFilterFieldName("y"); pass.setFilterLimits(-10,10); pass.filter(*cloud_rear); pass.setFilterFieldName("z"); pass.setFilterLimits(-0.5,0.6); pass.filter(*cloud_rear); pass.setInputCloud(cloud_top); pass.setFilterFieldName("y"); pass.setFilterLimits(-10,10); pass.filter(*cloud_top); pass.setFilterFieldName("z"); pass.setFilterLimits(4,7); pass.filter(*cloud_top); cloud_front->header.frame_id = "base_link"; cloud_rear->header.frame_id = "base_link"; cloud_top->header.frame_id = "base_link"; sensor_msgs::PointCloud2 cloud_to_pub; pcl::toROSMsg(*cloud_front, cloud_to_pub); pub_front.publish(cloud_to_pub); pcl::toROSMsg(*cloud_rear, cloud_to_pub); pub_rear.publish(cloud_to_pub); pcl::toROSMsg(*cloud_top, cloud_to_pub); pub_top.publish(cloud_to_pub); } int main(int argc, char **argv) { ros::init(argc, argv, "point_to_base_link"); ros::NodeHandle nh; message_filters::Subscriber<sensor_msgs::PointCloud2> sub_rear_cloud(nh, "/rear/velodyne_points", 2); message_filters::Subscriber<sensor_msgs::PointCloud2> sub_front_cloud(nh, "/velodyne_points", 2); typedef sync_policies::ApproximateTime<sensor_msgs::PointCloud2, sensor_msgs::PointCloud2> MySyncPolicy; // ApproximateTime takes a queue size as its constructor argument, hence MySyncPolicy(10) Synchronizer<MySyncPolicy> sync(MySyncPolicy(10), sub_front_cloud, sub_rear_cloud); sync.registerCallback(boost::bind(&synCurbCallback, _1, _2)); pub_front = nh.advertise<sensor_msgs::PointCloud2>("front_points_in_base_link",2); pub_rear = nh.advertise<sensor_msgs::PointCloud2>("rear_points_in_base_link",2); pub_top = nh.advertise<sensor_msgs::PointCloud2>("top_points_in_base_link",2); ros::NodeHandle pnh("~"); pnh.param("front_pitch", front_pitch,14.0); pnh.param("front_tx", front_tx,0.0); pnh.param("front_ty", front_ty,0.0); pnh.param("front_tz", front_tz,1.9); pnh.param("rear_roll", rear_roll,-22.0); pnh.param("rear_pitch", rear_pitch,0.0); pnh.param("rear_yaw", rear_yaw,90.0); pnh.param("rear_tx", rear_tx,-0.8); pnh.param("rear_ty", rear_ty,-0.2); pnh.param("rear_tz", rear_tz,1.7); ros::spin(); return 0; }
b7595e14ad01110feaf02d8ff74d397512b4624b
2edea43bfa775d9ab2343afc9731a720268a3f65
/reflectance-field_individual-project-in-progress/load-directions-and-intensities.cpp
4401780792d4eaaca0b82df14529144c969dab13
[]
no_license
terezadrskova/projects
866e56ef6fe7ddd23c25bf257132fb23f971a422
b9f2ad29cf6e03ce250b23ad286db968b62b7b36
refs/heads/master
2021-01-19T22:33:08.181483
2014-08-06T17:24:22
2014-08-06T17:24:22
null
0
0
null
null
null
null
UTF-8
C++
false
false
2,141
cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include "loadPPM.h" #include <QFileDialog> #include <QMessageBox> #include <QStringList> #include <QDebug> #include <iostream> #include <QPixmap> #include <QString> #include <QtCore/qmath.h> #include <iostream> #include <cmath> #include <cv.h> #include <highgui.h> using namespace cv; using namespace std; /* ---------------------------------------------------------------- * EXTRACT FILE AND CONVERT TO 2D ARRAY *-----------------------------------------------------------------*/ void MainWindow::extractFile(const QString &pathName, bool isDirection) { QFile inputFile(pathName); qDebug() << "Filepath: " << pathName; // Light Direction file couldn't be open if (!inputFile.open(QIODevice::ReadOnly)) { qDebug() << "There has been an error while loading the file!"; return; } // extract file QTextStream in(&inputFile); int i = 0; while (!in.atEnd()) { QString line = in.readLine(); //splitting string into list of strings to seperate each light direction QRegExp rx("(\\ )"); //RegEx for ' ' QStringList directionsList = line.split(rx); // extracking each token an inserting it into 2D array if (isDirection) { lightDirections[i][0] = directionsList.at(1).toFloat(); lightDirections[i][1] = directionsList.at(2).toFloat(); lightDirections[i][2] = directionsList.at(3).toFloat(); if(i==252) { qDebug() << "Light Directions loaded successfully" << endl; } } // change light intensities else { lightIntensities[i][0] = directionsList.at(1).toFloat(); lightIntensities[i][1] = directionsList.at(2).toFloat(); lightIntensities[i][2] = directionsList.at(3).toFloat(); if(i==252) { qDebug() << "Light Intensities loaded successfully"; } } // got ot next line i++; inputFile.close(); } return; }
b9be8c287a54e772130a0f25deac546ff04dd5df
726df6fc883aa216b81f8e0682ec427febdbf89d
/ICMUIInterface/settings.h
3d4148a44b1ec5755620135a6f12c69de355a4c5
[]
no_license
Qmax/QUTE
48ac34384885558ea4f6310f122b2e4f6450853d
09d65671d4280d6cd9b9f97243846708d7968cff
refs/heads/master
2021-01-01T05:33:53.379760
2014-12-04T11:38:05
2014-12-04T11:38:05
null
0
0
null
null
null
null
UTF-8
C++
false
false
3,582
h
/* * Author: Ravivarman.R,Qmax */ #ifndef SETTINGS_H #define SETTINGS_H #include <QWidget> #include <qlineedit.h> #include <qdebug.h> #include <qpluginloader.h> #include <QFile> #include <QXmlStreamReader> #include <QXmlStreamAttributes> #include <QString> #include "InterfacePTLibrary.h" #include "ApplicationCardInterface.h" #include "BackPlaneInterface.h" #include "ad5293.h" #include "ad7190component.h" #include "IPSOCCommunication.h" #include "hardwareinterface.h" namespace Ui { class Settings; } class Settings : public QWidget { Q_OBJECT public: Settings(IPSOCCOMMUNICATION*,HardwareInterface*,IApplicationCardInterface*,AD7190Component*,QWidget *); ~Settings(); protected: IntefaceBackPlane *IBackPlane; ////BackPlaneInterfacePlugin HardwareInterface *hwInterface; IPSOCCOMMUNICATION *IPsoc; QLineEdit *objForceVoltage,*objFrequency; IApplicationCardInterface *IAppCard; IQmaxLineEdit *ILineEdit; IQmaxNumberPanel *INumberPanel; AD7190Component *m_objAD7190Component; AD5293 *m_objAD5293Component; void openNumKBPanel(short int); void openNumKBPanel(short int,double,char,double,double,QWidget*); void changeEvent(QEvent *e); double m_nFrequency,m_nForceVoltage; unsigned short m_nICMMGR; unsigned short m_nR1RMSCAVG,m_nR1RMSGAIN,m_nR1RMSIN,m_nR2RMSCAVG,m_nR2RMSGAIN,m_nR2RMSIN; short int m_nLineEditIndex; unsigned short m_nFTW_LSW,m_nFTW_MSW; double m_nFTWOutputFrequency; double m_nRangeSlope[5],m_nRangeConstant[5]; double m_nAmplitude; //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ unsigned short int m_nAddress,m_nData; unsigned short int m_nBPAddress,m_nBPData; double m_nOffset,m_nDCVoltage; QLineEdit *objAddress,*objData; QLineEdit *objBPAddress,*objBPData; QLineEdit *objOffset; QLineEdit *objDCVoltage; //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ void calculateDDSFrequency(); private: Ui::Settings *ui; private slots: void on_applySrc_clicked(); void on_srcImpBox_10_currentIndexChanged(int index); void on_comboBox_currentIndexChanged(int index); void on_right_clicked(); void on_left_clicked(); void on_down_clicked(); void on_up_clicked(); double validateCursorPosition(short pos,double value,short incDec); void on_WriteAppCard_2_clicked(); void on_ReadAppCard_2_clicked(); void on_adc2_clicked(); void on_adc1_clicked(); void on_WriteAppCard_clicked(); void on_ReadAppCard_clicked(); void on_ExtInt_clicked(bool checked); void on_R2RMSIN_clicked(bool checked); void on_R2RMSGAIN_clicked(bool checked); void on_R2RMSCAVG_clicked(bool checked); void on_R1RMSIN_clicked(bool checked); void on_R1RMSGAIN_clicked(bool checked); void on_R1RMSCAVG_clicked(bool checked); void on_chkDivSel_clicked(bool checked); void on_chkPOTSel_clicked(bool checked); void on_chkCapSet_clicked(bool checked); void on_pushButton_clicked(); void receiveValue(unsigned int); void receiveValue(double); void receiveValue(QString); void callForceVoltageEdit(); void callFrequencyEdit(); //~~~~~~~~~~~~~~~~~~~~~~~~~~ void callAddressEdit(); void callDataEdit(); void callBPAddressEdit(); void callBPDataEdit(); void callOffset(); void callDCVoltage(); //~~~~~~~~~~~~~~~~~~~~~~~~~ signals: void FrequencyChange(double); void SrcImpChange(QString); void OffsetChange(double); void FeedbackChange(int); }; #endif // SETTINGS_H
[ "root@ravivarman.(none)" ]
root@ravivarman.(none)
373a2a25a247a82bed895506461e1d627af967cc
d1c34d724e105086fa7dcae8c8dfe641d6ca480b
/Source/Scripting/SBansheeEngine/Generated/BsScriptTAnimationCurve.generated.cpp
436e05fcb9fcbee03d4d670114e6ef37a47cffab
[]
no_license
ninpl/BansheeEngine
2172d753a09bcf7a2ca5262fbfa7e66be0924370
be1e9d157741e268dabf4161867abef37e4c226e
refs/heads/master
2022-09-30T22:13:44.309994
2018-08-25T11:29:37
2018-08-25T11:29:56
null
0
0
null
null
null
null
UTF-8
C++
false
false
10,475
cpp
#include "BsScriptTAnimationCurve.generated.h" #include "BsMonoMethod.h" #include "BsMonoClass.h" #include "BsMonoUtil.h" #include "BsScriptTKeyframe.generated.h" #include "BsScriptTKeyframe.generated.h" #include "BsScriptTKeyframe.generated.h" #include "Wrappers/BsScriptVector.h" #include "Wrappers/BsScriptQuaternion.h" #include "BsScriptTKeyframe.generated.h" namespace bs { ScriptTAnimationCurvefloat::ScriptTAnimationCurvefloat(MonoObject* managedInstance, const SPtr<TAnimationCurve<float>>& value) :ScriptObject(managedInstance), mInternal(value) { } void ScriptTAnimationCurvefloat::initRuntimeData() { metaData.scriptClass->addInternalCall("Internal_TAnimationCurve", (void*)&ScriptTAnimationCurvefloat::Internal_TAnimationCurve); metaData.scriptClass->addInternalCall("Internal_evaluate", (void*)&ScriptTAnimationCurvefloat::Internal_evaluate); metaData.scriptClass->addInternalCall("Internal_getKeyFrames", (void*)&ScriptTAnimationCurvefloat::Internal_getKeyFrames); } MonoObject* ScriptTAnimationCurvefloat::create(const SPtr<TAnimationCurve<float>>& value) { if(value == nullptr) return nullptr; bool dummy = false; void* ctorParams[1] = { &dummy }; MonoObject* managedInstance = metaData.scriptClass->createInstance("bool", ctorParams); new (bs_alloc<ScriptTAnimationCurvefloat>()) ScriptTAnimationCurvefloat(managedInstance, value); return managedInstance; } void ScriptTAnimationCurvefloat::Internal_TAnimationCurve(MonoObject* managedInstance, MonoArray* keyframes) { Vector<TKeyframe<float>> veckeyframes; if(keyframes != nullptr) { ScriptArray arraykeyframes(keyframes); veckeyframes.resize(arraykeyframes.size()); for(int i = 0; i < (int)arraykeyframes.size(); i++) { veckeyframes[i] = arraykeyframes.get<TKeyframe<float>>(i); } } SPtr<TAnimationCurve<float>> instance = bs_shared_ptr_new<TAnimationCurve<float>>(veckeyframes); new (bs_alloc<ScriptTAnimationCurvefloat>())ScriptTAnimationCurvefloat(managedInstance, instance); } float ScriptTAnimationCurvefloat::Internal_evaluate(ScriptTAnimationCurvefloat* thisPtr, float time, bool loop) { float tmp__output; tmp__output = thisPtr->getInternal()->evaluate(time, loop); float __output; __output = tmp__output; return __output; } MonoArray* ScriptTAnimationCurvefloat::Internal_getKeyFrames(ScriptTAnimationCurvefloat* thisPtr) { Vector<TKeyframe<float>> vec__output; vec__output = thisPtr->getInternal()->getKeyFrames(); MonoArray* __output; int arraySize__output = (int)vec__output.size(); ScriptArray array__output = ScriptArray::create<ScriptTKeyframefloat>(arraySize__output); for(int i = 0; i < arraySize__output; i++) { array__output.set(i, vec__output[i]); } __output = array__output.getInternal(); return __output; } ScriptTAnimationCurveVector3::ScriptTAnimationCurveVector3(MonoObject* managedInstance, const SPtr<TAnimationCurve<Vector3>>& value) :ScriptObject(managedInstance), mInternal(value) { } void ScriptTAnimationCurveVector3::initRuntimeData() { metaData.scriptClass->addInternalCall("Internal_TAnimationCurve", (void*)&ScriptTAnimationCurveVector3::Internal_TAnimationCurve); metaData.scriptClass->addInternalCall("Internal_evaluate", (void*)&ScriptTAnimationCurveVector3::Internal_evaluate); metaData.scriptClass->addInternalCall("Internal_getKeyFrames", (void*)&ScriptTAnimationCurveVector3::Internal_getKeyFrames); } MonoObject* ScriptTAnimationCurveVector3::create(const SPtr<TAnimationCurve<Vector3>>& value) { if(value == nullptr) return nullptr; bool dummy = false; void* ctorParams[1] = { &dummy }; MonoObject* managedInstance = metaData.scriptClass->createInstance("bool", ctorParams); new (bs_alloc<ScriptTAnimationCurveVector3>()) ScriptTAnimationCurveVector3(managedInstance, value); return managedInstance; } void ScriptTAnimationCurveVector3::Internal_TAnimationCurve(MonoObject* managedInstance, MonoArray* keyframes) { Vector<TKeyframe<Vector3>> veckeyframes; if(keyframes != nullptr) { ScriptArray arraykeyframes(keyframes); veckeyframes.resize(arraykeyframes.size()); for(int i = 0; i < (int)arraykeyframes.size(); i++) { veckeyframes[i] = ScriptTKeyframeVector3::fromInterop(arraykeyframes.get<__TKeyframeVector3Interop>(i)); } } SPtr<TAnimationCurve<Vector3>> instance = bs_shared_ptr_new<TAnimationCurve<Vector3>>(veckeyframes); new (bs_alloc<ScriptTAnimationCurveVector3>())ScriptTAnimationCurveVector3(managedInstance, instance); } void ScriptTAnimationCurveVector3::Internal_evaluate(ScriptTAnimationCurveVector3* thisPtr, float time, bool loop, Vector3* __output) { Vector3 tmp__output; tmp__output = thisPtr->getInternal()->evaluate(time, loop); *__output = tmp__output; } MonoArray* ScriptTAnimationCurveVector3::Internal_getKeyFrames(ScriptTAnimationCurveVector3* thisPtr) { Vector<TKeyframe<Vector3>> vec__output; vec__output = thisPtr->getInternal()->getKeyFrames(); MonoArray* __output; int arraySize__output = (int)vec__output.size(); ScriptArray array__output = ScriptArray::create<ScriptTKeyframeVector3>(arraySize__output); for(int i = 0; i < arraySize__output; i++) { array__output.set(i, ScriptTKeyframeVector3::toInterop(vec__output[i])); } __output = array__output.getInternal(); return __output; } ScriptTAnimationCurveQuaternion::ScriptTAnimationCurveQuaternion(MonoObject* managedInstance, const SPtr<TAnimationCurve<Quaternion>>& value) :ScriptObject(managedInstance), mInternal(value) { } void ScriptTAnimationCurveQuaternion::initRuntimeData() { metaData.scriptClass->addInternalCall("Internal_TAnimationCurve", (void*)&ScriptTAnimationCurveQuaternion::Internal_TAnimationCurve); metaData.scriptClass->addInternalCall("Internal_evaluate", (void*)&ScriptTAnimationCurveQuaternion::Internal_evaluate); metaData.scriptClass->addInternalCall("Internal_getKeyFrames", (void*)&ScriptTAnimationCurveQuaternion::Internal_getKeyFrames); } MonoObject* ScriptTAnimationCurveQuaternion::create(const SPtr<TAnimationCurve<Quaternion>>& value) { if(value == nullptr) return nullptr; bool dummy = false; void* ctorParams[1] = { &dummy }; MonoObject* managedInstance = metaData.scriptClass->createInstance("bool", ctorParams); new (bs_alloc<ScriptTAnimationCurveQuaternion>()) ScriptTAnimationCurveQuaternion(managedInstance, value); return managedInstance; } void ScriptTAnimationCurveQuaternion::Internal_TAnimationCurve(MonoObject* managedInstance, MonoArray* keyframes) { Vector<TKeyframe<Quaternion>> veckeyframes; if(keyframes != nullptr) { ScriptArray arraykeyframes(keyframes); veckeyframes.resize(arraykeyframes.size()); for(int i = 0; i < (int)arraykeyframes.size(); i++) { veckeyframes[i] = ScriptTKeyframeQuaternion::fromInterop(arraykeyframes.get<__TKeyframeQuaternionInterop>(i)); } } SPtr<TAnimationCurve<Quaternion>> instance = bs_shared_ptr_new<TAnimationCurve<Quaternion>>(veckeyframes); new (bs_alloc<ScriptTAnimationCurveQuaternion>())ScriptTAnimationCurveQuaternion(managedInstance, instance); } void ScriptTAnimationCurveQuaternion::Internal_evaluate(ScriptTAnimationCurveQuaternion* thisPtr, float time, bool loop, Quaternion* __output) { Quaternion tmp__output; tmp__output = thisPtr->getInternal()->evaluate(time, loop); *__output = tmp__output; } MonoArray* ScriptTAnimationCurveQuaternion::Internal_getKeyFrames(ScriptTAnimationCurveQuaternion* thisPtr) { Vector<TKeyframe<Quaternion>> vec__output; vec__output = thisPtr->getInternal()->getKeyFrames(); MonoArray* __output; int arraySize__output = (int)vec__output.size(); ScriptArray array__output = ScriptArray::create<ScriptTKeyframeQuaternion>(arraySize__output); for(int i = 0; i < arraySize__output; i++) { array__output.set(i, ScriptTKeyframeQuaternion::toInterop(vec__output[i])); } __output = array__output.getInternal(); return __output; } ScriptTAnimationCurveint32_t::ScriptTAnimationCurveint32_t(MonoObject* managedInstance, const SPtr<TAnimationCurve<int32_t>>& value) :ScriptObject(managedInstance), mInternal(value) { } void ScriptTAnimationCurveint32_t::initRuntimeData() { metaData.scriptClass->addInternalCall("Internal_TAnimationCurve", (void*)&ScriptTAnimationCurveint32_t::Internal_TAnimationCurve); metaData.scriptClass->addInternalCall("Internal_evaluate", (void*)&ScriptTAnimationCurveint32_t::Internal_evaluate); metaData.scriptClass->addInternalCall("Internal_getKeyFrames", (void*)&ScriptTAnimationCurveint32_t::Internal_getKeyFrames); } MonoObject* ScriptTAnimationCurveint32_t::create(const SPtr<TAnimationCurve<int32_t>>& value) { if(value == nullptr) return nullptr; bool dummy = false; void* ctorParams[1] = { &dummy }; MonoObject* managedInstance = metaData.scriptClass->createInstance("bool", ctorParams); new (bs_alloc<ScriptTAnimationCurveint32_t>()) ScriptTAnimationCurveint32_t(managedInstance, value); return managedInstance; } void ScriptTAnimationCurveint32_t::Internal_TAnimationCurve(MonoObject* managedInstance, MonoArray* keyframes) { Vector<TKeyframe<int32_t>> veckeyframes; if(keyframes != nullptr) { ScriptArray arraykeyframes(keyframes); veckeyframes.resize(arraykeyframes.size()); for(int i = 0; i < (int)arraykeyframes.size(); i++) { veckeyframes[i] = arraykeyframes.get<TKeyframe<int32_t>>(i); } } SPtr<TAnimationCurve<int32_t>> instance = bs_shared_ptr_new<TAnimationCurve<int32_t>>(veckeyframes); new (bs_alloc<ScriptTAnimationCurveint32_t>())ScriptTAnimationCurveint32_t(managedInstance, instance); } int32_t ScriptTAnimationCurveint32_t::Internal_evaluate(ScriptTAnimationCurveint32_t* thisPtr, float time, bool loop) { int32_t tmp__output; tmp__output = thisPtr->getInternal()->evaluate(time, loop); int32_t __output; __output = tmp__output; return __output; } MonoArray* ScriptTAnimationCurveint32_t::Internal_getKeyFrames(ScriptTAnimationCurveint32_t* thisPtr) { Vector<TKeyframe<int32_t>> vec__output; vec__output = thisPtr->getInternal()->getKeyFrames(); MonoArray* __output; int arraySize__output = (int)vec__output.size(); ScriptArray array__output = ScriptArray::create<ScriptTKeyframeint32_t>(arraySize__output); for(int i = 0; i < arraySize__output; i++) { array__output.set(i, vec__output[i]); } __output = array__output.getInternal(); return __output; } }
8f0cb81ba7135106dd7897c0a3f4a62797103db8
e763b855be527d69fb2e824dfb693d09e59cdacb
/aws-cpp-sdk-pinpoint/source/model/GetImportJobResult.cpp
fb49d74335068a5ef105c5ebb4aff6dfb0b61fda
[ "MIT", "Apache-2.0", "JSON" ]
permissive
34234344543255455465/aws-sdk-cpp
47de2d7bde504273a43c99188b544e497f743850
1d04ff6389a0ca24361523c58671ad0b2cde56f5
refs/heads/master
2023-06-10T16:15:54.618966
2018-05-07T23:32:08
2018-05-07T23:32:08
132,632,360
1
0
Apache-2.0
2023-06-01T23:20:47
2018-05-08T15:56:35
C++
UTF-8
C++
false
false
1,325
cpp
/* * Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * http://aws.amazon.com/apache2.0 * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ #include <aws/pinpoint/model/GetImportJobResult.h> #include <aws/core/utils/json/JsonSerializer.h> #include <aws/core/AmazonWebServiceResult.h> #include <aws/core/utils/StringUtils.h> #include <aws/core/utils/UnreferencedParam.h> #include <utility> using namespace Aws::Pinpoint::Model; using namespace Aws::Utils::Json; using namespace Aws::Utils; using namespace Aws; GetImportJobResult::GetImportJobResult() { } GetImportJobResult::GetImportJobResult(const Aws::AmazonWebServiceResult<JsonValue>& result) { *this = result; } GetImportJobResult& GetImportJobResult::operator =(const Aws::AmazonWebServiceResult<JsonValue>& result) { const JsonValue& jsonValue = result.GetPayload(); m_importJobResponse = jsonValue; return *this; }
9c7a54c0e8956eaf87a058c9c75bf9d790648f78
a3effde3c27c072090f0021bdae3306961eb2d92
/Codeforces/1113/B.cpp
046311900f2e2da1f6733a505a3676fb7ce90636
[]
no_license
anmolgup/Competitive-programming-code
f4837522bf648c90847d971481f830a47722da29
329101c4e45be68192715c9a0718f148e541b58b
refs/heads/master
2022-12-03T19:00:40.261727
2020-08-08T09:21:58
2020-08-08T09:21:58
286,011,774
1
0
null
null
null
null
UTF-8
C++
false
false
4,874
cpp
#include<bits/stdc++.h> using namespace std; //Optimisations #pragma GCC target ("avx2") #pragma GCC optimization ("unroll-loops") #pragma GCC optimize("O2") //shortcuts for functions #define pb push_back #define mp make_pair #define ff first #define ss second #define endl "\n" #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define th(n) cout<<n<<endl #define gc getchar_unlocked #define ms(s, n) memset(s, n, sizeof(s)) #define prec(n) fixed<<setprecision(n) #define n_l '\n' // make it python #define gcd __gcd #define append push_back #define str to_string #define upper(s) transform(s.begin(),s.end(),s.begin(),::toupper) #define lower(s) transform(s.begin(),s.end(),s.begin(),::tolower) #define print(arr) for(auto el: arr) cout<<el<<" ";cout<<endl // utility functions shortcuts #define max3(a,b,c) max(a,max(b,c)) #define min3(a,b,c) min(a,min(b,c)) #define sswap(a,b) {a=a^b;b=a^b;a=a^b;} #define swap(a,b) {auto temp=a; a=b; b=temp;} #define init(dp) memset(dp,-1,sizeof(dp)); #define set0(dp) memset(dp,0,sizeof(dp)); #define bits(x) __builtin_popcount(x) #define SORT(v) sort(all(v)) #define endl "\n" #define forr(i,n) for(ll i=0;i<n;i++) #define formatrix(i,n) for(ll i=0;i<n;i++, cout<<"\n") #define eof (scanf("%d" ,&n))!=EOF // declaration shortcuts #define vll vector<ll> #define vvl vector<vector<ll>> #define vpll vector<pair<ll,ll> > #define pll pair<ll,ll> #define ppl pair<ll,pp> #define ull unsigned long long #define ll long long #define mll map< ll, ll > #define sll set< ll > #define uni(v) v.erase(unique(v.begin(),v.end()),v.end()); #define ini(a, v) memset(a, v, sizeof(a)) // Constants constexpr int dx[] = { -1, 0, 1, 0, 1, 1, -1, -1}; constexpr int dy[] = {0, -1, 0, 1, 1, -1, 1, -1}; constexpr ll INF = 1999999999999999997; constexpr int inf = INT_MAX; constexpr int MAXSIZE = int(1e6) + 5; constexpr auto PI = 3.14159265358979323846L; constexpr auto oo = numeric_limits<int>::max() / 2 - 2; constexpr auto eps = 1e-6; constexpr auto mod = 1000000007; constexpr auto MOD = 1000000007; constexpr auto MOD9 = 1000000009; constexpr auto maxn = 100006; // Debugging // For reference: https://codeforces.com/blog/entry/65311 #define dbg(...) cout << "[" << #__VA_ARGS__ << "]: "; cout << to_string(__VA_ARGS__) << endl template <typename T, size_t N> int SIZE(const T (&t)[N]) { return N; } template<typename T> int SIZE(const T &t) { return t.size(); } string to_string(string s, int x1 = 0, int x2 = 1e9) { return '"' + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) : "") + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(char c) { return string({c}); } template<size_t N> string to_string(bitset<N> &b, int x1 = 0, int x2 = 1e9) { string t = ""; for (int __iii__ = min(x1, SIZE(b)), __jjj__ = min(x2, SIZE(b) - 1); __iii__ <= __jjj__; ++__iii__) { t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(A (&v), int x1 = 0, int x2 = 1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(A (&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if (l_v_l_v_l == 0) res += n_l; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2 - x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if (e != l) { if (rnk > 1) { res += n_l; t_a_b_s = l_v_l_v_l; }; } else { t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if (l_v_l_v_l == 0) res += n_l; return res; } void dbgs() {;} template<typename Heads, typename... Tails> void dbgs(Heads H, Tails... T) { cout << to_string(H) << " | "; dbgs(T...); } #define dbgm(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgs(__VA_ARGS__); cout << endl; #define n_l '\n' int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll n, sum = 0, x; cin >> n; mll cnt; forr(i, n) { cin >> x; sum += x; cnt[x]++; } ll mn = (*cnt.begin()).first; ll res = sum; for (auto it = cnt.begin(); it != cnt.end(); it++) { ll num = it -> first; for (auto i = 2; i * i <= num; i++) { if (num % i == 0) { ll currsum = sum; currsum = currsum - num + num/i; currsum += mn*i; currsum -= mn; res = min(res,currsum); } } } th(res); }
4c28cf394878c6e3c3d7c349bcf4a6a3ef7e58b7
b62b1897dac1a52f5aff86108c4e4451b3d608c7
/src/transmitter_test/transmitter_test.ino
72a906c8e9d9f58606d854bb7ec75f7a2c964565
[]
no_license
jmanuel1/epics-dendrometer
4ad4f6ab9da079144dc89ca9b56c5addddbbcfe5
4e82226a58c999e3d90f25738285016a2c8d94ff
refs/heads/master
2020-09-11T20:13:30.291800
2020-03-20T06:57:10
2020-03-20T06:57:10
222,178,604
0
0
null
null
null
null
UTF-8
C++
false
false
409
ino
#include <VirtualWire.h> void setup() { // default transmit pin is 12 vw_set_tx_pin(12); vw_set_ptt_inverted(true); vw_setup(2000); pinMode(LED_BUILTIN, OUTPUT); } void loop() { const char *message = "hello there!"; // 12 chars // light up digitalWrite(LED_BUILTIN, HIGH); vw_send((uint8_t *) message, strlen(message)); vw_wait_tx(); digitalWrite(LED_BUILTIN, LOW); delay(1000); }
a2e358188fb9a43694870c6354c3a26bca8c80d4
444f9026826d938fc7c23af55d2a3320424213a3
/CombineTools/src/CombineHarvester.cc
082570834c1d5466223fc4f5356d61c0aac96d14
[]
no_license
rcl11/CombineHarvester
833c2f9f870f2318b5e2f7c1d674dc3f332d79d0
b2693dc8d11743c071d169786415cbf0841b87c7
refs/heads/master
2021-01-21T07:39:09.411730
2015-10-04T10:45:37
2015-10-04T10:45:37
46,294,595
0
0
null
2015-11-16T18:43:50
2015-11-16T18:43:49
null
UTF-8
C++
false
false
26,421
cc
#include "CombineHarvester/CombineTools/interface/CombineHarvester.h" #include <iostream> #include <map> #include <string> #include <vector> #include <list> #include "CombineHarvester/CombineTools/interface/Observation.h" #include "CombineHarvester/CombineTools/interface/Process.h" #include "CombineHarvester/CombineTools/interface/Systematic.h" #include "CombineHarvester/CombineTools/interface/Parameter.h" #include "CombineHarvester/CombineTools/interface/Logging.h" #include "CombineHarvester/CombineTools/interface/TFileIO.h" namespace ch { CombineHarvester::CombineHarvester() : verbosity_(0), log_(&(std::cout)) { // if (verbosity_ >= 3) { // log() << "[CombineHarvester] Constructor called: " << this << "\n"; // } flags_["zero-negative-bins-on-import"] = true; flags_["allow-missing-shapes"] = true; // std::cout << "[CombineHarvester] Constructor called for " << this << "\n"; } CombineHarvester::~CombineHarvester() { // std::cout << "[CombineHarvester] Destructor called for " << this << "\n"; } void swap(CombineHarvester& first, CombineHarvester& second) { using std::swap; // std::cout << "[CombineHarvester] Swap " << &first << " <-> " // << &second << "\n"; swap(first.obs_, second.obs_); swap(first.procs_, second.procs_); swap(first.systs_, second.systs_); swap(first.params_, second.params_); swap(first.wspaces_, second.wspaces_); swap(first.verbosity_, second.verbosity_); swap(first.flags_, second.flags_); swap(first.log_, second.log_); } CombineHarvester::CombineHarvester(CombineHarvester const& other) : obs_(other.obs_), procs_(other.procs_), systs_(other.systs_), params_(other.params_), wspaces_(other.wspaces_), flags_(other.flags_), verbosity_(other.verbosity_), log_(other.log_) { // std::cout << "[CombineHarvester] Copy-constructor called " << &other // << " -> " << this << "\n"; } CombineHarvester CombineHarvester::deep() { CombineHarvester cpy; // std::cout << "[CombineHarvester] Deep copy called " << this // << " -> " << &cpy << "\n"; cpy.obs_.resize(obs_.size()); cpy.procs_.resize(procs_.size()); cpy.systs_.resize(systs_.size()); cpy.flags_ = flags_; cpy.verbosity_ = verbosity_; cpy.log_ = log_; // Build a map of workspace object pointers std::map<RooAbsData const*, RooAbsData *> dat_map; std::map<RooAbsPdf const*, RooAbsPdf *> pdf_map; std::map<RooRealVar const*, RooRealVar *> var_map; std::map<RooAbsReal const*, RooAbsReal *> fun_map; // Loop through each workspace and make a full copy for (auto const& it : wspaces_) { std::string ws_name = it.first; RooWorkspace const& o_wsp = *(it.second.get()); cpy.wspaces_.insert({ws_name, std::make_shared<RooWorkspace>(o_wsp)}); RooWorkspace & n_wsp = *(cpy.wspaces_.at(ws_name).get()); std::list<RooAbsData *> o_dat = o_wsp.allData(); RooArgSet const& o_pdf = o_wsp.allPdfs(); RooArgSet const& o_var = o_wsp.allVars(); RooArgSet const& o_fun = o_wsp.allFunctions(); std::list<RooAbsData *> n_dat = n_wsp.allData(); RooArgSet const& n_pdf = n_wsp.allPdfs(); RooArgSet const& n_var = n_wsp.allVars(); RooArgSet const& n_fun = n_wsp.allFunctions(); auto o_dat_it = o_dat.begin(); auto n_dat_it = n_dat.begin(); for (; o_dat_it != o_dat.end(); ++o_dat_it, ++n_dat_it) { dat_map[*o_dat_it] = *n_dat_it; } auto o_pdf_it = o_pdf.createIterator(); auto n_pdf_it = n_pdf.createIterator(); do { RooAbsPdf *o_pdf_ptr = static_cast<RooAbsPdf*>(**o_pdf_it); RooAbsPdf *n_pdf_ptr = static_cast<RooAbsPdf*>(**n_pdf_it); if (o_pdf_ptr && n_pdf_ptr) pdf_map[o_pdf_ptr] = n_pdf_ptr; n_pdf_it->Next(); } while (o_pdf_it->Next()); auto o_var_it = o_var.createIterator(); auto n_var_it = n_var.createIterator(); do { RooRealVar *o_var_ptr = static_cast<RooRealVar*>(**o_var_it); RooRealVar *n_var_ptr = static_cast<RooRealVar*>(**n_var_it); if (o_var_ptr && n_var_ptr) var_map[o_var_ptr] = n_var_ptr; n_var_it->Next(); } while (o_var_it->Next()); auto o_fun_it = o_fun.createIterator(); auto n_fun_it = n_fun.createIterator(); do { RooAbsReal *o_fun_ptr = static_cast<RooAbsReal*>(**o_fun_it); RooAbsReal *n_fun_ptr = static_cast<RooAbsReal*>(**n_fun_it); if (o_fun_ptr && n_fun_ptr) fun_map[o_fun_ptr] = n_fun_ptr; n_fun_it->Next(); } while (o_fun_it->Next()); } for (std::size_t i = 0; i < cpy.obs_.size(); ++i) { if (obs_[i]) { cpy.obs_[i] = std::make_shared<Observation>(*(obs_[i])); if (obs_[i]->data()) cpy.obs_[i]->set_data(dat_map.at(obs_[i]->data())); } } for (std::size_t i = 0; i < cpy.procs_.size(); ++i) { if (procs_[i]) { cpy.procs_[i] = std::make_shared<Process>(*(procs_[i])); if (procs_[i]->pdf()) cpy.procs_[i]->set_pdf(pdf_map.at(procs_[i]->pdf())); if (procs_[i]->data()) cpy.procs_[i]->set_data(dat_map.at(procs_[i]->data())); if (procs_[i]->norm()) cpy.procs_[i]->set_norm(fun_map.at(procs_[i]->norm())); } } for (std::size_t i = 0; i < cpy.systs_.size(); ++i) { if (systs_[i]) { cpy.systs_[i] = std::make_shared<Systematic>(*(systs_[i])); if (systs_[i]->data_u() || systs_[i]->data_d()) { cpy.systs_[i]->set_data( static_cast<RooDataHist*>(dat_map.at(systs_[i]->data_u())), static_cast<RooDataHist*>(dat_map.at(systs_[i]->data_d())), nullptr); } } } for (auto const& it : params_) { if (it.second) { cpy.params_.insert({it.first, std::make_shared<Parameter>(*(it.second))}); } else { params_.insert({it.first, nullptr}); } } for (auto it : cpy.params_) { for (unsigned i = 0; i < it.second->vars().size(); ++i) { it.second->vars()[i] = var_map.at(it.second->vars()[i]); } } return cpy; } CombineHarvester::CombineHarvester(CombineHarvester&& other) { // std::cout << "[CombineHarvester] Move-constructor called " << // &other << " -> " << this << "\n"; swap(*this, other); } CombineHarvester& CombineHarvester::operator=(CombineHarvester other) { // std::cout << "[CombineHarvester] Assignment operator called " << &other << // " -> " << this << "\n"; swap(*this, other); return (*this); } CombineHarvester CombineHarvester::cp() { // std::cout << "[CombineHarvester] Shallow copy method cp() called from " << // this << "\n"; return CombineHarvester(*this); } CombineHarvester & CombineHarvester::PrintAll() { return PrintObs().PrintProcs().PrintSysts().PrintParams(); } CombineHarvester & CombineHarvester::PrintObs() { std::cout << Observation::PrintHeader; for (unsigned i = 0; i < obs_.size(); ++i) std::cout << *(obs_[i]) << "\n"; return *this; } CombineHarvester & CombineHarvester::PrintProcs() { std::cout << Process::PrintHeader; for (unsigned i = 0; i < procs_.size(); ++i) std::cout << *(procs_[i]) << "\n"; return *this; } CombineHarvester & CombineHarvester::PrintSysts() { std::cout << Systematic::PrintHeader; for (unsigned i = 0; i < systs_.size(); ++i) std::cout << *(systs_[i]) << "\n"; return *this; } CombineHarvester & CombineHarvester::PrintParams() { std::cout << Parameter::PrintHeader; for (auto const& it : params_) std::cout << *(it.second) << "\n"; return *this; } /** * \brief Resolve a HistMapping object for the given Observation and load the *TH1 or RooAbsData from the TFile and naming pattern this HistMapping provides * * \pre * 1. Input Observation must not already store a shape (TH1 or RooAbsData) * 2. It must be possible to resolve this input to a single HistMapping * object. See the documentation of CombineHarvester::ResolveMapping for * further details. * 3. The resolved HistMapping object must provide a valid TFile. This file * must contain the TH1 or RooAbsData object as directed by the pattern in * the HistMapping after the substitution of the Observation properties. * * **If any of these conditions is unmet an exception will be thrown *and the calling CombineHarvester instance will remain unmodified.** * * \post * 1. The Observation object will contain either a TH1 or a pointer to a * RooAbsData object. * 2. If a TH1 is loaded, the Observation rate will be set to the Integral of * the histogram, discarding any existing value. */ void CombineHarvester::LoadShapes(Observation* entry, std::vector<HistMapping> const& mappings) { // Pre-condition #1 if (entry->shape() || entry->data()) { throw std::runtime_error(FNERROR("Observation already contains a shape")); } if (verbosity_ >= 2) { LOGLINE(log(), "Extracting shapes for Observation:"); log() << Observation::PrintHeader << *entry << "\n"; LOGLINE(log(), "Mappings:"); for (HistMapping const& m : mappings) log() << m << "\n"; } // Pre-condition #2 // ResolveMapping will throw if this fails HistMapping mapping = ResolveMapping(entry->process(), entry->bin(), mappings); boost::replace_all(mapping.pattern, "$CHANNEL", entry->bin()); boost::replace_all(mapping.pattern, "$BIN", entry->bin()); boost::replace_all(mapping.pattern, "$PROCESS", entry->process()); boost::replace_all(mapping.pattern, "$MASS", entry->mass()); if (verbosity_ >= 2) { LOGLINE(log(), "Resolved Mapping:"); log() << mapping << "\n"; } if (mapping.IsHist()) { if (verbosity_ >= 2) LOGLINE(log(), "Mapping type in TH1"); // Pre-condition #3 // GetClonedTH1 will throw if this fails std::unique_ptr<TH1> h = GetClonedTH1(mapping.file.get(), mapping.pattern); // Post-conditions #1 and #2 entry->set_shape(std::move(h), true); } else if (mapping.IsData()) { if (verbosity_ >= 2) LOGLINE(log(), "Mapping type is RooAbsData"); // Pre-condition #3 // SetupWorkspace will throw if workspace not found RooAbsData* dat = mapping.ws->data(mapping.WorkspaceObj().c_str()); if (!dat) { throw std::runtime_error(FNERROR("RooAbsData not found in workspace")); } // Post-condition #1 entry->set_data(dat); entry->set_rate(dat->sumEntries()); } } /** * \brief Resolve a HistMapping object for the given Process and load the TH1 or *RooAbsPdf from the TFile and naming pattern this HistMapping provides * * \pre * 1. Input Process must not already store a shape (TH1 or RooAbsPdf) or * normalisation (RooAbsReal) object * 2. It must be possible to resolve this input to a single HistMapping * object. See the documentation of CombineHarvester::ResolveMapping for * further details. * 3. The resolved HistMapping object must provide a valid TFile. This file * must contain the TH1 or RooAbsPdf object as directed by the pattern in * the HistMapping after the substitution of the Process properties. * * **If any of these conditions is unmet an exception will be thrown *and the calling CombineHarvester instance will remain unmodified.** * * \post * 1. The Process object will contain either a TH1 or a pointer to a RooAbsPdf * object. * 2. If a TH1 is loaded, the Process rate will be set to the Integral of the * histogram, discarding any existing value. * 3. It will also contain a pointer to a RooAbsReal object for the * normalisation, but only when the mapping resolved to a RooAbsPdf and a * RooAbsReal object can be found by appending the suffix "_norm" to the * same pattern. * 4. If a RooAbsPdf is loaded, any dependent parameters will be added to the * CombineHarvester instance. */ void CombineHarvester::LoadShapes(Process* entry, std::vector<HistMapping> const& mappings) { // Pre-condition #1 if (entry->shape() || entry->pdf()) { throw std::runtime_error(FNERROR("Process already contains a shape")); } if (verbosity_ >= 2) { LOGLINE(log(), "Extracting shapes for Process:"); log() << Process::PrintHeader << *entry << "\n"; LOGLINE(log(), "Mappings:"); for (HistMapping const& m : mappings) log() << m << "\n"; } // Pre-condition #2 // ResolveMapping will throw if this fails HistMapping mapping = ResolveMapping(entry->process(), entry->bin(), mappings); boost::replace_all(mapping.pattern, "$CHANNEL", entry->bin()); boost::replace_all(mapping.pattern, "$BIN", entry->bin()); boost::replace_all(mapping.pattern, "$PROCESS", entry->process()); boost::replace_all(mapping.pattern, "$MASS", entry->mass()); if (verbosity_ >= 2) { LOGLINE(log(), "Resolved Mapping:"); log() << mapping << "\n"; } if (mapping.IsHist()) { if (verbosity_ >= 2) LOGLINE(log(), "Mapping type in TH1"); // Pre-condition #3 // GetClonedTH1 will throw if this fails std::unique_ptr<TH1> h = GetClonedTH1(mapping.file.get(), mapping.pattern); if (flags_.at("zero-negative-bins-on-import")) { if (HasNegativeBins(h.get())) { LOGLINE(log(), "Warning: process shape has negative bins"); log() << Process::PrintHeader << *entry << "\n"; // ZeroNegativeBins(h.get()); } } // Post-conditions #1 and #2 entry->set_shape(std::move(h), true); } else if (mapping.IsPdf()) { if (verbosity_ >= 2) LOGLINE(log(), "Mapping type is RooAbsPdf/RooAbsData"); // Pre-condition #3 // Try and get this as RooAbsData first. If this doesn't work try pdf RooAbsData* data = mapping.ws->data(mapping.WorkspaceObj().c_str()); RooAbsPdf* pdf = mapping.ws->pdf(mapping.WorkspaceObj().c_str()); if (data) { if (verbosity_ >= 2) { data->printStream(log(), data->defaultPrintContents(0), data->defaultPrintStyle(0), "[LoadShapes] "); } entry->set_data(data); entry->set_rate(data->sumEntries()); } else if (pdf) { if (verbosity_ >= 2) { pdf->printStream(log(), pdf->defaultPrintContents(0), pdf->defaultPrintStyle(0), "[LoadShapes] "); } // Post-condition #1 entry->set_pdf(pdf); } else { // Pre-condition #3 if (flags_.at("allow-missing-shapes")) { LOGLINE(log(), "Warning, shape missing:"); log() << Process::PrintHeader << *entry << "\n"; } else { throw std::runtime_error(FNERROR("RooAbsPdf not found in workspace")); } } HistMapping norm_mapping = mapping; if (!data && norm_mapping.syst_pattern != "") { // For when we're not parsing a datacard, syst_pattern is being using to // note a different mapping for the normalisation term norm_mapping.pattern = norm_mapping.syst_pattern; boost::replace_all(norm_mapping.pattern, "$CHANNEL", entry->bin()); boost::replace_all(norm_mapping.pattern, "$BIN", entry->bin()); boost::replace_all(norm_mapping.pattern, "$PROCESS", entry->process()); boost::replace_all(norm_mapping.pattern, "$MASS", entry->mass()); } else { norm_mapping.pattern += "_norm"; } RooAbsReal* norm = norm_mapping.ws->function(norm_mapping.WorkspaceObj().c_str()); if (norm) { // Post-condition #3 entry->set_norm(norm); if (verbosity_ >= 2) { LOGLINE(log(), "Normalisation RooAbsReal found"); norm->printStream(log(), norm->defaultPrintContents(0), norm->defaultPrintStyle(0), "[LoadShapes] "); } // If we can upcast norm to a RooRealVar then we can interpret // it as a free parameter that should be added to the list RooRealVar* norm_var = dynamic_cast<RooRealVar*>(norm); if (norm_var) { RooArgSet tmp_set(*norm_var); ImportParameters(&tmp_set); } } // Post-condition #4 // Import any paramters of the RooAbsPdf and the RooRealVar RooAbsData const* data_obj = FindMatchingData(entry); if (data_obj) { if (verbosity_ >= 2) LOGLINE(log(), "Matching RooAbsData has been found"); if (pdf) { RooArgSet argset = ParametersByName(pdf, data_obj->get()); ImportParameters(&argset); } if (norm) { RooArgSet argset = ParametersByName(norm, data_obj->get()); ImportParameters(&argset); } } else { if (verbosity_ >= 2) LOGLINE(log(), "No RooAbsData found, assume observable CMS_th1x"); RooRealVar mx("CMS_th1x" , "CMS_th1x", 0, 1); RooArgSet tmp_set(mx); if (pdf) { RooArgSet argset = ParametersByName(pdf, &tmp_set); ImportParameters(&argset); } if (norm) { RooArgSet argset = ParametersByName(norm, &tmp_set); ImportParameters(&argset); } } } } void CombineHarvester::LoadShapes(Systematic* entry, std::vector<HistMapping> const& mappings) { if (entry->shape_u() || entry->shape_d() || entry->data_u() || entry->data_d()) { throw std::runtime_error(FNERROR("Systematic already contains a shape")); } if (verbosity_ >= 2) { LOGLINE(log(), "Extracting shapes for Systematic:"); log() << Systematic::PrintHeader << *entry << "\n"; LOGLINE(log(), "Mappings:"); for (HistMapping const& m : mappings) log() << m << "\n"; } // Pre-condition #2 // ResolveMapping will throw if this fails HistMapping mapping = ResolveMapping(entry->process(), entry->bin(), mappings); boost::replace_all(mapping.pattern, "$CHANNEL", entry->bin()); boost::replace_all(mapping.pattern, "$BIN", entry->bin()); boost::replace_all(mapping.pattern, "$PROCESS", entry->process()); boost::replace_all(mapping.pattern, "$MASS", entry->mass()); std::string p_s = mapping.IsPdf() ? mapping.SystWorkspaceObj() : mapping.syst_pattern; boost::replace_all(p_s, "$CHANNEL", entry->bin()); boost::replace_all(p_s, "$BIN", entry->bin()); boost::replace_all(p_s, "$PROCESS", entry->process()); boost::replace_all(p_s, "$MASS", entry->mass()); std::string p_s_hi = p_s; std::string p_s_lo = p_s; boost::replace_all(p_s_hi, "$SYSTEMATIC", entry->name() + "Up"); boost::replace_all(p_s_lo, "$SYSTEMATIC", entry->name() + "Down"); if (mapping.IsHist()) { if (verbosity_ >= 2) LOGLINE(log(), "Mapping type is TH1"); std::unique_ptr<TH1> h = GetClonedTH1(mapping.file.get(), mapping.pattern); std::unique_ptr<TH1> h_u = GetClonedTH1(mapping.file.get(), p_s_hi); std::unique_ptr<TH1> h_d = GetClonedTH1(mapping.file.get(), p_s_lo); if (flags_.at("zero-negative-bins-on-import")) { if (HasNegativeBins(h.get())) { LOGLINE(log(), "Warning: Systematic shape has negative bins"); log() << Systematic::PrintHeader << *entry << "\n"; // ZeroNegativeBins(h.get()); } if (HasNegativeBins(h_u.get())) { LOGLINE(log(), "Warning: Systematic shape_u has negative bins"); log() << Systematic::PrintHeader << *entry << "\n"; // ZeroNegativeBins(h_u.get()); } if (HasNegativeBins(h_d.get())) { LOGLINE(log(), "Warning: Systematic shape_d has negative bins"); log() << Systematic::PrintHeader << *entry << "\n"; // ZeroNegativeBins(h_d.get()); } } entry->set_shapes(std::move(h_u), std::move(h_d), h.get()); } else if (mapping.IsPdf()) { if (verbosity_ >= 2) LOGLINE(log(), "Mapping type is RooDataHist"); // Try and get this as RooAbsData first. If this doesn't work try pdf RooDataHist* h = dynamic_cast<RooDataHist*>(mapping.sys_ws->data(mapping.WorkspaceObj().c_str())); RooDataHist* h_u = dynamic_cast<RooDataHist*>(mapping.sys_ws->data(p_s_hi.c_str())); RooDataHist* h_d = dynamic_cast<RooDataHist*>(mapping.sys_ws->data(p_s_lo.c_str())); if (!h || !h_u || !h_d) { if (flags_.at("allow-missing-shapes")) { LOGLINE(log(), "Warning, shape missing:"); log() << Systematic::PrintHeader << *entry << "\n"; } else { throw std::runtime_error( FNERROR("All shapes must be of type RooDataHist")); } } else { entry->set_data(h_u, h_d, h); } } else { throw std::runtime_error( FNERROR("Resolved mapping is not of TH1 / RooAbsData type")); } } /** * Determines the best-matched HistMapping for a given process * * Given a process `p` and and bin `b`, this function will first call * GenerateShapeMapAttempts to obtain a list of process,bin pairs to search * for in the given vector of HistMapping object. The fist attempt will be for * a mapping that matches `p,b` exactly, followed by the substitution of each * with the wildcard `*`, making a total of four attempts: `p,b`, `*,b`, * `p,*`, `*,*` * * \pre One of the generated mapping attempts must match exactly with one of * the supplied HistMapping objects, otherwise an exception will be thrown. * * @param process Process name to match * @param bin Bin name to match * @param mappings Vector of HistMapping objects * * @return A reference to the selected HistMapping object */ HistMapping const& CombineHarvester::ResolveMapping( std::string const& process, std::string const& bin, std::vector<HistMapping> const& mappings) { StrPairVec attempts = GenerateShapeMapAttempts(process, bin); for (unsigned a = 0; a < attempts.size(); ++a) { for (unsigned m = 0; m < mappings.size(); ++m) { if ((attempts[a].first == mappings[m].process) && (attempts[a].second == mappings[m].category)) { return mappings[m]; } } } // If we get this far then we didn't find a valid mapping throw std::runtime_error(FNERROR("Valid mapping not found")); } CombineHarvester::StrPairVec CombineHarvester::GenerateShapeMapAttempts( std::string process, std::string category) { CombineHarvester::StrPairVec result; result.push_back(std::make_pair(process , category)); result.push_back(std::make_pair("*" , category)); result.push_back(std::make_pair(process , "*")); result.push_back(std::make_pair("*" , "*")); return result; } std::shared_ptr<RooWorkspace> CombineHarvester::SetupWorkspace( RooWorkspace const& ws, bool can_rename) { bool name_in_use = false; // 1) Does the UUID of this ws match any other ws in the map? for (auto const& it : wspaces_) { // - Yes: just return a ptr to the matched ws if (it.second->uuid() == ws.uuid()) { FNLOGC(log(), verbosity_ >= 1) << "Workspace with name " << it.second->GetName() << " has the same UUID, will use this one\n"; return it.second; } if (!name_in_use && strcmp(it.second->GetName(), ws.GetName()) == 0) { name_in_use = true; } } // 2) No: Ok will clone it in. Is the ws name already in use? if (!name_in_use) { // - No: clone with same name and return // IMPORTANT: Don't used RooWorkspace::Clone(), it seems to introduce // bugs // wspaces_[std::string(ws.GetName())] = std::shared_ptr<RooWorkspace>( // reinterpret_cast<RooWorkspace*>(ws.Clone())); wspaces_[std::string(ws.GetName())] = std::make_shared<RooWorkspace>(RooWorkspace(ws)); return wspaces_.at(ws.GetName()); } // 3) Am I allowed to rename (default no)? if (!can_rename) { // - No: throw runtime error throw std::runtime_error(FNERROR("A different workspace with name " + std::string(ws.GetName()) + " already exists")); } // 4) Yes: determine first available nameX, clone, return std::set<int> used_ints = {0}; std::string src_name(ws.GetName()); for (auto const& it : wspaces_) { std::string test_name(it.second->GetName()); if (test_name.find(src_name) == 0) { std::string postfix = test_name.substr(src_name.size()); try { int number = boost::lexical_cast<int>(postfix); used_ints.insert(number); } catch (boost::bad_lexical_cast & e) { } } } std::string new_name = src_name + boost::lexical_cast<std::string>(*(used_ints.rbegin()) + 1); FNLOGC(log(), verbosity_ >= 1) << "Workspace with name " << src_name << " already defined, renaming to " << new_name << "\n"; // wspaces_[new_name] = std::shared_ptr<RooWorkspace>( // reinterpret_cast<RooWorkspace*>(ws.Clone(new_name.c_str()))); std::shared_ptr<RooWorkspace> new_wsp = std::make_shared<RooWorkspace>(RooWorkspace(ws)); new_wsp->SetName(new_name.c_str()); wspaces_[new_name] = new_wsp; return wspaces_.at(new_name); } void CombineHarvester::ImportParameters(RooArgSet *vars) { auto x = vars->createIterator(); do { RooRealVar *y = dynamic_cast<RooRealVar*>(**x); if (y) { if (!params_.count(y->GetName())) { if (verbosity_ >= 1) { log() << "[ImportParameters] Creating parameter from RooRealVar:\n"; y->printStream(log(), y->defaultPrintContents(0), y->defaultPrintStyle(0), "[ImportParameters] "); } Parameter par; par.set_name(y->GetName()); par.set_val(y->getVal()); par.set_err_d(0.0); par.set_err_u(0.0); params_[y->GetName()] = std::make_shared<Parameter>(par); } else { if (verbosity_ >= 1) log() << "[ImportParameters] Parameter \"" << y->GetName() << "\" already exists\n"; } Parameter *param = params_[y->GetName()].get(); std::vector<RooRealVar *> & p_vars = param->vars(); if (std::find(p_vars.begin(), p_vars.end(), y) == p_vars.end()) { p_vars.push_back(y); if (verbosity_ >= 1) log() << "[ImportParameters] Parameter now stores " << p_vars.size() << " link(s) to RooRealVar objects\n"; } else { if (verbosity_ >= 1) log() << "[ImportParameters] Parameter already stores link to " "RooRealVar object\n"; } } } while (x->Next()); } RooAbsData const* CombineHarvester::FindMatchingData(Process const* proc) { RooAbsData const* data_obj = nullptr; for (unsigned i = 0; i < obs_.size(); ++i) { if (proc->bin() == obs_[i]->bin() && proc->bin_id() == obs_[i]->bin_id()) { data_obj = obs_[i]->data(); } } return data_obj; } }
48e1c61e01912cabc5b3fb980dd81f10a4e1c0f7
181319c35d03cf612364be7b9f14defa01de2a09
/src/erase-remove-idiom.cpp
dffe5ef5e7834fc64b0438eec4db78d77c88f712
[]
no_license
sid842/cpp-stl
3039c7358d12c7c7c133779570ac4e5986aaa6b3
66bb8a84c563f6df18e68d5d04d7d71154fe8383
refs/heads/master
2021-09-03T14:28:07.982434
2021-08-12T19:05:37
2021-08-12T19:05:37
212,173,949
0
1
null
2021-05-31T12:42:02
2019-10-01T18:47:27
C++
UTF-8
C++
false
false
853
cpp
#include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { vector<int> v = {11, 22, 33, 44, 55}; //removing elements from vector //bad strategy - loop over elements and remove those which satisfy the condition //this is bad because it is O(n^2) //good strategy - use std::remove() and vector.erase() algorithm which works //by doing a linear scan and copying the elements to the front of the list and then //resizing the vector. //erase-remove idiom for(auto itr = v.begin(); itr != v.end(); itr++) cout << *itr << " "; cout << endl; //remove_if returns the itr to the end of the valid array after elements removed. v.erase(remove_if(v.begin(), v.end(), [] (int e) { return e%2 == 1; }), v.end()); for(auto itr = v.begin(); itr != v.end(); itr++) cout << *itr << " "; cout << "\n"; }
f924685de3f404e6646b913196459f963a1640ed
cec481abc64972eec4750259dea52ac53f613dc1
/Functions/Problems/Problem2.cpp
fc4aa33dd6c43fdb3933de293a94889e32b19bfa
[]
no_license
sejalrastogi/GFG_CPlusPlus
653a2722dc880d28cef69c9e4140960dbe8e3892
f657980d7802106c337a4526568c3cccde89bfa8
refs/heads/master
2023-03-13T00:22:22.375943
2020-10-17T15:14:43
2020-10-17T15:14:43
301,520,077
0
0
null
null
null
null
UTF-8
C++
false
false
428
cpp
#include <iostream> using namespace std; void fun() { static int x = 1; //Static variable stays there even if the function call is over. Here, x=1 value is used only when it is called the first time i.e., it only initializes the value once. Another time, it will use the previous value and will update the previous value. x++; cout << x << " "; } int main() { fun(); fun(); fun(); return 0; } //2 3 4
d05ba8a06000e53042a10b4c691778c0d54f29af
ae84db67e168d4b133efc4472359818edfd3a51f
/linkedLists/LinkedList/LinkedListExercises.h
24d6a17924db37b11fa82cdca8f75c7f6c432e52
[]
no_license
singhdipesh14/OOPCPP
b26cc62049b65b4f805d5a304ececd31941be410
b0d6565fc2d99e603eb091402a98d9ff6683ca5b
refs/heads/master
2022-12-02T23:51:53.912264
2020-08-20T15:53:45
2020-08-20T15:53:45
289,047,046
0
0
null
null
null
null
UTF-8
C++
false
false
13,454
h
/** * @file LinkedListExercises.h * University of Illinois CS 400, MOOC 2, Week 1: Linked List * Spring 2019 * STUDENT STARTER FILE * * @author Eric Huber - University of Illinois staff * **/ /******************************************************************** Week 1: Linked List and Merge Sort Exercises There are two exercises in this file. Please read the code comments below and see the provided instructions PDF before you begin. The other provided code files in the starter zip also contain important comments and hints about how to approach this. This is the only file you can edit for the sake of grading! You can edit the other provided starter files for testing purposes, but the autograder will assume that this is the only file that has been edited by you, and the others will be replaced with the original intended versions at grading time. ********************************************************************/ // Prevent the header from being included more than once per cpp file #pragma once // It's good to put system headers first, so that if your own libraries // cause conflicts, the compiler will most likely identify the problem // as being in your own source code files, where it arises later. #include <iostream> #include <string> #include "LinkedList.h" /******************************************************************** Exercise 1: insertOrdered This LinkedList member function assumes that the current contents of the list are already sorted in increasing order. The function takes as input a new data item to be inserted to the same list. The new data item should be inserted to the list in the correct position, so that you preserve the overall sorted state of the list. For example, if your LinkedList<int> contains: [1, 2, 8, 9] And the input is 7, then the list should be updated to contain: [1, 2, 7, 8, 9] To be more precise, a new node should be created on the heap, and it should be inserted in front of the earliest node in the list that contains a greater data element. If no such other node exists, then the new item should be placed at the end (the back of the list). Also, be sure to update the size_ member of the list appropriately. Your implementation of this function should not change the memory locations of the existing nodes. That is, you should not push or pop the existing elements of the list if it would change their address. (The member functions for push and pop will add a new node or delete one, so these operations would not leave the original node in place even if you recreated the node with equivalent data.) You should use pointers to connect your new node at the correct insertion location, being sure to adjust the list's head and tail pointers if necessary, as well as any prev or next pointers of adjacent nodes in the list. Remember: LinkedList is a doubly-linked list. That means each node also refers to the previous item in the list, not just the next item. A correct implementation of this function has O(n) time complexity for a list of length n. That is, in the worst case, you would traverse each element of the list some constant number of times. You can use "make test" followed by "./test" to check the correctness of your implementation, and then you can use "./test [bench]" to run some interesting benchmarks on the speed of your code. ********************************************************************/ template <typename T> void LinkedList<T>::insertOrdered(const T& newData) { // ----------------------------------------------------------- // TODO: Your code here! Node* newNode = new Node(newData); if(!isSorted() ) { insertionSort(); } if(empty()){ pushFront(newNode->data); } else { if(newNode->data>=back()){ pushBack(newNode->data); } else if(newNode->data<=front()){ pushFront(newNode->data); } else{ Node* curr= head_; bool inserted = false; while(!inserted){ if(newNode->data<=curr->data){ newNode->next = curr; newNode->prev = curr->prev; curr->prev = newNode; curr = newNode->prev; curr->next = newNode; size_++; inserted=true; } curr = curr->next; } } } // ----------------------------------------------------------- // Please implement this function according to the description // above and in the instructions PDF. // Hints: // Make your new node on the heap and then find where it needs to // go in the list. A good way to do this is by considering special // base cases first, then walk the list from front to back and find // the earliest position where you should insert the new node. // When you insert the node, make sure to update any and all pointers // between it and adjacent nodes accordingly (next and prev pointers). // You may also need to update the head_ and tail_ pointers in some // cases. Also update the size_ variable of the list. // There are explicit examples of how to do all those things in the // other provided code for this project! // More hints: // First, practice your technique for traversing the list from front // to back. You can see examples of several ways to do this throughout // the provided code for this project. We recommend that you try using // a temporary pointer that you update to track your position as you // traverse from node to node. // Consider all the cases that can happen when you're trying to insert // the new node. Is the list currently empty? Does the new node go // at the beginning? Does it go somewhere in the middle? Does it go // at the end? Remember that this is a doubly-linked list, so there // may be prev and next pointers to adjust on both sides of the node // that you insert. // Be careful to make your conditional cases mutually exclusive when // necessary. Are you using "else" where you should? // Don't dereference a null pointer, ever! Always make sure the logic // checks for that before dereferencing. This is the most common cause // of crashes. The test program will show you which test cases are // causing you issues with that. A common issue is that when students // traverse an empty list or when they traverse to the end of the list, // they don't handle the null pointer at the tail properly. Be careful // to update all next, prev, head_, and tail_ pointers as needed on your // new node or on those existing nodes that are adjacent to the new node. } /******************************************************************** Exercise 2: Linear-time Merge This LinkedList member function is intended to perform the classic "merge" operation from the mergesort algorithm. It combines two sorted lists into a single sorted list. This algorithm is intended to run in linear time (that is, O(n) where n is the total number of elements in the input lists), so it is not appropriate to simply concatenate the two lists and then apply a sorting algorithm. Instead, the merge algorithm relies on the fact that the two input lists are already sorted separately in order to create the merged, sorted list in linear time. One of the implied input lists is the "*this" LinkedList instance that is calling the function, and the other input list is explicitly specified as the function argument "other". The function does NOT change either of the original lists directly, as the inputs are marked const. Instead, this function makes a new list containing the merged result, and it returns a copy of the new list. For example, one usage might look like this (OUTSIDE of this function, where we are making the call): LinkedList<int> leftList; // [... Add some sorted contents to leftList here. ...] LinkedList<int> rightList; // [... Add some sorted contents to rightList here. ...] LinkedList<int> mergedList = leftList.merge(rightList); You may assume that the two input lists have already been sorted. However, the lists may be empty, and they may contain repeated or overlapping elements. The lists may also have different lengths. For example, it's possible that these are the two input lists: Left list: [1, 2, 2, 3, 5, 5, 5, 6] Right list: [1, 3, 5, 7] And the result of merging those two sorted lists will contain all of the same elements, including the correct number of any duplicates, in sorted order: [1, 1, 2, 2, 3, 3, 5, 5, 5, 5, 6, 7] Because your implementation of this function operates on const inputs and returns a newly created list, you do not need to maintain the previous memory locations of any nodes as required in Exercise 1. You may need to make non-const "working copies" of the const input lists if you wish. You may approach this problem either iteratively or recursively, and you may use the member functions of the LinkedList class to make it simpler (such as push and pop), or you may edit the contents of lists explicitly by changing the pointers of a list or of its nodes (such as head_, tail_, next, and prev). Be sure that the size_ member of the resulting list is correct. A correct implementation of this function has O(n) time complexity for a list of length n. That is, in the worst case, you would traverse each element of the list some constant number of times. Important notes for getting the correct running time: 1. Since both lists being merged are already sorted themselves, there is a way to merge them together into a single sorted list in a single traversal pass down the lists. This can run in O(n) time. 2. You SHOULD NOT call any sorting function in your merge function. 3. You SHOULD NOT call the insertOrdered function in merge. That would result in a very slow running time. (The insertOrdered function was part of the insertion sort exercise. It has nothing to do with merge or merge sort.) You can use "make test" followed by "./test" to check the correctness of your implementation, and then you can use "./test [bench]" to run some interesting benchmarks on the speed of your code. ********************************************************************/ template <typename T> LinkedList<T> LinkedList<T>::merge(const LinkedList<T>& other) const { // You can't edit the original instance of LinkedList that is calling // merge because the function is marked const, and the "other" input // list is also marked const. However, here we'll make some convenient // "working copies" of the two lists: "*this" refers to the current // list object instance that is calling the merge member function, and // "other" refers to the list that was passed as an argument: LinkedList<T> left = *this; LinkedList<T> right = other; // So if this function was called as "A.merge(B)", then now, "left" // is a temporary copy of the "A" and "right" is a temporary copy // of the "B". // We will also create an empty list called "merged" where we can build // the final result we want. This is what we will return at the end of // the function. LinkedList<T> merged; // ----------------------------------------------------------- // TODO: Your code here! if (!left.isSorted()){ left.mergeSort(); } if (!right.isSorted()){ right.mergeSort(); } while((left.size()+right.size()>0)){ if(left.empty()){ merged.pushBack(right.front()); right.popFront(); } else if(right.empty()){ merged.pushBack(left.front()); left.popFront(); } else { if(left.front()<=right.front()){ merged.pushBack(left.front()); left.popFront(); } else { merged.pushBack(right.front()); right.popFront(); } } } // ----------------------------------------------------------- // Please implement this function according to the description // above and in the instructions PDF. // Hints: // 1. Assuming that the left and right lists are already sorted, remember // that the smallest items are already available at the front. You can // access them immediately. // 2. Think of which item needs to be placed first in the merged list. // Then think about what item should be placed second. You need to // think carefully about which list to take from next after you take // each single item. // 3. You can do this while walking down the left and right lists exactly // once. Do not loop over the lists multiple times. If you are doing // that, your implementation is probably already running in O(n^2) // time or worse, and not O(n). // 4. Remember, DO NOT try to use insertOrdered here. That would be // very slow. // ----------------------------------------------------------- // We return the merged list by value here. It may be copied out of the // function, but usually the compiler will optimize this to automatically // create it directly in the correct memory space outside without copying. // Don't worry about the speed of that right now. (By the way, did you // notice that all of our nodes are created on the heap? The part of the // list that we pass back is really small; it just contains two pointers // and an int.) return merged; }
0d4ac713f1d6c162ce5d4bfc1f49af85bb85eb84
35630b68e10b93e388d882c921e64a5e821150d8
/lexer/tokens/comma.cpp
197fdf554f3ccaa1368c635692cdbb0b7f1c841c
[]
no_license
Alexander1000/postgresman
049456558e91e970ebb76b9b6a92ed312c127208
17945fff143006d20b3aeb2bce025b7a9dc5bf3f
refs/heads/master
2021-08-23T01:54:12.500578
2017-12-02T09:29:30
2017-12-02T09:29:30
110,338,460
0
0
null
null
null
null
UTF-8
C++
false
false
106
cpp
class Comma : public Token { public: Comma(int line, int column) : Token(line, column) { } };
7e80cff4a95a7738df9f975e3541fe6bdf779851
ecdc3b6130c00146a28b74939b8fe43cff3023f3
/C++_button_ISR_LED.X/my_button_handler.h
b72668518db7d6353380414ad7caa5195023e39a
[]
no_license
PICdew/MPLAB_X_experiments
a1734beeddce612eafa5d820fdd94854f36a0485
a99f28f3347015f7b907122aca66ab7702b8fec2
refs/heads/master
2020-04-19T13:53:28.317981
2014-05-16T20:58:50
2014-05-16T20:58:50
null
0
0
null
null
null
null
UTF-8
C++
false
false
407
h
/* * File: my_button_handler.h * Author: John * * Created on April 21, 2014, 8:53 AM */ #ifndef MY_BUTTON_HANDLER_H #define MY_BUTTON_HANDLER_H class my_button_handler { public: static my_button_handler& get_instance(void); void do_the_button_thing(void); private: my_button_handler(); void handle_button_1(void); void handle_button_2(void); }; #endif /* MY_BUTTON_HANDLER_H */
812578af19aa5a40994c70c459184bca4569911c
e9490290a3eb2bbb783308f079c5aa31fcaa6f7d
/1 OpenMP/Week 3/3.1_ordered/3.1_ordered/main.cpp
75d7e022774eef813d5b84f27cff9fa4ea620793
[ "MIT" ]
permissive
WazaWaza2020/Parallel-computing-Coursera
70514e9bc2fcd7857a2a5996e839aedb0d5dbcf4
46e0b646713ccae9101b873e428c767e0bde2e85
refs/heads/master
2021-09-13T05:01:26.387374
2018-04-25T07:11:22
2018-04-25T07:11:22
null
0
0
null
null
null
null
WINDOWS-1251
C++
false
false
332
cpp
#include <stdio.h> #include <omp.h> #include <locale> int main() { setlocale(LC_ALL, "Russian"); int i, n; #pragma omp parallel private (i, n) { n = omp_get_thread_num(); #pragma omp for ordered for (i = 0; i<5; i++) { #pragma omp ordered { printf("ordered: Поток %d, итерация %d\n", n, i); } } } }
4b0a6252fb4cb7751d45c506b7d1f7f114e93b93
131f3d14e44ec07e1ab0093f953f51f8096ec617
/rpc/src/stub_client.cc
dbb1e8469a472b85d5f1e38eecf9d7ceda2db53e
[ "BSD-2-Clause" ]
permissive
tibkiss/codesloop
42e19541f875834fa771bc630444f7adc1d32ad9
3f5c2ccbefb611c47ea9cbc5a5e4b8e9a39d2d0f
refs/heads/master
2020-04-08T12:12:42.453779
2009-08-03T19:04:16
2009-08-03T19:04:16
null
0
0
null
null
null
null
UTF-8
C++
false
false
5,827
cc
/* Copyright (c) 2008,2009, David Beck, Tamas Foldi Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "stub_client.hh" #include "common.h" /** @file csl_rpc/src/stub_client.cc @brief implementation of codesloop interface descriptor */ using std::endl; namespace csl { namespace rpc { void stub_client::generate() { const char * class_name = (ifname_ + "_cli").c_str(); open_file((ifname_+"_cli.cc").c_str()); output_ << "#ifdef __cplusplus" << endl << "#include \"" << (ifname_+"_cli.hh").c_str() << "\"" << endl ; /*---------------------------------------------------------\ | Generate namespace info | \---------------------------------------------------------*/ this->generate_ns_open(); /*---------------------------------------------------------\ | Generate function bodies | \---------------------------------------------------------*/ iface::function_iterator func_it = ifc_->get_functions()->begin(); iface::func::param_iterator param_it; while ( func_it != ifc_->get_functions()->end() ) { /* synchronous call */ output_ << ls_ << "void " << class_name << "::" << (*func_it).name << " (" << endl ; this->generate_func_params( (*func_it).name, STUB_CLIENT, false); output_ << endl << ls_ << "{" << endl << ls_ << " csl::rpc::handle __handle;" << endl << ls_ << " this->create_handle(__handle);" << endl << endl ; /*---------------------------------------------------------\ | Synchronous function | \---------------------------------------------------------*/ output_ << ls_ << " " << (*func_it).name << "(" << endl << ls_ << " " << "__handle," << endl ; param_it = (*func_it).params.begin(); while( param_it != (*func_it).params.end() ) { /* ignore exceptions */ if( (*param_it).kind==MD_EXCEPTION) { param_it++; continue; } output_ << ls_ << " " << (*param_it).name ; param_it++; if ( (*param_it).kind!=MD_EXCEPTION && param_it != (*func_it).params.end() ) output_ << ","; output_ << endl; } output_ << ls_ << " );" << endl << endl << ls_ << " this->wait( __handle);" << endl << endl << ls_ << "}" << endl << endl ; /*---------------------------------------------------------\ | Function implementation on client side stub | \---------------------------------------------------------*/ output_ << ls_ << "void " << class_name << "::" << (*func_it).name << " (" << endl ; this->generate_func_params( (*func_it).name, STUB_CLIENT, true); output_ << endl << ls_ << "{" << endl << endl ; // serializer output_ << ls_ << " arch archiver(arch::SERIALIZE);" << endl << endl << ls_ << " archiver.serialize(__if_" << ifname_.c_str() << "_crc );" << endl << ls_ << " archiver.serialize(__func_"<< ifname_.c_str() << "_"<< (*func_it).name << "_id);" << endl ; param_it = (*func_it).params.begin(); while( param_it != (*func_it).params.end() ) { /* ignore exceptions and output parameters */ if( (*param_it).kind==MD_EXCEPTION ||(*param_it).kind==MD_OUTPUT) { param_it++; continue; } output_ << ls_ << " archiver.serialize(" << (*param_it).name << ");" << endl ; param_it++; } output_ << endl << ls_ << " send(__handle,archiver.get_pbuf());" << endl << ls_ << "}" << endl << endl << endl ; func_it++; } /*---------------------------------------------------------\ | Cleanup | \---------------------------------------------------------*/ generate_ns_close(); output_ << endl << "#endif /* __cplusplus */" << endl << "/* EOF */" << endl ; } }; }; /* EOF */
e765b64c20599f170f46ed1b2ebd2e57529d48cf
2ecf23a22ede90d51895eb0154e1f88faa4ae2df
/src/CPlusPlusTemplates/Chapter.16/Details/TestCounter.cpp
2a767ad4134e41b02ea3323f955e2bbc675101de
[]
no_license
zhangrongzhao/CPlusPlusTemplates
d7b53a8e1e9f4bd5b5191c26da7f135e28cba54f
5b4132d10c9eeab112f174397de2bfca26d5714e
refs/heads/master
2020-06-11T01:56:39.320247
2016-12-07T09:14:03
2016-12-07T09:14:03
75,818,671
0
0
null
null
null
null
UTF-8
C++
false
false
395
cpp
#include "stdafx.h" #include <iostream> #include "objectCounter.h" template<typename charT> class MyString :public ObjectCounter<MyString<charT>>{ }; void test_counter(){ MyString<char> s1, s2; MyString<wchar_t> ws; std::cout << "number of MyString<char>: " << MyString<char>::live() << std::endl; std::cout << "number of MyString<wchar_t>:" << MyString<wchar_t>::live() << std::endl; }
33c8e8f02c042e7e4f7f235ef4ae6bf80139c37a
6e022ee369ffc2d6c34ebcc3072fa22f71b19062
/Maxwell-Equation-Solver/Interface.cpp
795122ff8586c7b9788d8eef243eb63712e2e697
[ "MIT" ]
permissive
08jne01/Maxwell-Equation-Solver
5100679c302825a96ef7b17e8eb465d486a0032f
1e573d9c471b813448b50ed17414c4f5e978922c
refs/heads/master
2021-07-16T04:07:23.264709
2021-07-08T17:07:07
2021-07-08T17:07:07
152,332,781
5
0
null
null
null
null
UTF-8
C++
false
false
81
cpp
#include "Interface.h" //Interface class definitions Interface::Interface() { }
a82418bc93ed7d9d7e494495c7eb6044a7cdcfbe
d0c44dd3da2ef8c0ff835982a437946cbf4d2940
/cmake-build-debug/programs_tiling/function13731/function13731_no_schedule/function13731_no_schedule.cpp
61760197a2f043e7a7ea429cfa5b8a2aa0ec3c5a
[]
no_license
IsraMekki/tiramisu_code_generator
8b3f1d63cff62ba9f5242c019058d5a3119184a3
5a259d8e244af452e5301126683fa4320c2047a3
refs/heads/master
2020-04-29T17:27:57.987172
2019-04-23T16:50:32
2019-04-23T16:50:32
176,297,755
1
2
null
null
null
null
UTF-8
C++
false
false
1,831
cpp
#include <tiramisu/tiramisu.h> using namespace tiramisu; int main(int argc, char **argv){ tiramisu::init("function13731_no_schedule"); constant c0("c0", 512), c1("c1", 1024), c2("c2", 128); var i0("i0", 0, c0), i1("i1", 0, c1), i2("i2", 0, c2); input input00("input00", {i1}, p_int32); input input01("input01", {i0, i1}, p_int32); input input02("input02", {i1, i2}, p_int32); input input03("input03", {i0, i2}, p_int32); input input04("input04", {i2}, p_int32); input input05("input05", {i0}, p_int32); input input06("input06", {i0, i2}, p_int32); input input07("input07", {i1}, p_int32); computation comp0("comp0", {i0, i1, i2}, input00(i1) * input01(i0, i1) * input02(i1, i2) + input03(i0, i2) * input04(i2) - input05(i0) * input06(i0, i2) * input07(i1)); comp0.parallelize(i0); buffer buf00("buf00", {1024}, p_int32, a_input); buffer buf01("buf01", {512, 1024}, p_int32, a_input); buffer buf02("buf02", {1024, 128}, p_int32, a_input); buffer buf03("buf03", {512, 128}, p_int32, a_input); buffer buf04("buf04", {128}, p_int32, a_input); buffer buf05("buf05", {512}, p_int32, a_input); buffer buf06("buf06", {512, 128}, p_int32, a_input); buffer buf07("buf07", {1024}, p_int32, a_input); buffer buf0("buf0", {512, 1024, 128}, p_int32, a_output); input00.store_in(&buf00); input01.store_in(&buf01); input02.store_in(&buf02); input03.store_in(&buf03); input04.store_in(&buf04); input05.store_in(&buf05); input06.store_in(&buf06); input07.store_in(&buf07); comp0.store_in(&buf0); tiramisu::codegen({&buf00, &buf01, &buf02, &buf03, &buf04, &buf05, &buf06, &buf07, &buf0}, "../data/programs/function13731/function13731_no_schedule/function13731_no_schedule.o"); return 0; }
fe6adf664d6d6fbfe05f5187f9b99a16f6fce550
629d859edf65182cf430cc4a6e4b40e628b26cf9
/算法题/08最小路径和.cpp
15343863d2df8c543d061be7784efb1fdc8bc1fd
[]
no_license
ADreamyj/cpp
8f0a0622afc4bb7c764083634a7951818ceedfa3
c4672b3f8b3aa51a75741f030597cb17574b3a1c
refs/heads/master
2020-04-03T15:25:20.448530
2019-10-06T12:32:20
2019-10-06T12:32:20
null
0
0
null
null
null
null
GB18030
C++
false
false
1,471
cpp
#define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> #include<vector> #include<algorithm> //题目:给定一个m*n的网格,网格用非负数填充,找到一条从左上角到右下角 //的最短路径,每次只能向上或者是向下运动 //方法:动态规划 //状态: // 子状态:从(0,0)到达(1,0),(1,1)....(m - 1,n - 1)的最短路径 // F(i,j):从(0,0)到达F(i,j)的最短路径 //状态递归: // F(i,j) = min{F(i - 1 , j) , F(i,j - 1)} + (i,j); //初始化: // F(0,0) = (0,0) // 特殊情况:第0行和第0列 // F(0,i) = F(0,i - 1) + (0,i) // F(i,0) = F(i - 1,0) + (i,0) //返回结果: // F(m - 1,n - 1) int minPathSum(std::vector<std::vector<int>> &grid) { if (grid.empty() || grid[0].empty()) return 0; int m = grid.size(); int n = grid[0].size(); std::vector<std::vector<int>> v(m, std::vector<int>(n, 0)); //F(0,0) F(i,0) 和 F(0,i)进行初始化 v[0][0] = grid[0][0]; for (int i = 1; i < m; i++) { v[i][0] = grid[i][0] + v[i - 1][0]; } for (int j = 1; j < n; j++) { v[0][j] = grid[0][j] + v[0][j - 1]; } // F(i,j) = min{F(i-1,j) , F(i,j-1)} + (i,j) for (int i = 1; i < m; ++i) { for (int j = 1; j < n; ++j) { v[i][j] = std::min(v[i - 1][j], v[i][j - 1]) + grid[i][j]; } } return v[m - 1][n - 1]; } //int main() //{ // std::vector<std::vector<int>> v{ { 1, 1, 1 }, { 2, 2, 2 }, { 1, 1, 1 } }; // std::cout << minPathSum(v) << std::endl; // system("pause"); // return 0; //}
a8847ed5529f7e2c1f5fb23fcc3548674d287259
59138b1b34e2a9356ad7154a9705007349209e9a
/platform/JS/V8/v8/test/cctest/test-debug.cc
8c3818e8e9ec358b0ca929189139d2f49bae4ceb
[ "BSD-3-Clause", "bzip2-1.0.6", "SunPro", "Apache-2.0" ]
permissive
gboyraz/macchina.io
6b879fca2329e7060122adfc691b4870d4dc06ac
de79c4d2eace01e24d685ac7f7c2e8aadf6b2668
refs/heads/master
2020-06-29T13:18:05.975243
2019-08-04T22:43:08
2019-08-04T22:43:08
200,547,738
2
0
Apache-2.0
2019-08-04T22:29:19
2019-08-04T22:29:19
null
UTF-8
C++
false
false
238,207
cc
// Copyright 2012 the V8 project authors. All rights reserved. // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following // disclaimer in the documentation and/or other materials provided // with the distribution. // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived // from this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include <stdlib.h> #include <set> #include "src/v8.h" #include "src/api.h" #include "src/compilation-cache.h" #include "src/debug/debug-interface.h" #include "src/debug/debug.h" #include "src/deoptimizer.h" #include "src/frames.h" #include "src/objects-inl.h" #include "src/utils.h" #include "test/cctest/cctest.h" using ::v8::internal::EmbeddedVector; using ::v8::internal::Object; using ::v8::internal::Handle; using ::v8::internal::Heap; using ::v8::internal::JSGlobalProxy; using ::v8::internal::Code; using ::v8::internal::Debug; using ::v8::internal::StackFrame; using ::v8::internal::StepAction; using ::v8::internal::StepIn; // From StepAction enum using ::v8::internal::StepNext; // From StepAction enum using ::v8::internal::StepOut; // From StepAction enum using ::v8::internal::Vector; using ::v8::internal::StrLength; // Size of temp buffer for formatting small strings. #define SMALL_STRING_BUFFER_SIZE 80 // --- H e l p e r C l a s s e s // Helper class for creating a V8 enviromnent for running tests class DebugLocalContext { public: inline DebugLocalContext( v8::Isolate* isolate, v8::ExtensionConfiguration* extensions = 0, v8::Local<v8::ObjectTemplate> global_template = v8::Local<v8::ObjectTemplate>(), v8::Local<v8::Value> global_object = v8::Local<v8::Value>()) : scope_(isolate), context_(v8::Context::New(isolate, extensions, global_template, global_object)) { context_->Enter(); } inline DebugLocalContext( v8::ExtensionConfiguration* extensions = 0, v8::Local<v8::ObjectTemplate> global_template = v8::Local<v8::ObjectTemplate>(), v8::Local<v8::Value> global_object = v8::Local<v8::Value>()) : scope_(CcTest::isolate()), context_(v8::Context::New(CcTest::isolate(), extensions, global_template, global_object)) { context_->Enter(); } inline ~DebugLocalContext() { context_->Exit(); } inline v8::Local<v8::Context> context() { return context_; } inline v8::Context* operator->() { return *context_; } inline v8::Context* operator*() { return *context_; } inline v8::Isolate* GetIsolate() { return context_->GetIsolate(); } inline bool IsReady() { return !context_.IsEmpty(); } void ExposeDebug() { v8::internal::Isolate* isolate = reinterpret_cast<v8::internal::Isolate*>(context_->GetIsolate()); v8::internal::Factory* factory = isolate->factory(); // Expose the debug context global object in the global object for testing. CHECK(isolate->debug()->Load()); Handle<v8::internal::Context> debug_context = isolate->debug()->debug_context(); debug_context->set_security_token( v8::Utils::OpenHandle(*context_)->security_token()); Handle<JSGlobalProxy> global(Handle<JSGlobalProxy>::cast( v8::Utils::OpenHandle(*context_->Global()))); Handle<v8::internal::String> debug_string = factory->InternalizeOneByteString(STATIC_CHAR_VECTOR("debug")); v8::internal::JSObject::SetOwnPropertyIgnoreAttributes( global, debug_string, handle(debug_context->global_proxy()), v8::internal::DONT_ENUM) .Check(); } private: v8::HandleScope scope_; v8::Local<v8::Context> context_; }; // --- H e l p e r F u n c t i o n s // Compile and run the supplied source and return the requested function. static v8::Local<v8::Function> CompileFunction(v8::Isolate* isolate, const char* source, const char* function_name) { CompileRunChecked(isolate, source); v8::Local<v8::String> name = v8_str(isolate, function_name); v8::Local<v8::Context> context = isolate->GetCurrentContext(); v8::MaybeLocal<v8::Value> maybe_function = context->Global()->Get(context, name); return v8::Local<v8::Function>::Cast(maybe_function.ToLocalChecked()); } // Compile and run the supplied source and return the requested function. static v8::Local<v8::Function> CompileFunction(DebugLocalContext* env, const char* source, const char* function_name) { return CompileFunction(env->GetIsolate(), source, function_name); } static void SetDebugEventListener( v8::Isolate* isolate, v8::Debug::EventCallback that, v8::Local<v8::Value> data = v8::Local<v8::Value>()) { i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); i::HandleScope scope(i_isolate); if (that == nullptr) { i_isolate->debug()->SetDebugDelegate(nullptr, false); } else { i::Handle<i::Object> i_data = i_isolate->factory()->undefined_value(); if (!data.IsEmpty()) i_data = v8::Utils::OpenHandle(*data); i::NativeDebugDelegate* delegate = new i::NativeDebugDelegate(i_isolate, that, i_data); i_isolate->debug()->SetDebugDelegate(delegate, true); } } // Is there any debug info for the function? static bool HasBreakInfo(v8::Local<v8::Function> fun) { Handle<v8::internal::JSFunction> f = Handle<v8::internal::JSFunction>::cast(v8::Utils::OpenHandle(*fun)); Handle<v8::internal::SharedFunctionInfo> shared(f->shared()); return shared->HasBreakInfo(); } // Set a break point in a function with a position relative to function start, // and return the associated break point number. static int SetBreakPoint(v8::Local<v8::Function> fun, int position) { i::Handle<i::JSFunction> function = i::Handle<i::JSFunction>::cast(v8::Utils::OpenHandle(*fun)); position += function->shared()->start_position(); static int break_point = 0; v8::internal::Isolate* isolate = function->GetIsolate(); v8::internal::Debug* debug = isolate->debug(); debug->SetBreakPoint( function, Handle<Object>(v8::internal::Smi::FromInt(++break_point), isolate), &position); return break_point; } // Set a break point in a function using the Debug object and return the // associated break point number. static int SetBreakPointFromJS(v8::Isolate* isolate, const char* function_name, int line, int position) { EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; SNPrintF(buffer, "debug.Debug.setBreakPoint(%s,%d,%d)", function_name, line, position); buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; v8::Local<v8::Value> value = CompileRunChecked(isolate, buffer.start()); return value->Int32Value(isolate->GetCurrentContext()).FromJust(); } // Set a break point in a script identified by id using the global Debug object. static int SetScriptBreakPointByIdFromJS(v8::Isolate* isolate, int script_id, int line, int column) { EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; if (column >= 0) { // Column specified set script break point on precise location. SNPrintF(buffer, "debug.Debug.setScriptBreakPointById(%d,%d,%d)", script_id, line, column); } else { // Column not specified set script break point on line. SNPrintF(buffer, "debug.Debug.setScriptBreakPointById(%d,%d)", script_id, line); } buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; { v8::TryCatch try_catch(isolate); v8::Local<v8::Value> value = CompileRunChecked(isolate, buffer.start()); CHECK(!try_catch.HasCaught()); return value->Int32Value(isolate->GetCurrentContext()).FromJust(); } } // Set a break point in a script identified by name using the global Debug // object. static int SetScriptBreakPointByNameFromJS(v8::Isolate* isolate, const char* script_name, int line, int column) { EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; if (column >= 0) { // Column specified set script break point on precise location. SNPrintF(buffer, "debug.Debug.setScriptBreakPointByName(\"%s\",%d,%d)", script_name, line, column); } else { // Column not specified set script break point on line. SNPrintF(buffer, "debug.Debug.setScriptBreakPointByName(\"%s\",%d)", script_name, line); } buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; { v8::TryCatch try_catch(isolate); v8::Local<v8::Value> value = CompileRunChecked(isolate, buffer.start()); CHECK(!try_catch.HasCaught()); return value->Int32Value(isolate->GetCurrentContext()).FromJust(); } } // Clear a break point. static void ClearBreakPoint(int break_point) { v8::internal::Isolate* isolate = CcTest::i_isolate(); v8::internal::Debug* debug = isolate->debug(); debug->ClearBreakPoint( Handle<Object>(v8::internal::Smi::FromInt(break_point), isolate)); } // Clear a break point using the global Debug object. static void ClearBreakPointFromJS(v8::Isolate* isolate, int break_point_number) { EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; SNPrintF(buffer, "debug.Debug.clearBreakPoint(%d)", break_point_number); buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; CompileRunChecked(isolate, buffer.start()); } static void EnableScriptBreakPointFromJS(v8::Isolate* isolate, int break_point_number) { EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; SNPrintF(buffer, "debug.Debug.enableScriptBreakPoint(%d)", break_point_number); buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; CompileRunChecked(isolate, buffer.start()); } static void DisableScriptBreakPointFromJS(v8::Isolate* isolate, int break_point_number) { EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; SNPrintF(buffer, "debug.Debug.disableScriptBreakPoint(%d)", break_point_number); buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; CompileRunChecked(isolate, buffer.start()); } static void ChangeScriptBreakPointConditionFromJS(v8::Isolate* isolate, int break_point_number, const char* condition) { EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; SNPrintF(buffer, "debug.Debug.changeScriptBreakPointCondition(%d, \"%s\")", break_point_number, condition); buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0'; CompileRunChecked(isolate, buffer.start()); } // Change break on exception. static void ChangeBreakOnException(bool caught, bool uncaught) { v8::internal::Debug* debug = CcTest::i_isolate()->debug(); debug->ChangeBreakOnException(v8::internal::BreakException, caught); debug->ChangeBreakOnException(v8::internal::BreakUncaughtException, uncaught); } // Change break on exception using the global Debug object. static void ChangeBreakOnExceptionFromJS(v8::Isolate* isolate, bool caught, bool uncaught) { if (caught) { CompileRunChecked(isolate, "debug.Debug.setBreakOnException()"); } else { CompileRunChecked(isolate, "debug.Debug.clearBreakOnException()"); } if (uncaught) { CompileRunChecked(isolate, "debug.Debug.setBreakOnUncaughtException()"); } else { CompileRunChecked(isolate, "debug.Debug.clearBreakOnUncaughtException()"); } } // Change break on exception using the native API call. static void ChangeBreakOnExceptionFromAPI( v8::Isolate* isolate, v8::debug::ExceptionBreakState state) { v8::debug::ChangeBreakOnException(isolate, state); } // Prepare to step to next break location. static void PrepareStep(StepAction step_action) { v8::internal::Debug* debug = CcTest::i_isolate()->debug(); debug->PrepareStep(step_action); } static void ClearStepping() { CcTest::i_isolate()->debug()->ClearStepping(); } // This function is in namespace v8::internal to be friend with class // v8::internal::Debug. namespace v8 { namespace internal { // Collect the currently debugged functions. Handle<FixedArray> GetDebuggedFunctions() { Debug* debug = CcTest::i_isolate()->debug(); v8::internal::DebugInfoListNode* node = debug->debug_info_list_; // Find the number of debugged functions. int count = 0; while (node) { count++; node = node->next(); } // Allocate array for the debugged functions Handle<FixedArray> debugged_functions = CcTest::i_isolate()->factory()->NewFixedArray(count); // Run through the debug info objects and collect all functions. count = 0; while (node) { debugged_functions->set(count++, *node->debug_info()); node = node->next(); } return debugged_functions; } // Check that the debugger has been fully unloaded. void CheckDebuggerUnloaded() { // Check that the debugger context is cleared and that there is no debug // information stored for the debugger. CHECK(CcTest::i_isolate()->debug()->debug_context().is_null()); CHECK(!CcTest::i_isolate()->debug()->debug_info_list_); // Collect garbage to ensure weak handles are cleared. CcTest::CollectAllGarbage(); CcTest::CollectAllGarbage(Heap::kMakeHeapIterableMask); // Iterate the heap and check that there are no debugger related objects left. HeapIterator iterator(CcTest::heap()); for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) { CHECK(!obj->IsDebugInfo()); } } } // namespace internal } // namespace v8 // Check that the debugger has been fully unloaded. static void CheckDebuggerUnloaded() { v8::internal::CheckDebuggerUnloaded(); } // --- D e b u g E v e n t H a n d l e r s // --- // --- The different tests uses a number of debug event handlers. // --- // Source for the JavaScript function which picks out the function // name of a frame. const char* frame_function_name_source = "function frame_function_name(exec_state, frame_number) {" " return exec_state.frame(frame_number).func().name();" "}"; v8::Local<v8::Function> frame_function_name; // Source for the JavaScript function which pick out the name of the // first argument of a frame. const char* frame_argument_name_source = "function frame_argument_name(exec_state, frame_number) {" " return exec_state.frame(frame_number).argumentName(0);" "}"; v8::Local<v8::Function> frame_argument_name; // Source for the JavaScript function which pick out the value of the // first argument of a frame. const char* frame_argument_value_source = "function frame_argument_value(exec_state, frame_number) {" " return exec_state.frame(frame_number).argumentValue(0).value_;" "}"; v8::Local<v8::Function> frame_argument_value; // Source for the JavaScript function which pick out the name of the // first argument of a frame. const char* frame_local_name_source = "function frame_local_name(exec_state, frame_number) {" " return exec_state.frame(frame_number).localName(0);" "}"; v8::Local<v8::Function> frame_local_name; // Source for the JavaScript function which pick out the value of the // first argument of a frame. const char* frame_local_value_source = "function frame_local_value(exec_state, frame_number) {" " return exec_state.frame(frame_number).localValue(0).value_;" "}"; v8::Local<v8::Function> frame_local_value; // Source for the JavaScript function which picks out the source line for the // top frame. const char* frame_source_line_source = "function frame_source_line(exec_state) {" " return exec_state.frame(0).sourceLine();" "}"; v8::Local<v8::Function> frame_source_line; // Source for the JavaScript function which picks out the source column for the // top frame. const char* frame_source_column_source = "function frame_source_column(exec_state) {" " return exec_state.frame(0).sourceColumn();" "}"; v8::Local<v8::Function> frame_source_column; // Source for the JavaScript function which picks out the script name for the // top frame. const char* frame_script_name_source = "function frame_script_name(exec_state) {" " return exec_state.frame(0).func().script().name();" "}"; v8::Local<v8::Function> frame_script_name; // Source for the JavaScript function which returns the number of frames. static const char* frame_count_source = "function frame_count(exec_state) {" " return exec_state.frameCount();" "}"; v8::Local<v8::Function> frame_count; // Global variable to store the last function hit - used by some tests. char last_function_hit[80]; // Global variable to store the name for last script hit - used by some tests. char last_script_name_hit[80]; // Global variables to store the last source position - used by some tests. int last_source_line = -1; int last_source_column = -1; // Debug event handler which counts the break points which have been hit. int break_point_hit_count = 0; int break_point_hit_count_deoptimize = 0; static void DebugEventBreakPointHitCount( const v8::Debug::EventDetails& event_details) { v8::DebugEvent event = event_details.GetEvent(); v8::Local<v8::Object> exec_state = event_details.GetExecutionState(); v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext(); v8::internal::Isolate* isolate = CcTest::i_isolate(); Debug* debug = isolate->debug(); // When hitting a debug event listener there must be a break set. CHECK_NE(debug->break_id(), 0); // Count the number of breaks. if (event == v8::Break) { break_point_hit_count++; if (!frame_function_name.IsEmpty()) { // Get the name of the function. const int argc = 2; v8::Local<v8::Value> argv[argc] = { exec_state, v8::Integer::New(CcTest::isolate(), 0)}; v8::Local<v8::Value> result = frame_function_name->Call(context, exec_state, argc, argv) .ToLocalChecked(); if (result->IsUndefined()) { last_function_hit[0] = '\0'; } else { CHECK(result->IsString()); v8::Local<v8::String> function_name(result.As<v8::String>()); function_name->WriteUtf8(last_function_hit); } } if (!frame_source_line.IsEmpty()) { // Get the source line. const int argc = 1; v8::Local<v8::Value> argv[argc] = {exec_state}; v8::Local<v8::Value> result = frame_source_line->Call(context, exec_state, argc, argv) .ToLocalChecked(); CHECK(result->IsNumber()); last_source_line = result->Int32Value(context).FromJust(); } if (!frame_source_column.IsEmpty()) { // Get the source column. const int argc = 1; v8::Local<v8::Value> argv[argc] = {exec_state}; v8::Local<v8::Value> result = frame_source_column->Call(context, exec_state, argc, argv) .ToLocalChecked(); CHECK(result->IsNumber()); last_source_column = result->Int32Value(context).FromJust(); } if (!frame_script_name.IsEmpty()) { // Get the script name of the function script. const int argc = 1; v8::Local<v8::Value> argv[argc] = {exec_state}; v8::Local<v8::Value> result = frame_script_name->Call(context, exec_state, argc, argv) .ToLocalChecked(); if (result->IsUndefined()) { last_script_name_hit[0] = '\0'; } else { CHECK(result->IsString()); v8::Local<v8::String> script_name(result.As<v8::String>()); script_name->WriteUtf8(last_script_name_hit); } } // Perform a full deoptimization when the specified number of // breaks have been hit. if (break_point_hit_count == break_point_hit_count_deoptimize) { i::Deoptimizer::DeoptimizeAll(isolate); } } } // Debug event handler which counts a number of events and collects the stack // height if there is a function compiled for that. int exception_hit_count = 0; int uncaught_exception_hit_count = 0; int last_js_stack_height = -1; v8::Local<v8::Function> debug_event_listener_callback; int debug_event_listener_callback_result; static void DebugEventCounterClear() { break_point_hit_count = 0; exception_hit_count = 0; uncaught_exception_hit_count = 0; } static void DebugEventCounter( const v8::Debug::EventDetails& event_details) { v8::Isolate::AllowJavascriptExecutionScope allow_script(CcTest::isolate()); v8::DebugEvent event = event_details.GetEvent(); v8::Local<v8::Object> exec_state = event_details.GetExecutionState(); v8::Local<v8::Object> event_data = event_details.GetEventData(); v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext(); v8::internal::Debug* debug = CcTest::i_isolate()->debug(); // When hitting a debug event listener there must be a break set. CHECK_NE(debug->break_id(), 0); // Count the number of breaks. if (event == v8::Break) { break_point_hit_count++; } else if (event == v8::Exception) { exception_hit_count++; // Check whether the exception was uncaught. v8::Local<v8::String> fun_name = v8_str(CcTest::isolate(), "uncaught"); v8::Local<v8::Function> fun = v8::Local<v8::Function>::Cast( event_data->Get(context, fun_name).ToLocalChecked()); v8::Local<v8::Value> result = fun->Call(context, event_data, 0, NULL).ToLocalChecked(); if (result->IsTrue()) { uncaught_exception_hit_count++; } } // Collect the JavsScript stack height if the function frame_count is // compiled. if (!frame_count.IsEmpty()) { static const int kArgc = 1; v8::Local<v8::Value> argv[kArgc] = {exec_state}; // Using exec_state as receiver is just to have a receiver. v8::Local<v8::Value> result = frame_count->Call(context, exec_state, kArgc, argv).ToLocalChecked(); last_js_stack_height = result->Int32Value(context).FromJust(); } // Run callback from DebugEventListener and check the result. if (!debug_event_listener_callback.IsEmpty()) { v8::Local<v8::Value> result = debug_event_listener_callback->Call(context, event_data, 0, NULL) .ToLocalChecked(); CHECK(!result.IsEmpty()); CHECK_EQ(debug_event_listener_callback_result, result->Int32Value(context).FromJust()); } } // Debug event handler which evaluates a number of expressions when a break // point is hit. Each evaluated expression is compared with an expected value. // For this debug event handler to work the following two global varaibles // must be initialized. // checks: An array of expressions and expected results // evaluate_check_function: A JavaScript function (see below) // Structure for holding checks to do. struct EvaluateCheck { const char* expr; // An expression to evaluate when a break point is hit. v8::Local<v8::Value> expected; // The expected result. }; // Array of checks to do. struct EvaluateCheck* checks = NULL; // Source for The JavaScript function which can do the evaluation when a break // point is hit. const char* evaluate_check_source = "function evaluate_check(exec_state, expr, expected) {" " return exec_state.frame(0).evaluate(expr).value() === expected;" "}"; v8::Local<v8::Function> evaluate_check_function; // The actual debug event described by the longer comment above. static void DebugEventEvaluate( const v8::Debug::EventDetails& event_details) { v8::DebugEvent event = event_details.GetEvent(); v8::Local<v8::Object> exec_state = event_details.GetExecutionState(); v8::Isolate* isolate = CcTest::isolate(); v8::Local<v8::Context> context = isolate->GetCurrentContext(); v8::internal::Debug* debug = CcTest::i_isolate()->debug(); // When hitting a debug event listener there must be a break set. CHECK_NE(debug->break_id(), 0); if (event == v8::Break) { break_point_hit_count++; for (int i = 0; checks[i].expr != NULL; i++) { const int argc = 3; v8::Local<v8::String> string = v8_str(isolate, checks[i].expr); v8::Local<v8::Value> argv[argc] = {exec_state, string, checks[i].expected}; v8::Local<v8::Value> result = evaluate_check_function->Call(context, exec_state, argc, argv) .ToLocalChecked(); if (!result->IsTrue()) { v8::String::Utf8Value utf8(isolate, checks[i].expected); V8_Fatal(__FILE__, __LINE__, "%s != %s", checks[i].expr, *utf8); } } } } // This debug event listener removes a breakpoint in a function int debug_event_remove_break_point = 0; static void DebugEventRemoveBreakPoint( const v8::Debug::EventDetails& event_details) { v8::DebugEvent event = event_details.GetEvent(); v8::Local<v8::Value> data = event_details.GetCallbackData(); v8::internal::Debug* debug = CcTest::i_isolate()->debug(); // When hitting a debug event listener there must be a break set. CHECK_NE(debug->break_id(), 0); if (event == v8::Break) { break_point_hit_count++; CHECK(data->IsFunction()); ClearBreakPoint(debug_event_remove_break_point); } } // Debug event handler which counts break points hit and performs a step // afterwards. StepAction step_action = StepIn; // Step action to perform when stepping. static void DebugEventStep( const v8::Debug::EventDetails& event_details) { v8::DebugEvent event = event_details.GetEvent(); v8::internal::Debug* debug = CcTest::i_isolate()->debug(); // When hitting a debug event listener there must be a break set. CHECK_NE(debug->break_id(), 0); if (event == v8::Break) { break_point_hit_count++; PrepareStep(step_action); } } // Debug event handler which counts break points hit and performs a step // afterwards. For each call the expected function is checked. // For this debug event handler to work the following two global varaibles // must be initialized. // expected_step_sequence: An array of the expected function call sequence. // frame_function_name: A JavaScript function (see below). // String containing the expected function call sequence. Note: this only works // if functions have name length of one. const char* expected_step_sequence = NULL; // The actual debug event described by the longer comment above. static void DebugEventStepSequence( const v8::Debug::EventDetails& event_details) { v8::DebugEvent event = event_details.GetEvent(); v8::Local<v8::Object> exec_state = event_details.GetExecutionState(); v8::internal::Debug* debug = CcTest::i_isolate()->debug(); // When hitting a debug event listener there must be a break set. CHECK_NE(debug->break_id(), 0); if (event == v8::Break || event == v8::Exception) { // Check that the current function is the expected. CHECK(break_point_hit_count < StrLength(expected_step_sequence)); const int argc = 2; v8::Local<v8::Value> argv[argc] = {exec_state, v8::Integer::New(CcTest::isolate(), 0)}; v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext(); v8::Local<v8::Value> result = frame_function_name->Call(context, exec_state, argc, argv) .ToLocalChecked(); CHECK(result->IsString()); v8::String::Utf8Value function_name( CcTest::isolate(), result->ToString(context).ToLocalChecked()); CHECK_EQ(1, StrLength(*function_name)); CHECK_EQ((*function_name)[0], expected_step_sequence[break_point_hit_count]); // Perform step. break_point_hit_count++; PrepareStep(step_action); } } // Debug event handler which performs a garbage collection. static void DebugEventBreakPointCollectGarbage( const v8::Debug::EventDetails& event_details) { v8::DebugEvent event = event_details.GetEvent(); v8::internal::Debug* debug = CcTest::i_isolate()->debug(); // When hitting a debug event listener there must be a break set. CHECK_NE(debug->break_id(), 0); // Perform a garbage collection when break point is hit and continue. Based // on the number of break points hit either scavenge or mark compact // collector is used. if (event == v8::Break) { break_point_hit_count++; if (break_point_hit_count % 2 == 0) { // Scavenge. CcTest::CollectGarbage(v8::internal::NEW_SPACE); } else { // Mark sweep compact. CcTest::CollectAllGarbage(); } } } // Debug event handler which re-issues a debug break and calls the garbage // collector to have the heap verified. static void DebugEventBreak( const v8::Debug::EventDetails& event_details) { v8::DebugEvent event = event_details.GetEvent(); v8::internal::Debug* debug = CcTest::i_isolate()->debug(); // When hitting a debug event listener there must be a break set. CHECK_NE(debug->break_id(), 0); if (event == v8::Break) { // Count the number of breaks. break_point_hit_count++; // Run the garbage collector to enforce heap verification if option // --verify-heap is set. CcTest::CollectGarbage(v8::internal::NEW_SPACE); // Set the break flag again to come back here as soon as possible. v8::debug::DebugBreak(CcTest::isolate()); } } // Debug event handler which re-issues a debug break until a limit has been // reached. int max_break_point_hit_count = 0; bool terminate_after_max_break_point_hit = false; static void DebugEventBreakMax( const v8::Debug::EventDetails& event_details) { v8::DebugEvent event = event_details.GetEvent(); v8::Isolate* v8_isolate = CcTest::isolate(); v8::internal::Isolate* isolate = CcTest::i_isolate(); v8::internal::Debug* debug = isolate->debug(); // When hitting a debug event listener there must be a break set. CHECK_NE(debug->break_id(), 0); if (event == v8::Break) { if (break_point_hit_count < max_break_point_hit_count) { // Count the number of breaks. break_point_hit_count++; // Set the break flag again to come back here as soon as possible. v8::debug::DebugBreak(v8_isolate); } else if (terminate_after_max_break_point_hit) { // Terminate execution after the last break if requested. v8_isolate->TerminateExecution(); } // Perform a full deoptimization when the specified number of // breaks have been hit. if (break_point_hit_count == break_point_hit_count_deoptimize) { i::Deoptimizer::DeoptimizeAll(isolate); } } } // --- M e s s a g e C a l l b a c k // Message callback which counts the number of messages. int message_callback_count = 0; static void MessageCallbackCountClear() { message_callback_count = 0; } static void MessageCallbackCount(v8::Local<v8::Message> message, v8::Local<v8::Value> data) { message_callback_count++; } // --- T h e A c t u a l T e s t s // Test that the debug info in the VM is in sync with the functions being // debugged. TEST(DebugInfo) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); // Create a couple of functions for the test. v8::Local<v8::Function> foo = CompileFunction(&env, "function foo(){}", "foo"); v8::Local<v8::Function> bar = CompileFunction(&env, "function bar(){}", "bar"); // Initially no functions are debugged. CHECK_EQ(0, v8::internal::GetDebuggedFunctions()->length()); CHECK(!HasBreakInfo(foo)); CHECK(!HasBreakInfo(bar)); EnableDebugger(env->GetIsolate()); // One function (foo) is debugged. int bp1 = SetBreakPoint(foo, 0); CHECK_EQ(1, v8::internal::GetDebuggedFunctions()->length()); CHECK(HasBreakInfo(foo)); CHECK(!HasBreakInfo(bar)); // Two functions are debugged. int bp2 = SetBreakPoint(bar, 0); CHECK_EQ(2, v8::internal::GetDebuggedFunctions()->length()); CHECK(HasBreakInfo(foo)); CHECK(HasBreakInfo(bar)); // One function (bar) is debugged. ClearBreakPoint(bp1); CHECK_EQ(1, v8::internal::GetDebuggedFunctions()->length()); CHECK(!HasBreakInfo(foo)); CHECK(HasBreakInfo(bar)); // No functions are debugged. ClearBreakPoint(bp2); DisableDebugger(env->GetIsolate()); CHECK_EQ(0, v8::internal::GetDebuggedFunctions()->length()); CHECK(!HasBreakInfo(foo)); CHECK(!HasBreakInfo(bar)); } // Test that a break point can be set at an IC store location. TEST(BreakPointICStore) { break_point_hit_count = 0; DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); SetDebugEventListener(env->GetIsolate(), DebugEventBreakPointHitCount); v8::Local<v8::Function> foo = CompileFunction(&env, "function foo(){bar=0;}", "foo"); // Run without breakpoints. foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(0, break_point_hit_count); // Run with breakpoint int bp = SetBreakPoint(foo, 0); foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(1, break_point_hit_count); foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(2, break_point_hit_count); // Run without breakpoints. ClearBreakPoint(bp); foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(2, break_point_hit_count); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } // Test that a break point can be set at an IC load location. TEST(BreakPointICLoad) { break_point_hit_count = 0; DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); SetDebugEventListener(env->GetIsolate(), DebugEventBreakPointHitCount); CompileRunChecked(env->GetIsolate(), "bar=1"); v8::Local<v8::Function> foo = CompileFunction(&env, "function foo(){var x=bar;}", "foo"); // Run without breakpoints. foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(0, break_point_hit_count); // Run with breakpoint. int bp = SetBreakPoint(foo, 0); foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(1, break_point_hit_count); foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(2, break_point_hit_count); // Run without breakpoints. ClearBreakPoint(bp); foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(2, break_point_hit_count); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } // Test that a break point can be set at an IC call location. TEST(BreakPointICCall) { break_point_hit_count = 0; DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); SetDebugEventListener(env->GetIsolate(), DebugEventBreakPointHitCount); CompileRunChecked(env->GetIsolate(), "function bar(){}"); v8::Local<v8::Function> foo = CompileFunction(&env, "function foo(){bar();}", "foo"); // Run without breakpoints. foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(0, break_point_hit_count); // Run with breakpoint int bp = SetBreakPoint(foo, 0); foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(1, break_point_hit_count); foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(2, break_point_hit_count); // Run without breakpoints. ClearBreakPoint(bp); foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(2, break_point_hit_count); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } // Test that a break point can be set at an IC call location and survive a GC. TEST(BreakPointICCallWithGC) { break_point_hit_count = 0; DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); SetDebugEventListener(env->GetIsolate(), DebugEventBreakPointCollectGarbage); CompileRunChecked(env->GetIsolate(), "function bar(){return 1;}"); v8::Local<v8::Function> foo = CompileFunction(&env, "function foo(){return bar();}", "foo"); v8::Local<v8::Context> context = env.context(); // Run without breakpoints. CHECK_EQ(1, foo->Call(context, env->Global(), 0, NULL) .ToLocalChecked() ->Int32Value(context) .FromJust()); CHECK_EQ(0, break_point_hit_count); // Run with breakpoint. int bp = SetBreakPoint(foo, 0); CHECK_EQ(1, foo->Call(context, env->Global(), 0, NULL) .ToLocalChecked() ->Int32Value(context) .FromJust()); CHECK_EQ(1, break_point_hit_count); CHECK_EQ(1, foo->Call(context, env->Global(), 0, NULL) .ToLocalChecked() ->Int32Value(context) .FromJust()); CHECK_EQ(2, break_point_hit_count); // Run without breakpoints. ClearBreakPoint(bp); foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(2, break_point_hit_count); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } // Test that a break point can be set at an IC call location and survive a GC. TEST(BreakPointConstructCallWithGC) { break_point_hit_count = 0; DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); SetDebugEventListener(env->GetIsolate(), DebugEventBreakPointCollectGarbage); CompileRunChecked(env->GetIsolate(), "function bar(){ this.x = 1;}"); v8::Local<v8::Function> foo = CompileFunction(&env, "function foo(){return new bar(1).x;}", "foo"); v8::Local<v8::Context> context = env.context(); // Run without breakpoints. CHECK_EQ(1, foo->Call(context, env->Global(), 0, NULL) .ToLocalChecked() ->Int32Value(context) .FromJust()); CHECK_EQ(0, break_point_hit_count); // Run with breakpoint. int bp = SetBreakPoint(foo, 0); CHECK_EQ(1, foo->Call(context, env->Global(), 0, NULL) .ToLocalChecked() ->Int32Value(context) .FromJust()); CHECK_EQ(1, break_point_hit_count); CHECK_EQ(1, foo->Call(context, env->Global(), 0, NULL) .ToLocalChecked() ->Int32Value(context) .FromJust()); CHECK_EQ(2, break_point_hit_count); // Run without breakpoints. ClearBreakPoint(bp); foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(2, break_point_hit_count); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } // Test that a break point can be set at a return store location. TEST(BreakPointReturn) { break_point_hit_count = 0; DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); // Create a functions for checking the source line and column when hitting // a break point. frame_source_line = CompileFunction(&env, frame_source_line_source, "frame_source_line"); frame_source_column = CompileFunction(&env, frame_source_column_source, "frame_source_column"); SetDebugEventListener(env->GetIsolate(), DebugEventBreakPointHitCount); v8::Local<v8::Function> foo = CompileFunction(&env, "function foo(){}", "foo"); v8::Local<v8::Context> context = env.context(); // Run without breakpoints. foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(0, break_point_hit_count); // Run with breakpoint int bp = SetBreakPoint(foo, 0); foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(1, break_point_hit_count); CHECK_EQ(0, last_source_line); CHECK_EQ(15, last_source_column); foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(2, break_point_hit_count); CHECK_EQ(0, last_source_line); CHECK_EQ(15, last_source_column); // Run without breakpoints. ClearBreakPoint(bp); foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(2, break_point_hit_count); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } static void CallWithBreakPoints(v8::Local<v8::Context> context, v8::Local<v8::Object> recv, v8::Local<v8::Function> f, int break_point_count, int call_count) { break_point_hit_count = 0; for (int i = 0; i < call_count; i++) { f->Call(context, recv, 0, NULL).ToLocalChecked(); CHECK_EQ((i + 1) * break_point_count, break_point_hit_count); } } // Test GC during break point processing. TEST(GCDuringBreakPointProcessing) { break_point_hit_count = 0; DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); v8::Local<v8::Context> context = env.context(); SetDebugEventListener(env->GetIsolate(), DebugEventBreakPointCollectGarbage); v8::Local<v8::Function> foo; // Test IC store break point with garbage collection. foo = CompileFunction(&env, "function foo(){bar=0;}", "foo"); SetBreakPoint(foo, 0); CallWithBreakPoints(context, env->Global(), foo, 1, 10); // Test IC load break point with garbage collection. foo = CompileFunction(&env, "bar=1;function foo(){var x=bar;}", "foo"); SetBreakPoint(foo, 0); CallWithBreakPoints(context, env->Global(), foo, 1, 10); // Test IC call break point with garbage collection. foo = CompileFunction(&env, "function bar(){};function foo(){bar();}", "foo"); SetBreakPoint(foo, 0); CallWithBreakPoints(context, env->Global(), foo, 1, 10); // Test return break point with garbage collection. foo = CompileFunction(&env, "function foo(){}", "foo"); SetBreakPoint(foo, 0); CallWithBreakPoints(context, env->Global(), foo, 1, 25); // Test debug break slot break point with garbage collection. foo = CompileFunction(&env, "function foo(){var a;}", "foo"); SetBreakPoint(foo, 0); CallWithBreakPoints(context, env->Global(), foo, 1, 25); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } // Call the function three times with different garbage collections in between // and make sure that the break point survives. static void CallAndGC(v8::Local<v8::Context> context, v8::Local<v8::Object> recv, v8::Local<v8::Function> f) { break_point_hit_count = 0; for (int i = 0; i < 3; i++) { // Call function. f->Call(context, recv, 0, NULL).ToLocalChecked(); CHECK_EQ(1 + i * 3, break_point_hit_count); // Scavenge and call function. CcTest::CollectGarbage(v8::internal::NEW_SPACE); f->Call(context, recv, 0, NULL).ToLocalChecked(); CHECK_EQ(2 + i * 3, break_point_hit_count); // Mark sweep (and perhaps compact) and call function. CcTest::CollectAllGarbage(); f->Call(context, recv, 0, NULL).ToLocalChecked(); CHECK_EQ(3 + i * 3, break_point_hit_count); } } // Test that a break point can be set at a return store location. TEST(BreakPointSurviveGC) { break_point_hit_count = 0; DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); v8::Local<v8::Context> context = env.context(); SetDebugEventListener(env->GetIsolate(), DebugEventBreakPointHitCount); v8::Local<v8::Function> foo; // Test IC store break point with garbage collection. { CompileFunction(&env, "function foo(){}", "foo"); foo = CompileFunction(&env, "function foo(){bar=0;}", "foo"); SetBreakPoint(foo, 0); } CallAndGC(context, env->Global(), foo); // Test IC load break point with garbage collection. { CompileFunction(&env, "function foo(){}", "foo"); foo = CompileFunction(&env, "bar=1;function foo(){var x=bar;}", "foo"); SetBreakPoint(foo, 0); } CallAndGC(context, env->Global(), foo); // Test IC call break point with garbage collection. { CompileFunction(&env, "function foo(){}", "foo"); foo = CompileFunction(&env, "function bar(){};function foo(){bar();}", "foo"); SetBreakPoint(foo, 0); } CallAndGC(context, env->Global(), foo); // Test return break point with garbage collection. { CompileFunction(&env, "function foo(){}", "foo"); foo = CompileFunction(&env, "function foo(){}", "foo"); SetBreakPoint(foo, 0); } CallAndGC(context, env->Global(), foo); // Test non IC break point with garbage collection. { CompileFunction(&env, "function foo(){}", "foo"); foo = CompileFunction(&env, "function foo(){var bar=0;}", "foo"); SetBreakPoint(foo, 0); } CallAndGC(context, env->Global(), foo); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } // Test that break points can be set using the global Debug object. TEST(BreakPointThroughJavaScript) { break_point_hit_count = 0; DebugLocalContext env; v8::Isolate* isolate = env->GetIsolate(); v8::HandleScope scope(isolate); v8::Local<v8::Context> context = env.context(); env.ExposeDebug(); SetDebugEventListener(isolate, DebugEventBreakPointHitCount); CompileRunChecked(isolate, "function bar(){}"); CompileFunction(isolate, "function foo(){bar();bar();}", "foo"); // 012345678901234567890 // 1 2 // Break points are set at position 3 and 9 v8::Local<v8::String> source = v8_str(env->GetIsolate(), "foo()"); v8::Local<v8::Script> foo = v8::Script::Compile(context, source).ToLocalChecked(); CHECK_EQ(0, break_point_hit_count); // Run with one breakpoint int bp1 = SetBreakPointFromJS(env->GetIsolate(), "foo", 0, 3); foo->Run(context).ToLocalChecked(); CHECK_EQ(1, break_point_hit_count); foo->Run(context).ToLocalChecked(); CHECK_EQ(2, break_point_hit_count); // Run with two breakpoints int bp2 = SetBreakPointFromJS(env->GetIsolate(), "foo", 0, 9); foo->Run(context).ToLocalChecked(); CHECK_EQ(4, break_point_hit_count); foo->Run(context).ToLocalChecked(); CHECK_EQ(6, break_point_hit_count); // Run with one breakpoint ClearBreakPointFromJS(env->GetIsolate(), bp2); foo->Run(context).ToLocalChecked(); CHECK_EQ(7, break_point_hit_count); foo->Run(context).ToLocalChecked(); CHECK_EQ(8, break_point_hit_count); // Run without breakpoints. ClearBreakPointFromJS(env->GetIsolate(), bp1); foo->Run(context).ToLocalChecked(); CHECK_EQ(8, break_point_hit_count); SetDebugEventListener(isolate, nullptr); CheckDebuggerUnloaded(); // Make sure that the break point numbers are consecutive. CHECK_EQ(1, bp1); CHECK_EQ(2, bp2); } // Test that break points on scripts identified by name can be set using the // global Debug object. TEST(ScriptBreakPointByNameThroughJavaScript) { break_point_hit_count = 0; DebugLocalContext env; v8::Isolate* isolate = env->GetIsolate(); v8::HandleScope scope(isolate); v8::Local<v8::Context> context = env.context(); env.ExposeDebug(); SetDebugEventListener(isolate, DebugEventBreakPointHitCount); v8::Local<v8::String> script = v8_str(isolate, "function f() {\n" " function h() {\n" " a = 0; // line 2\n" " }\n" " b = 1; // line 4\n" " return h();\n" "}\n" "\n" "function g() {\n" " function h() {\n" " a = 0;\n" " }\n" " b = 2; // line 12\n" " h();\n" " b = 3; // line 14\n" " f(); // line 15\n" "}"); // Compile the script and get the two functions. v8::ScriptOrigin origin = v8::ScriptOrigin(v8_str(isolate, "test")); v8::Script::Compile(context, script, &origin) .ToLocalChecked() ->Run(context) .ToLocalChecked(); v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( env->Global()->Get(context, v8_str(isolate, "f")).ToLocalChecked()); v8::Local<v8::Function> g = v8::Local<v8::Function>::Cast( env->Global()->Get(context, v8_str(isolate, "g")).ToLocalChecked()); // Call f and g without break points. break_point_hit_count = 0; f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(0, break_point_hit_count); g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(0, break_point_hit_count); // Call f and g with break point on line 12. int sbp1 = SetScriptBreakPointByNameFromJS(isolate, "test", 12, 0); break_point_hit_count = 0; f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(0, break_point_hit_count); g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(1, break_point_hit_count); // Remove the break point again. break_point_hit_count = 0; ClearBreakPointFromJS(env->GetIsolate(), sbp1); f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(0, break_point_hit_count); g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(0, break_point_hit_count); // Call f and g with break point on line 2. int sbp2 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test", 2, 0); break_point_hit_count = 0; f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(1, break_point_hit_count); g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(2, break_point_hit_count); // Call f and g with break point on line 2, 4, 12, 14 and 15. int sbp3 = SetScriptBreakPointByNameFromJS(isolate, "test", 4, 0); int sbp4 = SetScriptBreakPointByNameFromJS(isolate, "test", 12, 0); int sbp5 = SetScriptBreakPointByNameFromJS(isolate, "test", 14, 0); int sbp6 = SetScriptBreakPointByNameFromJS(isolate, "test", 15, 0); break_point_hit_count = 0; f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(2, break_point_hit_count); g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(7, break_point_hit_count); // Remove all the break points again. break_point_hit_count = 0; ClearBreakPointFromJS(isolate, sbp2); ClearBreakPointFromJS(isolate, sbp3); ClearBreakPointFromJS(isolate, sbp4); ClearBreakPointFromJS(isolate, sbp5); ClearBreakPointFromJS(isolate, sbp6); f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(0, break_point_hit_count); g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(0, break_point_hit_count); SetDebugEventListener(isolate, nullptr); CheckDebuggerUnloaded(); // Make sure that the break point numbers are consecutive. CHECK_EQ(1, sbp1); CHECK_EQ(2, sbp2); CHECK_EQ(3, sbp3); CHECK_EQ(4, sbp4); CHECK_EQ(5, sbp5); CHECK_EQ(6, sbp6); } TEST(ScriptBreakPointByIdThroughJavaScript) { break_point_hit_count = 0; DebugLocalContext env; v8::Isolate* isolate = env->GetIsolate(); v8::HandleScope scope(isolate); v8::Local<v8::Context> context = env.context(); env.ExposeDebug(); SetDebugEventListener(isolate, DebugEventBreakPointHitCount); v8::Local<v8::String> source = v8_str(isolate, "function f() {\n" " function h() {\n" " a = 0; // line 2\n" " }\n" " b = 1; // line 4\n" " return h();\n" "}\n" "\n" "function g() {\n" " function h() {\n" " a = 0;\n" " }\n" " b = 2; // line 12\n" " h();\n" " b = 3; // line 14\n" " f(); // line 15\n" "}"); // Compile the script and get the two functions. v8::ScriptOrigin origin = v8::ScriptOrigin(v8_str(isolate, "test")); v8::Local<v8::Script> script = v8::Script::Compile(context, source, &origin).ToLocalChecked(); script->Run(context).ToLocalChecked(); v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( env->Global()->Get(context, v8_str(isolate, "f")).ToLocalChecked()); v8::Local<v8::Function> g = v8::Local<v8::Function>::Cast( env->Global()->Get(context, v8_str(isolate, "g")).ToLocalChecked()); // Get the script id knowing that internally it is a 32 integer. int script_id = script->GetUnboundScript()->GetId(); // Call f and g without break points. break_point_hit_count = 0; f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(0, break_point_hit_count); g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(0, break_point_hit_count); // Call f and g with break point on line 12. int sbp1 = SetScriptBreakPointByIdFromJS(env->GetIsolate(), script_id, 12, 0); break_point_hit_count = 0; f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(0, break_point_hit_count); g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(1, break_point_hit_count); // Remove the break point again. break_point_hit_count = 0; ClearBreakPointFromJS(env->GetIsolate(), sbp1); f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(0, break_point_hit_count); g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(0, break_point_hit_count); // Call f and g with break point on line 2. int sbp2 = SetScriptBreakPointByIdFromJS(env->GetIsolate(), script_id, 2, 0); break_point_hit_count = 0; f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(1, break_point_hit_count); g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(2, break_point_hit_count); // Call f and g with break point on line 2, 4, 12, 14 and 15. int sbp3 = SetScriptBreakPointByIdFromJS(env->GetIsolate(), script_id, 4, 0); int sbp4 = SetScriptBreakPointByIdFromJS(env->GetIsolate(), script_id, 12, 0); int sbp5 = SetScriptBreakPointByIdFromJS(env->GetIsolate(), script_id, 14, 0); int sbp6 = SetScriptBreakPointByIdFromJS(env->GetIsolate(), script_id, 15, 0); break_point_hit_count = 0; f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(2, break_point_hit_count); g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(7, break_point_hit_count); // Remove all the break points again. break_point_hit_count = 0; ClearBreakPointFromJS(env->GetIsolate(), sbp2); ClearBreakPointFromJS(env->GetIsolate(), sbp3); ClearBreakPointFromJS(env->GetIsolate(), sbp4); ClearBreakPointFromJS(env->GetIsolate(), sbp5); ClearBreakPointFromJS(env->GetIsolate(), sbp6); f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(0, break_point_hit_count); g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(0, break_point_hit_count); SetDebugEventListener(isolate, nullptr); CheckDebuggerUnloaded(); // Make sure that the break point numbers are consecutive. CHECK_EQ(1, sbp1); CHECK_EQ(2, sbp2); CHECK_EQ(3, sbp3); CHECK_EQ(4, sbp4); CHECK_EQ(5, sbp5); CHECK_EQ(6, sbp6); } // Test conditional script break points. TEST(EnableDisableScriptBreakPoint) { break_point_hit_count = 0; DebugLocalContext env; v8::Isolate* isolate = env->GetIsolate(); v8::HandleScope scope(isolate); v8::Local<v8::Context> context = env.context(); env.ExposeDebug(); SetDebugEventListener(isolate, DebugEventBreakPointHitCount); v8::Local<v8::String> script = v8_str(isolate, "function f() {\n" " a = 0; // line 1\n" "};"); // Compile the script and get function f. v8::ScriptOrigin origin = v8::ScriptOrigin(v8_str(isolate, "test")); v8::Script::Compile(context, script, &origin) .ToLocalChecked() ->Run(context) .ToLocalChecked(); v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( env->Global()->Get(context, v8_str(isolate, "f")).ToLocalChecked()); // Set script break point on line 1 (in function f). int sbp = SetScriptBreakPointByNameFromJS(isolate, "test", 1, 0); // Call f while enabeling and disabling the script break point. break_point_hit_count = 0; f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(1, break_point_hit_count); DisableScriptBreakPointFromJS(isolate, sbp); f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(1, break_point_hit_count); EnableScriptBreakPointFromJS(isolate, sbp); f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(2, break_point_hit_count); DisableScriptBreakPointFromJS(isolate, sbp); f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(2, break_point_hit_count); SetDebugEventListener(isolate, nullptr); CheckDebuggerUnloaded(); } // Test conditional script break points. TEST(ConditionalScriptBreakPoint) { break_point_hit_count = 0; DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); env.ExposeDebug(); SetDebugEventListener(env->GetIsolate(), DebugEventBreakPointHitCount); v8::Local<v8::String> script = v8_str(env->GetIsolate(), "count = 0;\n" "function f() {\n" " g(count++); // line 2\n" "};\n" "function g(x) {\n" " var a=x; // line 5\n" "};"); // Compile the script and get function f. v8::Local<v8::Context> context = env.context(); v8::ScriptOrigin origin = v8::ScriptOrigin(v8_str(env->GetIsolate(), "test")); v8::Script::Compile(context, script, &origin) .ToLocalChecked() ->Run(context) .ToLocalChecked(); v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( env->Global() ->Get(context, v8_str(env->GetIsolate(), "f")) .ToLocalChecked()); // Set script break point on line 5 (in function g). int sbp1 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test", 5, 0); // Call f with different conditions on the script break point. break_point_hit_count = 0; ChangeScriptBreakPointConditionFromJS(env->GetIsolate(), sbp1, "false"); f->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(0, break_point_hit_count); ChangeScriptBreakPointConditionFromJS(env->GetIsolate(), sbp1, "true"); break_point_hit_count = 0; f->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(1, break_point_hit_count); ChangeScriptBreakPointConditionFromJS(env->GetIsolate(), sbp1, "x % 2 == 0"); break_point_hit_count = 0; for (int i = 0; i < 10; i++) { f->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked(); } CHECK_EQ(5, break_point_hit_count); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } // Test when several scripts has the same script data TEST(ScriptBreakPointMultiple) { break_point_hit_count = 0; DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); env.ExposeDebug(); SetDebugEventListener(env->GetIsolate(), DebugEventBreakPointHitCount); v8::Local<v8::Context> context = env.context(); v8::Local<v8::Function> f; v8::Local<v8::String> script_f = v8_str(env->GetIsolate(), "function f() {\n" " a = 0; // line 1\n" "}"); v8::Local<v8::Function> g; v8::Local<v8::String> script_g = v8_str(env->GetIsolate(), "function g() {\n" " b = 0; // line 1\n" "}"); v8::ScriptOrigin origin = v8::ScriptOrigin(v8_str(env->GetIsolate(), "test")); // Compile the scripts with same script data and get the functions. v8::Script::Compile(context, script_f, &origin) .ToLocalChecked() ->Run(context) .ToLocalChecked(); f = v8::Local<v8::Function>::Cast( env->Global() ->Get(context, v8_str(env->GetIsolate(), "f")) .ToLocalChecked()); v8::Script::Compile(context, script_g, &origin) .ToLocalChecked() ->Run(context) .ToLocalChecked(); g = v8::Local<v8::Function>::Cast( env->Global() ->Get(context, v8_str(env->GetIsolate(), "g")) .ToLocalChecked()); int sbp = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test", 1, 0); // Call f and g and check that the script break point is active. break_point_hit_count = 0; f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(1, break_point_hit_count); g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(2, break_point_hit_count); // Clear the script break point. ClearBreakPointFromJS(env->GetIsolate(), sbp); // Call f and g and check that the script break point is no longer active. break_point_hit_count = 0; f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(0, break_point_hit_count); g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(0, break_point_hit_count); // Set script break point with the scripts loaded. sbp = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test", 1, 0); // Call f and g and check that the script break point is active. break_point_hit_count = 0; f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(1, break_point_hit_count); g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(2, break_point_hit_count); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } // Test the script origin which has both name and line offset. TEST(ScriptBreakPointLineOffset) { break_point_hit_count = 0; DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); env.ExposeDebug(); SetDebugEventListener(env->GetIsolate(), DebugEventBreakPointHitCount); v8::Local<v8::Context> context = env.context(); v8::Local<v8::Function> f; v8::Local<v8::String> script = v8_str(env->GetIsolate(), "function f() {\n" " a = 0; // line 8 as this script has line offset 7\n" " b = 0; // line 9 as this script has line offset 7\n" "}"); // Create script origin both name and line offset. v8::ScriptOrigin origin(v8_str(env->GetIsolate(), "test.html"), v8::Integer::New(env->GetIsolate(), 7)); // Compile the script and get the function. v8::Script::Compile(context, script, &origin) .ToLocalChecked() ->Run(context) .ToLocalChecked(); int sbp1 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 8, 0); int sbp2 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 9, 0); f = v8::Local<v8::Function>::Cast( env->Global() ->Get(context, v8_str(env->GetIsolate(), "f")) .ToLocalChecked()); // Call f and check that the script break point is active. break_point_hit_count = 0; f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(2, break_point_hit_count); // Clear the script break points. ClearBreakPointFromJS(env->GetIsolate(), sbp1); ClearBreakPointFromJS(env->GetIsolate(), sbp2); // Call f and check that no script break points are active. break_point_hit_count = 0; f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(0, break_point_hit_count); // Set a script break point with the script loaded. sbp1 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 9, 0); // Call f and check that the script break point is active. break_point_hit_count = 0; f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(1, break_point_hit_count); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } // Test script break points set on lines. TEST(ScriptBreakPointLine) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); env.ExposeDebug(); // Create a function for checking the function when hitting a break point. frame_function_name = CompileFunction(&env, frame_function_name_source, "frame_function_name"); SetDebugEventListener(env->GetIsolate(), DebugEventBreakPointHitCount); v8::Local<v8::Context> context = env.context(); v8::Local<v8::Function> f; v8::Local<v8::Function> g; v8::Local<v8::String> script = v8_str(env->GetIsolate(), "a = 0 // line 0\n" "function f() {\n" " a = 1; // line 2\n" "}\n" " a = 2; // line 4\n" " /* xx */ function g() { // line 5\n" " function h() { // line 6\n" " a = 3; // line 7\n" " }\n" " h(); // line 9\n" " a = 4; // line 10\n" " }\n" " a=5; // line 12"); // Compile the script and get the function. break_point_hit_count = 0; v8::ScriptOrigin origin(v8_str(env->GetIsolate(), "test.html"), v8::Integer::New(env->GetIsolate(), 0)); v8::Local<v8::Script> compiled = v8::Script::Compile(context, script, &origin).ToLocalChecked(); int sbp1 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 0, -1); int sbp2 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 1, -1); int sbp3 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 5, -1); compiled->Run(context).ToLocalChecked(); f = v8::Local<v8::Function>::Cast( env->Global() ->Get(context, v8_str(env->GetIsolate(), "f")) .ToLocalChecked()); g = v8::Local<v8::Function>::Cast( env->Global() ->Get(context, v8_str(env->GetIsolate(), "g")) .ToLocalChecked()); // Check that a break point was hit when the script was run. CHECK_EQ(1, break_point_hit_count); CHECK_EQ(0, StrLength(last_function_hit)); // Call f and check that the script break point. f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(2, break_point_hit_count); CHECK_EQ(0, strcmp("f", last_function_hit)); // Call g and check that the script break point. g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(3, break_point_hit_count); CHECK_EQ(0, strcmp("g", last_function_hit)); // Clear the script break point on g and set one on h. ClearBreakPointFromJS(env->GetIsolate(), sbp3); int sbp4 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 6, -1); // Call g and check that the script break point in h is hit. g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(4, break_point_hit_count); CHECK_EQ(0, strcmp("h", last_function_hit)); // Clear break points in f and h. Set a new one in the script between // functions f and g and test that there is no break points in f and g any // more. ClearBreakPointFromJS(env->GetIsolate(), sbp2); ClearBreakPointFromJS(env->GetIsolate(), sbp4); int sbp5 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 4, -1); break_point_hit_count = 0; f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(0, break_point_hit_count); // Set a break point in the code after the last function decleration. int sbp6 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 12, -1); // Reloading the script should not hit any break points. break_point_hit_count = 0; v8::Script::Compile(context, script, &origin) .ToLocalChecked() ->Run(context) .ToLocalChecked(); CHECK_EQ(0, break_point_hit_count); ClearBreakPointFromJS(env->GetIsolate(), sbp1); ClearBreakPointFromJS(env->GetIsolate(), sbp5); ClearBreakPointFromJS(env->GetIsolate(), sbp6); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } // Test top level script break points set on lines. TEST(ScriptBreakPointLineTopLevel) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); env.ExposeDebug(); SetDebugEventListener(env->GetIsolate(), DebugEventBreakPointHitCount); v8::Local<v8::Context> context = env.context(); v8::Local<v8::String> script = v8_str(env->GetIsolate(), "function f() {\n" " a = 1; // line 1\n" "}\n" "a = 2; // line 3\n"); v8::Local<v8::Function> f; { v8::HandleScope scope(env->GetIsolate()); CompileRunWithOrigin(script, "test.html"); } f = v8::Local<v8::Function>::Cast( env->Global() ->Get(context, v8_str(env->GetIsolate(), "f")) .ToLocalChecked()); CcTest::CollectAllGarbage(); SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 3, -1); // Call f and check that there was no break points. break_point_hit_count = 0; f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(0, break_point_hit_count); // Recompile and run script and check that break point was not reapplied. break_point_hit_count = 0; CompileRunWithOrigin(script, "test.html"); CHECK_EQ(0, break_point_hit_count); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } // Test that it is possible to add and remove break points in a top level // function which has no references but has not been collected yet. TEST(ScriptBreakPointTopLevelCrash) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); env.ExposeDebug(); SetDebugEventListener(env->GetIsolate(), DebugEventBreakPointHitCount); CompileRunWithOrigin( "function f() {\n" " return 0;\n" "}\n", "test.html"); int sbp1 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 1, -1); break_point_hit_count = 0; CompileRun("f();"); CHECK_EQ(1, break_point_hit_count); int sbp2 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 3, -1); ClearBreakPointFromJS(env->GetIsolate(), sbp1); ClearBreakPointFromJS(env->GetIsolate(), sbp2); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } // Test that it is possible to remove the last break point for a function // inside the break handling of that break point. TEST(RemoveBreakPointInBreak) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); v8::Local<v8::Context> context = env.context(); v8::Local<v8::Function> foo = CompileFunction(&env, "function foo(){a=1;}", "foo"); // Register the debug event listener pasing the function SetDebugEventListener(env->GetIsolate(), DebugEventRemoveBreakPoint, foo); debug_event_remove_break_point = SetBreakPoint(foo, 0); break_point_hit_count = 0; foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(1, break_point_hit_count); break_point_hit_count = 0; foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(0, break_point_hit_count); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } // Test that the debugger statement causes a break. TEST(DebuggerStatement) { break_point_hit_count = 0; DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); SetDebugEventListener(env->GetIsolate(), DebugEventBreakPointHitCount); v8::Local<v8::Context> context = env.context(); v8::Script::Compile(context, v8_str(env->GetIsolate(), "function bar(){debugger}")) .ToLocalChecked() ->Run(context) .ToLocalChecked(); v8::Script::Compile( context, v8_str(env->GetIsolate(), "function foo(){debugger;debugger;}")) .ToLocalChecked() ->Run(context) .ToLocalChecked(); v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast( env->Global() ->Get(context, v8_str(env->GetIsolate(), "foo")) .ToLocalChecked()); v8::Local<v8::Function> bar = v8::Local<v8::Function>::Cast( env->Global() ->Get(context, v8_str(env->GetIsolate(), "bar")) .ToLocalChecked()); // Run function with debugger statement bar->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(1, break_point_hit_count); // Run function with two debugger statement foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(3, break_point_hit_count); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } // Test setting a breakpoint on the debugger statement. TEST(DebuggerStatementBreakpoint) { break_point_hit_count = 0; DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); v8::Local<v8::Context> context = env.context(); SetDebugEventListener(env->GetIsolate(), DebugEventBreakPointHitCount); v8::Script::Compile(context, v8_str(env->GetIsolate(), "function foo(){debugger;}")) .ToLocalChecked() ->Run(context) .ToLocalChecked(); v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast( env->Global() ->Get(context, v8_str(env->GetIsolate(), "foo")) .ToLocalChecked()); // The debugger statement triggers breakpoint hit foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(1, break_point_hit_count); int bp = SetBreakPoint(foo, 0); // Set breakpoint does not duplicate hits foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(2, break_point_hit_count); ClearBreakPoint(bp); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } // Test that the evaluation of expressions when a break point is hit generates // the correct results. TEST(DebugEvaluate) { DebugLocalContext env; v8::Isolate* isolate = env->GetIsolate(); v8::HandleScope scope(isolate); env.ExposeDebug(); // Create a function for checking the evaluation when hitting a break point. evaluate_check_function = CompileFunction(&env, evaluate_check_source, "evaluate_check"); // Register the debug event listener SetDebugEventListener(isolate, DebugEventEvaluate); // Different expected vaules of x and a when in a break point (u = undefined, // d = Hello, world!). struct EvaluateCheck checks_uu[] = {{"x", v8::Undefined(isolate)}, {"a", v8::Undefined(isolate)}, {NULL, v8::Local<v8::Value>()}}; struct EvaluateCheck checks_hu[] = { {"x", v8_str(env->GetIsolate(), "Hello, world!")}, {"a", v8::Undefined(isolate)}, {NULL, v8::Local<v8::Value>()}}; struct EvaluateCheck checks_hh[] = { {"x", v8_str(env->GetIsolate(), "Hello, world!")}, {"a", v8_str(env->GetIsolate(), "Hello, world!")}, {NULL, v8::Local<v8::Value>()}}; // Simple test function. The "y=0" is in the function foo to provide a break // location. For "y=0" the "y" is at position 15 in the foo function // therefore setting breakpoint at position 15 will break at "y=0" and // setting it higher will break after. v8::Local<v8::Function> foo = CompileFunction(&env, "function foo(x) {" " var a;" " y=0;" // To ensure break location 1. " a=x;" " y=0;" // To ensure break location 2. "}", "foo"); const int foo_break_position_1 = 15; const int foo_break_position_2 = 29; v8::Local<v8::Context> context = env.context(); // Arguments with one parameter "Hello, world!" v8::Local<v8::Value> argv_foo[1] = { v8_str(env->GetIsolate(), "Hello, world!")}; // Call foo with breakpoint set before a=x and undefined as parameter. int bp = SetBreakPoint(foo, foo_break_position_1); checks = checks_uu; foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); // Call foo with breakpoint set before a=x and parameter "Hello, world!". checks = checks_hu; foo->Call(context, env->Global(), 1, argv_foo).ToLocalChecked(); // Call foo with breakpoint set after a=x and parameter "Hello, world!". ClearBreakPoint(bp); SetBreakPoint(foo, foo_break_position_2); checks = checks_hh; foo->Call(context, env->Global(), 1, argv_foo).ToLocalChecked(); // Test that overriding Object.prototype will not interfere into evaluation // on call frame. v8::Local<v8::Function> zoo = CompileFunction(&env, "x = undefined;" "function zoo(t) {" " var a=x;" " Object.prototype.x = 42;" " x=t;" " y=0;" // To ensure break location. " delete Object.prototype.x;" " x=a;" "}", "zoo"); const int zoo_break_position = 50; // Arguments with one parameter "Hello, world!" v8::Local<v8::Value> argv_zoo[1] = { v8_str(env->GetIsolate(), "Hello, world!")}; // Call zoo with breakpoint set at y=0. DebugEventCounterClear(); bp = SetBreakPoint(zoo, zoo_break_position); checks = checks_hu; zoo->Call(context, env->Global(), 1, argv_zoo).ToLocalChecked(); CHECK_EQ(1, break_point_hit_count); ClearBreakPoint(bp); // Test function with an inner function. The "y=0" is in function barbar // to provide a break location. For "y=0" the "y" is at position 8 in the // barbar function therefore setting breakpoint at position 8 will break at // "y=0" and setting it higher will break after. v8::Local<v8::Function> bar = CompileFunction(&env, "y = 0;" "x = 'Goodbye, world!';" "function bar(x, b) {" " var a;" " function barbar() {" " y=0; /* To ensure break location.*/" " a=x;" " };" " debug.Debug.clearAllBreakPoints();" " barbar();" " y=0;a=x;" "}", "bar"); const int barbar_break_position = 8; // Call bar setting breakpoint before a=x in barbar and undefined as // parameter. checks = checks_uu; v8::Local<v8::Value> argv_bar_1[2] = { v8::Undefined(isolate), v8::Number::New(isolate, barbar_break_position)}; bar->Call(context, env->Global(), 2, argv_bar_1).ToLocalChecked(); // Call bar setting breakpoint before a=x in barbar and parameter // "Hello, world!". checks = checks_hu; v8::Local<v8::Value> argv_bar_2[2] = { v8_str(env->GetIsolate(), "Hello, world!"), v8::Number::New(env->GetIsolate(), barbar_break_position)}; bar->Call(context, env->Global(), 2, argv_bar_2).ToLocalChecked(); // Call bar setting breakpoint after a=x in barbar and parameter // "Hello, world!". checks = checks_hh; v8::Local<v8::Value> argv_bar_3[2] = { v8_str(env->GetIsolate(), "Hello, world!"), v8::Number::New(env->GetIsolate(), barbar_break_position + 1)}; bar->Call(context, env->Global(), 2, argv_bar_3).ToLocalChecked(); SetDebugEventListener(isolate, nullptr); CheckDebuggerUnloaded(); } int debugEventCount = 0; static void CheckDebugEvent(const v8::Debug::EventDetails& eventDetails) { if (eventDetails.GetEvent() == v8::Break) ++debugEventCount; } // Test that the conditional breakpoints work event if code generation from // strings is prohibited in the debugee context. TEST(ConditionalBreakpointWithCodeGenerationDisallowed) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); env.ExposeDebug(); SetDebugEventListener(env->GetIsolate(), CheckDebugEvent); v8::Local<v8::Context> context = env.context(); v8::Local<v8::Function> foo = CompileFunction(&env, "function foo(x) {\n" " var s = 'String value2';\n" " return s + x;\n" "}", "foo"); // Set conditional breakpoint with condition 'true'. CompileRun("debug.Debug.setBreakPoint(foo, 2, 0, 'true')"); debugEventCount = 0; env->AllowCodeGenerationFromStrings(false); foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(1, debugEventCount); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } bool checkedDebugEvals = true; v8::Local<v8::Function> checkGlobalEvalFunction; v8::Local<v8::Function> checkFrameEvalFunction; static void CheckDebugEval(const v8::Debug::EventDetails& eventDetails) { if (eventDetails.GetEvent() == v8::Break) { ++debugEventCount; v8::HandleScope handleScope(CcTest::isolate()); v8::Local<v8::Value> args[] = {eventDetails.GetExecutionState()}; CHECK( checkGlobalEvalFunction->Call(eventDetails.GetEventContext(), eventDetails.GetEventContext()->Global(), 1, args) .ToLocalChecked() ->IsTrue()); CHECK(checkFrameEvalFunction->Call(eventDetails.GetEventContext(), eventDetails.GetEventContext()->Global(), 1, args) .ToLocalChecked() ->IsTrue()); } } // Test that the evaluation of expressions when a break point is hit generates // the correct results in case code generation from strings is disallowed in the // debugee context. TEST(DebugEvaluateWithCodeGenerationDisallowed) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); env.ExposeDebug(); SetDebugEventListener(env->GetIsolate(), CheckDebugEval); v8::Local<v8::Context> context = env.context(); v8::Local<v8::Function> foo = CompileFunction(&env, "var global = 'Global';\n" "function foo(x) {\n" " var local = 'Local';\n" " debugger;\n" " return local + x;\n" "}", "foo"); checkGlobalEvalFunction = CompileFunction(&env, "function checkGlobalEval(exec_state) {\n" " return exec_state.evaluateGlobal('global').value() === 'Global';\n" "}", "checkGlobalEval"); checkFrameEvalFunction = CompileFunction(&env, "function checkFrameEval(exec_state) {\n" " return exec_state.frame(0).evaluate('local').value() === 'Local';\n" "}", "checkFrameEval"); debugEventCount = 0; env->AllowCodeGenerationFromStrings(false); foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(1, debugEventCount); checkGlobalEvalFunction.Clear(); checkFrameEvalFunction.Clear(); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } // Simple test of the stepping mechanism using only store ICs. TEST(DebugStepLinear) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); // Create a function for testing stepping. v8::Local<v8::Function> foo = CompileFunction(&env, "function foo(){a=1;b=1;c=1;}", "foo"); // Run foo to allow it to get optimized. CompileRun("a=0; b=0; c=0; foo();"); // Register a debug event listener which steps and counts. SetDebugEventListener(env->GetIsolate(), DebugEventStep); SetBreakPoint(foo, 3); step_action = StepIn; break_point_hit_count = 0; v8::Local<v8::Context> context = env.context(); foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); // With stepping all break locations are hit. CHECK_EQ(4, break_point_hit_count); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); // Register a debug event listener which just counts. SetDebugEventListener(env->GetIsolate(), DebugEventBreakPointHitCount); SetBreakPoint(foo, 3); break_point_hit_count = 0; foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); // Without stepping only active break points are hit. CHECK_EQ(1, break_point_hit_count); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } // Test of the stepping mechanism for keyed load in a loop. TEST(DebugStepKeyedLoadLoop) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); // Register a debug event listener which steps and counts. SetDebugEventListener(env->GetIsolate(), DebugEventStep); // Create a function for testing stepping of keyed load. The statement 'y=1' // is there to have more than one breakable statement in the loop, TODO(315). v8::Local<v8::Function> foo = CompileFunction( &env, "function foo(a) {\n" " var x;\n" " var len = a.length;\n" " for (var i = 0; i < len; i++) {\n" " y = 1;\n" " x = a[i];\n" " }\n" "}\n" "y=0\n", "foo"); v8::Local<v8::Context> context = env.context(); // Create array [0,1,2,3,4,5,6,7,8,9] v8::Local<v8::Array> a = v8::Array::New(env->GetIsolate(), 10); for (int i = 0; i < 10; i++) { CHECK(a->Set(context, v8::Number::New(env->GetIsolate(), i), v8::Number::New(env->GetIsolate(), i)) .FromJust()); } // Call function without any break points to ensure inlining is in place. const int kArgc = 1; v8::Local<v8::Value> args[kArgc] = {a}; foo->Call(context, env->Global(), kArgc, args).ToLocalChecked(); // Set up break point and step through the function. SetBreakPoint(foo, 3); step_action = StepNext; break_point_hit_count = 0; foo->Call(context, env->Global(), kArgc, args).ToLocalChecked(); // With stepping all break locations are hit. CHECK_EQ(44, break_point_hit_count); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } // Test of the stepping mechanism for keyed store in a loop. TEST(DebugStepKeyedStoreLoop) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); // Register a debug event listener which steps and counts. SetDebugEventListener(env->GetIsolate(), DebugEventStep); // Create a function for testing stepping of keyed store. The statement 'y=1' // is there to have more than one breakable statement in the loop, TODO(315). v8::Local<v8::Function> foo = CompileFunction( &env, "function foo(a) {\n" " var len = a.length;\n" " for (var i = 0; i < len; i++) {\n" " y = 1;\n" " a[i] = 42;\n" " }\n" "}\n" "y=0\n", "foo"); v8::Local<v8::Context> context = env.context(); // Create array [0,1,2,3,4,5,6,7,8,9] v8::Local<v8::Array> a = v8::Array::New(env->GetIsolate(), 10); for (int i = 0; i < 10; i++) { CHECK(a->Set(context, v8::Number::New(env->GetIsolate(), i), v8::Number::New(env->GetIsolate(), i)) .FromJust()); } // Call function without any break points to ensure inlining is in place. const int kArgc = 1; v8::Local<v8::Value> args[kArgc] = {a}; foo->Call(context, env->Global(), kArgc, args).ToLocalChecked(); // Set up break point and step through the function. SetBreakPoint(foo, 3); step_action = StepNext; break_point_hit_count = 0; foo->Call(context, env->Global(), kArgc, args).ToLocalChecked(); // With stepping all break locations are hit. CHECK_EQ(44, break_point_hit_count); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } // Test of the stepping mechanism for named load in a loop. TEST(DebugStepNamedLoadLoop) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); // Register a debug event listener which steps and counts. SetDebugEventListener(env->GetIsolate(), DebugEventStep); v8::Local<v8::Context> context = env.context(); // Create a function for testing stepping of named load. v8::Local<v8::Function> foo = CompileFunction( &env, "function foo() {\n" " var a = [];\n" " var s = \"\";\n" " for (var i = 0; i < 10; i++) {\n" " var v = new V(i, i + 1);\n" " v.y;\n" " a.length;\n" // Special case: array length. " s.length;\n" // Special case: string length. " }\n" "}\n" "function V(x, y) {\n" " this.x = x;\n" " this.y = y;\n" "}\n", "foo"); // Call function without any break points to ensure inlining is in place. foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); // Set up break point and step through the function. SetBreakPoint(foo, 4); step_action = StepNext; break_point_hit_count = 0; foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); // With stepping all break locations are hit. CHECK_EQ(65, break_point_hit_count); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } static void DoDebugStepNamedStoreLoop(int expected) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); // Register a debug event listener which steps and counts. SetDebugEventListener(env->GetIsolate(), DebugEventStep); // Create a function for testing stepping of named store. v8::Local<v8::Context> context = env.context(); v8::Local<v8::Function> foo = CompileFunction( &env, "function foo() {\n" " var a = {a:1};\n" " for (var i = 0; i < 10; i++) {\n" " a.a = 2\n" " }\n" "}\n", "foo"); // Call function without any break points to ensure inlining is in place. foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); // Set up break point and step through the function. SetBreakPoint(foo, 3); step_action = StepNext; break_point_hit_count = 0; foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); // With stepping all expected break locations are hit. CHECK_EQ(expected, break_point_hit_count); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } // Test of the stepping mechanism for named load in a loop. TEST(DebugStepNamedStoreLoop) { DoDebugStepNamedStoreLoop(34); } // Test the stepping mechanism with different ICs. TEST(DebugStepLinearMixedICs) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); // Register a debug event listener which steps and counts. SetDebugEventListener(env->GetIsolate(), DebugEventStep); v8::Local<v8::Context> context = env.context(); // Create a function for testing stepping. v8::Local<v8::Function> foo = CompileFunction(&env, "function bar() {};" "function foo() {" " var x;" " var index='name';" " var y = {};" " a=1;b=2;x=a;y[index]=3;x=y[index];bar();}", "foo"); // Run functions to allow them to get optimized. CompileRun("a=0; b=0; bar(); foo();"); SetBreakPoint(foo, 0); step_action = StepIn; break_point_hit_count = 0; foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); // With stepping all break locations are hit. CHECK_EQ(10, break_point_hit_count); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); // Register a debug event listener which just counts. SetDebugEventListener(env->GetIsolate(), DebugEventBreakPointHitCount); SetBreakPoint(foo, 0); break_point_hit_count = 0; foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); // Without stepping only active break points are hit. CHECK_EQ(1, break_point_hit_count); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } TEST(DebugStepDeclarations) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); // Register a debug event listener which steps and counts. SetDebugEventListener(env->GetIsolate(), DebugEventStep); v8::Local<v8::Context> context = env.context(); // Create a function for testing stepping. Run it to allow it to get // optimized. const char* src = "function foo() { " " var a;" " var b = 1;" " var c = foo;" " var d = Math.floor;" " var e = b + d(1.2);" "}" "foo()"; v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); SetBreakPoint(foo, 0); // Stepping through the declarations. step_action = StepIn; break_point_hit_count = 0; foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(5, break_point_hit_count); // Get rid of the debug event listener. SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } TEST(DebugStepLocals) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); // Register a debug event listener which steps and counts. SetDebugEventListener(env->GetIsolate(), DebugEventStep); v8::Local<v8::Context> context = env.context(); // Create a function for testing stepping. Run it to allow it to get // optimized. const char* src = "function foo() { " " var a,b;" " a = 1;" " b = a + 2;" " b = 1 + 2 + 3;" " a = Math.floor(b);" "}" "foo()"; v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); SetBreakPoint(foo, 0); // Stepping through the declarations. step_action = StepIn; break_point_hit_count = 0; foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(5, break_point_hit_count); // Get rid of the debug event listener. SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } TEST(DebugStepIf) { DebugLocalContext env; v8::Isolate* isolate = env->GetIsolate(); v8::HandleScope scope(isolate); // Register a debug event listener which steps and counts. SetDebugEventListener(env->GetIsolate(), DebugEventStep); v8::Local<v8::Context> context = env.context(); // Create a function for testing stepping. Run it to allow it to get // optimized. const int argc = 1; const char* src = "function foo(x) { " " a = 1;" " if (x) {" " b = 1;" " } else {" " c = 1;" " d = 1;" " }" "}" "a=0; b=0; c=0; d=0; foo()"; v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); SetBreakPoint(foo, 0); // Stepping through the true part. step_action = StepIn; break_point_hit_count = 0; v8::Local<v8::Value> argv_true[argc] = {v8::True(isolate)}; foo->Call(context, env->Global(), argc, argv_true).ToLocalChecked(); CHECK_EQ(4, break_point_hit_count); // Stepping through the false part. step_action = StepIn; break_point_hit_count = 0; v8::Local<v8::Value> argv_false[argc] = {v8::False(isolate)}; foo->Call(context, env->Global(), argc, argv_false).ToLocalChecked(); CHECK_EQ(5, break_point_hit_count); // Get rid of the debug event listener. SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } TEST(DebugStepSwitch) { DebugLocalContext env; v8::Isolate* isolate = env->GetIsolate(); v8::HandleScope scope(isolate); // Register a debug event listener which steps and counts. SetDebugEventListener(isolate, DebugEventStep); v8::Local<v8::Context> context = env.context(); // Create a function for testing stepping. Run it to allow it to get // optimized. const int argc = 1; const char* src = "function foo(x) { " " a = 1;" " switch (x) {" " case 1:" " b = 1;" " case 2:" " c = 1;" " break;" " case 3:" " d = 1;" " e = 1;" " f = 1;" " break;" " }" "}" "a=0; b=0; c=0; d=0; e=0; f=0; foo()"; v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); SetBreakPoint(foo, 0); // One case with fall-through. step_action = StepIn; break_point_hit_count = 0; v8::Local<v8::Value> argv_1[argc] = {v8::Number::New(isolate, 1)}; foo->Call(context, env->Global(), argc, argv_1).ToLocalChecked(); CHECK_EQ(6, break_point_hit_count); // Another case. step_action = StepIn; break_point_hit_count = 0; v8::Local<v8::Value> argv_2[argc] = {v8::Number::New(isolate, 2)}; foo->Call(context, env->Global(), argc, argv_2).ToLocalChecked(); CHECK_EQ(5, break_point_hit_count); // Last case. step_action = StepIn; break_point_hit_count = 0; v8::Local<v8::Value> argv_3[argc] = {v8::Number::New(isolate, 3)}; foo->Call(context, env->Global(), argc, argv_3).ToLocalChecked(); CHECK_EQ(7, break_point_hit_count); // Get rid of the debug event listener. SetDebugEventListener(isolate, nullptr); CheckDebuggerUnloaded(); } TEST(DebugStepWhile) { DebugLocalContext env; v8::Isolate* isolate = env->GetIsolate(); v8::HandleScope scope(isolate); // Register a debug event listener which steps and counts. SetDebugEventListener(isolate, DebugEventStep); v8::Local<v8::Context> context = env.context(); // Create a function for testing stepping. Run it to allow it to get // optimized. const int argc = 1; const char* src = "function foo(x) { " " var a = 0;" " while (a < x) {" " a++;" " }" "}" "foo()"; v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); SetBreakPoint(foo, 8); // "var a = 0;" // Looping 0 times. We still should break at the while-condition once. step_action = StepIn; break_point_hit_count = 0; v8::Local<v8::Value> argv_0[argc] = {v8::Number::New(isolate, 0)}; foo->Call(context, env->Global(), argc, argv_0).ToLocalChecked(); CHECK_EQ(3, break_point_hit_count); // Looping 10 times. step_action = StepIn; break_point_hit_count = 0; v8::Local<v8::Value> argv_10[argc] = {v8::Number::New(isolate, 10)}; foo->Call(context, env->Global(), argc, argv_10).ToLocalChecked(); CHECK_EQ(23, break_point_hit_count); // Looping 100 times. step_action = StepIn; break_point_hit_count = 0; v8::Local<v8::Value> argv_100[argc] = {v8::Number::New(isolate, 100)}; foo->Call(context, env->Global(), argc, argv_100).ToLocalChecked(); CHECK_EQ(203, break_point_hit_count); // Get rid of the debug event listener. SetDebugEventListener(isolate, nullptr); CheckDebuggerUnloaded(); } TEST(DebugStepDoWhile) { DebugLocalContext env; v8::Isolate* isolate = env->GetIsolate(); v8::HandleScope scope(isolate); // Register a debug event listener which steps and counts. SetDebugEventListener(isolate, DebugEventStep); v8::Local<v8::Context> context = env.context(); // Create a function for testing stepping. Run it to allow it to get // optimized. const int argc = 1; const char* src = "function foo(x) { " " var a = 0;" " do {" " a++;" " } while (a < x)" "}" "foo()"; v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); SetBreakPoint(foo, 8); // "var a = 0;" // Looping 0 times. step_action = StepIn; break_point_hit_count = 0; v8::Local<v8::Value> argv_0[argc] = {v8::Number::New(isolate, 0)}; foo->Call(context, env->Global(), argc, argv_0).ToLocalChecked(); CHECK_EQ(4, break_point_hit_count); // Looping 10 times. step_action = StepIn; break_point_hit_count = 0; v8::Local<v8::Value> argv_10[argc] = {v8::Number::New(isolate, 10)}; foo->Call(context, env->Global(), argc, argv_10).ToLocalChecked(); CHECK_EQ(22, break_point_hit_count); // Looping 100 times. step_action = StepIn; break_point_hit_count = 0; v8::Local<v8::Value> argv_100[argc] = {v8::Number::New(isolate, 100)}; foo->Call(context, env->Global(), argc, argv_100).ToLocalChecked(); CHECK_EQ(202, break_point_hit_count); // Get rid of the debug event listener. SetDebugEventListener(isolate, nullptr); CheckDebuggerUnloaded(); } TEST(DebugStepFor) { DebugLocalContext env; v8::Isolate* isolate = env->GetIsolate(); v8::HandleScope scope(isolate); // Register a debug event listener which steps and counts. SetDebugEventListener(isolate, DebugEventStep); v8::Local<v8::Context> context = env.context(); // Create a function for testing stepping. Run it to allow it to get // optimized. const int argc = 1; const char* src = "function foo(x) { " " a = 1;" " for (i = 0; i < x; i++) {" " b = 1;" " }" "}" "a=0; b=0; i=0; foo()"; v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); SetBreakPoint(foo, 8); // "a = 1;" // Looping 0 times. step_action = StepIn; break_point_hit_count = 0; v8::Local<v8::Value> argv_0[argc] = {v8::Number::New(isolate, 0)}; foo->Call(context, env->Global(), argc, argv_0).ToLocalChecked(); CHECK_EQ(4, break_point_hit_count); // Looping 10 times. step_action = StepIn; break_point_hit_count = 0; v8::Local<v8::Value> argv_10[argc] = {v8::Number::New(isolate, 10)}; foo->Call(context, env->Global(), argc, argv_10).ToLocalChecked(); CHECK_EQ(34, break_point_hit_count); // Looping 100 times. step_action = StepIn; break_point_hit_count = 0; v8::Local<v8::Value> argv_100[argc] = {v8::Number::New(isolate, 100)}; foo->Call(context, env->Global(), argc, argv_100).ToLocalChecked(); CHECK_EQ(304, break_point_hit_count); // Get rid of the debug event listener. SetDebugEventListener(isolate, nullptr); CheckDebuggerUnloaded(); } TEST(DebugStepForContinue) { DebugLocalContext env; v8::Isolate* isolate = env->GetIsolate(); v8::HandleScope scope(isolate); // Register a debug event listener which steps and counts. SetDebugEventListener(isolate, DebugEventStep); v8::Local<v8::Context> context = env.context(); // Create a function for testing stepping. Run it to allow it to get // optimized. const int argc = 1; const char* src = "function foo(x) { " " var a = 0;" " var b = 0;" " var c = 0;" " for (var i = 0; i < x; i++) {" " a++;" " if (a % 2 == 0) continue;" " b++;" " c++;" " }" " return b;" "}" "foo()"; v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); v8::Local<v8::Value> result; SetBreakPoint(foo, 8); // "var a = 0;" // Each loop generates 4 or 5 steps depending on whether a is equal. // Looping 10 times. step_action = StepIn; break_point_hit_count = 0; v8::Local<v8::Value> argv_10[argc] = {v8::Number::New(isolate, 10)}; result = foo->Call(context, env->Global(), argc, argv_10).ToLocalChecked(); CHECK_EQ(5, result->Int32Value(context).FromJust()); CHECK_EQ(62, break_point_hit_count); // Looping 100 times. step_action = StepIn; break_point_hit_count = 0; v8::Local<v8::Value> argv_100[argc] = {v8::Number::New(isolate, 100)}; result = foo->Call(context, env->Global(), argc, argv_100).ToLocalChecked(); CHECK_EQ(50, result->Int32Value(context).FromJust()); CHECK_EQ(557, break_point_hit_count); // Get rid of the debug event listener. SetDebugEventListener(isolate, nullptr); CheckDebuggerUnloaded(); } TEST(DebugStepForBreak) { DebugLocalContext env; v8::Isolate* isolate = env->GetIsolate(); v8::HandleScope scope(isolate); // Register a debug event listener which steps and counts. SetDebugEventListener(isolate, DebugEventStep); v8::Local<v8::Context> context = env.context(); // Create a function for testing stepping. Run it to allow it to get // optimized. const int argc = 1; const char* src = "function foo(x) { " " var a = 0;" " var b = 0;" " var c = 0;" " for (var i = 0; i < 1000; i++) {" " a++;" " if (a == x) break;" " b++;" " c++;" " }" " return b;" "}" "foo()"; v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); v8::Local<v8::Value> result; SetBreakPoint(foo, 8); // "var a = 0;" // Each loop generates 5 steps except for the last (when break is executed) // which only generates 4. // Looping 10 times. step_action = StepIn; break_point_hit_count = 0; v8::Local<v8::Value> argv_10[argc] = {v8::Number::New(isolate, 10)}; result = foo->Call(context, env->Global(), argc, argv_10).ToLocalChecked(); CHECK_EQ(9, result->Int32Value(context).FromJust()); CHECK_EQ(64, break_point_hit_count); // Looping 100 times. step_action = StepIn; break_point_hit_count = 0; v8::Local<v8::Value> argv_100[argc] = {v8::Number::New(isolate, 100)}; result = foo->Call(context, env->Global(), argc, argv_100).ToLocalChecked(); CHECK_EQ(99, result->Int32Value(context).FromJust()); CHECK_EQ(604, break_point_hit_count); // Get rid of the debug event listener. SetDebugEventListener(isolate, nullptr); CheckDebuggerUnloaded(); } TEST(DebugStepForIn) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); // Register a debug event listener which steps and counts. SetDebugEventListener(env->GetIsolate(), DebugEventStep); v8::Local<v8::Context> context = env.context(); // Create a function for testing stepping. Run it to allow it to get // optimized. v8::Local<v8::Function> foo; const char* src_1 = "function foo() { " " var a = [1, 2];" " for (x in a) {" " b = 0;" " }" "}" "foo()"; foo = CompileFunction(&env, src_1, "foo"); SetBreakPoint(foo, 0); // "var a = ..." step_action = StepIn; break_point_hit_count = 0; foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(8, break_point_hit_count); // Create a function for testing stepping. Run it to allow it to get // optimized. const char* src_2 = "function foo() { " " var a = {a:[1, 2, 3]};" " for (x in a.a) {" " b = 0;" " }" "}" "foo()"; foo = CompileFunction(&env, src_2, "foo"); SetBreakPoint(foo, 0); // "var a = ..." step_action = StepIn; break_point_hit_count = 0; foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(10, break_point_hit_count); // Get rid of the debug event listener. SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } TEST(DebugStepWith) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); // Register a debug event listener which steps and counts. SetDebugEventListener(env->GetIsolate(), DebugEventStep); v8::Local<v8::Context> context = env.context(); // Create a function for testing stepping. Run it to allow it to get // optimized. const char* src = "function foo(x) { " " var a = {};" " with (a) {}" " with (b) {}" "}" "foo()"; CHECK(env->Global() ->Set(context, v8_str(env->GetIsolate(), "b"), v8::Object::New(env->GetIsolate())) .FromJust()); v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); v8::Local<v8::Value> result; SetBreakPoint(foo, 8); // "var a = {};" step_action = StepIn; break_point_hit_count = 0; foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(4, break_point_hit_count); // Get rid of the debug event listener. SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } TEST(DebugConditional) { DebugLocalContext env; v8::Isolate* isolate = env->GetIsolate(); v8::HandleScope scope(isolate); // Register a debug event listener which steps and counts. SetDebugEventListener(isolate, DebugEventStep); v8::Local<v8::Context> context = env.context(); // Create a function for testing stepping. Run it to allow it to get // optimized. const char* src = "function foo(x) { " " return x ? 1 : 2;" "}" "foo()"; v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); SetBreakPoint(foo, 0); // "var a;" step_action = StepIn; break_point_hit_count = 0; foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(2, break_point_hit_count); step_action = StepIn; break_point_hit_count = 0; const int argc = 1; v8::Local<v8::Value> argv_true[argc] = {v8::True(isolate)}; foo->Call(context, env->Global(), argc, argv_true).ToLocalChecked(); CHECK_EQ(2, break_point_hit_count); // Get rid of the debug event listener. SetDebugEventListener(isolate, nullptr); CheckDebuggerUnloaded(); } TEST(StepInOutSimple) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); // Create a function for checking the function when hitting a break point. frame_function_name = CompileFunction(&env, frame_function_name_source, "frame_function_name"); // Register a debug event listener which steps and counts. SetDebugEventListener(env->GetIsolate(), DebugEventStepSequence); v8::Local<v8::Context> context = env.context(); // Create a function for testing stepping. Run it to allow it to get // optimized. const char* src = "function a() {b();c();}; " "function b() {c();}; " "function c() {}; " "a(); b(); c()"; v8::Local<v8::Function> a = CompileFunction(&env, src, "a"); SetBreakPoint(a, 0); // Step through invocation of a with step in. step_action = StepIn; break_point_hit_count = 0; expected_step_sequence = "abcbaca"; a->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(StrLength(expected_step_sequence), break_point_hit_count); // Step through invocation of a with step next. step_action = StepNext; break_point_hit_count = 0; expected_step_sequence = "aaa"; a->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(StrLength(expected_step_sequence), break_point_hit_count); // Step through invocation of a with step out. step_action = StepOut; break_point_hit_count = 0; expected_step_sequence = "a"; a->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(StrLength(expected_step_sequence), break_point_hit_count); // Get rid of the debug event listener. SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } TEST(StepInOutTree) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); // Create a function for checking the function when hitting a break point. frame_function_name = CompileFunction(&env, frame_function_name_source, "frame_function_name"); // Register a debug event listener which steps and counts. SetDebugEventListener(env->GetIsolate(), DebugEventStepSequence); v8::Local<v8::Context> context = env.context(); // Create a function for testing stepping. Run it to allow it to get // optimized. const char* src = "function a() {b(c(d()),d());c(d());d()}; " "function b(x,y) {c();}; " "function c(x) {}; " "function d() {}; " "a(); b(); c(); d()"; v8::Local<v8::Function> a = CompileFunction(&env, src, "a"); SetBreakPoint(a, 0); // Step through invocation of a with step in. step_action = StepIn; break_point_hit_count = 0; expected_step_sequence = "adacadabcbadacada"; a->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(StrLength(expected_step_sequence), break_point_hit_count); // Step through invocation of a with step next. step_action = StepNext; break_point_hit_count = 0; expected_step_sequence = "aaaa"; a->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(StrLength(expected_step_sequence), break_point_hit_count); // Step through invocation of a with step out. step_action = StepOut; break_point_hit_count = 0; expected_step_sequence = "a"; a->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(StrLength(expected_step_sequence), break_point_hit_count); // Get rid of the debug event listener. SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } TEST(StepInOutBranch) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); // Create a function for checking the function when hitting a break point. frame_function_name = CompileFunction(&env, frame_function_name_source, "frame_function_name"); // Register a debug event listener which steps and counts. SetDebugEventListener(env->GetIsolate(), DebugEventStepSequence); v8::Local<v8::Context> context = env.context(); // Create a function for testing stepping. Run it to allow it to get // optimized. const char* src = "function a() {b(false);c();}; " "function b(x) {if(x){c();};}; " "function c() {}; " "a(); b(); c()"; v8::Local<v8::Function> a = CompileFunction(&env, src, "a"); SetBreakPoint(a, 0); // Step through invocation of a. step_action = StepIn; break_point_hit_count = 0; expected_step_sequence = "abbaca"; a->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(StrLength(expected_step_sequence), break_point_hit_count); // Get rid of the debug event listener. SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } // Test that step in does not step into native functions. TEST(DebugStepNatives) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); // Create a function for testing stepping. v8::Local<v8::Function> foo = CompileFunction( &env, "function foo(){debugger;Math.sin(1);}", "foo"); // Register a debug event listener which steps and counts. SetDebugEventListener(env->GetIsolate(), DebugEventStep); v8::Local<v8::Context> context = env.context(); step_action = StepIn; break_point_hit_count = 0; foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); // With stepping all break locations are hit. CHECK_EQ(3, break_point_hit_count); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); // Register a debug event listener which just counts. SetDebugEventListener(env->GetIsolate(), DebugEventBreakPointHitCount); break_point_hit_count = 0; foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); // Without stepping only active break points are hit. CHECK_EQ(1, break_point_hit_count); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } // Test that step in works with function.apply. TEST(DebugStepFunctionApply) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); // Create a function for testing stepping. v8::Local<v8::Function> foo = CompileFunction( &env, "function bar(x, y, z) { if (x == 1) { a = y; b = z; } }" "function foo(){ debugger; bar.apply(this, [1,2,3]); }", "foo"); // Register a debug event listener which steps and counts. SetDebugEventListener(env->GetIsolate(), DebugEventStep); v8::Local<v8::Context> context = env.context(); step_action = StepIn; break_point_hit_count = 0; foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); // With stepping all break locations are hit. CHECK_EQ(7, break_point_hit_count); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); // Register a debug event listener which just counts. SetDebugEventListener(env->GetIsolate(), DebugEventBreakPointHitCount); break_point_hit_count = 0; foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); // Without stepping only the debugger statement is hit. CHECK_EQ(1, break_point_hit_count); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } // Test that step in works with function.call. TEST(DebugStepFunctionCall) { DebugLocalContext env; v8::Isolate* isolate = env->GetIsolate(); v8::HandleScope scope(isolate); v8::Local<v8::Context> context = env.context(); // Create a function for testing stepping. v8::Local<v8::Function> foo = CompileFunction( &env, "function bar(x, y, z) { if (x == 1) { a = y; b = z; } }" "function foo(a){ debugger;" " if (a) {" " bar.call(this, 1, 2, 3);" " } else {" " bar.call(this, 0);" " }" "}", "foo"); // Register a debug event listener which steps and counts. SetDebugEventListener(isolate, DebugEventStep); step_action = StepIn; // Check stepping where the if condition in bar is false. break_point_hit_count = 0; foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(6, break_point_hit_count); // Check stepping where the if condition in bar is true. break_point_hit_count = 0; const int argc = 1; v8::Local<v8::Value> argv[argc] = {v8::True(isolate)}; foo->Call(context, env->Global(), argc, argv).ToLocalChecked(); CHECK_EQ(8, break_point_hit_count); SetDebugEventListener(isolate, nullptr); CheckDebuggerUnloaded(); // Register a debug event listener which just counts. SetDebugEventListener(isolate, DebugEventBreakPointHitCount); break_point_hit_count = 0; foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); // Without stepping only the debugger statement is hit. CHECK_EQ(1, break_point_hit_count); SetDebugEventListener(isolate, nullptr); CheckDebuggerUnloaded(); } // Test that step in works with Function.call.apply. TEST(DebugStepFunctionCallApply) { DebugLocalContext env; v8::Isolate* isolate = env->GetIsolate(); v8::HandleScope scope(isolate); v8::Local<v8::Context> context = env.context(); // Create a function for testing stepping. v8::Local<v8::Function> foo = CompileFunction(&env, "function bar() { }" "function foo(){ debugger;" " Function.call.apply(bar);" " Function.call.apply(Function.call, " "[Function.call, bar]);" "}", "foo"); // Register a debug event listener which steps and counts. SetDebugEventListener(isolate, DebugEventStep); step_action = StepIn; break_point_hit_count = 0; foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(6, break_point_hit_count); SetDebugEventListener(isolate, nullptr); CheckDebuggerUnloaded(); // Register a debug event listener which just counts. SetDebugEventListener(isolate, DebugEventBreakPointHitCount); break_point_hit_count = 0; foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); // Without stepping only the debugger statement is hit. CHECK_EQ(1, break_point_hit_count); SetDebugEventListener(isolate, nullptr); CheckDebuggerUnloaded(); } // Tests that breakpoint will be hit if it's set in script. TEST(PauseInScript) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); env.ExposeDebug(); // Register a debug event listener which counts. SetDebugEventListener(env->GetIsolate(), DebugEventCounter); v8::Local<v8::Context> context = env.context(); // Create a script that returns a function. const char* src = "(function (evt) {})"; const char* script_name = "StepInHandlerTest"; v8::ScriptOrigin origin(v8_str(env->GetIsolate(), script_name), v8::Integer::New(env->GetIsolate(), 0)); v8::Local<v8::Script> script = v8::Script::Compile(context, v8_str(env->GetIsolate(), src), &origin) .ToLocalChecked(); // Set breakpoint in the script. SetScriptBreakPointByNameFromJS(env->GetIsolate(), script_name, 0, -1); break_point_hit_count = 0; v8::Local<v8::Value> r = script->Run(context).ToLocalChecked(); CHECK(r->IsFunction()); CHECK_EQ(1, break_point_hit_count); // Get rid of the debug event listener. SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } static void DebugEventCounterCheck(int caught, int uncaught, int message) { CHECK_EQ(caught, exception_hit_count); CHECK_EQ(uncaught, uncaught_exception_hit_count); CHECK_EQ(message, message_callback_count); } // Test break on exceptions. For each exception break combination the number // of debug event exception callbacks and message callbacks are collected. The // number of debug event exception callbacks are used to check that the // debugger is called correctly and the number of message callbacks is used to // check that uncaught exceptions are still returned even if there is a break // for them. TEST(BreakOnException) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); env.ExposeDebug(); v8::Local<v8::Context> context = env.context(); // Create functions for testing break on exception. CompileFunction(&env, "function throws(){throw 1;}", "throws"); v8::Local<v8::Function> caught = CompileFunction(&env, "function caught(){try {throws();} catch(e) {};}", "caught"); v8::Local<v8::Function> notCaught = CompileFunction(&env, "function notCaught(){throws();}", "notCaught"); v8::Local<v8::Function> notCaughtFinally = CompileFunction( &env, "function notCaughtFinally(){try{throws();}finally{}}", "notCaughtFinally"); // In this edge case, even though this finally does not propagate the // exception, the debugger considers this uncaught, since we want to break // at the first throw for the general case where finally implicitly rethrows. v8::Local<v8::Function> edgeCaseFinally = CompileFunction( &env, "function caughtFinally(){L:try{throws();}finally{break L;}}", "caughtFinally"); env->GetIsolate()->AddMessageListener(MessageCallbackCount); SetDebugEventListener(env->GetIsolate(), DebugEventCounter); // Initial state should be no break on exceptions. DebugEventCounterClear(); MessageCallbackCountClear(); caught->Call(context, env->Global(), 0, NULL).ToLocalChecked(); DebugEventCounterCheck(0, 0, 0); CHECK(notCaught->Call(context, env->Global(), 0, NULL).IsEmpty()); DebugEventCounterCheck(0, 0, 1); CHECK(notCaughtFinally->Call(context, env->Global(), 0, NULL).IsEmpty()); DebugEventCounterCheck(0, 0, 2); edgeCaseFinally->Call(context, env->Global(), 0, NULL).ToLocalChecked(); DebugEventCounterCheck(0, 0, 2); // No break on exception DebugEventCounterClear(); MessageCallbackCountClear(); ChangeBreakOnException(false, false); caught->Call(context, env->Global(), 0, NULL).ToLocalChecked(); DebugEventCounterCheck(0, 0, 0); CHECK(notCaught->Call(context, env->Global(), 0, NULL).IsEmpty()); DebugEventCounterCheck(0, 0, 1); CHECK(notCaughtFinally->Call(context, env->Global(), 0, NULL).IsEmpty()); DebugEventCounterCheck(0, 0, 2); edgeCaseFinally->Call(context, env->Global(), 0, NULL).ToLocalChecked(); DebugEventCounterCheck(0, 0, 2); // Break on uncaught exception DebugEventCounterClear(); MessageCallbackCountClear(); ChangeBreakOnException(false, true); caught->Call(context, env->Global(), 0, NULL).ToLocalChecked(); DebugEventCounterCheck(0, 0, 0); CHECK(notCaught->Call(context, env->Global(), 0, NULL).IsEmpty()); DebugEventCounterCheck(1, 1, 1); CHECK(notCaughtFinally->Call(context, env->Global(), 0, NULL).IsEmpty()); DebugEventCounterCheck(2, 2, 2); edgeCaseFinally->Call(context, env->Global(), 0, NULL).ToLocalChecked(); DebugEventCounterCheck(3, 3, 2); // Break on exception and uncaught exception DebugEventCounterClear(); MessageCallbackCountClear(); ChangeBreakOnException(true, true); caught->Call(context, env->Global(), 0, NULL).ToLocalChecked(); DebugEventCounterCheck(1, 0, 0); CHECK(notCaught->Call(context, env->Global(), 0, NULL).IsEmpty()); DebugEventCounterCheck(2, 1, 1); CHECK(notCaughtFinally->Call(context, env->Global(), 0, NULL).IsEmpty()); DebugEventCounterCheck(3, 2, 2); edgeCaseFinally->Call(context, env->Global(), 0, NULL).ToLocalChecked(); DebugEventCounterCheck(4, 3, 2); // Break on exception DebugEventCounterClear(); MessageCallbackCountClear(); ChangeBreakOnException(true, false); caught->Call(context, env->Global(), 0, NULL).ToLocalChecked(); DebugEventCounterCheck(1, 0, 0); CHECK(notCaught->Call(context, env->Global(), 0, NULL).IsEmpty()); DebugEventCounterCheck(2, 1, 1); CHECK(notCaughtFinally->Call(context, env->Global(), 0, NULL).IsEmpty()); DebugEventCounterCheck(3, 2, 2); edgeCaseFinally->Call(context, env->Global(), 0, NULL).ToLocalChecked(); DebugEventCounterCheck(4, 3, 2); // No break on exception using JavaScript DebugEventCounterClear(); MessageCallbackCountClear(); ChangeBreakOnExceptionFromJS(env->GetIsolate(), false, false); caught->Call(context, env->Global(), 0, NULL).ToLocalChecked(); DebugEventCounterCheck(0, 0, 0); CHECK(notCaught->Call(context, env->Global(), 0, NULL).IsEmpty()); DebugEventCounterCheck(0, 0, 1); CHECK(notCaughtFinally->Call(context, env->Global(), 0, NULL).IsEmpty()); DebugEventCounterCheck(0, 0, 2); edgeCaseFinally->Call(context, env->Global(), 0, NULL).ToLocalChecked(); DebugEventCounterCheck(0, 0, 2); // Break on uncaught exception using JavaScript DebugEventCounterClear(); MessageCallbackCountClear(); ChangeBreakOnExceptionFromJS(env->GetIsolate(), false, true); caught->Call(context, env->Global(), 0, NULL).ToLocalChecked(); DebugEventCounterCheck(0, 0, 0); CHECK(notCaught->Call(context, env->Global(), 0, NULL).IsEmpty()); DebugEventCounterCheck(1, 1, 1); CHECK(notCaughtFinally->Call(context, env->Global(), 0, NULL).IsEmpty()); DebugEventCounterCheck(2, 2, 2); edgeCaseFinally->Call(context, env->Global(), 0, NULL).ToLocalChecked(); DebugEventCounterCheck(3, 3, 2); // Break on exception and uncaught exception using JavaScript DebugEventCounterClear(); MessageCallbackCountClear(); ChangeBreakOnExceptionFromJS(env->GetIsolate(), true, true); caught->Call(context, env->Global(), 0, NULL).ToLocalChecked(); DebugEventCounterCheck(1, 0, 0); CHECK(notCaught->Call(context, env->Global(), 0, NULL).IsEmpty()); DebugEventCounterCheck(2, 1, 1); CHECK(notCaughtFinally->Call(context, env->Global(), 0, NULL).IsEmpty()); DebugEventCounterCheck(3, 2, 2); edgeCaseFinally->Call(context, env->Global(), 0, NULL).ToLocalChecked(); DebugEventCounterCheck(4, 3, 2); // Break on exception using JavaScript DebugEventCounterClear(); MessageCallbackCountClear(); ChangeBreakOnExceptionFromJS(env->GetIsolate(), true, false); caught->Call(context, env->Global(), 0, NULL).ToLocalChecked(); DebugEventCounterCheck(1, 0, 0); CHECK(notCaught->Call(context, env->Global(), 0, NULL).IsEmpty()); DebugEventCounterCheck(2, 1, 1); CHECK(notCaughtFinally->Call(context, env->Global(), 0, NULL).IsEmpty()); DebugEventCounterCheck(3, 2, 2); edgeCaseFinally->Call(context, env->Global(), 0, NULL).ToLocalChecked(); DebugEventCounterCheck(4, 3, 2); // No break on exception using native API DebugEventCounterClear(); MessageCallbackCountClear(); ChangeBreakOnExceptionFromAPI(env->GetIsolate(), v8::debug::NoBreakOnException); caught->Call(context, env->Global(), 0, NULL).ToLocalChecked(); DebugEventCounterCheck(0, 0, 0); CHECK(notCaught->Call(context, env->Global(), 0, NULL).IsEmpty()); DebugEventCounterCheck(0, 0, 1); CHECK(notCaughtFinally->Call(context, env->Global(), 0, NULL).IsEmpty()); DebugEventCounterCheck(0, 0, 2); edgeCaseFinally->Call(context, env->Global(), 0, NULL).ToLocalChecked(); DebugEventCounterCheck(0, 0, 2); // // Break on uncaught exception using native API DebugEventCounterClear(); MessageCallbackCountClear(); ChangeBreakOnExceptionFromAPI(env->GetIsolate(), v8::debug::BreakOnUncaughtException); caught->Call(context, env->Global(), 0, NULL).ToLocalChecked(); DebugEventCounterCheck(0, 0, 0); CHECK(notCaught->Call(context, env->Global(), 0, NULL).IsEmpty()); DebugEventCounterCheck(1, 1, 1); CHECK(notCaughtFinally->Call(context, env->Global(), 0, NULL).IsEmpty()); DebugEventCounterCheck(2, 2, 2); edgeCaseFinally->Call(context, env->Global(), 0, NULL).ToLocalChecked(); DebugEventCounterCheck(3, 3, 2); // // Break on exception and uncaught exception using native API DebugEventCounterClear(); MessageCallbackCountClear(); ChangeBreakOnExceptionFromAPI(env->GetIsolate(), v8::debug::BreakOnAnyException); caught->Call(context, env->Global(), 0, NULL).ToLocalChecked(); DebugEventCounterCheck(1, 0, 0); CHECK(notCaught->Call(context, env->Global(), 0, NULL).IsEmpty()); DebugEventCounterCheck(2, 1, 1); CHECK(notCaughtFinally->Call(context, env->Global(), 0, NULL).IsEmpty()); DebugEventCounterCheck(3, 2, 2); edgeCaseFinally->Call(context, env->Global(), 0, NULL).ToLocalChecked(); DebugEventCounterCheck(4, 3, 2); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); env->GetIsolate()->RemoveMessageListeners(MessageCallbackCount); } static void try_finally_original_message(v8::Local<v8::Message> message, v8::Local<v8::Value> data) { v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext(); CHECK_EQ(2, message->GetLineNumber(context).FromJust()); CHECK_EQ(2, message->GetStartColumn(context).FromJust()); message_callback_count++; } TEST(TryFinallyOriginalMessage) { // Test that the debugger plays nicely with the pending message. message_callback_count = 0; DebugEventCounterClear(); DebugLocalContext env; v8::Isolate* isolate = CcTest::isolate(); isolate->AddMessageListener(try_finally_original_message); SetDebugEventListener(isolate, DebugEventCounter); ChangeBreakOnException(true, true); v8::HandleScope scope(isolate); CompileRun( "try {\n" " throw 1;\n" "} finally {\n" "}\n"); DebugEventCounterCheck(1, 1, 1); SetDebugEventListener(isolate, nullptr); isolate->RemoveMessageListeners(try_finally_original_message); } // Test break on exception from compiler errors. When compiling using // v8::Script::Compile there is no JavaScript stack whereas when compiling using // eval there are JavaScript frames. TEST(BreakOnCompileException) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); v8::Local<v8::Context> context = env.context(); // For this test, we want to break on uncaught exceptions: ChangeBreakOnException(false, true); // Create a function for checking the function when hitting a break point. frame_count = CompileFunction(&env, frame_count_source, "frame_count"); env->GetIsolate()->AddMessageListener(MessageCallbackCount); SetDebugEventListener(env->GetIsolate(), DebugEventCounter); DebugEventCounterClear(); MessageCallbackCountClear(); // Check initial state. CHECK_EQ(0, exception_hit_count); CHECK_EQ(0, uncaught_exception_hit_count); CHECK_EQ(0, message_callback_count); CHECK_EQ(-1, last_js_stack_height); // Throws SyntaxError: Unexpected end of input CHECK( v8::Script::Compile(context, v8_str(env->GetIsolate(), "+++")).IsEmpty()); // Exceptions with no stack are skipped. CHECK_EQ(0, exception_hit_count); CHECK_EQ(0, uncaught_exception_hit_count); CHECK_EQ(1, message_callback_count); CHECK_EQ(0, last_js_stack_height); // No JavaScript stack. // Throws SyntaxError: Unexpected identifier CHECK( v8::Script::Compile(context, v8_str(env->GetIsolate(), "x x")).IsEmpty()); // Exceptions with no stack are skipped. CHECK_EQ(0, exception_hit_count); CHECK_EQ(0, uncaught_exception_hit_count); CHECK_EQ(2, message_callback_count); CHECK_EQ(0, last_js_stack_height); // No JavaScript stack. // Throws SyntaxError: Unexpected end of input CHECK(v8::Script::Compile(context, v8_str(env->GetIsolate(), "eval('+++')")) .ToLocalChecked() ->Run(context) .IsEmpty()); CHECK_EQ(1, exception_hit_count); CHECK_EQ(1, uncaught_exception_hit_count); CHECK_EQ(3, message_callback_count); CHECK_EQ(1, last_js_stack_height); // Throws SyntaxError: Unexpected identifier CHECK(v8::Script::Compile(context, v8_str(env->GetIsolate(), "eval('x x')")) .ToLocalChecked() ->Run(context) .IsEmpty()); CHECK_EQ(2, exception_hit_count); CHECK_EQ(2, uncaught_exception_hit_count); CHECK_EQ(4, message_callback_count); CHECK_EQ(1, last_js_stack_height); } TEST(StepWithException) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); // For this test, we want to break on uncaught exceptions: ChangeBreakOnException(false, true); // Create a function for checking the function when hitting a break point. frame_function_name = CompileFunction(&env, frame_function_name_source, "frame_function_name"); // Register a debug event listener which steps and counts. SetDebugEventListener(env->GetIsolate(), DebugEventStepSequence); v8::Local<v8::Context> context = env.context(); // Create functions for testing stepping. const char* src = "function a() { n(); }; " "function b() { c(); }; " "function c() { n(); }; " "function d() { x = 1; try { e(); } catch(x) { x = 2; } }; " "function e() { n(); }; " "function f() { x = 1; try { g(); } catch(x) { x = 2; } }; " "function g() { h(); }; " "function h() { x = 1; throw 1; }; "; // Step through invocation of a. ClearStepping(); v8::Local<v8::Function> a = CompileFunction(&env, src, "a"); SetBreakPoint(a, 0); step_action = StepIn; break_point_hit_count = 0; expected_step_sequence = "aa"; CHECK(a->Call(context, env->Global(), 0, NULL).IsEmpty()); CHECK_EQ(StrLength(expected_step_sequence), break_point_hit_count); // Step through invocation of b + c. ClearStepping(); v8::Local<v8::Function> b = CompileFunction(&env, src, "b"); SetBreakPoint(b, 0); step_action = StepIn; break_point_hit_count = 0; expected_step_sequence = "bcc"; CHECK(b->Call(context, env->Global(), 0, NULL).IsEmpty()); CHECK_EQ(StrLength(expected_step_sequence), break_point_hit_count); // Step through invocation of d + e. ClearStepping(); v8::Local<v8::Function> d = CompileFunction(&env, src, "d"); SetBreakPoint(d, 0); ChangeBreakOnException(false, true); step_action = StepIn; break_point_hit_count = 0; expected_step_sequence = "ddedd"; d->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(StrLength(expected_step_sequence), break_point_hit_count); // Step through invocation of d + e now with break on caught exceptions. ChangeBreakOnException(true, true); step_action = StepIn; break_point_hit_count = 0; expected_step_sequence = "ddeedd"; d->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(StrLength(expected_step_sequence), break_point_hit_count); // Step through invocation of f + g + h. ClearStepping(); v8::Local<v8::Function> f = CompileFunction(&env, src, "f"); SetBreakPoint(f, 0); ChangeBreakOnException(false, true); step_action = StepIn; break_point_hit_count = 0; expected_step_sequence = "ffghhff"; f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(StrLength(expected_step_sequence), break_point_hit_count); // Step through invocation of f + g + h now with break on caught exceptions. ChangeBreakOnException(true, true); step_action = StepIn; break_point_hit_count = 0; expected_step_sequence = "ffghhhff"; f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(StrLength(expected_step_sequence), break_point_hit_count); // Get rid of the debug event listener. SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } TEST(DebugBreak) { i::FLAG_stress_compaction = false; #ifdef VERIFY_HEAP i::FLAG_verify_heap = true; #endif DebugLocalContext env; v8::Isolate* isolate = env->GetIsolate(); v8::HandleScope scope(isolate); // Register a debug event listener which sets the break flag and counts. SetDebugEventListener(isolate, DebugEventBreak); v8::Local<v8::Context> context = env.context(); // Create a function for testing stepping. const char* src = "function f0() {}" "function f1(x1) {}" "function f2(x1,x2) {}" "function f3(x1,x2,x3) {}"; v8::Local<v8::Function> f0 = CompileFunction(&env, src, "f0"); v8::Local<v8::Function> f1 = CompileFunction(&env, src, "f1"); v8::Local<v8::Function> f2 = CompileFunction(&env, src, "f2"); v8::Local<v8::Function> f3 = CompileFunction(&env, src, "f3"); // Call the function to make sure it is compiled. v8::Local<v8::Value> argv[] = { v8::Number::New(isolate, 1), v8::Number::New(isolate, 1), v8::Number::New(isolate, 1), v8::Number::New(isolate, 1)}; // Call all functions to make sure that they are compiled. f0->Call(context, env->Global(), 0, NULL).ToLocalChecked(); f1->Call(context, env->Global(), 0, NULL).ToLocalChecked(); f2->Call(context, env->Global(), 0, NULL).ToLocalChecked(); f3->Call(context, env->Global(), 0, NULL).ToLocalChecked(); // Set the debug break flag. v8::debug::DebugBreak(env->GetIsolate()); // Call all functions with different argument count. break_point_hit_count = 0; for (unsigned int i = 0; i < arraysize(argv); i++) { f0->Call(context, env->Global(), i, argv).ToLocalChecked(); f1->Call(context, env->Global(), i, argv).ToLocalChecked(); f2->Call(context, env->Global(), i, argv).ToLocalChecked(); f3->Call(context, env->Global(), i, argv).ToLocalChecked(); } // One break for each function called. CHECK(4 * arraysize(argv) == break_point_hit_count); // Get rid of the debug event listener. SetDebugEventListener(isolate, nullptr); CheckDebuggerUnloaded(); } // Test to ensure that JavaScript code keeps running while the debug break // through the stack limit flag is set but breaks are disabled. TEST(DisableBreak) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); // Register a debug event listener which sets the break flag and counts. SetDebugEventListener(env->GetIsolate(), DebugEventCounter); v8::Local<v8::Context> context = env.context(); // Create a function for testing stepping. const char* src = "function f() {g()};function g(){i=0; while(i<10){i++}}"; v8::Local<v8::Function> f = CompileFunction(&env, src, "f"); // Set, test and cancel debug break. v8::debug::DebugBreak(env->GetIsolate()); v8::debug::CancelDebugBreak(env->GetIsolate()); // Set the debug break flag. v8::debug::DebugBreak(env->GetIsolate()); // Call all functions with different argument count. break_point_hit_count = 0; f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(1, break_point_hit_count); { v8::debug::DebugBreak(env->GetIsolate()); i::Isolate* isolate = reinterpret_cast<i::Isolate*>(env->GetIsolate()); v8::internal::DisableBreak disable_break(isolate->debug()); f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(1, break_point_hit_count); } f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(2, break_point_hit_count); // Get rid of the debug event listener. SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } TEST(DisableDebuggerStatement) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); // Register a debug event listener which sets the break flag and counts. SetDebugEventListener(env->GetIsolate(), DebugEventCounter); CompileRun("debugger;"); CHECK_EQ(1, break_point_hit_count); // Check that we ignore debugger statement when breakpoints aren't active. i::Isolate* isolate = reinterpret_cast<i::Isolate*>(env->GetIsolate()); isolate->debug()->set_break_points_active(false); CompileRun("debugger;"); CHECK_EQ(1, break_point_hit_count); } static const char* kSimpleExtensionSource = "(function Foo() {" " return 4;" "})() "; // http://crbug.com/28933 // Test that debug break is disabled when bootstrapper is active. TEST(NoBreakWhenBootstrapping) { v8::Isolate* isolate = CcTest::isolate(); v8::HandleScope scope(isolate); // Register a debug event listener which sets the break flag and counts. SetDebugEventListener(isolate, DebugEventCounter); // Set the debug break flag. v8::debug::DebugBreak(isolate); break_point_hit_count = 0; { // Create a context with an extension to make sure that some JavaScript // code is executed during bootstrapping. v8::RegisterExtension(new v8::Extension("simpletest", kSimpleExtensionSource)); const char* extension_names[] = { "simpletest" }; v8::ExtensionConfiguration extensions(1, extension_names); v8::HandleScope handle_scope(isolate); v8::Context::New(isolate, &extensions); } // Check that no DebugBreak events occurred during the context creation. CHECK_EQ(0, break_point_hit_count); // Get rid of the debug event listener. SetDebugEventListener(isolate, nullptr); CheckDebuggerUnloaded(); } static void NamedEnum(const v8::PropertyCallbackInfo<v8::Array>& info) { v8::Local<v8::Array> result = v8::Array::New(info.GetIsolate(), 3); v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext(); CHECK(result->Set(context, v8::Integer::New(info.GetIsolate(), 0), v8_str(info.GetIsolate(), "a")) .FromJust()); CHECK(result->Set(context, v8::Integer::New(info.GetIsolate(), 1), v8_str(info.GetIsolate(), "b")) .FromJust()); CHECK(result->Set(context, v8::Integer::New(info.GetIsolate(), 2), v8_str(info.GetIsolate(), "c")) .FromJust()); info.GetReturnValue().Set(result); } static void IndexedEnum(const v8::PropertyCallbackInfo<v8::Array>& info) { v8::Isolate* isolate = info.GetIsolate(); v8::Local<v8::Array> result = v8::Array::New(isolate, 2); v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext(); CHECK(result->Set(context, v8::Integer::New(isolate, 0), v8::Number::New(isolate, 1)) .FromJust()); CHECK(result->Set(context, v8::Integer::New(isolate, 1), v8::Number::New(isolate, 10)) .FromJust()); info.GetReturnValue().Set(result); } static void NamedGetter(v8::Local<v8::Name> name, const v8::PropertyCallbackInfo<v8::Value>& info) { if (name->IsSymbol()) return; v8::String::Utf8Value n(CcTest::isolate(), v8::Local<v8::String>::Cast(name)); if (strcmp(*n, "a") == 0) { info.GetReturnValue().Set(v8_str(info.GetIsolate(), "AA")); return; } else if (strcmp(*n, "b") == 0) { info.GetReturnValue().Set(v8_str(info.GetIsolate(), "BB")); return; } else if (strcmp(*n, "c") == 0) { info.GetReturnValue().Set(v8_str(info.GetIsolate(), "CC")); return; } else { info.GetReturnValue().SetUndefined(); return; } info.GetReturnValue().Set(name); } static void IndexedGetter(uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info) { info.GetReturnValue().Set(static_cast<double>(index + 1)); } TEST(InterceptorPropertyMirror) { // Create a V8 environment with debug access. DebugLocalContext env; v8::Isolate* isolate = env->GetIsolate(); v8::HandleScope scope(isolate); env.ExposeDebug(); v8::Local<v8::Context> context = env.context(); // Create object with named interceptor. v8::Local<v8::ObjectTemplate> named = v8::ObjectTemplate::New(isolate); named->SetHandler(v8::NamedPropertyHandlerConfiguration( NamedGetter, NULL, NULL, NULL, NamedEnum)); CHECK(env->Global() ->Set(context, v8_str(isolate, "intercepted_named"), named->NewInstance(context).ToLocalChecked()) .FromJust()); // Create object with indexed interceptor. v8::Local<v8::ObjectTemplate> indexed = v8::ObjectTemplate::New(isolate); indexed->SetHandler(v8::IndexedPropertyHandlerConfiguration( IndexedGetter, NULL, NULL, NULL, IndexedEnum)); CHECK(env->Global() ->Set(context, v8_str(isolate, "intercepted_indexed"), indexed->NewInstance(context).ToLocalChecked()) .FromJust()); // Create object with both named and indexed interceptor. v8::Local<v8::ObjectTemplate> both = v8::ObjectTemplate::New(isolate); both->SetHandler(v8::NamedPropertyHandlerConfiguration( NamedGetter, NULL, NULL, NULL, NamedEnum)); both->SetHandler(v8::IndexedPropertyHandlerConfiguration( IndexedGetter, NULL, NULL, NULL, IndexedEnum)); CHECK(env->Global() ->Set(context, v8_str(isolate, "intercepted_both"), both->NewInstance(context).ToLocalChecked()) .FromJust()); // Get mirrors for the three objects with interceptor. CompileRun( "var named_mirror = debug.MakeMirror(intercepted_named);" "var indexed_mirror = debug.MakeMirror(intercepted_indexed);" "var both_mirror = debug.MakeMirror(intercepted_both)"); CHECK(CompileRun("named_mirror instanceof debug.ObjectMirror") ->BooleanValue(context) .FromJust()); CHECK(CompileRun("indexed_mirror instanceof debug.ObjectMirror") ->BooleanValue(context) .FromJust()); CHECK(CompileRun("both_mirror instanceof debug.ObjectMirror") ->BooleanValue(context) .FromJust()); // Get the property names from the interceptors CompileRun( "named_names = named_mirror.propertyNames();" "indexed_names = indexed_mirror.propertyNames();" "both_names = both_mirror.propertyNames()"); CHECK_EQ(3, CompileRun("named_names.length")->Int32Value(context).FromJust()); CHECK_EQ(2, CompileRun("indexed_names.length")->Int32Value(context).FromJust()); CHECK_EQ(5, CompileRun("both_names.length")->Int32Value(context).FromJust()); // Check the expected number of properties. const char* source; source = "named_mirror.properties().length"; CHECK_EQ(3, CompileRun(source)->Int32Value(context).FromJust()); source = "indexed_mirror.properties().length"; CHECK_EQ(2, CompileRun(source)->Int32Value(context).FromJust()); source = "both_mirror.properties().length"; CHECK_EQ(5, CompileRun(source)->Int32Value(context).FromJust()); // Get the interceptor properties for the object with only named interceptor. CompileRun("var named_values = named_mirror.properties()"); // Check that the properties are interceptor properties. for (int i = 0; i < 3; i++) { EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; SNPrintF(buffer, "named_values[%d] instanceof debug.PropertyMirror", i); CHECK(CompileRun(buffer.start())->BooleanValue(context).FromJust()); SNPrintF(buffer, "named_values[%d].isNative()", i); CHECK(CompileRun(buffer.start())->BooleanValue(context).FromJust()); } // Get the interceptor properties for the object with only indexed // interceptor. CompileRun("var indexed_values = indexed_mirror.properties()"); // Check that the properties are interceptor properties. for (int i = 0; i < 2; i++) { EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; SNPrintF(buffer, "indexed_values[%d] instanceof debug.PropertyMirror", i); CHECK(CompileRun(buffer.start())->BooleanValue(context).FromJust()); } // Get the interceptor properties for the object with both types of // interceptors. CompileRun("var both_values = both_mirror.properties()"); // Check that the properties are interceptor properties. for (int i = 0; i < 5; i++) { EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer; SNPrintF(buffer, "both_values[%d] instanceof debug.PropertyMirror", i); CHECK(CompileRun(buffer.start())->BooleanValue(context).FromJust()); } // Check the property names. source = "both_values[0].name() == '1'"; CHECK(CompileRun(source)->BooleanValue(context).FromJust()); source = "both_values[1].name() == '10'"; CHECK(CompileRun(source)->BooleanValue(context).FromJust()); source = "both_values[2].name() == 'a'"; CHECK(CompileRun(source)->BooleanValue(context).FromJust()); source = "both_values[3].name() == 'b'"; CHECK(CompileRun(source)->BooleanValue(context).FromJust()); source = "both_values[4].name() == 'c'"; CHECK(CompileRun(source)->BooleanValue(context).FromJust()); } TEST(HiddenPrototypePropertyMirror) { // Create a V8 environment with debug access. DebugLocalContext env; v8::Isolate* isolate = env->GetIsolate(); v8::HandleScope scope(isolate); env.ExposeDebug(); v8::Local<v8::FunctionTemplate> t0 = v8::FunctionTemplate::New(isolate); t0->InstanceTemplate()->Set(v8_str(isolate, "x"), v8::Number::New(isolate, 0)); v8::Local<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New(isolate); t1->SetHiddenPrototype(true); t1->InstanceTemplate()->Set(v8_str(isolate, "y"), v8::Number::New(isolate, 1)); v8::Local<v8::FunctionTemplate> t2 = v8::FunctionTemplate::New(isolate); t2->SetHiddenPrototype(true); t2->InstanceTemplate()->Set(v8_str(isolate, "z"), v8::Number::New(isolate, 2)); v8::Local<v8::FunctionTemplate> t3 = v8::FunctionTemplate::New(isolate); t3->InstanceTemplate()->Set(v8_str(isolate, "u"), v8::Number::New(isolate, 3)); v8::Local<v8::Context> context = env.context(); // Create object and set them on the global object. v8::Local<v8::Object> o0 = t0->GetFunction(context) .ToLocalChecked() ->NewInstance(context) .ToLocalChecked(); CHECK(env->Global()->Set(context, v8_str(isolate, "o0"), o0).FromJust()); v8::Local<v8::Object> o1 = t1->GetFunction(context) .ToLocalChecked() ->NewInstance(context) .ToLocalChecked(); CHECK(env->Global()->Set(context, v8_str(isolate, "o1"), o1).FromJust()); v8::Local<v8::Object> o2 = t2->GetFunction(context) .ToLocalChecked() ->NewInstance(context) .ToLocalChecked(); CHECK(env->Global()->Set(context, v8_str(isolate, "o2"), o2).FromJust()); v8::Local<v8::Object> o3 = t3->GetFunction(context) .ToLocalChecked() ->NewInstance(context) .ToLocalChecked(); CHECK(env->Global()->Set(context, v8_str(isolate, "o3"), o3).FromJust()); // Get mirrors for the four objects. CompileRun( "var o0_mirror = debug.MakeMirror(o0);" "var o1_mirror = debug.MakeMirror(o1);" "var o2_mirror = debug.MakeMirror(o2);" "var o3_mirror = debug.MakeMirror(o3)"); CHECK(CompileRun("o0_mirror instanceof debug.ObjectMirror") ->BooleanValue(context) .FromJust()); CHECK(CompileRun("o1_mirror instanceof debug.ObjectMirror") ->BooleanValue(context) .FromJust()); CHECK(CompileRun("o2_mirror instanceof debug.ObjectMirror") ->BooleanValue(context) .FromJust()); CHECK(CompileRun("o3_mirror instanceof debug.ObjectMirror") ->BooleanValue(context) .FromJust()); // Check that each object has one property. CHECK_EQ(1, CompileRun("o0_mirror.propertyNames().length") ->Int32Value(context) .FromJust()); CHECK_EQ(1, CompileRun("o1_mirror.propertyNames().length") ->Int32Value(context) .FromJust()); CHECK_EQ(1, CompileRun("o2_mirror.propertyNames().length") ->Int32Value(context) .FromJust()); CHECK_EQ(1, CompileRun("o3_mirror.propertyNames().length") ->Int32Value(context) .FromJust()); // Set o1 as prototype for o0. o1 has the hidden prototype flag so all // properties on o1 should be seen on o0. CHECK(o0->Set(context, v8_str(isolate, "__proto__"), o1).FromJust()); CHECK_EQ(2, CompileRun("o0_mirror.propertyNames().length") ->Int32Value(context) .FromJust()); CHECK_EQ(0, CompileRun("o0_mirror.property('x').value().value()") ->Int32Value(context) .FromJust()); CHECK_EQ(1, CompileRun("o0_mirror.property('y').value().value()") ->Int32Value(context) .FromJust()); // Set o2 as prototype for o0 (it will end up after o1 as o1 has the hidden // prototype flag. o2 also has the hidden prototype flag so all properties // on o2 should be seen on o0 as well as properties on o1. CHECK(o0->Set(context, v8_str(isolate, "__proto__"), o2).FromJust()); CHECK_EQ(3, CompileRun("o0_mirror.propertyNames().length") ->Int32Value(context) .FromJust()); CHECK_EQ(0, CompileRun("o0_mirror.property('x').value().value()") ->Int32Value(context) .FromJust()); CHECK_EQ(1, CompileRun("o0_mirror.property('y').value().value()") ->Int32Value(context) .FromJust()); CHECK_EQ(2, CompileRun("o0_mirror.property('z').value().value()") ->Int32Value(context) .FromJust()); // Set o3 as prototype for o0 (it will end up after o1 and o2 as both o1 and // o2 has the hidden prototype flag. o3 does not have the hidden prototype // flag so properties on o3 should not be seen on o0 whereas the properties // from o1 and o2 should still be seen on o0. // Final prototype chain: o0 -> o1 -> o2 -> o3 // Hidden prototypes: ^^ ^^ CHECK(o0->Set(context, v8_str(isolate, "__proto__"), o3).FromJust()); CHECK_EQ(3, CompileRun("o0_mirror.propertyNames().length") ->Int32Value(context) .FromJust()); CHECK_EQ(1, CompileRun("o3_mirror.propertyNames().length") ->Int32Value(context) .FromJust()); CHECK_EQ(0, CompileRun("o0_mirror.property('x').value().value()") ->Int32Value(context) .FromJust()); CHECK_EQ(1, CompileRun("o0_mirror.property('y').value().value()") ->Int32Value(context) .FromJust()); CHECK_EQ(2, CompileRun("o0_mirror.property('z').value().value()") ->Int32Value(context) .FromJust()); CHECK(CompileRun("o0_mirror.property('u').isUndefined()") ->BooleanValue(context) .FromJust()); // The prototype (__proto__) for o0 should be o3 as o1 and o2 are hidden. CHECK(CompileRun("o0_mirror.protoObject().value() == o3_mirror.value()") ->BooleanValue(context) .FromJust()); } static void ProtperyXNativeGetter( v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info) { info.GetReturnValue().Set(10); } TEST(NativeGetterPropertyMirror) { // Create a V8 environment with debug access. DebugLocalContext env; v8::Isolate* isolate = env->GetIsolate(); v8::HandleScope scope(isolate); env.ExposeDebug(); v8::Local<v8::Context> context = env.context(); v8::Local<v8::String> name = v8_str(isolate, "x"); // Create object with named accessor. v8::Local<v8::ObjectTemplate> named = v8::ObjectTemplate::New(isolate); named->SetAccessor(name, &ProtperyXNativeGetter, NULL, v8::Local<v8::Value>(), v8::DEFAULT, v8::None); // Create object with named property getter. CHECK(env->Global() ->Set(context, v8_str(isolate, "instance"), named->NewInstance(context).ToLocalChecked()) .FromJust()); CHECK_EQ(10, CompileRun("instance.x")->Int32Value(context).FromJust()); // Get mirror for the object with property getter. CompileRun("var instance_mirror = debug.MakeMirror(instance);"); CHECK(CompileRun("instance_mirror instanceof debug.ObjectMirror") ->BooleanValue(context) .FromJust()); CompileRun("var named_names = instance_mirror.propertyNames();"); CHECK_EQ(1, CompileRun("named_names.length")->Int32Value(context).FromJust()); CHECK(CompileRun("named_names[0] == 'x'")->BooleanValue(context).FromJust()); CHECK(CompileRun("instance_mirror.property('x').value().isNumber()") ->BooleanValue(context) .FromJust()); CHECK(CompileRun("instance_mirror.property('x').value().value() == 10") ->BooleanValue(context) .FromJust()); } static void ProtperyXNativeGetterThrowingError( v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info) { CompileRun("throw new Error('Error message');"); } TEST(NativeGetterThrowingErrorPropertyMirror) { // Create a V8 environment with debug access. DebugLocalContext env; v8::Isolate* isolate = env->GetIsolate(); v8::HandleScope scope(isolate); env.ExposeDebug(); v8::Local<v8::Context> context = env.context(); v8::Local<v8::String> name = v8_str(isolate, "x"); // Create object with named accessor. v8::Local<v8::ObjectTemplate> named = v8::ObjectTemplate::New(isolate); named->SetAccessor(name, &ProtperyXNativeGetterThrowingError, NULL, v8::Local<v8::Value>(), v8::DEFAULT, v8::None); // Create object with named property getter. CHECK(env->Global() ->Set(context, v8_str(isolate, "instance"), named->NewInstance(context).ToLocalChecked()) .FromJust()); // Get mirror for the object with property getter. CompileRun("var instance_mirror = debug.MakeMirror(instance);"); CHECK(CompileRun("instance_mirror instanceof debug.ObjectMirror") ->BooleanValue(context) .FromJust()); CompileRun("named_names = instance_mirror.propertyNames();"); CHECK_EQ(1, CompileRun("named_names.length")->Int32Value(context).FromJust()); CHECK(CompileRun("named_names[0] == 'x'")->BooleanValue(context).FromJust()); CHECK(CompileRun("instance_mirror.property('x').value().isError()") ->BooleanValue(context) .FromJust()); // Check that the message is that passed to the Error constructor. CHECK( CompileRun( "instance_mirror.property('x').value().message() == 'Error message'") ->BooleanValue(context) .FromJust()); } // Test that hidden properties object is not returned as an unnamed property // among regular properties. // See http://crbug.com/26491 TEST(NoHiddenProperties) { // Create a V8 environment with debug access. DebugLocalContext env; v8::Isolate* isolate = env->GetIsolate(); v8::HandleScope scope(isolate); env.ExposeDebug(); v8::Local<v8::Context> context = env.context(); // Create an object in the global scope. const char* source = "var obj = {a: 1};"; v8::Script::Compile(context, v8_str(isolate, source)) .ToLocalChecked() ->Run(context) .ToLocalChecked(); v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast( env->Global()->Get(context, v8_str(isolate, "obj")).ToLocalChecked()); // Set a hidden property on the object. obj->SetPrivate( env.context(), v8::Private::New(isolate, v8_str(isolate, "v8::test-debug::a")), v8::Int32::New(isolate, 11)) .FromJust(); // Get mirror for the object with property getter. CompileRun("var obj_mirror = debug.MakeMirror(obj);"); CHECK(CompileRun("obj_mirror instanceof debug.ObjectMirror") ->BooleanValue(context) .FromJust()); CompileRun("var named_names = obj_mirror.propertyNames();"); // There should be exactly one property. But there is also an unnamed // property whose value is hidden properties dictionary. The latter // property should not be in the list of reguar properties. CHECK_EQ(1, CompileRun("named_names.length")->Int32Value(context).FromJust()); CHECK(CompileRun("named_names[0] == 'a'")->BooleanValue(context).FromJust()); CHECK(CompileRun("obj_mirror.property('a').value().value() == 1") ->BooleanValue(context) .FromJust()); // Object created by t0 will become hidden prototype of object 'obj'. v8::Local<v8::FunctionTemplate> t0 = v8::FunctionTemplate::New(isolate); t0->InstanceTemplate()->Set(v8_str(isolate, "b"), v8::Number::New(isolate, 2)); t0->SetHiddenPrototype(true); v8::Local<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New(isolate); t1->InstanceTemplate()->Set(v8_str(isolate, "c"), v8::Number::New(isolate, 3)); // Create proto objects, add hidden properties to them and set them on // the global object. v8::Local<v8::Object> protoObj = t0->GetFunction(context) .ToLocalChecked() ->NewInstance(context) .ToLocalChecked(); protoObj->SetPrivate( env.context(), v8::Private::New(isolate, v8_str(isolate, "v8::test-debug::b")), v8::Int32::New(isolate, 12)) .FromJust(); CHECK(env->Global() ->Set(context, v8_str(isolate, "protoObj"), protoObj) .FromJust()); v8::Local<v8::Object> grandProtoObj = t1->GetFunction(context) .ToLocalChecked() ->NewInstance(context) .ToLocalChecked(); grandProtoObj->SetPrivate(env.context(), v8::Private::New( isolate, v8_str(isolate, "v8::test-debug::c")), v8::Int32::New(isolate, 13)) .FromJust(); CHECK(env->Global() ->Set(context, v8_str(isolate, "grandProtoObj"), grandProtoObj) .FromJust()); // Setting prototypes: obj->protoObj->grandProtoObj CHECK(protoObj->Set(context, v8_str(isolate, "__proto__"), grandProtoObj) .FromJust()); CHECK(obj->Set(context, v8_str(isolate, "__proto__"), protoObj).FromJust()); // Get mirror for the object with property getter. CompileRun("var obj_mirror = debug.MakeMirror(obj);"); CHECK(CompileRun("obj_mirror instanceof debug.ObjectMirror") ->BooleanValue(context) .FromJust()); CompileRun("var named_names = obj_mirror.propertyNames();"); // There should be exactly two properties - one from the object itself and // another from its hidden prototype. CHECK_EQ(2, CompileRun("named_names.length")->Int32Value(context).FromJust()); CHECK(CompileRun("named_names.sort(); named_names[0] == 'a' &&" "named_names[1] == 'b'") ->BooleanValue(context) .FromJust()); CHECK(CompileRun("obj_mirror.property('a').value().value() == 1") ->BooleanValue(context) .FromJust()); CHECK(CompileRun("obj_mirror.property('b').value().value() == 2") ->BooleanValue(context) .FromJust()); } TEST(SetDebugEventListenerOnUninitializedVM) { EnableDebugger(CcTest::isolate()); } // Source for a JavaScript function which returns the data parameter of a // function called in the context of the debugger. If no data parameter is // passed it throws an exception. static const char* debugger_call_with_data_source = "function debugger_call_with_data(exec_state, data) {" " if (data) return data;" " throw 'No data!'" "}"; v8::Local<v8::Function> debugger_call_with_data; // Source for a JavaScript function which returns the data parameter of a // function called in the context of the debugger. If no data parameter is // passed it throws an exception. static const char* debugger_call_with_closure_source = "var x = 3;" "(function (exec_state) {" " if (exec_state.y) return x - 1;" " exec_state.y = x;" " return exec_state.y" "})"; v8::Local<v8::Function> debugger_call_with_closure; // Function to retrieve the number of JavaScript frames by calling a JavaScript // in the debugger. static void CheckFrameCount(const v8::FunctionCallbackInfo<v8::Value>& args) { v8::Local<v8::Context> context = args.GetIsolate()->GetCurrentContext(); CHECK(v8::debug::Call(context, frame_count).ToLocalChecked()->IsNumber()); CHECK_EQ(args[0]->Int32Value(context).FromJust(), v8::debug::Call(context, frame_count) .ToLocalChecked() ->Int32Value(context) .FromJust()); } // Function to retrieve the source line of the top JavaScript frame by calling a // JavaScript function in the debugger. static void CheckSourceLine(const v8::FunctionCallbackInfo<v8::Value>& args) { v8::Local<v8::Context> context = args.GetIsolate()->GetCurrentContext(); CHECK( v8::debug::Call(context, frame_source_line).ToLocalChecked()->IsNumber()); CHECK_EQ(args[0]->Int32Value(context).FromJust(), v8::debug::Call(context, frame_source_line) .ToLocalChecked() ->Int32Value(context) .FromJust()); } // Function to test passing an additional parameter to a JavaScript function // called in the debugger. It also tests that functions called in the debugger // can throw exceptions. static void CheckDataParameter( const v8::FunctionCallbackInfo<v8::Value>& args) { v8::Local<v8::String> data = v8_str(args.GetIsolate(), "Test"); v8::Local<v8::Context> context = args.GetIsolate()->GetCurrentContext(); CHECK(v8::debug::Call(context, debugger_call_with_data, data) .ToLocalChecked() ->IsString()); for (int i = 0; i < 3; i++) { v8::TryCatch catcher(args.GetIsolate()); CHECK(v8::debug::Call(context, debugger_call_with_data).IsEmpty()); CHECK(catcher.HasCaught()); CHECK(catcher.Exception()->IsString()); } } // Function to test using a JavaScript with closure in the debugger. static void CheckClosure(const v8::FunctionCallbackInfo<v8::Value>& args) { v8::Local<v8::Context> context = args.GetIsolate()->GetCurrentContext(); CHECK(v8::debug::Call(context, debugger_call_with_closure) .ToLocalChecked() ->IsNumber()); CHECK_EQ(3, v8::debug::Call(context, debugger_call_with_closure) .ToLocalChecked() ->Int32Value(context) .FromJust()); } // Test functions called through the debugger. TEST(CallFunctionInDebugger) { // Create and enter a context with the functions CheckFrameCount, // CheckSourceLine and CheckDataParameter installed. v8::Isolate* isolate = CcTest::isolate(); v8::HandleScope scope(isolate); v8::Local<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(isolate); global_template->Set(v8_str(isolate, "CheckFrameCount"), v8::FunctionTemplate::New(isolate, CheckFrameCount)); global_template->Set(v8_str(isolate, "CheckSourceLine"), v8::FunctionTemplate::New(isolate, CheckSourceLine)); global_template->Set(v8_str(isolate, "CheckDataParameter"), v8::FunctionTemplate::New(isolate, CheckDataParameter)); global_template->Set(v8_str(isolate, "CheckClosure"), v8::FunctionTemplate::New(isolate, CheckClosure)); v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global_template); v8::Context::Scope context_scope(context); // Compile a function for checking the number of JavaScript frames. v8::Script::Compile(context, v8_str(isolate, frame_count_source)) .ToLocalChecked() ->Run(context) .ToLocalChecked(); frame_count = v8::Local<v8::Function>::Cast( context->Global() ->Get(context, v8_str(isolate, "frame_count")) .ToLocalChecked()); // Compile a function for returning the source line for the top frame. v8::Script::Compile(context, v8_str(isolate, frame_source_line_source)) .ToLocalChecked() ->Run(context) .ToLocalChecked(); frame_source_line = v8::Local<v8::Function>::Cast( context->Global() ->Get(context, v8_str(isolate, "frame_source_line")) .ToLocalChecked()); // Compile a function returning the data parameter. v8::Script::Compile(context, v8_str(isolate, debugger_call_with_data_source)) .ToLocalChecked() ->Run(context) .ToLocalChecked(); debugger_call_with_data = v8::Local<v8::Function>::Cast( context->Global() ->Get(context, v8_str(isolate, "debugger_call_with_data")) .ToLocalChecked()); // Compile a function capturing closure. debugger_call_with_closure = v8::Local<v8::Function>::Cast( v8::Script::Compile(context, v8_str(isolate, debugger_call_with_closure_source)) .ToLocalChecked() ->Run(context) .ToLocalChecked()); // Calling a function through the debugger returns 0 frames if there are // no JavaScript frames. CHECK(v8::Integer::New(isolate, 0) ->Equals(context, v8::debug::Call(context, frame_count).ToLocalChecked()) .FromJust()); // Test that the number of frames can be retrieved. v8::Script::Compile(context, v8_str(isolate, "CheckFrameCount(1)")) .ToLocalChecked() ->Run(context) .ToLocalChecked(); v8::Script::Compile(context, v8_str(isolate, "function f() {" " CheckFrameCount(2);" "}; f()")) .ToLocalChecked() ->Run(context) .ToLocalChecked(); // Test that the source line can be retrieved. v8::Script::Compile(context, v8_str(isolate, "CheckSourceLine(0)")) .ToLocalChecked() ->Run(context) .ToLocalChecked(); v8::Script::Compile(context, v8_str(isolate, "function f() {\n" " CheckSourceLine(1)\n" " CheckSourceLine(2)\n" " CheckSourceLine(3)\n" "}; f()")) .ToLocalChecked() ->Run(context) .ToLocalChecked(); // Test that a parameter can be passed to a function called in the debugger. v8::Script::Compile(context, v8_str(isolate, "CheckDataParameter()")) .ToLocalChecked() ->Run(context) .ToLocalChecked(); // Test that a function with closure can be run in the debugger. v8::Script::Compile(context, v8_str(isolate, "CheckClosure()")) .ToLocalChecked() ->Run(context) .ToLocalChecked(); // Test that the source line is correct when there is a line offset. v8::ScriptOrigin origin(v8_str(isolate, "test"), v8::Integer::New(isolate, 7)); v8::Script::Compile(context, v8_str(isolate, "CheckSourceLine(7)"), &origin) .ToLocalChecked() ->Run(context) .ToLocalChecked(); v8::Script::Compile(context, v8_str(isolate, "function f() {\n" " CheckSourceLine(8)\n" " CheckSourceLine(9)\n" " CheckSourceLine(10)\n" "}; f()"), &origin) .ToLocalChecked() ->Run(context) .ToLocalChecked(); } // Test that clearing the debug event listener actually clears all break points // and related information. TEST(DebuggerUnload) { DebugLocalContext env; // Check debugger is unloaded before it is used. CheckDebuggerUnloaded(); // Set a debug event listener. break_point_hit_count = 0; SetDebugEventListener(env->GetIsolate(), DebugEventBreakPointHitCount); v8::Local<v8::Context> context = env.context(); { v8::HandleScope scope(env->GetIsolate()); // Create a couple of functions for the test. v8::Local<v8::Function> foo = CompileFunction(&env, "function foo(){x=1}", "foo"); v8::Local<v8::Function> bar = CompileFunction(&env, "function bar(){y=2}", "bar"); // Set some break points. SetBreakPoint(foo, 0); SetBreakPoint(foo, 4); SetBreakPoint(bar, 0); SetBreakPoint(bar, 4); // Make sure that the break points are there. break_point_hit_count = 0; foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(2, break_point_hit_count); bar->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(4, break_point_hit_count); } // Remove the debug event listener without clearing breakpoints. Do this // outside a handle scope. SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } int event_listener_hit_count = 0; // Debugger event listener which clears itself while active. static void EventListenerClearingItself( const v8::Debug::EventDetails& details) { event_listener_hit_count++; // Clear debug event listener. SetDebugEventListener(details.GetIsolate(), nullptr); } // Test clearing the debug message handler while processing a debug event. TEST(DebuggerClearEventListenerWhileActive) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); // Check debugger is unloaded before it is used. CheckDebuggerUnloaded(); // Set a debug event listener. SetDebugEventListener(env->GetIsolate(), EventListenerClearingItself); // Run code to throw an uncaught exception. This should trigger the listener. CompileRun("throw 1"); // The event listener should have been called. CHECK_EQ(1, event_listener_hit_count); CheckDebuggerUnloaded(); } // Test for issue http://code.google.com/p/v8/issues/detail?id=289. // Make sure that DebugGetLoadedScripts doesn't return scripts // with disposed external source. class EmptyExternalStringResource : public v8::String::ExternalStringResource { public: EmptyExternalStringResource() { empty_[0] = 0; } virtual ~EmptyExternalStringResource() {} virtual size_t length() const { return empty_.length(); } virtual const uint16_t* data() const { return empty_.start(); } private: ::v8::internal::EmbeddedVector<uint16_t, 1> empty_; }; TEST(DebugScriptLineEndsAreAscending) { DebugLocalContext env; v8::Isolate* isolate = env->GetIsolate(); v8::HandleScope scope(isolate); env.ExposeDebug(); // Compile a test script. v8::Local<v8::String> script = v8_str(isolate, "function f() {\n" " debugger;\n" "}\n"); v8::ScriptOrigin origin1 = v8::ScriptOrigin(v8_str(isolate, "name")); v8::Local<v8::Script> script1 = v8::Script::Compile(env.context(), script, &origin1).ToLocalChecked(); USE(script1); Handle<v8::internal::FixedArray> instances; { v8::internal::Debug* debug = CcTest::i_isolate()->debug(); v8::internal::DebugScope debug_scope(debug); CHECK(!debug_scope.failed()); instances = debug->GetLoadedScripts(); } CHECK_GT(instances->length(), 0); for (int i = 0; i < instances->length(); i++) { Handle<v8::internal::Script> script = Handle<v8::internal::Script>( v8::internal::Script::cast(instances->get(i))); v8::internal::Script::InitLineEnds(script); v8::internal::FixedArray* ends = v8::internal::FixedArray::cast(script->line_ends()); CHECK_GT(ends->length(), 0); int prev_end = -1; for (int j = 0; j < ends->length(); j++) { const int curr_end = v8::internal::Smi::ToInt(ends->get(j)); CHECK_GT(curr_end, prev_end); prev_end = curr_end; } } } // Test script break points set on lines. TEST(ScriptNameAndData) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); env.ExposeDebug(); // Create functions for retrieving script name and data for the function on // the top frame when hitting a break point. frame_script_name = CompileFunction(&env, frame_script_name_source, "frame_script_name"); SetDebugEventListener(env->GetIsolate(), DebugEventBreakPointHitCount); v8::Local<v8::Context> context = env.context(); // Test function source. v8::Local<v8::String> script = v8_str(env->GetIsolate(), "function f() {\n" " debugger;\n" "}\n"); v8::ScriptOrigin origin1 = v8::ScriptOrigin(v8_str(env->GetIsolate(), "name")); v8::Local<v8::Script> script1 = v8::Script::Compile(context, script, &origin1).ToLocalChecked(); script1->Run(context).ToLocalChecked(); v8::Local<v8::Function> f; f = v8::Local<v8::Function>::Cast( env->Global() ->Get(context, v8_str(env->GetIsolate(), "f")) .ToLocalChecked()); f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(1, break_point_hit_count); CHECK_EQ(0, strcmp("name", last_script_name_hit)); // Compile the same script again without setting data. As the compilation // cache is disabled when debugging expect the data to be missing. v8::Script::Compile(context, script, &origin1) .ToLocalChecked() ->Run(context) .ToLocalChecked(); f = v8::Local<v8::Function>::Cast( env->Global() ->Get(context, v8_str(env->GetIsolate(), "f")) .ToLocalChecked()); f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(2, break_point_hit_count); CHECK_EQ(0, strcmp("name", last_script_name_hit)); v8::Local<v8::String> data_obj_source = v8_str(env->GetIsolate(), "({ a: 'abc',\n" " b: 123,\n" " toString: function() { return this.a + ' ' + this.b; }\n" "})\n"); v8::Script::Compile(context, data_obj_source) .ToLocalChecked() ->Run(context) .ToLocalChecked(); v8::ScriptOrigin origin2 = v8::ScriptOrigin(v8_str(env->GetIsolate(), "new name")); v8::Local<v8::Script> script2 = v8::Script::Compile(context, script, &origin2).ToLocalChecked(); script2->Run(context).ToLocalChecked(); f = v8::Local<v8::Function>::Cast( env->Global() ->Get(context, v8_str(env->GetIsolate(), "f")) .ToLocalChecked()); f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(3, break_point_hit_count); CHECK_EQ(0, strcmp("new name", last_script_name_hit)); v8::Local<v8::Script> script3 = v8::Script::Compile(context, script, &origin2).ToLocalChecked(); script3->Run(context).ToLocalChecked(); f = v8::Local<v8::Function>::Cast( env->Global() ->Get(context, v8_str(env->GetIsolate(), "f")) .ToLocalChecked()); f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(4, break_point_hit_count); } static v8::Local<v8::Context> expected_context; static v8::Local<v8::Value> expected_context_data; // Check that the expected context is the one generating the debug event. static void ContextCheckEventListener( const v8::Debug::EventDetails& event_details) { CHECK(event_details.GetEventContext() == expected_context); CHECK(event_details.GetEventContext()->GetEmbedderData(0)->StrictEquals( expected_context_data)); event_listener_hit_count++; } // Test which creates two contexts and sets different embedder data on each. // Checks that this data is set correctly and that when the debug event // listener is called the expected context is the one active. TEST(ContextData) { v8::Isolate* isolate = CcTest::isolate(); v8::HandleScope scope(isolate); // Create two contexts. v8::Local<v8::Context> context_1; v8::Local<v8::Context> context_2; v8::Local<v8::ObjectTemplate> global_template = v8::Local<v8::ObjectTemplate>(); v8::Local<v8::Value> global_object = v8::Local<v8::Value>(); context_1 = v8::Context::New(isolate, NULL, global_template, global_object); context_2 = v8::Context::New(isolate, NULL, global_template, global_object); SetDebugEventListener(isolate, ContextCheckEventListener); // Default data value is undefined. CHECK(context_1->GetEmbedderData(0)->IsUndefined()); CHECK(context_2->GetEmbedderData(0)->IsUndefined()); // Set and check different data values. v8::Local<v8::String> data_1 = v8_str(isolate, "1"); v8::Local<v8::String> data_2 = v8_str(isolate, "2"); context_1->SetEmbedderData(0, data_1); context_2->SetEmbedderData(0, data_2); CHECK(context_1->GetEmbedderData(0)->StrictEquals(data_1)); CHECK(context_2->GetEmbedderData(0)->StrictEquals(data_2)); // Simple test function which causes a break. const char* source = "function f() { debugger; }"; // Enter and run function in the first context. { v8::Context::Scope context_scope(context_1); expected_context = context_1; expected_context_data = data_1; v8::Local<v8::Function> f = CompileFunction(isolate, source, "f"); f->Call(context_1, context_1->Global(), 0, NULL).ToLocalChecked(); } // Enter and run function in the second context. { v8::Context::Scope context_scope(context_2); expected_context = context_2; expected_context_data = data_2; v8::Local<v8::Function> f = CompileFunction(isolate, source, "f"); f->Call(context_2, context_2->Global(), 0, NULL).ToLocalChecked(); } // Two times compile event and two times break event. CHECK_GT(event_listener_hit_count, 3); SetDebugEventListener(isolate, nullptr); CheckDebuggerUnloaded(); } // Debug event listener which issues a debug break when it hits a break event. static int event_listener_break_hit_count = 0; static void DebugBreakEventListener(const v8::Debug::EventDetails& details) { // Schedule a debug break for break events. if (details.GetEvent() == v8::Break) { event_listener_break_hit_count++; if (event_listener_break_hit_count == 1) { v8::debug::DebugBreak(details.GetIsolate()); } } } // Test that a debug break can be scheduled while in a event listener. TEST(DebugBreakInEventListener) { i::FLAG_turbo_inlining = false; // Make sure g is not inlined into f. DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); SetDebugEventListener(env->GetIsolate(), DebugBreakEventListener); v8::Local<v8::Context> context = env.context(); // Test functions. const char* script = "function f() { debugger; g(); } function g() { }"; CompileRun(script); v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( env->Global() ->Get(context, v8_str(env->GetIsolate(), "f")) .ToLocalChecked()); v8::Local<v8::Function> g = v8::Local<v8::Function>::Cast( env->Global() ->Get(context, v8_str(env->GetIsolate(), "g")) .ToLocalChecked()); // Call f then g. The debugger statement in f will cause a break which will // cause another break. f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(2, event_listener_break_hit_count); // Calling g will not cause any additional breaks. g->Call(context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(2, event_listener_break_hit_count); } #ifndef V8_INTERPRETED_REGEXP // Debug event handler which gets the function on the top frame and schedules a // break a number of times. static void DebugEventDebugBreak( const v8::Debug::EventDetails& event_details) { v8::DebugEvent event = event_details.GetEvent(); v8::Local<v8::Object> exec_state = event_details.GetExecutionState(); v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext(); if (event == v8::Break) { break_point_hit_count++; // Get the name of the top frame function. if (!frame_function_name.IsEmpty()) { // Get the name of the function. const int argc = 2; v8::Local<v8::Value> argv[argc] = { exec_state, v8::Integer::New(CcTest::isolate(), 0)}; v8::Local<v8::Value> result = frame_function_name->Call(context, exec_state, argc, argv) .ToLocalChecked(); if (result->IsUndefined()) { last_function_hit[0] = '\0'; } else { CHECK(result->IsString()); v8::Local<v8::String> function_name( result->ToString(context).ToLocalChecked()); function_name->WriteUtf8(last_function_hit); } } // Keep forcing breaks. if (break_point_hit_count < 20) { v8::debug::DebugBreak(CcTest::isolate()); } } } TEST(RegExpDebugBreak) { // This test only applies to native regexps. DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); v8::Local<v8::Context> context = env.context(); // Create a function for checking the function when hitting a break point. frame_function_name = CompileFunction(&env, frame_function_name_source, "frame_function_name"); // Test RegExp which matches white spaces and comments at the beginning of a // source line. const char* script = "var sourceLineBeginningSkip = /^(?:[ \\v\\h]*(?:\\/\\*.*?\\*\\/)*)*/;\n" "function f(s) { return s.match(sourceLineBeginningSkip)[0].length; }"; v8::Local<v8::Function> f = CompileFunction(env->GetIsolate(), script, "f"); const int argc = 1; v8::Local<v8::Value> argv[argc] = { v8_str(env->GetIsolate(), " /* xxx */ a=0;")}; v8::Local<v8::Value> result = f->Call(context, env->Global(), argc, argv).ToLocalChecked(); CHECK_EQ(12, result->Int32Value(context).FromJust()); SetDebugEventListener(env->GetIsolate(), DebugEventDebugBreak); v8::debug::DebugBreak(env->GetIsolate()); result = f->Call(context, env->Global(), argc, argv).ToLocalChecked(); // Check that there was only one break event. Matching RegExp should not // cause Break events. CHECK_EQ(1, break_point_hit_count); CHECK_EQ(0, strcmp("f", last_function_hit)); } #endif // V8_INTERPRETED_REGEXP // Test which creates a context and sets embedder data on it. Checks that this // data is set correctly and that when the debug event listener is called for // break event in an eval statement the expected context is the one returned by // Message.GetEventContext. TEST(EvalContextData) { v8::HandleScope scope(CcTest::isolate()); v8::Local<v8::Context> context_1; v8::Local<v8::ObjectTemplate> global_template = v8::Local<v8::ObjectTemplate>(); context_1 = v8::Context::New(CcTest::isolate(), NULL, global_template); SetDebugEventListener(CcTest::isolate(), ContextCheckEventListener); // Default data value is undefined. CHECK(context_1->GetEmbedderData(0)->IsUndefined()); // Set and check a data value. v8::Local<v8::String> data_1 = v8_str(CcTest::isolate(), "1"); context_1->SetEmbedderData(0, data_1); CHECK(context_1->GetEmbedderData(0)->StrictEquals(data_1)); // Simple test function with eval that causes a break. const char* source = "function f() { eval('debugger;'); }"; // Enter and run function in the context. { v8::Context::Scope context_scope(context_1); expected_context = context_1; expected_context_data = data_1; v8::Local<v8::Function> f = CompileFunction(CcTest::isolate(), source, "f"); f->Call(context_1, context_1->Global(), 0, NULL).ToLocalChecked(); } SetDebugEventListener(CcTest::isolate(), nullptr); // One time compile event and one time break event. CHECK_GT(event_listener_hit_count, 2); CheckDebuggerUnloaded(); } // Debug event listener which counts the after compile events. int after_compile_event_count = 0; static void AfterCompileEventListener(const v8::Debug::EventDetails& details) { // Count the number of scripts collected. if (details.GetEvent() == v8::AfterCompile) { after_compile_event_count++; } } // Tests that after compile event is sent as many times as there are scripts // compiled. TEST(AfterCompileEventWhenEventListenerIsReset) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); v8::Local<v8::Context> context = env.context(); const char* script = "var a=1"; SetDebugEventListener(env->GetIsolate(), AfterCompileEventListener); v8::Script::Compile(context, v8_str(env->GetIsolate(), script)) .ToLocalChecked() ->Run(context) .ToLocalChecked(); SetDebugEventListener(env->GetIsolate(), nullptr); SetDebugEventListener(env->GetIsolate(), AfterCompileEventListener); v8::debug::DebugBreak(env->GetIsolate()); v8::Script::Compile(context, v8_str(env->GetIsolate(), script)) .ToLocalChecked() ->Run(context) .ToLocalChecked(); // Setting listener to NULL should cause debugger unload. SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); // Compilation cache should be disabled when debugger is active. CHECK_EQ(2, after_compile_event_count); } // Syntax error event handler which counts a number of events. int compile_error_event_count = 0; static void CompileErrorEventCounterClear() { compile_error_event_count = 0; } static void CompileErrorEventCounter( const v8::Debug::EventDetails& event_details) { v8::DebugEvent event = event_details.GetEvent(); if (event == v8::CompileError) { compile_error_event_count++; } } // Tests that syntax error event is sent as many times as there are scripts // with syntax error compiled. TEST(SyntaxErrorEventOnSyntaxException) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); // For this test, we want to break on uncaught exceptions: ChangeBreakOnException(false, true); SetDebugEventListener(env->GetIsolate(), CompileErrorEventCounter); v8::Local<v8::Context> context = env.context(); CompileErrorEventCounterClear(); // Check initial state. CHECK_EQ(0, compile_error_event_count); // Throws SyntaxError: Unexpected end of input CHECK( v8::Script::Compile(context, v8_str(env->GetIsolate(), "+++")).IsEmpty()); CHECK_EQ(1, compile_error_event_count); CHECK(v8::Script::Compile(context, v8_str(env->GetIsolate(), "/sel\\/: \\")) .IsEmpty()); CHECK_EQ(2, compile_error_event_count); v8::Local<v8::Script> script = v8::Script::Compile(context, v8_str(env->GetIsolate(), "JSON.parse('1234:')")) .ToLocalChecked(); CHECK_EQ(2, compile_error_event_count); CHECK(script->Run(context).IsEmpty()); CHECK_EQ(3, compile_error_event_count); v8::Script::Compile(context, v8_str(env->GetIsolate(), "new RegExp('/\\/\\\\');")) .ToLocalChecked(); CHECK_EQ(3, compile_error_event_count); v8::Script::Compile(context, v8_str(env->GetIsolate(), "throw 1;")) .ToLocalChecked(); CHECK_EQ(3, compile_error_event_count); } // Tests that break event is sent when event listener is reset. TEST(BreakEventWhenEventListenerIsReset) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); v8::Local<v8::Context> context = env.context(); const char* script = "function f() {};"; SetDebugEventListener(env->GetIsolate(), AfterCompileEventListener); v8::Script::Compile(context, v8_str(env->GetIsolate(), script)) .ToLocalChecked() ->Run(context) .ToLocalChecked(); SetDebugEventListener(env->GetIsolate(), nullptr); SetDebugEventListener(env->GetIsolate(), AfterCompileEventListener); v8::debug::DebugBreak(env->GetIsolate()); v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( env->Global() ->Get(context, v8_str(env->GetIsolate(), "f")) .ToLocalChecked()); f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); // Setting event listener to NULL should cause debugger unload. SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); // Compilation cache should be disabled when debugger is active. CHECK_EQ(1, after_compile_event_count); } static int exception_event_count = 0; static void ExceptionEventListener(const v8::Debug::EventDetails& details) { if (details.GetEvent() == v8::Exception) exception_event_count++; } // Tests that exception event is sent when event listener is reset. TEST(ExceptionEventWhenEventListenerIsReset) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); v8::Local<v8::Context> context = env.context(); // For this test, we want to break on uncaught exceptions: ChangeBreakOnException(false, true); exception_event_count = 0; const char* script = "function f() {throw new Error()};"; SetDebugEventListener(env->GetIsolate(), AfterCompileEventListener); v8::Script::Compile(context, v8_str(env->GetIsolate(), script)) .ToLocalChecked() ->Run(context) .ToLocalChecked(); SetDebugEventListener(env->GetIsolate(), nullptr); SetDebugEventListener(env->GetIsolate(), ExceptionEventListener); v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( env->Global() ->Get(context, v8_str(env->GetIsolate(), "f")) .ToLocalChecked()); CHECK(f->Call(context, env->Global(), 0, NULL).IsEmpty()); // Setting event listener to NULL should cause debugger unload. SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); CHECK_EQ(1, exception_event_count); } // Tests after compile event is sent when there are some provisional // breakpoints out of the scripts lines range. TEST(ProvisionalBreakpointOnLineOutOfRange) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); env.ExposeDebug(); const char* script = "function f() {};"; const char* resource_name = "test_resource"; SetDebugEventListener(env->GetIsolate(), AfterCompileEventListener); v8::Local<v8::Context> context = env.context(); // Set a couple of provisional breakpoint on lines out of the script lines // range. int sbp1 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), resource_name, 3, -1 /* no column */); int sbp2 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), resource_name, 5, 5); after_compile_event_count = 0; v8::ScriptOrigin origin(v8_str(env->GetIsolate(), resource_name), v8::Integer::New(env->GetIsolate(), 10), v8::Integer::New(env->GetIsolate(), 1)); // Compile a script whose first line number is greater than the breakpoints' // lines. v8::Script::Compile(context, v8_str(env->GetIsolate(), script), &origin) .ToLocalChecked() ->Run(context) .ToLocalChecked(); // If the script is compiled successfully there is exactly one after compile // event. In case of an exception in debugger code after compile event is not // sent. CHECK_EQ(1, after_compile_event_count); ClearBreakPointFromJS(env->GetIsolate(), sbp1); ClearBreakPointFromJS(env->GetIsolate(), sbp2); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } static void BreakEventListener(const v8::Debug::EventDetails& details) { if (details.GetEvent() == v8::Break) break_point_hit_count++; } // Test that if DebugBreak is forced it is ignored when code from // debug-delay.js is executed. TEST(NoDebugBreakInAfterCompileEventListener) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); v8::Local<v8::Context> context = env.context(); // Register a debug event listener which sets the break flag and counts. SetDebugEventListener(env->GetIsolate(), BreakEventListener); // Set the debug break flag. v8::debug::DebugBreak(env->GetIsolate()); // Create a function for testing stepping. const char* src = "function f() { eval('var x = 10;'); } "; v8::Local<v8::Function> f = CompileFunction(&env, src, "f"); // There should be only one break event. CHECK_EQ(1, break_point_hit_count); // Set the debug break flag again. v8::debug::DebugBreak(env->GetIsolate()); f->Call(context, env->Global(), 0, NULL).ToLocalChecked(); // There should be one more break event when the script is evaluated in 'f'. CHECK_EQ(2, break_point_hit_count); // Get rid of the debug event listener. SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } // Test that the debug break flag works with function.apply. TEST(DebugBreakFunctionApply) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); v8::Local<v8::Context> context = env.context(); // Create a function for testing breaking in apply. v8::Local<v8::Function> foo = CompileFunction( &env, "function baz(x) { }" "function bar(x) { baz(); }" "function foo(){ bar.apply(this, [1]); }", "foo"); // Register a debug event listener which steps and counts. SetDebugEventListener(env->GetIsolate(), DebugEventBreakMax); // Set the debug break flag before calling the code using function.apply. v8::debug::DebugBreak(env->GetIsolate()); // Limit the number of debug breaks. This is a regression test for issue 493 // where this test would enter an infinite loop. break_point_hit_count = 0; max_break_point_hit_count = 10000; // 10000 => infinite loop. foo->Call(context, env->Global(), 0, NULL).ToLocalChecked(); // When keeping the debug break several break will happen. CHECK_GT(break_point_hit_count, 1); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } v8::Local<v8::Context> debugee_context; v8::Local<v8::Context> debugger_context; // Property getter that checks that current and calling contexts // are both the debugee contexts. static void NamedGetterWithCallingContextCheck( v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Value>& info) { CHECK_EQ(0, strcmp(*v8::String::Utf8Value(info.GetIsolate(), name), "a")); v8::Local<v8::Context> current = info.GetIsolate()->GetCurrentContext(); CHECK(current == debugee_context); CHECK(current != debugger_context); info.GetReturnValue().Set(1); } // Debug event listener that checks if the first argument of a function is // an object with property 'a' == 1. If the property has custom accessor // this handler will eventually invoke it. static void DebugEventGetAtgumentPropertyValue( const v8::Debug::EventDetails& event_details) { v8::DebugEvent event = event_details.GetEvent(); v8::Local<v8::Object> exec_state = event_details.GetExecutionState(); if (event == v8::Break) { break_point_hit_count++; CHECK(debugger_context == CcTest::isolate()->GetCurrentContext()); v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(CompileRun( "(function(exec_state) {\n" " return (exec_state.frame(0).argumentValue(0).property('a').\n" " value().value() == 1);\n" "})")); const int argc = 1; v8::Local<v8::Value> argv[argc] = {exec_state}; v8::Local<v8::Value> result = func->Call(debugger_context, exec_state, argc, argv).ToLocalChecked(); CHECK(result->IsTrue()); } } TEST(CallingContextIsNotDebugContext) { v8::internal::Debug* debug = CcTest::i_isolate()->debug(); // Create and enter a debugee context. DebugLocalContext env; v8::Isolate* isolate = env->GetIsolate(); v8::HandleScope scope(isolate); env.ExposeDebug(); // Save handles to the debugger and debugee contexts to be used in // NamedGetterWithCallingContextCheck. debugee_context = env.context(); debugger_context = v8::Utils::ToLocal(debug->debug_context()); // Create object with 'a' property accessor. v8::Local<v8::ObjectTemplate> named = v8::ObjectTemplate::New(isolate); named->SetAccessor(v8_str(isolate, "a"), NamedGetterWithCallingContextCheck); CHECK(env->Global() ->Set(debugee_context, v8_str(isolate, "obj"), named->NewInstance(debugee_context).ToLocalChecked()) .FromJust()); // Register the debug event listener SetDebugEventListener(isolate, DebugEventGetAtgumentPropertyValue); // Create a function that invokes debugger. v8::Local<v8::Function> foo = CompileFunction( &env, "function bar(x) { debugger; }" "function foo(){ bar(obj); }", "foo"); break_point_hit_count = 0; foo->Call(debugee_context, env->Global(), 0, NULL).ToLocalChecked(); CHECK_EQ(1, break_point_hit_count); SetDebugEventListener(isolate, nullptr); debugee_context = v8::Local<v8::Context>(); debugger_context = v8::Local<v8::Context>(); CheckDebuggerUnloaded(); } static v8::Local<v8::Value> expected_callback_data; static void DebugEventContextChecker(const v8::Debug::EventDetails& details) { CHECK(details.GetEventContext() == expected_context); CHECK(expected_callback_data->Equals(details.GetEventContext(), details.GetCallbackData()) .FromJust()); } // Check that event details contain context where debug event occurred. TEST(DebugEventContext) { v8::Isolate* isolate = CcTest::isolate(); v8::HandleScope scope(isolate); expected_context = v8::Context::New(isolate); expected_callback_data = v8::Int32::New(isolate, 2010); SetDebugEventListener(isolate, DebugEventContextChecker, expected_callback_data); v8::Context::Scope context_scope(expected_context); v8::Script::Compile(expected_context, v8_str(isolate, "(function(){debugger;})();")) .ToLocalChecked() ->Run(expected_context) .ToLocalChecked(); expected_context.Clear(); SetDebugEventListener(isolate, nullptr); expected_context_data = v8::Local<v8::Value>(); CheckDebuggerUnloaded(); } static bool debug_event_break_deoptimize_done = false; static void DebugEventBreakDeoptimize( const v8::Debug::EventDetails& event_details) { v8::DebugEvent event = event_details.GetEvent(); v8::Local<v8::Object> exec_state = event_details.GetExecutionState(); v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext(); if (event == v8::Break) { if (!frame_function_name.IsEmpty()) { // Get the name of the function. const int argc = 2; v8::Local<v8::Value> argv[argc] = { exec_state, v8::Integer::New(CcTest::isolate(), 0)}; v8::Local<v8::Value> result = frame_function_name->Call(context, exec_state, argc, argv) .ToLocalChecked(); if (!result->IsUndefined()) { char fn[80]; CHECK(result->IsString()); v8::Local<v8::String> function_name( result->ToString(context).ToLocalChecked()); function_name->WriteUtf8(fn); if (strcmp(fn, "bar") == 0) { i::Deoptimizer::DeoptimizeAll(CcTest::i_isolate()); debug_event_break_deoptimize_done = true; } } } v8::debug::DebugBreak(CcTest::isolate()); } } // Test deoptimization when execution is broken using the debug break stack // check interrupt. TEST(DeoptimizeDuringDebugBreak) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); env.ExposeDebug(); v8::Local<v8::Context> context = env.context(); // Create a function for checking the function when hitting a break point. frame_function_name = CompileFunction(&env, frame_function_name_source, "frame_function_name"); // Set a debug event listener which will keep interrupting execution until // debug break. When inside function bar it will deoptimize all functions. // This tests lazy deoptimization bailout for the stack check, as the first // time in function bar when using debug break and no break points will be at // the initial stack check. SetDebugEventListener(env->GetIsolate(), DebugEventBreakDeoptimize); // Compile and run function bar which will optimize it for some flag settings. v8::Local<v8::Function> f = CompileFunction(&env, "function bar(){}", "bar"); f->Call(context, v8::Undefined(env->GetIsolate()), 0, NULL).ToLocalChecked(); // Set debug break and call bar again. v8::debug::DebugBreak(env->GetIsolate()); f->Call(context, v8::Undefined(env->GetIsolate()), 0, NULL).ToLocalChecked(); CHECK(debug_event_break_deoptimize_done); SetDebugEventListener(env->GetIsolate(), nullptr); } static void DebugEventBreakWithOptimizedStack( const v8::Debug::EventDetails& event_details) { v8::Isolate* isolate = event_details.GetEventContext()->GetIsolate(); v8::DebugEvent event = event_details.GetEvent(); v8::Local<v8::Object> exec_state = event_details.GetExecutionState(); v8::Local<v8::Context> context = isolate->GetCurrentContext(); if (event == v8::Break) { if (!frame_function_name.IsEmpty()) { for (int i = 0; i < 2; i++) { const int argc = 2; v8::Local<v8::Value> argv[argc] = {exec_state, v8::Integer::New(isolate, i)}; // Get the name of the function in frame i. v8::Local<v8::Value> result = frame_function_name->Call(context, exec_state, argc, argv) .ToLocalChecked(); CHECK(result->IsString()); v8::Local<v8::String> function_name( result->ToString(context).ToLocalChecked()); CHECK( function_name->Equals(context, v8_str(isolate, "loop")).FromJust()); // Get the name of the first argument in frame i. result = frame_argument_name->Call(context, exec_state, argc, argv) .ToLocalChecked(); CHECK(result->IsString()); v8::Local<v8::String> argument_name( result->ToString(context).ToLocalChecked()); CHECK(argument_name->Equals(context, v8_str(isolate, "count")) .FromJust()); // Get the value of the first argument in frame i. If the // function is optimized the value will be undefined, otherwise // the value will be '1 - i'. // // TODO(3141533): We should be able to get the real value for // optimized frames. result = frame_argument_value->Call(context, exec_state, argc, argv) .ToLocalChecked(); CHECK(result->IsUndefined() || (result->Int32Value(context).FromJust() == 1 - i)); // Get the name of the first local variable. result = frame_local_name->Call(context, exec_state, argc, argv) .ToLocalChecked(); CHECK(result->IsString()); v8::Local<v8::String> local_name( result->ToString(context).ToLocalChecked()); CHECK(local_name->Equals(context, v8_str(isolate, "local")).FromJust()); // Get the value of the first local variable. If the function // is optimized the value will be undefined, otherwise it will // be 42. // // TODO(3141533): We should be able to get the real value for // optimized frames. result = frame_local_value->Call(context, exec_state, argc, argv) .ToLocalChecked(); CHECK(result->IsUndefined() || (result->Int32Value(context).FromJust() == 42)); } } } } static void ScheduleBreak(const v8::FunctionCallbackInfo<v8::Value>& args) { SetDebugEventListener(args.GetIsolate(), DebugEventBreakWithOptimizedStack); v8::debug::DebugBreak(args.GetIsolate()); } TEST(DebugBreakStackInspection) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); v8::Local<v8::Context> context = env.context(); frame_function_name = CompileFunction(&env, frame_function_name_source, "frame_function_name"); frame_argument_name = CompileFunction(&env, frame_argument_name_source, "frame_argument_name"); frame_argument_value = CompileFunction(&env, frame_argument_value_source, "frame_argument_value"); frame_local_name = CompileFunction(&env, frame_local_name_source, "frame_local_name"); frame_local_value = CompileFunction(&env, frame_local_value_source, "frame_local_value"); v8::Local<v8::FunctionTemplate> schedule_break_template = v8::FunctionTemplate::New(env->GetIsolate(), ScheduleBreak); v8::Local<v8::Function> schedule_break = schedule_break_template->GetFunction(context).ToLocalChecked(); CHECK(env->Global() ->Set(context, v8_str("scheduleBreak"), schedule_break) .FromJust()); const char* src = "function loop(count) {" " var local = 42;" " if (count < 1) { scheduleBreak(); loop(count + 1); }" "}" "loop(0);"; v8::Script::Compile(context, v8_str(env->GetIsolate(), src)) .ToLocalChecked() ->Run(context) .ToLocalChecked(); } // Test that setting the terminate execution flag during debug break processing. static void TestDebugBreakInLoop(const char* loop_head, const char** loop_bodies, const char* loop_tail) { // Receive 10 breaks for each test and then terminate JavaScript execution. static const int kBreaksPerTest = 10; for (int i = 0; loop_bodies[i] != NULL; i++) { // Perform a lazy deoptimization after various numbers of breaks // have been hit. EmbeddedVector<char, 1024> buffer; SNPrintF(buffer, "function f() {%s%s%s}", loop_head, loop_bodies[i], loop_tail); i::PrintF("%s\n", buffer.start()); for (int j = 0; j < 3; j++) { break_point_hit_count_deoptimize = j; if (j == 2) { break_point_hit_count_deoptimize = kBreaksPerTest; } break_point_hit_count = 0; max_break_point_hit_count = kBreaksPerTest; terminate_after_max_break_point_hit = true; // Function with infinite loop. CompileRun(buffer.start()); // Set the debug break to enter the debugger as soon as possible. v8::debug::DebugBreak(CcTest::isolate()); // Call function with infinite loop. CompileRun("f();"); CHECK_EQ(kBreaksPerTest, break_point_hit_count); CHECK(!CcTest::isolate()->IsExecutionTerminating()); } } } static const char* loop_bodies_1[] = {"", "g()", "if (a == 0) { g() }", "if (a == 1) { g() }", "if (a == 0) { g() } else { h() }", "if (a == 0) { continue }", NULL}; static const char* loop_bodies_2[] = { "if (a == 1) { continue }", "switch (a) { case 1: g(); }", "switch (a) { case 1: continue; }", "switch (a) { case 1: g(); break; default: h() }", "switch (a) { case 1: continue; break; default: h() }", NULL}; void DebugBreakLoop(const char* loop_header, const char** loop_bodies, const char* loop_footer) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); // Register a debug event listener which sets the break flag and counts. SetDebugEventListener(env->GetIsolate(), DebugEventBreakMax); CompileRun( "var a = 1;\n" "function g() { }\n" "function h() { }"); TestDebugBreakInLoop(loop_header, loop_bodies, loop_footer); // Get rid of the debug event listener. SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } TEST(DebugBreakInWhileTrue1) { DebugBreakLoop("while (true) {", loop_bodies_1, "}"); } TEST(DebugBreakInWhileTrue2) { DebugBreakLoop("while (true) {", loop_bodies_2, "}"); } TEST(DebugBreakInWhileCondition1) { DebugBreakLoop("while (a == 1) {", loop_bodies_1, "}"); } TEST(DebugBreakInWhileCondition2) { DebugBreakLoop("while (a == 1) {", loop_bodies_2, "}"); } TEST(DebugBreakInDoWhileTrue1) { DebugBreakLoop("do {", loop_bodies_1, "} while (true)"); } TEST(DebugBreakInDoWhileTrue2) { DebugBreakLoop("do {", loop_bodies_2, "} while (true)"); } TEST(DebugBreakInDoWhileCondition1) { DebugBreakLoop("do {", loop_bodies_1, "} while (a == 1)"); } TEST(DebugBreakInDoWhileCondition2) { DebugBreakLoop("do {", loop_bodies_2, "} while (a == 1)"); } TEST(DebugBreakInFor1) { DebugBreakLoop("for (;;) {", loop_bodies_1, "}"); } TEST(DebugBreakInFor2) { DebugBreakLoop("for (;;) {", loop_bodies_2, "}"); } TEST(DebugBreakInForCondition1) { DebugBreakLoop("for (;a == 1;) {", loop_bodies_1, "}"); } TEST(DebugBreakInForCondition2) { DebugBreakLoop("for (;a == 1;) {", loop_bodies_2, "}"); } v8::Local<v8::Script> inline_script; static void DebugBreakInlineListener( const v8::Debug::EventDetails& event_details) { v8::DebugEvent event = event_details.GetEvent(); v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext(); if (event != v8::Break) return; int expected_frame_count = 4; int expected_line_number[] = {1, 4, 7, 12}; i::Handle<i::Object> compiled_script = v8::Utils::OpenHandle(*inline_script); i::Handle<i::Script> source_script = i::Handle<i::Script>(i::Script::cast( i::JSFunction::cast(*compiled_script)->shared()->script())); int break_id = CcTest::i_isolate()->debug()->break_id(); char script[128]; i::Vector<char> script_vector(script, sizeof(script)); SNPrintF(script_vector, "%%GetFrameCount(%d)", break_id); v8::Local<v8::Value> result = CompileRun(script); int frame_count = result->Int32Value(context).FromJust(); CHECK_EQ(expected_frame_count, frame_count); for (int i = 0; i < frame_count; i++) { // The 6. element in the returned array of GetFrameDetails contains the // source position of that frame. SNPrintF(script_vector, "%%GetFrameDetails(%d, %d)[6]", break_id, i); v8::Local<v8::Value> result = CompileRun(script); CHECK_EQ(expected_line_number[i], i::Script::GetLineNumber(source_script, result->Int32Value(context).FromJust())); } SetDebugEventListener(CcTest::isolate(), nullptr); CcTest::isolate()->TerminateExecution(); } TEST(DebugBreakInline) { i::FLAG_allow_natives_syntax = true; DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); v8::Local<v8::Context> context = env.context(); const char* source = "function debug(b) { \n" " if (b) debugger; \n" "} \n" "function f(b) { \n" " debug(b) \n" "}; \n" "function g(b) { \n" " f(b); \n" "}; \n" "g(false); \n" "g(false); \n" "%OptimizeFunctionOnNextCall(g); \n" "g(true);"; SetDebugEventListener(env->GetIsolate(), DebugBreakInlineListener); inline_script = v8::Script::Compile(context, v8_str(env->GetIsolate(), source)) .ToLocalChecked(); inline_script->Run(context).ToLocalChecked(); } static void DebugEventStepNext( const v8::Debug::EventDetails& event_details) { v8::DebugEvent event = event_details.GetEvent(); if (event == v8::Break) { PrepareStep(StepNext); } } static void RunScriptInANewCFrame(const char* source) { v8::TryCatch try_catch(CcTest::isolate()); CompileRun(source); CHECK(try_catch.HasCaught()); } TEST(Regress131642) { // Bug description: // When doing StepNext through the first script, the debugger is not reset // after exiting through exception. A flawed implementation enabling the // debugger to step into Array.prototype.forEach breaks inside the callback // for forEach in the second script under the assumption that we are in a // recursive call. In an attempt to step out, we crawl the stack using the // recorded frame pointer from the first script and fail when not finding it // on the stack. DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); SetDebugEventListener(env->GetIsolate(), DebugEventStepNext); // We step through the first script. It exits through an exception. We run // this inside a new frame to record a different FP than the second script // would expect. const char* script_1 = "debugger; throw new Error();"; RunScriptInANewCFrame(script_1); // The second script uses forEach. const char* script_2 = "[0].forEach(function() { });"; CompileRun(script_2); SetDebugEventListener(env->GetIsolate(), nullptr); } // Import from test-heap.cc namespace v8 { namespace internal { namespace heap { int CountNativeContexts(); } // namespace heap } // namespace internal } // namespace v8 static void NopListener(const v8::Debug::EventDetails& event_details) { } TEST(DebuggerCreatesContextIffActive) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); CHECK_EQ(1, v8::internal::heap::CountNativeContexts()); SetDebugEventListener(env->GetIsolate(), nullptr); CompileRun("debugger;"); CHECK_EQ(1, v8::internal::heap::CountNativeContexts()); SetDebugEventListener(env->GetIsolate(), NopListener); CompileRun("debugger;"); CHECK_EQ(2, v8::internal::heap::CountNativeContexts()); SetDebugEventListener(env->GetIsolate(), nullptr); } TEST(LiveEditEnabled) { v8::internal::FLAG_allow_natives_syntax = true; LocalContext env; v8::HandleScope scope(env->GetIsolate()); v8::debug::SetLiveEditEnabled(env->GetIsolate(), true); CompileRun("%LiveEditCompareStrings('', '')"); } TEST(LiveEditDisabled) { v8::internal::FLAG_allow_natives_syntax = true; LocalContext env; v8::HandleScope scope(env->GetIsolate()); v8::debug::SetLiveEditEnabled(env->GetIsolate(), false); CompileRun("%LiveEditCompareStrings('', '')"); } TEST(PrecompiledFunction) { // Regression test for crbug.com/346207. If we have preparse data, parsing the // function in the presence of the debugger (and breakpoints) should still // succeed. The bug was that preparsing was done lazily and parsing was done // eagerly, so, the symbol streams didn't match. DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); env.ExposeDebug(); SetDebugEventListener(env->GetIsolate(), DebugBreakInlineListener); v8::Local<v8::Function> break_here = CompileFunction(&env, "function break_here(){}", "break_here"); SetBreakPoint(break_here, 0); const char* source = "var a = b = c = 1; \n" "function this_is_lazy() { \n" // This symbol won't appear in the preparse data. " var a; \n" "} \n" "function bar() { \n" " return \"bar\"; \n" "}; \n" "a = b = c = 2; \n" "bar(); \n"; v8::Local<v8::Value> result = ParserCacheCompileRun(source); CHECK(result->IsString()); v8::String::Utf8Value utf8(env->GetIsolate(), result); CHECK_EQ(0, strcmp("bar", *utf8)); SetDebugEventListener(env->GetIsolate(), nullptr); CheckDebuggerUnloaded(); } static void DebugBreakStackTraceListener( const v8::Debug::EventDetails& event_details) { v8::StackTrace::CurrentStackTrace(CcTest::isolate(), 10); } static void AddDebugBreak(const v8::FunctionCallbackInfo<v8::Value>& args) { v8::debug::DebugBreak(args.GetIsolate()); } TEST(DebugBreakStackTrace) { DebugLocalContext env; v8::HandleScope scope(env->GetIsolate()); SetDebugEventListener(env->GetIsolate(), DebugBreakStackTraceListener); v8::Local<v8::Context> context = env.context(); v8::Local<v8::FunctionTemplate> add_debug_break_template = v8::FunctionTemplate::New(env->GetIsolate(), AddDebugBreak); v8::Local<v8::Function> add_debug_break = add_debug_break_template->GetFunction(context).ToLocalChecked(); CHECK(env->Global() ->Set(context, v8_str("add_debug_break"), add_debug_break) .FromJust()); CompileRun("(function loop() {" " for (var j = 0; j < 1000; j++) {" " for (var i = 0; i < 1000; i++) {" " if (i == 999) add_debug_break();" " }" " }" "})()"); } v8::base::Semaphore terminate_requested_semaphore(0); v8::base::Semaphore terminate_fired_semaphore(0); bool terminate_already_fired = false; static void DebugBreakTriggerTerminate( const v8::Debug::EventDetails& event_details) { if (event_details.GetEvent() != v8::Break || terminate_already_fired) return; terminate_requested_semaphore.Signal(); // Wait for at most 2 seconds for the terminate request. CHECK(terminate_fired_semaphore.WaitFor(v8::base::TimeDelta::FromSeconds(2))); terminate_already_fired = true; } class TerminationThread : public v8::base::Thread { public: explicit TerminationThread(v8::Isolate* isolate) : Thread(Options("terminator")), isolate_(isolate) {} virtual void Run() { terminate_requested_semaphore.Wait(); isolate_->TerminateExecution(); terminate_fired_semaphore.Signal(); } private: v8::Isolate* isolate_; }; TEST(DebugBreakOffThreadTerminate) { DebugLocalContext env; v8::Isolate* isolate = env->GetIsolate(); v8::HandleScope scope(isolate); SetDebugEventListener(isolate, DebugBreakTriggerTerminate); TerminationThread terminator(isolate); terminator.Start(); v8::TryCatch try_catch(env->GetIsolate()); v8::debug::DebugBreak(isolate); CompileRun("while (true);"); CHECK(try_catch.HasTerminated()); } static void DebugEventExpectNoException( const v8::Debug::EventDetails& event_details) { v8::DebugEvent event = event_details.GetEvent(); CHECK_NE(v8::Exception, event); } static void TryCatchWrappedThrowCallback( const v8::FunctionCallbackInfo<v8::Value>& args) { v8::TryCatch try_catch(args.GetIsolate()); CompileRun("throw 'rejection';"); CHECK(try_catch.HasCaught()); } TEST(DebugPromiseInterceptedByTryCatch) { DebugLocalContext env; v8::Isolate* isolate = env->GetIsolate(); v8::HandleScope scope(isolate); SetDebugEventListener(isolate, &DebugEventExpectNoException); v8::Local<v8::Context> context = env.context(); ChangeBreakOnException(false, true); v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(isolate, TryCatchWrappedThrowCallback); CHECK(env->Global() ->Set(context, v8_str("fun"), fun->GetFunction(context).ToLocalChecked()) .FromJust()); CompileRun("var p = new Promise(function(res, rej) { fun(); res(); });"); CompileRun( "var r;" "p.then(function() { r = 'resolved'; }," " function() { r = 'rejected'; });"); CHECK(CompileRun("r")->Equals(context, v8_str("resolved")).FromJust()); } static int exception_event_counter = 0; static void DebugEventCountException( const v8::Debug::EventDetails& event_details) { v8::DebugEvent event = event_details.GetEvent(); if (event == v8::Exception) exception_event_counter++; } static void ThrowCallback(const v8::FunctionCallbackInfo<v8::Value>& args) { CompileRun("throw 'rejection';"); } TEST(DebugPromiseRejectedByCallback) { DebugLocalContext env; v8::Isolate* isolate = env->GetIsolate(); v8::HandleScope scope(isolate); SetDebugEventListener(isolate, &DebugEventCountException); v8::Local<v8::Context> context = env.context(); ChangeBreakOnException(false, true); exception_event_counter = 0; v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(isolate, ThrowCallback); CHECK(env->Global() ->Set(context, v8_str("fun"), fun->GetFunction(context).ToLocalChecked()) .FromJust()); CompileRun("var p = new Promise(function(res, rej) { fun(); res(); });"); CompileRun( "var r;" "p.then(function() { r = 'resolved'; }," " function(e) { r = 'rejected' + e; });"); CHECK( CompileRun("r")->Equals(context, v8_str("rejectedrejection")).FromJust()); CHECK_EQ(1, exception_event_counter); } static void DebugHarmonyScopingListener( const v8::Debug::EventDetails& event_details) { v8::DebugEvent event = event_details.GetEvent(); if (event != v8::Break) return; int break_id = CcTest::i_isolate()->debug()->break_id(); char script[128]; i::Vector<char> script_vector(script, sizeof(script)); SNPrintF(script_vector, "%%GetFrameCount(%d)", break_id); ExpectInt32(script, 1); SNPrintF(script_vector, "var frame = new FrameMirror(%d, 0);", break_id); CompileRun(script); ExpectInt32("frame.evaluate('x').value_", 1); ExpectInt32("frame.evaluate('y').value_", 2); CompileRun("var allScopes = frame.allScopes()"); ExpectInt32("allScopes.length", 2); ExpectBoolean("allScopes[0].scopeType() === ScopeType.Script", true); ExpectInt32("allScopes[0].scopeObject().value_.x", 1); ExpectInt32("allScopes[0].scopeObject().value_.y", 2); CompileRun("allScopes[0].setVariableValue('x', 5);"); CompileRun("allScopes[0].setVariableValue('y', 6);"); ExpectInt32("frame.evaluate('x + y').value_", 11); } TEST(DebugBreakInLexicalScopes) { i::FLAG_allow_natives_syntax = true; DebugLocalContext env; v8::Isolate* isolate = env->GetIsolate(); v8::HandleScope scope(isolate); SetDebugEventListener(isolate, DebugHarmonyScopingListener); CompileRun( "'use strict'; \n" "let x = 1; \n"); ExpectInt32( "'use strict'; \n" "let y = 2; \n" "debugger; \n" "x * y", 30); ExpectInt32( "x = 1; y = 2; \n" "debugger;" "x * y", 30); } static int after_compile_handler_depth = 0; static void HandleInterrupt(v8::Isolate* isolate, void* data) { CHECK_EQ(0, after_compile_handler_depth); } static void NoInterruptsOnDebugEvent( const v8::Debug::EventDetails& event_details) { if (event_details.GetEvent() != v8::AfterCompile) return; ++after_compile_handler_depth; // Do not allow nested AfterCompile events. CHECK(after_compile_handler_depth <= 1); v8::Isolate* isolate = event_details.GetEventContext()->GetIsolate(); v8::Isolate::AllowJavascriptExecutionScope allow_script(isolate); isolate->RequestInterrupt(&HandleInterrupt, nullptr); CompileRun("function foo() {}; foo();"); --after_compile_handler_depth; } TEST(NoInterruptsInDebugListener) { DebugLocalContext env; SetDebugEventListener(env->GetIsolate(), NoInterruptsOnDebugEvent); CompileRun("void(0);"); } TEST(BreakLocationIterator) { DebugLocalContext env; v8::Isolate* isolate = env->GetIsolate(); i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); v8::HandleScope scope(isolate); v8::Local<v8::Value> result = CompileRun( "function f() {\n" " debugger; \n" " f(); \n" " debugger; \n" "} \n" "f"); Handle<i::Object> function_obj = v8::Utils::OpenHandle(*result); Handle<i::JSFunction> function = Handle<i::JSFunction>::cast(function_obj); Handle<i::SharedFunctionInfo> shared(function->shared()); EnableDebugger(isolate); CHECK(i_isolate->debug()->EnsureBreakInfo(shared)); Handle<i::DebugInfo> debug_info(shared->GetDebugInfo()); { i::BreakIterator iterator(debug_info); CHECK(iterator.GetBreakLocation().IsDebuggerStatement()); CHECK_EQ(17, iterator.GetBreakLocation().position()); iterator.Next(); CHECK(iterator.GetBreakLocation().IsDebugBreakSlot()); CHECK_EQ(32, iterator.GetBreakLocation().position()); iterator.Next(); CHECK(iterator.GetBreakLocation().IsCall()); CHECK_EQ(32, iterator.GetBreakLocation().position()); iterator.Next(); CHECK(iterator.GetBreakLocation().IsDebuggerStatement()); CHECK_EQ(47, iterator.GetBreakLocation().position()); iterator.Next(); CHECK(iterator.GetBreakLocation().IsReturn()); CHECK_EQ(60, iterator.GetBreakLocation().position()); iterator.Next(); CHECK(iterator.Done()); } DisableDebugger(isolate); } size_t current_action = 0; StepAction actions[] = {StepNext, StepNext}; static void DebugStepOverFunctionWithCaughtExceptionListener( const v8::Debug::EventDetails& event_details) { v8::DebugEvent event = event_details.GetEvent(); if (event != v8::Break) return; ++break_point_hit_count; if (current_action >= 2) return; PrepareStep(actions[current_action]); } TEST(DebugStepOverFunctionWithCaughtException) { i::FLAG_allow_natives_syntax = true; DebugLocalContext env; v8::Isolate* isolate = env->GetIsolate(); v8::HandleScope scope(isolate); SetDebugEventListener(isolate, DebugStepOverFunctionWithCaughtExceptionListener); break_point_hit_count = 0; CompileRun( "function foo() {\n" " try { throw new Error(); } catch (e) {}\n" "}\n" "debugger;\n" "foo();\n" "foo();\n"); SetDebugEventListener(env->GetIsolate(), nullptr); CHECK_EQ(4, break_point_hit_count); } bool out_of_memory_callback_called = false; void OutOfMemoryCallback(void* data) { out_of_memory_callback_called = true; reinterpret_cast<v8::Isolate*>(data)->IncreaseHeapLimitForDebugging(); } UNINITIALIZED_TEST(DebugSetOutOfMemoryListener) { v8::Isolate::CreateParams create_params; create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); create_params.constraints.set_max_old_space_size(10); v8::Isolate* isolate = v8::Isolate::New(create_params); i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); { v8::Isolate::Scope i_scope(isolate); v8::HandleScope scope(isolate); LocalContext context(isolate); v8::debug::SetOutOfMemoryCallback(isolate, OutOfMemoryCallback, reinterpret_cast<void*>(isolate)); CHECK(!out_of_memory_callback_called); // The following allocation fails unless the out-of-memory callback // increases the heap limit. int length = 10 * i::MB / i::kPointerSize; i_isolate->factory()->NewFixedArray(length, i::TENURED); CHECK(out_of_memory_callback_called); } isolate->Dispose(); } TEST(DebugCoverage) { i::FLAG_always_opt = false; LocalContext env; v8::Isolate* isolate = env->GetIsolate(); v8::HandleScope scope(isolate); v8::debug::Coverage::SelectMode(isolate, v8::debug::Coverage::kPreciseCount); v8::Local<v8::String> source = v8_str( "function f() {\n" "}\n" "f();\n" "f();"); CompileRun(source); v8::debug::Coverage coverage = v8::debug::Coverage::CollectPrecise(isolate); CHECK_EQ(1u, coverage.ScriptCount()); v8::debug::Coverage::ScriptData script_data = coverage.GetScriptData(0); v8::Local<v8::debug::Script> script = script_data.GetScript(); CHECK(script->Source() .ToLocalChecked() ->Equals(env.local(), source) .FromMaybe(false)); CHECK_EQ(2u, script_data.FunctionCount()); v8::debug::Coverage::FunctionData function_data = script_data.GetFunctionData(0); v8::debug::Location start = script->GetSourceLocation(function_data.StartOffset()); v8::debug::Location end = script->GetSourceLocation(function_data.EndOffset()); CHECK_EQ(0, start.GetLineNumber()); CHECK_EQ(0, start.GetColumnNumber()); CHECK_EQ(3, end.GetLineNumber()); CHECK_EQ(4, end.GetColumnNumber()); CHECK_EQ(1, function_data.Count()); function_data = script_data.GetFunctionData(1); start = script->GetSourceLocation(function_data.StartOffset()); end = script->GetSourceLocation(function_data.EndOffset()); CHECK_EQ(0, start.GetLineNumber()); CHECK_EQ(0, start.GetColumnNumber()); CHECK_EQ(1, end.GetLineNumber()); CHECK_EQ(1, end.GetColumnNumber()); CHECK_EQ(2, function_data.Count()); } TEST(BuiltinsExceptionPrediction) { v8::Isolate* isolate = CcTest::isolate(); v8::HandleScope handle_scope(isolate); v8::Context::New(isolate); // TODO(gsathya): Fix catch prediction for the following. std::set<int> whitelist( {i::Builtins::kPromiseThenFinally, i::Builtins::kPromiseCatchFinally}); i::Builtins* builtins = CcTest::i_isolate()->builtins(); bool fail = false; for (int i = 0; i < i::Builtins::builtin_count; i++) { Code* builtin = builtins->builtin(static_cast<i::Builtins::Name>(i)); if (builtin->kind() != Code::BUILTIN) continue; if (whitelist.find(i) != whitelist.end()) continue; auto prediction = builtin->GetBuiltinCatchPrediction(); USE(prediction); } CHECK(!fail); } TEST(DebugGetPossibleBreakpointsReturnLocations) { LocalContext env; v8::Isolate* isolate = env->GetIsolate(); v8::HandleScope scope(isolate); v8::Local<v8::String> source = v8_str( "function fib(x) {\n" " if (x < 0) return;\n" " if (x === 0) return 1;\n" " if (x === 1) return fib(0);\n" " return x > 2 ? fib(x - 1) + fib(x - 2) : fib(1) + fib(0);\n" "}"); CompileRun(source); v8::PersistentValueVector<v8::debug::Script> scripts(isolate); v8::debug::GetLoadedScripts(isolate, scripts); CHECK(scripts.Size() == 1); std::vector<v8::debug::BreakLocation> locations; CHECK(scripts.Get(0)->GetPossibleBreakpoints( v8::debug::Location(0, 17), v8::debug::Location(), true, &locations)); int returns_count = 0; for (size_t i = 0; i < locations.size(); ++i) { if (locations[i].type() == v8::debug::kReturnBreakLocation) { ++returns_count; } } // With Ignition we generate one return location per return statement, // each has line = 5, column = 0 as statement position. CHECK(returns_count == 4); } TEST(DebugEvaluateNoSideEffect) { LocalContext env; i::Isolate* isolate = CcTest::i_isolate(); i::HandleScope scope(isolate); i::List<i::Handle<i::JSFunction>> list; { i::HeapIterator iterator(isolate->heap()); while (i::HeapObject* obj = iterator.next()) { if (!obj->IsJSFunction()) continue; i::JSFunction* fun = i::JSFunction::cast(obj); list.Add(i::Handle<i::JSFunction>(fun)); } } // Perform side effect check on all built-in functions. The side effect check // itself contains additional sanity checks. for (i::Handle<i::JSFunction> fun : list) { bool failed = false; { i::NoSideEffectScope scope(isolate, true); failed = !isolate->debug()->PerformSideEffectCheck(fun); } if (failed) isolate->clear_pending_exception(); } }
13af84ad2b9c1d2d6767ee61437651e808614564
7da54b0879af988c5c18f5bb1f6dd76d711bf93d
/GalaxyWar/c/GW_Tir_W.cpp
607b3cf41148b7f9e7a0e9d06d55720e308c6023
[]
no_license
Spirou003/Old_Galaxywar
e8bc92e6e98db56c28962e4cac3ae162316dac86
d01d0d63f15a4a0517ef942b9a09e8f2cc14b7ac
refs/heads/master
2021-01-18T21:39:18.900712
2016-10-01T15:53:23
2016-10-01T15:53:23
69,746,816
0
0
null
null
null
null
UTF-8
C++
false
false
2,827
cpp
#ifndef GW_TIR_W_C #define GW_TIR_W_C #include "../h/GW_Tir_W.hpp" #include "../h/GW_GraphicsIncluser.hpp" #define GW_NAMECLASS GW_Tir_W bool GW_NAMECLASS::exists = false; Sprite GW_Tir_W::SPRITIR[2]; bool GW_NAMECLASS::IsDead(void) const { return stop; } bool GW_Tir_W::Init(cbool previnit) { if (exists || !previnit) { return false; } Image* ptr[2]; ptr[0] = new Image(1, 5, Color(105, 105, 229)); if (ptr[0] == NULL) { return false; } ptr[1] = new Image(1, 5, Color(229, 105, 105)); if (ptr[1] == NULL) { delete(ptr[0]); return false; } ptr[0]->SetPixel(0, 1, Color(156, 156, 216)); ptr[0]->SetPixel(0, 3, Color(156, 156, 216)); ptr[0]->SetPixel(0, 2, Color(180, 180, 210)); ptr[1]->SetPixel(0, 1, Color(216, 156, 156)); ptr[1]->SetPixel(0, 3, Color(216, 156, 156)); ptr[1]->SetPixel(0, 2, Color(210, 180, 180)); GW_SetNoSmooth(ptr[1], &SPRITIR[0]); GW_SetNoSmooth(ptr[0], &SPRITIR[1]); GW_ToCenter(SPRITIR[0]); GW_ToCenter(SPRITIR[1]); SPRITIR[0].SetScale((GW_HAUTEUR+GW_LARGEUR)*10, 1); SPRITIR[1].SetScale((GW_HAUTEUR+GW_LARGEUR)*10, 1); exists = true; return true; } void GW_Tir_W::Close(void) { if (!exists) { return; } delete(SPRITIR[0].GetImage()); delete(SPRITIR[1].GetImage()); exists = false; } void GW_Tir_W::Draw(RenderWindow* App) { GW_Collisionnable::ClassicDraw(App, &SPRITIR[int(Level)], this); } bool GW_Tir_W::Dead(GW_JeuData* truc, RenderWindow* App) { return true; } const Shape* GW_Tir_W::GetPolygon(void) const { return &Carre; } void GW_NAMECLASS::Stop(void) { stop = true; } void GW_NAMECLASS::Action(GW_JeuData* truc, cfloat dt, bool lim) { p = dt; } float GW_NAMECLASS::GetRadius(void) const { return 5; } GW_NAMECLASS::GW_NAMECLASS(CSInt32 level, cfloat X, cfloat Y, cfloat Angle) { Level = level; scaleX = (GW_HAUTEUR+GW_LARGEUR); scaleY = 2; stop = false; end = false; type = TW; angle = Angle; x = X; y = Y; vx = cos(angle); vy = sin(angle); if (vx < M_SQRT1_2 && vx > -M_SQRT1_2) { x += vx*(GW_HAUTEUR/2 - y)/vy;//(x(A) + (h / 2 - y(A)) vx / vy, h / 2) y = GW_HAUTEUR/2; scaleX = GW_HAUTEUR*vy; } else { y += vy*(GW_LARGEUR/2 - x)/vx;//(x(A) + (h / 2 - y(A)) vx / vy, h / 2) x = GW_LARGEUR/2; scaleX = GW_LARGEUR*vx; } if (scaleX < 0) { scaleX *= -1; } dmax = scaleX; //printf("%d %d == %d %d == %d %d == %d\n", int(x), int(y), int(vx), int(vy), int(scaleX), int(scaleY), int(dmax)); } float GW_NAMECLASS::Power(CSInt32 malus) const { return p*(1+2*Level)*20; } GW_NAMECLASS::~GW_NAMECLASS(void) { ; } #undef GW_NAMECLASS #endif
0421f25f03e097dc45a45b452ae43f0295d09b89
1e8014bb81256091c4b33533e3d298c5354e98b5
/src/OpenGLViewer.cpp
2f75a7eb9d23e5ac7d8f4ebf5073e4ed4777d9f4
[]
no_license
cheneeheng/KINECT
3689e0459ffb6f903f5d483eaf0cf4a16061ac81
b6c4308668e72aabc3fd00542db24ea267ca8454
refs/heads/master
2021-01-19T10:25:34.064421
2017-07-19T13:11:09
2017-07-19T13:11:09
82,189,375
0
0
null
null
null
null
UTF-8
C++
false
false
2,961
cpp
/* * DataFilter.cpp * * Created on: Apr 19, 2017 * Author: chen */ #include "OpenGLViewer.h" OpenGLViewer::OpenGLViewer() { } OpenGLViewer::~OpenGLViewer() { } void OpenGLViewer::Initialize( GLuint &VertexArrayID, GLuint &vertexbuffer, GLuint &colorbuffer, GLuint &vertexbufferAxes, GLuint &colorbufferAxes, GLuint &programID, GLuint &MatrixID, GLuint &ViewMatrixID, GLuint &ModelMatrixID, glm::mat4 &ViewMatrix, glm::mat4 &ProjectionMatrix, std::vector<glm::vec3> &vertices, const std::string &obj_) { glGenVertexArrays(1, &VertexArrayID); glBindVertexArray(VertexArrayID); // [OBJ] ******************************************************************* bool res = loadOBJ(std::string("../object_models/" + obj_ + ".obj").c_str(), vertices); std::vector<glm::vec3> verticesC; for (int i=0;i<vertices.size();i++) verticesC.push_back(glm::vec3(0.0f,1.0f,0.0f)); //FOR THE object glGenBuffers(1, &vertexbuffer); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW); //For the Color glGenBuffers(1, &colorbuffer); glBindBuffer(GL_ARRAY_BUFFER, colorbuffer); glBufferData(GL_ARRAY_BUFFER, verticesC.size() * sizeof(glm::vec3), &verticesC[0], GL_STATIC_DRAW); // ******************************************************************* [OBJ] // [AXES] ****************************************************************** static const GLfloat g_color_buffer_data[] = { 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, }; static const GLfloat g_vertex_buffer_data[] = { 0.0f, 0.0f, 0.0f, 0.2f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.2f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.2f, }; //for axes glGenBuffers(1, &vertexbufferAxes); glBindBuffer(GL_ARRAY_BUFFER, vertexbufferAxes); glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW); //color glGenBuffers(1, &colorbufferAxes); glBindBuffer(GL_ARRAY_BUFFER, colorbufferAxes); glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW); // ****************************************************************** [AXES] // Create and compile our GLSL program from the shaders programID = LoadShaders( "../common/TransformVertexShader.vertexshader", "../common/ColorFragmentShader.fragmentshader" ); // Get a handle for our "MVP" uniform MatrixID = glGetUniformLocation(programID, "MVP"); ViewMatrixID = glGetUniformLocation(programID, "V"); ModelMatrixID = glGetUniformLocation(programID, "M"); ProjectionMatrix = glm::perspective(45.8f, 4.f / 3.f, 0.1f, 100.0f); ViewMatrix = glm::lookAt( glm::vec3( 0, 0, 0.5 ), // Camera is here glm::vec3( 0, 0, 1.5 ), // and looks here glm::vec3( 0, 1, 0 ) // Head is up (set to 0,-1,0 to look upside-down) ); }
9cb6b4f99c90f15f2ec7a6fd84d20774329c83cf
68e0ac801e67b9d4fe9b034044869a5d72cd4be6
/shared/src/util/include/temp_file.hpp
bf0f36c88ed5ac4d1dcca26d0a360faebcc9505a
[ "Apache-2.0" ]
permissive
TGAC/grassroots-api
3336e1c129769abcba9812cf6244b024d2d31caf
9692dcb428fc7a2a93b22a8510abff05deda1234
refs/heads/master
2021-01-17T14:26:02.561826
2018-02-19T19:55:07
2018-02-19T19:55:07
43,677,565
2
0
null
null
null
null
UTF-8
C++
false
false
4,556
hpp
/* ** Copyright 2014-2016 The Earlham Institute ** ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** ** http://www.apache.org/licenses/LICENSE-2.0 ** ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. */ /** * @file * @brief */ /* * temp_file.hpp * * Created on: 22 Apr 2015 * Author: tyrrells */ #ifndef TEMP_FILE_HPP_ #define TEMP_FILE_HPP_ #include <fstream> #include <cstdio> #include "uuid/uuid.h" #include "grassroots_util_library.h" #include "memory_allocations.h" using namespace std; /** * Create a temporary file to read and write data to. * * @ingroup util */ class GRASSROOTS_UTIL_API TempFile { public: /** * Create a TempFile. * * The filename used will be of the form * * \<working_dir_s\>/\<id\>.\<suffix_s\>. * * @param template_s The template for the filename. The filename * generated from this will depend upon the value of tenp_flag. * @param temp_flag If this is <code>true</code> then template_s will be passed * to tmpnam. If not, template_s will be used as is for the filename. * @return The new TempFile upon success or 0 upon failure. */ static TempFile *GetTempFile (const char *template_s, const bool temp_flag); /** * Create a TempFile. * * The filename used will be of the form * * \<working_dir_s\>/\<id\>.\<suffix_s\>. * * @param working_dir_s The directory where the underlying file will be created. * @param id The prefix of the filename. * @param suffix_s The suffix of the filename * @return The new TempFile upon success or 0 upon failure. */ static TempFile *GetTempFile (const char *working_dir_s, const uuid_t id, const char * const suffix_s); /** * The TempFile destructor. */ ~TempFile (); /** * Get the filename being used by this TempFile. * * @return The filename or 0 if it is not set. */ const char *GetFilename () const; /** * Open the TempFile * * @param mode_s The mode string which is the same as the c standard library mode string * used by fopen. * @return <code>true</code> if the file was opened * successfully, <code>false</code> otherwise. */ bool Open (const char *mode_s); /** * Close the TempFile * * @return 0 upon success. The value returned is the same as the c standard library * function fclose. */ int Close (); /** * Print a c-style string to the TempFile * * @param arg_s The strng to print. * @return <code>true</code> if the string was printed * successfully, <code>false</code> otherwise. */ bool Print (const char *arg_s); /** * Get the contents of the TempFile as a c-style string. * * @return The contents of the file or 0 upon error. */ const char *GetData (); /** * Clear the data from the underlying file. */ void ClearData (); /** * Check if a TempFile is currently open. * * @return <code>true</code> if the file is open * <code>false</code> otherwise. */ bool IsOpen () const; private: /** * @private * The actual underlying filename. */ char *tf_name_s; /** * @private * This determines whether tf_name_s is freed when this * TempFile gets deleted. */ MEM_FLAG tf_name_mem; /** * @private * The underlying FILE used. */ FILE *tf_handle_f; /** * @private * The data within the underlying file. */ char *tf_data_s; /** * The TempFile constructor. */ TempFile (); }; #ifdef __cplusplus extern "C" { #endif /** * Create a buffer of the required length to be used by * TempFile *GetTempFile (const char *template_s, const bool temp_flag). * * The filename used will be of the form * * \<working_dir_s\>/\<id\>.\<suffix_s\>. * * @param working_directory_s The directory where the underlying file will be created. * @param prefix_s The prefix of the filename. * @param temp_suffix_s The suffix of the filename * @return The buffer containing the required string or <code>NULL</code> * upon error. * @memberof TempFile */ GRASSROOTS_UTIL_API char *GetTempFilenameBuffer (const char * const working_directory_s, const char * const prefix_s, const char * const temp_suffix_s); #ifdef __cplusplus } #endif #endif /* TEMP_FILE_HPP_ */
4a15f5a86c84f10e5f0392e344bbe775483ead08
7b2069055c33e59a40da9f9ac4138599f412209c
/Lab3/Lab3/MyForm.cpp
69300cd7f5df449e1389a1d57d60b888d1e24623
[]
no_license
Divine5Beauty/Lab.Rab.Three
b90d88b395a1f7992bce70a33271768dba688d26
1c32d4b582eb139cfbbad34e9741ca572ee0b0fd
refs/heads/main
2023-05-03T18:01:26.658201
2021-05-21T18:58:44
2021-05-21T18:58:44
null
0
0
null
null
null
null
WINDOWS-1251
C++
false
false
632
cpp
#include "pch.h" using namespace System; #include "MyForm.h" using namespace Windows::Forms; [STAThread] int main() { Application::EnableVisualStyles(); // включает визуальные стили Application::SetCompatibleTextRenderingDefault(false); // настройка совместимого отображения текста по умолчанию Application::Run(gcnew Lab3::MyForm());//Запускает стандартный цикл обработки сообщений приложения в текущем потоке и делает указанную форму видимой. return 0; }
8dff8e930b1d400474f869467b7016c5d6ddeefd
7d7301514d34006d19b2775ae4f967a299299ed6
/c++primer/Second_1.18_notes/12/12.1.1.shared_ptr.cpp
2186ce4636b27f5dbe07d269289e79572e30b360
[]
no_license
xmlb88/algorithm
ae83ff0e478ea01f37bc686de14f7d009d45731b
cf02d9099569e2638e60029b89fd7b384f3c1a68
refs/heads/master
2023-06-16T00:21:27.922428
2021-07-17T03:46:50
2021-07-17T03:46:50
293,984,271
1
0
null
2020-12-02T09:08:28
2020-09-09T02:44:20
C++
GB18030
C++
false
false
1,086
cpp
#include <iostream> #include <memory> #include <vector> #include <string> using namespace std; void t_shared_ptr() { shared_ptr<string> p1; shared_ptr<list<int>> p2; // 如果p1不为空,检查它是否指向一个空string if (p1 && p1 -> empty()) *p1 = "hi"; // 如果p1指向一个空string,解引用p1,将一个新值赋予string /* shared_ptr<T> sp; unique_ptr<T> up; p; *p; p -> mem 等价于 (*p).mem p.get(); swap(p, q); p.swap(q); */ // shared_ptr独有的操作 // make_shared<T>(args) // 返回一个shared_ptr,指向一个动态分配的类型为T的对象,使用args初始化此对象 auto p = make_shared<stirng>("this"); // shared_ptr<T>p(q) // p是shared_ptr q的拷贝:此操作会递增q中的计数器,q中的指针必须能转换成T* shared_ptr<string> p(q); // p = q p.unique(); // 若p.use_count()为1,返回true,否则返回false p.use_count(); // 返回与p共享对象的智能指针数量;可能很慢,主要用于调试 }
db133ba74c1685ce2159b311f1226670877dd036
f8050958bf3b16d353d0de81709077833d57183b
/s32v234_sdk/libs/apex/llrt/inc/common.hpp
bcd222a7f8d256279fb5162e08cf0907d2b784eb
[ "WTFPL" ]
permissive
zhuhaijun753/Panorama4AIWAYS
9bf0482afc1e69fbf1dabf02b7df4f978efecb49
46e1988e54a5155be3b3b47c486b3f722be00b5c
refs/heads/master
2022-03-31T11:06:24.278727
2020-01-30T03:42:34
2020-01-30T03:42:34
null
0
0
null
null
null
null
UTF-8
C++
false
false
1,309
hpp
/***************************************************************************** * * NXP Confidential Proprietary * * Copyright (c) 2018 NXP Semiconductor; * All Rights Reserved * ***************************************************************************** * * THIS SOFTWARE IS PROVIDED BY NXP "AS IS" AND ANY EXPRESSED OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL NXP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************/ #ifndef _COMMON_HPP #define _COMMON_HPP //maximum 'dimension' support; can increase this if required (this is practically constrained, so an upper limit is reasonable) #define DESC_MAX_NUM_DIM 5 #define DEBUG_LVL 0 #endif //_COMMON_HPP
813bfca85af48ce141896352f3acfa56e41746f4
a05464ab568c7319ae3b81cb496bce9bd9960e1e
/server/src/Server.h
270405fce850901fb489d4ce456c846d3d6f8507
[]
no_license
nathanguigui/CPP_rtype_2019
832d4d1649cce3519deacdd0efeb9f7bcfc49f6c
38c2633a5930a48c95c41f31618118d72a8461b8
refs/heads/master
2020-12-09T09:12:55.246256
2019-12-04T09:16:02
2019-12-04T09:16:02
null
0
0
null
null
null
null
UTF-8
C++
false
false
328
h
// // Created by Lucas on 11/13/2019. // #ifndef CPP_RTYPE_2019_SERVER_H #define CPP_RTYPE_2019_SERVER_H #include <iostream> using namespace std; class Server { public: Server(string IP_Server); ~Server(); void Create_Session(string PartyKey); private: }; #endif //CPP_RTYPE_2019_SERVER_H
0cfbc1df13a4ef746b8f1117abfc8846d2e989bf
dc802fc27398276106626e12ab1e5ae957bde460
/modules/sabi_core/excludeFromBuild/ActiveLoader.h
9a8e847a6e1b077783ba6ec5430b5d5f14c27f57
[ "Unlicense" ]
permissive
simoncblyth/Optix7Sandbox
495e58e017cb94c28a506ce8f9e948526b1eba4d
06c703ea9bd00d65a32ea3dcd26d9fc208655543
refs/heads/master
2020-09-11T13:01:49.553607
2019-10-20T09:18:34
2019-10-20T09:18:34
null
0
0
null
null
null
null
UTF-8
C++
false
false
2,072
h
// This header file was auto-generated by ClassMate++ // Created: 2 Sep 2019 5:16:51 am // Copyright (c) 2019, HurleyWorks #pragma once using sabi::Surface; using sabi::MeshBuffers; using sabi::MeshBuffersHandle; using juce::File; using igl::read_triangle_mesh; using igl::per_vertex_normals; class ActiveLoader { public: ActiveLoader() = default; ~ActiveLoader() = default; void loadMesh(const std::string& path, LoadMeshCallback& callback) { /*File f(path); if (f.getFileExtension() == ".gltf") { return loadGLTF(path, callback); }*/ Eigen::MatrixXd V; Eigen::MatrixXi F; if (!read_triangle_mesh(path, V, F)) { LOG(CRITICAL) << "load mesh failed for: " << path; return; } MeshBuffersHandle m = std::make_shared<MeshBuffers>(); m->V = V.transpose().cast<float>(); // Libigl needs to be transposed // Compute per-vertex normals Eigen::MatrixXd N; per_vertex_normals(V, F, N); m->N = N.transpose().cast<float>(); // Libigl needs to be transposed MatrixXu indices = F.cast<unsigned>(); Surface s; s.indices() = indices.transpose(); // Libigl needs to be transposed m->S.push_back(s); callback(m, path); } #if 0 void loadGLTF(const std::string& path, LoadMeshCallback& callback) { ScopedStopWatch sw(_FN_); tinygltf::TinyGLTF loader; std::string err; std::string warn; tinygltf::Model model; bool ret = loader.LoadASCIIFromFile(&model, &err, &warn, path); if (!warn.empty()) LOG(WARNING) << warn.c_str(); if (!err.empty()) LOG(CRITICAL) << err.c_str(); if (!ret) throw std::runtime_error("Failed to parse glTF: " + path); for (const auto& gltf_buffer : model.buffers) { const uint64_t buf_size = gltf_buffer.data.size(); std::cerr << "Processing glTF buffer '" << gltf_buffer.name << "'\n" << "\tbyte size: " << buf_size << "\n" << "\turi : " << gltf_buffer.uri << std::endl; //scene.addBuffer(buf_size, gltf_buffer.data.data()); } glTFLoader gltf; gltf.processNodes(model, callback); } #endif }; // end class ActiveLoader
ac7c2a8d8f607e370c3d9dcce100255eb114b275
58a0ba5ee99ec7a0bba36748ba96a557eb798023
/CodeForces/Complete/1100-1199/1108A-TwoDistinctPoints.cpp
7ebcb51c1741c9fe49e31f09e01c20bdd7263c2e
[ "MIT" ]
permissive
adityanjr/code-DS-ALGO
5bdd503fb5f70d459c8e9b8e58690f9da159dd53
1c104c33d2f56fe671d586b702528a559925f875
refs/heads/master
2022-10-22T21:22:09.640237
2022-10-18T15:38:46
2022-10-18T15:38:46
217,567,198
40
54
MIT
2022-10-18T15:38:47
2019-10-25T15:50:28
C++
UTF-8
C++
false
false
228
cpp
#include <cstdio> int main(){ long n; scanf("%ld", &n); while(n--){ long ls, rs, lt, rt; scanf("%ld %ld %ld %ld\n", &ls, &rs, &lt, &rt); printf("%ld %ld\n", ls, lt + (ls == lt)); } return 0; }
63c947d8c92974760d91fd8218d141ec4ea7a807
d0fb46aecc3b69983e7f6244331a81dff42d9595
/datalake/include/alibabacloud/datalake/model/DeletePartitionColumnStatisticsRequest.h
676144b055ebdd24a37400002a4f1e0557634276
[ "Apache-2.0" ]
permissive
aliyun/aliyun-openapi-cpp-sdk
3d8d051d44ad00753a429817dd03957614c0c66a
e862bd03c844bcb7ccaa90571bceaa2802c7f135
refs/heads/master
2023-08-29T11:54:00.525102
2023-08-29T03:32:48
2023-08-29T03:32:48
115,379,460
104
82
NOASSERTION
2023-09-14T06:13:33
2017-12-26T02:53:27
C++
UTF-8
C++
false
false
1,738
h
/* * Copyright 2009-2017 Alibaba Cloud All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef ALIBABACLOUD_DATALAKE_MODEL_DELETEPARTITIONCOLUMNSTATISTICSREQUEST_H_ #define ALIBABACLOUD_DATALAKE_MODEL_DELETEPARTITIONCOLUMNSTATISTICSREQUEST_H_ #include <alibabacloud/datalake/DataLakeExport.h> #include <alibabacloud/core/RoaServiceRequest.h> #include <string> #include <vector> #include <map> namespace AlibabaCloud { namespace DataLake { namespace Model { class ALIBABACLOUD_DATALAKE_EXPORT DeletePartitionColumnStatisticsRequest : public RoaServiceRequest { public: DeletePartitionColumnStatisticsRequest(); ~DeletePartitionColumnStatisticsRequest(); std::string getCatalogId() const; void setCatalogId(const std::string &catalogId); std::string getDatabaseName() const; void setDatabaseName(const std::string &databaseName); std::string getTableName() const; void setTableName(const std::string &tableName); private: std::string catalogId_; std::string databaseName_; std::string tableName_; }; } // namespace Model } // namespace DataLake } // namespace AlibabaCloud #endif // !ALIBABACLOUD_DATALAKE_MODEL_DELETEPARTITIONCOLUMNSTATISTICSREQUEST_H_
00af3488a91437983d75a2b0c070f5dc65525d96
e745a83f2da94da5d8a0ec765a6da588572b1b0b
/ebox_stm32f103RCT6_VS/ebox_stm32f103RCT6_VS/example/mpu9250/mpu9250_test.cpp
6ddd1f8805f5e40a9d59647489166ad38c5150a7
[]
no_license
shicaiwei123/Control-switch
6f0735bc143a18cf790b05ecf95c60a77c3220c3
29e7ff124158503fd2237b6569c4b846b7fe135c
refs/heads/master
2021-01-01T06:06:33.828876
2017-07-19T13:20:42
2017-07-19T13:20:42
97,356,917
1
2
null
null
null
null
GB18030
C++
false
false
1,895
cpp
#include "ebox.h" #include "mpu9250.h" #include "my_math.h" #include "uart_vcan.h" FpsCounter fps; MPU9250AHRS mpu(&i2c1,MPU6500_Model_6555); UartVscan vscan(&uart1); #define SEND_ANGLE //#define SEND_RAW void setup() { ebox_init(); uart1.begin(115200); mpu.setGyroBias(-0.0151124271, -0.00376615906, 0.0124653624); mpu.setAccelBias(-0.00981201138, -0.00825439487, 0.146179199); mpu.setMagBiasSens( -18.786200, 17.835992, 14.496549, 0.986133, 1.038038, 0.975829); mpu.begin(200000, 200, MPU6500_Gyro_Full_Scale_500dps, MPU6500_Accel_Full_Scale_4g); fps.begin(); } int main(void) { setup(); #ifdef SEND_RAW float t, gx, gy, gz, ax, ay, az, mx, my, mz; #endif // SEND_RAW #ifdef SEND_ANGLE float data[5]; #endif // SEND_ANGLE while (1) { #ifdef SEND_ANGLE mpu.getAngle(data, data + 1, data + 2); data[3] = fps.getFps(); vscan.sendOscilloscope(data, 4); #endif // SEND_ANGLE #ifdef SEND_RAW mpu.getGyro(&gx, &gy, &gz); mpu.getAccel(&ax, &ay, &az); mpu.getMag(&mx, &my, &mz); //t = mpu.getTemp(); //uart1.printf("%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\r\n", // t, gx, gy, gz, ax, ay, az, fps.getFps());//读取六轴标准单位数据 //uart1.printf("%.3f\t%.3f\t%.3f\t%.3f\r\n", // mx, my, mz, fps.getFps());//读取磁力计标准单位数据 //uart1.printf("%.3f\t%.3f\t%.3f\r\n", // mx, my, mz);//读取磁力计标准单位数据 uart1.printf("%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\r\n", t, gx, gy, gz, ax, ay, az, mx, my, mz,fps.getFps());//读取九轴标准单位数据 //uart1.printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%.3f\r\n", // t, gx, gy, gz, ax, ay, az, fps.getFps());//读取六轴原始数据 //uart1.printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%.3f\r\n", // t, gx, gy, gz, ax, ay, az, mx, my, mz, fps.getFps());//读取九轴原始数据 #endif // SEND_RAW delay_ms(2); } }
52df8528777f5aedd40989b563f7285f9b86463c
b4e50bd35fa8bd07824edf17d5f91147f506c9a8
/EX12ListaN3.cpp
55a7fecec5bfdbd50ef83f7075b443aac8d9cf0d
[]
no_license
teago83/ExerciciosAntigosCPP
3065e26bb0299bd8418f4cff718d072ee6873cb1
4d7130f9e1044748ad18021cb168c9f83c73c8e8
refs/heads/master
2020-07-18T01:58:46.412881
2019-09-03T18:47:12
2019-09-03T18:47:12
206,149,230
0
0
null
null
null
null
ISO-8859-1
C++
false
false
541
cpp
//12. Escreva um algoritmo que leia um vetor inteiro de 30 posições e crie um segundo vetor, substituindo os valores nulos por 1. Mostre os 2 vetores. #include<iostream> #include<conio.h> #include<stdio.h> using namespace std; int v[30],vn; int main(){ cout<<"Digite os valores bem doidos pow:"<<endl; for(int i=0;i<30;i++){ cout<<"\nValor numero ("<<i<<"):"<<endl; cin>>v[i]; if(v[i]==0){ v[i]=1; vn++; } } for(int i=0;i<30;i++){ cout<<"\n"<<v[i]; } cout<<"\n\nNumero de valores nulos: "<<vn; return 0; }
c5e09990764199eeaf595257c5b18c9128fbe4d4
7bb98be114a835c04cb6c8454ecee85b15897b05
/src/fileview.cpp
4ab3ca37e5232bfa09a72691471038ccec281ddd
[ "BSD-3-Clause" ]
permissive
renzibei/Filesys
f3e09beb4d2e99488997123a38a3b0e0db708aa6
7919654f71d3254c52f9a46321bc450f4a9c1b93
refs/heads/master
2021-05-01T03:10:06.780778
2020-06-15T16:48:31
2020-06-15T16:48:31
121,186,883
1
0
null
2018-02-12T01:41:50
2018-02-12T01:41:50
null
UTF-8
C++
false
false
9,423
cpp
#include "fileview.h" #include <iostream> FileView::FileView(QWidget *parent) : QDialog(parent) { this->setWindowIcon(QIcon(":/new/icon/vfs_icon.svg")); InitDisk(); setWindowTitle( tr( "Virtual File System" ) ); WorkPathEdit = new QLineEdit(this); WorkPathEdit->setText( tr( "/" ) ); GoToBtn = new QPushButton; GoToBtn->setIcon(QIcon(":/new/icon/right.svg")); connect(GoToBtn, SIGNAL(clicked()), this, SLOT(slotGoTo())); QHBoxLayout * h_layout1 = new QHBoxLayout; h_layout1->addWidget(WorkPathEdit); h_layout1->addWidget(GoToBtn); FileListWidget = new DirList(this); FileListWidget->setViewMode(QListView::IconMode); UpFolBtn = new QPushButton; //UpFolBtn->setFixedWidth(32); UpFolBtn->setIcon(QIcon(":/new/icon/up.svg")); connect(UpFolBtn, SIGNAL(clicked()), this, SLOT(slotGoUpDIr())); NewFolBtn = new QPushButton; NewFolBtn->setIcon(QIcon(":/new/icon/new_folder.svg")); NewFileBtn = new QPushButton; NewFileBtn->setIcon(QIcon(":/new/icon/new_doc.svg")); //NewFolBtn->setText(tr("新建文件夹")); //NewFolBtn->setFixedHeight(40); connect(NewFolBtn, SIGNAL(clicked()), FileListWidget, SLOT(slotNewDir())); connect(NewFileBtn, SIGNAL(clicked()), FileListWidget, SLOT(slotNewFile())); QHBoxLayout * h_layout2 = new QHBoxLayout; h_layout2->addWidget(NewFolBtn); h_layout2->addWidget(NewFileBtn); h_layout2->addWidget(UpFolBtn); QVBoxLayout * v_layout = new QVBoxLayout(this); v_layout->addLayout(h_layout1); v_layout->addLayout(h_layout2); v_layout->addWidget( FileListWidget ); this->setLayout(v_layout); connect( FileListWidget, SIGNAL( itemDoubleClicked( QListWidgetItem * ) ), this, SLOT( slotShowDir( QListWidgetItem * ) ) ); UpdateFileList(); } DirList::DirList(QWidget *parent) : QListWidget(parent) { setContextMenuPolicy(Qt::DefaultContextMenu); } DirList::~DirList() { } void DirList::contextMenuEvent( QContextMenuEvent * event ) { QMenu* popMenu = new QMenu(this); QAction *add_folder_action = new QAction("新建文件夹", this); QAction *add_file_action = new QAction("新建文件", this); QAction *rename_action = new QAction("重命名", this); QAction *delete_action = new QAction("删除",this); QAction *edit_file_action = new QAction("编辑", this); if(this->itemAt(mapFromGlobal(QCursor::pos())) != NULL) { popMenu->addAction(delete_action); popMenu->addAction(rename_action); toDeleteItem = this->itemAt(mapFromGlobal(QCursor::pos())); QByteArray file_path = toDeleteItem->text().toLatin1(); int file_inode_id = GetPathInode(file_path.data()); if(inodes[file_inode_id].i_mode == 1) popMenu->addAction(edit_file_action); } else { popMenu->addAction(add_folder_action); popMenu->addAction(add_file_action); } connect(add_folder_action, SIGNAL(triggered()), this, SLOT(slotNewDir())); connect(add_file_action, SIGNAL(triggered()), this, SLOT(slotNewFile())); connect(delete_action, SIGNAL(triggered()), this, SLOT(slotDelete())); connect(edit_file_action, SIGNAL(triggered()), this, SLOT(slotEditFile())); connect(rename_action, SIGNAL(triggered()), this, SLOT(slotReName())); popMenu->exec(QCursor::pos()); } void DirList::slotReName() { QString oldFileName = toDeleteItem->text(), newFileName; int JudgeStatus = GetNewName(this, newFileName, 0, oldFileName); if(JudgeStatus == 0) ReName(oldFileName.toLatin1().data(), newFileName.toLatin1().data()); vfs_gui->UpdateFileList(); } int GetNewName(QWidget *parent, QString &the_new_file_name, int judge_type, const QString &the_old_name) { QString new_file_name, to_display; bool breakFlag = 0, changeFlag = 0; int ReturnStatus = 0; if(judge_type == 0) to_display = QWidget::tr("请输入文件名"); else to_display = QWidget::tr("请输入文件夹名"); if(the_old_name != NULL) new_file_name = the_old_name; for(; !breakFlag; ) { new_file_name = QInputDialog::getText(parent, "", to_display, QLineEdit::Normal, new_file_name, &changeFlag); if(changeFlag && new_file_name.size() > 0) { if(new_file_name.size() < 252) { int tar_inode_id = GetPathInode(new_file_name.toLatin1().data()); if(tar_inode_id < 0 || (the_old_name != NULL && tar_inode_id == GetPathInode(the_old_name.toLatin1().data()))) { the_new_file_name = new_file_name; //echo(new_file_name.toLatin1().data(), " "); breakFlag = 1; ReturnStatus = 0; } else { QMessageBox::warning(parent, "", QWidget::tr("文件名与当前文件重复")); ReturnStatus = -4; } } else { QMessageBox::warning(parent, "", QWidget::tr("文件名长度不能超过251字符")); ReturnStatus = -1; } } else if(changeFlag) { QMessageBox::warning(parent, "", QWidget::tr("文件名不能为空")); ReturnStatus = -2; } else { breakFlag = 1; ReturnStatus = -3; } } return ReturnStatus; } void DirList::slotNewFile() { if(find_free_dir_entry(wkpath->dir_inode) == -1) { QMessageBox::critical(this, "", tr("当前文件夹已满")); } else { QString new_file_name; int JudgeFlag = GetNewName(this, new_file_name); if(JudgeFlag == 0) DoEcho(new_file_name.toLatin1().data(), " "); vfs_gui->UpdateFileList(); } } void DirList::slotEditFile(QString *the_file_name) { QByteArray file_path; if(the_file_name == NULL) file_path = toDeleteItem->text().toLatin1(); else file_path = the_file_name->toLatin1(); EditTextWidget *text_edit_dialog = new EditTextWidget(this, file_path.data()); text_edit_dialog->show(); } void DirList::slotDelete() { //QMessageBox::information(vfs_gui, "", "正常"); QByteArray filePathStr = toDeleteItem->text().toLatin1(); char *filePath = filePathStr.data(); /* QString debugtext(filePath); QMessageBox::information(vfs_gui, "", debugtext); debugtext.setNum(d_inodeid); QMessageBox::information(vfs_gui, "", debugtext); */ int d_inodeid = GetPathInode(filePath); if(d_inodeid < 0) { QMessageBox::critical(this, "", "文件不存在"); } int status_value = -2; if(inodes[d_inodeid].i_mode == 0) status_value = delete_directory(d_inodeid); else status_value = delete_file(d_inodeid); if(status_value < 0) QMessageBox::critical(this, "", "删除失败"); vfs_gui->UpdateFileList(); } void DirList::SentDirName() { } void DirList::slotNewDir() { if(find_free_dir_entry(wkpath->dir_inode) == -1) { QMessageBox::critical(this, "", tr("当前文件夹已满")); } else { QString new_folder_name; int JudgeFlag = GetNewName(this, new_folder_name, 1); if(JudgeFlag == 0) MakeFolder(new_folder_name.toLatin1().data()); vfs_gui->UpdateFileList(); } } void FileView::slotGoTo() { QByteArray pathStr = WorkPathEdit->text().toLatin1(); char *path = pathStr.data(); int GoToStatus = GetPathInode(path, 1); if(GoToStatus == -1) QMessageBox::critical(this, "", tr("路径错误")); else if(inodes[GoToStatus].i_mode == 1) QMessageBox::critical(this, "", tr("目标是文件")); UpdateFileList(); } void FileView::UpdateFileList() { char abopath[input_buffer_length] = {0}; GetAboPath(abopath); //cout << this << endl; QString Qabopath(abopath); WorkPathEdit->setText( Qabopath ); FileListWidget->clear(); FILE *vfs = fopen(filename, "rb"); char dir_name[253] = {0}; int dir_entry_id = -1; for (int i = 2; i < 16; ++i) { fseek(vfs, DataBlkPos(inodes[wkpath->dir_inode].i_blocks[0]), SEEK_SET); fseek(vfs, DirsPos(i), SEEK_CUR); memset(dir_name, 0, sizeof(dir_name)); fread(dir_name, sizeof(char), 252, vfs); fread(&dir_entry_id, sizeof(int), 1, vfs); if(strlen(dir_name) != 0) { QString fileName(dir_name); if (inodes[dir_entry_id].i_mode == 0) { QListWidgetItem * pTmp = new QListWidgetItem( QIcon( ":/new/icon/folder2.svg" ), fileName ); FileListWidget->addItem( pTmp ); } else { QListWidgetItem * pTmp = new QListWidgetItem( QIcon( ":/new/icon/file1.svg" ), fileName ); FileListWidget->addItem( pTmp ); } } } fclose(vfs); } FileView::~FileView() { } void FileView::slotShowDir( QListWidgetItem * item ) { QString str = item->text(); QByteArray latinstr = str.toLatin1(); char *subDir = latinstr.data(); int Tar_Inode_id = GetPathInode(subDir); if(inodes[Tar_Inode_id].i_mode == 0) ChangeDir(subDir); else { //FileListWidget->toDeleteItem = item; FileListWidget->slotEditFile(&str); } UpdateFileList(); } void FileView::slotGoUpDIr() { GetPathInode("..",1); UpdateFileList(); }
97799264b5e07a8a7651b9638c78d0f0d97e358c
d49b8536d996a81fd2a356f2ccd850abd4447217
/VirusPack/LiquidBot_FixEd_By_Pr1muZ_anD_Ic3/source/httpd.cpp
9417f47d88af289650d4a4291ca4b709f08cd6eb
[]
no_license
JonnyBanana/botnets
28a90ab80f973478d54579f3d3eadc5feb33ff77
995b9c20aca5de0ae585ae17780a31e8bdfd9844
refs/heads/master
2021-07-01T01:51:01.211451
2020-10-22T23:14:57
2020-10-22T23:14:57
148,392,362
9
5
null
null
null
null
UTF-8
C++
false
false
13,675
cpp
#include "../header/includes.h" #include "../header/functions.h" #include "../header/externs.h" #ifndef NO_HTTPD DWORD WINAPI HTTP_Server_Thread(LPVOID param) { HTTPD httpd = *((HTTPD *)param); HTTPD *httpds = (HTTPD *)param; httpds->info = TRUE; char sendbuf[IRCLINE], buffer[4096], rBuffer[4096], file[MAX_PATH], *file_to_send = "\0"; int gsin_len; unsigned int i,r,b, max; unsigned long mode = 1; SOCKET gsock, ssock; SOCKADDR_IN gsin, ssin; memset(&ssin, 0, sizeof(ssin)); ssin.sin_family = AF_INET; ssin.sin_port = fhtons((unsigned short)httpd.port); ssin.sin_addr.s_addr = INADDR_ANY; if ((ssock = fsocket(AF_INET, SOCK_STREAM, 0)) != INVALID_SOCKET) { threads[httpd.threadnum].sock = ssock; if (fbind(ssock, (LPSOCKADDR)&ssin, sizeof(ssin)) != SOCKET_ERROR) { if (flisten(ssock, SOMAXCONN) != SOCKET_ERROR) { if (fioctlsocket(ssock,FIONBIO,&mode) != SOCKET_ERROR) { fd_set master, temp; FD_ZERO(&master); FD_ZERO(&temp); FD_SET(ssock, &master); max = ssock; while (1) { temp = master; if (fselect(max+1, &temp, NULL, NULL, NULL) == SOCKET_ERROR) break; for(i=0; i <= max; i++) { if (fFD_ISSET(i, &temp)) { //there is somthing to do if (i == ssock) { //there is a new connection request gsin_len = sizeof(gsin); if ((gsock = faccept(ssock, (LPSOCKADDR)&gsin, &gsin_len)) == INVALID_SOCKET) continue; else { FD_SET(gsock, &master); // add to master set if (gsock > (unsigned int)max) max = gsock; } } else { memset(buffer,0,sizeof(buffer)); memset(rBuffer,0,sizeof(rBuffer)); if (frecv(i, buffer, sizeof(buffer), 0) <= 0) { //socket error fclosesocket(i); FD_CLR(i, &master); // remove from master set } else { memset(file,0,sizeof(file)); for (b=0,r=0; b<strlen(buffer); b++,r++) { rBuffer[r] = buffer[b]; if (buffer[b] == '\n') { //check the request.... if (strstr(rBuffer,"GET ") != NULL && strlen(rBuffer) > 5) { //look for a GET request file_to_send = strtok(strstr(strstr(rBuffer,"GET ")," ")," "); strcpy(file,file_to_send); } else if (strcmp(rBuffer,"\r\n") == 0) { //end of the request check if there is anything to send back FD_CLR(i, &master); if (file != NULL) { if (strlen(file)+strlen(httpd.dir) < MAX_PATH) { unsigned long mode2 = 0; fioctlsocket(i,FIONBIO,&mode2); Check_Requested_File(i,httpd.dir,file,httpd.enabled,httpd.threadnum); } else fclosesocket(i); } else fclosesocket(i); break; } memset(rBuffer,0,sizeof(rBuffer)); r=-1; } } } } } } } } } } } sprintf(sendbuf, "[HTTPD]: Error: server failed, returned: <%d>.", fWSAGetLastError()); if (!httpd.silent) irc_privmsg(httpd.sock, httpd.chan, sendbuf, httpd.notice); addlog(sendbuf); fclosesocket(ssock); clearthread(httpd.threadnum); ExitThread(0); } DWORD WINAPI HTTP_Header(LPVOID param) { HTTPD httpd = *((HTTPD *)param); HTTPD *httpds = (HTTPD *)param; httpds->info = TRUE; char tFile[MAX_PATH], nFile[MAX_PATH], content[50], buffer[4096], date[70], time[30]; sprintf(tFile,httpd.file); sprintf(nFile,httpd.path); if (httpd.type) sprintf(content,"text/html"); else sprintf(content,"application/octet-stream"); GetDateFormat(0x409,0,0,"ddd, dd MMM yyyy",date,70); GetTimeFormat(0x409,0,0,"HH:mm:ss",time,30); if (httpd.length == -1) sprintf(buffer,"HTTP/1.0 200 OK\r\nServer: myBot\r\nCache-Control: no-cache,no-store,max-age=0\r\npragma: no-cache\r\nContent-Type: %s\r\nAccept-Ranges: bytes\r\nDate: %s %s GMT\r\nLast-Modified: %s %s GMT\r\nExpires: %s %s GMT\r\nConnection: close\r\n\r\n",content,date,time,date,time,date,time); else sprintf(buffer,"HTTP/1.0 200 OK\r\nServer: myBot\r\nCache-Control: no-cache,no-store,max-age=0\r\npragma: no-cache\r\nContent-Type: %s\r\nContent-Length: %i\r\nAccept-Ranges: bytes\r\nDate: %s %s GMT\r\nLast-Modified: %s %s GMT\r\nExpires: %s %s GMT\r\nConnection: close\r\n\r\n",content,httpd.length,date,time,date,time,date,time); fsend(httpd.sock,buffer,strlen(buffer),0); if (httpd.type == FALSE) HTTP_Send_File(httpd.sock,tFile); else GetFiles(tFile,httpd.sock,NULL,nFile); fclosesocket(httpd.sock); clearthread(httpd.threadnum); ExitThread(0); } int Check_Requested_File(SOCKET sock, char *dir ,char *rFile, BOOL dirinfo, int threadnum) { char buffer[IRCLINE], file[MAX_PATH], nFile[MAX_PATH], tFile[MAX_PATH]; DWORD c,d, id; BOOL directory = FALSE; memset(nFile,0,sizeof(nFile)); if (rFile[0] != 47) sprintf(file,"\\%s",rFile); else { rFile[0] = 92; sprintf(file,"%s",rFile); } for (c=0,d=0; c<strlen(file); c++,d++) { if ((((c+2 < strlen(file) && file[c] == 37 && file[c+1] == 50 && file[c+2] == 48)))) { nFile[d] = 32; c=c+2; } else nFile[d] = ((file[c] == 47)?(92):(file[c])); } sprintf(tFile,"%s%s",dir,nFile); strtok(tFile,"\n"); switch(GetFileAttributes(tFile)) { case FILE_ATTRIBUTE_DIRECTORY: directory = TRUE; break; case 0xFFFFFFFF: fclosesocket(sock); return 0; } if (nFile[d-1] == 92) directory = TRUE; HTTPD httpd; httpd.sock = sock; httpd.info = FALSE; if (directory) { if (dirinfo) { strcat(tFile,"*"); sprintf(httpd.file,tFile); File_To_HTML(nFile); sprintf(httpd.path,nFile); httpd.type = TRUE; httpd.length = -1; } else { fclosesocket(sock); return 0; } } else { HANDLE testfile = CreateFile(tFile,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,0,0); if (testfile != INVALID_HANDLE_VALUE) { sprintf(httpd.file,tFile); httpd.type = FALSE; httpd.length = GetFileSize(testfile,NULL); CloseHandle(testfile); } } sprintf(buffer,"[HTTPD]: Worker thread of server thread: %d.", threadnum); httpd.threadnum = addthread(buffer,HTTP_THREAD,NULL); threads[httpd.threadnum].parent = threadnum; if (threads[httpd.threadnum].tHandle = CreateThread(NULL, 0, &HTTP_Header, (LPVOID)&httpd, 0, &id)) { while (httpd.info == FALSE) Sleep(5); } else { fclosesocket(sock); sprintf(buffer,"[HTTPD]: Failed to start worker thread, error: <%d>.", GetLastError()); addlog(buffer); } return 0; } int GetFiles(char *current, SOCKET sock, char *chan, char *URL) { FILETIME ftBuf; SYSTEMTIME stBuf; WIN32_FIND_DATA WFD; char sendbuf[IRCLINE],parent[MAX_PATH],tmpDate[40],tmpDir[MAX_PATH+2]; int count=0, count2=0; unsigned int COL1=230, COL2=150, COL3=60, FILESIZE=30; memset(parent,0,sizeof(parent)); strtok(current,"\n"); if (chan) _snprintf(sendbuf,sizeof(sendbuf),"PRIVMSG %s :Searching for: %s\r\n",chan,current); else if (URL) { current[(strlen(current)-1)]=0; _snprintf(sendbuf,sizeof(sendbuf),"<HTML>\r\n<HEAD>\r\n<TITLE>Index of %s</TITLE>\r\n</HEAD>\r\n<BODY>\r\n", current); fsend(sock,sendbuf,strlen(sendbuf),0); _snprintf(sendbuf,sizeof(sendbuf),"<H1>Index of %s</H1>\r\n<TABLE BORDER=\"0\">\r\n", current); fsend(sock,sendbuf,strlen(sendbuf),0); current[(strlen(current))]='*'; _snprintf(sendbuf,sizeof(sendbuf),"<TR>\r\n<TD WIDTH=\"%d\"><CODE>Name</CODE></TD>\r\n<TD WIDTH=\"%d\"><CODE>Last Modified</CODE></TD>\r\n<TD WIDTH=\"%d\" ALIGN=\"right\"><CODE>Size</CODE></TD>\r\n</TR>\r\n", COL1, COL2, COL3); fsend(sock,sendbuf,strlen(sendbuf),0); _snprintf(sendbuf,sizeof(sendbuf),"<TR>\r\n<TD COLSPAN=\"3\"><HR></TD>\r\n</TR>\r\n"); } else _snprintf(sendbuf,sizeof(sendbuf),"Searching for: %s\r\n",current); fsend(sock,sendbuf,strlen(sendbuf),0); if (URL && strlen(URL) > 2) { unsigned int c; for (c=strlen(URL)-3; c!=0; c--) if (URL[c] == 47) break; strncpy(parent,URL,c+1); _snprintf(sendbuf,sizeof(sendbuf),"<TR>\r\n<TD COLSPAN=\"3\"><A HREF=\"%s\"><CODE>Parent Directory</CODE></A></TD>\r\n</TR>\r\n",parent); fsend(sock,sendbuf,strlen(sendbuf),0); } HANDLE Hnd = FindFirstFile(current, &WFD); while (FindNextFile(Hnd, &WFD)) { if ((WFD.dwFileAttributes) && (strcmp(WFD.cFileName, "..") && strcmp(WFD.cFileName, "."))) { FileTimeToLocalFileTime(&WFD.ftLastWriteTime, &ftBuf); FileTimeToSystemTime(&ftBuf, &stBuf); sprintf(tmpDate, "%2.2d/%2.2d/%4d %2.2d:%2.2d %s", stBuf.wMonth, stBuf.wDay, stBuf.wYear, HOUR(stBuf.wHour), stBuf.wMinute, AMPM(stBuf.wHour)); if (WFD.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { count2++; if (chan) { _snprintf(tmpDir,sizeof(tmpDir),"<%s>",WFD.cFileName); _snprintf(sendbuf,sizeof(sendbuf),"PRIVMSG %s :%-31s %-21s\n",chan,tmpDir, tmpDate); } else if (URL) { _snprintf(sendbuf,(sizeof(sendbuf)-1),"<TR>\r\n<TD WIDTH=\"%d\"><A HREF=\"",COL1); fsend(sock,sendbuf,strlen(sendbuf),0); _snprintf(sendbuf,(sizeof(sendbuf)-1),"%s%s/",URL,WFD.cFileName); fsend(sock,sendbuf,strlen(sendbuf),0); if(strlen(WFD.cFileName)>FILESIZE) _snprintf(sendbuf,(sizeof(sendbuf)-1),"\"><CODE>%.29s&gt;/</CODE></A>",WFD.cFileName); else _snprintf(sendbuf,(sizeof(sendbuf)-1),"\"><CODE>%s/</CODE></A>",WFD.cFileName); fsend(sock,sendbuf,strlen(sendbuf),0); _snprintf(sendbuf,(sizeof(sendbuf)-1),"</TD>\r\n<TD WIDTH=\"%d\"><CODE>%s</CODE></TD>\r\n<TD WIDTH=\"%d\" ALIGN=\"right\"><CODE>-</CODE></TD>\r\n</TR>\r\n", COL2,tmpDate,COL3); } else { _snprintf(tmpDir,sizeof(tmpDir),"<%s>",WFD.cFileName); _snprintf(sendbuf,sizeof(sendbuf),"%-31s %-21s\r\n",tmpDir,tmpDate); } } else { count++; if (chan) _snprintf(sendbuf,sizeof(sendbuf),"PRIVMSG %s :%-31s %-21s (%s bytes)\n",chan,WFD.cFileName,tmpDate,commaI64(WFD.nFileSizeLow)); else if (URL) { _snprintf(sendbuf,(sizeof(sendbuf)-1),"<TR>\r\n<TD WIDTH=\"%d\"><A HREF=\"",COL1); fsend(sock,sendbuf,strlen(sendbuf),0); _snprintf(sendbuf,(sizeof(sendbuf)-1),"%s%s",URL,WFD.cFileName); fsend(sock,sendbuf,strlen(sendbuf),0); if(strlen(WFD.cFileName)>(FILESIZE+1)) _snprintf(sendbuf,(sizeof(sendbuf)-1),"\"><CODE>%.30s&gt;</CODE></A>",WFD.cFileName); else _snprintf(sendbuf,(sizeof(sendbuf)-1),"\"><CODE>%s</CODE></A>",WFD.cFileName); fsend(sock,sendbuf,strlen(sendbuf),0); _snprintf(sendbuf,(sizeof(sendbuf)-1),"</TD>\r\n<TD WIDTH=\"%d\"><CODE>%s</CODE></TD>\r\n<TD WIDTH=\"%d\" ALIGN=\"right\"><CODE>%dk</CODE></TD>\r\n</TR>\r\n", COL2,tmpDate,COL3,(WFD.nFileSizeLow/1024)); } else _snprintf(sendbuf,sizeof(sendbuf),"%-31s %-21s (%i bytes)\r\n",WFD.cFileName,tmpDate,WFD.nFileSizeLow); } fsend(sock,sendbuf,strlen(sendbuf),0); if (chan) Sleep(FLOOD_DELAY); } } FindClose(Hnd); if (chan) sprintf(sendbuf,"PRIVMSG %s :Found %s Files and %s Directories\n",chan,commaI64(count),commaI64(count2)); else if (URL) sprintf(sendbuf,"<TR>\r\n<TD COLSPAN=\"3\"><HR></TD>\r\n</TR>\r\n</TABLE>\r\n</BODY>\r\n</HTML>\r\n"); else sprintf(sendbuf,"Found: %i Files and %i Directories\r\n",count,count2); fsend(sock,sendbuf,strlen(sendbuf),0); return 0; } void HTTP_Send_File(SOCKET sock, char *file) { char buffer[1024]; unsigned int Fsize,Fsend=1024,move; DWORD mode=0; HANDLE testfile = CreateFile(file,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,0,0); if (testfile == INVALID_HANDLE_VALUE) return; Fsize = GetFileSize(testfile,NULL); while (Fsize) { memset(buffer,0,sizeof(buffer)); if (Fsend>Fsize) Fsend=Fsize; move=0-Fsize; SetFilePointer(testfile, move, NULL, FILE_END); ReadFile(testfile, buffer, Fsend, &mode, NULL); int bytes_sent=fsend(sock, buffer, Fsend, 0); if (bytes_sent == SOCKET_ERROR) { if (fWSAGetLastError() != WSAEWOULDBLOCK) break; else bytes_sent = 0; } Fsize=Fsize-bytes_sent; } if (testfile != INVALID_HANDLE_VALUE) CloseHandle(testfile); return; } char *File_To_HTML(char *file) { for (unsigned int c=0; c<strlen(file); c++) if (file[c] == 92) file[c] = 47; return file; } void HTTP_Connect(SOCKET sock, char *chan, BOOL notice, BOOL silent, char *host, int port, char *method, char *url, char *referer) { char sendbuf[IRCLINE], buffer[256]; WSADATA wsaData; fWSAStartup(MAKEWORD(1,1), &wsaData); SOCKET hsock = fsocket(AF_INET, SOCK_STREAM, IPPROTO_TCP); SOCKADDR_IN ssin; memset(&ssin, 0, sizeof(ssin)); ssin.sin_family = AF_INET; ssin.sin_port = fhtons((unsigned short)port); ssin.sin_addr.s_addr = ResolveAddress(host); if (fconnect(hsock, (LPSOCKADDR)&ssin, sizeof(ssin)) != SOCKET_ERROR) { _snprintf(buffer,sizeof(buffer),"%s %s HTTP/1.1\nReferer: %s\nHost: %s\nConnection: close\n\n",method,url,((referer)?(referer):("")),host); fsend(hsock, buffer, strlen(buffer), 0); memcpy(buffer,0,sizeof(buffer)); frecv(hsock, buffer, sizeof(buffer), 0); } fclosesocket(hsock); fWSACleanup(); sprintf(sendbuf, buffer); if (!silent) irc_privmsg(sock, chan, sendbuf, notice); return; } #endif
25a60e1dc316095d9d0379f2a57e5c3a56a62030
6559c9c66342c546f41ced1548d239fbfea0d7c9
/Ramjet_Core/App/Application.h
b8ae9c7d75577ac479547995d7b8abdf1da7411c
[]
no_license
Fox3medium/RamjetEngine
e8f3cdbd74dc3e846974ebd3f6fb6c12415d0808
1b6c83f51821d94a8b35720bc09d6a4d28741fa9
refs/heads/master
2022-01-28T13:54:55.575843
2019-07-17T21:45:24
2019-07-17T21:45:24
194,148,095
0
0
null
null
null
null
UTF-8
C++
false
false
1,517
h
#pragma once #include <CoreBasicInclude.h> #include <Core/Common.h> #include <Utils/types.h> #include <Init/Window.h> #include <Managers/Control_Manager.h> #include <Rendering/Renderer/Layers/Layer.h> #include <Utils/Timer.h> namespace Core { namespace Debug { class DebugLayer; } } namespace App { using namespace Core::Rendering; class CORE_API Application { public: Core::Init::Window* window; Core::Manager::Control_Manager* C_Manager; Core::Debug::DebugLayer* m_DebugLayer; private: static Application* s_Instance; bool m_Running, m_Suspended; Timer* m_Timer; unsigned int m_Fps, m_UpdatePerSec; const char* m_Name; uint m_Width, m_Height; std::vector<Layer*> m_Layers; std::vector<Layer*> m_OverlayStack; public: Application(const char* name, uint width, uint height, bool fullscreen = false); virtual ~Application(); void pushLayer(Layer* layer); Layer* popLayer(Layer* layer); void pushOverlay(Layer* layer); Layer* popOverlay(Layer* layer); // Run to initialize the engine virtual void init(); // Run every second void tick(); // Run 60 times per second void update(); // Run as fast as possible void render(); void onEvent(); void start(); void suspend(); void resume(); void stop(); const uint getFPS() const { return m_Fps; } const uint getUPS() const { return m_UpdatePerSec; } inline static Application& getApplication() { return *s_Instance; } private: void platformInit(); void run(); }; }
fa3d25e4f997eefd1c00e24fd73cac4d07047da2
8264dae28229bc6b8d09c72f34c0bd2a27a71019
/src/layout.h
579d827c1d1f88775463e355236fecf90e1a49a0
[]
no_license
Bayonetta5/hge-gui
a502d9c725ea3460aa5d3b4820f2bcb68b22eb37
7859b47c6c204534276533f05b987111fadbb451
refs/heads/master
2023-03-19T13:28:19.128148
2013-10-16T18:39:32
2013-10-16T18:39:32
null
0
0
null
null
null
null
UTF-8
C++
false
false
550
h
#ifndef LAYOUT_H #define LAYOUT_H #include <vector> namespace Gui { class Widget; class Layout { public: Layout(); virtual ~Layout(); virtual void Update(float x, float y, float width, float height) = 0; void AddWidget(Widget* value); void RemoveWidget(Widget* value); std::vector<Widget*> Children() { return m_children; } protected: std::vector<Widget*> m_children; float m_x, m_y, m_width, m_height; }; } #endif // LAYOUT_H
670bba6acb9d62982507961e9b22af8d50af8c4e
fa44309e624fc5d7692306c9d071f5062894d100
/EcdMirror/StringUtil.h
5e4fc2c4e90223c3b2f3ed531d6fe45044a46b51
[]
no_license
QLLLll/Objectarx12_14
8be47480e19a24c9b28ef93b8ecb8a4fcdc03adb
41a2ce478e0fcfa69a90bc8df69dd599e499bc1c
refs/heads/master
2022-06-18T05:57:28.594922
2020-05-15T06:57:20
2020-05-15T06:57:20
257,164,398
0
3
null
null
null
null
GB18030
C++
false
false
453
h
#pragma once #include <vector> class CStringUtil { public: CStringUtil(); ~CStringUtil(); // 以一个字符串为分隔符对字符串进行分割 // bAddEmptyItem: 是否添加空字符串元素 static void Split(const CString &text, const TCHAR* separator, std::vector<CString> &result, bool bAddEmptyItem = true); // 通过分隔符合并字符串集合 static CString Join(const std::vector<CString> &items, const TCHAR* separator); };
2d1b9d7b936f81a35bb6d17174d0701cd8806cb0
4e8cd45798d12f970d0cb829dbd38459520e4a6a
/src/mbgl/annotation/annotation_source.hpp
db9221788f70fe761da9cf476cbb078323e3bd7c
[ "BSD-3-Clause", "IJG", "LicenseRef-scancode-warranty-disclaimer", "Zlib", "curl", "NCSA", "LicenseRef-scancode-openssl", "OpenSSL", "LicenseRef-scancode-ssleay-windows", "JSON", "ISC", "MIT", "BSL-1.0", "Apache-2.0", "BSD-2-Clause", "LicenseRef-scancode-unknown-license-reference", "blessing", "Libpng" ]
permissive
spurdow/mapbox-gl-native
1276027299de93b241ed4a0461748e7fa9395cc3
2e746058f75e5ed4afc3a7540a82c50479529af4
refs/heads/master
2021-01-21T16:44:28.147359
2016-07-19T14:02:11
2016-07-19T14:02:11
63,533,799
2
0
null
2016-07-17T13:41:21
2016-07-17T13:41:21
null
UTF-8
C++
false
false
570
hpp
#pragma once #include <mbgl/style/source.hpp> #include <mbgl/style/source_impl.hpp> namespace mbgl { class AnnotationSource : public style::Source { public: AnnotationSource(); class Impl; }; class AnnotationSource::Impl : public style::Source::Impl { public: Impl(Source&); void load(FileSource&) final; private: uint16_t getTileSize() const final { return util::tileSize; } Range<uint8_t> getZoomRange() final; std::unique_ptr<Tile> createTile(const OverscaledTileID&, const style::UpdateParameters&) final; }; } // namespace mbgl
aa39aa03f09100f27919f56c6efd5cbf8f3a1ccb
b8ea2981a2bceb011d958ad8a6c126d8b2bd1dbb
/src/Generator.cpp
f6a238f8102bb520214466bc068f081b93fae0f4
[ "MIT" ]
permissive
wandns/doxyrest
c1176ed2db403ee2969215e3acf37c7c9c07e504
f52d62d7dbc872be7b678cc3bf1554ad7c940e56
refs/heads/master
2020-04-27T02:38:58.668256
2019-03-29T07:36:08
2019-03-29T07:36:08
174,001,414
0
0
MIT
2019-06-13T02:52:51
2019-03-05T18:42:48
C++
UTF-8
C++
false
false
5,535
cpp
//.............................................................................. // // This file is part of the Doxyrest toolkit. // // Doxyrest is distributed under the MIT license. // For details see accompanying license.txt file, // the public copy of which is also available at: // http://tibbo.com/downloads/archive/doxyrest/license.txt // //.............................................................................. #include "pch.h" #include "Generator.h" #include "Module.h" #include "CmdLine.h" //.............................................................................. void Generator::prepare( Module* module, GlobalNamespace* globalNamespace ) { m_stringTemplate.create(); m_stringTemplate.m_luaState.registerFunction("includeFile", includeFile_lua, this); m_stringTemplate.m_luaState.registerFunction("generateFile", generateFile_lua, this); m_stringTemplate.m_luaState.createTable(); m_stringTemplate.m_luaState.setGlobal("g_exportCache"); globalNamespace->luaExport(&m_stringTemplate.m_luaState); m_stringTemplate.m_luaState.setGlobal("g_globalNamespace"); luaExportArray(&m_stringTemplate.m_luaState, module->m_groupArray); m_stringTemplate.m_luaState.setGlobal("g_groupArray"); luaExportArray(&m_stringTemplate.m_luaState, module->m_pageArray); m_stringTemplate.m_luaState.setGlobal("g_pageArray"); luaExportArray(&m_stringTemplate.m_luaState, module->m_exampleArray); m_stringTemplate.m_luaState.setGlobal("g_exampleArray"); sl::ConstIterator<Define> it = m_cmdLine->m_defineList.getHead(); for (; it; it++) { const Define* define = *it; if (define->m_hasValue) m_stringTemplate.m_luaState.setGlobalString(define->m_name, define->m_value); else m_stringTemplate.m_luaState.setGlobalBoolean(define->m_name, true); } // export cache is only needed during export-time m_stringTemplate.m_luaState.pushNil(); m_stringTemplate.m_luaState.setGlobal("g_exportCache"); } bool Generator::generate( const sl::StringRef& targetFileName, const sl::StringRef& frameFileName ) { bool result; sl::String frameFilePath = io::findFilePath(frameFileName, &m_cmdLine->m_frameDirList); if (frameFilePath.isEmpty()) { err::setFormatStringError("frame file '%s' not found", frameFileName.sz()); return false; } sl::String targetDir = io::getDir(targetFileName); result = io::ensureDirExists(targetDir); if (!result) return false; m_frameDir = io::getDir(frameFilePath); m_targetDir = io::getFullFilePath(targetDir); m_stringTemplate.m_luaState.setGlobalString("g_frameDir", m_frameDir); m_stringTemplate.m_luaState.setGlobalString("g_targetDir", m_targetDir); m_stringTemplate.m_luaState.setGlobalString("g_targetFileName", targetFileName); sl::String stringBuffer; result = m_stringTemplate.processFile(&stringBuffer, frameFilePath); if (!result) return false; io::File targetFile; result = targetFile.open(targetFileName); if (!result) return false; size_t size = stringBuffer.getLength(); result = targetFile.write(stringBuffer, size) != -1; if (!result) return false; targetFile.setSize(size); return true; } bool Generator::processFile( const sl::StringRef& targetFileName, const sl::StringRef& frameFileName, size_t baseArgCount ) { bool result; sl::String frameFilePath = io::findFilePath(frameFileName, m_frameDir, &m_cmdLine->m_frameDirList); if (frameFilePath.isEmpty()) { err::setFormatStringError("frame '%s' not found", frameFileName.sz()); return false; } sl::String prevFrameDir = m_stringTemplate.m_luaState.getGlobalString("g_frameDir"); m_frameDir = io::getDir(frameFilePath); m_stringTemplate.m_luaState.setGlobalString("g_frameDir", m_frameDir); int top = m_stringTemplate.m_luaState.getTop(); ASSERT((size_t)top >= baseArgCount); m_stringTemplate.setArgCount(top - baseArgCount); if (targetFileName.isEmpty()) { result = m_stringTemplate.processFile(NULL, frameFilePath); if (!result) return false; } else { sl::String prevTargetFileName = m_stringTemplate.m_luaState.getGlobalString("g_targetFileName"); m_stringTemplate.m_luaState.setGlobalString("g_targetFileName", targetFileName); sl::String targetFilePath = io::concatFilePath(m_targetDir, targetFileName); result = m_stringTemplate.processFileToFile(targetFilePath, frameFilePath); if (!result) return false; m_stringTemplate.m_luaState.setGlobalString("g_targetFileName", prevTargetFileName); } m_frameDir = prevFrameDir; m_stringTemplate.m_luaState.setGlobalString("g_frameDir", prevFrameDir); return true; } int Generator::includeFile_lua(lua_State* h) { lua::LuaNonOwnerState luaState(h); Generator* self = (Generator*)luaState.getContext(); ASSERT(self->m_stringTemplate.m_luaState == h); sl::StringRef fileName = luaState.getString(1); bool result = self->processFile(NULL, fileName, 1); if (!result) { luaState.prepareLastErrorString(); luaState.error(); ASSERT(false); return -1; } return 0; } int Generator::generateFile_lua(lua_State* h) { lua::LuaNonOwnerState luaState(h); Generator* self = (Generator*)luaState.getContext(); ASSERT(self->m_stringTemplate.m_luaState == h); sl::StringRef targetFileName = luaState.getString(1); sl::StringRef frameFileName = luaState.getString(2); bool result = self->processFile(targetFileName, frameFileName, 2); if (!result) { luaState.prepareLastErrorString(); luaState.error(); ASSERT(false); return -1; } return 0; } //..............................................................................
99e6e341ca7829023aeba76920751997c79fbd61
ba9322f7db02d797f6984298d892f74768193dcf
/imm/include/alibabacloud/imm/model/UpdateSetRequest.h
822e804d75558923a17f3ebaddee6ab0013cdcdf
[ "Apache-2.0" ]
permissive
sdk-team/aliyun-openapi-cpp-sdk
e27f91996b3bad9226c86f74475b5a1a91806861
a27fc0000a2b061cd10df09cbe4fff9db4a7c707
refs/heads/master
2022-08-21T18:25:53.080066
2022-07-25T10:01:05
2022-07-25T10:01:05
183,356,893
3
0
null
2019-04-25T04:34:29
2019-04-25T04:34:28
null
UTF-8
C++
false
false
1,734
h
/* * Copyright 2009-2017 Alibaba Cloud All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef ALIBABACLOUD_IMM_MODEL_UPDATESETREQUEST_H_ #define ALIBABACLOUD_IMM_MODEL_UPDATESETREQUEST_H_ #include <string> #include <vector> #include <alibabacloud/core/RpcServiceRequest.h> #include <alibabacloud/imm/ImmExport.h> namespace AlibabaCloud { namespace Imm { namespace Model { class ALIBABACLOUD_IMM_EXPORT UpdateSetRequest : public RpcServiceRequest { public: UpdateSetRequest(); ~UpdateSetRequest(); std::string getSetName()const; void setSetName(const std::string& setName); std::string getRegionId()const; void setRegionId(const std::string& regionId); std::string getProject()const; void setProject(const std::string& project); std::string getSetId()const; void setSetId(const std::string& setId); std::string getAccessKeyId()const; void setAccessKeyId(const std::string& accessKeyId); private: std::string setName_; std::string regionId_; std::string project_; std::string setId_; std::string accessKeyId_; }; } } } #endif // !ALIBABACLOUD_IMM_MODEL_UPDATESETREQUEST_H_
b8c7657b99c70613e87cb5534e6b59a0224cad9e
799516a742541b63a43db4f94a89e1e50bc629b0
/oj.ahstu.cc/1009.cpp
b87815a19ef06569d06e3d31d1943a88a2097a7e
[]
no_license
webturing/ACM
2e940a72d90c0f3f45d1758d8d46c765ac47f689
aab803107af653e18ea4f049ac642fefa420acc7
refs/heads/master
2021-01-23T04:19:09.859541
2017-06-12T14:27:13
2017-06-12T14:27:13
92,923,789
0
0
null
null
null
null
UTF-8
C++
false
false
214
cpp
#include <iostream> using namespace std; int main(int argc, char const *argv[]) { for (unsigned n, s; cin >> n;) { for (s = 0; n; n /= 10) s += n % 10; cout << s << endl; } return 0; }
47cc13755ac512f72d5451a4d5f2ff67b23551f7
09a320e960b0af9210a659b9cce9f155d3ab99b9
/ch9/p21.cpp
adbd476a572e7b52b6360cee19e41dace5396512
[]
no_license
hiteshkansal/Karumanchi
7f53d3289a4af3aba5ca9712cce1336f4e126752
84802ac10888bc8316658663e7f2491998b4bf22
refs/heads/master
2021-01-21T20:38:48.941685
2017-05-24T07:55:39
2017-05-24T07:55:39
92,263,712
0
0
null
null
null
null
UTF-8
C++
false
false
875
cpp
#include<iostream> #include<set> using namespace std; typedef struct edge { int s; int d; }edge; /* 6 7 0 1 0 2 1 3 2 1 3 5 0 4 4 5 */ int main() { int e,v; cin>>v>>e; edge ed[e]; for(int i=0;i<e;i++) cin>>ed[i].s>>ed[i].d; int mark[v]; for(int j=0;j<v;j++) mark[j]=0; set<edge*> st; for(int i=0;i<e;i++) { if((mark[ed[i].s]) && (mark[ed[i].d])) continue; if(!(mark[ed[i].s])) { st.insert(&ed[i]); mark[ed[i].s] = 1; mark[ed[i].d] = 1; } else if(!(mark[ed[i].d])) { st.insert(&ed[i]); mark[ed[i].s] = 1; mark[ed[i].d] = 1; } } auto res = st.begin(); while(res!=st.end()) { cout<<(*res)->s<<" "<<(*res)->d<<endl; res++; } return 0; }
1077a2489f425a51afddf4bd1fdc8a4f93dc9fc2
cbc7461ab8b9bf0c63ee2acd4bc3a3c5be094ad4
/example2/version.cpp
fe2af5372f45e50b590b079733e1a0fc8b655344
[]
no_license
ALTracer/cmake-examples-cpp-otus
8cd063f996e4f97b152d7dab2480504ad4516f04
43a14176f88f245d5bac02894e59db95a6426211
refs/heads/master
2020-06-04T16:19:13.595471
2019-06-15T21:38:14
2019-06-15T21:38:14
192,099,957
0
0
null
null
null
null
UTF-8
C++
false
false
82
cpp
#include "version.h" namespace examples { int getVersion() { return 0; } }
69440f7482e6588f91a5f30775234ba899e74b0a
df6c3d7d95507bf452ce8eda24e779a82049fcad
/final-project/simulationNonInertial/5/phi
950a4b7db440624e942648426a8a29404aa6e726
[]
no_license
aeronau/CFD-course
5c555e7869f0170f90e9d3532b06f7e78683b770
e891eb94950cfa0992d6ff003dbe45315af377c5
refs/heads/master
2021-09-01T04:18:32.111694
2017-12-24T18:18:44
2017-12-24T18:18:44
104,058,197
0
0
null
null
null
null
UTF-8
C++
false
false
146,090
/*--------------------------------*- C++ -*----------------------------------*\ | ========= | | | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox | | \\ / O peration | Version: 4.1 | | \\ / A nd | Web: www.OpenFOAM.org | | \\/ M anipulation | | \*---------------------------------------------------------------------------*/ FoamFile { version 2.0; format ascii; class surfaceScalarField; location "5"; object phi; } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // dimensions [0 3 -1 0 0 0 0]; internalField nonuniform List<scalar> 11516 ( -1.254e+08 50760.5 1.25642e+08 -1.46862e+08 531184 3.03504e+07 -6.48019e+07 -2.22804e+06 -6.9274e+07 4.20581e+07 -5.19369e+06 -9.2687e+07 1.33646e+08 -6.25616e+06 -7.77893e+07 2.05938e+08 -6.39791e+06 -5.86388e+07 2.62376e+08 -6.22285e+06 -4.24701e+07 3.06782e+08 -5.78181e+06 -2.96696e+07 3.38555e+08 -3.81965e+06 -1.6974e+07 3.56352e+08 -957163 -3.09333e+06 3.60732e+08 -174357 1.38051e+07 3.59723e+08 -694820 2.60665e+07 3.69141e+08 -233329 2.06393e+07 3.84782e+08 2.65398e+06 1.85931e+07 4.00637e+08 4.33733e+06 2.52809e+07 4.22326e+08 6.40434e+06 3.22409e+07 4.62549e+08 1.00185e+07 3.20843e+07 5.09263e+08 1.42307e+07 4.37093e+07 5.27142e+08 1.22947e+07 7.20324e+07 5.13695e+08 8.37859e+06 9.22437e+07 4.86646e+08 6.2093e+06 8.85297e+07 4.62615e+08 7.48055e+06 7.00335e+07 4.53986e+08 1.04524e+07 4.15103e+07 4.74846e+08 1.22934e+07 4.1582e+06 5.29361e+08 1.10655e+07 -3.14475e+07 6.04714e+08 6.81711e+06 -4.9754e+07 6.91543e+08 1.16e+06 -5.81492e+07 7.94892e+08 -959788 -7.53273e+07 9.04744e+08 -561936 -8.47626e+07 9.97356e+08 -1.95034e+06 -6.8013e+07 1.06337e+09 -4.33906e+06 -4.10905e+07 1.10686e+09 -4.62616e+06 -2.02356e+07 1.1307e+09 -1.82912e+06 -5.5204e+06 1.13782e+09 114231 7.8091e+06 1.12968e+09 -2.81282e+06 2.47725e+07 1.10606e+09 -3.26845e+06 3.99715e+07 1.06606e+09 2.16318e+06 5.00811e+07 1.01029e+09 8.67966e+06 5.87213e+07 9.46915e+08 6.89997e+06 6.75651e+07 8.77281e+08 3.88155e+06 7.67377e+07 7.86055e+08 1.10541e+07 9.14417e+07 6.73691e+08 1.27402e+07 1.11748e+08 5.4577e+08 4.12365e+06 1.36384e+08 3.96329e+08 657944 1.60812e+08 2.14481e+08 4.85076e+06 1.85039e+08 -1.34329e+07 7.43091e+06 2.18205e+08 -3.07267e+08 5.76733e+06 2.66287e+08 -6.93006e+08 4.06116e+06 3.29187e+08 -1.18947e+09 2.89378e+06 4.01304e+08 -1.80642e+09 1.25192e+06 4.84114e+08 -2.56708e+09 8.76617e+06 5.97653e+08 -3.52129e+09 3.63968e+07 7.41085e+08 -4.5397e+09 7.57665e+07 7.91695e+08 4.54901e+07 4.44128e+08 -1.18279e+08 1.28314e+08 -1.42212e+08 3.14368e+07 -5.80647e+07 -7.94974e+07 4.91034e+07 -1.06542e+08 1.40156e+08 -9.22425e+07 2.11574e+08 -7.27817e+07 2.66365e+08 -5.54967e+07 3.09164e+08 -4.21575e+07 3.43679e+08 -3.05e+07 3.66353e+08 -1.35976e+07 3.74109e+08 5.15027e+06 3.71317e+08 1.96155e+07 3.79866e+08 1.26592e+07 3.97944e+08 1.14208e+07 4.16178e+08 2.04201e+07 4.40845e+08 2.83582e+07 4.87565e+08 2.83537e+07 5.37047e+08 4.14421e+07 5.48936e+08 7.2182e+07 5.22824e+08 9.19835e+07 4.85912e+08 8.72643e+07 4.59264e+08 6.81501e+07 4.54315e+08 4.30624e+07 4.81719e+08 8.70403e+06 5.42856e+08 -2.80775e+07 6.19131e+08 -4.87612e+07 7.0008e+08 -6.09079e+07 8.01009e+08 -8.51894e+07 9.16422e+08 -1.00932e+08 1.01453e+09 -8.64009e+07 1.08198e+09 -5.94422e+07 1.12705e+09 -3.89323e+07 1.1599e+09 -2.52163e+07 1.18225e+09 -1.37455e+07 1.18439e+09 3.02825e+06 1.16836e+09 2.02138e+07 1.14843e+09 2.91198e+07 1.12584e+09 3.78345e+07 1.08715e+09 5.19956e+07 1.03164e+09 6.57989e+07 9.70459e+08 7.8831e+07 8.89124e+08 1.00956e+08 7.68982e+08 1.31497e+08 6.19771e+08 1.56212e+08 4.51529e+08 1.75831e+08 2.48598e+08 2.05345e+08 -1.50255e+07 2.50835e+08 -3.57865e+08 3.08425e+08 -7.90897e+08 3.73041e+08 -1.32272e+09 4.50737e+08 -1.96315e+09 5.53836e+08 -2.69861e+09 6.69106e+08 -3.365e+09 6.76743e+08 3.6255e+08 -9.40285e+07 3.00956e+06 2.02798e+08 -5.04474e+07 2.66673e+06 -5.50515e+06 3.88812e+07 367396 -1.50751e+08 1.10845e+08 -1.38621e+06 -1.55128e+08 1.67571e+08 -2.23294e+06 -1.23869e+08 2.15721e+08 -2.43505e+06 -9.53677e+07 2.57988e+08 -2.26255e+06 -7.24793e+07 2.9517e+08 -1.6157e+06 -5.36388e+07 3.2866e+08 -312811 -3.60848e+07 3.58018e+08 1.22052e+06 -1.71887e+07 3.77272e+08 1.60904e+06 1.25992e+07 3.89444e+08 2.13957e+06 3.84897e+07 4.03134e+08 3.71476e+06 3.83777e+07 4.18098e+08 5.89211e+06 4.15905e+07 4.344e+08 7.77729e+06 5.70295e+07 4.55475e+08 9.2617e+06 7.24523e+07 4.77776e+08 1.19431e+07 8.68171e+07 4.78726e+08 1.08931e+07 1.16378e+08 4.73894e+08 7.28412e+06 1.38999e+08 4.77753e+08 4.3222e+06 1.37973e+08 4.89031e+08 4.67542e+06 1.1524e+08 4.98421e+08 9.71007e+06 8.48985e+07 5.11209e+08 1.63279e+07 4.02632e+07 5.45745e+08 1.73601e+07 -2.38168e+07 6.11754e+08 1.25329e+07 -8.79965e+07 7.07028e+08 6.4585e+06 -1.30908e+08 8.10031e+08 1.02801e+06 -1.42873e+08 8.95669e+08 -2.6174e+06 -1.40362e+08 9.56949e+08 -3.71535e+06 -1.24922e+08 9.96896e+08 -3.87318e+06 -8.74317e+07 1.01832e+09 -3.96622e+06 -4.28314e+07 1.02161e+09 -4.02225e+06 -5.40422e+06 1.00852e+09 -3.93267e+06 2.42629e+07 9.87031e+08 -4.75868e+06 4.55959e+07 9.66877e+08 -5.30148e+06 6.10503e+07 9.50673e+08 -4.19863e+06 7.04735e+07 9.38471e+08 -1.96847e+06 7.40126e+07 9.29599e+08 1.34889e+06 7.56698e+07 9.23986e+08 3.01106e+06 7.96209e+07 9.18563e+08 3.83918e+06 8.79049e+07 9.05227e+08 7.20318e+06 1.07607e+08 8.74871e+08 9.46338e+06 1.43223e+08 8.20965e+08 8.30738e+06 1.93182e+08 7.3606e+08 7.66417e+06 2.48185e+08 6.10554e+08 8.66491e+06 3.07658e+08 4.37787e+08 9.55931e+06 3.77822e+08 2.13008e+08 8.75186e+06 4.61565e+08 -7.11165e+07 6.9953e+06 5.60018e+08 -4.26239e+08 5.47632e+06 6.74182e+08 -8.5619e+08 6.36183e+06 8.0506e+08 -1.33401e+09 1.0879e+07 9.44679e+08 -1.80057e+09 2.01326e+07 1.08062e+09 -2.20362e+09 3.11423e+07 1.09016e+09 1.58367e+07 6.05227e+08 -8.43768e+07 2.00419e+08 -3.90968e+07 -4.76565e+06 5.01098e+07 -1.63062e+08 1.20221e+08 -1.72695e+08 1.74877e+08 -1.43452e+08 2.21176e+08 -1.15301e+08 2.61844e+08 -9.14342e+07 2.97544e+08 -7.13011e+07 3.29418e+08 -5.25942e+07 3.58042e+08 -2.89558e+07 3.76796e+08 2.48502e+06 3.88424e+08 2.97476e+07 4.02032e+08 2.81069e+07 4.18988e+08 3.23375e+07 4.37657e+08 4.96636e+07 4.61983e+08 6.57278e+07 4.82478e+08 8.12631e+07 4.7928e+08 1.11802e+08 4.7041e+08 1.32921e+08 4.69612e+08 1.31881e+08 4.78583e+08 1.09968e+08 4.95847e+08 8.21175e+07 5.25481e+08 4.74598e+07 5.758e+08 -8.78014e+06 6.49541e+08 -7.50231e+07 7.4504e+08 -1.24366e+08 8.43185e+08 -1.4565e+08 9.21528e+08 -1.54575e+08 9.76631e+08 -1.48743e+08 1.0138e+09 -1.16988e+08 1.03465e+09 -7.47415e+07 1.03841e+09 -3.82243e+07 1.02601e+09 -9.25629e+06 1.00095e+09 1.34466e+07 9.73658e+08 3.14476e+07 9.51035e+08 4.46928e+07 9.35035e+08 4.88846e+07 9.30231e+08 4.95771e+07 9.34308e+08 5.64994e+07 9.40348e+08 6.90865e+07 9.44289e+08 8.75533e+07 9.36803e+08 1.23715e+08 9.02809e+08 1.79509e+08 8.32709e+08 2.38454e+08 7.21496e+08 2.96516e+08 5.64979e+08 3.64648e+08 3.58596e+08 4.46343e+08 9.33631e+07 5.42837e+08 -2.40094e+08 6.54219e+08 -6.38892e+08 7.80441e+08 -1.07012e+09 9.13524e+08 -1.47907e+09 1.0354e+09 -1.83113e+09 1.02405e+09 5.54035e+08 -6.63027e+07 2.42138e+06 2.70186e+08 1.56937e+07 2.97742e+06 -8.1369e+07 7.99442e+07 829779 -2.07888e+08 1.29105e+08 -637550 -1.94891e+08 1.6902e+08 -1.40878e+06 -1.52549e+08 2.03537e+08 -1.72827e+06 -1.17161e+08 2.35099e+08 -1.71898e+06 -8.99296e+07 2.6461e+08 -1.32357e+06 -6.7463e+07 2.91311e+08 -486278 -4.51918e+07 3.13444e+08 512575 -2.0741e+07 3.30761e+08 1.23316e+06 1.44609e+07 3.44045e+08 2.13669e+06 4.94593e+07 3.56507e+08 3.66118e+06 5.54335e+07 3.69686e+08 4.50399e+06 6.15833e+07 3.82953e+08 5.44594e+06 8.35211e+07 4.00377e+08 6.84415e+06 1.04743e+08 4.18527e+08 8.60218e+06 1.26052e+08 4.35224e+08 6.53046e+06 1.50121e+08 4.59663e+08 4.64907e+06 1.58077e+08 4.88775e+08 3.30546e+06 1.43905e+08 5.18234e+08 6.41757e+06 1.10782e+08 5.33659e+08 1.59088e+07 7.84897e+07 5.52585e+08 2.43864e+07 1.71416e+07 6.0239e+08 2.04543e+07 -7.70568e+07 6.85725e+08 9.23364e+06 -1.65135e+08 7.87993e+08 61538.4 -2.18806e+08 8.83372e+08 -5.52211e+06 -2.19207e+08 9.50843e+08 -8.41027e+06 -1.8684e+08 9.86815e+08 -9.46516e+06 -1.39037e+08 9.97777e+08 -1.00484e+07 -7.59822e+07 9.8845e+08 -1.04211e+07 -1.08103e+07 9.61162e+08 -1.05472e+07 4.38103e+07 9.19636e+08 -9.66319e+06 8.56037e+07 8.6369e+08 -7.98101e+06 1.18168e+08 7.94738e+08 -6.05196e+06 1.43562e+08 7.17937e+08 -4.42937e+06 1.58251e+08 6.39876e+08 -2.79289e+06 1.60779e+08 5.67533e+08 -867702 1.5435e+08 5.0758e+08 724158 1.44103e+08 4.62413e+08 1.9127e+06 1.36082e+08 4.29749e+08 4.4554e+06 1.40897e+08 3.95667e+08 7.4922e+06 1.76127e+08 3.41568e+08 1.00059e+07 2.44406e+08 2.52537e+08 1.35557e+07 3.29544e+08 1.06425e+08 1.64694e+07 4.37642e+08 -1.14387e+08 2.07144e+07 5.64166e+08 -4.23254e+08 2.16111e+07 7.09438e+08 -8.3814e+08 1.92117e+07 8.80849e+08 -1.37415e+09 1.6031e+07 1.08041e+09 -2.04214e+09 1.77647e+07 1.3088e+09 -2.85973e+09 3.37318e+07 1.58399e+09 -3.93735e+09 7.10757e+07 1.96587e+09 -5.59165e+09 1.20632e+08 2.56525e+09 8.10228e+07 1.39657e+09 -5.7673e+07 2.6447e+08 2.81631e+07 -8.1324e+07 9.31194e+07 -2.21752e+08 1.4113e+08 -2.15342e+08 1.79119e+08 -1.76082e+08 2.11653e+08 -1.41958e+08 2.41605e+08 -1.1448e+08 2.69764e+08 -9.07038e+07 2.95122e+08 -6.64775e+07 3.16393e+08 -3.61255e+07 3.3268e+08 2.16881e+06 3.45497e+08 3.82478e+07 3.57762e+08 4.32425e+07 3.71565e+08 4.99023e+07 3.85794e+08 7.31011e+07 4.03463e+08 9.51897e+07 4.19323e+08 1.1719e+08 4.34162e+08 1.40913e+08 4.542e+08 1.48041e+08 4.78094e+08 1.35973e+08 5.09102e+08 1.04601e+08 5.45306e+08 7.71986e+07 5.93651e+08 3.57441e+07 6.62786e+08 -4.70392e+07 7.48922e+08 -1.42525e+08 8.43184e+08 -2.09839e+08 9.28004e+08 -2.28013e+08 9.86777e+08 -2.14437e+08 1.01657e+09 -1.8119e+08 1.02371e+09 -1.27637e+08 1.01212e+09 -6.75355e+07 9.82064e+08 -1.33496e+07 9.35133e+08 3.25006e+07 8.7419e+08 6.99761e+07 8.02659e+08 9.95086e+07 7.25436e+08 1.19258e+08 6.46581e+08 1.26114e+08 5.72523e+08 1.23564e+08 5.11647e+08 1.1798e+08 4.66429e+08 1.15126e+08 4.3357e+08 1.23623e+08 4.03735e+08 1.59978e+08 3.59908e+08 2.32212e+08 2.80803e+08 3.28248e+08 1.48809e+08 4.37163e+08 -5.70228e+07 5.72639e+08 -3.47402e+08 7.22491e+08 -7.426e+08 8.98418e+08 -1.25352e+09 1.09915e+09 -1.87234e+09 1.32337e+09 -2.5904e+09 1.58191e+09 -3.47745e+09 1.92863e+09 -4.80132e+09 2.45064e+09 1.33032e+09 -9.68131e+06 1.49208e+07 2.97506e+08 6.47273e+07 3.47162e+06 -1.49999e+08 1.07747e+08 847427 -2.43308e+08 1.38164e+08 -440679 -2.15299e+08 1.64135e+08 -1.1486e+06 -1.66365e+08 1.88123e+08 -1.60437e+06 -1.27045e+08 2.11073e+08 -1.84365e+06 -9.68624e+07 2.33741e+08 -1.77389e+06 -7.20937e+07 2.57659e+08 -1.33178e+06 -4.87986e+07 2.81198e+08 -614990 -2.27388e+07 3.03373e+08 348296 1.20078e+07 3.19751e+08 1.38398e+06 5.60409e+07 3.33086e+08 2.81176e+06 6.88389e+07 3.48735e+08 3.68706e+06 7.51111e+07 3.67452e+08 4.04374e+06 9.7633e+07 3.91159e+08 5.18021e+06 1.21196e+08 4.16656e+08 5.58651e+06 1.43002e+08 4.46032e+08 4.50621e+06 1.597e+08 4.8048e+08 3.05667e+06 1.57048e+08 5.18912e+08 3.11573e+06 1.33338e+08 5.52374e+08 9.51955e+06 9.2563e+07 5.65055e+08 2.451e+07 6.11327e+07 5.92271e+08 3.2004e+07 -2.73257e+07 6.61796e+08 1.929e+07 -1.5406e+08 7.58749e+08 1.7304e+06 -2.53549e+08 8.55784e+08 -8.98079e+06 -2.97358e+08 9.2729e+08 -1.43461e+07 -2.67966e+08 9.62922e+08 -1.67133e+07 -1.98224e+08 9.65435e+08 -1.78519e+07 -1.16797e+08 9.44133e+08 -1.89547e+07 -2.85274e+07 9.12786e+08 -2.04829e+07 4.79854e+07 8.76064e+08 -1.99457e+07 1.06521e+08 8.25751e+08 -1.49428e+07 1.55023e+08 7.53861e+08 -9.36366e+06 2.01475e+08 6.59959e+08 -6.38061e+06 2.43378e+08 5.49142e+08 -4.30218e+06 2.7079e+08 4.27056e+08 -1.83462e+06 2.80409e+08 3.01584e+08 498280 2.73406e+08 1.81802e+08 2.18961e+06 2.53502e+08 7.59941e+07 3.21638e+06 2.2832e+08 -2.24898e+06 5.15188e+06 2.02598e+08 -7.22099e+07 4.68304e+06 2.30619e+08 -1.64161e+08 8.40557e+06 3.19393e+08 -3.32825e+08 3.03938e+07 4.60952e+08 -6.39561e+08 4.46747e+07 6.9101e+08 -1.02992e+09 2.85621e+07 9.06132e+08 -1.42472e+09 1.00998e+07 1.0544e+09 -1.7942e+09 6.44419e+06 1.18048e+09 -2.10714e+09 7.18987e+06 1.30907e+09 -2.33601e+09 1.12782e+07 1.45367e+09 -2.47002e+09 1.88537e+07 1.63408e+09 -2.46468e+09 3.64152e+07 1.89252e+09 -2.20083e+09 6.47691e+07 2.22103e+09 1.53719e+07 3.01047e+08 -3.08097e+06 2.91157e+08 7.86304e+07 -1.53775e+08 1.23858e+08 -2.60563e+08 1.5376e+08 -2.39301e+08 1.77923e+08 -1.93992e+08 1.99669e+08 -1.56833e+08 2.20549e+08 -1.27538e+08 2.41498e+08 -1.0229e+08 2.6387e+08 -7.70713e+07 2.86414e+08 -4.47609e+07 3.07348e+08 -4.03093e+06 3.22617e+08 4.19308e+07 3.35539e+08 5.43724e+07 3.51247e+08 6.1204e+07 3.70886e+08 8.44321e+07 3.93954e+08 1.08833e+08 4.17857e+08 1.31079e+08 4.44216e+08 1.47175e+08 4.74012e+08 1.45331e+08 5.07773e+08 1.24846e+08 5.50978e+08 8.63386e+07 6.01122e+08 6.36626e+07 6.67358e+08 1.06305e+07 7.5269e+08 -1.05683e+08 8.42725e+08 -2.24277e+08 9.2234e+08 -2.92487e+08 9.76943e+08 -2.91726e+08 1.00159e+09 -2.51337e+08 9.99828e+08 -1.93159e+08 9.78004e+08 -1.2086e+08 9.4091e+08 -4.74051e+07 8.90826e+08 1.94928e+07 8.30375e+08 7.94278e+07 7.56569e+08 1.3427e+08 6.63825e+08 1.84187e+08 5.54561e+08 2.21117e+08 4.34742e+08 2.39745e+08 3.11088e+08 2.41419e+08 1.93246e+08 2.29948e+08 8.9908e+07 2.12944e+08 9.0887e+06 2.00227e+08 -5.83945e+07 2.22601e+08 -1.39745e+08 3.13797e+08 -2.50361e+08 4.63229e+08 -4.81472e+08 7.06052e+08 -8.90161e+08 9.93829e+08 -1.34643e+09 1.15818e+09 -1.75526e+09 1.26977e+09 -2.07671e+09 1.38044e+09 -2.29831e+09 1.51123e+09 -2.42131e+09 1.6909e+09 -2.41573e+09 1.95943e+09 -2.18916e+09 2.28845e+09 3.127e+08 7.21688e+07 9.97416e+06 2.58863e+08 1.08131e+08 2.10469e+06 -1.7974e+08 1.27208e+08 487060 -2.54953e+08 1.41506e+08 -386983 -2.19834e+08 1.56867e+08 -1.06524e+06 -1.69478e+08 1.73423e+08 -1.70844e+06 -1.28888e+08 1.90437e+08 -2.25297e+06 -9.67243e+07 2.08056e+08 -2.54482e+06 -7.00521e+07 2.28683e+08 -2.51961e+06 -4.68448e+07 2.53943e+08 -2.29076e+06 -2.29053e+07 2.80701e+08 -1.10415e+06 6.63953e+06 3.01971e+08 -76525.3 5.7398e+07 3.18401e+08 1.36679e+06 7.81922e+07 3.35902e+08 2.36856e+06 8.51359e+07 3.58897e+08 3.07606e+06 1.05182e+08 3.88948e+08 4.03696e+06 1.26981e+08 4.23084e+08 3.93963e+06 1.44956e+08 4.61312e+08 3.16678e+06 1.55391e+08 5.03319e+08 2.30371e+06 1.4509e+08 5.47023e+08 4.06333e+06 1.14512e+08 5.78938e+08 1.52116e+07 6.91531e+07 5.85715e+08 3.63673e+07 3.75225e+07 6.23711e+08 3.87428e+07 -9.01164e+07 7.10248e+08 1.44082e+07 -2.43776e+08 8.10599e+08 -8.03988e+06 -3.359e+08 8.90661e+08 -1.85895e+07 -3.49608e+08 9.30313e+08 -2.26986e+07 -2.76632e+08 9.27306e+08 -2.39509e+07 -1.63535e+08 8.88823e+08 -2.58481e+07 -4.41892e+07 8.45383e+08 -2.94643e+07 5.29901e+07 8.12333e+08 -3.1812e+07 1.20583e+08 7.7359e+08 -2.48716e+07 1.76518e+08 7.13987e+08 -1.18386e+07 2.30925e+08 6.30303e+08 -3.82828e+06 2.90861e+08 5.23339e+08 -2.10785e+06 3.51694e+08 3.92735e+08 -537401 3.98667e+08 2.40801e+08 2.63403e+06 4.2368e+08 7.46555e+07 5.59069e+06 4.25228e+08 -9.70113e+07 7.70269e+06 4.07152e+08 -2.73087e+08 7.99961e+06 3.86247e+08 -4.96693e+08 1.34538e+07 4.02693e+08 -8.02544e+08 1.74216e+07 5.10848e+08 -1.16458e+09 1.51919e+07 6.64456e+08 -1.51491e+09 1.54859e+07 8.02348e+08 -1.73962e+09 7.02698e+06 9.21108e+08 -1.8225e+09 -1.02819e+07 1.01024e+09 -1.82614e+09 -1.41053e+07 1.07727e+09 -1.7772e+09 -1.31825e+07 1.14459e+09 -1.68794e+09 -1.07466e+07 1.22687e+09 -1.56057e+09 -8.91911e+06 1.33089e+09 -1.38203e+09 -7.27501e+06 1.47424e+09 -1.15048e+09 -7.90403e+06 1.68957e+09 -8.46236e+08 -1.71739e+07 1.96884e+09 -5.09507e+06 1.49429e+08 8.40515e+07 2.68675e+08 1.25963e+08 -1.88016e+08 1.47285e+08 -2.76096e+08 1.61532e+08 -2.4756e+08 1.75286e+08 -2.01165e+08 1.89326e+08 -1.63668e+08 2.03622e+08 -1.33848e+08 2.18912e+08 -1.08305e+08 2.37577e+08 -8.42924e+07 2.61612e+08 -5.54745e+07 2.86927e+08 -1.60793e+07 3.06636e+08 3.84545e+07 3.2217e+08 5.96178e+07 3.39704e+08 6.74033e+07 3.62909e+08 8.86815e+07 3.92394e+08 1.11101e+08 4.24673e+08 1.2984e+08 4.59384e+08 1.39943e+08 4.96653e+08 1.3195e+08 5.38365e+08 1.05263e+08 5.92098e+08 6.27783e+07 6.55765e+08 4.80989e+07 7.37434e+08 -2.36097e+07 8.29871e+08 -1.76634e+08 9.10484e+08 -3.06572e+08 9.6351e+08 -3.58307e+08 9.81855e+08 -3.27683e+08 9.69733e+08 -2.58826e+08 9.32711e+08 -1.78031e+08 8.77304e+08 -9.13195e+07 8.12273e+08 -1.11776e+07 7.47712e+08 6.11756e+07 6.8511e+08 1.30915e+08 6.10023e+08 2.0468e+08 5.1045e+08 2.78887e+08 3.85958e+08 3.40196e+08 2.41156e+08 3.79688e+08 8.25829e+07 3.9603e+08 -8.13268e+07 3.91389e+08 -2.42532e+08 3.71501e+08 -4.38003e+08 3.98286e+08 -7.25617e+08 5.18411e+08 -1.09701e+09 6.96423e+08 -1.47176e+09 8.55633e+08 -1.74083e+09 9.87484e+08 -1.83564e+09 1.08361e+09 -1.83205e+09 1.14313e+09 -1.77515e+09 1.20003e+09 -1.68108e+09 1.27538e+09 -1.55035e+09 1.37789e+09 -1.37281e+09 1.52527e+09 -1.15316e+09 1.75179e+09 -8.83303e+08 2.03855e+09 1.57283e+08 1.09535e+08 -459414 1.81674e+08 1.28401e+08 1.04029e+06 -1.9169e+08 1.33629e+08 416906 -2.52912e+08 1.38311e+08 -113500 -2.1498e+08 1.47519e+08 -822164 -1.66369e+08 1.59945e+08 -1.70017e+06 -1.26078e+08 1.73749e+08 -2.57953e+06 -9.23389e+07 1.88305e+08 -3.2232e+06 -6.34662e+07 2.05449e+08 -3.49098e+06 -3.9507e+07 2.30898e+08 -3.68204e+06 -2.09192e+07 2.60695e+08 -2.85791e+06 984648 2.88153e+08 -1.79549e+06 5.31444e+07 3.08514e+08 -456514 8.32383e+07 3.29203e+08 758929 9.10961e+07 3.56112e+08 1.86896e+06 1.08236e+08 3.8977e+08 2.81186e+06 1.2695e+08 4.28676e+08 3.06061e+06 1.40515e+08 4.70815e+08 2.37778e+06 1.45199e+08 5.16802e+08 2.56748e+06 1.29034e+08 5.62631e+08 6.58821e+06 9.11591e+07 5.90107e+08 2.45898e+07 4.30727e+07 5.91642e+08 5.14601e+07 5.82897e+06 6.42489e+08 4.27819e+07 -1.68845e+08 7.4148e+08 4.78789e+06 -3.35756e+08 8.34742e+08 -2.00903e+07 -3.98599e+08 8.89154e+08 -2.82151e+07 -3.66035e+08 8.90032e+08 -2.89017e+07 -2.39118e+08 8.3675e+08 -2.85e+07 -7.23572e+07 7.7423e+08 -3.40711e+07 6.17388e+07 7.42405e+08 -4.16759e+07 1.34679e+08 7.12322e+08 -3.99165e+07 1.97249e+08 6.58407e+08 -2.35168e+07 2.59082e+08 5.79318e+08 -1.92149e+06 3.15253e+08 4.82535e+08 9.30453e+06 3.79686e+08 3.69899e+08 8.23024e+06 4.54336e+08 2.32163e+08 6.98336e+06 5.23852e+08 6.30641e+07 1.0381e+07 5.73297e+08 -1.34705e+08 1.44565e+07 5.968e+08 -3.60515e+08 1.72886e+07 6.03611e+08 -6.30755e+08 2.04723e+07 6.25733e+08 -9.61106e+08 2.34255e+07 7.01317e+08 -1.27061e+09 1.60729e+07 7.97604e+08 -1.48493e+09 7.50904e+06 8.67935e+08 -1.59757e+09 4.4644e+06 9.13131e+08 -1.61456e+09 -954464 9.46142e+08 -1.56245e+09 -4.82591e+06 9.72655e+08 -1.46725e+09 -5.36384e+06 9.98734e+08 -1.34093e+09 -4.9559e+06 1.03455e+09 -1.18826e+09 -2.74632e+06 1.09275e+09 -1.00504e+09 -404984 1.1726e+09 -7.84537e+08 -871526 1.28616e+09 -5.55523e+08 -6.38144e+06 1.52198e+09 -4.44667e+08 -2.44656e+07 1.93517e+09 -6.7667e+06 1.42591e+08 1.27029e+08 1.7766e+08 1.50333e+08 -2.04924e+08 1.58232e+08 -2.7814e+08 1.63542e+08 -2.46433e+08 1.71474e+08 -2.02034e+08 1.81106e+08 -1.6577e+08 1.91316e+08 -1.36016e+08 2.02487e+08 -1.10462e+08 2.17094e+08 -8.80249e+07 2.40729e+08 -6.70221e+07 2.69252e+08 -3.25714e+07 2.94622e+08 2.65901e+07 3.13519e+08 5.80072e+07 3.34218e+08 6.73355e+07 3.6098e+08 8.63929e+07 3.94455e+08 1.06319e+08 4.30758e+08 1.2124e+08 4.69513e+08 1.26608e+08 5.11202e+08 1.1354e+08 5.60485e+08 8.08018e+07 6.27073e+08 3.61137e+07 7.06201e+08 3.27632e+07 7.98736e+08 -6.4451e+07 8.87277e+08 -2.53205e+08 9.45897e+08 -3.78922e+08 9.64656e+08 -3.99549e+08 9.45334e+08 -3.32357e+08 8.96223e+08 -2.33647e+08 8.20162e+08 -1.31512e+08 7.25125e+08 -3.36861e+07 6.28034e+08 4.9601e+07 5.48011e+08 1.20364e+08 4.85521e+08 1.93046e+08 4.18624e+08 2.80569e+08 3.26383e+08 3.76939e+08 2.01009e+08 4.67036e+08 4.40322e+07 5.3859e+08 -1.40965e+08 5.85671e+08 -3.52062e+08 6.1059e+08 -6.11085e+08 6.44036e+08 -9.29574e+08 7.34919e+08 -1.25153e+09 8.52678e+08 -1.48298e+09 9.34578e+08 -1.60164e+09 9.81736e+08 -1.62143e+09 1.01218e+09 -1.56693e+09 1.03108e+09 -1.46896e+09 1.04646e+09 -1.33984e+09 1.07452e+09 -1.18523e+09 1.13152e+09 -1.00043e+09 1.2114e+09 -7.78075e+08 1.33005e+09 -5.57474e+08 1.57367e+09 -4.85454e+08 1.97769e+09 1.42223e+08 1.0627e+08 -393133 9.11944e+07 1.26963e+08 965528 -2.04468e+08 1.2783e+08 899693 -2.46273e+08 1.29001e+08 467434 -2.0672e+08 1.36022e+08 -416881 -1.6109e+08 1.47157e+08 -1.60424e+06 -1.21694e+08 1.6024e+08 -2.87755e+06 -8.65279e+07 1.73945e+08 -3.91573e+06 -5.49499e+07 1.88908e+08 -4.51308e+06 -2.89552e+07 2.10551e+08 -4.93917e+06 -1.38596e+07 2.40667e+08 -4.95158e+06 -520969 2.74638e+08 -3.83421e+06 4.41522e+07 2.99866e+08 -2.62723e+06 8.40918e+07 3.23791e+08 -1.11316e+06 9.43574e+07 3.54676e+08 95960.4 1.06516e+08 3.93049e+08 1.40192e+06 1.21416e+08 4.34165e+08 2.04352e+06 1.32537e+08 4.76432e+08 1.999e+06 1.3443e+08 5.19746e+08 3.69e+06 1.15168e+08 5.59964e+08 1.14244e+07 7.03638e+07 5.76072e+08 3.86336e+07 1.71638e+07 5.76486e+08 6.86162e+07 -3.93038e+07 6.43957e+08 4.07227e+07 -2.60983e+08 7.53132e+08 -1.10815e+07 -4.20885e+08 8.30868e+08 -3.4398e+07 -4.30576e+08 8.53552e+08 -3.60153e+07 -3.41465e+08 8.05919e+08 -2.97335e+07 -1.49103e+08 7.13301e+08 -3.14843e+07 6.84339e+07 6.73392e+08 -4.42748e+07 1.62865e+08 6.67779e+08 -5.49384e+07 2.08816e+08 6.4192e+08 -4.9804e+07 2.83586e+08 5.81067e+08 -3.2261e+07 3.60328e+08 4.8012e+08 -1.4847e+06 4.24584e+08 3.55963e+08 2.04458e+07 4.88604e+08 2.25678e+08 2.06173e+07 5.67881e+08 8.23972e+07 1.70395e+07 6.50625e+08 -9.25021e+07 2.1186e+07 7.23553e+08 -3.06601e+08 2.85997e+07 7.7768e+08 -5.68286e+08 3.72181e+07 8.2627e+08 -8.63542e+08 3.81015e+07 8.8446e+08 -1.12905e+09 2.56303e+07 9.43547e+08 -1.31663e+09 1.36351e+07 9.73461e+08 -1.42402e+09 7.30773e+06 9.7051e+08 -1.46778e+09 5.66529e+06 9.55079e+08 -1.46571e+09 3.04147e+06 9.45952e+08 -1.42856e+09 646688 9.39678e+08 -1.35534e+09 -1.08461e+06 9.29529e+08 -1.23785e+09 -708880 9.24535e+08 -1.07974e+09 3.81254e+06 9.45006e+08 -8.78569e+08 6.43037e+06 9.91395e+08 -5.98589e+08 5.65525e+06 1.04798e+09 -2.70826e+08 -4.83503e+06 1.26432e+09 -1.39324e+08 -4.37576e+07 1.91989e+09 -1.25052e+07 1.97346e+08 1.29862e+08 8.61482e+07 1.53406e+08 -2.21145e+08 1.57651e+08 -2.75516e+08 1.60342e+08 -2.41694e+08 1.66602e+08 -2.00469e+08 1.74737e+08 -1.65949e+08 1.83138e+08 -1.36346e+08 1.91746e+08 -1.10502e+08 2.02648e+08 -8.9166e+07 2.2182e+08 -7.50144e+07 2.51125e+08 -5.04634e+07 2.83178e+08 5.86016e+06 3.06354e+08 4.91576e+07 3.29551e+08 6.17655e+07 3.60562e+08 7.67201e+07 3.98407e+08 9.3805e+07 4.37179e+08 1.07894e+08 4.76539e+08 1.11845e+08 5.17957e+08 9.6867e+07 5.70303e+08 5.91846e+07 6.50684e+08 1.12712e+07 7.47118e+08 1.89084e+07 8.44895e+08 -1.11641e+08 9.20134e+08 -3.31262e+08 9.47327e+08 -4.33067e+08 9.29851e+08 -4.10942e+08 8.77512e+08 -3.0211e+08 7.94587e+08 -1.73561e+08 6.81532e+08 -5.31723e+07 5.50984e+08 5.17155e+07 4.22586e+08 1.37434e+08 3.09846e+08 2.09735e+08 2.25592e+08 2.84169e+08 1.619e+08 3.72413e+08 8.56279e+07 4.78289e+08 -1.75696e+07 5.87998e+08 -1.59128e+08 6.99567e+08 -3.48945e+08 8.02315e+08 -5.81745e+08 8.81432e+08 -8.53202e+08 9.56276e+08 -1.11812e+09 1.0276e+09 -1.30985e+09 1.05913e+09 -1.41763e+09 1.05063e+09 -1.45688e+09 1.02789e+09 -1.45191e+09 1.01122e+09 -1.4155e+09 9.95402e+08 -1.34288e+09 9.74069e+08 -1.22225e+09 9.58102e+08 -1.06118e+09 9.86367e+08 -8.51839e+08 1.03351e+09 -5.67132e+08 1.09383e+09 -2.60165e+08 1.31886e+09 -1.93535e+08 1.92374e+09 1.74599e+08 9.2451e+07 1.37781e+06 1.03455e+07 1.13775e+08 1.95548e+06 -2.17417e+08 1.13612e+08 1.76063e+06 -2.38353e+08 1.14939e+08 1.32655e+06 -1.98508e+08 1.22453e+08 225458 -1.56082e+08 1.34299e+08 -1.28674e+06 -1.17515e+08 1.48535e+08 -2.99704e+06 -8.09238e+07 1.63534e+08 -4.53753e+06 -4.63028e+07 1.78304e+08 -5.5098e+06 -1.64776e+07 1.9381e+08 -5.87202e+06 1.44523e+06 2.19551e+08 -6.89003e+06 7.66759e+06 2.5776e+08 -5.9327e+06 3.4752e+07 2.89277e+08 -4.91659e+06 8.09189e+07 3.1575e+08 -3.46446e+06 9.70961e+07 3.48543e+08 -1.94891e+06 1.03889e+08 3.88972e+08 -189030 1.13987e+08 4.31994e+08 1.04818e+06 1.22176e+08 4.7551e+08 2.165e+06 1.2366e+08 5.15176e+08 5.84208e+06 1.0426e+08 5.43227e+08 1.95275e+07 5.81164e+07 5.36612e+08 5.85278e+07 -378742 5.39426e+08 8.5572e+07 -9.82655e+07 6.27688e+08 2.92411e+07 -3.58662e+08 7.46113e+08 -3.28868e+07 -4.89984e+08 7.97929e+08 -4.80767e+07 -4.19434e+08 7.78637e+08 -3.48585e+07 -2.69115e+08 6.62937e+08 -1.74089e+07 1.13017e+07 5.72825e+08 -3.08035e+07 2.24272e+08 5.89596e+08 -6.38798e+07 2.40743e+08 6.32008e+08 -7.70989e+07 2.67451e+08 6.57517e+08 -7.3403e+07 3.49904e+08 6.47047e+08 -6.02316e+07 4.4689e+08 5.83915e+08 -3.03943e+07 5.33298e+08 4.64289e+08 1.74117e+06 6.22812e+08 2.80877e+08 -7.54618e+06 7.72076e+08 6.9911e+07 -2.34293e+07 8.93198e+08 -1.3928e+08 -5.4058e+06 9.43418e+08 -3.78945e+08 3.02595e+07 9.91659e+08 -6.48352e+08 4.7094e+07 1.05372e+09 -9.00984e+08 3.53048e+07 1.10744e+09 -1.10439e+09 1.96372e+07 1.13184e+09 -1.26299e+09 1.13039e+07 1.12292e+09 -1.38795e+09 7.83997e+06 1.08752e+09 -1.48141e+09 6.85128e+06 1.03955e+09 -1.54358e+09 4.28007e+06 9.99424e+08 -1.57228e+09 668157 9.60712e+08 -1.55534e+09 -2.89321e+06 9.10388e+08 -1.47604e+09 -5.52864e+06 8.51236e+08 -1.34738e+09 2.79965e+06 8.26265e+08 -1.17876e+09 7.93579e+06 8.45629e+08 -8.83209e+08 4.33288e+06 7.98109e+08 -3.01069e+08 -1.12159e+07 7.68286e+08 3.67686e+08 -9.8905e+07 1.42697e+09 -3.30654e+07 2.65046e+08 1.18595e+08 -2.70388e+06 1.44846e+08 -2.38639e+08 1.49421e+08 -2.71708e+08 1.53373e+08 -2.36795e+08 1.60824e+08 -1.98983e+08 1.69585e+08 -1.6602e+08 1.77893e+08 -1.36326e+08 1.85348e+08 -1.09696e+08 1.93309e+08 -8.80709e+07 2.05196e+08 -7.60806e+07 2.30584e+08 -6.49933e+07 2.68132e+08 -2.16783e+07 2.97207e+08 3.16401e+07 3.22327e+08 5.14121e+07 3.55024e+08 6.22717e+07 3.95126e+08 7.59784e+07 4.36801e+08 8.98955e+07 4.78619e+08 9.43871e+07 5.2139e+08 8.16553e+07 5.75298e+08 4.52852e+07 6.66662e+08 -3.17086e+06 7.7555e+08 1.14254e+07 8.70793e+08 -1.66304e+08 9.25986e+08 -4.09675e+08 9.14892e+08 -4.61118e+08 8.6534e+08 -3.86798e+08 7.91371e+08 -2.34043e+08 6.8659e+08 -8.54094e+07 5.53088e+08 3.30591e+07 4.14181e+08 1.31355e+08 2.82607e+08 2.14436e+08 1.52172e+08 2.99779e+08 4.23238e+07 3.84861e+08 -3.02445e+07 4.65402e+08 -1.09355e+08 5.65129e+08 -2.11506e+08 6.78507e+08 -3.31476e+08 8.22745e+08 -4.95081e+08 1.00455e+09 -7.00133e+08 1.1428e+09 -9.11535e+08 1.21134e+09 -1.09144e+09 1.23316e+09 -1.23342e+09 1.21603e+09 -1.34599e+09 1.17246e+09 -1.43089e+09 1.11887e+09 -1.49033e+09 1.07209e+09 -1.52111e+09 1.02327e+09 -1.50594e+09 9.5363e+08 -1.41293e+09 8.62196e+08 -1.27106e+09 8.5965e+08 -1.08237e+09 8.77725e+08 -7.66696e+08 8.21267e+08 -2.41197e+08 8.36501e+08 2.61325e+08 1.36504e+09 1.77773e+08 7.21769e+07 1.47397e+06 -6.46329e+07 9.33257e+07 2.54929e+06 -2.29913e+08 9.38214e+07 2.75804e+06 -2.30925e+08 9.71988e+07 2.34241e+06 -1.92339e+08 1.06498e+08 1.0419e+06 -1.52793e+08 1.20102e+08 -748058 -1.14825e+08 1.36686e+08 -2.90669e+06 -7.69724e+07 1.55031e+08 -5.06578e+06 -3.96619e+07 1.72851e+08 -6.63949e+06 -5.28064e+06 1.8695e+08 -7.15897e+06 1.99374e+07 2.04907e+08 -7.82694e+06 2.58794e+07 2.40457e+08 -7.55802e+06 3.26938e+07 2.77164e+08 -7.10316e+06 7.51188e+07 3.05771e+08 -6.28151e+06 1.00143e+08 3.37369e+08 -4.54795e+06 1.04324e+08 3.76029e+08 -2.29081e+06 1.08554e+08 4.18421e+08 -76169.4 1.12409e+08 4.60495e+08 2.10696e+06 1.13134e+08 4.97397e+08 8.77422e+06 9.34194e+07 5.13888e+08 3.04757e+07 4.84134e+07 4.80887e+08 8.21596e+07 -1.25411e+07 4.91413e+08 9.64808e+07 -1.71627e+08 5.9942e+08 5.26192e+06 -4.5044e+08 7.24928e+08 -5.8667e+07 -5.3823e+08 7.34041e+08 -5.59692e+07 -3.55062e+08 6.45678e+08 -1.20484e+07 -1.42579e+08 4.5413e+08 1.71094e+07 2.35481e+08 3.76576e+08 -2.53558e+07 3.86856e+08 4.02827e+08 -8.72148e+07 3.51461e+08 4.58387e+08 -1.05254e+08 3.54848e+08 5.16072e+08 -9.54014e+07 4.18258e+08 5.61289e+08 -6.67189e+07 4.95093e+08 5.97304e+08 -2.75688e+07 5.48989e+08 6.54297e+08 -1.18689e+07 5.97104e+08 6.97626e+08 -5.96393e+07 8.0487e+08 5.99845e+08 -9.26931e+07 1.09576e+09 3.47271e+08 -1.75936e+07 1.2201e+09 -2.86361e+07 6.23101e+07 1.30952e+09 -4.22363e+08 6.26292e+07 1.38922e+09 -7.43941e+08 2.84206e+07 1.40518e+09 -1.01199e+09 1.04859e+07 1.39315e+09 -1.25801e+09 5.03087e+06 1.36578e+09 -1.49838e+09 6.7073e+06 1.32018e+09 -1.73154e+09 1.17004e+07 1.25541e+09 -1.95344e+09 1.37189e+07 1.19686e+09 -2.16344e+09 1.25933e+07 1.14398e+09 -2.35395e+09 8.94706e+06 1.07626e+09 -2.52411e+09 1.65137e+06 1.00649e+09 -2.74708e+09 2.90547e+07 1.01527e+09 -2.81055e+09 4.43105e+07 8.71617e+08 -2.28646e+09 -1.26557e+07 3.07417e+08 -1.1466e+09 -4.03066e+07 -2.89745e+08 7.29791e+08 -2.2213e+08 -1.91428e+08 -9.78873e+07 -8.94275e+07 1.04109e+08 -7.90592e+07 1.30391e+08 -2.54475e+08 1.36682e+08 -2.67704e+08 1.43883e+08 -2.33353e+08 1.54038e+08 -1.98743e+08 1.64707e+08 -1.66936e+08 1.74161e+08 -1.36939e+08 1.81919e+08 -1.09411e+08 1.8838e+08 -8.65869e+07 1.9463e+08 -7.29867e+07 2.11189e+08 -7.12858e+07 2.48931e+08 -4.94522e+07 2.85161e+08 5.03898e+06 3.12318e+08 3.58066e+07 3.43916e+08 4.55253e+07 3.83347e+08 5.52995e+07 4.25065e+08 6.92617e+07 4.67812e+08 7.49014e+07 5.14764e+08 6.47865e+07 5.7715e+08 3.38187e+07 6.83105e+08 -6.71943e+06 7.93799e+08 1.49029e+07 8.74686e+08 -2.29182e+08 9.05456e+08 -4.87636e+08 8.50814e+08 -4.51712e+08 7.8711e+08 -3.22079e+08 7.1991e+08 -1.31809e+08 6.05362e+08 2.70646e+07 4.45736e+08 1.34485e+08 2.90015e+08 2.1586e+08 1.85071e+08 2.59873e+08 1.7173e+08 2.81694e+08 2.45261e+08 3.14899e+08 3.02727e+08 4.22588e+08 1.95291e+08 6.35739e+08 -6.80762e+07 8.70927e+08 -3.1702e+08 1.07344e+09 -4.96375e+08 1.26271e+09 -6.5857e+08 1.38068e+09 -8.26315e+08 1.41752e+09 -9.98142e+08 1.42238e+09 -1.17474e+09 1.40054e+09 -1.34963e+09 1.3518e+09 -1.50882e+09 1.2822e+09 -1.64789e+09 1.21235e+09 -1.76951e+09 1.14149e+09 -1.86443e+09 1.04108e+09 -1.88613e+09 8.72263e+08 -1.91762e+09 9.13631e+08 -1.96427e+09 9.71128e+08 -1.58557e+09 4.43839e+08 -6.48323e+08 -1.15099e+08 7.357e+08 -2.23042e+08 -1.53809e+08 5.04472e+07 3.06453e+06 -1.25543e+08 6.87839e+07 3.86467e+06 -2.39309e+08 7.04763e+07 4.13148e+06 -2.24471e+08 7.63965e+07 3.64157e+06 -1.88544e+08 8.76456e+07 2.20284e+06 -1.51315e+08 1.03175e+08 237439 -1.13881e+08 1.22498e+08 -2.32656e+06 -7.52571e+07 1.45008e+08 -5.21186e+06 -3.60161e+07 1.68595e+08 -7.75004e+06 2.32821e+06 1.87554e+08 -9.02588e+06 3.64937e+07 1.98129e+08 -8.28455e+06 5.32063e+07 2.22114e+08 -8.17091e+06 4.76869e+07 2.65526e+08 -7.77534e+06 6.62012e+07 2.97633e+08 -8.46435e+06 1.02508e+08 3.24326e+08 -7.46127e+06 1.12673e+08 3.5581e+08 -4.63064e+06 1.11338e+08 3.93648e+08 -986623 1.08171e+08 4.30477e+08 2.53857e+06 1.07174e+08 4.59984e+08 1.36252e+07 8.66549e+07 4.61404e+08 4.58897e+07 4.05588e+07 4.09072e+08 1.09612e+08 -2.52032e+07 4.39394e+08 9.63508e+07 -2.5868e+08 5.67436e+08 -3.00801e+07 -5.21608e+08 7.00461e+08 -8.23369e+07 -5.64395e+08 6.41783e+08 -4.88978e+07 -2.21725e+08 4.08834e+08 5.33658e+07 9.66879e+07 1.35249e+08 9.06584e+07 5.27316e+08 9.40626e+07 -2.73572e+07 5.58643e+08 1.86373e+08 -1.24497e+08 4.61885e+08 2.95953e+08 -1.37949e+08 4.38582e+08 3.00863e+08 -7.93435e+07 5.31241e+08 1.14613e+08 2.29277e+07 6.85563e+08 -3.03753e+08 1.36783e+08 8.49666e+08 -8.17372e+08 1.85201e+08 9.38534e+08 -9.97712e+08 6.1648e+07 9.32662e+08 -8.82744e+08 -9.54194e+07 1.08484e+09 -9.54459e+08 -5.74793e+07 1.35522e+09 -1.20233e+09 -3.65144e+07 1.5965e+09 -1.45138e+09 -8.5773e+07 1.72445e+09 -1.65035e+09 -1.14416e+08 1.71798e+09 -1.82218e+09 -1.19603e+08 1.68243e+09 -2.01032e+09 -1.07965e+08 1.65656e+09 -2.26475e+09 -8.45299e+07 1.64777e+09 -2.61472e+09 -5.51491e+07 1.64144e+09 -3.03866e+09 -3.86671e+07 1.63229e+09 -3.51174e+09 -3.6023e+07 1.61848e+09 -3.99758e+09 -4.80304e+07 1.57214e+09 -4.29362e+09 -1.54511e+08 1.42112e+09 -4.15605e+09 -1.72689e+08 1.01928e+09 -3.71101e+09 1.16694e+08 2.86181e+08 -3.29561e+09 3.05324e+08 -4.20014e+08 -3.13418e+09 3.27209e+08 -7.73965e+08 -7.7787e+08 1.64702e+07 -2.55323e+09 -1.94681e+08 -2.48888e+09 8.6586e+07 -1.45257e+08 1.12222e+08 -2.67917e+08 1.21245e+08 -2.64334e+08 1.32487e+08 -2.31975e+08 1.45829e+08 -1.9995e+08 1.58979e+08 -1.68858e+08 1.7034e+08 -1.38463e+08 1.79156e+08 -1.10003e+08 1.85351e+08 -8.56435e+07 1.88112e+08 -6.81348e+07 1.93027e+08 -6.59025e+07 2.2205e+08 -6.73521e+07 2.68002e+08 -3.10111e+07 3.00427e+08 1.2506e+07 3.28309e+08 2.88371e+07 3.63251e+08 3.5778e+07 4.02521e+08 4.92337e+07 4.4395e+08 5.66629e+07 4.93184e+08 4.99023e+07 5.68816e+08 2.54205e+07 6.9603e+08 -3.77293e+06 7.9959e+08 2.70048e+07 8.58386e+08 -3.0351e+08 8.67653e+08 -5.66042e+08 7.67528e+08 -3.86563e+08 7.20658e+08 -2.01375e+08 6.85041e+08 2.4784e+07 5.4049e+08 1.82639e+08 3.35728e+08 2.6052e+08 1.82057e+08 2.81003e+08 1.32316e+08 2.7768e+08 1.50179e+08 3.28575e+08 1.80916e+08 4.53728e+08 2.17756e+08 5.9613e+08 2.64525e+08 6.74605e+08 2.71408e+08 7.92866e+08 2.55075e+08 1.05146e+09 1.05196e+08 1.38971e+09 -1.71821e+08 1.58191e+09 -4.75717e+08 1.61411e+09 -7.9295e+08 1.62348e+09 -1.14237e+09 1.64029e+09 -1.51836e+09 1.63534e+09 -1.90286e+09 1.59684e+09 -2.30703e+09 1.55701e+09 -2.776e+09 1.54985e+09 -3.3719e+09 1.56298e+09 -4.21447e+09 1.53638e+09 -4.75102e+09 1.25904e+09 -4.08788e+09 4.1309e+08 -2.82761e+09 -5.15903e+08 -1.7559e+09 -8.56385e+08 3.73271e+08 -2.33529e+09 -1.54675e+09 2.61278e+07 3.29232e+06 -1.77133e+08 4.1114e+07 4.95219e+06 -2.4624e+08 4.44074e+07 5.5819e+06 -2.2022e+08 5.23669e+07 5.04061e+06 -1.87252e+08 6.48942e+07 3.64118e+06 -1.51592e+08 8.17845e+07 1.70308e+06 -1.14743e+08 1.03427e+08 -1.0666e+06 -7.59964e+07 1.30005e+08 -4.53876e+06 -3.58115e+07 1.60583e+08 -8.12546e+06 4.92084e+06 1.91321e+08 -1.07477e+07 4.47438e+07 2.1144e+08 -1.02902e+07 7.55372e+07 2.19791e+08 -7.1247e+06 8.17788e+07 2.50304e+08 -5.75619e+06 7.43255e+07 2.91211e+08 -8.5629e+06 9.78273e+07 3.17709e+08 -9.86799e+06 1.23147e+08 3.3669e+08 -6.54836e+06 1.28296e+08 3.61515e+08 -1.89875e+06 1.16805e+08 3.88134e+08 4.12996e+06 1.08422e+08 4.02707e+08 2.18071e+07 8.49958e+07 3.78369e+08 6.89658e+07 3.50979e+07 3.14767e+08 1.36809e+08 -5.40737e+07 3.81115e+08 7.6339e+07 -3.57981e+08 5.34536e+08 -7.60717e+07 -5.71451e+08 6.7286e+08 -9.80632e+07 -5.77004e+08 4.94706e+08 -1.89324e+07 1.39415e+07 3.06293e+07 1.75916e+08 4.97212e+08 -8.26763e+07 1.7138e+08 6.22655e+08 9.37646e+07 -2.22784e+07 5.17185e+08 2.49546e+08 -1.18504e+08 4.97348e+08 3.02822e+08 -1.35652e+08 5.69082e+08 1.92853e+08 -5.33031e+07 7.30402e+08 -1.15427e+08 8.52356e+07 9.38188e+08 -5.70795e+08 1.98626e+08 1.12943e+09 -1.04479e+09 1.87313e+08 1.24202e+09 -1.31545e+09 -6.80347e+07 1.28624e+09 -1.38369e+09 -2.86402e+08 1.45504e+09 -1.42747e+09 -2.95105e+08 1.71027e+09 -1.5331e+09 -1.9521e+08 1.9129e+09 -1.68937e+09 -1.17315e+08 2.01309e+09 -1.9086e+09 -8.10041e+07 2.03208e+09 -2.20984e+09 -4.08909e+07 2.03658e+09 -2.5799e+09 -9.56822e+06 2.04443e+09 -2.96966e+09 -2.10898e+07 2.06116e+09 -3.32698e+09 -6.83483e+07 2.0618e+09 -3.59654e+09 -1.1959e+08 2.00585e+09 -3.74154e+09 -1.4875e+08 1.88675e+09 -3.72315e+09 -1.29581e+08 1.64483e+09 -3.43078e+09 -1.01662e+08 1.1765e+09 -2.947e+09 -4.54674e+07 5.16895e+08 -2.56686e+09 1.11622e+08 -2.73149e+08 -2.28184e+09 1.64587e+08 -9.36366e+08 -1.55001e+09 1.34785e+08 -1.68734e+09 -6.8262e+08 1.23861e+08 -3.58727e+09 1.98359e+08 -3.84918e+09 6.85219e+07 -1.98843e+08 9.21334e+07 -2.77621e+08 1.04212e+08 -2.61827e+08 1.19219e+08 -2.32267e+08 1.35439e+08 -2.01983e+08 1.51053e+08 -1.71251e+08 1.64641e+08 -1.40532e+08 1.75117e+08 -1.11286e+08 1.82016e+08 -8.5694e+07 1.84977e+08 -6.54607e+07 1.83614e+08 -5.66892e+07 1.93393e+08 -6.46988e+07 2.35619e+08 -5.9658e+07 2.80726e+08 -2.32359e+07 3.10782e+08 6.87681e+06 3.39071e+08 1.93985e+07 3.72898e+08 3.18061e+07 4.1119e+08 4.05126e+07 4.61906e+08 3.89023e+07 5.50936e+08 2.29759e+07 6.97063e+08 6.46626e+06 7.83811e+08 3.66961e+07 8.20469e+08 -4.02115e+08 8.19517e+08 -6.47023e+08 6.73287e+08 -2.39221e+08 6.7004e+08 1.25846e+07 7.002e+08 2.14438e+08 6.00856e+08 3.08603e+08 4.40585e+08 3.50236e+08 2.7662e+08 3.57956e+08 1.75108e+08 3.73535e+08 1.47895e+08 4.85433e+08 2.38789e+08 5.9779e+08 4.01393e+08 6.48784e+08 1.78214e+08 8.49256e+08 -5.40786e+08 1.24011e+09 -1.31502e+09 1.54312e+09 -1.81204e+09 1.70313e+09 -2.0993e+09 1.76297e+09 -2.31574e+09 1.75994e+09 -2.47426e+09 1.74998e+09 -2.61181e+09 1.77423e+09 -2.8392e+09 1.84301e+09 -3.23753e+09 1.92258e+09 -3.76265e+09 1.95297e+09 -4.27854e+09 1.90036e+09 -4.55243e+09 1.68276e+09 -4.34294e+09 1.19343e+09 -3.5633e+09 3.94396e+08 -2.46642e+09 -6.18646e+08 -1.57242e+09 -1.29444e+09 -6.0195e+08 -1.74133e+09 2.24024e+08 -3.08355e+09 -1.25631e+09 1.21938e+06 4.8573e+06 -2.15293e+08 1.16061e+07 6.80622e+06 -2.49345e+08 1.60104e+07 7.53268e+06 -2.177e+08 2.46143e+07 6.86213e+06 -1.87126e+08 3.71286e+07 5.62038e+06 -1.52634e+08 5.4178e+07 3.84696e+06 -1.16846e+08 7.68532e+07 1.081e+06 -7.90165e+07 1.06162e+08 -2.67224e+06 -3.94729e+07 1.42258e+08 -7.10489e+06 1.57488e+06 1.84072e+08 -1.10005e+07 4.31434e+07 2.24402e+08 -1.22414e+07 8.1372e+07 2.49189e+08 -8.13649e+06 1.04307e+08 2.67752e+08 -1.00304e+06 9.88309e+07 2.98253e+08 1.15474e+06 1.01378e+08 3.2329e+08 -4.16666e+06 1.30521e+08 3.36818e+08 -4.46991e+06 1.46177e+08 3.39914e+08 -432344 1.42277e+08 3.40732e+08 9.42196e+06 1.27833e+08 3.23651e+08 3.62614e+07 9.632e+07 2.53975e+08 1.02261e+08 4.14403e+07 2.06599e+08 1.55267e+08 -1.09242e+08 3.13459e+08 1.54678e+07 -4.36955e+08 5.22238e+08 -1.30495e+08 -6.20855e+08 6.59996e+08 -9.55629e+07 -5.82103e+08 4.36578e+08 1.5626e+07 2.87312e+08 2.68552e+08 2.16674e+08 5.74664e+08 4.34047e+08 1.21698e+08 4.33601e+08 4.37617e+08 3.84609e+07 5.33341e+08 3.57014e+08 -1.60757e+07 6.34678e+08 2.3753e+08 -5.20806e+07 7.72813e+08 5.89101e+07 -2.23807e+07 9.59962e+08 -1.91382e+08 4.69805e+07 1.16897e+09 -4.33353e+08 3.03194e+07 1.36481e+09 -5.46457e+08 -1.31136e+08 1.50498e+09 -5.69963e+08 -2.84534e+08 1.61338e+09 -6.42792e+08 -2.50799e+08 1.80407e+09 -8.1342e+08 -8.97321e+07 2.00272e+09 -1.06196e+09 6.69293e+07 2.13063e+09 -1.36049e+09 1.535e+08 2.19593e+09 -1.69607e+09 1.66145e+08 2.23921e+09 -2.06431e+09 1.65498e+08 2.27483e+09 -2.45199e+09 1.56813e+08 2.30625e+09 -2.81155e+09 1.22925e+08 2.32045e+09 -3.06349e+09 7.08196e+07 2.25403e+09 -3.10333e+09 2.56198e+07 2.01912e+09 -2.8648e+09 -1.3769e+07 1.63922e+09 -2.40876e+09 -5.93486e+06 1.14222e+09 -1.96053e+09 3.9384e+07 6.04046e+08 -1.89605e+09 2.88619e+07 2.93069e+08 -1.96303e+09 -4.04362e+07 -3.19292e+08 -1.91791e+09 -1.24906e+08 -1.00932e+09 -1.94421e+09 -1.30148e+08 -1.68542e+09 -3.00736e+09 2.53896e+08 -2.92213e+09 -6.57949e+07 -2.85483e+09 4.92656e+07 -2.40356e+08 7.0646e+07 -2.82957e+08 8.57695e+07 -2.59845e+08 1.03584e+08 -2.3301e+08 1.22011e+08 -2.03799e+08 1.3977e+08 -1.73307e+08 1.55573e+08 -1.42461e+08 1.68172e+08 -1.12793e+08 1.76259e+08 -8.60859e+07 1.80075e+08 -6.41347e+07 1.78661e+08 -4.95911e+07 1.75998e+08 -5.01493e+07 1.96916e+08 -6.0446e+07 2.42499e+08 -4.83485e+07 2.81565e+08 -1.90446e+07 3.1278e+08 392256 3.4439e+08 1.58501e+07 3.80176e+08 2.95577e+07 4.32499e+08 3.77041e+07 5.33787e+08 4.03282e+07 6.80193e+08 3.48887e+07 7.33164e+08 1.60235e+07 7.70659e+08 -5.5467e+08 7.53458e+08 -7.05728e+08 5.42353e+08 1.85987e+07 5.7996e+08 2.56453e+08 6.61785e+08 3.21509e+08 6.4473e+08 4.17362e+08 5.70935e+08 4.52427e+08 4.19152e+08 4.98841e+08 2.68171e+08 5.43667e+08 2.32098e+08 6.08099e+08 1.27029e+08 7.59754e+08 -3.94632e+08 1.05401e+09 -1.24418e+09 1.42623e+09 -1.99585e+09 1.75395e+09 -2.49878e+09 1.97112e+09 -2.78226e+09 2.06998e+09 -2.94445e+09 2.09633e+09 -3.12472e+09 2.12443e+09 -3.3535e+09 2.1614e+09 -3.61467e+09 2.2064e+09 -3.88043e+09 2.24161e+09 -4.08989e+09 2.20778e+09 -4.14276e+09 2.02789e+09 -3.92617e+09 1.65586e+09 -3.36463e+09 1.08638e+09 -2.58607e+09 4.04743e+08 -1.98058e+09 -2.51824e+08 -1.7545e+09 -9.65769e+08 -1.69496e+09 -1.56305e+09 -1.54742e+09 -2.10398e+09 -1.17986e+09 -3.25754e+09 -2.42344e+09 -2.49152e+07 5.04691e+06 -2.42468e+08 -1.93439e+07 7.53333e+06 -2.48949e+08 -1.47098e+07 8.75507e+06 -2.16593e+08 -7.62939e+06 8.73017e+06 -1.87077e+08 2.87411e+06 8.13873e+06 -1.53711e+08 1.8233e+07 6.67228e+06 -1.19385e+08 4.00964e+07 4.20292e+06 -8.33274e+07 6.99627e+07 735710 -4.57903e+07 1.09233e+08 -3.65078e+06 -6.63568e+06 1.58326e+08 -7.90506e+06 3.3588e+07 2.16143e+08 -9.81669e+06 7.09686e+07 2.73744e+08 -5.63133e+06 9.79327e+07 3.17068e+08 7.55949e+06 9.8537e+07 3.50022e+08 2.10505e+07 9.29233e+07 3.59134e+08 1.94917e+07 1.37661e+08 3.55692e+08 1.29998e+07 1.67628e+08 3.42589e+08 1.27222e+07 1.72622e+08 3.00184e+08 2.57376e+07 1.74095e+08 2.04891e+08 6.31924e+07 1.60988e+08 2.63663e+07 1.44597e+08 1.15616e+08 -7.73461e+07 1.42846e+08 -1.14277e+08 -4.78497e+07 -5.55978e+07 -3.91248e+08 2.68022e+08 -1.78038e+08 -7.40739e+08 6.03195e+08 -6.4539e+07 -2.80037e+08 -1.74258e+08 -2.50282e+08 -1.13123e+08 3.27121e+08 5.02178e+07 8.31909e+07 2.13536e+08 6.97951e+07 2.01111e+08 3.50881e+08 5.28546e+07 5.78689e+08 1.88057e+08 1.05105e+08 5.55496e+08 6.51384e+07 3.10898e+07 6.72378e+08 -6.37931e+07 460294 7.99832e+08 -2.03582e+08 -1.25144e+07 9.54178e+08 -3.59172e+08 -6.06316e+06 1.14656e+09 -5.08262e+08 -2.75784e+07 1.36915e+09 -5.86251e+08 -1.08866e+08 1.57311e+09 -5.73496e+08 -2.06222e+08 1.71888e+09 -5.86896e+08 -2.00294e+08 1.85037e+09 -7.07151e+08 -6.80192e+07 2.0209e+09 -9.11347e+08 6.72825e+07 2.17365e+09 -1.16319e+09 1.49275e+08 2.27176e+09 -1.42975e+09 1.76148e+08 2.3278e+09 -1.68855e+09 1.7631e+08 2.36522e+09 -1.92535e+09 1.85722e+08 2.37026e+09 -2.11809e+09 2.11631e+08 2.32938e+09 -2.24018e+09 2.3937e+08 2.23974e+09 -2.25854e+09 2.4174e+08 2.05929e+09 -2.09596e+09 2.28047e+08 1.64093e+09 -1.76655e+09 2.30061e+08 1.07501e+09 -1.50376e+09 2.71203e+08 5.7699e+08 -1.62794e+09 3.12224e+08 3.43194e+08 -2.08027e+09 1.94674e+08 4.50704e+08 -2.27859e+09 2.53407e+07 -2.5073e+08 -2.33482e+09 -1.3949e+08 -9.14026e+08 -2.49734e+09 -2.73397e+08 -1.33608e+09 -2.76912e+09 -3.24511e+08 -2.39257e+09 -2.86679e+08 -2.73083e+09 2.98351e+07 -2.69859e+08 4.80838e+07 -2.84222e+08 6.5402e+07 -2.58416e+08 8.46622e+07 -2.3292e+08 1.04385e+08 -2.04024e+08 1.2362e+08 -1.73704e+08 1.414e+08 -1.43052e+08 1.56434e+08 -1.13306e+08 1.66995e+08 -8.5683e+07 1.7218e+08 -6.16742e+07 1.734e+08 -4.38799e+07 1.724e+08 -3.62927e+07 1.80036e+08 -4.11194e+07 2.12171e+08 -4.14273e+07 2.52799e+08 -2.44865e+07 2.89134e+08 -8.56842e+06 3.23445e+08 7.81533e+06 3.63485e+08 2.76778e+07 4.22008e+08 5.37598e+07 5.23881e+08 9.4324e+07 6.07461e+08 1.02477e+08 5.54708e+08 2.25403e+07 6.15769e+08 -7.84577e+08 7.38309e+08 -2.99845e+08 -1.57081e+08 -2.9679e+08 -1.21383e+08 3.2515e+08 7.41153e+07 2.20168e+08 3.58251e+07 2.02193e+08 2.20341e+08 5.10218e+08 3.29634e+08 4.00773e+08 3.54672e+08 4.89677e+08 3.29398e+08 5.2886e+08 3.08207e+08 5.46607e+08 2.62083e+08 6.14315e+08 4.49143e+07 8.21384e+08 -5.17736e+08 1.23125e+09 -1.38897e+09 1.7329e+09 -2.17908e+09 2.03027e+09 -2.59487e+09 2.11963e+09 -2.74794e+09 2.21398e+09 -2.80943e+09 2.30731e+09 -2.88698e+09 2.38029e+09 -3.01641e+09 2.46365e+09 -3.16514e+09 2.53109e+09 -3.27751e+09 2.5659e+09 -3.31914e+09 2.55522e+09 -3.24159e+09 2.39512e+09 -2.94956e+09 1.97735e+09 -2.41744e+09 1.35313e+09 -1.90375e+09 8.25125e+08 -1.80699e+09 5.83163e+08 -1.87708e+09 -3.66613e+07 -1.96948e+09 -9.01044e+08 -2.10235e+09 -1.62106e+09 -2.46665e+09 -2.06232e+09 -3.41637e+09 -2.66544e+09 -2.81016e+09 -5.00141e+07 4.99982e+06 -2.58298e+08 -4.88932e+07 7.48236e+06 -2.44233e+08 -4.47054e+07 8.33217e+06 -2.14917e+08 -4.03665e+07 8.86182e+06 -1.84964e+08 -3.50993e+07 9.54762e+06 -1.51574e+08 -2.68149e+07 9.45977e+06 -1.18078e+08 -1.16845e+07 7.9697e+06 -8.5002e+07 1.36547e+07 5.26946e+06 -5.24414e+07 5.12516e+07 2.01278e+06 -1.91394e+07 1.01456e+08 -795809 1.58089e+07 1.64642e+08 -1.05577e+06 4.66943e+07 2.43944e+08 6.01262e+06 5.75935e+07 3.39246e+08 2.87115e+07 2.77597e+07 4.48183e+08 5.43919e+07 -7.44837e+06 4.95391e+08 5.49148e+07 9.3118e+07 5.04702e+08 5.33408e+07 1.54874e+08 5.04288e+08 6.14987e+07 1.61224e+08 4.2792e+08 7.2311e+07 2.2142e+08 2.03881e+08 1.15271e+08 2.97287e+08 -1.34574e+08 1.87735e+08 2.8269e+08 -3.9917e+08 1.29083e+08 2.21021e+07 -4.75291e+08 -2.11014e+07 -2.96648e+08 -9.36767e+07 -1.25456e+08 -1.27892e+08 -7.36402e+07 -1.71784e+08 -1.64299e+08 -1.47276e+08 -1.29092e+08 -1.82823e+08 -1.45341e+08 -1.41761e+06 -2.2257e+08 7.42565e+07 2.39084e+07 1.31804e+07 -1.94495e+07 -2.63788e+07 5.22723e+06 1.85261e+08 -5.04539e+07 -5.84218e+07 2.04552e+07 2.34869e+07 1.6972e+08 2.01945e+08 1.68123e+08 -1.75319e+08 1.49274e+07 7.33516e+08 -2.78153e+08 -9.38634e+06 8.25823e+08 -3.98797e+08 -7.53464e+06 9.61399e+08 -5.01464e+08 -3.40502e+07 1.11857e+09 -5.63207e+08 -7.63401e+07 1.31028e+09 -5.70243e+08 -1.13538e+08 1.51617e+09 -5.36156e+08 -1.32125e+08 1.70066e+09 -5.24001e+08 -1.10446e+08 1.85066e+09 -6.13575e+08 -3.12015e+07 2.00996e+09 -7.97724e+08 4.93946e+07 2.20134e+09 -1.04603e+09 1.05133e+08 2.36984e+09 -1.34043e+09 1.34448e+08 2.49099e+09 -1.65005e+09 1.37066e+08 2.56542e+09 -1.94219e+09 1.36082e+08 2.59177e+09 -2.18499e+09 1.40232e+08 2.54599e+09 -2.31988e+09 1.33787e+08 2.40297e+09 -2.22097e+09 7.30358e+07 2.13489e+09 -1.88156e+09 -2.7135e+07 1.80011e+09 -1.51469e+09 -5.18281e+07 1.36727e+09 -1.3192e+09 1.25163e+07 8.90048e+08 -1.35904e+09 1.48084e+08 4.73551e+08 -1.75381e+09 3.77525e+08 3.50299e+08 -2.21937e+09 3.98802e+08 4.82521e+08 -2.36182e+09 9.90937e+07 -2.49364e+08 -2.34967e+09 -1.7611e+08 -7.92114e+08 -2.0804e+09 -3.72308e+08 -1.26809e+09 -1.41644e+09 -4.22367e+08 -2.65213e+09 -2.53924e+08 -3.20656e+09 1.05671e+07 -2.88929e+08 2.50763e+07 -2.82027e+08 4.21364e+07 -2.5721e+08 6.00562e+07 -2.31311e+08 7.92779e+07 -2.02211e+08 9.92194e+07 -1.71902e+08 1.18739e+08 -1.41663e+08 1.36514e+08 -1.1236e+08 1.51236e+08 -8.44643e+07 1.61757e+08 -5.84943e+07 1.69396e+08 -3.72285e+07 1.78038e+08 -2.30682e+07 1.98007e+08 -1.70863e+07 2.31762e+08 -6.75459e+06 2.62554e+08 1.25358e+07 2.98236e+08 2.1345e+07 3.48306e+08 3.07548e+07 4.02059e+08 5.64394e+07 4.66915e+08 1.12152e+08 5.5831e+08 1.94075e+08 5.4309e+08 2.47364e+08 1.72673e+08 3.69985e+08 -3.75646e+07 -8.04194e+07 6.92319e+07 -1.07516e+08 6.45929e+06 -2.38831e+08 -7.83725e+07 -2.72509e+08 2.29308e+07 -1.00908e+08 2.09368e+07 -2.79108e+08 -3.78661e+07 3.41728e+07 9.52926e+07 1.80465e+07 -2.75859e+07 4.81082e+08 2.05904e+08 -1.27966e+08 -1.91747e+08 -6.97549e+07 -1.30132e+08 1.47361e+08 1.62747e+08 1.17481e+08 4.42299e+08 5.21376e+08 4.30075e+08 5.51925e+08 3.74795e+08 6.18389e+08 1.93369e+08 7.23273e+08 -1.49801e+08 9.03554e+08 -6.59765e+08 1.23709e+09 -1.30169e+09 1.75867e+09 -1.90329e+09 2.24172e+09 -2.28176e+09 2.39901e+09 -2.4475e+09 2.36324e+09 -2.51668e+09 2.4237e+09 -2.54019e+09 2.50694e+09 -2.55306e+09 2.57828e+09 -2.52899e+09 2.63033e+09 -2.422e+09 2.62515e+09 -2.20883e+09 2.55003e+09 -1.93324e+09 2.41096e+09 -1.6888e+09 2.17438e+09 -1.52943e+09 1.80106e+09 -1.47097e+09 1.32502e+09 -1.69224e+09 1.19716e+09 -2.03509e+09 1.29302e+09 -2.0154e+09 3.27403e+08 -2.10063e+09 -7.35535e+08 -2.29808e+09 -1.61664e+09 -2.32592e+09 -2.41368e+09 -1.88768e+09 -3.52781e+09 -3.64665e+09 -7.4976e+07 4.88213e+06 -2.66307e+08 -7.76693e+07 7.54391e+06 -2.36676e+08 -7.58001e+07 7.81977e+06 -2.11388e+08 -7.3866e+07 7.75048e+06 -1.80433e+08 -6.9643e+07 7.72495e+06 -1.47895e+08 -6.23742e+07 7.73909e+06 -1.15704e+08 -5.23418e+07 7.70932e+06 -8.31919e+07 -4.04349e+07 7.58934e+06 -4.94686e+07 -2.40832e+07 7.01147e+06 -1.57256e+07 2.58526e+06 6.01742e+06 1.54428e+07 4.1056e+07 9.33379e+06 3.78935e+07 9.02294e+07 2.75454e+07 3.05561e+07 1.26871e+08 7.64111e+07 -1.09751e+07 1.3501e+08 1.10718e+08 -1.5528e+06 4.15978e+07 8.70587e+07 2.12648e+08 1.68713e+07 9.20412e+07 1.95387e+08 -1.76986e+07 1.18661e+08 1.86068e+08 2.37057e+07 1.08084e+08 1.44449e+08 1.39562e+08 8.75747e+07 1.19298e+08 2.09967e+08 1.13511e+08 1.07197e+08 1.46727e+08 1.19397e+08 -3.18549e+07 -1.01898e+07 6.82325e+07 -1.62654e+08 -1.67492e+07 4.65827e+06 -3.45588e+07 -8.18093e+06 -3.81717e+07 -9.35538e+07 3.79431e+07 816195 9.04629e+06 -1.18531e+07 -3.53792e+07 4.98877e+07 4.35043e+07 -6.49193e+07 -5.11357e+06 -8.3803e+06 -3.41029e+06 -7.0817e+06 -3.67283e+06 3.17338e+07 -1.08985e+08 1.90779e+07 1.40361e+08 3.7742e+07 3.11335e+07 4.13123e+07 -4.19889e+08 -6.88716e+07 8.04922e+08 1.02657e+08 7.52064e+07 1.4951e+08 1.29696e+08 -4.99427e+08 -5.45153e+07 9.9092e+08 -6.09092e+08 -5.34155e+07 1.15052e+09 -6.97746e+08 -8.76238e+07 1.31788e+09 -7.28837e+08 -1.44952e+08 1.5088e+09 -7.0871e+08 -1.77402e+08 1.69785e+09 -6.87453e+08 -1.60318e+08 1.86741e+09 -7.21855e+08 -1.11236e+08 2.02813e+09 -8.32288e+08 -4.79741e+07 2.20618e+09 -1.00078e+09 8.4967e+06 2.40623e+09 -1.21896e+09 4.79023e+07 2.5922e+09 -1.47364e+09 7.03675e+07 2.7352e+09 -1.73326e+09 7.20456e+07 2.82257e+09 -1.96745e+09 6.3719e+07 2.84172e+09 -2.14164e+09 3.6385e+07 2.7711e+09 -2.20847e+09 -2.38744e+07 2.58286e+09 -2.15991e+09 -9.24638e+07 2.26081e+09 -2.10493e+09 -1.23802e+08 1.94046e+09 -2.13288e+09 -2.34443e+08 1.67792e+09 -2.12255e+09 -3.49674e+08 1.26313e+09 -1.87547e+09 -3.65229e+08 6.17505e+08 -1.69083e+09 -1.40796e+08 3.3145e+08 -1.63455e+09 9.92895e+07 3.61745e+08 -1.80471e+09 2.66677e+07 -7.56056e+07 -1.83273e+09 -1.24311e+08 -6.07063e+08 -1.30365e+09 -2.69527e+08 -1.48959e+09 -4.12282e+08 -3.95688e+08 -3.12226e+09 -2.66486e+08 -3.67658e+09 -7.56496e+06 -2.99878e+08 3.84125e+06 -2.77089e+08 1.91155e+07 -2.552e+08 3.31137e+07 -2.27293e+08 4.79571e+07 -1.97991e+08 6.54514e+07 -1.69241e+08 8.53245e+07 -1.40565e+08 1.05706e+08 -1.11301e+08 1.25601e+08 -8.3045e+07 1.43558e+08 -5.58886e+07 1.6096e+08 -3.10109e+07 1.87858e+08 -8.65882e+06 2.4444e+08 1.61491e+07 3.0757e+08 5.41014e+07 3.44936e+08 7.58079e+07 3.96112e+08 7.60907e+07 4.71434e+08 8.81812e+07 5.32266e+08 1.16906e+08 5.84657e+08 1.57648e+08 6.71001e+08 2.27628e+08 7.03753e+08 3.36385e+08 1.39028e+08 6.21305e+08 1.39664e+08 1.4066e+08 1.0541e+08 4.26446e+07 4.0757e+07 -2.71264e+07 -372311 3.20169e+06 7.46418e+07 1.2809e+07 -1.79915e+07 -5.04347e+06 -6.89872e+07 -1.51403e+07 8.22548e+07 6.1682e+07 -9.04265e+07 -7.83356e+06 -9.93072e+06 -7.41785e+06 -9.29924e+06 -8.37674e+06 -379307 -7.90476e+06 1.08644e+08 -289384 1.30832e+08 -1.31382e+07 6.05419e+06 -6.00357e+06 6.6071e+08 3.66574e+08 -1.27343e+08 -1.82152e+08 -6.23223e+07 -1.32704e+08 6.74912e+08 5.26791e+08 5.65259e+08 7.07648e+08 2.58259e+08 9.69075e+08 -2.58165e+08 1.29813e+09 -8.65557e+08 1.68946e+09 -1.38533e+09 2.14178e+09 -1.68057e+09 2.45257e+09 -1.79766e+09 2.50087e+09 -1.87325e+09 2.48768e+09 -1.92932e+09 2.57516e+09 -1.95221e+09 2.65563e+09 -1.93185e+09 2.69578e+09 -1.8483e+09 2.68945e+09 -1.73388e+09 2.63843e+09 -1.68233e+09 2.574e+09 -1.75036e+09 2.49278e+09 -1.89968e+09 2.30052e+09 -1.91249e+09 1.65141e+09 -1.92971e+09 1.0378e+09 -2.08508e+09 1.02383e+09 -2.20151e+09 1.30398e+09 -1.93024e+09 1.87461e+08 -1.61413e+09 -9.9412e+08 -1.23633e+09 -2.08986e+09 -7.1438e+08 -3.18173e+09 -3.4195e+08 -4.27968e+09 -4.21111e+09 -1.00293e+08 5.10493e+06 -2.67107e+08 -1.06286e+08 8.02619e+06 -2.26656e+08 -1.0759e+08 8.17651e+06 -2.0571e+08 -1.08572e+08 7.75415e+06 -1.74001e+08 -1.06546e+08 6.50684e+06 -1.42291e+08 -1.0031e+08 4.27398e+06 -1.10859e+08 -8.85395e+07 1.95557e+06 -8.0081e+07 -6.98271e+07 263835 -4.98358e+07 -4.46684e+07 -1.39163e+06 -1.85306e+07 -2.03618e+07 -1.74613e+06 1.70717e+07 -2.0227e+07 6.95035e+06 6.01795e+07 -1.04727e+08 4.20495e+07 1.1712e+08 -3.41132e+08 1.199e+08 2.02317e+08 -3.22889e+08 8.42814e+07 5.83234e+07 -2.07195e+07 -9.79124e+06 8.46286e+06 1.53673e+08 2.134e+07 5.66287e+07 1.94064e+08 5.31517e+07 1.29746e+08 1.00884e+08 8.59565e+07 1.75614e+08 -5.29005e+07 1.11837e+08 1.77582e+08 -2.16207e+08 1.28186e+08 1.544e+08 -4.9826e+08 1.51326e+08 1.08127e+08 -1.91863e+08 -7.83058e+06 1.4064e+08 -1.79106e+08 -2.01732e+08 -2.07494e+08 8.05958e+07 -1.52008e+08 -3.73356e+07 -8.17801e+07 3.76311e+07 1.9395e+07 -4.02829e+06 -3.30543e+07 -1.98523e+06 -5.88603e+06 -7.10597e+06 -4.90588e+06 3.00838e+07 -1.09145e+08 -2.20125e+08 3.27565e+07 1.4283e+08 3.65754e+07 4.19142e+07 5.10224e+07 -8.04545e+08 -7.81914e+07 8.28602e+08 2.25403e+08 1.95889e+08 2.52011e+08 2.15387e+08 -7.91192e+08 -7.79468e+07 1.0775e+09 -8.21232e+08 -8.42322e+07 1.28612e+09 -8.65333e+08 -1.03378e+08 1.48794e+09 -8.78812e+08 -1.43716e+08 1.69154e+09 -8.61121e+08 -1.80386e+08 1.89065e+09 -8.54807e+08 -1.8255e+08 2.07961e+09 -9.00478e+08 -1.53352e+08 2.27014e+09 -9.99147e+08 -1.16322e+08 2.47207e+09 -1.13387e+09 -7.15376e+07 2.67279e+09 -1.28729e+09 -2.86889e+07 2.84582e+09 -1.43379e+09 2.06713e+06 2.96583e+09 -1.54066e+09 2.15488e+07 3.0119e+09 -1.5833e+09 4.22818e+07 2.96766e+09 -1.56429e+09 6.39493e+07 2.83315e+09 -1.5406e+09 8.3332e+07 2.63047e+09 -1.6276e+09 6.56842e+07 2.43567e+09 -1.85795e+09 -4.7392e+07 2.35003e+09 -2.05227e+09 -1.99461e+08 2.16132e+09 -1.96596e+09 -3.37084e+08 1.57504e+09 -1.65815e+09 -4.38812e+08 8.12215e+08 -1.74749e+09 -2.64938e+08 7.78393e+08 -1.91763e+09 -9.42349e+07 7.31623e+08 -2.05667e+09 -1.62764e+08 3.27142e+08 -1.87499e+09 -2.09875e+08 -4.90598e+08 -1.43589e+09 -2.12195e+08 -1.65091e+09 -1.04792e+09 -2.26999e+08 -3.24829e+09 -1.55241e+08 -3.83497e+09 -2.55892e+07 -3.03852e+08 -1.6161e+07 -2.70064e+08 -2.367e+06 -2.51982e+08 1.01735e+07 -2.22639e+08 2.18765e+07 -1.92864e+08 3.18171e+07 -1.63405e+08 4.25339e+07 -1.365e+08 5.84902e+07 -1.13098e+08 7.87697e+07 -9.01562e+07 9.89321e+07 -6.32342e+07 1.22062e+08 -3.31314e+07 1.68229e+08 1.12716e+06 2.76469e+08 4.19527e+07 3.63383e+08 6.72505e+07 3.71894e+08 7.47618e+07 3.84357e+08 1.02753e+08 4.09345e+08 1.35054e+08 4.54926e+08 1.75081e+08 5.26342e+08 2.12728e+08 6.19214e+08 2.73118e+08 7.19221e+08 3.99871e+08 1.17654e+08 8.36011e+08 7.42119e+07 1.76168e+08 1.4392e+08 -6.54064e+06 9.0849e+07 2.79175e+07 -3.92685e+07 5.18146e+07 6.42511e+07 9.32762e+06 -1.8245e+06 -1.44415e+07 -6.86763e+07 -1.5468e+06 8.12325e+06 7.52264e+06 -1.07992e+06 2.39749e+07 -4.58896e+07 -3.14339e+07 -2.7514e+07 5.37527e+06 2.48054e+07 1.41598e+07 3.03082e+07 1.89527e+07 5.52303e+08 1.60832e+08 -3.91181e+07 -1.24175e+08 -6.83992e+07 -1.46562e+08 5.27037e+08 4.99465e+08 3.84162e+08 7.88688e+08 1.45812e+08 1.12584e+09 -1.93682e+08 1.51683e+09 -5.80782e+08 1.92212e+09 -8.78958e+08 2.28805e+09 -1.03792e+09 2.4943e+09 -1.14565e+09 2.53492e+09 -1.27267e+09 2.59301e+09 -1.37471e+09 2.70655e+09 -1.43516e+09 2.78689e+09 -1.44288e+09 2.80812e+09 -1.38335e+09 2.77284e+09 -1.27506e+09 2.71408e+09 -1.16751e+09 2.69146e+09 -1.18483e+09 2.73632e+09 -1.59178e+09 2.80721e+09 -2.19511e+09 2.14614e+09 -2.51654e+09 1.07318e+09 -2.42313e+09 5.3658e+08 -2.04279e+09 7.07818e+08 -1.37203e+09 -5.24494e+08 -9.30601e+08 -1.54748e+09 -5.75482e+08 -2.61172e+09 -2.45862e+08 -3.68967e+09 -2.94693e+07 -4.70439e+09 -4.69272e+09 -1.2625e+08 5.32061e+06 -2.62279e+08 -1.34133e+08 8.4755e+06 -2.16027e+08 -1.38095e+08 8.50086e+06 -1.98751e+08 -1.41906e+08 8.02607e+06 -1.66181e+08 -1.4321e+08 6.57508e+06 -1.3485e+08 -1.40143e+08 3.75428e+06 -1.04165e+08 -1.30353e+08 -571393 -7.4772e+07 -1.10266e+08 -6.97218e+06 -4.71267e+07 -7.57477e+07 -1.44354e+07 -2.13141e+07 -2.42991e+07 -1.91505e+07 3.37025e+06 4.338e+07 -1.7611e+07 3.03824e+07 1.2654e+08 -5.23317e+06 6.31141e+07 2.67894e+08 5.21399e+06 8.67871e+07 2.85993e+08 -1.53676e+07 9.03103e+07 1.88502e+08 1.37957e+07 1.22739e+08 6.42827e+07 5.93066e+07 1.48289e+08 -4.25751e+07 8.51834e+07 1.75604e+08 -1.46373e+08 9.74143e+07 2.03234e+08 -2.68533e+08 9.63956e+07 2.2199e+08 -4.36792e+08 1.0092e+08 2.37921e+08 -6.85252e+08 1.10035e+08 2.64511e+08 -8.90338e+08 2.19566e+07 3.41331e+08 -2.53871e+08 -2.31726e+08 3.72193e+08 8.44397e+06 7.43447e+07 -1.66717e+07 4.66928e+07 -2.9069e+08 -2.87249e+08 -2.84316e+08 1.14071e+07 -3.19019e+08 -4.75521e+07 3.34283e+07 2.85283e+07 9.647e+06 3.68398e+07 -1.0576e+07 3.99915e+08 -3.90027e+08 -2.11963e+07 8.6187e+07 -1.72964e+07 -1.33147e+07 -1.92518e+07 -1.33679e+09 -1.67661e+08 6.81691e+08 -1.10255e+08 -1.40978e+08 -1.194e+08 -1.50065e+08 3.94966e+08 3.62454e+08 3.43095e+08 2.88482e+08 -1.28723e+09 -1.13038e+08 9.10325e+08 -1.24463e+09 -1.0084e+08 1.15108e+09 -1.21133e+09 -1.112e+08 1.37935e+09 -1.176e+09 -1.36915e+08 1.60658e+09 -1.12533e+09 -1.70328e+08 1.8312e+09 -1.06493e+09 -1.93962e+08 2.04844e+09 -1.02574e+09 -1.92652e+08 2.26273e+09 -1.033e+09 -1.63795e+08 2.47743e+09 -1.07972e+09 -1.24675e+08 2.68793e+09 -1.14382e+09 -8.86713e+07 2.8806e+09 -1.20718e+09 -5.55553e+07 3.03345e+09 -1.24795e+09 -2.74522e+07 3.12046e+09 -1.23681e+09 1.10554e+06 3.11212e+09 -1.14894e+09 4.13866e+07 2.98067e+09 -1.00983e+09 8.52761e+07 2.77598e+09 -9.15407e+08 1.23195e+08 2.59585e+09 -9.98127e+08 1.19014e+08 2.57421e+09 -1.22591e+09 4.1291e+07 2.67998e+09 -1.37039e+09 -1.27305e+08 2.53566e+09 -1.33284e+09 -2.12875e+08 1.81908e+09 -1.30717e+09 -2.09814e+08 1.07083e+09 -1.62643e+09 1.51926e+08 1.06074e+09 -1.92481e+09 2.82228e+08 8.72061e+08 -2.02554e+09 2.69067e+07 5.22647e+08 -1.79747e+09 -1.50089e+08 -4.59306e+08 -1.42364e+09 -1.87297e+08 -1.75271e+09 -1.03994e+09 -1.92908e+08 -3.39207e+09 -1.31301e+08 -3.91134e+09 -4.40324e+07 -3.0254e+08 -3.50444e+07 -2.62681e+08 -2.21043e+07 -2.48188e+08 -1.00984e+07 -2.17907e+08 714938 -1.87766e+08 8.78036e+06 -1.57524e+08 1.13681e+07 -1.2829e+08 5.73356e+06 -1.01465e+08 -2.5037e+06 -8.1655e+07 1.09021e+06 -7.0392e+07 2.53438e+07 -5.91857e+07 7.50036e+07 -3.72445e+07 1.46412e+08 -5.17139e+06 1.94427e+08 2.66786e+07 2.07325e+08 1.00829e+08 2.2741e+08 1.66984e+08 2.87121e+08 1.85986e+08 3.83469e+08 1.99172e+08 4.91082e+08 2.20518e+08 6.20729e+08 2.61056e+08 8.12146e+08 3.32176e+08 1.20979e+09 4.88868e+08 1.34601e+08 4.08388e+08 -1.55472e+08 -5.57913e+07 -1.31753e+08 2.07746e+07 1.50951e+08 2.54879e+08 3.8566e+08 -464552 1.19401e+08 6.28298e+07 -6.56923e+07 -4.30875e+07 2.09219e+07 1.39451e+07 4.06531e+07 5.49687e+06 8.03777e+07 1.12691e+08 -7.51406e+07 -3.79988e+07 -6.58371e+07 6.64938e+06 -4.06359e+06 6.27284e+06 2.57073e+08 -3.72468e+08 5.57606e+07 2.27764e+07 6.4874e+07 2.13247e+07 7.61847e+07 -4.73775e+07 -2.96976e+07 -1.37407e+08 2.73555e+08 5.75955e+07 2.07501e+08 4.86867e+08 6.43058e+07 8.41662e+08 -1.11617e+08 1.1863e+09 -2.94833e+08 1.55262e+09 -4.81044e+08 1.94005e+09 -6.33218e+08 2.27721e+09 -7.56055e+08 2.48788e+09 -8.79617e+08 2.57445e+09 -9.95037e+08 2.6678e+09 -1.07506e+09 2.7883e+09 -1.11609e+09 2.86979e+09 -1.11111e+09 2.89086e+09 -1.06484e+09 2.87829e+09 -1.03324e+09 2.90536e+09 -1.10117e+09 3.04624e+09 -1.23159e+09 3.16222e+09 -1.23516e+09 3.01487e+09 -1.52921e+09 2.43897e+09 -2.06479e+09 1.46478e+09 -2.1567e+09 4.73677e+08 -1.65207e+09 4.11212e+08 -6.68151e+08 -1.17346e+09 -9.69341e+07 -2.04022e+09 1.46116e+08 -2.96011e+09 2.45481e+08 -3.94149e+09 2.64375e+08 -4.89556e+09 -4.96333e+09 -1.52148e+08 5.46662e+06 -2.53052e+08 -1.60422e+08 8.79579e+06 -2.05914e+08 -1.6732e+08 8.89131e+06 -1.90011e+08 -1.74603e+08 8.76366e+06 -1.56577e+08 -1.80427e+08 7.72104e+06 -1.25237e+08 -1.83193e+08 5.4678e+06 -9.48642e+07 -1.81202e+08 1.81717e+06 -6.60493e+07 -1.72715e+08 -3.33183e+06 -3.91511e+07 -1.5652e+08 -9.73881e+06 -1.39158e+07 -1.31598e+08 -1.77671e+07 1.10048e+07 -9.82726e+07 -2.459e+07 3.72778e+07 -5.96498e+07 -2.16925e+07 6.35493e+07 -5.88664e+06 -2.51439e+07 8.00853e+07 2.05184e+07 -2.36512e+07 1.12385e+08 -2.93863e+07 3.52581e+07 1.62334e+08 -1.23173e+08 7.43337e+07 1.91819e+08 -2.11658e+08 7.60828e+07 2.11782e+08 -2.91499e+08 7.17849e+07 2.34506e+08 -3.79436e+08 6.95164e+07 2.62622e+08 -5.00625e+08 7.85489e+07 3.02571e+08 -6.70593e+08 9.30279e+07 3.63595e+08 -8.25494e+08 6.73409e+07 4.57614e+08 -9.5201e+08 -3.81702e+07 5.54241e+08 -1.08167e+09 -9.24346e+07 6.55372e+08 -1.10452e+08 -8.34542e+07 -1.23397e+08 -1.01013e+08 -1.1629e+09 -1.20278e+08 7.80718e+08 -1.16296e+08 -1.3028e+08 -1.43748e+08 -1.52885e+08 -1.24183e+09 -1.37855e+08 9.12271e+08 -1.2802e+09 -1.2719e+08 1.09127e+09 -1.30168e+09 -1.22488e+08 1.30951e+09 -1.29885e+09 -1.48186e+08 1.53987e+09 -1.26253e+09 -1.90176e+08 1.77791e+09 -1.19399e+09 -2.20463e+08 2.00406e+09 -1.10177e+09 -2.33265e+08 2.21505e+09 -1.01402e+09 -2.18636e+08 2.42497e+09 -9.55103e+08 -1.77832e+08 2.63491e+09 -9.21135e+08 -1.33343e+08 2.83557e+09 -8.92496e+08 -1.00746e+08 3.01388e+09 -8.5742e+08 -7.0563e+07 3.14794e+09 -8.00956e+08 -3.67835e+07 3.20754e+09 -6.86679e+08 7.03773e+06 3.13438e+09 -3.81792e+08 7.15944e+07 2.80221e+09 4.38748e+07 1.12163e+08 2.4983e+09 2.28262e+08 1.40182e+08 2.54814e+09 4.56583e+07 1.73153e+08 2.82704e+09 -2.75042e+08 1.10716e+08 3.08298e+09 -5.38859e+08 -1.39936e+08 3.08403e+09 -8.61352e+08 -2.54637e+08 2.49429e+09 -1.02952e+09 -1.26157e+08 1.46034e+09 -1.29594e+09 4.37254e+08 1.01332e+09 -1.60436e+09 6.53974e+08 6.47545e+08 -1.67189e+09 2.65678e+08 4.47913e+08 -1.76563e+09 3.37104e+07 -2.85753e+08 -1.61582e+09 -5.0588e+07 -1.76713e+09 -1.3155e+09 -1.09326e+08 -3.53875e+09 -5.75828e+07 -3.90309e+09 -6.23492e+07 -2.96567e+08 -5.25544e+07 -2.56148e+08 -4.07371e+07 -2.43496e+08 -2.93799e+07 -2.12537e+08 -1.88278e+07 -1.82181e+08 -1.0418e+07 -1.51489e+08 -5.93845e+06 -1.21258e+08 -7.75505e+06 -9.2354e+07 -2.14555e+07 -6.58677e+07 -5.38798e+07 -4.22581e+07 -9.32434e+07 -2.82545e+07 -9.69882e+07 -3.45071e+07 -4.08175e+07 -6.00066e+07 6.2142e+07 -5.75479e+07 1.40914e+08 1.00849e+08 2.16032e+08 1.97319e+08 3.02393e+08 2.04176e+08 3.94237e+08 2.05537e+08 4.97751e+08 2.10304e+08 6.28249e+08 2.30169e+08 8.071e+08 2.71967e+08 1.06409e+09 3.17468e+08 1.21791e+09 2.40368e+08 9.95651e+08 -1.08959e+08 -2.47573e+07 6.79791e+07 4.42262e+07 2.07148e+08 8.77021e+08 -3.07419e+08 4.93327e+07 6.72963e+07 1.65605e+08 7.66969e+07 6.67366e+08 -2.65454e+08 5.53232e+08 7.29741e+07 4.41821e+08 5.00309e+08 2.83035e+08 8.74521e+08 8.30674e+07 1.21868e+09 -1.26443e+08 1.56608e+09 -3.01566e+08 1.90919e+09 -4.32552e+08 2.22e+09 -5.36822e+08 2.44878e+09 -6.34708e+08 2.57892e+09 -7.15395e+08 2.69489e+09 -7.60212e+08 2.81925e+09 -7.64056e+08 2.90842e+09 -7.19369e+08 2.94812e+09 -6.20647e+08 2.97483e+09 -5.00991e+08 3.05161e+09 -3.6426e+08 3.23004e+09 -1.5063e+08 3.31712e+09 8.65772e+07 3.07876e+09 -1.33643e+08 2.6575e+09 -9.90916e+08 2.14896e+09 -1.48525e+09 9.03141e+08 -1.108e+09 5.21278e+08 -3.048e+08 -1.27712e+09 2.16743e+08 -2.25021e+09 3.72718e+08 -3.03982e+09 4.63537e+08 -4.04743e+09 5.35332e+08 -5.05632e+09 -5.00526e+09 -1.77047e+08 5.7195e+06 -2.40697e+08 -1.86106e+08 8.99579e+06 -1.96034e+08 -1.96475e+08 9.36936e+06 -1.79301e+08 -2.07716e+08 9.72663e+06 -1.45116e+08 -2.18563e+08 9.18201e+06 -1.13316e+08 -2.27986e+08 7.59118e+06 -8.24204e+07 -2.3482e+08 4.86999e+06 -5.30768e+07 -2.38071e+08 1.14552e+06 -2.56099e+07 -2.37251e+08 -3.19436e+06 363925 -2.32285e+08 -7.74532e+06 2.61167e+07 -2.2333e+08 -1.24413e+07 5.3574e+07 -2.07447e+08 -2.20092e+07 8.44413e+07 -1.7283e+08 -5.33131e+07 1.1584e+08 -1.43243e+08 -4.48011e+07 1.56633e+08 -1.77732e+08 3.81345e+07 1.88556e+08 -2.45868e+08 6.47048e+07 2.17426e+08 -3.15321e+08 5.62467e+07 2.46644e+08 -3.91232e+08 5.36445e+07 2.78562e+08 -4.85305e+08 5.77381e+07 3.20306e+08 -6.08229e+08 7.11361e+07 3.75463e+08 -7.54065e+08 8.87554e+07 4.41577e+08 -8.90196e+08 8.37972e+07 5.24749e+08 -9.78421e+08 3.47851e+07 6.32679e+08 -1.03262e+09 -2.30573e+07 7.51216e+08 -1.07772e+09 -6.00354e+07 8.86374e+08 -1.13235e+09 -6.22701e+07 1.04313e+09 -1.18092e+09 -7.8382e+07 1.22902e+09 -1.19819e+09 -1.08856e+08 1.44641e+09 -1.18806e+09 -1.32849e+08 1.67366e+09 -1.13905e+09 -1.61295e+08 1.90266e+09 -1.05205e+09 -1.89661e+08 2.12146e+09 -9.3655e+08 -2.1363e+08 2.33099e+09 -8.13648e+08 -2.14492e+08 2.5383e+09 -7.01664e+08 -1.89226e+08 2.73965e+09 -6.01842e+08 -1.52882e+08 2.92466e+09 -4.98651e+08 -1.20924e+08 3.08018e+09 -3.7888e+08 -9.05096e+07 3.18621e+09 -2.29048e+08 -5.02513e+07 3.20133e+09 6.94409e+06 1.77348e+07 3.02272e+09 4.77288e+08 1.49378e+08 2.39329e+09 8.67992e+08 2.47835e+08 2.08616e+09 8.82116e+08 2.60804e+08 2.46849e+09 7.23893e+08 2.72075e+08 2.88768e+09 5.30339e+08 2.21176e+08 3.20222e+09 1.6772e+08 1.35246e+08 3.4196e+09 -4.48291e+08 -2.64216e+07 3.21551e+09 -8.09095e+08 4.69874e+07 1.85526e+09 -1.1748e+09 5.13088e+08 9.65099e+08 -1.45765e+09 7.1603e+08 3.16401e+08 -1.71928e+09 3.24109e+08 4.98374e+08 -1.8445e+09 1.7338e+08 -2.08836e+08 -1.5109e+09 8.71531e+07 -2.08739e+09 -1.26745e+09 -2.95252e+06 -3.72375e+09 1.84477e+06 -3.8126e+09 -8.00963e+07 -2.88168e+08 -6.97034e+07 -2.50716e+08 -5.93817e+07 -2.37524e+08 -4.89451e+07 -2.06088e+08 -3.90402e+07 -1.75447e+08 -3.08954e+07 -1.44203e+08 -2.58623e+07 -1.13092e+08 -2.52763e+07 -8.2838e+07 -3.06701e+07 -5.38667e+07 -4.35782e+07 -2.59979e+07 -7.18638e+07 1.13096e+06 -1.39297e+08 2.88451e+07 -2.63824e+08 4.82656e+07 -2.54243e+08 -4.60538e+07 -4.02591e+07 -1.25706e+07 1.55488e+08 1.07578e+08 2.84262e+08 1.67342e+08 3.92317e+08 1.82787e+08 5.01626e+08 1.87442e+08 6.28952e+08 2.00943e+08 7.88321e+08 2.22167e+08 9.97232e+08 2.3061e+08 1.1496e+09 1.48834e+08 1.10964e+09 -8.3929e+07 1.00661e+09 -2.31557e+08 8.71466e+08 -1.78801e+08 7.89912e+08 9.53511e+07 7.30546e+08 4.69983e+08 6.78746e+08 8.10263e+08 5.99684e+08 1.15231e+09 4.50976e+08 1.54126e+09 2.7181e+08 1.89231e+09 1.08262e+08 2.18884e+09 -3.41366e+07 2.42488e+09 -1.66292e+08 2.58547e+09 -2.76749e+08 2.71723e+09 -3.55797e+08 2.8482e+09 -4.08745e+08 2.96218e+09 -4.20835e+08 3.04259e+09 -3.12818e+08 3.10001e+09 -4.18515e+07 3.1398e+09 4.16224e+08 3.17889e+09 9.83444e+08 3.19168e+09 1.29962e+09 3.1385e+09 1.23916e+09 2.96539e+09 7.8402e+08 2.67763e+09 -1.16798e+08 1.93279e+09 -4.07839e+08 1.37722e+09 -6.82634e+07 -8.53735e+08 3.17701e+08 -2.26004e+09 5.25817e+08 -3.02318e+09 8.17102e+08 -4.20951e+09 9.57104e+08 -5.1749e+09 -4.88002e+09 -2.01106e+08 5.61711e+06 -2.26782e+08 -2.11702e+08 8.97471e+06 -1.85486e+08 -2.2471e+08 9.66888e+06 -1.67196e+08 -2.39255e+08 1.04681e+07 -1.3224e+08 -2.54709e+08 1.04566e+07 -9.94243e+07 -2.70343e+08 9.5324e+06 -6.72539e+07 -2.85181e+08 7.58328e+06 -3.64697e+07 -2.98211e+08 4.7308e+06 -7.5211e+06 -3.0858e+08 1.35308e+06 1.97978e+07 -3.15651e+08 -2.06511e+06 4.64664e+07 -3.18555e+08 -5.38677e+06 7.40765e+07 -3.14979e+08 -1.065e+07 1.04108e+08 -2.96588e+08 -2.33035e+07 1.36778e+08 -2.83832e+08 -1.80385e+07 1.81228e+08 -3.03446e+08 1.08374e+07 2.20434e+08 -3.47063e+08 2.93212e+07 2.54328e+08 -4.00981e+08 3.27757e+07 2.90101e+08 -4.67655e+08 3.59342e+07 3.30813e+08 -5.52186e+08 4.53181e+07 3.80566e+08 -6.56411e+08 6.11152e+07 4.38346e+08 -7.7374e+08 7.91347e+07 5.00358e+08 -8.86154e+08 8.92315e+07 5.75487e+08 -9.77852e+08 6.2086e+07 6.75097e+08 -1.0337e+09 1.52588e+07 8.0016e+08 -1.0574e+09 -1.40733e+07 9.43204e+08 -1.08735e+09 -2.97404e+07 1.10557e+09 -1.11406e+09 -3.64198e+07 1.29855e+09 -1.10652e+09 -6.429e+07 1.51145e+09 -1.06732e+09 -9.57911e+07 1.74019e+09 -9.99326e+08 -1.31411e+08 1.97769e+09 -8.86875e+08 -1.87794e+08 2.20963e+09 -7.36333e+08 -2.34869e+08 2.42926e+09 -5.69927e+08 -2.5221e+08 2.64019e+09 -4.04493e+08 -2.42985e+08 2.83756e+09 -2.42867e+08 -2.248e+08 3.01612e+09 -7.17173e+07 -2.13277e+08 3.16316e+09 1.21189e+08 -1.95512e+08 3.24601e+09 3.47249e+08 -1.49395e+08 3.20737e+09 6.31775e+08 -6.90555e+07 2.92994e+09 1.02631e+09 7.94252e+07 2.10252e+09 1.54609e+09 2.41406e+08 1.56089e+09 1.98001e+09 3.23837e+08 1.93233e+09 2.18497e+09 3.81577e+08 2.47996e+09 2.22281e+09 4.70792e+08 2.84565e+09 1.79646e+09 6.43279e+08 3.31918e+09 9.27295e+08 7.73997e+08 3.42086e+09 4.85344e+08 3.4235e+08 2.07486e+09 -2.97652e+08 5.0894e+08 1.36865e+09 -9.19251e+08 6.83197e+08 3.66915e+08 -1.26175e+09 3.89909e+08 5.89755e+08 -1.02298e+09 2.95313e+08 -6.00067e+08 -7.62014e+08 2.41502e+08 -2.48524e+09 -7.9346e+08 1.05951e+08 -3.7415e+09 6.08655e+07 -3.64134e+09 -9.72898e+07 -2.77635e+08 -8.76094e+07 -2.45013e+08 -7.84952e+07 -2.30726e+08 -6.86553e+07 -1.99138e+08 -5.91991e+07 -1.67957e+08 -5.13088e+07 -1.35803e+08 -4.61505e+07 -1.03501e+08 -4.45504e+07 -7.19297e+07 -4.66411e+07 -4.17323e+07 -5.14036e+07 -1.32776e+07 -5.69966e+07 1.30575e+07 -5.8872e+07 3.59622e+07 -5.51826e+07 4.50748e+07 -6.16535e+07 -1.71592e+07 -1.61777e+07 703250 1.06936e+08 5.75785e+07 2.42577e+08 1.0309e+08 3.6009e+08 1.34827e+08 4.71582e+08 1.50668e+08 5.95685e+08 1.64541e+08 7.52781e+08 1.79005e+08 9.3223e+08 1.5574e+08 1.08863e+09 7.97025e+07 1.12007e+09 -6.60261e+07 1.05566e+09 -1.81254e+08 9.68924e+08 -1.04666e+08 8.96003e+08 1.44175e+08 8.90068e+08 4.24913e+08 9.12185e+08 7.06614e+08 9.12968e+08 1.03432e+09 8.70438e+08 1.41063e+09 7.82776e+08 1.76045e+09 6.87893e+08 2.04807e+09 6.1021e+08 2.27854e+09 5.27761e+08 2.4656e+09 4.08903e+08 2.64982e+09 2.65211e+08 2.82849e+09 1.4583e+08 2.97017e+09 9.25744e+07 3.07366e+09 1.64118e+08 3.16948e+09 3.91133e+08 3.23564e+09 7.30519e+08 3.26e+09 1.03775e+09 3.36747e+09 1.12067e+09 3.61724e+09 1.20676e+09 3.59771e+09 1.59084e+09 3.15534e+09 1.22121e+09 2.74966e+09 1.48031e+08 3.02493e+09 1.84554e+08 -1.52677e+08 5.12114e+08 -2.13875e+09 1.02585e+09 -3.18586e+09 1.55394e+09 -4.45279e+09 1.64319e+09 -5.13317e+09 -4.55597e+09 -2.24656e+08 5.69419e+06 -2.11111e+08 -2.36472e+08 8.88546e+06 -1.74316e+08 -2.51068e+08 9.65435e+06 -1.54397e+08 -2.67809e+08 1.08663e+07 -1.18595e+08 -2.86532e+08 1.14213e+07 -8.43518e+07 -3.06917e+08 1.12099e+07 -5.02284e+07 -3.28265e+08 1.00268e+07 -1.70878e+07 -3.49601e+08 7.87957e+06 1.43963e+07 -3.69787e+08 5.02e+06 4.40853e+07 -3.87653e+08 1.83527e+06 7.24722e+07 -4.01658e+08 -1.49796e+06 1.00685e+08 -4.08417e+08 -6.25556e+06 1.29807e+08 -4.0345e+08 -1.69086e+07 1.61045e+08 -4.02472e+08 -1.23981e+07 2.07196e+08 -4.25375e+08 9.32845e+06 2.48955e+08 -4.59025e+08 1.4287e+07 2.89887e+08 -5.00966e+08 1.73212e+07 3.328e+08 -5.55447e+08 2.43176e+07 3.79158e+08 -6.23142e+08 3.55445e+07 4.2982e+08 -6.99496e+08 5.12424e+07 4.80576e+08 -7.84085e+08 7.03336e+07 5.29968e+08 -8.77805e+08 8.35946e+07 5.95597e+08 -9.58484e+08 8.44961e+07 6.938e+08 -1.01042e+09 5.82228e+07 8.12261e+08 -1.03965e+09 2.16484e+07 9.52468e+08 -1.05267e+09 953836 1.12931e+09 -1.05528e+09 -1.80969e+07 1.32533e+09 -1.02857e+09 -3.97269e+07 1.53049e+09 -9.62976e+08 -7.52544e+07 1.7575e+09 -8.65746e+08 -1.18552e+08 2.00823e+09 -7.31422e+08 -1.75103e+08 2.26135e+09 -5.55959e+08 -2.32068e+08 2.49834e+09 -3.5014e+08 -2.75455e+08 2.72398e+09 -1.30176e+08 -2.99532e+08 2.93597e+09 9.76764e+07 -3.0734e+08 3.12262e+09 3.37634e+08 -3.0579e+08 3.26436e+09 5.98975e+08 -2.94719e+08 3.32283e+09 8.95447e+08 -2.83148e+08 3.24804e+09 1.20466e+09 -2.71301e+08 2.97287e+09 1.45259e+09 -2.28045e+08 2.21437e+09 2.01223e+09 -1.01849e+08 1.248e+09 2.97178e+09 3.49646e+07 1.05167e+09 3.93782e+09 8.24525e+07 1.56587e+09 4.67014e+09 1.63939e+08 2.0832e+09 4.49721e+09 5.77574e+08 3.03555e+09 3.4815e+09 1.19473e+09 3.36007e+09 2.66954e+09 7.08352e+08 2.3314e+09 1.01252e+09 7.25511e+08 2.463e+09 -1.48505e+08 7.6585e+08 8.89762e+08 -4.65471e+08 6.03877e+08 4.72495e+08 -7.18724e+07 4.05323e+08 -1.23688e+09 1.74522e+08 2.82273e+08 -2.90271e+09 8.44736e+07 1.14513e+08 -3.70276e+09 1.1134e+08 -3.36103e+09 -1.15083e+08 -2.6654e+08 -1.06335e+08 -2.39209e+08 -9.79404e+07 -2.23892e+08 -8.83426e+07 -1.92312e+08 -7.87742e+07 -1.60521e+08 -7.04843e+07 -1.27186e+08 -6.47317e+07 -9.32799e+07 -6.24123e+07 -5.99393e+07 -6.36135e+07 -2.82854e+07 -6.70847e+07 506979 -6.98969e+07 2.46806e+07 -6.79652e+07 4.00934e+07 -5.61755e+07 3.4768e+07 -2.19757e+07 -3.82458e+07 1.92267e+07 -3.82606e+06 9.1052e+07 3.03234e+07 1.90611e+08 5.21797e+07 2.98273e+08 7.93589e+07 4.0032e+08 1.08472e+08 5.11162e+08 1.27074e+08 6.4324e+08 1.29478e+08 8.08305e+08 1.08778e+08 9.47829e+08 4.41643e+07 1.00179e+09 -6.06664e+07 9.97409e+08 -1.29858e+08 9.69287e+08 -7.41728e+07 9.70932e+08 1.28615e+08 1.00068e+09 3.61487e+08 1.04608e+09 5.94167e+08 1.08573e+09 8.86704e+08 1.11479e+09 1.21865e+09 1.1087e+09 1.54709e+09 1.07053e+09 1.82427e+09 1.02453e+09 2.04062e+09 9.84317e+08 2.21734e+09 9.71344e+08 2.37863e+09 9.9072e+08 2.53749e+09 9.90842e+08 2.71065e+09 9.22202e+08 2.89852e+09 8.47376e+08 3.05085e+09 9.14528e+08 3.10849e+09 1.16904e+09 3.09679e+09 1.50471e+09 3.18481e+09 1.45294e+09 3.90398e+09 9.85109e+08 4.70498e+09 1.17259e+09 4.24748e+09 1.5009e+09 3.25327e+09 8.09866e+08 4.54098e+09 1.01707e+09 4.77082e+08 1.56997e+09 -2.01602e+09 2.24035e+09 -3.38614e+09 2.62737e+09 -4.50632e+09 2.45082e+09 -4.8106e+09 -4.00631e+09 -2.48809e+08 5.13679e+06 -1.95212e+08 -2.60634e+08 8.27447e+06 -1.63435e+08 -2.75439e+08 9.15639e+06 -1.4178e+08 -2.9259e+08 1.07264e+07 -1.0547e+08 -3.12344e+08 1.17957e+07 -6.99277e+07 -3.34803e+08 1.22956e+07 -3.37463e+07 -3.59623e+08 1.19272e+07 2.12027e+06 -3.86011e+08 1.05699e+07 3.67799e+07 -4.1276e+08 8.29282e+06 6.965e+07 -4.38181e+08 5.37722e+06 1.00568e+08 -4.60056e+08 1.91691e+06 1.29619e+08 -4.74003e+08 -3.26638e+06 1.56653e+08 -4.78614e+08 -1.0073e+07 1.86782e+08 -4.89048e+08 -2.28262e+06 2.30687e+08 -5.15388e+08 1.25808e+07 2.73421e+08 -5.46424e+08 1.5747e+07 3.17865e+08 -5.8285e+08 1.98125e+07 3.6395e+08 -6.29224e+08 2.86885e+07 4.11497e+08 -6.85087e+08 4.18156e+07 4.5782e+08 -7.47151e+08 5.77588e+07 4.96934e+08 -8.17488e+08 7.75609e+07 5.37089e+08 -8.98241e+08 1.00498e+08 5.955e+08 -9.78061e+08 1.05733e+08 6.75058e+08 -1.03637e+09 9.42433e+07 7.82856e+08 -1.07433e+09 7.57548e+07 9.27118e+08 -1.09989e+09 4.62152e+07 1.10696e+09 -1.10147e+09 2.16041e+07 1.30569e+09 -1.07844e+09 1.15311e+06 1.50716e+09 -1.01705e+09 -3.30433e+07 1.73154e+09 -9.01892e+08 -9.17104e+07 1.98997e+09 -7.36879e+08 -1.56354e+08 2.25997e+09 -5.26307e+08 -2.20421e+08 2.51589e+09 -2.7142e+08 -2.84385e+08 2.76235e+09 1.34932e+07 -3.35191e+08 2.99763e+09 3.15157e+08 -3.60672e+08 3.19662e+09 6.32624e+08 -3.59949e+08 3.32525e+09 9.74793e+08 -3.36945e+08 3.33236e+09 1.3753e+09 -3.12263e+08 3.16856e+09 1.75904e+09 -3.16707e+08 2.91663e+09 1.95598e+09 -3.62899e+08 2.3938e+09 2.1702e+09 -4.47467e+08 1.50322e+09 2.72977e+09 -5.35588e+08 1.09116e+09 3.28949e+09 -6.35446e+08 1.72604e+09 3.92696e+09 -5.73143e+08 2.15396e+09 4.37037e+09 -5.35442e+07 2.79987e+09 4.20867e+09 1.03187e+09 2.69507e+09 3.30286e+09 9.41946e+08 2.51822e+09 1.40768e+09 5.99994e+08 4.00405e+09 6.41261e+08 6.75091e+08 1.1522e+09 8.64638e+08 7.74598e+08 -3.06205e+08 1.22523e+09 5.48106e+08 -1.95625e+09 1.33579e+09 2.64028e+08 -3.15213e+09 1.18993e+09 6.75931e+07 -3.55357e+09 1.30483e+08 -2.98743e+09 -1.33938e+08 -2.54105e+08 -1.26085e+08 -2.33469e+08 -1.18297e+08 -2.17579e+08 -1.08461e+08 -1.86654e+08 -9.79156e+07 -1.54613e+08 -8.79838e+07 -1.20183e+08 -8.01374e+07 -8.44242e+07 -7.56568e+07 -4.87006e+07 -7.51866e+07 -1.45919e+07 -7.82566e+07 1.58842e+07 -8.29531e+07 3.96389e+07 -8.59287e+07 5.05004e+07 -8.02521e+07 3.20335e+07 -6.46123e+07 -3.94187e+07 -2.56477e+07 -1.22494e+07 3.33364e+07 7.76407e+06 1.14309e+08 1.37847e+07 2.12868e+08 3.14519e+07 3.08169e+08 7.28617e+07 4.02772e+08 1.04725e+08 5.20837e+08 1.12624e+08 6.41708e+08 8.91079e+07 7.52249e+08 4.97536e+07 8.21487e+08 -1.62191e+07 8.29856e+08 -7.3639e+07 8.41325e+08 -3.66338e+07 8.87332e+08 1.0258e+08 9.59714e+08 2.8908e+08 1.04594e+09 4.77058e+08 1.14499e+09 7.01611e+08 1.24317e+09 9.71556e+08 1.30205e+09 1.27558e+09 1.28739e+09 1.56261e+09 1.21761e+09 1.78532e+09 1.14156e+09 1.94625e+09 1.11892e+09 2.05759e+09 1.18504e+09 2.15085e+09 1.32408e+09 2.2764e+09 1.48224e+09 2.44207e+09 1.6111e+09 2.57653e+09 1.67446e+09 2.61561e+09 1.68011e+09 2.57756e+09 1.66223e+09 2.60545e+09 1.35623e+09 3.69014e+09 9.93349e+08 5.07389e+09 1.5408e+09 4.82251e+09 1.94994e+09 3.9262e+09 1.60476e+09 5.59565e+09 2.04312e+09 7.83498e+08 2.91888e+09 -2.04875e+09 3.55778e+09 -3.41581e+09 3.63495e+09 -4.27203e+09 3.19172e+09 -4.27035e+09 -3.30275e+09 -2.7363e+08 5.00095e+06 -1.78131e+08 -2.84955e+08 7.45553e+06 -1.5277e+08 -2.98668e+08 8.15626e+06 -1.30035e+08 -3.14129e+08 9.87183e+06 -9.40228e+07 -3.32017e+08 1.13383e+07 -5.77867e+07 -3.52861e+08 1.25175e+07 -1.99906e+07 -3.76728e+08 1.30466e+07 1.83401e+07 -4.03155e+08 1.26996e+07 5.61212e+07 -4.31124e+08 1.13721e+07 9.23626e+07 -4.59041e+08 9.15086e+06 1.26207e+08 -4.84345e+08 6.16387e+06 1.56671e+08 -5.024e+08 1.5859e+06 1.81938e+08 -5.14492e+08 -1.88023e+06 2.08066e+08 -5.32094e+08 8.00209e+06 2.4778e+08 -5.58856e+08 1.87009e+07 2.88648e+08 -5.87634e+08 2.30699e+07 3.3148e+08 -6.19767e+08 2.99507e+07 3.75471e+08 -6.59625e+08 4.25463e+07 4.19546e+08 -7.08594e+08 5.83957e+07 4.58313e+08 -7.64302e+08 7.57703e+07 4.86701e+08 -8.33949e+08 9.74763e+07 5.14876e+08 -9.17076e+08 1.18112e+08 5.6272e+08 -9.97765e+08 1.36045e+08 6.3059e+08 -1.07077e+09 1.38212e+08 7.22818e+08 -1.13589e+09 1.25078e+08 8.63313e+08 -1.18422e+09 1.10856e+08 1.04539e+09 -1.212e+09 9.51724e+07 1.23414e+09 -1.22377e+09 8.65828e+07 1.42709e+09 -1.2071e+09 7.18389e+07 1.63874e+09 -1.12681e+09 2.28341e+07 1.88404e+09 -9.72152e+08 -5.20152e+07 2.15618e+09 -7.50672e+08 -1.30903e+08 2.42532e+09 -4.64399e+08 -2.0589e+08 2.68183e+09 -1.2493e+08 -2.68586e+08 2.92881e+09 2.35535e+08 -2.88066e+08 3.13206e+09 5.88304e+08 -2.32294e+08 3.21081e+09 9.62446e+08 -8.48722e+07 3.04068e+09 1.55135e+09 1.14447e+08 2.44669e+09 1.94541e+09 1.51345e+08 2.33858e+09 1.70088e+09 -3.45706e+06 2.6185e+09 1.21694e+09 -2.47307e+08 2.20213e+09 6.59281e+08 -5.65513e+08 2.17127e+09 2.80018e+08 -9.18173e+08 3.03021e+09 1.08157e+09 -1.04872e+09 2.4528e+09 2.3577e+09 -9.98293e+08 2.61114e+09 3.42383e+09 -2.21044e+08 1.97387e+09 3.50136e+09 4.10431e+08 2.2239e+09 2.29597e+09 3.74792e+08 5.02808e+09 2.16991e+09 4.55964e+08 9.7373e+08 2.69957e+09 8.22488e+08 -1.47889e+09 2.96249e+09 6.07085e+08 -2.68926e+09 3.03187e+09 1.81385e+08 -3.313e+09 2.75107e+09 -2.2351e+07 -3.19738e+09 9.13861e+07 -2.49782e+09 -1.5447e+08 -2.41504e+08 -1.47769e+08 -2.28152e+08 -1.40597e+08 -2.12266e+08 -1.30187e+08 -1.83086e+08 -1.17784e+08 -1.5175e+08 -1.04668e+08 -1.16974e+08 -9.24478e+07 -7.97756e+07 -8.28223e+07 -4.15993e+07 -7.72401e+07 -4.29533e+06 -7.64766e+07 2.95275e+07 -8.02691e+07 5.56951e+07 -8.80314e+07 6.60728e+07 -9.22809e+07 4.18952e+07 -9.64278e+07 -1.98832e+07 -7.38761e+07 -8.23178e+06 -2.94166e+07 -3.36481e+06 3.7312e+07 -9.98069e+06 1.23059e+08 412886 2.07162e+08 5.71331e+07 2.88497e+08 1.07282e+08 3.74665e+08 1.19958e+08 4.75087e+08 1.20728e+08 5.63423e+08 9.94461e+07 6.15684e+08 5.6949e+07 6.39605e+08 3.08597e+07 6.49846e+08 5.07069e+07 6.95482e+08 1.38575e+08 7.80511e+08 2.76635e+08 8.7732e+08 4.38466e+08 9.94468e+08 5.94804e+08 1.13821e+09 7.64021e+08 1.27773e+09 9.93505e+08 1.34611e+09 1.27705e+09 1.32226e+09 1.53006e+09 1.23684e+09 1.73556e+09 1.13572e+09 1.9225e+09 1.10171e+09 2.09858e+09 1.22198e+09 2.2674e+09 1.42175e+09 2.38277e+09 1.57779e+09 2.39403e+09 1.64437e+09 2.26306e+09 1.69658e+09 1.91536e+09 1.78278e+09 1.56828e+09 2.13383e+09 2.29391e+09 2.33806e+09 3.90663e+09 1.86715e+09 5.14039e+09 1.579e+09 4.7232e+09 2.22459e+09 5.40851e+09 2.94125e+09 5.85217e+08 4.02608e+09 -2.25205e+09 4.71714e+09 -3.44652e+09 4.63738e+09 -3.96568e+09 3.98373e+09 -3.61079e+09 -2.52277e+09 -3.0096e+08 4.19104e+06 -1.61091e+08 -3.1158e+08 6.23905e+06 -1.42623e+08 -3.23174e+08 6.64385e+06 -1.19747e+08 -3.35168e+08 8.25605e+06 -8.53899e+07 -3.48462e+08 9.81525e+06 -4.98266e+07 -3.63951e+08 1.13823e+07 -1.17735e+07 -3.82033e+08 1.2602e+07 2.76925e+07 -4.02688e+08 1.31926e+07 6.73678e+07 -4.25337e+08 1.29383e+07 1.06003e+08 -4.48843e+08 1.1711e+07 1.42163e+08 -4.71417e+08 9.44612e+06 1.73976e+08 -4.9023e+08 5.45414e+06 1.98502e+08 -5.04397e+08 3.97637e+06 2.21085e+08 -5.22953e+08 1.16972e+07 2.55506e+08 -5.46884e+08 2.10159e+07 2.91819e+08 -5.70746e+08 2.65228e+07 3.30052e+08 -5.96009e+08 3.53709e+07 3.68467e+08 -6.28494e+08 5.03959e+07 4.047e+08 -6.71513e+08 6.90979e+07 4.34323e+08 -7.26108e+08 8.76333e+07 4.53116e+08 -7.93285e+08 1.09743e+08 4.74755e+08 -8.72413e+08 1.34925e+08 5.0923e+08 -9.58808e+08 1.51992e+08 5.58312e+08 -1.0459e+09 1.61882e+08 6.40257e+08 -1.13291e+09 1.6279e+08 7.74674e+08 -1.20888e+09 1.5498e+08 9.42696e+08 -1.26765e+09 1.56566e+08 1.1125e+09 -1.32007e+09 1.67061e+08 1.28649e+09 -1.35916e+09 1.74217e+08 1.4751e+09 -1.35216e+09 1.61184e+08 1.68582e+09 -1.27443e+09 1.1652e+08 1.93018e+09 -1.12128e+09 4.68165e+07 2.18996e+09 -8.86279e+08 -2.32928e+07 2.43117e+09 -5.63167e+08 -6.68384e+07 2.6302e+09 -1.40928e+08 -4.33188e+07 2.70252e+09 3.00309e+08 5.81019e+07 2.65308e+09 5.13883e+08 1.94337e+08 2.54388e+09 7.01246e+08 2.71091e+08 1.83657e+09 1.49476e+09 1.96859e+08 1.19139e+09 2.55684e+09 9.12442e+07 1.34781e+09 3.02931e+09 8.06298e+07 1.55724e+09 2.84388e+09 1.55317e+08 2.13303e+09 2.8676e+09 1.7564e+08 2.77834e+09 3.18459e+09 3.23582e+08 1.80886e+09 3.21663e+09 5.03055e+07 2.5679e+09 2.82711e+09 -2.34919e+08 2.67458e+09 2.13229e+09 -2.21678e+08 3.23793e+09 2.66794e+09 -4.02562e+08 5.02641e+09 3.90345e+09 -2.80658e+08 1.13007e+08 4.92094e+09 2.792e+08 -2.66937e+09 5.01409e+09 4.94985e+08 -3.17612e+09 4.86183e+09 1.6419e+08 -3.24213e+09 4.5834e+09 -1.41925e+08 -2.72451e+09 5.64765e+06 -1.92231e+09 -1.76257e+08 -2.26598e+08 -1.72011e+08 -2.21947e+08 -1.66286e+08 -2.07613e+08 -1.55714e+08 -1.8202e+08 -1.41306e+08 -1.53281e+08 -1.24078e+08 -1.2e+08 -1.05521e+08 -8.30435e+07 -8.75122e+07 -4.36905e+07 -7.21903e+07 -3.76904e+06 -6.17601e+07 3.3944e+07 -5.84522e+07 6.49113e+07 -6.5011e+07 8.09568e+07 -8.19833e+07 6.392e+07 -1.00065e+08 9.47979e+06 -9.49483e+07 7.26474e+06 -6.97024e+07 -1.19085e+06 -1.90753e+07 -2.36931e+07 5.86904e+07 -2.49849e+07 1.37893e+08 4.7829e+07 2.08636e+08 1.22348e+08 2.86448e+08 1.56921e+08 3.63038e+08 1.66867e+08 4.36454e+08 1.6956e+08 4.94388e+08 1.56589e+08 5.17411e+08 1.4708e+08 5.27382e+08 1.77412e+08 5.59007e+08 2.41475e+08 6.36744e+08 3.41255e+08 7.39368e+08 4.83348e+08 8.49505e+08 6.16355e+08 9.88793e+08 7.08505e+08 1.1702e+09 8.24137e+08 1.32858e+09 1.05921e+09 1.40743e+09 1.3486e+09 1.43637e+09 1.63193e+09 1.46045e+09 1.92924e+09 1.50548e+09 2.22004e+09 1.56238e+09 2.44835e+09 1.55016e+09 2.54984e+09 1.44701e+09 2.53399e+09 1.26885e+09 2.45093e+09 1.06501e+09 2.18742e+09 9.63718e+08 1.77919e+09 1.7938e+09 1.78258e+09 3.38027e+09 2.40324e+09 3.60859e+09 4.70996e+09 2.99716e+09 5.15091e+09 3.65303e+09 4.39114e+09 4.36656e+09 -3.69868e+08 5.35194e+09 -2.91461e+09 5.99783e+09 -3.55552e+09 5.79375e+09 -3.56474e+09 4.92055e+09 -2.85867e+09 -1.7517e+09 -3.31236e+08 4.37163e+06 -1.41244e+08 -3.42655e+08 5.41111e+06 -1.31508e+08 -3.52554e+08 5.14496e+06 -1.10577e+08 -3.60545e+08 6.2391e+06 -7.97895e+07 -3.67728e+08 7.46505e+06 -4.67517e+07 -3.75233e+08 8.96005e+06 -1.02621e+07 -3.83774e+08 1.04108e+07 2.85844e+07 -3.93758e+08 1.15745e+07 6.85083e+07 -4.0523e+08 1.2203e+07 1.08161e+08 -4.17954e+08 1.20353e+07 1.45862e+08 -4.31346e+08 1.0843e+07 1.79403e+08 -4.44382e+08 8.03472e+06 2.05536e+08 -4.55261e+08 5.58736e+06 2.25666e+08 -4.68945e+08 1.08859e+07 2.55285e+08 -4.86128e+08 1.8131e+07 2.86522e+08 -5.02874e+08 2.28034e+07 3.18775e+08 -5.20732e+08 3.0338e+07 3.50041e+08 -5.45116e+08 4.40801e+07 3.79585e+08 -5.81084e+08 6.30799e+07 4.01964e+08 -6.29723e+08 8.30544e+07 4.12432e+08 -6.94114e+08 1.03943e+08 4.21288e+08 -7.65427e+08 1.23601e+08 4.42414e+08 -8.43185e+08 1.41706e+08 4.78862e+08 -9.39912e+08 1.52913e+08 5.56375e+08 -1.04513e+09 1.52458e+08 6.92985e+08 -1.1387e+09 1.51375e+08 8.51826e+08 -1.22499e+09 1.58782e+08 1.0019e+09 -1.31196e+09 1.75119e+08 1.15648e+09 -1.39793e+09 1.87918e+08 1.32721e+09 -1.46497e+09 1.94263e+08 1.50767e+09 -1.48543e+09 1.7916e+08 1.71509e+09 -1.44856e+09 1.26269e+08 1.96629e+09 -1.35304e+09 4.97591e+07 2.21742e+09 -1.20815e+09 -1.39357e+07 2.41646e+09 -9.40613e+08 -3.55783e+07 2.37307e+09 -2.1319e+08 -3.73e+07 1.83984e+09 9.30128e+08 -6.88277e+07 1.34028e+09 1.663e+09 -1.57986e+08 1.13366e+09 2.05208e+09 -1.48617e+08 8.09454e+08 2.6516e+09 3.22044e+07 5.72134e+08 3.31354e+09 2.49977e+08 5.03756e+08 4.05595e+09 5.58672e+08 6.92405e+08 5.21683e+09 9.26733e+08 5.88515e+08 6.21012e+09 1.4567e+09 -7.24449e+08 6.63179e+09 1.84029e+09 2.57672e+08 5.48146e+09 1.21337e+09 2.56096e+09 3.25389e+09 4.89299e+07 5.36547e+09 4.30582e+09 -1.13805e+09 5.07855e+09 6.54588e+09 -9.15853e+08 -1.20125e+09 7.46674e+09 -5.81353e+07 -3.49731e+09 7.15301e+09 4.24231e+08 -3.24752e+09 6.80301e+09 1.53943e+08 -3.02009e+09 6.58749e+09 -2.17127e+08 -2.27917e+09 -6.29971e+07 -1.29345e+09 -1.98291e+08 -2.08618e+08 -1.98593e+08 -2.13005e+08 -1.96167e+08 -2.02039e+08 -1.87231e+08 -1.82289e+08 -1.72286e+08 -1.58744e+08 -1.51802e+08 -1.29822e+08 -1.26779e+08 -9.6152e+07 -9.88179e+07 -5.86227e+07 -7.027e+07 -1.86289e+07 -4.43471e+07 2.14946e+07 -2.54249e+07 5.7662e+07 -2.034e+07 8.28224e+07 -3.8523e+07 8.37043e+07 -6.90983e+07 4.28557e+07 -7.92209e+07 2.46084e+07 -7.73821e+07 7.2781e+06 -5.24522e+07 -3.07608e+07 1.86641e+07 -6.39107e+07 1.08212e+08 1.13167e+07 1.84257e+08 1.20215e+08 2.5416e+08 1.74056e+08 3.30721e+08 2.07239e+08 4.03176e+08 2.25495e+08 4.60811e+08 2.28551e+08 5.00631e+08 2.42612e+08 5.24036e+08 2.80629e+08 5.59233e+08 3.39093e+08 6.31677e+08 4.1513e+08 7.46528e+08 5.23675e+08 8.80528e+08 6.39209e+08 1.03596e+09 6.8955e+08 1.24503e+09 6.93629e+08 1.47971e+09 8.25191e+08 1.66164e+09 1.10467e+09 1.79446e+09 1.41628e+09 1.91884e+09 1.7194e+09 2.03618e+09 1.98064e+09 2.07989e+09 2.18431e+09 2.02927e+09 2.37987e+09 1.90214e+09 2.61052e+09 1.6498e+09 2.85658e+09 1.27948e+09 3.00962e+09 8.85219e+08 2.99639e+09 1.21065e+09 2.82824e+09 2.91269e+09 2.45477e+09 4.643e+09 4.12301e+09 5.17054e+09 4.59855e+09 5.65587e+09 2.73315e+09 6.23849e+09 -1.86973e+09 6.93933e+09 -3.6597e+09 7.30917e+09 -3.48646e+09 6.94537e+09 -3.03309e+09 5.98968e+09 -2.11571e+09 -1.02419e+09 -3.64948e+08 4.47341e+06 -1.1865e+08 -3.80077e+08 5.40961e+06 -1.18267e+08 -3.90504e+08 4.45036e+06 -1.01586e+08 -3.96315e+08 4.8588e+06 -7.64931e+07 -3.9848e+08 5.41135e+06 -4.83289e+07 -3.98131e+08 6.34306e+06 -1.58071e+07 -3.96081e+08 7.3482e+06 1.99543e+07 -3.93092e+08 8.27735e+06 5.78062e+07 -3.89965e+08 8.96064e+06 9.6454e+07 -3.87515e+08 9.21437e+06 1.34247e+08 -3.86603e+08 8.71302e+06 1.6874e+08 -3.8819e+08 6.88761e+06 1.96756e+08 -3.8977e+08 3.5689e+06 2.16655e+08 -3.92954e+08 5.1409e+06 2.42206e+08 -3.99469e+08 8.66744e+06 2.69249e+08 -4.07246e+08 1.20157e+07 2.96561e+08 -4.15825e+08 1.61644e+07 3.23357e+08 -4.3185e+08 2.73939e+07 3.48063e+08 -4.61981e+08 4.52529e+07 3.65451e+08 -5.10228e+08 6.48653e+07 3.71038e+08 -5.69768e+08 8.24163e+07 3.75414e+08 -6.33015e+08 9.69538e+07 3.77635e+08 -7.01544e+08 1.01324e+08 4.05427e+08 -7.74666e+08 9.92176e+07 4.82523e+08 -8.37154e+08 9.68289e+07 6.05236e+08 -8.81633e+08 9.6006e+07 7.41682e+08 -9.00983e+08 1.03291e+08 8.63126e+08 -8.9193e+08 1.14193e+08 9.78768e+08 -8.55642e+08 1.21751e+08 1.11249e+09 -8.18238e+08 1.39845e+08 1.26985e+09 -7.95894e+08 1.58041e+08 1.46982e+09 -7.73523e+08 1.32602e+08 1.73986e+09 -6.86459e+08 4.64307e+07 2.00422e+09 -4.46988e+08 -7.63402e+07 2.16319e+09 -5.72252e+07 -1.99603e+08 2.06873e+09 4.75614e+08 -2.88165e+08 1.44265e+09 1.14791e+09 -3.10019e+08 7.98055e+08 1.71114e+09 -2.89986e+08 6.60205e+08 2.09855e+09 -2.62629e+08 4.64372e+08 2.52145e+09 -2.4218e+08 1.5976e+08 3.10068e+09 -2.25077e+08 -8.20688e+07 3.95697e+09 -1.29918e+08 -2.45934e+08 5.00148e+09 1.32378e+08 -8.18252e+08 5.65375e+09 7.04499e+08 -2.29771e+09 6.88159e+09 1.82432e+09 -3.02166e+09 6.00642e+09 2.00785e+09 1.24146e+09 4.72917e+09 2.04144e+08 6.2727e+09 7.79546e+09 -1.92091e+09 3.84853e+09 1.04101e+10 -1.38312e+09 -2.4592e+09 1.05768e+10 -2.29091e+08 -3.43486e+09 9.65023e+09 2.05674e+08 -2.52217e+09 8.77198e+09 2.61966e+06 -2.14043e+09 8.11582e+09 -3.34133e+08 -1.29079e+09 -2.17693e+08 -3.88827e+08 -2.15393e+08 -1.83161e+08 -2.23742e+08 -1.96632e+08 -2.27896e+08 -1.91413e+08 -2.24414e+08 -1.7947e+08 -2.13073e+08 -1.63735e+08 -1.93489e+08 -1.42509e+08 -1.65706e+08 -1.16273e+08 -1.30476e+08 -8.54174e+07 -8.95656e+07 -5.05798e+07 -4.61945e+07 -1.31232e+07 -5.41936e+06 2.39746e+07 2.45928e+07 5.57218e+07 2.95642e+07 7.40037e+07 957361 6.2366e+07 -2.46862e+07 3.91637e+07 -4.37685e+07 1.46217e+07 -5.95915e+07 -2.6717e+07 -2.95398e+07 -9.68452e+07 7.43865e+07 -7.50637e+07 1.73256e+08 6.31927e+07 2.56206e+08 1.59421e+08 3.36432e+08 2.02628e+08 4.14488e+08 2.27831e+08 4.83322e+08 2.37735e+08 5.39621e+08 2.52564e+08 5.95119e+08 2.92806e+08 6.59772e+08 3.47807e+08 7.43998e+08 4.12085e+08 8.71086e+08 4.80789e+08 1.04224e+09 5.65046e+08 1.24914e+09 5.91193e+08 1.5055e+09 5.14984e+08 1.78385e+09 5.36208e+08 1.99411e+09 7.59074e+08 2.1137e+09 1.03464e+09 2.18184e+09 1.29478e+09 2.24496e+09 1.53031e+09 2.31467e+09 1.73819e+09 2.37765e+09 1.96157e+09 2.40598e+09 2.24302e+09 2.33282e+09 2.60191e+09 2.07881e+09 3.02142e+09 1.7579e+09 3.32854e+09 1.65928e+09 3.49511e+09 2.64806e+09 3.15113e+09 5.45532e+09 3.19217e+09 7.73886e+09 2.41546e+09 8.26454e+09 2.42399e+08 8.25704e+09 -3.25607e+09 8.30085e+09 -3.92954e+09 8.12705e+09 -3.10128e+09 7.52185e+09 -2.42844e+09 6.54013e+09 -1.47482e+09 -4.23102e+08 -3.94356e+08 6.98958e+06 -8.89716e+07 -4.18695e+08 7.74943e+06 -9.881e+07 -4.34647e+08 6.0237e+06 -8.96011e+07 -4.43866e+08 5.98573e+06 -7.19319e+07 -4.46697e+08 6.17256e+06 -5.09788e+07 -4.43567e+08 6.75007e+06 -2.54299e+07 -4.34707e+08 7.30443e+06 3.77851e+06 -4.20761e+08 7.64539e+06 3.60662e+07 -4.0294e+08 7.6541e+06 7.06642e+07 -3.83253e+08 7.2608e+06 1.06619e+08 -3.64081e+08 6.32945e+06 1.41536e+08 -3.47843e+08 4.6228e+06 1.72061e+08 -3.35326e+08 1.30507e+06 1.95139e+08 -3.2386e+08 -49165.2 2.18603e+08 -3.14022e+08 -66146.7 2.42402e+08 -3.06318e+08 1.48185e+06 2.65682e+08 -3.00897e+08 2.87932e+06 2.88308e+08 -3.03332e+08 9.30048e+06 3.10198e+08 -3.23745e+08 2.27723e+07 3.2858e+08 -3.71961e+08 4.46617e+07 3.39375e+08 -4.43814e+08 6.61925e+07 3.39706e+08 -4.95605e+08 6.79945e+07 3.17943e+08 -5.15816e+08 5.23969e+07 3.18857e+08 -5.35272e+08 4.1788e+07 3.9992e+08 -5.50128e+08 4.16348e+07 5.17959e+08 -5.33766e+08 4.65234e+07 6.25439e+08 -4.78012e+08 5.44486e+07 7.02796e+08 -3.73751e+08 6.44252e+07 7.61942e+08 -2.11742e+08 7.19224e+07 8.29136e+08 -2.57178e+07 9.79275e+07 9.33285e+08 1.41535e+08 1.34651e+08 1.11187e+09 2.90646e+08 1.30351e+08 1.40121e+09 4.48382e+08 5.36045e+07 1.72953e+09 6.67517e+08 -8.68911e+07 1.95665e+09 8.89234e+08 -2.19875e+08 1.97176e+09 9.647e+08 -3.25534e+08 1.56529e+09 1.06202e+09 -4.39712e+08 9.80154e+08 1.38914e+09 -4.99006e+08 6.52968e+08 1.79327e+09 -5.28865e+08 3.98476e+08 2.29639e+09 -6.0203e+08 6.6624e+07 3.0069e+09 -7.29742e+08 -2.4382e+08 3.93906e+09 -8.19701e+08 -5.43709e+08 5.02144e+09 -7.93697e+08 -1.28295e+09 5.5052e+09 -3.81731e+08 -2.63192e+09 6.77577e+09 8.89654e+08 -5.39473e+09 7.57643e+09 8.34527e+08 -5.45968e+08 9.40179e+09 -5.42868e+08 4.89128e+09 1.28681e+10 -2.06747e+09 2.39937e+09 1.33089e+10 -1.30884e+09 -1.60917e+09 1.15459e+10 -3.20014e+08 -1.36417e+09 9.7747e+09 -3.27016e+07 -7.39852e+08 8.13892e+09 -1.31536e+08 -4.13411e+08 6.72304e+09 -4.0585e+08 4.91444e+08 -3.51351e+08 6.12766e+08 -2.14868e+08 -1.47319e+08 -2.34134e+08 -1.68288e+08 -2.48383e+08 -1.70351e+08 -2.56246e+08 -1.65312e+08 -2.56156e+08 -1.57698e+08 -2.46144e+08 -1.46077e+08 -2.2483e+08 -1.30786e+08 -1.91664e+08 -1.11627e+08 -1.47199e+08 -8.83225e+07 -9.36346e+07 -6.08801e+07 -3.48862e+07 -3.11271e+07 2.26871e+07 -2.4625e+06 6.71332e+07 2.16302e+07 7.97557e+07 3.46565e+07 6.47767e+07 3.15739e+07 3.58839e+07 1.38186e+07 -4.87109e+06 -2.26838e+07 -4.32869e+07 -9.80741e+07 1.83817e+07 -1.67354e+08 1.44033e+08 -5.90816e+07 2.41197e+08 9.62409e+07 3.35233e+08 1.53884e+08 4.21601e+08 1.68473e+08 4.97613e+08 1.73804e+08 5.73219e+08 1.90966e+08 6.55392e+08 2.27739e+08 7.49803e+08 2.78031e+08 8.52569e+08 3.4144e+08 9.93038e+08 3.7485e+08 1.1953e+09 4.18153e+08 1.44689e+09 4.25722e+08 1.72056e+09 3.17413e+08 1.95188e+09 3.01379e+08 2.06309e+09 5.01193e+08 2.12778e+09 6.84897e+08 2.24611e+09 7.78807e+08 2.43245e+09 8.23206e+08 2.72185e+09 8.61237e+08 3.13428e+09 9.28259e+08 3.62741e+09 1.05497e+09 4.10997e+09 1.29534e+09 4.50827e+09 1.7017e+09 4.85067e+09 2.07335e+09 4.98763e+09 2.84699e+09 5.61047e+09 3.27878e+09 9.29184e+09 2.41897e+08 1.23837e+10 -1.29469e+09 1.17739e+10 -1.26104e+09 9.66723e+09 -2.48352e+09 8.2123e+09 -2.81876e+09 7.28737e+09 -2.24273e+09 6.24792e+09 -1.56082e+09 4.92979e+09 -6.19395e+08 1.27896e+08 -3.95668e+08 1.01712e+07 -5.4865e+07 -4.31009e+08 1.06484e+07 -7.34921e+07 -4.5509e+08 8.33923e+06 -7.36729e+07 -4.75471e+08 9.26146e+06 -6.10453e+07 -4.89954e+08 1.0475e+07 -4.75249e+07 -4.96698e+08 1.20579e+07 -3.15624e+07 -4.94177e+08 1.36326e+07 -1.34412e+07 -4.81757e+08 1.49716e+07 7.33109e+06 -4.59767e+08 1.59075e+07 3.11107e+07 -4.30198e+08 1.63187e+07 5.84743e+07 -3.96078e+08 1.60307e+07 8.7902e+07 -3.6043e+08 1.48599e+07 1.15613e+08 -3.27031e+08 1.23195e+07 1.39777e+08 -2.94764e+08 9.71675e+06 1.6222e+08 -2.62967e+08 7.52553e+06 1.83169e+08 -2.314e+08 5.31323e+06 2.02207e+08 -2.00468e+08 4.60739e+06 2.19605e+08 -1.73377e+08 6.05001e+06 2.36094e+08 -1.67934e+08 2.13425e+07 2.51777e+08 -2.12597e+08 5.5072e+07 2.77819e+08 -3.04264e+08 8.22447e+07 3.04324e+08 -4.036e+08 7.37752e+07 2.8614e+08 -4.38212e+08 2.89115e+07 2.53562e+08 -4.1049e+08 -4.68882e+06 3.07791e+08 -3.71219e+08 -1.13012e+07 4.227e+08 -3.02243e+08 -3.55161e+06 4.9678e+08 -1.91318e+08 8.52087e+06 5.25969e+08 -2.84792e+07 1.52612e+07 5.2847e+08 2.04949e+08 1.53335e+07 5.23038e+08 4.18666e+08 5.33786e+07 6.06057e+08 5.59215e+08 1.10774e+08 7.96146e+08 7.22756e+08 1.19431e+08 1.04857e+09 9.17769e+08 9.60439e+07 1.36277e+09 1.06845e+09 8.78365e+07 1.63267e+09 1.25796e+09 3.59001e+07 1.64127e+09 1.53108e+09 -1.45223e+08 1.30751e+09 1.77583e+09 -3.64303e+08 9.43345e+08 2.00768e+09 -4.78855e+08 7.17941e+08 2.24177e+09 -5.4194e+08 5.01522e+08 2.52771e+09 -6.39905e+08 1.98152e+08 2.94624e+09 -7.76973e+08 -1.23945e+08 3.51148e+09 -8.59846e+08 -4.89417e+08 3.93324e+09 -8.08386e+08 -1.18623e+09 4.16636e+09 -5.53355e+08 -2.55954e+09 6.21911e+09 9.75341e+07 -7.79323e+09 7.97139e+09 -7.68352e+08 -1.74445e+09 1.20384e+10 -1.76601e+09 2.47369e+09 1.30768e+10 -1.56434e+09 2.84105e+09 9.73877e+09 -3.95394e+08 2.04199e+09 6.28933e+09 -9.97613e+07 2.07097e+09 4.72974e+09 -1.13815e+08 7.95808e+08 3.12269e+09 -1.12604e+08 1.18747e+09 1.88492e+09 -7.14491e+07 1.66835e+09 -4.53762e+08 1.17695e+09 -1.6507e+08 -1.02225e+08 -1.89697e+08 -1.3285e+08 -2.10171e+08 -1.41454e+08 -2.34107e+08 -1.32131e+08 -2.5527e+08 -1.26226e+08 -2.67616e+08 -1.22003e+08 -2.67413e+08 -1.17827e+08 -2.52551e+08 -1.12136e+08 -2.22215e+08 -1.03622e+08 -1.77614e+08 -9.05962e+07 -1.21258e+08 -7.41058e+07 -5.56169e+07 -5.80999e+07 1.35795e+07 -4.35835e+07 7.53396e+07 -3.07113e+07 1.16413e+08 -2.23733e+07 1.27029e+08 -2.10581e+07 1.01263e+08 -3.44144e+07 1.95001e+07 -6.79287e+07 -4.65788e+07 -1.49987e+08 3.71166e+07 -1.57914e+08 1.71782e+08 -4.90268e+06 2.84762e+08 8.02876e+07 3.96464e+08 5.72956e+07 4.87525e+08 4.90276e+07 5.71149e+08 6.48534e+07 6.66731e+08 9.76642e+07 7.80883e+08 1.41203e+08 8.98649e+08 2.05503e+08 1.03129e+09 2.19344e+08 1.21569e+09 2.43532e+08 1.44813e+09 2.54882e+08 1.65358e+09 1.77721e+08 1.86883e+09 1.26649e+08 2.24061e+09 1.59099e+08 2.72712e+09 1.70259e+08 3.11764e+09 1.68731e+08 3.32945e+09 1.5798e+08 3.47207e+09 1.35417e+08 3.6294e+09 1.12178e+08 3.82906e+09 8.95115e+07 4.06378e+09 1.42706e+08 4.37835e+09 3.60494e+08 4.97333e+09 4.97776e+08 6.07013e+09 1.03929e+09 8.47856e+09 8.61394e+08 1.13214e+10 -3.4416e+09 1.2721e+10 -4.52344e+09 1.01417e+10 -3.19092e+08 6.58327e+09 6.02202e+08 3.7776e+09 -2.16277e+08 2.65731e+09 -1.34097e+09 1.24929e+09 -3.76554e+08 -2.05046e+08 6.85706e+08 2.51331e+08 -2.76204e+08 1.00129e+07 -1.89261e+07 -3.08663e+08 6.67122e+06 -4.72274e+07 -3.24616e+08 3.61962e+06 -6.03102e+07 -3.53471e+08 1.1009e+07 -4.19659e+07 -3.88266e+08 1.59769e+07 -2.74387e+07 -4.20927e+08 1.89798e+07 -1.66487e+07 -4.4643e+08 2.14294e+07 -8.03269e+06 -4.62157e+08 2.3707e+07 791994 -4.66437e+08 2.59684e+07 1.08672e+07 -4.5929e+08 2.83091e+07 2.40409e+07 -4.41627e+08 3.05165e+07 3.96414e+07 -4.14652e+08 3.18426e+07 5.4557e+07 -3.81053e+08 3.16959e+07 6.89034e+07 -3.42104e+08 3.00503e+07 8.31775e+07 -2.97862e+08 2.72472e+07 9.64691e+07 -2.47884e+08 2.38353e+07 1.07024e+08 -1.92178e+08 2.04412e+07 1.14379e+08 -1.30552e+08 1.63038e+07 1.2335e+08 -7.23448e+07 1.66637e+07 1.37405e+08 -7.75509e+07 7.37537e+07 1.57971e+08 -1.74856e+08 1.70518e+08 1.75762e+08 -3.09422e+08 1.75886e+08 1.88931e+08 -4.18054e+08 1.15599e+08 1.80007e+08 -4.38919e+08 2.55791e+07 2.00174e+08 -3.6777e+08 -2.75325e+07 2.87099e+08 -2.5266e+08 -2.87132e+07 3.40189e+08 -1.11594e+08 -1.36044e+07 3.39731e+08 7.19809e+07 -1.60178e+07 3.10915e+08 3.00087e+08 -3.37399e+07 2.80329e+08 5.16857e+08 1.67103e+06 3.38557e+08 6.85773e+08 1.17372e+08 4.57465e+08 7.98939e+08 2.37842e+08 6.41269e+08 1.00564e+09 2.55869e+08 8.38387e+08 1.47102e+09 1.95679e+08 9.02302e+08 1.97897e+09 1.74409e+08 8.7798e+08 2.25348e+09 1.34883e+08 7.9669e+08 2.36199e+09 4.46218e+07 6.63613e+08 2.39471e+09 -3.04982e+07 5.62925e+08 2.31555e+09 -8.80273e+07 4.89033e+08 2.07248e+09 -1.5974e+08 3.8958e+08 1.73069e+09 -2.86639e+08 2.59836e+08 1.47563e+09 -3.60552e+08 -1.86104e+08 8.14147e+08 -1.66625e+07 -7.58706e+08 -1.43509e+09 1.46558e+08 -6.96682e+08 -3.7532e+09 -2.40257e+07 -5.69753e+09 -1.2988e+09 -1.52072e+09 -2.92965e+09 -1.58004e+08 -9.57021e+08 2.17139e+09 -2.96991e+09 9.37797e+08 4.56051e+09 -1.05105e+09 6.12202e+07 -1.667e+08 1.73659e+09 -4.52974e+08 -5.66926e+08 1.3485e+09 1.32918e+08 8.01615e+08 9.42759e+08 5.77945e+07 1.35715e+09 8.20934e+08 4.53594e+08 1.18707e+09 4.52825e+08 9.2054e+08 3.48189e+07 -5.72791e+07 3.13598e+07 -1.21909e+08 4.17754e+07 -1.45914e+08 7.69833e+06 -8.43278e+07 -4.65956e+07 -5.355e+07 -9.83428e+07 -4.90304e+07 -1.39055e+08 -5.34552e+07 -1.67035e+08 -5.82469e+07 -1.80376e+08 -6.23709e+07 -1.76824e+08 -6.45544e+07 -1.55434e+08 -6.50524e+07 -1.17964e+08 -6.61951e+07 -6.83436e+07 -6.76207e+07 -1.10252e+07 -6.90729e+07 4.92161e+07 -7.32462e+07 1.05634e+08 -8.11094e+07 1.5084e+08 -9.77404e+07 1.64428e+08 -1.17132e+08 4.13644e+07 -9.19665e+07 -1.8493e+08 2.82701e+07 -1.50525e+08 5.17769e+07 1.10527e+08 -4.57375e+07 3.17538e+08 -6.99449e+07 4.53786e+08 -9.38964e+07 5.39204e+08 -7.85372e+07 6.21506e+08 -4.22495e+07 7.26144e+08 -4.68629e+06 8.51288e+08 3.57642e+07 9.42085e+08 6.22928e+07 1.02259e+09 1.29426e+08 1.24972e+09 1.06822e+08 1.62229e+09 1.37126e+06 1.97986e+09 -2.1975e+07 2.29753e+09 -1.77065e+07 2.59735e+09 -2.00579e+07 2.82364e+09 -1.29865e+06 2.93696e+09 -5.71488e+06 2.94917e+09 -1.86016e+07 2.86056e+09 -1.32794e+07 2.67129e+09 -2.29582e+07 2.41012e+09 -4.56681e+07 2.4465e+09 -2.06927e+08 3.29261e+09 -5.09627e+08 4.69503e+09 -2.9503e+08 6.05933e+09 -5.87972e+08 4.03633e+09 -2.9931e+09 -9.71387e+08 -6.07978e+08 -2.40235e+09 1.89879e+09 -8.32717e+08 -1.09417e+09 -2.71921e+08 -1.45271e+09 -1.13744e+09 -5.27284e+08 -1.78794e+09 2.21438e+08 -8.33081e+08 1.2206e+08 -3.00461e+07 1.26536e+08 -1.75256e+06 1.13589e+08 -3.28899e+07 7.97221e+07 -2.28716e+07 9.41668e+06 3.2655e+07 -6.59096e+07 5.21031e+07 -1.29244e+08 5.09862e+07 -1.80816e+08 4.80083e+07 -2.23475e+08 4.79964e+07 -2.5856e+08 5.01225e+07 -2.84338e+08 5.30121e+07 -2.99896e+08 5.65322e+07 -3.06903e+08 6.00289e+07 -3.06023e+08 6.24411e+07 -2.96219e+08 6.28315e+07 -2.76729e+08 6.04135e+07 -2.47979e+08 5.44707e+07 -2.06575e+08 4.25749e+07 -1.40962e+08 2.16102e+07 -6.77837e+07 1.67019e+07 -7.51663e+07 1.03597e+08 -1.80915e+08 2.36144e+08 -3.29402e+08 2.97493e+08 -5.23081e+08 3.06767e+08 -6.89943e+08 2.79769e+08 -6.55534e+08 1.72162e+08 -4.69661e+08 1.00241e+08 -2.78322e+08 1.08732e+08 -5.87382e+07 5.97376e+07 1.84142e+08 7.13491e+06 4.52189e+08 4.03444e+07 7.27472e+08 1.49458e+08 1.03135e+09 3.01193e+08 1.44071e+09 3.87482e+08 1.90916e+09 3.85365e+08 2.34756e+09 3.82618e+08 2.7164e+09 3.60323e+08 2.98081e+09 3.20579e+08 3.18493e+09 2.71265e+08 3.34436e+09 2.37889e+08 3.45828e+09 1.88131e+08 3.52031e+09 1.16406e+08 3.09986e+09 1.79809e+08 1.843e+09 4.49158e+08 1.93024e+08 9.16625e+08 -5.67303e+09 9.97813e+07 -9.10135e+09 4.54083e+08 -7.40807e+09 3.58092e+08 -2.36952e+09 -6.37576e+08 -2.29304e+09 -4.54959e+08 -3.21897e+09 1.61712e+08 -2.59275e+09 2.58183e+07 -1.01448e+09 -3.32513e+08 -3.4125e+07 9.39951e+07 9.38933e+08 2.68501e+08 1.16434e+08 -4.76118e+07 -9.46016e+07 -9.18826e+07 -8.6057e+07 -8.78405e+07 -9.48303e+07 -1.04557e+08 -1.14748e+08 -1.23633e+08 -1.3267e+08 -1.44141e+08 -1.61284e+08 -1.91222e+08 -2.42254e+08 -3.30156e+08 -4.73782e+08 -6.20728e+08 -5.5327e+08 -3.01109e+08 -7.57171e+07 1.40343e+08 3.08382e+08 3.85841e+08 4.29138e+08 5.18996e+08 5.9937e+08 6.52517e+08 8.04757e+08 1.04096e+09 1.32052e+09 1.65944e+09 1.996e+09 2.3223e+09 2.63953e+09 2.90754e+09 3.11014e+09 3.28433e+09 3.40218e+09 3.43246e+09 3.37375e+09 3.29882e+09 3.90029e+09 3.38114e+09 7.46811e+08 3.46053e+08 1.47586e+09 -2.17217e+08 -1.64124e+09 -2.229e+09 -2.39879e+09 -2.19834e+09 7.36649e+07 2.1529e+07 -7.44642e+06 -1.20972e+07 -5.96371e+06 -1.52023e+07 -4.73399e+06 5.96421e+07 9.28726e+06 1.14492e+07 -1.03823e+07 -6.70211e+07 -5.43639e+07 8.64212e+06 4.75814e+07 -5.17215e+06 3.61752e+07 -5.31611e+06 -1.16599e+07 -1.44923e+07 -4.60971e+06 -4.31627e+06 4.91656e+07 -1.08744e+07 1.54325e+07 -4.36475e+06 -5.41426e+06 -2.53826e+06 -1.73424e+07 -6.86875e+07 -2.40046e+07 7.56861e+07 2.07096e+07 152007 7.69096e+06 3.167e+06 3.52575e+06 -4.24322e+07 3.33199e+07 7.45294e+06 3.60784e+07 6.40553e+07 -1.18282e+07 -7.27839e+07 -1.25836e+07 -1.03952e+07 -1.18834e+07 -1.74254e+07 6.04531e+06 -5.263e+07 -3.13373e+07 -5.66073e+06 3.44412e+07 5.2095e+07 2.00055e+07 -2.96353e+07 -1.77616e+07 8.64706e+06 4.62852e+06 4.47271e+06 5.02711e+06 5.05656e+06 1.52974e+07 3.09669e+07 -3.10495e+07 -3.11283e+07 -1.03083e+07 7.05944e+06 5.01143e+06 6.55212e+06 6.08767e+06 7.2901e+07 5.18434e+07 -6.27353e+06 -4.66853e+06 -1.07847e+07 -8.6912e+06 -7.49833e+06 6.1653e+07 -540439 -4.66155e+06 6.09174e+06 -6.30109e+07 -5.3106e+06 9.06681e+06 1.48077e+07 -9.2538e+06 2.89441e+06 -1.10655e+07 -2.48281e+06 1.33115e+06 -1.8624e+07 7.80547e+07 -2.10631e+07 407820 5.78661e+06 3.22267e+06 563136 -6.20888e+07 -8.98311e+06 8.20219e+07 -6.34828e+06 1.61872e+06 -1.03764e+07 8.52717e+06 -4.32827e+06 -6.15045e+07 1.44791e+07 3.33913e+06 5.0539e+07 3.69985e+07 5.48007e+06 -2.04014e+07 -1.23354e+07 -4.78672e+06 -4.04081e+06 -6.15155e+06 -4.53803e+07 2.8959e+07 -1.20831e+06 -1.90251e+07 3.97947e+07 1.20133e+07 -2.33458e+07 -1.3323e+07 1.27542e+07 7.14977e+06 5.70767e+06 7.21756e+06 6.13098e+06 -1.02211e+07 3.7496e+06 1.92151e+06 -4.76546e+06 1.92798e+06 3.46235e+06 1.42595e+06 4.07744e+06 6.94441e+06 5.59226e+06 -7.082e+07 -1.86163e+06 2.67646e+06 -781075 -545477 5.92622e+07 2.71785e+07 -7.00254e+07 -4.43332e+06 -8.06416e+07 -7.20192e+06 7.75394e+07 -953797 7.70772e+06 1.1939e+06 2.23735e+06 3.0983e+06 -8.32787e+06 9.86149e+07 -1.6021e+07 4.42103e+07 2.11689e+06 -1.20301e+08 -1.13674e+08 -4.87164e+06 -3.24199e+07 -607509 1.35223e+08 3.46733e+06 4.30914e+06 1.02654e+07 2.00233e+06 5.10474e+06 2.37102e+07 7.07466e+06 4.48479e+06 3.95051e+06 5.54475e+06 1.82502e+07 -4.16317e+07 -1.12676e+07 3.2983e+07 1.05669e+07 5.71389e+06 6.37151e+06 9.4459e+06 -8.38527e+07 5.1062e+06 -5.92534e+06 -590029 -1.6608e+06 -2.23117e+06 -7.70223e+07 2.64654e+07 -2.67855e+07 7.47042e+07 5.35673e+06 -7.38632e+06 7.47393e+06 -3.38041e+06 -1.0953e+08 -3.83572e+06 3.15948e+06 -1.17893e+06 4.22851e+06 383534 -7.83846e+07 2.84025e+07 -3.06038e+07 -1.23638e+06 8.57134e+07 1.02734e+07 2.06459e+06 -7.68539e+07 8.80061e+06 -1.79309e+07 2.73975e+07 -1.50104e+07 5.3485e+07 3.4298e+07 -9.23894e+07 -4.02092e+06 -7.52662e+07 -5.89453e+07 9.26461e+07 1.25374e+06 1.14034e+07 9.64556e+06 9.18424e+06 7.23814e+06 3.42427e+07 -4.12309e+07 4.95844e+07 6.12555e+06 -4.80433e+07 -6.86037e+06 -5.55703e+07 1.60661e+07 2.49999e+07 6.8398e+06 4.39299e+06 5.46521e+06 6.57542e+06 1.60047e+07 1.46503e+07 7.78949e+06 3.83609e+06 3.92385e+06 8.10895e+06 1.04359e+07 -8.84436e+06 -1.40474e+07 469979 2.59145e+06 -1.68593e+06 2.70833e+06 -7.42998e+07 3.26259e+06 -821362 -805770 -3.1673e+06 436589 -3.14938e+07 -5.60276e+06 -5.82606e+06 4.42809e+07 4.21939e+06 -7.79439e+06 8.6884e+06 -5.76893e+06 -368815 -34787.3 477646 -210563 2.33921e+07 -1.814e+07 1.39518e+07 2.0474e+06 -1.37686e+07 2.66024e+07 -2.68508e+08 -1.0467e+08 -1.95068e+08 -1.53486e+08 -3.95719e+07 3.96985e+06 -1.99345e+08 -4.40588e+07 -3.45599e+07 -3.90499e+07 -3.72922e+07 103753 -1.55567e+08 -6.51092e+07 -8.74222e+07 -8.47199e+07 -1.84532e+08 -2.28377e+07 -3.9821e+07 -5.90493e+07 -3.90973e+07 -1.52934e+08 -1.32988e+07 -4.71358e+07 -3.84496e+07 -3.92924e+07 1.12186e+07 1.85626e+08 4.74706e+07 -3.77659e+07 1.82692e+08 2.68011e+07 -5.28536e+07 1.40043e+07 -7.78201e+06 -6.6188e+07 -1.42612e+07 1.93465e+07 2.18723e+07 4.89565e+07 2.59515e+07 -3.84435e+07 2.523e+07 -5.81043e+07 -1.89168e+07 -2.10779e+07 2.06314e+07 5.26546e+07 3.49795e+07 3.06338e+07 1.59836e+07 -2.35383e+08 4.38403e+07 -2.49295e+08 -1.10707e+08 5.04092e+07 -1.49952e+08 2.51647e+07 -7.0023e+07 -7.57284e+07 -6.60601e+07 -7.36733e+06 -1.33393e+08 -6.72629e+07 -6.82466e+07 -5.15186e+07 2.6044e+07 -7.44052e+07 -4.07055e+07 -6.74347e+07 -1.06333e+07 -3.1643e+07 -1.1958e+07 -2.85385e+07 1.29453e+07 2.11373e+08 1.61998e+08 -5.29848e+07 1.90349e+08 1.7818e+07 2.20333e+06 -1.04119e+07 -6.65328e+07 -1.48314e+07 2.95182e+06 3.68995e+07 5.23799e+07 3.08391e+07 8.30582e+07 -5.80825e+07 -1.93815e+07 -2.78265e+07 5.35279e+07 2.45042e+07 2.40643e+07 -5.45728e+07 -1.07438e+08 -3.29799e+06 1.23001e+07 -2.77518e+06 -3.84406e+07 -1.03677e+08 -1.71802e+08 -7.44217e+07 -1.74261e+07 3.22147e+06 -6.2106e+06 3.67269e+07 -4.58404e+07 -4.71696e+07 -9.71244e+07 7.663e+07 -4.65003e+06 -1.74117e+06 -3.42616e+07 -5.94473e+07 -8.86938e+07 -7.46805e+07 -1.97726e+08 7.84467e+06 -1.05531e+06 -8.80937e+06 -167816 -4.04602e+07 -2.23554e+07 1.60633e+07 4.58751e+07 1.04094e+06 1.74353e+06 -4.50492e+07 -1.31614e+07 -2.45809e+06 -5.3586e+06 -7.69939e+06 -9.87884e+07 6.99759e+07 -2.25683e+06 -1.06633e+07 -8.05655e+06 -9.94565e+06 -1.18759e+07 4.61122e+06 -6.7273e+07 -1.60402e+07 -7.06164e+06 -6.34071e+06 -1.86245e+07 -5.08663e+06 -9.10314e+06 2.02761e+06 4.82821e+06 7.40254e+06 778347 1.66222e+08 -7.88919e+07 1.56099e+08 3.63105e+07 3.24226e+07 3.95218e+07 1.70063e+08 -151030 -1.54706e+08 2.43389e+07 4.11834e+07 3.55318e+07 5.72642e+07 453389 1.63964e+08 -1.39365e+08 2.55384e+07 1.23363e+08 4.73785e+07 3.76372e+07 5.73085e+07 -7.57293e+06 1.73018e+08 -4.51771e+07 -1.93223e+08 -1.24384e+07 -6.61738e+06 -1.38203e+06 5.40663e+07 -2.05064e+07 -2.73289e+08 7.01058e+06 -1.15255e+07 1.34541e+07 1.10604e+08 -3.36255e+07 -4.77337e+07 -2.01894e+08 1.14484e+07 4.02145e+07 2.4998e+07 8.43283e+07 -3.16729e+07 -3.29562e+08 2.51915e+07 4.58144e+07 1.35068e+07 2.90505e+06 -3.57657e+07 -1.58636e+07 1.31183e+07 -2.586e+06 3.76948e+07 4.97402e+06 -1.41665e+07 -5.00987e+07 -1.59048e+07 -5.62904e+06 -1.45762e+07 -6.42594e+06 -7.38821e+07 6.31323e+07 -2.6754e+07 -1.00551e+07 -6.03403e+06 -8.81515e+06 -1.35391e+07 -8.50337e+06 -3.25589e+07 -1.51366e+07 -5.68156e+06 -4.40326e+06 -1.77711e+07 -3.50202e+06 -3.06525e+07 -1.65594e+07 3.79689e+06 1.26825e+07 3.40075e+07 -64631.4 1.37997e+08 1.84273e+08 9.28268e+07 -2.71424e+07 -2.63757e+07 -2.28035e+07 1.25969e+08 8.0759e+06 1.39665e+06 3.60642e+07 1.58814e+07 3.0267e+07 -1.714e+07 1.02923e+08 8.73289e+07 766232 614857 3.93737e+06 1.34926e+07 7.06994e+07 -4.02367e+07 -1.01921e+08 -1.24763e+07 -2.41144e+07 -4.92824e+07 -1.26372e+08 -8.60502e+07 -1.82364e+08 -8.11385e+07 -1.09899e+07 -4.28234e+07 -3.81645e+07 -2.52837e+08 -2.07805e+07 -3.17003e+07 -4.57507e+07 -7.35095e+07 -6.93488e+07 -2.62239e+08 -6.62495e+07 -7.6176e+07 -7.94945e+07 -9.76261e+07 -7.67558e+07 -2.31705e+07 3.11173e+06 -7.69566e+06 7.02846e+06 2.13717e+07 -905111 -7.90717e+07 7.30224e+07 -2.08067e+06 -2.55037e+06 521167 -4.28195e+06 -1.7007e+06 -3.50645e+06 -3.23701e+07 -2.86723e+06 -5.33512e+06 -1.05108e+07 -2.65519e+06 -2.28938e+06 -6.75138e+07 -9.51487e+06 -2.34997e+07 -8.78089e+06 -2.03115e+07 130692 -2.62431e+06 -659672 -315233 3.43167e+06 -9.49515e+06 2.87646e+06 3.76634e+06 2.70094e+06 -2.98235e+06 3.20153e+06 -94616.5 3.66758e+06 1.68704e+08 -2.0594e+08 1.46844e+08 4.22018e+07 5.16717e+07 5.20726e+07 1.71324e+08 -612571 -2.82409e+08 1.56398e+07 6.23597e+07 8.91546e+07 7.55571e+07 -951202 -2.51978e+08 -7.66554e+06 6.72562e+07 8.8922e+07 7.44522e+07 -1.32873e+07 -4.52698e+07 -1.51493e+08 2.84724e+07 -4.25852e+07 -4.3765e+07 -1.20732e+07 1.72581e+07 -2.55891e+08 -1.53719e+07 -3.90816e+07 -6.61592e+07 3.1991e+07 -2.18822e+07 -2.42488e+07 -6.30286e+07 -3.8934e+07 -7.11829e+07 8.4661e+06 -6.29093e+07 -7.32363e+07 -8.05653e+07 -2.70708e+07 987739 -6.78841e+06 1.65413e+07 -2.17387e+06 1.84392e+07 -9.3839e+07 8.49147e+07 -7.25077e+06 -3.37852e+06 2.2261e+07 5.11188e+06 7.92305e+06 -6.7045e+06 5.27129e+06 -6.51539e+07 -6.39312e+06 -1.49213e+07 -6.62191e+06 -1.69243e+07 521248 -1.42844e+07 -6.57224e+06 -183768 7.46152e+06 1.27536e+07 -2.27293e+06 -8.29593e+07 -1.00201e+07 -2.15784e+07 -6.23329e+06 -2.11336e+07 -5.92946e+06 -428346 1.03511e+07 -3.43594e+06 -8.0343e+06 -9.90235e+06 1.64917e+07 1.54633e+06 -9.57804e+06 5.41124e+07 8.06696e+07 -3.40503e+07 -8.18498e+06 -4.69977e+06 -4.91102e+06 2.35417e+07 -4.41125e+06 -3.61331e+06 3.31185e+07 1.33036e+07 1.54209e+07 -4.4665e+06 1.06162e+08 -1.85434e+07 -1.54312e+07 -1.19501e+07 -3.2778e+07 3.96929e+07 2.76486e+07 8.70789e+06 3.5428e+07 -3.11521e+08 1.63583e+07 1.20231e+07 2.01108e+07 3.16927e+07 1.17571e+07 -3.92246e+08 -9.87055e+07 2.73468e+07 3.87766e+07 4.36959e+07 -3.39177e+08 -2.28582e+07 -2.05668e+07 -3.58023e+08 -2.92196e+08 -1.16965e+08 -3.12099e+08 -8.60271e+07 -1.00739e+07 1.27623e+08 -2.41185e+07 -3.15169e+07 -2.36912e+07 -3.64797e+08 8.62061e+07 -5.89552e+06 -2.02633e+07 -1.34442e+07 1.35435e+08 -3.7207e+07 -2.86287e+07 -2.557e+07 -1.9973e+07 -3.59301e+08 -2.48301e+07 -1.67673e+07 -2.98699e+08 -8.77457e+07 -3.98413e+07 -5.42553e+07 -9.78185e+07 6.69409e+07 3.71049e+07 7.20205e+07 7.27583e+07 -1.69313e+08 4.06894e+07 6.95602e+07 4.11393e+07 -2.83853e+08 8.54455e+06 8.22291e+07 7.47399e+07 9.46155e+07 9.68009e+07 -1.62142e+07 -8.02807e+06 4.39583e+06 2.51375e+08 1.83417e+08 -2.92125e+07 -1.08656e+07 3.9067e+06 3.94199e+06 5.78967e+06 9.57476e+07 -1.85606e+08 4.04088e+07 1.62319e+07 1.96946e+07 -4.24044e+07 2.2357e+07 1.54011e+07 1.81158e+07 -2.04957e+07 -5.2892e+07 -2.50333e+07 1.28148e+08 7.74607e+06 -1.18729e+07 -5.62646e+06 1.56196e+07 921823 -6.20215e+06 -1.2525e+07 8.21603e+06 -1.59581e+07 -5.21868e+06 -1.06907e+07 -6.67085e+06 4.23269e+07 1.99286e+07 -1.07843e+07 -2.5035e+07 -2.92842e+07 -2.17097e+07 -1.11868e+06 1.7376e+06 -1.12618e+07 -1.37614e+07 -9.50021e+06 -2.05664e+07 -6.15668e+06 118749 3.69085e+06 4.44888e+06 -1.95893e+06 -9.36167e+06 -783755 1.06548e+07 3.91991e+06 3.3491e+06 4.08628e+07 -6.93818e+07 -4.05334e+06 2.11866e+06 -3.31134e+06 389053 -6.43233e+06 9.64522e+06 1.36407e+07 -3.78002e+06 -6.80005e+06 -6.78642e+06 -2.57739e+06 2.87391e+07 -5.80805e+06 -8.48267e+06 -1.37125e+07 -6.80305e+06 -5.49235e+06 -1.09684e+06 868089 -78207.4 8.17899e+06 -2.415e+06 1.19314e+07 5.94435e+06 2.9594e+06 -1.31249e+07 -1.86754e+06 -5.83282e+06 4.24852e+06 4.0932e+07 1.9978e+07 1.29902e+07 1.53659e+07 3.26822e+07 8.51233e+07 -1.01193e+07 -7.53035e+06 -2.11356e+06 3.73009e+06 -1.16973e+07 1.55762e+06 6.89694e+07 2.47644e+06 8.51156e+06 5.00393e+06 1.03688e+06 -6.1303e+06 -5.72915e+06 1.0029e+07 -9.81663e+06 1.1773e+07 8.70929e+06 -8.64516e+06 -1.21981e+07 6.57996e+06 1.35222e+07 -4.73164e+06 3.98125e+06 -1.34262e+07 7.83234e+06 -7.01866e+06 6.90595e+07 7.4061e+06 -2.03828e+06 -3.191e+06 1.14808e+07 5.73413e+07 -7.09686e+07 8.48478e+06 3.9712e+06 -5.81872e+06 1.71329e+07 4.55329e+07 1.4196e+07 -1.12917e+06 -2.0008e+06 1.32279e+06 1.85365e+06 -3.73072e+06 1.58944e+07 2.08817e+06 -887734 -1.52198e+07 5.9273e+06 -6.18901e+06 2.55482e+06 -3.6833e+07 -2.10064e+07 1.41291e+07 4.21035e+07 1.03055e+06 -8.85371e+07 -9.93793e+06 -2.27428e+07 -1.21012e+07 -9.52012e+06 -8.90439e+06 -5.61977e+07 -1.53611e+07 -5.12029e+06 -1.46143e+07 -5.83964e+06 -6.64409e+07 -1.54655e+07 -6.95539e+06 -2.0013e+07 -5.11265e+06 -2.87472e+07 -1.68727e+07 -2.72804e+06 1.14067e+07 3.31684e+07 -4.10994e+07 -225300 587012 -6.50299e+06 1.40158e+06 -364058 2.32006e+07 -3.22715e+07 -3.24206e+07 405297 1.05859e+07 -2.49309e+07 7.12219e+06 1.0507e+07 1.36935e+07 6.62522e+06 -2.61219e+07 1.19372e+07 3.41348e+07 4.41807e+06 7.50269e+06 8.80033e+06 2.51635e+06 1.21608e+07 -3.59919e+07 8.67611e+06 2.44118e+07 1.84375e+07 1.37104e+07 5.65614e+06 2.74029e+06 -1.14368e+07 -3.49066e+06 -3.75443e+07 311282 -2.94309e+07 1.96339e+06 -1.57623e+06 2.09934e+07 -3.06232e+07 549885 -2.93055e+07 -8.51428e+06 1.04485e+07 -4.3332e+07 1.05084e+07 1.82631e+07 1.91309e+07 9.0806e+06 -3.10107e+07 3.67094e+07 8.35395e+06 8.16778e+06 9.09625e+06 6.87903e+06 -4.1432e+07 1.21521e+07 2.31375e+07 2.23957e+07 1.31438e+07 3.89955e+06 1.6542e+07 -3.36152e+06 -2.37256e+07 -2.33107e+07 5.10183e+06 -3.67896e+06 733661 8.74884e+06 -6.08212e+06 -4.85829e+06 -2.23272e+06 2.58027e+07 -1.09018e+07 -6.62209e+06 -7.85533e+06 -5.54609e+06 3.26182e+07 -1.90445e+07 -6.22718e+06 -7.4118e+06 -1.08496e+07 -2.47905e+06 -4.90386e+06 1.78156e+07 -9.44223e+06 -9.28321e+06 -9.51427e+06 -6.59294e+06 -2.36737e+06 1.34561e+06 -1.95797e+06 6.78838e+06 -4.98211e+06 -9.56805e+06 -423008 1.47198e+07 -537467 805660 -1.07225e+07 -9.19716e+06 3.91474e+06 -1.79868e+07 -1.35142e+06 -806251 322860 -1.53996e+07 -318434 -2.99477e+06 -3.36542e+06 507641 -1.73764e+07 -1.35062e+06 -1.35354e+07 -3.31434e+06 -8.36652e+06 -588619 -227571 -2.91595e+06 3.75649e+06 -45991.2 6.0447e+06 -844805 2.78049e+07 1.43671e+06 -1.38114e+07 -2.01135e+07 -1.27744e+07 6.16053e+07 1.76326e+07 -1.25319e+06 2.22112e+06 1.52977e+07 9.7541e+07 -7.01295e+06 -1.76883e+06 1.20241e+07 3.32582e+06 1.57545e+06 2.23686e+06 1.49562e+07 -4.57451e+06 -2.79667e+06 -1.36624e+07 -5.7618e+06 5.30388e+07 1.82068e+07 1.05928e+07 3.54983e+06 3.09645e+07 5.1668e+06 -2.74681e+06 -6.31939e+06 5.58208e+06 2.52051e+07 -5.93742e+06 -2.677e+07 871235 1.56408e+07 7.17509e+07 8.59524e+06 2.05676e+06 4.94451e+06 3.96714e+06 1.10122e+08 -1.11415e+08 5.92348e+06 9.90249e+06 -907154 1.9685e+07 8.1178e+07 2.22495e+07 1.28625e+06 -822710 2.94649e+07 1.14613e+06 4.89345e+06 -675284 -3.78001e+06 -1.64628e+06 1.12912e+06 511891 -4.12865e+07 -1.51434e+07 1.55482e+07 4.10706e+07 2.50182e+06 -2.12989e+07 -1.29033e+07 -3.07133e+07 -1.70158e+07 -6.80573e+06 -2.41505e+06 -1.99793e+06 -1.69892e+07 -2.80325e+06 -6.37026e+06 -9.04019e+06 -5.99042e+06 -1.74612e+07 -7.47515e+06 -2.24468e+07 -6.52138e+06 -5.07185e+06 -5.92261e+06 433225 4.24972e+06 6.33253e+06 -3.63451e+07 1.87019e+06 -1.48788e+06 -3.13301e+07 2.35697e+06 3.6648e+06 2.35159e+07 -3.97847e+06 -2.87602e+07 -3.13862e+07 1.00798e+07 -3.15977e+07 9.13301e+06 1.86341e+07 8.92918e+06 1.7404e+07 882635 1.11253e+07 -2.14313e+06 3.85131e+06 6.22461e+06 8.07868e+06 125043 1.89708e+07 4.20066e+06 -2.48252e+07 -2.61801e+07 7.70739e+06 -2.25996e+07 7.16382e+06 1.98811e+07 1.80836e+07 1.03256e+07 -4.11075e+07 1.49259e+06 -5.99191e+06 899281 2.74427e+07 -3.04066e+07 -3.73901e+07 2.57075e+06 -2.65882e+06 -1.85293e+07 3.08723e+06 7.29481e+06 9.23135e+06 4.36855e+06 2.87169e+06 -9.37742e+06 4.43965e+06 4.41897e+06 6.47174e+06 2.14157e+06 -2.59176e+07 5.66077e+06 1.88695e+07 1.39877e+07 8.61539e+06 6.60246e+06 1.33584e+06 -9.97698e+06 -4.02018e+06 -1.46108e+06 -2.27348e+07 878713 -861928 967282 -1.9267e+07 -8.64221e+06 6.41347e+06 1.93205e+07 5.00644e+06 -8.4363e+07 1.99186e+06 -3.87704e+06 -2.06142e+06 1.03971e+07 4.70359e+06 -7.11192e+07 -7.10847e+06 -1.40361e+07 -6.8448e+06 -1.40783e+07 -7.16842e+07 -7.17953e+06 -1.9472e+07 -5.86287e+06 -1.59172e+07 -1.25748e+07 -6.09758e+06 -383297 7.05856e+06 1.2366e+07 -2.21828e+07 1.28969e+06 -1.29293e+07 178048 43651.5 2.26825e+06 3.57481e+07 -3.38066e+07 -2.60179e+07 -1.0337e+06 1.54749e+07 6.47403e+07 5.50255e+06 6.00566e+06 9.80477e+06 4.33525e+06 1.04273e+08 4.08904e+06 -1.22244e+08 4.94618e+06 7.61036e+06 6.29246e+06 5.17862e+06 6.14271e+06 7.82626e+07 1.02548e+07 1.89577e+07 1.24668e+07 2.98217e+07 2.77857e+06 1.71672e+06 810437 -6.17603e+06 3.01056e+07 -2.8702e+07 7.39445e+06 -2.89203e+07 1.38701e+08 -1.34472e+08 4.5479e+06 8.46697e+06 1.13115e+07 4.03319e+06 2.56611e+06 2.86886e+06 1.32913e+08 7.56427e+06 1.05742e+07 8.86447e+06 8.61836e+06 -3.55953e+06 2.12831e+07 1.71425e+06 -1.63543e+07 -1.63245e+07 1.25428e+08 1.20608e+07 1.81397e+07 2.53793e+07 1.02254e+07 -56784 4.92469e+06 1.12451e+07 -2.69723e+07 1.17461e+06 3.80752e+07 1.26098e+07 8.90756e+06 3.68643e+06 535659 1.18353e+07 5.21346e+06 2.85187e+07 3.15069e+06 1.0799e+07 1.41123e+07 4.85176e+06 1.00046e+07 4.39453e+07 2.05435e+06 2.2529e+07 -2.17235e+06 1.85777e+07 3.4725e+06 3.54826e+06 -922070 -8.72613e+06 -1.70505e+07 2.10756e+07 -1.08153e+06 -5.7679e+06 -1.90434e+06 -500403 1.03003e+07 -3.78551e+06 -2.38989e+07 -350361 4.24898e+07 1.31703e+07 5.9499e+06 5.35011e+06 1.28706e+07 -1.09636e+07 1.75638e+07 3.63918e+07 2.41501e+06 1.85819e+07 1.92068e+07 2.58019e+06 4.50038e+07 4.71584e+06 2.12402e+07 2.05504e+06 2.06901e+07 6.52279e+06 3.56236e+06 -2.32243e+06 6.14125e+06 -1.97474e+07 -1.68481e+06 -1.41735e+07 7.30326e+06 1.82699e+07 -5.33203e+06 -2.07096e+06 -7.4889e+07 -5.62244e+06 -6.06608e+06 -6.78921e+06 -4.30263e+06 -1.2627e+08 1.04819e+06 -5.73216e+06 2.58528e+06 -2.13324e+06 1.93365e+06 1.98401e+06 -1.10709e+08 -5.97458e+06 -1.69266e+07 -1.1867e+07 -1.89278e+06 -6.77953e+06 973805 3.98008e+06 5.44892e+06 -1.75988e+07 1.8143e+07 4.22151e+06 1.59555e+07 -1.30666e+08 -3.4475e+06 -2.29652e+06 2.91603e+06 -7.21735e+06 -5.56977e+06 243301 -9.52455e+07 -5.13474e+06 -1.10863e+07 -1.03507e+07 302118 -127983 -7.22751e+06 -6.30088e+06 1.34941e+06 1.10666e+07 -1.14292e+08 -4.18193e+06 -1.14788e+07 -6.25959e+06 -9.67991e+06 4.83969e+06 -3.11815e+07 -1.04424e+06 -1.50327e+07 762539 -2.3733e+07 -8.75712e+06 7.28738e+06 2.50638e+07 1.10251e+06 -1.17932e+08 -2.20171e+06 -3.76349e+06 -9.25399e+06 1.52361e+06 -9.98949e+06 -7.69533e+07 3.73474e+06 -1.18558e+07 -1.50807e+07 -6.40782e+06 -1.20097e+08 -9.4884e+06 -2.75353e+07 -1.56025e+07 -2.35939e+07 -2.90332e+06 722946 -4.45864e+06 -4.64809e+06 1.09091e+07 -2.58983e+07 6.95616e+06 -1.17111e+06 -1.95181e+06 2.46582e+06 3.3633e+07 -1.05334e+06 -2.51698e+07 -4.15944e+07 7.86868e+06 9.50301e+07 2.01925e+07 1.05478e+07 1.201e+07 1.62975e+07 4.39389e+07 9.82171e+06 -5.02251e+07 1.05529e+07 9.77538e+06 9.78362e+06 5.91934e+06 2.13596e+07 -4.46997e+06 -1.55293e+07 -2.0344e+07 3.98679e+06 1.67796e+07 2.8261e+07 2.74285e+07 2.64613e+07 2.04953e+07 3.69463e+07 -3.26675e+07 -2.90466e+07 1.17955e+06 1.37279e+08 -6.02797e+07 4.72863e+06 7.30379e+06 1.08884e+07 8.88372e+06 1.05839e+07 3.58404e+07 1.51851e+07 1.1917e+07 1.89609e+07 6.88872e+06 1.49921e+08 1.89208e+07 2.59046e+07 1.95244e+07 3.1974e+07 8.03053e+06 1.84676e+06 -2.21804e+06 -9.00366e+06 2.85391e+06 -255684 9.35149e+06 -5.15621e+06 -1.26296e+07 3.07227e+06 1.43205e+07 2.26668e+06 2.67253e+06 -2.13451e+06 8.47594e+06 1.54714e+07 1.47572e+07 412520 1.64159e+07 886088 1.58382e+07 3.57813e+06 6.14991e+06 3.6869e+06 -1.66259e+07 1.03127e+07 2.96959e+06 767546 1.31856e+07 -1.50097e+06 1.33989e+07 1.03905e+07 -1.66837e+07 6.07211e+06 579247 -987433 726217 1.76854e+06 1.5497e+07 -7.63192e+06 4.22959e+06 -1.17982e+07 3.04601e+06 -2.78066e+06 -3.50517e+06 4.08895e+06 -7.21262e+06 -8.64206e+06 -5.45459e+06 2.58304e+06 3.56187e+06 -958588 -8.96179e+06 -8.34366e+06 8.78672e+06 -9.57073e+06 6.58196e+06 -1.37321e+06 2.15143e+06 2.30837e+06 -1.85669e+06 7.27964e+06 -1.93385e+06 -4.02981e+06 8.90087e+06 2.94665e+06 6.93414e+06 -5.6961e+07 -1.03194e+06 -5.47773e+06 -4.66639e+06 -3.65389e+06 -5.074e+07 1.02796e+07 -2.94473e+06 -471461 -956002 -4.11428e+06 -7.3924e+06 1.34802e+06 1.73146e+07 7.75425e+06 5.4754e+06 -3.8104e+07 162805 475491 -1.29195e+06 -1.42966e+06 5.40259e+06 -1.20765e+07 196826 -2.0193e+06 -2.39801e+07 2.12375e+06 1.08132e+07 2.89718e+06 8.27557e+06 -885038 6.14696e+06 1.77399e+06 2.03267e+06 2.23239e+06 1.8077e+06 -1.28572e+07 3.25287e+06 796441 9.53966e+06 1.20003e+06 713077 604846 57669.9 -1.00906e+06 2.82861e+07 -2.54177e+07 4.31507e+06 -1.75781e+07 5.73538e+06 1.14931e+07 8.48873e+06 -1.0507e+07 244136 -7.40392e+06 -2.09197e+07 4.07294e+06 5.72384e+06 -4.16412e+07 -1.80223e+07 -1.16839e+07 2.96105e+07 -2.24621e+07 -1.25279e+07 1.26732e+07 3.6527e+06 5.44467e+06 1.16664e+07 473376 -1.06618e+06 7.65378e+07 -1.33002e+06 1.98477e+07 1.97699e+06 -160038 -851736 -3.03184e+06 -2.23618e+07 -2.33049e+07 -1.8865e+07 1.35156e+06 -6.37045e+07 -264586 -2.08309e+07 -7.39393e+07 1.00387e+06 -1.15711e+07 -7.6847e+06 5.15489e+06 8.7182e+06 -321959 -3.0692e+06 481781 -3.96525e+06 -1.70224e+07 -1.78425e+07 -1.88273e+07 -1.0178e+07 4.53977e+07 4.36047e+06 -1.80649e+06 778833 -1.54028e+06 91286.2 -1.61471e+07 -1.71628e+07 -1.88421e+07 -2.01384e+07 -4.36467e+07 7.56096e+06 -6.36449e+07 1.24918e+07 -2.11082e+06 -1.39721e+06 1.87649e+06 1.34716e+06 -1.60697e+07 -1.76211e+07 -1.92995e+07 -209579 -4.93689e+07 -6.16461e+06 9.51628e+06 3.10941e+07 1.54136e+07 2.41156e+07 -1.73342e+06 -1.89185e+07 -2.12091e+06 9.42419e+06 1.36854e+06 4.15129e+07 4.49146e+07 9.1742e+06 9.68904e+06 3.30856e+07 9.63714e+06 1.07134e+07 1.38928e+07 1.35304e+07 1.84685e+07 -1.86581e+07 577974 -2.89628e+06 -7.69617e+06 -5.16895e+07 8.99018e+06 2.9211e+07 1.95031e+07 2.7915e+07 -563731 -1.75503e+07 1.05652e+06 4.25089e+07 5.05499e+07 -8.64273e+06 1.08113e+07 1.10424e+07 -7.6637e+06 4.16912e+07 7.13052e+06 1.14961e+07 1.70271e+07 2.42554e+07 2.51766e+07 -1.8128e+07 376103 355859 8.12609e+07 -5.37899e+06 4.5615e+06 2.93461e+06 -3.08812e+06 2.81379e+06 -1.78157e+07 -1.97783e+07 -1.89467e+07 5.16265e+06 -7.63968e+07 -1.03306e+07 -3.97154e+07 -2.67828e+06 -161512 -373416 1.54787e+06 2.95838e+06 -1.0876e+07 -8.97871e+06 -1.49708e+07 -2.76388e+06 -1.01413e+07 -1.59893e+07 5.14748e+06 2.30812e+07 1.47209e+07 2.30339e+07 -2.17676e+07 -4.0613e+06 -4.5328e+06 7.58241e+06 4.5448e+06 -2.92807e+06 4.42383e+07 1.95601e+06 2.69846e+07 4.92123e+06 317044 1.54743e+07 2.36881e+07 2.32478e+07 -2.11784e+07 -2.45396e+06 -3.86651e+06 4.76755e+06 794229 -924623 1.23677e+06 2.20593e+07 9.12636e+06 1.60453e+07 -5.5234e+06 -2.10528e+07 -4.2216e+06 -1.95744e+07 4.72831e+07 389264 3.31273e+06 -2.4049e+06 -2.38072e+06 7.5635e+06 8.89925e+06 1.18071e+07 -2.06699e+07 -257158 -4.52245e+06 1.07268e+08 -2.91057e+07 2.86912e+06 6.95747e+06 1.28725e+07 3.26771e+06 8.81793e+06 -3.5342e+07 -2.42724e+07 -2.50305e+07 -7.89347e+06 -4.30061e+07 -1.82434e+06 2.54128e+07 3.41236e+07 -9.06127e+07 -1.11331e+07 -3.30249e+06 -6.21096e+06 4.88352e+06 4.56504e+06 5.74836e+06 -8.75949e+06 -6.67931e+06 -1.43461e+07 1.15259e+08 -1.33225e+06 -2.853e+07 1.44964e+07 3.40429e+06 1.31023e+07 -2.95698e+07 -2.77247e+07 -3.07741e+07 4.76833e+06 -9.8498e+07 -1.27859e+07 3.1715e+07 -1.25995e+08 3.02436e+07 3.61583e+06 -1.13707e+06 -211197 4.74648e+06 1.20302e+07 1.27101e+07 -2.78704e+07 -2.71805e+07 -2.88772e+07 -8.28328e+07 3.57182e+07 -4.44767e+06 5.87547e+06 -3.07378e+06 -7.06519e+06 -3.26045e+06 1.94945e+07 1.52608e+07 1.83417e+07 6.66824e+06 5.83073e+07 4.3258e+06 -2.7414e+07 -3.68171e+07 9.76454e+07 4.23103e+06 7.03976e+06 4.01386e+06 -9.56208e+06 -2.66651e+06 -3.63756e+06 1.14013e+07 1.20101e+07 1.60281e+07 -8.50831e+07 8.23271e+06 3.30435e+07 -3.97339e+06 -9.18555e+06 -2.84499e+06 1.51987e+07 2.3245e+07 2.2562e+07 -4.18376e+06 7.14998e+07 7.51985e+06 -3.72289e+07 1.0199e+08 -3.03279e+07 -4.78189e+06 2.07318e+06 1.40287e+07 -6.03577e+06 -294022 -1.79071e+06 1.04945e+07 1.36096e+07 1.67973e+07 1.08579e+07 1.1325e+08 -2.31462e+07 -3.56609e+07 5.5702e+06 1.36939e+07 1.13476e+07 -3.15295e+07 -2.06296e+07 -1.71163e+07 -9.38609e+07 -1.30086e+07 2.19663e+07 206332 4.07108e+07 -5.7829e+07 -1.17513e+07 7.71891e+06 1.16583e+07 1.15316e+07 -2.35789e+07 -2.83284e+07 -2.59156e+07 -1.31976e+07 -2.32692e+07 1.32938e+08 -5.9216e+07 -1.20163e+07 1.78636e+07 -890579 1.3452e+07 -3.28459e+07 -4.25785e+07 -2.42219e+07 -1.67692e+06 -8.86252e+07 8.25987e+07 -1.20727e+08 1.21516e+07 4.52464e+07 -676931 4.87495e+06 7.50934e+06 -2.80263e+07 -1.13948e+07 -1.91383e+07 -1.4841e+07 -2.28609e+07 1.51599e+06 7.08634e+06 2.03512e+06 5.77182e+06 5.03486e+06 2.17105e+06 8.08575e+06 8.47259e+06 4.54458e+07 5.81428e+06 -1.09082e+07 5.87593e+06 188702 2.50819e+07 -111700 -6.18958e+06 2.91535e+06 3.29146e+06 5.39573e+06 1.25005e+07 1.26384e+07 2.66948e+06 1.1797e+06 8.44189e+06 -7.62351e+06 3.55341e+06 9.64914e+06 -858077 7.71991e+06 2.33409e+06 -3.89095e+06 251501 -1.71916e+06 -8.80785e+06 1.15197e+07 198833 986421 9.62609e+06 -964749 3.30927e+06 4.94735e+06 -3.3277e+06 659305 381417 -2.9774e+07 -726306 -3.38555e+07 5.09637e+07 -2.44883e+06 -658138 4.01547e+07 631943 4.57359e+07 191786 -27880.4 1.27122e+07 3.34165e+07 121129 -2.98009e+07 120209 6.76629e+07 -1.3168e+06 -220527 3.69777e+07 397805 7.68869e+06 -5.31795e+07 -1.46484e+06 -9.14542e+06 7.17014e+06 1.37386e+06 2.69547e+07 -1.23984e+07 4.70717e+07 889067 -3.08534e+06 -5.86054e+07 5.31984e+07 212309 3.54306e+07 -2.09776e+06 -2.6503e+07 -4.02378e+07 -1.76414e+06 1.23545e+07 3.9759e+07 -3.59946e+07 918329 1.51085e+07 699368 6.7535e+06 -3.09751e+07 931123 -3.43804e+07 -2.40104e+06 5.12709e+07 -2.73571e+06 3.17434e+07 1.39921e+06 1.19863e+07 3.44032e+07 -3.14856e+06 -3.1368e+07 3.7227e+07 -1.15861e+06 1.51494e+06 -3.12567e+06 5.15245e+07 486680 -1.15443e+06 -3.42323e+06 4.33567e+07 -5.03192e+07 -596530 -4.06824e+07 3.38398e+06 -4.1738e+06 3.12012e+07 -4.92079e+07 -4.75266e+06 -2.89714e+06 4.04338e+07 -3.89688e+06 -3.03606e+07 -3.45609e+07 1.34224e+07 3.3919e+07 -3.1506e+07 3.0329e+07 3.24194e+06 -2.9944e+06 3.37779e+07 1.7964e+06 -1.75379e+06 -3.73892e+06 4.36462e+07 -3.33342e+06 6.27795e+06 -3.60642e+07 2.38002e+07 -1.01478e+06 3.78377e+07 -1.43556e+06 -7.30169e+06 971066 -5.95973e+06 3.7545e+06 -1.05693e+06 1.94362e+06 4.65309e+06 274565 1.1732e+07 -1.07514e+06 48127.1 2.12107e+06 1.69583e+07 -932440 -1.58456e+07 -3.30177e+06 9.25379e+06 -606631 -1.58188e+06 -4.80408e+06 6.5757e+06 -562525 -3.29285e+06 -6.34924e+06 -382918 750662 -1.60728e+06 -1.69909e+07 1.27664e+06 825332 -136023 3.23656e+06 -4.74717e+06 -26693.4 2.26524e+06 -1.92463e+06 2.74685e+06 -2.01039e+06 -5.00503e+06 -532634 4.86966e+06 1.14625e+07 -1.32453e+07 442493 -851895 -113190 2.76308e+06 1.47577e+06 -1.30368e+07 1.45021e+07 -1.15698e+07 1.3356e+07 -6.64211e+06 -1.50443e+06 -9.06529e+06 7.10777e+06 291779 -723055 6.86601e+06 1.06805e+07 4.29484e+06 1.80896e+07 -427201 -1.86215e+07 1.32552e+07 2.18036e+06 -4.83382e+06 -3.11107e+06 1.18413e+07 1.08902e+06 1.41033e+06 8.59273e+06 -2.23155e+06 3.64181e+06 -1.06805e+07 2.90346e+06 -8.84098e+06 1.48933e+07 1.12373e+06 7.12592e+06 1.32876e+07 -1.07283e+07 -1.50553e+06 5.45934e+06 2.79874e+06 -4.96897e+06 -9.98748e+06 2.085e+07 1.88065e+07 1.33881e+07 1.28143e+07 -759689 -6.34366e+06 -9.87421e+06 8.01531e+06 1.46028e+07 -1.56997e+07 7.27486e+06 1.43943e+06 -1.44062e+06 9.88527e+06 -746158 309930 -966837 -3.41747e+06 -8.68057e+06 9.02559e+06 -532596 -996764 -1.02226e+07 3.00218e+06 -1.40155e+07 1.28093e+07 -2.24974e+06 -370210 6.47404e+06 1.6023e+06 -7.00674e+06 5.31712e+06 3.33072e+06 1.32565e+07 8.24087e+06 -3.84154e+06 -1.20521e+07 1.74489e+06 -1.9669e+07 1.78887e+06 -181700 -1.19582e+07 -78868.2 -2.17508e+06 2.6015e+06 -2.8645e+06 3.88894e+06 -3.05569e+06 -864684 -2.01882e+06 1.98394e+06 -1.07186e+06 3.26928e+07 -3.35763e+06 -6.80448e+06 -6.63764e+06 -4.24384e+06 -3.47002e+06 1.1101e+07 1.32015e+07 -1.09355e+07 -962210 3.92969e+06 -8.77598e+06 7.07587e+06 -1.49687e+06 -1.49558e+06 1.96692e+06 -5.70436e+06 709271 2.89647e+06 1.6506e+06 6.05637e+06 1.17881e+06 8.60515e+06 -2.54462e+06 1.25565e+06 -5.51566e+06 -4.51443e+06 2.03961e+06 595181 -4.66591e+06 1.02925e+06 -3.83298e+06 985375 -7.9349e+06 4.76169e+06 -1.82648e+07 6.07567e+06 -1.18352e+07 -1.72141e+07 -20284.8 374333 13525.2 -1.40668e+07 1.46167e+07 -2.17478e+06 -1.47571e+07 -2.82041e+07 -2.2914e+06 -690317 841774 9.09661e+06 -1.11515e+06 -8.21791e+06 -3.40555e+06 3.5899e+06 2.98995e+06 1.21029e+06 -1.2495e+06 -2.48659e+06 477251 1.06991e+07 -6.90687e+06 1.46743e+07 2.66995e+06 -1.84832e+07 1.14467e+06 -1.47832e+07 -1.50728e+06 -1.77057e+07 4.50229e+06 1.1287e+07 -1.52188e+07 797222 -6.09944e+06 1.50259e+07 -5.70682e+06 -2.36974e+07 3.04815e+06 2.33387e+06 2.50436e+07 8.73358e+06 1.09051e+07 -2.97815e+07 -4.95921e+06 891162 -2.44393e+06 7.17048e+06 -1.40657e+06 7.33145e+06 2.04505e+06 -29576 -4.2033e+06 -1.32072e+06 4.28855e+06 -2.0599e+07 -1.13735e+07 -1.83729e+07 5.5262e+06 2.87604e+07 5.49146e+06 -1.70946e+06 3.87702e+07 -2.67697e+07 -5.62684e+06 2.30002e+07 6.55054e+06 1.41255e+07 -4.44564e+06 -2.49927e+07 -1.59589e+06 -2.19283e+07 -1.2255e+06 -1.66017e+07 1.44011e+06 3.18427e+06 -7.51229e+06 5.35551e+06 -666486 -1.91867e+07 2.87265e+06 -2.91043e+06 -2.19319e+07 2.24727e+07 2.93702e+06 2.22616e+07 2.15954e+06 -475240 -1.49718e+07 2.46776e+07 -1.9367e+06 889515 -1.67788e+07 2.08283e+07 8.98646e+06 -4.39603e+06 -94694.3 -1.45199e+07 2.06797e+06 2.42336e+06 -2.1313e+07 -1.69717e+06 2.66077e+06 -1.39047e+07 -4.00812e+06 -3.43987e+06 9.30423e+06 -2.26468e+07 1.70219e+07 -9.6327e+06 -7.24175e+06 -1.40298e+07 3.59512e+06 -2.29786e+06 -3.38286e+07 -3.60582e+07 5.32726e+07 2.60078e+06 3.6155e+07 711937 4.15307e+07 314657 1.19284e+07 3.72333e+07 2.37858e+06 -3.12984e+07 5.37043e+07 1.55489e+06 1.44443e+06 2.69583e+06 3.36515e+07 -2.19694e+06 -4.92877e+07 -3.27262e+07 -2.20494e+06 -4.68806e+07 -3.92043e+06 -3.06136e+06 4.35097e+07 -3.64439e+07 -1.86499e+06 2.76449e+06 -3.09182e+07 -3.42605e+07 1.2567e+07 3.42529e+07 -3.05214e+07 2.98923e+07 -2.54066e+06 3.2577e+07 -1.43678e+06 -182961 -2.91361e+07 -3.48126e+07 8.74549e+06 2.41172e+07 360620 -2.62089e+07 1.31899e+06 -1.61814e+06 -9.25033e+06 1.05029e+06 490799 -3.3024e+07 -191802 -2.28493e+07 -2.52027e+07 252064 585454 -615001 -2.8995e+07 571065 -1.57292e+07 -3.78021e+06 -1.66163e+06 2.57898e+07 3.84343e+06 1.62349e+06 -947455 4.20741e+06 -1.10874e+07 2.8952e+06 -619092 -2.29035e+07 2.09663e+07 -592625 -1796.51 -1.31258e+07 74186.5 -1.68103e+07 8.52471e+06 2.8517e+07 -2.91555e+06 -2.04295e+07 -3.00951e+07 522050 -893584 2.19312e+06 -5.71129e+06 1.8216e+06 -3.31646e+07 -3.04553e+07 6.18856e+06 -1.4673e+06 2.6662e+07 3.40004e+06 -2.35835e+07 380871 -7.60175e+06 -2.80088e+07 -2.53572e+07 2.76195e+06 -3.02681e+07 430500 1.93532e+06 -2.57736e+07 1.65774e+06 3.23313e+06 -1.5721e+07 2.21386e+07 -3.08179e+06 1.55673e+07 -3.25255e+06 4.04943e+06 -1.1583e+07 2.09413e+07 4.20276e+06 -2.29543e+06 -1.07331e+07 6.02575e+06 2.52252e+07 4.35953e+06 -2.46798e+07 -2.47275e+07 -2.76802e+06 -5.55268e+06 2.98916e+06 -2.15547e+06 -2.38558e+07 2.37684e+06 4.85978e+06 -1.50002e+06 -1.69791e+07 1.81032e+07 827049 -1.10487e+07 -1.16338e+07 5.86712e+06 977056 1.20486e+07 -1.78567e+06 -2.81338e+07 60441.8 262783 -1.18151e+06 6.33563e+06 -9.75258e+06 -1.93347e+06 -1.57412e+06 -3.44295e+06 -3.23101e+06 1.51928e+06 -391937 -9.92673e+06 781936 -1.03894e+06 -3.81049e+07 -2.6855e+06 3.09632e+07 1.77689e+06 5.05988e+06 1.94828e+07 -1.66187e+06 9.11585e+06 5.82334e+06 -796794 1.25129e+07 5.95064e+06 -9.9437e+06 -3.18706e+06 438537 3.01924e+06 -6.26946e+06 -1.53029e+06 -3.27594e+07 -811296 7.43324e+06 1.29706e+07 -5.15297e+06 -309952 -446924 -4.05708e+06 2.61995e+06 846999 -354021 4.34309e+06 -1.43027e+06 2.90564e+06 -1.97539e+06 9.25628e+06 -2.48687e+06 -547749 -8.64888e+06 4.22756e+06 -1.79553e+06 -1.85971e+06 1.66185e+06 1.72386e+06 -1.86627e+06 1.37711e+06 -8.06488e+06 -4.0493e+06 1.09112e+06 -5.05106e+06 -3.76779e+06 -997634 -610721 3.48876e+06 -1.95547e+06 -6.16657e+06 -1.53086e+07 -8.53247e+06 -3.16389e+06 6.00135e+06 -2.14564e+06 5.15829e+06 -3.54349e+06 -5.51638e+06 208172 2.09862e+06 -598149 537011 2.30838e+07 2.18485e+07 -9.34994e+06 5.85438e+06 -2.21832e+07 -1.17564e+07 -2.16657e+07 4.23091e+06 8.60849e+06 6.76471e+06 -2.62017e+07 -6.82067e+06 3.49412e+06 -2.12831e+07 -1.91633e+06 2.76431e+06 3.67199e+06 -1.79143e+07 -7.18052e+06 -1.67515e+07 1.49731e+06 1.90928e+07 -2.20768e+06 1.60216e+06 1.94308e+07 1.83143e+06 1.95795e+07 2.17178e+07 4.41658e+06 1.64371e+07 -5.75713e+06 -1.68414e+07 5.21869e+06 -2.11877e+06 -2.14679e+07 -1.43861e+07 -3.26771e+06 -2.03927e+07 -4.85512e+06 1.84735e+06 2.45185e+07 8.05347e+06 3.57265e+06 4.28776e+06 2.56447e+07 379052 1.73003e+07 -2.72055e+07 5.88889e+06 -696119 -2.85346e+07 -2.77312e+07 -784389 -6.56973e+06 -1.97906e+07 148366 7.08541e+06 -1.4319e+06 -3.3187e+07 -3.34703e+06 -8.80257e+06 3.85973e+06 -1.6593e+07 2.24631e+07 -1.14607e+07 4.71341e+06 -3.26976e+06 -2.44735e+07 1.38811e+07 4.13516e+06 -6.25252e+06 -3.11799e+07 3.6774e+07 1.17157e+07 -2.96417e+07 -2.39774e+07 1.94907e+07 7.31297e+06 9.49181e+06 -8.7966e+06 -8.21275e+06 5.48614e+06 764095 -1.15242e+07 4.24681e+06 -6.06991e+06 -481648 -876407 -2.16737e+07 -2.90912e+07 3.9101e+07 -4.78863e+06 3.07254e+07 794517 4.20093e+07 577874 1.1663e+07 2.79043e+07 791062 -2.8808e+07 4.98428e+07 319996 -588233 6.142e+06 350475 7.81416e+06 -2.63049e+07 -1.21201e+06 -2.37034e+06 -2.87627e+06 -8.16116e+06 3.08108e+06 3.09066e+07 -3.44725e+07 -1.26921e+07 -1.71577e+07 -3.19723e+07 1.20373e+06 1.13429e+07 3.18721e+07 -3.44103e+07 58021.6 1.03223e+07 688417 5.67532e+06 -1.74511e+06 -3.14933e+07 -3.10828e+07 1.27877e+07 2.80615e+07 4.98356e+06 -5.67182e+06 -3.39756e+07 -2.51323e+07 -2.90917e+07 -3.48775e+06 -4.7423e+06 -1.17247e+07 3.25234e+06 -1.78025e+06 -3.209e+07 2.13628e+06 -3.61598e+06 -2.89358e+06 -3.34569e+07 4.75512e+06 -5.4234e+06 -2.28082e+07 2.99025e+07 5.00949e+06 2.20188e+07 4.87643e+06 -1.73881e+07 2.78735e+07 6.03581e+06 -1.79487e+07 -5.71022e+06 1.07814e+07 2.78576e+07 -2.42168e+07 -2.75592e+07 -9.33257e+06 3.51388e+06 -2.66197e+07 2.59513e+06 2.98766e+06 -2.82619e+07 2.65785e+07 -2.86304e+06 4.02031e+06 -2.16727e+07 4.17882e+06 -2.49904e+07 560454 -2.80045e+07 -3.3005e+07 1.20445e+07 -3.24794e+06 2.67921e+07 -3.38816e+07 3.72736e+06 1.34e+06 -1.52648e+07 -3.70765e+07 1.65746e+06 -2.0783e+07 -2.76187e+07 1.7462e+06 2.35125e+06 -4.22379e+07 813363 -2.34267e+07 -3.12619e+06 -5.21309e+06 3.38789e+07 3.26208e+06 3.82827e+06 -3.45826e+06 948467 -1.96297e+07 4.7795e+06 -3.58641e+06 -3.10557e+07 3.51738e+07 2.21269e+06 -2.89558e+07 -2.57242e+07 1.18141e+07 2.99417e+07 -1.13258e+06 -1.82102e+07 -3.24297e+07 -664089 -2.2988e+06 1.67463e+06 -5.71667e+06 -2.12455e+06 -1.2877e+07 -1.44423e+07 -323749 -5.97532e+06 -1.10255e+07 9.37924e+06 755328 6.22057e+06 426064 1.22973e+07 2.09585e+06 6.30312e+06 1.25806e+07 2.49619e+06 -1.30971e+07 1.18346e+07 727373 832849 1.25844e+06 1.01411e+07 -2.88431e+06 -1.01954e+07 -8.88179e+06 -2.00229e+06 -9.95736e+06 -9.03876e+06 -2.46964e+06 1.11809e+07 -1.00504e+07 1.49059e+06 508178 -5.81868e+06 -1.12767e+07 6.18401e+06 1.17431e+07 -1.11095e+07 6.66948e+06 -1.09271e+06 1.05913e+07 68951 551717 -1.24873e+07 -1.05868e+07 1.21611e+07 2.93276e+07 4.44521e+06 -3.35884e+07 -2.1085e+06 1.48918e+06 -3.92731e+07 3.9105e+06 -4.14344e+06 -4.02488e+07 1.99149e+06 -1.7733e+07 -1.82818e+07 -2.23368e+06 -4.10572e+06 -5.64336e+06 -4.91898e+07 4.00826e+06 -1.83949e+07 -2.52506e+06 -460588 2.96959e+07 795842 6.07157e+06 -5.12285e+06 -193395 -1.49246e+07 6.66507e+06 2.0867e+06 -3.10311e+07 4.49689e+07 -432453 2.27e+06 -3.88535e+07 4.19845e+06 -2.63381e+07 7.44875e+06 2.28451e+07 2.76301e+06 -9.67742e+06 -1.406e+07 6.33439e+06 -5.53827e+06 1.3996e+06 -8.22505e+06 1.56416e+07 -7.29589e+06 3.55983e+07 -3.69835e+07 4.91444e+06 8.52666e+06 -3.34248e+07 -2.07251e+07 -1.67602e+07 2.59742e+06 -3.72495e+07 720065 3.36239e+06 -4.85154e+07 -1.02502e+06 -3.59505e+07 2.64546e+06 -3.7496e+06 4.06123e+07 -1.24326e+06 2.57445e+07 -2.21386e+07 2.56089e+06 -1.29759e+06 -2.53624e+07 3.31114e+07 -2.81308e+06 -6.88587e+06 -2.91352e+07 3.98914e+07 806919 -2.96126e+07 -2.54566e+07 1.51132e+07 3.58454e+07 1.90151e+06 -2.16344e+07 -1.26473e+07 3.15885e+06 -1.49263e+07 -4.86987e+06 -2.16036e+06 -3.30527e+07 2.1682e+06 -4.39604e+06 2.40746e+06 -2.79297e+06 4.57936e+06 -1.72811e+06 -328141 -2.06422e+06 1.14607e+07 862027 -7.72974e+06 -417379 512453 -408253 -1.93227e+07 994380 40199.3 -3.80724e+06 2.01592e+06 260797 9.33476e+06 -3.80157e+06 4.53892e+06 -2.83523e+06 4.17329e+06 -636424 6.3421e+06 1.13488e+06 1.10666e+07 532559 -1.13405e+06 -8.24982e+06 -259782 232133 -2.05123e+06 2.957e+06 -224321 -747864 -1.55283e+07 2.7589e+06 -3.5284e+06 1.83583e+06 -2.23392e+06 545436 -1.19608e+07 1.0265e+07 -1.02891e+07 1.0545e+07 -442458 903756 4.85032e+06 -3.92433e+06 1.9215e+06 -3.2088e+06 -950450 -7.26073e+06 925197 -580412 -2.1893e+07 452229 -7.06801e+06 -741663 1.75577e+06 6.17998e+06 3.00458e+06 -1.84719e+06 2.85778e+06 -3.22322e+06 -7.30023e+06 4.36508e+06 -1.46192e+06 240175 4.70748e+06 7.51666e+06 -277709 228908 -5.75705e+06 -600092 4.77943e+06 2.69703e+06 2.38139e+06 -2.06771e+07 -1.03963e+06 5.12716e+06 1.53192e+06 -3.27426e+06 -2.80707e+06 4.63807e+06 7.78106e+06 -1.8371e+07 -1.7689e+07 4.72626e+06 2.84992e+07 -1.92477e+06 258927 7.43605e+06 470015 -986296 2.15005e+07 632376 1.54007e+07 1.28301e+07 -3.10439e+06 -2.52488e+06 730056 3.45107e+07 -2.24978e+06 1.73947e+07 696666 5.72797e+06 -2.86776e+07 -60489.9 -5.08937e+06 887903 -6.57474e+06 2.73235e+07 1.48779e+06 259956 -3.69407e+07 821892 1.1485e+06 -1.34013e+07 -1.88639e+07 -3.15917e+06 1.49678e+07 8.13317e+06 958378 3.18842e+06 761810 1.08481e+07 -1.41371e+07 -1.63819e+06 -2.18574e+07 3.19111e+07 3.8009e+06 95788.9 1.98391e+07 1.49796e+07 9.55622e+06 6.35575e+06 2.58057e+07 2.93734e+06 1.37221e+06 3.58829e+07 3.65097e+06 2.32462e+07 1.32957e+06 -2.07727e+06 -2.52757e+07 -151761 -2.39892e+07 5.61876e+06 -3.32459e+06 -2.10546e+07 3.00062e+06 2.77964e+07 -1.02852e+06 -1.93128e+06 -2.87855e+07 -1.3602e+07 -1.7584e+07 5.82199e+06 1.04043e+07 1.14548e+07 1.82556e+06 -3.19972e+06 4.23729e+06 -3.96845e+06 1.48453e+07 3.07541e+06 -3.04991e+06 -1.29493e+06 -1.85198e+07 -1.63416e+07 2.13944e+06 -3.09803e+06 -9.02384e+06 4.40483e+06 -3.95128e+06 9.95468e+06 8.02012e+06 1.49061e+07 -4.33678e+06 -3.73944e+06 2.10715e+07 211734 -1.73606e+07 1.25939e+07 1.09433e+06 -2.43794e+06 -1.68195e+07 2.0331e+07 7.52226e+06 -1.63345e+06 -1.43552e+07 -5.8687e+06 1.06923e+07 -755743 3.47123e+06 6.42678e+06 -1.96254e+07 5.33468e+06 9.91332e+06 -9.49176e+06 -2.94532e+06 1.39833e+06 1.95561e+07 -2.61265e+07 -1.2406e+07 6.25464e+06 -55094.4 3.85423e+06 2.30563e+06 -1.27576e+07 -2.5933e+07 2.92446e+07 2.31765e+07 2.99455e+06 2.56866e+06 -3.02847e+07 -2.36559e+07 -3.13015e+07 3.43854e+06 -3.76513e+06 -2.51582e+07 3.43024e+06 -7277.72 -2.94054e+07 -5.5716e+06 1.72593e+06 1.13847e+06 -3.41137e+07 3.00575e+06 1.74928e+06 -2.82152e+07 3.66752e+07 1.16004e+06 2.98862e+07 1.76727e+06 -1.91913e+07 3.7895e+07 2.72561e+06 -2.17399e+07 5.60048e+06 2.14827e+07 3.1969e+07 -2.06187e+07 -2.05089e+07 -1.8026e+07 -2.58999e+06 -3.30835e+07 -2.9805e+06 4.38263e+06 -2.28737e+07 2.9662e+07 -3.90746e+06 3.77879e+06 -1.95555e+07 9.72355e+06 -1.80221e+07 3.2619e+07 -2.42989e+06 3.08889e+07 -4.15524e+07 -1.08343e+06 -1.18671e+06 -2.81408e+07 -4.24494e+07 852636 -3.06161e+07 -2.80094e+07 4.18351e+06 649460 -5.04834e+07 1.1406e+07 -1.23067e+07 -5.80147e+06 1.85905e+06 2.33015e+07 2.49884e+06 3.40511e+06 -983427 2.1865e+06 -6.41497e+06 4.13923e+06 -503660 -3.43686e+07 3.03751e+07 1.80086e+06 -2.09553e+07 -2.14361e+07 1.56455e+07 2.03413e+07 541981 -1.78786e+07 -3.04953e+07 1.58363e+06 -4.09948e+06 -1.07461e+06 -7.47044e+06 -5.30575e+06 5.1336e+06 -7.34423e+06 1.6821e+07 -8.66999e+06 -4.55922e+06 9.49729e+06 4.2666e+06 9.60708e+06 -1.35133e+07 -7.83423e+06 7.33584e+06 -3.64836e+06 2.6868e+06 -260364 1.41098e+07 6.33239e+06 -6.02676e+06 1.60397e+07 -5.4232e+06 7.65451e+06 -1.06981e+07 4.70409e+06 -1.40614e+07 6.69915e+06 -6.71683e+06 2.34577e+07 3.22999e+06 -3.03193e+06 -2.08357e+07 4.81839e+06 4.9928e+06 -7.40392e+06 -6.70292e+06 -4.84582e+06 4.89669e+06 -1.32259e+07 2.76759e+06 2.35947e+06 2.39313e+06 4.96879e+06 3.15405e+06 3.91494e+06 -31286.2 5.30217e+06 271272 -4.3534e+06 -1.29094e+07 -9.36774e+06 1.66834e+07 2.31261e+06 1.73277e+07 2.56477e+06 1.15784e+07 2.06468e+06 1.0538e+07 -5.13075e+06 2.57473e+06 2.13493e+07 328957 2.07887e+06 2.11665e+07 -2.44444e+06 2.93154e+06 -2.07431e+07 3.08779e+06 -3.86714e+06 2.96285e+06 -3.55731e+06 2.47133e+07 -2.00235e+06 1.5532e+06 -2.84287e+07 -1.50208e+07 -7.81745e+06 -91457.9 1.28778e+07 -3.25055e+06 1.81407e+06 656104 479909 3.95125e+06 -5.39448e+06 -1.23252e+07 3.91604e+06 -2.96366e+06 1.94841e+07 3.67116e+06 5.30077e+06 50332.9 119232 1.14583e+07 -642219 -3.78657e+06 1.2334e+07 3.56925e+06 -1.18126e+06 -3.4861e+06 1.91459e+07 7.63944e+06 -4.11583e+06 2.12553e+07 -2.15083e+07 4.36759e+06 -1.77646e+07 3.7034e+06 -2.1738e+07 3.75461e+06 -1.18087e+06 -9.27864e+06 -1.42447e+07 3.54199e+06 2.75992e+06 -1.17987e+06 2.65514e+07 1.21815e+07 5.98643e+06 1.78306e+07 3.42027e+06 -2.60711e+06 -1.63658e+07 -707064 2.19132e+06 -225576 -93417.3 -2.78333e+06 8.43856e+06 -797792 3.76007e+06 -1.59545e+07 6.13309e+06 1.05514e+06 -3.99948e+06 968992 -1.44448e+06 -191789 7.46472e+06 934192 4.99628e+06 222985 1.76821e+06 -6.74211e+06 -360185 -481367 -1.23362e+06 -398232 1.42932e+07 -1.77306e+06 5.47927e+06 -1.61585e+07 2.90861e+06 -4.48495e+06 -2.83619e+06 -3.80069e+06 889803 1.08387e+06 -794196 47154.2 2.22091e+06 ) ; boundaryField { inlet { type calculated; value nonuniform List<scalar> 60 ( 6.89342e+06 -1.80437e+06 2.96666e+07 2.2988e+07 2.14655e+07 1.84923e+07 1.65809e+07 1.33581e+07 -3.72056e+06 -1.89816e+07 -1.20474e+06 -2.33519e+07 4.74649e+06 -2.02366e+07 1.22556e+07 -1.5797e+07 2.06307e+07 -1.06387e+07 2.99943e+07 -4.71937e+06 4.01319e+07 1.67991e+06 5.10708e+07 8.64825e+06 6.23955e+07 1.58173e+07 7.56067e+07 2.3332e+07 9.06724e+07 3.22845e+07 1.07765e+08 4.29969e+07 1.26727e+08 5.53575e+07 1.47093e+08 6.90606e+07 1.68171e+08 8.35195e+07 1.89551e+08 9.87086e+07 2.1143e+08 1.1479e+08 2.34471e+08 1.32313e+08 2.58864e+08 1.51113e+08 2.85106e+08 1.70422e+08 3.13118e+08 1.87832e+08 3.41145e+08 1.97706e+08 3.61453e+08 1.87689e+08 3.52088e+08 1.30744e+08 2.30972e+08 -6.9678e+07 -1.43732e+08 -3.26903e+08 ) ; } outlet { type calculated; value nonuniform List<scalar> 60 ( -5.08671e+09 -3.70145e+09 -2.40287e+09 -2.01118e+09 -6.48168e+09 -5.50758e+09 -1.12131e+09 -1.15384e+09 -6.85423e+08 -7.3116e+08 -4.27581e+08 -4.7405e+08 -1.75233e+08 -2.34559e+08 3.38443e+08 2.27273e+08 1.18424e+09 9.70248e+08 1.81657e+09 1.57098e+09 4.55955e+08 1.23826e+08 -4.00889e+09 -9.25903e+07 -2.63608e+09 -3.33308e+09 -6.94038e+08 -1.29831e+09 3.34241e+08 -3.96925e+07 -7.2376e+08 3.0459e+08 -8.16224e+08 4.08686e+08 -1.25307e+09 5.26824e+08 -1.34094e+09 8.40759e+08 -1.00782e+09 1.38762e+09 -2.84821e+08 2.02387e+09 7.09576e+08 2.62544e+09 2.18712e+09 3.30678e+09 4.02071e+09 4.15745e+09 6.02352e+09 5.20204e+09 7.42941e+09 5.71519e+09 6.05435e+09 4.01533e+09 1.73388e+09 -7.87251e+08 5.54122e+08 -1.02533e+08 -9.75441e+07 -1.28945e+09 ) ; } rightSide { type symmetryPlane; value uniform 0; } leftSide { type symmetryPlane; value uniform 0; } duct { type calculated; value uniform 0; } cylinder { type calculated; value nonuniform List<scalar> 448 ( 0 2.32831e-10 0 0 -7.45058e-09 0 0 7.45058e-09 0 0 7.45058e-09 -7.45058e-09 7.45058e-09 0 0 2.32831e-10 0 0 0 0 -7.45058e-09 0 -7.45058e-09 -7.45058e-09 -7.45058e-09 0 7.45058e-09 0 7.45058e-09 -2.32831e-10 0 2.32831e-10 0 0 1.86265e-09 -1.86265e-09 0 0 -7.45058e-09 -7.45058e-09 0 0 0 1.86265e-09 0 -7.45058e-09 -1.86265e-09 3.72529e-09 0 -7.45058e-09 7.45058e-09 0 3.72529e-09 1.86265e-09 -1.49012e-08 0 -7.45058e-09 0 1.86265e-09 0 2.32831e-10 0 0 0 0 1.86265e-09 0 0 7.45058e-09 0 1.86265e-09 -1.86265e-09 -7.45058e-09 7.45058e-09 -7.45058e-09 0 0 0 0 0 0 7.45058e-09 0 0 -1.86265e-09 -7.45058e-09 7.45058e-09 0 -7.45058e-09 0 0 -3.72529e-09 0 0 0 7.45058e-09 -7.45058e-09 3.72529e-09 0 0 -7.45058e-09 0 -7.45058e-09 0 0 -7.45058e-09 0 -1.86265e-09 0 7.45058e-09 0 0 -3.72529e-09 0 7.45058e-09 0 0 0 7.45058e-09 0 -1.86265e-09 0 1.49012e-08 -7.45058e-09 0 -7.45058e-09 -3.72529e-09 0 7.45058e-09 -7.45058e-09 0 0 -1.86265e-09 -1.86265e-09 -7.45058e-09 7.45058e-09 0 0 1.86265e-09 0 0 7.45058e-09 7.45058e-09 -7.45058e-09 3.72529e-09 0 0 -7.45058e-09 1.86265e-09 0 0 0 0 -1.86265e-09 -7.45058e-09 1.86265e-09 -1.86265e-09 -7.45058e-09 0 0 0 7.45058e-09 0 0 0 0 0 0 0 0 -1.86265e-09 -3.72529e-09 0 7.45058e-09 0 0 1.86265e-09 1.86265e-09 1.86265e-09 1.86265e-09 -7.45058e-09 0 -1.86265e-09 0 0 0 0 0 1.86265e-09 1.86265e-09 0 7.45058e-09 -1.86265e-09 0 0 0 1.86265e-09 0 2.91038e-11 1.86265e-09 -9.31323e-10 9.31323e-10 -2.91038e-11 -9.31323e-10 0 -9.31323e-10 -2.91038e-11 0 0 0 -5.82077e-11 0 0 0 -9.31323e-10 -9.31323e-10 0 9.31323e-10 9.31323e-10 0 0 9.31323e-10 0 0 0 0 0 -9.31323e-10 0 0 9.31323e-10 9.31323e-10 2.91038e-11 0 0 9.31323e-10 -2.91038e-11 0 0 -9.31323e-10 2.91038e-11 0 -1.86265e-09 0 0 0 0 9.31323e-10 2.91038e-11 -9.31323e-10 9.31323e-10 -9.31323e-10 -5.82077e-11 -9.31323e-10 9.31323e-10 0 2.91038e-11 9.31323e-10 0 0 -2.91038e-11 -9.31323e-10 0 1.16415e-10 0 9.31323e-10 1.86265e-09 0 -1.86265e-09 0 0 0 0 0 3.72529e-09 0 -7.45058e-09 7.45058e-09 0 7.45058e-09 9.31323e-10 0 3.72529e-09 0 0 9.31323e-10 0 0 -9.31323e-10 0 -1.86265e-09 -9.31323e-10 0 1.86265e-09 0 9.31323e-10 0 2.32831e-10 0 0 3.72529e-09 0 0 0 0 1.86265e-09 9.31323e-10 9.31323e-10 0 0 0 -9.31323e-10 0 0 7.45058e-09 0 -9.31323e-10 0 3.72529e-09 -9.31323e-10 -7.45058e-09 1.86265e-09 9.31323e-10 0 9.31323e-10 0 -1.86265e-09 0 0 0 0 9.31323e-10 3.72529e-09 0 5.58794e-09 9.31323e-10 -1.86265e-09 0 -2.32831e-10 7.45058e-09 0 0 9.31323e-10 -1.16415e-10 -2.32831e-10 7.45058e-09 0 -9.31323e-10 1.86265e-09 3.72529e-09 9.31323e-10 -7.45058e-09 -1.86265e-09 -9.31323e-10 0 0 0 0 0 0 7.45058e-09 0 0 0 3.72529e-09 -7.45058e-09 1.86265e-09 9.31323e-10 3.72529e-09 0 9.31323e-10 0 -2.32831e-10 0 0 9.31323e-10 -9.31323e-10 0 1.86265e-09 0 0 0 3.72529e-09 0 7.45058e-09 1.49012e-08 7.45058e-09 0 7.45058e-09 0 -1.86265e-09 7.45058e-09 0 -3.72529e-09 0 1.86265e-09 0 0 7.45058e-09 1.86265e-09 9.31323e-10 0 0 0 -1.86265e-09 -9.31323e-10 0 1.16415e-10 2.32831e-10 -7.45058e-09 0 0 7.45058e-09 0 0 7.45058e-09 9.31323e-10 7.45058e-09 0 9.31323e-10 1.86265e-09 -1.16415e-10 0 0 1.86265e-09 0 0 0 -1.86265e-09 -9.31323e-10 7.45058e-09 0 0 -4.65661e-10 -3.72529e-09 0 0 0 0 0 -1.86265e-09 -9.31323e-10 9.31323e-10 0 0 0 0 -1.86265e-09 ) ; } } // ************************************************************************* //
d76e54c4d6d1737af2e16412e9e4adf4c59a2916
a9d0dcb8b0164a42828c71ff55b396fec555320a
/projekt_gra/opengl/glm/glm/core/type_mat2x4.inl
9da29a7f2699dab2e466277ba3751e5394d262ed
[ "MIT" ]
permissive
pwltomaszek/projekt_gra
5215c3c3de4347b9f01a3473fe46831c6b738257
887e367ac6bbd30102de6b58a62d937c55961f93
refs/heads/master
2020-04-09T15:34:08.241279
2011-01-23T22:45:54
2011-01-23T22:45:54
1,031,551
0
0
null
null
null
null
UTF-8
C++
false
false
12,031
inl
/////////////////////////////////////////////////////////////////////////////////////////////////// // OpenGL Mathematics Copyright (c) 2005 - 2010 G-Truc Creation (www.g-truc.net) /////////////////////////////////////////////////////////////////////////////////////////////////// // Created : 2006-08-05 // Updated : 2010-02-03 // Licence : This source is under MIT License // File : glm/core/type_mat2x4.inl /////////////////////////////////////////////////////////////////////////////////////////////////// namespace glm{ namespace detail { template <typename T> inline typename tmat2x4<T>::size_type tmat2x4<T>::col_size() { return 4; } template <typename T> inline typename tmat2x4<T>::size_type tmat2x4<T>::row_size() { return 2; } ////////////////////////////////////// // Accesses template <typename T> inline typename tmat2x4<T>::col_type & tmat2x4<T>::operator[] ( size_type i ) { assert(i < col_size()); return this->value[i]; } template <typename T> inline typename tmat2x4<T>::col_type const & tmat2x4<T>::operator[] ( size_type i ) const { assert(i < col_size()); return this->value[i]; } ////////////////////////////////////////////////////////////// // Constructors template <typename T> inline tmat2x4<T>::tmat2x4() { value_type const Zero(0); value_type const One(1); this->value[0] = col_type(One, Zero, Zero, Zero); this->value[1] = col_type(Zero, One, Zero, Zero); } template <typename T> inline tmat2x4<T>::tmat2x4 ( tmat2x4<T> const & m ) { this->value[0] = m.value[0]; this->value[1] = m.value[1]; } template <typename T> inline tmat2x4<T>::tmat2x4 ( ctor ) {} template <typename T> inline tmat2x4<T>::tmat2x4 ( value_type const & s ) { value_type const Zero(0); this->value[0] = col_type(s, Zero, Zero, Zero); this->value[1] = col_type(Zero, Zero, Zero, Zero); } template <typename T> inline tmat2x4<T>::tmat2x4 ( value_type const & x0, value_type const & y0, value_type const & z0, value_type const & w0, value_type const & x1, value_type const & y1, value_type const & z1, value_type const & w1 ) { this->value[0] = col_type(x0, y0, z0, w0); this->value[1] = col_type(x1, y1, z1, w1); } template <typename T> inline tmat2x4<T>::tmat2x4 ( col_type const & v0, col_type const & v1 ) { this->value[0] = v0; this->value[1] = v1; } // Conversion template <typename T> template <typename U> inline tmat2x4<T>::tmat2x4 ( tmat2x4<U> const & m ) { this->value[0] = col_type(m[0]); this->value[1] = col_type(m[1]); } template <typename T> inline tmat2x4<T>::tmat2x4 ( tmat2x2<T> const & m ) { this->value[0] = col_type(m[0], detail::tvec2<T>(0)); this->value[1] = col_type(m[1], detail::tvec2<T>(0)); } template <typename T> inline tmat2x4<T>::tmat2x4 ( tmat3x3<T> const & m ) { this->value[0] = col_type(m[0], T(0)); this->value[1] = col_type(m[1], T(0)); } template <typename T> inline tmat2x4<T>::tmat2x4 ( tmat4x4<T> const & m ) { this->value[0] = col_type(m[0]); this->value[1] = col_type(m[1]); } template <typename T> inline tmat2x4<T>::tmat2x4 ( tmat2x3<T> const & m ) { this->value[0] = col_type(m[0], T(0)); this->value[1] = col_type(m[1], T(0)); } template <typename T> inline tmat2x4<T>::tmat2x4 ( tmat3x2<T> const & m ) { this->value[0] = col_type(m[0], detail::tvec2<T>(0)); this->value[1] = col_type(m[1], detail::tvec2<T>(0)); } template <typename T> inline tmat2x4<T>::tmat2x4 ( tmat3x4<T> const & m ) { this->value[0] = m[0]; this->value[1] = m[1]; } template <typename T> inline tmat2x4<T>::tmat2x4 ( tmat4x2<T> const & m ) { this->value[0] = col_type(m[0], detail::tvec2<T>(T(0))); this->value[1] = col_type(m[1], detail::tvec2<T>(T(0))); } template <typename T> inline tmat2x4<T>::tmat2x4 ( tmat4x3<T> const & m ) { this->value[0] = col_type(m[0], T(0)); this->value[1] = col_type(m[1], T(0)); } ////////////////////////////////////////////////////////////// // Unary updatable operators template <typename T> inline tmat2x4<T>& tmat2x4<T>::operator= ( tmat2x4<T> const & m ) { this->value[0] = m[0]; this->value[1] = m[1]; return *this; } template <typename T> template <typename U> inline tmat2x4<T>& tmat2x4<T>::operator= ( tmat2x4<U> const & m ) { this->value[0] = m[0]; this->value[1] = m[1]; return *this; } template <typename T> template <typename U> inline tmat2x4<T>& tmat2x4<T>::operator+= ( U const & s ) { this->value[0] += s; this->value[1] += s; return *this; } template <typename T> template <typename U> inline tmat2x4<T>& tmat2x4<T>::operator+= ( tmat2x4<U> const & m ) { this->value[0] += m[0]; this->value[1] += m[1]; return *this; } template <typename T> template <typename U> inline tmat2x4<T>& tmat2x4<T>::operator-= ( U const & s ) { this->value[0] -= s; this->value[1] -= s; return *this; } template <typename T> template <typename U> inline tmat2x4<T>& tmat2x4<T>::operator-= ( tmat2x4<U> const & m ) { this->value[0] -= m[0]; this->value[1] -= m[1]; return *this; } template <typename T> template <typename U> inline tmat2x4<T>& tmat2x4<T>::operator*= ( U const & s ) { this->value[0] *= s; this->value[1] *= s; return *this; } template <typename T> template <typename U> inline tmat2x4<T>& tmat2x4<T>::operator*= ( tmat2x4<U> const & m ) { return (*this = tmat2x4<T>(*this * m)); } template <typename T> template <typename U> inline tmat2x4<T> & tmat2x4<T>::operator/= ( U const & s ) { this->value[0] /= s; this->value[1] /= s; return *this; } template <typename T> inline tmat2x4<T>& tmat2x4<T>::operator++ () { ++this->value[0]; ++this->value[1]; return *this; } template <typename T> inline tmat2x4<T>& tmat2x4<T>::operator-- () { --this->value[0]; --this->value[1]; return *this; } ////////////////////////////////////////////////////////////// // Binary operators template <typename T> inline tmat2x4<T> operator+ ( tmat2x4<T> const & m, typename tmat2x4<T>::value_type const & s ) { return tmat2x4<T>( m[0] + s, m[1] + s); } template <typename T> inline tmat2x4<T> operator+ ( tmat2x4<T> const & m1, tmat2x4<T> const & m2 ) { return tmat2x4<T>( m1[0] + m2[0], m1[1] + m2[1]); } template <typename T> inline tmat2x4<T> operator- ( tmat2x4<T> const & m, typename tmat2x4<T>::value_type const & s ) { return tmat2x4<T>( m[0] - s, m[1] - s); } template <typename T> inline tmat2x4<T> operator- ( tmat2x4<T> const & m1, tmat2x4<T> const & m2 ) { return tmat2x4<T>( m1[0] - m2[0], m1[1] - m2[1]); } template <typename T> inline tmat2x4<T> operator* ( tmat2x4<T> const & m, typename tmat2x4<T>::value_type const & s ) { return tmat2x4<T>( m[0] * s, m[1] * s); } template <typename T> inline tmat2x4<T> operator* ( typename tmat2x4<T>::value_type const & s, tmat2x4<T> const & m ) { return tmat2x4<T>( m[0] * s, m[1] * s); } template <typename T> inline typename tmat2x4<T>::row_type operator* ( tmat2x4<T> const & m, typename tmat2x4<T>::col_type const & v ) { return typename tmat2x4<T>::row_type( m[0][0] * v.x + m[1][0] * v.y, m[0][1] * v.x + m[1][1] * v.y, m[0][2] * v.x + m[1][2] * v.y, m[0][3] * v.x + m[1][3] * v.y); } template <typename T> inline typename tmat2x4<T>::col_type operator* ( typename tmat2x4<T>::row_type const & v, tmat2x4<T> const & m ) { return typename tmat2x4<T>::col_type( m[0][0] * v.x + m[1][0] * v.y + m[2][0] * v.z + m[3][0] * v.w, m[0][1] * v.x + m[1][1] * v.y + m[2][1] * v.z + m[3][1] * v.w); } template <typename T> inline tmat4x4<T> operator* ( tmat2x4<T> const & m1, tmat4x2<T> const & m2 ) { typename tmat2x4<T>::value_type SrcA00 = m1[0][0]; typename tmat2x4<T>::value_type SrcA01 = m1[0][1]; typename tmat2x4<T>::value_type SrcA02 = m1[0][2]; typename tmat2x4<T>::value_type SrcA03 = m1[0][3]; typename tmat2x4<T>::value_type SrcA10 = m1[1][0]; typename tmat2x4<T>::value_type SrcA11 = m1[1][1]; typename tmat2x4<T>::value_type SrcA12 = m1[1][2]; typename tmat2x4<T>::value_type SrcA13 = m1[1][3]; typename tmat2x4<T>::value_type SrcB00 = m2[0][0]; typename tmat2x4<T>::value_type SrcB01 = m2[0][1]; typename tmat2x4<T>::value_type SrcB10 = m2[1][0]; typename tmat2x4<T>::value_type SrcB11 = m2[1][1]; typename tmat2x4<T>::value_type SrcB20 = m2[2][0]; typename tmat2x4<T>::value_type SrcB21 = m2[2][1]; typename tmat2x4<T>::value_type SrcB30 = m2[3][0]; typename tmat2x4<T>::value_type SrcB31 = m2[3][1]; tmat4x4<T> Result(tmat4x4<T>::null); Result[0][0] = SrcA00 * SrcB00 + SrcA10 * SrcB01; Result[0][1] = SrcA01 * SrcB00 + SrcA11 * SrcB01; Result[0][2] = SrcA02 * SrcB00 + SrcA12 * SrcB01; Result[0][3] = SrcA03 * SrcB00 + SrcA13 * SrcB01; Result[1][0] = SrcA00 * SrcB10 + SrcA10 * SrcB11; Result[1][1] = SrcA01 * SrcB10 + SrcA11 * SrcB11; Result[1][2] = SrcA02 * SrcB10 + SrcA12 * SrcB11; Result[1][3] = SrcA03 * SrcB10 + SrcA13 * SrcB11; Result[2][0] = SrcA00 * SrcB20 + SrcA10 * SrcB21; Result[2][1] = SrcA01 * SrcB20 + SrcA11 * SrcB21; Result[2][2] = SrcA02 * SrcB20 + SrcA12 * SrcB21; Result[2][3] = SrcA03 * SrcB20 + SrcA13 * SrcB21; Result[3][0] = SrcA00 * SrcB30 + SrcA10 * SrcB31; Result[3][1] = SrcA01 * SrcB30 + SrcA11 * SrcB31; Result[3][2] = SrcA02 * SrcB30 + SrcA12 * SrcB31; Result[3][3] = SrcA03 * SrcB30 + SrcA13 * SrcB31; return Result; } template <typename T> inline tmat2x4<T> operator/ ( tmat2x4<T> const & m, typename tmat2x4<T>::value_type const & s ) { return tmat2x4<T>( m[0] / s, m[1] / s, m[2] / s, m[3] / s); } template <typename T> inline tmat2x4<T> operator/ ( typename tmat2x4<T>::value_type const & s, tmat2x4<T> const & m ) { return tmat2x4<T>( s / m[0], s / m[1], s / m[2], s / m[3]); } // Unary constant operators template <typename T> inline tmat2x4<T> const operator- ( tmat2x4<T> const & m ) { return tmat2x4<T>( -m[0], -m[1]); } template <typename T> inline tmat2x4<T> const operator++ ( tmat2x4<T> const & m, int ) { return tmat2x4<T>( m[0] + typename tmat2x4<T>::value_type(1), m[1] + typename tmat2x4<T>::value_type(1)); } template <typename T> inline tmat2x4<T> const operator-- ( tmat2x4<T> const & m, int ) { return tmat2x4<T>( m[0] - typename tmat2x4<T>::value_type(1), m[1] - typename tmat2x4<T>::value_type(1)); } } //namespace detail } //namespace glm
674ee3e90bb9f3a5be0215af14e056921cdb107b
bd8ea61964838d300e50dacd84a7acd23761a1bf
/src/server/game/Entities/Unit/Unit.h
24b247e167dacaf67a6b507654c4c4806be26ba0
[]
no_license
AtVirus/Tenerby
95241aadd5604d49639c80b340bfd182aa290ba9
7f9b77f57d23aa05bb40fbde34384fb072fdd68b
refs/heads/master
2020-12-30T18:15:46.086139
2013-06-07T05:14:35
2013-06-07T05:14:35
null
0
0
null
null
null
null
UTF-8
C++
false
false
115,018
h
/* * Copyright (C) 2008-2012 TrinityCore <http://www.trinitycore.org/> * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along * with this program. If not, see <http://www.gnu.org/licenses/>. */ #ifndef __UNIT_H #define __UNIT_H #include "Common.h" #include "Object.h" #include "Opcodes.h" #include "SpellAuraDefines.h" #include "UpdateFields.h" #include "SharedDefines.h" #include "ThreatManager.h" #include "HostileRefManager.h" #include "FollowerReference.h" #include "FollowerRefManager.h" #include "EventProcessor.h" #include "MotionMaster.h" #include "DBCStructure.h" #include "SpellInfo.h" #include "Path.h" #include "WorldPacket.h" #include "Timer.h" #include <list> #define WORLD_TRIGGER 12999 enum SpellInterruptFlags { SPELL_INTERRUPT_FLAG_MOVEMENT = 0x01, // why need this for instant? SPELL_INTERRUPT_FLAG_PUSH_BACK = 0x02, // push back SPELL_INTERRUPT_FLAG_UNK3 = 0x04, // any info? SPELL_INTERRUPT_FLAG_INTERRUPT = 0x08, // interrupt SPELL_INTERRUPT_FLAG_ABORT_ON_DMG = 0x10, // _complete_ interrupt on direct damage //SPELL_INTERRUPT_UNK = 0x20 // unk, 564 of 727 spells having this spell start with "Glyph" }; // See SpellAuraInterruptFlags for other values definitions enum SpellChannelInterruptFlags { CHANNEL_INTERRUPT_FLAG_INTERRUPT = 0x08, // interrupt CHANNEL_FLAG_DELAY = 0x4000 }; enum SpellAuraInterruptFlags { AURA_INTERRUPT_FLAG_HITBYSPELL = 0x00000001, // 0 removed when getting hit by a negative spell? AURA_INTERRUPT_FLAG_TAKE_DAMAGE = 0x00000002, // 1 removed by any damage AURA_INTERRUPT_FLAG_CAST = 0x00000004, // 2 cast any spells AURA_INTERRUPT_FLAG_MOVE = 0x00000008, // 3 removed by any movement AURA_INTERRUPT_FLAG_TURNING = 0x00000010, // 4 removed by any turning AURA_INTERRUPT_FLAG_JUMP = 0x00000020, // 5 removed by entering combat AURA_INTERRUPT_FLAG_NOT_MOUNTED = 0x00000040, // 6 removed by dismounting AURA_INTERRUPT_FLAG_NOT_ABOVEWATER = 0x00000080, // 7 removed by entering water AURA_INTERRUPT_FLAG_NOT_UNDERWATER = 0x00000100, // 8 removed by leaving water AURA_INTERRUPT_FLAG_NOT_SHEATHED = 0x00000200, // 9 removed by unsheathing AURA_INTERRUPT_FLAG_TALK = 0x00000400, // 10 talk to npc / loot? action on creature AURA_INTERRUPT_FLAG_USE = 0x00000800, // 11 mine/use/open action on gameobject AURA_INTERRUPT_FLAG_MELEE_ATTACK = 0x00001000, // 12 removed by attacking AURA_INTERRUPT_FLAG_SPELL_ATTACK = 0x00002000, // 13 ??? AURA_INTERRUPT_FLAG_UNK14 = 0x00004000, // 14 AURA_INTERRUPT_FLAG_TRANSFORM = 0x00008000, // 15 removed by transform? AURA_INTERRUPT_FLAG_UNK16 = 0x00010000, // 16 AURA_INTERRUPT_FLAG_MOUNT = 0x00020000, // 17 misdirect, aspect, swim speed AURA_INTERRUPT_FLAG_NOT_SEATED = 0x00040000, // 18 removed by standing up (used by food and drink mostly and sleep/Fake Death like) AURA_INTERRUPT_FLAG_CHANGE_MAP = 0x00080000, // 19 leaving map/getting teleported AURA_INTERRUPT_FLAG_IMMUNE_OR_LOST_SELECTION = 0x00100000, // 20 removed by auras that make you invulnerable, or make other to lose selection on you AURA_INTERRUPT_FLAG_UNK21 = 0x00200000, // 21 AURA_INTERRUPT_FLAG_TELEPORTED = 0x00400000, // 22 AURA_INTERRUPT_FLAG_ENTER_PVP_COMBAT = 0x00800000, // 23 removed by entering pvp combat AURA_INTERRUPT_FLAG_DIRECT_DAMAGE = 0x01000000, // 24 removed by any direct damage AURA_INTERRUPT_FLAG_LANDING = 0x02000000, // 25 removed by hitting the ground AURA_INTERRUPT_FLAG_NOT_VICTIM = (AURA_INTERRUPT_FLAG_HITBYSPELL | AURA_INTERRUPT_FLAG_TAKE_DAMAGE | AURA_INTERRUPT_FLAG_DIRECT_DAMAGE), }; enum SpellModOp { SPELLMOD_DAMAGE = 0, SPELLMOD_DURATION = 1, SPELLMOD_THREAT = 2, SPELLMOD_EFFECT1 = 3, SPELLMOD_CHARGES = 4, SPELLMOD_RANGE = 5, SPELLMOD_RADIUS = 6, SPELLMOD_CRITICAL_CHANCE = 7, SPELLMOD_ALL_EFFECTS = 8, SPELLMOD_NOT_LOSE_CASTING_TIME = 9, SPELLMOD_CASTING_TIME = 10, SPELLMOD_COOLDOWN = 11, SPELLMOD_EFFECT2 = 12, SPELLMOD_IGNORE_ARMOR = 13, SPELLMOD_COST = 14, SPELLMOD_CRIT_DAMAGE_BONUS = 15, SPELLMOD_RESIST_MISS_CHANCE = 16, SPELLMOD_JUMP_TARGETS = 17, SPELLMOD_CHANCE_OF_SUCCESS = 18, SPELLMOD_ACTIVATION_TIME = 19, SPELLMOD_DAMAGE_MULTIPLIER = 20, SPELLMOD_GLOBAL_COOLDOWN = 21, SPELLMOD_DOT = 22, SPELLMOD_EFFECT3 = 23, SPELLMOD_BONUS_MULTIPLIER = 24, // spellmod 25 SPELLMOD_PROC_PER_MINUTE = 26, SPELLMOD_VALUE_MULTIPLIER = 27, SPELLMOD_RESIST_DISPEL_CHANCE = 28, SPELLMOD_CRIT_DAMAGE_BONUS_2 = 29, //one not used spell SPELLMOD_SPELL_COST_REFUND_ON_FAIL = 30 }; #define MAX_SPELLMOD 32 enum SpellValueMod { SPELLVALUE_BASE_POINT0, SPELLVALUE_BASE_POINT1, SPELLVALUE_BASE_POINT2, SPELLVALUE_RADIUS_MOD, SPELLVALUE_MAX_TARGETS, SPELLVALUE_AURA_STACK, }; typedef std::pair<SpellValueMod, int32> CustomSpellValueMod; class CustomSpellValues : public std::vector<CustomSpellValueMod> { public: void AddSpellMod(SpellValueMod mod, int32 value) { push_back(std::make_pair(mod, value)); } }; enum SpellFacingFlags { SPELL_FACING_FLAG_INFRONT = 0x0001 }; #define BASE_MINDAMAGE 1.0f #define BASE_MAXDAMAGE 2.0f #define BASE_ATTACK_TIME 2000 // byte value (UNIT_FIELD_BYTES_1, 0) enum UnitStandStateType { UNIT_STAND_STATE_STAND = 0, UNIT_STAND_STATE_SIT = 1, UNIT_STAND_STATE_SIT_CHAIR = 2, UNIT_STAND_STATE_SLEEP = 3, UNIT_STAND_STATE_SIT_LOW_CHAIR = 4, UNIT_STAND_STATE_SIT_MEDIUM_CHAIR = 5, UNIT_STAND_STATE_SIT_HIGH_CHAIR = 6, UNIT_STAND_STATE_DEAD = 7, UNIT_STAND_STATE_KNEEL = 8, UNIT_STAND_STATE_SUBMERGED = 9 }; // byte flag value (UNIT_FIELD_BYTES_1, 2) enum UnitStandFlags { UNIT_STAND_FLAGS_UNK1 = 0x01, UNIT_STAND_FLAGS_CREEP = 0x02, UNIT_STAND_FLAGS_UNTRACKABLE = 0x04, UNIT_STAND_FLAGS_UNK4 = 0x08, UNIT_STAND_FLAGS_UNK5 = 0x10, UNIT_STAND_FLAGS_ALL = 0xFF }; // byte flags value (UNIT_FIELD_BYTES_1, 3) enum UnitBytes1_Flags { UNIT_BYTE1_FLAG_ALWAYS_STAND = 0x01, UNIT_BYTE1_FLAG_HOVER = 0x02, UNIT_BYTE1_FLAG_UNK_3 = 0x04, UNIT_BYTE1_FLAG_ALL = 0xFF }; // high byte (3 from 0..3) of UNIT_FIELD_BYTES_2 enum ShapeshiftForm { FORM_NONE = 0x00, FORM_CAT = 0x01, FORM_TREE = 0x02, FORM_TRAVEL = 0x03, FORM_AQUA = 0x04, FORM_BEAR = 0x05, FORM_AMBIENT = 0x06, FORM_GHOUL = 0x07, FORM_DIREBEAR = 0x08, FORM_STEVES_GHOUL = 0x09, FORM_THARONJA_SKELETON = 0x0A, FORM_TEST_OF_STRENGTH = 0x0B, FORM_BLB_PLAYER = 0x0C, FORM_SHADOW_DANCE = 0x0D, FORM_CREATUREBEAR = 0x0E, FORM_CREATURECAT = 0x0F, FORM_GHOSTWOLF = 0x10, FORM_BATTLESTANCE = 0x11, FORM_DEFENSIVESTANCE = 0x12, FORM_BERSERKERSTANCE = 0x13, FORM_TEST = 0x14, FORM_ZOMBIE = 0x15, FORM_METAMORPHOSIS = 0x16, FORM_UNDEAD = 0x19, FORM_MASTER_ANGLER = 0x1A, FORM_FLIGHT_EPIC = 0x1B, FORM_SHADOW = 0x1C, FORM_FLIGHT = 0x1D, FORM_STEALTH = 0x1E, FORM_MOONKIN = 0x1F, FORM_SPIRITOFREDEMPTION = 0x20 }; // low byte (0 from 0..3) of UNIT_FIELD_BYTES_2 enum SheathState { SHEATH_STATE_UNARMED = 0, // non prepared weapon SHEATH_STATE_MELEE = 1, // prepared melee weapon SHEATH_STATE_RANGED = 2 // prepared ranged weapon }; #define MAX_SHEATH_STATE 3 // byte (1 from 0..3) of UNIT_FIELD_BYTES_2 enum UnitPVPStateFlags { UNIT_BYTE2_FLAG_PVP = 0x01, UNIT_BYTE2_FLAG_UNK1 = 0x02, UNIT_BYTE2_FLAG_FFA_PVP = 0x04, UNIT_BYTE2_FLAG_SANCTUARY = 0x08, UNIT_BYTE2_FLAG_UNK4 = 0x10, UNIT_BYTE2_FLAG_UNK5 = 0x20, UNIT_BYTE2_FLAG_UNK6 = 0x40, UNIT_BYTE2_FLAG_UNK7 = 0x80 }; // byte (2 from 0..3) of UNIT_FIELD_BYTES_2 enum UnitRename { UNIT_CAN_BE_RENAMED = 0x01, UNIT_CAN_BE_ABANDONED = 0x02, }; #define CREATURE_MAX_SPELLS 8 #define MAX_SPELL_CHARM 4 #define MAX_SPELL_VEHICLE 6 #define MAX_SPELL_POSSESS 8 #define MAX_SPELL_CONTROL_BAR 10 enum Swing { NOSWING = 0, SINGLEHANDEDSWING = 1, TWOHANDEDSWING = 2 }; enum VictimState { VICTIMSTATE_INTACT = 0, // set when attacker misses VICTIMSTATE_HIT = 1, // victim got clear/blocked hit VICTIMSTATE_DODGE = 2, VICTIMSTATE_PARRY = 3, VICTIMSTATE_INTERRUPT = 4, VICTIMSTATE_BLOCKS = 5, // unused? not set when blocked, even on full block VICTIMSTATE_EVADES = 6, VICTIMSTATE_IS_IMMUNE = 7, VICTIMSTATE_DEFLECTS = 8 }; enum HitInfo { HITINFO_NORMALSWING = 0x00000000, HITINFO_UNK1 = 0x00000001, // req correct packet structure HITINFO_AFFECTS_VICTIM = 0x00000002, HITINFO_OFFHAND = 0x00000004, HITINFO_UNK2 = 0x00000008, HITINFO_MISS = 0x00000010, HITINFO_FULL_ABSORB = 0x00000020, HITINFO_PARTIAL_ABSORB = 0x00000040, HITINFO_FULL_RESIST = 0x00000080, HITINFO_PARTIAL_RESIST = 0x00000100, HITINFO_CRITICALHIT = 0x00000200, // critical hit // 0x00000400 // 0x00000800 // 0x00001000 HITINFO_BLOCK = 0x00002000, // blocked damage // 0x00004000 // Hides worldtext for 0 damage // 0x00008000 // Related to blood visual HITINFO_GLANCING = 0x00010000, HITINFO_CRUSHING = 0x00020000, HITINFO_NO_ANIMATION = 0x00040000, // 0x00080000 // 0x00100000 HITINFO_SWINGNOHITSOUND = 0x00200000, // unused? // 0x00400000 HITINFO_RAGE_GAIN = 0x00800000 }; //i would like to remove this: (it is defined in item.h enum InventorySlot { NULL_BAG = 0, NULL_SLOT = 255 }; struct FactionTemplateEntry; struct SpellValue; class AuraApplication; class Aura; class UnitAura; class AuraEffect; class Creature; class Spell; class SpellInfo; class DynamicObject; class GameObject; class Item; class Pet; class PetAura; class Minion; class Guardian; class UnitAI; class Totem; class Transport; class Vehicle; typedef std::list<Unit*> UnitList; typedef std::list< std::pair<Aura*, uint8> > DispelChargesList; struct SpellImmune { uint32 type; uint32 spellId; }; typedef std::list<SpellImmune> SpellImmuneList; enum UnitModifierType { BASE_VALUE = 0, BASE_PCT = 1, TOTAL_VALUE = 2, TOTAL_PCT = 3, MODIFIER_TYPE_END = 4 }; enum WeaponDamageRange { MINDAMAGE, MAXDAMAGE }; enum DamageTypeToSchool { RESISTANCE, DAMAGE_DEALT, DAMAGE_TAKEN }; enum AuraRemoveMode { AURA_REMOVE_NONE = 0, AURA_REMOVE_BY_DEFAULT = 1, // scripted remove, remove by stack with aura with different ids and sc aura remove AURA_REMOVE_BY_CANCEL, AURA_REMOVE_BY_ENEMY_SPELL, // dispel and absorb aura destroy AURA_REMOVE_BY_EXPIRE, // aura duration has ended AURA_REMOVE_BY_DEATH }; enum TriggerCastFlags { TRIGGERED_NONE = 0x00000000, //! Not triggered TRIGGERED_IGNORE_GCD = 0x00000001, //! Will ignore GCD TRIGGERED_IGNORE_SPELL_AND_CATEGORY_CD = 0x00000002, //! Will ignore Spell and Category cooldowns TRIGGERED_IGNORE_POWER_AND_REAGENT_COST = 0x00000004, //! Will ignore power and reagent cost TRIGGERED_IGNORE_CAST_ITEM = 0x00000008, //! Will not take away cast item or update related achievement criteria TRIGGERED_IGNORE_AURA_SCALING = 0x00000010, //! Will ignore aura scaling TRIGGERED_IGNORE_CAST_IN_PROGRESS = 0x00000020, //! Will not check if a current cast is in progress TRIGGERED_IGNORE_COMBO_POINTS = 0x00000040, //! Will ignore combo point requirement TRIGGERED_CAST_DIRECTLY = 0x00000080, //! In Spell::prepare, will be cast directly without setting containers for executed spell TRIGGERED_IGNORE_AURA_INTERRUPT_FLAGS = 0x00000100, //! Will ignore interruptible aura's at cast TRIGGERED_IGNORE_SET_FACING = 0x00000200, //! Will not adjust facing to target (if any) TRIGGERED_IGNORE_SHAPESHIFT = 0x00000400, //! Will ignore shapeshift checks TRIGGERED_IGNORE_CASTER_AURASTATE = 0x00000800, //! Will ignore caster aura states including combat requirements and death state TRIGGERED_IGNORE_CASTER_MOUNTED_OR_ON_VEHICLE = 0x00002000, //! Will ignore mounted/on vehicle restrictions TRIGGERED_IGNORE_CASTER_AURAS = 0x00010000, //! Will ignore caster aura restrictions or requirements TRIGGERED_DISALLOW_PROC_EVENTS = 0x00020000, //! Disallows proc events from triggered spell (default) TRIGGERED_DONT_REPORT_CAST_ERROR = 0x00040000, //! Will return SPELL_FAILED_DONT_REPORT in CheckCast functions TRIGGERED_FULL_MASK = 0xFFFFFFFF, }; enum UnitMods { UNIT_MOD_STAT_STRENGTH, // UNIT_MOD_STAT_STRENGTH..UNIT_MOD_STAT_SPIRIT must be in existed order, it's accessed by index values of Stats enum. UNIT_MOD_STAT_AGILITY, UNIT_MOD_STAT_STAMINA, UNIT_MOD_STAT_INTELLECT, UNIT_MOD_STAT_SPIRIT, UNIT_MOD_HEALTH, UNIT_MOD_MANA, // UNIT_MOD_MANA..UNIT_MOD_RUNIC_POWER must be in existed order, it's accessed by index values of Powers enum. UNIT_MOD_RAGE, UNIT_MOD_FOCUS, UNIT_MOD_ENERGY, UNIT_MOD_HAPPINESS, UNIT_MOD_RUNE, UNIT_MOD_RUNIC_POWER, UNIT_MOD_ARMOR, // UNIT_MOD_ARMOR..UNIT_MOD_RESISTANCE_ARCANE must be in existed order, it's accessed by index values of SpellSchools enum. UNIT_MOD_RESISTANCE_HOLY, UNIT_MOD_RESISTANCE_FIRE, UNIT_MOD_RESISTANCE_NATURE, UNIT_MOD_RESISTANCE_FROST, UNIT_MOD_RESISTANCE_SHADOW, UNIT_MOD_RESISTANCE_ARCANE, UNIT_MOD_ATTACK_POWER, UNIT_MOD_ATTACK_POWER_RANGED, UNIT_MOD_DAMAGE_MAINHAND, UNIT_MOD_DAMAGE_OFFHAND, UNIT_MOD_DAMAGE_RANGED, UNIT_MOD_END, // synonyms UNIT_MOD_STAT_START = UNIT_MOD_STAT_STRENGTH, UNIT_MOD_STAT_END = UNIT_MOD_STAT_SPIRIT + 1, UNIT_MOD_RESISTANCE_START = UNIT_MOD_ARMOR, UNIT_MOD_RESISTANCE_END = UNIT_MOD_RESISTANCE_ARCANE + 1, UNIT_MOD_POWER_START = UNIT_MOD_MANA, UNIT_MOD_POWER_END = UNIT_MOD_RUNIC_POWER + 1 }; enum BaseModGroup { CRIT_PERCENTAGE, RANGED_CRIT_PERCENTAGE, OFFHAND_CRIT_PERCENTAGE, SHIELD_BLOCK_VALUE, BASEMOD_END }; enum BaseModType { FLAT_MOD, PCT_MOD }; #define MOD_END (PCT_MOD+1) enum DeathState { ALIVE = 0, JUST_DIED = 1, CORPSE = 2, DEAD = 3, JUST_ALIVED = 4, }; enum UnitState { UNIT_STATE_DIED = 0x00000001, // player has fake death aura UNIT_STATE_MELEE_ATTACKING = 0x00000002, // player is melee attacking someone //UNIT_STATE_MELEE_ATTACK_BY = 0x00000004, // player is melee attack by someone UNIT_STATE_STUNNED = 0x00000008, UNIT_STATE_ROAMING = 0x00000010, UNIT_STATE_CHASE = 0x00000020, //UNIT_STATE_SEARCHING = 0x00000040, UNIT_STATE_FLEEING = 0x00000080, UNIT_STATE_IN_FLIGHT = 0x00000100, // player is in flight mode UNIT_STATE_FOLLOW = 0x00000200, UNIT_STATE_ROOT = 0x00000400, UNIT_STATE_CONFUSED = 0x00000800, UNIT_STATE_DISTRACTED = 0x00001000, UNIT_STATE_ISOLATED = 0x00002000, // area auras do not affect other players UNIT_STATE_ATTACK_PLAYER = 0x00004000, UNIT_STATE_CASTING = 0x00008000, UNIT_STATE_POSSESSED = 0x00010000, UNIT_STATE_CHARGING = 0x00020000, UNIT_STATE_JUMPING = 0x00040000, UNIT_STATE_ONVEHICLE = 0x00080000, UNIT_STATE_MOVE = 0x00100000, UNIT_STATE_ROTATING = 0x00200000, UNIT_STATE_EVADE = 0x00400000, UNIT_STATE_ROAMING_MOVE = 0x00800000, UNIT_STATE_CONFUSED_MOVE = 0x01000000, UNIT_STATE_FLEEING_MOVE = 0x02000000, UNIT_STATE_CHASE_MOVE = 0x04000000, UNIT_STATE_FOLLOW_MOVE = 0x08000000, UNIT_STATE_UNATTACKABLE = (UNIT_STATE_IN_FLIGHT | UNIT_STATE_ONVEHICLE), // for real move using movegen check and stop (except unstoppable flight) UNIT_STATE_MOVING = UNIT_STATE_ROAMING_MOVE | UNIT_STATE_CONFUSED_MOVE | UNIT_STATE_FLEEING_MOVE | UNIT_STATE_CHASE_MOVE | UNIT_STATE_FOLLOW_MOVE , UNIT_STATE_CONTROLLED = (UNIT_STATE_CONFUSED | UNIT_STATE_STUNNED | UNIT_STATE_FLEEING), UNIT_STATE_LOST_CONTROL = (UNIT_STATE_CONTROLLED | UNIT_STATE_JUMPING | UNIT_STATE_CHARGING), UNIT_STATE_SIGHTLESS = (UNIT_STATE_LOST_CONTROL | UNIT_STATE_EVADE), UNIT_STATE_CANNOT_AUTOATTACK = (UNIT_STATE_LOST_CONTROL | UNIT_STATE_CASTING), UNIT_STATE_CANNOT_TURN = (UNIT_STATE_LOST_CONTROL | UNIT_STATE_ROTATING), // stay by different reasons UNIT_STATE_NOT_MOVE = UNIT_STATE_ROOT | UNIT_STATE_STUNNED | UNIT_STATE_DIED | UNIT_STATE_DISTRACTED, UNIT_STATE_ALL_STATE = 0xffffffff //(UNIT_STATE_STOPPED | UNIT_STATE_MOVING | UNIT_STATE_IN_COMBAT | UNIT_STATE_IN_FLIGHT) }; enum UnitMoveType { MOVE_WALK = 0, MOVE_RUN = 1, MOVE_RUN_BACK = 2, MOVE_SWIM = 3, MOVE_SWIM_BACK = 4, MOVE_TURN_RATE = 5, MOVE_FLIGHT = 6, MOVE_FLIGHT_BACK = 7, MOVE_PITCH_RATE = 8 }; #define MAX_MOVE_TYPE 9 extern float baseMoveSpeed[MAX_MOVE_TYPE]; extern float playerBaseMoveSpeed[MAX_MOVE_TYPE]; enum WeaponAttackType { BASE_ATTACK = 0, OFF_ATTACK = 1, RANGED_ATTACK = 2, MAX_ATTACK }; enum CombatRating { CR_WEAPON_SKILL = 0, CR_DEFENSE_SKILL = 1, CR_DODGE = 2, CR_PARRY = 3, CR_BLOCK = 4, CR_HIT_MELEE = 5, CR_HIT_RANGED = 6, CR_HIT_SPELL = 7, CR_CRIT_MELEE = 8, CR_CRIT_RANGED = 9, CR_CRIT_SPELL = 10, CR_HIT_TAKEN_MELEE = 11, CR_HIT_TAKEN_RANGED = 12, CR_HIT_TAKEN_SPELL = 13, CR_CRIT_TAKEN_MELEE = 14, CR_CRIT_TAKEN_RANGED = 15, CR_CRIT_TAKEN_SPELL = 16, CR_HASTE_MELEE = 17, CR_HASTE_RANGED = 18, CR_HASTE_SPELL = 19, CR_WEAPON_SKILL_MAINHAND = 20, CR_WEAPON_SKILL_OFFHAND = 21, CR_WEAPON_SKILL_RANGED = 22, CR_EXPERTISE = 23, CR_ARMOR_PENETRATION = 24 }; #define MAX_COMBAT_RATING 25 enum DamageEffectType { DIRECT_DAMAGE = 0, // used for normal weapon damage (not for class abilities or spells) SPELL_DIRECT_DAMAGE = 1, // spell/class abilities damage DOT = 2, HEAL = 3, NODAMAGE = 4, // used also in case when damage applied to health but not applied to spell channelInterruptFlags/etc SELF_DAMAGE = 5 }; // Value masks for UNIT_FIELD_FLAGS enum UnitFlags { UNIT_FLAG_SERVER_CONTROLLED = 0x00000001, // set only when unit movement is controlled by server - by SPLINE/MONSTER_MOVE packets, together with UNIT_FLAG_STUNNED; only set to units controlled by client; client function CGUnit_C::IsClientControlled returns false when set for owner UNIT_FLAG_NON_ATTACKABLE = 0x00000002, // not attackable UNIT_FLAG_DISABLE_MOVE = 0x00000004, UNIT_FLAG_PVP_ATTACKABLE = 0x00000008, // allow apply pvp rules to attackable state in addition to faction dependent state UNIT_FLAG_RENAME = 0x00000010, UNIT_FLAG_PREPARATION = 0x00000020, // don't take reagents for spells with SPELL_ATTR5_NO_REAGENT_WHILE_PREP UNIT_FLAG_UNK_6 = 0x00000040, UNIT_FLAG_NOT_ATTACKABLE_1 = 0x00000080, // ?? (UNIT_FLAG_PVP_ATTACKABLE | UNIT_FLAG_NOT_ATTACKABLE_1) is NON_PVP_ATTACKABLE UNIT_FLAG_IMMUNE_TO_PC = 0x00000100, // disables combat/assistance with PlayerCharacters (PC) - see Unit::_IsValidAttackTarget, Unit::_IsValidAssistTarget UNIT_FLAG_IMMUNE_TO_NPC = 0x00000200, // disables combat/assistance with NonPlayerCharacters (NPC) - see Unit::_IsValidAttackTarget, Unit::_IsValidAssistTarget UNIT_FLAG_LOOTING = 0x00000400, // loot animation UNIT_FLAG_PET_IN_COMBAT = 0x00000800, // in combat?, 2.0.8 UNIT_FLAG_PVP = 0x00001000, // changed in 3.0.3 UNIT_FLAG_SILENCED = 0x00002000, // silenced, 2.1.1 UNIT_FLAG_UNK_14 = 0x00004000, // 2.0.8 UNIT_FLAG_UNK_15 = 0x00008000, UNIT_FLAG_UNK_16 = 0x00010000, UNIT_FLAG_PACIFIED = 0x00020000, // 3.0.3 ok UNIT_FLAG_STUNNED = 0x00040000, // 3.0.3 ok UNIT_FLAG_IN_COMBAT = 0x00080000, UNIT_FLAG_TAXI_FLIGHT = 0x00100000, // disable casting at client side spell not allowed by taxi flight (mounted?), probably used with 0x4 flag UNIT_FLAG_DISARMED = 0x00200000, // 3.0.3, disable melee spells casting..., "Required melee weapon" added to melee spells tooltip. UNIT_FLAG_CONFUSED = 0x00400000, UNIT_FLAG_FLEEING = 0x00800000, UNIT_FLAG_PLAYER_CONTROLLED = 0x01000000, // used in spell Eyes of the Beast for pet... let attack by controlled creature UNIT_FLAG_NOT_SELECTABLE = 0x02000000, UNIT_FLAG_SKINNABLE = 0x04000000, UNIT_FLAG_MOUNT = 0x08000000, UNIT_FLAG_UNK_28 = 0x10000000, UNIT_FLAG_UNK_29 = 0x20000000, // used in Feing Death spell UNIT_FLAG_SHEATHE = 0x40000000, UNIT_FLAG_UNK_31 = 0x80000000 }; // Value masks for UNIT_FIELD_FLAGS_2 enum UnitFlags2 { UNIT_FLAG2_FEIGN_DEATH = 0x00000001, UNIT_FLAG2_UNK1 = 0x00000002, // Hide unit model (show only player equip) UNIT_FLAG2_IGNORE_REPUTATION = 0x00000004, UNIT_FLAG2_COMPREHEND_LANG = 0x00000008, UNIT_FLAG2_MIRROR_IMAGE = 0x00000010, UNIT_FLAG2_FORCE_MOVE = 0x00000040, UNIT_FLAG2_DISARM_OFFHAND = 0x00000080, UNIT_FLAG2_DISARM_RANGED = 0x00000400, // this does not disable ranged weapon display (maybe additional flag needed?) UNIT_FLAG2_REGENERATE_POWER = 0x00000800, UNIT_FLAG2_ALLOW_ENEMY_INTERACT = 0x00004000, UNIT_FLAG2_ALLOW_CHEAT_SPELLS = 0x00040000, // allows casting spells with AttributesEx7 & SPELL_ATTR7_IS_CHEAT_SPELL }; /// Non Player Character flags enum NPCFlags { UNIT_NPC_FLAG_NONE = 0x00000000, UNIT_NPC_FLAG_GOSSIP = 0x00000001, // 100% UNIT_NPC_FLAG_QUESTGIVER = 0x00000002, // guessed, probably ok UNIT_NPC_FLAG_UNK1 = 0x00000004, UNIT_NPC_FLAG_UNK2 = 0x00000008, UNIT_NPC_FLAG_TRAINER = 0x00000010, // 100% UNIT_NPC_FLAG_TRAINER_CLASS = 0x00000020, // 100% UNIT_NPC_FLAG_TRAINER_PROFESSION = 0x00000040, // 100% UNIT_NPC_FLAG_VENDOR = 0x00000080, // 100% UNIT_NPC_FLAG_VENDOR_AMMO = 0x00000100, // 100%, general goods vendor UNIT_NPC_FLAG_VENDOR_FOOD = 0x00000200, // 100% UNIT_NPC_FLAG_VENDOR_POISON = 0x00000400, // guessed UNIT_NPC_FLAG_VENDOR_REAGENT = 0x00000800, // 100% UNIT_NPC_FLAG_REPAIR = 0x00001000, // 100% UNIT_NPC_FLAG_FLIGHTMASTER = 0x00002000, // 100% UNIT_NPC_FLAG_SPIRITHEALER = 0x00004000, // guessed UNIT_NPC_FLAG_SPIRITGUIDE = 0x00008000, // guessed UNIT_NPC_FLAG_INNKEEPER = 0x00010000, // 100% UNIT_NPC_FLAG_BANKER = 0x00020000, // 100% UNIT_NPC_FLAG_PETITIONER = 0x00040000, // 100% 0xC0000 = guild petitions, 0x40000 = arena team petitions UNIT_NPC_FLAG_TABARDDESIGNER = 0x00080000, // 100% UNIT_NPC_FLAG_BATTLEMASTER = 0x00100000, // 100% UNIT_NPC_FLAG_AUCTIONEER = 0x00200000, // 100% UNIT_NPC_FLAG_STABLEMASTER = 0x00400000, // 100% UNIT_NPC_FLAG_GUILD_BANKER = 0x00800000, // cause client to send 997 opcode UNIT_NPC_FLAG_SPELLCLICK = 0x01000000, // cause client to send 1015 opcode (spell click) UNIT_NPC_FLAG_PLAYER_VEHICLE = 0x02000000, // players with mounts that have vehicle data should have it set }; enum MovementFlags { MOVEMENTFLAG_NONE = 0x00000000, MOVEMENTFLAG_FORWARD = 0x00000001, MOVEMENTFLAG_BACKWARD = 0x00000002, MOVEMENTFLAG_STRAFE_LEFT = 0x00000004, MOVEMENTFLAG_STRAFE_RIGHT = 0x00000008, MOVEMENTFLAG_LEFT = 0x00000010, MOVEMENTFLAG_RIGHT = 0x00000020, MOVEMENTFLAG_PITCH_UP = 0x00000040, MOVEMENTFLAG_PITCH_DOWN = 0x00000080, MOVEMENTFLAG_WALKING = 0x00000100, // Walking MOVEMENTFLAG_ONTRANSPORT = 0x00000200, // Used for flying on some creatures MOVEMENTFLAG_DISABLE_GRAVITY = 0x00000400, // Former MOVEMENTFLAG_LEVITATING. This is used when walking is not possible. MOVEMENTFLAG_ROOT = 0x00000800, // Must not be set along with MOVEMENTFLAG_MASK_MOVING MOVEMENTFLAG_FALLING = 0x00001000, // damage dealt on that type of falling MOVEMENTFLAG_FALLING_FAR = 0x00002000, MOVEMENTFLAG_PENDING_STOP = 0x00004000, MOVEMENTFLAG_PENDING_STRAFE_STOP = 0x00008000, MOVEMENTFLAG_PENDING_FORWARD = 0x00010000, MOVEMENTFLAG_PENDING_BACKWARD = 0x00020000, MOVEMENTFLAG_PENDING_STRAFE_LEFT = 0x00040000, MOVEMENTFLAG_PENDING_STRAFE_RIGHT = 0x00080000, MOVEMENTFLAG_PENDING_ROOT = 0x00100000, MOVEMENTFLAG_SWIMMING = 0x00200000, // appears with fly flag also MOVEMENTFLAG_ASCENDING = 0x00400000, // press "space" when flying MOVEMENTFLAG_DESCENDING = 0x00800000, MOVEMENTFLAG_CAN_FLY = 0x01000000, // Appears when unit can fly AND also walk MOVEMENTFLAG_FLYING = 0x02000000, // unit is actually flying. pretty sure this is only used for players. creatures use disable_gravity MOVEMENTFLAG_SPLINE_ELEVATION = 0x04000000, // used for flight paths MOVEMENTFLAG_SPLINE_ENABLED = 0x08000000, // used for flight paths MOVEMENTFLAG_WATERWALKING = 0x10000000, // prevent unit from falling through water MOVEMENTFLAG_FALLING_SLOW = 0x20000000, // active rogue safe fall spell (passive) MOVEMENTFLAG_HOVER = 0x40000000, // hover, cannot jump // TODO: Check if PITCH_UP and PITCH_DOWN really belong here.. MOVEMENTFLAG_MASK_MOVING = MOVEMENTFLAG_FORWARD | MOVEMENTFLAG_BACKWARD | MOVEMENTFLAG_STRAFE_LEFT | MOVEMENTFLAG_STRAFE_RIGHT | MOVEMENTFLAG_PITCH_UP | MOVEMENTFLAG_PITCH_DOWN | MOVEMENTFLAG_FALLING | MOVEMENTFLAG_FALLING_FAR | MOVEMENTFLAG_ASCENDING | MOVEMENTFLAG_DESCENDING | MOVEMENTFLAG_SPLINE_ELEVATION, MOVEMENTFLAG_MASK_TURNING = MOVEMENTFLAG_LEFT | MOVEMENTFLAG_RIGHT, MOVEMENTFLAG_MASK_MOVING_FLY = MOVEMENTFLAG_FLYING | MOVEMENTFLAG_ASCENDING | MOVEMENTFLAG_DESCENDING, //! TODO if needed: add more flags to this masks that are exclusive to players MOVEMENTFLAG_MASK_PLAYER_ONLY = MOVEMENTFLAG_FLYING, }; enum MovementFlags2 { MOVEMENTFLAG2_NONE = 0x00000000, MOVEMENTFLAG2_NO_STRAFE = 0x00000001, MOVEMENTFLAG2_NO_JUMPING = 0x00000002, MOVEMENTFLAG2_UNK3 = 0x00000004, // Overrides various clientside checks MOVEMENTFLAG2_FULL_SPEED_TURNING = 0x00000008, MOVEMENTFLAG2_FULL_SPEED_PITCHING = 0x00000010, MOVEMENTFLAG2_ALWAYS_ALLOW_PITCHING = 0x00000020, MOVEMENTFLAG2_UNK7 = 0x00000040, MOVEMENTFLAG2_UNK8 = 0x00000080, MOVEMENTFLAG2_UNK9 = 0x00000100, MOVEMENTFLAG2_UNK10 = 0x00000200, MOVEMENTFLAG2_INTERPOLATED_MOVEMENT = 0x00000400, MOVEMENTFLAG2_INTERPOLATED_TURNING = 0x00000800, MOVEMENTFLAG2_INTERPOLATED_PITCHING = 0x00001000, MOVEMENTFLAG2_UNK14 = 0x00002000, MOVEMENTFLAG2_UNK15 = 0x00004000, MOVEMENTFLAG2_UNK16 = 0x00008000, }; enum SplineFlags { SPLINEFLAG_WALKMODE = 0x00001000, SPLINEFLAG_FLYING = 0x00002000, SPLINEFLAG_TRANSPORT = 0x00800000, SPLINEFLAG_EXIT_VEHICLE = 0x01000000, }; enum SplineType { SPLINETYPE_FACING_ANGLE = 4, }; enum UnitTypeMask { UNIT_MASK_NONE = 0x00000000, UNIT_MASK_SUMMON = 0x00000001, UNIT_MASK_MINION = 0x00000002, UNIT_MASK_GUARDIAN = 0x00000004, UNIT_MASK_TOTEM = 0x00000008, UNIT_MASK_PET = 0x00000010, UNIT_MASK_VEHICLE = 0x00000020, UNIT_MASK_PUPPET = 0x00000040, UNIT_MASK_HUNTER_PET = 0x00000080, UNIT_MASK_CONTROLABLE_GUARDIAN = 0x00000100, UNIT_MASK_ACCESSORY = 0x00000200, }; namespace Movement{ class MoveSpline; } enum DiminishingLevels { DIMINISHING_LEVEL_1 = 0, DIMINISHING_LEVEL_2 = 1, DIMINISHING_LEVEL_3 = 2, DIMINISHING_LEVEL_IMMUNE = 3, DIMINISHING_LEVEL_4 = 3, DIMINISHING_LEVEL_TAUNT_IMMUNE = 4, }; struct DiminishingReturn { DiminishingReturn(DiminishingGroup group, uint32 t, uint32 count) : DRGroup(group), stack(0), hitTime(t), hitCount(count) {} DiminishingGroup DRGroup:16; uint16 stack:16; uint32 hitTime; uint32 hitCount; }; enum MeleeHitOutcome { MELEE_HIT_EVADE, MELEE_HIT_MISS, MELEE_HIT_DODGE, MELEE_HIT_BLOCK, MELEE_HIT_PARRY, MELEE_HIT_GLANCING, MELEE_HIT_CRIT, MELEE_HIT_CRUSHING, MELEE_HIT_NORMAL }; class DispelInfo { private: Unit* const m_dispeller; uint32 const m_dispellerSpellId; uint8 m_chargesRemoved; public: explicit DispelInfo(Unit* _dispeller, uint32 _dispellerSpellId, uint8 _chargesRemoved) : m_dispeller(_dispeller), m_dispellerSpellId(_dispellerSpellId), m_chargesRemoved(_chargesRemoved) {} Unit* GetDispeller() { return m_dispeller; } uint32 GetDispellerSpellId() { return m_dispellerSpellId; } uint8 GetRemovedCharges() { return m_chargesRemoved; } void SetRemovedCharges(uint8 amount) { m_chargesRemoved = amount; } }; struct CleanDamage { CleanDamage(uint32 mitigated, uint32 absorbed, WeaponAttackType _attackType, MeleeHitOutcome _hitOutCome) : absorbed_damage(absorbed), mitigated_damage(mitigated), attackType(_attackType), hitOutCome(_hitOutCome) {} uint32 absorbed_damage; uint32 mitigated_damage; WeaponAttackType attackType; MeleeHitOutcome hitOutCome; }; struct CalcDamageInfo; class DamageInfo { private: Unit* const m_attacker; Unit* const m_victim; uint32 m_damage; SpellInfo const* const m_spellInfo; SpellSchoolMask const m_schoolMask; DamageEffectType const m_damageType; WeaponAttackType m_attackType; uint32 m_absorb; uint32 m_resist; uint32 m_block; public: explicit DamageInfo(Unit* _attacker, Unit* _victim, uint32 _damage, SpellInfo const* _spellInfo, SpellSchoolMask _schoolMask, DamageEffectType _damageType); explicit DamageInfo(CalcDamageInfo& dmgInfo); void ModifyDamage(int32 amount); void AbsorbDamage(uint32 amount); void ResistDamage(uint32 amount); void BlockDamage(uint32 amount); Unit* GetAttacker() const { return m_attacker; }; Unit* GetVictim() const { return m_victim; }; SpellInfo const* GetSpellInfo() const { return m_spellInfo; }; SpellSchoolMask GetSchoolMask() const { return m_schoolMask; }; DamageEffectType GetDamageType() const { return m_damageType; }; WeaponAttackType GetAttackType() const { return m_attackType; }; uint32 GetDamage() const { return m_damage; }; uint32 GetAbsorb() const { return m_absorb; }; uint32 GetResist() const { return m_resist; }; uint32 GetBlock() const { return m_block; }; }; class HealInfo { private: Unit* const m_healer; Unit* const m_target; uint32 m_heal; uint32 m_absorb; SpellInfo const* const m_spellInfo; SpellSchoolMask const m_schoolMask; public: explicit HealInfo(Unit* _healer, Unit* _target, uint32 _heal, SpellInfo const* _spellInfo, SpellSchoolMask _schoolMask) : m_healer(_healer), m_target(_target), m_heal(_heal), m_spellInfo(_spellInfo), m_schoolMask(_schoolMask) { m_absorb = 0; } void AbsorbHeal(uint32 amount) { amount = std::min(amount, GetHeal()); m_absorb += amount; m_heal -= amount; } uint32 GetHeal() const { return m_heal; }; }; class ProcEventInfo { private: Unit* const _actor; Unit* const _actionTarget; Unit* const _procTarget; uint32 _typeMask; uint32 _spellTypeMask; uint32 _spellPhaseMask; uint32 _hitMask; Spell* _spell; DamageInfo* _damageInfo; HealInfo* _healInfo; public: explicit ProcEventInfo(Unit* actor, Unit* actionTarget, Unit* procTarget, uint32 typeMask, uint32 spellTypeMask, uint32 spellPhaseMask, uint32 hitMask, Spell* spell, DamageInfo* damageInfo, HealInfo* healInfo); Unit* GetActor() { return _actor; }; Unit* GetActionTarget() const { return _actionTarget; } Unit* GetProcTarget() const { return _procTarget; } uint32 GetTypeMask() const { return _typeMask; } uint32 GetSpellTypeMask() const { return _spellTypeMask; } uint32 GetSpellPhaseMask() const { return _spellPhaseMask; } uint32 GetHitMask() const { return _hitMask; } SpellInfo const* GetSpellInfo() const { return NULL; } SpellSchoolMask GetSchoolMask() const { return SPELL_SCHOOL_MASK_NONE; } DamageInfo* GetDamageInfo() const { return _damageInfo; } HealInfo* GetHealInfo() const { return _healInfo; } }; // Struct for use in Unit::CalculateMeleeDamage // Need create structure like in SMSG_ATTACKERSTATEUPDATE opcode struct CalcDamageInfo { Unit *attacker; // Attacker Unit *target; // Target for damage uint32 damageSchoolMask; uint32 damage; uint32 absorb; uint32 resist; uint32 blocked_amount; uint32 HitInfo; uint32 TargetState; // Helper WeaponAttackType attackType; // uint32 procAttacker; uint32 procVictim; uint32 procEx; uint32 cleanDamage; // Used only for rage calculation MeleeHitOutcome hitOutCome; // TODO: remove this field (need use TargetState) }; // Spell damage info structure based on structure sending in SMSG_SPELLNONMELEEDAMAGELOG opcode struct SpellNonMeleeDamage{ SpellNonMeleeDamage(Unit* _attacker, Unit* _target, uint32 _SpellID, uint32 _schoolMask) : target(_target), attacker(_attacker), SpellID(_SpellID), damage(0), overkill(0), schoolMask(_schoolMask), absorb(0), resist(0), physicalLog(false), unused(false), blocked(0), HitInfo(0), cleanDamage(0) {} Unit *target; Unit *attacker; uint32 SpellID; uint32 damage; uint32 overkill; uint32 schoolMask; uint32 absorb; uint32 resist; bool physicalLog; bool unused; uint32 blocked; uint32 HitInfo; // Used for help uint32 cleanDamage; }; struct SpellPeriodicAuraLogInfo { SpellPeriodicAuraLogInfo(AuraEffect const* _auraEff, uint32 _damage, uint32 _overDamage, uint32 _absorb, uint32 _resist, float _multiplier, bool _critical) : auraEff(_auraEff), damage(_damage), overDamage(_overDamage), absorb(_absorb), resist(_resist), multiplier(_multiplier), critical(_critical){} AuraEffect const* auraEff; uint32 damage; uint32 overDamage; // overkill/overheal uint32 absorb; uint32 resist; float multiplier; bool critical; }; uint32 createProcExtendMask(SpellNonMeleeDamage* damageInfo, SpellMissInfo missCondition); enum UnitAnimationState { ANIMATION_ON_GROUND = 0, ANIMATION_SWIMMING = 1, ANIMATION_HOVER = 2, ANIMATION_FLYING = 3, }; struct MonsterMoveData { MonsterMoveData() : SplineFlag(0), AnimationState(ANIMATION_ON_GROUND), Time(0), SpeedZ(0.0f) {} Position DestLocation; uint32 SplineFlag; UnitAnimationState AnimationState; // Only used with SPLINEFLAG_ANIMATIONTIER uint32 Time; float SpeedZ; // Only used with SPLINEFLAG_TRAJECTORY }; #define MAX_DECLINED_NAME_CASES 5 struct DeclinedName { std::string name[MAX_DECLINED_NAME_CASES]; }; enum CurrentSpellTypes { CURRENT_MELEE_SPELL = 0, CURRENT_GENERIC_SPELL = 1, CURRENT_CHANNELED_SPELL = 2, CURRENT_AUTOREPEAT_SPELL = 3 }; #define CURRENT_FIRST_NON_MELEE_SPELL 1 #define CURRENT_MAX_SPELL 4 struct GlobalCooldown { explicit GlobalCooldown(uint32 _dur = 0, uint32 _time = 0) : duration(_dur), cast_time(_time) {} uint32 duration; uint32 cast_time; }; typedef UNORDERED_MAP<uint32 /*category*/, GlobalCooldown> GlobalCooldownList; class GlobalCooldownMgr // Shared by Player and CharmInfo { public: GlobalCooldownMgr() {} public: bool HasGlobalCooldown(SpellInfo const* spellInfo) const; void AddGlobalCooldown(SpellInfo const* spellInfo, uint32 gcd); void CancelGlobalCooldown(SpellInfo const* spellInfo); private: GlobalCooldownList m_GlobalCooldowns; }; enum ActiveStates { ACT_PASSIVE = 0x01, // 0x01 - passive ACT_DISABLED = 0x81, // 0x80 - castable ACT_ENABLED = 0xC1, // 0x40 | 0x80 - auto cast + castable ACT_COMMAND = 0x07, // 0x01 | 0x02 | 0x04 ACT_REACTION = 0x06, // 0x02 | 0x04 ACT_DECIDE = 0x00 // custom }; enum ReactStates { REACT_PASSIVE = 0, REACT_DEFENSIVE = 1, REACT_AGGRESSIVE = 2 }; enum CommandStates { COMMAND_STAY = 0, COMMAND_FOLLOW = 1, COMMAND_ATTACK = 2, COMMAND_ABANDON = 3 }; #define UNIT_ACTION_BUTTON_ACTION(X) (uint32(X) & 0x00FFFFFF) #define UNIT_ACTION_BUTTON_TYPE(X) ((uint32(X) & 0xFF000000) >> 24) #define MAKE_UNIT_ACTION_BUTTON(A, T) (uint32(A) | (uint32(T) << 24)) struct UnitActionBarEntry { UnitActionBarEntry() : packedData(uint32(ACT_DISABLED) << 24) {} uint32 packedData; // helper ActiveStates GetType() const { return ActiveStates(UNIT_ACTION_BUTTON_TYPE(packedData)); } uint32 GetAction() const { return UNIT_ACTION_BUTTON_ACTION(packedData); } bool IsActionBarForSpell() const { ActiveStates Type = GetType(); return Type == ACT_DISABLED || Type == ACT_ENABLED || Type == ACT_PASSIVE; } void SetActionAndType(uint32 action, ActiveStates type) { packedData = MAKE_UNIT_ACTION_BUTTON(action, type); } void SetType(ActiveStates type) { packedData = MAKE_UNIT_ACTION_BUTTON(UNIT_ACTION_BUTTON_ACTION(packedData), type); } void SetAction(uint32 action) { packedData = (packedData & 0xFF000000) | UNIT_ACTION_BUTTON_ACTION(action); } }; typedef std::list<Player*> SharedVisionList; enum CharmType { CHARM_TYPE_CHARM, CHARM_TYPE_POSSESS, CHARM_TYPE_VEHICLE, CHARM_TYPE_CONVERT, }; typedef UnitActionBarEntry CharmSpellInfo; enum ActionBarIndex { ACTION_BAR_INDEX_START = 0, ACTION_BAR_INDEX_PET_SPELL_START = 3, ACTION_BAR_INDEX_PET_SPELL_END = 7, ACTION_BAR_INDEX_END = 10, }; #define MAX_UNIT_ACTION_BAR_INDEX (ACTION_BAR_INDEX_END-ACTION_BAR_INDEX_START) struct CharmInfo { public: explicit CharmInfo(Unit* unit); ~CharmInfo(); void RestoreState(); uint32 GetPetNumber() const { return m_petnumber; } void SetPetNumber(uint32 petnumber, bool statwindow); void SetCommandState(CommandStates st) { m_CommandState = st; } CommandStates GetCommandState() const { return m_CommandState; } bool HasCommandState(CommandStates state) const { return (m_CommandState == state); } void InitPossessCreateSpells(); void InitCharmCreateSpells(); void InitPetActionBar(); void InitEmptyActionBar(bool withAttack = true); //return true if successful bool AddSpellToActionBar(SpellInfo const* spellInfo, ActiveStates newstate = ACT_DECIDE); bool RemoveSpellFromActionBar(uint32 spell_id); void LoadPetActionBar(const std::string& data); void BuildActionBar(WorldPacket* data); void SetSpellAutocast(SpellInfo const* spellInfo, bool state); void SetActionBar(uint8 index, uint32 spellOrAction, ActiveStates type) { PetActionBar[index].SetActionAndType(spellOrAction, type); } UnitActionBarEntry const* GetActionBarEntry(uint8 index) const { return &(PetActionBar[index]); } void ToggleCreatureAutocast(SpellInfo const* spellInfo, bool apply); CharmSpellInfo* GetCharmSpell(uint8 index) { return &(m_charmspells[index]); } GlobalCooldownMgr& GetGlobalCooldownMgr() { return m_GlobalCooldownMgr; } void SetIsCommandAttack(bool val); bool IsCommandAttack(); void SetIsAtStay(bool val); bool IsAtStay(); void SetIsFollowing(bool val); bool IsFollowing(); void SetIsReturning(bool val); bool IsReturning(); void SaveStayPosition(); void GetStayPosition(float &x, float &y, float &z); private: Unit* m_unit; UnitActionBarEntry PetActionBar[MAX_UNIT_ACTION_BAR_INDEX]; CharmSpellInfo m_charmspells[4]; CommandStates m_CommandState; uint32 m_petnumber; bool m_barInit; //for restoration after charmed ReactStates m_oldReactState; bool m_isCommandAttack; bool m_isAtStay; bool m_isFollowing; bool m_isReturning; float m_stayX; float m_stayY; float m_stayZ; GlobalCooldownMgr m_GlobalCooldownMgr; }; // for clearing special attacks #define REACTIVE_TIMER_START 4000 enum ReactiveType { REACTIVE_DEFENSE = 0, REACTIVE_HUNTER_PARRY = 1, REACTIVE_OVERPOWER = 2 }; #define MAX_REACTIVE 3 #define SUMMON_SLOT_PET 0 #define SUMMON_SLOT_TOTEM 1 #define MAX_TOTEM_SLOT 5 #define SUMMON_SLOT_MINIPET 5 #define SUMMON_SLOT_QUEST 6 #define MAX_SUMMON_SLOT 7 #define MAX_GAMEOBJECT_SLOT 4 enum PlayerTotemType { SUMMON_TYPE_TOTEM_FIRE = 63, SUMMON_TYPE_TOTEM_EARTH = 81, SUMMON_TYPE_TOTEM_WATER = 82, SUMMON_TYPE_TOTEM_AIR = 83, }; // delay time next attack to prevent client attack animation problems #define ATTACK_DISPLAY_DELAY 200 #define MAX_PLAYER_STEALTH_DETECT_RANGE 30.0f // max distance for detection targets by player struct SpellProcEventEntry; // used only privately class Unit : public WorldObject { public: typedef std::set<Unit*> AttackerSet; typedef std::set<Unit*> ControlList; typedef std::pair<uint32, uint8> spellEffectPair; typedef std::multimap<uint32, Aura*> AuraMap; typedef std::multimap<uint32, AuraApplication*> AuraApplicationMap; typedef std::multimap<AuraStateType, AuraApplication*> AuraStateAurasMap; typedef std::list<AuraEffect*> AuraEffectList; typedef std::list<Aura*> AuraList; typedef std::list<AuraApplication *> AuraApplicationList; typedef std::list<DiminishingReturn> Diminishing; typedef std::set<uint32> ComboPointHolderSet; typedef std::map<uint8, AuraApplication*> VisibleAuraMap; virtual ~Unit(); UnitAI* GetAI() { return i_AI; } void SetAI(UnitAI* newAI) { i_AI = newAI; } void AddToWorld(); void RemoveFromWorld(); void CleanupBeforeRemoveFromMap(bool finalCleanup); void CleanupsBeforeDelete(bool finalCleanup = true); // used in ~Creature/~Player (or before mass creature delete to remove cross-references to already deleted units) DiminishingLevels GetDiminishing(DiminishingGroup group); void IncrDiminishing(DiminishingGroup group); float ApplyDiminishingToDuration(DiminishingGroup group, int32 &duration, Unit* caster, DiminishingLevels Level, int32 limitduration); void ApplyDiminishingAura(DiminishingGroup group, bool apply); void ClearDiminishings() { m_Diminishing.clear(); } // target dependent range checks float GetSpellMaxRangeForTarget(Unit const* target, SpellInfo const* spellInfo) const; float GetSpellMinRangeForTarget(Unit const* target, SpellInfo const* spellInfo) const; virtual void Update(uint32 time); void setAttackTimer(WeaponAttackType type, uint32 time) { m_attackTimer[type] = time; } void resetAttackTimer(WeaponAttackType type = BASE_ATTACK); uint32 getAttackTimer(WeaponAttackType type) const { return m_attackTimer[type]; } bool isAttackReady(WeaponAttackType type = BASE_ATTACK) const { return m_attackTimer[type] == 0; } bool haveOffhandWeapon() const; bool CanDualWield() const { return m_canDualWield; } void SetCanDualWield(bool value) { m_canDualWield = value; } float GetCombatReach() const { return m_floatValues[UNIT_FIELD_COMBATREACH]; } float GetMeleeReach() const { float reach = m_floatValues[UNIT_FIELD_COMBATREACH]; return reach > MIN_MELEE_REACH ? reach : MIN_MELEE_REACH; } bool IsWithinCombatRange(const Unit* obj, float dist2compare) const; bool IsWithinMeleeRange(const Unit* obj, float dist = MELEE_RANGE) const; void GetRandomContactPoint(const Unit* target, float &x, float &y, float &z, float distance2dMin, float distance2dMax) const; uint32 m_extraAttacks; bool m_canDualWield; void _addAttacker(Unit* pAttacker) // must be called only from Unit::Attack(Unit*) { m_attackers.insert(pAttacker); } void _removeAttacker(Unit* pAttacker) // must be called only from Unit::AttackStop() { m_attackers.erase(pAttacker); } Unit* getAttackerForHelper() const // If someone wants to help, who to give them { if (getVictim() != NULL) return getVictim(); if (!m_attackers.empty()) return *(m_attackers.begin()); return NULL; } bool Attack(Unit* victim, bool meleeAttack); void CastStop(uint32 except_spellid = 0); bool AttackStop(); void RemoveAllAttackers(); AttackerSet const& getAttackers() const { return m_attackers; } bool isAttackingPlayer() const; Unit* getVictim() const { return m_attacking; } void CombatStop(bool includingCast = false); void CombatStopWithPets(bool includingCast = false); void StopAttackFaction(uint32 faction_id); Unit* SelectNearbyTarget(Unit* exclude = NULL, float dist = NOMINAL_MELEE_RANGE) const; void SendMeleeAttackStop(Unit* victim = NULL); void SendMeleeAttackStart(Unit* victim); void AddUnitState(uint32 f) { m_state |= f; } bool HasUnitState(const uint32 f) const { return (m_state & f); } void ClearUnitState(uint32 f) { m_state &= ~f; } bool CanFreeMove() const { return !HasUnitState(UNIT_STATE_CONFUSED | UNIT_STATE_FLEEING | UNIT_STATE_IN_FLIGHT | UNIT_STATE_ROOT | UNIT_STATE_STUNNED | UNIT_STATE_DISTRACTED) && GetOwnerGUID() == 0; } uint32 HasUnitTypeMask(uint32 mask) const { return mask & m_unitTypeMask; } void AddUnitTypeMask(uint32 mask) { m_unitTypeMask |= mask; } bool isSummon() const { return m_unitTypeMask & UNIT_MASK_SUMMON; } bool isGuardian() const { return m_unitTypeMask & UNIT_MASK_GUARDIAN; } bool isPet() const { return m_unitTypeMask & UNIT_MASK_PET; } bool isHunterPet() const{ return m_unitTypeMask & UNIT_MASK_HUNTER_PET; } bool isTotem() const { return m_unitTypeMask & UNIT_MASK_TOTEM; } bool IsVehicle() const { return m_unitTypeMask & UNIT_MASK_VEHICLE; } uint8 getLevel() const { return uint8(GetUInt32Value(UNIT_FIELD_LEVEL)); } uint8 getLevelForTarget(WorldObject const* /*target*/) const { return getLevel(); } void SetLevel(uint8 lvl); uint8 getRace() const { return GetByteValue(UNIT_FIELD_BYTES_0, 0); } uint32 getRaceMask() const { return 1 << (getRace()-1); } uint8 getClass() const { return GetByteValue(UNIT_FIELD_BYTES_0, 1); } uint32 getClassMask() const { return 1 << (getClass()-1); } uint8 getGender() const { return GetByteValue(UNIT_FIELD_BYTES_0, 2); } float GetStat(Stats stat) const { return float(GetUInt32Value(UNIT_FIELD_STAT0+stat)); } void SetStat(Stats stat, int32 val) { SetStatInt32Value(UNIT_FIELD_STAT0+stat, val); } uint32 GetArmor() const { return GetResistance(SPELL_SCHOOL_NORMAL); } void SetArmor(int32 val) { SetResistance(SPELL_SCHOOL_NORMAL, val); } uint32 GetResistance(SpellSchools school) const { return GetUInt32Value(UNIT_FIELD_RESISTANCES+school); } uint32 GetResistance(SpellSchoolMask mask) const; void SetResistance(SpellSchools school, int32 val) { SetStatInt32Value(UNIT_FIELD_RESISTANCES+school, val); } uint32 GetHealth() const { return GetUInt32Value(UNIT_FIELD_HEALTH); } uint32 GetMaxHealth() const { return GetUInt32Value(UNIT_FIELD_MAXHEALTH); } bool IsFullHealth() const { return GetHealth() == GetMaxHealth(); } bool HealthBelowPct(int32 pct) const { return GetHealth() * uint64(100) < GetMaxHealth() * uint64(pct); } bool HealthBelowPctDamaged(int32 pct, uint32 damage) const { return (int32(GetHealth()) - damage) * int64(100) < GetMaxHealth() * int64(pct); } bool HealthAbovePct(int32 pct) const { return GetHealth() * uint64(100) > GetMaxHealth() * uint64(pct); } bool HealthAbovePctHealed(int32 pct, uint32 heal) const { return (GetHealth() + heal) * uint64(100) > GetMaxHealth() * uint64(pct); } float GetHealthPct() const { return GetMaxHealth() ? 100.f * GetHealth() / GetMaxHealth() : 0.0f; } uint32 CountPctFromMaxHealth(int32 pct) const { return CalculatePctN(GetMaxHealth(), pct); } uint32 CountPctFromCurHealth(int32 pct) const { return CalculatePctN(GetHealth(), pct); } void SetHealth(uint32 val); void SetMaxHealth(uint32 val); inline void SetFullHealth() { SetHealth(GetMaxHealth()); } int32 ModifyHealth(int32 val); int32 GetHealthGain(int32 dVal); Powers getPowerType() const { return Powers(GetByteValue(UNIT_FIELD_BYTES_0, 3)); } void setPowerType(Powers power); uint32 GetPower(Powers power) const { return GetUInt32Value(UNIT_FIELD_POWER1 +power); } uint32 GetMaxPower(Powers power) const { return GetUInt32Value(UNIT_FIELD_MAXPOWER1+power); } void SetPower(Powers power, uint32 val); void SetMaxPower(Powers power, uint32 val); // returns the change in power int32 ModifyPower(Powers power, int32 val); int32 ModifyPowerPct(Powers power, float pct, bool apply = true); uint32 GetAttackTime(WeaponAttackType att) const { float f_BaseAttackTime = GetFloatValue(UNIT_FIELD_BASEATTACKTIME+att) / m_modAttackSpeedPct[att]; return (uint32)f_BaseAttackTime; } void SetAttackTime(WeaponAttackType att, uint32 val) { SetFloatValue(UNIT_FIELD_BASEATTACKTIME+att, val*m_modAttackSpeedPct[att]); } void ApplyAttackTimePercentMod(WeaponAttackType att, float val, bool apply); void ApplyCastTimePercentMod(float val, bool apply); SheathState GetSheath() const { return SheathState(GetByteValue(UNIT_FIELD_BYTES_2, 0)); } virtual void SetSheath(SheathState sheathed) { SetByteValue(UNIT_FIELD_BYTES_2, 0, sheathed); } // faction template id uint32 getFaction() const { return GetUInt32Value(UNIT_FIELD_FACTIONTEMPLATE); } void setFaction(uint32 faction) { SetUInt32Value(UNIT_FIELD_FACTIONTEMPLATE, faction); } FactionTemplateEntry const* getFactionTemplateEntry() const; ReputationRank GetReactionTo(Unit const* target) const; ReputationRank static GetFactionReactionTo(FactionTemplateEntry const* factionTemplateEntry, Unit const* target); bool IsHostileTo(Unit const* unit) const; bool IsHostileToPlayers() const; bool IsFriendlyTo(Unit const* unit) const; bool IsNeutralToAll() const; bool IsInPartyWith(Unit const* unit) const; bool IsInRaidWith(Unit const* unit) const; void GetPartyMembers(std::list<Unit*> &units); bool IsContestedGuard() const { if (FactionTemplateEntry const* entry = getFactionTemplateEntry()) return entry->IsContestedGuardFaction(); return false; } bool IsPvP() const { return HasByteFlag(UNIT_FIELD_BYTES_2, 1, UNIT_BYTE2_FLAG_PVP); } void SetPvP(bool state) { if (state) SetByteFlag(UNIT_FIELD_BYTES_2, 1, UNIT_BYTE2_FLAG_PVP); else RemoveByteFlag(UNIT_FIELD_BYTES_2, 1, UNIT_BYTE2_FLAG_PVP); } uint32 GetCreatureType() const; uint32 GetCreatureTypeMask() const { uint32 creatureType = GetCreatureType(); return (creatureType >= 1) ? (1 << (creatureType - 1)) : 0; } uint8 getStandState() const { return GetByteValue(UNIT_FIELD_BYTES_1, 0); } bool IsSitState() const; bool IsStandState() const; void SetStandState(uint8 state); void SetStandFlags(uint8 flags) { SetByteFlag(UNIT_FIELD_BYTES_1, 2, flags); } void RemoveStandFlags(uint8 flags) { RemoveByteFlag(UNIT_FIELD_BYTES_1, 2, flags); } bool IsMounted() const { return HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_MOUNT); } uint32 GetMountID() const { return GetUInt32Value(UNIT_FIELD_MOUNTDISPLAYID); } void Mount(uint32 mount, uint32 vehicleId = 0, uint32 creatureEntry = 0); void Dismount(); uint16 GetMaxSkillValueForLevel(Unit const* target = NULL) const { return (target ? getLevelForTarget(target) : getLevel()) * 5; } void DealDamageMods(Unit* victim, uint32 &damage, uint32* absorb); uint32 DealDamage(Unit* victim, uint32 damage, CleanDamage const* cleanDamage = NULL, DamageEffectType damagetype = DIRECT_DAMAGE, SpellSchoolMask damageSchoolMask = SPELL_SCHOOL_MASK_NORMAL, SpellInfo const* spellProto = NULL, bool durabilityLoss = true); void Kill(Unit* victim, bool durabilityLoss = true); int32 DealHeal(Unit* victim, uint32 addhealth); void ProcDamageAndSpell(Unit* victim, uint32 procAttacker, uint32 procVictim, uint32 procEx, uint32 amount, WeaponAttackType attType = BASE_ATTACK, SpellInfo const* procSpell = NULL, SpellInfo const* procAura = NULL); void ProcDamageAndSpellFor(bool isVictim, Unit* target, uint32 procFlag, uint32 procExtra, WeaponAttackType attType, SpellInfo const* procSpell, uint32 damage, SpellInfo const* procAura = NULL); void GetProcAurasTriggeredOnEvent(std::list<AuraApplication*>& aurasTriggeringProc, std::list<AuraApplication*>* procAuras, ProcEventInfo eventInfo); void TriggerAurasProcOnEvent(CalcDamageInfo& damageInfo); void TriggerAurasProcOnEvent(std::list<AuraApplication*>* myProcAuras, std::list<AuraApplication*>* targetProcAuras, Unit* actionTarget, uint32 typeMaskActor, uint32 typeMaskActionTarget, uint32 spellTypeMask, uint32 spellPhaseMask, uint32 hitMask, Spell* spell, DamageInfo* damageInfo, HealInfo* healInfo); void TriggerAurasProcOnEvent(ProcEventInfo& eventInfo, std::list<AuraApplication*>& procAuras); void HandleEmoteCommand(uint32 anim_id); void AttackerStateUpdate (Unit* victim, WeaponAttackType attType = BASE_ATTACK, bool extra = false); void CalculateMeleeDamage(Unit* victim, uint32 damage, CalcDamageInfo* damageInfo, WeaponAttackType attackType = BASE_ATTACK); void DealMeleeDamage(CalcDamageInfo* damageInfo, bool durabilityLoss); void HandleProcExtraAttackFor(Unit* victim); void CalculateSpellDamageTaken(SpellNonMeleeDamage* damageInfo, int32 damage, SpellInfo const* spellInfo, WeaponAttackType attackType = BASE_ATTACK, bool crit = false); void DealSpellDamage(SpellNonMeleeDamage* damageInfo, bool durabilityLoss); // player or player's pet resilience (-1%) float GetMeleeCritChanceReduction() const { return GetCombatRatingReduction(CR_CRIT_TAKEN_MELEE); } float GetRangedCritChanceReduction() const { return GetCombatRatingReduction(CR_CRIT_TAKEN_RANGED); } float GetSpellCritChanceReduction() const { return GetCombatRatingReduction(CR_CRIT_TAKEN_SPELL); } // player or player's pet resilience (-1%) uint32 GetMeleeCritDamageReduction(uint32 damage) const { return GetCombatRatingDamageReduction(CR_CRIT_TAKEN_MELEE, 2.2f, 33.0f, damage); } uint32 GetRangedCritDamageReduction(uint32 damage) const { return GetCombatRatingDamageReduction(CR_CRIT_TAKEN_RANGED, 2.2f, 33.0f, damage); } uint32 GetSpellCritDamageReduction(uint32 damage) const { return GetCombatRatingDamageReduction(CR_CRIT_TAKEN_SPELL, 2.2f, 33.0f, damage); } // player or player's pet resilience (-1%), cap 100% uint32 GetMeleeDamageReduction(uint32 damage) const { return GetCombatRatingDamageReduction(CR_CRIT_TAKEN_MELEE, 2.0f, 100.0f, damage); } uint32 GetRangedDamageReduction(uint32 damage) const { return GetCombatRatingDamageReduction(CR_CRIT_TAKEN_RANGED, 2.0f, 100.0f, damage); } uint32 GetSpellDamageReduction(uint32 damage) const { return GetCombatRatingDamageReduction(CR_CRIT_TAKEN_SPELL, 2.0f, 100.0f, damage); } void ApplyResilience(const Unit* victim, float * crit, int32 * damage, bool isCrit, CombatRating type) const; float MeleeSpellMissChance(const Unit* victim, WeaponAttackType attType, int32 skillDiff, uint32 spellId) const; SpellMissInfo MeleeSpellHitResult(Unit* victim, SpellInfo const* spell); SpellMissInfo MagicSpellHitResult(Unit* victim, SpellInfo const* spell); SpellMissInfo SpellHitResult(Unit* victim, SpellInfo const* spell, bool canReflect = false); float GetUnitDodgeChance() const; float GetUnitParryChance() const; float GetUnitBlockChance() const; float GetUnitMissChance(WeaponAttackType attType) const; float GetUnitCriticalChance(WeaponAttackType attackType, const Unit* victim) const; int32 GetMechanicResistChance(const SpellInfo* spell); bool CanUseAttackType(uint8 attacktype) const { switch (attacktype) { case BASE_ATTACK: return !HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_DISARMED); case OFF_ATTACK: return !HasFlag(UNIT_FIELD_FLAGS_2, UNIT_FLAG2_DISARM_OFFHAND); case RANGED_ATTACK: return !HasFlag(UNIT_FIELD_FLAGS_2, UNIT_FLAG2_DISARM_RANGED); } return true; } virtual uint32 GetShieldBlockValue() const =0; uint32 GetShieldBlockValue(uint32 soft_cap, uint32 hard_cap) const { uint32 value = GetShieldBlockValue(); if (value >= hard_cap) { value = (soft_cap + hard_cap) / 2; } else if (value > soft_cap) { value = soft_cap + ((value - soft_cap) / 2); } return value; } uint32 GetUnitMeleeSkill(Unit const* target = NULL) const { return (target ? getLevelForTarget(target) : getLevel()) * 5; } uint32 GetDefenseSkillValue(Unit const* target = NULL) const; uint32 GetWeaponSkillValue(WeaponAttackType attType, Unit const* target = NULL) const; float GetWeaponProcChance() const; float GetPPMProcChance(uint32 WeaponSpeed, float PPM, const SpellInfo* spellProto) const; MeleeHitOutcome RollMeleeOutcomeAgainst (const Unit* victim, WeaponAttackType attType) const; MeleeHitOutcome RollMeleeOutcomeAgainst (const Unit* victim, WeaponAttackType attType, int32 crit_chance, int32 miss_chance, int32 dodge_chance, int32 parry_chance, int32 block_chance) const; bool isVendor() const { return HasFlag(UNIT_NPC_FLAGS, UNIT_NPC_FLAG_VENDOR); } bool isTrainer() const { return HasFlag(UNIT_NPC_FLAGS, UNIT_NPC_FLAG_TRAINER); } bool isQuestGiver() const { return HasFlag(UNIT_NPC_FLAGS, UNIT_NPC_FLAG_QUESTGIVER); } bool isGossip() const { return HasFlag(UNIT_NPC_FLAGS, UNIT_NPC_FLAG_GOSSIP); } bool isTaxi() const { return HasFlag(UNIT_NPC_FLAGS, UNIT_NPC_FLAG_FLIGHTMASTER); } bool isGuildMaster() const { return HasFlag(UNIT_NPC_FLAGS, UNIT_NPC_FLAG_PETITIONER); } bool isBattleMaster() const { return HasFlag(UNIT_NPC_FLAGS, UNIT_NPC_FLAG_BATTLEMASTER); } bool isBanker() const { return HasFlag(UNIT_NPC_FLAGS, UNIT_NPC_FLAG_BANKER); } bool isInnkeeper() const { return HasFlag(UNIT_NPC_FLAGS, UNIT_NPC_FLAG_INNKEEPER); } bool isSpiritHealer() const { return HasFlag(UNIT_NPC_FLAGS, UNIT_NPC_FLAG_SPIRITHEALER); } bool isSpiritGuide() const { return HasFlag(UNIT_NPC_FLAGS, UNIT_NPC_FLAG_SPIRITGUIDE); } bool isTabardDesigner()const { return HasFlag(UNIT_NPC_FLAGS, UNIT_NPC_FLAG_TABARDDESIGNER); } bool isAuctioner() const { return HasFlag(UNIT_NPC_FLAGS, UNIT_NPC_FLAG_AUCTIONEER); } bool isArmorer() const { return HasFlag(UNIT_NPC_FLAGS, UNIT_NPC_FLAG_REPAIR); } bool isServiceProvider() const { return HasFlag(UNIT_NPC_FLAGS, UNIT_NPC_FLAG_VENDOR | UNIT_NPC_FLAG_TRAINER | UNIT_NPC_FLAG_FLIGHTMASTER | UNIT_NPC_FLAG_PETITIONER | UNIT_NPC_FLAG_BATTLEMASTER | UNIT_NPC_FLAG_BANKER | UNIT_NPC_FLAG_INNKEEPER | UNIT_NPC_FLAG_SPIRITHEALER | UNIT_NPC_FLAG_SPIRITGUIDE | UNIT_NPC_FLAG_TABARDDESIGNER | UNIT_NPC_FLAG_AUCTIONEER); } bool isSpiritService() const { return HasFlag(UNIT_NPC_FLAGS, UNIT_NPC_FLAG_SPIRITHEALER | UNIT_NPC_FLAG_SPIRITGUIDE); } bool isInFlight() const { return HasUnitState(UNIT_STATE_IN_FLIGHT); } bool isInCombat() const { return HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_IN_COMBAT); } void CombatStart(Unit* target, bool initialAggro = true); void SetInCombatState(bool PvP, Unit* enemy = NULL); void SetInCombatWith(Unit* enemy); void ClearInCombat(); uint32 GetCombatTimer() const { return m_CombatTimer; } bool HasAuraTypeWithFamilyFlags(AuraType auraType, uint32 familyName, uint32 familyFlags) const; bool virtual HasSpell(uint32 /*spellID*/) const { return false; } bool HasBreakableByDamageAuraType(AuraType type, uint32 excludeAura = 0) const; bool HasBreakableByDamageCrowdControlAura(Unit* excludeCasterChannel = NULL) const; bool HasStealthAura() const { return HasAuraType(SPELL_AURA_MOD_STEALTH); } bool HasInvisibilityAura() const { return HasAuraType(SPELL_AURA_MOD_INVISIBILITY); } bool isFeared() const { return HasAuraType(SPELL_AURA_MOD_FEAR); } bool isInRoots() const { return HasAuraType(SPELL_AURA_MOD_ROOT); } bool IsPolymorphed() const; bool isFrozen() const; bool isTargetableForAttack(bool checkFakeDeath = true) const; bool IsValidAttackTarget(Unit const* target) const; bool _IsValidAttackTarget(Unit const* target, SpellInfo const* bySpell) const; bool IsValidAssistTarget(Unit const* target) const; bool _IsValidAssistTarget(Unit const* target, SpellInfo const* bySpell) const; virtual bool IsInWater() const; virtual bool IsUnderWater() const; bool isInAccessiblePlaceFor(Creature const* c) const; void SendHealSpellLog(Unit* victim, uint32 SpellID, uint32 Damage, uint32 OverHeal, uint32 Absorb, bool critical = false); int32 HealBySpell(Unit* victim, SpellInfo const* spellInfo, uint32 addHealth, bool critical = false); void SendEnergizeSpellLog(Unit* victim, uint32 SpellID, uint32 Damage, Powers powertype); void EnergizeBySpell(Unit* victim, uint32 SpellID, uint32 Damage, Powers powertype); uint32 SpellNonMeleeDamageLog(Unit* victim, uint32 spellID, uint32 damage); void CastSpell(SpellCastTargets const& targets, SpellInfo const* spellInfo, CustomSpellValues const* value, TriggerCastFlags triggerFlags = TRIGGERED_NONE, Item* castItem = NULL, AuraEffect const* triggeredByAura = NULL, uint64 originalCaster = 0); void CastSpell(Unit* victim, uint32 spellId, bool triggered, Item* castItem = NULL, AuraEffect const* triggeredByAura = NULL, uint64 originalCaster = 0); void CastSpell(Unit* victim, uint32 spellId, TriggerCastFlags triggerFlags = TRIGGERED_NONE, Item* castItem = NULL, AuraEffect const* triggeredByAura = NULL, uint64 originalCaster = 0); void CastSpell(Unit* victim, SpellInfo const* spellInfo, bool triggered, Item* castItem= NULL, AuraEffect const* triggeredByAura = NULL, uint64 originalCaster = 0); void CastSpell(Unit* victim, SpellInfo const* spellInfo, TriggerCastFlags triggerFlags = TRIGGERED_NONE, Item* castItem= NULL, AuraEffect const* triggeredByAura = NULL, uint64 originalCaster = 0); void CastSpell(float x, float y, float z, uint32 spellId, bool triggered, Item* castItem = NULL, AuraEffect const* triggeredByAura = NULL, uint64 originalCaster = 0); void CastCustomSpell(Unit* Victim, uint32 spellId, int32 const* bp0, int32 const* bp1, int32 const* bp2, bool triggered, Item* castItem= NULL, AuraEffect const* triggeredByAura = NULL, uint64 originalCaster = 0); void CastCustomSpell(uint32 spellId, SpellValueMod mod, int32 value, Unit* Victim = NULL, bool triggered = true, Item* castItem = NULL, AuraEffect const* triggeredByAura = NULL, uint64 originalCaster = 0); void CastCustomSpell(uint32 spellId, CustomSpellValues const &value, Unit* Victim = NULL, bool triggered = true, Item* castItem = NULL, AuraEffect const* triggeredByAura = NULL, uint64 originalCaster = 0); void CastSpell(GameObject* go, uint32 spellId, bool triggered, Item* castItem = NULL, AuraEffect* triggeredByAura = NULL, uint64 originalCaster = 0); Aura* AddAura(uint32 spellId, Unit* target); Aura* AddAura(SpellInfo const* spellInfo, uint8 effMask, Unit* target); void SetAuraStack(uint32 spellId, Unit* target, uint32 stack); void SendPlaySpellVisual(uint32 id); void SendPlaySpellImpact(uint64 guid, uint32 id); void DeMorph(); void SendAttackStateUpdate(CalcDamageInfo* damageInfo); void SendAttackStateUpdate(uint32 HitInfo, Unit* target, uint8 SwingType, SpellSchoolMask damageSchoolMask, uint32 Damage, uint32 AbsorbDamage, uint32 Resist, VictimState TargetState, uint32 BlockedAmount); void SendSpellNonMeleeDamageLog(SpellNonMeleeDamage* log); void SendSpellNonMeleeDamageLog(Unit* target, uint32 SpellID, uint32 Damage, SpellSchoolMask damageSchoolMask, uint32 AbsorbedDamage, uint32 Resist, bool PhysicalDamage, uint32 Blocked, bool CriticalHit = false); void SendPeriodicAuraLog(SpellPeriodicAuraLogInfo* pInfo); void SendSpellMiss(Unit* target, uint32 spellID, SpellMissInfo missInfo); void SendSpellDamageResist(Unit* target, uint32 spellId); void SendSpellDamageImmune(Unit* target, uint32 spellId); void NearTeleportTo(float x, float y, float z, float orientation, bool casting = false); virtual bool UpdatePosition(float x, float y, float z, float ang, bool teleport = false); // returns true if unit's position really changed bool UpdatePosition(const Position &pos, bool teleport = false) { return UpdatePosition(pos.GetPositionX(), pos.GetPositionY(), pos.GetPositionZ(), pos.GetOrientation(), teleport); } void UpdateOrientation(float orientation); void UpdateHeight(float newZ); void KnockbackFrom(float x, float y, float speedXY, float speedZ); void JumpTo(float speedXY, float speedZ, bool forward = true); void JumpTo(WorldObject* obj, float speedZ); void MonsterMoveWithSpeed(float x, float y, float z, float speed); //void SetFacing(float ori, WorldObject* obj = NULL); void SendMonsterMoveExitVehicle(Position const* newPos); //void SendMonsterMove(float NewPosX, float NewPosY, float NewPosZ, uint8 type, uint32 MovementFlags, uint32 Time, Player* player = NULL); void SendMonsterMoveTransport(Unit* vehicleOwner); void SendMovementFlagUpdate(); /*! These methods send the same packet to the client in apply and unapply case. The client-side interpretation of this packet depends on the presence of relevant movementflags which are sent with movementinfo. Furthermore, these packets are broadcast to nearby players as well as the current unit. */ void SendMovementHover(); void SendMovementFeatherFall(); void SendMovementWaterWalking(); void SendMovementGravityChange(); void SendMovementCanFlyChange(); bool IsLevitating() const { return m_movementInfo.HasMovementFlag(MOVEMENTFLAG_DISABLE_GRAVITY);} bool IsWalking() const { return m_movementInfo.HasMovementFlag(MOVEMENTFLAG_WALKING);} virtual bool SetWalk(bool enable); virtual bool SetDisableGravity(bool disable, bool packetOnly = false); bool SetHover(bool enable); void SetInFront(Unit const* target); void SetFacingTo(float ori); void SetFacingToObject(WorldObject* object); void SendChangeCurrentVictimOpcode(HostileReference* pHostileReference); void SendClearThreatListOpcode(); void SendRemoveFromThreatListOpcode(HostileReference* pHostileReference); void SendThreatListUpdate(); void SendClearTarget(); void BuildHeartBeatMsg(WorldPacket* data) const; bool isAlive() const { return (m_deathState == ALIVE); }; bool isDying() const { return (m_deathState == JUST_DIED); }; bool isDead() const { return (m_deathState == DEAD || m_deathState == CORPSE); }; DeathState getDeathState() { return m_deathState; }; virtual void setDeathState(DeathState s); // overwrited in Creature/Player/Pet uint64 GetOwnerGUID() const { return GetUInt64Value(UNIT_FIELD_SUMMONEDBY); } uint64 GetCreatorGUID() const { return GetUInt64Value(UNIT_FIELD_CREATEDBY); } void SetCreatorGUID(uint64 creator) { SetUInt64Value(UNIT_FIELD_CREATEDBY, creator); } uint64 GetMinionGUID() const { return GetUInt64Value(UNIT_FIELD_SUMMON); } void SetMinionGUID(uint64 guid) { SetUInt64Value(UNIT_FIELD_SUMMON, guid); } uint64 GetCharmerGUID() const { return GetUInt64Value(UNIT_FIELD_CHARMEDBY); } void SetCharmerGUID(uint64 owner) { SetUInt64Value(UNIT_FIELD_CHARMEDBY, owner); } uint64 GetCharmGUID() const { return GetUInt64Value(UNIT_FIELD_CHARM); } void SetPetGUID(uint64 guid) { m_SummonSlot[SUMMON_SLOT_PET] = guid; } uint64 GetPetGUID() const { return m_SummonSlot[SUMMON_SLOT_PET]; } void SetCritterGUID(uint64 guid) { SetUInt64Value(UNIT_FIELD_CRITTER, guid); } uint64 GetCritterGUID() const { return GetUInt64Value(UNIT_FIELD_CRITTER); } bool IsControlledByPlayer() const { return m_ControlledByPlayer; } uint64 GetCharmerOrOwnerGUID() const { return GetCharmerGUID() ? GetCharmerGUID() : GetOwnerGUID(); } uint64 GetCharmerOrOwnerOrOwnGUID() const { if (uint64 guid = GetCharmerOrOwnerGUID()) return guid; return GetGUID(); } bool isCharmedOwnedByPlayerOrPlayer() const { return IS_PLAYER_GUID(GetCharmerOrOwnerOrOwnGUID()); } Player* GetSpellModOwner() const; Unit* GetOwner() const; Guardian *GetGuardianPet() const; Minion *GetFirstMinion() const; Unit* GetCharmer() const; Unit* GetCharm() const; Unit* GetCharmerOrOwner() const { return GetCharmerGUID() ? GetCharmer() : GetOwner(); } Unit* GetCharmerOrOwnerOrSelf() const { if (Unit* u = GetCharmerOrOwner()) return u; return (Unit*)this; } Player* GetCharmerOrOwnerPlayerOrPlayerItself() const; Player* GetAffectingPlayer() const; void SetMinion(Minion *minion, bool apply); void GetAllMinionsByEntry(std::list<Creature*>& Minions, uint32 entry); void RemoveAllMinionsByEntry(uint32 entry); void SetCharm(Unit* target, bool apply); Unit* GetNextRandomRaidMemberOrPet(float radius); bool SetCharmedBy(Unit* charmer, CharmType type, AuraApplication const* aurApp = NULL); void RemoveCharmedBy(Unit* charmer); void RestoreFaction(); ControlList m_Controlled; Unit* GetFirstControlled() const; void RemoveAllControlled(); bool isCharmed() const { return GetCharmerGUID() != 0; } bool isPossessed() const { return HasUnitState(UNIT_STATE_POSSESSED); } bool isPossessedByPlayer() const { return HasUnitState(UNIT_STATE_POSSESSED) && IS_PLAYER_GUID(GetCharmerGUID()); } bool isPossessing() const { if (Unit* u = GetCharm()) return u->isPossessed(); else return false; } bool isPossessing(Unit* u) const { return u->isPossessed() && GetCharmGUID() == u->GetGUID(); } CharmInfo* GetCharmInfo() { return m_charmInfo; } CharmInfo* InitCharmInfo(); void DeleteCharmInfo(); void UpdateCharmAI(); //Player* GetMoverSource() const; Player* m_movedPlayer; SharedVisionList const& GetSharedVisionList() { return m_sharedVision; } void AddPlayerToVision(Player* player); void RemovePlayerFromVision(Player* player); bool HasSharedVision() const { return !m_sharedVision.empty(); } void RemoveBindSightAuras(); void RemoveCharmAuras(); Pet* CreateTamedPetFrom(Creature* creatureTarget, uint32 spell_id = 0); Pet* CreateTamedPetFrom(uint32 creatureEntry, uint32 spell_id = 0); bool InitTamedPet(Pet* pet, uint8 level, uint32 spell_id); // aura apply/remove helpers - you should better not use these Aura* _TryStackingOrRefreshingExistingAura(SpellInfo const* newAura, uint8 effMask, Unit* caster, int32* baseAmount = NULL, Item* castItem = NULL, uint64 casterGUID = 0); void _AddAura(UnitAura* aura, Unit* caster); AuraApplication * _CreateAuraApplication(Aura* aura, uint8 effMask); void _ApplyAuraEffect(Aura* aura, uint8 effIndex); void _ApplyAura(AuraApplication * aurApp, uint8 effMask); void _UnapplyAura(AuraApplicationMap::iterator &i, AuraRemoveMode removeMode); void _UnapplyAura(AuraApplication * aurApp, AuraRemoveMode removeMode); void _RemoveNoStackAuraApplicationsDueToAura(Aura* aura); void _RemoveNoStackAurasDueToAura(Aura* aura); bool _IsNoStackAuraDueToAura(Aura* appliedAura, Aura* existingAura) const; void _RegisterAuraEffect(AuraEffect* aurEff, bool apply); // m_ownedAuras container management AuraMap & GetOwnedAuras() { return m_ownedAuras; } AuraMap const& GetOwnedAuras() const { return m_ownedAuras; } void RemoveOwnedAura(AuraMap::iterator &i, AuraRemoveMode removeMode = AURA_REMOVE_BY_DEFAULT); void RemoveOwnedAura(uint32 spellId, uint64 casterGUID = 0, uint8 reqEffMask = 0, AuraRemoveMode removeMode = AURA_REMOVE_BY_DEFAULT); void RemoveOwnedAura(Aura* aura, AuraRemoveMode removeMode = AURA_REMOVE_BY_DEFAULT); Aura* GetOwnedAura(uint32 spellId, uint64 casterGUID = 0, uint64 itemCasterGUID = 0, uint8 reqEffMask = 0, Aura* except = NULL) const; // m_appliedAuras container management AuraApplicationMap & GetAppliedAuras() { return m_appliedAuras; } AuraApplicationMap const& GetAppliedAuras() const { return m_appliedAuras; } void RemoveAura(AuraApplicationMap::iterator &i, AuraRemoveMode mode = AURA_REMOVE_BY_DEFAULT); void RemoveAura(uint32 spellId, uint64 casterGUID = 0, uint8 reqEffMask = 0, AuraRemoveMode removeMode = AURA_REMOVE_BY_DEFAULT); void RemoveAura(AuraApplication * aurApp, AuraRemoveMode mode = AURA_REMOVE_BY_DEFAULT); void RemoveAura(Aura* aur, AuraRemoveMode mode = AURA_REMOVE_BY_DEFAULT); void RemoveAurasDueToSpell(uint32 spellId, uint64 casterGUID = 0, uint8 reqEffMask = 0, AuraRemoveMode removeMode = AURA_REMOVE_BY_DEFAULT); void RemoveAuraFromStack(uint32 spellId, uint64 casterGUID = 0, AuraRemoveMode removeMode = AURA_REMOVE_BY_DEFAULT); void RemoveAurasDueToSpellByDispel(uint32 spellId, uint32 dispellerSpellId, uint64 casterGUID, Unit* dispeller, uint8 chargesRemoved = 1); void RemoveAurasDueToSpellBySteal(uint32 spellId, uint64 casterGUID, Unit* stealer); void RemoveAurasDueToItemSpell(Item* castItem, uint32 spellId); void RemoveAurasByType(AuraType auraType, uint64 casterGUID = 0, Aura* except = NULL, bool negative = true, bool positive = true); void RemoveNotOwnSingleTargetAuras(uint32 newPhase = 0x0); void RemoveAurasWithInterruptFlags(uint32 flag, uint32 except = 0); void RemoveAurasWithAttribute(uint32 flags); void RemoveAurasWithFamily(SpellFamilyNames family, uint32 familyFlag1, uint32 familyFlag2, uint32 familyFlag3, uint64 casterGUID); void RemoveAurasWithMechanic(uint32 mechanic_mask, AuraRemoveMode removemode = AURA_REMOVE_BY_DEFAULT, uint32 except=0); void RemoveMovementImpairingAuras(); void RemoveAreaAurasDueToLeaveWorld(); void RemoveAllAuras(); void RemoveArenaAuras(); void RemoveAllAurasOnDeath(); void RemoveAllAurasRequiringDeadTarget(); void RemoveAllAurasExceptType(AuraType type); void DelayOwnedAuras(uint32 spellId, uint64 caster, int32 delaytime); void _RemoveAllAuraStatMods(); void _ApplyAllAuraStatMods(); AuraEffectList const& GetAuraEffectsByType(AuraType type) const { return m_modAuras[type]; } AuraList & GetSingleCastAuras() { return m_scAuras; } AuraList const& GetSingleCastAuras() const { return m_scAuras; } AuraEffect* GetAuraEffect(uint32 spellId, uint8 effIndex, uint64 casterGUID = 0) const; AuraEffect* GetAuraEffectOfRankedSpell(uint32 spellId, uint8 effIndex, uint64 casterGUID = 0) const; AuraEffect* GetAuraEffect(AuraType type, SpellFamilyNames name, uint32 iconId, uint8 effIndex) const; // spell mustn't have familyflags AuraEffect* GetAuraEffect(AuraType type, SpellFamilyNames family, uint32 familyFlag1, uint32 familyFlag2, uint32 familyFlag3, uint64 casterGUID =0); inline AuraEffect* GetDummyAuraEffect(SpellFamilyNames name, uint32 iconId, uint8 effIndex) const { return GetAuraEffect(SPELL_AURA_DUMMY, name, iconId, effIndex);} AuraApplication * GetAuraApplication(uint32 spellId, uint64 casterGUID = 0, uint64 itemCasterGUID = 0, uint8 reqEffMask = 0, AuraApplication * except = NULL) const; Aura* GetAura(uint32 spellId, uint64 casterGUID = 0, uint64 itemCasterGUID = 0, uint8 reqEffMask = 0) const; AuraApplication * GetAuraApplicationOfRankedSpell(uint32 spellId, uint64 casterGUID = 0, uint64 itemCasterGUID = 0, uint8 reqEffMask = 0, AuraApplication * except = NULL) const; Aura* GetAuraOfRankedSpell(uint32 spellId, uint64 casterGUID = 0, uint64 itemCasterGUID = 0, uint8 reqEffMask = 0) const; void GetDispellableAuraList(Unit* caster, uint32 dispelMask, DispelChargesList& dispelList); bool HasAuraEffect(uint32 spellId, uint8 effIndex, uint64 caster = 0) const; uint32 GetAuraCount(uint32 spellId) const; bool HasAura(uint32 spellId, uint64 casterGUID = 0, uint64 itemCasterGUID = 0, uint8 reqEffMask = 0) const; bool HasAuraType(AuraType auraType) const; bool HasAuraTypeWithCaster(AuraType auratype, uint64 caster) const; bool HasAuraTypeWithMiscvalue(AuraType auratype, int32 miscvalue) const; bool HasAuraTypeWithAffectMask(AuraType auratype, SpellInfo const* affectedSpell) const; bool HasAuraTypeWithValue(AuraType auratype, int32 value) const; bool HasNegativeAuraWithInterruptFlag(uint32 flag, uint64 guid = 0); bool HasNegativeAuraWithAttribute(uint32 flag, uint64 guid = 0); bool HasAuraWithMechanic(uint32 mechanicMask); AuraEffect* IsScriptOverriden(SpellInfo const* spell, int32 script) const; uint32 GetDiseasesByCaster(uint64 casterGUID, bool remove = false); uint32 GetDoTsByCaster(uint64 casterGUID) const; int32 GetTotalAuraModifier(AuraType auratype) const; float GetTotalAuraMultiplier(AuraType auratype) const; int32 GetMaxPositiveAuraModifier(AuraType auratype) const; int32 GetMaxNegativeAuraModifier(AuraType auratype) const; int32 GetTotalAuraModifierByMiscMask(AuraType auratype, uint32 misc_mask) const; float GetTotalAuraMultiplierByMiscMask(AuraType auratype, uint32 misc_mask) const; int32 GetMaxPositiveAuraModifierByMiscMask(AuraType auratype, uint32 misc_mask, const AuraEffect* except = NULL) const; int32 GetMaxNegativeAuraModifierByMiscMask(AuraType auratype, uint32 misc_mask) const; int32 GetTotalAuraModifierByMiscValue(AuraType auratype, int32 misc_value) const; float GetTotalAuraMultiplierByMiscValue(AuraType auratype, int32 misc_value) const; int32 GetMaxPositiveAuraModifierByMiscValue(AuraType auratype, int32 misc_value) const; int32 GetMaxNegativeAuraModifierByMiscValue(AuraType auratype, int32 misc_value) const; int32 GetTotalAuraModifierByAffectMask(AuraType auratype, SpellInfo const* affectedSpell) const; float GetTotalAuraMultiplierByAffectMask(AuraType auratype, SpellInfo const* affectedSpell) const; int32 GetMaxPositiveAuraModifierByAffectMask(AuraType auratype, SpellInfo const* affectedSpell) const; int32 GetMaxNegativeAuraModifierByAffectMask(AuraType auratype, SpellInfo const* affectedSpell) const; float GetResistanceBuffMods(SpellSchools school, bool positive) const { return GetFloatValue(positive ? UNIT_FIELD_RESISTANCEBUFFMODSPOSITIVE+school : UNIT_FIELD_RESISTANCEBUFFMODSNEGATIVE+school); } void SetResistanceBuffMods(SpellSchools school, bool positive, float val) { SetFloatValue(positive ? UNIT_FIELD_RESISTANCEBUFFMODSPOSITIVE+school : UNIT_FIELD_RESISTANCEBUFFMODSNEGATIVE+school, val); } void ApplyResistanceBuffModsMod(SpellSchools school, bool positive, float val, bool apply) { ApplyModSignedFloatValue(positive ? UNIT_FIELD_RESISTANCEBUFFMODSPOSITIVE+school : UNIT_FIELD_RESISTANCEBUFFMODSNEGATIVE+school, val, apply); } void ApplyResistanceBuffModsPercentMod(SpellSchools school, bool positive, float val, bool apply) { ApplyPercentModFloatValue(positive ? UNIT_FIELD_RESISTANCEBUFFMODSPOSITIVE+school : UNIT_FIELD_RESISTANCEBUFFMODSNEGATIVE+school, val, apply); } void InitStatBuffMods() { for (uint8 i = STAT_STRENGTH; i < MAX_STATS; ++i) SetFloatValue(UNIT_FIELD_POSSTAT0+i, 0); for (uint8 i = STAT_STRENGTH; i < MAX_STATS; ++i) SetFloatValue(UNIT_FIELD_NEGSTAT0+i, 0); } void ApplyStatBuffMod(Stats stat, float val, bool apply) { ApplyModSignedFloatValue((val > 0 ? UNIT_FIELD_POSSTAT0+stat : UNIT_FIELD_NEGSTAT0+stat), val, apply); } void ApplyStatPercentBuffMod(Stats stat, float val, bool apply) { ApplyPercentModFloatValue(UNIT_FIELD_POSSTAT0+stat, val, apply); ApplyPercentModFloatValue(UNIT_FIELD_NEGSTAT0+stat, val, apply); } void SetCreateStat(Stats stat, float val) { m_createStats[stat] = val; } void SetCreateHealth(uint32 val) { SetUInt32Value(UNIT_FIELD_BASE_HEALTH, val); } uint32 GetCreateHealth() const { return GetUInt32Value(UNIT_FIELD_BASE_HEALTH); } void SetCreateMana(uint32 val) { SetUInt32Value(UNIT_FIELD_BASE_MANA, val); } uint32 GetCreateMana() const { return GetUInt32Value(UNIT_FIELD_BASE_MANA); } uint32 GetCreatePowers(Powers power) const; float GetPosStat(Stats stat) const { return GetFloatValue(UNIT_FIELD_POSSTAT0+stat); } float GetNegStat(Stats stat) const { return GetFloatValue(UNIT_FIELD_NEGSTAT0+stat); } float GetCreateStat(Stats stat) const { return m_createStats[stat]; } void SetCurrentCastedSpell(Spell* pSpell); virtual void ProhibitSpellSchool(SpellSchoolMask /*idSchoolMask*/, uint32 /*unTimeMs*/) { } void InterruptSpell(CurrentSpellTypes spellType, bool withDelayed = true, bool withInstant = true); void FinishSpell(CurrentSpellTypes spellType, bool ok = true); // set withDelayed to true to account delayed spells as casted // delayed+channeled spells are always accounted as casted // we can skip channeled or delayed checks using flags bool IsNonMeleeSpellCasted(bool withDelayed, bool skipChanneled = false, bool skipAutorepeat = false, bool isAutoshoot = false, bool skipInstant = true) const; // set withDelayed to true to interrupt delayed spells too // delayed+channeled spells are always interrupted void InterruptNonMeleeSpells(bool withDelayed, uint32 spellid = 0, bool withInstant = true); Spell* GetCurrentSpell(CurrentSpellTypes spellType) const { return m_currentSpells[spellType]; } Spell* GetCurrentSpell(uint32 spellType) const { return m_currentSpells[spellType]; } Spell* FindCurrentSpellBySpellId(uint32 spell_id) const; int32 GetCurrentSpellCastTime(uint32 spell_id) const; uint32 m_addDmgOnce; uint64 m_SummonSlot[MAX_SUMMON_SLOT]; uint64 m_ObjectSlot[MAX_GAMEOBJECT_SLOT]; ShapeshiftForm GetShapeshiftForm() const { return ShapeshiftForm(GetByteValue(UNIT_FIELD_BYTES_2, 3)); } void SetShapeshiftForm(ShapeshiftForm form) { SetByteValue(UNIT_FIELD_BYTES_2, 3, form); } inline bool IsInFeralForm() const { ShapeshiftForm form = GetShapeshiftForm(); return form == FORM_CAT || form == FORM_BEAR || form == FORM_DIREBEAR; } inline bool IsInDisallowedMountForm() const { ShapeshiftForm form = GetShapeshiftForm(); return form != FORM_NONE && form != FORM_BATTLESTANCE && form != FORM_BERSERKERSTANCE && form != FORM_DEFENSIVESTANCE && form != FORM_SHADOW && form != FORM_STEALTH && form != FORM_UNDEAD; } float m_modMeleeHitChance; float m_modRangedHitChance; float m_modSpellHitChance; int32 m_baseSpellCritChance; float m_threatModifier[MAX_SPELL_SCHOOL]; float m_modAttackSpeedPct[3]; // Event handler EventProcessor m_Events; // stat system bool HandleStatModifier(UnitMods unitMod, UnitModifierType modifierType, float amount, bool apply); void SetModifierValue(UnitMods unitMod, UnitModifierType modifierType, float value) { m_auraModifiersGroup[unitMod][modifierType] = value; } float GetModifierValue(UnitMods unitMod, UnitModifierType modifierType) const; float GetTotalStatValue(Stats stat) const; float GetTotalAuraModValue(UnitMods unitMod) const; SpellSchools GetSpellSchoolByAuraGroup(UnitMods unitMod) const; Stats GetStatByAuraGroup(UnitMods unitMod) const; Powers GetPowerTypeByAuraGroup(UnitMods unitMod) const; bool CanModifyStats() const { return m_canModifyStats; } void SetCanModifyStats(bool modifyStats) { m_canModifyStats = modifyStats; } virtual bool UpdateStats(Stats stat) = 0; virtual bool UpdateAllStats() = 0; virtual void UpdateResistances(uint32 school) = 0; virtual void UpdateArmor() = 0; virtual void UpdateMaxHealth() = 0; virtual void UpdateMaxPower(Powers power) = 0; virtual void UpdateAttackPowerAndDamage(bool ranged = false) = 0; virtual void UpdateDamagePhysical(WeaponAttackType attType) = 0; float GetTotalAttackPowerValue(WeaponAttackType attType) const; float GetWeaponDamageRange(WeaponAttackType attType, WeaponDamageRange type) const; void SetBaseWeaponDamage(WeaponAttackType attType, WeaponDamageRange damageRange, float value) { m_weaponDamage[attType][damageRange] = value; } bool isInFrontInMap(Unit const* target, float distance, float arc = M_PI) const; bool isInBackInMap(Unit const* target, float distance, float arc = M_PI) const; // Visibility system bool IsVisible() const { return (m_serverSideVisibility.GetValue(SERVERSIDE_VISIBILITY_GM) > SEC_PLAYER) ? false : true; } void SetVisible(bool x); // common function for visibility checks for player/creatures with detection code void SetPhaseMask(uint32 newPhaseMask, bool update);// overwrite WorldObject::SetPhaseMask void UpdateObjectVisibility(bool forced = true); SpellImmuneList m_spellImmune[MAX_SPELL_IMMUNITY]; uint32 m_lastSanctuaryTime; // Threat related methods bool CanHaveThreatList() const; void AddThreat(Unit* victim, float fThreat, SpellSchoolMask schoolMask = SPELL_SCHOOL_MASK_NORMAL, SpellInfo const* threatSpell = NULL); float ApplyTotalThreatModifier(float fThreat, SpellSchoolMask schoolMask = SPELL_SCHOOL_MASK_NORMAL); void DeleteThreatList(); void TauntApply(Unit* victim); void TauntFadeOut(Unit* taunter); ThreatManager& getThreatManager() { return m_ThreatManager; } void addHatedBy(HostileReference* pHostileReference) { m_HostileRefManager.insertFirst(pHostileReference); }; void removeHatedBy(HostileReference* /*pHostileReference*/) { /* nothing to do yet */ } HostileRefManager& getHostileRefManager() { return m_HostileRefManager; } VisibleAuraMap const* GetVisibleAuras() { return &m_visibleAuras; } AuraApplication * GetVisibleAura(uint8 slot) { VisibleAuraMap::iterator itr = m_visibleAuras.find(slot); if (itr != m_visibleAuras.end()) return itr->second; return 0; } void SetVisibleAura(uint8 slot, AuraApplication * aur); void RemoveVisibleAura(uint8 slot); uint32 GetInterruptMask() const { return m_interruptMask; } void AddInterruptMask(uint32 mask) { m_interruptMask |= mask; } void UpdateInterruptMask(); uint32 GetDisplayId() { return GetUInt32Value(UNIT_FIELD_DISPLAYID); } void SetDisplayId(uint32 modelId); uint32 GetNativeDisplayId() { return GetUInt32Value(UNIT_FIELD_NATIVEDISPLAYID); } void RestoreDisplayId(); void SetNativeDisplayId(uint32 modelId) { SetUInt32Value(UNIT_FIELD_NATIVEDISPLAYID, modelId); } void setTransForm(uint32 spellid) { m_transform = spellid;} uint32 getTransForm() const { return m_transform;} // DynamicObject management void _RegisterDynObject(DynamicObject* dynObj); void _UnregisterDynObject(DynamicObject* dynObj); DynamicObject* GetDynObject(uint32 spellId); void RemoveDynObject(uint32 spellId); void RemoveAllDynObjects(); GameObject* GetGameObject(uint32 spellId) const; void AddGameObject(GameObject* gameObj); void RemoveGameObject(GameObject* gameObj, bool del); void RemoveGameObject(uint32 spellid, bool del); void RemoveAllGameObjects(); uint32 CalculateDamage(WeaponAttackType attType, bool normalized, bool addTotalPct); float GetAPMultiplier(WeaponAttackType attType, bool normalized); void ModifyAuraState(AuraStateType flag, bool apply); uint32 BuildAuraStateUpdateForTarget(Unit* target) const; bool HasAuraState(AuraStateType flag, SpellInfo const* spellProto = NULL, Unit const* Caster = NULL) const; void UnsummonAllTotems(); Unit* GetMagicHitRedirectTarget(Unit* victim, SpellInfo const* spellInfo); Unit* GetMeleeHitRedirectTarget(Unit* victim, SpellInfo const* spellInfo = NULL); int32 SpellBaseDamageBonus(SpellSchoolMask schoolMask); int32 SpellBaseHealingBonus(SpellSchoolMask schoolMask); int32 SpellBaseDamageBonusForVictim(SpellSchoolMask schoolMask, Unit* victim); int32 SpellBaseHealingBonusForVictim(SpellSchoolMask schoolMask, Unit* victim); uint32 SpellDamageBonus(Unit* victim, SpellInfo const* spellProto, uint32 damage, DamageEffectType damagetype, uint32 stack = 1); uint32 SpellHealingBonus(Unit* victim, SpellInfo const* spellProto, uint32 healamount, DamageEffectType damagetype, uint32 stack = 1); bool isSpellBlocked(Unit* victim, SpellInfo const* spellProto, WeaponAttackType attackType = BASE_ATTACK); bool isBlockCritical(); bool isSpellCrit(Unit* victim, SpellInfo const* spellProto, SpellSchoolMask schoolMask, WeaponAttackType attackType = BASE_ATTACK) const; uint32 SpellCriticalDamageBonus(SpellInfo const* spellProto, uint32 damage, Unit* victim); uint32 SpellCriticalHealingBonus(SpellInfo const* spellProto, uint32 damage, Unit* victim); void SetLastManaUse(uint32 spellCastTime) { m_lastManaUse = spellCastTime; } bool IsUnderLastManaUseEffect() const; void SetContestedPvP(Player* attackedPlayer = NULL); void MeleeDamageBonus(Unit* victim, uint32 *damage, WeaponAttackType attType, SpellInfo const* spellProto = NULL); uint32 GetCastingTimeForBonus(SpellInfo const* spellProto, DamageEffectType damagetype, uint32 CastingTime); uint32 GetRemainingPeriodicAmount(uint64 caster, uint32 spellId, AuraType auraType, uint8 effectIndex = 0) const; void ApplySpellImmune(uint32 spellId, uint32 op, uint32 type, bool apply); void ApplySpellDispelImmunity(const SpellInfo* spellProto, DispelType type, bool apply); virtual bool IsImmunedToSpell(SpellInfo const* spellInfo); // redefined in Creature bool IsImmunedToDamage(SpellSchoolMask meleeSchoolMask); bool IsImmunedToDamage(SpellInfo const* spellInfo); virtual bool IsImmunedToSpellEffect(SpellInfo const* spellInfo, uint32 index) const; // redefined in Creature static bool IsDamageReducedByArmor(SpellSchoolMask damageSchoolMask, SpellInfo const* spellInfo = NULL, uint8 effIndex = MAX_SPELL_EFFECTS); uint32 CalcArmorReducedDamage(Unit* victim, const uint32 damage, SpellInfo const* spellInfo, WeaponAttackType attackType=MAX_ATTACK); void CalcAbsorbResist(Unit* victim, SpellSchoolMask schoolMask, DamageEffectType damagetype, const uint32 damage, uint32 *absorb, uint32 *resist, SpellInfo const* spellInfo = NULL); void CalcHealAbsorb(Unit* victim, const SpellInfo* spellProto, uint32 &healAmount, uint32 &absorb); void UpdateSpeed(UnitMoveType mtype, bool forced); float GetSpeed(UnitMoveType mtype) const; float GetSpeedRate(UnitMoveType mtype) const { return m_speed_rate[mtype]; } void SetSpeed(UnitMoveType mtype, float rate, bool forced = false); float m_TempSpeed; bool isHover() const { return HasAuraType(SPELL_AURA_HOVER); } float ApplyEffectModifiers(SpellInfo const* spellProto, uint8 effect_index, float value) const; int32 CalculateSpellDamage(Unit const* target, SpellInfo const* spellProto, uint8 effect_index, int32 const* basePoints = NULL) const; int32 CalcSpellDuration(SpellInfo const* spellProto); int32 ModSpellDuration(SpellInfo const* spellProto, Unit const* target, int32 duration, bool positive, uint32 effectMask); void ModSpellCastTime(SpellInfo const* spellProto, int32 & castTime, Spell* spell=NULL); float CalculateLevelPenalty(SpellInfo const* spellProto) const; void addFollower(FollowerReference* pRef) { m_FollowingRefManager.insertFirst(pRef); } void removeFollower(FollowerReference* /*pRef*/) { /* nothing to do yet */ } static Unit* GetUnit(WorldObject& object, uint64 guid); static Player* GetPlayer(WorldObject& object, uint64 guid); static Creature* GetCreature(WorldObject& object, uint64 guid); MotionMaster* GetMotionMaster() { return &i_motionMaster; } const MotionMaster* GetMotionMaster() const { return &i_motionMaster; } bool IsStopped() const { return !(HasUnitState(UNIT_STATE_MOVING)); } void StopMoving(); void AddUnitMovementFlag(uint32 f) { m_movementInfo.flags |= f; } void RemoveUnitMovementFlag(uint32 f) { m_movementInfo.flags &= ~f; } bool HasUnitMovementFlag(uint32 f) const { return (m_movementInfo.flags & f) == f; } uint32 GetUnitMovementFlags() const { return m_movementInfo.flags; } void SetUnitMovementFlags(uint32 f) { m_movementInfo.flags = f; } void AddExtraUnitMovementFlag(uint16 f) { m_movementInfo.flags2 |= f; } void RemoveExtraUnitMovementFlag(uint16 f) { m_movementInfo.flags2 &= ~f; } uint16 HasExtraUnitMovementFlag(uint16 f) const { return m_movementInfo.flags2 & f; } uint16 GetExtraUnitMovementFlags() const { return m_movementInfo.flags2; } void SetExtraUnitMovementFlags(uint16 f) { m_movementInfo.flags2 = f; } float GetPositionZMinusOffset() const { float offset = 0.0f; if (HasUnitMovementFlag(MOVEMENTFLAG_HOVER)) offset = GetFloatValue(UNIT_FIELD_HOVERHEIGHT); return GetPositionZ() - offset; } void SetControlled(bool apply, UnitState state); void AddComboPointHolder(uint32 lowguid) { m_ComboPointHolders.insert(lowguid); } void RemoveComboPointHolder(uint32 lowguid) { m_ComboPointHolders.erase(lowguid); } void ClearComboPointHolders(); ///----------Pet responses methods----------------- void SendPetCastFail(uint32 spellid, SpellCastResult msg); void SendPetActionFeedback (uint8 msg); void SendPetTalk (uint32 pettalk); void SendPetAIReaction(uint64 guid); ///----------End of Pet responses methods---------- void propagateSpeedChange() { GetMotionMaster()->propagateSpeedChange(); } // reactive attacks void ClearAllReactives(); void StartReactiveTimer(ReactiveType reactive) { m_reactiveTimer[reactive] = REACTIVE_TIMER_START;} void UpdateReactives(uint32 p_time); // group updates void UpdateAuraForGroup(uint8 slot); // proc trigger system bool CanProc(){return !m_procDeep;} void SetCantProc(bool apply) { if (apply) ++m_procDeep; else { ASSERT(m_procDeep); --m_procDeep; } } // pet auras typedef std::set<PetAura const*> PetAuraSet; PetAuraSet m_petAuras; void AddPetAura(PetAura const* petSpell); void RemovePetAura(PetAura const* petSpell); uint32 GetModelForForm(ShapeshiftForm form); uint32 GetModelForTotem(PlayerTotemType totemType); void SetReducedThreatPercent(uint32 pct, uint64 guid) { m_reducedThreatPercent = pct; m_misdirectionTargetGUID = guid; } uint32 GetReducedThreatPercent() { return m_reducedThreatPercent; } Unit* GetMisdirectionTarget() { return m_misdirectionTargetGUID ? GetUnit(*this, m_misdirectionTargetGUID) : NULL; } bool IsAIEnabled, NeedChangeAI; bool CreateVehicleKit(uint32 id, uint32 creatureEntry); void RemoveVehicleKit(); Vehicle* GetVehicleKit()const { return m_vehicleKit; } Vehicle* GetVehicle() const { return m_vehicle; } bool IsOnVehicle(const Unit* vehicle) const { return m_vehicle && m_vehicle == vehicle->GetVehicleKit(); } Unit* GetVehicleBase() const; Creature* GetVehicleCreatureBase() const; float GetTransOffsetX() const { return m_movementInfo.t_pos.GetPositionX(); } float GetTransOffsetY() const { return m_movementInfo.t_pos.GetPositionY(); } float GetTransOffsetZ() const { return m_movementInfo.t_pos.GetPositionZ(); } float GetTransOffsetO() const { return m_movementInfo.t_pos.GetOrientation(); } uint32 GetTransTime() const { return m_movementInfo.t_time; } int8 GetTransSeat() const { return m_movementInfo.t_seat; } uint64 GetTransGUID() const; bool m_ControlledByPlayer; bool HandleSpellClick(Unit* clicker, int8 seatId = -1); void EnterVehicle(Unit* base, int8 seatId = -1); void ExitVehicle(Position const* exitPosition = NULL); void ChangeSeat(int8 seatId, bool next = true); // Should only be called by AuraEffect::HandleAuraControlVehicle(AuraApplication const* auraApp, uint8 mode, bool apply) const; void _ExitVehicle(Position const* exitPosition = NULL); void _EnterVehicle(Vehicle* vehicle, int8 seatId, AuraApplication const* aurApp = NULL); void BuildMovementPacket(ByteBuffer *data) const; bool isMoving() const { return m_movementInfo.HasMovementFlag(MOVEMENTFLAG_MASK_MOVING); } bool isTurning() const { return m_movementInfo.HasMovementFlag(MOVEMENTFLAG_MASK_TURNING); } virtual bool CanFly() const = 0; bool IsFlying() const { return m_movementInfo.HasMovementFlag(MOVEMENTFLAG_FLYING | MOVEMENTFLAG_DISABLE_GRAVITY); } void SetCanFly(bool apply); void RewardRage(uint32 damage, uint32 weaponSpeedHitFactor, bool attacker); virtual float GetFollowAngle() const { return static_cast<float>(M_PI/2); } void OutDebugInfo() const; virtual bool isBeingLoaded() const { return false;} bool IsDuringRemoveFromWorld() const {return m_duringRemoveFromWorld;} Pet* ToPet(){ if (isPet()) return reinterpret_cast<Pet*>(this); else return NULL; } Totem* ToTotem(){ if (isTotem()) return reinterpret_cast<Totem*>(this); else return NULL; } TempSummon* ToTempSummon() { if (isSummon()) return reinterpret_cast<TempSummon*>(this); else return NULL; } const TempSummon* ToTempSummon() const { if (isSummon()) return reinterpret_cast<const TempSummon*>(this); else return NULL; } void SetTarget(uint64 guid) { if (!_targetLocked) SetUInt64Value(UNIT_FIELD_TARGET, guid); } void FocusTarget(Spell const* focusSpell, uint64 target) { // already focused if (_focusSpell) return; _focusSpell = focusSpell; _targetLocked = true; SetUInt64Value(UNIT_FIELD_TARGET, target); } void ReleaseFocus(Spell const* focusSpell) { // focused to something else if (focusSpell != _focusSpell) return; _focusSpell = NULL; _targetLocked = false; if (Unit* victim = getVictim()) SetUInt64Value(UNIT_FIELD_TARGET, victim->GetGUID()); else SetUInt64Value(UNIT_FIELD_TARGET, 0); } // Movement info Movement::MoveSpline * movespline; protected: explicit Unit (bool isWorldObject); UnitAI* i_AI, *i_disabledAI; void _UpdateSpells(uint32 time); void _DeleteRemovedAuras(); void _UpdateAutoRepeatSpell(); bool m_AutoRepeatFirstCast; uint32 m_attackTimer[MAX_ATTACK]; float m_createStats[MAX_STATS]; AttackerSet m_attackers; Unit* m_attacking; DeathState m_deathState; int32 m_procDeep; typedef std::list<DynamicObject*> DynObjectList; DynObjectList m_dynObj; typedef std::list<GameObject*> GameObjectList; GameObjectList m_gameObj; bool m_isSorted; uint32 m_transform; Spell* m_currentSpells[CURRENT_MAX_SPELL]; AuraMap m_ownedAuras; AuraApplicationMap m_appliedAuras; AuraList m_removedAuras; AuraMap::iterator m_auraUpdateIterator; uint32 m_removedAurasCount; AuraEffectList m_modAuras[TOTAL_AURAS]; AuraList m_scAuras; // casted singlecast auras AuraApplicationList m_interruptableAuras; // auras which have interrupt mask applied on unit AuraStateAurasMap m_auraStateAuras; // Used for improve performance of aura state checks on aura apply/remove uint32 m_interruptMask; float m_auraModifiersGroup[UNIT_MOD_END][MODIFIER_TYPE_END]; float m_weaponDamage[MAX_ATTACK][2]; bool m_canModifyStats; VisibleAuraMap m_visibleAuras; float m_speed_rate[MAX_MOVE_TYPE]; CharmInfo* m_charmInfo; SharedVisionList m_sharedVision; virtual SpellSchoolMask GetMeleeDamageSchoolMask() const; MotionMaster i_motionMaster; uint32 m_reactiveTimer[MAX_REACTIVE]; uint32 m_regenTimer; ThreatManager m_ThreatManager; Vehicle* m_vehicle; Vehicle* m_vehicleKit; uint32 m_unitTypeMask; bool IsAlwaysVisibleFor(WorldObject const* seer) const; bool IsAlwaysDetectableFor(WorldObject const* seer) const; void DisableSpline(); private: bool IsTriggeredAtSpellProcEvent(Unit* victim, Aura* aura, SpellInfo const* procSpell, uint32 procFlag, uint32 procExtra, WeaponAttackType attType, bool isVictim, bool active, SpellProcEventEntry const* & spellProcEvent); bool HandleDummyAuraProc(Unit* victim, uint32 damage, AuraEffect* triggeredByAura, SpellInfo const* procSpell, uint32 procFlag, uint32 procEx, uint32 cooldown); bool HandleHasteAuraProc(Unit* victim, uint32 damage, AuraEffect* triggeredByAura, SpellInfo const* procSpell, uint32 procFlag, uint32 procEx, uint32 cooldown); bool HandleSpellCritChanceAuraProc(Unit* victim, uint32 damage, AuraEffect* triggredByAura, SpellInfo const* procSpell, uint32 procFlag, uint32 procEx, uint32 cooldown); bool HandleObsModEnergyAuraProc(Unit* victim, uint32 damage, AuraEffect* triggeredByAura, SpellInfo const* procSpell, uint32 procFlag, uint32 procEx, uint32 cooldown); bool HandleModDamagePctTakenAuraProc(Unit* victim, uint32 damage, AuraEffect* triggeredByAura, SpellInfo const* procSpell, uint32 procFlag, uint32 procEx, uint32 cooldown); bool HandleAuraProc(Unit* victim, uint32 damage, Aura* triggeredByAura, SpellInfo const* procSpell, uint32 procFlag, uint32 procEx, uint32 cooldown, bool * handled); bool HandleProcTriggerSpell(Unit* victim, uint32 damage, AuraEffect* triggeredByAura, SpellInfo const* procSpell, uint32 procFlag, uint32 procEx, uint32 cooldown); bool HandleOverrideClassScriptAuraProc(Unit* victim, uint32 damage, AuraEffect* triggeredByAura, SpellInfo const* procSpell, uint32 cooldown); bool HandleAuraRaidProcFromChargeWithValue(AuraEffect* triggeredByAura); bool HandleAuraRaidProcFromCharge(AuraEffect* triggeredByAura); void UpdateSplineMovement(uint32 t_diff); // player or player's pet float GetCombatRatingReduction(CombatRating cr) const; uint32 GetCombatRatingDamageReduction(CombatRating cr, float rate, float cap, uint32 damage) const; void SetFeared(bool apply); void SetConfused(bool apply); void SetStunned(bool apply); void SetRooted(bool apply); uint32 m_rootTimes; uint32 m_state; // Even derived shouldn't modify uint32 m_CombatTimer; uint32 m_lastManaUse; // msecs TimeTrackerSmall m_movesplineTimer; Diminishing m_Diminishing; // Manage all Units that are threatened by us HostileRefManager m_HostileRefManager; FollowerRefManager m_FollowingRefManager; ComboPointHolderSet m_ComboPointHolders; uint32 m_reducedThreatPercent; uint64 m_misdirectionTargetGUID; bool m_cleanupDone; // lock made to not add stuff after cleanup before delete bool m_duringRemoveFromWorld; // lock made to not add stuff after begining removing from world Spell const* _focusSpell; bool _targetLocked; // locks the target during spell cast for proper facing }; namespace Trinity { // Binary predicate for sorting Units based on percent value of a power class PowerPctOrderPred { public: PowerPctOrderPred(Powers power, bool ascending = true) : m_power(power), m_ascending(ascending) {} bool operator() (const Unit* a, const Unit* b) const { float rA = a->GetMaxPower(m_power) ? float(a->GetPower(m_power)) / float(a->GetMaxPower(m_power)) : 0.0f; float rB = b->GetMaxPower(m_power) ? float(b->GetPower(m_power)) / float(b->GetMaxPower(m_power)) : 0.0f; return m_ascending ? rA < rB : rA > rB; } private: const Powers m_power; const bool m_ascending; }; // Binary predicate for sorting Units based on percent value of health class HealthPctOrderPred { public: HealthPctOrderPred(bool ascending = true) : m_ascending(ascending) {} bool operator() (const Unit* a, const Unit* b) const { float rA = a->GetMaxHealth() ? float(a->GetHealth()) / float(a->GetMaxHealth()) : 0.0f; float rB = b->GetMaxHealth() ? float(b->GetHealth()) / float(b->GetMaxHealth()) : 0.0f; return m_ascending ? rA < rB : rA > rB; } private: const bool m_ascending; }; } #endif
6cb86d493da3c083dd1d3f2738f1c72255609c21
f92d576a21fb41ca65b50874f24d5dfff4ec5528
/src/io/ArgParser.cpp
a365f2d3e2c348609866fd605506c23b1cf5d58f
[ "MIT" ]
permissive
WenYuZhi/GPH
293f2e788cffe300f701059c48d9e31bf346b3ee
c3ded30f5cd780a86e8c92d9db6ea89764fcc499
refs/heads/master
2023-06-19T20:34:46.366597
2020-04-18T18:53:32
2020-04-18T18:53:32
null
0
0
null
null
null
null
UTF-8
C++
false
false
1,571
cpp
#include "ArgParser.h" #include "Message.h" #include "clipp/clipp.h" #include <iostream> #include <limits> std::optional<ArgInfo> parseArgs(int argc, char** argv) { using namespace clipp; ArgInfo arginfo; arginfo.timelimit = std::numeric_limits<int>::max(); arginfo.nthreads = -1; arginfo.probFile = "mip.mps"; arginfo.writeSol = false; #ifndef NDEBUG arginfo.verbosity = 2; #endif auto cli = (value("input file", arginfo.probFile), option("-l") & value("tlimit", arginfo.timelimit) .doc("time limit in seconds"), #ifndef NDEBUG option("-v") & value("verbosity", arginfo.verbosity), #endif option("-t", "--thread") & value("nthreads", arginfo.nthreads) .doc("number of threads to use"), option("-w").set(arginfo.writeSol).doc("write solution to disk"), option("-s", "--solution") & value("start_sol", arginfo.solutionFile) .doc("path to solution to improve"), option("-c", "--config") & value("config", arginfo.configFile).doc("configuration file")); if (!parse(argc, argv, cli)) { std::cout << make_man_page(cli, argv[0]); return {}; } #ifndef NDEBUG // set verbosity level switch (arginfo.verbosity) { case 1: Message::verbosity = Message::QUIET; break; case 2: Message::verbosity = Message::DEBUG; break; case 3: Message::verbosity = Message::DEBUG_DETAILS; break; } #endif return arginfo; }
c61742f4b0a48f5307a3e1daad5fc949c2233e07
b73eec3a26bdcffb6a19dec6b8b048079befe04c
/3rdparty/meshlab-master/src/meshlabplugins/edit_align/align/OccupancyGrid.h
83894498296ffd6d1967550b844f8da953aa9096
[ "GPL-1.0-or-later", "GPL-3.0-only", "MIT" ]
permissive
HoEmpire/slambook2
c876494174e7f636bdf5b5743fab7d9918c52898
96d360f32aa5d8b5c5dcbbf9ee7ba865e84409f4
refs/heads/master
2020-07-24T02:35:39.488466
2019-11-25T03:08:17
2019-11-25T03:08:17
207,775,582
0
0
MIT
2019-09-11T09:35:56
2019-09-11T09:35:56
null
UTF-8
C++
false
false
6,101
h
/**************************************************************************** * VCGLib o o * * Visual and Computer Graphics Library o o * * _ O _ * * Copyright(C) 2004 \/)\/ * * Visual Computing Lab /\/| * * ISTI - Italian National Research Council | * * \ * * All rights reserved. * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * * for more details. * * * ****************************************************************************/ #ifndef ALIGN_OCCUPANCY_GRID_H #define ALIGN_OCCUPANCY_GRID_H #include "AlignPair.h" #include <bitset> #include <vcg/space/index/grid_static_obj.h> namespace vcg { /* Occupancy Grid * Used to find the mesh pairs (arcs) to be used for ICP * It counts over the cell of a grid how many meshes passes through that cell. * It compute the relative overlaps and returns a set of arcs with overlap greater than a given threshold. */ class OccupancyGrid{ public: typedef AlignPair::A2Mesh A2Mesh; // Class to keep for each voxel the id of the mesh passing throuhg it. // based on bitset class MeshCounter { private: #define OG_MAX_MCB_SIZE 2048 std::bitset<OG_MAX_MCB_SIZE> cnt; public: static int MaxVal() {return OG_MAX_MCB_SIZE;} bool Empty() const {return cnt.none();} void Clear() {cnt.reset();} bool IsSet(size_t i) const {return cnt.test(i);} void Set(size_t i) {cnt.set(i);} void UnSet(size_t i) {cnt.reset(i);} size_t Count() const { return cnt.count();} // Return a vector with all the id of the meshes void Pack(std::vector<int> &v) const { v.clear(); for(size_t i=0;i<OG_MAX_MCB_SIZE;++i) if(cnt.test(int(i))) v.push_back(int(i)); } bool operator < (const MeshCounter &c) const { size_t ii=0; if(cnt==c.cnt) return false; while(ii<OG_MAX_MCB_SIZE){ if(cnt[ii]!=c.cnt[ii]) return cnt[ii]<c.cnt[ii]; ++ii; } return false; } }; // Class for collecting cumulative information about each mesh in the OG. // This info are collected in the Compute() by scanning the OG after we filled it with all the meshes. class OGMeshInfo { public: OGMeshInfo() {Init(-1); used=false;} void Init(int _id) { coverage=0;area=0; id=_id; } int id; // the id of the mesh int area; // number of voxels in the OG touched by this mesh int coverage; // quanto e' ricoperta da altre mesh eccetto se stessa (eg: se ho due mesh di 1000 con overlap al 30% la covrg e' 300) std::vector<int> densityDistribution; // Distribution of the of the density of the voxels touched by this mesh: // densityDistribution[i] says how many voxel (among the ones coverd by this mesh) // are covered by <i> othermeshes. Sum(densityDistribution) == area; // if densityDistribution[1] > 0 means that this mesh is the unique to cover some portion of the space. bool operator < (OGMeshInfo &o) const { return area<o.area;} static int MaxStat() { return 64;} bool used; }; /* Classe con informazioni su un arco plausibile */ class OGArcInfo { public: enum sort {AREA,NORM_AREA,DEGREE}; int s,t; // source and target (come indici nel gruppo corrente) //ArcPt a; int area; // float norm_area; OGArcInfo(int _s,int _t,int _area,float _n){s=_s;t=_t;area=_area;norm_area=_n;} OGArcInfo(int _s,int _t,int _a){s=_s;t=_t;area=_a;} bool operator < (const OGArcInfo &p) const {return norm_area < p.norm_area;} }; void Clear() {}; bool Init(int _mn, Box3d bb, int size); void Add(const char *MeshName, Matrix44d &Tr, int id); void AddMeshes(std::vector<std::string> &names, std::vector<Matrix44d> &trv,int size); template <class MESH> void AddMesh(MESH &M, const Matrix44d &Tr, int ind); void RemoveMesh(int id); void ChooseArcs(std::vector<std::pair<int,int> > &AV, std::vector<int> &BNV,std::vector<int> &adjcnt, float normarea= 0.3); void Compute(); void ComputeUsefulMesh(FILE *elfp=0); void Dump(FILE *fp); void ComputeTotalArea(); GridStaticObj<MeshCounter, float> G; int mn; int TotalArea; int MaxCount; // massimo numero di mesh che passano per una cella; std::vector<OGArcInfo> SVA; // SortedVirtual Arcs; std::map<int,OGMeshInfo> VM; // High level information for each mesh. Mapped by mesh id }; // Implementation of the templated AddMesh template <class MESH> void OccupancyGrid::AddMesh(MESH &M, const Matrix44d &Tr, int ind) { Matrix44f Trf; Trf.Import(Tr); typename MESH::VertexIterator vi; for(vi=M.vert.begin();vi!=M.vert.end();++vi) { if(!(*vi).IsD()) G.Grid( Trf * Point3f::Construct((*vi).P()) ).Set(ind); } VM[ind].Init(ind); VM[ind].used=true; } } #endif
a53b0a4cf1170cb16520715fd884aa7e9fbd056e
bb998a6e9fcd5615c707b627a351bca1733090d4
/src/include/mcmcutil.hpp
0c8a9e8cf35452ed2377561addf4616c6f29c3bf
[]
no_license
heavenmarshal/lasvdinv
231cc25cbd633beeb5c8605928ff38c8e65e5766
442b7c9a4a6c4a05369c33cea0d3256d47c603ba
refs/heads/master
2021-04-03T05:58:33.300063
2018-07-29T15:37:22
2018-07-29T15:37:22
124,973,752
0
0
null
null
null
null
UTF-8
C++
false
false
2,804
hpp
#ifndef __MCMCUTIL_HPP__ #define __MCMCUTIL_HPP__ #include<cmath> #include<cstdlib> #include<ctime> #include<random> extern "C"{ #include "matrix.h" } #define LOG2PI 1.83787706641 #define SQ(x) ((x)*(x)) class priorBase{ public: priorBase(int nparam_):nparam(nparam_){}; virtual ~priorBase(){}; virtual double evalLogPrior(const double* param){ return 0.0;}; protected: int nparam; }; class kernelBase{ public: kernelBase(int nparam_): nparam(nparam_), generator(time(NULL)){}; virtual ~kernelBase(){}; virtual void propose(double* from, double* to){}; virtual double logDensity(double* from, double* to){return 0.0;}; protected: int nparam; std::default_random_engine generator; }; class likelihoodBase{ public: likelihoodBase(int nparam_): nparam(nparam_){}; virtual ~likelihoodBase(){}; virtual double evalLogLikelihood(double* param){ return 0.0;}; protected: int nparam; }; class uniformPrior: public priorBase{ public: uniformPrior(int nparam_, double *lb, double *ub): priorBase(nparam_) { lowb = new_dup_vector(lb,nparam_); upb = new_dup_vector(ub,nparam_); } ~uniformPrior() { free(lowb); free(upb); } double evalLogPrior(const double* param) { bool isin = true; int i; double logprior; for(i=0; i<nparam; ++i) if(param[i] > upb[i] || param[i] < lowb[i]) { isin = false; break; } logprior = isin? 0.0: -INFINITY; return logprior; } private: double *lowb; double *upb; }; class mcmcBase{ public: mcmcBase(int nparam_,int nmc_, int nburn_, int nthin_, double* x0, double post0, priorBase* prior_, likelihoodBase* likelihood_, kernelBase* kernel_); mcmcBase(int nparam_, int nmc_, int nburn_, int nthin_, double* x0, double post0, priorBase *prior_); virtual ~mcmcBase(); virtual void run(); void getSample(double* output); protected: int nparam; int nmc; int nburn; int nthin; int nsample; int naccept; double clogpost; double* current; double** sample; std::default_random_engine generator; priorBase* prior; likelihoodBase* likelihood; kernelBase* kernel; double evalLogPosterior(double *param); }; class normalKernel: public kernelBase{ public: normalKernel(int nparam_, double sigma_): kernelBase(nparam_), sigma(sigma_){}; double logDensity(double* from, double *to) { int i; double tmp, logden; tmp = 0.0; for(i=0; i<nparam; ++i) tmp -= SQ(from[i]-to[i]); logden = -0.5*LOG2PI-nparam*log(sigma); logden += 0.5*tmp/sigma/sigma; return logden; } void propose(double* from, double* to); private: double sigma; }; #endif
d565d42c4c7dd4498bce9fa00fd927e70094e3af
5d1524ba83dc2a13ce87c07f70dd7f2911cf64c9
/pcp.cpp
a76ec28f916cae2242e4e04f47a1346af64d5aa1
[]
no_license
Horolsky/pcp
9c257973f49d5c8404216d99bbc62e691fb177de
9e0cb130f71f97c886e9ee71533803ecff9035fe
refs/heads/master
2023-08-03T16:33:48.087810
2021-09-04T13:52:11
2021-09-04T13:52:11
null
0
0
null
null
null
null
UTF-8
C++
false
false
4,583
cpp
/** * Producer-Consumer Problem sandbox * semaphores solution */ #include <atomic> #include <chrono> #include <iostream> #include <string> #include <thread> #include <condition_variable> #include "deque.hpp" std::mutex buffer_mutex; std::mutex factory_mutex; std::atomic<int> fillCount; std::atomic<int> emptyCount; bool logging {false}; class ItemFactory { public: ItemFactory() : m_jobs(100) { log("init"); } ItemFactory(int jobs) : m_jobs(jobs) { log("init"); } std::string produce() { factory_mutex.lock(); std::string item = "item n. " + std::to_string(++produced_items); if (logging) log("produced "+item); factory_mutex.unlock(); return item; } void consume(std::string item) { factory_mutex.lock(); log("consumed "+item); ++consumed_items; factory_mutex.unlock(); } bool consuming_in_progress() const { return consumed_items < m_jobs; } bool producing_in_progress() const { return produced_items < m_jobs; } private: void log(std::string &&msg) { if (logging) { std::cout << "ItFact [" << this << "] " << msg << std::endl; std::cout << " - prod: " << produced_items << ", cons: " << consumed_items << std::endl; } } int produced_items {0}; int consumed_items {0}; int m_jobs; }; class Buffer { public: static const int CAPACITY{32}; // Buffer() { m_deque = deque<std::string, CAPACITY>(); log("init"); }; // ~Buffer() { delete m_deque; } void put(std::string item) { log("put call with " + item); m_deque.push_front(item); log("pushed " + item); } std::string remove() { log("remove call"); auto item = m_deque.pop_back(); log("removed " + item); return item; } int size() const { return m_deque.size(); } private: deque<std::string, CAPACITY> m_deque {}; void log(std::string &&msg) { if (logging) { std::cout << "Buffer [" << this << "] " << msg << std::endl; std::cout << " - size: " << m_deque.size() << std::endl; } } }; ItemFactory factory; Buffer buffer; void producer() { while (factory.producing_in_progress()) { buffer_mutex.lock(); if (emptyCount > 0) { try { auto item = factory.produce(); --emptyCount; buffer.put(item); ++fillCount; } catch(const std::exception& e) { std::cerr << e.what() << '\n'; std::cerr << "emptyCount: " << emptyCount << '\n'; exit(1); } } buffer_mutex.unlock(); } } void consumer() { while (factory.consuming_in_progress()) { buffer_mutex.lock(); if (fillCount > 0) { try { --fillCount; auto item = buffer.remove(); ++emptyCount; factory.consume(item); } catch(const std::exception& e) { std::cerr << e.what() << '\n'; std::cerr << "fillCount: " << fillCount << '\n'; exit(1); } } buffer_mutex.unlock(); } } int main (int argc, char* argv[]) { if (argc < 4) { std::cout << "pcp args: <n of jobs> <nof producers> <n of consumers>\n"; } int n_jobs = std::stoi(argv[1]); int n_prods = std::stoi(argv[2]); int n_cons = std::stoi(argv[3]); if (argc == 5 && std::stoi(argv[4]) == 1) logging = true; fillCount = 0; emptyCount = Buffer::CAPACITY; factory = ItemFactory(n_jobs); buffer = Buffer(); std::thread *prods = new std::thread[n_prods]; std::thread *cons = new std::thread[n_cons]; auto start = std::chrono::steady_clock::now(); for (int i = 0; i < n_prods; ++i) { prods[i] = std::thread(producer); } for (int i = 0; i < n_cons; ++i) { cons[i] = std::thread(consumer); } for (int i = 0; i < n_prods; ++i) { prods[i].join(); } for (int i = 0; i < n_cons; ++i) { cons[i].join(); } auto end = std::chrono::steady_clock::now(); std::cout << "working time: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << std::endl; delete[] prods; delete[] cons; return 0; }
884205de1179a49df21d8669682e90ea2398e698
ad0d8d56ee5e843018d47e5d402dc2a743afd9e4
/src/EngineImpl.hpp
6b380c394dda419ca8c5eced9aa2e54ac457d186
[]
no_license
tsup/tsyn
9d1f8d3b6e8fb4ee68d634a6a45db92adace93fd
e2d5eefc533a6d9a37f6051630a71e7a6f5ab881
refs/heads/master
2020-12-24T13:21:10.428975
2013-05-01T13:02:51
2013-05-01T13:04:54
null
0
0
null
null
null
null
UTF-8
C++
false
false
959
hpp
#pragma once #include "Types.hpp" #include "RingBuffer.hpp" namespace tsyn { class Clock; class EngineImpl { public: EngineImpl() = delete; EngineImpl( const EngineImpl& ) = delete; EngineImpl& operator=( const EngineImpl& ) = delete; EngineImpl( const Clock& clk ) : m_sendQueue() , m_receiveQueue() , m_clock( clk ) { } template < class Starter > void start( Starter starter ) { starter( m_sendQueue, m_receiveQueue ); } bool send( const Peer::Id& peerId, Data&& payload ) { return m_sendQueue.push( SendQueueData::Ref( new SendQueueData{ m_clock.now(), peerId, payload } ) ); } bool receive( QueueData::Ref& queueData ) { return m_receiveQueue.pop( queueData ); } private: SendQueue m_sendQueue; ReceiveQueue m_receiveQueue; const Clock& m_clock; }; }
8479e72a26353935b88643c5478d5a0ba6093655
6df12ed6d237c8bb015be3a5362eb626ea4ecf8b
/Program6_Marcus_Garity_/Source.cpp
52bbef5a6b8ca2806b5765d3f3dfb387c3f051f1
[]
no_license
MichaelEssmyer/201R-Spring-2020
1aeaa66042609ba85107ba903913e6f9d0b33f84
aca9be0c76b1b6c5faee5fe927ade8f081d4e73a
refs/heads/master
2020-12-18T15:41:45.129492
2020-05-05T07:11:58
2020-05-05T07:11:58
235,436,706
0
16
null
2020-02-04T05:56:09
2020-01-21T20:37:19
null
UTF-8
C++
false
false
2,284
cpp
// Name: Marcus Garity // Email: [email protected] // Program: 6 // Created Date: 4/12/2020 // Due Date: 4/16/2020 #include <iostream> #include <string> #include <vector> #include <cctype> // is a recursive function that takes in token, token goals, turns, and augmentations std::string FindPath(int, int, int); // main game loop of the program. void GameLoop(); int main() { GameLoop(); } void GameLoop() { bool valid = true; char UserInput; std::string garbage; while (valid) { int token_goal; int turns; std::cout << "Enter the number of tokens you want to reach:" << std::endl; std::cin >> token_goal; std::cin.ignore(); std::cout << "What is the number of turns:" << std::endl; std::cin >> turns; std::cin.ignore(); std::cout << "Searching for solution within 10 turn(s)..." << std::endl; std::cout << FindPath(token_goal, turns, 13); std::cout << "Would you like to play again? [Y/N]" << std::endl; std::cin >> UserInput; std::getline(std::cin, garbage); if (std::tolower(UserInput) == 'n') valid = false; } } std::string FindPathadd(int token_goal, int turns, int tokens) { return "Adding 25, you get " + std::to_string(tokens + 25) + " tokens.\n" + FindPath(token_goal, turns - 1, tokens + 25); } std::string FindPathhalf(int token_goal, int turns, int tokens) { return "Reducing by half, you get " + std::to_string(tokens / 2) + " tokens.\n" + FindPath(token_goal, turns - 1, tokens / 2); } std::string FindPath(int token_goal, int turns, int tokens) { std::string (*Branch[2])(int token_goal, int turns, int tokens); Branch[0] = &FindPathhalf; Branch[1] = &FindPathadd; if (tokens == token_goal) return " Solution found with 1 turn(s) remaining.\n"; if (turns == 0) return " Sorry, solution wasn't found within allotted turns.\n"; if (tokens % 2 != 0) return (*Branch[1])(token_goal, turns, tokens); if (tokens % 2 == 0) { for (int i = 0; i < 2; i++) { return (*Branch[i])(token_goal,turns,tokens); } //return "Adding 25, you get " + std::to_string(tokens+25)+" tokens.\n" + FindPath(token_goal, turns - 1, tokens + 25); //return "Reducing by half you get" + std::to_string(tokens/2)+" tokens.\n" + FindPath(token_goal, turns - 1, tokens/2); } }
b9783a65fe58fab1d5f2623e2013e2f93f766da0
f26310396bec198fad8ed0d1c7c10228ac57efd6
/Info702/TP1/tp2-PIZZO-MARIE/MedianFilter.cpp
2ba6f1624df429f54ce35c7666352e64cc70f271
[]
no_license
KerTakanov/TPS_Master
ecbe07fbe792ef1ae9b57700f7a81a9e8b79660c
90df5247f2985951145bd4d8ac808c96717d9cef
refs/heads/master
2021-09-14T12:31:33.314800
2018-05-13T23:27:10
2018-05-13T23:27:10
106,376,810
0
1
null
null
null
null
UTF-8
C++
false
false
1,173
cpp
// // Created by Meimei on 23/10/2017. // #include "MedianFilter.h" #include <algorithm> MedianFilter::MedianFilter() { } MedianFilter::MedianFilter(Image2D<unsigned char> img, int mask_size) : m_img(img), m_mask_size(mask_size) { } void MedianFilter::build_mask(int x, int y) { m_mask = std::vector<unsigned char>((unsigned)(m_mask_size * 2 + 1)); for (int i = x - m_mask_size; i <= x + m_mask_size; ++i) { for (int j = y - m_mask_size; j <= y + m_mask_size; ++j) { if (i >= 0 && j >= 0 && i < m_img.h() && j < m_img.w()) m_mask.push_back((unsigned char) m_img.at(i, j)); } } std::sort(m_mask.begin(), m_mask.end()); } unsigned char MedianFilter::get_median() { return m_mask[(m_mask.size() + 1) / 2]; } Image2D<unsigned char> MedianFilter::apply() { Image2D<unsigned char> w_img(m_img.w(), m_img.h()); int i = 0; for (int x = 0; x < m_img.h(); ++x) { for (int y = 0; y < m_img.w(); ++y) { build_mask(x, y); w_img.at(x, y) = get_median(); ++i; } } std::cout << std::endl << "i:" << i << std::endl; return w_img; }
6cb97308d6d22a263a3ba959a8abfa5d57991921
b30e76c6d3e17aa313b21563b6ecffb093fff974
/src/channel.h
942ab8f7b5260a7f4cd3cb118a319767b698db67
[]
no_license
gitobhub/netlib
7b594377fbdfd9ad0e40706f8b11a9c328fe4510
1f38930b348f053c00a35ff553836183490c0001
refs/heads/master
2020-03-07T17:11:30.849951
2018-06-11T06:13:57
2018-06-11T06:13:57
127,604,726
0
0
null
null
null
null
UTF-8
C++
false
false
1,314
h
#ifndef CHANNEL_H #define CHANNEL_H #include <functional> #include <base.h> #include <poll.h> class Channel : noncopyable { public: typedef std::function<void(void)> ReadCallback; typedef std::function<void(void)> WriteCallback; typedef std::function<void(void)> ErrorCallback; Channel(int fd); ~Channel(); int fd() const { return fd_; } void setFd(int fd) { fd_ = fd; } int events() const { return events_; } void setEvents(int events) { events_ = events; } void setRevents(int revents) { revents_ = revents; } int index() const { return index_; } void setIndex(int index) { index_ = index; } bool isNoneEvent() const { return events_ == 0; } void enableReading() { events_ |= POLLIN; } void enableWriting() { events_ |= POLLOUT; } void disableWriting() { events_ &= ~POLLOUT; } bool isWriting() const { return events_ & POLLOUT; } void setReadCallback(const ReadCallback &cb) { readCallback_ = cb; } void setWriteCallback(const WriteCallback &cb) { writeCallback_ = cb; } void setErrorCallback(const ErrorCallback &cb) { errorCallback_ = cb; } void handleEvents(); private: int fd_; int events_ = 0; int revents_ = 0; int index_ = -1; bool eventHandling_ = 0; ReadCallback readCallback_; WriteCallback writeCallback_; ErrorCallback errorCallback_; }; #endif //CHANNEL_H
3a47fa4c5b68055afe654e5f49f64d0d1fd07f58
2624472904fbb26024b050c460d363ed4e6e646f
/arduino/_legacy/SceneManager_Development/SceneManagerInterrupt/Sensor.h
92ee3cfb8e76eebb9a40fe9bb562e7e1f8442cd3
[]
no_license
m9dfukc/The-Space-Beyond-Me
9a8296e8b78cf28671f541f5b273fe976d1c2e44
32fbb1563c32ccb11d233e53e7d1dce097a866bd
refs/heads/master
2020-12-24T13:35:44.824015
2011-10-16T09:23:12
2011-10-16T09:23:12
2,570,902
11
0
null
null
null
null
UTF-8
C++
false
false
624
h
#ifndef SENSOR_H #define SENSOR_H namespace sbm { class Sensor { public: void setup(void (*_nextFrame)(), void (*_nextScene)(), void (*_endFilm)(), void (*_startFilm)()); void update(); private: char input; bool forward; long framePos; long frameLastSceneTrigger; unsigned int nextSceneThreshold; unsigned int nextSceneCounter; unsigned int startThreshold; unsigned int startCounter; unsigned int endThreshold; unsigned int endCounter; void (*nextFrame)(); void (*nextScene)(); void (*endFilm)(); void (*startFilm)(); }; extern Sensor sensor; }; // end namespace sbm #endif
5b42fc48f9ab1c965b5c182ef361b0d43b254b62
06406559616dd6e8509dd45ee5de8b1957520b56
/fflib/include/epoll_i.h
46bdfe9d1462619797467536a8007b1c0ea429a6
[]
no_license
bestboyxie/ffown
57b93a2b5a295c0d7eab46aee1b3981f8e7f97c9
0ba33a0bc741c79fb4d01d4138c568a06c37246b
refs/heads/master
2016-08-11T21:19:17.709065
2015-11-16T13:23:09
2015-11-16T13:23:09
46,275,501
2
1
null
null
null
null
UTF-8
C++
false
false
142
h
#ifndef _EPOLL_I_ #define _EPOLL_I_ #include "netbase.h" namespace ff { class epoll_i: public io_demultiplexer_i { }; } #endif
eb8ba5d2f297e1aef494aa10ea7025314cc355f6
165e538caf7ef2b0916f1ccbac28d9496a1399c2
/include/term_utility.h
0ac26cad61fb15cd2f06c536216a0ee798822a60
[]
no_license
johnmave126/comp2012h-register
a08b04edf50dcc4a5402d74db3d804bfb4681698
41216a4fd5f50bfad7a72f1203f3ef517d28bc01
refs/heads/master
2016-09-11T06:34:53.028517
2013-05-16T18:36:13
2013-05-16T18:36:13
null
0
0
null
null
null
null
UTF-8
C++
false
false
927
h
/* * Register for COMP2012H * Author: TAN, Shuhao * Stu ID: 20090398 * 2013 Spring * * term_utility.h * * header file for some terminal utilities */ #ifndef _TERM_UTILITY_H #define _TERM_UTILITY_H #include "register_common.h" #ifdef WIN_TERM #include <windows.h> #endif class Term { public: /* default constructor/destructor */ Term(); ~Term(); /* * init * * Init the terminal environment */ void init(); /* * clr * * Clear the screen */ void clr(); /* * restore * * Restore the terminal environment */ void restore(); /* * restore * * Pause and prompt some information */ void pause(); private: #ifdef WIN_TERM HANDLE hStdOut; #endif }; #endif
e9ab66f52e6f8374f4e9ae4b47627a0929cb02d9
5eeeb8f15def20a9c5bb9bf99b9621eb54e5975d
/paramwindow.h
b1f493244e7b5c366200fdd0c3c0dbceb17259aa
[]
no_license
KabaevRoman/KursOOP
fad42f600716a32571582538e007ac41943bd55f
2e2ff6b45f883cee13b860645e8614c0c4254275
refs/heads/main
2023-04-11T10:16:23.889099
2021-04-08T17:15:42
2021-04-08T17:15:42
null
0
0
null
null
null
null
UTF-8
C++
false
false
780
h
#ifndef PARAMWINDOW_H #define PARAMWINDOW_H #include <QWidget> #include <QCloseEvent> #include <QLabel> #include <QSpinBox> #include <QPushButton> #include "paramdata.h" class TParamWindow : public QWidget { Q_OBJECT QLabel *l1; QLabel *l2; QLabel *l3; QSpinBox *NumOfWorkers; QDoubleSpinBox *probability; QPushButton *accept_btn; QPushButton *cancel_btn; public: TParamWindow(bool,QWidget *parent = 0); ~TParamWindow(); inline void setEnable(const bool m) { setEnabled(m); } void setCurrentParams(const TParamData); protected: void closeEvent(QCloseEvent*); protected slots: void setModelParam(); signals: void closing(); void sendModelParams(TParamData); void restoreParams(); }; #endif // PARAMWINDOW_H
a26adb266fd4fbe13b7bc4ecd702ba2b3542124d
0f65a0338eef71865123abab8d17d0d415979707
/learngl_3_3_shaderClass/main.cpp
1bea8f707cea2ff82590de816a736f326f282ef6
[]
no_license
PumpkinJimmy/CG_ws
07e6b63c337fd2ab2c440c384b05844d4f497381
33785bbeefde6363290775323243df34860b3ac0
refs/heads/master
2023-07-29T00:35:16.249161
2021-09-05T02:25:33
2021-09-05T02:25:33
402,431,315
0
0
null
null
null
null
UTF-8
C++
false
false
973
cpp
#include <glad/glad.h> #include <GLFW/glfw3.h> #include <iostream> int main() { glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); GLFWwindow* window = glfwCreateWindow(800, 600, "Learn OpenGL", NULL, NULL); if (window == NULL) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } glfwMakeContextCurrent(window); if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { std::cout << "Failded to initialize GLAD" << std::endl; return -1; } glViewport(0, 0, 800, 600); glfwSetFramebufferSizeCallback(window, [](GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); }); while (!glfwWindowShouldClose(window)) { glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glfwSwapBuffers(window); glfwPollEvents(); } glfwTerminate(); return 0; }
2b1b386ce659d3dcbd225549f6af75a4b5a2a2dc
82883bc52dbe5ff96db70bcdca8c39a539dc4ed0
/books.cpp
53e31ff0fd5ca936546b6d7574e95d7026311e90
[]
no_license
TerryCM/competitive
3f40f02396050bb69f94e6eae2709f6419b961d6
9246a3257be5744d29fa6649fa7ce579d0376a1b
refs/heads/main
2023-03-12T20:40:07.630159
2021-03-02T17:31:42
2021-03-02T17:31:42
343,856,424
0
0
null
null
null
null
UTF-8
C++
false
false
604
cpp
#include <iostream> #include <vector> using namespace std; #define nax 100005 int main(){ int b; int m; vector<int> v; scanf("%i %i",&b,&m); for(int i=0;i<b;i++){ int a; scanf("%i",&a); v.push_back(a); } int best= 0; int value = 0; int counter = 0; for(int i=0;i<b;i++){ // for(int j=i;j<b;j++){ // value+=v[j]; // counter++; // if(value<=m && counter>best){ // best = counter; // } // } if(value+v[i] >= b){ if(counter > best){ best = counter; } i--; value = 0; counter = 0; } else{ value+=v[i]; counter++; } } printf("%i",best); }
dc199e62951669cda49c45f67a0a03ed58223b82
b4c656d1fe6e11343791f390afaf69c5ccec21b4
/botan/botan_all.cpp
bb8a22ccd0a3f84e488dd9d02d62d4aba715e780
[]
no_license
miurahr/Nixnote2
71b368106e990ebec2ae38ff8d096a5dfde87046
4fbe2041bf01cfb11ea14d16b0e2739f2fb47c5e
refs/heads/master
2023-06-08T18:55:23.547373
2013-08-28T13:13:39
2013-08-28T13:13:39
15,855,959
1
0
null
null
null
null
UTF-8
C++
false
false
1,573,512
cpp
/* * Botan 1.10.4 Amalgamation * (C) 1999-2011 Jack Lloyd and others * * Distributed under the terms of the Botan license */ #include "botan_all.h" #include <map> #include <vector> #include <string> #include <deque> #include <utility> namespace Botan { /** * Represents a DLL or shared object */ class Dynamically_Loaded_Library { public: /** * Load a DLL (or fail with an exception) * @param lib_name name or path to a library * * If you don't use a full path, the search order will be defined * by whatever the system linker does by default. Always using fully * qualified pathnames can help prevent code injection attacks (eg * via manipulation of LD_LIBRARY_PATH on Linux) */ Dynamically_Loaded_Library(const std::string& lib_name); /** * Unload the DLL * @warning Any pointers returned by resolve()/resolve_symbol() * should not be used after this destructor runs. */ ~Dynamically_Loaded_Library(); /** * Load a symbol (or fail with an exception) * @param symbol names the symbol to load * @return address of the loaded symbol */ void* resolve_symbol(const std::string& symbol); /** * Convenience function for casting symbol to the right type * @param symbol names the symbol to load * @return address of the loaded symbol */ template<typename T> T resolve(const std::string& symbol) { #if defined(__GNUC__) && __GNUC__ < 4 return (T)(resolve_symbol(symbol)); #else return reinterpret_cast<T>(resolve_symbol(symbol)); #endif } private: Dynamically_Loaded_Library(const Dynamically_Loaded_Library&); Dynamically_Loaded_Library& operator=(const Dynamically_Loaded_Library&); std::string lib_name; void* lib; }; } namespace Botan { using namespace Botan; /** * TLS Handshake Hash */ class HandshakeHash { public: void update(const byte in[], size_t length) { data += std::make_pair(in, length); } void update(const MemoryRegion<byte>& in) { data += in; } void update(byte in) { data.push_back(in); } SecureVector<byte> final(); SecureVector<byte> final_ssl3(const MemoryRegion<byte>&); private: SecureVector<byte> data; }; } namespace Botan { /** * Mutex Base Class */ class Mutex { public: /** * Lock the mutex */ virtual void lock() = 0; /** * Unlock the mutex */ virtual void unlock() = 0; virtual ~Mutex() {} }; /** * Mutex Factory */ class Mutex_Factory { public: /** * @return newly allocated mutex */ virtual Mutex* make() = 0; virtual ~Mutex_Factory() {} }; /** * Mutex Holding Class for RAII */ class Mutex_Holder { public: /** * Hold onto a mutex until we leave scope * @param m the mutex to lock */ Mutex_Holder(Mutex* m) : mux(m) { if(!mux) throw Invalid_Argument("Mutex_Holder: Argument was NULL"); mux->lock(); } ~Mutex_Holder() { mux->unlock(); } private: Mutex* mux; }; } namespace Botan { /** * Copy-on-Predicate Algorithm * @param current the first iterator value * @param end the final iterator value * @param dest an output iterator * @param copy_p the predicate */ template<typename InputIterator, typename OutputIterator, typename Predicate> OutputIterator copy_if(InputIterator current, InputIterator end, OutputIterator dest, Predicate copy_p) { while(current != end) { if(copy_p(*current)) *dest++ = *current; ++current; } return dest; } /** * Searching through a std::map * @param mapping the map to search * @param key is what to look for * @param null_result is the value to return if key is not in mapping * @return mapping[key] or null_result */ template<typename K, typename V> inline V search_map(const std::map<K, V>& mapping, const K& key, const V& null_result = V()) { typename std::map<K, V>::const_iterator i = mapping.find(key); if(i == mapping.end()) return null_result; return i->second; } /** * Function adaptor for delete operation */ template<class T> class del_fun : public std::unary_function<T, void> { public: void operator()(T* ptr) { delete ptr; } }; /** * Delete the second half of a pair of objects */ template<typename Pair> void delete2nd(Pair& pair) { delete pair.second; } /** * Insert a key/value pair into a multimap */ template<typename K, typename V> void multimap_insert(std::multimap<K, V>& multimap, const K& key, const V& value) { #if defined(BOTAN_BUILD_COMPILER_IS_SUN_STUDIO) // Work around a strange bug in Sun Studio multimap.insert(std::make_pair<const K, V>(key, value)); #else multimap.insert(std::make_pair(key, value)); #endif } } namespace Botan { /** * @param prov_name a provider name * @return weight for this provider */ size_t static_provider_weight(const std::string& prov_name); /** * Algorithm_Cache (used by Algorithm_Factory) */ template<typename T> class Algorithm_Cache { public: /** * @param algo_spec names the requested algorithm * @param pref_provider suggests a preferred provider * @return prototype object, or NULL */ const T* get(const std::string& algo_spec, const std::string& pref_provider); /** * Add a new algorithm implementation to the cache * @param algo the algorithm prototype object * @param requested_name how this name will be requested * @param provider_name is the name of the provider of this prototype */ void add(T* algo, const std::string& requested_name, const std::string& provider_name); /** * Set the preferred provider * @param algo_spec names the algorithm * @param provider names the preferred provider */ void set_preferred_provider(const std::string& algo_spec, const std::string& provider); /** * Return the list of providers of this algorithm * @param algo_name names the algorithm * @return list of providers of this algorithm */ std::vector<std::string> providers_of(const std::string& algo_name); /** * Clear the cache */ void clear_cache(); /** * Constructor * @param m a mutex to serialize internal access */ Algorithm_Cache(Mutex* m) : mutex(m) {} ~Algorithm_Cache() { clear_cache(); delete mutex; } private: typedef typename std::map<std::string, std::map<std::string, T*> >::iterator algorithms_iterator; typedef typename std::map<std::string, T*>::iterator provider_iterator; algorithms_iterator find_algorithm(const std::string& algo_spec); Mutex* mutex; std::map<std::string, std::string> aliases; std::map<std::string, std::string> pref_providers; std::map<std::string, std::map<std::string, T*> > algorithms; }; /* * Look for an algorithm implementation in the cache, also checking aliases * Assumes object lock is held */ template<typename T> typename Algorithm_Cache<T>::algorithms_iterator Algorithm_Cache<T>::find_algorithm(const std::string& algo_spec) { algorithms_iterator algo = algorithms.find(algo_spec); // Not found? Check if a known alias if(algo == algorithms.end()) { std::map<std::string, std::string>::const_iterator alias = aliases.find(algo_spec); if(alias != aliases.end()) algo = algorithms.find(alias->second); } return algo; } /* * Look for an algorithm implementation by a particular provider */ template<typename T> const T* Algorithm_Cache<T>::get(const std::string& algo_spec, const std::string& requested_provider) { Mutex_Holder lock(mutex); algorithms_iterator algo = find_algorithm(algo_spec); if(algo == algorithms.end()) // algo not found at all (no providers) return 0; // If a provider is requested specifically, return it or fail entirely if(requested_provider != "") { provider_iterator prov = algo->second.find(requested_provider); if(prov != algo->second.end()) return prov->second; return 0; } const T* prototype = 0; std::string prototype_provider; size_t prototype_prov_weight = 0; const std::string pref_provider = search_map(pref_providers, algo_spec); for(provider_iterator i = algo->second.begin(); i != algo->second.end(); ++i) { const std::string prov_name = i->first; const size_t prov_weight = static_provider_weight(prov_name); // preferred prov exists, return immediately if(prov_name == pref_provider) return i->second; if(prototype == 0 || prov_weight > prototype_prov_weight) { prototype = i->second; prototype_provider = i->first; prototype_prov_weight = prov_weight; } } return prototype; } /* * Add an implementation to the cache */ template<typename T> void Algorithm_Cache<T>::add(T* algo, const std::string& requested_name, const std::string& provider) { if(!algo) return; Mutex_Holder lock(mutex); if(algo->name() != requested_name && aliases.find(requested_name) == aliases.end()) { aliases[requested_name] = algo->name(); } if(!algorithms[algo->name()][provider]) algorithms[algo->name()][provider] = algo; else delete algo; } /* * Find the providers of this algo (if any) */ template<typename T> std::vector<std::string> Algorithm_Cache<T>::providers_of(const std::string& algo_name) { Mutex_Holder lock(mutex); std::vector<std::string> providers; algorithms_iterator algo = find_algorithm(algo_name); if(algo != algorithms.end()) { provider_iterator provider = algo->second.begin(); while(provider != algo->second.end()) { providers.push_back(provider->first); ++provider; } } return providers; } /* * Set the preferred provider for an algorithm */ template<typename T> void Algorithm_Cache<T>::set_preferred_provider(const std::string& algo_spec, const std::string& provider) { Mutex_Holder lock(mutex); pref_providers[algo_spec] = provider; } /* * Clear out the cache */ template<typename T> void Algorithm_Cache<T>::clear_cache() { algorithms_iterator algo = algorithms.begin(); while(algo != algorithms.end()) { provider_iterator provider = algo->second.begin(); while(provider != algo->second.end()) { delete provider->second; ++provider; } ++algo; } algorithms.clear(); } } namespace Botan { /** * TLS Handshake Message Base Class */ class HandshakeMessage { public: void send(Record_Writer&, HandshakeHash&) const; virtual Handshake_Type type() const = 0; virtual ~HandshakeMessage() {} private: HandshakeMessage& operator=(const HandshakeMessage&) { return (*this); } virtual SecureVector<byte> serialize() const = 0; virtual void deserialize(const MemoryRegion<byte>&) = 0; }; /** * Client Hello Message */ class Client_Hello : public HandshakeMessage { public: Handshake_Type type() const { return CLIENT_HELLO; } Version_Code version() const { return c_version; } const SecureVector<byte>& session_id() const { return sess_id; } std::vector<u16bit> ciphersuites() const { return suites; } std::vector<byte> compression_algos() const { return comp_algos; } const SecureVector<byte>& random() const { return c_random; } std::string hostname() const { return requested_hostname; } std::string srp_identifier() const { return requested_srp_id; } bool offered_suite(u16bit) const; Client_Hello(RandomNumberGenerator& rng, Record_Writer&, const TLS_Policy&, HandshakeHash&); Client_Hello(const MemoryRegion<byte>& buf, Handshake_Type type) { if(type == CLIENT_HELLO) deserialize(buf); else deserialize_sslv2(buf); } private: SecureVector<byte> serialize() const; void deserialize(const MemoryRegion<byte>&); void deserialize_sslv2(const MemoryRegion<byte>&); Version_Code c_version; SecureVector<byte> sess_id, c_random; std::vector<u16bit> suites; std::vector<byte> comp_algos; std::string requested_hostname; std::string requested_srp_id; }; /** * Client Key Exchange Message */ class Client_Key_Exchange : public HandshakeMessage { public: Handshake_Type type() const { return CLIENT_KEX; } SecureVector<byte> pre_master_secret() const; SecureVector<byte> pre_master_secret(RandomNumberGenerator& rng, const Private_Key* key, Version_Code version); Client_Key_Exchange(RandomNumberGenerator& rng, Record_Writer& output, HandshakeHash& hash, const Public_Key* my_key, Version_Code using_version, Version_Code pref_version); Client_Key_Exchange(const MemoryRegion<byte>& buf, const CipherSuite& suite, Version_Code using_version); private: SecureVector<byte> serialize() const; void deserialize(const MemoryRegion<byte>&); SecureVector<byte> key_material, pre_master; bool include_length; }; /** * Certificate Message */ class Certificate : public HandshakeMessage { public: Handshake_Type type() const { return CERTIFICATE; } std::vector<X509_Certificate> cert_chain() const { return certs; } Certificate(Record_Writer&, const std::vector<X509_Certificate>&, HandshakeHash&); Certificate(const MemoryRegion<byte>& buf) { deserialize(buf); } private: SecureVector<byte> serialize() const; void deserialize(const MemoryRegion<byte>&); std::vector<X509_Certificate> certs; }; /** * Certificate Request Message */ class Certificate_Req : public HandshakeMessage { public: Handshake_Type type() const { return CERTIFICATE_REQUEST; } std::vector<Certificate_Type> acceptable_types() const { return types; } std::vector<X509_DN> acceptable_CAs() const { return names; } /* TODO Certificate_Req(Record_Writer&, HandshakeHash&, const X509_Certificate&); */ Certificate_Req(Record_Writer&, HandshakeHash&, const std::vector<X509_Certificate>&); Certificate_Req(const MemoryRegion<byte>& buf) { deserialize(buf); } private: SecureVector<byte> serialize() const; void deserialize(const MemoryRegion<byte>&); std::vector<X509_DN> names; std::vector<Certificate_Type> types; }; /** * Certificate Verify Message */ class Certificate_Verify : public HandshakeMessage { public: Handshake_Type type() const { return CERTIFICATE_VERIFY; } bool verify(const X509_Certificate&, HandshakeHash&); Certificate_Verify(RandomNumberGenerator& rng, Record_Writer&, HandshakeHash&, const Private_Key*); Certificate_Verify(const MemoryRegion<byte>& buf) { deserialize(buf); } private: SecureVector<byte> serialize() const; void deserialize(const MemoryRegion<byte>&); SecureVector<byte> signature; }; /** * Finished Message */ class Finished : public HandshakeMessage { public: Handshake_Type type() const { return FINISHED; } bool verify(const MemoryRegion<byte>&, Version_Code, const HandshakeHash&, Connection_Side); Finished(Record_Writer&, Version_Code, Connection_Side, const MemoryRegion<byte>&, HandshakeHash&); Finished(const MemoryRegion<byte>& buf) { deserialize(buf); } private: SecureVector<byte> serialize() const; void deserialize(const MemoryRegion<byte>&); SecureVector<byte> compute_verify(const MemoryRegion<byte>&, HandshakeHash, Connection_Side, Version_Code); Connection_Side side; SecureVector<byte> verification_data; }; /** * Hello Request Message */ class Hello_Request : public HandshakeMessage { public: Handshake_Type type() const { return HELLO_REQUEST; } Hello_Request(Record_Writer&); Hello_Request(const MemoryRegion<byte>& buf) { deserialize(buf); } private: SecureVector<byte> serialize() const; void deserialize(const MemoryRegion<byte>&); }; /** * Server Hello Message */ class Server_Hello : public HandshakeMessage { public: Handshake_Type type() const { return SERVER_HELLO; } Version_Code version() { return s_version; } const SecureVector<byte>& session_id() const { return sess_id; } u16bit ciphersuite() const { return suite; } byte compression_algo() const { return comp_algo; } const SecureVector<byte>& random() const { return s_random; } Server_Hello(RandomNumberGenerator& rng, Record_Writer&, const TLS_Policy&, const std::vector<X509_Certificate>&, const Client_Hello&, Version_Code, HandshakeHash&); Server_Hello(const MemoryRegion<byte>& buf) { deserialize(buf); } private: SecureVector<byte> serialize() const; void deserialize(const MemoryRegion<byte>&); Version_Code s_version; SecureVector<byte> sess_id, s_random; u16bit suite; byte comp_algo; }; /** * Server Key Exchange Message */ class Server_Key_Exchange : public HandshakeMessage { public: Handshake_Type type() const { return SERVER_KEX; } Public_Key* key() const; bool verify(const X509_Certificate&, const MemoryRegion<byte>&, const MemoryRegion<byte>&) const; Server_Key_Exchange(RandomNumberGenerator& rng, Record_Writer&, const Public_Key*, const Private_Key*, const MemoryRegion<byte>&, const MemoryRegion<byte>&, HandshakeHash&); Server_Key_Exchange(const MemoryRegion<byte>& buf) { deserialize(buf); } private: SecureVector<byte> serialize() const; SecureVector<byte> serialize_params() const; void deserialize(const MemoryRegion<byte>&); std::vector<BigInt> params; SecureVector<byte> signature; }; /** * Server Hello Done Message */ class Server_Hello_Done : public HandshakeMessage { public: Handshake_Type type() const { return SERVER_HELLO_DONE; } Server_Hello_Done(Record_Writer&, HandshakeHash&); Server_Hello_Done(const MemoryRegion<byte>& buf) { deserialize(buf); } private: SecureVector<byte> serialize() const; void deserialize(const MemoryRegion<byte>&); }; } namespace Botan { /** * SSL/TLS Handshake State */ class Handshake_State { public: Client_Hello* client_hello; Server_Hello* server_hello; Certificate* server_certs; Server_Key_Exchange* server_kex; Certificate_Req* cert_req; Server_Hello_Done* server_hello_done; Certificate* client_certs; Client_Key_Exchange* client_kex; Certificate_Verify* client_verify; Finished* client_finished; Finished* server_finished; Public_Key* kex_pub; Private_Key* kex_priv; CipherSuite suite; SessionKeys keys; HandshakeHash hash; SecureQueue queue; Version_Code version; bool got_client_ccs, got_server_ccs, do_client_auth; Handshake_State(); ~Handshake_State(); }; } namespace Botan { /** * Engine for implementations that use some kind of SIMD */ class SIMD_Engine : public Engine { public: std::string provider_name() const { return "simd"; } BlockCipher* find_block_cipher(const SCAN_Name&, Algorithm_Factory&) const; HashFunction* find_hash(const SCAN_Name& request, Algorithm_Factory&) const; }; } namespace Botan { /** * Helper class for decoding TLS protocol messages */ class TLS_Data_Reader { public: TLS_Data_Reader(const MemoryRegion<byte>& buf_in) : buf(buf_in), offset(0) {} size_t remaining_bytes() const { return buf.size() - offset; } bool has_remaining() const { return (remaining_bytes() > 0); } void discard_next(size_t bytes) { assert_at_least(bytes); offset += bytes; } u16bit get_u16bit() { assert_at_least(2); u16bit result = make_u16bit(buf[offset], buf[offset+1]); offset += 2; return result; } byte get_byte() { assert_at_least(1); byte result = buf[offset]; offset += 1; return result; } template<typename T, typename Container> Container get_elem(size_t num_elems) { assert_at_least(num_elems * sizeof(T)); Container result(num_elems); for(size_t i = 0; i != num_elems; ++i) result[i] = load_be<T>(&buf[offset], i); offset += num_elems * sizeof(T); return result; } template<typename T> SecureVector<T> get_range(size_t len_bytes, size_t min_elems, size_t max_elems) { const size_t num_elems = get_num_elems(len_bytes, sizeof(T), min_elems, max_elems); return get_elem<T, SecureVector<T> >(num_elems); } template<typename T> std::vector<T> get_range_vector(size_t len_bytes, size_t min_elems, size_t max_elems) { const size_t num_elems = get_num_elems(len_bytes, sizeof(T), min_elems, max_elems); return get_elem<T, std::vector<T> >(num_elems); } template<typename T> SecureVector<T> get_fixed(size_t size) { return get_elem<T, SecureVector<T> >(size); } private: size_t get_length_field(size_t len_bytes) { assert_at_least(len_bytes); if(len_bytes == 1) return get_byte(); else if(len_bytes == 2) return get_u16bit(); throw Decoding_Error("TLS_Data_Reader: Bad length size"); } size_t get_num_elems(size_t len_bytes, size_t T_size, size_t min_elems, size_t max_elems) { const size_t byte_length = get_length_field(len_bytes); if(byte_length % T_size != 0) throw Decoding_Error("TLS_Data_Reader: Size isn't multiple of T"); const size_t num_elems = byte_length / T_size; if(num_elems < min_elems || num_elems > max_elems) throw Decoding_Error("TLS_Data_Reader: Range outside paramaters"); return num_elems; } void assert_at_least(size_t n) const { if(buf.size() - offset < n) throw Decoding_Error("TLS_Data_Reader: Corrupt packet"); } const MemoryRegion<byte>& buf; size_t offset; }; /** * Helper function for encoding length-tagged vectors */ template<typename T> void append_tls_length_value(MemoryRegion<byte>& buf, const T* vals, size_t vals_size, size_t tag_size) { const size_t T_size = sizeof(T); const size_t val_bytes = T_size * vals_size; if(tag_size != 1 && tag_size != 2) throw std::invalid_argument("append_tls_length_value: invalid tag size"); if((tag_size == 1 && val_bytes > 255) || (tag_size == 2 && val_bytes > 65535)) throw std::invalid_argument("append_tls_length_value: value too large"); for(size_t i = 0; i != tag_size; ++i) buf.push_back(get_byte(sizeof(val_bytes)-tag_size+i, val_bytes)); for(size_t i = 0; i != vals_size; ++i) for(size_t j = 0; j != T_size; ++j) buf.push_back(get_byte(j, vals[i])); } template<typename T> void append_tls_length_value(MemoryRegion<byte>& buf, const MemoryRegion<T>& vals, size_t tag_size) { append_tls_length_value(buf, &vals[0], vals.size(), tag_size); } template<typename T> void append_tls_length_value(MemoryRegion<byte>& buf, const std::vector<T>& vals, size_t tag_size) { append_tls_length_value(buf, &vals[0], vals.size(), tag_size); } } namespace Botan { /** * File Tree Walking Entropy Source */ class FTW_EntropySource : public EntropySource { public: std::string name() const { return "Proc Walker"; } void poll(Entropy_Accumulator& accum); FTW_EntropySource(const std::string& root_dir); ~FTW_EntropySource(); private: std::string path; class File_Descriptor_Source* dir; }; } #if defined(BOTAN_TARGET_CPU_HAS_SSE2) #include <emmintrin.h> namespace Botan { class SIMD_SSE2 { public: static bool enabled() { return CPUID::has_sse2(); } SIMD_SSE2(const u32bit B[4]) { reg = _mm_loadu_si128(reinterpret_cast<const __m128i*>(B)); } SIMD_SSE2(u32bit B0, u32bit B1, u32bit B2, u32bit B3) { reg = _mm_set_epi32(B0, B1, B2, B3); } SIMD_SSE2(u32bit B) { reg = _mm_set1_epi32(B); } static SIMD_SSE2 load_le(const void* in) { return _mm_loadu_si128(reinterpret_cast<const __m128i*>(in)); } static SIMD_SSE2 load_be(const void* in) { return load_le(in).bswap(); } void store_le(byte out[]) const { _mm_storeu_si128(reinterpret_cast<__m128i*>(out), reg); } void store_be(byte out[]) const { bswap().store_le(out); } void rotate_left(size_t rot) { reg = _mm_or_si128(_mm_slli_epi32(reg, static_cast<int>(rot)), _mm_srli_epi32(reg, static_cast<int>(32-rot))); } void rotate_right(size_t rot) { rotate_left(32 - rot); } void operator+=(const SIMD_SSE2& other) { reg = _mm_add_epi32(reg, other.reg); } SIMD_SSE2 operator+(const SIMD_SSE2& other) const { return _mm_add_epi32(reg, other.reg); } void operator-=(const SIMD_SSE2& other) { reg = _mm_sub_epi32(reg, other.reg); } SIMD_SSE2 operator-(const SIMD_SSE2& other) const { return _mm_sub_epi32(reg, other.reg); } void operator^=(const SIMD_SSE2& other) { reg = _mm_xor_si128(reg, other.reg); } SIMD_SSE2 operator^(const SIMD_SSE2& other) const { return _mm_xor_si128(reg, other.reg); } void operator|=(const SIMD_SSE2& other) { reg = _mm_or_si128(reg, other.reg); } SIMD_SSE2 operator&(const SIMD_SSE2& other) { return _mm_and_si128(reg, other.reg); } void operator&=(const SIMD_SSE2& other) { reg = _mm_and_si128(reg, other.reg); } SIMD_SSE2 operator<<(size_t shift) const { return _mm_slli_epi32(reg, static_cast<int>(shift)); } SIMD_SSE2 operator>>(size_t shift) const { return _mm_srli_epi32(reg, static_cast<int>(shift)); } SIMD_SSE2 operator~() const { return _mm_xor_si128(reg, _mm_set1_epi32(0xFFFFFFFF)); } // (~reg) & other SIMD_SSE2 andc(const SIMD_SSE2& other) { return _mm_andnot_si128(reg, other.reg); } SIMD_SSE2 bswap() const { __m128i T = reg; T = _mm_shufflehi_epi16(T, _MM_SHUFFLE(2, 3, 0, 1)); T = _mm_shufflelo_epi16(T, _MM_SHUFFLE(2, 3, 0, 1)); return _mm_or_si128(_mm_srli_epi16(T, 8), _mm_slli_epi16(T, 8)); } static void transpose(SIMD_SSE2& B0, SIMD_SSE2& B1, SIMD_SSE2& B2, SIMD_SSE2& B3) { __m128i T0 = _mm_unpacklo_epi32(B0.reg, B1.reg); __m128i T1 = _mm_unpacklo_epi32(B2.reg, B3.reg); __m128i T2 = _mm_unpackhi_epi32(B0.reg, B1.reg); __m128i T3 = _mm_unpackhi_epi32(B2.reg, B3.reg); B0.reg = _mm_unpacklo_epi64(T0, T1); B1.reg = _mm_unpackhi_epi64(T0, T1); B2.reg = _mm_unpacklo_epi64(T2, T3); B3.reg = _mm_unpackhi_epi64(T2, T3); } private: SIMD_SSE2(__m128i in) { reg = in; } __m128i reg; }; } #endif namespace Botan { /** * Entropy source using the rdrand instruction first introduced on * Intel's Ivy Bridge architecture. */ class Intel_Rdrand : public EntropySource { public: std::string name() const { return "Intel Rdrand"; } void poll(Entropy_Accumulator& accum); }; } namespace Botan { /** * XOR arrays. Postcondition out[i] = in[i] ^ out[i] forall i = 0...length * @param out the input/output buffer * @param in the read-only input buffer * @param length the length of the buffers */ inline void xor_buf(byte out[], const byte in[], size_t length) { while(length >= 8) { #if BOTAN_TARGET_UNALIGNED_MEMORY_ACCESS_OK *reinterpret_cast<u64bit*>(out) ^= *reinterpret_cast<const u64bit*>(in); #else out[0] ^= in[0]; out[1] ^= in[1]; out[2] ^= in[2]; out[3] ^= in[3]; out[4] ^= in[4]; out[5] ^= in[5]; out[6] ^= in[6]; out[7] ^= in[7]; #endif out += 8; in += 8; length -= 8; } for(size_t i = 0; i != length; ++i) out[i] ^= in[i]; } /** * XOR arrays. Postcondition out[i] = in[i] ^ in2[i] forall i = 0...length * @param out the output buffer * @param in the first input buffer * @param in2 the second output buffer * @param length the length of the three buffers */ inline void xor_buf(byte out[], const byte in[], const byte in2[], size_t length) { while(length >= 8) { #if BOTAN_TARGET_UNALIGNED_MEMORY_ACCESS_OK *reinterpret_cast<u64bit*>(out) = *reinterpret_cast<const u64bit*>(in) ^ *reinterpret_cast<const u64bit*>(in2); #else out[0] = in[0] ^ in2[0]; out[1] = in[1] ^ in2[1]; out[2] = in[2] ^ in2[2]; out[3] = in[3] ^ in2[3]; out[4] = in[4] ^ in2[4]; out[5] = in[5] ^ in2[5]; out[6] = in[6] ^ in2[6]; out[7] = in[7] ^ in2[7]; #endif in += 8; in2 += 8; out += 8; length -= 8; } for(size_t i = 0; i != length; ++i) out[i] = in[i] ^ in2[i]; } } namespace Botan { /** * Entropy source using high resolution timers * * @note Any results from timers are marked as not contributing entropy * to the poll, as a local attacker could observe them directly. */ class High_Resolution_Timestamp : public EntropySource { public: std::string name() const { return "High Resolution Timestamp"; } void poll(Entropy_Accumulator& accum); }; } namespace Botan { /** * Power of 2 test. T should be an unsigned integer type * @param arg an integer value * @return true iff arg is 2^n for some n > 0 */ template<typename T> inline bool power_of_2(T arg) { return ((arg != 0 && arg != 1) && ((arg & (arg-1)) == 0)); } /** * Return the index of the highest set bit * T is an unsigned integer type * @param n an integer value * @return index of the highest set bit in n */ template<typename T> inline size_t high_bit(T n) { for(size_t i = 8*sizeof(T); i > 0; --i) if((n >> (i - 1)) & 0x01) return i; return 0; } /** * Return the index of the lowest set bit * T is an unsigned integer type * @param n an integer value * @return index of the lowest set bit in n */ template<typename T> inline size_t low_bit(T n) { for(size_t i = 0; i != 8*sizeof(T); ++i) if((n >> i) & 0x01) return (i + 1); return 0; } /** * Return the number of significant bytes in n * @param n an integer value * @return number of significant bytes in n */ template<typename T> inline size_t significant_bytes(T n) { for(size_t i = 0; i != sizeof(T); ++i) if(get_byte(i, n)) return sizeof(T)-i; return 0; } /** * Compute Hamming weights * @param n an integer value * @return number of bits in n set to 1 */ template<typename T> inline size_t hamming_weight(T n) { const byte NIBBLE_WEIGHTS[] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 }; size_t weight = 0; for(size_t i = 0; i != 2*sizeof(T); ++i) weight += NIBBLE_WEIGHTS[(n >> (4*i)) & 0x0F]; return weight; } /** * Count the trailing zero bits in n * @param n an integer value * @return maximum x st 2^x divides n */ template<typename T> inline size_t ctz(T n) { for(size_t i = 0; i != 8*sizeof(T); ++i) if((n >> i) & 0x01) return i; return 8*sizeof(T); } } namespace Botan { /** * Estimate work factor for discrete logarithm * @param prime_group_size size of the group in bits * @return estimated security level for this group */ size_t dl_work_factor(size_t prime_group_size); } namespace Botan { /** * No-Op Mutex Factory */ class Noop_Mutex_Factory : public Mutex_Factory { public: Mutex* make(); }; } namespace Botan { /** * Round up * @param n an integer * @param align_to the alignment boundary * @return n rounded up to a multiple of align_to */ template<typename T> inline T round_up(T n, T align_to) { if(n % align_to || n == 0) n += align_to - (n % align_to); return n; } /** * Round down * @param n an integer * @param align_to the alignment boundary * @return n rounded down to a multiple of align_to */ template<typename T> inline T round_down(T n, T align_to) { return (n - (n % align_to)); } } namespace Botan { /** * Pooling Allocator */ class Pooling_Allocator : public Allocator { public: void* allocate(size_t); void deallocate(void*, size_t); void destroy(); /** * @param mutex used for internal locking */ Pooling_Allocator(Mutex* mutex); ~Pooling_Allocator(); private: void get_more_core(size_t); byte* allocate_blocks(size_t); virtual void* alloc_block(size_t) = 0; virtual void dealloc_block(void*, size_t) = 0; class Memory_Block { public: Memory_Block(void*); static size_t bitmap_size() { return BITMAP_SIZE; } static size_t block_size() { return BLOCK_SIZE; } bool contains(void*, size_t) const; byte* alloc(size_t); void free(void*, size_t); bool operator<(const Memory_Block& other) const { if(buffer < other.buffer && other.buffer < buffer_end) return false; return (buffer < other.buffer); } private: typedef u64bit bitmap_type; static const size_t BITMAP_SIZE = 8 * sizeof(bitmap_type); static const size_t BLOCK_SIZE = 64; bitmap_type bitmap; byte* buffer, *buffer_end; }; std::vector<Memory_Block> blocks; std::vector<Memory_Block>::iterator last_used; std::vector<std::pair<void*, size_t> > allocated; Mutex* mutex; }; } namespace Botan { /** * Allocator using malloc */ class Malloc_Allocator : public Allocator { public: void* allocate(size_t); void deallocate(void*, size_t); std::string type() const { return "malloc"; } }; /** * Allocator using malloc plus locking */ class Locking_Allocator : public Pooling_Allocator { public: /** * @param mutex used for internal locking */ Locking_Allocator(Mutex* mutex) : Pooling_Allocator(mutex) {} std::string type() const { return "locking"; } private: void* alloc_block(size_t); void dealloc_block(void*, size_t); }; } namespace Botan { /** * Fixed Window Exponentiator */ class Fixed_Window_Exponentiator : public Modular_Exponentiator { public: void set_exponent(const BigInt&); void set_base(const BigInt&); BigInt execute() const; Modular_Exponentiator* copy() const { return new Fixed_Window_Exponentiator(*this); } Fixed_Window_Exponentiator(const BigInt&, Power_Mod::Usage_Hints); private: Modular_Reducer reducer; BigInt exp; size_t window_bits; std::vector<BigInt> g; Power_Mod::Usage_Hints hints; }; /** * Montgomery Exponentiator */ class Montgomery_Exponentiator : public Modular_Exponentiator { public: void set_exponent(const BigInt&); void set_base(const BigInt&); BigInt execute() const; Modular_Exponentiator* copy() const { return new Montgomery_Exponentiator(*this); } Montgomery_Exponentiator(const BigInt&, Power_Mod::Usage_Hints); private: BigInt exp, modulus; BigInt R2, R_mod; std::vector<BigInt> g; word mod_prime; size_t mod_words, exp_bits, window_bits; Power_Mod::Usage_Hints hints; }; } #if (BOTAN_MP_WORD_BITS != 64) #error The mp_x86_64 module requires that BOTAN_MP_WORD_BITS == 64 #endif namespace Botan { extern "C" { /* * Helper Macros for x86-64 Assembly */ #define ASM(x) x "\n\t" /* * Word Multiply */ inline word word_madd2(word a, word b, word* c) { asm( ASM("mulq %[b]") ASM("addq %[c],%[a]") ASM("adcq $0,%[carry]") : [a]"=a"(a), [b]"=rm"(b), [carry]"=&d"(*c) : "0"(a), "1"(b), [c]"g"(*c) : "cc"); return a; } /* * Word Multiply/Add */ inline word word_madd3(word a, word b, word c, word* d) { asm( ASM("mulq %[b]") ASM("addq %[c],%[a]") ASM("adcq $0,%[carry]") ASM("addq %[d],%[a]") ASM("adcq $0,%[carry]") : [a]"=a"(a), [b]"=rm"(b), [carry]"=&d"(*d) : "0"(a), "1"(b), [c]"g"(c), [d]"g"(*d) : "cc"); return a; } #undef ASM } } namespace Botan { /** * Unix Program Info */ struct Unix_Program { /** * @param n is the name and arguments of what we are going run * @param p is the priority level (lower prio numbers get polled first) */ Unix_Program(const char* n, size_t p) { name_and_args = n; priority = p; working = true; } /** * The name and arguments for this command */ std::string name_and_args; /** * Priority: we scan from low to high */ size_t priority; /** * Does this source seem to be working? */ bool working; }; /** * Command Output DataSource */ class DataSource_Command : public DataSource { public: size_t read(byte[], size_t); size_t peek(byte[], size_t, size_t) const; bool end_of_data() const; std::string id() const; int fd() const; DataSource_Command(const std::string&, const std::vector<std::string>& paths); ~DataSource_Command(); private: void create_pipe(const std::vector<std::string>&); void shutdown_pipe(); const size_t MAX_BLOCK_USECS, KILL_WAIT; std::vector<std::string> arg_list; struct pipe_wrapper* pipe; }; } namespace Botan { /** * Allocator that uses memory maps backed by disk. We zeroize the map * upon deallocation. If swap occurs, the VM will swap to the shared * file backing rather than to a swap device, which means we know where * it is and can zap it later. */ class MemoryMapping_Allocator : public Pooling_Allocator { public: /** * @param mutex used for internal locking */ MemoryMapping_Allocator(Mutex* mutex) : Pooling_Allocator(mutex) {} std::string type() const { return "mmap"; } private: void* alloc_block(size_t); void dealloc_block(void*, size_t); }; } #if defined(BOTAN_HAS_SIMD_SSE2) namespace Botan { typedef SIMD_SSE2 SIMD_32; } #elif defined(BOTAN_HAS_SIMD_ALTIVEC) namespace Botan { typedef SIMD_Altivec SIMD_32; } #elif defined(BOTAN_HAS_SIMD_SCALAR) namespace Botan { typedef SIMD_Scalar SIMD_32; } #else #error "No SIMD module defined" #endif namespace Botan { /** * Entropy source reading from kernel devices like /dev/random */ class Device_EntropySource : public EntropySource { public: std::string name() const { return "RNG Device Reader"; } void poll(Entropy_Accumulator& accum); Device_EntropySource(const std::vector<std::string>& fsnames); ~Device_EntropySource(); private: /** A class handling reading from a Unix character device */ class Device_Reader { public: typedef int fd_type; // Does not own fd, a transient class Device_Reader(fd_type device_fd) : fd(device_fd) {} void close(); size_t get(byte out[], size_t length, size_t ms_wait_time); static fd_type open(const std::string& pathname); private: fd_type fd; }; std::vector<Device_Reader> devices; }; } namespace Botan { void assertion_failure(const char* expr_str, const char* msg, const char* func, const char* file, int line); #define BOTAN_ASSERT(expr, msg) \ do { \ if(!(expr)) \ Botan::assertion_failure(#expr, \ msg, \ BOTAN_ASSERT_FUNCTION, \ __FILE__, \ __LINE__); \ } while(0) #define BOTAN_ASSERT_EQUAL(value1, value2, msg) \ do { \ if(value1 != value2) \ Botan::assertion_failure(#value1 " == " #value2, \ msg, \ BOTAN_ASSERT_FUNCTION, \ __FILE__, \ __LINE__); \ } while(0) /* * Unfortunately getting the function name from the preprocessor * isn't standard in C++98 (C++0x uses C99's __func__) */ #if defined(BOTAN_BUILD_COMPILER_IS_GCC) || \ defined(BOTAN_BUILD_COMPILER_IS_CLANG) || \ defined(BOTAN_BUILD_COMPILER_IS_INTEL) #define BOTAN_ASSERT_FUNCTION __PRETTY_FUNCTION__ #elif defined(BOTAN_BUILD_COMPILER_IS_MSVC) #define BOTAN_ASSERT_FUNCTION __FUNCTION__ #else #define BOTAN_ASSERT_FUNCTION ((const char*)0) #endif } namespace Botan { /* * The size of the word type, in bits */ const size_t MP_WORD_BITS = BOTAN_MP_WORD_BITS; extern "C" { /* * Addition/Subtraction Operations */ void bigint_add2(word x[], size_t x_size, const word y[], size_t y_size); void bigint_add3(word z[], const word x[], size_t x_size, const word y[], size_t y_size); word bigint_add2_nc(word x[], size_t x_size, const word y[], size_t y_size); word bigint_add3_nc(word z[], const word x[], size_t x_size, const word y[], size_t y_size); word bigint_sub2(word x[], size_t x_size, const word y[], size_t y_size); /** * x = y - x; assumes y >= x */ void bigint_sub2_rev(word x[], const word y[], size_t y_size); word bigint_sub3(word z[], const word x[], size_t x_size, const word y[], size_t y_size); /* * Shift Operations */ void bigint_shl1(word x[], size_t x_size, size_t word_shift, size_t bit_shift); void bigint_shr1(word x[], size_t x_size, size_t word_shift, size_t bit_shift); void bigint_shl2(word y[], const word x[], size_t x_size, size_t word_shift, size_t bit_shift); void bigint_shr2(word y[], const word x[], size_t x_size, size_t word_shift, size_t bit_shift); /* * Simple O(N^2) Multiplication and Squaring */ void bigint_simple_mul(word z[], const word x[], size_t x_size, const word y[], size_t y_size); void bigint_simple_sqr(word z[], const word x[], size_t x_size); /* * Linear Multiply */ void bigint_linmul2(word x[], size_t x_size, word y); void bigint_linmul3(word z[], const word x[], size_t x_size, word y); /** * Montgomery Reduction * @param z integer to reduce (also output in first p_size+1 words) * @param z_size size of z (should be >= 2*p_size+1) * @param p modulus * @param p_size size of p * @param p_dash Montgomery value * @param workspace array of at least 2*(p_size+1) words */ void bigint_monty_redc(word z[], size_t z_size, const word p[], size_t p_size, word p_dash, word workspace[]); /* * Montgomery Multiplication */ void bigint_monty_mul(word z[], size_t z_size, const word x[], size_t x_size, size_t x_sw, const word y[], size_t y_size, size_t y_sw, const word p[], size_t p_size, word p_dash, word workspace[]); /* * Montgomery Squaring */ void bigint_monty_sqr(word z[], size_t z_size, const word x[], size_t x_size, size_t x_sw, const word p[], size_t p_size, word p_dash, word workspace[]); /* * Division operation */ size_t bigint_divcore(word q, word y2, word y1, word x3, word x2, word x1); /** * Compare x and y */ s32bit bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size); /** * Compute ((n1<<bits) + n0) / d */ word bigint_divop(word n1, word n0, word d); /** * Compute ((n1<<bits) + n0) % d */ word bigint_modop(word n1, word n0, word d); /* * Comba Multiplication / Squaring */ void bigint_comba_mul4(word z[8], const word x[4], const word y[4]); void bigint_comba_mul6(word z[12], const word x[6], const word y[6]); void bigint_comba_mul8(word z[16], const word x[8], const word y[8]); void bigint_comba_mul16(word z[32], const word x[16], const word y[16]); void bigint_comba_sqr4(word out[8], const word in[4]); void bigint_comba_sqr6(word out[12], const word in[6]); void bigint_comba_sqr8(word out[16], const word in[8]); void bigint_comba_sqr16(word out[32], const word in[16]); } /* * High Level Multiplication/Squaring Interfaces */ void bigint_mul(word z[], size_t z_size, word workspace[], const word x[], size_t x_size, size_t x_sw, const word y[], size_t y_size, size_t y_sw); void bigint_sqr(word z[], size_t z_size, word workspace[], const word x[], size_t x_size, size_t x_sw); } namespace Botan { /** * Pthread Mutex Factory */ class Pthread_Mutex_Factory : public Mutex_Factory { public: Mutex* make(); }; } namespace Botan { /** * Core Engine */ class Core_Engine : public Engine { public: std::string provider_name() const { return "core"; } PK_Ops::Key_Agreement* get_key_agreement_op(const Private_Key& key) const; PK_Ops::Signature* get_signature_op(const Private_Key& key) const; PK_Ops::Verification* get_verify_op(const Public_Key& key) const; PK_Ops::Encryption* get_encryption_op(const Public_Key& key) const; PK_Ops::Decryption* get_decryption_op(const Private_Key& key) const; Modular_Exponentiator* mod_exp(const BigInt& n, Power_Mod::Usage_Hints) const; Keyed_Filter* get_cipher(const std::string&, Cipher_Dir, Algorithm_Factory&); BlockCipher* find_block_cipher(const SCAN_Name&, Algorithm_Factory&) const; StreamCipher* find_stream_cipher(const SCAN_Name&, Algorithm_Factory&) const; HashFunction* find_hash(const SCAN_Name& request, Algorithm_Factory&) const; MessageAuthenticationCode* find_mac(const SCAN_Name& request, Algorithm_Factory&) const; PBKDF* find_pbkdf(const SCAN_Name& algo_spec, Algorithm_Factory& af) const; }; /** * Create a cipher mode filter object * @param block_cipher a block cipher object * @param direction are we encrypting or decrypting? * @param mode the name of the cipher mode to use * @param padding the mode padding to use (only used for ECB, CBC) */ Keyed_Filter* get_cipher_mode(const BlockCipher* block_cipher, Cipher_Dir direction, const std::string& mode, const std::string& padding); } namespace Botan { /** * Unix Entropy Source */ class Unix_EntropySource : public EntropySource { public: std::string name() const { return "Unix Entropy Source"; } void poll(Entropy_Accumulator& accum); void add_sources(const Unix_Program[], size_t); Unix_EntropySource(const std::vector<std::string>& path); private: static std::vector<Unix_Program> get_default_sources(); void fast_poll(Entropy_Accumulator& accum); const std::vector<std::string> PATH; std::vector<Unix_Program> sources; }; } namespace Botan { /** * EGD Entropy Source */ class EGD_EntropySource : public EntropySource { public: std::string name() const { return "EGD/PRNGD"; } void poll(Entropy_Accumulator& accum); EGD_EntropySource(const std::vector<std::string>&); ~EGD_EntropySource(); private: class EGD_Socket { public: EGD_Socket(const std::string& path); void close(); size_t read(byte outbuf[], size_t length); private: static int open_socket(const std::string& path); std::string socket_path; int m_fd; // cached fd }; std::vector<EGD_Socket> sockets; }; } namespace Botan { Public_Key* make_public_key(const AlgorithmIdentifier& alg_id, const MemoryRegion<byte>& key_bits); Private_Key* make_private_key(const AlgorithmIdentifier& alg_id, const MemoryRegion<byte>& key_bits, RandomNumberGenerator& rng); } namespace Botan { /** * Container of output buffers for Pipe */ class Output_Buffers { public: size_t read(byte[], size_t, Pipe::message_id); size_t peek(byte[], size_t, size_t, Pipe::message_id) const; size_t remaining(Pipe::message_id) const; void add(class SecureQueue*); void retire(); Pipe::message_id message_count() const; Output_Buffers(); ~Output_Buffers(); private: class SecureQueue* get(Pipe::message_id) const; std::deque<SecureQueue*> buffers; Pipe::message_id offset; }; } namespace Botan { /** * SSL/TLS Alert Message */ class Alert { public: /** * @return if this alert is a fatal one or not */ bool is_fatal() const { return fatal; } /** * @return type of alert */ Alert_Type type() const { return type_code; } /** * Deserialize an Alert message * @param buf the serialized alert */ Alert(const MemoryRegion<byte>& buf) { if(buf.size() != 2) throw Decoding_Error("Alert: Bad size for alert message"); if(buf[0] == 1) fatal = false; else if(buf[0] == 2) fatal = true; else throw Decoding_Error("Alert: Bad type code for alert level"); type_code = static_cast<Alert_Type>(buf[1]); } private: bool fatal; Alert_Type type_code; }; } namespace Botan { /** * Check if we can at least potentially lock memory */ bool has_mlock(); /** * Lock memory into RAM if possible * @param addr the start of the memory block * @param length the length of the memory block in bytes * @returns true if successful, false otherwise */ bool lock_mem(void* addr, size_t length); /** * Unlock memory locked with lock_mem() * @param addr the start of the memory block * @param length the length of the memory block in bytes */ void unlock_mem(void* addr, size_t length); } namespace Botan { template<typename T> inline void prefetch_readonly(const T* addr, size_t length) { #if defined(__GNUG__) const size_t Ts_per_cache_line = CPUID::cache_line_size() / sizeof(T); for(size_t i = 0; i <= length; i += Ts_per_cache_line) __builtin_prefetch(addr + i, 0); #endif } template<typename T> inline void prefetch_readwrite(const T* addr, size_t length) { #if defined(__GNUG__) const size_t Ts_per_cache_line = CPUID::cache_line_size() / sizeof(T); for(size_t i = 0; i <= length; i += Ts_per_cache_line) __builtin_prefetch(addr + i, 1); #endif } } namespace Botan { const u64bit Camellia_SBOX1[256] = { 0x7070700070000070, 0x8282820082000082, 0x2C2C2C002C00002C, 0xECECEC00EC0000EC, 0xB3B3B300B30000B3, 0x2727270027000027, 0xC0C0C000C00000C0, 0xE5E5E500E50000E5, 0xE4E4E400E40000E4, 0x8585850085000085, 0x5757570057000057, 0x3535350035000035, 0xEAEAEA00EA0000EA, 0x0C0C0C000C00000C, 0xAEAEAE00AE0000AE, 0x4141410041000041, 0x2323230023000023, 0xEFEFEF00EF0000EF, 0x6B6B6B006B00006B, 0x9393930093000093, 0x4545450045000045, 0x1919190019000019, 0xA5A5A500A50000A5, 0x2121210021000021, 0xEDEDED00ED0000ED, 0x0E0E0E000E00000E, 0x4F4F4F004F00004F, 0x4E4E4E004E00004E, 0x1D1D1D001D00001D, 0x6565650065000065, 0x9292920092000092, 0xBDBDBD00BD0000BD, 0x8686860086000086, 0xB8B8B800B80000B8, 0xAFAFAF00AF0000AF, 0x8F8F8F008F00008F, 0x7C7C7C007C00007C, 0xEBEBEB00EB0000EB, 0x1F1F1F001F00001F, 0xCECECE00CE0000CE, 0x3E3E3E003E00003E, 0x3030300030000030, 0xDCDCDC00DC0000DC, 0x5F5F5F005F00005F, 0x5E5E5E005E00005E, 0xC5C5C500C50000C5, 0x0B0B0B000B00000B, 0x1A1A1A001A00001A, 0xA6A6A600A60000A6, 0xE1E1E100E10000E1, 0x3939390039000039, 0xCACACA00CA0000CA, 0xD5D5D500D50000D5, 0x4747470047000047, 0x5D5D5D005D00005D, 0x3D3D3D003D00003D, 0xD9D9D900D90000D9, 0x0101010001000001, 0x5A5A5A005A00005A, 0xD6D6D600D60000D6, 0x5151510051000051, 0x5656560056000056, 0x6C6C6C006C00006C, 0x4D4D4D004D00004D, 0x8B8B8B008B00008B, 0x0D0D0D000D00000D, 0x9A9A9A009A00009A, 0x6666660066000066, 0xFBFBFB00FB0000FB, 0xCCCCCC00CC0000CC, 0xB0B0B000B00000B0, 0x2D2D2D002D00002D, 0x7474740074000074, 0x1212120012000012, 0x2B2B2B002B00002B, 0x2020200020000020, 0xF0F0F000F00000F0, 0xB1B1B100B10000B1, 0x8484840084000084, 0x9999990099000099, 0xDFDFDF00DF0000DF, 0x4C4C4C004C00004C, 0xCBCBCB00CB0000CB, 0xC2C2C200C20000C2, 0x3434340034000034, 0x7E7E7E007E00007E, 0x7676760076000076, 0x0505050005000005, 0x6D6D6D006D00006D, 0xB7B7B700B70000B7, 0xA9A9A900A90000A9, 0x3131310031000031, 0xD1D1D100D10000D1, 0x1717170017000017, 0x0404040004000004, 0xD7D7D700D70000D7, 0x1414140014000014, 0x5858580058000058, 0x3A3A3A003A00003A, 0x6161610061000061, 0xDEDEDE00DE0000DE, 0x1B1B1B001B00001B, 0x1111110011000011, 0x1C1C1C001C00001C, 0x3232320032000032, 0x0F0F0F000F00000F, 0x9C9C9C009C00009C, 0x1616160016000016, 0x5353530053000053, 0x1818180018000018, 0xF2F2F200F20000F2, 0x2222220022000022, 0xFEFEFE00FE0000FE, 0x4444440044000044, 0xCFCFCF00CF0000CF, 0xB2B2B200B20000B2, 0xC3C3C300C30000C3, 0xB5B5B500B50000B5, 0x7A7A7A007A00007A, 0x9191910091000091, 0x2424240024000024, 0x0808080008000008, 0xE8E8E800E80000E8, 0xA8A8A800A80000A8, 0x6060600060000060, 0xFCFCFC00FC0000FC, 0x6969690069000069, 0x5050500050000050, 0xAAAAAA00AA0000AA, 0xD0D0D000D00000D0, 0xA0A0A000A00000A0, 0x7D7D7D007D00007D, 0xA1A1A100A10000A1, 0x8989890089000089, 0x6262620062000062, 0x9797970097000097, 0x5454540054000054, 0x5B5B5B005B00005B, 0x1E1E1E001E00001E, 0x9595950095000095, 0xE0E0E000E00000E0, 0xFFFFFF00FF0000FF, 0x6464640064000064, 0xD2D2D200D20000D2, 0x1010100010000010, 0xC4C4C400C40000C4, 0x0000000000000000, 0x4848480048000048, 0xA3A3A300A30000A3, 0xF7F7F700F70000F7, 0x7575750075000075, 0xDBDBDB00DB0000DB, 0x8A8A8A008A00008A, 0x0303030003000003, 0xE6E6E600E60000E6, 0xDADADA00DA0000DA, 0x0909090009000009, 0x3F3F3F003F00003F, 0xDDDDDD00DD0000DD, 0x9494940094000094, 0x8787870087000087, 0x5C5C5C005C00005C, 0x8383830083000083, 0x0202020002000002, 0xCDCDCD00CD0000CD, 0x4A4A4A004A00004A, 0x9090900090000090, 0x3333330033000033, 0x7373730073000073, 0x6767670067000067, 0xF6F6F600F60000F6, 0xF3F3F300F30000F3, 0x9D9D9D009D00009D, 0x7F7F7F007F00007F, 0xBFBFBF00BF0000BF, 0xE2E2E200E20000E2, 0x5252520052000052, 0x9B9B9B009B00009B, 0xD8D8D800D80000D8, 0x2626260026000026, 0xC8C8C800C80000C8, 0x3737370037000037, 0xC6C6C600C60000C6, 0x3B3B3B003B00003B, 0x8181810081000081, 0x9696960096000096, 0x6F6F6F006F00006F, 0x4B4B4B004B00004B, 0x1313130013000013, 0xBEBEBE00BE0000BE, 0x6363630063000063, 0x2E2E2E002E00002E, 0xE9E9E900E90000E9, 0x7979790079000079, 0xA7A7A700A70000A7, 0x8C8C8C008C00008C, 0x9F9F9F009F00009F, 0x6E6E6E006E00006E, 0xBCBCBC00BC0000BC, 0x8E8E8E008E00008E, 0x2929290029000029, 0xF5F5F500F50000F5, 0xF9F9F900F90000F9, 0xB6B6B600B60000B6, 0x2F2F2F002F00002F, 0xFDFDFD00FD0000FD, 0xB4B4B400B40000B4, 0x5959590059000059, 0x7878780078000078, 0x9898980098000098, 0x0606060006000006, 0x6A6A6A006A00006A, 0xE7E7E700E70000E7, 0x4646460046000046, 0x7171710071000071, 0xBABABA00BA0000BA, 0xD4D4D400D40000D4, 0x2525250025000025, 0xABABAB00AB0000AB, 0x4242420042000042, 0x8888880088000088, 0xA2A2A200A20000A2, 0x8D8D8D008D00008D, 0xFAFAFA00FA0000FA, 0x7272720072000072, 0x0707070007000007, 0xB9B9B900B90000B9, 0x5555550055000055, 0xF8F8F800F80000F8, 0xEEEEEE00EE0000EE, 0xACACAC00AC0000AC, 0x0A0A0A000A00000A, 0x3636360036000036, 0x4949490049000049, 0x2A2A2A002A00002A, 0x6868680068000068, 0x3C3C3C003C00003C, 0x3838380038000038, 0xF1F1F100F10000F1, 0xA4A4A400A40000A4, 0x4040400040000040, 0x2828280028000028, 0xD3D3D300D30000D3, 0x7B7B7B007B00007B, 0xBBBBBB00BB0000BB, 0xC9C9C900C90000C9, 0x4343430043000043, 0xC1C1C100C10000C1, 0x1515150015000015, 0xE3E3E300E30000E3, 0xADADAD00AD0000AD, 0xF4F4F400F40000F4, 0x7777770077000077, 0xC7C7C700C70000C7, 0x8080800080000080, 0x9E9E9E009E00009E }; const u64bit Camellia_SBOX2[256] = { 0x00E0E0E0E0E00000, 0x0005050505050000, 0x0058585858580000, 0x00D9D9D9D9D90000, 0x0067676767670000, 0x004E4E4E4E4E0000, 0x0081818181810000, 0x00CBCBCBCBCB0000, 0x00C9C9C9C9C90000, 0x000B0B0B0B0B0000, 0x00AEAEAEAEAE0000, 0x006A6A6A6A6A0000, 0x00D5D5D5D5D50000, 0x0018181818180000, 0x005D5D5D5D5D0000, 0x0082828282820000, 0x0046464646460000, 0x00DFDFDFDFDF0000, 0x00D6D6D6D6D60000, 0x0027272727270000, 0x008A8A8A8A8A0000, 0x0032323232320000, 0x004B4B4B4B4B0000, 0x0042424242420000, 0x00DBDBDBDBDB0000, 0x001C1C1C1C1C0000, 0x009E9E9E9E9E0000, 0x009C9C9C9C9C0000, 0x003A3A3A3A3A0000, 0x00CACACACACA0000, 0x0025252525250000, 0x007B7B7B7B7B0000, 0x000D0D0D0D0D0000, 0x0071717171710000, 0x005F5F5F5F5F0000, 0x001F1F1F1F1F0000, 0x00F8F8F8F8F80000, 0x00D7D7D7D7D70000, 0x003E3E3E3E3E0000, 0x009D9D9D9D9D0000, 0x007C7C7C7C7C0000, 0x0060606060600000, 0x00B9B9B9B9B90000, 0x00BEBEBEBEBE0000, 0x00BCBCBCBCBC0000, 0x008B8B8B8B8B0000, 0x0016161616160000, 0x0034343434340000, 0x004D4D4D4D4D0000, 0x00C3C3C3C3C30000, 0x0072727272720000, 0x0095959595950000, 0x00ABABABABAB0000, 0x008E8E8E8E8E0000, 0x00BABABABABA0000, 0x007A7A7A7A7A0000, 0x00B3B3B3B3B30000, 0x0002020202020000, 0x00B4B4B4B4B40000, 0x00ADADADADAD0000, 0x00A2A2A2A2A20000, 0x00ACACACACAC0000, 0x00D8D8D8D8D80000, 0x009A9A9A9A9A0000, 0x0017171717170000, 0x001A1A1A1A1A0000, 0x0035353535350000, 0x00CCCCCCCCCC0000, 0x00F7F7F7F7F70000, 0x0099999999990000, 0x0061616161610000, 0x005A5A5A5A5A0000, 0x00E8E8E8E8E80000, 0x0024242424240000, 0x0056565656560000, 0x0040404040400000, 0x00E1E1E1E1E10000, 0x0063636363630000, 0x0009090909090000, 0x0033333333330000, 0x00BFBFBFBFBF0000, 0x0098989898980000, 0x0097979797970000, 0x0085858585850000, 0x0068686868680000, 0x00FCFCFCFCFC0000, 0x00ECECECECEC0000, 0x000A0A0A0A0A0000, 0x00DADADADADA0000, 0x006F6F6F6F6F0000, 0x0053535353530000, 0x0062626262620000, 0x00A3A3A3A3A30000, 0x002E2E2E2E2E0000, 0x0008080808080000, 0x00AFAFAFAFAF0000, 0x0028282828280000, 0x00B0B0B0B0B00000, 0x0074747474740000, 0x00C2C2C2C2C20000, 0x00BDBDBDBDBD0000, 0x0036363636360000, 0x0022222222220000, 0x0038383838380000, 0x0064646464640000, 0x001E1E1E1E1E0000, 0x0039393939390000, 0x002C2C2C2C2C0000, 0x00A6A6A6A6A60000, 0x0030303030300000, 0x00E5E5E5E5E50000, 0x0044444444440000, 0x00FDFDFDFDFD0000, 0x0088888888880000, 0x009F9F9F9F9F0000, 0x0065656565650000, 0x0087878787870000, 0x006B6B6B6B6B0000, 0x00F4F4F4F4F40000, 0x0023232323230000, 0x0048484848480000, 0x0010101010100000, 0x00D1D1D1D1D10000, 0x0051515151510000, 0x00C0C0C0C0C00000, 0x00F9F9F9F9F90000, 0x00D2D2D2D2D20000, 0x00A0A0A0A0A00000, 0x0055555555550000, 0x00A1A1A1A1A10000, 0x0041414141410000, 0x00FAFAFAFAFA0000, 0x0043434343430000, 0x0013131313130000, 0x00C4C4C4C4C40000, 0x002F2F2F2F2F0000, 0x00A8A8A8A8A80000, 0x00B6B6B6B6B60000, 0x003C3C3C3C3C0000, 0x002B2B2B2B2B0000, 0x00C1C1C1C1C10000, 0x00FFFFFFFFFF0000, 0x00C8C8C8C8C80000, 0x00A5A5A5A5A50000, 0x0020202020200000, 0x0089898989890000, 0x0000000000000000, 0x0090909090900000, 0x0047474747470000, 0x00EFEFEFEFEF0000, 0x00EAEAEAEAEA0000, 0x00B7B7B7B7B70000, 0x0015151515150000, 0x0006060606060000, 0x00CDCDCDCDCD0000, 0x00B5B5B5B5B50000, 0x0012121212120000, 0x007E7E7E7E7E0000, 0x00BBBBBBBBBB0000, 0x0029292929290000, 0x000F0F0F0F0F0000, 0x00B8B8B8B8B80000, 0x0007070707070000, 0x0004040404040000, 0x009B9B9B9B9B0000, 0x0094949494940000, 0x0021212121210000, 0x0066666666660000, 0x00E6E6E6E6E60000, 0x00CECECECECE0000, 0x00EDEDEDEDED0000, 0x00E7E7E7E7E70000, 0x003B3B3B3B3B0000, 0x00FEFEFEFEFE0000, 0x007F7F7F7F7F0000, 0x00C5C5C5C5C50000, 0x00A4A4A4A4A40000, 0x0037373737370000, 0x00B1B1B1B1B10000, 0x004C4C4C4C4C0000, 0x0091919191910000, 0x006E6E6E6E6E0000, 0x008D8D8D8D8D0000, 0x0076767676760000, 0x0003030303030000, 0x002D2D2D2D2D0000, 0x00DEDEDEDEDE0000, 0x0096969696960000, 0x0026262626260000, 0x007D7D7D7D7D0000, 0x00C6C6C6C6C60000, 0x005C5C5C5C5C0000, 0x00D3D3D3D3D30000, 0x00F2F2F2F2F20000, 0x004F4F4F4F4F0000, 0x0019191919190000, 0x003F3F3F3F3F0000, 0x00DCDCDCDCDC0000, 0x0079797979790000, 0x001D1D1D1D1D0000, 0x0052525252520000, 0x00EBEBEBEBEB0000, 0x00F3F3F3F3F30000, 0x006D6D6D6D6D0000, 0x005E5E5E5E5E0000, 0x00FBFBFBFBFB0000, 0x0069696969690000, 0x00B2B2B2B2B20000, 0x00F0F0F0F0F00000, 0x0031313131310000, 0x000C0C0C0C0C0000, 0x00D4D4D4D4D40000, 0x00CFCFCFCFCF0000, 0x008C8C8C8C8C0000, 0x00E2E2E2E2E20000, 0x0075757575750000, 0x00A9A9A9A9A90000, 0x004A4A4A4A4A0000, 0x0057575757570000, 0x0084848484840000, 0x0011111111110000, 0x0045454545450000, 0x001B1B1B1B1B0000, 0x00F5F5F5F5F50000, 0x00E4E4E4E4E40000, 0x000E0E0E0E0E0000, 0x0073737373730000, 0x00AAAAAAAAAA0000, 0x00F1F1F1F1F10000, 0x00DDDDDDDDDD0000, 0x0059595959590000, 0x0014141414140000, 0x006C6C6C6C6C0000, 0x0092929292920000, 0x0054545454540000, 0x00D0D0D0D0D00000, 0x0078787878780000, 0x0070707070700000, 0x00E3E3E3E3E30000, 0x0049494949490000, 0x0080808080800000, 0x0050505050500000, 0x00A7A7A7A7A70000, 0x00F6F6F6F6F60000, 0x0077777777770000, 0x0093939393930000, 0x0086868686860000, 0x0083838383830000, 0x002A2A2A2A2A0000, 0x00C7C7C7C7C70000, 0x005B5B5B5B5B0000, 0x00E9E9E9E9E90000, 0x00EEEEEEEEEE0000, 0x008F8F8F8F8F0000, 0x0001010101010000, 0x003D3D3D3D3D0000 }; const u64bit Camellia_SBOX3[256] = { 0x3800383800383800, 0x4100414100414100, 0x1600161600161600, 0x7600767600767600, 0xD900D9D900D9D900, 0x9300939300939300, 0x6000606000606000, 0xF200F2F200F2F200, 0x7200727200727200, 0xC200C2C200C2C200, 0xAB00ABAB00ABAB00, 0x9A009A9A009A9A00, 0x7500757500757500, 0x0600060600060600, 0x5700575700575700, 0xA000A0A000A0A000, 0x9100919100919100, 0xF700F7F700F7F700, 0xB500B5B500B5B500, 0xC900C9C900C9C900, 0xA200A2A200A2A200, 0x8C008C8C008C8C00, 0xD200D2D200D2D200, 0x9000909000909000, 0xF600F6F600F6F600, 0x0700070700070700, 0xA700A7A700A7A700, 0x2700272700272700, 0x8E008E8E008E8E00, 0xB200B2B200B2B200, 0x4900494900494900, 0xDE00DEDE00DEDE00, 0x4300434300434300, 0x5C005C5C005C5C00, 0xD700D7D700D7D700, 0xC700C7C700C7C700, 0x3E003E3E003E3E00, 0xF500F5F500F5F500, 0x8F008F8F008F8F00, 0x6700676700676700, 0x1F001F1F001F1F00, 0x1800181800181800, 0x6E006E6E006E6E00, 0xAF00AFAF00AFAF00, 0x2F002F2F002F2F00, 0xE200E2E200E2E200, 0x8500858500858500, 0x0D000D0D000D0D00, 0x5300535300535300, 0xF000F0F000F0F000, 0x9C009C9C009C9C00, 0x6500656500656500, 0xEA00EAEA00EAEA00, 0xA300A3A300A3A300, 0xAE00AEAE00AEAE00, 0x9E009E9E009E9E00, 0xEC00ECEC00ECEC00, 0x8000808000808000, 0x2D002D2D002D2D00, 0x6B006B6B006B6B00, 0xA800A8A800A8A800, 0x2B002B2B002B2B00, 0x3600363600363600, 0xA600A6A600A6A600, 0xC500C5C500C5C500, 0x8600868600868600, 0x4D004D4D004D4D00, 0x3300333300333300, 0xFD00FDFD00FDFD00, 0x6600666600666600, 0x5800585800585800, 0x9600969600969600, 0x3A003A3A003A3A00, 0x0900090900090900, 0x9500959500959500, 0x1000101000101000, 0x7800787800787800, 0xD800D8D800D8D800, 0x4200424200424200, 0xCC00CCCC00CCCC00, 0xEF00EFEF00EFEF00, 0x2600262600262600, 0xE500E5E500E5E500, 0x6100616100616100, 0x1A001A1A001A1A00, 0x3F003F3F003F3F00, 0x3B003B3B003B3B00, 0x8200828200828200, 0xB600B6B600B6B600, 0xDB00DBDB00DBDB00, 0xD400D4D400D4D400, 0x9800989800989800, 0xE800E8E800E8E800, 0x8B008B8B008B8B00, 0x0200020200020200, 0xEB00EBEB00EBEB00, 0x0A000A0A000A0A00, 0x2C002C2C002C2C00, 0x1D001D1D001D1D00, 0xB000B0B000B0B000, 0x6F006F6F006F6F00, 0x8D008D8D008D8D00, 0x8800888800888800, 0x0E000E0E000E0E00, 0x1900191900191900, 0x8700878700878700, 0x4E004E4E004E4E00, 0x0B000B0B000B0B00, 0xA900A9A900A9A900, 0x0C000C0C000C0C00, 0x7900797900797900, 0x1100111100111100, 0x7F007F7F007F7F00, 0x2200222200222200, 0xE700E7E700E7E700, 0x5900595900595900, 0xE100E1E100E1E100, 0xDA00DADA00DADA00, 0x3D003D3D003D3D00, 0xC800C8C800C8C800, 0x1200121200121200, 0x0400040400040400, 0x7400747400747400, 0x5400545400545400, 0x3000303000303000, 0x7E007E7E007E7E00, 0xB400B4B400B4B400, 0x2800282800282800, 0x5500555500555500, 0x6800686800686800, 0x5000505000505000, 0xBE00BEBE00BEBE00, 0xD000D0D000D0D000, 0xC400C4C400C4C400, 0x3100313100313100, 0xCB00CBCB00CBCB00, 0x2A002A2A002A2A00, 0xAD00ADAD00ADAD00, 0x0F000F0F000F0F00, 0xCA00CACA00CACA00, 0x7000707000707000, 0xFF00FFFF00FFFF00, 0x3200323200323200, 0x6900696900696900, 0x0800080800080800, 0x6200626200626200, 0x0000000000000000, 0x2400242400242400, 0xD100D1D100D1D100, 0xFB00FBFB00FBFB00, 0xBA00BABA00BABA00, 0xED00EDED00EDED00, 0x4500454500454500, 0x8100818100818100, 0x7300737300737300, 0x6D006D6D006D6D00, 0x8400848400848400, 0x9F009F9F009F9F00, 0xEE00EEEE00EEEE00, 0x4A004A4A004A4A00, 0xC300C3C300C3C300, 0x2E002E2E002E2E00, 0xC100C1C100C1C100, 0x0100010100010100, 0xE600E6E600E6E600, 0x2500252500252500, 0x4800484800484800, 0x9900999900999900, 0xB900B9B900B9B900, 0xB300B3B300B3B300, 0x7B007B7B007B7B00, 0xF900F9F900F9F900, 0xCE00CECE00CECE00, 0xBF00BFBF00BFBF00, 0xDF00DFDF00DFDF00, 0x7100717100717100, 0x2900292900292900, 0xCD00CDCD00CDCD00, 0x6C006C6C006C6C00, 0x1300131300131300, 0x6400646400646400, 0x9B009B9B009B9B00, 0x6300636300636300, 0x9D009D9D009D9D00, 0xC000C0C000C0C000, 0x4B004B4B004B4B00, 0xB700B7B700B7B700, 0xA500A5A500A5A500, 0x8900898900898900, 0x5F005F5F005F5F00, 0xB100B1B100B1B100, 0x1700171700171700, 0xF400F4F400F4F400, 0xBC00BCBC00BCBC00, 0xD300D3D300D3D300, 0x4600464600464600, 0xCF00CFCF00CFCF00, 0x3700373700373700, 0x5E005E5E005E5E00, 0x4700474700474700, 0x9400949400949400, 0xFA00FAFA00FAFA00, 0xFC00FCFC00FCFC00, 0x5B005B5B005B5B00, 0x9700979700979700, 0xFE00FEFE00FEFE00, 0x5A005A5A005A5A00, 0xAC00ACAC00ACAC00, 0x3C003C3C003C3C00, 0x4C004C4C004C4C00, 0x0300030300030300, 0x3500353500353500, 0xF300F3F300F3F300, 0x2300232300232300, 0xB800B8B800B8B800, 0x5D005D5D005D5D00, 0x6A006A6A006A6A00, 0x9200929200929200, 0xD500D5D500D5D500, 0x2100212100212100, 0x4400444400444400, 0x5100515100515100, 0xC600C6C600C6C600, 0x7D007D7D007D7D00, 0x3900393900393900, 0x8300838300838300, 0xDC00DCDC00DCDC00, 0xAA00AAAA00AAAA00, 0x7C007C7C007C7C00, 0x7700777700777700, 0x5600565600565600, 0x0500050500050500, 0x1B001B1B001B1B00, 0xA400A4A400A4A400, 0x1500151500151500, 0x3400343400343400, 0x1E001E1E001E1E00, 0x1C001C1C001C1C00, 0xF800F8F800F8F800, 0x5200525200525200, 0x2000202000202000, 0x1400141400141400, 0xE900E9E900E9E900, 0xBD00BDBD00BDBD00, 0xDD00DDDD00DDDD00, 0xE400E4E400E4E400, 0xA100A1A100A1A100, 0xE000E0E000E0E000, 0x8A008A8A008A8A00, 0xF100F1F100F1F100, 0xD600D6D600D6D600, 0x7A007A7A007A7A00, 0xBB00BBBB00BBBB00, 0xE300E3E300E3E300, 0x4000404000404000, 0x4F004F4F004F4F00 }; const u64bit Camellia_SBOX4[256] = { 0x7070007000007070, 0x2C2C002C00002C2C, 0xB3B300B30000B3B3, 0xC0C000C00000C0C0, 0xE4E400E40000E4E4, 0x5757005700005757, 0xEAEA00EA0000EAEA, 0xAEAE00AE0000AEAE, 0x2323002300002323, 0x6B6B006B00006B6B, 0x4545004500004545, 0xA5A500A50000A5A5, 0xEDED00ED0000EDED, 0x4F4F004F00004F4F, 0x1D1D001D00001D1D, 0x9292009200009292, 0x8686008600008686, 0xAFAF00AF0000AFAF, 0x7C7C007C00007C7C, 0x1F1F001F00001F1F, 0x3E3E003E00003E3E, 0xDCDC00DC0000DCDC, 0x5E5E005E00005E5E, 0x0B0B000B00000B0B, 0xA6A600A60000A6A6, 0x3939003900003939, 0xD5D500D50000D5D5, 0x5D5D005D00005D5D, 0xD9D900D90000D9D9, 0x5A5A005A00005A5A, 0x5151005100005151, 0x6C6C006C00006C6C, 0x8B8B008B00008B8B, 0x9A9A009A00009A9A, 0xFBFB00FB0000FBFB, 0xB0B000B00000B0B0, 0x7474007400007474, 0x2B2B002B00002B2B, 0xF0F000F00000F0F0, 0x8484008400008484, 0xDFDF00DF0000DFDF, 0xCBCB00CB0000CBCB, 0x3434003400003434, 0x7676007600007676, 0x6D6D006D00006D6D, 0xA9A900A90000A9A9, 0xD1D100D10000D1D1, 0x0404000400000404, 0x1414001400001414, 0x3A3A003A00003A3A, 0xDEDE00DE0000DEDE, 0x1111001100001111, 0x3232003200003232, 0x9C9C009C00009C9C, 0x5353005300005353, 0xF2F200F20000F2F2, 0xFEFE00FE0000FEFE, 0xCFCF00CF0000CFCF, 0xC3C300C30000C3C3, 0x7A7A007A00007A7A, 0x2424002400002424, 0xE8E800E80000E8E8, 0x6060006000006060, 0x6969006900006969, 0xAAAA00AA0000AAAA, 0xA0A000A00000A0A0, 0xA1A100A10000A1A1, 0x6262006200006262, 0x5454005400005454, 0x1E1E001E00001E1E, 0xE0E000E00000E0E0, 0x6464006400006464, 0x1010001000001010, 0x0000000000000000, 0xA3A300A30000A3A3, 0x7575007500007575, 0x8A8A008A00008A8A, 0xE6E600E60000E6E6, 0x0909000900000909, 0xDDDD00DD0000DDDD, 0x8787008700008787, 0x8383008300008383, 0xCDCD00CD0000CDCD, 0x9090009000009090, 0x7373007300007373, 0xF6F600F60000F6F6, 0x9D9D009D00009D9D, 0xBFBF00BF0000BFBF, 0x5252005200005252, 0xD8D800D80000D8D8, 0xC8C800C80000C8C8, 0xC6C600C60000C6C6, 0x8181008100008181, 0x6F6F006F00006F6F, 0x1313001300001313, 0x6363006300006363, 0xE9E900E90000E9E9, 0xA7A700A70000A7A7, 0x9F9F009F00009F9F, 0xBCBC00BC0000BCBC, 0x2929002900002929, 0xF9F900F90000F9F9, 0x2F2F002F00002F2F, 0xB4B400B40000B4B4, 0x7878007800007878, 0x0606000600000606, 0xE7E700E70000E7E7, 0x7171007100007171, 0xD4D400D40000D4D4, 0xABAB00AB0000ABAB, 0x8888008800008888, 0x8D8D008D00008D8D, 0x7272007200007272, 0xB9B900B90000B9B9, 0xF8F800F80000F8F8, 0xACAC00AC0000ACAC, 0x3636003600003636, 0x2A2A002A00002A2A, 0x3C3C003C00003C3C, 0xF1F100F10000F1F1, 0x4040004000004040, 0xD3D300D30000D3D3, 0xBBBB00BB0000BBBB, 0x4343004300004343, 0x1515001500001515, 0xADAD00AD0000ADAD, 0x7777007700007777, 0x8080008000008080, 0x8282008200008282, 0xECEC00EC0000ECEC, 0x2727002700002727, 0xE5E500E50000E5E5, 0x8585008500008585, 0x3535003500003535, 0x0C0C000C00000C0C, 0x4141004100004141, 0xEFEF00EF0000EFEF, 0x9393009300009393, 0x1919001900001919, 0x2121002100002121, 0x0E0E000E00000E0E, 0x4E4E004E00004E4E, 0x6565006500006565, 0xBDBD00BD0000BDBD, 0xB8B800B80000B8B8, 0x8F8F008F00008F8F, 0xEBEB00EB0000EBEB, 0xCECE00CE0000CECE, 0x3030003000003030, 0x5F5F005F00005F5F, 0xC5C500C50000C5C5, 0x1A1A001A00001A1A, 0xE1E100E10000E1E1, 0xCACA00CA0000CACA, 0x4747004700004747, 0x3D3D003D00003D3D, 0x0101000100000101, 0xD6D600D60000D6D6, 0x5656005600005656, 0x4D4D004D00004D4D, 0x0D0D000D00000D0D, 0x6666006600006666, 0xCCCC00CC0000CCCC, 0x2D2D002D00002D2D, 0x1212001200001212, 0x2020002000002020, 0xB1B100B10000B1B1, 0x9999009900009999, 0x4C4C004C00004C4C, 0xC2C200C20000C2C2, 0x7E7E007E00007E7E, 0x0505000500000505, 0xB7B700B70000B7B7, 0x3131003100003131, 0x1717001700001717, 0xD7D700D70000D7D7, 0x5858005800005858, 0x6161006100006161, 0x1B1B001B00001B1B, 0x1C1C001C00001C1C, 0x0F0F000F00000F0F, 0x1616001600001616, 0x1818001800001818, 0x2222002200002222, 0x4444004400004444, 0xB2B200B20000B2B2, 0xB5B500B50000B5B5, 0x9191009100009191, 0x0808000800000808, 0xA8A800A80000A8A8, 0xFCFC00FC0000FCFC, 0x5050005000005050, 0xD0D000D00000D0D0, 0x7D7D007D00007D7D, 0x8989008900008989, 0x9797009700009797, 0x5B5B005B00005B5B, 0x9595009500009595, 0xFFFF00FF0000FFFF, 0xD2D200D20000D2D2, 0xC4C400C40000C4C4, 0x4848004800004848, 0xF7F700F70000F7F7, 0xDBDB00DB0000DBDB, 0x0303000300000303, 0xDADA00DA0000DADA, 0x3F3F003F00003F3F, 0x9494009400009494, 0x5C5C005C00005C5C, 0x0202000200000202, 0x4A4A004A00004A4A, 0x3333003300003333, 0x6767006700006767, 0xF3F300F30000F3F3, 0x7F7F007F00007F7F, 0xE2E200E20000E2E2, 0x9B9B009B00009B9B, 0x2626002600002626, 0x3737003700003737, 0x3B3B003B00003B3B, 0x9696009600009696, 0x4B4B004B00004B4B, 0xBEBE00BE0000BEBE, 0x2E2E002E00002E2E, 0x7979007900007979, 0x8C8C008C00008C8C, 0x6E6E006E00006E6E, 0x8E8E008E00008E8E, 0xF5F500F50000F5F5, 0xB6B600B60000B6B6, 0xFDFD00FD0000FDFD, 0x5959005900005959, 0x9898009800009898, 0x6A6A006A00006A6A, 0x4646004600004646, 0xBABA00BA0000BABA, 0x2525002500002525, 0x4242004200004242, 0xA2A200A20000A2A2, 0xFAFA00FA0000FAFA, 0x0707000700000707, 0x5555005500005555, 0xEEEE00EE0000EEEE, 0x0A0A000A00000A0A, 0x4949004900004949, 0x6868006800006868, 0x3838003800003838, 0xA4A400A40000A4A4, 0x2828002800002828, 0x7B7B007B00007B7B, 0xC9C900C90000C9C9, 0xC1C100C10000C1C1, 0xE3E300E30000E3E3, 0xF4F400F40000F4F4, 0xC7C700C70000C7C7, 0x9E9E009E00009E9E }; const u64bit Camellia_SBOX5[256] = { 0x00E0E0E000E0E0E0, 0x0005050500050505, 0x0058585800585858, 0x00D9D9D900D9D9D9, 0x0067676700676767, 0x004E4E4E004E4E4E, 0x0081818100818181, 0x00CBCBCB00CBCBCB, 0x00C9C9C900C9C9C9, 0x000B0B0B000B0B0B, 0x00AEAEAE00AEAEAE, 0x006A6A6A006A6A6A, 0x00D5D5D500D5D5D5, 0x0018181800181818, 0x005D5D5D005D5D5D, 0x0082828200828282, 0x0046464600464646, 0x00DFDFDF00DFDFDF, 0x00D6D6D600D6D6D6, 0x0027272700272727, 0x008A8A8A008A8A8A, 0x0032323200323232, 0x004B4B4B004B4B4B, 0x0042424200424242, 0x00DBDBDB00DBDBDB, 0x001C1C1C001C1C1C, 0x009E9E9E009E9E9E, 0x009C9C9C009C9C9C, 0x003A3A3A003A3A3A, 0x00CACACA00CACACA, 0x0025252500252525, 0x007B7B7B007B7B7B, 0x000D0D0D000D0D0D, 0x0071717100717171, 0x005F5F5F005F5F5F, 0x001F1F1F001F1F1F, 0x00F8F8F800F8F8F8, 0x00D7D7D700D7D7D7, 0x003E3E3E003E3E3E, 0x009D9D9D009D9D9D, 0x007C7C7C007C7C7C, 0x0060606000606060, 0x00B9B9B900B9B9B9, 0x00BEBEBE00BEBEBE, 0x00BCBCBC00BCBCBC, 0x008B8B8B008B8B8B, 0x0016161600161616, 0x0034343400343434, 0x004D4D4D004D4D4D, 0x00C3C3C300C3C3C3, 0x0072727200727272, 0x0095959500959595, 0x00ABABAB00ABABAB, 0x008E8E8E008E8E8E, 0x00BABABA00BABABA, 0x007A7A7A007A7A7A, 0x00B3B3B300B3B3B3, 0x0002020200020202, 0x00B4B4B400B4B4B4, 0x00ADADAD00ADADAD, 0x00A2A2A200A2A2A2, 0x00ACACAC00ACACAC, 0x00D8D8D800D8D8D8, 0x009A9A9A009A9A9A, 0x0017171700171717, 0x001A1A1A001A1A1A, 0x0035353500353535, 0x00CCCCCC00CCCCCC, 0x00F7F7F700F7F7F7, 0x0099999900999999, 0x0061616100616161, 0x005A5A5A005A5A5A, 0x00E8E8E800E8E8E8, 0x0024242400242424, 0x0056565600565656, 0x0040404000404040, 0x00E1E1E100E1E1E1, 0x0063636300636363, 0x0009090900090909, 0x0033333300333333, 0x00BFBFBF00BFBFBF, 0x0098989800989898, 0x0097979700979797, 0x0085858500858585, 0x0068686800686868, 0x00FCFCFC00FCFCFC, 0x00ECECEC00ECECEC, 0x000A0A0A000A0A0A, 0x00DADADA00DADADA, 0x006F6F6F006F6F6F, 0x0053535300535353, 0x0062626200626262, 0x00A3A3A300A3A3A3, 0x002E2E2E002E2E2E, 0x0008080800080808, 0x00AFAFAF00AFAFAF, 0x0028282800282828, 0x00B0B0B000B0B0B0, 0x0074747400747474, 0x00C2C2C200C2C2C2, 0x00BDBDBD00BDBDBD, 0x0036363600363636, 0x0022222200222222, 0x0038383800383838, 0x0064646400646464, 0x001E1E1E001E1E1E, 0x0039393900393939, 0x002C2C2C002C2C2C, 0x00A6A6A600A6A6A6, 0x0030303000303030, 0x00E5E5E500E5E5E5, 0x0044444400444444, 0x00FDFDFD00FDFDFD, 0x0088888800888888, 0x009F9F9F009F9F9F, 0x0065656500656565, 0x0087878700878787, 0x006B6B6B006B6B6B, 0x00F4F4F400F4F4F4, 0x0023232300232323, 0x0048484800484848, 0x0010101000101010, 0x00D1D1D100D1D1D1, 0x0051515100515151, 0x00C0C0C000C0C0C0, 0x00F9F9F900F9F9F9, 0x00D2D2D200D2D2D2, 0x00A0A0A000A0A0A0, 0x0055555500555555, 0x00A1A1A100A1A1A1, 0x0041414100414141, 0x00FAFAFA00FAFAFA, 0x0043434300434343, 0x0013131300131313, 0x00C4C4C400C4C4C4, 0x002F2F2F002F2F2F, 0x00A8A8A800A8A8A8, 0x00B6B6B600B6B6B6, 0x003C3C3C003C3C3C, 0x002B2B2B002B2B2B, 0x00C1C1C100C1C1C1, 0x00FFFFFF00FFFFFF, 0x00C8C8C800C8C8C8, 0x00A5A5A500A5A5A5, 0x0020202000202020, 0x0089898900898989, 0x0000000000000000, 0x0090909000909090, 0x0047474700474747, 0x00EFEFEF00EFEFEF, 0x00EAEAEA00EAEAEA, 0x00B7B7B700B7B7B7, 0x0015151500151515, 0x0006060600060606, 0x00CDCDCD00CDCDCD, 0x00B5B5B500B5B5B5, 0x0012121200121212, 0x007E7E7E007E7E7E, 0x00BBBBBB00BBBBBB, 0x0029292900292929, 0x000F0F0F000F0F0F, 0x00B8B8B800B8B8B8, 0x0007070700070707, 0x0004040400040404, 0x009B9B9B009B9B9B, 0x0094949400949494, 0x0021212100212121, 0x0066666600666666, 0x00E6E6E600E6E6E6, 0x00CECECE00CECECE, 0x00EDEDED00EDEDED, 0x00E7E7E700E7E7E7, 0x003B3B3B003B3B3B, 0x00FEFEFE00FEFEFE, 0x007F7F7F007F7F7F, 0x00C5C5C500C5C5C5, 0x00A4A4A400A4A4A4, 0x0037373700373737, 0x00B1B1B100B1B1B1, 0x004C4C4C004C4C4C, 0x0091919100919191, 0x006E6E6E006E6E6E, 0x008D8D8D008D8D8D, 0x0076767600767676, 0x0003030300030303, 0x002D2D2D002D2D2D, 0x00DEDEDE00DEDEDE, 0x0096969600969696, 0x0026262600262626, 0x007D7D7D007D7D7D, 0x00C6C6C600C6C6C6, 0x005C5C5C005C5C5C, 0x00D3D3D300D3D3D3, 0x00F2F2F200F2F2F2, 0x004F4F4F004F4F4F, 0x0019191900191919, 0x003F3F3F003F3F3F, 0x00DCDCDC00DCDCDC, 0x0079797900797979, 0x001D1D1D001D1D1D, 0x0052525200525252, 0x00EBEBEB00EBEBEB, 0x00F3F3F300F3F3F3, 0x006D6D6D006D6D6D, 0x005E5E5E005E5E5E, 0x00FBFBFB00FBFBFB, 0x0069696900696969, 0x00B2B2B200B2B2B2, 0x00F0F0F000F0F0F0, 0x0031313100313131, 0x000C0C0C000C0C0C, 0x00D4D4D400D4D4D4, 0x00CFCFCF00CFCFCF, 0x008C8C8C008C8C8C, 0x00E2E2E200E2E2E2, 0x0075757500757575, 0x00A9A9A900A9A9A9, 0x004A4A4A004A4A4A, 0x0057575700575757, 0x0084848400848484, 0x0011111100111111, 0x0045454500454545, 0x001B1B1B001B1B1B, 0x00F5F5F500F5F5F5, 0x00E4E4E400E4E4E4, 0x000E0E0E000E0E0E, 0x0073737300737373, 0x00AAAAAA00AAAAAA, 0x00F1F1F100F1F1F1, 0x00DDDDDD00DDDDDD, 0x0059595900595959, 0x0014141400141414, 0x006C6C6C006C6C6C, 0x0092929200929292, 0x0054545400545454, 0x00D0D0D000D0D0D0, 0x0078787800787878, 0x0070707000707070, 0x00E3E3E300E3E3E3, 0x0049494900494949, 0x0080808000808080, 0x0050505000505050, 0x00A7A7A700A7A7A7, 0x00F6F6F600F6F6F6, 0x0077777700777777, 0x0093939300939393, 0x0086868600868686, 0x0083838300838383, 0x002A2A2A002A2A2A, 0x00C7C7C700C7C7C7, 0x005B5B5B005B5B5B, 0x00E9E9E900E9E9E9, 0x00EEEEEE00EEEEEE, 0x008F8F8F008F8F8F, 0x0001010100010101, 0x003D3D3D003D3D3D }; const u64bit Camellia_SBOX6[256] = { 0x3800383838003838, 0x4100414141004141, 0x1600161616001616, 0x7600767676007676, 0xD900D9D9D900D9D9, 0x9300939393009393, 0x6000606060006060, 0xF200F2F2F200F2F2, 0x7200727272007272, 0xC200C2C2C200C2C2, 0xAB00ABABAB00ABAB, 0x9A009A9A9A009A9A, 0x7500757575007575, 0x0600060606000606, 0x5700575757005757, 0xA000A0A0A000A0A0, 0x9100919191009191, 0xF700F7F7F700F7F7, 0xB500B5B5B500B5B5, 0xC900C9C9C900C9C9, 0xA200A2A2A200A2A2, 0x8C008C8C8C008C8C, 0xD200D2D2D200D2D2, 0x9000909090009090, 0xF600F6F6F600F6F6, 0x0700070707000707, 0xA700A7A7A700A7A7, 0x2700272727002727, 0x8E008E8E8E008E8E, 0xB200B2B2B200B2B2, 0x4900494949004949, 0xDE00DEDEDE00DEDE, 0x4300434343004343, 0x5C005C5C5C005C5C, 0xD700D7D7D700D7D7, 0xC700C7C7C700C7C7, 0x3E003E3E3E003E3E, 0xF500F5F5F500F5F5, 0x8F008F8F8F008F8F, 0x6700676767006767, 0x1F001F1F1F001F1F, 0x1800181818001818, 0x6E006E6E6E006E6E, 0xAF00AFAFAF00AFAF, 0x2F002F2F2F002F2F, 0xE200E2E2E200E2E2, 0x8500858585008585, 0x0D000D0D0D000D0D, 0x5300535353005353, 0xF000F0F0F000F0F0, 0x9C009C9C9C009C9C, 0x6500656565006565, 0xEA00EAEAEA00EAEA, 0xA300A3A3A300A3A3, 0xAE00AEAEAE00AEAE, 0x9E009E9E9E009E9E, 0xEC00ECECEC00ECEC, 0x8000808080008080, 0x2D002D2D2D002D2D, 0x6B006B6B6B006B6B, 0xA800A8A8A800A8A8, 0x2B002B2B2B002B2B, 0x3600363636003636, 0xA600A6A6A600A6A6, 0xC500C5C5C500C5C5, 0x8600868686008686, 0x4D004D4D4D004D4D, 0x3300333333003333, 0xFD00FDFDFD00FDFD, 0x6600666666006666, 0x5800585858005858, 0x9600969696009696, 0x3A003A3A3A003A3A, 0x0900090909000909, 0x9500959595009595, 0x1000101010001010, 0x7800787878007878, 0xD800D8D8D800D8D8, 0x4200424242004242, 0xCC00CCCCCC00CCCC, 0xEF00EFEFEF00EFEF, 0x2600262626002626, 0xE500E5E5E500E5E5, 0x6100616161006161, 0x1A001A1A1A001A1A, 0x3F003F3F3F003F3F, 0x3B003B3B3B003B3B, 0x8200828282008282, 0xB600B6B6B600B6B6, 0xDB00DBDBDB00DBDB, 0xD400D4D4D400D4D4, 0x9800989898009898, 0xE800E8E8E800E8E8, 0x8B008B8B8B008B8B, 0x0200020202000202, 0xEB00EBEBEB00EBEB, 0x0A000A0A0A000A0A, 0x2C002C2C2C002C2C, 0x1D001D1D1D001D1D, 0xB000B0B0B000B0B0, 0x6F006F6F6F006F6F, 0x8D008D8D8D008D8D, 0x8800888888008888, 0x0E000E0E0E000E0E, 0x1900191919001919, 0x8700878787008787, 0x4E004E4E4E004E4E, 0x0B000B0B0B000B0B, 0xA900A9A9A900A9A9, 0x0C000C0C0C000C0C, 0x7900797979007979, 0x1100111111001111, 0x7F007F7F7F007F7F, 0x2200222222002222, 0xE700E7E7E700E7E7, 0x5900595959005959, 0xE100E1E1E100E1E1, 0xDA00DADADA00DADA, 0x3D003D3D3D003D3D, 0xC800C8C8C800C8C8, 0x1200121212001212, 0x0400040404000404, 0x7400747474007474, 0x5400545454005454, 0x3000303030003030, 0x7E007E7E7E007E7E, 0xB400B4B4B400B4B4, 0x2800282828002828, 0x5500555555005555, 0x6800686868006868, 0x5000505050005050, 0xBE00BEBEBE00BEBE, 0xD000D0D0D000D0D0, 0xC400C4C4C400C4C4, 0x3100313131003131, 0xCB00CBCBCB00CBCB, 0x2A002A2A2A002A2A, 0xAD00ADADAD00ADAD, 0x0F000F0F0F000F0F, 0xCA00CACACA00CACA, 0x7000707070007070, 0xFF00FFFFFF00FFFF, 0x3200323232003232, 0x6900696969006969, 0x0800080808000808, 0x6200626262006262, 0x0000000000000000, 0x2400242424002424, 0xD100D1D1D100D1D1, 0xFB00FBFBFB00FBFB, 0xBA00BABABA00BABA, 0xED00EDEDED00EDED, 0x4500454545004545, 0x8100818181008181, 0x7300737373007373, 0x6D006D6D6D006D6D, 0x8400848484008484, 0x9F009F9F9F009F9F, 0xEE00EEEEEE00EEEE, 0x4A004A4A4A004A4A, 0xC300C3C3C300C3C3, 0x2E002E2E2E002E2E, 0xC100C1C1C100C1C1, 0x0100010101000101, 0xE600E6E6E600E6E6, 0x2500252525002525, 0x4800484848004848, 0x9900999999009999, 0xB900B9B9B900B9B9, 0xB300B3B3B300B3B3, 0x7B007B7B7B007B7B, 0xF900F9F9F900F9F9, 0xCE00CECECE00CECE, 0xBF00BFBFBF00BFBF, 0xDF00DFDFDF00DFDF, 0x7100717171007171, 0x2900292929002929, 0xCD00CDCDCD00CDCD, 0x6C006C6C6C006C6C, 0x1300131313001313, 0x6400646464006464, 0x9B009B9B9B009B9B, 0x6300636363006363, 0x9D009D9D9D009D9D, 0xC000C0C0C000C0C0, 0x4B004B4B4B004B4B, 0xB700B7B7B700B7B7, 0xA500A5A5A500A5A5, 0x8900898989008989, 0x5F005F5F5F005F5F, 0xB100B1B1B100B1B1, 0x1700171717001717, 0xF400F4F4F400F4F4, 0xBC00BCBCBC00BCBC, 0xD300D3D3D300D3D3, 0x4600464646004646, 0xCF00CFCFCF00CFCF, 0x3700373737003737, 0x5E005E5E5E005E5E, 0x4700474747004747, 0x9400949494009494, 0xFA00FAFAFA00FAFA, 0xFC00FCFCFC00FCFC, 0x5B005B5B5B005B5B, 0x9700979797009797, 0xFE00FEFEFE00FEFE, 0x5A005A5A5A005A5A, 0xAC00ACACAC00ACAC, 0x3C003C3C3C003C3C, 0x4C004C4C4C004C4C, 0x0300030303000303, 0x3500353535003535, 0xF300F3F3F300F3F3, 0x2300232323002323, 0xB800B8B8B800B8B8, 0x5D005D5D5D005D5D, 0x6A006A6A6A006A6A, 0x9200929292009292, 0xD500D5D5D500D5D5, 0x2100212121002121, 0x4400444444004444, 0x5100515151005151, 0xC600C6C6C600C6C6, 0x7D007D7D7D007D7D, 0x3900393939003939, 0x8300838383008383, 0xDC00DCDCDC00DCDC, 0xAA00AAAAAA00AAAA, 0x7C007C7C7C007C7C, 0x7700777777007777, 0x5600565656005656, 0x0500050505000505, 0x1B001B1B1B001B1B, 0xA400A4A4A400A4A4, 0x1500151515001515, 0x3400343434003434, 0x1E001E1E1E001E1E, 0x1C001C1C1C001C1C, 0xF800F8F8F800F8F8, 0x5200525252005252, 0x2000202020002020, 0x1400141414001414, 0xE900E9E9E900E9E9, 0xBD00BDBDBD00BDBD, 0xDD00DDDDDD00DDDD, 0xE400E4E4E400E4E4, 0xA100A1A1A100A1A1, 0xE000E0E0E000E0E0, 0x8A008A8A8A008A8A, 0xF100F1F1F100F1F1, 0xD600D6D6D600D6D6, 0x7A007A7A7A007A7A, 0xBB00BBBBBB00BBBB, 0xE300E3E3E300E3E3, 0x4000404040004040, 0x4F004F4F4F004F4F }; const u64bit Camellia_SBOX7[256] = { 0x7070007070700070, 0x2C2C002C2C2C002C, 0xB3B300B3B3B300B3, 0xC0C000C0C0C000C0, 0xE4E400E4E4E400E4, 0x5757005757570057, 0xEAEA00EAEAEA00EA, 0xAEAE00AEAEAE00AE, 0x2323002323230023, 0x6B6B006B6B6B006B, 0x4545004545450045, 0xA5A500A5A5A500A5, 0xEDED00EDEDED00ED, 0x4F4F004F4F4F004F, 0x1D1D001D1D1D001D, 0x9292009292920092, 0x8686008686860086, 0xAFAF00AFAFAF00AF, 0x7C7C007C7C7C007C, 0x1F1F001F1F1F001F, 0x3E3E003E3E3E003E, 0xDCDC00DCDCDC00DC, 0x5E5E005E5E5E005E, 0x0B0B000B0B0B000B, 0xA6A600A6A6A600A6, 0x3939003939390039, 0xD5D500D5D5D500D5, 0x5D5D005D5D5D005D, 0xD9D900D9D9D900D9, 0x5A5A005A5A5A005A, 0x5151005151510051, 0x6C6C006C6C6C006C, 0x8B8B008B8B8B008B, 0x9A9A009A9A9A009A, 0xFBFB00FBFBFB00FB, 0xB0B000B0B0B000B0, 0x7474007474740074, 0x2B2B002B2B2B002B, 0xF0F000F0F0F000F0, 0x8484008484840084, 0xDFDF00DFDFDF00DF, 0xCBCB00CBCBCB00CB, 0x3434003434340034, 0x7676007676760076, 0x6D6D006D6D6D006D, 0xA9A900A9A9A900A9, 0xD1D100D1D1D100D1, 0x0404000404040004, 0x1414001414140014, 0x3A3A003A3A3A003A, 0xDEDE00DEDEDE00DE, 0x1111001111110011, 0x3232003232320032, 0x9C9C009C9C9C009C, 0x5353005353530053, 0xF2F200F2F2F200F2, 0xFEFE00FEFEFE00FE, 0xCFCF00CFCFCF00CF, 0xC3C300C3C3C300C3, 0x7A7A007A7A7A007A, 0x2424002424240024, 0xE8E800E8E8E800E8, 0x6060006060600060, 0x6969006969690069, 0xAAAA00AAAAAA00AA, 0xA0A000A0A0A000A0, 0xA1A100A1A1A100A1, 0x6262006262620062, 0x5454005454540054, 0x1E1E001E1E1E001E, 0xE0E000E0E0E000E0, 0x6464006464640064, 0x1010001010100010, 0x0000000000000000, 0xA3A300A3A3A300A3, 0x7575007575750075, 0x8A8A008A8A8A008A, 0xE6E600E6E6E600E6, 0x0909000909090009, 0xDDDD00DDDDDD00DD, 0x8787008787870087, 0x8383008383830083, 0xCDCD00CDCDCD00CD, 0x9090009090900090, 0x7373007373730073, 0xF6F600F6F6F600F6, 0x9D9D009D9D9D009D, 0xBFBF00BFBFBF00BF, 0x5252005252520052, 0xD8D800D8D8D800D8, 0xC8C800C8C8C800C8, 0xC6C600C6C6C600C6, 0x8181008181810081, 0x6F6F006F6F6F006F, 0x1313001313130013, 0x6363006363630063, 0xE9E900E9E9E900E9, 0xA7A700A7A7A700A7, 0x9F9F009F9F9F009F, 0xBCBC00BCBCBC00BC, 0x2929002929290029, 0xF9F900F9F9F900F9, 0x2F2F002F2F2F002F, 0xB4B400B4B4B400B4, 0x7878007878780078, 0x0606000606060006, 0xE7E700E7E7E700E7, 0x7171007171710071, 0xD4D400D4D4D400D4, 0xABAB00ABABAB00AB, 0x8888008888880088, 0x8D8D008D8D8D008D, 0x7272007272720072, 0xB9B900B9B9B900B9, 0xF8F800F8F8F800F8, 0xACAC00ACACAC00AC, 0x3636003636360036, 0x2A2A002A2A2A002A, 0x3C3C003C3C3C003C, 0xF1F100F1F1F100F1, 0x4040004040400040, 0xD3D300D3D3D300D3, 0xBBBB00BBBBBB00BB, 0x4343004343430043, 0x1515001515150015, 0xADAD00ADADAD00AD, 0x7777007777770077, 0x8080008080800080, 0x8282008282820082, 0xECEC00ECECEC00EC, 0x2727002727270027, 0xE5E500E5E5E500E5, 0x8585008585850085, 0x3535003535350035, 0x0C0C000C0C0C000C, 0x4141004141410041, 0xEFEF00EFEFEF00EF, 0x9393009393930093, 0x1919001919190019, 0x2121002121210021, 0x0E0E000E0E0E000E, 0x4E4E004E4E4E004E, 0x6565006565650065, 0xBDBD00BDBDBD00BD, 0xB8B800B8B8B800B8, 0x8F8F008F8F8F008F, 0xEBEB00EBEBEB00EB, 0xCECE00CECECE00CE, 0x3030003030300030, 0x5F5F005F5F5F005F, 0xC5C500C5C5C500C5, 0x1A1A001A1A1A001A, 0xE1E100E1E1E100E1, 0xCACA00CACACA00CA, 0x4747004747470047, 0x3D3D003D3D3D003D, 0x0101000101010001, 0xD6D600D6D6D600D6, 0x5656005656560056, 0x4D4D004D4D4D004D, 0x0D0D000D0D0D000D, 0x6666006666660066, 0xCCCC00CCCCCC00CC, 0x2D2D002D2D2D002D, 0x1212001212120012, 0x2020002020200020, 0xB1B100B1B1B100B1, 0x9999009999990099, 0x4C4C004C4C4C004C, 0xC2C200C2C2C200C2, 0x7E7E007E7E7E007E, 0x0505000505050005, 0xB7B700B7B7B700B7, 0x3131003131310031, 0x1717001717170017, 0xD7D700D7D7D700D7, 0x5858005858580058, 0x6161006161610061, 0x1B1B001B1B1B001B, 0x1C1C001C1C1C001C, 0x0F0F000F0F0F000F, 0x1616001616160016, 0x1818001818180018, 0x2222002222220022, 0x4444004444440044, 0xB2B200B2B2B200B2, 0xB5B500B5B5B500B5, 0x9191009191910091, 0x0808000808080008, 0xA8A800A8A8A800A8, 0xFCFC00FCFCFC00FC, 0x5050005050500050, 0xD0D000D0D0D000D0, 0x7D7D007D7D7D007D, 0x8989008989890089, 0x9797009797970097, 0x5B5B005B5B5B005B, 0x9595009595950095, 0xFFFF00FFFFFF00FF, 0xD2D200D2D2D200D2, 0xC4C400C4C4C400C4, 0x4848004848480048, 0xF7F700F7F7F700F7, 0xDBDB00DBDBDB00DB, 0x0303000303030003, 0xDADA00DADADA00DA, 0x3F3F003F3F3F003F, 0x9494009494940094, 0x5C5C005C5C5C005C, 0x0202000202020002, 0x4A4A004A4A4A004A, 0x3333003333330033, 0x6767006767670067, 0xF3F300F3F3F300F3, 0x7F7F007F7F7F007F, 0xE2E200E2E2E200E2, 0x9B9B009B9B9B009B, 0x2626002626260026, 0x3737003737370037, 0x3B3B003B3B3B003B, 0x9696009696960096, 0x4B4B004B4B4B004B, 0xBEBE00BEBEBE00BE, 0x2E2E002E2E2E002E, 0x7979007979790079, 0x8C8C008C8C8C008C, 0x6E6E006E6E6E006E, 0x8E8E008E8E8E008E, 0xF5F500F5F5F500F5, 0xB6B600B6B6B600B6, 0xFDFD00FDFDFD00FD, 0x5959005959590059, 0x9898009898980098, 0x6A6A006A6A6A006A, 0x4646004646460046, 0xBABA00BABABA00BA, 0x2525002525250025, 0x4242004242420042, 0xA2A200A2A2A200A2, 0xFAFA00FAFAFA00FA, 0x0707000707070007, 0x5555005555550055, 0xEEEE00EEEEEE00EE, 0x0A0A000A0A0A000A, 0x4949004949490049, 0x6868006868680068, 0x3838003838380038, 0xA4A400A4A4A400A4, 0x2828002828280028, 0x7B7B007B7B7B007B, 0xC9C900C9C9C900C9, 0xC1C100C1C1C100C1, 0xE3E300E3E3E300E3, 0xF4F400F4F4F400F4, 0xC7C700C7C7C700C7, 0x9E9E009E9E9E009E }; const u64bit Camellia_SBOX8[256] = { 0x7070700070707000, 0x8282820082828200, 0x2C2C2C002C2C2C00, 0xECECEC00ECECEC00, 0xB3B3B300B3B3B300, 0x2727270027272700, 0xC0C0C000C0C0C000, 0xE5E5E500E5E5E500, 0xE4E4E400E4E4E400, 0x8585850085858500, 0x5757570057575700, 0x3535350035353500, 0xEAEAEA00EAEAEA00, 0x0C0C0C000C0C0C00, 0xAEAEAE00AEAEAE00, 0x4141410041414100, 0x2323230023232300, 0xEFEFEF00EFEFEF00, 0x6B6B6B006B6B6B00, 0x9393930093939300, 0x4545450045454500, 0x1919190019191900, 0xA5A5A500A5A5A500, 0x2121210021212100, 0xEDEDED00EDEDED00, 0x0E0E0E000E0E0E00, 0x4F4F4F004F4F4F00, 0x4E4E4E004E4E4E00, 0x1D1D1D001D1D1D00, 0x6565650065656500, 0x9292920092929200, 0xBDBDBD00BDBDBD00, 0x8686860086868600, 0xB8B8B800B8B8B800, 0xAFAFAF00AFAFAF00, 0x8F8F8F008F8F8F00, 0x7C7C7C007C7C7C00, 0xEBEBEB00EBEBEB00, 0x1F1F1F001F1F1F00, 0xCECECE00CECECE00, 0x3E3E3E003E3E3E00, 0x3030300030303000, 0xDCDCDC00DCDCDC00, 0x5F5F5F005F5F5F00, 0x5E5E5E005E5E5E00, 0xC5C5C500C5C5C500, 0x0B0B0B000B0B0B00, 0x1A1A1A001A1A1A00, 0xA6A6A600A6A6A600, 0xE1E1E100E1E1E100, 0x3939390039393900, 0xCACACA00CACACA00, 0xD5D5D500D5D5D500, 0x4747470047474700, 0x5D5D5D005D5D5D00, 0x3D3D3D003D3D3D00, 0xD9D9D900D9D9D900, 0x0101010001010100, 0x5A5A5A005A5A5A00, 0xD6D6D600D6D6D600, 0x5151510051515100, 0x5656560056565600, 0x6C6C6C006C6C6C00, 0x4D4D4D004D4D4D00, 0x8B8B8B008B8B8B00, 0x0D0D0D000D0D0D00, 0x9A9A9A009A9A9A00, 0x6666660066666600, 0xFBFBFB00FBFBFB00, 0xCCCCCC00CCCCCC00, 0xB0B0B000B0B0B000, 0x2D2D2D002D2D2D00, 0x7474740074747400, 0x1212120012121200, 0x2B2B2B002B2B2B00, 0x2020200020202000, 0xF0F0F000F0F0F000, 0xB1B1B100B1B1B100, 0x8484840084848400, 0x9999990099999900, 0xDFDFDF00DFDFDF00, 0x4C4C4C004C4C4C00, 0xCBCBCB00CBCBCB00, 0xC2C2C200C2C2C200, 0x3434340034343400, 0x7E7E7E007E7E7E00, 0x7676760076767600, 0x0505050005050500, 0x6D6D6D006D6D6D00, 0xB7B7B700B7B7B700, 0xA9A9A900A9A9A900, 0x3131310031313100, 0xD1D1D100D1D1D100, 0x1717170017171700, 0x0404040004040400, 0xD7D7D700D7D7D700, 0x1414140014141400, 0x5858580058585800, 0x3A3A3A003A3A3A00, 0x6161610061616100, 0xDEDEDE00DEDEDE00, 0x1B1B1B001B1B1B00, 0x1111110011111100, 0x1C1C1C001C1C1C00, 0x3232320032323200, 0x0F0F0F000F0F0F00, 0x9C9C9C009C9C9C00, 0x1616160016161600, 0x5353530053535300, 0x1818180018181800, 0xF2F2F200F2F2F200, 0x2222220022222200, 0xFEFEFE00FEFEFE00, 0x4444440044444400, 0xCFCFCF00CFCFCF00, 0xB2B2B200B2B2B200, 0xC3C3C300C3C3C300, 0xB5B5B500B5B5B500, 0x7A7A7A007A7A7A00, 0x9191910091919100, 0x2424240024242400, 0x0808080008080800, 0xE8E8E800E8E8E800, 0xA8A8A800A8A8A800, 0x6060600060606000, 0xFCFCFC00FCFCFC00, 0x6969690069696900, 0x5050500050505000, 0xAAAAAA00AAAAAA00, 0xD0D0D000D0D0D000, 0xA0A0A000A0A0A000, 0x7D7D7D007D7D7D00, 0xA1A1A100A1A1A100, 0x8989890089898900, 0x6262620062626200, 0x9797970097979700, 0x5454540054545400, 0x5B5B5B005B5B5B00, 0x1E1E1E001E1E1E00, 0x9595950095959500, 0xE0E0E000E0E0E000, 0xFFFFFF00FFFFFF00, 0x6464640064646400, 0xD2D2D200D2D2D200, 0x1010100010101000, 0xC4C4C400C4C4C400, 0x0000000000000000, 0x4848480048484800, 0xA3A3A300A3A3A300, 0xF7F7F700F7F7F700, 0x7575750075757500, 0xDBDBDB00DBDBDB00, 0x8A8A8A008A8A8A00, 0x0303030003030300, 0xE6E6E600E6E6E600, 0xDADADA00DADADA00, 0x0909090009090900, 0x3F3F3F003F3F3F00, 0xDDDDDD00DDDDDD00, 0x9494940094949400, 0x8787870087878700, 0x5C5C5C005C5C5C00, 0x8383830083838300, 0x0202020002020200, 0xCDCDCD00CDCDCD00, 0x4A4A4A004A4A4A00, 0x9090900090909000, 0x3333330033333300, 0x7373730073737300, 0x6767670067676700, 0xF6F6F600F6F6F600, 0xF3F3F300F3F3F300, 0x9D9D9D009D9D9D00, 0x7F7F7F007F7F7F00, 0xBFBFBF00BFBFBF00, 0xE2E2E200E2E2E200, 0x5252520052525200, 0x9B9B9B009B9B9B00, 0xD8D8D800D8D8D800, 0x2626260026262600, 0xC8C8C800C8C8C800, 0x3737370037373700, 0xC6C6C600C6C6C600, 0x3B3B3B003B3B3B00, 0x8181810081818100, 0x9696960096969600, 0x6F6F6F006F6F6F00, 0x4B4B4B004B4B4B00, 0x1313130013131300, 0xBEBEBE00BEBEBE00, 0x6363630063636300, 0x2E2E2E002E2E2E00, 0xE9E9E900E9E9E900, 0x7979790079797900, 0xA7A7A700A7A7A700, 0x8C8C8C008C8C8C00, 0x9F9F9F009F9F9F00, 0x6E6E6E006E6E6E00, 0xBCBCBC00BCBCBC00, 0x8E8E8E008E8E8E00, 0x2929290029292900, 0xF5F5F500F5F5F500, 0xF9F9F900F9F9F900, 0xB6B6B600B6B6B600, 0x2F2F2F002F2F2F00, 0xFDFDFD00FDFDFD00, 0xB4B4B400B4B4B400, 0x5959590059595900, 0x7878780078787800, 0x9898980098989800, 0x0606060006060600, 0x6A6A6A006A6A6A00, 0xE7E7E700E7E7E700, 0x4646460046464600, 0x7171710071717100, 0xBABABA00BABABA00, 0xD4D4D400D4D4D400, 0x2525250025252500, 0xABABAB00ABABAB00, 0x4242420042424200, 0x8888880088888800, 0xA2A2A200A2A2A200, 0x8D8D8D008D8D8D00, 0xFAFAFA00FAFAFA00, 0x7272720072727200, 0x0707070007070700, 0xB9B9B900B9B9B900, 0x5555550055555500, 0xF8F8F800F8F8F800, 0xEEEEEE00EEEEEE00, 0xACACAC00ACACAC00, 0x0A0A0A000A0A0A00, 0x3636360036363600, 0x4949490049494900, 0x2A2A2A002A2A2A00, 0x6868680068686800, 0x3C3C3C003C3C3C00, 0x3838380038383800, 0xF1F1F100F1F1F100, 0xA4A4A400A4A4A400, 0x4040400040404000, 0x2828280028282800, 0xD3D3D300D3D3D300, 0x7B7B7B007B7B7B00, 0xBBBBBB00BBBBBB00, 0xC9C9C900C9C9C900, 0x4343430043434300, 0xC1C1C100C1C1C100, 0x1515150015151500, 0xE3E3E300E3E3E300, 0xADADAD00ADADAD00, 0xF4F4F400F4F4F400, 0x7777770077777700, 0xC7C7C700C7C7C700, 0x8080800080808000, 0x9E9E9E009E9E9E00 }; } namespace Botan { extern "C" { /* * Helper Macros for x86-64 Assembly */ #ifndef ASM #define ASM(x) x "\n\t" #endif #define ADDSUB2_OP(OPERATION, INDEX) \ ASM("movq 8*" #INDEX "(%[y]), %[carry]") \ ASM(OPERATION " %[carry], 8*" #INDEX "(%[x])") \ #define ADDSUB3_OP(OPERATION, INDEX) \ ASM("movq 8*" #INDEX "(%[x]), %[carry]") \ ASM(OPERATION " 8*" #INDEX "(%[y]), %[carry]") \ ASM("movq %[carry], 8*" #INDEX "(%[z])") \ #define LINMUL_OP(WRITE_TO, INDEX) \ ASM("movq 8*" #INDEX "(%[x]),%%rax") \ ASM("mulq %[y]") \ ASM("addq %[carry],%%rax") \ ASM("adcq $0,%%rdx") \ ASM("movq %%rdx,%[carry]") \ ASM("movq %%rax, 8*" #INDEX "(%[" WRITE_TO "])") #define MULADD_OP(IGNORED, INDEX) \ ASM("movq 8*" #INDEX "(%[x]),%%rax") \ ASM("mulq %[y]") \ ASM("addq %[carry],%%rax") \ ASM("adcq $0,%%rdx") \ ASM("addq 8*" #INDEX "(%[z]),%%rax") \ ASM("adcq $0,%%rdx") \ ASM("movq %%rdx,%[carry]") \ ASM("movq %%rax, 8*" #INDEX " (%[z])") #define DO_8_TIMES(MACRO, ARG) \ MACRO(ARG, 0) \ MACRO(ARG, 1) \ MACRO(ARG, 2) \ MACRO(ARG, 3) \ MACRO(ARG, 4) \ MACRO(ARG, 5) \ MACRO(ARG, 6) \ MACRO(ARG, 7) #define ADD_OR_SUBTRACT(CORE_CODE) \ ASM("rorq %[carry]") \ CORE_CODE \ ASM("sbbq %[carry],%[carry]") \ ASM("negq %[carry]") /* * Word Addition */ inline word word_add(word x, word y, word* carry) { asm( ADD_OR_SUBTRACT(ASM("adcq %[y],%[x]")) : [x]"=r"(x), [carry]"=r"(*carry) : "0"(x), [y]"rm"(y), "1"(*carry) : "cc"); return x; } /* * Eight Word Block Addition, Two Argument */ inline word word8_add2(word x[8], const word y[8], word carry) { asm( ADD_OR_SUBTRACT(DO_8_TIMES(ADDSUB2_OP, "adcq")) : [carry]"=r"(carry) : [x]"r"(x), [y]"r"(y), "0"(carry) : "cc", "memory"); return carry; } /* * Eight Word Block Addition, Three Argument */ inline word word8_add3(word z[8], const word x[8], const word y[8], word carry) { asm( ADD_OR_SUBTRACT(DO_8_TIMES(ADDSUB3_OP, "adcq")) : [carry]"=r"(carry) : [x]"r"(x), [y]"r"(y), [z]"r"(z), "0"(carry) : "cc", "memory"); return carry; } /* * Word Subtraction */ inline word word_sub(word x, word y, word* carry) { asm( ADD_OR_SUBTRACT(ASM("sbbq %[y],%[x]")) : [x]"=r"(x), [carry]"=r"(*carry) : "0"(x), [y]"rm"(y), "1"(*carry) : "cc"); return x; } /* * Eight Word Block Subtraction, Two Argument */ inline word word8_sub2(word x[8], const word y[8], word carry) { asm( ADD_OR_SUBTRACT(DO_8_TIMES(ADDSUB2_OP, "sbbq")) : [carry]"=r"(carry) : [x]"r"(x), [y]"r"(y), "0"(carry) : "cc", "memory"); return carry; } /* * Eight Word Block Subtraction, Two Argument */ inline word word8_sub2_rev(word x[8], const word y[8], word carry) { asm( ADD_OR_SUBTRACT(DO_8_TIMES(ADDSUB3_OP, "sbbq")) : [carry]"=r"(carry) : [x]"r"(y), [y]"r"(x), [z]"r"(x), "0"(carry) : "cc", "memory"); return carry; } /* * Eight Word Block Subtraction, Three Argument */ inline word word8_sub3(word z[8], const word x[8], const word y[8], word carry) { asm( ADD_OR_SUBTRACT(DO_8_TIMES(ADDSUB3_OP, "sbbq")) : [carry]"=r"(carry) : [x]"r"(x), [y]"r"(y), [z]"r"(z), "0"(carry) : "cc", "memory"); return carry; } /* * Eight Word Block Linear Multiplication */ inline word word8_linmul2(word x[8], word y, word carry) { asm( DO_8_TIMES(LINMUL_OP, "x") : [carry]"=r"(carry) : [x]"r"(x), [y]"rm"(y), "0"(carry) : "cc", "%rax", "%rdx"); return carry; } /* * Eight Word Block Linear Multiplication */ inline word word8_linmul3(word z[8], const word x[8], word y, word carry) { asm( DO_8_TIMES(LINMUL_OP, "z") : [carry]"=r"(carry) : [z]"r"(z), [x]"r"(x), [y]"rm"(y), "0"(carry) : "cc", "%rax", "%rdx"); return carry; } /* * Eight Word Block Multiply/Add */ inline word word8_madd3(word z[8], const word x[8], word y, word carry) { asm( DO_8_TIMES(MULADD_OP, "") : [carry]"=r"(carry) : [z]"r"(z), [x]"r"(x), [y]"rm"(y), "0"(carry) : "cc", "%rax", "%rdx"); return carry; } /* * Multiply-Add Accumulator */ inline void word3_muladd(word* w2, word* w1, word* w0, word x, word y) { asm( ASM("mulq %[y]") ASM("addq %[x],%[w0]") ASM("adcq %[y],%[w1]") ASM("adcq $0,%[w2]") : [w0]"=r"(*w0), [w1]"=r"(*w1), [w2]"=r"(*w2) : [x]"a"(x), [y]"d"(y), "0"(*w0), "1"(*w1), "2"(*w2) : "cc"); } /* * Multiply-Add Accumulator */ inline void word3_muladd_2(word* w2, word* w1, word* w0, word x, word y) { asm( ASM("mulq %[y]") ASM("addq %[x],%[w0]") ASM("adcq %[y],%[w1]") ASM("adcq $0,%[w2]") ASM("addq %[x],%[w0]") ASM("adcq %[y],%[w1]") ASM("adcq $0,%[w2]") : [w0]"=r"(*w0), [w1]"=r"(*w1), [w2]"=r"(*w2) : [x]"a"(x), [y]"d"(y), "0"(*w0), "1"(*w1), "2"(*w2) : "cc"); } #undef ASM #undef DO_8_TIMES #undef ADD_OR_SUBTRACT #undef ADDSUB2_OP #undef ADDSUB3_OP #undef LINMUL_OP #undef MULADD_OP } } /* * OctetString * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> namespace Botan { /* * Create an OctetString from RNG output */ OctetString::OctetString(RandomNumberGenerator& rng, size_t length) { bits = rng.random_vec(length); } /* * Create an OctetString from a hex string */ void OctetString::change(const std::string& hex_string) { bits.resize(1 + hex_string.length() / 2); bits.resize(hex_decode(&bits[0], hex_string)); } /* * Create an OctetString from a byte string */ void OctetString::change(const byte in[], size_t n) { bits.resize(n); bits.copy(in, n); } /* * Set the parity of each key byte to odd */ void OctetString::set_odd_parity() { const byte ODD_PARITY[256] = { 0x01, 0x01, 0x02, 0x02, 0x04, 0x04, 0x07, 0x07, 0x08, 0x08, 0x0B, 0x0B, 0x0D, 0x0D, 0x0E, 0x0E, 0x10, 0x10, 0x13, 0x13, 0x15, 0x15, 0x16, 0x16, 0x19, 0x19, 0x1A, 0x1A, 0x1C, 0x1C, 0x1F, 0x1F, 0x20, 0x20, 0x23, 0x23, 0x25, 0x25, 0x26, 0x26, 0x29, 0x29, 0x2A, 0x2A, 0x2C, 0x2C, 0x2F, 0x2F, 0x31, 0x31, 0x32, 0x32, 0x34, 0x34, 0x37, 0x37, 0x38, 0x38, 0x3B, 0x3B, 0x3D, 0x3D, 0x3E, 0x3E, 0x40, 0x40, 0x43, 0x43, 0x45, 0x45, 0x46, 0x46, 0x49, 0x49, 0x4A, 0x4A, 0x4C, 0x4C, 0x4F, 0x4F, 0x51, 0x51, 0x52, 0x52, 0x54, 0x54, 0x57, 0x57, 0x58, 0x58, 0x5B, 0x5B, 0x5D, 0x5D, 0x5E, 0x5E, 0x61, 0x61, 0x62, 0x62, 0x64, 0x64, 0x67, 0x67, 0x68, 0x68, 0x6B, 0x6B, 0x6D, 0x6D, 0x6E, 0x6E, 0x70, 0x70, 0x73, 0x73, 0x75, 0x75, 0x76, 0x76, 0x79, 0x79, 0x7A, 0x7A, 0x7C, 0x7C, 0x7F, 0x7F, 0x80, 0x80, 0x83, 0x83, 0x85, 0x85, 0x86, 0x86, 0x89, 0x89, 0x8A, 0x8A, 0x8C, 0x8C, 0x8F, 0x8F, 0x91, 0x91, 0x92, 0x92, 0x94, 0x94, 0x97, 0x97, 0x98, 0x98, 0x9B, 0x9B, 0x9D, 0x9D, 0x9E, 0x9E, 0xA1, 0xA1, 0xA2, 0xA2, 0xA4, 0xA4, 0xA7, 0xA7, 0xA8, 0xA8, 0xAB, 0xAB, 0xAD, 0xAD, 0xAE, 0xAE, 0xB0, 0xB0, 0xB3, 0xB3, 0xB5, 0xB5, 0xB6, 0xB6, 0xB9, 0xB9, 0xBA, 0xBA, 0xBC, 0xBC, 0xBF, 0xBF, 0xC1, 0xC1, 0xC2, 0xC2, 0xC4, 0xC4, 0xC7, 0xC7, 0xC8, 0xC8, 0xCB, 0xCB, 0xCD, 0xCD, 0xCE, 0xCE, 0xD0, 0xD0, 0xD3, 0xD3, 0xD5, 0xD5, 0xD6, 0xD6, 0xD9, 0xD9, 0xDA, 0xDA, 0xDC, 0xDC, 0xDF, 0xDF, 0xE0, 0xE0, 0xE3, 0xE3, 0xE5, 0xE5, 0xE6, 0xE6, 0xE9, 0xE9, 0xEA, 0xEA, 0xEC, 0xEC, 0xEF, 0xEF, 0xF1, 0xF1, 0xF2, 0xF2, 0xF4, 0xF4, 0xF7, 0xF7, 0xF8, 0xF8, 0xFB, 0xFB, 0xFD, 0xFD, 0xFE, 0xFE }; for(size_t j = 0; j != bits.size(); ++j) bits[j] = ODD_PARITY[bits[j]]; } /* * Hex encode an OctetString */ std::string OctetString::as_string() const { return hex_encode(&bits[0], bits.size()); } /* * XOR Operation for OctetStrings */ OctetString& OctetString::operator^=(const OctetString& k) { if(&k == this) { zeroise(bits); return (*this); } xor_buf(&bits[0], k.begin(), std::min(length(), k.length())); return (*this); } /* * Equality Operation for OctetStrings */ bool operator==(const OctetString& s1, const OctetString& s2) { return (s1.bits_of() == s2.bits_of()); } /* * Unequality Operation for OctetStrings */ bool operator!=(const OctetString& s1, const OctetString& s2) { return !(s1 == s2); } /* * Append Operation for OctetStrings */ OctetString operator+(const OctetString& k1, const OctetString& k2) { SecureVector<byte> out; out += k1.bits_of(); out += k2.bits_of(); return OctetString(out); } /* * XOR Operation for OctetStrings */ OctetString operator^(const OctetString& k1, const OctetString& k2) { SecureVector<byte> ret(std::max(k1.length(), k2.length())); ret.copy(k1.begin(), k1.length()); xor_buf(ret, k2.begin(), k2.length()); return OctetString(ret); } } /* * Algorithm Factory * (C) 2008-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> namespace Botan { namespace { /* * Template functions for the factory prototype/search algorithm */ template<typename T> T* engine_get_algo(Engine*, const SCAN_Name&, Algorithm_Factory&) { return 0; } template<> BlockCipher* engine_get_algo(Engine* engine, const SCAN_Name& request, Algorithm_Factory& af) { return engine->find_block_cipher(request, af); } template<> StreamCipher* engine_get_algo(Engine* engine, const SCAN_Name& request, Algorithm_Factory& af) { return engine->find_stream_cipher(request, af); } template<> HashFunction* engine_get_algo(Engine* engine, const SCAN_Name& request, Algorithm_Factory& af) { return engine->find_hash(request, af); } template<> MessageAuthenticationCode* engine_get_algo(Engine* engine, const SCAN_Name& request, Algorithm_Factory& af) { return engine->find_mac(request, af); } template<> PBKDF* engine_get_algo(Engine* engine, const SCAN_Name& request, Algorithm_Factory& af) { return engine->find_pbkdf(request, af); } template<typename T> const T* factory_prototype(const std::string& algo_spec, const std::string& provider, const std::vector<Engine*>& engines, Algorithm_Factory& af, Algorithm_Cache<T>* cache) { if(const T* cache_hit = cache->get(algo_spec, provider)) return cache_hit; SCAN_Name scan_name(algo_spec); if(scan_name.cipher_mode() != "") return 0; for(size_t i = 0; i != engines.size(); ++i) { if(provider == "" || engines[i]->provider_name() == provider) { if(T* impl = engine_get_algo<T>(engines[i], scan_name, af)) cache->add(impl, algo_spec, engines[i]->provider_name()); } } return cache->get(algo_spec, provider); } } /* * Setup caches */ Algorithm_Factory::Algorithm_Factory(Mutex_Factory& mf) { block_cipher_cache = new Algorithm_Cache<BlockCipher>(mf.make()); stream_cipher_cache = new Algorithm_Cache<StreamCipher>(mf.make()); hash_cache = new Algorithm_Cache<HashFunction>(mf.make()); mac_cache = new Algorithm_Cache<MessageAuthenticationCode>(mf.make()); pbkdf_cache = new Algorithm_Cache<PBKDF>(mf.make()); } /* * Delete all engines */ Algorithm_Factory::~Algorithm_Factory() { delete block_cipher_cache; delete stream_cipher_cache; delete hash_cache; delete mac_cache; delete pbkdf_cache; std::for_each(engines.begin(), engines.end(), del_fun<Engine>()); } void Algorithm_Factory::clear_caches() { block_cipher_cache->clear_cache(); stream_cipher_cache->clear_cache(); hash_cache->clear_cache(); mac_cache->clear_cache(); pbkdf_cache->clear_cache(); } void Algorithm_Factory::add_engine(Engine* engine) { clear_caches(); engines.push_back(engine); } /* * Set the preferred provider for an algorithm */ void Algorithm_Factory::set_preferred_provider(const std::string& algo_spec, const std::string& provider) { if(prototype_block_cipher(algo_spec)) block_cipher_cache->set_preferred_provider(algo_spec, provider); else if(prototype_stream_cipher(algo_spec)) stream_cipher_cache->set_preferred_provider(algo_spec, provider); else if(prototype_hash_function(algo_spec)) hash_cache->set_preferred_provider(algo_spec, provider); else if(prototype_mac(algo_spec)) mac_cache->set_preferred_provider(algo_spec, provider); else if(prototype_pbkdf(algo_spec)) pbkdf_cache->set_preferred_provider(algo_spec, provider); } /* * Get an engine out of the list */ Engine* Algorithm_Factory::get_engine_n(size_t n) const { if(n >= engines.size()) return 0; return engines[n]; } /* * Return the possible providers of a request * Note: assumes you don't have different types by the same name */ std::vector<std::string> Algorithm_Factory::providers_of(const std::string& algo_spec) { /* The checks with if(prototype_X(algo_spec)) have the effect of forcing a full search, since otherwise there might not be any providers at all in the cache. */ if(prototype_block_cipher(algo_spec)) return block_cipher_cache->providers_of(algo_spec); else if(prototype_stream_cipher(algo_spec)) return stream_cipher_cache->providers_of(algo_spec); else if(prototype_hash_function(algo_spec)) return hash_cache->providers_of(algo_spec); else if(prototype_mac(algo_spec)) return mac_cache->providers_of(algo_spec); else if(prototype_pbkdf(algo_spec)) return pbkdf_cache->providers_of(algo_spec); else return std::vector<std::string>(); } /* * Return the prototypical block cipher corresponding to this request */ const BlockCipher* Algorithm_Factory::prototype_block_cipher(const std::string& algo_spec, const std::string& provider) { return factory_prototype<BlockCipher>(algo_spec, provider, engines, *this, block_cipher_cache); } /* * Return the prototypical stream cipher corresponding to this request */ const StreamCipher* Algorithm_Factory::prototype_stream_cipher(const std::string& algo_spec, const std::string& provider) { return factory_prototype<StreamCipher>(algo_spec, provider, engines, *this, stream_cipher_cache); } /* * Return the prototypical object corresponding to this request (if found) */ const HashFunction* Algorithm_Factory::prototype_hash_function(const std::string& algo_spec, const std::string& provider) { return factory_prototype<HashFunction>(algo_spec, provider, engines, *this, hash_cache); } /* * Return the prototypical object corresponding to this request */ const MessageAuthenticationCode* Algorithm_Factory::prototype_mac(const std::string& algo_spec, const std::string& provider) { return factory_prototype<MessageAuthenticationCode>(algo_spec, provider, engines, *this, mac_cache); } /* * Return the prototypical object corresponding to this request */ const PBKDF* Algorithm_Factory::prototype_pbkdf(const std::string& algo_spec, const std::string& provider) { return factory_prototype<PBKDF>(algo_spec, provider, engines, *this, pbkdf_cache); } /* * Return a new block cipher corresponding to this request */ BlockCipher* Algorithm_Factory::make_block_cipher(const std::string& algo_spec, const std::string& provider) { if(const BlockCipher* proto = prototype_block_cipher(algo_spec, provider)) return proto->clone(); throw Algorithm_Not_Found(algo_spec); } /* * Return a new stream cipher corresponding to this request */ StreamCipher* Algorithm_Factory::make_stream_cipher(const std::string& algo_spec, const std::string& provider) { if(const StreamCipher* proto = prototype_stream_cipher(algo_spec, provider)) return proto->clone(); throw Algorithm_Not_Found(algo_spec); } /* * Return a new object corresponding to this request */ HashFunction* Algorithm_Factory::make_hash_function(const std::string& algo_spec, const std::string& provider) { if(const HashFunction* proto = prototype_hash_function(algo_spec, provider)) return proto->clone(); throw Algorithm_Not_Found(algo_spec); } /* * Return a new object corresponding to this request */ MessageAuthenticationCode* Algorithm_Factory::make_mac(const std::string& algo_spec, const std::string& provider) { if(const MessageAuthenticationCode* proto = prototype_mac(algo_spec, provider)) return proto->clone(); throw Algorithm_Not_Found(algo_spec); } /* * Return a new object corresponding to this request */ PBKDF* Algorithm_Factory::make_pbkdf(const std::string& algo_spec, const std::string& provider) { if(const PBKDF* proto = prototype_pbkdf(algo_spec, provider)) return proto->clone(); throw Algorithm_Not_Found(algo_spec); } /* * Add a new block cipher */ void Algorithm_Factory::add_block_cipher(BlockCipher* block_cipher, const std::string& provider) { block_cipher_cache->add(block_cipher, block_cipher->name(), provider); } /* * Add a new stream cipher */ void Algorithm_Factory::add_stream_cipher(StreamCipher* stream_cipher, const std::string& provider) { stream_cipher_cache->add(stream_cipher, stream_cipher->name(), provider); } /* * Add a new hash */ void Algorithm_Factory::add_hash_function(HashFunction* hash, const std::string& provider) { hash_cache->add(hash, hash->name(), provider); } /* * Add a new mac */ void Algorithm_Factory::add_mac(MessageAuthenticationCode* mac, const std::string& provider) { mac_cache->add(mac, mac->name(), provider); } /* * Add a new PBKDF */ void Algorithm_Factory::add_pbkdf(PBKDF* pbkdf, const std::string& provider) { pbkdf_cache->add(pbkdf, pbkdf->name(), provider); } } /* * Default provider weights for Algorithm_Cache * (C) 2008 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /** * Return a static provider weighing */ size_t static_provider_weight(const std::string& prov_name) { /* * Prefer asm over C++, but prefer anything over OpenSSL or GNU MP; to use * them, set the provider explicitly for the algorithms you want */ if(prov_name == "aes_isa") return 9; if(prov_name == "simd") return 8; if(prov_name == "asm") return 7; if(prov_name == "core") return 5; if(prov_name == "openssl") return 2; if(prov_name == "gmp") return 1; return 0; // other/unknown } } /* * Memory Mapping Allocator * (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <vector> #include <cstring> #include <sys/types.h> #include <sys/mman.h> #include <sys/stat.h> #include <unistd.h> #include <stdlib.h> #include <fcntl.h> #include <errno.h> #ifndef MAP_FAILED #define MAP_FAILED -1 #endif namespace Botan { namespace { /* * MemoryMapping_Allocator Exception */ class BOTAN_DLL MemoryMapping_Failed : public Exception { public: MemoryMapping_Failed(const std::string& msg) : Exception("MemoryMapping_Allocator: " + msg) {} }; } /* * Memory Map a File into Memory */ void* MemoryMapping_Allocator::alloc_block(size_t n) { class TemporaryFile { public: int get_fd() const { return fd; } TemporaryFile(const std::string& base) { const std::string mkstemp_template = base + "XXXXXX"; std::vector<char> filepath(mkstemp_template.begin(), mkstemp_template.end()); filepath.push_back(0); // add terminating NULL mode_t old_umask = ::umask(077); fd = ::mkstemp(&filepath[0]); ::umask(old_umask); if(fd == -1) throw MemoryMapping_Failed("Temporary file allocation failed"); if(::unlink(&filepath[0]) != 0) throw MemoryMapping_Failed("Could not unlink temporary file"); } ~TemporaryFile() { /* * We can safely close here, because post-mmap the file * will continue to exist until the mmap is unmapped from * our address space upon deallocation (or process exit). */ if(fd != -1 && ::close(fd) == -1) throw MemoryMapping_Failed("Could not close file"); } private: int fd; }; TemporaryFile file("/tmp/botan_"); if(file.get_fd() == -1) throw MemoryMapping_Failed("Could not create file"); std::vector<byte> zeros(4096); size_t remaining = n; while(remaining) { const size_t write_try = std::min(zeros.size(), remaining); ssize_t wrote_got = ::write(file.get_fd(), &zeros[0], write_try); if(wrote_got == -1 && errno != EINTR) throw MemoryMapping_Failed("Could not write to file"); remaining -= wrote_got; } #ifndef MAP_NOSYNC #define MAP_NOSYNC 0 #endif void* ptr = ::mmap(0, n, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_NOSYNC, file.get_fd(), 0); if(ptr == static_cast<void*>(MAP_FAILED)) throw MemoryMapping_Failed("Could not map file"); return ptr; } /* * Remove a Memory Mapping */ void MemoryMapping_Allocator::dealloc_block(void* ptr, size_t n) { if(ptr == 0) return; const byte PATTERNS[] = { 0x00, 0xF5, 0x5A, 0xAF, 0x00 }; // The char* casts are for Solaris, args are void* on most other systems for(size_t i = 0; i != sizeof(PATTERNS); ++i) { std::memset(ptr, PATTERNS[i], n); if(::msync(static_cast<char*>(ptr), n, MS_SYNC)) throw MemoryMapping_Failed("Sync operation failed"); } if(::munmap(static_cast<char*>(ptr), n)) throw MemoryMapping_Failed("Could not unmap file"); } } /* * Pooling Allocator * (C) 1999-2008 Jack Lloyd * 2005 Matthew Gregan * 2005-2006 Matt Johnston * * Distributed under the terms of the Botan license */ #include <algorithm> #include <exception> namespace Botan { /* * Memory_Block Constructor */ Pooling_Allocator::Memory_Block::Memory_Block(void* buf) { buffer = static_cast<byte*>(buf); bitmap = 0; buffer_end = buffer + (BLOCK_SIZE * BITMAP_SIZE); } /* * See if ptr is contained by this block */ bool Pooling_Allocator::Memory_Block::contains(void* ptr, size_t length) const { return ((buffer <= ptr) && (buffer_end >= static_cast<byte*>(ptr) + length * BLOCK_SIZE)); } /* * Allocate some memory, if possible */ byte* Pooling_Allocator::Memory_Block::alloc(size_t n) { if(n == 0 || n > BITMAP_SIZE) return 0; if(n == BITMAP_SIZE) { if(bitmap) return 0; else { bitmap = ~bitmap; return buffer; } } bitmap_type mask = (static_cast<bitmap_type>(1) << n) - 1; size_t offset = 0; while(bitmap & mask) { mask <<= 1; ++offset; if((bitmap & mask) == 0) break; if(mask >> 63) break; } if(bitmap & mask) return 0; bitmap |= mask; return buffer + offset * BLOCK_SIZE; } /* * Mark this memory as free, if we own it */ void Pooling_Allocator::Memory_Block::free(void* ptr, size_t blocks) { clear_mem(static_cast<byte*>(ptr), blocks * BLOCK_SIZE); const size_t offset = (static_cast<byte*>(ptr) - buffer) / BLOCK_SIZE; if(offset == 0 && blocks == BITMAP_SIZE) bitmap = ~bitmap; else { for(size_t j = 0; j != blocks; ++j) bitmap &= ~(static_cast<bitmap_type>(1) << (j+offset)); } } /* * Pooling_Allocator Constructor */ Pooling_Allocator::Pooling_Allocator(Mutex* m) : mutex(m) { last_used = blocks.begin(); } /* * Pooling_Allocator Destructor */ Pooling_Allocator::~Pooling_Allocator() { delete mutex; if(blocks.size()) throw Invalid_State("Pooling_Allocator: Never released memory"); } /* * Free all remaining memory */ void Pooling_Allocator::destroy() { Mutex_Holder lock(mutex); blocks.clear(); for(size_t j = 0; j != allocated.size(); ++j) dealloc_block(allocated[j].first, allocated[j].second); allocated.clear(); } /* * Allocation */ void* Pooling_Allocator::allocate(size_t n) { const size_t BITMAP_SIZE = Memory_Block::bitmap_size(); const size_t BLOCK_SIZE = Memory_Block::block_size(); Mutex_Holder lock(mutex); if(n <= BITMAP_SIZE * BLOCK_SIZE) { const size_t block_no = round_up(n, BLOCK_SIZE) / BLOCK_SIZE; byte* mem = allocate_blocks(block_no); if(mem) return mem; get_more_core(BOTAN_MEM_POOL_CHUNK_SIZE); mem = allocate_blocks(block_no); if(mem) return mem; throw Memory_Exhaustion(); } void* new_buf = alloc_block(n); if(new_buf) return new_buf; throw Memory_Exhaustion(); } /* * Deallocation */ void Pooling_Allocator::deallocate(void* ptr, size_t n) { const size_t BITMAP_SIZE = Memory_Block::bitmap_size(); const size_t BLOCK_SIZE = Memory_Block::block_size(); if(ptr == 0 && n == 0) return; Mutex_Holder lock(mutex); if(n > BITMAP_SIZE * BLOCK_SIZE) dealloc_block(ptr, n); else { const size_t block_no = round_up(n, BLOCK_SIZE) / BLOCK_SIZE; std::vector<Memory_Block>::iterator i = std::lower_bound(blocks.begin(), blocks.end(), Memory_Block(ptr)); if(i == blocks.end() || !i->contains(ptr, block_no)) throw Invalid_State("Pointer released to the wrong allocator"); i->free(ptr, block_no); } } /* * Try to get some memory from an existing block */ byte* Pooling_Allocator::allocate_blocks(size_t n) { if(blocks.empty()) return 0; std::vector<Memory_Block>::iterator i = last_used; do { byte* mem = i->alloc(n); if(mem) { last_used = i; return mem; } ++i; if(i == blocks.end()) i = blocks.begin(); } while(i != last_used); return 0; } /* * Allocate more memory for the pool */ void Pooling_Allocator::get_more_core(size_t in_bytes) { const size_t BITMAP_SIZE = Memory_Block::bitmap_size(); const size_t BLOCK_SIZE = Memory_Block::block_size(); const size_t TOTAL_BLOCK_SIZE = BLOCK_SIZE * BITMAP_SIZE; // upper bound on allocation is 1 MiB in_bytes = std::min<size_t>(in_bytes, 1024 * 1024); const size_t in_blocks = round_up(in_bytes, BLOCK_SIZE) / TOTAL_BLOCK_SIZE; const size_t to_allocate = in_blocks * TOTAL_BLOCK_SIZE; void* ptr = alloc_block(to_allocate); if(ptr == 0) throw Memory_Exhaustion(); allocated.push_back(std::make_pair(ptr, to_allocate)); for(size_t j = 0; j != in_blocks; ++j) { byte* byte_ptr = static_cast<byte*>(ptr); blocks.push_back(Memory_Block(byte_ptr + j * TOTAL_BLOCK_SIZE)); } std::sort(blocks.begin(), blocks.end()); last_used = std::lower_bound(blocks.begin(), blocks.end(), Memory_Block(ptr)); } } /* * Basic Allocators * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <cstdlib> #include <cstring> namespace Botan { namespace { /* * Perform Memory Allocation */ void* do_malloc(size_t n, bool do_lock) { void* ptr = std::malloc(n); if(!ptr) return 0; if(do_lock) lock_mem(ptr, n); std::memset(ptr, 0, n); return ptr; } /* * Perform Memory Deallocation */ void do_free(void* ptr, size_t n, bool do_lock) { if(!ptr) return; std::memset(ptr, 0, n); if(do_lock) unlock_mem(ptr, n); std::free(ptr); } } /* * Malloc_Allocator's Allocation */ void* Malloc_Allocator::allocate(size_t n) { void* ptr = do_malloc(n, false); if(!ptr) throw Memory_Exhaustion(); return ptr; } /* * Malloc_Allocator's Deallocation */ void Malloc_Allocator::deallocate(void* ptr, size_t n) { do_free(ptr, n, false); } /* * Locking_Allocator's Allocation */ void* Locking_Allocator::alloc_block(size_t n) { return do_malloc(n, true); } /* * Locking_Allocator's Deallocation */ void Locking_Allocator::dealloc_block(void* ptr, size_t n) { do_free(ptr, n, true); } /* * Get an allocator */ Allocator* Allocator::get(bool locking) { std::string type = ""; if(!locking) type = "malloc"; Allocator* alloc = global_state().get_allocator(type); if(alloc) return alloc; throw Internal_Error("Couldn't find an allocator to use in get_allocator"); } } /* * Algorithm Identifier * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Create an AlgorithmIdentifier */ AlgorithmIdentifier::AlgorithmIdentifier(const OID& alg_id, const MemoryRegion<byte>& param) { oid = alg_id; parameters = param; } /* * Create an AlgorithmIdentifier */ AlgorithmIdentifier::AlgorithmIdentifier(const std::string& alg_id, const MemoryRegion<byte>& param) { oid = OIDS::lookup(alg_id); parameters = param; } /* * Create an AlgorithmIdentifier */ AlgorithmIdentifier::AlgorithmIdentifier(const OID& alg_id, Encoding_Option option) { const byte DER_NULL[] = { 0x05, 0x00 }; oid = alg_id; if(option == USE_NULL_PARAM) { parameters += std::make_pair<const byte*, size_t>( DER_NULL, sizeof(DER_NULL)); } } /* * Create an AlgorithmIdentifier */ AlgorithmIdentifier::AlgorithmIdentifier(const std::string& alg_id, Encoding_Option option) { const byte DER_NULL[] = { 0x05, 0x00 }; oid = OIDS::lookup(alg_id); if(option == USE_NULL_PARAM) { parameters += std::make_pair<const byte*, size_t>( DER_NULL, sizeof(DER_NULL)); } } /* * Compare two AlgorithmIdentifiers */ bool operator==(const AlgorithmIdentifier& a1, const AlgorithmIdentifier& a2) { if(a1.oid != a2.oid) return false; if(a1.parameters != a2.parameters) return false; return true; } /* * Compare two AlgorithmIdentifiers */ bool operator!=(const AlgorithmIdentifier& a1, const AlgorithmIdentifier& a2) { return !(a1 == a2); } /* * DER encode an AlgorithmIdentifier */ void AlgorithmIdentifier::encode_into(DER_Encoder& codec) const { codec.start_cons(SEQUENCE) .encode(oid) .raw_bytes(parameters) .end_cons(); } /* * Decode a BER encoded AlgorithmIdentifier */ void AlgorithmIdentifier::decode_from(BER_Decoder& codec) { codec.start_cons(SEQUENCE) .decode(oid) .raw_bytes(parameters) .end_cons(); } } /* * AlternativeName * (C) 1999-2007 Jack Lloyd * 2007 Yves Jerschow * * Distributed under the terms of the Botan license */ namespace Botan { namespace { /* * Check if type is a known ASN.1 string type */ bool is_string_type(ASN1_Tag tag) { return (tag == NUMERIC_STRING || tag == PRINTABLE_STRING || tag == VISIBLE_STRING || tag == T61_STRING || tag == IA5_STRING || tag == UTF8_STRING || tag == BMP_STRING); } } /* * Create an AlternativeName */ AlternativeName::AlternativeName(const std::string& email_addr, const std::string& uri, const std::string& dns, const std::string& ip) { add_attribute("RFC822", email_addr); add_attribute("DNS", dns); add_attribute("URI", uri); add_attribute("IP", ip); } /* * Add an attribute to an alternative name */ void AlternativeName::add_attribute(const std::string& type, const std::string& str) { if(type == "" || str == "") return; typedef std::multimap<std::string, std::string>::iterator iter; std::pair<iter, iter> range = alt_info.equal_range(type); for(iter j = range.first; j != range.second; ++j) if(j->second == str) return; multimap_insert(alt_info, type, str); } /* * Add an OtherName field */ void AlternativeName::add_othername(const OID& oid, const std::string& value, ASN1_Tag type) { if(value == "") return; multimap_insert(othernames, oid, ASN1_String(value, type)); } /* * Get the attributes of this alternative name */ std::multimap<std::string, std::string> AlternativeName::get_attributes() const { return alt_info; } /* * Get the otherNames */ std::multimap<OID, ASN1_String> AlternativeName::get_othernames() const { return othernames; } /* * Return all of the alternative names */ std::multimap<std::string, std::string> AlternativeName::contents() const { std::multimap<std::string, std::string> names; typedef std::multimap<std::string, std::string>::const_iterator rdn_iter; for(rdn_iter j = alt_info.begin(); j != alt_info.end(); ++j) multimap_insert(names, j->first, j->second); typedef std::multimap<OID, ASN1_String>::const_iterator on_iter; for(on_iter j = othernames.begin(); j != othernames.end(); ++j) multimap_insert(names, OIDS::lookup(j->first), j->second.value()); return names; } /* * Return if this object has anything useful */ bool AlternativeName::has_items() const { return (alt_info.size() > 0 || othernames.size() > 0); } namespace { /* * DER encode an AlternativeName entry */ void encode_entries(DER_Encoder& encoder, const std::multimap<std::string, std::string>& attr, const std::string& type, ASN1_Tag tagging) { typedef std::multimap<std::string, std::string>::const_iterator iter; std::pair<iter, iter> range = attr.equal_range(type); for(iter j = range.first; j != range.second; ++j) { if(type == "RFC822" || type == "DNS" || type == "URI") { ASN1_String asn1_string(j->second, IA5_STRING); encoder.add_object(tagging, CONTEXT_SPECIFIC, asn1_string.iso_8859()); } else if(type == "IP") { const u32bit ip = string_to_ipv4(j->second); byte ip_buf[4] = { 0 }; store_be(ip, ip_buf); encoder.add_object(tagging, CONTEXT_SPECIFIC, ip_buf, 4); } } } } /* * DER encode an AlternativeName extension */ void AlternativeName::encode_into(DER_Encoder& der) const { der.start_cons(SEQUENCE); encode_entries(der, alt_info, "RFC822", ASN1_Tag(1)); encode_entries(der, alt_info, "DNS", ASN1_Tag(2)); encode_entries(der, alt_info, "URI", ASN1_Tag(6)); encode_entries(der, alt_info, "IP", ASN1_Tag(7)); std::multimap<OID, ASN1_String>::const_iterator i; for(i = othernames.begin(); i != othernames.end(); ++i) { der.start_explicit(0) .encode(i->first) .start_explicit(0) .encode(i->second) .end_explicit() .end_explicit(); } der.end_cons(); } /* * Decode a BER encoded AlternativeName */ void AlternativeName::decode_from(BER_Decoder& source) { BER_Decoder names = source.start_cons(SEQUENCE); while(names.more_items()) { BER_Object obj = names.get_next_object(); if((obj.class_tag != CONTEXT_SPECIFIC) && (obj.class_tag != (CONTEXT_SPECIFIC | CONSTRUCTED))) continue; const ASN1_Tag tag = obj.type_tag; if(tag == 0) { BER_Decoder othername(obj.value); OID oid; othername.decode(oid); if(othername.more_items()) { BER_Object othername_value_outer = othername.get_next_object(); othername.verify_end(); if(othername_value_outer.type_tag != ASN1_Tag(0) || othername_value_outer.class_tag != (CONTEXT_SPECIFIC | CONSTRUCTED) ) throw Decoding_Error("Invalid tags on otherName value"); BER_Decoder othername_value_inner(othername_value_outer.value); BER_Object value = othername_value_inner.get_next_object(); othername_value_inner.verify_end(); const ASN1_Tag value_type = value.type_tag; if(is_string_type(value_type) && value.class_tag == UNIVERSAL) add_othername(oid, ASN1::to_string(value), value_type); } } else if(tag == 1 || tag == 2 || tag == 6) { const std::string value = Charset::transcode(ASN1::to_string(obj), LATIN1_CHARSET, LOCAL_CHARSET); if(tag == 1) add_attribute("RFC822", value); if(tag == 2) add_attribute("DNS", value); if(tag == 6) add_attribute("URI", value); } else if(tag == 7) { if(obj.value.size() == 4) { const u32bit ip = load_be<u32bit>(&obj.value[0], 0); add_attribute("IP", ipv4_to_string(ip)); } } } } } /* * Attribute * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Create an Attribute */ Attribute::Attribute(const OID& attr_oid, const MemoryRegion<byte>& attr_value) { oid = attr_oid; parameters = attr_value; } /* * Create an Attribute */ Attribute::Attribute(const std::string& attr_oid, const MemoryRegion<byte>& attr_value) { oid = OIDS::lookup(attr_oid); parameters = attr_value; } /* * DER encode a Attribute */ void Attribute::encode_into(DER_Encoder& codec) const { codec.start_cons(SEQUENCE) .encode(oid) .start_cons(SET) .raw_bytes(parameters) .end_cons() .end_cons(); } /* * Decode a BER encoded Attribute */ void Attribute::decode_from(BER_Decoder& codec) { codec.start_cons(SEQUENCE) .decode(oid) .start_cons(SET) .raw_bytes(parameters) .end_cons() .end_cons(); } } /* * ASN.1 Internals * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * BER Decoding Exceptions */ BER_Decoding_Error::BER_Decoding_Error(const std::string& str) : Decoding_Error("BER: " + str) {} BER_Bad_Tag::BER_Bad_Tag(const std::string& str, ASN1_Tag tag) : BER_Decoding_Error(str + ": " + to_string(tag)) {} BER_Bad_Tag::BER_Bad_Tag(const std::string& str, ASN1_Tag tag1, ASN1_Tag tag2) : BER_Decoding_Error(str + ": " + to_string(tag1) + "/" + to_string(tag2)) {} namespace ASN1 { /* * Put some arbitrary bytes into a SEQUENCE */ SecureVector<byte> put_in_sequence(const MemoryRegion<byte>& contents) { return DER_Encoder() .start_cons(SEQUENCE) .raw_bytes(contents) .end_cons() .get_contents(); } /* * Convert a BER object into a string object */ std::string to_string(const BER_Object& obj) { return std::string(reinterpret_cast<const char*>(&obj.value[0]), obj.value.size()); } /* * Do heuristic tests for BER data */ bool maybe_BER(DataSource& source) { byte first_byte; if(!source.peek_byte(first_byte)) throw Stream_IO_Error("ASN1::maybe_BER: Source was empty"); if(first_byte == (SEQUENCE | CONSTRUCTED)) return true; return false; } } } /* * ASN.1 OID * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * ASN.1 OID Constructor */ OID::OID(const std::string& oid_str) { if(oid_str != "") { try { id = parse_asn1_oid(oid_str); } catch(...) { throw Invalid_OID(oid_str); } if(id.size() < 2 || id[0] > 2) throw Invalid_OID(oid_str); if((id[0] == 0 || id[0] == 1) && id[1] > 39) throw Invalid_OID(oid_str); } } /* * Clear the current OID */ void OID::clear() { id.clear(); } /* * Return this OID as a string */ std::string OID::as_string() const { std::string oid_str; for(size_t i = 0; i != id.size(); ++i) { oid_str += to_string(id[i]); if(i != id.size() - 1) oid_str += '.'; } return oid_str; } /* * OID equality comparison */ bool OID::operator==(const OID& oid) const { if(id.size() != oid.id.size()) return false; for(size_t i = 0; i != id.size(); ++i) if(id[i] != oid.id[i]) return false; return true; } /* * Append another component to the OID */ OID& OID::operator+=(u32bit component) { id.push_back(component); return (*this); } /* * Append another component to the OID */ OID operator+(const OID& oid, u32bit component) { OID new_oid(oid); new_oid += component; return new_oid; } /* * OID inequality comparison */ bool operator!=(const OID& a, const OID& b) { return !(a == b); } /* * Compare two OIDs */ bool operator<(const OID& a, const OID& b) { std::vector<u32bit> oid1 = a.get_id(); std::vector<u32bit> oid2 = b.get_id(); if(oid1.size() < oid2.size()) return true; if(oid1.size() > oid2.size()) return false; for(size_t i = 0; i != oid1.size(); ++i) { if(oid1[i] < oid2[i]) return true; if(oid1[i] > oid2[i]) return false; } return false; } /* * DER encode an OBJECT IDENTIFIER */ void OID::encode_into(DER_Encoder& der) const { if(id.size() < 2) throw Invalid_Argument("OID::encode_into: OID is invalid"); MemoryVector<byte> encoding; encoding.push_back(40 * id[0] + id[1]); for(size_t i = 2; i != id.size(); ++i) { if(id[i] == 0) encoding.push_back(0); else { size_t blocks = high_bit(id[i]) + 6; blocks = (blocks - (blocks % 7)) / 7; for(size_t j = 0; j != blocks - 1; ++j) encoding.push_back(0x80 | ((id[i] >> 7*(blocks-j-1)) & 0x7F)); encoding.push_back(id[i] & 0x7F); } } der.add_object(OBJECT_ID, UNIVERSAL, encoding); } /* * Decode a BER encoded OBJECT IDENTIFIER */ void OID::decode_from(BER_Decoder& decoder) { BER_Object obj = decoder.get_next_object(); if(obj.type_tag != OBJECT_ID || obj.class_tag != UNIVERSAL) throw BER_Bad_Tag("Error decoding OID, unknown tag", obj.type_tag, obj.class_tag); if(obj.value.size() < 2) throw BER_Decoding_Error("OID encoding is too short"); clear(); id.push_back(obj.value[0] / 40); id.push_back(obj.value[0] % 40); size_t i = 0; while(i != obj.value.size() - 1) { u32bit component = 0; while(i != obj.value.size() - 1) { ++i; component = (component << 7) + (obj.value[i] & 0x7F); if(!(obj.value[i] & 0x80)) break; } id.push_back(component); } } } /* * Simple ASN.1 String Types * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { /* * Choose an encoding for the string */ ASN1_Tag choose_encoding(const std::string& str, const std::string& type) { static const byte IS_PRINTABLE[256] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; for(size_t i = 0; i != str.size(); ++i) { if(!IS_PRINTABLE[static_cast<byte>(str[i])]) { if(type == "utf8") return UTF8_STRING; if(type == "latin1") return T61_STRING; throw Invalid_Argument("choose_encoding: Bad string type " + type); } } return PRINTABLE_STRING; } } /* * Create an ASN1_String */ ASN1_String::ASN1_String(const std::string& str, ASN1_Tag t) : tag(t) { iso_8859_str = Charset::transcode(str, LOCAL_CHARSET, LATIN1_CHARSET); if(tag == DIRECTORY_STRING) tag = choose_encoding(iso_8859_str, "latin1"); if(tag != NUMERIC_STRING && tag != PRINTABLE_STRING && tag != VISIBLE_STRING && tag != T61_STRING && tag != IA5_STRING && tag != UTF8_STRING && tag != BMP_STRING) throw Invalid_Argument("ASN1_String: Unknown string type " + to_string(tag)); } /* * Create an ASN1_String */ ASN1_String::ASN1_String(const std::string& str) { iso_8859_str = Charset::transcode(str, LOCAL_CHARSET, LATIN1_CHARSET); tag = choose_encoding(iso_8859_str, "latin1"); } /* * Return this string in ISO 8859-1 encoding */ std::string ASN1_String::iso_8859() const { return iso_8859_str; } /* * Return this string in local encoding */ std::string ASN1_String::value() const { return Charset::transcode(iso_8859_str, LATIN1_CHARSET, LOCAL_CHARSET); } /* * Return the type of this string object */ ASN1_Tag ASN1_String::tagging() const { return tag; } /* * DER encode an ASN1_String */ void ASN1_String::encode_into(DER_Encoder& encoder) const { std::string value = iso_8859(); if(tagging() == UTF8_STRING) value = Charset::transcode(value, LATIN1_CHARSET, UTF8_CHARSET); encoder.add_object(tagging(), UNIVERSAL, value); } /* * Decode a BER encoded ASN1_String */ void ASN1_String::decode_from(BER_Decoder& source) { BER_Object obj = source.get_next_object(); Character_Set charset_is; if(obj.type_tag == BMP_STRING) charset_is = UCS2_CHARSET; else if(obj.type_tag == UTF8_STRING) charset_is = UTF8_CHARSET; else charset_is = LATIN1_CHARSET; *this = ASN1_String( Charset::transcode(ASN1::to_string(obj), charset_is, LOCAL_CHARSET), obj.type_tag); } } /* * X.509 Time Types * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Create an X509_Time */ X509_Time::X509_Time(const std::string& time_str) { set_to(time_str); } /* * Create an X509_Time */ X509_Time::X509_Time(u64bit timer) { calendar_point cal = calendar_value(timer); year = cal.year; month = cal.month; day = cal.day; hour = cal.hour; minute = cal.minutes; second = cal.seconds; tag = (year >= 2050) ? GENERALIZED_TIME : UTC_TIME; } /* * Create an X509_Time */ X509_Time::X509_Time(const std::string& t_spec, ASN1_Tag t) : tag(t) { set_to(t_spec, tag); } /* * Set the time with a human readable string */ void X509_Time::set_to(const std::string& time_str) { if(time_str == "") { year = month = day = hour = minute = second = 0; tag = NO_OBJECT; return; } std::vector<std::string> params; std::string current; for(size_t j = 0; j != time_str.size(); ++j) { if(Charset::is_digit(time_str[j])) current += time_str[j]; else { if(current != "") params.push_back(current); current.clear(); } } if(current != "") params.push_back(current); if(params.size() < 3 || params.size() > 6) throw Invalid_Argument("Invalid time specification " + time_str); year = to_u32bit(params[0]); month = to_u32bit(params[1]); day = to_u32bit(params[2]); hour = (params.size() >= 4) ? to_u32bit(params[3]) : 0; minute = (params.size() >= 5) ? to_u32bit(params[4]) : 0; second = (params.size() == 6) ? to_u32bit(params[5]) : 0; tag = (year >= 2050) ? GENERALIZED_TIME : UTC_TIME; if(!passes_sanity_check()) throw Invalid_Argument("Invalid time specification " + time_str); } /* * Set the time with an ISO time format string */ void X509_Time::set_to(const std::string& t_spec, ASN1_Tag spec_tag) { if(spec_tag != GENERALIZED_TIME && spec_tag != UTC_TIME) throw Invalid_Argument("X509_Time: Invalid tag " + to_string(spec_tag)); if(spec_tag == GENERALIZED_TIME && t_spec.size() != 13 && t_spec.size() != 15) throw Invalid_Argument("Invalid GeneralizedTime: " + t_spec); if(spec_tag == UTC_TIME && t_spec.size() != 11 && t_spec.size() != 13) throw Invalid_Argument("Invalid UTCTime: " + t_spec); if(t_spec[t_spec.size()-1] != 'Z') throw Invalid_Argument("Invalid time encoding: " + t_spec); const size_t YEAR_SIZE = (spec_tag == UTC_TIME) ? 2 : 4; std::vector<std::string> params; std::string current; for(size_t j = 0; j != YEAR_SIZE; ++j) current += t_spec[j]; params.push_back(current); current.clear(); for(size_t j = YEAR_SIZE; j != t_spec.size() - 1; ++j) { current += t_spec[j]; if(current.size() == 2) { params.push_back(current); current.clear(); } } year = to_u32bit(params[0]); month = to_u32bit(params[1]); day = to_u32bit(params[2]); hour = to_u32bit(params[3]); minute = to_u32bit(params[4]); second = (params.size() == 6) ? to_u32bit(params[5]) : 0; tag = spec_tag; if(spec_tag == UTC_TIME) { if(year >= 50) year += 1900; else year += 2000; } if(!passes_sanity_check()) throw Invalid_Argument("Invalid time specification " + t_spec); } /* * DER encode a X509_Time */ void X509_Time::encode_into(DER_Encoder& der) const { if(tag != GENERALIZED_TIME && tag != UTC_TIME) throw Invalid_Argument("X509_Time: Bad encoding tag"); der.add_object(tag, UNIVERSAL, Charset::transcode(as_string(), LOCAL_CHARSET, LATIN1_CHARSET)); } /* * Decode a BER encoded X509_Time */ void X509_Time::decode_from(BER_Decoder& source) { BER_Object ber_time = source.get_next_object(); set_to(Charset::transcode(ASN1::to_string(ber_time), LATIN1_CHARSET, LOCAL_CHARSET), ber_time.type_tag); } /* * Return a string representation of the time */ std::string X509_Time::as_string() const { if(time_is_set() == false) throw Invalid_State("X509_Time::as_string: No time set"); std::string asn1rep; if(tag == GENERALIZED_TIME) asn1rep = to_string(year, 4); else if(tag == UTC_TIME) { if(year < 1950 || year >= 2050) throw Encoding_Error("X509_Time: The time " + readable_string() + " cannot be encoded as a UTCTime"); u32bit asn1year = (year >= 2000) ? (year - 2000) : (year - 1900); asn1rep = to_string(asn1year, 2); } else throw Invalid_Argument("X509_Time: Invalid tag " + to_string(tag)); asn1rep += to_string(month, 2) + to_string(day, 2); asn1rep += to_string(hour, 2) + to_string(minute, 2) + to_string(second, 2); asn1rep += "Z"; return asn1rep; } /* * Return if the time has been set somehow */ bool X509_Time::time_is_set() const { return (year != 0); } /* * Return a human readable string representation */ std::string X509_Time::readable_string() const { if(time_is_set() == false) throw Invalid_State("X509_Time::readable_string: No time set"); std::string readable; readable += to_string(year, 4) + "/"; readable += to_string(month ) + "/"; readable += to_string(day ) + " "; readable += to_string(hour ) + ":"; readable += to_string(minute, 2) + ":"; readable += to_string(second, 2) + " UTC"; return readable; } /* * Do a general sanity check on the time */ bool X509_Time::passes_sanity_check() const { if(year < 1950 || year > 2100) return false; if(month == 0 || month > 12) return false; if(day == 0 || day > 31) return false; if(hour >= 24 || minute > 60 || second > 60) return false; return true; } /* * Compare this time against another */ s32bit X509_Time::cmp(const X509_Time& other) const { if(time_is_set() == false) throw Invalid_State("X509_Time::cmp: No time set"); const s32bit EARLIER = -1, LATER = 1, SAME_TIME = 0; if(year < other.year) return EARLIER; if(year > other.year) return LATER; if(month < other.month) return EARLIER; if(month > other.month) return LATER; if(day < other.day) return EARLIER; if(day > other.day) return LATER; if(hour < other.hour) return EARLIER; if(hour > other.hour) return LATER; if(minute < other.minute) return EARLIER; if(minute > other.minute) return LATER; if(second < other.second) return EARLIER; if(second > other.second) return LATER; return SAME_TIME; } /* * Compare two X509_Times for in various ways */ bool operator==(const X509_Time& t1, const X509_Time& t2) { return (t1.cmp(t2) == 0); } bool operator!=(const X509_Time& t1, const X509_Time& t2) { return (t1.cmp(t2) != 0); } bool operator<=(const X509_Time& t1, const X509_Time& t2) { return (t1.cmp(t2) <= 0); } bool operator>=(const X509_Time& t1, const X509_Time& t2) { return (t1.cmp(t2) >= 0); } bool operator<(const X509_Time& t1, const X509_Time& t2) { return (t1.cmp(t2) < 0); } bool operator>(const X509_Time& t1, const X509_Time& t2) { return (t1.cmp(t2) > 0); } } /* * BER Decoder * (C) 1999-2008 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { /* * BER decode an ASN.1 type tag */ size_t decode_tag(DataSource* ber, ASN1_Tag& type_tag, ASN1_Tag& class_tag) { byte b; if(!ber->read_byte(b)) { class_tag = type_tag = NO_OBJECT; return 0; } if((b & 0x1F) != 0x1F) { type_tag = ASN1_Tag(b & 0x1F); class_tag = ASN1_Tag(b & 0xE0); return 1; } size_t tag_bytes = 1; class_tag = ASN1_Tag(b & 0xE0); size_t tag_buf = 0; while(true) { if(!ber->read_byte(b)) throw BER_Decoding_Error("Long-form tag truncated"); if(tag_buf & 0xFF000000) throw BER_Decoding_Error("Long-form tag overflowed 32 bits"); ++tag_bytes; tag_buf = (tag_buf << 7) | (b & 0x7F); if((b & 0x80) == 0) break; } type_tag = ASN1_Tag(tag_buf); return tag_bytes; } /* * Find the EOC marker */ size_t find_eoc(DataSource*); /* * BER decode an ASN.1 length field */ size_t decode_length(DataSource* ber, size_t& field_size) { byte b; if(!ber->read_byte(b)) throw BER_Decoding_Error("Length field not found"); field_size = 1; if((b & 0x80) == 0) return b; field_size += (b & 0x7F); if(field_size == 1) return find_eoc(ber); if(field_size > 5) throw BER_Decoding_Error("Length field is too large"); size_t length = 0; for(size_t i = 0; i != field_size - 1; ++i) { if(get_byte(0, length) != 0) throw BER_Decoding_Error("Field length overflow"); if(!ber->read_byte(b)) throw BER_Decoding_Error("Corrupted length field"); length = (length << 8) | b; } return length; } /* * BER decode an ASN.1 length field */ size_t decode_length(DataSource* ber) { size_t dummy; return decode_length(ber, dummy); } /* * Find the EOC marker */ size_t find_eoc(DataSource* ber) { SecureVector<byte> buffer(DEFAULT_BUFFERSIZE), data; while(true) { const size_t got = ber->peek(&buffer[0], buffer.size(), data.size()); if(got == 0) break; data += std::make_pair(&buffer[0], got); } DataSource_Memory source(data); data.clear(); size_t length = 0; while(true) { ASN1_Tag type_tag, class_tag; size_t tag_size = decode_tag(&source, type_tag, class_tag); if(type_tag == NO_OBJECT) break; size_t length_size = 0; size_t item_size = decode_length(&source, length_size); source.discard_next(item_size); length += item_size + length_size + tag_size; if(type_tag == EOC) break; } return length; } } /* * Check a type invariant on BER data */ void BER_Object::assert_is_a(ASN1_Tag type_tag, ASN1_Tag class_tag) { if(this->type_tag != type_tag || this->class_tag != class_tag) throw BER_Decoding_Error("Tag mismatch when decoding"); } /* * Check if more objects are there */ bool BER_Decoder::more_items() const { if(source->end_of_data() && (pushed.type_tag == NO_OBJECT)) return false; return true; } /* * Verify that no bytes remain in the source */ BER_Decoder& BER_Decoder::verify_end() { if(!source->end_of_data() || (pushed.type_tag != NO_OBJECT)) throw Invalid_State("BER_Decoder::verify_end called, but data remains"); return (*this); } /* * Save all the bytes remaining in the source */ BER_Decoder& BER_Decoder::raw_bytes(MemoryRegion<byte>& out) { out.clear(); byte buf; while(source->read_byte(buf)) out.push_back(buf); return (*this); } /* * Discard all the bytes remaining in the source */ BER_Decoder& BER_Decoder::discard_remaining() { byte buf; while(source->read_byte(buf)) ; return (*this); } /* * Return the BER encoding of the next object */ BER_Object BER_Decoder::get_next_object() { BER_Object next; if(pushed.type_tag != NO_OBJECT) { next = pushed; pushed.class_tag = pushed.type_tag = NO_OBJECT; return next; } decode_tag(source, next.type_tag, next.class_tag); if(next.type_tag == NO_OBJECT) return next; size_t length = decode_length(source); next.value.resize(length); if(source->read(&next.value[0], length) != length) throw BER_Decoding_Error("Value truncated"); if(next.type_tag == EOC && next.class_tag == UNIVERSAL) return get_next_object(); return next; } /* * Push a object back into the stream */ void BER_Decoder::push_back(const BER_Object& obj) { if(pushed.type_tag != NO_OBJECT) throw Invalid_State("BER_Decoder: Only one push back is allowed"); pushed = obj; } /* * Begin decoding a CONSTRUCTED type */ BER_Decoder BER_Decoder::start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag) { BER_Object obj = get_next_object(); obj.assert_is_a(type_tag, ASN1_Tag(class_tag | CONSTRUCTED)); BER_Decoder result(&obj.value[0], obj.value.size()); result.parent = this; return result; } /* * Finish decoding a CONSTRUCTED type */ BER_Decoder& BER_Decoder::end_cons() { if(!parent) throw Invalid_State("BER_Decoder::end_cons called with NULL parent"); if(!source->end_of_data()) throw Decoding_Error("BER_Decoder::end_cons called with data left"); return (*parent); } /* * BER_Decoder Constructor */ BER_Decoder::BER_Decoder(DataSource& src) { source = &src; owns = false; pushed.type_tag = pushed.class_tag = NO_OBJECT; parent = 0; } /* * BER_Decoder Constructor */ BER_Decoder::BER_Decoder(const byte data[], size_t length) { source = new DataSource_Memory(data, length); owns = true; pushed.type_tag = pushed.class_tag = NO_OBJECT; parent = 0; } /* * BER_Decoder Constructor */ BER_Decoder::BER_Decoder(const MemoryRegion<byte>& data) { source = new DataSource_Memory(data); owns = true; pushed.type_tag = pushed.class_tag = NO_OBJECT; parent = 0; } /* * BER_Decoder Copy Constructor */ BER_Decoder::BER_Decoder(const BER_Decoder& other) { source = other.source; owns = false; if(other.owns) { other.owns = false; owns = true; } pushed.type_tag = pushed.class_tag = NO_OBJECT; parent = other.parent; } /* * BER_Decoder Destructor */ BER_Decoder::~BER_Decoder() { if(owns) delete source; source = 0; } /* * Request for an object to decode itself */ BER_Decoder& BER_Decoder::decode(ASN1_Object& obj) { obj.decode_from(*this); return (*this); } /* * Decode a BER encoded NULL */ BER_Decoder& BER_Decoder::decode_null() { BER_Object obj = get_next_object(); obj.assert_is_a(NULL_TAG, UNIVERSAL); if(obj.value.size()) throw BER_Decoding_Error("NULL object had nonzero size"); return (*this); } /* * Decode a BER encoded BOOLEAN */ BER_Decoder& BER_Decoder::decode(bool& out) { return decode(out, BOOLEAN, UNIVERSAL); } /* * Decode a small BER encoded INTEGER */ BER_Decoder& BER_Decoder::decode(size_t& out) { return decode(out, INTEGER, UNIVERSAL); } /* * Decode a BER encoded INTEGER */ BER_Decoder& BER_Decoder::decode(BigInt& out) { return decode(out, INTEGER, UNIVERSAL); } BER_Decoder& BER_Decoder::decode_octet_string_bigint(BigInt& out) { SecureVector<byte> out_vec; decode(out_vec, OCTET_STRING); out = BigInt::decode(&out_vec[0], out_vec.size()); return (*this); } /* * Decode a BER encoded BOOLEAN */ BER_Decoder& BER_Decoder::decode(bool& out, ASN1_Tag type_tag, ASN1_Tag class_tag) { BER_Object obj = get_next_object(); obj.assert_is_a(type_tag, class_tag); if(obj.value.size() != 1) throw BER_Decoding_Error("BER boolean value had invalid size"); out = (obj.value[0]) ? true : false; return (*this); } /* * Decode a small BER encoded INTEGER */ BER_Decoder& BER_Decoder::decode(size_t& out, ASN1_Tag type_tag, ASN1_Tag class_tag) { BigInt integer; decode(integer, type_tag, class_tag); if(integer.bits() > 32) throw BER_Decoding_Error("Decoded integer value larger than expected"); out = 0; for(size_t i = 0; i != 4; ++i) out = (out << 8) | integer.byte_at(3-i); return (*this); } /* * Decode a BER encoded INTEGER */ BER_Decoder& BER_Decoder::decode(BigInt& out, ASN1_Tag type_tag, ASN1_Tag class_tag) { BER_Object obj = get_next_object(); obj.assert_is_a(type_tag, class_tag); if(obj.value.empty()) out = 0; else { const bool negative = (obj.value[0] & 0x80) ? true : false; if(negative) { for(size_t i = obj.value.size(); i > 0; --i) if(obj.value[i-1]--) break; for(size_t i = 0; i != obj.value.size(); ++i) obj.value[i] = ~obj.value[i]; } out = BigInt(&obj.value[0], obj.value.size()); if(negative) out.flip_sign(); } return (*this); } /* * BER decode a BIT STRING or OCTET STRING */ BER_Decoder& BER_Decoder::decode(MemoryRegion<byte>& out, ASN1_Tag real_type) { return decode(out, real_type, real_type, UNIVERSAL); } /* * BER decode a BIT STRING or OCTET STRING */ BER_Decoder& BER_Decoder::decode(MemoryRegion<byte>& buffer, ASN1_Tag real_type, ASN1_Tag type_tag, ASN1_Tag class_tag) { if(real_type != OCTET_STRING && real_type != BIT_STRING) throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", real_type); BER_Object obj = get_next_object(); obj.assert_is_a(type_tag, class_tag); if(real_type == OCTET_STRING) buffer = obj.value; else { if(obj.value[0] >= 8) throw BER_Decoding_Error("Bad number of unused bits in BIT STRING"); buffer.resize(obj.value.size() - 1); copy_mem(&buffer[0], &obj.value[1], obj.value.size() - 1); } return (*this); } /* * Decode an OPTIONAL string type */ BER_Decoder& BER_Decoder::decode_optional_string(MemoryRegion<byte>& out, ASN1_Tag real_type, u16bit type_no) { BER_Object obj = get_next_object(); ASN1_Tag type_tag = static_cast<ASN1_Tag>(type_no); out.clear(); push_back(obj); if(obj.type_tag == type_tag && obj.class_tag == CONTEXT_SPECIFIC) decode(out, real_type, type_tag, CONTEXT_SPECIFIC); return (*this); } } /* * DER Encoder * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> namespace Botan { namespace { /* * DER encode an ASN.1 type tag */ SecureVector<byte> encode_tag(ASN1_Tag type_tag, ASN1_Tag class_tag) { if((class_tag | 0xE0) != 0xE0) throw Encoding_Error("DER_Encoder: Invalid class tag " + to_string(class_tag)); SecureVector<byte> encoded_tag; if(type_tag <= 30) encoded_tag.push_back(static_cast<byte>(type_tag | class_tag)); else { size_t blocks = high_bit(type_tag) + 6; blocks = (blocks - (blocks % 7)) / 7; encoded_tag.push_back(class_tag | 0x1F); for(size_t i = 0; i != blocks - 1; ++i) encoded_tag.push_back(0x80 | ((type_tag >> 7*(blocks-i-1)) & 0x7F)); encoded_tag.push_back(type_tag & 0x7F); } return encoded_tag; } /* * DER encode an ASN.1 length field */ SecureVector<byte> encode_length(size_t length) { SecureVector<byte> encoded_length; if(length <= 127) encoded_length.push_back(static_cast<byte>(length)); else { const size_t top_byte = significant_bytes(length); encoded_length.push_back(static_cast<byte>(0x80 | top_byte)); for(size_t i = sizeof(length) - top_byte; i != sizeof(length); ++i) encoded_length.push_back(get_byte(i, length)); } return encoded_length; } } /* * Return the encoded SEQUENCE/SET */ SecureVector<byte> DER_Encoder::DER_Sequence::get_contents() { const ASN1_Tag real_class_tag = ASN1_Tag(class_tag | CONSTRUCTED); if(type_tag == SET) { std::sort(set_contents.begin(), set_contents.end()); for(size_t i = 0; i != set_contents.size(); ++i) contents += set_contents[i]; set_contents.clear(); } SecureVector<byte> result; result += encode_tag(type_tag, real_class_tag); result += encode_length(contents.size()); result += contents; contents.clear(); return result; } /* * Add an encoded value to the SEQUENCE/SET */ void DER_Encoder::DER_Sequence::add_bytes(const byte data[], size_t length) { if(type_tag == SET) set_contents.push_back(SecureVector<byte>(data, length)); else contents += std::make_pair(data, length); } /* * Return the type and class taggings */ ASN1_Tag DER_Encoder::DER_Sequence::tag_of() const { return ASN1_Tag(type_tag | class_tag); } /* * DER_Sequence Constructor */ DER_Encoder::DER_Sequence::DER_Sequence(ASN1_Tag t1, ASN1_Tag t2) : type_tag(t1), class_tag(t2) { } /* * Return the encoded contents */ SecureVector<byte> DER_Encoder::get_contents() { if(subsequences.size() != 0) throw Invalid_State("DER_Encoder: Sequence hasn't been marked done"); SecureVector<byte> output; std::swap(output, contents); return output; } /* * Start a new ASN.1 SEQUENCE/SET/EXPLICIT */ DER_Encoder& DER_Encoder::start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag) { subsequences.push_back(DER_Sequence(type_tag, class_tag)); return (*this); } /* * Finish the current ASN.1 SEQUENCE/SET/EXPLICIT */ DER_Encoder& DER_Encoder::end_cons() { if(subsequences.empty()) throw Invalid_State("DER_Encoder::end_cons: No such sequence"); SecureVector<byte> seq = subsequences[subsequences.size()-1].get_contents(); subsequences.pop_back(); raw_bytes(seq); return (*this); } /* * Start a new ASN.1 EXPLICIT encoding */ DER_Encoder& DER_Encoder::start_explicit(u16bit type_no) { ASN1_Tag type_tag = static_cast<ASN1_Tag>(type_no); if(type_tag == SET) throw Internal_Error("DER_Encoder.start_explicit(SET); cannot perform"); return start_cons(type_tag, CONTEXT_SPECIFIC); } /* * Finish the current ASN.1 EXPLICIT encoding */ DER_Encoder& DER_Encoder::end_explicit() { return end_cons(); } /* * Write raw bytes into the stream */ DER_Encoder& DER_Encoder::raw_bytes(const MemoryRegion<byte>& val) { return raw_bytes(&val[0], val.size()); } /* * Write raw bytes into the stream */ DER_Encoder& DER_Encoder::raw_bytes(const byte bytes[], size_t length) { if(subsequences.size()) subsequences[subsequences.size()-1].add_bytes(bytes, length); else contents += std::make_pair(bytes, length); return (*this); } /* * Encode a NULL object */ DER_Encoder& DER_Encoder::encode_null() { return add_object(NULL_TAG, UNIVERSAL, 0, 0); } /* * DER encode a BOOLEAN */ DER_Encoder& DER_Encoder::encode(bool is_true) { return encode(is_true, BOOLEAN, UNIVERSAL); } /* * DER encode a small INTEGER */ DER_Encoder& DER_Encoder::encode(size_t n) { return encode(BigInt(n), INTEGER, UNIVERSAL); } /* * DER encode a small INTEGER */ DER_Encoder& DER_Encoder::encode(const BigInt& n) { return encode(n, INTEGER, UNIVERSAL); } /* * DER encode an OCTET STRING or BIT STRING */ DER_Encoder& DER_Encoder::encode(const MemoryRegion<byte>& bytes, ASN1_Tag real_type) { return encode(&bytes[0], bytes.size(), real_type, real_type, UNIVERSAL); } /* * Encode this object */ DER_Encoder& DER_Encoder::encode(const byte bytes[], size_t length, ASN1_Tag real_type) { return encode(bytes, length, real_type, real_type, UNIVERSAL); } /* * DER encode a BOOLEAN */ DER_Encoder& DER_Encoder::encode(bool is_true, ASN1_Tag type_tag, ASN1_Tag class_tag) { byte val = is_true ? 0xFF : 0x00; return add_object(type_tag, class_tag, &val, 1); } /* * DER encode a small INTEGER */ DER_Encoder& DER_Encoder::encode(size_t n, ASN1_Tag type_tag, ASN1_Tag class_tag) { return encode(BigInt(n), type_tag, class_tag); } /* * DER encode an INTEGER */ DER_Encoder& DER_Encoder::encode(const BigInt& n, ASN1_Tag type_tag, ASN1_Tag class_tag) { if(n == 0) return add_object(type_tag, class_tag, 0); bool extra_zero = (n.bits() % 8 == 0); SecureVector<byte> contents(extra_zero + n.bytes()); BigInt::encode(&contents[extra_zero], n); if(n < 0) { for(size_t i = 0; i != contents.size(); ++i) contents[i] = ~contents[i]; for(size_t i = contents.size(); i > 0; --i) if(++contents[i-1]) break; } return add_object(type_tag, class_tag, contents); } /* * DER encode an OCTET STRING or BIT STRING */ DER_Encoder& DER_Encoder::encode(const MemoryRegion<byte>& bytes, ASN1_Tag real_type, ASN1_Tag type_tag, ASN1_Tag class_tag) { return encode(&bytes[0], bytes.size(), real_type, type_tag, class_tag); } /* * DER encode an OCTET STRING or BIT STRING */ DER_Encoder& DER_Encoder::encode(const byte bytes[], size_t length, ASN1_Tag real_type, ASN1_Tag type_tag, ASN1_Tag class_tag) { if(real_type != OCTET_STRING && real_type != BIT_STRING) throw Invalid_Argument("DER_Encoder: Invalid tag for byte/bit string"); if(real_type == BIT_STRING) { SecureVector<byte> encoded; encoded.push_back(0); encoded += std::make_pair(bytes, length); return add_object(type_tag, class_tag, encoded); } else return add_object(type_tag, class_tag, bytes, length); } /* * Conditionally write some values to the stream */ DER_Encoder& DER_Encoder::encode_if(bool cond, DER_Encoder& codec) { if(cond) return raw_bytes(codec.get_contents()); return (*this); } /* * Request for an object to encode itself */ DER_Encoder& DER_Encoder::encode(const ASN1_Object& obj) { obj.encode_into(*this); return (*this); } /* * Write the encoding of the byte(s) */ DER_Encoder& DER_Encoder::add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const byte rep[], size_t length) { SecureVector<byte> buffer; buffer += encode_tag(type_tag, class_tag); buffer += encode_length(length); buffer += std::make_pair(rep, length); return raw_bytes(buffer); } /* * Write the encoding of the byte(s) */ DER_Encoder& DER_Encoder::add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const MemoryRegion<byte>& rep_buf) { const byte* rep = &rep_buf[0]; const size_t rep_len = rep_buf.size(); return add_object(type_tag, class_tag, rep, rep_len); } /* * Write the encoding of the byte(s) */ DER_Encoder& DER_Encoder::add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const std::string& rep_str) { const byte* rep = reinterpret_cast<const byte*>(rep_str.data()); const size_t rep_len = rep_str.size(); return add_object(type_tag, class_tag, rep, rep_len); } /* * Write the encoding of the byte */ DER_Encoder& DER_Encoder::add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, byte rep) { return add_object(type_tag, class_tag, &rep, 1); } } /* * X509_DN * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Create an empty X509_DN */ X509_DN::X509_DN() { } /* * Create an X509_DN */ X509_DN::X509_DN(const std::multimap<OID, std::string>& args) { std::multimap<OID, std::string>::const_iterator j; for(j = args.begin(); j != args.end(); ++j) add_attribute(j->first, j->second); } /* * Create an X509_DN */ X509_DN::X509_DN(const std::multimap<std::string, std::string>& args) { std::multimap<std::string, std::string>::const_iterator j; for(j = args.begin(); j != args.end(); ++j) add_attribute(OIDS::lookup(j->first), j->second); } /* * Add an attribute to a X509_DN */ void X509_DN::add_attribute(const std::string& type, const std::string& str) { OID oid = OIDS::lookup(type); add_attribute(oid, str); } /* * Add an attribute to a X509_DN */ void X509_DN::add_attribute(const OID& oid, const std::string& str) { if(str == "") return; typedef std::multimap<OID, ASN1_String>::iterator rdn_iter; std::pair<rdn_iter, rdn_iter> range = dn_info.equal_range(oid); for(rdn_iter j = range.first; j != range.second; ++j) if(j->second.value() == str) return; multimap_insert(dn_info, oid, ASN1_String(str)); dn_bits.clear(); } /* * Get the attributes of this X509_DN */ std::multimap<OID, std::string> X509_DN::get_attributes() const { typedef std::multimap<OID, ASN1_String>::const_iterator rdn_iter; std::multimap<OID, std::string> retval; for(rdn_iter j = dn_info.begin(); j != dn_info.end(); ++j) multimap_insert(retval, j->first, j->second.value()); return retval; } /* * Get the contents of this X.500 Name */ std::multimap<std::string, std::string> X509_DN::contents() const { typedef std::multimap<OID, ASN1_String>::const_iterator rdn_iter; std::multimap<std::string, std::string> retval; for(rdn_iter j = dn_info.begin(); j != dn_info.end(); ++j) multimap_insert(retval, OIDS::lookup(j->first), j->second.value()); return retval; } /* * Get a single attribute type */ std::vector<std::string> X509_DN::get_attribute(const std::string& attr) const { typedef std::multimap<OID, ASN1_String>::const_iterator rdn_iter; const OID oid = OIDS::lookup(deref_info_field(attr)); std::pair<rdn_iter, rdn_iter> range = dn_info.equal_range(oid); std::vector<std::string> values; for(rdn_iter j = range.first; j != range.second; ++j) values.push_back(j->second.value()); return values; } /* * Return the BER encoded data, if any */ MemoryVector<byte> X509_DN::get_bits() const { return dn_bits; } /* * Deref aliases in a subject/issuer info request */ std::string X509_DN::deref_info_field(const std::string& info) { if(info == "Name" || info == "CommonName") return "X520.CommonName"; if(info == "SerialNumber") return "X520.SerialNumber"; if(info == "Country") return "X520.Country"; if(info == "Organization") return "X520.Organization"; if(info == "Organizational Unit" || info == "OrgUnit") return "X520.OrganizationalUnit"; if(info == "Locality") return "X520.Locality"; if(info == "State" || info == "Province") return "X520.State"; if(info == "Email") return "RFC822"; return info; } /* * Compare two X509_DNs for equality */ bool operator==(const X509_DN& dn1, const X509_DN& dn2) { typedef std::multimap<OID, std::string>::const_iterator rdn_iter; std::multimap<OID, std::string> attr1 = dn1.get_attributes(); std::multimap<OID, std::string> attr2 = dn2.get_attributes(); if(attr1.size() != attr2.size()) return false; rdn_iter p1 = attr1.begin(); rdn_iter p2 = attr2.begin(); while(true) { if(p1 == attr1.end() && p2 == attr2.end()) break; if(p1 == attr1.end()) return false; if(p2 == attr2.end()) return false; if(p1->first != p2->first) return false; if(!x500_name_cmp(p1->second, p2->second)) return false; ++p1; ++p2; } return true; } /* * Compare two X509_DNs for inequality */ bool operator!=(const X509_DN& dn1, const X509_DN& dn2) { return !(dn1 == dn2); } /* * Compare two X509_DNs */ bool operator<(const X509_DN& dn1, const X509_DN& dn2) { typedef std::multimap<OID, std::string>::const_iterator rdn_iter; std::multimap<OID, std::string> attr1 = dn1.get_attributes(); std::multimap<OID, std::string> attr2 = dn2.get_attributes(); if(attr1.size() < attr2.size()) return true; if(attr1.size() > attr2.size()) return false; for(rdn_iter p1 = attr1.begin(); p1 != attr1.end(); ++p1) { std::multimap<OID, std::string>::const_iterator p2; p2 = attr2.find(p1->first); if(p2 == attr2.end()) return false; if(p1->second > p2->second) return false; if(p1->second < p2->second) return true; } return false; } namespace { /* * DER encode a RelativeDistinguishedName */ void do_ava(DER_Encoder& encoder, const std::multimap<OID, std::string>& dn_info, ASN1_Tag string_type, const std::string& oid_str, bool must_exist = false) { typedef std::multimap<OID, std::string>::const_iterator rdn_iter; const OID oid = OIDS::lookup(oid_str); const bool exists = (dn_info.find(oid) != dn_info.end()); if(!exists && must_exist) throw Encoding_Error("X509_DN: No entry for " + oid_str); if(!exists) return; std::pair<rdn_iter, rdn_iter> range = dn_info.equal_range(oid); for(rdn_iter j = range.first; j != range.second; ++j) { encoder.start_cons(SET) .start_cons(SEQUENCE) .encode(oid) .encode(ASN1_String(j->second, string_type)) .end_cons() .end_cons(); } } } /* * DER encode a DistinguishedName */ void X509_DN::encode_into(DER_Encoder& der) const { std::multimap<OID, std::string> dn_info = get_attributes(); der.start_cons(SEQUENCE); if(!dn_bits.empty()) der.raw_bytes(dn_bits); else { do_ava(der, dn_info, PRINTABLE_STRING, "X520.Country"); do_ava(der, dn_info, DIRECTORY_STRING, "X520.State"); do_ava(der, dn_info, DIRECTORY_STRING, "X520.Locality"); do_ava(der, dn_info, DIRECTORY_STRING, "X520.Organization"); do_ava(der, dn_info, DIRECTORY_STRING, "X520.OrganizationalUnit"); do_ava(der, dn_info, DIRECTORY_STRING, "X520.CommonName"); do_ava(der, dn_info, PRINTABLE_STRING, "X520.SerialNumber"); } der.end_cons(); } /* * Decode a BER encoded DistinguishedName */ void X509_DN::decode_from(BER_Decoder& source) { MemoryVector<byte> bits; source.start_cons(SEQUENCE) .raw_bytes(bits) .end_cons(); BER_Decoder sequence(bits); while(sequence.more_items()) { BER_Decoder rdn = sequence.start_cons(SET); while(rdn.more_items()) { OID oid; ASN1_String str; rdn.start_cons(SEQUENCE) .decode(oid) .decode(str) .verify_end() .end_cons(); add_attribute(oid, str.value()); } } dn_bits = bits; } } /* * Runtime benchmarking * (C) 2008 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <memory> namespace Botan { namespace { /** * Benchmark Buffered_Computation (hash or MAC) */ std::pair<u64bit, u64bit> bench_buf_comp(Buffered_Computation* buf_comp, u64bit nanoseconds_max, const byte buf[], size_t buf_len) { u64bit reps = 0; u64bit nanoseconds_used = 0; while(nanoseconds_used < nanoseconds_max) { const u64bit start = get_nanoseconds_clock(); buf_comp->update(buf, buf_len); nanoseconds_used += get_nanoseconds_clock() - start; ++reps; } return std::make_pair(reps * buf_len, nanoseconds_used); } /** * Benchmark block cipher */ std::pair<u64bit, u64bit> bench_block_cipher(BlockCipher* block_cipher, u64bit nanoseconds_max, byte buf[], size_t buf_len) { const size_t in_blocks = buf_len / block_cipher->block_size(); u64bit reps = 0; u64bit nanoseconds_used = 0; block_cipher->set_key(buf, block_cipher->maximum_keylength()); while(nanoseconds_used < nanoseconds_max) { const u64bit start = get_nanoseconds_clock(); block_cipher->encrypt_n(buf, buf, in_blocks); nanoseconds_used += get_nanoseconds_clock() - start; ++reps; } return std::make_pair(reps * in_blocks * block_cipher->block_size(), nanoseconds_used); } /** * Benchmark stream */ std::pair<u64bit, u64bit> bench_stream_cipher(StreamCipher* stream_cipher, u64bit nanoseconds_max, byte buf[], size_t buf_len) { u64bit reps = 0; u64bit nanoseconds_used = 0; stream_cipher->set_key(buf, stream_cipher->maximum_keylength()); while(nanoseconds_used < nanoseconds_max) { const u64bit start = get_nanoseconds_clock(); stream_cipher->cipher1(buf, buf_len); nanoseconds_used += get_nanoseconds_clock() - start; ++reps; } return std::make_pair(reps * buf_len, nanoseconds_used); } /** * Benchmark hash */ std::pair<u64bit, u64bit> bench_hash(HashFunction* hash, u64bit nanoseconds_max, const byte buf[], size_t buf_len) { return bench_buf_comp(hash, nanoseconds_max, buf, buf_len); } /** * Benchmark MAC */ std::pair<u64bit, u64bit> bench_mac(MessageAuthenticationCode* mac, u64bit nanoseconds_max, const byte buf[], size_t buf_len) { mac->set_key(buf, mac->maximum_keylength()); return bench_buf_comp(mac, nanoseconds_max, buf, buf_len); } } std::map<std::string, double> algorithm_benchmark(const std::string& name, Algorithm_Factory& af, RandomNumberGenerator& rng, u32bit milliseconds, size_t buf_size) { std::vector<std::string> providers = af.providers_of(name); std::map<std::string, double> all_results; if(providers.empty()) // no providers, nothing to do return all_results; const u64bit ns_per_provider = (static_cast<u64bit>(milliseconds) * 1000 * 1000) / providers.size(); std::vector<byte> buf(buf_size * 1024); rng.randomize(&buf[0], buf.size()); for(size_t i = 0; i != providers.size(); ++i) { const std::string provider = providers[i]; std::pair<u64bit, u64bit> results(0, 0); if(const BlockCipher* proto = af.prototype_block_cipher(name, provider)) { std::auto_ptr<BlockCipher> block_cipher(proto->clone()); results = bench_block_cipher(block_cipher.get(), ns_per_provider, &buf[0], buf.size()); } else if(const StreamCipher* proto = af.prototype_stream_cipher(name, provider)) { std::auto_ptr<StreamCipher> stream_cipher(proto->clone()); results = bench_stream_cipher(stream_cipher.get(), ns_per_provider, &buf[0], buf.size()); } else if(const HashFunction* proto = af.prototype_hash_function(name, provider)) { std::auto_ptr<HashFunction> hash(proto->clone()); results = bench_hash(hash.get(), ns_per_provider, &buf[0], buf.size()); } else if(const MessageAuthenticationCode* proto = af.prototype_mac(name, provider)) { std::auto_ptr<MessageAuthenticationCode> mac(proto->clone()); results = bench_mac(mac.get(), ns_per_provider, &buf[0], buf.size()); } if(results.first && results.second) { /* 953.67 == 1000 * 1000 * 1000 / 1024 / 1024 - the conversion factor from bytes per nanosecond to mebibytes per second. */ double speed = (953.67 * results.first) / results.second; all_results[provider] = speed; } } return all_results; } } /* * AES * (C) 1999-2010 Jack Lloyd * * Based on the public domain reference implemenation * * Distributed under the terms of the Botan license */ namespace Botan { namespace { const byte SE[256] = { 0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76, 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0, 0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15, 0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75, 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84, 0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF, 0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8, 0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2, 0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73, 0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB, 0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79, 0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08, 0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A, 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E, 0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF, 0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16 }; const byte SD[256] = { 0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38, 0xBF, 0x40, 0xA3, 0x9E, 0x81, 0xF3, 0xD7, 0xFB, 0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87, 0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB, 0x54, 0x7B, 0x94, 0x32, 0xA6, 0xC2, 0x23, 0x3D, 0xEE, 0x4C, 0x95, 0x0B, 0x42, 0xFA, 0xC3, 0x4E, 0x08, 0x2E, 0xA1, 0x66, 0x28, 0xD9, 0x24, 0xB2, 0x76, 0x5B, 0xA2, 0x49, 0x6D, 0x8B, 0xD1, 0x25, 0x72, 0xF8, 0xF6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xD4, 0xA4, 0x5C, 0xCC, 0x5D, 0x65, 0xB6, 0x92, 0x6C, 0x70, 0x48, 0x50, 0xFD, 0xED, 0xB9, 0xDA, 0x5E, 0x15, 0x46, 0x57, 0xA7, 0x8D, 0x9D, 0x84, 0x90, 0xD8, 0xAB, 0x00, 0x8C, 0xBC, 0xD3, 0x0A, 0xF7, 0xE4, 0x58, 0x05, 0xB8, 0xB3, 0x45, 0x06, 0xD0, 0x2C, 0x1E, 0x8F, 0xCA, 0x3F, 0x0F, 0x02, 0xC1, 0xAF, 0xBD, 0x03, 0x01, 0x13, 0x8A, 0x6B, 0x3A, 0x91, 0x11, 0x41, 0x4F, 0x67, 0xDC, 0xEA, 0x97, 0xF2, 0xCF, 0xCE, 0xF0, 0xB4, 0xE6, 0x73, 0x96, 0xAC, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85, 0xE2, 0xF9, 0x37, 0xE8, 0x1C, 0x75, 0xDF, 0x6E, 0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89, 0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B, 0xFC, 0x56, 0x3E, 0x4B, 0xC6, 0xD2, 0x79, 0x20, 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4, 0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31, 0xB1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xEC, 0x5F, 0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D, 0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF, 0xA0, 0xE0, 0x3B, 0x4D, 0xAE, 0x2A, 0xF5, 0xB0, 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61, 0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C, 0x7D }; const u32bit TE[1024] = { 0xC66363A5, 0xF87C7C84, 0xEE777799, 0xF67B7B8D, 0xFFF2F20D, 0xD66B6BBD, 0xDE6F6FB1, 0x91C5C554, 0x60303050, 0x02010103, 0xCE6767A9, 0x562B2B7D, 0xE7FEFE19, 0xB5D7D762, 0x4DABABE6, 0xEC76769A, 0x8FCACA45, 0x1F82829D, 0x89C9C940, 0xFA7D7D87, 0xEFFAFA15, 0xB25959EB, 0x8E4747C9, 0xFBF0F00B, 0x41ADADEC, 0xB3D4D467, 0x5FA2A2FD, 0x45AFAFEA, 0x239C9CBF, 0x53A4A4F7, 0xE4727296, 0x9BC0C05B, 0x75B7B7C2, 0xE1FDFD1C, 0x3D9393AE, 0x4C26266A, 0x6C36365A, 0x7E3F3F41, 0xF5F7F702, 0x83CCCC4F, 0x6834345C, 0x51A5A5F4, 0xD1E5E534, 0xF9F1F108, 0xE2717193, 0xABD8D873, 0x62313153, 0x2A15153F, 0x0804040C, 0x95C7C752, 0x46232365, 0x9DC3C35E, 0x30181828, 0x379696A1, 0x0A05050F, 0x2F9A9AB5, 0x0E070709, 0x24121236, 0x1B80809B, 0xDFE2E23D, 0xCDEBEB26, 0x4E272769, 0x7FB2B2CD, 0xEA75759F, 0x1209091B, 0x1D83839E, 0x582C2C74, 0x341A1A2E, 0x361B1B2D, 0xDC6E6EB2, 0xB45A5AEE, 0x5BA0A0FB, 0xA45252F6, 0x763B3B4D, 0xB7D6D661, 0x7DB3B3CE, 0x5229297B, 0xDDE3E33E, 0x5E2F2F71, 0x13848497, 0xA65353F5, 0xB9D1D168, 0x00000000, 0xC1EDED2C, 0x40202060, 0xE3FCFC1F, 0x79B1B1C8, 0xB65B5BED, 0xD46A6ABE, 0x8DCBCB46, 0x67BEBED9, 0x7239394B, 0x944A4ADE, 0x984C4CD4, 0xB05858E8, 0x85CFCF4A, 0xBBD0D06B, 0xC5EFEF2A, 0x4FAAAAE5, 0xEDFBFB16, 0x864343C5, 0x9A4D4DD7, 0x66333355, 0x11858594, 0x8A4545CF, 0xE9F9F910, 0x04020206, 0xFE7F7F81, 0xA05050F0, 0x783C3C44, 0x259F9FBA, 0x4BA8A8E3, 0xA25151F3, 0x5DA3A3FE, 0x804040C0, 0x058F8F8A, 0x3F9292AD, 0x219D9DBC, 0x70383848, 0xF1F5F504, 0x63BCBCDF, 0x77B6B6C1, 0xAFDADA75, 0x42212163, 0x20101030, 0xE5FFFF1A, 0xFDF3F30E, 0xBFD2D26D, 0x81CDCD4C, 0x180C0C14, 0x26131335, 0xC3ECEC2F, 0xBE5F5FE1, 0x359797A2, 0x884444CC, 0x2E171739, 0x93C4C457, 0x55A7A7F2, 0xFC7E7E82, 0x7A3D3D47, 0xC86464AC, 0xBA5D5DE7, 0x3219192B, 0xE6737395, 0xC06060A0, 0x19818198, 0x9E4F4FD1, 0xA3DCDC7F, 0x44222266, 0x542A2A7E, 0x3B9090AB, 0x0B888883, 0x8C4646CA, 0xC7EEEE29, 0x6BB8B8D3, 0x2814143C, 0xA7DEDE79, 0xBC5E5EE2, 0x160B0B1D, 0xADDBDB76, 0xDBE0E03B, 0x64323256, 0x743A3A4E, 0x140A0A1E, 0x924949DB, 0x0C06060A, 0x4824246C, 0xB85C5CE4, 0x9FC2C25D, 0xBDD3D36E, 0x43ACACEF, 0xC46262A6, 0x399191A8, 0x319595A4, 0xD3E4E437, 0xF279798B, 0xD5E7E732, 0x8BC8C843, 0x6E373759, 0xDA6D6DB7, 0x018D8D8C, 0xB1D5D564, 0x9C4E4ED2, 0x49A9A9E0, 0xD86C6CB4, 0xAC5656FA, 0xF3F4F407, 0xCFEAEA25, 0xCA6565AF, 0xF47A7A8E, 0x47AEAEE9, 0x10080818, 0x6FBABAD5, 0xF0787888, 0x4A25256F, 0x5C2E2E72, 0x381C1C24, 0x57A6A6F1, 0x73B4B4C7, 0x97C6C651, 0xCBE8E823, 0xA1DDDD7C, 0xE874749C, 0x3E1F1F21, 0x964B4BDD, 0x61BDBDDC, 0x0D8B8B86, 0x0F8A8A85, 0xE0707090, 0x7C3E3E42, 0x71B5B5C4, 0xCC6666AA, 0x904848D8, 0x06030305, 0xF7F6F601, 0x1C0E0E12, 0xC26161A3, 0x6A35355F, 0xAE5757F9, 0x69B9B9D0, 0x17868691, 0x99C1C158, 0x3A1D1D27, 0x279E9EB9, 0xD9E1E138, 0xEBF8F813, 0x2B9898B3, 0x22111133, 0xD26969BB, 0xA9D9D970, 0x078E8E89, 0x339494A7, 0x2D9B9BB6, 0x3C1E1E22, 0x15878792, 0xC9E9E920, 0x87CECE49, 0xAA5555FF, 0x50282878, 0xA5DFDF7A, 0x038C8C8F, 0x59A1A1F8, 0x09898980, 0x1A0D0D17, 0x65BFBFDA, 0xD7E6E631, 0x844242C6, 0xD06868B8, 0x824141C3, 0x299999B0, 0x5A2D2D77, 0x1E0F0F11, 0x7BB0B0CB, 0xA85454FC, 0x6DBBBBD6, 0x2C16163A, 0xA5C66363, 0x84F87C7C, 0x99EE7777, 0x8DF67B7B, 0x0DFFF2F2, 0xBDD66B6B, 0xB1DE6F6F, 0x5491C5C5, 0x50603030, 0x03020101, 0xA9CE6767, 0x7D562B2B, 0x19E7FEFE, 0x62B5D7D7, 0xE64DABAB, 0x9AEC7676, 0x458FCACA, 0x9D1F8282, 0x4089C9C9, 0x87FA7D7D, 0x15EFFAFA, 0xEBB25959, 0xC98E4747, 0x0BFBF0F0, 0xEC41ADAD, 0x67B3D4D4, 0xFD5FA2A2, 0xEA45AFAF, 0xBF239C9C, 0xF753A4A4, 0x96E47272, 0x5B9BC0C0, 0xC275B7B7, 0x1CE1FDFD, 0xAE3D9393, 0x6A4C2626, 0x5A6C3636, 0x417E3F3F, 0x02F5F7F7, 0x4F83CCCC, 0x5C683434, 0xF451A5A5, 0x34D1E5E5, 0x08F9F1F1, 0x93E27171, 0x73ABD8D8, 0x53623131, 0x3F2A1515, 0x0C080404, 0x5295C7C7, 0x65462323, 0x5E9DC3C3, 0x28301818, 0xA1379696, 0x0F0A0505, 0xB52F9A9A, 0x090E0707, 0x36241212, 0x9B1B8080, 0x3DDFE2E2, 0x26CDEBEB, 0x694E2727, 0xCD7FB2B2, 0x9FEA7575, 0x1B120909, 0x9E1D8383, 0x74582C2C, 0x2E341A1A, 0x2D361B1B, 0xB2DC6E6E, 0xEEB45A5A, 0xFB5BA0A0, 0xF6A45252, 0x4D763B3B, 0x61B7D6D6, 0xCE7DB3B3, 0x7B522929, 0x3EDDE3E3, 0x715E2F2F, 0x97138484, 0xF5A65353, 0x68B9D1D1, 0x00000000, 0x2CC1EDED, 0x60402020, 0x1FE3FCFC, 0xC879B1B1, 0xEDB65B5B, 0xBED46A6A, 0x468DCBCB, 0xD967BEBE, 0x4B723939, 0xDE944A4A, 0xD4984C4C, 0xE8B05858, 0x4A85CFCF, 0x6BBBD0D0, 0x2AC5EFEF, 0xE54FAAAA, 0x16EDFBFB, 0xC5864343, 0xD79A4D4D, 0x55663333, 0x94118585, 0xCF8A4545, 0x10E9F9F9, 0x06040202, 0x81FE7F7F, 0xF0A05050, 0x44783C3C, 0xBA259F9F, 0xE34BA8A8, 0xF3A25151, 0xFE5DA3A3, 0xC0804040, 0x8A058F8F, 0xAD3F9292, 0xBC219D9D, 0x48703838, 0x04F1F5F5, 0xDF63BCBC, 0xC177B6B6, 0x75AFDADA, 0x63422121, 0x30201010, 0x1AE5FFFF, 0x0EFDF3F3, 0x6DBFD2D2, 0x4C81CDCD, 0x14180C0C, 0x35261313, 0x2FC3ECEC, 0xE1BE5F5F, 0xA2359797, 0xCC884444, 0x392E1717, 0x5793C4C4, 0xF255A7A7, 0x82FC7E7E, 0x477A3D3D, 0xACC86464, 0xE7BA5D5D, 0x2B321919, 0x95E67373, 0xA0C06060, 0x98198181, 0xD19E4F4F, 0x7FA3DCDC, 0x66442222, 0x7E542A2A, 0xAB3B9090, 0x830B8888, 0xCA8C4646, 0x29C7EEEE, 0xD36BB8B8, 0x3C281414, 0x79A7DEDE, 0xE2BC5E5E, 0x1D160B0B, 0x76ADDBDB, 0x3BDBE0E0, 0x56643232, 0x4E743A3A, 0x1E140A0A, 0xDB924949, 0x0A0C0606, 0x6C482424, 0xE4B85C5C, 0x5D9FC2C2, 0x6EBDD3D3, 0xEF43ACAC, 0xA6C46262, 0xA8399191, 0xA4319595, 0x37D3E4E4, 0x8BF27979, 0x32D5E7E7, 0x438BC8C8, 0x596E3737, 0xB7DA6D6D, 0x8C018D8D, 0x64B1D5D5, 0xD29C4E4E, 0xE049A9A9, 0xB4D86C6C, 0xFAAC5656, 0x07F3F4F4, 0x25CFEAEA, 0xAFCA6565, 0x8EF47A7A, 0xE947AEAE, 0x18100808, 0xD56FBABA, 0x88F07878, 0x6F4A2525, 0x725C2E2E, 0x24381C1C, 0xF157A6A6, 0xC773B4B4, 0x5197C6C6, 0x23CBE8E8, 0x7CA1DDDD, 0x9CE87474, 0x213E1F1F, 0xDD964B4B, 0xDC61BDBD, 0x860D8B8B, 0x850F8A8A, 0x90E07070, 0x427C3E3E, 0xC471B5B5, 0xAACC6666, 0xD8904848, 0x05060303, 0x01F7F6F6, 0x121C0E0E, 0xA3C26161, 0x5F6A3535, 0xF9AE5757, 0xD069B9B9, 0x91178686, 0x5899C1C1, 0x273A1D1D, 0xB9279E9E, 0x38D9E1E1, 0x13EBF8F8, 0xB32B9898, 0x33221111, 0xBBD26969, 0x70A9D9D9, 0x89078E8E, 0xA7339494, 0xB62D9B9B, 0x223C1E1E, 0x92158787, 0x20C9E9E9, 0x4987CECE, 0xFFAA5555, 0x78502828, 0x7AA5DFDF, 0x8F038C8C, 0xF859A1A1, 0x80098989, 0x171A0D0D, 0xDA65BFBF, 0x31D7E6E6, 0xC6844242, 0xB8D06868, 0xC3824141, 0xB0299999, 0x775A2D2D, 0x111E0F0F, 0xCB7BB0B0, 0xFCA85454, 0xD66DBBBB, 0x3A2C1616, 0x63A5C663, 0x7C84F87C, 0x7799EE77, 0x7B8DF67B, 0xF20DFFF2, 0x6BBDD66B, 0x6FB1DE6F, 0xC55491C5, 0x30506030, 0x01030201, 0x67A9CE67, 0x2B7D562B, 0xFE19E7FE, 0xD762B5D7, 0xABE64DAB, 0x769AEC76, 0xCA458FCA, 0x829D1F82, 0xC94089C9, 0x7D87FA7D, 0xFA15EFFA, 0x59EBB259, 0x47C98E47, 0xF00BFBF0, 0xADEC41AD, 0xD467B3D4, 0xA2FD5FA2, 0xAFEA45AF, 0x9CBF239C, 0xA4F753A4, 0x7296E472, 0xC05B9BC0, 0xB7C275B7, 0xFD1CE1FD, 0x93AE3D93, 0x266A4C26, 0x365A6C36, 0x3F417E3F, 0xF702F5F7, 0xCC4F83CC, 0x345C6834, 0xA5F451A5, 0xE534D1E5, 0xF108F9F1, 0x7193E271, 0xD873ABD8, 0x31536231, 0x153F2A15, 0x040C0804, 0xC75295C7, 0x23654623, 0xC35E9DC3, 0x18283018, 0x96A13796, 0x050F0A05, 0x9AB52F9A, 0x07090E07, 0x12362412, 0x809B1B80, 0xE23DDFE2, 0xEB26CDEB, 0x27694E27, 0xB2CD7FB2, 0x759FEA75, 0x091B1209, 0x839E1D83, 0x2C74582C, 0x1A2E341A, 0x1B2D361B, 0x6EB2DC6E, 0x5AEEB45A, 0xA0FB5BA0, 0x52F6A452, 0x3B4D763B, 0xD661B7D6, 0xB3CE7DB3, 0x297B5229, 0xE33EDDE3, 0x2F715E2F, 0x84971384, 0x53F5A653, 0xD168B9D1, 0x00000000, 0xED2CC1ED, 0x20604020, 0xFC1FE3FC, 0xB1C879B1, 0x5BEDB65B, 0x6ABED46A, 0xCB468DCB, 0xBED967BE, 0x394B7239, 0x4ADE944A, 0x4CD4984C, 0x58E8B058, 0xCF4A85CF, 0xD06BBBD0, 0xEF2AC5EF, 0xAAE54FAA, 0xFB16EDFB, 0x43C58643, 0x4DD79A4D, 0x33556633, 0x85941185, 0x45CF8A45, 0xF910E9F9, 0x02060402, 0x7F81FE7F, 0x50F0A050, 0x3C44783C, 0x9FBA259F, 0xA8E34BA8, 0x51F3A251, 0xA3FE5DA3, 0x40C08040, 0x8F8A058F, 0x92AD3F92, 0x9DBC219D, 0x38487038, 0xF504F1F5, 0xBCDF63BC, 0xB6C177B6, 0xDA75AFDA, 0x21634221, 0x10302010, 0xFF1AE5FF, 0xF30EFDF3, 0xD26DBFD2, 0xCD4C81CD, 0x0C14180C, 0x13352613, 0xEC2FC3EC, 0x5FE1BE5F, 0x97A23597, 0x44CC8844, 0x17392E17, 0xC45793C4, 0xA7F255A7, 0x7E82FC7E, 0x3D477A3D, 0x64ACC864, 0x5DE7BA5D, 0x192B3219, 0x7395E673, 0x60A0C060, 0x81981981, 0x4FD19E4F, 0xDC7FA3DC, 0x22664422, 0x2A7E542A, 0x90AB3B90, 0x88830B88, 0x46CA8C46, 0xEE29C7EE, 0xB8D36BB8, 0x143C2814, 0xDE79A7DE, 0x5EE2BC5E, 0x0B1D160B, 0xDB76ADDB, 0xE03BDBE0, 0x32566432, 0x3A4E743A, 0x0A1E140A, 0x49DB9249, 0x060A0C06, 0x246C4824, 0x5CE4B85C, 0xC25D9FC2, 0xD36EBDD3, 0xACEF43AC, 0x62A6C462, 0x91A83991, 0x95A43195, 0xE437D3E4, 0x798BF279, 0xE732D5E7, 0xC8438BC8, 0x37596E37, 0x6DB7DA6D, 0x8D8C018D, 0xD564B1D5, 0x4ED29C4E, 0xA9E049A9, 0x6CB4D86C, 0x56FAAC56, 0xF407F3F4, 0xEA25CFEA, 0x65AFCA65, 0x7A8EF47A, 0xAEE947AE, 0x08181008, 0xBAD56FBA, 0x7888F078, 0x256F4A25, 0x2E725C2E, 0x1C24381C, 0xA6F157A6, 0xB4C773B4, 0xC65197C6, 0xE823CBE8, 0xDD7CA1DD, 0x749CE874, 0x1F213E1F, 0x4BDD964B, 0xBDDC61BD, 0x8B860D8B, 0x8A850F8A, 0x7090E070, 0x3E427C3E, 0xB5C471B5, 0x66AACC66, 0x48D89048, 0x03050603, 0xF601F7F6, 0x0E121C0E, 0x61A3C261, 0x355F6A35, 0x57F9AE57, 0xB9D069B9, 0x86911786, 0xC15899C1, 0x1D273A1D, 0x9EB9279E, 0xE138D9E1, 0xF813EBF8, 0x98B32B98, 0x11332211, 0x69BBD269, 0xD970A9D9, 0x8E89078E, 0x94A73394, 0x9BB62D9B, 0x1E223C1E, 0x87921587, 0xE920C9E9, 0xCE4987CE, 0x55FFAA55, 0x28785028, 0xDF7AA5DF, 0x8C8F038C, 0xA1F859A1, 0x89800989, 0x0D171A0D, 0xBFDA65BF, 0xE631D7E6, 0x42C68442, 0x68B8D068, 0x41C38241, 0x99B02999, 0x2D775A2D, 0x0F111E0F, 0xB0CB7BB0, 0x54FCA854, 0xBBD66DBB, 0x163A2C16, 0x6363A5C6, 0x7C7C84F8, 0x777799EE, 0x7B7B8DF6, 0xF2F20DFF, 0x6B6BBDD6, 0x6F6FB1DE, 0xC5C55491, 0x30305060, 0x01010302, 0x6767A9CE, 0x2B2B7D56, 0xFEFE19E7, 0xD7D762B5, 0xABABE64D, 0x76769AEC, 0xCACA458F, 0x82829D1F, 0xC9C94089, 0x7D7D87FA, 0xFAFA15EF, 0x5959EBB2, 0x4747C98E, 0xF0F00BFB, 0xADADEC41, 0xD4D467B3, 0xA2A2FD5F, 0xAFAFEA45, 0x9C9CBF23, 0xA4A4F753, 0x727296E4, 0xC0C05B9B, 0xB7B7C275, 0xFDFD1CE1, 0x9393AE3D, 0x26266A4C, 0x36365A6C, 0x3F3F417E, 0xF7F702F5, 0xCCCC4F83, 0x34345C68, 0xA5A5F451, 0xE5E534D1, 0xF1F108F9, 0x717193E2, 0xD8D873AB, 0x31315362, 0x15153F2A, 0x04040C08, 0xC7C75295, 0x23236546, 0xC3C35E9D, 0x18182830, 0x9696A137, 0x05050F0A, 0x9A9AB52F, 0x0707090E, 0x12123624, 0x80809B1B, 0xE2E23DDF, 0xEBEB26CD, 0x2727694E, 0xB2B2CD7F, 0x75759FEA, 0x09091B12, 0x83839E1D, 0x2C2C7458, 0x1A1A2E34, 0x1B1B2D36, 0x6E6EB2DC, 0x5A5AEEB4, 0xA0A0FB5B, 0x5252F6A4, 0x3B3B4D76, 0xD6D661B7, 0xB3B3CE7D, 0x29297B52, 0xE3E33EDD, 0x2F2F715E, 0x84849713, 0x5353F5A6, 0xD1D168B9, 0x00000000, 0xEDED2CC1, 0x20206040, 0xFCFC1FE3, 0xB1B1C879, 0x5B5BEDB6, 0x6A6ABED4, 0xCBCB468D, 0xBEBED967, 0x39394B72, 0x4A4ADE94, 0x4C4CD498, 0x5858E8B0, 0xCFCF4A85, 0xD0D06BBB, 0xEFEF2AC5, 0xAAAAE54F, 0xFBFB16ED, 0x4343C586, 0x4D4DD79A, 0x33335566, 0x85859411, 0x4545CF8A, 0xF9F910E9, 0x02020604, 0x7F7F81FE, 0x5050F0A0, 0x3C3C4478, 0x9F9FBA25, 0xA8A8E34B, 0x5151F3A2, 0xA3A3FE5D, 0x4040C080, 0x8F8F8A05, 0x9292AD3F, 0x9D9DBC21, 0x38384870, 0xF5F504F1, 0xBCBCDF63, 0xB6B6C177, 0xDADA75AF, 0x21216342, 0x10103020, 0xFFFF1AE5, 0xF3F30EFD, 0xD2D26DBF, 0xCDCD4C81, 0x0C0C1418, 0x13133526, 0xECEC2FC3, 0x5F5FE1BE, 0x9797A235, 0x4444CC88, 0x1717392E, 0xC4C45793, 0xA7A7F255, 0x7E7E82FC, 0x3D3D477A, 0x6464ACC8, 0x5D5DE7BA, 0x19192B32, 0x737395E6, 0x6060A0C0, 0x81819819, 0x4F4FD19E, 0xDCDC7FA3, 0x22226644, 0x2A2A7E54, 0x9090AB3B, 0x8888830B, 0x4646CA8C, 0xEEEE29C7, 0xB8B8D36B, 0x14143C28, 0xDEDE79A7, 0x5E5EE2BC, 0x0B0B1D16, 0xDBDB76AD, 0xE0E03BDB, 0x32325664, 0x3A3A4E74, 0x0A0A1E14, 0x4949DB92, 0x06060A0C, 0x24246C48, 0x5C5CE4B8, 0xC2C25D9F, 0xD3D36EBD, 0xACACEF43, 0x6262A6C4, 0x9191A839, 0x9595A431, 0xE4E437D3, 0x79798BF2, 0xE7E732D5, 0xC8C8438B, 0x3737596E, 0x6D6DB7DA, 0x8D8D8C01, 0xD5D564B1, 0x4E4ED29C, 0xA9A9E049, 0x6C6CB4D8, 0x5656FAAC, 0xF4F407F3, 0xEAEA25CF, 0x6565AFCA, 0x7A7A8EF4, 0xAEAEE947, 0x08081810, 0xBABAD56F, 0x787888F0, 0x25256F4A, 0x2E2E725C, 0x1C1C2438, 0xA6A6F157, 0xB4B4C773, 0xC6C65197, 0xE8E823CB, 0xDDDD7CA1, 0x74749CE8, 0x1F1F213E, 0x4B4BDD96, 0xBDBDDC61, 0x8B8B860D, 0x8A8A850F, 0x707090E0, 0x3E3E427C, 0xB5B5C471, 0x6666AACC, 0x4848D890, 0x03030506, 0xF6F601F7, 0x0E0E121C, 0x6161A3C2, 0x35355F6A, 0x5757F9AE, 0xB9B9D069, 0x86869117, 0xC1C15899, 0x1D1D273A, 0x9E9EB927, 0xE1E138D9, 0xF8F813EB, 0x9898B32B, 0x11113322, 0x6969BBD2, 0xD9D970A9, 0x8E8E8907, 0x9494A733, 0x9B9BB62D, 0x1E1E223C, 0x87879215, 0xE9E920C9, 0xCECE4987, 0x5555FFAA, 0x28287850, 0xDFDF7AA5, 0x8C8C8F03, 0xA1A1F859, 0x89898009, 0x0D0D171A, 0xBFBFDA65, 0xE6E631D7, 0x4242C684, 0x6868B8D0, 0x4141C382, 0x9999B029, 0x2D2D775A, 0x0F0F111E, 0xB0B0CB7B, 0x5454FCA8, 0xBBBBD66D, 0x16163A2C }; const u32bit TD[1024] = { 0x51F4A750, 0x7E416553, 0x1A17A4C3, 0x3A275E96, 0x3BAB6BCB, 0x1F9D45F1, 0xACFA58AB, 0x4BE30393, 0x2030FA55, 0xAD766DF6, 0x88CC7691, 0xF5024C25, 0x4FE5D7FC, 0xC52ACBD7, 0x26354480, 0xB562A38F, 0xDEB15A49, 0x25BA1B67, 0x45EA0E98, 0x5DFEC0E1, 0xC32F7502, 0x814CF012, 0x8D4697A3, 0x6BD3F9C6, 0x038F5FE7, 0x15929C95, 0xBF6D7AEB, 0x955259DA, 0xD4BE832D, 0x587421D3, 0x49E06929, 0x8EC9C844, 0x75C2896A, 0xF48E7978, 0x99583E6B, 0x27B971DD, 0xBEE14FB6, 0xF088AD17, 0xC920AC66, 0x7DCE3AB4, 0x63DF4A18, 0xE51A3182, 0x97513360, 0x62537F45, 0xB16477E0, 0xBB6BAE84, 0xFE81A01C, 0xF9082B94, 0x70486858, 0x8F45FD19, 0x94DE6C87, 0x527BF8B7, 0xAB73D323, 0x724B02E2, 0xE31F8F57, 0x6655AB2A, 0xB2EB2807, 0x2FB5C203, 0x86C57B9A, 0xD33708A5, 0x302887F2, 0x23BFA5B2, 0x02036ABA, 0xED16825C, 0x8ACF1C2B, 0xA779B492, 0xF307F2F0, 0x4E69E2A1, 0x65DAF4CD, 0x0605BED5, 0xD134621F, 0xC4A6FE8A, 0x342E539D, 0xA2F355A0, 0x058AE132, 0xA4F6EB75, 0x0B83EC39, 0x4060EFAA, 0x5E719F06, 0xBD6E1051, 0x3E218AF9, 0x96DD063D, 0xDD3E05AE, 0x4DE6BD46, 0x91548DB5, 0x71C45D05, 0x0406D46F, 0x605015FF, 0x1998FB24, 0xD6BDE997, 0x894043CC, 0x67D99E77, 0xB0E842BD, 0x07898B88, 0xE7195B38, 0x79C8EEDB, 0xA17C0A47, 0x7C420FE9, 0xF8841EC9, 0x00000000, 0x09808683, 0x322BED48, 0x1E1170AC, 0x6C5A724E, 0xFD0EFFFB, 0x0F853856, 0x3DAED51E, 0x362D3927, 0x0A0FD964, 0x685CA621, 0x9B5B54D1, 0x24362E3A, 0x0C0A67B1, 0x9357E70F, 0xB4EE96D2, 0x1B9B919E, 0x80C0C54F, 0x61DC20A2, 0x5A774B69, 0x1C121A16, 0xE293BA0A, 0xC0A02AE5, 0x3C22E043, 0x121B171D, 0x0E090D0B, 0xF28BC7AD, 0x2DB6A8B9, 0x141EA9C8, 0x57F11985, 0xAF75074C, 0xEE99DDBB, 0xA37F60FD, 0xF701269F, 0x5C72F5BC, 0x44663BC5, 0x5BFB7E34, 0x8B432976, 0xCB23C6DC, 0xB6EDFC68, 0xB8E4F163, 0xD731DCCA, 0x42638510, 0x13972240, 0x84C61120, 0x854A247D, 0xD2BB3DF8, 0xAEF93211, 0xC729A16D, 0x1D9E2F4B, 0xDCB230F3, 0x0D8652EC, 0x77C1E3D0, 0x2BB3166C, 0xA970B999, 0x119448FA, 0x47E96422, 0xA8FC8CC4, 0xA0F03F1A, 0x567D2CD8, 0x223390EF, 0x87494EC7, 0xD938D1C1, 0x8CCAA2FE, 0x98D40B36, 0xA6F581CF, 0xA57ADE28, 0xDAB78E26, 0x3FADBFA4, 0x2C3A9DE4, 0x5078920D, 0x6A5FCC9B, 0x547E4662, 0xF68D13C2, 0x90D8B8E8, 0x2E39F75E, 0x82C3AFF5, 0x9F5D80BE, 0x69D0937C, 0x6FD52DA9, 0xCF2512B3, 0xC8AC993B, 0x10187DA7, 0xE89C636E, 0xDB3BBB7B, 0xCD267809, 0x6E5918F4, 0xEC9AB701, 0x834F9AA8, 0xE6956E65, 0xAAFFE67E, 0x21BCCF08, 0xEF15E8E6, 0xBAE79BD9, 0x4A6F36CE, 0xEA9F09D4, 0x29B07CD6, 0x31A4B2AF, 0x2A3F2331, 0xC6A59430, 0x35A266C0, 0x744EBC37, 0xFC82CAA6, 0xE090D0B0, 0x33A7D815, 0xF104984A, 0x41ECDAF7, 0x7FCD500E, 0x1791F62F, 0x764DD68D, 0x43EFB04D, 0xCCAA4D54, 0xE49604DF, 0x9ED1B5E3, 0x4C6A881B, 0xC12C1FB8, 0x4665517F, 0x9D5EEA04, 0x018C355D, 0xFA877473, 0xFB0B412E, 0xB3671D5A, 0x92DBD252, 0xE9105633, 0x6DD64713, 0x9AD7618C, 0x37A10C7A, 0x59F8148E, 0xEB133C89, 0xCEA927EE, 0xB761C935, 0xE11CE5ED, 0x7A47B13C, 0x9CD2DF59, 0x55F2733F, 0x1814CE79, 0x73C737BF, 0x53F7CDEA, 0x5FFDAA5B, 0xDF3D6F14, 0x7844DB86, 0xCAAFF381, 0xB968C43E, 0x3824342C, 0xC2A3405F, 0x161DC372, 0xBCE2250C, 0x283C498B, 0xFF0D9541, 0x39A80171, 0x080CB3DE, 0xD8B4E49C, 0x6456C190, 0x7BCB8461, 0xD532B670, 0x486C5C74, 0xD0B85742, 0x5051F4A7, 0x537E4165, 0xC31A17A4, 0x963A275E, 0xCB3BAB6B, 0xF11F9D45, 0xABACFA58, 0x934BE303, 0x552030FA, 0xF6AD766D, 0x9188CC76, 0x25F5024C, 0xFC4FE5D7, 0xD7C52ACB, 0x80263544, 0x8FB562A3, 0x49DEB15A, 0x6725BA1B, 0x9845EA0E, 0xE15DFEC0, 0x02C32F75, 0x12814CF0, 0xA38D4697, 0xC66BD3F9, 0xE7038F5F, 0x9515929C, 0xEBBF6D7A, 0xDA955259, 0x2DD4BE83, 0xD3587421, 0x2949E069, 0x448EC9C8, 0x6A75C289, 0x78F48E79, 0x6B99583E, 0xDD27B971, 0xB6BEE14F, 0x17F088AD, 0x66C920AC, 0xB47DCE3A, 0x1863DF4A, 0x82E51A31, 0x60975133, 0x4562537F, 0xE0B16477, 0x84BB6BAE, 0x1CFE81A0, 0x94F9082B, 0x58704868, 0x198F45FD, 0x8794DE6C, 0xB7527BF8, 0x23AB73D3, 0xE2724B02, 0x57E31F8F, 0x2A6655AB, 0x07B2EB28, 0x032FB5C2, 0x9A86C57B, 0xA5D33708, 0xF2302887, 0xB223BFA5, 0xBA02036A, 0x5CED1682, 0x2B8ACF1C, 0x92A779B4, 0xF0F307F2, 0xA14E69E2, 0xCD65DAF4, 0xD50605BE, 0x1FD13462, 0x8AC4A6FE, 0x9D342E53, 0xA0A2F355, 0x32058AE1, 0x75A4F6EB, 0x390B83EC, 0xAA4060EF, 0x065E719F, 0x51BD6E10, 0xF93E218A, 0x3D96DD06, 0xAEDD3E05, 0x464DE6BD, 0xB591548D, 0x0571C45D, 0x6F0406D4, 0xFF605015, 0x241998FB, 0x97D6BDE9, 0xCC894043, 0x7767D99E, 0xBDB0E842, 0x8807898B, 0x38E7195B, 0xDB79C8EE, 0x47A17C0A, 0xE97C420F, 0xC9F8841E, 0x00000000, 0x83098086, 0x48322BED, 0xAC1E1170, 0x4E6C5A72, 0xFBFD0EFF, 0x560F8538, 0x1E3DAED5, 0x27362D39, 0x640A0FD9, 0x21685CA6, 0xD19B5B54, 0x3A24362E, 0xB10C0A67, 0x0F9357E7, 0xD2B4EE96, 0x9E1B9B91, 0x4F80C0C5, 0xA261DC20, 0x695A774B, 0x161C121A, 0x0AE293BA, 0xE5C0A02A, 0x433C22E0, 0x1D121B17, 0x0B0E090D, 0xADF28BC7, 0xB92DB6A8, 0xC8141EA9, 0x8557F119, 0x4CAF7507, 0xBBEE99DD, 0xFDA37F60, 0x9FF70126, 0xBC5C72F5, 0xC544663B, 0x345BFB7E, 0x768B4329, 0xDCCB23C6, 0x68B6EDFC, 0x63B8E4F1, 0xCAD731DC, 0x10426385, 0x40139722, 0x2084C611, 0x7D854A24, 0xF8D2BB3D, 0x11AEF932, 0x6DC729A1, 0x4B1D9E2F, 0xF3DCB230, 0xEC0D8652, 0xD077C1E3, 0x6C2BB316, 0x99A970B9, 0xFA119448, 0x2247E964, 0xC4A8FC8C, 0x1AA0F03F, 0xD8567D2C, 0xEF223390, 0xC787494E, 0xC1D938D1, 0xFE8CCAA2, 0x3698D40B, 0xCFA6F581, 0x28A57ADE, 0x26DAB78E, 0xA43FADBF, 0xE42C3A9D, 0x0D507892, 0x9B6A5FCC, 0x62547E46, 0xC2F68D13, 0xE890D8B8, 0x5E2E39F7, 0xF582C3AF, 0xBE9F5D80, 0x7C69D093, 0xA96FD52D, 0xB3CF2512, 0x3BC8AC99, 0xA710187D, 0x6EE89C63, 0x7BDB3BBB, 0x09CD2678, 0xF46E5918, 0x01EC9AB7, 0xA8834F9A, 0x65E6956E, 0x7EAAFFE6, 0x0821BCCF, 0xE6EF15E8, 0xD9BAE79B, 0xCE4A6F36, 0xD4EA9F09, 0xD629B07C, 0xAF31A4B2, 0x312A3F23, 0x30C6A594, 0xC035A266, 0x37744EBC, 0xA6FC82CA, 0xB0E090D0, 0x1533A7D8, 0x4AF10498, 0xF741ECDA, 0x0E7FCD50, 0x2F1791F6, 0x8D764DD6, 0x4D43EFB0, 0x54CCAA4D, 0xDFE49604, 0xE39ED1B5, 0x1B4C6A88, 0xB8C12C1F, 0x7F466551, 0x049D5EEA, 0x5D018C35, 0x73FA8774, 0x2EFB0B41, 0x5AB3671D, 0x5292DBD2, 0x33E91056, 0x136DD647, 0x8C9AD761, 0x7A37A10C, 0x8E59F814, 0x89EB133C, 0xEECEA927, 0x35B761C9, 0xEDE11CE5, 0x3C7A47B1, 0x599CD2DF, 0x3F55F273, 0x791814CE, 0xBF73C737, 0xEA53F7CD, 0x5B5FFDAA, 0x14DF3D6F, 0x867844DB, 0x81CAAFF3, 0x3EB968C4, 0x2C382434, 0x5FC2A340, 0x72161DC3, 0x0CBCE225, 0x8B283C49, 0x41FF0D95, 0x7139A801, 0xDE080CB3, 0x9CD8B4E4, 0x906456C1, 0x617BCB84, 0x70D532B6, 0x74486C5C, 0x42D0B857, 0xA75051F4, 0x65537E41, 0xA4C31A17, 0x5E963A27, 0x6BCB3BAB, 0x45F11F9D, 0x58ABACFA, 0x03934BE3, 0xFA552030, 0x6DF6AD76, 0x769188CC, 0x4C25F502, 0xD7FC4FE5, 0xCBD7C52A, 0x44802635, 0xA38FB562, 0x5A49DEB1, 0x1B6725BA, 0x0E9845EA, 0xC0E15DFE, 0x7502C32F, 0xF012814C, 0x97A38D46, 0xF9C66BD3, 0x5FE7038F, 0x9C951592, 0x7AEBBF6D, 0x59DA9552, 0x832DD4BE, 0x21D35874, 0x692949E0, 0xC8448EC9, 0x896A75C2, 0x7978F48E, 0x3E6B9958, 0x71DD27B9, 0x4FB6BEE1, 0xAD17F088, 0xAC66C920, 0x3AB47DCE, 0x4A1863DF, 0x3182E51A, 0x33609751, 0x7F456253, 0x77E0B164, 0xAE84BB6B, 0xA01CFE81, 0x2B94F908, 0x68587048, 0xFD198F45, 0x6C8794DE, 0xF8B7527B, 0xD323AB73, 0x02E2724B, 0x8F57E31F, 0xAB2A6655, 0x2807B2EB, 0xC2032FB5, 0x7B9A86C5, 0x08A5D337, 0x87F23028, 0xA5B223BF, 0x6ABA0203, 0x825CED16, 0x1C2B8ACF, 0xB492A779, 0xF2F0F307, 0xE2A14E69, 0xF4CD65DA, 0xBED50605, 0x621FD134, 0xFE8AC4A6, 0x539D342E, 0x55A0A2F3, 0xE132058A, 0xEB75A4F6, 0xEC390B83, 0xEFAA4060, 0x9F065E71, 0x1051BD6E, 0x8AF93E21, 0x063D96DD, 0x05AEDD3E, 0xBD464DE6, 0x8DB59154, 0x5D0571C4, 0xD46F0406, 0x15FF6050, 0xFB241998, 0xE997D6BD, 0x43CC8940, 0x9E7767D9, 0x42BDB0E8, 0x8B880789, 0x5B38E719, 0xEEDB79C8, 0x0A47A17C, 0x0FE97C42, 0x1EC9F884, 0x00000000, 0x86830980, 0xED48322B, 0x70AC1E11, 0x724E6C5A, 0xFFFBFD0E, 0x38560F85, 0xD51E3DAE, 0x3927362D, 0xD9640A0F, 0xA621685C, 0x54D19B5B, 0x2E3A2436, 0x67B10C0A, 0xE70F9357, 0x96D2B4EE, 0x919E1B9B, 0xC54F80C0, 0x20A261DC, 0x4B695A77, 0x1A161C12, 0xBA0AE293, 0x2AE5C0A0, 0xE0433C22, 0x171D121B, 0x0D0B0E09, 0xC7ADF28B, 0xA8B92DB6, 0xA9C8141E, 0x198557F1, 0x074CAF75, 0xDDBBEE99, 0x60FDA37F, 0x269FF701, 0xF5BC5C72, 0x3BC54466, 0x7E345BFB, 0x29768B43, 0xC6DCCB23, 0xFC68B6ED, 0xF163B8E4, 0xDCCAD731, 0x85104263, 0x22401397, 0x112084C6, 0x247D854A, 0x3DF8D2BB, 0x3211AEF9, 0xA16DC729, 0x2F4B1D9E, 0x30F3DCB2, 0x52EC0D86, 0xE3D077C1, 0x166C2BB3, 0xB999A970, 0x48FA1194, 0x642247E9, 0x8CC4A8FC, 0x3F1AA0F0, 0x2CD8567D, 0x90EF2233, 0x4EC78749, 0xD1C1D938, 0xA2FE8CCA, 0x0B3698D4, 0x81CFA6F5, 0xDE28A57A, 0x8E26DAB7, 0xBFA43FAD, 0x9DE42C3A, 0x920D5078, 0xCC9B6A5F, 0x4662547E, 0x13C2F68D, 0xB8E890D8, 0xF75E2E39, 0xAFF582C3, 0x80BE9F5D, 0x937C69D0, 0x2DA96FD5, 0x12B3CF25, 0x993BC8AC, 0x7DA71018, 0x636EE89C, 0xBB7BDB3B, 0x7809CD26, 0x18F46E59, 0xB701EC9A, 0x9AA8834F, 0x6E65E695, 0xE67EAAFF, 0xCF0821BC, 0xE8E6EF15, 0x9BD9BAE7, 0x36CE4A6F, 0x09D4EA9F, 0x7CD629B0, 0xB2AF31A4, 0x23312A3F, 0x9430C6A5, 0x66C035A2, 0xBC37744E, 0xCAA6FC82, 0xD0B0E090, 0xD81533A7, 0x984AF104, 0xDAF741EC, 0x500E7FCD, 0xF62F1791, 0xD68D764D, 0xB04D43EF, 0x4D54CCAA, 0x04DFE496, 0xB5E39ED1, 0x881B4C6A, 0x1FB8C12C, 0x517F4665, 0xEA049D5E, 0x355D018C, 0x7473FA87, 0x412EFB0B, 0x1D5AB367, 0xD25292DB, 0x5633E910, 0x47136DD6, 0x618C9AD7, 0x0C7A37A1, 0x148E59F8, 0x3C89EB13, 0x27EECEA9, 0xC935B761, 0xE5EDE11C, 0xB13C7A47, 0xDF599CD2, 0x733F55F2, 0xCE791814, 0x37BF73C7, 0xCDEA53F7, 0xAA5B5FFD, 0x6F14DF3D, 0xDB867844, 0xF381CAAF, 0xC43EB968, 0x342C3824, 0x405FC2A3, 0xC372161D, 0x250CBCE2, 0x498B283C, 0x9541FF0D, 0x017139A8, 0xB3DE080C, 0xE49CD8B4, 0xC1906456, 0x84617BCB, 0xB670D532, 0x5C74486C, 0x5742D0B8, 0xF4A75051, 0x4165537E, 0x17A4C31A, 0x275E963A, 0xAB6BCB3B, 0x9D45F11F, 0xFA58ABAC, 0xE303934B, 0x30FA5520, 0x766DF6AD, 0xCC769188, 0x024C25F5, 0xE5D7FC4F, 0x2ACBD7C5, 0x35448026, 0x62A38FB5, 0xB15A49DE, 0xBA1B6725, 0xEA0E9845, 0xFEC0E15D, 0x2F7502C3, 0x4CF01281, 0x4697A38D, 0xD3F9C66B, 0x8F5FE703, 0x929C9515, 0x6D7AEBBF, 0x5259DA95, 0xBE832DD4, 0x7421D358, 0xE0692949, 0xC9C8448E, 0xC2896A75, 0x8E7978F4, 0x583E6B99, 0xB971DD27, 0xE14FB6BE, 0x88AD17F0, 0x20AC66C9, 0xCE3AB47D, 0xDF4A1863, 0x1A3182E5, 0x51336097, 0x537F4562, 0x6477E0B1, 0x6BAE84BB, 0x81A01CFE, 0x082B94F9, 0x48685870, 0x45FD198F, 0xDE6C8794, 0x7BF8B752, 0x73D323AB, 0x4B02E272, 0x1F8F57E3, 0x55AB2A66, 0xEB2807B2, 0xB5C2032F, 0xC57B9A86, 0x3708A5D3, 0x2887F230, 0xBFA5B223, 0x036ABA02, 0x16825CED, 0xCF1C2B8A, 0x79B492A7, 0x07F2F0F3, 0x69E2A14E, 0xDAF4CD65, 0x05BED506, 0x34621FD1, 0xA6FE8AC4, 0x2E539D34, 0xF355A0A2, 0x8AE13205, 0xF6EB75A4, 0x83EC390B, 0x60EFAA40, 0x719F065E, 0x6E1051BD, 0x218AF93E, 0xDD063D96, 0x3E05AEDD, 0xE6BD464D, 0x548DB591, 0xC45D0571, 0x06D46F04, 0x5015FF60, 0x98FB2419, 0xBDE997D6, 0x4043CC89, 0xD99E7767, 0xE842BDB0, 0x898B8807, 0x195B38E7, 0xC8EEDB79, 0x7C0A47A1, 0x420FE97C, 0x841EC9F8, 0x00000000, 0x80868309, 0x2BED4832, 0x1170AC1E, 0x5A724E6C, 0x0EFFFBFD, 0x8538560F, 0xAED51E3D, 0x2D392736, 0x0FD9640A, 0x5CA62168, 0x5B54D19B, 0x362E3A24, 0x0A67B10C, 0x57E70F93, 0xEE96D2B4, 0x9B919E1B, 0xC0C54F80, 0xDC20A261, 0x774B695A, 0x121A161C, 0x93BA0AE2, 0xA02AE5C0, 0x22E0433C, 0x1B171D12, 0x090D0B0E, 0x8BC7ADF2, 0xB6A8B92D, 0x1EA9C814, 0xF1198557, 0x75074CAF, 0x99DDBBEE, 0x7F60FDA3, 0x01269FF7, 0x72F5BC5C, 0x663BC544, 0xFB7E345B, 0x4329768B, 0x23C6DCCB, 0xEDFC68B6, 0xE4F163B8, 0x31DCCAD7, 0x63851042, 0x97224013, 0xC6112084, 0x4A247D85, 0xBB3DF8D2, 0xF93211AE, 0x29A16DC7, 0x9E2F4B1D, 0xB230F3DC, 0x8652EC0D, 0xC1E3D077, 0xB3166C2B, 0x70B999A9, 0x9448FA11, 0xE9642247, 0xFC8CC4A8, 0xF03F1AA0, 0x7D2CD856, 0x3390EF22, 0x494EC787, 0x38D1C1D9, 0xCAA2FE8C, 0xD40B3698, 0xF581CFA6, 0x7ADE28A5, 0xB78E26DA, 0xADBFA43F, 0x3A9DE42C, 0x78920D50, 0x5FCC9B6A, 0x7E466254, 0x8D13C2F6, 0xD8B8E890, 0x39F75E2E, 0xC3AFF582, 0x5D80BE9F, 0xD0937C69, 0xD52DA96F, 0x2512B3CF, 0xAC993BC8, 0x187DA710, 0x9C636EE8, 0x3BBB7BDB, 0x267809CD, 0x5918F46E, 0x9AB701EC, 0x4F9AA883, 0x956E65E6, 0xFFE67EAA, 0xBCCF0821, 0x15E8E6EF, 0xE79BD9BA, 0x6F36CE4A, 0x9F09D4EA, 0xB07CD629, 0xA4B2AF31, 0x3F23312A, 0xA59430C6, 0xA266C035, 0x4EBC3774, 0x82CAA6FC, 0x90D0B0E0, 0xA7D81533, 0x04984AF1, 0xECDAF741, 0xCD500E7F, 0x91F62F17, 0x4DD68D76, 0xEFB04D43, 0xAA4D54CC, 0x9604DFE4, 0xD1B5E39E, 0x6A881B4C, 0x2C1FB8C1, 0x65517F46, 0x5EEA049D, 0x8C355D01, 0x877473FA, 0x0B412EFB, 0x671D5AB3, 0xDBD25292, 0x105633E9, 0xD647136D, 0xD7618C9A, 0xA10C7A37, 0xF8148E59, 0x133C89EB, 0xA927EECE, 0x61C935B7, 0x1CE5EDE1, 0x47B13C7A, 0xD2DF599C, 0xF2733F55, 0x14CE7918, 0xC737BF73, 0xF7CDEA53, 0xFDAA5B5F, 0x3D6F14DF, 0x44DB8678, 0xAFF381CA, 0x68C43EB9, 0x24342C38, 0xA3405FC2, 0x1DC37216, 0xE2250CBC, 0x3C498B28, 0x0D9541FF, 0xA8017139, 0x0CB3DE08, 0xB4E49CD8, 0x56C19064, 0xCB84617B, 0x32B670D5, 0x6C5C7448, 0xB85742D0 }; /* * AES Encryption */ void aes_encrypt_n(const byte in[], byte out[], size_t blocks, const MemoryRegion<u32bit>& EK, const MemoryRegion<byte>& ME) { const size_t BLOCK_SIZE = 16; const u32bit* TE0 = TE; const u32bit* TE1 = TE + 256; const u32bit* TE2 = TE + 512; const u32bit* TE3 = TE + 768; for(size_t i = 0; i != blocks; ++i) { u32bit T0 = load_be<u32bit>(in, 0) ^ EK[0]; u32bit T1 = load_be<u32bit>(in, 1) ^ EK[1]; u32bit T2 = load_be<u32bit>(in, 2) ^ EK[2]; u32bit T3 = load_be<u32bit>(in, 3) ^ EK[3]; /* Use only the first 256 entries of the TE table and do the * rotations directly in the code. This reduces the number of * cache lines potentially used in the first round from 64 to 16 * (assuming a typical 64 byte cache line), which makes timing * attacks a little harder; the first round is particularly * vulnerable. */ u32bit B0 = TE[get_byte(0, T0)] ^ rotate_right(TE[get_byte(1, T1)], 8) ^ rotate_right(TE[get_byte(2, T2)], 16) ^ rotate_right(TE[get_byte(3, T3)], 24) ^ EK[4]; u32bit B1 = TE[get_byte(0, T1)] ^ rotate_right(TE[get_byte(1, T2)], 8) ^ rotate_right(TE[get_byte(2, T3)], 16) ^ rotate_right(TE[get_byte(3, T0)], 24) ^ EK[5]; u32bit B2 = TE[get_byte(0, T2)] ^ rotate_right(TE[get_byte(1, T3)], 8) ^ rotate_right(TE[get_byte(2, T0)], 16) ^ rotate_right(TE[get_byte(3, T1)], 24) ^ EK[6]; u32bit B3 = TE[get_byte(0, T3)] ^ rotate_right(TE[get_byte(1, T0)], 8) ^ rotate_right(TE[get_byte(2, T1)], 16) ^ rotate_right(TE[get_byte(3, T2)], 24) ^ EK[7]; for(size_t r = 2*4; r < EK.size(); r += 2*4) { T0 = TE0[get_byte(0, B0)] ^ TE1[get_byte(1, B1)] ^ TE2[get_byte(2, B2)] ^ TE3[get_byte(3, B3)] ^ EK[r]; T1 = TE0[get_byte(0, B1)] ^ TE1[get_byte(1, B2)] ^ TE2[get_byte(2, B3)] ^ TE3[get_byte(3, B0)] ^ EK[r+1]; T2 = TE0[get_byte(0, B2)] ^ TE1[get_byte(1, B3)] ^ TE2[get_byte(2, B0)] ^ TE3[get_byte(3, B1)] ^ EK[r+2]; T3 = TE0[get_byte(0, B3)] ^ TE1[get_byte(1, B0)] ^ TE2[get_byte(2, B1)] ^ TE3[get_byte(3, B2)] ^ EK[r+3]; B0 = TE0[get_byte(0, T0)] ^ TE1[get_byte(1, T1)] ^ TE2[get_byte(2, T2)] ^ TE3[get_byte(3, T3)] ^ EK[r+4]; B1 = TE0[get_byte(0, T1)] ^ TE1[get_byte(1, T2)] ^ TE2[get_byte(2, T3)] ^ TE3[get_byte(3, T0)] ^ EK[r+5]; B2 = TE0[get_byte(0, T2)] ^ TE1[get_byte(1, T3)] ^ TE2[get_byte(2, T0)] ^ TE3[get_byte(3, T1)] ^ EK[r+6]; B3 = TE0[get_byte(0, T3)] ^ TE1[get_byte(1, T0)] ^ TE2[get_byte(2, T1)] ^ TE3[get_byte(3, T2)] ^ EK[r+7]; } /* Joseph Bonneau and Ilya Mironov's paper "Cache-Collision Timing Attacks Against AES" describes an attack that can recover AES keys with as few as 2**13 samples. http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.88.4753 They recommend using a byte-wide table, which still allows an attack but increases the samples required from 2**13 to 2**25: """In addition to OpenSSL v. 0.9.8.(a), which was used in our experiments, the AES implementations of Crypto++ 5.2.1 and LibTomCrypt 1.09 use the original Rijndael C implementation with very few changes and are highly vulnerable. The AES implementations in libgcrypt v. 1.2.2 and Botan v. 1.4.2 are also vulnerable, but use a smaller byte-wide final table which lessens the effectiveness of the attacks.""" */ out[ 0] = SE[get_byte(0, B0)] ^ ME[0]; out[ 1] = SE[get_byte(1, B1)] ^ ME[1]; out[ 2] = SE[get_byte(2, B2)] ^ ME[2]; out[ 3] = SE[get_byte(3, B3)] ^ ME[3]; out[ 4] = SE[get_byte(0, B1)] ^ ME[4]; out[ 5] = SE[get_byte(1, B2)] ^ ME[5]; out[ 6] = SE[get_byte(2, B3)] ^ ME[6]; out[ 7] = SE[get_byte(3, B0)] ^ ME[7]; out[ 8] = SE[get_byte(0, B2)] ^ ME[8]; out[ 9] = SE[get_byte(1, B3)] ^ ME[9]; out[10] = SE[get_byte(2, B0)] ^ ME[10]; out[11] = SE[get_byte(3, B1)] ^ ME[11]; out[12] = SE[get_byte(0, B3)] ^ ME[12]; out[13] = SE[get_byte(1, B0)] ^ ME[13]; out[14] = SE[get_byte(2, B1)] ^ ME[14]; out[15] = SE[get_byte(3, B2)] ^ ME[15]; in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * AES Decryption */ void aes_decrypt_n(const byte in[], byte out[], size_t blocks, const MemoryRegion<u32bit>& DK, const MemoryRegion<byte>& MD) { const size_t BLOCK_SIZE = 16; const u32bit* TD0 = TD; const u32bit* TD1 = TD + 256; const u32bit* TD2 = TD + 512; const u32bit* TD3 = TD + 768; for(size_t i = 0; i != blocks; ++i) { u32bit T0 = load_be<u32bit>(in, 0) ^ DK[0]; u32bit T1 = load_be<u32bit>(in, 1) ^ DK[1]; u32bit T2 = load_be<u32bit>(in, 2) ^ DK[2]; u32bit T3 = load_be<u32bit>(in, 3) ^ DK[3]; u32bit B0 = TD[get_byte(0, T0)] ^ rotate_right(TD[get_byte(1, T3)], 8) ^ rotate_right(TD[get_byte(2, T2)], 16) ^ rotate_right(TD[get_byte(3, T1)], 24) ^ DK[4]; u32bit B1 = TD[get_byte(0, T1)] ^ rotate_right(TD[get_byte(1, T0)], 8) ^ rotate_right(TD[get_byte(2, T3)], 16) ^ rotate_right(TD[get_byte(3, T2)], 24) ^ DK[5]; u32bit B2 = TD[get_byte(0, T2)] ^ rotate_right(TD[get_byte(1, T1)], 8) ^ rotate_right(TD[get_byte(2, T0)], 16) ^ rotate_right(TD[get_byte(3, T3)], 24) ^ DK[6]; u32bit B3 = TD[get_byte(0, T3)] ^ rotate_right(TD[get_byte(1, T2)], 8) ^ rotate_right(TD[get_byte(2, T1)], 16) ^ rotate_right(TD[get_byte(3, T0)], 24) ^ DK[7]; for(size_t r = 2*4; r < DK.size(); r += 2*4) { T0 = TD0[get_byte(0, B0)] ^ TD1[get_byte(1, B3)] ^ TD2[get_byte(2, B2)] ^ TD3[get_byte(3, B1)] ^ DK[r]; T1 = TD0[get_byte(0, B1)] ^ TD1[get_byte(1, B0)] ^ TD2[get_byte(2, B3)] ^ TD3[get_byte(3, B2)] ^ DK[r+1]; T2 = TD0[get_byte(0, B2)] ^ TD1[get_byte(1, B1)] ^ TD2[get_byte(2, B0)] ^ TD3[get_byte(3, B3)] ^ DK[r+2]; T3 = TD0[get_byte(0, B3)] ^ TD1[get_byte(1, B2)] ^ TD2[get_byte(2, B1)] ^ TD3[get_byte(3, B0)] ^ DK[r+3]; B0 = TD0[get_byte(0, T0)] ^ TD1[get_byte(1, T3)] ^ TD2[get_byte(2, T2)] ^ TD3[get_byte(3, T1)] ^ DK[r+4]; B1 = TD0[get_byte(0, T1)] ^ TD1[get_byte(1, T0)] ^ TD2[get_byte(2, T3)] ^ TD3[get_byte(3, T2)] ^ DK[r+5]; B2 = TD0[get_byte(0, T2)] ^ TD1[get_byte(1, T1)] ^ TD2[get_byte(2, T0)] ^ TD3[get_byte(3, T3)] ^ DK[r+6]; B3 = TD0[get_byte(0, T3)] ^ TD1[get_byte(1, T2)] ^ TD2[get_byte(2, T1)] ^ TD3[get_byte(3, T0)] ^ DK[r+7]; } out[ 0] = SD[get_byte(0, B0)] ^ MD[0]; out[ 1] = SD[get_byte(1, B3)] ^ MD[1]; out[ 2] = SD[get_byte(2, B2)] ^ MD[2]; out[ 3] = SD[get_byte(3, B1)] ^ MD[3]; out[ 4] = SD[get_byte(0, B1)] ^ MD[4]; out[ 5] = SD[get_byte(1, B0)] ^ MD[5]; out[ 6] = SD[get_byte(2, B3)] ^ MD[6]; out[ 7] = SD[get_byte(3, B2)] ^ MD[7]; out[ 8] = SD[get_byte(0, B2)] ^ MD[8]; out[ 9] = SD[get_byte(1, B1)] ^ MD[9]; out[10] = SD[get_byte(2, B0)] ^ MD[10]; out[11] = SD[get_byte(3, B3)] ^ MD[11]; out[12] = SD[get_byte(0, B3)] ^ MD[12]; out[13] = SD[get_byte(1, B2)] ^ MD[13]; out[14] = SD[get_byte(2, B1)] ^ MD[14]; out[15] = SD[get_byte(3, B0)] ^ MD[15]; in += BLOCK_SIZE; out += BLOCK_SIZE; } } void aes_key_schedule(const byte key[], size_t length, MemoryRegion<u32bit>& EK, MemoryRegion<u32bit>& DK, MemoryRegion<byte>& ME, MemoryRegion<byte>& MD) { static const u32bit RC[10] = { 0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000, 0x40000000, 0x80000000, 0x1B000000, 0x36000000 }; const size_t rounds = (length / 4) + 6; SecureVector<u32bit> XEK(length + 32), XDK(length + 32); const size_t X = length / 4; for(size_t i = 0; i != X; ++i) XEK[i] = load_be<u32bit>(key, i); for(size_t i = X; i < 4*(rounds+1); i += X) { XEK[i] = XEK[i-X] ^ RC[(i-X)/X] ^ make_u32bit(SE[get_byte(1, XEK[i-1])], SE[get_byte(2, XEK[i-1])], SE[get_byte(3, XEK[i-1])], SE[get_byte(0, XEK[i-1])]); for(size_t j = 1; j != X; ++j) { XEK[i+j] = XEK[i+j-X]; if(X == 8 && j == 4) XEK[i+j] ^= make_u32bit(SE[get_byte(0, XEK[i+j-1])], SE[get_byte(1, XEK[i+j-1])], SE[get_byte(2, XEK[i+j-1])], SE[get_byte(3, XEK[i+j-1])]); else XEK[i+j] ^= XEK[i+j-1]; } } for(size_t i = 0; i != 4*(rounds+1); i += 4) { XDK[i ] = XEK[4*rounds-i ]; XDK[i+1] = XEK[4*rounds-i+1]; XDK[i+2] = XEK[4*rounds-i+2]; XDK[i+3] = XEK[4*rounds-i+3]; } for(size_t i = 4; i != length + 24; ++i) XDK[i] = TD[SE[get_byte(0, XDK[i])] + 0] ^ TD[SE[get_byte(1, XDK[i])] + 256] ^ TD[SE[get_byte(2, XDK[i])] + 512] ^ TD[SE[get_byte(3, XDK[i])] + 768]; for(size_t i = 0; i != 4; ++i) { store_be(XEK[i+4*rounds], &ME[4*i]); store_be(XEK[i], &MD[4*i]); } EK.resize(length + 24); DK.resize(length + 24); copy_mem(&EK[0], &XEK[0], EK.size()); copy_mem(&DK[0], &XDK[0], DK.size()); } } void AES_128::encrypt_n(const byte in[], byte out[], size_t blocks) const { aes_encrypt_n(in, out, blocks, EK, ME); } void AES_128::decrypt_n(const byte in[], byte out[], size_t blocks) const { aes_decrypt_n(in, out, blocks, DK, MD); } void AES_128::key_schedule(const byte key[], size_t length) { aes_key_schedule(key, length, EK, DK, ME, MD); } void AES_128::clear() { zeroise(EK); zeroise(DK); zeroise(ME); zeroise(MD); } void AES_192::encrypt_n(const byte in[], byte out[], size_t blocks) const { aes_encrypt_n(in, out, blocks, EK, ME); } void AES_192::decrypt_n(const byte in[], byte out[], size_t blocks) const { aes_decrypt_n(in, out, blocks, DK, MD); } void AES_192::key_schedule(const byte key[], size_t length) { aes_key_schedule(key, length, EK, DK, ME, MD); } void AES_192::clear() { zeroise(EK); zeroise(DK); zeroise(ME); zeroise(MD); } void AES_256::encrypt_n(const byte in[], byte out[], size_t blocks) const { aes_encrypt_n(in, out, blocks, EK, ME); } void AES_256::decrypt_n(const byte in[], byte out[], size_t blocks) const { aes_decrypt_n(in, out, blocks, DK, MD); } void AES_256::key_schedule(const byte key[], size_t length) { aes_key_schedule(key, length, EK, DK, ME, MD); } void AES_256::clear() { zeroise(EK); zeroise(DK); zeroise(ME); zeroise(MD); } } /* * S-Box and P-Box Tables for Blowfish * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { const u32bit Blowfish::P_INIT[18] = { 0x243F6A88, 0x85A308D3, 0x13198A2E, 0x03707344, 0xA4093822, 0x299F31D0, 0x082EFA98, 0xEC4E6C89, 0x452821E6, 0x38D01377, 0xBE5466CF, 0x34E90C6C, 0xC0AC29B7, 0xC97C50DD, 0x3F84D5B5, 0xB5470917, 0x9216D5D9, 0x8979FB1B }; const u32bit Blowfish::S_INIT[1024] = { 0xD1310BA6, 0x98DFB5AC, 0x2FFD72DB, 0xD01ADFB7, 0xB8E1AFED, 0x6A267E96, 0xBA7C9045, 0xF12C7F99, 0x24A19947, 0xB3916CF7, 0x0801F2E2, 0x858EFC16, 0x636920D8, 0x71574E69, 0xA458FEA3, 0xF4933D7E, 0x0D95748F, 0x728EB658, 0x718BCD58, 0x82154AEE, 0x7B54A41D, 0xC25A59B5, 0x9C30D539, 0x2AF26013, 0xC5D1B023, 0x286085F0, 0xCA417918, 0xB8DB38EF, 0x8E79DCB0, 0x603A180E, 0x6C9E0E8B, 0xB01E8A3E, 0xD71577C1, 0xBD314B27, 0x78AF2FDA, 0x55605C60, 0xE65525F3, 0xAA55AB94, 0x57489862, 0x63E81440, 0x55CA396A, 0x2AAB10B6, 0xB4CC5C34, 0x1141E8CE, 0xA15486AF, 0x7C72E993, 0xB3EE1411, 0x636FBC2A, 0x2BA9C55D, 0x741831F6, 0xCE5C3E16, 0x9B87931E, 0xAFD6BA33, 0x6C24CF5C, 0x7A325381, 0x28958677, 0x3B8F4898, 0x6B4BB9AF, 0xC4BFE81B, 0x66282193, 0x61D809CC, 0xFB21A991, 0x487CAC60, 0x5DEC8032, 0xEF845D5D, 0xE98575B1, 0xDC262302, 0xEB651B88, 0x23893E81, 0xD396ACC5, 0x0F6D6FF3, 0x83F44239, 0x2E0B4482, 0xA4842004, 0x69C8F04A, 0x9E1F9B5E, 0x21C66842, 0xF6E96C9A, 0x670C9C61, 0xABD388F0, 0x6A51A0D2, 0xD8542F68, 0x960FA728, 0xAB5133A3, 0x6EEF0B6C, 0x137A3BE4, 0xBA3BF050, 0x7EFB2A98, 0xA1F1651D, 0x39AF0176, 0x66CA593E, 0x82430E88, 0x8CEE8619, 0x456F9FB4, 0x7D84A5C3, 0x3B8B5EBE, 0xE06F75D8, 0x85C12073, 0x401A449F, 0x56C16AA6, 0x4ED3AA62, 0x363F7706, 0x1BFEDF72, 0x429B023D, 0x37D0D724, 0xD00A1248, 0xDB0FEAD3, 0x49F1C09B, 0x075372C9, 0x80991B7B, 0x25D479D8, 0xF6E8DEF7, 0xE3FE501A, 0xB6794C3B, 0x976CE0BD, 0x04C006BA, 0xC1A94FB6, 0x409F60C4, 0x5E5C9EC2, 0x196A2463, 0x68FB6FAF, 0x3E6C53B5, 0x1339B2EB, 0x3B52EC6F, 0x6DFC511F, 0x9B30952C, 0xCC814544, 0xAF5EBD09, 0xBEE3D004, 0xDE334AFD, 0x660F2807, 0x192E4BB3, 0xC0CBA857, 0x45C8740F, 0xD20B5F39, 0xB9D3FBDB, 0x5579C0BD, 0x1A60320A, 0xD6A100C6, 0x402C7279, 0x679F25FE, 0xFB1FA3CC, 0x8EA5E9F8, 0xDB3222F8, 0x3C7516DF, 0xFD616B15, 0x2F501EC8, 0xAD0552AB, 0x323DB5FA, 0xFD238760, 0x53317B48, 0x3E00DF82, 0x9E5C57BB, 0xCA6F8CA0, 0x1A87562E, 0xDF1769DB, 0xD542A8F6, 0x287EFFC3, 0xAC6732C6, 0x8C4F5573, 0x695B27B0, 0xBBCA58C8, 0xE1FFA35D, 0xB8F011A0, 0x10FA3D98, 0xFD2183B8, 0x4AFCB56C, 0x2DD1D35B, 0x9A53E479, 0xB6F84565, 0xD28E49BC, 0x4BFB9790, 0xE1DDF2DA, 0xA4CB7E33, 0x62FB1341, 0xCEE4C6E8, 0xEF20CADA, 0x36774C01, 0xD07E9EFE, 0x2BF11FB4, 0x95DBDA4D, 0xAE909198, 0xEAAD8E71, 0x6B93D5A0, 0xD08ED1D0, 0xAFC725E0, 0x8E3C5B2F, 0x8E7594B7, 0x8FF6E2FB, 0xF2122B64, 0x8888B812, 0x900DF01C, 0x4FAD5EA0, 0x688FC31C, 0xD1CFF191, 0xB3A8C1AD, 0x2F2F2218, 0xBE0E1777, 0xEA752DFE, 0x8B021FA1, 0xE5A0CC0F, 0xB56F74E8, 0x18ACF3D6, 0xCE89E299, 0xB4A84FE0, 0xFD13E0B7, 0x7CC43B81, 0xD2ADA8D9, 0x165FA266, 0x80957705, 0x93CC7314, 0x211A1477, 0xE6AD2065, 0x77B5FA86, 0xC75442F5, 0xFB9D35CF, 0xEBCDAF0C, 0x7B3E89A0, 0xD6411BD3, 0xAE1E7E49, 0x00250E2D, 0x2071B35E, 0x226800BB, 0x57B8E0AF, 0x2464369B, 0xF009B91E, 0x5563911D, 0x59DFA6AA, 0x78C14389, 0xD95A537F, 0x207D5BA2, 0x02E5B9C5, 0x83260376, 0x6295CFA9, 0x11C81968, 0x4E734A41, 0xB3472DCA, 0x7B14A94A, 0x1B510052, 0x9A532915, 0xD60F573F, 0xBC9BC6E4, 0x2B60A476, 0x81E67400, 0x08BA6FB5, 0x571BE91F, 0xF296EC6B, 0x2A0DD915, 0xB6636521, 0xE7B9F9B6, 0xFF34052E, 0xC5855664, 0x53B02D5D, 0xA99F8FA1, 0x08BA4799, 0x6E85076A, 0x4B7A70E9, 0xB5B32944, 0xDB75092E, 0xC4192623, 0xAD6EA6B0, 0x49A7DF7D, 0x9CEE60B8, 0x8FEDB266, 0xECAA8C71, 0x699A17FF, 0x5664526C, 0xC2B19EE1, 0x193602A5, 0x75094C29, 0xA0591340, 0xE4183A3E, 0x3F54989A, 0x5B429D65, 0x6B8FE4D6, 0x99F73FD6, 0xA1D29C07, 0xEFE830F5, 0x4D2D38E6, 0xF0255DC1, 0x4CDD2086, 0x8470EB26, 0x6382E9C6, 0x021ECC5E, 0x09686B3F, 0x3EBAEFC9, 0x3C971814, 0x6B6A70A1, 0x687F3584, 0x52A0E286, 0xB79C5305, 0xAA500737, 0x3E07841C, 0x7FDEAE5C, 0x8E7D44EC, 0x5716F2B8, 0xB03ADA37, 0xF0500C0D, 0xF01C1F04, 0x0200B3FF, 0xAE0CF51A, 0x3CB574B2, 0x25837A58, 0xDC0921BD, 0xD19113F9, 0x7CA92FF6, 0x94324773, 0x22F54701, 0x3AE5E581, 0x37C2DADC, 0xC8B57634, 0x9AF3DDA7, 0xA9446146, 0x0FD0030E, 0xECC8C73E, 0xA4751E41, 0xE238CD99, 0x3BEA0E2F, 0x3280BBA1, 0x183EB331, 0x4E548B38, 0x4F6DB908, 0x6F420D03, 0xF60A04BF, 0x2CB81290, 0x24977C79, 0x5679B072, 0xBCAF89AF, 0xDE9A771F, 0xD9930810, 0xB38BAE12, 0xDCCF3F2E, 0x5512721F, 0x2E6B7124, 0x501ADDE6, 0x9F84CD87, 0x7A584718, 0x7408DA17, 0xBC9F9ABC, 0xE94B7D8C, 0xEC7AEC3A, 0xDB851DFA, 0x63094366, 0xC464C3D2, 0xEF1C1847, 0x3215D908, 0xDD433B37, 0x24C2BA16, 0x12A14D43, 0x2A65C451, 0x50940002, 0x133AE4DD, 0x71DFF89E, 0x10314E55, 0x81AC77D6, 0x5F11199B, 0x043556F1, 0xD7A3C76B, 0x3C11183B, 0x5924A509, 0xF28FE6ED, 0x97F1FBFA, 0x9EBABF2C, 0x1E153C6E, 0x86E34570, 0xEAE96FB1, 0x860E5E0A, 0x5A3E2AB3, 0x771FE71C, 0x4E3D06FA, 0x2965DCB9, 0x99E71D0F, 0x803E89D6, 0x5266C825, 0x2E4CC978, 0x9C10B36A, 0xC6150EBA, 0x94E2EA78, 0xA5FC3C53, 0x1E0A2DF4, 0xF2F74EA7, 0x361D2B3D, 0x1939260F, 0x19C27960, 0x5223A708, 0xF71312B6, 0xEBADFE6E, 0xEAC31F66, 0xE3BC4595, 0xA67BC883, 0xB17F37D1, 0x018CFF28, 0xC332DDEF, 0xBE6C5AA5, 0x65582185, 0x68AB9802, 0xEECEA50F, 0xDB2F953B, 0x2AEF7DAD, 0x5B6E2F84, 0x1521B628, 0x29076170, 0xECDD4775, 0x619F1510, 0x13CCA830, 0xEB61BD96, 0x0334FE1E, 0xAA0363CF, 0xB5735C90, 0x4C70A239, 0xD59E9E0B, 0xCBAADE14, 0xEECC86BC, 0x60622CA7, 0x9CAB5CAB, 0xB2F3846E, 0x648B1EAF, 0x19BDF0CA, 0xA02369B9, 0x655ABB50, 0x40685A32, 0x3C2AB4B3, 0x319EE9D5, 0xC021B8F7, 0x9B540B19, 0x875FA099, 0x95F7997E, 0x623D7DA8, 0xF837889A, 0x97E32D77, 0x11ED935F, 0x16681281, 0x0E358829, 0xC7E61FD6, 0x96DEDFA1, 0x7858BA99, 0x57F584A5, 0x1B227263, 0x9B83C3FF, 0x1AC24696, 0xCDB30AEB, 0x532E3054, 0x8FD948E4, 0x6DBC3128, 0x58EBF2EF, 0x34C6FFEA, 0xFE28ED61, 0xEE7C3C73, 0x5D4A14D9, 0xE864B7E3, 0x42105D14, 0x203E13E0, 0x45EEE2B6, 0xA3AAABEA, 0xDB6C4F15, 0xFACB4FD0, 0xC742F442, 0xEF6ABBB5, 0x654F3B1D, 0x41CD2105, 0xD81E799E, 0x86854DC7, 0xE44B476A, 0x3D816250, 0xCF62A1F2, 0x5B8D2646, 0xFC8883A0, 0xC1C7B6A3, 0x7F1524C3, 0x69CB7492, 0x47848A0B, 0x5692B285, 0x095BBF00, 0xAD19489D, 0x1462B174, 0x23820E00, 0x58428D2A, 0x0C55F5EA, 0x1DADF43E, 0x233F7061, 0x3372F092, 0x8D937E41, 0xD65FECF1, 0x6C223BDB, 0x7CDE3759, 0xCBEE7460, 0x4085F2A7, 0xCE77326E, 0xA6078084, 0x19F8509E, 0xE8EFD855, 0x61D99735, 0xA969A7AA, 0xC50C06C2, 0x5A04ABFC, 0x800BCADC, 0x9E447A2E, 0xC3453484, 0xFDD56705, 0x0E1E9EC9, 0xDB73DBD3, 0x105588CD, 0x675FDA79, 0xE3674340, 0xC5C43465, 0x713E38D8, 0x3D28F89E, 0xF16DFF20, 0x153E21E7, 0x8FB03D4A, 0xE6E39F2B, 0xDB83ADF7, 0xE93D5A68, 0x948140F7, 0xF64C261C, 0x94692934, 0x411520F7, 0x7602D4F7, 0xBCF46B2E, 0xD4A20068, 0xD4082471, 0x3320F46A, 0x43B7D4B7, 0x500061AF, 0x1E39F62E, 0x97244546, 0x14214F74, 0xBF8B8840, 0x4D95FC1D, 0x96B591AF, 0x70F4DDD3, 0x66A02F45, 0xBFBC09EC, 0x03BD9785, 0x7FAC6DD0, 0x31CB8504, 0x96EB27B3, 0x55FD3941, 0xDA2547E6, 0xABCA0A9A, 0x28507825, 0x530429F4, 0x0A2C86DA, 0xE9B66DFB, 0x68DC1462, 0xD7486900, 0x680EC0A4, 0x27A18DEE, 0x4F3FFEA2, 0xE887AD8C, 0xB58CE006, 0x7AF4D6B6, 0xAACE1E7C, 0xD3375FEC, 0xCE78A399, 0x406B2A42, 0x20FE9E35, 0xD9F385B9, 0xEE39D7AB, 0x3B124E8B, 0x1DC9FAF7, 0x4B6D1856, 0x26A36631, 0xEAE397B2, 0x3A6EFA74, 0xDD5B4332, 0x6841E7F7, 0xCA7820FB, 0xFB0AF54E, 0xD8FEB397, 0x454056AC, 0xBA489527, 0x55533A3A, 0x20838D87, 0xFE6BA9B7, 0xD096954B, 0x55A867BC, 0xA1159A58, 0xCCA92963, 0x99E1DB33, 0xA62A4A56, 0x3F3125F9, 0x5EF47E1C, 0x9029317C, 0xFDF8E802, 0x04272F70, 0x80BB155C, 0x05282CE3, 0x95C11548, 0xE4C66D22, 0x48C1133F, 0xC70F86DC, 0x07F9C9EE, 0x41041F0F, 0x404779A4, 0x5D886E17, 0x325F51EB, 0xD59BC0D1, 0xF2BCC18F, 0x41113564, 0x257B7834, 0x602A9C60, 0xDFF8E8A3, 0x1F636C1B, 0x0E12B4C2, 0x02E1329E, 0xAF664FD1, 0xCAD18115, 0x6B2395E0, 0x333E92E1, 0x3B240B62, 0xEEBEB922, 0x85B2A20E, 0xE6BA0D99, 0xDE720C8C, 0x2DA2F728, 0xD0127845, 0x95B794FD, 0x647D0862, 0xE7CCF5F0, 0x5449A36F, 0x877D48FA, 0xC39DFD27, 0xF33E8D1E, 0x0A476341, 0x992EFF74, 0x3A6F6EAB, 0xF4F8FD37, 0xA812DC60, 0xA1EBDDF8, 0x991BE14C, 0xDB6E6B0D, 0xC67B5510, 0x6D672C37, 0x2765D43B, 0xDCD0E804, 0xF1290DC7, 0xCC00FFA3, 0xB5390F92, 0x690FED0B, 0x667B9FFB, 0xCEDB7D9C, 0xA091CF0B, 0xD9155EA3, 0xBB132F88, 0x515BAD24, 0x7B9479BF, 0x763BD6EB, 0x37392EB3, 0xCC115979, 0x8026E297, 0xF42E312D, 0x6842ADA7, 0xC66A2B3B, 0x12754CCC, 0x782EF11C, 0x6A124237, 0xB79251E7, 0x06A1BBE6, 0x4BFB6350, 0x1A6B1018, 0x11CAEDFA, 0x3D25BDD8, 0xE2E1C3C9, 0x44421659, 0x0A121386, 0xD90CEC6E, 0xD5ABEA2A, 0x64AF674E, 0xDA86A85F, 0xBEBFE988, 0x64E4C3FE, 0x9DBC8057, 0xF0F7C086, 0x60787BF8, 0x6003604D, 0xD1FD8346, 0xF6381FB0, 0x7745AE04, 0xD736FCCC, 0x83426B33, 0xF01EAB71, 0xB0804187, 0x3C005E5F, 0x77A057BE, 0xBDE8AE24, 0x55464299, 0xBF582E61, 0x4E58F48F, 0xF2DDFDA2, 0xF474EF38, 0x8789BDC2, 0x5366F9C3, 0xC8B38E74, 0xB475F255, 0x46FCD9B9, 0x7AEB2661, 0x8B1DDF84, 0x846A0E79, 0x915F95E2, 0x466E598E, 0x20B45770, 0x8CD55591, 0xC902DE4C, 0xB90BACE1, 0xBB8205D0, 0x11A86248, 0x7574A99E, 0xB77F19B6, 0xE0A9DC09, 0x662D09A1, 0xC4324633, 0xE85A1F02, 0x09F0BE8C, 0x4A99A025, 0x1D6EFE10, 0x1AB93D1D, 0x0BA5A4DF, 0xA186F20F, 0x2868F169, 0xDCB7DA83, 0x573906FE, 0xA1E2CE9B, 0x4FCD7F52, 0x50115E01, 0xA70683FA, 0xA002B5C4, 0x0DE6D027, 0x9AF88C27, 0x773F8641, 0xC3604C06, 0x61A806B5, 0xF0177A28, 0xC0F586E0, 0x006058AA, 0x30DC7D62, 0x11E69ED7, 0x2338EA63, 0x53C2DD94, 0xC2C21634, 0xBBCBEE56, 0x90BCB6DE, 0xEBFC7DA1, 0xCE591D76, 0x6F05E409, 0x4B7C0188, 0x39720A3D, 0x7C927C24, 0x86E3725F, 0x724D9DB9, 0x1AC15BB4, 0xD39EB8FC, 0xED545578, 0x08FCA5B5, 0xD83D7CD3, 0x4DAD0FC4, 0x1E50EF5E, 0xB161E6F8, 0xA28514D9, 0x6C51133C, 0x6FD5C7E7, 0x56E14EC4, 0x362ABFCE, 0xDDC6C837, 0xD79A3234, 0x92638212, 0x670EFA8E, 0x406000E0, 0x3A39CE37, 0xD3FAF5CF, 0xABC27737, 0x5AC52D1B, 0x5CB0679E, 0x4FA33742, 0xD3822740, 0x99BC9BBE, 0xD5118E9D, 0xBF0F7315, 0xD62D1C7E, 0xC700C47B, 0xB78C1B6B, 0x21A19045, 0xB26EB1BE, 0x6A366EB4, 0x5748AB2F, 0xBC946E79, 0xC6A376D2, 0x6549C2C8, 0x530FF8EE, 0x468DDE7D, 0xD5730A1D, 0x4CD04DC6, 0x2939BBDB, 0xA9BA4650, 0xAC9526E8, 0xBE5EE304, 0xA1FAD5F0, 0x6A2D519A, 0x63EF8CE2, 0x9A86EE22, 0xC089C2B8, 0x43242EF6, 0xA51E03AA, 0x9CF2D0A4, 0x83C061BA, 0x9BE96A4D, 0x8FE51550, 0xBA645BD6, 0x2826A2F9, 0xA73A3AE1, 0x4BA99586, 0xEF5562E9, 0xC72FEFD3, 0xF752F7DA, 0x3F046F69, 0x77FA0A59, 0x80E4A915, 0x87B08601, 0x9B09E6AD, 0x3B3EE593, 0xE990FD5A, 0x9E34D797, 0x2CF0B7D9, 0x022B8B51, 0x96D5AC3A, 0x017DA67D, 0xD1CF3ED6, 0x7C7D2D28, 0x1F9F25CF, 0xADF2B89B, 0x5AD6B472, 0x5A88F54C, 0xE029AC71, 0xE019A5E6, 0x47B0ACFD, 0xED93FA9B, 0xE8D3C48D, 0x283B57CC, 0xF8D56629, 0x79132E28, 0x785F0191, 0xED756055, 0xF7960E44, 0xE3D35E8C, 0x15056DD4, 0x88F46DBA, 0x03A16125, 0x0564F0BD, 0xC3EB9E15, 0x3C9057A2, 0x97271AEC, 0xA93A072A, 0x1B3F6D9B, 0x1E6321F5, 0xF59C66FB, 0x26DCF319, 0x7533D928, 0xB155FDF5, 0x03563482, 0x8ABA3CBB, 0x28517711, 0xC20AD9F8, 0xABCC5167, 0xCCAD925F, 0x4DE81751, 0x3830DC8E, 0x379D5862, 0x9320F991, 0xEA7A90C2, 0xFB3E7BCE, 0x5121CE64, 0x774FBE32, 0xA8B6E37E, 0xC3293D46, 0x48DE5369, 0x6413E680, 0xA2AE0810, 0xDD6DB224, 0x69852DFD, 0x09072166, 0xB39A460A, 0x6445C0DD, 0x586CDECF, 0x1C20C8AE, 0x5BBEF7DD, 0x1B588D40, 0xCCD2017F, 0x6BB4E3BB, 0xDDA26A7E, 0x3A59FF45, 0x3E350A44, 0xBCB4CDD5, 0x72EACEA8, 0xFA6484BB, 0x8D6612AE, 0xBF3C6F47, 0xD29BE463, 0x542F5D9E, 0xAEC2771B, 0xF64E6370, 0x740E0D8D, 0xE75B1357, 0xF8721671, 0xAF537D5D, 0x4040CB08, 0x4EB4E2CC, 0x34D2466A, 0x0115AF84, 0xE1B00428, 0x95983A1D, 0x06B89FB4, 0xCE6EA048, 0x6F3F3B82, 0x3520AB82, 0x011A1D4B, 0x277227F8, 0x611560B1, 0xE7933FDC, 0xBB3A792B, 0x344525BD, 0xA08839E1, 0x51CE794B, 0x2F32C9B7, 0xA01FBAC9, 0xE01CC87E, 0xBCC7D1F6, 0xCF0111C3, 0xA1E8AAC7, 0x1A908749, 0xD44FBD9A, 0xD0DADECB, 0xD50ADA38, 0x0339C32A, 0xC6913667, 0x8DF9317C, 0xE0B12B4F, 0xF79E59B7, 0x43F5BB3A, 0xF2D519FF, 0x27D9459C, 0xBF97222C, 0x15E6FC2A, 0x0F91FC71, 0x9B941525, 0xFAE59361, 0xCEB69CEB, 0xC2A86459, 0x12BAA8D1, 0xB6C1075E, 0xE3056A0C, 0x10D25065, 0xCB03A442, 0xE0EC6E0E, 0x1698DB3B, 0x4C98A0BE, 0x3278E964, 0x9F1F9532, 0xE0D392DF, 0xD3A0342B, 0x8971F21E, 0x1B0A7441, 0x4BA3348C, 0xC5BE7120, 0xC37632D8, 0xDF359F8D, 0x9B992F2E, 0xE60B6F47, 0x0FE3F11D, 0xE54CDA54, 0x1EDAD891, 0xCE6279CF, 0xCD3E7E6F, 0x1618B166, 0xFD2C1D05, 0x848FD2C5, 0xF6FB2299, 0xF523F357, 0xA6327623, 0x93A83531, 0x56CCCD02, 0xACF08162, 0x5A75EBB5, 0x6E163697, 0x88D273CC, 0xDE966292, 0x81B949D0, 0x4C50901B, 0x71C65614, 0xE6C6C7BD, 0x327A140A, 0x45E1D006, 0xC3F27B9A, 0xC9AA53FD, 0x62A80F00, 0xBB25BFE2, 0x35BDD2F6, 0x71126905, 0xB2040222, 0xB6CBCF7C, 0xCD769C2B, 0x53113EC0, 0x1640E3D3, 0x38ABBD60, 0x2547ADF0, 0xBA38209C, 0xF746CE76, 0x77AFA1C5, 0x20756060, 0x85CBFE4E, 0x8AE88DD8, 0x7AAAF9B0, 0x4CF9AA7E, 0x1948C25C, 0x02FB8A8C, 0x01C36AE4, 0xD6EBE1F9, 0x90D4F869, 0xA65CDEA0, 0x3F09252D, 0xC208E69F, 0xB74E6132, 0xCE77E25B, 0x578FDFE3, 0x3AC372E6 }; } /* * Blowfish * (C) 1999-2011 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Blowfish Encryption */ void Blowfish::encrypt_n(const byte in[], byte out[], size_t blocks) const { const u32bit* S1 = &S[0]; const u32bit* S2 = &S[256]; const u32bit* S3 = &S[512]; const u32bit* S4 = &S[768]; for(size_t i = 0; i != blocks; ++i) { u32bit L = load_be<u32bit>(in, 0); u32bit R = load_be<u32bit>(in, 1); for(size_t j = 0; j != 16; j += 2) { L ^= P[j]; R ^= ((S1[get_byte(0, L)] + S2[get_byte(1, L)]) ^ S3[get_byte(2, L)]) + S4[get_byte(3, L)]; R ^= P[j+1]; L ^= ((S1[get_byte(0, R)] + S2[get_byte(1, R)]) ^ S3[get_byte(2, R)]) + S4[get_byte(3, R)]; } L ^= P[16]; R ^= P[17]; store_be(out, R, L); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * Blowfish Decryption */ void Blowfish::decrypt_n(const byte in[], byte out[], size_t blocks) const { const u32bit* S1 = &S[0]; const u32bit* S2 = &S[256]; const u32bit* S3 = &S[512]; const u32bit* S4 = &S[768]; for(size_t i = 0; i != blocks; ++i) { u32bit L = load_be<u32bit>(in, 0); u32bit R = load_be<u32bit>(in, 1); for(size_t j = 17; j != 1; j -= 2) { L ^= P[j]; R ^= ((S1[get_byte(0, L)] + S2[get_byte(1, L)]) ^ S3[get_byte(2, L)]) + S4[get_byte(3, L)]; R ^= P[j-1]; L ^= ((S1[get_byte(0, R)] + S2[get_byte(1, R)]) ^ S3[get_byte(2, R)]) + S4[get_byte(3, R)]; } L ^= P[1]; R ^= P[0]; store_be(out, R, L); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * Blowfish Key Schedule */ void Blowfish::key_schedule(const byte key[], size_t length) { clear(); const byte null_salt[16] = { 0 }; key_expansion(key, length, null_salt); } void Blowfish::key_expansion(const byte key[], size_t length, const byte salt[16]) { for(size_t i = 0, j = 0; i != 18; ++i, j += 4) P[i] ^= make_u32bit(key[(j ) % length], key[(j+1) % length], key[(j+2) % length], key[(j+3) % length]); u32bit L = 0, R = 0; generate_sbox(P, L, R, salt, 0); generate_sbox(S, L, R, salt, 2); } /* * Modified key schedule used for bcrypt password hashing */ void Blowfish::eks_key_schedule(const byte key[], size_t length, const byte salt[16], size_t workfactor) { if(length == 0 || length >= 56) throw Invalid_Key_Length("EKSBlowfish", length); if(workfactor == 0) throw std::invalid_argument("Bcrypt work factor must be at least 1"); /* * On a 2.8 GHz Core-i7, workfactor == 18 takes about 25 seconds to * hash a password. This seems like a reasonable upper bound for the * time being. */ if(workfactor > 18) throw std::invalid_argument("Requested Bcrypt work factor too large"); clear(); const byte null_salt[16] = { 0 }; key_expansion(key, length, salt); const size_t rounds = 1 << workfactor; for(size_t r = 0; r != rounds; ++r) { key_expansion(key, length, null_salt); key_expansion(salt, 16, null_salt); } } /* * Generate one of the Sboxes */ void Blowfish::generate_sbox(MemoryRegion<u32bit>& box, u32bit& L, u32bit& R, const byte salt[16], size_t salt_off) const { const u32bit* S1 = &S[0]; const u32bit* S2 = &S[256]; const u32bit* S3 = &S[512]; const u32bit* S4 = &S[768]; for(size_t i = 0; i != box.size(); i += 2) { L ^= load_be<u32bit>(salt, (i + salt_off) % 4); R ^= load_be<u32bit>(salt, (i + salt_off + 1) % 4); for(size_t j = 0; j != 16; j += 2) { L ^= P[j]; R ^= ((S1[get_byte(0, L)] + S2[get_byte(1, L)]) ^ S3[get_byte(2, L)]) + S4[get_byte(3, L)]; R ^= P[j+1]; L ^= ((S1[get_byte(0, R)] + S2[get_byte(1, R)]) ^ S3[get_byte(2, R)]) + S4[get_byte(3, R)]; } u32bit T = R; R = L ^ P[16]; L = T ^ P[17]; box[i] = L; box[i+1] = R; } } /* * Clear memory of sensitive data */ void Blowfish::clear() { std::copy(P_INIT, P_INIT + 18, P.begin()); std::copy(S_INIT, S_INIT + 1024, S.begin()); } } /* * Camellia * (C) 2012 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace Camellia_F { namespace { /* * We use the slow byte-wise version of F in the first and last rounds * to help protect against timing attacks */ u64bit F_SLOW(u64bit v, u64bit K) { static const byte SBOX[256] = { 0x70, 0x82, 0x2C, 0xEC, 0xB3, 0x27, 0xC0, 0xE5, 0xE4, 0x85, 0x57, 0x35, 0xEA, 0x0C, 0xAE, 0x41, 0x23, 0xEF, 0x6B, 0x93, 0x45, 0x19, 0xA5, 0x21, 0xED, 0x0E, 0x4F, 0x4E, 0x1D, 0x65, 0x92, 0xBD, 0x86, 0xB8, 0xAF, 0x8F, 0x7C, 0xEB, 0x1F, 0xCE, 0x3E, 0x30, 0xDC, 0x5F, 0x5E, 0xC5, 0x0B, 0x1A, 0xA6, 0xE1, 0x39, 0xCA, 0xD5, 0x47, 0x5D, 0x3D, 0xD9, 0x01, 0x5A, 0xD6, 0x51, 0x56, 0x6C, 0x4D, 0x8B, 0x0D, 0x9A, 0x66, 0xFB, 0xCC, 0xB0, 0x2D, 0x74, 0x12, 0x2B, 0x20, 0xF0, 0xB1, 0x84, 0x99, 0xDF, 0x4C, 0xCB, 0xC2, 0x34, 0x7E, 0x76, 0x05, 0x6D, 0xB7, 0xA9, 0x31, 0xD1, 0x17, 0x04, 0xD7, 0x14, 0x58, 0x3A, 0x61, 0xDE, 0x1B, 0x11, 0x1C, 0x32, 0x0F, 0x9C, 0x16, 0x53, 0x18, 0xF2, 0x22, 0xFE, 0x44, 0xCF, 0xB2, 0xC3, 0xB5, 0x7A, 0x91, 0x24, 0x08, 0xE8, 0xA8, 0x60, 0xFC, 0x69, 0x50, 0xAA, 0xD0, 0xA0, 0x7D, 0xA1, 0x89, 0x62, 0x97, 0x54, 0x5B, 0x1E, 0x95, 0xE0, 0xFF, 0x64, 0xD2, 0x10, 0xC4, 0x00, 0x48, 0xA3, 0xF7, 0x75, 0xDB, 0x8A, 0x03, 0xE6, 0xDA, 0x09, 0x3F, 0xDD, 0x94, 0x87, 0x5C, 0x83, 0x02, 0xCD, 0x4A, 0x90, 0x33, 0x73, 0x67, 0xF6, 0xF3, 0x9D, 0x7F, 0xBF, 0xE2, 0x52, 0x9B, 0xD8, 0x26, 0xC8, 0x37, 0xC6, 0x3B, 0x81, 0x96, 0x6F, 0x4B, 0x13, 0xBE, 0x63, 0x2E, 0xE9, 0x79, 0xA7, 0x8C, 0x9F, 0x6E, 0xBC, 0x8E, 0x29, 0xF5, 0xF9, 0xB6, 0x2F, 0xFD, 0xB4, 0x59, 0x78, 0x98, 0x06, 0x6A, 0xE7, 0x46, 0x71, 0xBA, 0xD4, 0x25, 0xAB, 0x42, 0x88, 0xA2, 0x8D, 0xFA, 0x72, 0x07, 0xB9, 0x55, 0xF8, 0xEE, 0xAC, 0x0A, 0x36, 0x49, 0x2A, 0x68, 0x3C, 0x38, 0xF1, 0xA4, 0x40, 0x28, 0xD3, 0x7B, 0xBB, 0xC9, 0x43, 0xC1, 0x15, 0xE3, 0xAD, 0xF4, 0x77, 0xC7, 0x80, 0x9E }; const u64bit x = v ^ K; const byte t1 = SBOX[get_byte(0, x)]; const byte t2 = rotate_left(SBOX[get_byte(1, x)], 1); const byte t3 = rotate_left(SBOX[get_byte(2, x)], 7); const byte t4 = SBOX[rotate_left(get_byte(3, x), 1)]; const byte t5 = rotate_left(SBOX[get_byte(4, x)], 1); const byte t6 = rotate_left(SBOX[get_byte(5, x)], 7); const byte t7 = SBOX[rotate_left(get_byte(6, x), 1)]; const byte t8 = SBOX[get_byte(7, x)]; const byte y1 = t1 ^ t3 ^ t4 ^ t6 ^ t7 ^ t8; const byte y2 = t1 ^ t2 ^ t4 ^ t5 ^ t7 ^ t8; const byte y3 = t1 ^ t2 ^ t3 ^ t5 ^ t6 ^ t8; const byte y4 = t2 ^ t3 ^ t4 ^ t5 ^ t6 ^ t7; const byte y5 = t1 ^ t2 ^ t6 ^ t7 ^ t8; const byte y6 = t2 ^ t3 ^ t5 ^ t7 ^ t8; const byte y7 = t3 ^ t4 ^ t5 ^ t6 ^ t8; const byte y8 = t1 ^ t4 ^ t5 ^ t6 ^ t7; return make_u64bit(y1, y2, y3, y4, y5, y6, y7, y8); } inline u64bit F(u64bit v, u64bit K) { const u64bit x = v ^ K; return Camellia_SBOX1[get_byte(0, x)] ^ Camellia_SBOX2[get_byte(1, x)] ^ Camellia_SBOX3[get_byte(2, x)] ^ Camellia_SBOX4[get_byte(3, x)] ^ Camellia_SBOX5[get_byte(4, x)] ^ Camellia_SBOX6[get_byte(5, x)] ^ Camellia_SBOX7[get_byte(6, x)] ^ Camellia_SBOX8[get_byte(7, x)]; } inline u64bit FL(u64bit v, u64bit K) { u32bit x1 = (v >> 32); u32bit x2 = (v & 0xFFFFFFFF); const u32bit k1 = (K >> 32); const u32bit k2 = (K & 0xFFFFFFFF); x2 ^= rotate_left(x1 & k1, 1); x1 ^= (x2 | k2); return ((static_cast<u64bit>(x1) << 32) | x2); } inline u64bit FLINV(u64bit v, u64bit K) { u32bit x1 = (v >> 32); u32bit x2 = (v & 0xFFFFFFFF); const u32bit k1 = (K >> 32); const u32bit k2 = (K & 0xFFFFFFFF); x1 ^= (x2 | k2); x2 ^= rotate_left(x1 & k1, 1); return ((static_cast<u64bit>(x1) << 32) | x2); } /* * Camellia Encryption */ void encrypt(const byte in[], byte out[], size_t blocks, const SecureVector<u64bit>& SK, const size_t rounds) { for(size_t i = 0; i != blocks; ++i) { u64bit D1 = load_be<u64bit>(in, 0); u64bit D2 = load_be<u64bit>(in, 1); const u64bit* K = &SK[0]; D1 ^= *K++; D2 ^= *K++; D2 ^= F_SLOW(D1, *K++); D1 ^= F_SLOW(D2, *K++); for(size_t r = 1; r != rounds - 1; ++r) { if(r % 3 == 0) { D1 = FL (D1, *K++); D2 = FLINV(D2, *K++); } D2 ^= F(D1, *K++); D1 ^= F(D2, *K++); } D2 ^= F_SLOW(D1, *K++); D1 ^= F_SLOW(D2, *K++); D2 ^= *K++; D1 ^= *K++; store_be(out, D2, D1); in += 16; out += 16; } } /* * Camellia Decryption */ void decrypt(const byte in[], byte out[], size_t blocks, const SecureVector<u64bit>& SK, const size_t rounds) { for(size_t i = 0; i != blocks; ++i) { u64bit D1 = load_be<u64bit>(in, 0); u64bit D2 = load_be<u64bit>(in, 1); const u64bit* K = &SK[SK.size()-1]; D2 ^= *K--; D1 ^= *K--; D2 ^= F_SLOW(D1, *K--); D1 ^= F_SLOW(D2, *K--); for(size_t r = 1; r != rounds - 1; ++r) { if(r % 3 == 0) { D1 = FL (D1, *K--); D2 = FLINV(D2, *K--); } D2 ^= F(D1, *K--); D1 ^= F(D2, *K--); } D2 ^= F_SLOW(D1, *K--); D1 ^= F_SLOW(D2, *K--); D1 ^= *K--; D2 ^= *K; store_be(out, D2, D1); in += 16; out += 16; } } u64bit left_rot_hi(u64bit h, u64bit l, size_t shift) { return (h << shift) | ((l >> (64-shift))); } u64bit left_rot_lo(u64bit h, u64bit l, size_t shift) { return (h >> (64-shift)) | (l << shift); } /* * Camellia Key Schedule */ void key_schedule(SecureVector<u64bit>& SK, const byte key[], size_t length) { const u64bit Sigma1 = 0xA09E667F3BCC908B; const u64bit Sigma2 = 0xB67AE8584CAA73B2; const u64bit Sigma3 = 0xC6EF372FE94F82BE; const u64bit Sigma4 = 0x54FF53A5F1D36F1C; const u64bit Sigma5 = 0x10E527FADE682D1D; const u64bit Sigma6 = 0xB05688C2B3E6C1FD; const u64bit KL_H = load_be<u64bit>(key, 0); const u64bit KL_L = load_be<u64bit>(key, 1); const u64bit KR_H = (length >= 24) ? load_be<u64bit>(key, 2) : 0; const u64bit KR_L = (length == 32) ? load_be<u64bit>(key, 3) : ((length == 24) ? ~KR_H : 0); u64bit D1 = KL_H ^ KR_H; u64bit D2 = KL_L ^ KR_L; D2 ^= F(D1, Sigma1); D1 ^= F(D2, Sigma2); D1 ^= KL_H; D2 ^= KL_L; D2 ^= F(D1, Sigma3); D1 ^= F(D2, Sigma4); const u64bit KA_H = D1; const u64bit KA_L = D2; D1 = KA_H ^ KR_H; D2 = KA_L ^ KR_L; D2 ^= F(D1, Sigma5); D1 ^= F(D2, Sigma6); const u64bit KB_H = D1; const u64bit KB_L = D2; if(length == 16) { SK.resize(26); SK[ 0] = KL_H; SK[ 1] = KL_L; SK[ 2] = KA_H; SK[ 3] = KA_L; SK[ 4] = left_rot_hi(KL_H, KL_L, 15); SK[ 5] = left_rot_lo(KL_H, KL_L, 15); SK[ 6] = left_rot_hi(KA_H, KA_L, 15); SK[ 7] = left_rot_lo(KA_H, KA_L, 15); SK[ 8] = left_rot_hi(KA_H, KA_L, 30); SK[ 9] = left_rot_lo(KA_H, KA_L, 30); SK[10] = left_rot_hi(KL_H, KL_L, 45); SK[11] = left_rot_lo(KL_H, KL_L, 45); SK[12] = left_rot_hi(KA_H, KA_L, 45); SK[13] = left_rot_lo(KL_H, KL_L, 60); SK[14] = left_rot_hi(KA_H, KA_L, 60); SK[15] = left_rot_lo(KA_H, KA_L, 60); SK[16] = left_rot_lo(KL_H, KL_L, 77-64); SK[17] = left_rot_hi(KL_H, KL_L, 77-64); SK[18] = left_rot_lo(KL_H, KL_L, 94-64); SK[19] = left_rot_hi(KL_H, KL_L, 94-64); SK[20] = left_rot_lo(KA_H, KA_L, 94-64); SK[21] = left_rot_hi(KA_H, KA_L, 94-64); SK[22] = left_rot_lo(KL_H, KL_L, 111-64); SK[23] = left_rot_hi(KL_H, KL_L, 111-64); SK[24] = left_rot_lo(KA_H, KA_L, 111-64); SK[25] = left_rot_hi(KA_H, KA_L, 111-64); } else { SK.resize(34); SK[ 0] = KL_H; SK[ 1] = KL_L; SK[ 2] = KB_H; SK[ 3] = KB_L; SK[ 4] = left_rot_hi(KR_H, KR_L, 15); SK[ 5] = left_rot_lo(KR_H, KR_L, 15); SK[ 6] = left_rot_hi(KA_H, KA_L, 15); SK[ 7] = left_rot_lo(KA_H, KA_L, 15); SK[ 8] = left_rot_hi(KR_H, KR_L, 30); SK[ 9] = left_rot_lo(KR_H, KR_L, 30); SK[10] = left_rot_hi(KB_H, KB_L, 30); SK[11] = left_rot_lo(KB_H, KB_L, 30); SK[12] = left_rot_hi(KL_H, KL_L, 45); SK[13] = left_rot_lo(KL_H, KL_L, 45); SK[14] = left_rot_hi(KA_H, KA_L, 45); SK[15] = left_rot_lo(KA_H, KA_L, 45); SK[16] = left_rot_hi(KL_H, KL_L, 60); SK[17] = left_rot_lo(KL_H, KL_L, 60); SK[18] = left_rot_hi(KR_H, KR_L, 60); SK[19] = left_rot_lo(KR_H, KR_L, 60); SK[20] = left_rot_hi(KB_H, KB_L, 60); SK[21] = left_rot_lo(KB_H, KB_L, 60); SK[22] = left_rot_lo(KL_H, KL_L, 77-64); SK[23] = left_rot_hi(KL_H, KL_L, 77-64); SK[24] = left_rot_lo(KA_H, KA_L, 77-64); SK[25] = left_rot_hi(KA_H, KA_L, 77-64); SK[26] = left_rot_lo(KR_H, KR_L, 94-64); SK[27] = left_rot_hi(KR_H, KR_L, 94-64); SK[28] = left_rot_lo(KA_H, KA_L, 94-64); SK[29] = left_rot_hi(KA_H, KA_L, 94-64); SK[30] = left_rot_lo(KL_H, KL_L, 111-64); SK[31] = left_rot_hi(KL_H, KL_L, 111-64); SK[32] = left_rot_lo(KB_H, KB_L, 111-64); SK[33] = left_rot_hi(KB_H, KB_L, 111-64); } } } } void Camellia_128::encrypt_n(const byte in[], byte out[], size_t blocks) const { Camellia_F::encrypt(in, out, blocks, SK, 9); } void Camellia_192::encrypt_n(const byte in[], byte out[], size_t blocks) const { Camellia_F::encrypt(in, out, blocks, SK, 12); } void Camellia_256::encrypt_n(const byte in[], byte out[], size_t blocks) const { Camellia_F::encrypt(in, out, blocks, SK, 12); } void Camellia_128::decrypt_n(const byte in[], byte out[], size_t blocks) const { Camellia_F::decrypt(in, out, blocks, SK, 9); } void Camellia_192::decrypt_n(const byte in[], byte out[], size_t blocks) const { Camellia_F::decrypt(in, out, blocks, SK, 12); } void Camellia_256::decrypt_n(const byte in[], byte out[], size_t blocks) const { Camellia_F::decrypt(in, out, blocks, SK, 12); } void Camellia_128::key_schedule(const byte key[], size_t length) { Camellia_F::key_schedule(SK, key, length); } void Camellia_192::key_schedule(const byte key[], size_t length) { Camellia_F::key_schedule(SK, key, length); } void Camellia_256::key_schedule(const byte key[], size_t length) { Camellia_F::key_schedule(SK, key, length); } } /* * Block Cipher Cascade * (C) 2010 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { void Cascade_Cipher::encrypt_n(const byte in[], byte out[], size_t blocks) const { size_t c1_blocks = blocks * (block_size() / cipher1->block_size()); size_t c2_blocks = blocks * (block_size() / cipher2->block_size()); cipher1->encrypt_n(in, out, c1_blocks); cipher2->encrypt_n(out, out, c2_blocks); } void Cascade_Cipher::decrypt_n(const byte in[], byte out[], size_t blocks) const { size_t c1_blocks = blocks * (block_size() / cipher1->block_size()); size_t c2_blocks = blocks * (block_size() / cipher2->block_size()); cipher2->decrypt_n(in, out, c2_blocks); cipher1->decrypt_n(out, out, c1_blocks); } void Cascade_Cipher::key_schedule(const byte key[], size_t) { const byte* key2 = key + cipher1->maximum_keylength(); cipher1->set_key(key , cipher1->maximum_keylength()); cipher2->set_key(key2, cipher2->maximum_keylength()); } void Cascade_Cipher::clear() { cipher1->clear(); cipher2->clear(); } std::string Cascade_Cipher::name() const { return "Cascade(" + cipher1->name() + "," + cipher2->name() + ")"; } BlockCipher* Cascade_Cipher::clone() const { return new Cascade_Cipher(cipher1->clone(), cipher2->clone()); } namespace { size_t euclids_algorithm(size_t a, size_t b) { while(b != 0) // gcd { size_t t = b; b = a % b; a = t; } return a; } size_t block_size_for_cascade(size_t bs, size_t bs2) { if(bs == bs2) return bs; size_t gcd = euclids_algorithm(bs, bs2); return (bs * bs2) / gcd; } } Cascade_Cipher::Cascade_Cipher(BlockCipher* c1, BlockCipher* c2) : cipher1(c1), cipher2(c2) { block = block_size_for_cascade(c1->block_size(), c2->block_size()); if(block_size() % c1->block_size() || block_size() % c2->block_size()) throw Internal_Error("Failure in " + name() + " constructor"); } Cascade_Cipher::~Cascade_Cipher() { delete cipher1; delete cipher2; } } /* * CAST-128 * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { /* * CAST-128 Round Type 1 */ inline void R1(u32bit& L, u32bit R, u32bit MK, u32bit RK) { u32bit T = rotate_left(MK + R, RK); L ^= (CAST_SBOX1[get_byte(0, T)] ^ CAST_SBOX2[get_byte(1, T)]) - CAST_SBOX3[get_byte(2, T)] + CAST_SBOX4[get_byte(3, T)]; } /* * CAST-128 Round Type 2 */ inline void R2(u32bit& L, u32bit R, u32bit MK, u32bit RK) { u32bit T = rotate_left(MK ^ R, RK); L ^= (CAST_SBOX1[get_byte(0, T)] - CAST_SBOX2[get_byte(1, T)] + CAST_SBOX3[get_byte(2, T)]) ^ CAST_SBOX4[get_byte(3, T)]; } /* * CAST-128 Round Type 3 */ inline void R3(u32bit& L, u32bit R, u32bit MK, u32bit RK) { u32bit T = rotate_left(MK - R, RK); L ^= ((CAST_SBOX1[get_byte(0, T)] + CAST_SBOX2[get_byte(1, T)]) ^ CAST_SBOX3[get_byte(2, T)]) - CAST_SBOX4[get_byte(3, T)]; } } /* * CAST-128 Encryption */ void CAST_128::encrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u32bit L = load_be<u32bit>(in, 0); u32bit R = load_be<u32bit>(in, 1); R1(L, R, MK[ 0], RK[ 0]); R2(R, L, MK[ 1], RK[ 1]); R3(L, R, MK[ 2], RK[ 2]); R1(R, L, MK[ 3], RK[ 3]); R2(L, R, MK[ 4], RK[ 4]); R3(R, L, MK[ 5], RK[ 5]); R1(L, R, MK[ 6], RK[ 6]); R2(R, L, MK[ 7], RK[ 7]); R3(L, R, MK[ 8], RK[ 8]); R1(R, L, MK[ 9], RK[ 9]); R2(L, R, MK[10], RK[10]); R3(R, L, MK[11], RK[11]); R1(L, R, MK[12], RK[12]); R2(R, L, MK[13], RK[13]); R3(L, R, MK[14], RK[14]); R1(R, L, MK[15], RK[15]); store_be(out, R, L); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * CAST-128 Decryption */ void CAST_128::decrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u32bit L = load_be<u32bit>(in, 0); u32bit R = load_be<u32bit>(in, 1); R1(L, R, MK[15], RK[15]); R3(R, L, MK[14], RK[14]); R2(L, R, MK[13], RK[13]); R1(R, L, MK[12], RK[12]); R3(L, R, MK[11], RK[11]); R2(R, L, MK[10], RK[10]); R1(L, R, MK[ 9], RK[ 9]); R3(R, L, MK[ 8], RK[ 8]); R2(L, R, MK[ 7], RK[ 7]); R1(R, L, MK[ 6], RK[ 6]); R3(L, R, MK[ 5], RK[ 5]); R2(R, L, MK[ 4], RK[ 4]); R1(L, R, MK[ 3], RK[ 3]); R3(R, L, MK[ 2], RK[ 2]); R2(L, R, MK[ 1], RK[ 1]); R1(R, L, MK[ 0], RK[ 0]); store_be(out, R, L); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * CAST-128 Key Schedule */ void CAST_128::key_schedule(const byte key[], size_t length) { clear(); SecureVector<u32bit> X(4); for(size_t j = 0; j != length; ++j) X[j/4] = (X[j/4] << 8) + key[j]; cast_ks(MK, X); cast_ks(RK, X); for(size_t j = 0; j != 16; ++j) RK[j] %= 32; } /* * S-Box Based Key Expansion */ void CAST_128::cast_ks(MemoryRegion<u32bit>& K, MemoryRegion<u32bit>& X) { class ByteReader { public: byte operator()(size_t i) { return (X[i/4] >> (8*(3 - (i%4)))); } ByteReader(const u32bit* x) : X(x) {} private: const u32bit* X; }; SecureVector<u32bit> Z(4); ByteReader x(&X[0]), z(&Z[0]); Z[0] = X[0] ^ S5[x(13)] ^ S6[x(15)] ^ S7[x(12)] ^ S8[x(14)] ^ S7[x( 8)]; Z[1] = X[2] ^ S5[z( 0)] ^ S6[z( 2)] ^ S7[z( 1)] ^ S8[z( 3)] ^ S8[x(10)]; Z[2] = X[3] ^ S5[z( 7)] ^ S6[z( 6)] ^ S7[z( 5)] ^ S8[z( 4)] ^ S5[x( 9)]; Z[3] = X[1] ^ S5[z(10)] ^ S6[z( 9)] ^ S7[z(11)] ^ S8[z( 8)] ^ S6[x(11)]; K[ 0] = S5[z( 8)] ^ S6[z( 9)] ^ S7[z( 7)] ^ S8[z( 6)] ^ S5[z( 2)]; K[ 1] = S5[z(10)] ^ S6[z(11)] ^ S7[z( 5)] ^ S8[z( 4)] ^ S6[z( 6)]; K[ 2] = S5[z(12)] ^ S6[z(13)] ^ S7[z( 3)] ^ S8[z( 2)] ^ S7[z( 9)]; K[ 3] = S5[z(14)] ^ S6[z(15)] ^ S7[z( 1)] ^ S8[z( 0)] ^ S8[z(12)]; X[0] = Z[2] ^ S5[z( 5)] ^ S6[z( 7)] ^ S7[z( 4)] ^ S8[z( 6)] ^ S7[z( 0)]; X[1] = Z[0] ^ S5[x( 0)] ^ S6[x( 2)] ^ S7[x( 1)] ^ S8[x( 3)] ^ S8[z( 2)]; X[2] = Z[1] ^ S5[x( 7)] ^ S6[x( 6)] ^ S7[x( 5)] ^ S8[x( 4)] ^ S5[z( 1)]; X[3] = Z[3] ^ S5[x(10)] ^ S6[x( 9)] ^ S7[x(11)] ^ S8[x( 8)] ^ S6[z( 3)]; K[ 4] = S5[x( 3)] ^ S6[x( 2)] ^ S7[x(12)] ^ S8[x(13)] ^ S5[x( 8)]; K[ 5] = S5[x( 1)] ^ S6[x( 0)] ^ S7[x(14)] ^ S8[x(15)] ^ S6[x(13)]; K[ 6] = S5[x( 7)] ^ S6[x( 6)] ^ S7[x( 8)] ^ S8[x( 9)] ^ S7[x( 3)]; K[ 7] = S5[x( 5)] ^ S6[x( 4)] ^ S7[x(10)] ^ S8[x(11)] ^ S8[x( 7)]; Z[0] = X[0] ^ S5[x(13)] ^ S6[x(15)] ^ S7[x(12)] ^ S8[x(14)] ^ S7[x( 8)]; Z[1] = X[2] ^ S5[z( 0)] ^ S6[z( 2)] ^ S7[z( 1)] ^ S8[z( 3)] ^ S8[x(10)]; Z[2] = X[3] ^ S5[z( 7)] ^ S6[z( 6)] ^ S7[z( 5)] ^ S8[z( 4)] ^ S5[x( 9)]; Z[3] = X[1] ^ S5[z(10)] ^ S6[z( 9)] ^ S7[z(11)] ^ S8[z( 8)] ^ S6[x(11)]; K[ 8] = S5[z( 3)] ^ S6[z( 2)] ^ S7[z(12)] ^ S8[z(13)] ^ S5[z( 9)]; K[ 9] = S5[z( 1)] ^ S6[z( 0)] ^ S7[z(14)] ^ S8[z(15)] ^ S6[z(12)]; K[10] = S5[z( 7)] ^ S6[z( 6)] ^ S7[z( 8)] ^ S8[z( 9)] ^ S7[z( 2)]; K[11] = S5[z( 5)] ^ S6[z( 4)] ^ S7[z(10)] ^ S8[z(11)] ^ S8[z( 6)]; X[0] = Z[2] ^ S5[z( 5)] ^ S6[z( 7)] ^ S7[z( 4)] ^ S8[z( 6)] ^ S7[z( 0)]; X[1] = Z[0] ^ S5[x( 0)] ^ S6[x( 2)] ^ S7[x( 1)] ^ S8[x( 3)] ^ S8[z( 2)]; X[2] = Z[1] ^ S5[x( 7)] ^ S6[x( 6)] ^ S7[x( 5)] ^ S8[x( 4)] ^ S5[z( 1)]; X[3] = Z[3] ^ S5[x(10)] ^ S6[x( 9)] ^ S7[x(11)] ^ S8[x( 8)] ^ S6[z( 3)]; K[12] = S5[x( 8)] ^ S6[x( 9)] ^ S7[x( 7)] ^ S8[x( 6)] ^ S5[x( 3)]; K[13] = S5[x(10)] ^ S6[x(11)] ^ S7[x( 5)] ^ S8[x( 4)] ^ S6[x( 7)]; K[14] = S5[x(12)] ^ S6[x(13)] ^ S7[x( 3)] ^ S8[x( 2)] ^ S7[x( 8)]; K[15] = S5[x(14)] ^ S6[x(15)] ^ S7[x( 1)] ^ S8[x( 0)] ^ S8[x(13)]; } } /* * CAST-256 * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { /* * CAST-256 Round Type 1 */ void round1(u32bit& out, u32bit in, u32bit mask, u32bit rot) { u32bit temp = rotate_left(mask + in, rot); out ^= (CAST_SBOX1[get_byte(0, temp)] ^ CAST_SBOX2[get_byte(1, temp)]) - CAST_SBOX3[get_byte(2, temp)] + CAST_SBOX4[get_byte(3, temp)]; } /* * CAST-256 Round Type 2 */ void round2(u32bit& out, u32bit in, u32bit mask, u32bit rot) { u32bit temp = rotate_left(mask ^ in, rot); out ^= (CAST_SBOX1[get_byte(0, temp)] - CAST_SBOX2[get_byte(1, temp)] + CAST_SBOX3[get_byte(2, temp)]) ^ CAST_SBOX4[get_byte(3, temp)]; } /* * CAST-256 Round Type 3 */ void round3(u32bit& out, u32bit in, u32bit mask, u32bit rot) { u32bit temp = rotate_left(mask - in, rot); out ^= ((CAST_SBOX1[get_byte(0, temp)] + CAST_SBOX2[get_byte(1, temp)]) ^ CAST_SBOX3[get_byte(2, temp)]) - CAST_SBOX4[get_byte(3, temp)]; } } /* * CAST-256 Encryption */ void CAST_256::encrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u32bit A = load_be<u32bit>(in, 0); u32bit B = load_be<u32bit>(in, 1); u32bit C = load_be<u32bit>(in, 2); u32bit D = load_be<u32bit>(in, 3); round1(C, D, MK[ 0], RK[ 0]); round2(B, C, MK[ 1], RK[ 1]); round3(A, B, MK[ 2], RK[ 2]); round1(D, A, MK[ 3], RK[ 3]); round1(C, D, MK[ 4], RK[ 4]); round2(B, C, MK[ 5], RK[ 5]); round3(A, B, MK[ 6], RK[ 6]); round1(D, A, MK[ 7], RK[ 7]); round1(C, D, MK[ 8], RK[ 8]); round2(B, C, MK[ 9], RK[ 9]); round3(A, B, MK[10], RK[10]); round1(D, A, MK[11], RK[11]); round1(C, D, MK[12], RK[12]); round2(B, C, MK[13], RK[13]); round3(A, B, MK[14], RK[14]); round1(D, A, MK[15], RK[15]); round1(C, D, MK[16], RK[16]); round2(B, C, MK[17], RK[17]); round3(A, B, MK[18], RK[18]); round1(D, A, MK[19], RK[19]); round1(C, D, MK[20], RK[20]); round2(B, C, MK[21], RK[21]); round3(A, B, MK[22], RK[22]); round1(D, A, MK[23], RK[23]); round1(D, A, MK[27], RK[27]); round3(A, B, MK[26], RK[26]); round2(B, C, MK[25], RK[25]); round1(C, D, MK[24], RK[24]); round1(D, A, MK[31], RK[31]); round3(A, B, MK[30], RK[30]); round2(B, C, MK[29], RK[29]); round1(C, D, MK[28], RK[28]); round1(D, A, MK[35], RK[35]); round3(A, B, MK[34], RK[34]); round2(B, C, MK[33], RK[33]); round1(C, D, MK[32], RK[32]); round1(D, A, MK[39], RK[39]); round3(A, B, MK[38], RK[38]); round2(B, C, MK[37], RK[37]); round1(C, D, MK[36], RK[36]); round1(D, A, MK[43], RK[43]); round3(A, B, MK[42], RK[42]); round2(B, C, MK[41], RK[41]); round1(C, D, MK[40], RK[40]); round1(D, A, MK[47], RK[47]); round3(A, B, MK[46], RK[46]); round2(B, C, MK[45], RK[45]); round1(C, D, MK[44], RK[44]); store_be(out, A, B, C, D); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * CAST-256 Decryption */ void CAST_256::decrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u32bit A = load_be<u32bit>(in, 0); u32bit B = load_be<u32bit>(in, 1); u32bit C = load_be<u32bit>(in, 2); u32bit D = load_be<u32bit>(in, 3); round1(C, D, MK[44], RK[44]); round2(B, C, MK[45], RK[45]); round3(A, B, MK[46], RK[46]); round1(D, A, MK[47], RK[47]); round1(C, D, MK[40], RK[40]); round2(B, C, MK[41], RK[41]); round3(A, B, MK[42], RK[42]); round1(D, A, MK[43], RK[43]); round1(C, D, MK[36], RK[36]); round2(B, C, MK[37], RK[37]); round3(A, B, MK[38], RK[38]); round1(D, A, MK[39], RK[39]); round1(C, D, MK[32], RK[32]); round2(B, C, MK[33], RK[33]); round3(A, B, MK[34], RK[34]); round1(D, A, MK[35], RK[35]); round1(C, D, MK[28], RK[28]); round2(B, C, MK[29], RK[29]); round3(A, B, MK[30], RK[30]); round1(D, A, MK[31], RK[31]); round1(C, D, MK[24], RK[24]); round2(B, C, MK[25], RK[25]); round3(A, B, MK[26], RK[26]); round1(D, A, MK[27], RK[27]); round1(D, A, MK[23], RK[23]); round3(A, B, MK[22], RK[22]); round2(B, C, MK[21], RK[21]); round1(C, D, MK[20], RK[20]); round1(D, A, MK[19], RK[19]); round3(A, B, MK[18], RK[18]); round2(B, C, MK[17], RK[17]); round1(C, D, MK[16], RK[16]); round1(D, A, MK[15], RK[15]); round3(A, B, MK[14], RK[14]); round2(B, C, MK[13], RK[13]); round1(C, D, MK[12], RK[12]); round1(D, A, MK[11], RK[11]); round3(A, B, MK[10], RK[10]); round2(B, C, MK[ 9], RK[ 9]); round1(C, D, MK[ 8], RK[ 8]); round1(D, A, MK[ 7], RK[ 7]); round3(A, B, MK[ 6], RK[ 6]); round2(B, C, MK[ 5], RK[ 5]); round1(C, D, MK[ 4], RK[ 4]); round1(D, A, MK[ 3], RK[ 3]); round3(A, B, MK[ 2], RK[ 2]); round2(B, C, MK[ 1], RK[ 1]); round1(C, D, MK[ 0], RK[ 0]); store_be(out, A, B, C, D); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * CAST-256 Key Schedule */ void CAST_256::key_schedule(const byte key[], size_t length) { SecureVector<u32bit> K(8); for(size_t j = 0; j != length; ++j) K[j/4] = (K[j/4] << 8) + key[j]; u32bit A = K[0], B = K[1], C = K[2], D = K[3], E = K[4], F = K[5], G = K[6], H = K[7]; for(size_t j = 0; j != 48; j += 4) { round1(G, H, KEY_MASK[4*j+ 0], KEY_ROT[(4*j+ 0) % 32]); round2(F, G, KEY_MASK[4*j+ 1], KEY_ROT[(4*j+ 1) % 32]); round3(E, F, KEY_MASK[4*j+ 2], KEY_ROT[(4*j+ 2) % 32]); round1(D, E, KEY_MASK[4*j+ 3], KEY_ROT[(4*j+ 3) % 32]); round2(C, D, KEY_MASK[4*j+ 4], KEY_ROT[(4*j+ 4) % 32]); round3(B, C, KEY_MASK[4*j+ 5], KEY_ROT[(4*j+ 5) % 32]); round1(A, B, KEY_MASK[4*j+ 6], KEY_ROT[(4*j+ 6) % 32]); round2(H, A, KEY_MASK[4*j+ 7], KEY_ROT[(4*j+ 7) % 32]); round1(G, H, KEY_MASK[4*j+ 8], KEY_ROT[(4*j+ 8) % 32]); round2(F, G, KEY_MASK[4*j+ 9], KEY_ROT[(4*j+ 9) % 32]); round3(E, F, KEY_MASK[4*j+10], KEY_ROT[(4*j+10) % 32]); round1(D, E, KEY_MASK[4*j+11], KEY_ROT[(4*j+11) % 32]); round2(C, D, KEY_MASK[4*j+12], KEY_ROT[(4*j+12) % 32]); round3(B, C, KEY_MASK[4*j+13], KEY_ROT[(4*j+13) % 32]); round1(A, B, KEY_MASK[4*j+14], KEY_ROT[(4*j+14) % 32]); round2(H, A, KEY_MASK[4*j+15], KEY_ROT[(4*j+15) % 32]); RK[j ] = (A % 32); RK[j+1] = (C % 32); RK[j+2] = (E % 32); RK[j+3] = (G % 32); MK[j ] = H; MK[j+1] = F; MK[j+2] = D; MK[j+3] = B; } } } /* * S-Box Tables for CAST-128 and CAST-256 * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { const u32bit CAST_SBOX1[256] = { 0x30FB40D4, 0x9FA0FF0B, 0x6BECCD2F, 0x3F258C7A, 0x1E213F2F, 0x9C004DD3, 0x6003E540, 0xCF9FC949, 0xBFD4AF27, 0x88BBBDB5, 0xE2034090, 0x98D09675, 0x6E63A0E0, 0x15C361D2, 0xC2E7661D, 0x22D4FF8E, 0x28683B6F, 0xC07FD059, 0xFF2379C8, 0x775F50E2, 0x43C340D3, 0xDF2F8656, 0x887CA41A, 0xA2D2BD2D, 0xA1C9E0D6, 0x346C4819, 0x61B76D87, 0x22540F2F, 0x2ABE32E1, 0xAA54166B, 0x22568E3A, 0xA2D341D0, 0x66DB40C8, 0xA784392F, 0x004DFF2F, 0x2DB9D2DE, 0x97943FAC, 0x4A97C1D8, 0x527644B7, 0xB5F437A7, 0xB82CBAEF, 0xD751D159, 0x6FF7F0ED, 0x5A097A1F, 0x827B68D0, 0x90ECF52E, 0x22B0C054, 0xBC8E5935, 0x4B6D2F7F, 0x50BB64A2, 0xD2664910, 0xBEE5812D, 0xB7332290, 0xE93B159F, 0xB48EE411, 0x4BFF345D, 0xFD45C240, 0xAD31973F, 0xC4F6D02E, 0x55FC8165, 0xD5B1CAAD, 0xA1AC2DAE, 0xA2D4B76D, 0xC19B0C50, 0x882240F2, 0x0C6E4F38, 0xA4E4BFD7, 0x4F5BA272, 0x564C1D2F, 0xC59C5319, 0xB949E354, 0xB04669FE, 0xB1B6AB8A, 0xC71358DD, 0x6385C545, 0x110F935D, 0x57538AD5, 0x6A390493, 0xE63D37E0, 0x2A54F6B3, 0x3A787D5F, 0x6276A0B5, 0x19A6FCDF, 0x7A42206A, 0x29F9D4D5, 0xF61B1891, 0xBB72275E, 0xAA508167, 0x38901091, 0xC6B505EB, 0x84C7CB8C, 0x2AD75A0F, 0x874A1427, 0xA2D1936B, 0x2AD286AF, 0xAA56D291, 0xD7894360, 0x425C750D, 0x93B39E26, 0x187184C9, 0x6C00B32D, 0x73E2BB14, 0xA0BEBC3C, 0x54623779, 0x64459EAB, 0x3F328B82, 0x7718CF82, 0x59A2CEA6, 0x04EE002E, 0x89FE78E6, 0x3FAB0950, 0x325FF6C2, 0x81383F05, 0x6963C5C8, 0x76CB5AD6, 0xD49974C9, 0xCA180DCF, 0x380782D5, 0xC7FA5CF6, 0x8AC31511, 0x35E79E13, 0x47DA91D0, 0xF40F9086, 0xA7E2419E, 0x31366241, 0x051EF495, 0xAA573B04, 0x4A805D8D, 0x548300D0, 0x00322A3C, 0xBF64CDDF, 0xBA57A68E, 0x75C6372B, 0x50AFD341, 0xA7C13275, 0x915A0BF5, 0x6B54BFAB, 0x2B0B1426, 0xAB4CC9D7, 0x449CCD82, 0xF7FBF265, 0xAB85C5F3, 0x1B55DB94, 0xAAD4E324, 0xCFA4BD3F, 0x2DEAA3E2, 0x9E204D02, 0xC8BD25AC, 0xEADF55B3, 0xD5BD9E98, 0xE31231B2, 0x2AD5AD6C, 0x954329DE, 0xADBE4528, 0xD8710F69, 0xAA51C90F, 0xAA786BF6, 0x22513F1E, 0xAA51A79B, 0x2AD344CC, 0x7B5A41F0, 0xD37CFBAD, 0x1B069505, 0x41ECE491, 0xB4C332E6, 0x032268D4, 0xC9600ACC, 0xCE387E6D, 0xBF6BB16C, 0x6A70FB78, 0x0D03D9C9, 0xD4DF39DE, 0xE01063DA, 0x4736F464, 0x5AD328D8, 0xB347CC96, 0x75BB0FC3, 0x98511BFB, 0x4FFBCC35, 0xB58BCF6A, 0xE11F0ABC, 0xBFC5FE4A, 0xA70AEC10, 0xAC39570A, 0x3F04442F, 0x6188B153, 0xE0397A2E, 0x5727CB79, 0x9CEB418F, 0x1CACD68D, 0x2AD37C96, 0x0175CB9D, 0xC69DFF09, 0xC75B65F0, 0xD9DB40D8, 0xEC0E7779, 0x4744EAD4, 0xB11C3274, 0xDD24CB9E, 0x7E1C54BD, 0xF01144F9, 0xD2240EB1, 0x9675B3FD, 0xA3AC3755, 0xD47C27AF, 0x51C85F4D, 0x56907596, 0xA5BB15E6, 0x580304F0, 0xCA042CF1, 0x011A37EA, 0x8DBFAADB, 0x35BA3E4A, 0x3526FFA0, 0xC37B4D09, 0xBC306ED9, 0x98A52666, 0x5648F725, 0xFF5E569D, 0x0CED63D0, 0x7C63B2CF, 0x700B45E1, 0xD5EA50F1, 0x85A92872, 0xAF1FBDA7, 0xD4234870, 0xA7870BF3, 0x2D3B4D79, 0x42E04198, 0x0CD0EDE7, 0x26470DB8, 0xF881814C, 0x474D6AD7, 0x7C0C5E5C, 0xD1231959, 0x381B7298, 0xF5D2F4DB, 0xAB838653, 0x6E2F1E23, 0x83719C9E, 0xBD91E046, 0x9A56456E, 0xDC39200C, 0x20C8C571, 0x962BDA1C, 0xE1E696FF, 0xB141AB08, 0x7CCA89B9, 0x1A69E783, 0x02CC4843, 0xA2F7C579, 0x429EF47D, 0x427B169C, 0x5AC9F049, 0xDD8F0F00, 0x5C8165BF }; const u32bit CAST_SBOX2[256] = { 0x1F201094, 0xEF0BA75B, 0x69E3CF7E, 0x393F4380, 0xFE61CF7A, 0xEEC5207A, 0x55889C94, 0x72FC0651, 0xADA7EF79, 0x4E1D7235, 0xD55A63CE, 0xDE0436BA, 0x99C430EF, 0x5F0C0794, 0x18DCDB7D, 0xA1D6EFF3, 0xA0B52F7B, 0x59E83605, 0xEE15B094, 0xE9FFD909, 0xDC440086, 0xEF944459, 0xBA83CCB3, 0xE0C3CDFB, 0xD1DA4181, 0x3B092AB1, 0xF997F1C1, 0xA5E6CF7B, 0x01420DDB, 0xE4E7EF5B, 0x25A1FF41, 0xE180F806, 0x1FC41080, 0x179BEE7A, 0xD37AC6A9, 0xFE5830A4, 0x98DE8B7F, 0x77E83F4E, 0x79929269, 0x24FA9F7B, 0xE113C85B, 0xACC40083, 0xD7503525, 0xF7EA615F, 0x62143154, 0x0D554B63, 0x5D681121, 0xC866C359, 0x3D63CF73, 0xCEE234C0, 0xD4D87E87, 0x5C672B21, 0x071F6181, 0x39F7627F, 0x361E3084, 0xE4EB573B, 0x602F64A4, 0xD63ACD9C, 0x1BBC4635, 0x9E81032D, 0x2701F50C, 0x99847AB4, 0xA0E3DF79, 0xBA6CF38C, 0x10843094, 0x2537A95E, 0xF46F6FFE, 0xA1FF3B1F, 0x208CFB6A, 0x8F458C74, 0xD9E0A227, 0x4EC73A34, 0xFC884F69, 0x3E4DE8DF, 0xEF0E0088, 0x3559648D, 0x8A45388C, 0x1D804366, 0x721D9BFD, 0xA58684BB, 0xE8256333, 0x844E8212, 0x128D8098, 0xFED33FB4, 0xCE280AE1, 0x27E19BA5, 0xD5A6C252, 0xE49754BD, 0xC5D655DD, 0xEB667064, 0x77840B4D, 0xA1B6A801, 0x84DB26A9, 0xE0B56714, 0x21F043B7, 0xE5D05860, 0x54F03084, 0x066FF472, 0xA31AA153, 0xDADC4755, 0xB5625DBF, 0x68561BE6, 0x83CA6B94, 0x2D6ED23B, 0xECCF01DB, 0xA6D3D0BA, 0xB6803D5C, 0xAF77A709, 0x33B4A34C, 0x397BC8D6, 0x5EE22B95, 0x5F0E5304, 0x81ED6F61, 0x20E74364, 0xB45E1378, 0xDE18639B, 0x881CA122, 0xB96726D1, 0x8049A7E8, 0x22B7DA7B, 0x5E552D25, 0x5272D237, 0x79D2951C, 0xC60D894C, 0x488CB402, 0x1BA4FE5B, 0xA4B09F6B, 0x1CA815CF, 0xA20C3005, 0x8871DF63, 0xB9DE2FCB, 0x0CC6C9E9, 0x0BEEFF53, 0xE3214517, 0xB4542835, 0x9F63293C, 0xEE41E729, 0x6E1D2D7C, 0x50045286, 0x1E6685F3, 0xF33401C6, 0x30A22C95, 0x31A70850, 0x60930F13, 0x73F98417, 0xA1269859, 0xEC645C44, 0x52C877A9, 0xCDFF33A6, 0xA02B1741, 0x7CBAD9A2, 0x2180036F, 0x50D99C08, 0xCB3F4861, 0xC26BD765, 0x64A3F6AB, 0x80342676, 0x25A75E7B, 0xE4E6D1FC, 0x20C710E6, 0xCDF0B680, 0x17844D3B, 0x31EEF84D, 0x7E0824E4, 0x2CCB49EB, 0x846A3BAE, 0x8FF77888, 0xEE5D60F6, 0x7AF75673, 0x2FDD5CDB, 0xA11631C1, 0x30F66F43, 0xB3FAEC54, 0x157FD7FA, 0xEF8579CC, 0xD152DE58, 0xDB2FFD5E, 0x8F32CE19, 0x306AF97A, 0x02F03EF8, 0x99319AD5, 0xC242FA0F, 0xA7E3EBB0, 0xC68E4906, 0xB8DA230C, 0x80823028, 0xDCDEF3C8, 0xD35FB171, 0x088A1BC8, 0xBEC0C560, 0x61A3C9E8, 0xBCA8F54D, 0xC72FEFFA, 0x22822E99, 0x82C570B4, 0xD8D94E89, 0x8B1C34BC, 0x301E16E6, 0x273BE979, 0xB0FFEAA6, 0x61D9B8C6, 0x00B24869, 0xB7FFCE3F, 0x08DC283B, 0x43DAF65A, 0xF7E19798, 0x7619B72F, 0x8F1C9BA4, 0xDC8637A0, 0x16A7D3B1, 0x9FC393B7, 0xA7136EEB, 0xC6BCC63E, 0x1A513742, 0xEF6828BC, 0x520365D6, 0x2D6A77AB, 0x3527ED4B, 0x821FD216, 0x095C6E2E, 0xDB92F2FB, 0x5EEA29CB, 0x145892F5, 0x91584F7F, 0x5483697B, 0x2667A8CC, 0x85196048, 0x8C4BACEA, 0x833860D4, 0x0D23E0F9, 0x6C387E8A, 0x0AE6D249, 0xB284600C, 0xD835731D, 0xDCB1C647, 0xAC4C56EA, 0x3EBD81B3, 0x230EABB0, 0x6438BC87, 0xF0B5B1FA, 0x8F5EA2B3, 0xFC184642, 0x0A036B7A, 0x4FB089BD, 0x649DA589, 0xA345415E, 0x5C038323, 0x3E5D3BB9, 0x43D79572, 0x7E6DD07C, 0x06DFDF1E, 0x6C6CC4EF, 0x7160A539, 0x73BFBE70, 0x83877605, 0x4523ECF1 }; const u32bit CAST_SBOX3[256] = { 0x8DEFC240, 0x25FA5D9F, 0xEB903DBF, 0xE810C907, 0x47607FFF, 0x369FE44B, 0x8C1FC644, 0xAECECA90, 0xBEB1F9BF, 0xEEFBCAEA, 0xE8CF1950, 0x51DF07AE, 0x920E8806, 0xF0AD0548, 0xE13C8D83, 0x927010D5, 0x11107D9F, 0x07647DB9, 0xB2E3E4D4, 0x3D4F285E, 0xB9AFA820, 0xFADE82E0, 0xA067268B, 0x8272792E, 0x553FB2C0, 0x489AE22B, 0xD4EF9794, 0x125E3FBC, 0x21FFFCEE, 0x825B1BFD, 0x9255C5ED, 0x1257A240, 0x4E1A8302, 0xBAE07FFF, 0x528246E7, 0x8E57140E, 0x3373F7BF, 0x8C9F8188, 0xA6FC4EE8, 0xC982B5A5, 0xA8C01DB7, 0x579FC264, 0x67094F31, 0xF2BD3F5F, 0x40FFF7C1, 0x1FB78DFC, 0x8E6BD2C1, 0x437BE59B, 0x99B03DBF, 0xB5DBC64B, 0x638DC0E6, 0x55819D99, 0xA197C81C, 0x4A012D6E, 0xC5884A28, 0xCCC36F71, 0xB843C213, 0x6C0743F1, 0x8309893C, 0x0FEDDD5F, 0x2F7FE850, 0xD7C07F7E, 0x02507FBF, 0x5AFB9A04, 0xA747D2D0, 0x1651192E, 0xAF70BF3E, 0x58C31380, 0x5F98302E, 0x727CC3C4, 0x0A0FB402, 0x0F7FEF82, 0x8C96FDAD, 0x5D2C2AAE, 0x8EE99A49, 0x50DA88B8, 0x8427F4A0, 0x1EAC5790, 0x796FB449, 0x8252DC15, 0xEFBD7D9B, 0xA672597D, 0xADA840D8, 0x45F54504, 0xFA5D7403, 0xE83EC305, 0x4F91751A, 0x925669C2, 0x23EFE941, 0xA903F12E, 0x60270DF2, 0x0276E4B6, 0x94FD6574, 0x927985B2, 0x8276DBCB, 0x02778176, 0xF8AF918D, 0x4E48F79E, 0x8F616DDF, 0xE29D840E, 0x842F7D83, 0x340CE5C8, 0x96BBB682, 0x93B4B148, 0xEF303CAB, 0x984FAF28, 0x779FAF9B, 0x92DC560D, 0x224D1E20, 0x8437AA88, 0x7D29DC96, 0x2756D3DC, 0x8B907CEE, 0xB51FD240, 0xE7C07CE3, 0xE566B4A1, 0xC3E9615E, 0x3CF8209D, 0x6094D1E3, 0xCD9CA341, 0x5C76460E, 0x00EA983B, 0xD4D67881, 0xFD47572C, 0xF76CEDD9, 0xBDA8229C, 0x127DADAA, 0x438A074E, 0x1F97C090, 0x081BDB8A, 0x93A07EBE, 0xB938CA15, 0x97B03CFF, 0x3DC2C0F8, 0x8D1AB2EC, 0x64380E51, 0x68CC7BFB, 0xD90F2788, 0x12490181, 0x5DE5FFD4, 0xDD7EF86A, 0x76A2E214, 0xB9A40368, 0x925D958F, 0x4B39FFFA, 0xBA39AEE9, 0xA4FFD30B, 0xFAF7933B, 0x6D498623, 0x193CBCFA, 0x27627545, 0x825CF47A, 0x61BD8BA0, 0xD11E42D1, 0xCEAD04F4, 0x127EA392, 0x10428DB7, 0x8272A972, 0x9270C4A8, 0x127DE50B, 0x285BA1C8, 0x3C62F44F, 0x35C0EAA5, 0xE805D231, 0x428929FB, 0xB4FCDF82, 0x4FB66A53, 0x0E7DC15B, 0x1F081FAB, 0x108618AE, 0xFCFD086D, 0xF9FF2889, 0x694BCC11, 0x236A5CAE, 0x12DECA4D, 0x2C3F8CC5, 0xD2D02DFE, 0xF8EF5896, 0xE4CF52DA, 0x95155B67, 0x494A488C, 0xB9B6A80C, 0x5C8F82BC, 0x89D36B45, 0x3A609437, 0xEC00C9A9, 0x44715253, 0x0A874B49, 0xD773BC40, 0x7C34671C, 0x02717EF6, 0x4FEB5536, 0xA2D02FFF, 0xD2BF60C4, 0xD43F03C0, 0x50B4EF6D, 0x07478CD1, 0x006E1888, 0xA2E53F55, 0xB9E6D4BC, 0xA2048016, 0x97573833, 0xD7207D67, 0xDE0F8F3D, 0x72F87B33, 0xABCC4F33, 0x7688C55D, 0x7B00A6B0, 0x947B0001, 0x570075D2, 0xF9BB88F8, 0x8942019E, 0x4264A5FF, 0x856302E0, 0x72DBD92B, 0xEE971B69, 0x6EA22FDE, 0x5F08AE2B, 0xAF7A616D, 0xE5C98767, 0xCF1FEBD2, 0x61EFC8C2, 0xF1AC2571, 0xCC8239C2, 0x67214CB8, 0xB1E583D1, 0xB7DC3E62, 0x7F10BDCE, 0xF90A5C38, 0x0FF0443D, 0x606E6DC6, 0x60543A49, 0x5727C148, 0x2BE98A1D, 0x8AB41738, 0x20E1BE24, 0xAF96DA0F, 0x68458425, 0x99833BE5, 0x600D457D, 0x282F9350, 0x8334B362, 0xD91D1120, 0x2B6D8DA0, 0x642B1E31, 0x9C305A00, 0x52BCE688, 0x1B03588A, 0xF7BAEFD5, 0x4142ED9C, 0xA4315C11, 0x83323EC5, 0xDFEF4636, 0xA133C501, 0xE9D3531C, 0xEE353783 }; const u32bit CAST_SBOX4[256] = { 0x9DB30420, 0x1FB6E9DE, 0xA7BE7BEF, 0xD273A298, 0x4A4F7BDB, 0x64AD8C57, 0x85510443, 0xFA020ED1, 0x7E287AFF, 0xE60FB663, 0x095F35A1, 0x79EBF120, 0xFD059D43, 0x6497B7B1, 0xF3641F63, 0x241E4ADF, 0x28147F5F, 0x4FA2B8CD, 0xC9430040, 0x0CC32220, 0xFDD30B30, 0xC0A5374F, 0x1D2D00D9, 0x24147B15, 0xEE4D111A, 0x0FCA5167, 0x71FF904C, 0x2D195FFE, 0x1A05645F, 0x0C13FEFE, 0x081B08CA, 0x05170121, 0x80530100, 0xE83E5EFE, 0xAC9AF4F8, 0x7FE72701, 0xD2B8EE5F, 0x06DF4261, 0xBB9E9B8A, 0x7293EA25, 0xCE84FFDF, 0xF5718801, 0x3DD64B04, 0xA26F263B, 0x7ED48400, 0x547EEBE6, 0x446D4CA0, 0x6CF3D6F5, 0x2649ABDF, 0xAEA0C7F5, 0x36338CC1, 0x503F7E93, 0xD3772061, 0x11B638E1, 0x72500E03, 0xF80EB2BB, 0xABE0502E, 0xEC8D77DE, 0x57971E81, 0xE14F6746, 0xC9335400, 0x6920318F, 0x081DBB99, 0xFFC304A5, 0x4D351805, 0x7F3D5CE3, 0xA6C866C6, 0x5D5BCCA9, 0xDAEC6FEA, 0x9F926F91, 0x9F46222F, 0x3991467D, 0xA5BF6D8E, 0x1143C44F, 0x43958302, 0xD0214EEB, 0x022083B8, 0x3FB6180C, 0x18F8931E, 0x281658E6, 0x26486E3E, 0x8BD78A70, 0x7477E4C1, 0xB506E07C, 0xF32D0A25, 0x79098B02, 0xE4EABB81, 0x28123B23, 0x69DEAD38, 0x1574CA16, 0xDF871B62, 0x211C40B7, 0xA51A9EF9, 0x0014377B, 0x041E8AC8, 0x09114003, 0xBD59E4D2, 0xE3D156D5, 0x4FE876D5, 0x2F91A340, 0x557BE8DE, 0x00EAE4A7, 0x0CE5C2EC, 0x4DB4BBA6, 0xE756BDFF, 0xDD3369AC, 0xEC17B035, 0x06572327, 0x99AFC8B0, 0x56C8C391, 0x6B65811C, 0x5E146119, 0x6E85CB75, 0xBE07C002, 0xC2325577, 0x893FF4EC, 0x5BBFC92D, 0xD0EC3B25, 0xB7801AB7, 0x8D6D3B24, 0x20C763EF, 0xC366A5FC, 0x9C382880, 0x0ACE3205, 0xAAC9548A, 0xECA1D7C7, 0x041AFA32, 0x1D16625A, 0x6701902C, 0x9B757A54, 0x31D477F7, 0x9126B031, 0x36CC6FDB, 0xC70B8B46, 0xD9E66A48, 0x56E55A79, 0x026A4CEB, 0x52437EFF, 0x2F8F76B4, 0x0DF980A5, 0x8674CDE3, 0xEDDA04EB, 0x17A9BE04, 0x2C18F4DF, 0xB7747F9D, 0xAB2AF7B4, 0xEFC34D20, 0x2E096B7C, 0x1741A254, 0xE5B6A035, 0x213D42F6, 0x2C1C7C26, 0x61C2F50F, 0x6552DAF9, 0xD2C231F8, 0x25130F69, 0xD8167FA2, 0x0418F2C8, 0x001A96A6, 0x0D1526AB, 0x63315C21, 0x5E0A72EC, 0x49BAFEFD, 0x187908D9, 0x8D0DBD86, 0x311170A7, 0x3E9B640C, 0xCC3E10D7, 0xD5CAD3B6, 0x0CAEC388, 0xF73001E1, 0x6C728AFF, 0x71EAE2A1, 0x1F9AF36E, 0xCFCBD12F, 0xC1DE8417, 0xAC07BE6B, 0xCB44A1D8, 0x8B9B0F56, 0x013988C3, 0xB1C52FCA, 0xB4BE31CD, 0xD8782806, 0x12A3A4E2, 0x6F7DE532, 0x58FD7EB6, 0xD01EE900, 0x24ADFFC2, 0xF4990FC5, 0x9711AAC5, 0x001D7B95, 0x82E5E7D2, 0x109873F6, 0x00613096, 0xC32D9521, 0xADA121FF, 0x29908415, 0x7FBB977F, 0xAF9EB3DB, 0x29C9ED2A, 0x5CE2A465, 0xA730F32C, 0xD0AA3FE8, 0x8A5CC091, 0xD49E2CE7, 0x0CE454A9, 0xD60ACD86, 0x015F1919, 0x77079103, 0xDEA03AF6, 0x78A8565E, 0xDEE356DF, 0x21F05CBE, 0x8B75E387, 0xB3C50651, 0xB8A5C3EF, 0xD8EEB6D2, 0xE523BE77, 0xC2154529, 0x2F69EFDF, 0xAFE67AFB, 0xF470C4B2, 0xF3E0EB5B, 0xD6CC9876, 0x39E4460C, 0x1FDA8538, 0x1987832F, 0xCA007367, 0xA99144F8, 0x296B299E, 0x492FC295, 0x9266BEAB, 0xB5676E69, 0x9BD3DDDA, 0xDF7E052F, 0xDB25701C, 0x1B5E51EE, 0xF65324E6, 0x6AFCE36C, 0x0316CC04, 0x8644213E, 0xB7DC59D0, 0x7965291F, 0xCCD6FD43, 0x41823979, 0x932BCDF6, 0xB657C34D, 0x4EDFD282, 0x7AE5290C, 0x3CB9536B, 0x851E20FE, 0x9833557E, 0x13ECF0B0, 0xD3FFB372, 0x3F85C5C1, 0x0AEF7ED2 }; const u32bit CAST_128::S5[256] = { 0x7EC90C04, 0x2C6E74B9, 0x9B0E66DF, 0xA6337911, 0xB86A7FFF, 0x1DD358F5, 0x44DD9D44, 0x1731167F, 0x08FBF1FA, 0xE7F511CC, 0xD2051B00, 0x735ABA00, 0x2AB722D8, 0x386381CB, 0xACF6243A, 0x69BEFD7A, 0xE6A2E77F, 0xF0C720CD, 0xC4494816, 0xCCF5C180, 0x38851640, 0x15B0A848, 0xE68B18CB, 0x4CAADEFF, 0x5F480A01, 0x0412B2AA, 0x259814FC, 0x41D0EFE2, 0x4E40B48D, 0x248EB6FB, 0x8DBA1CFE, 0x41A99B02, 0x1A550A04, 0xBA8F65CB, 0x7251F4E7, 0x95A51725, 0xC106ECD7, 0x97A5980A, 0xC539B9AA, 0x4D79FE6A, 0xF2F3F763, 0x68AF8040, 0xED0C9E56, 0x11B4958B, 0xE1EB5A88, 0x8709E6B0, 0xD7E07156, 0x4E29FEA7, 0x6366E52D, 0x02D1C000, 0xC4AC8E05, 0x9377F571, 0x0C05372A, 0x578535F2, 0x2261BE02, 0xD642A0C9, 0xDF13A280, 0x74B55BD2, 0x682199C0, 0xD421E5EC, 0x53FB3CE8, 0xC8ADEDB3, 0x28A87FC9, 0x3D959981, 0x5C1FF900, 0xFE38D399, 0x0C4EFF0B, 0x062407EA, 0xAA2F4FB1, 0x4FB96976, 0x90C79505, 0xB0A8A774, 0xEF55A1FF, 0xE59CA2C2, 0xA6B62D27, 0xE66A4263, 0xDF65001F, 0x0EC50966, 0xDFDD55BC, 0x29DE0655, 0x911E739A, 0x17AF8975, 0x32C7911C, 0x89F89468, 0x0D01E980, 0x524755F4, 0x03B63CC9, 0x0CC844B2, 0xBCF3F0AA, 0x87AC36E9, 0xE53A7426, 0x01B3D82B, 0x1A9E7449, 0x64EE2D7E, 0xCDDBB1DA, 0x01C94910, 0xB868BF80, 0x0D26F3FD, 0x9342EDE7, 0x04A5C284, 0x636737B6, 0x50F5B616, 0xF24766E3, 0x8ECA36C1, 0x136E05DB, 0xFEF18391, 0xFB887A37, 0xD6E7F7D4, 0xC7FB7DC9, 0x3063FCDF, 0xB6F589DE, 0xEC2941DA, 0x26E46695, 0xB7566419, 0xF654EFC5, 0xD08D58B7, 0x48925401, 0xC1BACB7F, 0xE5FF550F, 0xB6083049, 0x5BB5D0E8, 0x87D72E5A, 0xAB6A6EE1, 0x223A66CE, 0xC62BF3CD, 0x9E0885F9, 0x68CB3E47, 0x086C010F, 0xA21DE820, 0xD18B69DE, 0xF3F65777, 0xFA02C3F6, 0x407EDAC3, 0xCBB3D550, 0x1793084D, 0xB0D70EBA, 0x0AB378D5, 0xD951FB0C, 0xDED7DA56, 0x4124BBE4, 0x94CA0B56, 0x0F5755D1, 0xE0E1E56E, 0x6184B5BE, 0x580A249F, 0x94F74BC0, 0xE327888E, 0x9F7B5561, 0xC3DC0280, 0x05687715, 0x646C6BD7, 0x44904DB3, 0x66B4F0A3, 0xC0F1648A, 0x697ED5AF, 0x49E92FF6, 0x309E374F, 0x2CB6356A, 0x85808573, 0x4991F840, 0x76F0AE02, 0x083BE84D, 0x28421C9A, 0x44489406, 0x736E4CB8, 0xC1092910, 0x8BC95FC6, 0x7D869CF4, 0x134F616F, 0x2E77118D, 0xB31B2BE1, 0xAA90B472, 0x3CA5D717, 0x7D161BBA, 0x9CAD9010, 0xAF462BA2, 0x9FE459D2, 0x45D34559, 0xD9F2DA13, 0xDBC65487, 0xF3E4F94E, 0x176D486F, 0x097C13EA, 0x631DA5C7, 0x445F7382, 0x175683F4, 0xCDC66A97, 0x70BE0288, 0xB3CDCF72, 0x6E5DD2F3, 0x20936079, 0x459B80A5, 0xBE60E2DB, 0xA9C23101, 0xEBA5315C, 0x224E42F2, 0x1C5C1572, 0xF6721B2C, 0x1AD2FFF3, 0x8C25404E, 0x324ED72F, 0x4067B7FD, 0x0523138E, 0x5CA3BC78, 0xDC0FD66E, 0x75922283, 0x784D6B17, 0x58EBB16E, 0x44094F85, 0x3F481D87, 0xFCFEAE7B, 0x77B5FF76, 0x8C2302BF, 0xAAF47556, 0x5F46B02A, 0x2B092801, 0x3D38F5F7, 0x0CA81F36, 0x52AF4A8A, 0x66D5E7C0, 0xDF3B0874, 0x95055110, 0x1B5AD7A8, 0xF61ED5AD, 0x6CF6E479, 0x20758184, 0xD0CEFA65, 0x88F7BE58, 0x4A046826, 0x0FF6F8F3, 0xA09C7F70, 0x5346ABA0, 0x5CE96C28, 0xE176EDA3, 0x6BAC307F, 0x376829D2, 0x85360FA9, 0x17E3FE2A, 0x24B79767, 0xF5A96B20, 0xD6CD2595, 0x68FF1EBF, 0x7555442C, 0xF19F06BE, 0xF9E0659A, 0xEEB9491D, 0x34010718, 0xBB30CAB8, 0xE822FE15, 0x88570983, 0x750E6249, 0xDA627E55, 0x5E76FFA8, 0xB1534546, 0x6D47DE08, 0xEFE9E7D4 }; const u32bit CAST_128::S6[256] = { 0xF6FA8F9D, 0x2CAC6CE1, 0x4CA34867, 0xE2337F7C, 0x95DB08E7, 0x016843B4, 0xECED5CBC, 0x325553AC, 0xBF9F0960, 0xDFA1E2ED, 0x83F0579D, 0x63ED86B9, 0x1AB6A6B8, 0xDE5EBE39, 0xF38FF732, 0x8989B138, 0x33F14961, 0xC01937BD, 0xF506C6DA, 0xE4625E7E, 0xA308EA99, 0x4E23E33C, 0x79CBD7CC, 0x48A14367, 0xA3149619, 0xFEC94BD5, 0xA114174A, 0xEAA01866, 0xA084DB2D, 0x09A8486F, 0xA888614A, 0x2900AF98, 0x01665991, 0xE1992863, 0xC8F30C60, 0x2E78EF3C, 0xD0D51932, 0xCF0FEC14, 0xF7CA07D2, 0xD0A82072, 0xFD41197E, 0x9305A6B0, 0xE86BE3DA, 0x74BED3CD, 0x372DA53C, 0x4C7F4448, 0xDAB5D440, 0x6DBA0EC3, 0x083919A7, 0x9FBAEED9, 0x49DBCFB0, 0x4E670C53, 0x5C3D9C01, 0x64BDB941, 0x2C0E636A, 0xBA7DD9CD, 0xEA6F7388, 0xE70BC762, 0x35F29ADB, 0x5C4CDD8D, 0xF0D48D8C, 0xB88153E2, 0x08A19866, 0x1AE2EAC8, 0x284CAF89, 0xAA928223, 0x9334BE53, 0x3B3A21BF, 0x16434BE3, 0x9AEA3906, 0xEFE8C36E, 0xF890CDD9, 0x80226DAE, 0xC340A4A3, 0xDF7E9C09, 0xA694A807, 0x5B7C5ECC, 0x221DB3A6, 0x9A69A02F, 0x68818A54, 0xCEB2296F, 0x53C0843A, 0xFE893655, 0x25BFE68A, 0xB4628ABC, 0xCF222EBF, 0x25AC6F48, 0xA9A99387, 0x53BDDB65, 0xE76FFBE7, 0xE967FD78, 0x0BA93563, 0x8E342BC1, 0xE8A11BE9, 0x4980740D, 0xC8087DFC, 0x8DE4BF99, 0xA11101A0, 0x7FD37975, 0xDA5A26C0, 0xE81F994F, 0x9528CD89, 0xFD339FED, 0xB87834BF, 0x5F04456D, 0x22258698, 0xC9C4C83B, 0x2DC156BE, 0x4F628DAA, 0x57F55EC5, 0xE2220ABE, 0xD2916EBF, 0x4EC75B95, 0x24F2C3C0, 0x42D15D99, 0xCD0D7FA0, 0x7B6E27FF, 0xA8DC8AF0, 0x7345C106, 0xF41E232F, 0x35162386, 0xE6EA8926, 0x3333B094, 0x157EC6F2, 0x372B74AF, 0x692573E4, 0xE9A9D848, 0xF3160289, 0x3A62EF1D, 0xA787E238, 0xF3A5F676, 0x74364853, 0x20951063, 0x4576698D, 0xB6FAD407, 0x592AF950, 0x36F73523, 0x4CFB6E87, 0x7DA4CEC0, 0x6C152DAA, 0xCB0396A8, 0xC50DFE5D, 0xFCD707AB, 0x0921C42F, 0x89DFF0BB, 0x5FE2BE78, 0x448F4F33, 0x754613C9, 0x2B05D08D, 0x48B9D585, 0xDC049441, 0xC8098F9B, 0x7DEDE786, 0xC39A3373, 0x42410005, 0x6A091751, 0x0EF3C8A6, 0x890072D6, 0x28207682, 0xA9A9F7BE, 0xBF32679D, 0xD45B5B75, 0xB353FD00, 0xCBB0E358, 0x830F220A, 0x1F8FB214, 0xD372CF08, 0xCC3C4A13, 0x8CF63166, 0x061C87BE, 0x88C98F88, 0x6062E397, 0x47CF8E7A, 0xB6C85283, 0x3CC2ACFB, 0x3FC06976, 0x4E8F0252, 0x64D8314D, 0xDA3870E3, 0x1E665459, 0xC10908F0, 0x513021A5, 0x6C5B68B7, 0x822F8AA0, 0x3007CD3E, 0x74719EEF, 0xDC872681, 0x073340D4, 0x7E432FD9, 0x0C5EC241, 0x8809286C, 0xF592D891, 0x08A930F6, 0x957EF305, 0xB7FBFFBD, 0xC266E96F, 0x6FE4AC98, 0xB173ECC0, 0xBC60B42A, 0x953498DA, 0xFBA1AE12, 0x2D4BD736, 0x0F25FAAB, 0xA4F3FCEB, 0xE2969123, 0x257F0C3D, 0x9348AF49, 0x361400BC, 0xE8816F4A, 0x3814F200, 0xA3F94043, 0x9C7A54C2, 0xBC704F57, 0xDA41E7F9, 0xC25AD33A, 0x54F4A084, 0xB17F5505, 0x59357CBE, 0xEDBD15C8, 0x7F97C5AB, 0xBA5AC7B5, 0xB6F6DEAF, 0x3A479C3A, 0x5302DA25, 0x653D7E6A, 0x54268D49, 0x51A477EA, 0x5017D55B, 0xD7D25D88, 0x44136C76, 0x0404A8C8, 0xB8E5A121, 0xB81A928A, 0x60ED5869, 0x97C55B96, 0xEAEC991B, 0x29935913, 0x01FDB7F1, 0x088E8DFA, 0x9AB6F6F5, 0x3B4CBF9F, 0x4A5DE3AB, 0xE6051D35, 0xA0E1D855, 0xD36B4CF1, 0xF544EDEB, 0xB0E93524, 0xBEBB8FBD, 0xA2D762CF, 0x49C92F54, 0x38B5F331, 0x7128A454, 0x48392905, 0xA65B1DB8, 0x851C97BD, 0xD675CF2F }; const u32bit CAST_128::S7[256] = { 0x85E04019, 0x332BF567, 0x662DBFFF, 0xCFC65693, 0x2A8D7F6F, 0xAB9BC912, 0xDE6008A1, 0x2028DA1F, 0x0227BCE7, 0x4D642916, 0x18FAC300, 0x50F18B82, 0x2CB2CB11, 0xB232E75C, 0x4B3695F2, 0xB28707DE, 0xA05FBCF6, 0xCD4181E9, 0xE150210C, 0xE24EF1BD, 0xB168C381, 0xFDE4E789, 0x5C79B0D8, 0x1E8BFD43, 0x4D495001, 0x38BE4341, 0x913CEE1D, 0x92A79C3F, 0x089766BE, 0xBAEEADF4, 0x1286BECF, 0xB6EACB19, 0x2660C200, 0x7565BDE4, 0x64241F7A, 0x8248DCA9, 0xC3B3AD66, 0x28136086, 0x0BD8DFA8, 0x356D1CF2, 0x107789BE, 0xB3B2E9CE, 0x0502AA8F, 0x0BC0351E, 0x166BF52A, 0xEB12FF82, 0xE3486911, 0xD34D7516, 0x4E7B3AFF, 0x5F43671B, 0x9CF6E037, 0x4981AC83, 0x334266CE, 0x8C9341B7, 0xD0D854C0, 0xCB3A6C88, 0x47BC2829, 0x4725BA37, 0xA66AD22B, 0x7AD61F1E, 0x0C5CBAFA, 0x4437F107, 0xB6E79962, 0x42D2D816, 0x0A961288, 0xE1A5C06E, 0x13749E67, 0x72FC081A, 0xB1D139F7, 0xF9583745, 0xCF19DF58, 0xBEC3F756, 0xC06EBA30, 0x07211B24, 0x45C28829, 0xC95E317F, 0xBC8EC511, 0x38BC46E9, 0xC6E6FA14, 0xBAE8584A, 0xAD4EBC46, 0x468F508B, 0x7829435F, 0xF124183B, 0x821DBA9F, 0xAFF60FF4, 0xEA2C4E6D, 0x16E39264, 0x92544A8B, 0x009B4FC3, 0xABA68CED, 0x9AC96F78, 0x06A5B79A, 0xB2856E6E, 0x1AEC3CA9, 0xBE838688, 0x0E0804E9, 0x55F1BE56, 0xE7E5363B, 0xB3A1F25D, 0xF7DEBB85, 0x61FE033C, 0x16746233, 0x3C034C28, 0xDA6D0C74, 0x79AAC56C, 0x3CE4E1AD, 0x51F0C802, 0x98F8F35A, 0x1626A49F, 0xEED82B29, 0x1D382FE3, 0x0C4FB99A, 0xBB325778, 0x3EC6D97B, 0x6E77A6A9, 0xCB658B5C, 0xD45230C7, 0x2BD1408B, 0x60C03EB7, 0xB9068D78, 0xA33754F4, 0xF430C87D, 0xC8A71302, 0xB96D8C32, 0xEBD4E7BE, 0xBE8B9D2D, 0x7979FB06, 0xE7225308, 0x8B75CF77, 0x11EF8DA4, 0xE083C858, 0x8D6B786F, 0x5A6317A6, 0xFA5CF7A0, 0x5DDA0033, 0xF28EBFB0, 0xF5B9C310, 0xA0EAC280, 0x08B9767A, 0xA3D9D2B0, 0x79D34217, 0x021A718D, 0x9AC6336A, 0x2711FD60, 0x438050E3, 0x069908A8, 0x3D7FEDC4, 0x826D2BEF, 0x4EEB8476, 0x488DCF25, 0x36C9D566, 0x28E74E41, 0xC2610ACA, 0x3D49A9CF, 0xBAE3B9DF, 0xB65F8DE6, 0x92AEAF64, 0x3AC7D5E6, 0x9EA80509, 0xF22B017D, 0xA4173F70, 0xDD1E16C3, 0x15E0D7F9, 0x50B1B887, 0x2B9F4FD5, 0x625ABA82, 0x6A017962, 0x2EC01B9C, 0x15488AA9, 0xD716E740, 0x40055A2C, 0x93D29A22, 0xE32DBF9A, 0x058745B9, 0x3453DC1E, 0xD699296E, 0x496CFF6F, 0x1C9F4986, 0xDFE2ED07, 0xB87242D1, 0x19DE7EAE, 0x053E561A, 0x15AD6F8C, 0x66626C1C, 0x7154C24C, 0xEA082B2A, 0x93EB2939, 0x17DCB0F0, 0x58D4F2AE, 0x9EA294FB, 0x52CF564C, 0x9883FE66, 0x2EC40581, 0x763953C3, 0x01D6692E, 0xD3A0C108, 0xA1E7160E, 0xE4F2DFA6, 0x693ED285, 0x74904698, 0x4C2B0EDD, 0x4F757656, 0x5D393378, 0xA132234F, 0x3D321C5D, 0xC3F5E194, 0x4B269301, 0xC79F022F, 0x3C997E7E, 0x5E4F9504, 0x3FFAFBBD, 0x76F7AD0E, 0x296693F4, 0x3D1FCE6F, 0xC61E45BE, 0xD3B5AB34, 0xF72BF9B7, 0x1B0434C0, 0x4E72B567, 0x5592A33D, 0xB5229301, 0xCFD2A87F, 0x60AEB767, 0x1814386B, 0x30BCC33D, 0x38A0C07D, 0xFD1606F2, 0xC363519B, 0x589DD390, 0x5479F8E6, 0x1CB8D647, 0x97FD61A9, 0xEA7759F4, 0x2D57539D, 0x569A58CF, 0xE84E63AD, 0x462E1B78, 0x6580F87E, 0xF3817914, 0x91DA55F4, 0x40A230F3, 0xD1988F35, 0xB6E318D2, 0x3FFA50BC, 0x3D40F021, 0xC3C0BDAE, 0x4958C24C, 0x518F36B2, 0x84B1D370, 0x0FEDCE83, 0x878DDADA, 0xF2A279C7, 0x94E01BE8, 0x90716F4B, 0x954B8AA3 }; const u32bit CAST_128::S8[256] = { 0xE216300D, 0xBBDDFFFC, 0xA7EBDABD, 0x35648095, 0x7789F8B7, 0xE6C1121B, 0x0E241600, 0x052CE8B5, 0x11A9CFB0, 0xE5952F11, 0xECE7990A, 0x9386D174, 0x2A42931C, 0x76E38111, 0xB12DEF3A, 0x37DDDDFC, 0xDE9ADEB1, 0x0A0CC32C, 0xBE197029, 0x84A00940, 0xBB243A0F, 0xB4D137CF, 0xB44E79F0, 0x049EEDFD, 0x0B15A15D, 0x480D3168, 0x8BBBDE5A, 0x669DED42, 0xC7ECE831, 0x3F8F95E7, 0x72DF191B, 0x7580330D, 0x94074251, 0x5C7DCDFA, 0xABBE6D63, 0xAA402164, 0xB301D40A, 0x02E7D1CA, 0x53571DAE, 0x7A3182A2, 0x12A8DDEC, 0xFDAA335D, 0x176F43E8, 0x71FB46D4, 0x38129022, 0xCE949AD4, 0xB84769AD, 0x965BD862, 0x82F3D055, 0x66FB9767, 0x15B80B4E, 0x1D5B47A0, 0x4CFDE06F, 0xC28EC4B8, 0x57E8726E, 0x647A78FC, 0x99865D44, 0x608BD593, 0x6C200E03, 0x39DC5FF6, 0x5D0B00A3, 0xAE63AFF2, 0x7E8BD632, 0x70108C0C, 0xBBD35049, 0x2998DF04, 0x980CF42A, 0x9B6DF491, 0x9E7EDD53, 0x06918548, 0x58CB7E07, 0x3B74EF2E, 0x522FFFB1, 0xD24708CC, 0x1C7E27CD, 0xA4EB215B, 0x3CF1D2E2, 0x19B47A38, 0x424F7618, 0x35856039, 0x9D17DEE7, 0x27EB35E6, 0xC9AFF67B, 0x36BAF5B8, 0x09C467CD, 0xC18910B1, 0xE11DBF7B, 0x06CD1AF8, 0x7170C608, 0x2D5E3354, 0xD4DE495A, 0x64C6D006, 0xBCC0C62C, 0x3DD00DB3, 0x708F8F34, 0x77D51B42, 0x264F620F, 0x24B8D2BF, 0x15C1B79E, 0x46A52564, 0xF8D7E54E, 0x3E378160, 0x7895CDA5, 0x859C15A5, 0xE6459788, 0xC37BC75F, 0xDB07BA0C, 0x0676A3AB, 0x7F229B1E, 0x31842E7B, 0x24259FD7, 0xF8BEF472, 0x835FFCB8, 0x6DF4C1F2, 0x96F5B195, 0xFD0AF0FC, 0xB0FE134C, 0xE2506D3D, 0x4F9B12EA, 0xF215F225, 0xA223736F, 0x9FB4C428, 0x25D04979, 0x34C713F8, 0xC4618187, 0xEA7A6E98, 0x7CD16EFC, 0x1436876C, 0xF1544107, 0xBEDEEE14, 0x56E9AF27, 0xA04AA441, 0x3CF7C899, 0x92ECBAE6, 0xDD67016D, 0x151682EB, 0xA842EEDF, 0xFDBA60B4, 0xF1907B75, 0x20E3030F, 0x24D8C29E, 0xE139673B, 0xEFA63FB8, 0x71873054, 0xB6F2CF3B, 0x9F326442, 0xCB15A4CC, 0xB01A4504, 0xF1E47D8D, 0x844A1BE5, 0xBAE7DFDC, 0x42CBDA70, 0xCD7DAE0A, 0x57E85B7A, 0xD53F5AF6, 0x20CF4D8C, 0xCEA4D428, 0x79D130A4, 0x3486EBFB, 0x33D3CDDC, 0x77853B53, 0x37EFFCB5, 0xC5068778, 0xE580B3E6, 0x4E68B8F4, 0xC5C8B37E, 0x0D809EA2, 0x398FEB7C, 0x132A4F94, 0x43B7950E, 0x2FEE7D1C, 0x223613BD, 0xDD06CAA2, 0x37DF932B, 0xC4248289, 0xACF3EBC3, 0x5715F6B7, 0xEF3478DD, 0xF267616F, 0xC148CBE4, 0x9052815E, 0x5E410FAB, 0xB48A2465, 0x2EDA7FA4, 0xE87B40E4, 0xE98EA084, 0x5889E9E1, 0xEFD390FC, 0xDD07D35B, 0xDB485694, 0x38D7E5B2, 0x57720101, 0x730EDEBC, 0x5B643113, 0x94917E4F, 0x503C2FBA, 0x646F1282, 0x7523D24A, 0xE0779695, 0xF9C17A8F, 0x7A5B2121, 0xD187B896, 0x29263A4D, 0xBA510CDF, 0x81F47C9F, 0xAD1163ED, 0xEA7B5965, 0x1A00726E, 0x11403092, 0x00DA6D77, 0x4A0CDD61, 0xAD1F4603, 0x605BDFB0, 0x9EEDC364, 0x22EBE6A8, 0xCEE7D28A, 0xA0E736A0, 0x5564A6B9, 0x10853209, 0xC7EB8F37, 0x2DE705CA, 0x8951570F, 0xDF09822B, 0xBD691A6C, 0xAA12E4F2, 0x87451C0F, 0xE0F6A27A, 0x3ADA4819, 0x4CF1764F, 0x0D771C2B, 0x67CDB156, 0x350D8384, 0x5938FA0F, 0x42399EF3, 0x36997B07, 0x0E84093D, 0x4AA93E61, 0x8360D87B, 0x1FA98B0C, 0x1149382C, 0xE97625A5, 0x0614D1B7, 0x0E25244B, 0x0C768347, 0x589E8D82, 0x0D2059D1, 0xA466BB1E, 0xF8DA0A82, 0x04F19130, 0xBA6E4EC0, 0x99265164, 0x1EE7230D, 0x50B2AD80, 0xEAEE6801, 0x8DB2A283, 0xEA8BF59E }; const u32bit CAST_256::KEY_MASK[192] = { 0x5A827999, 0xC95C653A, 0x383650DB, 0xA7103C7C, 0x15EA281D, 0x84C413BE, 0xF39DFF5F, 0x6277EB00, 0xD151D6A1, 0x402BC242, 0xAF05ADE3, 0x1DDF9984, 0x8CB98525, 0xFB9370C6, 0x6A6D5C67, 0xD9474808, 0x482133A9, 0xB6FB1F4A, 0x25D50AEB, 0x94AEF68C, 0x0388E22D, 0x7262CDCE, 0xE13CB96F, 0x5016A510, 0xBEF090B1, 0x2DCA7C52, 0x9CA467F3, 0x0B7E5394, 0x7A583F35, 0xE9322AD6, 0x580C1677, 0xC6E60218, 0x35BFEDB9, 0xA499D95A, 0x1373C4FB, 0x824DB09C, 0xF1279C3D, 0x600187DE, 0xCEDB737F, 0x3DB55F20, 0xAC8F4AC1, 0x1B693662, 0x8A432203, 0xF91D0DA4, 0x67F6F945, 0xD6D0E4E6, 0x45AAD087, 0xB484BC28, 0x235EA7C9, 0x9238936A, 0x01127F0B, 0x6FEC6AAC, 0xDEC6564D, 0x4DA041EE, 0xBC7A2D8F, 0x2B541930, 0x9A2E04D1, 0x0907F072, 0x77E1DC13, 0xE6BBC7B4, 0x5595B355, 0xC46F9EF6, 0x33498A97, 0xA2237638, 0x10FD61D9, 0x7FD74D7A, 0xEEB1391B, 0x5D8B24BC, 0xCC65105D, 0x3B3EFBFE, 0xAA18E79F, 0x18F2D340, 0x87CCBEE1, 0xF6A6AA82, 0x65809623, 0xD45A81C4, 0x43346D65, 0xB20E5906, 0x20E844A7, 0x8FC23048, 0xFE9C1BE9, 0x6D76078A, 0xDC4FF32B, 0x4B29DECC, 0xBA03CA6D, 0x28DDB60E, 0x97B7A1AF, 0x06918D50, 0x756B78F1, 0xE4456492, 0x531F5033, 0xC1F93BD4, 0x30D32775, 0x9FAD1316, 0x0E86FEB7, 0x7D60EA58, 0xEC3AD5F9, 0x5B14C19A, 0xC9EEAD3B, 0x38C898DC, 0xA7A2847D, 0x167C701E, 0x85565BBF, 0xF4304760, 0x630A3301, 0xD1E41EA2, 0x40BE0A43, 0xAF97F5E4, 0x1E71E185, 0x8D4BCD26, 0xFC25B8C7, 0x6AFFA468, 0xD9D99009, 0x48B37BAA, 0xB78D674B, 0x266752EC, 0x95413E8D, 0x041B2A2E, 0x72F515CF, 0xE1CF0170, 0x50A8ED11, 0xBF82D8B2, 0x2E5CC453, 0x9D36AFF4, 0x0C109B95, 0x7AEA8736, 0xE9C472D7, 0x589E5E78, 0xC7784A19, 0x365235BA, 0xA52C215B, 0x14060CFC, 0x82DFF89D, 0xF1B9E43E, 0x6093CFDF, 0xCF6DBB80, 0x3E47A721, 0xAD2192C2, 0x1BFB7E63, 0x8AD56A04, 0xF9AF55A5, 0x68894146, 0xD7632CE7, 0x463D1888, 0xB5170429, 0x23F0EFCA, 0x92CADB6B, 0x01A4C70C, 0x707EB2AD, 0xDF589E4E, 0x4E3289EF, 0xBD0C7590, 0x2BE66131, 0x9AC04CD2, 0x099A3873, 0x78742414, 0xE74E0FB5, 0x5627FB56, 0xC501E6F7, 0x33DBD298, 0xA2B5BE39, 0x118FA9DA, 0x8069957B, 0xEF43811C, 0x5E1D6CBD, 0xCCF7585E, 0x3BD143FF, 0xAAAB2FA0, 0x19851B41, 0x885F06E2, 0xF738F283, 0x6612DE24, 0xD4ECC9C5, 0x43C6B566, 0xB2A0A107, 0x217A8CA8, 0x90547849, 0xFF2E63EA, 0x6E084F8B, 0xDCE23B2C, 0x4BBC26CD, 0xBA96126E, 0x296FFE0F, 0x9849E9B0, 0x0723D551, 0x75FDC0F2, 0xE4D7AC93, 0x53B19834, 0xC28B83D5, 0x31656F76, 0xA03F5B17, 0x0F1946B8 }; const byte CAST_256::KEY_ROT[32] = { 0x13, 0x04, 0x15, 0x06, 0x17, 0x08, 0x19, 0x0A, 0x1B, 0x0C, 0x1D, 0x0E, 0x1F, 0x10, 0x01, 0x12, 0x03, 0x14, 0x05, 0x16, 0x07, 0x18, 0x09, 0x1A, 0x0B, 0x1C, 0x0D, 0x1E, 0x0F, 0x00, 0x11, 0x02 }; } /* * DES * (C) 1999-2008 Jack Lloyd * * Based on a public domain implemenation by Phil Karn (who in turn * credited Richard Outerbridge and Jim Gillogly) * * Distributed under the terms of the Botan license */ namespace Botan { namespace { /* * DES Key Schedule */ void des_key_schedule(u32bit round_key[32], const byte key[8]) { static const byte ROT[16] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 }; u32bit C = ((key[7] & 0x80) << 20) | ((key[6] & 0x80) << 19) | ((key[5] & 0x80) << 18) | ((key[4] & 0x80) << 17) | ((key[3] & 0x80) << 16) | ((key[2] & 0x80) << 15) | ((key[1] & 0x80) << 14) | ((key[0] & 0x80) << 13) | ((key[7] & 0x40) << 13) | ((key[6] & 0x40) << 12) | ((key[5] & 0x40) << 11) | ((key[4] & 0x40) << 10) | ((key[3] & 0x40) << 9) | ((key[2] & 0x40) << 8) | ((key[1] & 0x40) << 7) | ((key[0] & 0x40) << 6) | ((key[7] & 0x20) << 6) | ((key[6] & 0x20) << 5) | ((key[5] & 0x20) << 4) | ((key[4] & 0x20) << 3) | ((key[3] & 0x20) << 2) | ((key[2] & 0x20) << 1) | ((key[1] & 0x20) ) | ((key[0] & 0x20) >> 1) | ((key[7] & 0x10) >> 1) | ((key[6] & 0x10) >> 2) | ((key[5] & 0x10) >> 3) | ((key[4] & 0x10) >> 4); u32bit D = ((key[7] & 0x02) << 26) | ((key[6] & 0x02) << 25) | ((key[5] & 0x02) << 24) | ((key[4] & 0x02) << 23) | ((key[3] & 0x02) << 22) | ((key[2] & 0x02) << 21) | ((key[1] & 0x02) << 20) | ((key[0] & 0x02) << 19) | ((key[7] & 0x04) << 17) | ((key[6] & 0x04) << 16) | ((key[5] & 0x04) << 15) | ((key[4] & 0x04) << 14) | ((key[3] & 0x04) << 13) | ((key[2] & 0x04) << 12) | ((key[1] & 0x04) << 11) | ((key[0] & 0x04) << 10) | ((key[7] & 0x08) << 8) | ((key[6] & 0x08) << 7) | ((key[5] & 0x08) << 6) | ((key[4] & 0x08) << 5) | ((key[3] & 0x08) << 4) | ((key[2] & 0x08) << 3) | ((key[1] & 0x08) << 2) | ((key[0] & 0x08) << 1) | ((key[3] & 0x10) >> 1) | ((key[2] & 0x10) >> 2) | ((key[1] & 0x10) >> 3) | ((key[0] & 0x10) >> 4); for(size_t i = 0; i != 16; ++i) { C = ((C << ROT[i]) | (C >> (28-ROT[i]))) & 0x0FFFFFFF; D = ((D << ROT[i]) | (D >> (28-ROT[i]))) & 0x0FFFFFFF; round_key[2*i ] = ((C & 0x00000010) << 22) | ((C & 0x00000800) << 17) | ((C & 0x00000020) << 16) | ((C & 0x00004004) << 15) | ((C & 0x00000200) << 11) | ((C & 0x00020000) << 10) | ((C & 0x01000000) >> 6) | ((C & 0x00100000) >> 4) | ((C & 0x00010000) << 3) | ((C & 0x08000000) >> 2) | ((C & 0x00800000) << 1) | ((D & 0x00000010) << 8) | ((D & 0x00000002) << 7) | ((D & 0x00000001) << 2) | ((D & 0x00000200) ) | ((D & 0x00008000) >> 2) | ((D & 0x00000088) >> 3) | ((D & 0x00001000) >> 7) | ((D & 0x00080000) >> 9) | ((D & 0x02020000) >> 14) | ((D & 0x00400000) >> 21); round_key[2*i+1] = ((C & 0x00000001) << 28) | ((C & 0x00000082) << 18) | ((C & 0x00002000) << 14) | ((C & 0x00000100) << 10) | ((C & 0x00001000) << 9) | ((C & 0x00040000) << 6) | ((C & 0x02400000) << 4) | ((C & 0x00008000) << 2) | ((C & 0x00200000) >> 1) | ((C & 0x04000000) >> 10) | ((D & 0x00000020) << 6) | ((D & 0x00000100) ) | ((D & 0x00000800) >> 1) | ((D & 0x00000040) >> 3) | ((D & 0x00010000) >> 4) | ((D & 0x00000400) >> 5) | ((D & 0x00004000) >> 10) | ((D & 0x04000000) >> 13) | ((D & 0x00800000) >> 14) | ((D & 0x00100000) >> 18) | ((D & 0x01000000) >> 24) | ((D & 0x08000000) >> 26); } } /* * DES Encryption */ void des_encrypt(u32bit& L, u32bit& R, const u32bit round_key[32]) { for(size_t i = 0; i != 16; i += 2) { u32bit T0, T1; T0 = rotate_right(R, 4) ^ round_key[2*i]; T1 = R ^ round_key[2*i + 1]; L ^= DES_SPBOX1[get_byte(0, T0)] ^ DES_SPBOX2[get_byte(0, T1)] ^ DES_SPBOX3[get_byte(1, T0)] ^ DES_SPBOX4[get_byte(1, T1)] ^ DES_SPBOX5[get_byte(2, T0)] ^ DES_SPBOX6[get_byte(2, T1)] ^ DES_SPBOX7[get_byte(3, T0)] ^ DES_SPBOX8[get_byte(3, T1)]; T0 = rotate_right(L, 4) ^ round_key[2*i + 2]; T1 = L ^ round_key[2*i + 3]; R ^= DES_SPBOX1[get_byte(0, T0)] ^ DES_SPBOX2[get_byte(0, T1)] ^ DES_SPBOX3[get_byte(1, T0)] ^ DES_SPBOX4[get_byte(1, T1)] ^ DES_SPBOX5[get_byte(2, T0)] ^ DES_SPBOX6[get_byte(2, T1)] ^ DES_SPBOX7[get_byte(3, T0)] ^ DES_SPBOX8[get_byte(3, T1)]; } } /* * DES Decryption */ void des_decrypt(u32bit& L, u32bit& R, const u32bit round_key[32]) { for(size_t i = 16; i != 0; i -= 2) { u32bit T0, T1; T0 = rotate_right(R, 4) ^ round_key[2*i - 2]; T1 = R ^ round_key[2*i - 1]; L ^= DES_SPBOX1[get_byte(0, T0)] ^ DES_SPBOX2[get_byte(0, T1)] ^ DES_SPBOX3[get_byte(1, T0)] ^ DES_SPBOX4[get_byte(1, T1)] ^ DES_SPBOX5[get_byte(2, T0)] ^ DES_SPBOX6[get_byte(2, T1)] ^ DES_SPBOX7[get_byte(3, T0)] ^ DES_SPBOX8[get_byte(3, T1)]; T0 = rotate_right(L, 4) ^ round_key[2*i - 4]; T1 = L ^ round_key[2*i - 3]; R ^= DES_SPBOX1[get_byte(0, T0)] ^ DES_SPBOX2[get_byte(0, T1)] ^ DES_SPBOX3[get_byte(1, T0)] ^ DES_SPBOX4[get_byte(1, T1)] ^ DES_SPBOX5[get_byte(2, T0)] ^ DES_SPBOX6[get_byte(2, T1)] ^ DES_SPBOX7[get_byte(3, T0)] ^ DES_SPBOX8[get_byte(3, T1)]; } } } /* * DES Encryption */ void DES::encrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u64bit T = (DES_IPTAB1[in[0]] ) | (DES_IPTAB1[in[1]] << 1) | (DES_IPTAB1[in[2]] << 2) | (DES_IPTAB1[in[3]] << 3) | (DES_IPTAB1[in[4]] << 4) | (DES_IPTAB1[in[5]] << 5) | (DES_IPTAB1[in[6]] << 6) | (DES_IPTAB2[in[7]] ); u32bit L = static_cast<u32bit>(T >> 32); u32bit R = static_cast<u32bit>(T); des_encrypt(L, R, &round_key[0]); T = (DES_FPTAB1[get_byte(0, L)] << 5) | (DES_FPTAB1[get_byte(1, L)] << 3) | (DES_FPTAB1[get_byte(2, L)] << 1) | (DES_FPTAB2[get_byte(3, L)] << 1) | (DES_FPTAB1[get_byte(0, R)] << 4) | (DES_FPTAB1[get_byte(1, R)] << 2) | (DES_FPTAB1[get_byte(2, R)] ) | (DES_FPTAB2[get_byte(3, R)] ); T = rotate_left(T, 32); store_be(T, out); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * DES Decryption */ void DES::decrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u64bit T = (DES_IPTAB1[in[0]] ) | (DES_IPTAB1[in[1]] << 1) | (DES_IPTAB1[in[2]] << 2) | (DES_IPTAB1[in[3]] << 3) | (DES_IPTAB1[in[4]] << 4) | (DES_IPTAB1[in[5]] << 5) | (DES_IPTAB1[in[6]] << 6) | (DES_IPTAB2[in[7]] ); u32bit L = static_cast<u32bit>(T >> 32); u32bit R = static_cast<u32bit>(T); des_decrypt(L, R, &round_key[0]); T = (DES_FPTAB1[get_byte(0, L)] << 5) | (DES_FPTAB1[get_byte(1, L)] << 3) | (DES_FPTAB1[get_byte(2, L)] << 1) | (DES_FPTAB2[get_byte(3, L)] << 1) | (DES_FPTAB1[get_byte(0, R)] << 4) | (DES_FPTAB1[get_byte(1, R)] << 2) | (DES_FPTAB1[get_byte(2, R)] ) | (DES_FPTAB2[get_byte(3, R)] ); T = rotate_left(T, 32); store_be(T, out); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * DES Key Schedule */ void DES::key_schedule(const byte key[], size_t) { des_key_schedule(&round_key[0], key); } /* * TripleDES Encryption */ void TripleDES::encrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u64bit T = (DES_IPTAB1[in[0]] ) | (DES_IPTAB1[in[1]] << 1) | (DES_IPTAB1[in[2]] << 2) | (DES_IPTAB1[in[3]] << 3) | (DES_IPTAB1[in[4]] << 4) | (DES_IPTAB1[in[5]] << 5) | (DES_IPTAB1[in[6]] << 6) | (DES_IPTAB2[in[7]] ); u32bit L = static_cast<u32bit>(T >> 32); u32bit R = static_cast<u32bit>(T); des_encrypt(L, R, &round_key[0]); des_decrypt(R, L, &round_key[32]); des_encrypt(L, R, &round_key[64]); T = (DES_FPTAB1[get_byte(0, L)] << 5) | (DES_FPTAB1[get_byte(1, L)] << 3) | (DES_FPTAB1[get_byte(2, L)] << 1) | (DES_FPTAB2[get_byte(3, L)] << 1) | (DES_FPTAB1[get_byte(0, R)] << 4) | (DES_FPTAB1[get_byte(1, R)] << 2) | (DES_FPTAB1[get_byte(2, R)] ) | (DES_FPTAB2[get_byte(3, R)] ); T = rotate_left(T, 32); store_be(T, out); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * TripleDES Decryption */ void TripleDES::decrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u64bit T = (DES_IPTAB1[in[0]] ) | (DES_IPTAB1[in[1]] << 1) | (DES_IPTAB1[in[2]] << 2) | (DES_IPTAB1[in[3]] << 3) | (DES_IPTAB1[in[4]] << 4) | (DES_IPTAB1[in[5]] << 5) | (DES_IPTAB1[in[6]] << 6) | (DES_IPTAB2[in[7]] ); u32bit L = static_cast<u32bit>(T >> 32); u32bit R = static_cast<u32bit>(T); des_decrypt(L, R, &round_key[64]); des_encrypt(R, L, &round_key[32]); des_decrypt(L, R, &round_key[0]); T = (DES_FPTAB1[get_byte(0, L)] << 5) | (DES_FPTAB1[get_byte(1, L)] << 3) | (DES_FPTAB1[get_byte(2, L)] << 1) | (DES_FPTAB2[get_byte(3, L)] << 1) | (DES_FPTAB1[get_byte(0, R)] << 4) | (DES_FPTAB1[get_byte(1, R)] << 2) | (DES_FPTAB1[get_byte(2, R)] ) | (DES_FPTAB2[get_byte(3, R)] ); T = rotate_left(T, 32); store_be(T, out); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * TripleDES Key Schedule */ void TripleDES::key_schedule(const byte key[], size_t length) { des_key_schedule(&round_key[0], key); des_key_schedule(&round_key[32], key + 8); if(length == 24) des_key_schedule(&round_key[64], key + 16); else copy_mem(&round_key[64], &round_key[0], 32); } } /* * Substitution/Permutation Tables for DES * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { const u32bit DES_SPBOX1[256] = { 0x01010400, 0x00000000, 0x00010000, 0x01010404, 0x01010004, 0x00010404, 0x00000004, 0x00010000, 0x00000400, 0x01010400, 0x01010404, 0x00000400, 0x01000404, 0x01010004, 0x01000000, 0x00000004, 0x00000404, 0x01000400, 0x01000400, 0x00010400, 0x00010400, 0x01010000, 0x01010000, 0x01000404, 0x00010004, 0x01000004, 0x01000004, 0x00010004, 0x00000000, 0x00000404, 0x00010404, 0x01000000, 0x00010000, 0x01010404, 0x00000004, 0x01010000, 0x01010400, 0x01000000, 0x01000000, 0x00000400, 0x01010004, 0x00010000, 0x00010400, 0x01000004, 0x00000400, 0x00000004, 0x01000404, 0x00010404, 0x01010404, 0x00010004, 0x01010000, 0x01000404, 0x01000004, 0x00000404, 0x00010404, 0x01010400, 0x00000404, 0x01000400, 0x01000400, 0x00000000, 0x00010004, 0x00010400, 0x00000000, 0x01010004, 0x01010400, 0x00000000, 0x00010000, 0x01010404, 0x01010004, 0x00010404, 0x00000004, 0x00010000, 0x00000400, 0x01010400, 0x01010404, 0x00000400, 0x01000404, 0x01010004, 0x01000000, 0x00000004, 0x00000404, 0x01000400, 0x01000400, 0x00010400, 0x00010400, 0x01010000, 0x01010000, 0x01000404, 0x00010004, 0x01000004, 0x01000004, 0x00010004, 0x00000000, 0x00000404, 0x00010404, 0x01000000, 0x00010000, 0x01010404, 0x00000004, 0x01010000, 0x01010400, 0x01000000, 0x01000000, 0x00000400, 0x01010004, 0x00010000, 0x00010400, 0x01000004, 0x00000400, 0x00000004, 0x01000404, 0x00010404, 0x01010404, 0x00010004, 0x01010000, 0x01000404, 0x01000004, 0x00000404, 0x00010404, 0x01010400, 0x00000404, 0x01000400, 0x01000400, 0x00000000, 0x00010004, 0x00010400, 0x00000000, 0x01010004, 0x01010400, 0x00000000, 0x00010000, 0x01010404, 0x01010004, 0x00010404, 0x00000004, 0x00010000, 0x00000400, 0x01010400, 0x01010404, 0x00000400, 0x01000404, 0x01010004, 0x01000000, 0x00000004, 0x00000404, 0x01000400, 0x01000400, 0x00010400, 0x00010400, 0x01010000, 0x01010000, 0x01000404, 0x00010004, 0x01000004, 0x01000004, 0x00010004, 0x00000000, 0x00000404, 0x00010404, 0x01000000, 0x00010000, 0x01010404, 0x00000004, 0x01010000, 0x01010400, 0x01000000, 0x01000000, 0x00000400, 0x01010004, 0x00010000, 0x00010400, 0x01000004, 0x00000400, 0x00000004, 0x01000404, 0x00010404, 0x01010404, 0x00010004, 0x01010000, 0x01000404, 0x01000004, 0x00000404, 0x00010404, 0x01010400, 0x00000404, 0x01000400, 0x01000400, 0x00000000, 0x00010004, 0x00010400, 0x00000000, 0x01010004, 0x01010400, 0x00000000, 0x00010000, 0x01010404, 0x01010004, 0x00010404, 0x00000004, 0x00010000, 0x00000400, 0x01010400, 0x01010404, 0x00000400, 0x01000404, 0x01010004, 0x01000000, 0x00000004, 0x00000404, 0x01000400, 0x01000400, 0x00010400, 0x00010400, 0x01010000, 0x01010000, 0x01000404, 0x00010004, 0x01000004, 0x01000004, 0x00010004, 0x00000000, 0x00000404, 0x00010404, 0x01000000, 0x00010000, 0x01010404, 0x00000004, 0x01010000, 0x01010400, 0x01000000, 0x01000000, 0x00000400, 0x01010004, 0x00010000, 0x00010400, 0x01000004, 0x00000400, 0x00000004, 0x01000404, 0x00010404, 0x01010404, 0x00010004, 0x01010000, 0x01000404, 0x01000004, 0x00000404, 0x00010404, 0x01010400, 0x00000404, 0x01000400, 0x01000400, 0x00000000, 0x00010004, 0x00010400, 0x00000000, 0x01010004 }; const u32bit DES_SPBOX2[256] = { 0x80108020, 0x80008000, 0x00008000, 0x00108020, 0x00100000, 0x00000020, 0x80100020, 0x80008020, 0x80000020, 0x80108020, 0x80108000, 0x80000000, 0x80008000, 0x00100000, 0x00000020, 0x80100020, 0x00108000, 0x00100020, 0x80008020, 0x00000000, 0x80000000, 0x00008000, 0x00108020, 0x80100000, 0x00100020, 0x80000020, 0x00000000, 0x00108000, 0x00008020, 0x80108000, 0x80100000, 0x00008020, 0x00000000, 0x00108020, 0x80100020, 0x00100000, 0x80008020, 0x80100000, 0x80108000, 0x00008000, 0x80100000, 0x80008000, 0x00000020, 0x80108020, 0x00108020, 0x00000020, 0x00008000, 0x80000000, 0x00008020, 0x80108000, 0x00100000, 0x80000020, 0x00100020, 0x80008020, 0x80000020, 0x00100020, 0x00108000, 0x00000000, 0x80008000, 0x00008020, 0x80000000, 0x80100020, 0x80108020, 0x00108000, 0x80108020, 0x80008000, 0x00008000, 0x00108020, 0x00100000, 0x00000020, 0x80100020, 0x80008020, 0x80000020, 0x80108020, 0x80108000, 0x80000000, 0x80008000, 0x00100000, 0x00000020, 0x80100020, 0x00108000, 0x00100020, 0x80008020, 0x00000000, 0x80000000, 0x00008000, 0x00108020, 0x80100000, 0x00100020, 0x80000020, 0x00000000, 0x00108000, 0x00008020, 0x80108000, 0x80100000, 0x00008020, 0x00000000, 0x00108020, 0x80100020, 0x00100000, 0x80008020, 0x80100000, 0x80108000, 0x00008000, 0x80100000, 0x80008000, 0x00000020, 0x80108020, 0x00108020, 0x00000020, 0x00008000, 0x80000000, 0x00008020, 0x80108000, 0x00100000, 0x80000020, 0x00100020, 0x80008020, 0x80000020, 0x00100020, 0x00108000, 0x00000000, 0x80008000, 0x00008020, 0x80000000, 0x80100020, 0x80108020, 0x00108000, 0x80108020, 0x80008000, 0x00008000, 0x00108020, 0x00100000, 0x00000020, 0x80100020, 0x80008020, 0x80000020, 0x80108020, 0x80108000, 0x80000000, 0x80008000, 0x00100000, 0x00000020, 0x80100020, 0x00108000, 0x00100020, 0x80008020, 0x00000000, 0x80000000, 0x00008000, 0x00108020, 0x80100000, 0x00100020, 0x80000020, 0x00000000, 0x00108000, 0x00008020, 0x80108000, 0x80100000, 0x00008020, 0x00000000, 0x00108020, 0x80100020, 0x00100000, 0x80008020, 0x80100000, 0x80108000, 0x00008000, 0x80100000, 0x80008000, 0x00000020, 0x80108020, 0x00108020, 0x00000020, 0x00008000, 0x80000000, 0x00008020, 0x80108000, 0x00100000, 0x80000020, 0x00100020, 0x80008020, 0x80000020, 0x00100020, 0x00108000, 0x00000000, 0x80008000, 0x00008020, 0x80000000, 0x80100020, 0x80108020, 0x00108000, 0x80108020, 0x80008000, 0x00008000, 0x00108020, 0x00100000, 0x00000020, 0x80100020, 0x80008020, 0x80000020, 0x80108020, 0x80108000, 0x80000000, 0x80008000, 0x00100000, 0x00000020, 0x80100020, 0x00108000, 0x00100020, 0x80008020, 0x00000000, 0x80000000, 0x00008000, 0x00108020, 0x80100000, 0x00100020, 0x80000020, 0x00000000, 0x00108000, 0x00008020, 0x80108000, 0x80100000, 0x00008020, 0x00000000, 0x00108020, 0x80100020, 0x00100000, 0x80008020, 0x80100000, 0x80108000, 0x00008000, 0x80100000, 0x80008000, 0x00000020, 0x80108020, 0x00108020, 0x00000020, 0x00008000, 0x80000000, 0x00008020, 0x80108000, 0x00100000, 0x80000020, 0x00100020, 0x80008020, 0x80000020, 0x00100020, 0x00108000, 0x00000000, 0x80008000, 0x00008020, 0x80000000, 0x80100020, 0x80108020, 0x00108000 }; const u32bit DES_SPBOX3[256] = { 0x00000208, 0x08020200, 0x00000000, 0x08020008, 0x08000200, 0x00000000, 0x00020208, 0x08000200, 0x00020008, 0x08000008, 0x08000008, 0x00020000, 0x08020208, 0x00020008, 0x08020000, 0x00000208, 0x08000000, 0x00000008, 0x08020200, 0x00000200, 0x00020200, 0x08020000, 0x08020008, 0x00020208, 0x08000208, 0x00020200, 0x00020000, 0x08000208, 0x00000008, 0x08020208, 0x00000200, 0x08000000, 0x08020200, 0x08000000, 0x00020008, 0x00000208, 0x00020000, 0x08020200, 0x08000200, 0x00000000, 0x00000200, 0x00020008, 0x08020208, 0x08000200, 0x08000008, 0x00000200, 0x00000000, 0x08020008, 0x08000208, 0x00020000, 0x08000000, 0x08020208, 0x00000008, 0x00020208, 0x00020200, 0x08000008, 0x08020000, 0x08000208, 0x00000208, 0x08020000, 0x00020208, 0x00000008, 0x08020008, 0x00020200, 0x00000208, 0x08020200, 0x00000000, 0x08020008, 0x08000200, 0x00000000, 0x00020208, 0x08000200, 0x00020008, 0x08000008, 0x08000008, 0x00020000, 0x08020208, 0x00020008, 0x08020000, 0x00000208, 0x08000000, 0x00000008, 0x08020200, 0x00000200, 0x00020200, 0x08020000, 0x08020008, 0x00020208, 0x08000208, 0x00020200, 0x00020000, 0x08000208, 0x00000008, 0x08020208, 0x00000200, 0x08000000, 0x08020200, 0x08000000, 0x00020008, 0x00000208, 0x00020000, 0x08020200, 0x08000200, 0x00000000, 0x00000200, 0x00020008, 0x08020208, 0x08000200, 0x08000008, 0x00000200, 0x00000000, 0x08020008, 0x08000208, 0x00020000, 0x08000000, 0x08020208, 0x00000008, 0x00020208, 0x00020200, 0x08000008, 0x08020000, 0x08000208, 0x00000208, 0x08020000, 0x00020208, 0x00000008, 0x08020008, 0x00020200, 0x00000208, 0x08020200, 0x00000000, 0x08020008, 0x08000200, 0x00000000, 0x00020208, 0x08000200, 0x00020008, 0x08000008, 0x08000008, 0x00020000, 0x08020208, 0x00020008, 0x08020000, 0x00000208, 0x08000000, 0x00000008, 0x08020200, 0x00000200, 0x00020200, 0x08020000, 0x08020008, 0x00020208, 0x08000208, 0x00020200, 0x00020000, 0x08000208, 0x00000008, 0x08020208, 0x00000200, 0x08000000, 0x08020200, 0x08000000, 0x00020008, 0x00000208, 0x00020000, 0x08020200, 0x08000200, 0x00000000, 0x00000200, 0x00020008, 0x08020208, 0x08000200, 0x08000008, 0x00000200, 0x00000000, 0x08020008, 0x08000208, 0x00020000, 0x08000000, 0x08020208, 0x00000008, 0x00020208, 0x00020200, 0x08000008, 0x08020000, 0x08000208, 0x00000208, 0x08020000, 0x00020208, 0x00000008, 0x08020008, 0x00020200, 0x00000208, 0x08020200, 0x00000000, 0x08020008, 0x08000200, 0x00000000, 0x00020208, 0x08000200, 0x00020008, 0x08000008, 0x08000008, 0x00020000, 0x08020208, 0x00020008, 0x08020000, 0x00000208, 0x08000000, 0x00000008, 0x08020200, 0x00000200, 0x00020200, 0x08020000, 0x08020008, 0x00020208, 0x08000208, 0x00020200, 0x00020000, 0x08000208, 0x00000008, 0x08020208, 0x00000200, 0x08000000, 0x08020200, 0x08000000, 0x00020008, 0x00000208, 0x00020000, 0x08020200, 0x08000200, 0x00000000, 0x00000200, 0x00020008, 0x08020208, 0x08000200, 0x08000008, 0x00000200, 0x00000000, 0x08020008, 0x08000208, 0x00020000, 0x08000000, 0x08020208, 0x00000008, 0x00020208, 0x00020200, 0x08000008, 0x08020000, 0x08000208, 0x00000208, 0x08020000, 0x00020208, 0x00000008, 0x08020008, 0x00020200 }; const u32bit DES_SPBOX4[256] = { 0x00802001, 0x00002081, 0x00002081, 0x00000080, 0x00802080, 0x00800081, 0x00800001, 0x00002001, 0x00000000, 0x00802000, 0x00802000, 0x00802081, 0x00000081, 0x00000000, 0x00800080, 0x00800001, 0x00000001, 0x00002000, 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002001, 0x00002080, 0x00800081, 0x00000001, 0x00002080, 0x00800080, 0x00002000, 0x00802080, 0x00802081, 0x00000081, 0x00800080, 0x00800001, 0x00802000, 0x00802081, 0x00000081, 0x00000000, 0x00000000, 0x00802000, 0x00002080, 0x00800080, 0x00800081, 0x00000001, 0x00802001, 0x00002081, 0x00002081, 0x00000080, 0x00802081, 0x00000081, 0x00000001, 0x00002000, 0x00800001, 0x00002001, 0x00802080, 0x00800081, 0x00002001, 0x00002080, 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002000, 0x00802080, 0x00802001, 0x00002081, 0x00002081, 0x00000080, 0x00802080, 0x00800081, 0x00800001, 0x00002001, 0x00000000, 0x00802000, 0x00802000, 0x00802081, 0x00000081, 0x00000000, 0x00800080, 0x00800001, 0x00000001, 0x00002000, 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002001, 0x00002080, 0x00800081, 0x00000001, 0x00002080, 0x00800080, 0x00002000, 0x00802080, 0x00802081, 0x00000081, 0x00800080, 0x00800001, 0x00802000, 0x00802081, 0x00000081, 0x00000000, 0x00000000, 0x00802000, 0x00002080, 0x00800080, 0x00800081, 0x00000001, 0x00802001, 0x00002081, 0x00002081, 0x00000080, 0x00802081, 0x00000081, 0x00000001, 0x00002000, 0x00800001, 0x00002001, 0x00802080, 0x00800081, 0x00002001, 0x00002080, 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002000, 0x00802080, 0x00802001, 0x00002081, 0x00002081, 0x00000080, 0x00802080, 0x00800081, 0x00800001, 0x00002001, 0x00000000, 0x00802000, 0x00802000, 0x00802081, 0x00000081, 0x00000000, 0x00800080, 0x00800001, 0x00000001, 0x00002000, 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002001, 0x00002080, 0x00800081, 0x00000001, 0x00002080, 0x00800080, 0x00002000, 0x00802080, 0x00802081, 0x00000081, 0x00800080, 0x00800001, 0x00802000, 0x00802081, 0x00000081, 0x00000000, 0x00000000, 0x00802000, 0x00002080, 0x00800080, 0x00800081, 0x00000001, 0x00802001, 0x00002081, 0x00002081, 0x00000080, 0x00802081, 0x00000081, 0x00000001, 0x00002000, 0x00800001, 0x00002001, 0x00802080, 0x00800081, 0x00002001, 0x00002080, 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002000, 0x00802080, 0x00802001, 0x00002081, 0x00002081, 0x00000080, 0x00802080, 0x00800081, 0x00800001, 0x00002001, 0x00000000, 0x00802000, 0x00802000, 0x00802081, 0x00000081, 0x00000000, 0x00800080, 0x00800001, 0x00000001, 0x00002000, 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002001, 0x00002080, 0x00800081, 0x00000001, 0x00002080, 0x00800080, 0x00002000, 0x00802080, 0x00802081, 0x00000081, 0x00800080, 0x00800001, 0x00802000, 0x00802081, 0x00000081, 0x00000000, 0x00000000, 0x00802000, 0x00002080, 0x00800080, 0x00800081, 0x00000001, 0x00802001, 0x00002081, 0x00002081, 0x00000080, 0x00802081, 0x00000081, 0x00000001, 0x00002000, 0x00800001, 0x00002001, 0x00802080, 0x00800081, 0x00002001, 0x00002080, 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002000, 0x00802080 }; const u32bit DES_SPBOX5[256] = { 0x00000100, 0x02080100, 0x02080000, 0x42000100, 0x00080000, 0x00000100, 0x40000000, 0x02080000, 0x40080100, 0x00080000, 0x02000100, 0x40080100, 0x42000100, 0x42080000, 0x00080100, 0x40000000, 0x02000000, 0x40080000, 0x40080000, 0x00000000, 0x40000100, 0x42080100, 0x42080100, 0x02000100, 0x42080000, 0x40000100, 0x00000000, 0x42000000, 0x02080100, 0x02000000, 0x42000000, 0x00080100, 0x00080000, 0x42000100, 0x00000100, 0x02000000, 0x40000000, 0x02080000, 0x42000100, 0x40080100, 0x02000100, 0x40000000, 0x42080000, 0x02080100, 0x40080100, 0x00000100, 0x02000000, 0x42080000, 0x42080100, 0x00080100, 0x42000000, 0x42080100, 0x02080000, 0x00000000, 0x40080000, 0x42000000, 0x00080100, 0x02000100, 0x40000100, 0x00080000, 0x00000000, 0x40080000, 0x02080100, 0x40000100, 0x00000100, 0x02080100, 0x02080000, 0x42000100, 0x00080000, 0x00000100, 0x40000000, 0x02080000, 0x40080100, 0x00080000, 0x02000100, 0x40080100, 0x42000100, 0x42080000, 0x00080100, 0x40000000, 0x02000000, 0x40080000, 0x40080000, 0x00000000, 0x40000100, 0x42080100, 0x42080100, 0x02000100, 0x42080000, 0x40000100, 0x00000000, 0x42000000, 0x02080100, 0x02000000, 0x42000000, 0x00080100, 0x00080000, 0x42000100, 0x00000100, 0x02000000, 0x40000000, 0x02080000, 0x42000100, 0x40080100, 0x02000100, 0x40000000, 0x42080000, 0x02080100, 0x40080100, 0x00000100, 0x02000000, 0x42080000, 0x42080100, 0x00080100, 0x42000000, 0x42080100, 0x02080000, 0x00000000, 0x40080000, 0x42000000, 0x00080100, 0x02000100, 0x40000100, 0x00080000, 0x00000000, 0x40080000, 0x02080100, 0x40000100, 0x00000100, 0x02080100, 0x02080000, 0x42000100, 0x00080000, 0x00000100, 0x40000000, 0x02080000, 0x40080100, 0x00080000, 0x02000100, 0x40080100, 0x42000100, 0x42080000, 0x00080100, 0x40000000, 0x02000000, 0x40080000, 0x40080000, 0x00000000, 0x40000100, 0x42080100, 0x42080100, 0x02000100, 0x42080000, 0x40000100, 0x00000000, 0x42000000, 0x02080100, 0x02000000, 0x42000000, 0x00080100, 0x00080000, 0x42000100, 0x00000100, 0x02000000, 0x40000000, 0x02080000, 0x42000100, 0x40080100, 0x02000100, 0x40000000, 0x42080000, 0x02080100, 0x40080100, 0x00000100, 0x02000000, 0x42080000, 0x42080100, 0x00080100, 0x42000000, 0x42080100, 0x02080000, 0x00000000, 0x40080000, 0x42000000, 0x00080100, 0x02000100, 0x40000100, 0x00080000, 0x00000000, 0x40080000, 0x02080100, 0x40000100, 0x00000100, 0x02080100, 0x02080000, 0x42000100, 0x00080000, 0x00000100, 0x40000000, 0x02080000, 0x40080100, 0x00080000, 0x02000100, 0x40080100, 0x42000100, 0x42080000, 0x00080100, 0x40000000, 0x02000000, 0x40080000, 0x40080000, 0x00000000, 0x40000100, 0x42080100, 0x42080100, 0x02000100, 0x42080000, 0x40000100, 0x00000000, 0x42000000, 0x02080100, 0x02000000, 0x42000000, 0x00080100, 0x00080000, 0x42000100, 0x00000100, 0x02000000, 0x40000000, 0x02080000, 0x42000100, 0x40080100, 0x02000100, 0x40000000, 0x42080000, 0x02080100, 0x40080100, 0x00000100, 0x02000000, 0x42080000, 0x42080100, 0x00080100, 0x42000000, 0x42080100, 0x02080000, 0x00000000, 0x40080000, 0x42000000, 0x00080100, 0x02000100, 0x40000100, 0x00080000, 0x00000000, 0x40080000, 0x02080100, 0x40000100 }; const u32bit DES_SPBOX6[256] = { 0x20000010, 0x20400000, 0x00004000, 0x20404010, 0x20400000, 0x00000010, 0x20404010, 0x00400000, 0x20004000, 0x00404010, 0x00400000, 0x20000010, 0x00400010, 0x20004000, 0x20000000, 0x00004010, 0x00000000, 0x00400010, 0x20004010, 0x00004000, 0x00404000, 0x20004010, 0x00000010, 0x20400010, 0x20400010, 0x00000000, 0x00404010, 0x20404000, 0x00004010, 0x00404000, 0x20404000, 0x20000000, 0x20004000, 0x00000010, 0x20400010, 0x00404000, 0x20404010, 0x00400000, 0x00004010, 0x20000010, 0x00400000, 0x20004000, 0x20000000, 0x00004010, 0x20000010, 0x20404010, 0x00404000, 0x20400000, 0x00404010, 0x20404000, 0x00000000, 0x20400010, 0x00000010, 0x00004000, 0x20400000, 0x00404010, 0x00004000, 0x00400010, 0x20004010, 0x00000000, 0x20404000, 0x20000000, 0x00400010, 0x20004010, 0x20000010, 0x20400000, 0x00004000, 0x20404010, 0x20400000, 0x00000010, 0x20404010, 0x00400000, 0x20004000, 0x00404010, 0x00400000, 0x20000010, 0x00400010, 0x20004000, 0x20000000, 0x00004010, 0x00000000, 0x00400010, 0x20004010, 0x00004000, 0x00404000, 0x20004010, 0x00000010, 0x20400010, 0x20400010, 0x00000000, 0x00404010, 0x20404000, 0x00004010, 0x00404000, 0x20404000, 0x20000000, 0x20004000, 0x00000010, 0x20400010, 0x00404000, 0x20404010, 0x00400000, 0x00004010, 0x20000010, 0x00400000, 0x20004000, 0x20000000, 0x00004010, 0x20000010, 0x20404010, 0x00404000, 0x20400000, 0x00404010, 0x20404000, 0x00000000, 0x20400010, 0x00000010, 0x00004000, 0x20400000, 0x00404010, 0x00004000, 0x00400010, 0x20004010, 0x00000000, 0x20404000, 0x20000000, 0x00400010, 0x20004010, 0x20000010, 0x20400000, 0x00004000, 0x20404010, 0x20400000, 0x00000010, 0x20404010, 0x00400000, 0x20004000, 0x00404010, 0x00400000, 0x20000010, 0x00400010, 0x20004000, 0x20000000, 0x00004010, 0x00000000, 0x00400010, 0x20004010, 0x00004000, 0x00404000, 0x20004010, 0x00000010, 0x20400010, 0x20400010, 0x00000000, 0x00404010, 0x20404000, 0x00004010, 0x00404000, 0x20404000, 0x20000000, 0x20004000, 0x00000010, 0x20400010, 0x00404000, 0x20404010, 0x00400000, 0x00004010, 0x20000010, 0x00400000, 0x20004000, 0x20000000, 0x00004010, 0x20000010, 0x20404010, 0x00404000, 0x20400000, 0x00404010, 0x20404000, 0x00000000, 0x20400010, 0x00000010, 0x00004000, 0x20400000, 0x00404010, 0x00004000, 0x00400010, 0x20004010, 0x00000000, 0x20404000, 0x20000000, 0x00400010, 0x20004010, 0x20000010, 0x20400000, 0x00004000, 0x20404010, 0x20400000, 0x00000010, 0x20404010, 0x00400000, 0x20004000, 0x00404010, 0x00400000, 0x20000010, 0x00400010, 0x20004000, 0x20000000, 0x00004010, 0x00000000, 0x00400010, 0x20004010, 0x00004000, 0x00404000, 0x20004010, 0x00000010, 0x20400010, 0x20400010, 0x00000000, 0x00404010, 0x20404000, 0x00004010, 0x00404000, 0x20404000, 0x20000000, 0x20004000, 0x00000010, 0x20400010, 0x00404000, 0x20404010, 0x00400000, 0x00004010, 0x20000010, 0x00400000, 0x20004000, 0x20000000, 0x00004010, 0x20000010, 0x20404010, 0x00404000, 0x20400000, 0x00404010, 0x20404000, 0x00000000, 0x20400010, 0x00000010, 0x00004000, 0x20400000, 0x00404010, 0x00004000, 0x00400010, 0x20004010, 0x00000000, 0x20404000, 0x20000000, 0x00400010, 0x20004010 }; const u32bit DES_SPBOX7[256] = { 0x00200000, 0x04200002, 0x04000802, 0x00000000, 0x00000800, 0x04000802, 0x00200802, 0x04200800, 0x04200802, 0x00200000, 0x00000000, 0x04000002, 0x00000002, 0x04000000, 0x04200002, 0x00000802, 0x04000800, 0x00200802, 0x00200002, 0x04000800, 0x04000002, 0x04200000, 0x04200800, 0x00200002, 0x04200000, 0x00000800, 0x00000802, 0x04200802, 0x00200800, 0x00000002, 0x04000000, 0x00200800, 0x04000000, 0x00200800, 0x00200000, 0x04000802, 0x04000802, 0x04200002, 0x04200002, 0x00000002, 0x00200002, 0x04000000, 0x04000800, 0x00200000, 0x04200800, 0x00000802, 0x00200802, 0x04200800, 0x00000802, 0x04000002, 0x04200802, 0x04200000, 0x00200800, 0x00000000, 0x00000002, 0x04200802, 0x00000000, 0x00200802, 0x04200000, 0x00000800, 0x04000002, 0x04000800, 0x00000800, 0x00200002, 0x00200000, 0x04200002, 0x04000802, 0x00000000, 0x00000800, 0x04000802, 0x00200802, 0x04200800, 0x04200802, 0x00200000, 0x00000000, 0x04000002, 0x00000002, 0x04000000, 0x04200002, 0x00000802, 0x04000800, 0x00200802, 0x00200002, 0x04000800, 0x04000002, 0x04200000, 0x04200800, 0x00200002, 0x04200000, 0x00000800, 0x00000802, 0x04200802, 0x00200800, 0x00000002, 0x04000000, 0x00200800, 0x04000000, 0x00200800, 0x00200000, 0x04000802, 0x04000802, 0x04200002, 0x04200002, 0x00000002, 0x00200002, 0x04000000, 0x04000800, 0x00200000, 0x04200800, 0x00000802, 0x00200802, 0x04200800, 0x00000802, 0x04000002, 0x04200802, 0x04200000, 0x00200800, 0x00000000, 0x00000002, 0x04200802, 0x00000000, 0x00200802, 0x04200000, 0x00000800, 0x04000002, 0x04000800, 0x00000800, 0x00200002, 0x00200000, 0x04200002, 0x04000802, 0x00000000, 0x00000800, 0x04000802, 0x00200802, 0x04200800, 0x04200802, 0x00200000, 0x00000000, 0x04000002, 0x00000002, 0x04000000, 0x04200002, 0x00000802, 0x04000800, 0x00200802, 0x00200002, 0x04000800, 0x04000002, 0x04200000, 0x04200800, 0x00200002, 0x04200000, 0x00000800, 0x00000802, 0x04200802, 0x00200800, 0x00000002, 0x04000000, 0x00200800, 0x04000000, 0x00200800, 0x00200000, 0x04000802, 0x04000802, 0x04200002, 0x04200002, 0x00000002, 0x00200002, 0x04000000, 0x04000800, 0x00200000, 0x04200800, 0x00000802, 0x00200802, 0x04200800, 0x00000802, 0x04000002, 0x04200802, 0x04200000, 0x00200800, 0x00000000, 0x00000002, 0x04200802, 0x00000000, 0x00200802, 0x04200000, 0x00000800, 0x04000002, 0x04000800, 0x00000800, 0x00200002, 0x00200000, 0x04200002, 0x04000802, 0x00000000, 0x00000800, 0x04000802, 0x00200802, 0x04200800, 0x04200802, 0x00200000, 0x00000000, 0x04000002, 0x00000002, 0x04000000, 0x04200002, 0x00000802, 0x04000800, 0x00200802, 0x00200002, 0x04000800, 0x04000002, 0x04200000, 0x04200800, 0x00200002, 0x04200000, 0x00000800, 0x00000802, 0x04200802, 0x00200800, 0x00000002, 0x04000000, 0x00200800, 0x04000000, 0x00200800, 0x00200000, 0x04000802, 0x04000802, 0x04200002, 0x04200002, 0x00000002, 0x00200002, 0x04000000, 0x04000800, 0x00200000, 0x04200800, 0x00000802, 0x00200802, 0x04200800, 0x00000802, 0x04000002, 0x04200802, 0x04200000, 0x00200800, 0x00000000, 0x00000002, 0x04200802, 0x00000000, 0x00200802, 0x04200000, 0x00000800, 0x04000002, 0x04000800, 0x00000800, 0x00200002 }; const u32bit DES_SPBOX8[256] = { 0x10001040, 0x00001000, 0x00040000, 0x10041040, 0x10000000, 0x10001040, 0x00000040, 0x10000000, 0x00040040, 0x10040000, 0x10041040, 0x00041000, 0x10041000, 0x00041040, 0x00001000, 0x00000040, 0x10040000, 0x10000040, 0x10001000, 0x00001040, 0x00041000, 0x00040040, 0x10040040, 0x10041000, 0x00001040, 0x00000000, 0x00000000, 0x10040040, 0x10000040, 0x10001000, 0x00041040, 0x00040000, 0x00041040, 0x00040000, 0x10041000, 0x00001000, 0x00000040, 0x10040040, 0x00001000, 0x00041040, 0x10001000, 0x00000040, 0x10000040, 0x10040000, 0x10040040, 0x10000000, 0x00040000, 0x10001040, 0x00000000, 0x10041040, 0x00040040, 0x10000040, 0x10040000, 0x10001000, 0x10001040, 0x00000000, 0x10041040, 0x00041000, 0x00041000, 0x00001040, 0x00001040, 0x00040040, 0x10000000, 0x10041000, 0x10001040, 0x00001000, 0x00040000, 0x10041040, 0x10000000, 0x10001040, 0x00000040, 0x10000000, 0x00040040, 0x10040000, 0x10041040, 0x00041000, 0x10041000, 0x00041040, 0x00001000, 0x00000040, 0x10040000, 0x10000040, 0x10001000, 0x00001040, 0x00041000, 0x00040040, 0x10040040, 0x10041000, 0x00001040, 0x00000000, 0x00000000, 0x10040040, 0x10000040, 0x10001000, 0x00041040, 0x00040000, 0x00041040, 0x00040000, 0x10041000, 0x00001000, 0x00000040, 0x10040040, 0x00001000, 0x00041040, 0x10001000, 0x00000040, 0x10000040, 0x10040000, 0x10040040, 0x10000000, 0x00040000, 0x10001040, 0x00000000, 0x10041040, 0x00040040, 0x10000040, 0x10040000, 0x10001000, 0x10001040, 0x00000000, 0x10041040, 0x00041000, 0x00041000, 0x00001040, 0x00001040, 0x00040040, 0x10000000, 0x10041000, 0x10001040, 0x00001000, 0x00040000, 0x10041040, 0x10000000, 0x10001040, 0x00000040, 0x10000000, 0x00040040, 0x10040000, 0x10041040, 0x00041000, 0x10041000, 0x00041040, 0x00001000, 0x00000040, 0x10040000, 0x10000040, 0x10001000, 0x00001040, 0x00041000, 0x00040040, 0x10040040, 0x10041000, 0x00001040, 0x00000000, 0x00000000, 0x10040040, 0x10000040, 0x10001000, 0x00041040, 0x00040000, 0x00041040, 0x00040000, 0x10041000, 0x00001000, 0x00000040, 0x10040040, 0x00001000, 0x00041040, 0x10001000, 0x00000040, 0x10000040, 0x10040000, 0x10040040, 0x10000000, 0x00040000, 0x10001040, 0x00000000, 0x10041040, 0x00040040, 0x10000040, 0x10040000, 0x10001000, 0x10001040, 0x00000000, 0x10041040, 0x00041000, 0x00041000, 0x00001040, 0x00001040, 0x00040040, 0x10000000, 0x10041000, 0x10001040, 0x00001000, 0x00040000, 0x10041040, 0x10000000, 0x10001040, 0x00000040, 0x10000000, 0x00040040, 0x10040000, 0x10041040, 0x00041000, 0x10041000, 0x00041040, 0x00001000, 0x00000040, 0x10040000, 0x10000040, 0x10001000, 0x00001040, 0x00041000, 0x00040040, 0x10040040, 0x10041000, 0x00001040, 0x00000000, 0x00000000, 0x10040040, 0x10000040, 0x10001000, 0x00041040, 0x00040000, 0x00041040, 0x00040000, 0x10041000, 0x00001000, 0x00000040, 0x10040040, 0x00001000, 0x00041040, 0x10001000, 0x00000040, 0x10000040, 0x10040000, 0x10040040, 0x10000000, 0x00040000, 0x10001040, 0x00000000, 0x10041040, 0x00040040, 0x10000040, 0x10040000, 0x10001000, 0x10001040, 0x00000000, 0x10041040, 0x00041000, 0x00041000, 0x00001040, 0x00001040, 0x00040040, 0x10000000, 0x10041000 }; const u64bit DES_IPTAB1[256] = { 0x0000000000000000, 0x0000000200000000, 0x0000000000000002, 0x0000000200000002, 0x0000020000000000, 0x0000020200000000, 0x0000020000000002, 0x0000020200000002, 0x0000000000000200, 0x0000000200000200, 0x0000000000000202, 0x0000000200000202, 0x0000020000000200, 0x0000020200000200, 0x0000020000000202, 0x0000020200000202, 0x0002000000000000, 0x0002000200000000, 0x0002000000000002, 0x0002000200000002, 0x0002020000000000, 0x0002020200000000, 0x0002020000000002, 0x0002020200000002, 0x0002000000000200, 0x0002000200000200, 0x0002000000000202, 0x0002000200000202, 0x0002020000000200, 0x0002020200000200, 0x0002020000000202, 0x0002020200000202, 0x0000000000020000, 0x0000000200020000, 0x0000000000020002, 0x0000000200020002, 0x0000020000020000, 0x0000020200020000, 0x0000020000020002, 0x0000020200020002, 0x0000000000020200, 0x0000000200020200, 0x0000000000020202, 0x0000000200020202, 0x0000020000020200, 0x0000020200020200, 0x0000020000020202, 0x0000020200020202, 0x0002000000020000, 0x0002000200020000, 0x0002000000020002, 0x0002000200020002, 0x0002020000020000, 0x0002020200020000, 0x0002020000020002, 0x0002020200020002, 0x0002000000020200, 0x0002000200020200, 0x0002000000020202, 0x0002000200020202, 0x0002020000020200, 0x0002020200020200, 0x0002020000020202, 0x0002020200020202, 0x0200000000000000, 0x0200000200000000, 0x0200000000000002, 0x0200000200000002, 0x0200020000000000, 0x0200020200000000, 0x0200020000000002, 0x0200020200000002, 0x0200000000000200, 0x0200000200000200, 0x0200000000000202, 0x0200000200000202, 0x0200020000000200, 0x0200020200000200, 0x0200020000000202, 0x0200020200000202, 0x0202000000000000, 0x0202000200000000, 0x0202000000000002, 0x0202000200000002, 0x0202020000000000, 0x0202020200000000, 0x0202020000000002, 0x0202020200000002, 0x0202000000000200, 0x0202000200000200, 0x0202000000000202, 0x0202000200000202, 0x0202020000000200, 0x0202020200000200, 0x0202020000000202, 0x0202020200000202, 0x0200000000020000, 0x0200000200020000, 0x0200000000020002, 0x0200000200020002, 0x0200020000020000, 0x0200020200020000, 0x0200020000020002, 0x0200020200020002, 0x0200000000020200, 0x0200000200020200, 0x0200000000020202, 0x0200000200020202, 0x0200020000020200, 0x0200020200020200, 0x0200020000020202, 0x0200020200020202, 0x0202000000020000, 0x0202000200020000, 0x0202000000020002, 0x0202000200020002, 0x0202020000020000, 0x0202020200020000, 0x0202020000020002, 0x0202020200020002, 0x0202000000020200, 0x0202000200020200, 0x0202000000020202, 0x0202000200020202, 0x0202020000020200, 0x0202020200020200, 0x0202020000020202, 0x0202020200020202, 0x0000000002000000, 0x0000000202000000, 0x0000000002000002, 0x0000000202000002, 0x0000020002000000, 0x0000020202000000, 0x0000020002000002, 0x0000020202000002, 0x0000000002000200, 0x0000000202000200, 0x0000000002000202, 0x0000000202000202, 0x0000020002000200, 0x0000020202000200, 0x0000020002000202, 0x0000020202000202, 0x0002000002000000, 0x0002000202000000, 0x0002000002000002, 0x0002000202000002, 0x0002020002000000, 0x0002020202000000, 0x0002020002000002, 0x0002020202000002, 0x0002000002000200, 0x0002000202000200, 0x0002000002000202, 0x0002000202000202, 0x0002020002000200, 0x0002020202000200, 0x0002020002000202, 0x0002020202000202, 0x0000000002020000, 0x0000000202020000, 0x0000000002020002, 0x0000000202020002, 0x0000020002020000, 0x0000020202020000, 0x0000020002020002, 0x0000020202020002, 0x0000000002020200, 0x0000000202020200, 0x0000000002020202, 0x0000000202020202, 0x0000020002020200, 0x0000020202020200, 0x0000020002020202, 0x0000020202020202, 0x0002000002020000, 0x0002000202020000, 0x0002000002020002, 0x0002000202020002, 0x0002020002020000, 0x0002020202020000, 0x0002020002020002, 0x0002020202020002, 0x0002000002020200, 0x0002000202020200, 0x0002000002020202, 0x0002000202020202, 0x0002020002020200, 0x0002020202020200, 0x0002020002020202, 0x0002020202020202, 0x0200000002000000, 0x0200000202000000, 0x0200000002000002, 0x0200000202000002, 0x0200020002000000, 0x0200020202000000, 0x0200020002000002, 0x0200020202000002, 0x0200000002000200, 0x0200000202000200, 0x0200000002000202, 0x0200000202000202, 0x0200020002000200, 0x0200020202000200, 0x0200020002000202, 0x0200020202000202, 0x0202000002000000, 0x0202000202000000, 0x0202000002000002, 0x0202000202000002, 0x0202020002000000, 0x0202020202000000, 0x0202020002000002, 0x0202020202000002, 0x0202000002000200, 0x0202000202000200, 0x0202000002000202, 0x0202000202000202, 0x0202020002000200, 0x0202020202000200, 0x0202020002000202, 0x0202020202000202, 0x0200000002020000, 0x0200000202020000, 0x0200000002020002, 0x0200000202020002, 0x0200020002020000, 0x0200020202020000, 0x0200020002020002, 0x0200020202020002, 0x0200000002020200, 0x0200000202020200, 0x0200000002020202, 0x0200000202020202, 0x0200020002020200, 0x0200020202020200, 0x0200020002020202, 0x0200020202020202, 0x0202000002020000, 0x0202000202020000, 0x0202000002020002, 0x0202000202020002, 0x0202020002020000, 0x0202020202020000, 0x0202020002020002, 0x0202020202020002, 0x0202000002020200, 0x0202000202020200, 0x0202000002020202, 0x0202000202020202, 0x0202020002020200, 0x0202020202020200, 0x0202020002020202, 0x0202020202020202 }; const u64bit DES_IPTAB2[256] = { 0x0000000000000000, 0x0000010000000000, 0x0000000000000100, 0x0000010000000100, 0x0001000000000000, 0x0001010000000000, 0x0001000000000100, 0x0001010000000100, 0x0000000000010000, 0x0000010000010000, 0x0000000000010100, 0x0000010000010100, 0x0001000000010000, 0x0001010000010000, 0x0001000000010100, 0x0001010000010100, 0x0100000000000000, 0x0100010000000000, 0x0100000000000100, 0x0100010000000100, 0x0101000000000000, 0x0101010000000000, 0x0101000000000100, 0x0101010000000100, 0x0100000000010000, 0x0100010000010000, 0x0100000000010100, 0x0100010000010100, 0x0101000000010000, 0x0101010000010000, 0x0101000000010100, 0x0101010000010100, 0x0000000001000000, 0x0000010001000000, 0x0000000001000100, 0x0000010001000100, 0x0001000001000000, 0x0001010001000000, 0x0001000001000100, 0x0001010001000100, 0x0000000001010000, 0x0000010001010000, 0x0000000001010100, 0x0000010001010100, 0x0001000001010000, 0x0001010001010000, 0x0001000001010100, 0x0001010001010100, 0x0100000001000000, 0x0100010001000000, 0x0100000001000100, 0x0100010001000100, 0x0101000001000000, 0x0101010001000000, 0x0101000001000100, 0x0101010001000100, 0x0100000001010000, 0x0100010001010000, 0x0100000001010100, 0x0100010001010100, 0x0101000001010000, 0x0101010001010000, 0x0101000001010100, 0x0101010001010100, 0x0000000100000000, 0x0000010100000000, 0x0000000100000100, 0x0000010100000100, 0x0001000100000000, 0x0001010100000000, 0x0001000100000100, 0x0001010100000100, 0x0000000100010000, 0x0000010100010000, 0x0000000100010100, 0x0000010100010100, 0x0001000100010000, 0x0001010100010000, 0x0001000100010100, 0x0001010100010100, 0x0100000100000000, 0x0100010100000000, 0x0100000100000100, 0x0100010100000100, 0x0101000100000000, 0x0101010100000000, 0x0101000100000100, 0x0101010100000100, 0x0100000100010000, 0x0100010100010000, 0x0100000100010100, 0x0100010100010100, 0x0101000100010000, 0x0101010100010000, 0x0101000100010100, 0x0101010100010100, 0x0000000101000000, 0x0000010101000000, 0x0000000101000100, 0x0000010101000100, 0x0001000101000000, 0x0001010101000000, 0x0001000101000100, 0x0001010101000100, 0x0000000101010000, 0x0000010101010000, 0x0000000101010100, 0x0000010101010100, 0x0001000101010000, 0x0001010101010000, 0x0001000101010100, 0x0001010101010100, 0x0100000101000000, 0x0100010101000000, 0x0100000101000100, 0x0100010101000100, 0x0101000101000000, 0x0101010101000000, 0x0101000101000100, 0x0101010101000100, 0x0100000101010000, 0x0100010101010000, 0x0100000101010100, 0x0100010101010100, 0x0101000101010000, 0x0101010101010000, 0x0101000101010100, 0x0101010101010100, 0x0000000000000001, 0x0000010000000001, 0x0000000000000101, 0x0000010000000101, 0x0001000000000001, 0x0001010000000001, 0x0001000000000101, 0x0001010000000101, 0x0000000000010001, 0x0000010000010001, 0x0000000000010101, 0x0000010000010101, 0x0001000000010001, 0x0001010000010001, 0x0001000000010101, 0x0001010000010101, 0x0100000000000001, 0x0100010000000001, 0x0100000000000101, 0x0100010000000101, 0x0101000000000001, 0x0101010000000001, 0x0101000000000101, 0x0101010000000101, 0x0100000000010001, 0x0100010000010001, 0x0100000000010101, 0x0100010000010101, 0x0101000000010001, 0x0101010000010001, 0x0101000000010101, 0x0101010000010101, 0x0000000001000001, 0x0000010001000001, 0x0000000001000101, 0x0000010001000101, 0x0001000001000001, 0x0001010001000001, 0x0001000001000101, 0x0001010001000101, 0x0000000001010001, 0x0000010001010001, 0x0000000001010101, 0x0000010001010101, 0x0001000001010001, 0x0001010001010001, 0x0001000001010101, 0x0001010001010101, 0x0100000001000001, 0x0100010001000001, 0x0100000001000101, 0x0100010001000101, 0x0101000001000001, 0x0101010001000001, 0x0101000001000101, 0x0101010001000101, 0x0100000001010001, 0x0100010001010001, 0x0100000001010101, 0x0100010001010101, 0x0101000001010001, 0x0101010001010001, 0x0101000001010101, 0x0101010001010101, 0x0000000100000001, 0x0000010100000001, 0x0000000100000101, 0x0000010100000101, 0x0001000100000001, 0x0001010100000001, 0x0001000100000101, 0x0001010100000101, 0x0000000100010001, 0x0000010100010001, 0x0000000100010101, 0x0000010100010101, 0x0001000100010001, 0x0001010100010001, 0x0001000100010101, 0x0001010100010101, 0x0100000100000001, 0x0100010100000001, 0x0100000100000101, 0x0100010100000101, 0x0101000100000001, 0x0101010100000001, 0x0101000100000101, 0x0101010100000101, 0x0100000100010001, 0x0100010100010001, 0x0100000100010101, 0x0100010100010101, 0x0101000100010001, 0x0101010100010001, 0x0101000100010101, 0x0101010100010101, 0x0000000101000001, 0x0000010101000001, 0x0000000101000101, 0x0000010101000101, 0x0001000101000001, 0x0001010101000001, 0x0001000101000101, 0x0001010101000101, 0x0000000101010001, 0x0000010101010001, 0x0000000101010101, 0x0000010101010101, 0x0001000101010001, 0x0001010101010001, 0x0001000101010101, 0x0001010101010101, 0x0100000101000001, 0x0100010101000001, 0x0100000101000101, 0x0100010101000101, 0x0101000101000001, 0x0101010101000001, 0x0101000101000101, 0x0101010101000101, 0x0100000101010001, 0x0100010101010001, 0x0100000101010101, 0x0100010101010101, 0x0101000101010001, 0x0101010101010001, 0x0101000101010101, 0x0101010101010101 }; const u64bit DES_FPTAB1[256] = { 0x0000000000000000, 0x0000000100000000, 0x0000000004000000, 0x0000000104000000, 0x0000000000040000, 0x0000000100040000, 0x0000000004040000, 0x0000000104040000, 0x0000000000000400, 0x0000000100000400, 0x0000000004000400, 0x0000000104000400, 0x0000000000040400, 0x0000000100040400, 0x0000000004040400, 0x0000000104040400, 0x0000000000000004, 0x0000000100000004, 0x0000000004000004, 0x0000000104000004, 0x0000000000040004, 0x0000000100040004, 0x0000000004040004, 0x0000000104040004, 0x0000000000000404, 0x0000000100000404, 0x0000000004000404, 0x0000000104000404, 0x0000000000040404, 0x0000000100040404, 0x0000000004040404, 0x0000000104040404, 0x0400000000000000, 0x0400000100000000, 0x0400000004000000, 0x0400000104000000, 0x0400000000040000, 0x0400000100040000, 0x0400000004040000, 0x0400000104040000, 0x0400000000000400, 0x0400000100000400, 0x0400000004000400, 0x0400000104000400, 0x0400000000040400, 0x0400000100040400, 0x0400000004040400, 0x0400000104040400, 0x0400000000000004, 0x0400000100000004, 0x0400000004000004, 0x0400000104000004, 0x0400000000040004, 0x0400000100040004, 0x0400000004040004, 0x0400000104040004, 0x0400000000000404, 0x0400000100000404, 0x0400000004000404, 0x0400000104000404, 0x0400000000040404, 0x0400000100040404, 0x0400000004040404, 0x0400000104040404, 0x0004000000000000, 0x0004000100000000, 0x0004000004000000, 0x0004000104000000, 0x0004000000040000, 0x0004000100040000, 0x0004000004040000, 0x0004000104040000, 0x0004000000000400, 0x0004000100000400, 0x0004000004000400, 0x0004000104000400, 0x0004000000040400, 0x0004000100040400, 0x0004000004040400, 0x0004000104040400, 0x0004000000000004, 0x0004000100000004, 0x0004000004000004, 0x0004000104000004, 0x0004000000040004, 0x0004000100040004, 0x0004000004040004, 0x0004000104040004, 0x0004000000000404, 0x0004000100000404, 0x0004000004000404, 0x0004000104000404, 0x0004000000040404, 0x0004000100040404, 0x0004000004040404, 0x0004000104040404, 0x0404000000000000, 0x0404000100000000, 0x0404000004000000, 0x0404000104000000, 0x0404000000040000, 0x0404000100040000, 0x0404000004040000, 0x0404000104040000, 0x0404000000000400, 0x0404000100000400, 0x0404000004000400, 0x0404000104000400, 0x0404000000040400, 0x0404000100040400, 0x0404000004040400, 0x0404000104040400, 0x0404000000000004, 0x0404000100000004, 0x0404000004000004, 0x0404000104000004, 0x0404000000040004, 0x0404000100040004, 0x0404000004040004, 0x0404000104040004, 0x0404000000000404, 0x0404000100000404, 0x0404000004000404, 0x0404000104000404, 0x0404000000040404, 0x0404000100040404, 0x0404000004040404, 0x0404000104040404, 0x0000040000000000, 0x0000040100000000, 0x0000040004000000, 0x0000040104000000, 0x0000040000040000, 0x0000040100040000, 0x0000040004040000, 0x0000040104040000, 0x0000040000000400, 0x0000040100000400, 0x0000040004000400, 0x0000040104000400, 0x0000040000040400, 0x0000040100040400, 0x0000040004040400, 0x0000040104040400, 0x0000040000000004, 0x0000040100000004, 0x0000040004000004, 0x0000040104000004, 0x0000040000040004, 0x0000040100040004, 0x0000040004040004, 0x0000040104040004, 0x0000040000000404, 0x0000040100000404, 0x0000040004000404, 0x0000040104000404, 0x0000040000040404, 0x0000040100040404, 0x0000040004040404, 0x0000040104040404, 0x0400040000000000, 0x0400040100000000, 0x0400040004000000, 0x0400040104000000, 0x0400040000040000, 0x0400040100040000, 0x0400040004040000, 0x0400040104040000, 0x0400040000000400, 0x0400040100000400, 0x0400040004000400, 0x0400040104000400, 0x0400040000040400, 0x0400040100040400, 0x0400040004040400, 0x0400040104040400, 0x0400040000000004, 0x0400040100000004, 0x0400040004000004, 0x0400040104000004, 0x0400040000040004, 0x0400040100040004, 0x0400040004040004, 0x0400040104040004, 0x0400040000000404, 0x0400040100000404, 0x0400040004000404, 0x0400040104000404, 0x0400040000040404, 0x0400040100040404, 0x0400040004040404, 0x0400040104040404, 0x0004040000000000, 0x0004040100000000, 0x0004040004000000, 0x0004040104000000, 0x0004040000040000, 0x0004040100040000, 0x0004040004040000, 0x0004040104040000, 0x0004040000000400, 0x0004040100000400, 0x0004040004000400, 0x0004040104000400, 0x0004040000040400, 0x0004040100040400, 0x0004040004040400, 0x0004040104040400, 0x0004040000000004, 0x0004040100000004, 0x0004040004000004, 0x0004040104000004, 0x0004040000040004, 0x0004040100040004, 0x0004040004040004, 0x0004040104040004, 0x0004040000000404, 0x0004040100000404, 0x0004040004000404, 0x0004040104000404, 0x0004040000040404, 0x0004040100040404, 0x0004040004040404, 0x0004040104040404, 0x0404040000000000, 0x0404040100000000, 0x0404040004000000, 0x0404040104000000, 0x0404040000040000, 0x0404040100040000, 0x0404040004040000, 0x0404040104040000, 0x0404040000000400, 0x0404040100000400, 0x0404040004000400, 0x0404040104000400, 0x0404040000040400, 0x0404040100040400, 0x0404040004040400, 0x0404040104040400, 0x0404040000000004, 0x0404040100000004, 0x0404040004000004, 0x0404040104000004, 0x0404040000040004, 0x0404040100040004, 0x0404040004040004, 0x0404040104040004, 0x0404040000000404, 0x0404040100000404, 0x0404040004000404, 0x0404040104000404, 0x0404040000040404, 0x0404040100040404, 0x0404040004040404, 0x0404040104040404 }; const u64bit DES_FPTAB2[256] = { 0x0000000000000000, 0x0000004000000000, 0x0000000001000000, 0x0000004001000000, 0x0000000000010000, 0x0000004000010000, 0x0000000001010000, 0x0000004001010000, 0x0000000000000100, 0x0000004000000100, 0x0000000001000100, 0x0000004001000100, 0x0000000000010100, 0x0000004000010100, 0x0000000001010100, 0x0000004001010100, 0x0000000000000001, 0x0000004000000001, 0x0000000001000001, 0x0000004001000001, 0x0000000000010001, 0x0000004000010001, 0x0000000001010001, 0x0000004001010001, 0x0000000000000101, 0x0000004000000101, 0x0000000001000101, 0x0000004001000101, 0x0000000000010101, 0x0000004000010101, 0x0000000001010101, 0x0000004001010101, 0x0100000000000000, 0x0100004000000000, 0x0100000001000000, 0x0100004001000000, 0x0100000000010000, 0x0100004000010000, 0x0100000001010000, 0x0100004001010000, 0x0100000000000100, 0x0100004000000100, 0x0100000001000100, 0x0100004001000100, 0x0100000000010100, 0x0100004000010100, 0x0100000001010100, 0x0100004001010100, 0x0100000000000001, 0x0100004000000001, 0x0100000001000001, 0x0100004001000001, 0x0100000000010001, 0x0100004000010001, 0x0100000001010001, 0x0100004001010001, 0x0100000000000101, 0x0100004000000101, 0x0100000001000101, 0x0100004001000101, 0x0100000000010101, 0x0100004000010101, 0x0100000001010101, 0x0100004001010101, 0x0001000000000000, 0x0001004000000000, 0x0001000001000000, 0x0001004001000000, 0x0001000000010000, 0x0001004000010000, 0x0001000001010000, 0x0001004001010000, 0x0001000000000100, 0x0001004000000100, 0x0001000001000100, 0x0001004001000100, 0x0001000000010100, 0x0001004000010100, 0x0001000001010100, 0x0001004001010100, 0x0001000000000001, 0x0001004000000001, 0x0001000001000001, 0x0001004001000001, 0x0001000000010001, 0x0001004000010001, 0x0001000001010001, 0x0001004001010001, 0x0001000000000101, 0x0001004000000101, 0x0001000001000101, 0x0001004001000101, 0x0001000000010101, 0x0001004000010101, 0x0001000001010101, 0x0001004001010101, 0x0101000000000000, 0x0101004000000000, 0x0101000001000000, 0x0101004001000000, 0x0101000000010000, 0x0101004000010000, 0x0101000001010000, 0x0101004001010000, 0x0101000000000100, 0x0101004000000100, 0x0101000001000100, 0x0101004001000100, 0x0101000000010100, 0x0101004000010100, 0x0101000001010100, 0x0101004001010100, 0x0101000000000001, 0x0101004000000001, 0x0101000001000001, 0x0101004001000001, 0x0101000000010001, 0x0101004000010001, 0x0101000001010001, 0x0101004001010001, 0x0101000000000101, 0x0101004000000101, 0x0101000001000101, 0x0101004001000101, 0x0101000000010101, 0x0101004000010101, 0x0101000001010101, 0x0101004001010101, 0x0000010000000000, 0x0000014000000000, 0x0000010001000000, 0x0000014001000000, 0x0000010000010000, 0x0000014000010000, 0x0000010001010000, 0x0000014001010000, 0x0000010000000100, 0x0000014000000100, 0x0000010001000100, 0x0000014001000100, 0x0000010000010100, 0x0000014000010100, 0x0000010001010100, 0x0000014001010100, 0x0000010000000001, 0x0000014000000001, 0x0000010001000001, 0x0000014001000001, 0x0000010000010001, 0x0000014000010001, 0x0000010001010001, 0x0000014001010001, 0x0000010000000101, 0x0000014000000101, 0x0000010001000101, 0x0000014001000101, 0x0000010000010101, 0x0000014000010101, 0x0000010001010101, 0x0000014001010101, 0x0100010000000000, 0x0100014000000000, 0x0100010001000000, 0x0100014001000000, 0x0100010000010000, 0x0100014000010000, 0x0100010001010000, 0x0100014001010000, 0x0100010000000100, 0x0100014000000100, 0x0100010001000100, 0x0100014001000100, 0x0100010000010100, 0x0100014000010100, 0x0100010001010100, 0x0100014001010100, 0x0100010000000001, 0x0100014000000001, 0x0100010001000001, 0x0100014001000001, 0x0100010000010001, 0x0100014000010001, 0x0100010001010001, 0x0100014001010001, 0x0100010000000101, 0x0100014000000101, 0x0100010001000101, 0x0100014001000101, 0x0100010000010101, 0x0100014000010101, 0x0100010001010101, 0x0100014001010101, 0x0001010000000000, 0x0001014000000000, 0x0001010001000000, 0x0001014001000000, 0x0001010000010000, 0x0001014000010000, 0x0001010001010000, 0x0001014001010000, 0x0001010000000100, 0x0001014000000100, 0x0001010001000100, 0x0001014001000100, 0x0001010000010100, 0x0001014000010100, 0x0001010001010100, 0x0001014001010100, 0x0001010000000001, 0x0001014000000001, 0x0001010001000001, 0x0001014001000001, 0x0001010000010001, 0x0001014000010001, 0x0001010001010001, 0x0001014001010001, 0x0001010000000101, 0x0001014000000101, 0x0001010001000101, 0x0001014001000101, 0x0001010000010101, 0x0001014000010101, 0x0001010001010101, 0x0001014001010101, 0x0101010000000000, 0x0101014000000000, 0x0101010001000000, 0x0101014001000000, 0x0101010000010000, 0x0101014000010000, 0x0101010001010000, 0x0101014001010000, 0x0101010000000100, 0x0101014000000100, 0x0101010001000100, 0x0101014001000100, 0x0101010000010100, 0x0101014000010100, 0x0101010001010100, 0x0101014001010100, 0x0101010000000001, 0x0101014000000001, 0x0101010001000001, 0x0101014001000001, 0x0101010000010001, 0x0101014000010001, 0x0101010001010001, 0x0101014001010001, 0x0101010000000101, 0x0101014000000101, 0x0101010001000101, 0x0101014001000101, 0x0101010000010101, 0x0101014000010101, 0x0101010001010101, 0x0101014001010101 }; } /* * DES * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * DESX Encryption */ void DESX::encrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { xor_buf(out, in, &K1[0], BLOCK_SIZE); des.encrypt(out); xor_buf(out, &K2[0], BLOCK_SIZE); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * DESX Decryption */ void DESX::decrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { xor_buf(out, in, &K2[0], BLOCK_SIZE); des.decrypt(out); xor_buf(out, &K1[0], BLOCK_SIZE); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * DESX Key Schedule */ void DESX::key_schedule(const byte key[], size_t) { K1.copy(key, 8); des.set_key(key + 8, 8); K2.copy(key + 16, 8); } } /* * GOST 28147-89 * (C) 1999-2009,2011 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { byte GOST_28147_89_Params::sbox_entry(size_t row, size_t col) const { byte x = sboxes[4 * col + (row / 2)]; return (row % 2 == 0) ? (x >> 4) : (x & 0x0F); } GOST_28147_89_Params::GOST_28147_89_Params(const std::string& n) : name(n) { // Encoded in the packed fromat from RFC 4357 // GostR3411_94_TestParamSet (OID 1.2.643.2.2.31.0) static const byte GOST_R_3411_TEST_PARAMS[64] = { 0x4E, 0x57, 0x64, 0xD1, 0xAB, 0x8D, 0xCB, 0xBF, 0x94, 0x1A, 0x7A, 0x4D, 0x2C, 0xD1, 0x10, 0x10, 0xD6, 0xA0, 0x57, 0x35, 0x8D, 0x38, 0xF2, 0xF7, 0x0F, 0x49, 0xD1, 0x5A, 0xEA, 0x2F, 0x8D, 0x94, 0x62, 0xEE, 0x43, 0x09, 0xB3, 0xF4, 0xA6, 0xA2, 0x18, 0xC6, 0x98, 0xE3, 0xC1, 0x7C, 0xE5, 0x7E, 0x70, 0x6B, 0x09, 0x66, 0xF7, 0x02, 0x3C, 0x8B, 0x55, 0x95, 0xBF, 0x28, 0x39, 0xB3, 0x2E, 0xCC }; // GostR3411-94-CryptoProParamSet (OID 1.2.643.2.2.31.1) static const byte GOST_R_3411_CRYPTOPRO_PARAMS[64] = { 0xA5, 0x74, 0x77, 0xD1, 0x4F, 0xFA, 0x66, 0xE3, 0x54, 0xC7, 0x42, 0x4A, 0x60, 0xEC, 0xB4, 0x19, 0x82, 0x90, 0x9D, 0x75, 0x1D, 0x4F, 0xC9, 0x0B, 0x3B, 0x12, 0x2F, 0x54, 0x79, 0x08, 0xA0, 0xAF, 0xD1, 0x3E, 0x1A, 0x38, 0xC7, 0xB1, 0x81, 0xC6, 0xE6, 0x56, 0x05, 0x87, 0x03, 0x25, 0xEB, 0xFE, 0x9C, 0x6D, 0xF8, 0x6D, 0x2E, 0xAB, 0xDE, 0x20, 0xBA, 0x89, 0x3C, 0x92, 0xF8, 0xD3, 0x53, 0xBC }; if(name == "R3411_94_TestParam") sboxes = GOST_R_3411_TEST_PARAMS; else if(name == "R3411_CryptoPro") sboxes = GOST_R_3411_CRYPTOPRO_PARAMS; else throw Invalid_Argument("GOST_28147_89_Params: Unknown " + name); } /* * GOST Constructor */ GOST_28147_89::GOST_28147_89(const GOST_28147_89_Params& param) : SBOX(1024), EK(8) { // Convert the parallel 4x4 sboxes into larger word-based sboxes for(size_t i = 0; i != 4; ++i) for(size_t j = 0; j != 256; ++j) { const u32bit T = (param.sbox_entry(2*i , j % 16)) | (param.sbox_entry(2*i+1, j / 16) << 4); SBOX[256*i+j] = rotate_left(T, (11+8*i) % 32); } } std::string GOST_28147_89::name() const { /* 'Guess' the right name for the sbox on the basis of the values. This would need to be updated if support for other sbox parameters is added. Preferably, we would just store the string value in the constructor, but can't break binary compat. */ std::string sbox_name = ""; if(SBOX[0] == 0x00072000) sbox_name = "R3411_94_TestParam"; else if(SBOX[0] == 0x0002D000) sbox_name = "R3411_CryptoPro"; else throw Internal_Error("GOST-28147 unrecognized sbox value"); return "GOST-28147-89(" + sbox_name + ")"; } /* * Two rounds of GOST */ #define GOST_2ROUND(N1, N2, R1, R2) \ do { \ u32bit T0 = N1 + EK[R1]; \ N2 ^= SBOX[get_byte(3, T0)] | \ SBOX[get_byte(2, T0)+256] | \ SBOX[get_byte(1, T0)+512] | \ SBOX[get_byte(0, T0)+768]; \ \ u32bit T1 = N2 + EK[R2]; \ N1 ^= SBOX[get_byte(3, T1)] | \ SBOX[get_byte(2, T1)+256] | \ SBOX[get_byte(1, T1)+512] | \ SBOX[get_byte(0, T1)+768]; \ } while(0) /* * GOST Encryption */ void GOST_28147_89::encrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u32bit N1 = load_le<u32bit>(in, 0); u32bit N2 = load_le<u32bit>(in, 1); for(size_t j = 0; j != 3; ++j) { GOST_2ROUND(N1, N2, 0, 1); GOST_2ROUND(N1, N2, 2, 3); GOST_2ROUND(N1, N2, 4, 5); GOST_2ROUND(N1, N2, 6, 7); } GOST_2ROUND(N1, N2, 7, 6); GOST_2ROUND(N1, N2, 5, 4); GOST_2ROUND(N1, N2, 3, 2); GOST_2ROUND(N1, N2, 1, 0); store_le(out, N2, N1); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * GOST Decryption */ void GOST_28147_89::decrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u32bit N1 = load_le<u32bit>(in, 0); u32bit N2 = load_le<u32bit>(in, 1); GOST_2ROUND(N1, N2, 0, 1); GOST_2ROUND(N1, N2, 2, 3); GOST_2ROUND(N1, N2, 4, 5); GOST_2ROUND(N1, N2, 6, 7); for(size_t j = 0; j != 3; ++j) { GOST_2ROUND(N1, N2, 7, 6); GOST_2ROUND(N1, N2, 5, 4); GOST_2ROUND(N1, N2, 3, 2); GOST_2ROUND(N1, N2, 1, 0); } store_le(out, N2, N1); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * GOST Key Schedule */ void GOST_28147_89::key_schedule(const byte key[], size_t) { for(size_t i = 0; i != 8; ++i) EK[i] = load_le<u32bit>(key, i); } } /* * IDEA * (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { /* * Multiplication modulo 65537 */ inline u16bit mul(u16bit x, u16bit y) { const u32bit P = static_cast<u32bit>(x) * y; // P ? 0xFFFF : 0 const u16bit P_mask = !P - 1; const u32bit P_hi = P >> 16; const u32bit P_lo = P & 0xFFFF; const u16bit r_1 = (P_lo - P_hi) + (P_lo < P_hi); const u16bit r_2 = 1 - x - y; return (r_1 & P_mask) | (r_2 & ~P_mask); } /* * Find multiplicative inverses modulo 65537 * * 65537 is prime; thus Fermat's little theorem tells us that * x^65537 == x modulo 65537, which means * x^(65537-2) == x^-1 modulo 65537 since * x^(65537-2) * x == 1 mod 65537 * * Do the exponentiation with a basic square and multiply: all bits are * of exponent are 1 so we always multiply */ u16bit mul_inv(u16bit x) { u16bit y = x; for(size_t i = 0; i != 15; ++i) { y = mul(y, y); // square y = mul(y, x); } return y; } /** * IDEA is involutional, depending only on the key schedule */ void idea_op(const byte in[], byte out[], size_t blocks, const u16bit K[52]) { const size_t BLOCK_SIZE = 8; for(size_t i = 0; i != blocks; ++i) { u16bit X1 = load_be<u16bit>(in, 0); u16bit X2 = load_be<u16bit>(in, 1); u16bit X3 = load_be<u16bit>(in, 2); u16bit X4 = load_be<u16bit>(in, 3); for(size_t j = 0; j != 8; ++j) { X1 = mul(X1, K[6*j+0]); X2 += K[6*j+1]; X3 += K[6*j+2]; X4 = mul(X4, K[6*j+3]); u16bit T0 = X3; X3 = mul(X3 ^ X1, K[6*j+4]); u16bit T1 = X2; X2 = mul((X2 ^ X4) + X3, K[6*j+5]); X3 += X2; X1 ^= X2; X4 ^= X3; X2 ^= T0; X3 ^= T1; } X1 = mul(X1, K[48]); X2 += K[50]; X3 += K[49]; X4 = mul(X4, K[51]); store_be(out, X1, X3, X2, X4); in += BLOCK_SIZE; out += BLOCK_SIZE; } } } /* * IDEA Encryption */ void IDEA::encrypt_n(const byte in[], byte out[], size_t blocks) const { idea_op(in, out, blocks, &EK[0]); } /* * IDEA Decryption */ void IDEA::decrypt_n(const byte in[], byte out[], size_t blocks) const { idea_op(in, out, blocks, &DK[0]); } /* * IDEA Key Schedule */ void IDEA::key_schedule(const byte key[], size_t) { for(size_t i = 0; i != 8; ++i) EK[i] = load_be<u16bit>(key, i); for(size_t i = 1, j = 8, offset = 0; j != 52; i %= 8, ++i, ++j) { EK[i+7+offset] = static_cast<u16bit>((EK[(i % 8) + offset] << 9) | (EK[((i+1) % 8) + offset] >> 7)); offset += (i == 8) ? 8 : 0; } DK[51] = mul_inv(EK[3]); DK[50] = -EK[2]; DK[49] = -EK[1]; DK[48] = mul_inv(EK[0]); for(size_t i = 1, j = 4, counter = 47; i != 8; ++i, j += 6) { DK[counter--] = EK[j+1]; DK[counter--] = EK[j]; DK[counter--] = mul_inv(EK[j+5]); DK[counter--] = -EK[j+3]; DK[counter--] = -EK[j+4]; DK[counter--] = mul_inv(EK[j+2]); } DK[5] = EK[47]; DK[4] = EK[46]; DK[3] = mul_inv(EK[51]); DK[2] = -EK[50]; DK[1] = -EK[49]; DK[0] = mul_inv(EK[48]); } } /* * IDEA in SSE2 * (C) 2009 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <emmintrin.h> namespace Botan { namespace { inline __m128i mul(__m128i X, u16bit K_16) { const __m128i zeros = _mm_set1_epi16(0); const __m128i ones = _mm_set1_epi16(1); const __m128i K = _mm_set1_epi16(K_16); const __m128i X_is_zero = _mm_cmpeq_epi16(X, zeros); const __m128i K_is_zero = _mm_cmpeq_epi16(K, zeros); const __m128i mul_lo = _mm_mullo_epi16(X, K); const __m128i mul_hi = _mm_mulhi_epu16(X, K); __m128i T = _mm_sub_epi16(mul_lo, mul_hi); // Unsigned compare; cmp = 1 if mul_lo < mul_hi else 0 const __m128i subs = _mm_subs_epu16(mul_hi, mul_lo); const __m128i cmp = _mm_min_epu8( _mm_or_si128(subs, _mm_srli_epi16(subs, 8)), ones); T = _mm_add_epi16(T, cmp); /* Selection: if X[i] is zero then assign 1-K if K is zero then assign 1-X[i] Could if() off value of K_16 for the second, but this gives a constant time implementation which is a nice bonus. */ T = _mm_or_si128( _mm_andnot_si128(X_is_zero, T), _mm_and_si128(_mm_sub_epi16(ones, K), X_is_zero)); T = _mm_or_si128( _mm_andnot_si128(K_is_zero, T), _mm_and_si128(_mm_sub_epi16(ones, X), K_is_zero)); return T; } /* * 4x8 matrix transpose * * FIXME: why do I need the extra set of unpack_epi32 here? Inverse in * transpose_out doesn't need it. Something with the shuffle? Removing * that extra unpack could easily save 3-4 cycles per block, and would * also help a lot with register pressure on 32-bit x86 */ void transpose_in(__m128i& B0, __m128i& B1, __m128i& B2, __m128i& B3) { __m128i T0 = _mm_unpackhi_epi32(B0, B1); __m128i T1 = _mm_unpacklo_epi32(B0, B1); __m128i T2 = _mm_unpackhi_epi32(B2, B3); __m128i T3 = _mm_unpacklo_epi32(B2, B3); __m128i T4 = _mm_unpacklo_epi32(T0, T1); __m128i T5 = _mm_unpackhi_epi32(T0, T1); __m128i T6 = _mm_unpacklo_epi32(T2, T3); __m128i T7 = _mm_unpackhi_epi32(T2, T3); T0 = _mm_shufflehi_epi16(T4, _MM_SHUFFLE(1, 3, 0, 2)); T1 = _mm_shufflehi_epi16(T5, _MM_SHUFFLE(1, 3, 0, 2)); T2 = _mm_shufflehi_epi16(T6, _MM_SHUFFLE(1, 3, 0, 2)); T3 = _mm_shufflehi_epi16(T7, _MM_SHUFFLE(1, 3, 0, 2)); T0 = _mm_shufflelo_epi16(T0, _MM_SHUFFLE(1, 3, 0, 2)); T1 = _mm_shufflelo_epi16(T1, _MM_SHUFFLE(1, 3, 0, 2)); T2 = _mm_shufflelo_epi16(T2, _MM_SHUFFLE(1, 3, 0, 2)); T3 = _mm_shufflelo_epi16(T3, _MM_SHUFFLE(1, 3, 0, 2)); T0 = _mm_shuffle_epi32(T0, _MM_SHUFFLE(3, 1, 2, 0)); T1 = _mm_shuffle_epi32(T1, _MM_SHUFFLE(3, 1, 2, 0)); T2 = _mm_shuffle_epi32(T2, _MM_SHUFFLE(3, 1, 2, 0)); T3 = _mm_shuffle_epi32(T3, _MM_SHUFFLE(3, 1, 2, 0)); B0 = _mm_unpacklo_epi64(T0, T2); B1 = _mm_unpackhi_epi64(T0, T2); B2 = _mm_unpacklo_epi64(T1, T3); B3 = _mm_unpackhi_epi64(T1, T3); } /* * 4x8 matrix transpose (reverse) */ void transpose_out(__m128i& B0, __m128i& B1, __m128i& B2, __m128i& B3) { __m128i T0 = _mm_unpacklo_epi64(B0, B1); __m128i T1 = _mm_unpacklo_epi64(B2, B3); __m128i T2 = _mm_unpackhi_epi64(B0, B1); __m128i T3 = _mm_unpackhi_epi64(B2, B3); T0 = _mm_shuffle_epi32(T0, _MM_SHUFFLE(3, 1, 2, 0)); T1 = _mm_shuffle_epi32(T1, _MM_SHUFFLE(3, 1, 2, 0)); T2 = _mm_shuffle_epi32(T2, _MM_SHUFFLE(3, 1, 2, 0)); T3 = _mm_shuffle_epi32(T3, _MM_SHUFFLE(3, 1, 2, 0)); T0 = _mm_shufflehi_epi16(T0, _MM_SHUFFLE(3, 1, 2, 0)); T1 = _mm_shufflehi_epi16(T1, _MM_SHUFFLE(3, 1, 2, 0)); T2 = _mm_shufflehi_epi16(T2, _MM_SHUFFLE(3, 1, 2, 0)); T3 = _mm_shufflehi_epi16(T3, _MM_SHUFFLE(3, 1, 2, 0)); T0 = _mm_shufflelo_epi16(T0, _MM_SHUFFLE(3, 1, 2, 0)); T1 = _mm_shufflelo_epi16(T1, _MM_SHUFFLE(3, 1, 2, 0)); T2 = _mm_shufflelo_epi16(T2, _MM_SHUFFLE(3, 1, 2, 0)); T3 = _mm_shufflelo_epi16(T3, _MM_SHUFFLE(3, 1, 2, 0)); B0 = _mm_unpacklo_epi32(T0, T1); B1 = _mm_unpackhi_epi32(T0, T1); B2 = _mm_unpacklo_epi32(T2, T3); B3 = _mm_unpackhi_epi32(T2, T3); } /* * IDEA encryption/decryption in SSE2 */ void idea_op_8(const byte in[64], byte out[64], const u16bit EK[52]) { const __m128i* in_mm = reinterpret_cast<const __m128i*>(in); __m128i B0 = _mm_loadu_si128(in_mm + 0); __m128i B1 = _mm_loadu_si128(in_mm + 1); __m128i B2 = _mm_loadu_si128(in_mm + 2); __m128i B3 = _mm_loadu_si128(in_mm + 3); transpose_in(B0, B1, B2, B3); // byte swap B0 = _mm_or_si128(_mm_slli_epi16(B0, 8), _mm_srli_epi16(B0, 8)); B1 = _mm_or_si128(_mm_slli_epi16(B1, 8), _mm_srli_epi16(B1, 8)); B2 = _mm_or_si128(_mm_slli_epi16(B2, 8), _mm_srli_epi16(B2, 8)); B3 = _mm_or_si128(_mm_slli_epi16(B3, 8), _mm_srli_epi16(B3, 8)); for(size_t i = 0; i != 8; ++i) { B0 = mul(B0, EK[6*i+0]); B1 = _mm_add_epi16(B1, _mm_set1_epi16(EK[6*i+1])); B2 = _mm_add_epi16(B2, _mm_set1_epi16(EK[6*i+2])); B3 = mul(B3, EK[6*i+3]); __m128i T0 = B2; B2 = _mm_xor_si128(B2, B0); B2 = mul(B2, EK[6*i+4]); __m128i T1 = B1; B1 = _mm_xor_si128(B1, B3); B1 = _mm_add_epi16(B1, B2); B1 = mul(B1, EK[6*i+5]); B2 = _mm_add_epi16(B2, B1); B0 = _mm_xor_si128(B0, B1); B1 = _mm_xor_si128(B1, T0); B3 = _mm_xor_si128(B3, B2); B2 = _mm_xor_si128(B2, T1); } B0 = mul(B0, EK[48]); B1 = _mm_add_epi16(B1, _mm_set1_epi16(EK[50])); B2 = _mm_add_epi16(B2, _mm_set1_epi16(EK[49])); B3 = mul(B3, EK[51]); // byte swap B0 = _mm_or_si128(_mm_slli_epi16(B0, 8), _mm_srli_epi16(B0, 8)); B1 = _mm_or_si128(_mm_slli_epi16(B1, 8), _mm_srli_epi16(B1, 8)); B2 = _mm_or_si128(_mm_slli_epi16(B2, 8), _mm_srli_epi16(B2, 8)); B3 = _mm_or_si128(_mm_slli_epi16(B3, 8), _mm_srli_epi16(B3, 8)); transpose_out(B0, B2, B1, B3); __m128i* out_mm = reinterpret_cast<__m128i*>(out); _mm_storeu_si128(out_mm + 0, B0); _mm_storeu_si128(out_mm + 1, B2); _mm_storeu_si128(out_mm + 2, B1); _mm_storeu_si128(out_mm + 3, B3); } } /* * IDEA Encryption */ void IDEA_SSE2::encrypt_n(const byte in[], byte out[], size_t blocks) const { const u16bit* KS = &this->get_EK()[0]; while(blocks >= 8) { idea_op_8(in, out, KS); in += 8 * BLOCK_SIZE; out += 8 * BLOCK_SIZE; blocks -= 8; } if(blocks) IDEA::encrypt_n(in, out, blocks); } /* * IDEA Decryption */ void IDEA_SSE2::decrypt_n(const byte in[], byte out[], size_t blocks) const { const u16bit* KS = &this->get_DK()[0]; while(blocks >= 8) { idea_op_8(in, out, KS); in += 8 * BLOCK_SIZE; out += 8 * BLOCK_SIZE; blocks -= 8; } if(blocks) IDEA::decrypt_n(in, out, blocks); } } /* * KASUMI * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { /* * KASUMI S-Boxes */ const byte KASUMI_SBOX_S7[128] = { 0x36, 0x32, 0x3E, 0x38, 0x16, 0x22, 0x5E, 0x60, 0x26, 0x06, 0x3F, 0x5D, 0x02, 0x12, 0x7B, 0x21, 0x37, 0x71, 0x27, 0x72, 0x15, 0x43, 0x41, 0x0C, 0x2F, 0x49, 0x2E, 0x1B, 0x19, 0x6F, 0x7C, 0x51, 0x35, 0x09, 0x79, 0x4F, 0x34, 0x3C, 0x3A, 0x30, 0x65, 0x7F, 0x28, 0x78, 0x68, 0x46, 0x47, 0x2B, 0x14, 0x7A, 0x48, 0x3D, 0x17, 0x6D, 0x0D, 0x64, 0x4D, 0x01, 0x10, 0x07, 0x52, 0x0A, 0x69, 0x62, 0x75, 0x74, 0x4C, 0x0B, 0x59, 0x6A, 0x00, 0x7D, 0x76, 0x63, 0x56, 0x45, 0x1E, 0x39, 0x7E, 0x57, 0x70, 0x33, 0x11, 0x05, 0x5F, 0x0E, 0x5A, 0x54, 0x5B, 0x08, 0x23, 0x67, 0x20, 0x61, 0x1C, 0x42, 0x66, 0x1F, 0x1A, 0x2D, 0x4B, 0x04, 0x55, 0x5C, 0x25, 0x4A, 0x50, 0x31, 0x44, 0x1D, 0x73, 0x2C, 0x40, 0x6B, 0x6C, 0x18, 0x6E, 0x53, 0x24, 0x4E, 0x2A, 0x13, 0x0F, 0x29, 0x58, 0x77, 0x3B, 0x03 }; const u16bit KASUMI_SBOX_S9[512] = { 0x00A7, 0x00EF, 0x00A1, 0x017B, 0x0187, 0x014E, 0x0009, 0x0152, 0x0026, 0x00E2, 0x0030, 0x0166, 0x01C4, 0x0181, 0x005A, 0x018D, 0x00B7, 0x00FD, 0x0093, 0x014B, 0x019F, 0x0154, 0x0033, 0x016A, 0x0132, 0x01F4, 0x0106, 0x0052, 0x00D8, 0x009F, 0x0164, 0x00B1, 0x00AF, 0x00F1, 0x01E9, 0x0025, 0x00CE, 0x0011, 0x0000, 0x014D, 0x002C, 0x00FE, 0x017A, 0x003A, 0x008F, 0x00DC, 0x0051, 0x0190, 0x005F, 0x0003, 0x013B, 0x00F5, 0x0036, 0x00EB, 0x00DA, 0x0195, 0x01D8, 0x0108, 0x00AC, 0x01EE, 0x0173, 0x0122, 0x018F, 0x004C, 0x00A5, 0x00C5, 0x018B, 0x0079, 0x0101, 0x01E0, 0x01A7, 0x00D4, 0x00F0, 0x001C, 0x01CE, 0x00B0, 0x0196, 0x01FB, 0x0120, 0x00DF, 0x01F5, 0x0197, 0x00F9, 0x0109, 0x0059, 0x00BA, 0x00DD, 0x01AC, 0x00A4, 0x004A, 0x01B8, 0x00C4, 0x01CA, 0x01A5, 0x015E, 0x00A3, 0x00E8, 0x009E, 0x0086, 0x0162, 0x000D, 0x00FA, 0x01EB, 0x008E, 0x00BF, 0x0045, 0x00C1, 0x01A9, 0x0098, 0x00E3, 0x016E, 0x0087, 0x0158, 0x012C, 0x0114, 0x00F2, 0x01B5, 0x0140, 0x0071, 0x0116, 0x000B, 0x00F3, 0x0057, 0x013D, 0x0024, 0x005D, 0x01F0, 0x001B, 0x01E7, 0x01BE, 0x01E2, 0x0029, 0x0044, 0x009C, 0x01C9, 0x0083, 0x0146, 0x0193, 0x0153, 0x0014, 0x0027, 0x0073, 0x01BA, 0x007C, 0x01DB, 0x0180, 0x01FC, 0x0035, 0x0070, 0x00AA, 0x01DF, 0x0097, 0x007E, 0x00A9, 0x0049, 0x010C, 0x0117, 0x0141, 0x00A8, 0x016C, 0x016B, 0x0124, 0x002E, 0x01F3, 0x0189, 0x0147, 0x0144, 0x0018, 0x01C8, 0x010B, 0x009D, 0x01CC, 0x01E8, 0x01AA, 0x0135, 0x00E5, 0x01B7, 0x01FA, 0x00D0, 0x010F, 0x015D, 0x0191, 0x01B2, 0x00EC, 0x0010, 0x00D1, 0x0167, 0x0034, 0x0038, 0x0078, 0x00C7, 0x0115, 0x01D1, 0x01A0, 0x00FC, 0x011F, 0x00F6, 0x0006, 0x0053, 0x0131, 0x01A4, 0x0159, 0x0099, 0x01F6, 0x0041, 0x003D, 0x00F4, 0x011A, 0x00AD, 0x00DE, 0x01A2, 0x0043, 0x0182, 0x0170, 0x0105, 0x0065, 0x01DC, 0x0123, 0x00C3, 0x01AE, 0x0031, 0x004F, 0x00A6, 0x014A, 0x0118, 0x017F, 0x0175, 0x0080, 0x017E, 0x0198, 0x009B, 0x01EF, 0x016F, 0x0184, 0x0112, 0x006B, 0x01CB, 0x01A1, 0x003E, 0x01C6, 0x0084, 0x00E1, 0x00CB, 0x013C, 0x00EA, 0x000E, 0x012D, 0x005B, 0x01F7, 0x011E, 0x01A8, 0x00D3, 0x015B, 0x0133, 0x008C, 0x0176, 0x0023, 0x0067, 0x007D, 0x01AB, 0x0013, 0x00D6, 0x01C5, 0x0092, 0x01F2, 0x013A, 0x01BC, 0x00E6, 0x0100, 0x0149, 0x00C6, 0x011D, 0x0032, 0x0074, 0x004E, 0x019A, 0x000A, 0x00CD, 0x01FE, 0x00AB, 0x00E7, 0x002D, 0x008B, 0x01D3, 0x001D, 0x0056, 0x01F9, 0x0020, 0x0048, 0x001A, 0x0156, 0x0096, 0x0139, 0x01EA, 0x01AF, 0x00EE, 0x019B, 0x0145, 0x0095, 0x01D9, 0x0028, 0x0077, 0x00AE, 0x0163, 0x00B9, 0x00E9, 0x0185, 0x0047, 0x01C0, 0x0111, 0x0174, 0x0037, 0x006E, 0x00B2, 0x0142, 0x000C, 0x01D5, 0x0188, 0x0171, 0x00BE, 0x0001, 0x006D, 0x0177, 0x0089, 0x00B5, 0x0058, 0x004B, 0x0134, 0x0104, 0x01E4, 0x0062, 0x0110, 0x0172, 0x0113, 0x019C, 0x006F, 0x0150, 0x013E, 0x0004, 0x01F8, 0x01EC, 0x0103, 0x0130, 0x004D, 0x0151, 0x01B3, 0x0015, 0x0165, 0x012F, 0x014C, 0x01E3, 0x0012, 0x002F, 0x0055, 0x0019, 0x01F1, 0x01DA, 0x0121, 0x0064, 0x010D, 0x0128, 0x01DE, 0x010E, 0x006A, 0x001F, 0x0068, 0x01B1, 0x0054, 0x019E, 0x01E6, 0x018A, 0x0060, 0x0063, 0x009A, 0x01FF, 0x0094, 0x019D, 0x0169, 0x0199, 0x00FF, 0x00A2, 0x00D7, 0x012E, 0x00C9, 0x010A, 0x015F, 0x0157, 0x0090, 0x01B9, 0x016D, 0x006C, 0x012A, 0x00FB, 0x0022, 0x00B6, 0x01FD, 0x008A, 0x00D2, 0x014F, 0x0085, 0x0137, 0x0160, 0x0148, 0x008D, 0x018C, 0x015A, 0x007B, 0x013F, 0x01C2, 0x0119, 0x01AD, 0x00E4, 0x01BB, 0x01E1, 0x005C, 0x0194, 0x01E5, 0x01A6, 0x00F8, 0x0129, 0x0017, 0x00D5, 0x0082, 0x01D2, 0x0016, 0x00D9, 0x011B, 0x0046, 0x0126, 0x0168, 0x01A3, 0x007F, 0x0138, 0x0179, 0x0007, 0x01D4, 0x00C2, 0x0002, 0x0075, 0x0127, 0x01CF, 0x0102, 0x00E0, 0x01BF, 0x00F7, 0x00BB, 0x0050, 0x018E, 0x011C, 0x0161, 0x0069, 0x0186, 0x012B, 0x01D7, 0x01D6, 0x00B8, 0x0039, 0x00C8, 0x015C, 0x003F, 0x00CC, 0x00BC, 0x0021, 0x01C3, 0x0061, 0x001E, 0x0136, 0x00DB, 0x005E, 0x00A0, 0x0081, 0x01ED, 0x0040, 0x00B3, 0x0107, 0x0066, 0x00BD, 0x00CF, 0x0072, 0x0192, 0x01B6, 0x01DD, 0x0183, 0x007A, 0x00C0, 0x002A, 0x017D, 0x0005, 0x0091, 0x0076, 0x00B4, 0x01C1, 0x0125, 0x0143, 0x0088, 0x017C, 0x002B, 0x0042, 0x003C, 0x01C7, 0x0155, 0x01BD, 0x00CA, 0x01B0, 0x0008, 0x00ED, 0x000F, 0x0178, 0x01B4, 0x01D0, 0x003B, 0x01CD }; /* * KASUMI FI Function */ u16bit FI(u16bit I, u16bit K) { u16bit D9 = (I >> 7); byte D7 = (I & 0x7F); D9 = KASUMI_SBOX_S9[D9] ^ D7; D7 = KASUMI_SBOX_S7[D7] ^ (D9 & 0x7F); D7 ^= (K >> 9); D9 = KASUMI_SBOX_S9[D9 ^ (K & 0x1FF)] ^ D7; D7 = KASUMI_SBOX_S7[D7] ^ (D9 & 0x7F); return (D7 << 9) | D9; } } /* * KASUMI Encryption */ void KASUMI::encrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u16bit B0 = load_be<u16bit>(in, 0); u16bit B1 = load_be<u16bit>(in, 1); u16bit B2 = load_be<u16bit>(in, 2); u16bit B3 = load_be<u16bit>(in, 3); for(size_t j = 0; j != 8; j += 2) { const u16bit* K = &EK[8*j]; u16bit R = B1 ^ (rotate_left(B0, 1) & K[0]); u16bit L = B0 ^ (rotate_left(R, 1) | K[1]); L = FI(L ^ K[ 2], K[ 3]) ^ R; R = FI(R ^ K[ 4], K[ 5]) ^ L; L = FI(L ^ K[ 6], K[ 7]) ^ R; R = B2 ^= R; L = B3 ^= L; R = FI(R ^ K[10], K[11]) ^ L; L = FI(L ^ K[12], K[13]) ^ R; R = FI(R ^ K[14], K[15]) ^ L; R ^= (rotate_left(L, 1) & K[8]); L ^= (rotate_left(R, 1) | K[9]); B0 ^= L; B1 ^= R; } store_be(out, B0, B1, B2, B3); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * KASUMI Decryption */ void KASUMI::decrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u16bit B0 = load_be<u16bit>(in, 0); u16bit B1 = load_be<u16bit>(in, 1); u16bit B2 = load_be<u16bit>(in, 2); u16bit B3 = load_be<u16bit>(in, 3); for(size_t j = 0; j != 8; j += 2) { const u16bit* K = &EK[8*(6-j)]; u16bit L = B2, R = B3; L = FI(L ^ K[10], K[11]) ^ R; R = FI(R ^ K[12], K[13]) ^ L; L = FI(L ^ K[14], K[15]) ^ R; L ^= (rotate_left(R, 1) & K[8]); R ^= (rotate_left(L, 1) | K[9]); R = B0 ^= R; L = B1 ^= L; L ^= (rotate_left(R, 1) & K[0]); R ^= (rotate_left(L, 1) | K[1]); R = FI(R ^ K[2], K[3]) ^ L; L = FI(L ^ K[4], K[5]) ^ R; R = FI(R ^ K[6], K[7]) ^ L; B2 ^= L; B3 ^= R; } store_be(out, B0, B1, B2, B3); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * KASUMI Key Schedule */ void KASUMI::key_schedule(const byte key[], size_t) { static const u16bit RC[] = { 0x0123, 0x4567, 0x89AB, 0xCDEF, 0xFEDC, 0xBA98, 0x7654, 0x3210 }; SecureVector<u16bit> K(16); for(size_t i = 0; i != 8; ++i) { K[i] = load_be<u16bit>(key, i); K[i+8] = K[i] ^ RC[i]; } for(size_t i = 0; i != 8; ++i) { EK[8*i ] = rotate_left(K[(i+0) % 8 ], 2); EK[8*i+1] = rotate_left(K[(i+2) % 8 + 8], 1); EK[8*i+2] = rotate_left(K[(i+1) % 8 ], 5); EK[8*i+3] = K[(i+4) % 8 + 8]; EK[8*i+4] = rotate_left(K[(i+5) % 8 ], 8); EK[8*i+5] = K[(i+3) % 8 + 8]; EK[8*i+6] = rotate_left(K[(i+6) % 8 ], 13); EK[8*i+7] = K[(i+7) % 8 + 8]; } } } /* * Lion * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Lion Encryption */ void Lion::encrypt_n(const byte in[], byte out[], size_t blocks) const { SecureVector<byte> buffer_vec(LEFT_SIZE); byte* buffer = &buffer_vec[0]; for(size_t i = 0; i != blocks; ++i) { xor_buf(buffer, in, &key1[0], LEFT_SIZE); cipher->set_key(buffer, LEFT_SIZE); cipher->cipher(in + LEFT_SIZE, out + LEFT_SIZE, RIGHT_SIZE); hash->update(out + LEFT_SIZE, RIGHT_SIZE); hash->final(buffer); xor_buf(out, in, buffer, LEFT_SIZE); xor_buf(buffer, out, &key2[0], LEFT_SIZE); cipher->set_key(buffer, LEFT_SIZE); cipher->cipher1(out + LEFT_SIZE, RIGHT_SIZE); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * Lion Decryption */ void Lion::decrypt_n(const byte in[], byte out[], size_t blocks) const { SecureVector<byte> buffer_vec(LEFT_SIZE); byte* buffer = &buffer_vec[0]; for(size_t i = 0; i != blocks; ++i) { xor_buf(buffer, in, &key2[0], LEFT_SIZE); cipher->set_key(buffer, LEFT_SIZE); cipher->cipher(in + LEFT_SIZE, out + LEFT_SIZE, RIGHT_SIZE); hash->update(out + LEFT_SIZE, RIGHT_SIZE); hash->final(buffer); xor_buf(out, in, buffer, LEFT_SIZE); xor_buf(buffer, out, &key1[0], LEFT_SIZE); cipher->set_key(buffer, LEFT_SIZE); cipher->cipher1(out + LEFT_SIZE, RIGHT_SIZE); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * Lion Key Schedule */ void Lion::key_schedule(const byte key[], size_t length) { clear(); key1.copy(key, length / 2); key2.copy(key + length / 2, length / 2); } /* * Return the name of this type */ std::string Lion::name() const { return "Lion(" + hash->name() + "," + cipher->name() + "," + to_string(BLOCK_SIZE) + ")"; } /* * Return a clone of this object */ BlockCipher* Lion::clone() const { return new Lion(hash->clone(), cipher->clone(), BLOCK_SIZE); } /* * Clear memory of sensitive data */ void Lion::clear() { hash->clear(); cipher->clear(); zeroise(key1); zeroise(key2); } /* * Lion Constructor */ Lion::Lion(HashFunction* hash_in, StreamCipher* sc_in, size_t block_len) : BLOCK_SIZE(std::max<size_t>(2*hash_in->output_length() + 1, block_len)), LEFT_SIZE(hash_in->output_length()), RIGHT_SIZE(BLOCK_SIZE - LEFT_SIZE), hash(hash_in), cipher(sc_in) { if(2*LEFT_SIZE + 1 > BLOCK_SIZE) throw Invalid_Argument(name() + ": Chosen block size is too small"); if(!cipher->valid_keylength(LEFT_SIZE)) throw Invalid_Argument(name() + ": This stream/hash combo is invalid"); key1.resize(LEFT_SIZE); key2.resize(LEFT_SIZE); } } /* * Luby-Rackoff * (C) 1999-2008 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Luby-Rackoff Encryption */ void LubyRackoff::encrypt_n(const byte in[], byte out[], size_t blocks) const { const size_t len = hash->output_length(); SecureVector<byte> buffer_vec(len); byte* buffer = &buffer_vec[0]; for(size_t i = 0; i != blocks; ++i) { hash->update(K1); hash->update(in, len); hash->final(buffer); xor_buf(out + len, in + len, buffer, len); hash->update(K2); hash->update(out + len, len); hash->final(buffer); xor_buf(out, in, buffer, len); hash->update(K1); hash->update(out, len); hash->final(buffer); xor_buf(out + len, buffer, len); hash->update(K2); hash->update(out + len, len); hash->final(buffer); xor_buf(out, buffer, len); in += 2 * len; out += 2 * len; } } /* * Luby-Rackoff Decryption */ void LubyRackoff::decrypt_n(const byte in[], byte out[], size_t blocks) const { const size_t len = hash->output_length(); SecureVector<byte> buffer_vec(len); byte* buffer = &buffer_vec[0]; for(size_t i = 0; i != blocks; ++i) { hash->update(K2); hash->update(in + len, len); hash->final(buffer); xor_buf(out, in, buffer, len); hash->update(K1); hash->update(out, len); hash->final(buffer); xor_buf(out + len, in + len, buffer, len); hash->update(K2); hash->update(out + len, len); hash->final(buffer); xor_buf(out, buffer, len); hash->update(K1); hash->update(out, len); hash->final(buffer); xor_buf(out + len, buffer, len); in += 2 * len; out += 2 * len; } } /* * Luby-Rackoff Key Schedule */ void LubyRackoff::key_schedule(const byte key[], size_t length) { K1.resize(length / 2); K2.resize(length / 2); copy_mem(&K1[0], key , length / 2); copy_mem(&K2[0], key + length / 2, length / 2); } /* * Clear memory of sensitive data */ void LubyRackoff::clear() { zeroise(K1); zeroise(K2); hash->clear(); } /* * Return a clone of this object */ BlockCipher* LubyRackoff::clone() const { return new LubyRackoff(hash->clone()); } /* * Return the name of this type */ std::string LubyRackoff::name() const { return "Luby-Rackoff(" + hash->name() + ")"; } /* * Luby-Rackoff Constructor */ LubyRackoff::LubyRackoff(HashFunction* h) : hash(h) { } } /* * MARS * (C) 1999-2009 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { /** * The MARS sbox */ const u32bit SBOX[512] = { 0x09D0C479, 0x28C8FFE0, 0x84AA6C39, 0x9DAD7287, 0x7DFF9BE3, 0xD4268361, 0xC96DA1D4, 0x7974CC93, 0x85D0582E, 0x2A4B5705, 0x1CA16A62, 0xC3BD279D, 0x0F1F25E5, 0x5160372F, 0xC695C1FB, 0x4D7FF1E4, 0xAE5F6BF4, 0x0D72EE46, 0xFF23DE8A, 0xB1CF8E83, 0xF14902E2, 0x3E981E42, 0x8BF53EB6, 0x7F4BF8AC, 0x83631F83, 0x25970205, 0x76AFE784, 0x3A7931D4, 0x4F846450, 0x5C64C3F6, 0x210A5F18, 0xC6986A26, 0x28F4E826, 0x3A60A81C, 0xD340A664, 0x7EA820C4, 0x526687C5, 0x7EDDD12B, 0x32A11D1D, 0x9C9EF086, 0x80F6E831, 0xAB6F04AD, 0x56FB9B53, 0x8B2E095C, 0xB68556AE, 0xD2250B0D, 0x294A7721, 0xE21FB253, 0xAE136749, 0xE82AAE86, 0x93365104, 0x99404A66, 0x78A784DC, 0xB69BA84B, 0x04046793, 0x23DB5C1E, 0x46CAE1D6, 0x2FE28134, 0x5A223942, 0x1863CD5B, 0xC190C6E3, 0x07DFB846, 0x6EB88816, 0x2D0DCC4A, 0xA4CCAE59, 0x3798670D, 0xCBFA9493, 0x4F481D45, 0xEAFC8CA8, 0xDB1129D6, 0xB0449E20, 0x0F5407FB, 0x6167D9A8, 0xD1F45763, 0x4DAA96C3, 0x3BEC5958, 0xABABA014, 0xB6CCD201, 0x38D6279F, 0x02682215, 0x8F376CD5, 0x092C237E, 0xBFC56593, 0x32889D2C, 0x854B3E95, 0x05BB9B43, 0x7DCD5DCD, 0xA02E926C, 0xFAE527E5, 0x36A1C330, 0x3412E1AE, 0xF257F462, 0x3C4F1D71, 0x30A2E809, 0x68E5F551, 0x9C61BA44, 0x5DED0AB8, 0x75CE09C8, 0x9654F93E, 0x698C0CCA, 0x243CB3E4, 0x2B062B97, 0x0F3B8D9E, 0x00E050DF, 0xFC5D6166, 0xE35F9288, 0xC079550D, 0x0591AEE8, 0x8E531E74, 0x75FE3578, 0x2F6D829A, 0xF60B21AE, 0x95E8EB8D, 0x6699486B, 0x901D7D9B, 0xFD6D6E31, 0x1090ACEF, 0xE0670DD8, 0xDAB2E692, 0xCD6D4365, 0xE5393514, 0x3AF345F0, 0x6241FC4D, 0x460DA3A3, 0x7BCF3729, 0x8BF1D1E0, 0x14AAC070, 0x1587ED55, 0x3AFD7D3E, 0xD2F29E01, 0x29A9D1F6, 0xEFB10C53, 0xCF3B870F, 0xB414935C, 0x664465ED, 0x024ACAC7, 0x59A744C1, 0x1D2936A7, 0xDC580AA6, 0xCF574CA8, 0x040A7A10, 0x6CD81807, 0x8A98BE4C, 0xACCEA063, 0xC33E92B5, 0xD1E0E03D, 0xB322517E, 0x2092BD13, 0x386B2C4A, 0x52E8DD58, 0x58656DFB, 0x50820371, 0x41811896, 0xE337EF7E, 0xD39FB119, 0xC97F0DF6, 0x68FEA01B, 0xA150A6E5, 0x55258962, 0xEB6FF41B, 0xD7C9CD7A, 0xA619CD9E, 0xBCF09576, 0x2672C073, 0xF003FB3C, 0x4AB7A50B, 0x1484126A, 0x487BA9B1, 0xA64FC9C6, 0xF6957D49, 0x38B06A75, 0xDD805FCD, 0x63D094CF, 0xF51C999E, 0x1AA4D343, 0xB8495294, 0xCE9F8E99, 0xBFFCD770, 0xC7C275CC, 0x378453A7, 0x7B21BE33, 0x397F41BD, 0x4E94D131, 0x92CC1F98, 0x5915EA51, 0x99F861B7, 0xC9980A88, 0x1D74FD5F, 0xB0A495F8, 0x614DEED0, 0xB5778EEA, 0x5941792D, 0xFA90C1F8, 0x33F824B4, 0xC4965372, 0x3FF6D550, 0x4CA5FEC0, 0x8630E964, 0x5B3FBBD6, 0x7DA26A48, 0xB203231A, 0x04297514, 0x2D639306, 0x2EB13149, 0x16A45272, 0x532459A0, 0x8E5F4872, 0xF966C7D9, 0x07128DC0, 0x0D44DB62, 0xAFC8D52D, 0x06316131, 0xD838E7CE, 0x1BC41D00, 0x3A2E8C0F, 0xEA83837E, 0xB984737D, 0x13BA4891, 0xC4F8B949, 0xA6D6ACB3, 0xA215CDCE, 0x8359838B, 0x6BD1AA31, 0xF579DD52, 0x21B93F93, 0xF5176781, 0x187DFDDE, 0xE94AEB76, 0x2B38FD54, 0x431DE1DA, 0xAB394825, 0x9AD3048F, 0xDFEA32AA, 0x659473E3, 0x623F7863, 0xF3346C59, 0xAB3AB685, 0x3346A90B, 0x6B56443E, 0xC6DE01F8, 0x8D421FC0, 0x9B0ED10C, 0x88F1A1E9, 0x54C1F029, 0x7DEAD57B, 0x8D7BA426, 0x4CF5178A, 0x551A7CCA, 0x1A9A5F08, 0xFCD651B9, 0x25605182, 0xE11FC6C3, 0xB6FD9676, 0x337B3027, 0xB7C8EB14, 0x9E5FD030, 0x6B57E354, 0xAD913CF7, 0x7E16688D, 0x58872A69, 0x2C2FC7DF, 0xE389CCC6, 0x30738DF1, 0x0824A734, 0xE1797A8B, 0xA4A8D57B, 0x5B5D193B, 0xC8A8309B, 0x73F9A978, 0x73398D32, 0x0F59573E, 0xE9DF2B03, 0xE8A5B6C8, 0x848D0704, 0x98DF93C2, 0x720A1DC3, 0x684F259A, 0x943BA848, 0xA6370152, 0x863B5EA3, 0xD17B978B, 0x6D9B58EF, 0x0A700DD4, 0xA73D36BF, 0x8E6A0829, 0x8695BC14, 0xE35B3447, 0x933AC568, 0x8894B022, 0x2F511C27, 0xDDFBCC3C, 0x006662B6, 0x117C83FE, 0x4E12B414, 0xC2BCA766, 0x3A2FEC10, 0xF4562420, 0x55792E2A, 0x46F5D857, 0xCEDA25CE, 0xC3601D3B, 0x6C00AB46, 0xEFAC9C28, 0xB3C35047, 0x611DFEE3, 0x257C3207, 0xFDD58482, 0x3B14D84F, 0x23BECB64, 0xA075F3A3, 0x088F8EAD, 0x07ADF158, 0x7796943C, 0xFACABF3D, 0xC09730CD, 0xF7679969, 0xDA44E9ED, 0x2C854C12, 0x35935FA3, 0x2F057D9F, 0x690624F8, 0x1CB0BAFD, 0x7B0DBDC6, 0x810F23BB, 0xFA929A1A, 0x6D969A17, 0x6742979B, 0x74AC7D05, 0x010E65C4, 0x86A3D963, 0xF907B5A0, 0xD0042BD3, 0x158D7D03, 0x287A8255, 0xBBA8366F, 0x096EDC33, 0x21916A7B, 0x77B56B86, 0x951622F9, 0xA6C5E650, 0x8CEA17D1, 0xCD8C62BC, 0xA3D63433, 0x358A68FD, 0x0F9B9D3C, 0xD6AA295B, 0xFE33384A, 0xC000738E, 0xCD67EB2F, 0xE2EB6DC2, 0x97338B02, 0x06C9F246, 0x419CF1AD, 0x2B83C045, 0x3723F18A, 0xCB5B3089, 0x160BEAD7, 0x5D494656, 0x35F8A74B, 0x1E4E6C9E, 0x000399BD, 0x67466880, 0xB4174831, 0xACF423B2, 0xCA815AB3, 0x5A6395E7, 0x302A67C5, 0x8BDB446B, 0x108F8FA4, 0x10223EDA, 0x92B8B48B, 0x7F38D0EE, 0xAB2701D4, 0x0262D415, 0xAF224A30, 0xB3D88ABA, 0xF8B2C3AF, 0xDAF7EF70, 0xCC97D3B7, 0xE9614B6C, 0x2BAEBFF4, 0x70F687CF, 0x386C9156, 0xCE092EE5, 0x01E87DA6, 0x6CE91E6A, 0xBB7BCC84, 0xC7922C20, 0x9D3B71FD, 0x060E41C6, 0xD7590F15, 0x4E03BB47, 0x183C198E, 0x63EEB240, 0x2DDBF49A, 0x6D5CBA54, 0x923750AF, 0xF9E14236, 0x7838162B, 0x59726C72, 0x81B66760, 0xBB2926C1, 0x48A0CE0D, 0xA6C0496D, 0xAD43507B, 0x718D496A, 0x9DF057AF, 0x44B1BDE6, 0x054356DC, 0xDE7CED35, 0xD51A138B, 0x62088CC9, 0x35830311, 0xC96EFCA2, 0x686F86EC, 0x8E77CB68, 0x63E1D6B8, 0xC80F9778, 0x79C491FD, 0x1B4C67F2, 0x72698D7D, 0x5E368C31, 0xF7D95E2E, 0xA1D3493F, 0xDCD9433E, 0x896F1552, 0x4BC4CA7A, 0xA6D1BAF4, 0xA5A96DCC, 0x0BEF8B46, 0xA169FDA7, 0x74DF40B7, 0x4E208804, 0x9A756607, 0x038E87C8, 0x20211E44, 0x8B7AD4BF, 0xC6403F35, 0x1848E36D, 0x80BDB038, 0x1E62891C, 0x643D2107, 0xBF04D6F8, 0x21092C8C, 0xF644F389, 0x0778404E, 0x7B78ADB8, 0xA2C52D53, 0x42157ABE, 0xA2253E2E, 0x7BF3F4AE, 0x80F594F9, 0x953194E7, 0x77EB92ED, 0xB3816930, 0xDA8D9336, 0xBF447469, 0xF26D9483, 0xEE6FAED5, 0x71371235, 0xDE425F73, 0xB4E59F43, 0x7DBE2D4E, 0x2D37B185, 0x49DC9A63, 0x98C39D98, 0x1301C9A2, 0x389B1BBF, 0x0C18588D, 0xA421C1BA, 0x7AA3865C, 0x71E08558, 0x3C5CFCAA, 0x7D239CA4, 0x0297D9DD, 0xD7DC2830, 0x4B37802B, 0x7428AB54, 0xAEEE0347, 0x4B3FBB85, 0x692F2F08, 0x134E578E, 0x36D9E0BF, 0xAE8B5FCF, 0xEDB93ECF, 0x2B27248E, 0x170EB1EF, 0x7DC57FD6, 0x1E760F16, 0xB1136601, 0x864E1B9B, 0xD7EA7319, 0x3AB871BD, 0xCFA4D76F, 0xE31BD782, 0x0DBEB469, 0xABB96061, 0x5370F85D, 0xFFB07E37, 0xDA30D0FB, 0xEBC977B6, 0x0B98B40F, 0x3A4D0FE6, 0xDF4FC26B, 0x159CF22A, 0xC298D6E2, 0x2B78EF6A, 0x61A94AC0, 0xAB561187, 0x14EEA0F0, 0xDF0D4164, 0x19AF70EE }; /* * MARS Encryption Round */ inline void encrypt_round(u32bit& A, u32bit& B, u32bit& C, u32bit& D, u32bit EK1, u32bit EK2) { const u32bit X = A + EK1; A = rotate_left(A, 13); u32bit Y = A * EK2; u32bit Z = SBOX[X % 512]; Y = rotate_left(Y, 5); Z ^= Y; C += rotate_left(X, Y % 32); Y = rotate_left(Y, 5); Z ^= Y; D ^= Y; B += rotate_left(Z, Y % 32); } /* * MARS Decryption Round */ inline void decrypt_round(u32bit& A, u32bit& B, u32bit& C, u32bit& D, u32bit EK1, u32bit EK2) { u32bit Y = A * EK1; A = rotate_right(A, 13); const u32bit X = A + EK2; u32bit Z = SBOX[X % 512]; Y = rotate_left(Y, 5); Z ^= Y; C -= rotate_left(X, Y % 32); Y = rotate_left(Y, 5); Z ^= Y; D ^= Y; B -= rotate_left(Z, Y % 32); } /* * MARS Forward Mixing Operation */ void forward_mix(u32bit& A, u32bit& B, u32bit& C, u32bit& D) { for(size_t j = 0; j != 2; ++j) { B ^= SBOX[get_byte(3, A)]; B += SBOX[get_byte(2, A) + 256]; C += SBOX[get_byte(1, A)]; D ^= SBOX[get_byte(0, A) + 256]; A = rotate_right(A, 24) + D; C ^= SBOX[get_byte(3, B)]; C += SBOX[get_byte(2, B) + 256]; D += SBOX[get_byte(1, B)]; A ^= SBOX[get_byte(0, B) + 256]; B = rotate_right(B, 24) + C; D ^= SBOX[get_byte(3, C)]; D += SBOX[get_byte(2, C) + 256]; A += SBOX[get_byte(1, C)]; B ^= SBOX[get_byte(0, C) + 256]; C = rotate_right(C, 24); A ^= SBOX[get_byte(3, D)]; A += SBOX[get_byte(2, D) + 256]; B += SBOX[get_byte(1, D)]; C ^= SBOX[get_byte(0, D) + 256]; D = rotate_right(D, 24); } } /* * MARS Reverse Mixing Operation */ void reverse_mix(u32bit& A, u32bit& B, u32bit& C, u32bit& D) { for(size_t j = 0; j != 2; ++j) { B ^= SBOX[get_byte(3, A) + 256]; C -= SBOX[get_byte(0, A)]; D -= SBOX[get_byte(1, A) + 256]; D ^= SBOX[get_byte(2, A)]; A = rotate_left(A, 24); C ^= SBOX[get_byte(3, B) + 256]; D -= SBOX[get_byte(0, B)]; A -= SBOX[get_byte(1, B) + 256]; A ^= SBOX[get_byte(2, B)]; C -= (B = rotate_left(B, 24)); D ^= SBOX[get_byte(3, C) + 256]; A -= SBOX[get_byte(0, C)]; B -= SBOX[get_byte(1, C) + 256]; B ^= SBOX[get_byte(2, C)]; C = rotate_left(C, 24); D -= A; A ^= SBOX[get_byte(3, D) + 256]; B -= SBOX[get_byte(0, D)]; C -= SBOX[get_byte(1, D) + 256]; C ^= SBOX[get_byte(2, D)]; D = rotate_left(D, 24); } } /* * Generate a mask for runs of bits */ u32bit gen_mask(u32bit input) { u32bit mask = 0; for(u32bit j = 2; j != 31; ++j) { const u32bit region = (input >> (j-1)) & 0x07; if(region == 0x00 || region == 0x07) { const u32bit low = (j < 9) ? 0 : (j - 9); const u32bit high = (j < 23) ? j : 23; for(u32bit k = low; k != high; ++k) { const u32bit value = (input >> k) & 0x3FF; if(value == 0 || value == 0x3FF) { mask |= 1 << j; break; } } } } return mask; } } /* * MARS Encryption */ void MARS::encrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u32bit A = load_le<u32bit>(in, 0) + EK[0]; u32bit B = load_le<u32bit>(in, 1) + EK[1]; u32bit C = load_le<u32bit>(in, 2) + EK[2]; u32bit D = load_le<u32bit>(in, 3) + EK[3]; forward_mix(A, B, C, D); encrypt_round(A, B, C, D, EK[ 4], EK[ 5]); encrypt_round(B, C, D, A, EK[ 6], EK[ 7]); encrypt_round(C, D, A, B, EK[ 8], EK[ 9]); encrypt_round(D, A, B, C, EK[10], EK[11]); encrypt_round(A, B, C, D, EK[12], EK[13]); encrypt_round(B, C, D, A, EK[14], EK[15]); encrypt_round(C, D, A, B, EK[16], EK[17]); encrypt_round(D, A, B, C, EK[18], EK[19]); encrypt_round(A, D, C, B, EK[20], EK[21]); encrypt_round(B, A, D, C, EK[22], EK[23]); encrypt_round(C, B, A, D, EK[24], EK[25]); encrypt_round(D, C, B, A, EK[26], EK[27]); encrypt_round(A, D, C, B, EK[28], EK[29]); encrypt_round(B, A, D, C, EK[30], EK[31]); encrypt_round(C, B, A, D, EK[32], EK[33]); encrypt_round(D, C, B, A, EK[34], EK[35]); reverse_mix(A, B, C, D); A -= EK[36]; B -= EK[37]; C -= EK[38]; D -= EK[39]; store_le(out, A, B, C, D); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * MARS Decryption */ void MARS::decrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u32bit A = load_le<u32bit>(in, 3) + EK[39]; u32bit B = load_le<u32bit>(in, 2) + EK[38]; u32bit C = load_le<u32bit>(in, 1) + EK[37]; u32bit D = load_le<u32bit>(in, 0) + EK[36]; forward_mix(A, B, C, D); decrypt_round(A, B, C, D, EK[35], EK[34]); decrypt_round(B, C, D, A, EK[33], EK[32]); decrypt_round(C, D, A, B, EK[31], EK[30]); decrypt_round(D, A, B, C, EK[29], EK[28]); decrypt_round(A, B, C, D, EK[27], EK[26]); decrypt_round(B, C, D, A, EK[25], EK[24]); decrypt_round(C, D, A, B, EK[23], EK[22]); decrypt_round(D, A, B, C, EK[21], EK[20]); decrypt_round(A, D, C, B, EK[19], EK[18]); decrypt_round(B, A, D, C, EK[17], EK[16]); decrypt_round(C, B, A, D, EK[15], EK[14]); decrypt_round(D, C, B, A, EK[13], EK[12]); decrypt_round(A, D, C, B, EK[11], EK[10]); decrypt_round(B, A, D, C, EK[ 9], EK[ 8]); decrypt_round(C, B, A, D, EK[ 7], EK[ 6]); decrypt_round(D, C, B, A, EK[ 5], EK[ 4]); reverse_mix(A, B, C, D); A -= EK[3]; B -= EK[2]; C -= EK[1]; D -= EK[0]; store_le(out, D, C, B, A); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * MARS Key Schedule */ void MARS::key_schedule(const byte key[], size_t length) { SecureVector<u32bit> T(15); for(size_t i = 0; i != length / 4; ++i) T[i] = load_le<u32bit>(key, i); T[length / 4] = static_cast<u32bit>(length) / 4; for(u32bit i = 0; i != 4; ++i) { T[ 0] ^= rotate_left(T[ 8] ^ T[13], 3) ^ (i ); T[ 1] ^= rotate_left(T[ 9] ^ T[14], 3) ^ (i + 4); T[ 2] ^= rotate_left(T[10] ^ T[ 0], 3) ^ (i + 8); T[ 3] ^= rotate_left(T[11] ^ T[ 1], 3) ^ (i + 12); T[ 4] ^= rotate_left(T[12] ^ T[ 2], 3) ^ (i + 16); T[ 5] ^= rotate_left(T[13] ^ T[ 3], 3) ^ (i + 20); T[ 6] ^= rotate_left(T[14] ^ T[ 4], 3) ^ (i + 24); T[ 7] ^= rotate_left(T[ 0] ^ T[ 5], 3) ^ (i + 28); T[ 8] ^= rotate_left(T[ 1] ^ T[ 6], 3) ^ (i + 32); T[ 9] ^= rotate_left(T[ 2] ^ T[ 7], 3) ^ (i + 36); T[10] ^= rotate_left(T[ 3] ^ T[ 8], 3) ^ (i + 40); T[11] ^= rotate_left(T[ 4] ^ T[ 9], 3) ^ (i + 44); T[12] ^= rotate_left(T[ 5] ^ T[10], 3) ^ (i + 48); T[13] ^= rotate_left(T[ 6] ^ T[11], 3) ^ (i + 52); T[14] ^= rotate_left(T[ 7] ^ T[12], 3) ^ (i + 56); for(size_t j = 0; j != 4; ++j) { T[ 0] = rotate_left(T[ 0] + SBOX[T[14] % 512], 9); T[ 1] = rotate_left(T[ 1] + SBOX[T[ 0] % 512], 9); T[ 2] = rotate_left(T[ 2] + SBOX[T[ 1] % 512], 9); T[ 3] = rotate_left(T[ 3] + SBOX[T[ 2] % 512], 9); T[ 4] = rotate_left(T[ 4] + SBOX[T[ 3] % 512], 9); T[ 5] = rotate_left(T[ 5] + SBOX[T[ 4] % 512], 9); T[ 6] = rotate_left(T[ 6] + SBOX[T[ 5] % 512], 9); T[ 7] = rotate_left(T[ 7] + SBOX[T[ 6] % 512], 9); T[ 8] = rotate_left(T[ 8] + SBOX[T[ 7] % 512], 9); T[ 9] = rotate_left(T[ 9] + SBOX[T[ 8] % 512], 9); T[10] = rotate_left(T[10] + SBOX[T[ 9] % 512], 9); T[11] = rotate_left(T[11] + SBOX[T[10] % 512], 9); T[12] = rotate_left(T[12] + SBOX[T[11] % 512], 9); T[13] = rotate_left(T[13] + SBOX[T[12] % 512], 9); T[14] = rotate_left(T[14] + SBOX[T[13] % 512], 9); } EK[10*i + 0] = T[ 0]; EK[10*i + 1] = T[ 4]; EK[10*i + 2] = T[ 8]; EK[10*i + 3] = T[12]; EK[10*i + 4] = T[ 1]; EK[10*i + 5] = T[ 5]; EK[10*i + 6] = T[ 9]; EK[10*i + 7] = T[13]; EK[10*i + 8] = T[ 2]; EK[10*i + 9] = T[ 6]; } for(size_t i = 5; i != 37; i += 2) { const u32bit key3 = EK[i] & 3; EK[i] |= 3; EK[i] ^= rotate_left(SBOX[265 + key3], EK[i-1] % 32) & gen_mask(EK[i]); } } } /* * MISTY1 * (C) 1999-2009 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { static const byte MISTY1_SBOX_S7[128] = { 0x1B, 0x32, 0x33, 0x5A, 0x3B, 0x10, 0x17, 0x54, 0x5B, 0x1A, 0x72, 0x73, 0x6B, 0x2C, 0x66, 0x49, 0x1F, 0x24, 0x13, 0x6C, 0x37, 0x2E, 0x3F, 0x4A, 0x5D, 0x0F, 0x40, 0x56, 0x25, 0x51, 0x1C, 0x04, 0x0B, 0x46, 0x20, 0x0D, 0x7B, 0x35, 0x44, 0x42, 0x2B, 0x1E, 0x41, 0x14, 0x4B, 0x79, 0x15, 0x6F, 0x0E, 0x55, 0x09, 0x36, 0x74, 0x0C, 0x67, 0x53, 0x28, 0x0A, 0x7E, 0x38, 0x02, 0x07, 0x60, 0x29, 0x19, 0x12, 0x65, 0x2F, 0x30, 0x39, 0x08, 0x68, 0x5F, 0x78, 0x2A, 0x4C, 0x64, 0x45, 0x75, 0x3D, 0x59, 0x48, 0x03, 0x57, 0x7C, 0x4F, 0x62, 0x3C, 0x1D, 0x21, 0x5E, 0x27, 0x6A, 0x70, 0x4D, 0x3A, 0x01, 0x6D, 0x6E, 0x63, 0x18, 0x77, 0x23, 0x05, 0x26, 0x76, 0x00, 0x31, 0x2D, 0x7A, 0x7F, 0x61, 0x50, 0x22, 0x11, 0x06, 0x47, 0x16, 0x52, 0x4E, 0x71, 0x3E, 0x69, 0x43, 0x34, 0x5C, 0x58, 0x7D }; static const u16bit MISTY1_SBOX_S9[512] = { 0x01C3, 0x00CB, 0x0153, 0x019F, 0x01E3, 0x00E9, 0x00FB, 0x0035, 0x0181, 0x00B9, 0x0117, 0x01EB, 0x0133, 0x0009, 0x002D, 0x00D3, 0x00C7, 0x014A, 0x0037, 0x007E, 0x00EB, 0x0164, 0x0193, 0x01D8, 0x00A3, 0x011E, 0x0055, 0x002C, 0x001D, 0x01A2, 0x0163, 0x0118, 0x014B, 0x0152, 0x01D2, 0x000F, 0x002B, 0x0030, 0x013A, 0x00E5, 0x0111, 0x0138, 0x018E, 0x0063, 0x00E3, 0x00C8, 0x01F4, 0x001B, 0x0001, 0x009D, 0x00F8, 0x01A0, 0x016D, 0x01F3, 0x001C, 0x0146, 0x007D, 0x00D1, 0x0082, 0x01EA, 0x0183, 0x012D, 0x00F4, 0x019E, 0x01D3, 0x00DD, 0x01E2, 0x0128, 0x01E0, 0x00EC, 0x0059, 0x0091, 0x0011, 0x012F, 0x0026, 0x00DC, 0x00B0, 0x018C, 0x010F, 0x01F7, 0x00E7, 0x016C, 0x00B6, 0x00F9, 0x00D8, 0x0151, 0x0101, 0x014C, 0x0103, 0x00B8, 0x0154, 0x012B, 0x01AE, 0x0017, 0x0071, 0x000C, 0x0047, 0x0058, 0x007F, 0x01A4, 0x0134, 0x0129, 0x0084, 0x015D, 0x019D, 0x01B2, 0x01A3, 0x0048, 0x007C, 0x0051, 0x01CA, 0x0023, 0x013D, 0x01A7, 0x0165, 0x003B, 0x0042, 0x00DA, 0x0192, 0x00CE, 0x00C1, 0x006B, 0x009F, 0x01F1, 0x012C, 0x0184, 0x00FA, 0x0196, 0x01E1, 0x0169, 0x017D, 0x0031, 0x0180, 0x010A, 0x0094, 0x01DA, 0x0186, 0x013E, 0x011C, 0x0060, 0x0175, 0x01CF, 0x0067, 0x0119, 0x0065, 0x0068, 0x0099, 0x0150, 0x0008, 0x0007, 0x017C, 0x00B7, 0x0024, 0x0019, 0x00DE, 0x0127, 0x00DB, 0x00E4, 0x01A9, 0x0052, 0x0109, 0x0090, 0x019C, 0x01C1, 0x0028, 0x01B3, 0x0135, 0x016A, 0x0176, 0x00DF, 0x01E5, 0x0188, 0x00C5, 0x016E, 0x01DE, 0x01B1, 0x00C3, 0x01DF, 0x0036, 0x00EE, 0x01EE, 0x00F0, 0x0093, 0x0049, 0x009A, 0x01B6, 0x0069, 0x0081, 0x0125, 0x000B, 0x005E, 0x00B4, 0x0149, 0x01C7, 0x0174, 0x003E, 0x013B, 0x01B7, 0x008E, 0x01C6, 0x00AE, 0x0010, 0x0095, 0x01EF, 0x004E, 0x00F2, 0x01FD, 0x0085, 0x00FD, 0x00F6, 0x00A0, 0x016F, 0x0083, 0x008A, 0x0156, 0x009B, 0x013C, 0x0107, 0x0167, 0x0098, 0x01D0, 0x01E9, 0x0003, 0x01FE, 0x00BD, 0x0122, 0x0089, 0x00D2, 0x018F, 0x0012, 0x0033, 0x006A, 0x0142, 0x00ED, 0x0170, 0x011B, 0x00E2, 0x014F, 0x0158, 0x0131, 0x0147, 0x005D, 0x0113, 0x01CD, 0x0079, 0x0161, 0x01A5, 0x0179, 0x009E, 0x01B4, 0x00CC, 0x0022, 0x0132, 0x001A, 0x00E8, 0x0004, 0x0187, 0x01ED, 0x0197, 0x0039, 0x01BF, 0x01D7, 0x0027, 0x018B, 0x00C6, 0x009C, 0x00D0, 0x014E, 0x006C, 0x0034, 0x01F2, 0x006E, 0x00CA, 0x0025, 0x00BA, 0x0191, 0x00FE, 0x0013, 0x0106, 0x002F, 0x01AD, 0x0172, 0x01DB, 0x00C0, 0x010B, 0x01D6, 0x00F5, 0x01EC, 0x010D, 0x0076, 0x0114, 0x01AB, 0x0075, 0x010C, 0x01E4, 0x0159, 0x0054, 0x011F, 0x004B, 0x00C4, 0x01BE, 0x00F7, 0x0029, 0x00A4, 0x000E, 0x01F0, 0x0077, 0x004D, 0x017A, 0x0086, 0x008B, 0x00B3, 0x0171, 0x00BF, 0x010E, 0x0104, 0x0097, 0x015B, 0x0160, 0x0168, 0x00D7, 0x00BB, 0x0066, 0x01CE, 0x00FC, 0x0092, 0x01C5, 0x006F, 0x0016, 0x004A, 0x00A1, 0x0139, 0x00AF, 0x00F1, 0x0190, 0x000A, 0x01AA, 0x0143, 0x017B, 0x0056, 0x018D, 0x0166, 0x00D4, 0x01FB, 0x014D, 0x0194, 0x019A, 0x0087, 0x01F8, 0x0123, 0x00A7, 0x01B8, 0x0141, 0x003C, 0x01F9, 0x0140, 0x002A, 0x0155, 0x011A, 0x01A1, 0x0198, 0x00D5, 0x0126, 0x01AF, 0x0061, 0x012E, 0x0157, 0x01DC, 0x0072, 0x018A, 0x00AA, 0x0096, 0x0115, 0x00EF, 0x0045, 0x007B, 0x008D, 0x0145, 0x0053, 0x005F, 0x0178, 0x00B2, 0x002E, 0x0020, 0x01D5, 0x003F, 0x01C9, 0x01E7, 0x01AC, 0x0044, 0x0038, 0x0014, 0x00B1, 0x016B, 0x00AB, 0x00B5, 0x005A, 0x0182, 0x01C8, 0x01D4, 0x0018, 0x0177, 0x0064, 0x00CF, 0x006D, 0x0100, 0x0199, 0x0130, 0x015A, 0x0005, 0x0120, 0x01BB, 0x01BD, 0x00E0, 0x004F, 0x00D6, 0x013F, 0x01C4, 0x012A, 0x0015, 0x0006, 0x00FF, 0x019B, 0x00A6, 0x0043, 0x0088, 0x0050, 0x015F, 0x01E8, 0x0121, 0x0073, 0x017E, 0x00BC, 0x00C2, 0x00C9, 0x0173, 0x0189, 0x01F5, 0x0074, 0x01CC, 0x01E6, 0x01A8, 0x0195, 0x001F, 0x0041, 0x000D, 0x01BA, 0x0032, 0x003D, 0x01D1, 0x0080, 0x00A8, 0x0057, 0x01B9, 0x0162, 0x0148, 0x00D9, 0x0105, 0x0062, 0x007A, 0x0021, 0x01FF, 0x0112, 0x0108, 0x01C0, 0x00A9, 0x011D, 0x01B0, 0x01A6, 0x00CD, 0x00F3, 0x005C, 0x0102, 0x005B, 0x01D9, 0x0144, 0x01F6, 0x00AD, 0x00A5, 0x003A, 0x01CB, 0x0136, 0x017F, 0x0046, 0x00E1, 0x001E, 0x01DD, 0x00E6, 0x0137, 0x01FA, 0x0185, 0x008C, 0x008F, 0x0040, 0x01B5, 0x00BE, 0x0078, 0x0000, 0x00AC, 0x0110, 0x015E, 0x0124, 0x0002, 0x01BC, 0x00A2, 0x00EA, 0x0070, 0x01FC, 0x0116, 0x015C, 0x004C, 0x01C2 }; /* * MISTY1 FI Function */ u16bit FI(u16bit input, u16bit key7, u16bit key9) { u16bit D9 = input >> 7, D7 = input & 0x7F; D9 = MISTY1_SBOX_S9[D9] ^ D7; D7 = (MISTY1_SBOX_S7[D7] ^ key7 ^ D9) & 0x7F; D9 = MISTY1_SBOX_S9[D9 ^ key9] ^ D7; return static_cast<u16bit>((D7 << 9) | D9); } } /* * MISTY1 Encryption */ void MISTY1::encrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u16bit B0 = load_be<u16bit>(in, 0); u16bit B1 = load_be<u16bit>(in, 1); u16bit B2 = load_be<u16bit>(in, 2); u16bit B3 = load_be<u16bit>(in, 3); for(size_t j = 0; j != 12; j += 3) { const u16bit* RK = &EK[8 * j]; B1 ^= B0 & RK[0]; B0 ^= B1 | RK[1]; B3 ^= B2 & RK[2]; B2 ^= B3 | RK[3]; u32bit T0, T1; T0 = FI(B0 ^ RK[ 4], RK[ 5], RK[ 6]) ^ B1; T1 = FI(B1 ^ RK[ 7], RK[ 8], RK[ 9]) ^ T0; T0 = FI(T0 ^ RK[10], RK[11], RK[12]) ^ T1; B2 ^= T1 ^ RK[13]; B3 ^= T0; T0 = FI(B2 ^ RK[14], RK[15], RK[16]) ^ B3; T1 = FI(B3 ^ RK[17], RK[18], RK[19]) ^ T0; T0 = FI(T0 ^ RK[20], RK[21], RK[22]) ^ T1; B0 ^= T1 ^ RK[23]; B1 ^= T0; } B1 ^= B0 & EK[96]; B0 ^= B1 | EK[97]; B3 ^= B2 & EK[98]; B2 ^= B3 | EK[99]; store_be(out, B2, B3, B0, B1); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * MISTY1 Decryption */ void MISTY1::decrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u16bit B0 = load_be<u16bit>(in, 2); u16bit B1 = load_be<u16bit>(in, 3); u16bit B2 = load_be<u16bit>(in, 0); u16bit B3 = load_be<u16bit>(in, 1); for(size_t j = 0; j != 12; j += 3) { const u16bit* RK = &DK[8 * j]; B2 ^= B3 | RK[0]; B3 ^= B2 & RK[1]; B0 ^= B1 | RK[2]; B1 ^= B0 & RK[3]; u32bit T0, T1; T0 = FI(B2 ^ RK[ 4], RK[ 5], RK[ 6]) ^ B3; T1 = FI(B3 ^ RK[ 7], RK[ 8], RK[ 9]) ^ T0; T0 = FI(T0 ^ RK[10], RK[11], RK[12]) ^ T1; B0 ^= T1 ^ RK[13]; B1 ^= T0; T0 = FI(B0 ^ RK[14], RK[15], RK[16]) ^ B1; T1 = FI(B1 ^ RK[17], RK[18], RK[19]) ^ T0; T0 = FI(T0 ^ RK[20], RK[21], RK[22]) ^ T1; B2 ^= T1 ^ RK[23]; B3 ^= T0; } B2 ^= B3 | DK[96]; B3 ^= B2 & DK[97]; B0 ^= B1 | DK[98]; B1 ^= B0 & DK[99]; store_be(out, B0, B1, B2, B3); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * MISTY1 Key Schedule */ void MISTY1::key_schedule(const byte key[], size_t length) { SecureVector<u16bit> KS(32); for(size_t i = 0; i != length / 2; ++i) KS[i] = load_be<u16bit>(key, i); for(size_t i = 0; i != 8; ++i) { KS[i+ 8] = FI(KS[i], KS[(i+1) % 8] >> 9, KS[(i+1) % 8] & 0x1FF); KS[i+16] = KS[i+8] >> 9; KS[i+24] = KS[i+8] & 0x1FF; } /* * Precomputed indexes for the orderings of the subkeys (MISTY1 reuses * values) */ static const byte EK_ORDER[100] = { 0x00, 0x0E, 0x0A, 0x04, 0x00, 0x15, 0x1D, 0x02, 0x11, 0x19, 0x07, 0x13, 0x1B, 0x04, 0x01, 0x16, 0x1E, 0x03, 0x12, 0x1A, 0x00, 0x14, 0x1C, 0x05, 0x01, 0x0F, 0x0B, 0x05, 0x02, 0x17, 0x1F, 0x04, 0x13, 0x1B, 0x01, 0x15, 0x1D, 0x06, 0x03, 0x10, 0x18, 0x05, 0x14, 0x1C, 0x02, 0x16, 0x1E, 0x07, 0x02, 0x08, 0x0C, 0x06, 0x04, 0x11, 0x19, 0x06, 0x15, 0x1D, 0x03, 0x17, 0x1F, 0x00, 0x05, 0x12, 0x1A, 0x07, 0x16, 0x1E, 0x04, 0x10, 0x18, 0x01, 0x03, 0x09, 0x0D, 0x07, 0x06, 0x13, 0x1B, 0x00, 0x17, 0x1F, 0x05, 0x11, 0x19, 0x02, 0x07, 0x14, 0x1C, 0x01, 0x10, 0x18, 0x06, 0x12, 0x1A, 0x03, 0x04, 0x0A, 0x0E, 0x00 }; static const byte DK_ORDER[100] = { 0x00, 0x0E, 0x0A, 0x04, 0x07, 0x14, 0x1C, 0x01, 0x10, 0x18, 0x06, 0x12, 0x1A, 0x03, 0x06, 0x13, 0x1B, 0x00, 0x17, 0x1F, 0x05, 0x11, 0x19, 0x02, 0x07, 0x0D, 0x09, 0x03, 0x05, 0x12, 0x1A, 0x07, 0x16, 0x1E, 0x04, 0x10, 0x18, 0x01, 0x04, 0x11, 0x19, 0x06, 0x15, 0x1D, 0x03, 0x17, 0x1F, 0x00, 0x06, 0x0C, 0x08, 0x02, 0x03, 0x10, 0x18, 0x05, 0x14, 0x1C, 0x02, 0x16, 0x1E, 0x07, 0x02, 0x17, 0x1F, 0x04, 0x13, 0x1B, 0x01, 0x15, 0x1D, 0x06, 0x05, 0x0B, 0x0F, 0x01, 0x01, 0x16, 0x1E, 0x03, 0x12, 0x1A, 0x00, 0x14, 0x1C, 0x05, 0x00, 0x15, 0x1D, 0x02, 0x11, 0x19, 0x07, 0x13, 0x1B, 0x04, 0x04, 0x0A, 0x0E, 0x00 }; for(size_t i = 0; i != 100; ++i) { EK[i] = KS[EK_ORDER[i]]; DK[i] = KS[DK_ORDER[i]]; } } /* * MISTY1 Constructor */ MISTY1::MISTY1(size_t rounds) : EK(100), DK(100) { if(rounds != 8) throw Invalid_Argument("MISTY1: Invalid number of rounds: " + to_string(rounds)); } } /* * Noekeon * (C) 1999-2008 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { /* * Noekeon's Theta Operation */ inline void theta(u32bit& A0, u32bit& A1, u32bit& A2, u32bit& A3, const u32bit EK[4]) { u32bit T = A0 ^ A2; T ^= rotate_left(T, 8) ^ rotate_right(T, 8); A1 ^= T; A3 ^= T; A0 ^= EK[0]; A1 ^= EK[1]; A2 ^= EK[2]; A3 ^= EK[3]; T = A1 ^ A3; T ^= rotate_left(T, 8) ^ rotate_right(T, 8); A0 ^= T; A2 ^= T; } /* * Theta With Null Key */ inline void theta(u32bit& A0, u32bit& A1, u32bit& A2, u32bit& A3) { u32bit T = A0 ^ A2; T ^= rotate_left(T, 8) ^ rotate_right(T, 8); A1 ^= T; A3 ^= T; T = A1 ^ A3; T ^= rotate_left(T, 8) ^ rotate_right(T, 8); A0 ^= T; A2 ^= T; } /* * Noekeon's Gamma S-Box Layer */ inline void gamma(u32bit& A0, u32bit& A1, u32bit& A2, u32bit& A3) { A1 ^= ~A3 & ~A2; A0 ^= A2 & A1; u32bit T = A3; A3 = A0; A0 = T; A2 ^= A0 ^ A1 ^ A3; A1 ^= ~A3 & ~A2; A0 ^= A2 & A1; } } /* * Noekeon Round Constants */ const byte Noekeon::RC[] = { 0x80, 0x1B, 0x36, 0x6C, 0xD8, 0xAB, 0x4D, 0x9A, 0x2F, 0x5E, 0xBC, 0x63, 0xC6, 0x97, 0x35, 0x6A, 0xD4 }; /* * Noekeon Encryption */ void Noekeon::encrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u32bit A0 = load_be<u32bit>(in, 0); u32bit A1 = load_be<u32bit>(in, 1); u32bit A2 = load_be<u32bit>(in, 2); u32bit A3 = load_be<u32bit>(in, 3); for(size_t j = 0; j != 16; ++j) { A0 ^= RC[j]; theta(A0, A1, A2, A3, &EK[0]); A1 = rotate_left(A1, 1); A2 = rotate_left(A2, 5); A3 = rotate_left(A3, 2); gamma(A0, A1, A2, A3); A1 = rotate_right(A1, 1); A2 = rotate_right(A2, 5); A3 = rotate_right(A3, 2); } A0 ^= RC[16]; theta(A0, A1, A2, A3, &EK[0]); store_be(out, A0, A1, A2, A3); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * Noekeon Encryption */ void Noekeon::decrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u32bit A0 = load_be<u32bit>(in, 0); u32bit A1 = load_be<u32bit>(in, 1); u32bit A2 = load_be<u32bit>(in, 2); u32bit A3 = load_be<u32bit>(in, 3); for(size_t j = 16; j != 0; --j) { theta(A0, A1, A2, A3, &DK[0]); A0 ^= RC[j]; A1 = rotate_left(A1, 1); A2 = rotate_left(A2, 5); A3 = rotate_left(A3, 2); gamma(A0, A1, A2, A3); A1 = rotate_right(A1, 1); A2 = rotate_right(A2, 5); A3 = rotate_right(A3, 2); } theta(A0, A1, A2, A3, &DK[0]); A0 ^= RC[0]; store_be(out, A0, A1, A2, A3); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * Noekeon Key Schedule */ void Noekeon::key_schedule(const byte key[], size_t) { u32bit A0 = load_be<u32bit>(key, 0); u32bit A1 = load_be<u32bit>(key, 1); u32bit A2 = load_be<u32bit>(key, 2); u32bit A3 = load_be<u32bit>(key, 3); for(size_t i = 0; i != 16; ++i) { A0 ^= RC[i]; theta(A0, A1, A2, A3); A1 = rotate_left(A1, 1); A2 = rotate_left(A2, 5); A3 = rotate_left(A3, 2); gamma(A0, A1, A2, A3); A1 = rotate_right(A1, 1); A2 = rotate_right(A2, 5); A3 = rotate_right(A3, 2); } A0 ^= RC[16]; DK[0] = A0; DK[1] = A1; DK[2] = A2; DK[3] = A3; theta(A0, A1, A2, A3); EK[0] = A0; EK[1] = A1; EK[2] = A2; EK[3] = A3; } /* * Clear memory of sensitive data */ void Noekeon::clear() { zeroise(EK); zeroise(DK); } } /* * Noekeon in SIMD * (C) 2010 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Noekeon's Theta Operation */ #define NOK_SIMD_THETA(A0, A1, A2, A3, K0, K1, K2, K3) \ do { \ SIMD_32 T = A0 ^ A2; \ SIMD_32 T_l8 = T; \ SIMD_32 T_r8 = T; \ T_l8.rotate_left(8); \ T_r8.rotate_right(8); \ T ^= T_l8; \ T ^= T_r8; \ A1 ^= T; \ A3 ^= T; \ \ A0 ^= K0; \ A1 ^= K1; \ A2 ^= K2; \ A3 ^= K3; \ \ T = A1 ^ A3; \ T_l8 = T; \ T_r8 = T; \ T_l8.rotate_left(8); \ T_r8.rotate_right(8); \ T ^= T_l8; \ T ^= T_r8; \ A0 ^= T; \ A2 ^= T; \ } while(0) /* * Noekeon's Gamma S-Box Layer */ #define NOK_SIMD_GAMMA(A0, A1, A2, A3) \ do \ { \ A1 ^= A3.andc(~A2); \ A0 ^= A2 & A1; \ \ SIMD_32 T = A3; \ A3 = A0; \ A0 = T; \ \ A2 ^= A0 ^ A1 ^ A3; \ \ A1 ^= A3.andc(~A2); \ A0 ^= A2 & A1; \ } while(0) /* * Noekeon Encryption */ void Noekeon_SIMD::encrypt_n(const byte in[], byte out[], size_t blocks) const { const SecureVector<u32bit>& EK = this->get_EK(); SIMD_32 K0 = SIMD_32(EK[0]); SIMD_32 K1 = SIMD_32(EK[1]); SIMD_32 K2 = SIMD_32(EK[2]); SIMD_32 K3 = SIMD_32(EK[3]); while(blocks >= 4) { SIMD_32 A0 = SIMD_32::load_be(in ); SIMD_32 A1 = SIMD_32::load_be(in + 16); SIMD_32 A2 = SIMD_32::load_be(in + 32); SIMD_32 A3 = SIMD_32::load_be(in + 48); SIMD_32::transpose(A0, A1, A2, A3); for(size_t i = 0; i != 16; ++i) { A0 ^= SIMD_32(RC[i]); NOK_SIMD_THETA(A0, A1, A2, A3, K0, K1, K2, K3); A1.rotate_left(1); A2.rotate_left(5); A3.rotate_left(2); NOK_SIMD_GAMMA(A0, A1, A2, A3); A1.rotate_right(1); A2.rotate_right(5); A3.rotate_right(2); } A0 ^= SIMD_32(RC[16]); NOK_SIMD_THETA(A0, A1, A2, A3, K0, K1, K2, K3); SIMD_32::transpose(A0, A1, A2, A3); A0.store_be(out); A1.store_be(out + 16); A2.store_be(out + 32); A3.store_be(out + 48); in += 64; out += 64; blocks -= 4; } if(blocks) Noekeon::encrypt_n(in, out, blocks); } /* * Noekeon Encryption */ void Noekeon_SIMD::decrypt_n(const byte in[], byte out[], size_t blocks) const { const SecureVector<u32bit>& DK = this->get_DK(); SIMD_32 K0 = SIMD_32(DK[0]); SIMD_32 K1 = SIMD_32(DK[1]); SIMD_32 K2 = SIMD_32(DK[2]); SIMD_32 K3 = SIMD_32(DK[3]); while(blocks >= 4) { SIMD_32 A0 = SIMD_32::load_be(in ); SIMD_32 A1 = SIMD_32::load_be(in + 16); SIMD_32 A2 = SIMD_32::load_be(in + 32); SIMD_32 A3 = SIMD_32::load_be(in + 48); SIMD_32::transpose(A0, A1, A2, A3); for(size_t i = 0; i != 16; ++i) { NOK_SIMD_THETA(A0, A1, A2, A3, K0, K1, K2, K3); A0 ^= SIMD_32(RC[16-i]); A1.rotate_left(1); A2.rotate_left(5); A3.rotate_left(2); NOK_SIMD_GAMMA(A0, A1, A2, A3); A1.rotate_right(1); A2.rotate_right(5); A3.rotate_right(2); } NOK_SIMD_THETA(A0, A1, A2, A3, K0, K1, K2, K3); A0 ^= SIMD_32(RC[0]); SIMD_32::transpose(A0, A1, A2, A3); A0.store_be(out); A1.store_be(out + 16); A2.store_be(out + 32); A3.store_be(out + 48); in += 64; out += 64; blocks -= 4; } if(blocks) Noekeon::decrypt_n(in, out, blocks); } } /* * RC2 * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * RC2 Encryption */ void RC2::encrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u16bit R0 = load_le<u16bit>(in, 0); u16bit R1 = load_le<u16bit>(in, 1); u16bit R2 = load_le<u16bit>(in, 2); u16bit R3 = load_le<u16bit>(in, 3); for(size_t j = 0; j != 16; ++j) { R0 += (R1 & ~R3) + (R2 & R3) + K[4*j]; R0 = rotate_left(R0, 1); R1 += (R2 & ~R0) + (R3 & R0) + K[4*j + 1]; R1 = rotate_left(R1, 2); R2 += (R3 & ~R1) + (R0 & R1) + K[4*j + 2]; R2 = rotate_left(R2, 3); R3 += (R0 & ~R2) + (R1 & R2) + K[4*j + 3]; R3 = rotate_left(R3, 5); if(j == 4 || j == 10) { R0 += K[R3 % 64]; R1 += K[R0 % 64]; R2 += K[R1 % 64]; R3 += K[R2 % 64]; } } store_le(out, R0, R1, R2, R3); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * RC2 Decryption */ void RC2::decrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u16bit R0 = load_le<u16bit>(in, 0); u16bit R1 = load_le<u16bit>(in, 1); u16bit R2 = load_le<u16bit>(in, 2); u16bit R3 = load_le<u16bit>(in, 3); for(size_t j = 0; j != 16; ++j) { R3 = rotate_right(R3, 5); R3 -= (R0 & ~R2) + (R1 & R2) + K[63 - (4*j + 0)]; R2 = rotate_right(R2, 3); R2 -= (R3 & ~R1) + (R0 & R1) + K[63 - (4*j + 1)]; R1 = rotate_right(R1, 2); R1 -= (R2 & ~R0) + (R3 & R0) + K[63 - (4*j + 2)]; R0 = rotate_right(R0, 1); R0 -= (R1 & ~R3) + (R2 & R3) + K[63 - (4*j + 3)]; if(j == 4 || j == 10) { R3 -= K[R2 % 64]; R2 -= K[R1 % 64]; R1 -= K[R0 % 64]; R0 -= K[R3 % 64]; } } store_le(out, R0, R1, R2, R3); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * RC2 Key Schedule */ void RC2::key_schedule(const byte key[], size_t length) { static const byte TABLE[256] = { 0xD9, 0x78, 0xF9, 0xC4, 0x19, 0xDD, 0xB5, 0xED, 0x28, 0xE9, 0xFD, 0x79, 0x4A, 0xA0, 0xD8, 0x9D, 0xC6, 0x7E, 0x37, 0x83, 0x2B, 0x76, 0x53, 0x8E, 0x62, 0x4C, 0x64, 0x88, 0x44, 0x8B, 0xFB, 0xA2, 0x17, 0x9A, 0x59, 0xF5, 0x87, 0xB3, 0x4F, 0x13, 0x61, 0x45, 0x6D, 0x8D, 0x09, 0x81, 0x7D, 0x32, 0xBD, 0x8F, 0x40, 0xEB, 0x86, 0xB7, 0x7B, 0x0B, 0xF0, 0x95, 0x21, 0x22, 0x5C, 0x6B, 0x4E, 0x82, 0x54, 0xD6, 0x65, 0x93, 0xCE, 0x60, 0xB2, 0x1C, 0x73, 0x56, 0xC0, 0x14, 0xA7, 0x8C, 0xF1, 0xDC, 0x12, 0x75, 0xCA, 0x1F, 0x3B, 0xBE, 0xE4, 0xD1, 0x42, 0x3D, 0xD4, 0x30, 0xA3, 0x3C, 0xB6, 0x26, 0x6F, 0xBF, 0x0E, 0xDA, 0x46, 0x69, 0x07, 0x57, 0x27, 0xF2, 0x1D, 0x9B, 0xBC, 0x94, 0x43, 0x03, 0xF8, 0x11, 0xC7, 0xF6, 0x90, 0xEF, 0x3E, 0xE7, 0x06, 0xC3, 0xD5, 0x2F, 0xC8, 0x66, 0x1E, 0xD7, 0x08, 0xE8, 0xEA, 0xDE, 0x80, 0x52, 0xEE, 0xF7, 0x84, 0xAA, 0x72, 0xAC, 0x35, 0x4D, 0x6A, 0x2A, 0x96, 0x1A, 0xD2, 0x71, 0x5A, 0x15, 0x49, 0x74, 0x4B, 0x9F, 0xD0, 0x5E, 0x04, 0x18, 0xA4, 0xEC, 0xC2, 0xE0, 0x41, 0x6E, 0x0F, 0x51, 0xCB, 0xCC, 0x24, 0x91, 0xAF, 0x50, 0xA1, 0xF4, 0x70, 0x39, 0x99, 0x7C, 0x3A, 0x85, 0x23, 0xB8, 0xB4, 0x7A, 0xFC, 0x02, 0x36, 0x5B, 0x25, 0x55, 0x97, 0x31, 0x2D, 0x5D, 0xFA, 0x98, 0xE3, 0x8A, 0x92, 0xAE, 0x05, 0xDF, 0x29, 0x10, 0x67, 0x6C, 0xBA, 0xC9, 0xD3, 0x00, 0xE6, 0xCF, 0xE1, 0x9E, 0xA8, 0x2C, 0x63, 0x16, 0x01, 0x3F, 0x58, 0xE2, 0x89, 0xA9, 0x0D, 0x38, 0x34, 0x1B, 0xAB, 0x33, 0xFF, 0xB0, 0xBB, 0x48, 0x0C, 0x5F, 0xB9, 0xB1, 0xCD, 0x2E, 0xC5, 0xF3, 0xDB, 0x47, 0xE5, 0xA5, 0x9C, 0x77, 0x0A, 0xA6, 0x20, 0x68, 0xFE, 0x7F, 0xC1, 0xAD }; SecureVector<byte> L(128); L.copy(key, length); for(size_t i = length; i != 128; ++i) L[i] = TABLE[(L[i-1] + L[i-length]) % 256]; L[128-length] = TABLE[L[128-length]]; for(s32bit i = 127-length; i >= 0; --i) L[i] = TABLE[L[i+1] ^ L[i+length]]; load_le<u16bit>(&K[0], &L[0], 64); } /* * Return the code of the effective key bits */ byte RC2::EKB_code(size_t ekb) { const byte EKB[256] = { 0xBD, 0x56, 0xEA, 0xF2, 0xA2, 0xF1, 0xAC, 0x2A, 0xB0, 0x93, 0xD1, 0x9C, 0x1B, 0x33, 0xFD, 0xD0, 0x30, 0x04, 0xB6, 0xDC, 0x7D, 0xDF, 0x32, 0x4B, 0xF7, 0xCB, 0x45, 0x9B, 0x31, 0xBB, 0x21, 0x5A, 0x41, 0x9F, 0xE1, 0xD9, 0x4A, 0x4D, 0x9E, 0xDA, 0xA0, 0x68, 0x2C, 0xC3, 0x27, 0x5F, 0x80, 0x36, 0x3E, 0xEE, 0xFB, 0x95, 0x1A, 0xFE, 0xCE, 0xA8, 0x34, 0xA9, 0x13, 0xF0, 0xA6, 0x3F, 0xD8, 0x0C, 0x78, 0x24, 0xAF, 0x23, 0x52, 0xC1, 0x67, 0x17, 0xF5, 0x66, 0x90, 0xE7, 0xE8, 0x07, 0xB8, 0x60, 0x48, 0xE6, 0x1E, 0x53, 0xF3, 0x92, 0xA4, 0x72, 0x8C, 0x08, 0x15, 0x6E, 0x86, 0x00, 0x84, 0xFA, 0xF4, 0x7F, 0x8A, 0x42, 0x19, 0xF6, 0xDB, 0xCD, 0x14, 0x8D, 0x50, 0x12, 0xBA, 0x3C, 0x06, 0x4E, 0xEC, 0xB3, 0x35, 0x11, 0xA1, 0x88, 0x8E, 0x2B, 0x94, 0x99, 0xB7, 0x71, 0x74, 0xD3, 0xE4, 0xBF, 0x3A, 0xDE, 0x96, 0x0E, 0xBC, 0x0A, 0xED, 0x77, 0xFC, 0x37, 0x6B, 0x03, 0x79, 0x89, 0x62, 0xC6, 0xD7, 0xC0, 0xD2, 0x7C, 0x6A, 0x8B, 0x22, 0xA3, 0x5B, 0x05, 0x5D, 0x02, 0x75, 0xD5, 0x61, 0xE3, 0x18, 0x8F, 0x55, 0x51, 0xAD, 0x1F, 0x0B, 0x5E, 0x85, 0xE5, 0xC2, 0x57, 0x63, 0xCA, 0x3D, 0x6C, 0xB4, 0xC5, 0xCC, 0x70, 0xB2, 0x91, 0x59, 0x0D, 0x47, 0x20, 0xC8, 0x4F, 0x58, 0xE0, 0x01, 0xE2, 0x16, 0x38, 0xC4, 0x6F, 0x3B, 0x0F, 0x65, 0x46, 0xBE, 0x7E, 0x2D, 0x7B, 0x82, 0xF9, 0x40, 0xB5, 0x1D, 0x73, 0xF8, 0xEB, 0x26, 0xC7, 0x87, 0x97, 0x25, 0x54, 0xB1, 0x28, 0xAA, 0x98, 0x9D, 0xA5, 0x64, 0x6D, 0x7A, 0xD4, 0x10, 0x81, 0x44, 0xEF, 0x49, 0xD6, 0xAE, 0x2E, 0xDD, 0x76, 0x5C, 0x2F, 0xA7, 0x1C, 0xC9, 0x09, 0x69, 0x9A, 0x83, 0xCF, 0x29, 0x39, 0xB9, 0xE9, 0x4C, 0xFF, 0x43, 0xAB }; if(ekb < 256) return EKB[ekb]; else throw Encoding_Error("RC2::EKB_code: EKB is too large"); } } /* * RC5 * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> namespace Botan { /* * RC5 Encryption */ void RC5::encrypt_n(const byte in[], byte out[], size_t blocks) const { const size_t rounds = (S.size() - 2) / 2; for(size_t i = 0; i != blocks; ++i) { u32bit A = load_le<u32bit>(in, 0); u32bit B = load_le<u32bit>(in, 1); A += S[0]; B += S[1]; for(size_t j = 0; j != rounds; j += 4) { A = rotate_left(A ^ B, B % 32) + S[2*j+2]; B = rotate_left(B ^ A, A % 32) + S[2*j+3]; A = rotate_left(A ^ B, B % 32) + S[2*j+4]; B = rotate_left(B ^ A, A % 32) + S[2*j+5]; A = rotate_left(A ^ B, B % 32) + S[2*j+6]; B = rotate_left(B ^ A, A % 32) + S[2*j+7]; A = rotate_left(A ^ B, B % 32) + S[2*j+8]; B = rotate_left(B ^ A, A % 32) + S[2*j+9]; } store_le(out, A, B); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * RC5 Decryption */ void RC5::decrypt_n(const byte in[], byte out[], size_t blocks) const { const size_t rounds = (S.size() - 2) / 2; for(size_t i = 0; i != blocks; ++i) { u32bit A = load_le<u32bit>(in, 0); u32bit B = load_le<u32bit>(in, 1); for(size_t j = rounds; j != 0; j -= 4) { B = rotate_right(B - S[2*j+1], A % 32) ^ A; A = rotate_right(A - S[2*j ], B % 32) ^ B; B = rotate_right(B - S[2*j-1], A % 32) ^ A; A = rotate_right(A - S[2*j-2], B % 32) ^ B; B = rotate_right(B - S[2*j-3], A % 32) ^ A; A = rotate_right(A - S[2*j-4], B % 32) ^ B; B = rotate_right(B - S[2*j-5], A % 32) ^ A; A = rotate_right(A - S[2*j-6], B % 32) ^ B; } B -= S[1]; A -= S[0]; store_le(out, A, B); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * RC5 Key Schedule */ void RC5::key_schedule(const byte key[], size_t length) { const size_t WORD_KEYLENGTH = (((length - 1) / 4) + 1); const size_t MIX_ROUNDS = 3 * std::max(WORD_KEYLENGTH, S.size()); S[0] = 0xB7E15163; for(size_t i = 1; i != S.size(); ++i) S[i] = S[i-1] + 0x9E3779B9; SecureVector<u32bit> K(8); for(s32bit i = length-1; i >= 0; --i) K[i/4] = (K[i/4] << 8) + key[i]; u32bit A = 0, B = 0; for(size_t i = 0; i != MIX_ROUNDS; ++i) { A = rotate_left(S[i % S.size()] + A + B, 3); B = rotate_left(K[i % WORD_KEYLENGTH] + A + B, (A + B) % 32); S[i % S.size()] = A; K[i % WORD_KEYLENGTH] = B; } } /* * Return the name of this type */ std::string RC5::name() const { return "RC5(" + to_string(get_rounds()) + ")"; } /* * RC5 Constructor */ RC5::RC5(size_t rounds) { if(rounds < 8 || rounds > 32 || (rounds % 4 != 0)) throw Invalid_Argument("RC5: Invalid number of rounds " + to_string(rounds)); S.resize(2*rounds + 2); } } /* * RC6 * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> namespace Botan { /* * RC6 Encryption */ void RC6::encrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u32bit A = load_le<u32bit>(in, 0); u32bit B = load_le<u32bit>(in, 1); u32bit C = load_le<u32bit>(in, 2); u32bit D = load_le<u32bit>(in, 3); B += S[0]; D += S[1]; for(size_t j = 0; j != 20; j += 4) { u32bit T1, T2; T1 = rotate_left(B*(2*B+1), 5); T2 = rotate_left(D*(2*D+1), 5); A = rotate_left(A ^ T1, T2 % 32) + S[2*j+2]; C = rotate_left(C ^ T2, T1 % 32) + S[2*j+3]; T1 = rotate_left(C*(2*C+1), 5); T2 = rotate_left(A*(2*A+1), 5); B = rotate_left(B ^ T1, T2 % 32) + S[2*j+4]; D = rotate_left(D ^ T2, T1 % 32) + S[2*j+5]; T1 = rotate_left(D*(2*D+1), 5); T2 = rotate_left(B*(2*B+1), 5); C = rotate_left(C ^ T1, T2 % 32) + S[2*j+6]; A = rotate_left(A ^ T2, T1 % 32) + S[2*j+7]; T1 = rotate_left(A*(2*A+1), 5); T2 = rotate_left(C*(2*C+1), 5); D = rotate_left(D ^ T1, T2 % 32) + S[2*j+8]; B = rotate_left(B ^ T2, T1 % 32) + S[2*j+9]; } A += S[42]; C += S[43]; store_le(out, A, B, C, D); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * RC6 Decryption */ void RC6::decrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u32bit A = load_le<u32bit>(in, 0); u32bit B = load_le<u32bit>(in, 1); u32bit C = load_le<u32bit>(in, 2); u32bit D = load_le<u32bit>(in, 3); C -= S[43]; A -= S[42]; for(size_t j = 0; j != 20; j += 4) { u32bit T1, T2; T1 = rotate_left(A*(2*A+1), 5); T2 = rotate_left(C*(2*C+1), 5); B = rotate_right(B - S[41 - 2*j], T1 % 32) ^ T2; D = rotate_right(D - S[40 - 2*j], T2 % 32) ^ T1; T1 = rotate_left(D*(2*D+1), 5); T2 = rotate_left(B*(2*B+1), 5); A = rotate_right(A - S[39 - 2*j], T1 % 32) ^ T2; C = rotate_right(C - S[38 - 2*j], T2 % 32) ^ T1; T1 = rotate_left(C*(2*C+1), 5); T2 = rotate_left(A*(2*A+1), 5); D = rotate_right(D - S[37 - 2*j], T1 % 32) ^ T2; B = rotate_right(B - S[36 - 2*j], T2 % 32) ^ T1; T1 = rotate_left(B*(2*B+1), 5); T2 = rotate_left(D*(2*D+1), 5); C = rotate_right(C - S[35 - 2*j], T1 % 32) ^ T2; A = rotate_right(A - S[34 - 2*j], T2 % 32) ^ T1; } D -= S[1]; B -= S[0]; store_le(out, A, B, C, D); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * RC6 Key Schedule */ void RC6::key_schedule(const byte key[], size_t length) { const size_t WORD_KEYLENGTH = (((length - 1) / 4) + 1); const size_t MIX_ROUNDS = 3 * std::max(WORD_KEYLENGTH, S.size()); S[0] = 0xB7E15163; for(size_t i = 1; i != S.size(); ++i) S[i] = S[i-1] + 0x9E3779B9; SecureVector<u32bit> K(8); for(s32bit i = length-1; i >= 0; --i) K[i/4] = (K[i/4] << 8) + key[i]; u32bit A = 0, B = 0; for(size_t i = 0; i != MIX_ROUNDS; ++i) { A = rotate_left(S[i % S.size()] + A + B, 3); B = rotate_left(K[i % WORD_KEYLENGTH] + A + B, (A + B) % 32); S[i % S.size()] = A; K[i % WORD_KEYLENGTH] = B; } } } /* * SAFER-SK * (C) 1999-2009 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { const byte EXP[256] = { 0x01, 0x2D, 0xE2, 0x93, 0xBE, 0x45, 0x15, 0xAE, 0x78, 0x03, 0x87, 0xA4, 0xB8, 0x38, 0xCF, 0x3F, 0x08, 0x67, 0x09, 0x94, 0xEB, 0x26, 0xA8, 0x6B, 0xBD, 0x18, 0x34, 0x1B, 0xBB, 0xBF, 0x72, 0xF7, 0x40, 0x35, 0x48, 0x9C, 0x51, 0x2F, 0x3B, 0x55, 0xE3, 0xC0, 0x9F, 0xD8, 0xD3, 0xF3, 0x8D, 0xB1, 0xFF, 0xA7, 0x3E, 0xDC, 0x86, 0x77, 0xD7, 0xA6, 0x11, 0xFB, 0xF4, 0xBA, 0x92, 0x91, 0x64, 0x83, 0xF1, 0x33, 0xEF, 0xDA, 0x2C, 0xB5, 0xB2, 0x2B, 0x88, 0xD1, 0x99, 0xCB, 0x8C, 0x84, 0x1D, 0x14, 0x81, 0x97, 0x71, 0xCA, 0x5F, 0xA3, 0x8B, 0x57, 0x3C, 0x82, 0xC4, 0x52, 0x5C, 0x1C, 0xE8, 0xA0, 0x04, 0xB4, 0x85, 0x4A, 0xF6, 0x13, 0x54, 0xB6, 0xDF, 0x0C, 0x1A, 0x8E, 0xDE, 0xE0, 0x39, 0xFC, 0x20, 0x9B, 0x24, 0x4E, 0xA9, 0x98, 0x9E, 0xAB, 0xF2, 0x60, 0xD0, 0x6C, 0xEA, 0xFA, 0xC7, 0xD9, 0x00, 0xD4, 0x1F, 0x6E, 0x43, 0xBC, 0xEC, 0x53, 0x89, 0xFE, 0x7A, 0x5D, 0x49, 0xC9, 0x32, 0xC2, 0xF9, 0x9A, 0xF8, 0x6D, 0x16, 0xDB, 0x59, 0x96, 0x44, 0xE9, 0xCD, 0xE6, 0x46, 0x42, 0x8F, 0x0A, 0xC1, 0xCC, 0xB9, 0x65, 0xB0, 0xD2, 0xC6, 0xAC, 0x1E, 0x41, 0x62, 0x29, 0x2E, 0x0E, 0x74, 0x50, 0x02, 0x5A, 0xC3, 0x25, 0x7B, 0x8A, 0x2A, 0x5B, 0xF0, 0x06, 0x0D, 0x47, 0x6F, 0x70, 0x9D, 0x7E, 0x10, 0xCE, 0x12, 0x27, 0xD5, 0x4C, 0x4F, 0xD6, 0x79, 0x30, 0x68, 0x36, 0x75, 0x7D, 0xE4, 0xED, 0x80, 0x6A, 0x90, 0x37, 0xA2, 0x5E, 0x76, 0xAA, 0xC5, 0x7F, 0x3D, 0xAF, 0xA5, 0xE5, 0x19, 0x61, 0xFD, 0x4D, 0x7C, 0xB7, 0x0B, 0xEE, 0xAD, 0x4B, 0x22, 0xF5, 0xE7, 0x73, 0x23, 0x21, 0xC8, 0x05, 0xE1, 0x66, 0xDD, 0xB3, 0x58, 0x69, 0x63, 0x56, 0x0F, 0xA1, 0x31, 0x95, 0x17, 0x07, 0x3A, 0x28 }; const byte LOG[512] = { 0x80, 0x00, 0xB0, 0x09, 0x60, 0xEF, 0xB9, 0xFD, 0x10, 0x12, 0x9F, 0xE4, 0x69, 0xBA, 0xAD, 0xF8, 0xC0, 0x38, 0xC2, 0x65, 0x4F, 0x06, 0x94, 0xFC, 0x19, 0xDE, 0x6A, 0x1B, 0x5D, 0x4E, 0xA8, 0x82, 0x70, 0xED, 0xE8, 0xEC, 0x72, 0xB3, 0x15, 0xC3, 0xFF, 0xAB, 0xB6, 0x47, 0x44, 0x01, 0xAC, 0x25, 0xC9, 0xFA, 0x8E, 0x41, 0x1A, 0x21, 0xCB, 0xD3, 0x0D, 0x6E, 0xFE, 0x26, 0x58, 0xDA, 0x32, 0x0F, 0x20, 0xA9, 0x9D, 0x84, 0x98, 0x05, 0x9C, 0xBB, 0x22, 0x8C, 0x63, 0xE7, 0xC5, 0xE1, 0x73, 0xC6, 0xAF, 0x24, 0x5B, 0x87, 0x66, 0x27, 0xF7, 0x57, 0xF4, 0x96, 0xB1, 0xB7, 0x5C, 0x8B, 0xD5, 0x54, 0x79, 0xDF, 0xAA, 0xF6, 0x3E, 0xA3, 0xF1, 0x11, 0xCA, 0xF5, 0xD1, 0x17, 0x7B, 0x93, 0x83, 0xBC, 0xBD, 0x52, 0x1E, 0xEB, 0xAE, 0xCC, 0xD6, 0x35, 0x08, 0xC8, 0x8A, 0xB4, 0xE2, 0xCD, 0xBF, 0xD9, 0xD0, 0x50, 0x59, 0x3F, 0x4D, 0x62, 0x34, 0x0A, 0x48, 0x88, 0xB5, 0x56, 0x4C, 0x2E, 0x6B, 0x9E, 0xD2, 0x3D, 0x3C, 0x03, 0x13, 0xFB, 0x97, 0x51, 0x75, 0x4A, 0x91, 0x71, 0x23, 0xBE, 0x76, 0x2A, 0x5F, 0xF9, 0xD4, 0x55, 0x0B, 0xDC, 0x37, 0x31, 0x16, 0x74, 0xD7, 0x77, 0xA7, 0xE6, 0x07, 0xDB, 0xA4, 0x2F, 0x46, 0xF3, 0x61, 0x45, 0x67, 0xE3, 0x0C, 0xA2, 0x3B, 0x1C, 0x85, 0x18, 0x04, 0x1D, 0x29, 0xA0, 0x8F, 0xB2, 0x5A, 0xD8, 0xA6, 0x7E, 0xEE, 0x8D, 0x53, 0x4B, 0xA1, 0x9A, 0xC1, 0x0E, 0x7A, 0x49, 0xA5, 0x2C, 0x81, 0xC4, 0xC7, 0x36, 0x2B, 0x7F, 0x43, 0x95, 0x33, 0xF2, 0x6C, 0x68, 0x6D, 0xF0, 0x02, 0x28, 0xCE, 0xDD, 0x9B, 0xEA, 0x5E, 0x99, 0x7C, 0x14, 0x86, 0xCF, 0xE5, 0x42, 0xB8, 0x40, 0x78, 0x2D, 0x3A, 0xE9, 0x64, 0x1F, 0x92, 0x90, 0x7D, 0x39, 0x6F, 0xE0, 0x89, 0x30, 0x80, 0x00, 0xB0, 0x09, 0x60, 0xEF, 0xB9, 0xFD, 0x10, 0x12, 0x9F, 0xE4, 0x69, 0xBA, 0xAD, 0xF8, 0xC0, 0x38, 0xC2, 0x65, 0x4F, 0x06, 0x94, 0xFC, 0x19, 0xDE, 0x6A, 0x1B, 0x5D, 0x4E, 0xA8, 0x82, 0x70, 0xED, 0xE8, 0xEC, 0x72, 0xB3, 0x15, 0xC3, 0xFF, 0xAB, 0xB6, 0x47, 0x44, 0x01, 0xAC, 0x25, 0xC9, 0xFA, 0x8E, 0x41, 0x1A, 0x21, 0xCB, 0xD3, 0x0D, 0x6E, 0xFE, 0x26, 0x58, 0xDA, 0x32, 0x0F, 0x20, 0xA9, 0x9D, 0x84, 0x98, 0x05, 0x9C, 0xBB, 0x22, 0x8C, 0x63, 0xE7, 0xC5, 0xE1, 0x73, 0xC6, 0xAF, 0x24, 0x5B, 0x87, 0x66, 0x27, 0xF7, 0x57, 0xF4, 0x96, 0xB1, 0xB7, 0x5C, 0x8B, 0xD5, 0x54, 0x79, 0xDF, 0xAA, 0xF6, 0x3E, 0xA3, 0xF1, 0x11, 0xCA, 0xF5, 0xD1, 0x17, 0x7B, 0x93, 0x83, 0xBC, 0xBD, 0x52, 0x1E, 0xEB, 0xAE, 0xCC, 0xD6, 0x35, 0x08, 0xC8, 0x8A, 0xB4, 0xE2, 0xCD, 0xBF, 0xD9, 0xD0, 0x50, 0x59, 0x3F, 0x4D, 0x62, 0x34, 0x0A, 0x48, 0x88, 0xB5, 0x56, 0x4C, 0x2E, 0x6B, 0x9E, 0xD2, 0x3D, 0x3C, 0x03, 0x13, 0xFB, 0x97, 0x51, 0x75, 0x4A, 0x91, 0x71, 0x23, 0xBE, 0x76, 0x2A, 0x5F, 0xF9, 0xD4, 0x55, 0x0B, 0xDC, 0x37, 0x31, 0x16, 0x74, 0xD7, 0x77, 0xA7, 0xE6, 0x07, 0xDB, 0xA4, 0x2F, 0x46, 0xF3, 0x61, 0x45, 0x67, 0xE3, 0x0C, 0xA2, 0x3B, 0x1C, 0x85, 0x18, 0x04, 0x1D, 0x29, 0xA0, 0x8F, 0xB2, 0x5A, 0xD8, 0xA6, 0x7E, 0xEE, 0x8D, 0x53, 0x4B, 0xA1, 0x9A, 0xC1, 0x0E, 0x7A, 0x49, 0xA5, 0x2C, 0x81, 0xC4, 0xC7, 0x36, 0x2B, 0x7F, 0x43, 0x95, 0x33, 0xF2, 0x6C, 0x68, 0x6D, 0xF0, 0x02, 0x28, 0xCE, 0xDD, 0x9B, 0xEA, 0x5E, 0x99, 0x7C, 0x14, 0x86, 0xCF, 0xE5, 0x42, 0xB8, 0x40, 0x78, 0x2D, 0x3A, 0xE9, 0x64, 0x1F, 0x92, 0x90, 0x7D, 0x39, 0x6F, 0xE0, 0x89, 0x30 }; } /* * SAFER-SK Encryption */ void SAFER_SK::encrypt_n(const byte in[], byte out[], size_t blocks) const { const size_t rounds = get_rounds(); for(size_t i = 0; i != blocks; ++i) { byte A = in[0], B = in[1], C = in[2], D = in[3], E = in[4], F = in[5], G = in[6], H = in[7], X, Y; for(size_t j = 0; j != 16*rounds; j += 16) { A = EXP[A ^ EK[j ]]; B = LOG[B + EK[j+1]]; C = LOG[C + EK[j+2]]; D = EXP[D ^ EK[j+3]]; E = EXP[E ^ EK[j+4]]; F = LOG[F + EK[j+5]]; G = LOG[G + EK[j+6]]; H = EXP[H ^ EK[j+7]]; A += EK[j+ 8]; B ^= EK[j+ 9]; C ^= EK[j+10]; D += EK[j+11]; E += EK[j+12]; F ^= EK[j+13]; G ^= EK[j+14]; H += EK[j+15]; B += A; D += C; F += E; H += G; A += B; C += D; E += F; G += H; C += A; G += E; D += B; H += F; A += C; E += G; B += D; F += H; H += D; Y = D + H; D = B + F; X = B + D; B = A + E; A += B; F = C + G; E = C + F; C = X; G = Y; } out[0] = A ^ EK[16*rounds+0]; out[1] = B + EK[16*rounds+1]; out[2] = C + EK[16*rounds+2]; out[3] = D ^ EK[16*rounds+3]; out[4] = E ^ EK[16*rounds+4]; out[5] = F + EK[16*rounds+5]; out[6] = G + EK[16*rounds+6]; out[7] = H ^ EK[16*rounds+7]; in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * SAFER-SK Decryption */ void SAFER_SK::decrypt_n(const byte in[], byte out[], size_t blocks) const { const size_t rounds = get_rounds(); for(size_t i = 0; i != blocks; ++i) { byte A = in[0], B = in[1], C = in[2], D = in[3], E = in[4], F = in[5], G = in[6], H = in[7]; A ^= EK[16*rounds+0]; B -= EK[16*rounds+1]; C -= EK[16*rounds+2]; D ^= EK[16*rounds+3]; E ^= EK[16*rounds+4]; F -= EK[16*rounds+5]; G -= EK[16*rounds+6]; H ^= EK[16*rounds+7]; for(s32bit j = 16*(rounds-1); j >= 0; j -= 16) { byte T = E; E = B; B = C; C = T; T = F; F = D; D = G; G = T; A -= E; B -= F; C -= G; D -= H; E -= A; F -= B; G -= C; H -= D; A -= C; E -= G; B -= D; F -= H; C -= A; G -= E; D -= B; H -= F; A -= B; C -= D; E -= F; G -= H; B -= A; D -= C; F -= E; H -= G; A = LOG[A - EK[j+8 ] + 256]; B = EXP[B ^ EK[j+9 ]]; C = EXP[C ^ EK[j+10]]; D = LOG[D - EK[j+11] + 256]; E = LOG[E - EK[j+12] + 256]; F = EXP[F ^ EK[j+13]]; G = EXP[G ^ EK[j+14]]; H = LOG[H - EK[j+15] + 256]; A ^= EK[j+0]; B -= EK[j+1]; C -= EK[j+2]; D ^= EK[j+3]; E ^= EK[j+4]; F -= EK[j+5]; G -= EK[j+6]; H ^= EK[j+7]; } out[0] = A; out[1] = B; out[2] = C; out[3] = D; out[4] = E; out[5] = F; out[6] = G; out[7] = H; in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * SAFER-SK Key Schedule */ void SAFER_SK::key_schedule(const byte key[], size_t) { const byte BIAS[208] = { 0x16, 0x73, 0x3B, 0x1E, 0x8E, 0x70, 0xBD, 0x86, 0x47, 0x7E, 0x24, 0x56, 0xF1, 0x77, 0x88, 0x46, 0xB1, 0xBA, 0xA3, 0xB7, 0x10, 0x0A, 0xC5, 0x37, 0xC9, 0x5A, 0x28, 0xAC, 0x64, 0xA5, 0xEC, 0xAB, 0xC6, 0x67, 0x95, 0x58, 0x0D, 0xF8, 0x9A, 0xF6, 0x66, 0xDC, 0x05, 0x3D, 0xD3, 0x8A, 0xC3, 0xD8, 0x6A, 0xE9, 0x36, 0x49, 0x43, 0xBF, 0xEB, 0xD4, 0x9B, 0x68, 0xA0, 0x65, 0x5D, 0x57, 0x92, 0x1F, 0x71, 0x5C, 0xBB, 0x22, 0xC1, 0xBE, 0x7B, 0xBC, 0x63, 0x94, 0x5F, 0x2A, 0x61, 0xB8, 0x34, 0x32, 0xFD, 0xFB, 0x17, 0x40, 0xE6, 0x51, 0x1D, 0x41, 0x8F, 0x29, 0xDD, 0x04, 0x80, 0xDE, 0xE7, 0x31, 0x7F, 0x01, 0xA2, 0xF7, 0x39, 0xDA, 0x6F, 0x23, 0xFE, 0x3A, 0xD0, 0x1C, 0xD1, 0x30, 0x3E, 0x12, 0xCD, 0x0F, 0xE0, 0xA8, 0xAF, 0x82, 0x59, 0x2C, 0x7D, 0xAD, 0xB2, 0xEF, 0xC2, 0x87, 0xCE, 0x75, 0x13, 0x02, 0x90, 0x4F, 0x2E, 0x72, 0x33, 0x85, 0x8D, 0xCF, 0xA9, 0x81, 0xE2, 0xC4, 0x27, 0x2F, 0x7A, 0x9F, 0x52, 0xE1, 0x15, 0x38, 0x2B, 0xFC, 0x42, 0xC7, 0x08, 0xE4, 0x09, 0x55, 0x5E, 0x8C, 0x76, 0x60, 0xFF, 0xDF, 0xD7, 0x98, 0xFA, 0x0B, 0x00, 0x1A, 0xF9, 0xA6, 0xB9, 0xE8, 0x9E, 0x62, 0xD9, 0x91, 0x50, 0xD2, 0xEE, 0x18, 0xB4, 0x07, 0xEA, 0x5B, 0xA4, 0xC8, 0x0E, 0xCB, 0x48, 0x69, 0x4E, 0x9C, 0x35, 0x79, 0x45, 0x4D, 0x54, 0xE5, 0x3C, 0x0C, 0x4A, 0x8B, 0x3F, 0xCC, 0xA7, 0xDB }; const byte KEY_INDEX[208] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x09, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x01, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x09, 0x0A, 0x0B, 0x05, 0x06, 0x07, 0x08, 0x00, 0x01, 0x02, 0x03, 0x0F, 0x10, 0x11, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x07, 0x08, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x11, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x09, 0x0A, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x01, 0x02, 0x0E, 0x0F, 0x10, 0x11, 0x09, 0x0A, 0x0B, 0x0C, 0x06, 0x07, 0x08, 0x00, 0x01, 0x02, 0x03, 0x04, 0x10, 0x11, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x08, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x09, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x01, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x09, 0x0A, 0x0B, 0x05, 0x06, 0x07, 0x08, 0x00, 0x01, 0x02, 0x03, 0x0F, 0x10, 0x11, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x07, 0x08, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x11, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }; SecureVector<byte> KB(18); for(size_t i = 0; i != 8; ++i) { KB[ 8] ^= KB[i] = rotate_left(key[i], 5); KB[17] ^= KB[i+9] = EK[i] = key[i+8]; } for(size_t i = 0; i != get_rounds(); ++i) { for(size_t j = 0; j != 18; ++j) KB[j] = rotate_left(KB[j], 6); for(size_t j = 0; j != 16; ++j) EK[16*i+j+8] = KB[KEY_INDEX[16*i+j]] + BIAS[16*i+j]; } } /* * Return the name of this type */ std::string SAFER_SK::name() const { return "SAFER-SK(" + to_string(get_rounds()) + ")"; } /* * Return a clone of this object */ BlockCipher* SAFER_SK::clone() const { return new SAFER_SK(get_rounds()); } /* * SAFER-SK Constructor */ SAFER_SK::SAFER_SK(size_t rounds) { if(rounds > 13 || rounds == 0) throw Invalid_Argument(name() + ": Invalid number of rounds"); EK.resize(16 * rounds + 8); } } /* * SEED * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * SEED G Function */ u32bit SEED::G_FUNC::operator()(u32bit X) const { return (S0[get_byte(3, X)] ^ S1[get_byte(2, X)] ^ S2[get_byte(1, X)] ^ S3[get_byte(0, X)]); } /* * SEED Encryption */ void SEED::encrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u32bit B0 = load_be<u32bit>(in, 0); u32bit B1 = load_be<u32bit>(in, 1); u32bit B2 = load_be<u32bit>(in, 2); u32bit B3 = load_be<u32bit>(in, 3); G_FUNC G; for(size_t j = 0; j != 16; j += 2) { u32bit T0, T1; T0 = B2 ^ K[2*j]; T1 = G(B2 ^ B3 ^ K[2*j+1]); T0 = G(T1 + T0); T1 = G(T1 + T0); B1 ^= T1; B0 ^= T0 + T1; T0 = B0 ^ K[2*j+2]; T1 = G(B0 ^ B1 ^ K[2*j+3]); T0 = G(T1 + T0); T1 = G(T1 + T0); B3 ^= T1; B2 ^= T0 + T1; } store_be(out, B2, B3, B0, B1); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * SEED Decryption */ void SEED::decrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u32bit B0 = load_be<u32bit>(in, 0); u32bit B1 = load_be<u32bit>(in, 1); u32bit B2 = load_be<u32bit>(in, 2); u32bit B3 = load_be<u32bit>(in, 3); G_FUNC G; for(size_t j = 0; j != 16; j += 2) { u32bit T0, T1; T0 = B2 ^ K[30-2*j]; T1 = G(B2 ^ B3 ^ K[31-2*j]); T0 = G(T1 + T0); T1 = G(T1 + T0); B1 ^= T1; B0 ^= T0 + T1; T0 = B0 ^ K[28-2*j]; T1 = G(B0 ^ B1 ^ K[29-2*j]); T0 = G(T1 + T0); T1 = G(T1 + T0); B3 ^= T1; B2 ^= T0 + T1; } store_be(out, B2, B3, B0, B1); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * SEED Key Schedule */ void SEED::key_schedule(const byte key[], size_t) { const u32bit RC[16] = { 0x9E3779B9, 0x3C6EF373, 0x78DDE6E6, 0xF1BBCDCC, 0xE3779B99, 0xC6EF3733, 0x8DDE6E67, 0x1BBCDCCF, 0x3779B99E, 0x6EF3733C, 0xDDE6E678, 0xBBCDCCF1, 0x779B99E3, 0xEF3733C6, 0xDE6E678D, 0xBCDCCF1B }; SecureVector<u32bit> WK(4); for(size_t i = 0; i != 4; ++i) WK[i] = load_be<u32bit>(key, i); G_FUNC G; for(size_t i = 0; i != 16; i += 2) { K[2*i ] = G(WK[0] + WK[2] - RC[i]); K[2*i+1] = G(WK[1] - WK[3] + RC[i]) ^ K[2*i]; byte T = get_byte(3, WK[0]); WK[0] = (WK[0] >> 8) | (get_byte(3, WK[1]) << 24); WK[1] = (WK[1] >> 8) | (T << 24); K[2*i+2] = G(WK[0] + WK[2] - RC[i+1]); K[2*i+3] = G(WK[1] - WK[3] + RC[i+1]) ^ K[2*i+2]; T = get_byte(0, WK[3]); WK[3] = (WK[3] << 8) | get_byte(0, WK[2]); WK[2] = (WK[2] << 8) | T; } } } /* * S-Box Tables for SEED * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { const u32bit SEED::G_FUNC::S0[256] = { 0x2989A1A8, 0x05858184, 0x16C6D2D4, 0x13C3D3D0, 0x14445054, 0x1D0D111C, 0x2C8CA0AC, 0x25052124, 0x1D4D515C, 0x03434340, 0x18081018, 0x1E0E121C, 0x11415150, 0x3CCCF0FC, 0x0ACAC2C8, 0x23436360, 0x28082028, 0x04444044, 0x20002020, 0x1D8D919C, 0x20C0E0E0, 0x22C2E2E0, 0x08C8C0C8, 0x17071314, 0x2585A1A4, 0x0F8F838C, 0x03030300, 0x3B4B7378, 0x3B8BB3B8, 0x13031310, 0x12C2D2D0, 0x2ECEE2EC, 0x30407070, 0x0C8C808C, 0x3F0F333C, 0x2888A0A8, 0x32023230, 0x1DCDD1DC, 0x36C6F2F4, 0x34447074, 0x2CCCE0EC, 0x15859194, 0x0B0B0308, 0x17475354, 0x1C4C505C, 0x1B4B5358, 0x3D8DB1BC, 0x01010100, 0x24042024, 0x1C0C101C, 0x33437370, 0x18889098, 0x10001010, 0x0CCCC0CC, 0x32C2F2F0, 0x19C9D1D8, 0x2C0C202C, 0x27C7E3E4, 0x32427270, 0x03838380, 0x1B8B9398, 0x11C1D1D0, 0x06868284, 0x09C9C1C8, 0x20406060, 0x10405050, 0x2383A3A0, 0x2BCBE3E8, 0x0D0D010C, 0x3686B2B4, 0x1E8E929C, 0x0F4F434C, 0x3787B3B4, 0x1A4A5258, 0x06C6C2C4, 0x38487078, 0x2686A2A4, 0x12021210, 0x2F8FA3AC, 0x15C5D1D4, 0x21416160, 0x03C3C3C0, 0x3484B0B4, 0x01414140, 0x12425250, 0x3D4D717C, 0x0D8D818C, 0x08080008, 0x1F0F131C, 0x19899198, 0x00000000, 0x19091118, 0x04040004, 0x13435350, 0x37C7F3F4, 0x21C1E1E0, 0x3DCDF1FC, 0x36467274, 0x2F0F232C, 0x27072324, 0x3080B0B0, 0x0B8B8388, 0x0E0E020C, 0x2B8BA3A8, 0x2282A2A0, 0x2E4E626C, 0x13839390, 0x0D4D414C, 0x29496168, 0x3C4C707C, 0x09090108, 0x0A0A0208, 0x3F8FB3BC, 0x2FCFE3EC, 0x33C3F3F0, 0x05C5C1C4, 0x07878384, 0x14041014, 0x3ECEF2FC, 0x24446064, 0x1ECED2DC, 0x2E0E222C, 0x0B4B4348, 0x1A0A1218, 0x06060204, 0x21012120, 0x2B4B6368, 0x26466264, 0x02020200, 0x35C5F1F4, 0x12829290, 0x0A8A8288, 0x0C0C000C, 0x3383B3B0, 0x3E4E727C, 0x10C0D0D0, 0x3A4A7278, 0x07474344, 0x16869294, 0x25C5E1E4, 0x26062224, 0x00808080, 0x2D8DA1AC, 0x1FCFD3DC, 0x2181A1A0, 0x30003030, 0x37073334, 0x2E8EA2AC, 0x36063234, 0x15051114, 0x22022220, 0x38083038, 0x34C4F0F4, 0x2787A3A4, 0x05454144, 0x0C4C404C, 0x01818180, 0x29C9E1E8, 0x04848084, 0x17879394, 0x35053134, 0x0BCBC3C8, 0x0ECEC2CC, 0x3C0C303C, 0x31417170, 0x11011110, 0x07C7C3C4, 0x09898188, 0x35457174, 0x3BCBF3F8, 0x1ACAD2D8, 0x38C8F0F8, 0x14849094, 0x19495158, 0x02828280, 0x04C4C0C4, 0x3FCFF3FC, 0x09494148, 0x39093138, 0x27476364, 0x00C0C0C0, 0x0FCFC3CC, 0x17C7D3D4, 0x3888B0B8, 0x0F0F030C, 0x0E8E828C, 0x02424240, 0x23032320, 0x11819190, 0x2C4C606C, 0x1BCBD3D8, 0x2484A0A4, 0x34043034, 0x31C1F1F0, 0x08484048, 0x02C2C2C0, 0x2F4F636C, 0x3D0D313C, 0x2D0D212C, 0x00404040, 0x3E8EB2BC, 0x3E0E323C, 0x3C8CB0BC, 0x01C1C1C0, 0x2A8AA2A8, 0x3A8AB2B8, 0x0E4E424C, 0x15455154, 0x3B0B3338, 0x1CCCD0DC, 0x28486068, 0x3F4F737C, 0x1C8C909C, 0x18C8D0D8, 0x0A4A4248, 0x16465254, 0x37477374, 0x2080A0A0, 0x2DCDE1EC, 0x06464244, 0x3585B1B4, 0x2B0B2328, 0x25456164, 0x3ACAF2F8, 0x23C3E3E0, 0x3989B1B8, 0x3181B1B0, 0x1F8F939C, 0x1E4E525C, 0x39C9F1F8, 0x26C6E2E4, 0x3282B2B0, 0x31013130, 0x2ACAE2E8, 0x2D4D616C, 0x1F4F535C, 0x24C4E0E4, 0x30C0F0F0, 0x0DCDC1CC, 0x08888088, 0x16061214, 0x3A0A3238, 0x18485058, 0x14C4D0D4, 0x22426260, 0x29092128, 0x07070304, 0x33033330, 0x28C8E0E8, 0x1B0B1318, 0x05050104, 0x39497178, 0x10809090, 0x2A4A6268, 0x2A0A2228, 0x1A8A9298 }; const u32bit SEED::G_FUNC::S1[256] = { 0x38380830, 0xE828C8E0, 0x2C2D0D21, 0xA42686A2, 0xCC0FCFC3, 0xDC1ECED2, 0xB03383B3, 0xB83888B0, 0xAC2F8FA3, 0x60204060, 0x54154551, 0xC407C7C3, 0x44044440, 0x6C2F4F63, 0x682B4B63, 0x581B4B53, 0xC003C3C3, 0x60224262, 0x30330333, 0xB43585B1, 0x28290921, 0xA02080A0, 0xE022C2E2, 0xA42787A3, 0xD013C3D3, 0x90118191, 0x10110111, 0x04060602, 0x1C1C0C10, 0xBC3C8CB0, 0x34360632, 0x480B4B43, 0xEC2FCFE3, 0x88088880, 0x6C2C4C60, 0xA82888A0, 0x14170713, 0xC404C4C0, 0x14160612, 0xF434C4F0, 0xC002C2C2, 0x44054541, 0xE021C1E1, 0xD416C6D2, 0x3C3F0F33, 0x3C3D0D31, 0x8C0E8E82, 0x98188890, 0x28280820, 0x4C0E4E42, 0xF436C6F2, 0x3C3E0E32, 0xA42585A1, 0xF839C9F1, 0x0C0D0D01, 0xDC1FCFD3, 0xD818C8D0, 0x282B0B23, 0x64264662, 0x783A4A72, 0x24270723, 0x2C2F0F23, 0xF031C1F1, 0x70324272, 0x40024242, 0xD414C4D0, 0x40014141, 0xC000C0C0, 0x70334373, 0x64274763, 0xAC2C8CA0, 0x880B8B83, 0xF437C7F3, 0xAC2D8DA1, 0x80008080, 0x1C1F0F13, 0xC80ACAC2, 0x2C2C0C20, 0xA82A8AA2, 0x34340430, 0xD012C2D2, 0x080B0B03, 0xEC2ECEE2, 0xE829C9E1, 0x5C1D4D51, 0x94148490, 0x18180810, 0xF838C8F0, 0x54174753, 0xAC2E8EA2, 0x08080800, 0xC405C5C1, 0x10130313, 0xCC0DCDC1, 0x84068682, 0xB83989B1, 0xFC3FCFF3, 0x7C3D4D71, 0xC001C1C1, 0x30310131, 0xF435C5F1, 0x880A8A82, 0x682A4A62, 0xB03181B1, 0xD011C1D1, 0x20200020, 0xD417C7D3, 0x00020202, 0x20220222, 0x04040400, 0x68284860, 0x70314171, 0x04070703, 0xD81BCBD3, 0x9C1D8D91, 0x98198991, 0x60214161, 0xBC3E8EB2, 0xE426C6E2, 0x58194951, 0xDC1DCDD1, 0x50114151, 0x90108090, 0xDC1CCCD0, 0x981A8A92, 0xA02383A3, 0xA82B8BA3, 0xD010C0D0, 0x80018181, 0x0C0F0F03, 0x44074743, 0x181A0A12, 0xE023C3E3, 0xEC2CCCE0, 0x8C0D8D81, 0xBC3F8FB3, 0x94168692, 0x783B4B73, 0x5C1C4C50, 0xA02282A2, 0xA02181A1, 0x60234363, 0x20230323, 0x4C0D4D41, 0xC808C8C0, 0x9C1E8E92, 0x9C1C8C90, 0x383A0A32, 0x0C0C0C00, 0x2C2E0E22, 0xB83A8AB2, 0x6C2E4E62, 0x9C1F8F93, 0x581A4A52, 0xF032C2F2, 0x90128292, 0xF033C3F3, 0x48094941, 0x78384870, 0xCC0CCCC0, 0x14150511, 0xF83BCBF3, 0x70304070, 0x74354571, 0x7C3F4F73, 0x34350531, 0x10100010, 0x00030303, 0x64244460, 0x6C2D4D61, 0xC406C6C2, 0x74344470, 0xD415C5D1, 0xB43484B0, 0xE82ACAE2, 0x08090901, 0x74364672, 0x18190911, 0xFC3ECEF2, 0x40004040, 0x10120212, 0xE020C0E0, 0xBC3D8DB1, 0x04050501, 0xF83ACAF2, 0x00010101, 0xF030C0F0, 0x282A0A22, 0x5C1E4E52, 0xA82989A1, 0x54164652, 0x40034343, 0x84058581, 0x14140410, 0x88098981, 0x981B8B93, 0xB03080B0, 0xE425C5E1, 0x48084840, 0x78394971, 0x94178793, 0xFC3CCCF0, 0x1C1E0E12, 0x80028282, 0x20210121, 0x8C0C8C80, 0x181B0B13, 0x5C1F4F53, 0x74374773, 0x54144450, 0xB03282B2, 0x1C1D0D11, 0x24250521, 0x4C0F4F43, 0x00000000, 0x44064642, 0xEC2DCDE1, 0x58184850, 0x50124252, 0xE82BCBE3, 0x7C3E4E72, 0xD81ACAD2, 0xC809C9C1, 0xFC3DCDF1, 0x30300030, 0x94158591, 0x64254561, 0x3C3C0C30, 0xB43686B2, 0xE424C4E0, 0xB83B8BB3, 0x7C3C4C70, 0x0C0E0E02, 0x50104050, 0x38390931, 0x24260622, 0x30320232, 0x84048480, 0x68294961, 0x90138393, 0x34370733, 0xE427C7E3, 0x24240420, 0xA42484A0, 0xC80BCBC3, 0x50134353, 0x080A0A02, 0x84078783, 0xD819C9D1, 0x4C0C4C40, 0x80038383, 0x8C0F8F83, 0xCC0ECEC2, 0x383B0B33, 0x480A4A42, 0xB43787B3 }; const u32bit SEED::G_FUNC::S2[256] = { 0xA1A82989, 0x81840585, 0xD2D416C6, 0xD3D013C3, 0x50541444, 0x111C1D0D, 0xA0AC2C8C, 0x21242505, 0x515C1D4D, 0x43400343, 0x10181808, 0x121C1E0E, 0x51501141, 0xF0FC3CCC, 0xC2C80ACA, 0x63602343, 0x20282808, 0x40440444, 0x20202000, 0x919C1D8D, 0xE0E020C0, 0xE2E022C2, 0xC0C808C8, 0x13141707, 0xA1A42585, 0x838C0F8F, 0x03000303, 0x73783B4B, 0xB3B83B8B, 0x13101303, 0xD2D012C2, 0xE2EC2ECE, 0x70703040, 0x808C0C8C, 0x333C3F0F, 0xA0A82888, 0x32303202, 0xD1DC1DCD, 0xF2F436C6, 0x70743444, 0xE0EC2CCC, 0x91941585, 0x03080B0B, 0x53541747, 0x505C1C4C, 0x53581B4B, 0xB1BC3D8D, 0x01000101, 0x20242404, 0x101C1C0C, 0x73703343, 0x90981888, 0x10101000, 0xC0CC0CCC, 0xF2F032C2, 0xD1D819C9, 0x202C2C0C, 0xE3E427C7, 0x72703242, 0x83800383, 0x93981B8B, 0xD1D011C1, 0x82840686, 0xC1C809C9, 0x60602040, 0x50501040, 0xA3A02383, 0xE3E82BCB, 0x010C0D0D, 0xB2B43686, 0x929C1E8E, 0x434C0F4F, 0xB3B43787, 0x52581A4A, 0xC2C406C6, 0x70783848, 0xA2A42686, 0x12101202, 0xA3AC2F8F, 0xD1D415C5, 0x61602141, 0xC3C003C3, 0xB0B43484, 0x41400141, 0x52501242, 0x717C3D4D, 0x818C0D8D, 0x00080808, 0x131C1F0F, 0x91981989, 0x00000000, 0x11181909, 0x00040404, 0x53501343, 0xF3F437C7, 0xE1E021C1, 0xF1FC3DCD, 0x72743646, 0x232C2F0F, 0x23242707, 0xB0B03080, 0x83880B8B, 0x020C0E0E, 0xA3A82B8B, 0xA2A02282, 0x626C2E4E, 0x93901383, 0x414C0D4D, 0x61682949, 0x707C3C4C, 0x01080909, 0x02080A0A, 0xB3BC3F8F, 0xE3EC2FCF, 0xF3F033C3, 0xC1C405C5, 0x83840787, 0x10141404, 0xF2FC3ECE, 0x60642444, 0xD2DC1ECE, 0x222C2E0E, 0x43480B4B, 0x12181A0A, 0x02040606, 0x21202101, 0x63682B4B, 0x62642646, 0x02000202, 0xF1F435C5, 0x92901282, 0x82880A8A, 0x000C0C0C, 0xB3B03383, 0x727C3E4E, 0xD0D010C0, 0x72783A4A, 0x43440747, 0x92941686, 0xE1E425C5, 0x22242606, 0x80800080, 0xA1AC2D8D, 0xD3DC1FCF, 0xA1A02181, 0x30303000, 0x33343707, 0xA2AC2E8E, 0x32343606, 0x11141505, 0x22202202, 0x30383808, 0xF0F434C4, 0xA3A42787, 0x41440545, 0x404C0C4C, 0x81800181, 0xE1E829C9, 0x80840484, 0x93941787, 0x31343505, 0xC3C80BCB, 0xC2CC0ECE, 0x303C3C0C, 0x71703141, 0x11101101, 0xC3C407C7, 0x81880989, 0x71743545, 0xF3F83BCB, 0xD2D81ACA, 0xF0F838C8, 0x90941484, 0x51581949, 0x82800282, 0xC0C404C4, 0xF3FC3FCF, 0x41480949, 0x31383909, 0x63642747, 0xC0C000C0, 0xC3CC0FCF, 0xD3D417C7, 0xB0B83888, 0x030C0F0F, 0x828C0E8E, 0x42400242, 0x23202303, 0x91901181, 0x606C2C4C, 0xD3D81BCB, 0xA0A42484, 0x30343404, 0xF1F031C1, 0x40480848, 0xC2C002C2, 0x636C2F4F, 0x313C3D0D, 0x212C2D0D, 0x40400040, 0xB2BC3E8E, 0x323C3E0E, 0xB0BC3C8C, 0xC1C001C1, 0xA2A82A8A, 0xB2B83A8A, 0x424C0E4E, 0x51541545, 0x33383B0B, 0xD0DC1CCC, 0x60682848, 0x737C3F4F, 0x909C1C8C, 0xD0D818C8, 0x42480A4A, 0x52541646, 0x73743747, 0xA0A02080, 0xE1EC2DCD, 0x42440646, 0xB1B43585, 0x23282B0B, 0x61642545, 0xF2F83ACA, 0xE3E023C3, 0xB1B83989, 0xB1B03181, 0x939C1F8F, 0x525C1E4E, 0xF1F839C9, 0xE2E426C6, 0xB2B03282, 0x31303101, 0xE2E82ACA, 0x616C2D4D, 0x535C1F4F, 0xE0E424C4, 0xF0F030C0, 0xC1CC0DCD, 0x80880888, 0x12141606, 0x32383A0A, 0x50581848, 0xD0D414C4, 0x62602242, 0x21282909, 0x03040707, 0x33303303, 0xE0E828C8, 0x13181B0B, 0x01040505, 0x71783949, 0x90901080, 0x62682A4A, 0x22282A0A, 0x92981A8A }; const u32bit SEED::G_FUNC::S3[256] = { 0x08303838, 0xC8E0E828, 0x0D212C2D, 0x86A2A426, 0xCFC3CC0F, 0xCED2DC1E, 0x83B3B033, 0x88B0B838, 0x8FA3AC2F, 0x40606020, 0x45515415, 0xC7C3C407, 0x44404404, 0x4F636C2F, 0x4B63682B, 0x4B53581B, 0xC3C3C003, 0x42626022, 0x03333033, 0x85B1B435, 0x09212829, 0x80A0A020, 0xC2E2E022, 0x87A3A427, 0xC3D3D013, 0x81919011, 0x01111011, 0x06020406, 0x0C101C1C, 0x8CB0BC3C, 0x06323436, 0x4B43480B, 0xCFE3EC2F, 0x88808808, 0x4C606C2C, 0x88A0A828, 0x07131417, 0xC4C0C404, 0x06121416, 0xC4F0F434, 0xC2C2C002, 0x45414405, 0xC1E1E021, 0xC6D2D416, 0x0F333C3F, 0x0D313C3D, 0x8E828C0E, 0x88909818, 0x08202828, 0x4E424C0E, 0xC6F2F436, 0x0E323C3E, 0x85A1A425, 0xC9F1F839, 0x0D010C0D, 0xCFD3DC1F, 0xC8D0D818, 0x0B23282B, 0x46626426, 0x4A72783A, 0x07232427, 0x0F232C2F, 0xC1F1F031, 0x42727032, 0x42424002, 0xC4D0D414, 0x41414001, 0xC0C0C000, 0x43737033, 0x47636427, 0x8CA0AC2C, 0x8B83880B, 0xC7F3F437, 0x8DA1AC2D, 0x80808000, 0x0F131C1F, 0xCAC2C80A, 0x0C202C2C, 0x8AA2A82A, 0x04303434, 0xC2D2D012, 0x0B03080B, 0xCEE2EC2E, 0xC9E1E829, 0x4D515C1D, 0x84909414, 0x08101818, 0xC8F0F838, 0x47535417, 0x8EA2AC2E, 0x08000808, 0xC5C1C405, 0x03131013, 0xCDC1CC0D, 0x86828406, 0x89B1B839, 0xCFF3FC3F, 0x4D717C3D, 0xC1C1C001, 0x01313031, 0xC5F1F435, 0x8A82880A, 0x4A62682A, 0x81B1B031, 0xC1D1D011, 0x00202020, 0xC7D3D417, 0x02020002, 0x02222022, 0x04000404, 0x48606828, 0x41717031, 0x07030407, 0xCBD3D81B, 0x8D919C1D, 0x89919819, 0x41616021, 0x8EB2BC3E, 0xC6E2E426, 0x49515819, 0xCDD1DC1D, 0x41515011, 0x80909010, 0xCCD0DC1C, 0x8A92981A, 0x83A3A023, 0x8BA3A82B, 0xC0D0D010, 0x81818001, 0x0F030C0F, 0x47434407, 0x0A12181A, 0xC3E3E023, 0xCCE0EC2C, 0x8D818C0D, 0x8FB3BC3F, 0x86929416, 0x4B73783B, 0x4C505C1C, 0x82A2A022, 0x81A1A021, 0x43636023, 0x03232023, 0x4D414C0D, 0xC8C0C808, 0x8E929C1E, 0x8C909C1C, 0x0A32383A, 0x0C000C0C, 0x0E222C2E, 0x8AB2B83A, 0x4E626C2E, 0x8F939C1F, 0x4A52581A, 0xC2F2F032, 0x82929012, 0xC3F3F033, 0x49414809, 0x48707838, 0xCCC0CC0C, 0x05111415, 0xCBF3F83B, 0x40707030, 0x45717435, 0x4F737C3F, 0x05313435, 0x00101010, 0x03030003, 0x44606424, 0x4D616C2D, 0xC6C2C406, 0x44707434, 0xC5D1D415, 0x84B0B434, 0xCAE2E82A, 0x09010809, 0x46727436, 0x09111819, 0xCEF2FC3E, 0x40404000, 0x02121012, 0xC0E0E020, 0x8DB1BC3D, 0x05010405, 0xCAF2F83A, 0x01010001, 0xC0F0F030, 0x0A22282A, 0x4E525C1E, 0x89A1A829, 0x46525416, 0x43434003, 0x85818405, 0x04101414, 0x89818809, 0x8B93981B, 0x80B0B030, 0xC5E1E425, 0x48404808, 0x49717839, 0x87939417, 0xCCF0FC3C, 0x0E121C1E, 0x82828002, 0x01212021, 0x8C808C0C, 0x0B13181B, 0x4F535C1F, 0x47737437, 0x44505414, 0x82B2B032, 0x0D111C1D, 0x05212425, 0x4F434C0F, 0x00000000, 0x46424406, 0xCDE1EC2D, 0x48505818, 0x42525012, 0xCBE3E82B, 0x4E727C3E, 0xCAD2D81A, 0xC9C1C809, 0xCDF1FC3D, 0x00303030, 0x85919415, 0x45616425, 0x0C303C3C, 0x86B2B436, 0xC4E0E424, 0x8BB3B83B, 0x4C707C3C, 0x0E020C0E, 0x40505010, 0x09313839, 0x06222426, 0x02323032, 0x84808404, 0x49616829, 0x83939013, 0x07333437, 0xC7E3E427, 0x04202424, 0x84A0A424, 0xCBC3C80B, 0x43535013, 0x0A02080A, 0x87838407, 0xC9D1D819, 0x4C404C0C, 0x83838003, 0x8F838C0F, 0xCEC2CC0E, 0x0B33383B, 0x4A42480A, 0x87B3B437 }; } /* * Serpent * (C) 1999-2007 Jack Lloyd * * The sbox expressions used here were discovered by Dag Arne Osvik and * are described in his paper "Speeding Up Serpent". * * Distributed under the terms of the Botan license */ namespace Botan { namespace { /* * Serpent Encryption S-Box 1 */ inline void SBoxE1(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) { u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; T3 ^= T0; T4 = T1; T1 &= T3; T4 ^= T2; T1 ^= T0; T0 |= T3; T0 ^= T4; T4 ^= T3; T3 ^= T2; T2 |= T1; T2 ^= T4; T4 = ~T4; T4 |= T1; T1 ^= T3; T1 ^= T4; T3 |= T0; T1 ^= T3; T4 ^= T3; B0 = T1; B1 = T4; B2 = T2; B3 = T0; } /* * Serpent Encryption S-Box 2 */ inline void SBoxE2(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) { u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; T0 = ~T0; T2 = ~T2; T4 = T0; T0 &= T1; T2 ^= T0; T0 |= T3; T3 ^= T2; T1 ^= T0; T0 ^= T4; T4 |= T1; T1 ^= T3; T2 |= T0; T2 &= T4; T0 ^= T1; T1 &= T2; T1 ^= T0; T0 &= T2; T0 ^= T4; B0 = T2; B1 = T0; B2 = T3; B3 = T1; } /* * Serpent Encryption S-Box 3 */ inline void SBoxE3(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) { u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; T4 = T0; T0 &= T2; T0 ^= T3; T2 ^= T1; T2 ^= T0; T3 |= T4; T3 ^= T1; T4 ^= T2; T1 = T3; T3 |= T4; T3 ^= T0; T0 &= T1; T4 ^= T0; T1 ^= T3; T1 ^= T4; T4 = ~T4; B0 = T2; B1 = T3; B2 = T1; B3 = T4; } /* * Serpent Encryption S-Box 4 */ inline void SBoxE4(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) { u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; T4 = T0; T0 |= T3; T3 ^= T1; T1 &= T4; T4 ^= T2; T2 ^= T3; T3 &= T0; T4 |= T1; T3 ^= T4; T0 ^= T1; T4 &= T0; T1 ^= T3; T4 ^= T2; T1 |= T0; T1 ^= T2; T0 ^= T3; T2 = T1; T1 |= T3; T1 ^= T0; B0 = T1; B1 = T2; B2 = T3; B3 = T4; } /* * Serpent Encryption S-Box 5 */ inline void SBoxE5(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) { u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; T1 ^= T3; T3 = ~T3; T2 ^= T3; T3 ^= T0; T4 = T1; T1 &= T3; T1 ^= T2; T4 ^= T3; T0 ^= T4; T2 &= T4; T2 ^= T0; T0 &= T1; T3 ^= T0; T4 |= T1; T4 ^= T0; T0 |= T3; T0 ^= T2; T2 &= T3; T0 = ~T0; T4 ^= T2; B0 = T1; B1 = T4; B2 = T0; B3 = T3; } /* * Serpent Encryption S-Box 6 */ inline void SBoxE6(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) { u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; T0 ^= T1; T1 ^= T3; T3 = ~T3; T4 = T1; T1 &= T0; T2 ^= T3; T1 ^= T2; T2 |= T4; T4 ^= T3; T3 &= T1; T3 ^= T0; T4 ^= T1; T4 ^= T2; T2 ^= T0; T0 &= T3; T2 = ~T2; T0 ^= T4; T4 |= T3; T2 ^= T4; B0 = T1; B1 = T3; B2 = T0; B3 = T2; } /* * Serpent Encryption S-Box 7 */ inline void SBoxE7(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) { u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; T2 = ~T2; T4 = T3; T3 &= T0; T0 ^= T4; T3 ^= T2; T2 |= T4; T1 ^= T3; T2 ^= T0; T0 |= T1; T2 ^= T1; T4 ^= T0; T0 |= T3; T0 ^= T2; T4 ^= T3; T4 ^= T0; T3 = ~T3; T2 &= T4; T2 ^= T3; B0 = T0; B1 = T1; B2 = T4; B3 = T2; } /* * Serpent Encryption S-Box 8 */ inline void SBoxE8(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) { u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; T4 = T1; T1 |= T2; T1 ^= T3; T4 ^= T2; T2 ^= T1; T3 |= T4; T3 &= T0; T4 ^= T2; T3 ^= T1; T1 |= T4; T1 ^= T0; T0 |= T4; T0 ^= T2; T1 ^= T4; T2 ^= T1; T1 &= T0; T1 ^= T4; T2 = ~T2; T2 |= T0; T4 ^= T2; B0 = T4; B1 = T3; B2 = T1; B3 = T0; } /* * Serpent Decryption S-Box 1 */ inline void SBoxD1(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) { u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; T2 = ~T2; T4 = T1; T1 |= T0; T4 = ~T4; T1 ^= T2; T2 |= T4; T1 ^= T3; T0 ^= T4; T2 ^= T0; T0 &= T3; T4 ^= T0; T0 |= T1; T0 ^= T2; T3 ^= T4; T2 ^= T1; T3 ^= T0; T3 ^= T1; T2 &= T3; T4 ^= T2; B0 = T0; B1 = T4; B2 = T1; B3 = T3; } /* * Serpent Decryption S-Box 2 */ inline void SBoxD2(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) { u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; T4 = T1; T1 ^= T3; T3 &= T1; T4 ^= T2; T3 ^= T0; T0 |= T1; T2 ^= T3; T0 ^= T4; T0 |= T2; T1 ^= T3; T0 ^= T1; T1 |= T3; T1 ^= T0; T4 = ~T4; T4 ^= T1; T1 |= T0; T1 ^= T0; T1 |= T4; T3 ^= T1; B0 = T4; B1 = T0; B2 = T3; B3 = T2; } /* * Serpent Decryption S-Box 3 */ inline void SBoxD3(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) { u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; T2 ^= T3; T3 ^= T0; T4 = T3; T3 &= T2; T3 ^= T1; T1 |= T2; T1 ^= T4; T4 &= T3; T2 ^= T3; T4 &= T0; T4 ^= T2; T2 &= T1; T2 |= T0; T3 = ~T3; T2 ^= T3; T0 ^= T3; T0 &= T1; T3 ^= T4; T3 ^= T0; B0 = T1; B1 = T4; B2 = T2; B3 = T3; } /* * Serpent Decryption S-Box 4 */ inline void SBoxD4(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) { u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; T4 = T2; T2 ^= T1; T0 ^= T2; T4 &= T2; T4 ^= T0; T0 &= T1; T1 ^= T3; T3 |= T4; T2 ^= T3; T0 ^= T3; T1 ^= T4; T3 &= T2; T3 ^= T1; T1 ^= T0; T1 |= T2; T0 ^= T3; T1 ^= T4; T0 ^= T1; B0 = T2; B1 = T1; B2 = T3; B3 = T0; } /* * Serpent Decryption S-Box 5 */ inline void SBoxD5(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) { u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; T4 = T2; T2 &= T3; T2 ^= T1; T1 |= T3; T1 &= T0; T4 ^= T2; T4 ^= T1; T1 &= T2; T0 = ~T0; T3 ^= T4; T1 ^= T3; T3 &= T0; T3 ^= T2; T0 ^= T1; T2 &= T0; T3 ^= T0; T2 ^= T4; T2 |= T3; T3 ^= T0; T2 ^= T1; B0 = T0; B1 = T3; B2 = T2; B3 = T4; } /* * Serpent Decryption S-Box 6 */ inline void SBoxD6(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) { u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; T1 = ~T1; T4 = T3; T2 ^= T1; T3 |= T0; T3 ^= T2; T2 |= T1; T2 &= T0; T4 ^= T3; T2 ^= T4; T4 |= T0; T4 ^= T1; T1 &= T2; T1 ^= T3; T4 ^= T2; T3 &= T4; T4 ^= T1; T3 ^= T4; T4 = ~T4; T3 ^= T0; B0 = T1; B1 = T4; B2 = T3; B3 = T2; } /* * Serpent Decryption S-Box 7 */ inline void SBoxD7(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) { u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; T0 ^= T2; T4 = T2; T2 &= T0; T4 ^= T3; T2 = ~T2; T3 ^= T1; T2 ^= T3; T4 |= T0; T0 ^= T2; T3 ^= T4; T4 ^= T1; T1 &= T3; T1 ^= T0; T0 ^= T3; T0 |= T2; T3 ^= T1; T4 ^= T0; B0 = T1; B1 = T2; B2 = T4; B3 = T3; } /* * Serpent Decryption S-Box 8 */ inline void SBoxD8(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) { u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; T4 = T2; T2 ^= T0; T0 &= T3; T4 |= T3; T2 = ~T2; T3 ^= T1; T1 |= T0; T0 ^= T2; T2 &= T4; T3 &= T4; T1 ^= T2; T2 ^= T0; T0 |= T2; T4 ^= T1; T0 ^= T3; T3 ^= T4; T4 |= T0; T3 ^= T2; T4 ^= T2; B0 = T3; B1 = T0; B2 = T1; B3 = T4; } /* * Serpent's Linear Transformation */ inline void transform(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) { B0 = rotate_left(B0, 13); B2 = rotate_left(B2, 3); B1 ^= B0 ^ B2; B3 ^= B2 ^ (B0 << 3); B1 = rotate_left(B1, 1); B3 = rotate_left(B3, 7); B0 ^= B1 ^ B3; B2 ^= B3 ^ (B1 << 7); B0 = rotate_left(B0, 5); B2 = rotate_left(B2, 22); } /* * Serpent's Inverse Linear Transformation */ inline void i_transform(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) { B2 = rotate_right(B2, 22); B0 = rotate_right(B0, 5); B2 ^= B3 ^ (B1 << 7); B0 ^= B1 ^ B3; B3 = rotate_right(B3, 7); B1 = rotate_right(B1, 1); B3 ^= B2 ^ (B0 << 3); B1 ^= B0 ^ B2; B2 = rotate_right(B2, 3); B0 = rotate_right(B0, 13); } } /* * XOR a key block with a data block */ #define key_xor(round, B0, B1, B2, B3) \ B0 ^= round_key[4*round ]; \ B1 ^= round_key[4*round+1]; \ B2 ^= round_key[4*round+2]; \ B3 ^= round_key[4*round+3]; /* * Serpent Encryption */ void Serpent::encrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u32bit B0 = load_le<u32bit>(in, 0); u32bit B1 = load_le<u32bit>(in, 1); u32bit B2 = load_le<u32bit>(in, 2); u32bit B3 = load_le<u32bit>(in, 3); key_xor( 0,B0,B1,B2,B3); SBoxE1(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor( 1,B0,B1,B2,B3); SBoxE2(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor( 2,B0,B1,B2,B3); SBoxE3(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor( 3,B0,B1,B2,B3); SBoxE4(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor( 4,B0,B1,B2,B3); SBoxE5(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor( 5,B0,B1,B2,B3); SBoxE6(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor( 6,B0,B1,B2,B3); SBoxE7(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor( 7,B0,B1,B2,B3); SBoxE8(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor( 8,B0,B1,B2,B3); SBoxE1(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor( 9,B0,B1,B2,B3); SBoxE2(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(10,B0,B1,B2,B3); SBoxE3(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(11,B0,B1,B2,B3); SBoxE4(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(12,B0,B1,B2,B3); SBoxE5(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(13,B0,B1,B2,B3); SBoxE6(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(14,B0,B1,B2,B3); SBoxE7(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(15,B0,B1,B2,B3); SBoxE8(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(16,B0,B1,B2,B3); SBoxE1(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(17,B0,B1,B2,B3); SBoxE2(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(18,B0,B1,B2,B3); SBoxE3(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(19,B0,B1,B2,B3); SBoxE4(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(20,B0,B1,B2,B3); SBoxE5(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(21,B0,B1,B2,B3); SBoxE6(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(22,B0,B1,B2,B3); SBoxE7(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(23,B0,B1,B2,B3); SBoxE8(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(24,B0,B1,B2,B3); SBoxE1(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(25,B0,B1,B2,B3); SBoxE2(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(26,B0,B1,B2,B3); SBoxE3(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(27,B0,B1,B2,B3); SBoxE4(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(28,B0,B1,B2,B3); SBoxE5(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(29,B0,B1,B2,B3); SBoxE6(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(30,B0,B1,B2,B3); SBoxE7(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(31,B0,B1,B2,B3); SBoxE8(B0,B1,B2,B3); key_xor(32,B0,B1,B2,B3); store_le(out, B0, B1, B2, B3); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * Serpent Decryption */ void Serpent::decrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u32bit B0 = load_le<u32bit>(in, 0); u32bit B1 = load_le<u32bit>(in, 1); u32bit B2 = load_le<u32bit>(in, 2); u32bit B3 = load_le<u32bit>(in, 3); key_xor(32,B0,B1,B2,B3); SBoxD8(B0,B1,B2,B3); key_xor(31,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD7(B0,B1,B2,B3); key_xor(30,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD6(B0,B1,B2,B3); key_xor(29,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD5(B0,B1,B2,B3); key_xor(28,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD4(B0,B1,B2,B3); key_xor(27,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD3(B0,B1,B2,B3); key_xor(26,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD2(B0,B1,B2,B3); key_xor(25,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD1(B0,B1,B2,B3); key_xor(24,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD8(B0,B1,B2,B3); key_xor(23,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD7(B0,B1,B2,B3); key_xor(22,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD6(B0,B1,B2,B3); key_xor(21,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD5(B0,B1,B2,B3); key_xor(20,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD4(B0,B1,B2,B3); key_xor(19,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD3(B0,B1,B2,B3); key_xor(18,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD2(B0,B1,B2,B3); key_xor(17,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD1(B0,B1,B2,B3); key_xor(16,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD8(B0,B1,B2,B3); key_xor(15,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD7(B0,B1,B2,B3); key_xor(14,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD6(B0,B1,B2,B3); key_xor(13,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD5(B0,B1,B2,B3); key_xor(12,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD4(B0,B1,B2,B3); key_xor(11,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD3(B0,B1,B2,B3); key_xor(10,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD2(B0,B1,B2,B3); key_xor( 9,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD1(B0,B1,B2,B3); key_xor( 8,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD8(B0,B1,B2,B3); key_xor( 7,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD7(B0,B1,B2,B3); key_xor( 6,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD6(B0,B1,B2,B3); key_xor( 5,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD5(B0,B1,B2,B3); key_xor( 4,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD4(B0,B1,B2,B3); key_xor( 3,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD3(B0,B1,B2,B3); key_xor( 2,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD2(B0,B1,B2,B3); key_xor( 1,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD1(B0,B1,B2,B3); key_xor( 0,B0,B1,B2,B3); store_le(out, B0, B1, B2, B3); in += BLOCK_SIZE; out += BLOCK_SIZE; } } #undef key_xor #undef transform #undef i_transform /* * Serpent Key Schedule */ void Serpent::key_schedule(const byte key[], size_t length) { const u32bit PHI = 0x9E3779B9; SecureVector<u32bit> W(140); for(size_t i = 0; i != length / 4; ++i) W[i] = load_le<u32bit>(key, i); W[length / 4] |= u32bit(1) << ((length%4)*8); for(size_t i = 8; i != 140; ++i) { u32bit wi = W[i-8] ^ W[i-5] ^ W[i-3] ^ W[i-1] ^ PHI ^ u32bit(i-8); W[i] = rotate_left(wi, 11); } SBoxE4(W[ 8],W[ 9],W[ 10],W[ 11]); SBoxE3(W[ 12],W[ 13],W[ 14],W[ 15]); SBoxE2(W[ 16],W[ 17],W[ 18],W[ 19]); SBoxE1(W[ 20],W[ 21],W[ 22],W[ 23]); SBoxE8(W[ 24],W[ 25],W[ 26],W[ 27]); SBoxE7(W[ 28],W[ 29],W[ 30],W[ 31]); SBoxE6(W[ 32],W[ 33],W[ 34],W[ 35]); SBoxE5(W[ 36],W[ 37],W[ 38],W[ 39]); SBoxE4(W[ 40],W[ 41],W[ 42],W[ 43]); SBoxE3(W[ 44],W[ 45],W[ 46],W[ 47]); SBoxE2(W[ 48],W[ 49],W[ 50],W[ 51]); SBoxE1(W[ 52],W[ 53],W[ 54],W[ 55]); SBoxE8(W[ 56],W[ 57],W[ 58],W[ 59]); SBoxE7(W[ 60],W[ 61],W[ 62],W[ 63]); SBoxE6(W[ 64],W[ 65],W[ 66],W[ 67]); SBoxE5(W[ 68],W[ 69],W[ 70],W[ 71]); SBoxE4(W[ 72],W[ 73],W[ 74],W[ 75]); SBoxE3(W[ 76],W[ 77],W[ 78],W[ 79]); SBoxE2(W[ 80],W[ 81],W[ 82],W[ 83]); SBoxE1(W[ 84],W[ 85],W[ 86],W[ 87]); SBoxE8(W[ 88],W[ 89],W[ 90],W[ 91]); SBoxE7(W[ 92],W[ 93],W[ 94],W[ 95]); SBoxE6(W[ 96],W[ 97],W[ 98],W[ 99]); SBoxE5(W[100],W[101],W[102],W[103]); SBoxE4(W[104],W[105],W[106],W[107]); SBoxE3(W[108],W[109],W[110],W[111]); SBoxE2(W[112],W[113],W[114],W[115]); SBoxE1(W[116],W[117],W[118],W[119]); SBoxE8(W[120],W[121],W[122],W[123]); SBoxE7(W[124],W[125],W[126],W[127]); SBoxE6(W[128],W[129],W[130],W[131]); SBoxE5(W[132],W[133],W[134],W[135]); SBoxE4(W[136],W[137],W[138],W[139]); round_key.copy(&W[8], 132); } } /* * Serpent (SIMD) * (C) 2009 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { #define SBoxE1(B0, B1, B2, B3) \ do { \ B3 ^= B0; \ SIMD_32 B4 = B1; \ B1 &= B3; \ B4 ^= B2; \ B1 ^= B0; \ B0 |= B3; \ B0 ^= B4; \ B4 ^= B3; \ B3 ^= B2; \ B2 |= B1; \ B2 ^= B4; \ B4 = ~B4; \ B4 |= B1; \ B1 ^= B3; \ B1 ^= B4; \ B3 |= B0; \ B1 ^= B3; \ B4 ^= B3; \ B3 = B0; \ B0 = B1; \ B1 = B4; \ } while(0); #define SBoxE2(B0, B1, B2, B3) \ do { \ B0 = ~B0; \ B2 = ~B2; \ SIMD_32 B4 = B0; \ B0 &= B1; \ B2 ^= B0; \ B0 |= B3; \ B3 ^= B2; \ B1 ^= B0; \ B0 ^= B4; \ B4 |= B1; \ B1 ^= B3; \ B2 |= B0; \ B2 &= B4; \ B0 ^= B1; \ B1 &= B2; \ B1 ^= B0; \ B0 &= B2; \ B4 ^= B0; \ B0 = B2; \ B2 = B3; \ B3 = B1; \ B1 = B4; \ } while(0); #define SBoxE3(B0, B1, B2, B3) \ do { \ SIMD_32 B4 = B0; \ B0 &= B2; \ B0 ^= B3; \ B2 ^= B1; \ B2 ^= B0; \ B3 |= B4; \ B3 ^= B1; \ B4 ^= B2; \ B1 = B3; \ B3 |= B4; \ B3 ^= B0; \ B0 &= B1; \ B4 ^= B0; \ B1 ^= B3; \ B1 ^= B4; \ B0 = B2; \ B2 = B1; \ B1 = B3; \ B3 = ~B4; \ } while(0); #define SBoxE4(B0, B1, B2, B3) \ do { \ SIMD_32 B4 = B0; \ B0 |= B3; \ B3 ^= B1; \ B1 &= B4; \ B4 ^= B2; \ B2 ^= B3; \ B3 &= B0; \ B4 |= B1; \ B3 ^= B4; \ B0 ^= B1; \ B4 &= B0; \ B1 ^= B3; \ B4 ^= B2; \ B1 |= B0; \ B1 ^= B2; \ B0 ^= B3; \ B2 = B1; \ B1 |= B3; \ B0 ^= B1; \ B1 = B2; \ B2 = B3; \ B3 = B4; \ } while(0); #define SBoxE5(B0, B1, B2, B3) \ do { \ B1 ^= B3; \ B3 = ~B3; \ B2 ^= B3; \ B3 ^= B0; \ SIMD_32 B4 = B1; \ B1 &= B3; \ B1 ^= B2; \ B4 ^= B3; \ B0 ^= B4; \ B2 &= B4; \ B2 ^= B0; \ B0 &= B1; \ B3 ^= B0; \ B4 |= B1; \ B4 ^= B0; \ B0 |= B3; \ B0 ^= B2; \ B2 &= B3; \ B0 = ~B0; \ B4 ^= B2; \ B2 = B0; \ B0 = B1; \ B1 = B4; \ } while(0); #define SBoxE6(B0, B1, B2, B3) \ do { \ B0 ^= B1; \ B1 ^= B3; \ B3 = ~B3; \ SIMD_32 B4 = B1; \ B1 &= B0; \ B2 ^= B3; \ B1 ^= B2; \ B2 |= B4; \ B4 ^= B3; \ B3 &= B1; \ B3 ^= B0; \ B4 ^= B1; \ B4 ^= B2; \ B2 ^= B0; \ B0 &= B3; \ B2 = ~B2; \ B0 ^= B4; \ B4 |= B3; \ B4 ^= B2; \ B2 = B0; \ B0 = B1; \ B1 = B3; \ B3 = B4; \ } while(0); #define SBoxE7(B0, B1, B2, B3) \ do { \ B2 = ~B2; \ SIMD_32 B4 = B3; \ B3 &= B0; \ B0 ^= B4; \ B3 ^= B2; \ B2 |= B4; \ B1 ^= B3; \ B2 ^= B0; \ B0 |= B1; \ B2 ^= B1; \ B4 ^= B0; \ B0 |= B3; \ B0 ^= B2; \ B4 ^= B3; \ B4 ^= B0; \ B3 = ~B3; \ B2 &= B4; \ B3 ^= B2; \ B2 = B4; \ } while(0); #define SBoxE8(B0, B1, B2, B3) \ do { \ SIMD_32 B4 = B1; \ B1 |= B2; \ B1 ^= B3; \ B4 ^= B2; \ B2 ^= B1; \ B3 |= B4; \ B3 &= B0; \ B4 ^= B2; \ B3 ^= B1; \ B1 |= B4; \ B1 ^= B0; \ B0 |= B4; \ B0 ^= B2; \ B1 ^= B4; \ B2 ^= B1; \ B1 &= B0; \ B1 ^= B4; \ B2 = ~B2; \ B2 |= B0; \ B4 ^= B2; \ B2 = B1; \ B1 = B3; \ B3 = B0; \ B0 = B4; \ } while(0); #define SBoxD1(B0, B1, B2, B3) \ do { \ B2 = ~B2; \ SIMD_32 B4 = B1; \ B1 |= B0; \ B4 = ~B4; \ B1 ^= B2; \ B2 |= B4; \ B1 ^= B3; \ B0 ^= B4; \ B2 ^= B0; \ B0 &= B3; \ B4 ^= B0; \ B0 |= B1; \ B0 ^= B2; \ B3 ^= B4; \ B2 ^= B1; \ B3 ^= B0; \ B3 ^= B1; \ B2 &= B3; \ B4 ^= B2; \ B2 = B1; \ B1 = B4; \ } while(0); #define SBoxD2(B0, B1, B2, B3) \ do { \ SIMD_32 B4 = B1; \ B1 ^= B3; \ B3 &= B1; \ B4 ^= B2; \ B3 ^= B0; \ B0 |= B1; \ B2 ^= B3; \ B0 ^= B4; \ B0 |= B2; \ B1 ^= B3; \ B0 ^= B1; \ B1 |= B3; \ B1 ^= B0; \ B4 = ~B4; \ B4 ^= B1; \ B1 |= B0; \ B1 ^= B0; \ B1 |= B4; \ B3 ^= B1; \ B1 = B0; \ B0 = B4; \ B4 = B2; \ B2 = B3; \ B3 = B4; \ } while(0); #define SBoxD3(B0, B1, B2, B3) \ do { \ B2 ^= B3; \ B3 ^= B0; \ SIMD_32 B4 = B3; \ B3 &= B2; \ B3 ^= B1; \ B1 |= B2; \ B1 ^= B4; \ B4 &= B3; \ B2 ^= B3; \ B4 &= B0; \ B4 ^= B2; \ B2 &= B1; \ B2 |= B0; \ B3 = ~B3; \ B2 ^= B3; \ B0 ^= B3; \ B0 &= B1; \ B3 ^= B4; \ B3 ^= B0; \ B0 = B1; \ B1 = B4; \ } while(0); #define SBoxD4(B0, B1, B2, B3) \ do { \ SIMD_32 B4 = B2; \ B2 ^= B1; \ B0 ^= B2; \ B4 &= B2; \ B4 ^= B0; \ B0 &= B1; \ B1 ^= B3; \ B3 |= B4; \ B2 ^= B3; \ B0 ^= B3; \ B1 ^= B4; \ B3 &= B2; \ B3 ^= B1; \ B1 ^= B0; \ B1 |= B2; \ B0 ^= B3; \ B1 ^= B4; \ B0 ^= B1; \ B4 = B0; \ B0 = B2; \ B2 = B3; \ B3 = B4; \ } while(0); #define SBoxD5(B0, B1, B2, B3) \ do { \ SIMD_32 B4 = B2; \ B2 &= B3; \ B2 ^= B1; \ B1 |= B3; \ B1 &= B0; \ B4 ^= B2; \ B4 ^= B1; \ B1 &= B2; \ B0 = ~B0; \ B3 ^= B4; \ B1 ^= B3; \ B3 &= B0; \ B3 ^= B2; \ B0 ^= B1; \ B2 &= B0; \ B3 ^= B0; \ B2 ^= B4; \ B2 |= B3; \ B3 ^= B0; \ B2 ^= B1; \ B1 = B3; \ B3 = B4; \ } while(0); #define SBoxD6(B0, B1, B2, B3) \ do { \ B1 = ~B1; \ SIMD_32 B4 = B3; \ B2 ^= B1; \ B3 |= B0; \ B3 ^= B2; \ B2 |= B1; \ B2 &= B0; \ B4 ^= B3; \ B2 ^= B4; \ B4 |= B0; \ B4 ^= B1; \ B1 &= B2; \ B1 ^= B3; \ B4 ^= B2; \ B3 &= B4; \ B4 ^= B1; \ B3 ^= B4; \ B4 = ~B4; \ B3 ^= B0; \ B0 = B1; \ B1 = B4; \ B4 = B3; \ B3 = B2; \ B2 = B4; \ } while(0); #define SBoxD7(B0, B1, B2, B3) \ do { \ B0 ^= B2; \ SIMD_32 B4 = B2; \ B2 &= B0; \ B4 ^= B3; \ B2 = ~B2; \ B3 ^= B1; \ B2 ^= B3; \ B4 |= B0; \ B0 ^= B2; \ B3 ^= B4; \ B4 ^= B1; \ B1 &= B3; \ B1 ^= B0; \ B0 ^= B3; \ B0 |= B2; \ B3 ^= B1; \ B4 ^= B0; \ B0 = B1; \ B1 = B2; \ B2 = B4; \ } while(0); #define SBoxD8(B0, B1, B2, B3) \ do { \ SIMD_32 B4 = B2; \ B2 ^= B0; \ B0 &= B3; \ B4 |= B3; \ B2 = ~B2; \ B3 ^= B1; \ B1 |= B0; \ B0 ^= B2; \ B2 &= B4; \ B3 &= B4; \ B1 ^= B2; \ B2 ^= B0; \ B0 |= B2; \ B4 ^= B1; \ B0 ^= B3; \ B3 ^= B4; \ B4 |= B0; \ B3 ^= B2; \ B4 ^= B2; \ B2 = B1; \ B1 = B0; \ B0 = B3; \ B3 = B4; \ } while(0); #define key_xor(round, B0, B1, B2, B3) \ do { \ B0 ^= SIMD_32(keys[4*round ]); \ B1 ^= SIMD_32(keys[4*round+1]); \ B2 ^= SIMD_32(keys[4*round+2]); \ B3 ^= SIMD_32(keys[4*round+3]); \ } while(0); /* * Serpent's linear transformations */ #define transform(B0, B1, B2, B3) \ do { \ B0.rotate_left(13); \ B2.rotate_left(3); \ B1 ^= B0 ^ B2; \ B3 ^= B2 ^ (B0 << 3); \ B1.rotate_left(1); \ B3.rotate_left(7); \ B0 ^= B1 ^ B3; \ B2 ^= B3 ^ (B1 << 7); \ B0.rotate_left(5); \ B2.rotate_left(22); \ } while(0); #define i_transform(B0, B1, B2, B3) \ do { \ B2.rotate_right(22); \ B0.rotate_right(5); \ B2 ^= B3 ^ (B1 << 7); \ B0 ^= B1 ^ B3; \ B3.rotate_right(7); \ B1.rotate_right(1); \ B3 ^= B2 ^ (B0 << 3); \ B1 ^= B0 ^ B2; \ B2.rotate_right(3); \ B0.rotate_right(13); \ } while(0); /* * SIMD Serpent Encryption of 4 blocks in parallel */ void serpent_encrypt_4(const byte in[64], byte out[64], const u32bit keys[132]) { SIMD_32 B0 = SIMD_32::load_le(in); SIMD_32 B1 = SIMD_32::load_le(in + 16); SIMD_32 B2 = SIMD_32::load_le(in + 32); SIMD_32 B3 = SIMD_32::load_le(in + 48); SIMD_32::transpose(B0, B1, B2, B3); key_xor( 0,B0,B1,B2,B3); SBoxE1(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor( 1,B0,B1,B2,B3); SBoxE2(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor( 2,B0,B1,B2,B3); SBoxE3(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor( 3,B0,B1,B2,B3); SBoxE4(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor( 4,B0,B1,B2,B3); SBoxE5(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor( 5,B0,B1,B2,B3); SBoxE6(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor( 6,B0,B1,B2,B3); SBoxE7(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor( 7,B0,B1,B2,B3); SBoxE8(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor( 8,B0,B1,B2,B3); SBoxE1(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor( 9,B0,B1,B2,B3); SBoxE2(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(10,B0,B1,B2,B3); SBoxE3(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(11,B0,B1,B2,B3); SBoxE4(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(12,B0,B1,B2,B3); SBoxE5(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(13,B0,B1,B2,B3); SBoxE6(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(14,B0,B1,B2,B3); SBoxE7(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(15,B0,B1,B2,B3); SBoxE8(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(16,B0,B1,B2,B3); SBoxE1(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(17,B0,B1,B2,B3); SBoxE2(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(18,B0,B1,B2,B3); SBoxE3(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(19,B0,B1,B2,B3); SBoxE4(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(20,B0,B1,B2,B3); SBoxE5(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(21,B0,B1,B2,B3); SBoxE6(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(22,B0,B1,B2,B3); SBoxE7(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(23,B0,B1,B2,B3); SBoxE8(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(24,B0,B1,B2,B3); SBoxE1(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(25,B0,B1,B2,B3); SBoxE2(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(26,B0,B1,B2,B3); SBoxE3(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(27,B0,B1,B2,B3); SBoxE4(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(28,B0,B1,B2,B3); SBoxE5(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(29,B0,B1,B2,B3); SBoxE6(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(30,B0,B1,B2,B3); SBoxE7(B0,B1,B2,B3); transform(B0,B1,B2,B3); key_xor(31,B0,B1,B2,B3); SBoxE8(B0,B1,B2,B3); key_xor(32,B0,B1,B2,B3); SIMD_32::transpose(B0, B1, B2, B3); B0.store_le(out); B1.store_le(out + 16); B2.store_le(out + 32); B3.store_le(out + 48); } /* * SIMD Serpent Decryption of 4 blocks in parallel */ void serpent_decrypt_4(const byte in[64], byte out[64], const u32bit keys[132]) { SIMD_32 B0 = SIMD_32::load_le(in); SIMD_32 B1 = SIMD_32::load_le(in + 16); SIMD_32 B2 = SIMD_32::load_le(in + 32); SIMD_32 B3 = SIMD_32::load_le(in + 48); SIMD_32::transpose(B0, B1, B2, B3); key_xor(32,B0,B1,B2,B3); SBoxD8(B0,B1,B2,B3); key_xor(31,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD7(B0,B1,B2,B3); key_xor(30,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD6(B0,B1,B2,B3); key_xor(29,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD5(B0,B1,B2,B3); key_xor(28,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD4(B0,B1,B2,B3); key_xor(27,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD3(B0,B1,B2,B3); key_xor(26,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD2(B0,B1,B2,B3); key_xor(25,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD1(B0,B1,B2,B3); key_xor(24,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD8(B0,B1,B2,B3); key_xor(23,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD7(B0,B1,B2,B3); key_xor(22,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD6(B0,B1,B2,B3); key_xor(21,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD5(B0,B1,B2,B3); key_xor(20,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD4(B0,B1,B2,B3); key_xor(19,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD3(B0,B1,B2,B3); key_xor(18,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD2(B0,B1,B2,B3); key_xor(17,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD1(B0,B1,B2,B3); key_xor(16,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD8(B0,B1,B2,B3); key_xor(15,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD7(B0,B1,B2,B3); key_xor(14,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD6(B0,B1,B2,B3); key_xor(13,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD5(B0,B1,B2,B3); key_xor(12,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD4(B0,B1,B2,B3); key_xor(11,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD3(B0,B1,B2,B3); key_xor(10,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD2(B0,B1,B2,B3); key_xor( 9,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD1(B0,B1,B2,B3); key_xor( 8,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD8(B0,B1,B2,B3); key_xor( 7,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD7(B0,B1,B2,B3); key_xor( 6,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD6(B0,B1,B2,B3); key_xor( 5,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD5(B0,B1,B2,B3); key_xor( 4,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD4(B0,B1,B2,B3); key_xor( 3,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD3(B0,B1,B2,B3); key_xor( 2,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD2(B0,B1,B2,B3); key_xor( 1,B0,B1,B2,B3); i_transform(B0,B1,B2,B3); SBoxD1(B0,B1,B2,B3); key_xor( 0,B0,B1,B2,B3); SIMD_32::transpose(B0, B1, B2, B3); B0.store_le(out); B1.store_le(out + 16); B2.store_le(out + 32); B3.store_le(out + 48); } } #undef key_xor #undef transform #undef i_transform #undef SBoxE1 #undef SBoxE2 #undef SBoxE3 #undef SBoxE4 #undef SBoxE5 #undef SBoxE6 #undef SBoxE7 #undef SBoxE8 #undef SBoxD1 #undef SBoxD2 #undef SBoxD3 #undef SBoxD4 #undef SBoxD5 #undef SBoxD6 #undef SBoxD7 #undef SBoxD8 /* * Serpent Encryption */ void Serpent_SIMD::encrypt_n(const byte in[], byte out[], size_t blocks) const { const u32bit* KS = &(this->get_round_keys()[0]); while(blocks >= 4) { serpent_encrypt_4(in, out, KS); in += 4 * BLOCK_SIZE; out += 4 * BLOCK_SIZE; blocks -= 4; } if(blocks) Serpent::encrypt_n(in, out, blocks); } /* * Serpent Decryption */ void Serpent_SIMD::decrypt_n(const byte in[], byte out[], size_t blocks) const { const u32bit* KS = &(this->get_round_keys()[0]); while(blocks >= 4) { serpent_decrypt_4(in, out, KS); in += 4 * BLOCK_SIZE; out += 4 * BLOCK_SIZE; blocks -= 4; } if(blocks) Serpent::decrypt_n(in, out, blocks); } } /* * Skipjack * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { /* * Skipjack Stepping Rule 'A' */ void step_A(u16bit& W1, u16bit& W4, size_t round, const byte FTAB[]) { byte G1 = get_byte(0, W1), G2 = get_byte(1, W1), G3; G3 = FTAB[((4*round-4)%10)*256 + G2] ^ G1; G1 = FTAB[((4*round-3)%10)*256 + G3] ^ G2; G2 = FTAB[((4*round-2)%10)*256 + G1] ^ G3; G3 = FTAB[((4*round-1)%10)*256 + G2] ^ G1; W1 = make_u16bit(G2, G3); W4 ^= W1 ^ round; } /* * Skipjack Stepping Rule 'B' */ void step_B(u16bit& W1, u16bit& W2, size_t round, const byte FTAB[]) { W2 ^= W1 ^ round; byte G1 = get_byte(0, W1), G2 = get_byte(1, W1), G3; G3 = FTAB[((4*round-4)%10)*256 + G2] ^ G1; G1 = FTAB[((4*round-3)%10)*256 + G3] ^ G2; G2 = FTAB[((4*round-2)%10)*256 + G1] ^ G3; G3 = FTAB[((4*round-1)%10)*256 + G2] ^ G1; W1 = make_u16bit(G2, G3); } /* * Skipjack Invserse Stepping Rule 'A' */ void step_Ai(u16bit& W1, u16bit& W2, size_t round, const byte FTAB[]) { W1 ^= W2 ^ round; byte G1 = get_byte(1, W2), G2 = get_byte(0, W2), G3; G3 = FTAB[((4 * round - 1) % 10)*256 + G2] ^ G1; G1 = FTAB[((4 * round - 2) % 10)*256 + G3] ^ G2; G2 = FTAB[((4 * round - 3) % 10)*256 + G1] ^ G3; G3 = FTAB[((4 * round - 4) % 10)*256 + G2] ^ G1; W2 = make_u16bit(G3, G2); } /* * Skipjack Invserse Stepping Rule 'B' */ void step_Bi(u16bit& W2, u16bit& W3, size_t round, const byte FTAB[]) { byte G1 = get_byte(1, W2), G2 = get_byte(0, W2), G3; G3 = FTAB[((4 * round - 1) % 10)*256 + G2] ^ G1; G1 = FTAB[((4 * round - 2) % 10)*256 + G3] ^ G2; G2 = FTAB[((4 * round - 3) % 10)*256 + G1] ^ G3; G3 = FTAB[((4 * round - 4) % 10)*256 + G2] ^ G1; W2 = make_u16bit(G3, G2); W3 ^= W2 ^ round; } } /* * Skipjack Encryption */ void Skipjack::encrypt_n(const byte in[], byte out[], size_t blocks) const { const byte* ftab = &FTAB[0]; for(size_t i = 0; i != blocks; ++i) { u16bit W1 = load_le<u16bit>(in, 3); u16bit W2 = load_le<u16bit>(in, 2); u16bit W3 = load_le<u16bit>(in, 1); u16bit W4 = load_le<u16bit>(in, 0); step_A(W1, W4, 1, ftab); step_A(W4, W3, 2, ftab); step_A(W3, W2, 3, ftab); step_A(W2, W1, 4, ftab); step_A(W1, W4, 5, ftab); step_A(W4, W3, 6, ftab); step_A(W3, W2, 7, ftab); step_A(W2, W1, 8, ftab); step_B(W1, W2, 9, ftab); step_B(W4, W1, 10, ftab); step_B(W3, W4, 11, ftab); step_B(W2, W3, 12, ftab); step_B(W1, W2, 13, ftab); step_B(W4, W1, 14, ftab); step_B(W3, W4, 15, ftab); step_B(W2, W3, 16, ftab); step_A(W1, W4, 17, ftab); step_A(W4, W3, 18, ftab); step_A(W3, W2, 19, ftab); step_A(W2, W1, 20, ftab); step_A(W1, W4, 21, ftab); step_A(W4, W3, 22, ftab); step_A(W3, W2, 23, ftab); step_A(W2, W1, 24, ftab); step_B(W1, W2, 25, ftab); step_B(W4, W1, 26, ftab); step_B(W3, W4, 27, ftab); step_B(W2, W3, 28, ftab); step_B(W1, W2, 29, ftab); step_B(W4, W1, 30, ftab); step_B(W3, W4, 31, ftab); step_B(W2, W3, 32, ftab); store_le(out, W4, W3, W2, W1); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * Skipjack Decryption */ void Skipjack::decrypt_n(const byte in[], byte out[], size_t blocks) const { const byte* ftab = &FTAB[0]; for(size_t i = 0; i != blocks; ++i) { u16bit W1 = load_le<u16bit>(in, 3); u16bit W2 = load_le<u16bit>(in, 2); u16bit W3 = load_le<u16bit>(in, 1); u16bit W4 = load_le<u16bit>(in, 0); step_Bi(W2, W3, 32, ftab); step_Bi(W3, W4, 31, ftab); step_Bi(W4, W1, 30, ftab); step_Bi(W1, W2, 29, ftab); step_Bi(W2, W3, 28, ftab); step_Bi(W3, W4, 27, ftab); step_Bi(W4, W1, 26, ftab); step_Bi(W1, W2, 25, ftab); step_Ai(W1, W2, 24, ftab); step_Ai(W2, W3, 23, ftab); step_Ai(W3, W4, 22, ftab); step_Ai(W4, W1, 21, ftab); step_Ai(W1, W2, 20, ftab); step_Ai(W2, W3, 19, ftab); step_Ai(W3, W4, 18, ftab); step_Ai(W4, W1, 17, ftab); step_Bi(W2, W3, 16, ftab); step_Bi(W3, W4, 15, ftab); step_Bi(W4, W1, 14, ftab); step_Bi(W1, W2, 13, ftab); step_Bi(W2, W3, 12, ftab); step_Bi(W3, W4, 11, ftab); step_Bi(W4, W1, 10, ftab); step_Bi(W1, W2, 9, ftab); step_Ai(W1, W2, 8, ftab); step_Ai(W2, W3, 7, ftab); step_Ai(W3, W4, 6, ftab); step_Ai(W4, W1, 5, ftab); step_Ai(W1, W2, 4, ftab); step_Ai(W2, W3, 3, ftab); step_Ai(W3, W4, 2, ftab); step_Ai(W4, W1, 1, ftab); store_le(out, W4, W3, W2, W1); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * Skipjack Key Schedule */ void Skipjack::key_schedule(const byte key[], size_t) { static const byte F[256] = { 0xA3, 0xD7, 0x09, 0x83, 0xF8, 0x48, 0xF6, 0xF4, 0xB3, 0x21, 0x15, 0x78, 0x99, 0xB1, 0xAF, 0xF9, 0xE7, 0x2D, 0x4D, 0x8A, 0xCE, 0x4C, 0xCA, 0x2E, 0x52, 0x95, 0xD9, 0x1E, 0x4E, 0x38, 0x44, 0x28, 0x0A, 0xDF, 0x02, 0xA0, 0x17, 0xF1, 0x60, 0x68, 0x12, 0xB7, 0x7A, 0xC3, 0xE9, 0xFA, 0x3D, 0x53, 0x96, 0x84, 0x6B, 0xBA, 0xF2, 0x63, 0x9A, 0x19, 0x7C, 0xAE, 0xE5, 0xF5, 0xF7, 0x16, 0x6A, 0xA2, 0x39, 0xB6, 0x7B, 0x0F, 0xC1, 0x93, 0x81, 0x1B, 0xEE, 0xB4, 0x1A, 0xEA, 0xD0, 0x91, 0x2F, 0xB8, 0x55, 0xB9, 0xDA, 0x85, 0x3F, 0x41, 0xBF, 0xE0, 0x5A, 0x58, 0x80, 0x5F, 0x66, 0x0B, 0xD8, 0x90, 0x35, 0xD5, 0xC0, 0xA7, 0x33, 0x06, 0x65, 0x69, 0x45, 0x00, 0x94, 0x56, 0x6D, 0x98, 0x9B, 0x76, 0x97, 0xFC, 0xB2, 0xC2, 0xB0, 0xFE, 0xDB, 0x20, 0xE1, 0xEB, 0xD6, 0xE4, 0xDD, 0x47, 0x4A, 0x1D, 0x42, 0xED, 0x9E, 0x6E, 0x49, 0x3C, 0xCD, 0x43, 0x27, 0xD2, 0x07, 0xD4, 0xDE, 0xC7, 0x67, 0x18, 0x89, 0xCB, 0x30, 0x1F, 0x8D, 0xC6, 0x8F, 0xAA, 0xC8, 0x74, 0xDC, 0xC9, 0x5D, 0x5C, 0x31, 0xA4, 0x70, 0x88, 0x61, 0x2C, 0x9F, 0x0D, 0x2B, 0x87, 0x50, 0x82, 0x54, 0x64, 0x26, 0x7D, 0x03, 0x40, 0x34, 0x4B, 0x1C, 0x73, 0xD1, 0xC4, 0xFD, 0x3B, 0xCC, 0xFB, 0x7F, 0xAB, 0xE6, 0x3E, 0x5B, 0xA5, 0xAD, 0x04, 0x23, 0x9C, 0x14, 0x51, 0x22, 0xF0, 0x29, 0x79, 0x71, 0x7E, 0xFF, 0x8C, 0x0E, 0xE2, 0x0C, 0xEF, 0xBC, 0x72, 0x75, 0x6F, 0x37, 0xA1, 0xEC, 0xD3, 0x8E, 0x62, 0x8B, 0x86, 0x10, 0xE8, 0x08, 0x77, 0x11, 0xBE, 0x92, 0x4F, 0x24, 0xC5, 0x32, 0x36, 0x9D, 0xCF, 0xF3, 0xA6, 0xBB, 0xAC, 0x5E, 0x6C, 0xA9, 0x13, 0x57, 0x25, 0xB5, 0xE3, 0xBD, 0xA8, 0x3A, 0x01, 0x05, 0x59, 0x2A, 0x46 }; for(size_t i = 0; i != 10; ++i) for(size_t j = 0; j != 256; ++j) FTAB[256*i+j] = F[j ^ key[9-i]]; } /* * Clear memory of sensitive data */ void Skipjack::clear() { zeroise(FTAB); } } /* * S-Box and Diffusion Tables for Square * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { const byte Square::SE[256] = { 0xB1, 0xCE, 0xC3, 0x95, 0x5A, 0xAD, 0xE7, 0x02, 0x4D, 0x44, 0xFB, 0x91, 0x0C, 0x87, 0xA1, 0x50, 0xCB, 0x67, 0x54, 0xDD, 0x46, 0x8F, 0xE1, 0x4E, 0xF0, 0xFD, 0xFC, 0xEB, 0xF9, 0xC4, 0x1A, 0x6E, 0x5E, 0xF5, 0xCC, 0x8D, 0x1C, 0x56, 0x43, 0xFE, 0x07, 0x61, 0xF8, 0x75, 0x59, 0xFF, 0x03, 0x22, 0x8A, 0xD1, 0x13, 0xEE, 0x88, 0x00, 0x0E, 0x34, 0x15, 0x80, 0x94, 0xE3, 0xED, 0xB5, 0x53, 0x23, 0x4B, 0x47, 0x17, 0xA7, 0x90, 0x35, 0xAB, 0xD8, 0xB8, 0xDF, 0x4F, 0x57, 0x9A, 0x92, 0xDB, 0x1B, 0x3C, 0xC8, 0x99, 0x04, 0x8E, 0xE0, 0xD7, 0x7D, 0x85, 0xBB, 0x40, 0x2C, 0x3A, 0x45, 0xF1, 0x42, 0x65, 0x20, 0x41, 0x18, 0x72, 0x25, 0x93, 0x70, 0x36, 0x05, 0xF2, 0x0B, 0xA3, 0x79, 0xEC, 0x08, 0x27, 0x31, 0x32, 0xB6, 0x7C, 0xB0, 0x0A, 0x73, 0x5B, 0x7B, 0xB7, 0x81, 0xD2, 0x0D, 0x6A, 0x26, 0x9E, 0x58, 0x9C, 0x83, 0x74, 0xB3, 0xAC, 0x30, 0x7A, 0x69, 0x77, 0x0F, 0xAE, 0x21, 0xDE, 0xD0, 0x2E, 0x97, 0x10, 0xA4, 0x98, 0xA8, 0xD4, 0x68, 0x2D, 0x62, 0x29, 0x6D, 0x16, 0x49, 0x76, 0xC7, 0xE8, 0xC1, 0x96, 0x37, 0xE5, 0xCA, 0xF4, 0xE9, 0x63, 0x12, 0xC2, 0xA6, 0x14, 0xBC, 0xD3, 0x28, 0xAF, 0x2F, 0xE6, 0x24, 0x52, 0xC6, 0xA0, 0x09, 0xBD, 0x8C, 0xCF, 0x5D, 0x11, 0x5F, 0x01, 0xC5, 0x9F, 0x3D, 0xA2, 0x9B, 0xC9, 0x3B, 0xBE, 0x51, 0x19, 0x1F, 0x3F, 0x5C, 0xB2, 0xEF, 0x4A, 0xCD, 0xBF, 0xBA, 0x6F, 0x64, 0xD9, 0xF3, 0x3E, 0xB4, 0xAA, 0xDC, 0xD5, 0x06, 0xC0, 0x7E, 0xF6, 0x66, 0x6C, 0x84, 0x71, 0x38, 0xB9, 0x1D, 0x7F, 0x9D, 0x48, 0x8B, 0x2A, 0xDA, 0xA5, 0x33, 0x82, 0x39, 0xD6, 0x78, 0x86, 0xFA, 0xE4, 0x2B, 0xA9, 0x1E, 0x89, 0x60, 0x6B, 0xEA, 0x55, 0x4C, 0xF7, 0xE2 }; const byte Square::SD[256] = { 0x35, 0xBE, 0x07, 0x2E, 0x53, 0x69, 0xDB, 0x28, 0x6F, 0xB7, 0x76, 0x6B, 0x0C, 0x7D, 0x36, 0x8B, 0x92, 0xBC, 0xA9, 0x32, 0xAC, 0x38, 0x9C, 0x42, 0x63, 0xC8, 0x1E, 0x4F, 0x24, 0xE5, 0xF7, 0xC9, 0x61, 0x8D, 0x2F, 0x3F, 0xB3, 0x65, 0x7F, 0x70, 0xAF, 0x9A, 0xEA, 0xF5, 0x5B, 0x98, 0x90, 0xB1, 0x87, 0x71, 0x72, 0xED, 0x37, 0x45, 0x68, 0xA3, 0xE3, 0xEF, 0x5C, 0xC5, 0x50, 0xC1, 0xD6, 0xCA, 0x5A, 0x62, 0x5F, 0x26, 0x09, 0x5D, 0x14, 0x41, 0xE8, 0x9D, 0xCE, 0x40, 0xFD, 0x08, 0x17, 0x4A, 0x0F, 0xC7, 0xB4, 0x3E, 0x12, 0xFC, 0x25, 0x4B, 0x81, 0x2C, 0x04, 0x78, 0xCB, 0xBB, 0x20, 0xBD, 0xF9, 0x29, 0x99, 0xA8, 0xD3, 0x60, 0xDF, 0x11, 0x97, 0x89, 0x7E, 0xFA, 0xE0, 0x9B, 0x1F, 0xD2, 0x67, 0xE2, 0x64, 0x77, 0x84, 0x2B, 0x9E, 0x8A, 0xF1, 0x6D, 0x88, 0x79, 0x74, 0x57, 0xDD, 0xE6, 0x39, 0x7B, 0xEE, 0x83, 0xE1, 0x58, 0xF2, 0x0D, 0x34, 0xF8, 0x30, 0xE9, 0xB9, 0x23, 0x54, 0x15, 0x44, 0x0B, 0x4D, 0x66, 0x3A, 0x03, 0xA2, 0x91, 0x94, 0x52, 0x4C, 0xC3, 0x82, 0xE7, 0x80, 0xC0, 0xB6, 0x0E, 0xC2, 0x6C, 0x93, 0xEC, 0xAB, 0x43, 0x95, 0xF6, 0xD8, 0x46, 0x86, 0x05, 0x8C, 0xB0, 0x75, 0x00, 0xCC, 0x85, 0xD7, 0x3D, 0x73, 0x7A, 0x48, 0xE4, 0xD1, 0x59, 0xAD, 0xB8, 0xC6, 0xD0, 0xDC, 0xA1, 0xAA, 0x02, 0x1D, 0xBF, 0xB5, 0x9F, 0x51, 0xC4, 0xA5, 0x10, 0x22, 0xCF, 0x01, 0xBA, 0x8F, 0x31, 0x7C, 0xAE, 0x96, 0xDA, 0xF0, 0x56, 0x47, 0xD4, 0xEB, 0x4E, 0xD9, 0x13, 0x8E, 0x49, 0x55, 0x16, 0xFF, 0x3B, 0xF4, 0xA4, 0xB2, 0x06, 0xA0, 0xA7, 0xFB, 0x1B, 0x6E, 0x3C, 0x33, 0xCD, 0x18, 0x5E, 0x6A, 0xD5, 0xA6, 0x21, 0xDE, 0xFE, 0x2A, 0x1C, 0xF3, 0x0A, 0x1A, 0x19, 0x27, 0x2D }; const byte Square::Log[256] = { 0x00, 0x00, 0x01, 0x86, 0x02, 0x0D, 0x87, 0x4C, 0x03, 0xD2, 0x0E, 0xAE, 0x88, 0x22, 0x4D, 0x93, 0x04, 0x1A, 0xD3, 0xCB, 0x0F, 0x98, 0xAF, 0xA8, 0x89, 0xF0, 0x23, 0x59, 0x4E, 0x35, 0x94, 0x09, 0x05, 0x8F, 0x1B, 0x6E, 0xD4, 0x39, 0xCC, 0xBB, 0x10, 0x68, 0x99, 0x77, 0xB0, 0xDF, 0xA9, 0x72, 0x8A, 0xFA, 0xF1, 0xA0, 0x24, 0x52, 0x5A, 0x60, 0x4F, 0x2F, 0x36, 0xDC, 0x95, 0x32, 0x0A, 0x1F, 0x06, 0xA5, 0x90, 0x49, 0x1C, 0x5D, 0x6F, 0xB8, 0xD5, 0xC1, 0x3A, 0xB5, 0xCD, 0x63, 0xBC, 0x3D, 0x11, 0x44, 0x69, 0x81, 0x9A, 0x27, 0x78, 0xC4, 0xB1, 0xE6, 0xE0, 0xEA, 0xAA, 0x55, 0x73, 0xD8, 0x8B, 0xF6, 0xFB, 0x16, 0xF2, 0xF4, 0xA1, 0x40, 0x25, 0x42, 0x53, 0xE4, 0x5B, 0xA3, 0x61, 0xBF, 0x50, 0xF8, 0x30, 0x2D, 0x37, 0x8D, 0xDD, 0x66, 0x96, 0x18, 0x33, 0xEE, 0x0B, 0xFD, 0x20, 0xD0, 0x07, 0x57, 0xA6, 0xC9, 0x91, 0xAC, 0x4A, 0x84, 0x1D, 0xDA, 0x5E, 0x9E, 0x70, 0x75, 0xB9, 0x6C, 0xD6, 0xE8, 0xC2, 0x7F, 0x3B, 0xB3, 0xB6, 0x47, 0xCE, 0xEC, 0x64, 0x2B, 0xBD, 0xE2, 0x3E, 0x14, 0x12, 0x29, 0x45, 0x7D, 0x6A, 0x9C, 0x82, 0xC7, 0x9B, 0xC6, 0x28, 0x7C, 0x79, 0x7A, 0xC5, 0x7B, 0xB2, 0x46, 0xE7, 0x7E, 0xE1, 0x13, 0xEB, 0x2A, 0xAB, 0x83, 0x56, 0xC8, 0x74, 0x6B, 0xD9, 0x9D, 0x8C, 0x65, 0xF7, 0x2C, 0xFC, 0xCF, 0x17, 0xED, 0xF3, 0x3F, 0xF5, 0x15, 0xA2, 0xBE, 0x41, 0xE3, 0x26, 0xC3, 0x43, 0x80, 0x54, 0xD7, 0xE5, 0xE9, 0x5C, 0xB7, 0xA4, 0x48, 0x62, 0x3C, 0xC0, 0xB4, 0x51, 0x5F, 0xF9, 0x9F, 0x31, 0x1E, 0x2E, 0xDB, 0x38, 0xBA, 0x8E, 0x6D, 0xDE, 0x71, 0x67, 0x76, 0x97, 0xA7, 0x19, 0xCA, 0x34, 0x08, 0xEF, 0x58, 0x0C, 0x4B, 0xFE, 0x85, 0x21, 0x92, 0xD1, 0xAD }; const byte Square::ALog[255] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xF5, 0x1F, 0x3E, 0x7C, 0xF8, 0x05, 0x0A, 0x14, 0x28, 0x50, 0xA0, 0xB5, 0x9F, 0xCB, 0x63, 0xC6, 0x79, 0xF2, 0x11, 0x22, 0x44, 0x88, 0xE5, 0x3F, 0x7E, 0xFC, 0x0D, 0x1A, 0x34, 0x68, 0xD0, 0x55, 0xAA, 0xA1, 0xB7, 0x9B, 0xC3, 0x73, 0xE6, 0x39, 0x72, 0xE4, 0x3D, 0x7A, 0xF4, 0x1D, 0x3A, 0x74, 0xE8, 0x25, 0x4A, 0x94, 0xDD, 0x4F, 0x9E, 0xC9, 0x67, 0xCE, 0x69, 0xD2, 0x51, 0xA2, 0xB1, 0x97, 0xDB, 0x43, 0x86, 0xF9, 0x07, 0x0E, 0x1C, 0x38, 0x70, 0xE0, 0x35, 0x6A, 0xD4, 0x5D, 0xBA, 0x81, 0xF7, 0x1B, 0x36, 0x6C, 0xD8, 0x45, 0x8A, 0xE1, 0x37, 0x6E, 0xDC, 0x4D, 0x9A, 0xC1, 0x77, 0xEE, 0x29, 0x52, 0xA4, 0xBD, 0x8F, 0xEB, 0x23, 0x46, 0x8C, 0xED, 0x2F, 0x5E, 0xBC, 0x8D, 0xEF, 0x2B, 0x56, 0xAC, 0xAD, 0xAF, 0xAB, 0xA3, 0xB3, 0x93, 0xD3, 0x53, 0xA6, 0xB9, 0x87, 0xFB, 0x03, 0x06, 0x0C, 0x18, 0x30, 0x60, 0xC0, 0x75, 0xEA, 0x21, 0x42, 0x84, 0xFD, 0x0F, 0x1E, 0x3C, 0x78, 0xF0, 0x15, 0x2A, 0x54, 0xA8, 0xA5, 0xBF, 0x8B, 0xE3, 0x33, 0x66, 0xCC, 0x6D, 0xDA, 0x41, 0x82, 0xF1, 0x17, 0x2E, 0x5C, 0xB8, 0x85, 0xFF, 0x0B, 0x16, 0x2C, 0x58, 0xB0, 0x95, 0xDF, 0x4B, 0x96, 0xD9, 0x47, 0x8E, 0xE9, 0x27, 0x4E, 0x9C, 0xCD, 0x6F, 0xDE, 0x49, 0x92, 0xD1, 0x57, 0xAE, 0xA9, 0xA7, 0xBB, 0x83, 0xF3, 0x13, 0x26, 0x4C, 0x98, 0xC5, 0x7F, 0xFE, 0x09, 0x12, 0x24, 0x48, 0x90, 0xD5, 0x5F, 0xBE, 0x89, 0xE7, 0x3B, 0x76, 0xEC, 0x2D, 0x5A, 0xB4, 0x9D, 0xCF, 0x6B, 0xD6, 0x59, 0xB2, 0x91, 0xD7, 0x5B, 0xB6, 0x99, 0xC7, 0x7B, 0xF6, 0x19, 0x32, 0x64, 0xC8, 0x65, 0xCA, 0x61, 0xC2, 0x71, 0xE2, 0x31, 0x62, 0xC4, 0x7D, 0xFA }; const u32bit Square::TE0[256] = { 0x97B1B126, 0x69CECEA7, 0x73C3C3B0, 0xDF95954A, 0xB45A5AEE, 0xAFADAD02, 0x3BE7E7DC, 0x04020206, 0x9A4D4DD7, 0x884444CC, 0x03FBFBF8, 0xD7919146, 0x180C0C14, 0xFB87877C, 0xB7A1A116, 0xA05050F0, 0x63CBCBA8, 0xCE6767A9, 0xA85454FC, 0x4FDDDD92, 0x8C4646CA, 0xEB8F8F64, 0x37E1E1D6, 0x9C4E4ED2, 0x15F0F0E5, 0x0FFDFDF2, 0x0DFCFCF1, 0x23EBEBC8, 0x07F9F9FE, 0x7DC4C4B9, 0x341A1A2E, 0xDC6E6EB2, 0xBC5E5EE2, 0x1FF5F5EA, 0x6DCCCCA1, 0xEF8D8D62, 0x381C1C24, 0xAC5656FA, 0x864343C5, 0x09FEFEF7, 0x0E070709, 0xC26161A3, 0x05F8F8FD, 0xEA75759F, 0xB25959EB, 0x0BFFFFF4, 0x06030305, 0x44222266, 0xE18A8A6B, 0x57D1D186, 0x26131335, 0x29EEEEC7, 0xE588886D, 0x00000000, 0x1C0E0E12, 0x6834345C, 0x2A15153F, 0xF5808075, 0xDD949449, 0x33E3E3D0, 0x2FEDEDC2, 0x9FB5B52A, 0xA65353F5, 0x46232365, 0x964B4BDD, 0x8E4747C9, 0x2E171739, 0xBBA7A71C, 0xD5909045, 0x6A35355F, 0xA3ABAB08, 0x45D8D89D, 0x85B8B83D, 0x4BDFDF94, 0x9E4F4FD1, 0xAE5757F9, 0xC19A9A5B, 0xD1929243, 0x43DBDB98, 0x361B1B2D, 0x783C3C44, 0x65C8C8AD, 0xC799995E, 0x0804040C, 0xE98E8E67, 0x35E0E0D5, 0x5BD7D78C, 0xFA7D7D87, 0xFF85857A, 0x83BBBB38, 0x804040C0, 0x582C2C74, 0x743A3A4E, 0x8A4545CF, 0x17F1F1E6, 0x844242C6, 0xCA6565AF, 0x40202060, 0x824141C3, 0x30181828, 0xE4727296, 0x4A25256F, 0xD3939340, 0xE0707090, 0x6C36365A, 0x0A05050F, 0x11F2F2E3, 0x160B0B1D, 0xB3A3A310, 0xF279798B, 0x2DECECC1, 0x10080818, 0x4E272769, 0x62313153, 0x64323256, 0x99B6B62F, 0xF87C7C84, 0x95B0B025, 0x140A0A1E, 0xE6737395, 0xB65B5BED, 0xF67B7B8D, 0x9BB7B72C, 0xF7818176, 0x51D2D283, 0x1A0D0D17, 0xD46A6ABE, 0x4C26266A, 0xC99E9E57, 0xB05858E8, 0xCD9C9C51, 0xF3838370, 0xE874749C, 0x93B3B320, 0xADACAC01, 0x60303050, 0xF47A7A8E, 0xD26969BB, 0xEE777799, 0x1E0F0F11, 0xA9AEAE07, 0x42212163, 0x49DEDE97, 0x55D0D085, 0x5C2E2E72, 0xDB97974C, 0x20101030, 0xBDA4A419, 0xC598985D, 0xA5A8A80D, 0x5DD4D489, 0xD06868B8, 0x5A2D2D77, 0xC46262A6, 0x5229297B, 0xDA6D6DB7, 0x2C16163A, 0x924949DB, 0xEC76769A, 0x7BC7C7BC, 0x25E8E8CD, 0x77C1C1B6, 0xD996964F, 0x6E373759, 0x3FE5E5DA, 0x61CACAAB, 0x1DF4F4E9, 0x27E9E9CE, 0xC66363A5, 0x24121236, 0x71C2C2B3, 0xB9A6A61F, 0x2814143C, 0x8DBCBC31, 0x53D3D380, 0x50282878, 0xABAFAF04, 0x5E2F2F71, 0x39E6E6DF, 0x4824246C, 0xA45252F6, 0x79C6C6BF, 0xB5A0A015, 0x1209091B, 0x8FBDBD32, 0xED8C8C61, 0x6BCFCFA4, 0xBA5D5DE7, 0x22111133, 0xBE5F5FE1, 0x02010103, 0x7FC5C5BA, 0xCB9F9F54, 0x7A3D3D47, 0xB1A2A213, 0xC39B9B58, 0x67C9C9AE, 0x763B3B4D, 0x89BEBE37, 0xA25151F3, 0x3219192B, 0x3E1F1F21, 0x7E3F3F41, 0xB85C5CE4, 0x91B2B223, 0x2BEFEFC4, 0x944A4ADE, 0x6FCDCDA2, 0x8BBFBF34, 0x81BABA3B, 0xDE6F6FB1, 0xC86464AC, 0x47D9D99E, 0x13F3F3E0, 0x7C3E3E42, 0x9DB4B429, 0xA1AAAA0B, 0x4DDCDC91, 0x5FD5D58A, 0x0C06060A, 0x75C0C0B5, 0xFC7E7E82, 0x19F6F6EF, 0xCC6666AA, 0xD86C6CB4, 0xFD848479, 0xE2717193, 0x70383848, 0x87B9B93E, 0x3A1D1D27, 0xFE7F7F81, 0xCF9D9D52, 0x904848D8, 0xE38B8B68, 0x542A2A7E, 0x41DADA9B, 0xBFA5A51A, 0x66333355, 0xF1828273, 0x7239394B, 0x59D6D68F, 0xF0787888, 0xF986867F, 0x01FAFAFB, 0x3DE4E4D9, 0x562B2B7D, 0xA7A9A90E, 0x3C1E1E22, 0xE789896E, 0xC06060A0, 0xD66B6BBD, 0x21EAEACB, 0xAA5555FF, 0x984C4CD4, 0x1BF7F7EC, 0x31E2E2D3 }; const u32bit Square::TE1[256] = { 0x2697B1B1, 0xA769CECE, 0xB073C3C3, 0x4ADF9595, 0xEEB45A5A, 0x02AFADAD, 0xDC3BE7E7, 0x06040202, 0xD79A4D4D, 0xCC884444, 0xF803FBFB, 0x46D79191, 0x14180C0C, 0x7CFB8787, 0x16B7A1A1, 0xF0A05050, 0xA863CBCB, 0xA9CE6767, 0xFCA85454, 0x924FDDDD, 0xCA8C4646, 0x64EB8F8F, 0xD637E1E1, 0xD29C4E4E, 0xE515F0F0, 0xF20FFDFD, 0xF10DFCFC, 0xC823EBEB, 0xFE07F9F9, 0xB97DC4C4, 0x2E341A1A, 0xB2DC6E6E, 0xE2BC5E5E, 0xEA1FF5F5, 0xA16DCCCC, 0x62EF8D8D, 0x24381C1C, 0xFAAC5656, 0xC5864343, 0xF709FEFE, 0x090E0707, 0xA3C26161, 0xFD05F8F8, 0x9FEA7575, 0xEBB25959, 0xF40BFFFF, 0x05060303, 0x66442222, 0x6BE18A8A, 0x8657D1D1, 0x35261313, 0xC729EEEE, 0x6DE58888, 0x00000000, 0x121C0E0E, 0x5C683434, 0x3F2A1515, 0x75F58080, 0x49DD9494, 0xD033E3E3, 0xC22FEDED, 0x2A9FB5B5, 0xF5A65353, 0x65462323, 0xDD964B4B, 0xC98E4747, 0x392E1717, 0x1CBBA7A7, 0x45D59090, 0x5F6A3535, 0x08A3ABAB, 0x9D45D8D8, 0x3D85B8B8, 0x944BDFDF, 0xD19E4F4F, 0xF9AE5757, 0x5BC19A9A, 0x43D19292, 0x9843DBDB, 0x2D361B1B, 0x44783C3C, 0xAD65C8C8, 0x5EC79999, 0x0C080404, 0x67E98E8E, 0xD535E0E0, 0x8C5BD7D7, 0x87FA7D7D, 0x7AFF8585, 0x3883BBBB, 0xC0804040, 0x74582C2C, 0x4E743A3A, 0xCF8A4545, 0xE617F1F1, 0xC6844242, 0xAFCA6565, 0x60402020, 0xC3824141, 0x28301818, 0x96E47272, 0x6F4A2525, 0x40D39393, 0x90E07070, 0x5A6C3636, 0x0F0A0505, 0xE311F2F2, 0x1D160B0B, 0x10B3A3A3, 0x8BF27979, 0xC12DECEC, 0x18100808, 0x694E2727, 0x53623131, 0x56643232, 0x2F99B6B6, 0x84F87C7C, 0x2595B0B0, 0x1E140A0A, 0x95E67373, 0xEDB65B5B, 0x8DF67B7B, 0x2C9BB7B7, 0x76F78181, 0x8351D2D2, 0x171A0D0D, 0xBED46A6A, 0x6A4C2626, 0x57C99E9E, 0xE8B05858, 0x51CD9C9C, 0x70F38383, 0x9CE87474, 0x2093B3B3, 0x01ADACAC, 0x50603030, 0x8EF47A7A, 0xBBD26969, 0x99EE7777, 0x111E0F0F, 0x07A9AEAE, 0x63422121, 0x9749DEDE, 0x8555D0D0, 0x725C2E2E, 0x4CDB9797, 0x30201010, 0x19BDA4A4, 0x5DC59898, 0x0DA5A8A8, 0x895DD4D4, 0xB8D06868, 0x775A2D2D, 0xA6C46262, 0x7B522929, 0xB7DA6D6D, 0x3A2C1616, 0xDB924949, 0x9AEC7676, 0xBC7BC7C7, 0xCD25E8E8, 0xB677C1C1, 0x4FD99696, 0x596E3737, 0xDA3FE5E5, 0xAB61CACA, 0xE91DF4F4, 0xCE27E9E9, 0xA5C66363, 0x36241212, 0xB371C2C2, 0x1FB9A6A6, 0x3C281414, 0x318DBCBC, 0x8053D3D3, 0x78502828, 0x04ABAFAF, 0x715E2F2F, 0xDF39E6E6, 0x6C482424, 0xF6A45252, 0xBF79C6C6, 0x15B5A0A0, 0x1B120909, 0x328FBDBD, 0x61ED8C8C, 0xA46BCFCF, 0xE7BA5D5D, 0x33221111, 0xE1BE5F5F, 0x03020101, 0xBA7FC5C5, 0x54CB9F9F, 0x477A3D3D, 0x13B1A2A2, 0x58C39B9B, 0xAE67C9C9, 0x4D763B3B, 0x3789BEBE, 0xF3A25151, 0x2B321919, 0x213E1F1F, 0x417E3F3F, 0xE4B85C5C, 0x2391B2B2, 0xC42BEFEF, 0xDE944A4A, 0xA26FCDCD, 0x348BBFBF, 0x3B81BABA, 0xB1DE6F6F, 0xACC86464, 0x9E47D9D9, 0xE013F3F3, 0x427C3E3E, 0x299DB4B4, 0x0BA1AAAA, 0x914DDCDC, 0x8A5FD5D5, 0x0A0C0606, 0xB575C0C0, 0x82FC7E7E, 0xEF19F6F6, 0xAACC6666, 0xB4D86C6C, 0x79FD8484, 0x93E27171, 0x48703838, 0x3E87B9B9, 0x273A1D1D, 0x81FE7F7F, 0x52CF9D9D, 0xD8904848, 0x68E38B8B, 0x7E542A2A, 0x9B41DADA, 0x1ABFA5A5, 0x55663333, 0x73F18282, 0x4B723939, 0x8F59D6D6, 0x88F07878, 0x7FF98686, 0xFB01FAFA, 0xD93DE4E4, 0x7D562B2B, 0x0EA7A9A9, 0x223C1E1E, 0x6EE78989, 0xA0C06060, 0xBDD66B6B, 0xCB21EAEA, 0xFFAA5555, 0xD4984C4C, 0xEC1BF7F7, 0xD331E2E2 }; const u32bit Square::TE2[256] = { 0xB12697B1, 0xCEA769CE, 0xC3B073C3, 0x954ADF95, 0x5AEEB45A, 0xAD02AFAD, 0xE7DC3BE7, 0x02060402, 0x4DD79A4D, 0x44CC8844, 0xFBF803FB, 0x9146D791, 0x0C14180C, 0x877CFB87, 0xA116B7A1, 0x50F0A050, 0xCBA863CB, 0x67A9CE67, 0x54FCA854, 0xDD924FDD, 0x46CA8C46, 0x8F64EB8F, 0xE1D637E1, 0x4ED29C4E, 0xF0E515F0, 0xFDF20FFD, 0xFCF10DFC, 0xEBC823EB, 0xF9FE07F9, 0xC4B97DC4, 0x1A2E341A, 0x6EB2DC6E, 0x5EE2BC5E, 0xF5EA1FF5, 0xCCA16DCC, 0x8D62EF8D, 0x1C24381C, 0x56FAAC56, 0x43C58643, 0xFEF709FE, 0x07090E07, 0x61A3C261, 0xF8FD05F8, 0x759FEA75, 0x59EBB259, 0xFFF40BFF, 0x03050603, 0x22664422, 0x8A6BE18A, 0xD18657D1, 0x13352613, 0xEEC729EE, 0x886DE588, 0x00000000, 0x0E121C0E, 0x345C6834, 0x153F2A15, 0x8075F580, 0x9449DD94, 0xE3D033E3, 0xEDC22FED, 0xB52A9FB5, 0x53F5A653, 0x23654623, 0x4BDD964B, 0x47C98E47, 0x17392E17, 0xA71CBBA7, 0x9045D590, 0x355F6A35, 0xAB08A3AB, 0xD89D45D8, 0xB83D85B8, 0xDF944BDF, 0x4FD19E4F, 0x57F9AE57, 0x9A5BC19A, 0x9243D192, 0xDB9843DB, 0x1B2D361B, 0x3C44783C, 0xC8AD65C8, 0x995EC799, 0x040C0804, 0x8E67E98E, 0xE0D535E0, 0xD78C5BD7, 0x7D87FA7D, 0x857AFF85, 0xBB3883BB, 0x40C08040, 0x2C74582C, 0x3A4E743A, 0x45CF8A45, 0xF1E617F1, 0x42C68442, 0x65AFCA65, 0x20604020, 0x41C38241, 0x18283018, 0x7296E472, 0x256F4A25, 0x9340D393, 0x7090E070, 0x365A6C36, 0x050F0A05, 0xF2E311F2, 0x0B1D160B, 0xA310B3A3, 0x798BF279, 0xECC12DEC, 0x08181008, 0x27694E27, 0x31536231, 0x32566432, 0xB62F99B6, 0x7C84F87C, 0xB02595B0, 0x0A1E140A, 0x7395E673, 0x5BEDB65B, 0x7B8DF67B, 0xB72C9BB7, 0x8176F781, 0xD28351D2, 0x0D171A0D, 0x6ABED46A, 0x266A4C26, 0x9E57C99E, 0x58E8B058, 0x9C51CD9C, 0x8370F383, 0x749CE874, 0xB32093B3, 0xAC01ADAC, 0x30506030, 0x7A8EF47A, 0x69BBD269, 0x7799EE77, 0x0F111E0F, 0xAE07A9AE, 0x21634221, 0xDE9749DE, 0xD08555D0, 0x2E725C2E, 0x974CDB97, 0x10302010, 0xA419BDA4, 0x985DC598, 0xA80DA5A8, 0xD4895DD4, 0x68B8D068, 0x2D775A2D, 0x62A6C462, 0x297B5229, 0x6DB7DA6D, 0x163A2C16, 0x49DB9249, 0x769AEC76, 0xC7BC7BC7, 0xE8CD25E8, 0xC1B677C1, 0x964FD996, 0x37596E37, 0xE5DA3FE5, 0xCAAB61CA, 0xF4E91DF4, 0xE9CE27E9, 0x63A5C663, 0x12362412, 0xC2B371C2, 0xA61FB9A6, 0x143C2814, 0xBC318DBC, 0xD38053D3, 0x28785028, 0xAF04ABAF, 0x2F715E2F, 0xE6DF39E6, 0x246C4824, 0x52F6A452, 0xC6BF79C6, 0xA015B5A0, 0x091B1209, 0xBD328FBD, 0x8C61ED8C, 0xCFA46BCF, 0x5DE7BA5D, 0x11332211, 0x5FE1BE5F, 0x01030201, 0xC5BA7FC5, 0x9F54CB9F, 0x3D477A3D, 0xA213B1A2, 0x9B58C39B, 0xC9AE67C9, 0x3B4D763B, 0xBE3789BE, 0x51F3A251, 0x192B3219, 0x1F213E1F, 0x3F417E3F, 0x5CE4B85C, 0xB22391B2, 0xEFC42BEF, 0x4ADE944A, 0xCDA26FCD, 0xBF348BBF, 0xBA3B81BA, 0x6FB1DE6F, 0x64ACC864, 0xD99E47D9, 0xF3E013F3, 0x3E427C3E, 0xB4299DB4, 0xAA0BA1AA, 0xDC914DDC, 0xD58A5FD5, 0x060A0C06, 0xC0B575C0, 0x7E82FC7E, 0xF6EF19F6, 0x66AACC66, 0x6CB4D86C, 0x8479FD84, 0x7193E271, 0x38487038, 0xB93E87B9, 0x1D273A1D, 0x7F81FE7F, 0x9D52CF9D, 0x48D89048, 0x8B68E38B, 0x2A7E542A, 0xDA9B41DA, 0xA51ABFA5, 0x33556633, 0x8273F182, 0x394B7239, 0xD68F59D6, 0x7888F078, 0x867FF986, 0xFAFB01FA, 0xE4D93DE4, 0x2B7D562B, 0xA90EA7A9, 0x1E223C1E, 0x896EE789, 0x60A0C060, 0x6BBDD66B, 0xEACB21EA, 0x55FFAA55, 0x4CD4984C, 0xF7EC1BF7, 0xE2D331E2 }; const u32bit Square::TE3[256] = { 0xB1B12697, 0xCECEA769, 0xC3C3B073, 0x95954ADF, 0x5A5AEEB4, 0xADAD02AF, 0xE7E7DC3B, 0x02020604, 0x4D4DD79A, 0x4444CC88, 0xFBFBF803, 0x919146D7, 0x0C0C1418, 0x87877CFB, 0xA1A116B7, 0x5050F0A0, 0xCBCBA863, 0x6767A9CE, 0x5454FCA8, 0xDDDD924F, 0x4646CA8C, 0x8F8F64EB, 0xE1E1D637, 0x4E4ED29C, 0xF0F0E515, 0xFDFDF20F, 0xFCFCF10D, 0xEBEBC823, 0xF9F9FE07, 0xC4C4B97D, 0x1A1A2E34, 0x6E6EB2DC, 0x5E5EE2BC, 0xF5F5EA1F, 0xCCCCA16D, 0x8D8D62EF, 0x1C1C2438, 0x5656FAAC, 0x4343C586, 0xFEFEF709, 0x0707090E, 0x6161A3C2, 0xF8F8FD05, 0x75759FEA, 0x5959EBB2, 0xFFFFF40B, 0x03030506, 0x22226644, 0x8A8A6BE1, 0xD1D18657, 0x13133526, 0xEEEEC729, 0x88886DE5, 0x00000000, 0x0E0E121C, 0x34345C68, 0x15153F2A, 0x808075F5, 0x949449DD, 0xE3E3D033, 0xEDEDC22F, 0xB5B52A9F, 0x5353F5A6, 0x23236546, 0x4B4BDD96, 0x4747C98E, 0x1717392E, 0xA7A71CBB, 0x909045D5, 0x35355F6A, 0xABAB08A3, 0xD8D89D45, 0xB8B83D85, 0xDFDF944B, 0x4F4FD19E, 0x5757F9AE, 0x9A9A5BC1, 0x929243D1, 0xDBDB9843, 0x1B1B2D36, 0x3C3C4478, 0xC8C8AD65, 0x99995EC7, 0x04040C08, 0x8E8E67E9, 0xE0E0D535, 0xD7D78C5B, 0x7D7D87FA, 0x85857AFF, 0xBBBB3883, 0x4040C080, 0x2C2C7458, 0x3A3A4E74, 0x4545CF8A, 0xF1F1E617, 0x4242C684, 0x6565AFCA, 0x20206040, 0x4141C382, 0x18182830, 0x727296E4, 0x25256F4A, 0x939340D3, 0x707090E0, 0x36365A6C, 0x05050F0A, 0xF2F2E311, 0x0B0B1D16, 0xA3A310B3, 0x79798BF2, 0xECECC12D, 0x08081810, 0x2727694E, 0x31315362, 0x32325664, 0xB6B62F99, 0x7C7C84F8, 0xB0B02595, 0x0A0A1E14, 0x737395E6, 0x5B5BEDB6, 0x7B7B8DF6, 0xB7B72C9B, 0x818176F7, 0xD2D28351, 0x0D0D171A, 0x6A6ABED4, 0x26266A4C, 0x9E9E57C9, 0x5858E8B0, 0x9C9C51CD, 0x838370F3, 0x74749CE8, 0xB3B32093, 0xACAC01AD, 0x30305060, 0x7A7A8EF4, 0x6969BBD2, 0x777799EE, 0x0F0F111E, 0xAEAE07A9, 0x21216342, 0xDEDE9749, 0xD0D08555, 0x2E2E725C, 0x97974CDB, 0x10103020, 0xA4A419BD, 0x98985DC5, 0xA8A80DA5, 0xD4D4895D, 0x6868B8D0, 0x2D2D775A, 0x6262A6C4, 0x29297B52, 0x6D6DB7DA, 0x16163A2C, 0x4949DB92, 0x76769AEC, 0xC7C7BC7B, 0xE8E8CD25, 0xC1C1B677, 0x96964FD9, 0x3737596E, 0xE5E5DA3F, 0xCACAAB61, 0xF4F4E91D, 0xE9E9CE27, 0x6363A5C6, 0x12123624, 0xC2C2B371, 0xA6A61FB9, 0x14143C28, 0xBCBC318D, 0xD3D38053, 0x28287850, 0xAFAF04AB, 0x2F2F715E, 0xE6E6DF39, 0x24246C48, 0x5252F6A4, 0xC6C6BF79, 0xA0A015B5, 0x09091B12, 0xBDBD328F, 0x8C8C61ED, 0xCFCFA46B, 0x5D5DE7BA, 0x11113322, 0x5F5FE1BE, 0x01010302, 0xC5C5BA7F, 0x9F9F54CB, 0x3D3D477A, 0xA2A213B1, 0x9B9B58C3, 0xC9C9AE67, 0x3B3B4D76, 0xBEBE3789, 0x5151F3A2, 0x19192B32, 0x1F1F213E, 0x3F3F417E, 0x5C5CE4B8, 0xB2B22391, 0xEFEFC42B, 0x4A4ADE94, 0xCDCDA26F, 0xBFBF348B, 0xBABA3B81, 0x6F6FB1DE, 0x6464ACC8, 0xD9D99E47, 0xF3F3E013, 0x3E3E427C, 0xB4B4299D, 0xAAAA0BA1, 0xDCDC914D, 0xD5D58A5F, 0x06060A0C, 0xC0C0B575, 0x7E7E82FC, 0xF6F6EF19, 0x6666AACC, 0x6C6CB4D8, 0x848479FD, 0x717193E2, 0x38384870, 0xB9B93E87, 0x1D1D273A, 0x7F7F81FE, 0x9D9D52CF, 0x4848D890, 0x8B8B68E3, 0x2A2A7E54, 0xDADA9B41, 0xA5A51ABF, 0x33335566, 0x828273F1, 0x39394B72, 0xD6D68F59, 0x787888F0, 0x86867FF9, 0xFAFAFB01, 0xE4E4D93D, 0x2B2B7D56, 0xA9A90EA7, 0x1E1E223C, 0x89896EE7, 0x6060A0C0, 0x6B6BBDD6, 0xEAEACB21, 0x5555FFAA, 0x4C4CD498, 0xF7F7EC1B, 0xE2E2D331 }; const u32bit Square::TD0[256] = { 0xE368BC02, 0x5585620C, 0x2A3F2331, 0x61AB13F7, 0x98D46D72, 0x21CB9A19, 0x3C22A461, 0x459D3DCD, 0x05FDB423, 0x2BC4075F, 0x9B2C01C0, 0x3DD9800F, 0x486C5C74, 0xF97F7E85, 0xF173AB1F, 0xB6EDDE0E, 0x283C6BED, 0x4997781A, 0x9F2A918D, 0xC9579F33, 0xA907A8AA, 0xA50DED7D, 0x7C422D8F, 0x764DB0C9, 0x4D91E857, 0xCEA963CC, 0xB4EE96D2, 0x3028E1B6, 0x0DF161B9, 0xBD196726, 0x419BAD80, 0xC0A06EC7, 0x5183F241, 0x92DBF034, 0x6FA21EFC, 0x8F32CE4C, 0x13E03373, 0x69A7C66D, 0xE56D6493, 0xBF1A2FFA, 0xBB1CBFB7, 0x587403B5, 0xE76E2C4F, 0x5D89B796, 0xE89C052A, 0x446619A3, 0x342E71FB, 0x0FF22965, 0xFE81827A, 0xB11322F1, 0xA30835EC, 0xCD510F7E, 0xFF7AA614, 0x5C7293F8, 0x2FC29712, 0xF370E3C3, 0x992F491C, 0xD1431568, 0xC2A3261B, 0x88CC32B3, 0x8ACF7A6F, 0xB0E8069F, 0x7A47F51E, 0xD2BB79DA, 0xE6950821, 0x4398E55C, 0xD0B83106, 0x11E37BAF, 0x7E416553, 0xCCAA2B10, 0xD8B4E49C, 0x6456A7D4, 0xFB7C3659, 0x724B2084, 0xEA9F4DF6, 0x6A5FAADF, 0x2DC1DFCE, 0x70486858, 0xCAAFF381, 0x0605D891, 0x5A774B69, 0x94DE28A5, 0x39DF1042, 0x813BC347, 0xFC82CAA6, 0x23C8D2C5, 0x03F86CB2, 0x080CD59A, 0xDAB7AC40, 0x7DB909E1, 0x3824342C, 0xCF5247A2, 0xDCB274D1, 0x63A85B2B, 0x35D55595, 0x479E7511, 0x15E5EBE2, 0x4B9430C6, 0x4A6F14A8, 0x91239C86, 0x4C6ACC39, 0x5F8AFF4A, 0x0406904D, 0xEE99DDBB, 0x1E1152CA, 0xAAFFC418, 0xEB646998, 0x07FEFCFF, 0x8B345E01, 0x567D0EBE, 0xBAE79BD9, 0x4263C132, 0x75B5DC7B, 0x97264417, 0x67AECB66, 0x95250CCB, 0xEC9A9567, 0x57862AD0, 0x60503799, 0xB8E4D305, 0x65AD83BA, 0x19EFAE35, 0xA4F6C913, 0xC15B4AA9, 0x873E1BD6, 0xA0F0595E, 0x18148A5B, 0xAF02703B, 0xAB04E076, 0xDD4950BF, 0xDF4A1863, 0xC6A5B656, 0x853D530A, 0xFA871237, 0x77B694A7, 0x4665517F, 0xED61B109, 0x1BECE6E9, 0xD5458525, 0xF5753B52, 0x7FBA413D, 0x27CE4288, 0xB2EB4E43, 0xD6BDE997, 0x527B9EF3, 0x62537F45, 0x2C3AFBA0, 0x7BBCD170, 0xB91FF76B, 0x121B171D, 0xFD79EEC8, 0x3A277CF0, 0x0C0A45D7, 0x96DD6079, 0x2233F6AB, 0xACFA1C89, 0xC8ACBB5D, 0xA10B7D30, 0xD4BEA14B, 0xBEE10B94, 0x25CD0A54, 0x547E4662, 0xA2F31182, 0x17E6A33E, 0x263566E6, 0xC3580275, 0x83388B9B, 0x7844BDC2, 0x020348DC, 0x4F92A08B, 0x2E39B37C, 0x4E6984E5, 0xF0888F71, 0x362D3927, 0x9CD2FD3F, 0x01FB246E, 0x893716DD, 0x00000000, 0xF68D57E0, 0xE293986C, 0x744EF815, 0x9320D45A, 0xAD0138E7, 0xD3405DB4, 0x1A17C287, 0xB3106A2D, 0x5078D62F, 0xF48E1F3C, 0xA70EA5A1, 0x71B34C36, 0x9AD725AE, 0x5E71DB24, 0x161D8750, 0xEF62F9D5, 0x8D318690, 0x1C121A16, 0xA6F581CF, 0x5B8C6F07, 0x37D61D49, 0x6E593A92, 0x84C67764, 0x86C53FB8, 0xD746CDF9, 0xE090D0B0, 0x29C74F83, 0xE49640FD, 0x0E090D0B, 0x6DA15620, 0x8EC9EA22, 0xDB4C882E, 0xF776738E, 0xB515B2BC, 0x10185FC1, 0x322BA96A, 0x6BA48EB1, 0xAEF95455, 0x406089EE, 0x6655EF08, 0xE9672144, 0x3E21ECBD, 0x2030BE77, 0xF28BC7AD, 0x80C0E729, 0x141ECF8C, 0xBCE24348, 0xC4A6FE8A, 0x31D3C5D8, 0xB716FA60, 0x5380BA9D, 0xD94FC0F2, 0x1DE93E78, 0x24362E3A, 0xE16BF4DE, 0xCB54D7EF, 0x09F7F1F4, 0x82C3AFF5, 0x0BF4B928, 0x9D29D951, 0xC75E9238, 0xF8845AEB, 0x90D8B8E8, 0xDEB13C0D, 0x33D08D04, 0x685CE203, 0xC55DDAE4, 0x3BDC589E, 0x0A0F9D46, 0x3FDAC8D3, 0x598F27DB, 0xA8FC8CC4, 0x79BF99AC, 0x6C5A724E, 0x8CCAA2FE, 0x9ED1B5E3, 0x1FEA76A4, 0x73B004EA }; const u32bit Square::TD1[256] = { 0x02E368BC, 0x0C558562, 0x312A3F23, 0xF761AB13, 0x7298D46D, 0x1921CB9A, 0x613C22A4, 0xCD459D3D, 0x2305FDB4, 0x5F2BC407, 0xC09B2C01, 0x0F3DD980, 0x74486C5C, 0x85F97F7E, 0x1FF173AB, 0x0EB6EDDE, 0xED283C6B, 0x1A499778, 0x8D9F2A91, 0x33C9579F, 0xAAA907A8, 0x7DA50DED, 0x8F7C422D, 0xC9764DB0, 0x574D91E8, 0xCCCEA963, 0xD2B4EE96, 0xB63028E1, 0xB90DF161, 0x26BD1967, 0x80419BAD, 0xC7C0A06E, 0x415183F2, 0x3492DBF0, 0xFC6FA21E, 0x4C8F32CE, 0x7313E033, 0x6D69A7C6, 0x93E56D64, 0xFABF1A2F, 0xB7BB1CBF, 0xB5587403, 0x4FE76E2C, 0x965D89B7, 0x2AE89C05, 0xA3446619, 0xFB342E71, 0x650FF229, 0x7AFE8182, 0xF1B11322, 0xECA30835, 0x7ECD510F, 0x14FF7AA6, 0xF85C7293, 0x122FC297, 0xC3F370E3, 0x1C992F49, 0x68D14315, 0x1BC2A326, 0xB388CC32, 0x6F8ACF7A, 0x9FB0E806, 0x1E7A47F5, 0xDAD2BB79, 0x21E69508, 0x5C4398E5, 0x06D0B831, 0xAF11E37B, 0x537E4165, 0x10CCAA2B, 0x9CD8B4E4, 0xD46456A7, 0x59FB7C36, 0x84724B20, 0xF6EA9F4D, 0xDF6A5FAA, 0xCE2DC1DF, 0x58704868, 0x81CAAFF3, 0x910605D8, 0x695A774B, 0xA594DE28, 0x4239DF10, 0x47813BC3, 0xA6FC82CA, 0xC523C8D2, 0xB203F86C, 0x9A080CD5, 0x40DAB7AC, 0xE17DB909, 0x2C382434, 0xA2CF5247, 0xD1DCB274, 0x2B63A85B, 0x9535D555, 0x11479E75, 0xE215E5EB, 0xC64B9430, 0xA84A6F14, 0x8691239C, 0x394C6ACC, 0x4A5F8AFF, 0x4D040690, 0xBBEE99DD, 0xCA1E1152, 0x18AAFFC4, 0x98EB6469, 0xFF07FEFC, 0x018B345E, 0xBE567D0E, 0xD9BAE79B, 0x324263C1, 0x7B75B5DC, 0x17972644, 0x6667AECB, 0xCB95250C, 0x67EC9A95, 0xD057862A, 0x99605037, 0x05B8E4D3, 0xBA65AD83, 0x3519EFAE, 0x13A4F6C9, 0xA9C15B4A, 0xD6873E1B, 0x5EA0F059, 0x5B18148A, 0x3BAF0270, 0x76AB04E0, 0xBFDD4950, 0x63DF4A18, 0x56C6A5B6, 0x0A853D53, 0x37FA8712, 0xA777B694, 0x7F466551, 0x09ED61B1, 0xE91BECE6, 0x25D54585, 0x52F5753B, 0x3D7FBA41, 0x8827CE42, 0x43B2EB4E, 0x97D6BDE9, 0xF3527B9E, 0x4562537F, 0xA02C3AFB, 0x707BBCD1, 0x6BB91FF7, 0x1D121B17, 0xC8FD79EE, 0xF03A277C, 0xD70C0A45, 0x7996DD60, 0xAB2233F6, 0x89ACFA1C, 0x5DC8ACBB, 0x30A10B7D, 0x4BD4BEA1, 0x94BEE10B, 0x5425CD0A, 0x62547E46, 0x82A2F311, 0x3E17E6A3, 0xE6263566, 0x75C35802, 0x9B83388B, 0xC27844BD, 0xDC020348, 0x8B4F92A0, 0x7C2E39B3, 0xE54E6984, 0x71F0888F, 0x27362D39, 0x3F9CD2FD, 0x6E01FB24, 0xDD893716, 0x00000000, 0xE0F68D57, 0x6CE29398, 0x15744EF8, 0x5A9320D4, 0xE7AD0138, 0xB4D3405D, 0x871A17C2, 0x2DB3106A, 0x2F5078D6, 0x3CF48E1F, 0xA1A70EA5, 0x3671B34C, 0xAE9AD725, 0x245E71DB, 0x50161D87, 0xD5EF62F9, 0x908D3186, 0x161C121A, 0xCFA6F581, 0x075B8C6F, 0x4937D61D, 0x926E593A, 0x6484C677, 0xB886C53F, 0xF9D746CD, 0xB0E090D0, 0x8329C74F, 0xFDE49640, 0x0B0E090D, 0x206DA156, 0x228EC9EA, 0x2EDB4C88, 0x8EF77673, 0xBCB515B2, 0xC110185F, 0x6A322BA9, 0xB16BA48E, 0x55AEF954, 0xEE406089, 0x086655EF, 0x44E96721, 0xBD3E21EC, 0x772030BE, 0xADF28BC7, 0x2980C0E7, 0x8C141ECF, 0x48BCE243, 0x8AC4A6FE, 0xD831D3C5, 0x60B716FA, 0x9D5380BA, 0xF2D94FC0, 0x781DE93E, 0x3A24362E, 0xDEE16BF4, 0xEFCB54D7, 0xF409F7F1, 0xF582C3AF, 0x280BF4B9, 0x519D29D9, 0x38C75E92, 0xEBF8845A, 0xE890D8B8, 0x0DDEB13C, 0x0433D08D, 0x03685CE2, 0xE4C55DDA, 0x9E3BDC58, 0x460A0F9D, 0xD33FDAC8, 0xDB598F27, 0xC4A8FC8C, 0xAC79BF99, 0x4E6C5A72, 0xFE8CCAA2, 0xE39ED1B5, 0xA41FEA76, 0xEA73B004 }; const u32bit Square::TD2[256] = { 0xBC02E368, 0x620C5585, 0x23312A3F, 0x13F761AB, 0x6D7298D4, 0x9A1921CB, 0xA4613C22, 0x3DCD459D, 0xB42305FD, 0x075F2BC4, 0x01C09B2C, 0x800F3DD9, 0x5C74486C, 0x7E85F97F, 0xAB1FF173, 0xDE0EB6ED, 0x6BED283C, 0x781A4997, 0x918D9F2A, 0x9F33C957, 0xA8AAA907, 0xED7DA50D, 0x2D8F7C42, 0xB0C9764D, 0xE8574D91, 0x63CCCEA9, 0x96D2B4EE, 0xE1B63028, 0x61B90DF1, 0x6726BD19, 0xAD80419B, 0x6EC7C0A0, 0xF2415183, 0xF03492DB, 0x1EFC6FA2, 0xCE4C8F32, 0x337313E0, 0xC66D69A7, 0x6493E56D, 0x2FFABF1A, 0xBFB7BB1C, 0x03B55874, 0x2C4FE76E, 0xB7965D89, 0x052AE89C, 0x19A34466, 0x71FB342E, 0x29650FF2, 0x827AFE81, 0x22F1B113, 0x35ECA308, 0x0F7ECD51, 0xA614FF7A, 0x93F85C72, 0x97122FC2, 0xE3C3F370, 0x491C992F, 0x1568D143, 0x261BC2A3, 0x32B388CC, 0x7A6F8ACF, 0x069FB0E8, 0xF51E7A47, 0x79DAD2BB, 0x0821E695, 0xE55C4398, 0x3106D0B8, 0x7BAF11E3, 0x65537E41, 0x2B10CCAA, 0xE49CD8B4, 0xA7D46456, 0x3659FB7C, 0x2084724B, 0x4DF6EA9F, 0xAADF6A5F, 0xDFCE2DC1, 0x68587048, 0xF381CAAF, 0xD8910605, 0x4B695A77, 0x28A594DE, 0x104239DF, 0xC347813B, 0xCAA6FC82, 0xD2C523C8, 0x6CB203F8, 0xD59A080C, 0xAC40DAB7, 0x09E17DB9, 0x342C3824, 0x47A2CF52, 0x74D1DCB2, 0x5B2B63A8, 0x559535D5, 0x7511479E, 0xEBE215E5, 0x30C64B94, 0x14A84A6F, 0x9C869123, 0xCC394C6A, 0xFF4A5F8A, 0x904D0406, 0xDDBBEE99, 0x52CA1E11, 0xC418AAFF, 0x6998EB64, 0xFCFF07FE, 0x5E018B34, 0x0EBE567D, 0x9BD9BAE7, 0xC1324263, 0xDC7B75B5, 0x44179726, 0xCB6667AE, 0x0CCB9525, 0x9567EC9A, 0x2AD05786, 0x37996050, 0xD305B8E4, 0x83BA65AD, 0xAE3519EF, 0xC913A4F6, 0x4AA9C15B, 0x1BD6873E, 0x595EA0F0, 0x8A5B1814, 0x703BAF02, 0xE076AB04, 0x50BFDD49, 0x1863DF4A, 0xB656C6A5, 0x530A853D, 0x1237FA87, 0x94A777B6, 0x517F4665, 0xB109ED61, 0xE6E91BEC, 0x8525D545, 0x3B52F575, 0x413D7FBA, 0x428827CE, 0x4E43B2EB, 0xE997D6BD, 0x9EF3527B, 0x7F456253, 0xFBA02C3A, 0xD1707BBC, 0xF76BB91F, 0x171D121B, 0xEEC8FD79, 0x7CF03A27, 0x45D70C0A, 0x607996DD, 0xF6AB2233, 0x1C89ACFA, 0xBB5DC8AC, 0x7D30A10B, 0xA14BD4BE, 0x0B94BEE1, 0x0A5425CD, 0x4662547E, 0x1182A2F3, 0xA33E17E6, 0x66E62635, 0x0275C358, 0x8B9B8338, 0xBDC27844, 0x48DC0203, 0xA08B4F92, 0xB37C2E39, 0x84E54E69, 0x8F71F088, 0x3927362D, 0xFD3F9CD2, 0x246E01FB, 0x16DD8937, 0x00000000, 0x57E0F68D, 0x986CE293, 0xF815744E, 0xD45A9320, 0x38E7AD01, 0x5DB4D340, 0xC2871A17, 0x6A2DB310, 0xD62F5078, 0x1F3CF48E, 0xA5A1A70E, 0x4C3671B3, 0x25AE9AD7, 0xDB245E71, 0x8750161D, 0xF9D5EF62, 0x86908D31, 0x1A161C12, 0x81CFA6F5, 0x6F075B8C, 0x1D4937D6, 0x3A926E59, 0x776484C6, 0x3FB886C5, 0xCDF9D746, 0xD0B0E090, 0x4F8329C7, 0x40FDE496, 0x0D0B0E09, 0x56206DA1, 0xEA228EC9, 0x882EDB4C, 0x738EF776, 0xB2BCB515, 0x5FC11018, 0xA96A322B, 0x8EB16BA4, 0x5455AEF9, 0x89EE4060, 0xEF086655, 0x2144E967, 0xECBD3E21, 0xBE772030, 0xC7ADF28B, 0xE72980C0, 0xCF8C141E, 0x4348BCE2, 0xFE8AC4A6, 0xC5D831D3, 0xFA60B716, 0xBA9D5380, 0xC0F2D94F, 0x3E781DE9, 0x2E3A2436, 0xF4DEE16B, 0xD7EFCB54, 0xF1F409F7, 0xAFF582C3, 0xB9280BF4, 0xD9519D29, 0x9238C75E, 0x5AEBF884, 0xB8E890D8, 0x3C0DDEB1, 0x8D0433D0, 0xE203685C, 0xDAE4C55D, 0x589E3BDC, 0x9D460A0F, 0xC8D33FDA, 0x27DB598F, 0x8CC4A8FC, 0x99AC79BF, 0x724E6C5A, 0xA2FE8CCA, 0xB5E39ED1, 0x76A41FEA, 0x04EA73B0 }; const u32bit Square::TD3[256] = { 0x68BC02E3, 0x85620C55, 0x3F23312A, 0xAB13F761, 0xD46D7298, 0xCB9A1921, 0x22A4613C, 0x9D3DCD45, 0xFDB42305, 0xC4075F2B, 0x2C01C09B, 0xD9800F3D, 0x6C5C7448, 0x7F7E85F9, 0x73AB1FF1, 0xEDDE0EB6, 0x3C6BED28, 0x97781A49, 0x2A918D9F, 0x579F33C9, 0x07A8AAA9, 0x0DED7DA5, 0x422D8F7C, 0x4DB0C976, 0x91E8574D, 0xA963CCCE, 0xEE96D2B4, 0x28E1B630, 0xF161B90D, 0x196726BD, 0x9BAD8041, 0xA06EC7C0, 0x83F24151, 0xDBF03492, 0xA21EFC6F, 0x32CE4C8F, 0xE0337313, 0xA7C66D69, 0x6D6493E5, 0x1A2FFABF, 0x1CBFB7BB, 0x7403B558, 0x6E2C4FE7, 0x89B7965D, 0x9C052AE8, 0x6619A344, 0x2E71FB34, 0xF229650F, 0x81827AFE, 0x1322F1B1, 0x0835ECA3, 0x510F7ECD, 0x7AA614FF, 0x7293F85C, 0xC297122F, 0x70E3C3F3, 0x2F491C99, 0x431568D1, 0xA3261BC2, 0xCC32B388, 0xCF7A6F8A, 0xE8069FB0, 0x47F51E7A, 0xBB79DAD2, 0x950821E6, 0x98E55C43, 0xB83106D0, 0xE37BAF11, 0x4165537E, 0xAA2B10CC, 0xB4E49CD8, 0x56A7D464, 0x7C3659FB, 0x4B208472, 0x9F4DF6EA, 0x5FAADF6A, 0xC1DFCE2D, 0x48685870, 0xAFF381CA, 0x05D89106, 0x774B695A, 0xDE28A594, 0xDF104239, 0x3BC34781, 0x82CAA6FC, 0xC8D2C523, 0xF86CB203, 0x0CD59A08, 0xB7AC40DA, 0xB909E17D, 0x24342C38, 0x5247A2CF, 0xB274D1DC, 0xA85B2B63, 0xD5559535, 0x9E751147, 0xE5EBE215, 0x9430C64B, 0x6F14A84A, 0x239C8691, 0x6ACC394C, 0x8AFF4A5F, 0x06904D04, 0x99DDBBEE, 0x1152CA1E, 0xFFC418AA, 0x646998EB, 0xFEFCFF07, 0x345E018B, 0x7D0EBE56, 0xE79BD9BA, 0x63C13242, 0xB5DC7B75, 0x26441797, 0xAECB6667, 0x250CCB95, 0x9A9567EC, 0x862AD057, 0x50379960, 0xE4D305B8, 0xAD83BA65, 0xEFAE3519, 0xF6C913A4, 0x5B4AA9C1, 0x3E1BD687, 0xF0595EA0, 0x148A5B18, 0x02703BAF, 0x04E076AB, 0x4950BFDD, 0x4A1863DF, 0xA5B656C6, 0x3D530A85, 0x871237FA, 0xB694A777, 0x65517F46, 0x61B109ED, 0xECE6E91B, 0x458525D5, 0x753B52F5, 0xBA413D7F, 0xCE428827, 0xEB4E43B2, 0xBDE997D6, 0x7B9EF352, 0x537F4562, 0x3AFBA02C, 0xBCD1707B, 0x1FF76BB9, 0x1B171D12, 0x79EEC8FD, 0x277CF03A, 0x0A45D70C, 0xDD607996, 0x33F6AB22, 0xFA1C89AC, 0xACBB5DC8, 0x0B7D30A1, 0xBEA14BD4, 0xE10B94BE, 0xCD0A5425, 0x7E466254, 0xF31182A2, 0xE6A33E17, 0x3566E626, 0x580275C3, 0x388B9B83, 0x44BDC278, 0x0348DC02, 0x92A08B4F, 0x39B37C2E, 0x6984E54E, 0x888F71F0, 0x2D392736, 0xD2FD3F9C, 0xFB246E01, 0x3716DD89, 0x00000000, 0x8D57E0F6, 0x93986CE2, 0x4EF81574, 0x20D45A93, 0x0138E7AD, 0x405DB4D3, 0x17C2871A, 0x106A2DB3, 0x78D62F50, 0x8E1F3CF4, 0x0EA5A1A7, 0xB34C3671, 0xD725AE9A, 0x71DB245E, 0x1D875016, 0x62F9D5EF, 0x3186908D, 0x121A161C, 0xF581CFA6, 0x8C6F075B, 0xD61D4937, 0x593A926E, 0xC6776484, 0xC53FB886, 0x46CDF9D7, 0x90D0B0E0, 0xC74F8329, 0x9640FDE4, 0x090D0B0E, 0xA156206D, 0xC9EA228E, 0x4C882EDB, 0x76738EF7, 0x15B2BCB5, 0x185FC110, 0x2BA96A32, 0xA48EB16B, 0xF95455AE, 0x6089EE40, 0x55EF0866, 0x672144E9, 0x21ECBD3E, 0x30BE7720, 0x8BC7ADF2, 0xC0E72980, 0x1ECF8C14, 0xE24348BC, 0xA6FE8AC4, 0xD3C5D831, 0x16FA60B7, 0x80BA9D53, 0x4FC0F2D9, 0xE93E781D, 0x362E3A24, 0x6BF4DEE1, 0x54D7EFCB, 0xF7F1F409, 0xC3AFF582, 0xF4B9280B, 0x29D9519D, 0x5E9238C7, 0x845AEBF8, 0xD8B8E890, 0xB13C0DDE, 0xD08D0433, 0x5CE20368, 0x5DDAE4C5, 0xDC589E3B, 0x0F9D460A, 0xDAC8D33F, 0x8F27DB59, 0xFC8CC4A8, 0xBF99AC79, 0x5A724E6C, 0xCAA2FE8C, 0xD1B5E39E, 0xEA76A41F, 0xB004EA73 }; } /* * Square * (C) 1999-2007 Jack Lloyd * * Based on the public domain reference implemenation * * Distributed under the terms of the Botan license */ namespace Botan { /* * Square Encryption */ void Square::encrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u32bit B0, B1, B2, B3; B0 = TE0[in[ 0] ^ ME[ 0]] ^ TE1[in[ 4] ^ ME[ 4]] ^ TE2[in[ 8] ^ ME[ 8]] ^ TE3[in[12] ^ ME[12]] ^ EK[0]; B1 = TE0[in[ 1] ^ ME[ 1]] ^ TE1[in[ 5] ^ ME[ 5]] ^ TE2[in[ 9] ^ ME[ 9]] ^ TE3[in[13] ^ ME[13]] ^ EK[1]; B2 = TE0[in[ 2] ^ ME[ 2]] ^ TE1[in[ 6] ^ ME[ 6]] ^ TE2[in[10] ^ ME[10]] ^ TE3[in[14] ^ ME[14]] ^ EK[2]; B3 = TE0[in[ 3] ^ ME[ 3]] ^ TE1[in[ 7] ^ ME[ 7]] ^ TE2[in[11] ^ ME[11]] ^ TE3[in[15] ^ ME[15]] ^ EK[3]; for(size_t j = 1; j != 7; j += 2) { u32bit T0, T1, T2, T3; T0 = TE0[get_byte(0, B0)] ^ TE1[get_byte(0, B1)] ^ TE2[get_byte(0, B2)] ^ TE3[get_byte(0, B3)] ^ EK[4*j+0]; T1 = TE0[get_byte(1, B0)] ^ TE1[get_byte(1, B1)] ^ TE2[get_byte(1, B2)] ^ TE3[get_byte(1, B3)] ^ EK[4*j+1]; T2 = TE0[get_byte(2, B0)] ^ TE1[get_byte(2, B1)] ^ TE2[get_byte(2, B2)] ^ TE3[get_byte(2, B3)] ^ EK[4*j+2]; T3 = TE0[get_byte(3, B0)] ^ TE1[get_byte(3, B1)] ^ TE2[get_byte(3, B2)] ^ TE3[get_byte(3, B3)] ^ EK[4*j+3]; B0 = TE0[get_byte(0, T0)] ^ TE1[get_byte(0, T1)] ^ TE2[get_byte(0, T2)] ^ TE3[get_byte(0, T3)] ^ EK[4*j+4]; B1 = TE0[get_byte(1, T0)] ^ TE1[get_byte(1, T1)] ^ TE2[get_byte(1, T2)] ^ TE3[get_byte(1, T3)] ^ EK[4*j+5]; B2 = TE0[get_byte(2, T0)] ^ TE1[get_byte(2, T1)] ^ TE2[get_byte(2, T2)] ^ TE3[get_byte(2, T3)] ^ EK[4*j+6]; B3 = TE0[get_byte(3, T0)] ^ TE1[get_byte(3, T1)] ^ TE2[get_byte(3, T2)] ^ TE3[get_byte(3, T3)] ^ EK[4*j+7]; } out[ 0] = SE[get_byte(0, B0)] ^ ME[16]; out[ 1] = SE[get_byte(0, B1)] ^ ME[17]; out[ 2] = SE[get_byte(0, B2)] ^ ME[18]; out[ 3] = SE[get_byte(0, B3)] ^ ME[19]; out[ 4] = SE[get_byte(1, B0)] ^ ME[20]; out[ 5] = SE[get_byte(1, B1)] ^ ME[21]; out[ 6] = SE[get_byte(1, B2)] ^ ME[22]; out[ 7] = SE[get_byte(1, B3)] ^ ME[23]; out[ 8] = SE[get_byte(2, B0)] ^ ME[24]; out[ 9] = SE[get_byte(2, B1)] ^ ME[25]; out[10] = SE[get_byte(2, B2)] ^ ME[26]; out[11] = SE[get_byte(2, B3)] ^ ME[27]; out[12] = SE[get_byte(3, B0)] ^ ME[28]; out[13] = SE[get_byte(3, B1)] ^ ME[29]; out[14] = SE[get_byte(3, B2)] ^ ME[30]; out[15] = SE[get_byte(3, B3)] ^ ME[31]; in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * Square Decryption */ void Square::decrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u32bit B0, B1, B2, B3; B0 = TD0[in[ 0] ^ MD[ 0]] ^ TD1[in[ 4] ^ MD[ 4]] ^ TD2[in[ 8] ^ MD[ 8]] ^ TD3[in[12] ^ MD[12]] ^ DK[0]; B1 = TD0[in[ 1] ^ MD[ 1]] ^ TD1[in[ 5] ^ MD[ 5]] ^ TD2[in[ 9] ^ MD[ 9]] ^ TD3[in[13] ^ MD[13]] ^ DK[1]; B2 = TD0[in[ 2] ^ MD[ 2]] ^ TD1[in[ 6] ^ MD[ 6]] ^ TD2[in[10] ^ MD[10]] ^ TD3[in[14] ^ MD[14]] ^ DK[2]; B3 = TD0[in[ 3] ^ MD[ 3]] ^ TD1[in[ 7] ^ MD[ 7]] ^ TD2[in[11] ^ MD[11]] ^ TD3[in[15] ^ MD[15]] ^ DK[3]; for(size_t j = 1; j != 7; j += 2) { u32bit T0, T1, T2, T3; T0 = TD0[get_byte(0, B0)] ^ TD1[get_byte(0, B1)] ^ TD2[get_byte(0, B2)] ^ TD3[get_byte(0, B3)] ^ DK[4*j+0]; T1 = TD0[get_byte(1, B0)] ^ TD1[get_byte(1, B1)] ^ TD2[get_byte(1, B2)] ^ TD3[get_byte(1, B3)] ^ DK[4*j+1]; T2 = TD0[get_byte(2, B0)] ^ TD1[get_byte(2, B1)] ^ TD2[get_byte(2, B2)] ^ TD3[get_byte(2, B3)] ^ DK[4*j+2]; T3 = TD0[get_byte(3, B0)] ^ TD1[get_byte(3, B1)] ^ TD2[get_byte(3, B2)] ^ TD3[get_byte(3, B3)] ^ DK[4*j+3]; B0 = TD0[get_byte(0, T0)] ^ TD1[get_byte(0, T1)] ^ TD2[get_byte(0, T2)] ^ TD3[get_byte(0, T3)] ^ DK[4*j+4]; B1 = TD0[get_byte(1, T0)] ^ TD1[get_byte(1, T1)] ^ TD2[get_byte(1, T2)] ^ TD3[get_byte(1, T3)] ^ DK[4*j+5]; B2 = TD0[get_byte(2, T0)] ^ TD1[get_byte(2, T1)] ^ TD2[get_byte(2, T2)] ^ TD3[get_byte(2, T3)] ^ DK[4*j+6]; B3 = TD0[get_byte(3, T0)] ^ TD1[get_byte(3, T1)] ^ TD2[get_byte(3, T2)] ^ TD3[get_byte(3, T3)] ^ DK[4*j+7]; } out[ 0] = SD[get_byte(0, B0)] ^ MD[16]; out[ 1] = SD[get_byte(0, B1)] ^ MD[17]; out[ 2] = SD[get_byte(0, B2)] ^ MD[18]; out[ 3] = SD[get_byte(0, B3)] ^ MD[19]; out[ 4] = SD[get_byte(1, B0)] ^ MD[20]; out[ 5] = SD[get_byte(1, B1)] ^ MD[21]; out[ 6] = SD[get_byte(1, B2)] ^ MD[22]; out[ 7] = SD[get_byte(1, B3)] ^ MD[23]; out[ 8] = SD[get_byte(2, B0)] ^ MD[24]; out[ 9] = SD[get_byte(2, B1)] ^ MD[25]; out[10] = SD[get_byte(2, B2)] ^ MD[26]; out[11] = SD[get_byte(2, B3)] ^ MD[27]; out[12] = SD[get_byte(3, B0)] ^ MD[28]; out[13] = SD[get_byte(3, B1)] ^ MD[29]; out[14] = SD[get_byte(3, B2)] ^ MD[30]; out[15] = SD[get_byte(3, B3)] ^ MD[31]; in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * Square Key Schedule */ void Square::key_schedule(const byte key[], size_t) { SecureVector<u32bit> XEK(36), XDK(36); for(size_t i = 0; i != 4; ++i) XEK[i] = load_be<u32bit>(key, i); for(size_t i = 0; i != 8; ++i) { XEK[4*i+4] = XEK[4*i ] ^ rotate_left(XEK[4*i+3], 8) ^ (0x01000000 << i); XEK[4*i+5] = XEK[4*i+1] ^ XEK[4*i+4]; XEK[4*i+6] = XEK[4*i+2] ^ XEK[4*i+5]; XEK[4*i+7] = XEK[4*i+3] ^ XEK[4*i+6]; for(size_t j = 0; j != 4; ++j) XDK[28 - 4*i + j] = XEK[4*(i+1)+j]; transform(&XEK[4*i]); } for(size_t i = 0; i != 4; ++i) for(size_t j = 0; j != 4; ++j) { ME[4*i+j ] = get_byte(j, XEK[i ]); ME[4*i+j+16] = get_byte(j, XEK[i+32]); MD[4*i+j ] = get_byte(j, XDK[i ]); MD[4*i+j+16] = get_byte(j, XEK[i ]); } EK.copy(&XEK[4], 28); DK.copy(&XDK[4], 28); } /* * Square's Inverse Linear Transformation */ void Square::transform(u32bit round_key[4]) { static const byte G[4][4] = { { 2, 1, 1, 3 }, { 3, 2, 1, 1 }, { 1, 3, 2, 1 }, { 1, 1, 3, 2 } }; for(size_t i = 0; i != 4; ++i) { byte A[4] = { 0 }, B[4] = { 0 }; store_be(round_key[i], A); for(size_t j = 0; j != 4; ++j) for(size_t k = 0; k != 4; ++k) { const byte a = A[k]; const byte b = G[k][j]; if(a && b) B[j] ^= ALog[(Log[a] + Log[b]) % 255]; } round_key[i] = load_be<u32bit>(B, 0); } } /* * Clear memory of sensitive data */ void Square::clear() { zeroise(EK); zeroise(DK); zeroise(ME); zeroise(MD); } } /* * TEA * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * TEA Encryption */ void TEA::encrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u32bit L = load_be<u32bit>(in, 0); u32bit R = load_be<u32bit>(in, 1); u32bit S = 0; for(size_t j = 0; j != 32; ++j) { S += 0x9E3779B9; L += ((R << 4) + K[0]) ^ (R + S) ^ ((R >> 5) + K[1]); R += ((L << 4) + K[2]) ^ (L + S) ^ ((L >> 5) + K[3]); } store_be(out, L, R); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * TEA Decryption */ void TEA::decrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u32bit L = load_be<u32bit>(in, 0); u32bit R = load_be<u32bit>(in, 1); u32bit S = 0xC6EF3720; for(size_t j = 0; j != 32; ++j) { R -= ((L << 4) + K[2]) ^ (L + S) ^ ((L >> 5) + K[3]); L -= ((R << 4) + K[0]) ^ (R + S) ^ ((R >> 5) + K[1]); S -= 0x9E3779B9; } store_be(out, L, R); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * TEA Key Schedule */ void TEA::key_schedule(const byte key[], size_t) { for(size_t i = 0; i != 4; ++i) K[i] = load_be<u32bit>(key, i); } } /* * S-Box and MDS Tables for Twofish * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { const byte Twofish::Q0[256] = { 0xA9, 0x67, 0xB3, 0xE8, 0x04, 0xFD, 0xA3, 0x76, 0x9A, 0x92, 0x80, 0x78, 0xE4, 0xDD, 0xD1, 0x38, 0x0D, 0xC6, 0x35, 0x98, 0x18, 0xF7, 0xEC, 0x6C, 0x43, 0x75, 0x37, 0x26, 0xFA, 0x13, 0x94, 0x48, 0xF2, 0xD0, 0x8B, 0x30, 0x84, 0x54, 0xDF, 0x23, 0x19, 0x5B, 0x3D, 0x59, 0xF3, 0xAE, 0xA2, 0x82, 0x63, 0x01, 0x83, 0x2E, 0xD9, 0x51, 0x9B, 0x7C, 0xA6, 0xEB, 0xA5, 0xBE, 0x16, 0x0C, 0xE3, 0x61, 0xC0, 0x8C, 0x3A, 0xF5, 0x73, 0x2C, 0x25, 0x0B, 0xBB, 0x4E, 0x89, 0x6B, 0x53, 0x6A, 0xB4, 0xF1, 0xE1, 0xE6, 0xBD, 0x45, 0xE2, 0xF4, 0xB6, 0x66, 0xCC, 0x95, 0x03, 0x56, 0xD4, 0x1C, 0x1E, 0xD7, 0xFB, 0xC3, 0x8E, 0xB5, 0xE9, 0xCF, 0xBF, 0xBA, 0xEA, 0x77, 0x39, 0xAF, 0x33, 0xC9, 0x62, 0x71, 0x81, 0x79, 0x09, 0xAD, 0x24, 0xCD, 0xF9, 0xD8, 0xE5, 0xC5, 0xB9, 0x4D, 0x44, 0x08, 0x86, 0xE7, 0xA1, 0x1D, 0xAA, 0xED, 0x06, 0x70, 0xB2, 0xD2, 0x41, 0x7B, 0xA0, 0x11, 0x31, 0xC2, 0x27, 0x90, 0x20, 0xF6, 0x60, 0xFF, 0x96, 0x5C, 0xB1, 0xAB, 0x9E, 0x9C, 0x52, 0x1B, 0x5F, 0x93, 0x0A, 0xEF, 0x91, 0x85, 0x49, 0xEE, 0x2D, 0x4F, 0x8F, 0x3B, 0x47, 0x87, 0x6D, 0x46, 0xD6, 0x3E, 0x69, 0x64, 0x2A, 0xCE, 0xCB, 0x2F, 0xFC, 0x97, 0x05, 0x7A, 0xAC, 0x7F, 0xD5, 0x1A, 0x4B, 0x0E, 0xA7, 0x5A, 0x28, 0x14, 0x3F, 0x29, 0x88, 0x3C, 0x4C, 0x02, 0xB8, 0xDA, 0xB0, 0x17, 0x55, 0x1F, 0x8A, 0x7D, 0x57, 0xC7, 0x8D, 0x74, 0xB7, 0xC4, 0x9F, 0x72, 0x7E, 0x15, 0x22, 0x12, 0x58, 0x07, 0x99, 0x34, 0x6E, 0x50, 0xDE, 0x68, 0x65, 0xBC, 0xDB, 0xF8, 0xC8, 0xA8, 0x2B, 0x40, 0xDC, 0xFE, 0x32, 0xA4, 0xCA, 0x10, 0x21, 0xF0, 0xD3, 0x5D, 0x0F, 0x00, 0x6F, 0x9D, 0x36, 0x42, 0x4A, 0x5E, 0xC1, 0xE0 }; const byte Twofish::Q1[256] = { 0x75, 0xF3, 0xC6, 0xF4, 0xDB, 0x7B, 0xFB, 0xC8, 0x4A, 0xD3, 0xE6, 0x6B, 0x45, 0x7D, 0xE8, 0x4B, 0xD6, 0x32, 0xD8, 0xFD, 0x37, 0x71, 0xF1, 0xE1, 0x30, 0x0F, 0xF8, 0x1B, 0x87, 0xFA, 0x06, 0x3F, 0x5E, 0xBA, 0xAE, 0x5B, 0x8A, 0x00, 0xBC, 0x9D, 0x6D, 0xC1, 0xB1, 0x0E, 0x80, 0x5D, 0xD2, 0xD5, 0xA0, 0x84, 0x07, 0x14, 0xB5, 0x90, 0x2C, 0xA3, 0xB2, 0x73, 0x4C, 0x54, 0x92, 0x74, 0x36, 0x51, 0x38, 0xB0, 0xBD, 0x5A, 0xFC, 0x60, 0x62, 0x96, 0x6C, 0x42, 0xF7, 0x10, 0x7C, 0x28, 0x27, 0x8C, 0x13, 0x95, 0x9C, 0xC7, 0x24, 0x46, 0x3B, 0x70, 0xCA, 0xE3, 0x85, 0xCB, 0x11, 0xD0, 0x93, 0xB8, 0xA6, 0x83, 0x20, 0xFF, 0x9F, 0x77, 0xC3, 0xCC, 0x03, 0x6F, 0x08, 0xBF, 0x40, 0xE7, 0x2B, 0xE2, 0x79, 0x0C, 0xAA, 0x82, 0x41, 0x3A, 0xEA, 0xB9, 0xE4, 0x9A, 0xA4, 0x97, 0x7E, 0xDA, 0x7A, 0x17, 0x66, 0x94, 0xA1, 0x1D, 0x3D, 0xF0, 0xDE, 0xB3, 0x0B, 0x72, 0xA7, 0x1C, 0xEF, 0xD1, 0x53, 0x3E, 0x8F, 0x33, 0x26, 0x5F, 0xEC, 0x76, 0x2A, 0x49, 0x81, 0x88, 0xEE, 0x21, 0xC4, 0x1A, 0xEB, 0xD9, 0xC5, 0x39, 0x99, 0xCD, 0xAD, 0x31, 0x8B, 0x01, 0x18, 0x23, 0xDD, 0x1F, 0x4E, 0x2D, 0xF9, 0x48, 0x4F, 0xF2, 0x65, 0x8E, 0x78, 0x5C, 0x58, 0x19, 0x8D, 0xE5, 0x98, 0x57, 0x67, 0x7F, 0x05, 0x64, 0xAF, 0x63, 0xB6, 0xFE, 0xF5, 0xB7, 0x3C, 0xA5, 0xCE, 0xE9, 0x68, 0x44, 0xE0, 0x4D, 0x43, 0x69, 0x29, 0x2E, 0xAC, 0x15, 0x59, 0xA8, 0x0A, 0x9E, 0x6E, 0x47, 0xDF, 0x34, 0x35, 0x6A, 0xCF, 0xDC, 0x22, 0xC9, 0xC0, 0x9B, 0x89, 0xD4, 0xED, 0xAB, 0x12, 0xA2, 0x0D, 0x52, 0xBB, 0x02, 0x2F, 0xA9, 0xD7, 0x61, 0x1E, 0xB4, 0x50, 0x04, 0xF6, 0xC2, 0x16, 0x25, 0x86, 0x56, 0x55, 0x09, 0xBE, 0x91 }; const byte Twofish::RS[32] = { 0x01, 0xA4, 0x02, 0xA4, 0xA4, 0x56, 0xA1, 0x55, 0x55, 0x82, 0xFC, 0x87, 0x87, 0xF3, 0xC1, 0x5A, 0x5A, 0x1E, 0x47, 0x58, 0x58, 0xC6, 0xAE, 0xDB, 0xDB, 0x68, 0x3D, 0x9E, 0x9E, 0xE5, 0x19, 0x03 }; const byte Twofish::EXP_TO_POLY[255] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x4D, 0x9A, 0x79, 0xF2, 0xA9, 0x1F, 0x3E, 0x7C, 0xF8, 0xBD, 0x37, 0x6E, 0xDC, 0xF5, 0xA7, 0x03, 0x06, 0x0C, 0x18, 0x30, 0x60, 0xC0, 0xCD, 0xD7, 0xE3, 0x8B, 0x5B, 0xB6, 0x21, 0x42, 0x84, 0x45, 0x8A, 0x59, 0xB2, 0x29, 0x52, 0xA4, 0x05, 0x0A, 0x14, 0x28, 0x50, 0xA0, 0x0D, 0x1A, 0x34, 0x68, 0xD0, 0xED, 0x97, 0x63, 0xC6, 0xC1, 0xCF, 0xD3, 0xEB, 0x9B, 0x7B, 0xF6, 0xA1, 0x0F, 0x1E, 0x3C, 0x78, 0xF0, 0xAD, 0x17, 0x2E, 0x5C, 0xB8, 0x3D, 0x7A, 0xF4, 0xA5, 0x07, 0x0E, 0x1C, 0x38, 0x70, 0xE0, 0x8D, 0x57, 0xAE, 0x11, 0x22, 0x44, 0x88, 0x5D, 0xBA, 0x39, 0x72, 0xE4, 0x85, 0x47, 0x8E, 0x51, 0xA2, 0x09, 0x12, 0x24, 0x48, 0x90, 0x6D, 0xDA, 0xF9, 0xBF, 0x33, 0x66, 0xCC, 0xD5, 0xE7, 0x83, 0x4B, 0x96, 0x61, 0xC2, 0xC9, 0xDF, 0xF3, 0xAB, 0x1B, 0x36, 0x6C, 0xD8, 0xFD, 0xB7, 0x23, 0x46, 0x8C, 0x55, 0xAA, 0x19, 0x32, 0x64, 0xC8, 0xDD, 0xF7, 0xA3, 0x0B, 0x16, 0x2C, 0x58, 0xB0, 0x2D, 0x5A, 0xB4, 0x25, 0x4A, 0x94, 0x65, 0xCA, 0xD9, 0xFF, 0xB3, 0x2B, 0x56, 0xAC, 0x15, 0x2A, 0x54, 0xA8, 0x1D, 0x3A, 0x74, 0xE8, 0x9D, 0x77, 0xEE, 0x91, 0x6F, 0xDE, 0xF1, 0xAF, 0x13, 0x26, 0x4C, 0x98, 0x7D, 0xFA, 0xB9, 0x3F, 0x7E, 0xFC, 0xB5, 0x27, 0x4E, 0x9C, 0x75, 0xEA, 0x99, 0x7F, 0xFE, 0xB1, 0x2F, 0x5E, 0xBC, 0x35, 0x6A, 0xD4, 0xE5, 0x87, 0x43, 0x86, 0x41, 0x82, 0x49, 0x92, 0x69, 0xD2, 0xE9, 0x9F, 0x73, 0xE6, 0x81, 0x4F, 0x9E, 0x71, 0xE2, 0x89, 0x5F, 0xBE, 0x31, 0x62, 0xC4, 0xC5, 0xC7, 0xC3, 0xCB, 0xDB, 0xFB, 0xBB, 0x3B, 0x76, 0xEC, 0x95, 0x67, 0xCE, 0xD1, 0xEF, 0x93, 0x6B, 0xD6, 0xE1, 0x8F, 0x53, 0xA6 }; const byte Twofish::POLY_TO_EXP[255] = { 0x00, 0x01, 0x17, 0x02, 0x2E, 0x18, 0x53, 0x03, 0x6A, 0x2F, 0x93, 0x19, 0x34, 0x54, 0x45, 0x04, 0x5C, 0x6B, 0xB6, 0x30, 0xA6, 0x94, 0x4B, 0x1A, 0x8C, 0x35, 0x81, 0x55, 0xAA, 0x46, 0x0D, 0x05, 0x24, 0x5D, 0x87, 0x6C, 0x9B, 0xB7, 0xC1, 0x31, 0x2B, 0xA7, 0xA3, 0x95, 0x98, 0x4C, 0xCA, 0x1B, 0xE6, 0x8D, 0x73, 0x36, 0xCD, 0x82, 0x12, 0x56, 0x62, 0xAB, 0xF0, 0x47, 0x4F, 0x0E, 0xBD, 0x06, 0xD4, 0x25, 0xD2, 0x5E, 0x27, 0x88, 0x66, 0x6D, 0xD6, 0x9C, 0x79, 0xB8, 0x08, 0xC2, 0xDF, 0x32, 0x68, 0x2C, 0xFD, 0xA8, 0x8A, 0xA4, 0x5A, 0x96, 0x29, 0x99, 0x22, 0x4D, 0x60, 0xCB, 0xE4, 0x1C, 0x7B, 0xE7, 0x3B, 0x8E, 0x9E, 0x74, 0xF4, 0x37, 0xD8, 0xCE, 0xF9, 0x83, 0x6F, 0x13, 0xB2, 0x57, 0xE1, 0x63, 0xDC, 0xAC, 0xC4, 0xF1, 0xAF, 0x48, 0x0A, 0x50, 0x42, 0x0F, 0xBA, 0xBE, 0xC7, 0x07, 0xDE, 0xD5, 0x78, 0x26, 0x65, 0xD3, 0xD1, 0x5F, 0xE3, 0x28, 0x21, 0x89, 0x59, 0x67, 0xFC, 0x6E, 0xB1, 0xD7, 0xF8, 0x9D, 0xF3, 0x7A, 0x3A, 0xB9, 0xC6, 0x09, 0x41, 0xC3, 0xAE, 0xE0, 0xDB, 0x33, 0x44, 0x69, 0x92, 0x2D, 0x52, 0xFE, 0x16, 0xA9, 0x0C, 0x8B, 0x80, 0xA5, 0x4A, 0x5B, 0xB5, 0x97, 0xC9, 0x2A, 0xA2, 0x9A, 0xC0, 0x23, 0x86, 0x4E, 0xBC, 0x61, 0xEF, 0xCC, 0x11, 0xE5, 0x72, 0x1D, 0x3D, 0x7C, 0xEB, 0xE8, 0xE9, 0x3C, 0xEA, 0x8F, 0x7D, 0x9F, 0xEC, 0x75, 0x1E, 0xF5, 0x3E, 0x38, 0xF6, 0xD9, 0x3F, 0xCF, 0x76, 0xFA, 0x1F, 0x84, 0xA0, 0x70, 0xED, 0x14, 0x90, 0xB3, 0x7E, 0x58, 0xFB, 0xE2, 0x20, 0x64, 0xD0, 0xDD, 0x77, 0xAD, 0xDA, 0xC5, 0x40, 0xF2, 0x39, 0xB0, 0xF7, 0x49, 0xB4, 0x0B, 0x7F, 0x51, 0x15, 0x43, 0x91, 0x10, 0x71, 0xBB, 0xEE, 0xBF, 0x85, 0xC8, 0xA1 }; const u32bit Twofish::MDS0[256] = { 0xBCBC3275, 0xECEC21F3, 0x202043C6, 0xB3B3C9F4, 0xDADA03DB, 0x02028B7B, 0xE2E22BFB, 0x9E9EFAC8, 0xC9C9EC4A, 0xD4D409D3, 0x18186BE6, 0x1E1E9F6B, 0x98980E45, 0xB2B2387D, 0xA6A6D2E8, 0x2626B74B, 0x3C3C57D6, 0x93938A32, 0x8282EED8, 0x525298FD, 0x7B7BD437, 0xBBBB3771, 0x5B5B97F1, 0x474783E1, 0x24243C30, 0x5151E20F, 0xBABAC6F8, 0x4A4AF31B, 0xBFBF4887, 0x0D0D70FA, 0xB0B0B306, 0x7575DE3F, 0xD2D2FD5E, 0x7D7D20BA, 0x666631AE, 0x3A3AA35B, 0x59591C8A, 0x00000000, 0xCDCD93BC, 0x1A1AE09D, 0xAEAE2C6D, 0x7F7FABC1, 0x2B2BC7B1, 0xBEBEB90E, 0xE0E0A080, 0x8A8A105D, 0x3B3B52D2, 0x6464BAD5, 0xD8D888A0, 0xE7E7A584, 0x5F5FE807, 0x1B1B1114, 0x2C2CC2B5, 0xFCFCB490, 0x3131272C, 0x808065A3, 0x73732AB2, 0x0C0C8173, 0x79795F4C, 0x6B6B4154, 0x4B4B0292, 0x53536974, 0x94948F36, 0x83831F51, 0x2A2A3638, 0xC4C49CB0, 0x2222C8BD, 0xD5D5F85A, 0xBDBDC3FC, 0x48487860, 0xFFFFCE62, 0x4C4C0796, 0x4141776C, 0xC7C7E642, 0xEBEB24F7, 0x1C1C1410, 0x5D5D637C, 0x36362228, 0x6767C027, 0xE9E9AF8C, 0x4444F913, 0x1414EA95, 0xF5F5BB9C, 0xCFCF18C7, 0x3F3F2D24, 0xC0C0E346, 0x7272DB3B, 0x54546C70, 0x29294CCA, 0xF0F035E3, 0x0808FE85, 0xC6C617CB, 0xF3F34F11, 0x8C8CE4D0, 0xA4A45993, 0xCACA96B8, 0x68683BA6, 0xB8B84D83, 0x38382820, 0xE5E52EFF, 0xADAD569F, 0x0B0B8477, 0xC8C81DC3, 0x9999FFCC, 0x5858ED03, 0x19199A6F, 0x0E0E0A08, 0x95957EBF, 0x70705040, 0xF7F730E7, 0x6E6ECF2B, 0x1F1F6EE2, 0xB5B53D79, 0x09090F0C, 0x616134AA, 0x57571682, 0x9F9F0B41, 0x9D9D803A, 0x111164EA, 0x2525CDB9, 0xAFAFDDE4, 0x4545089A, 0xDFDF8DA4, 0xA3A35C97, 0xEAEAD57E, 0x353558DA, 0xEDEDD07A, 0x4343FC17, 0xF8F8CB66, 0xFBFBB194, 0x3737D3A1, 0xFAFA401D, 0xC2C2683D, 0xB4B4CCF0, 0x32325DDE, 0x9C9C71B3, 0x5656E70B, 0xE3E3DA72, 0x878760A7, 0x15151B1C, 0xF9F93AEF, 0x6363BFD1, 0x3434A953, 0x9A9A853E, 0xB1B1428F, 0x7C7CD133, 0x88889B26, 0x3D3DA65F, 0xA1A1D7EC, 0xE4E4DF76, 0x8181942A, 0x91910149, 0x0F0FFB81, 0xEEEEAA88, 0x161661EE, 0xD7D77321, 0x9797F5C4, 0xA5A5A81A, 0xFEFE3FEB, 0x6D6DB5D9, 0x7878AEC5, 0xC5C56D39, 0x1D1DE599, 0x7676A4CD, 0x3E3EDCAD, 0xCBCB6731, 0xB6B6478B, 0xEFEF5B01, 0x12121E18, 0x6060C523, 0x6A6AB0DD, 0x4D4DF61F, 0xCECEE94E, 0xDEDE7C2D, 0x55559DF9, 0x7E7E5A48, 0x2121B24F, 0x03037AF2, 0xA0A02665, 0x5E5E198E, 0x5A5A6678, 0x65654B5C, 0x62624E58, 0xFDFD4519, 0x0606F48D, 0x404086E5, 0xF2F2BE98, 0x3333AC57, 0x17179067, 0x05058E7F, 0xE8E85E05, 0x4F4F7D64, 0x89896AAF, 0x10109563, 0x74742FB6, 0x0A0A75FE, 0x5C5C92F5, 0x9B9B74B7, 0x2D2D333C, 0x3030D6A5, 0x2E2E49CE, 0x494989E9, 0x46467268, 0x77775544, 0xA8A8D8E0, 0x9696044D, 0x2828BD43, 0xA9A92969, 0xD9D97929, 0x8686912E, 0xD1D187AC, 0xF4F44A15, 0x8D8D1559, 0xD6D682A8, 0xB9B9BC0A, 0x42420D9E, 0xF6F6C16E, 0x2F2FB847, 0xDDDD06DF, 0x23233934, 0xCCCC6235, 0xF1F1C46A, 0xC1C112CF, 0x8585EBDC, 0x8F8F9E22, 0x7171A1C9, 0x9090F0C0, 0xAAAA539B, 0x0101F189, 0x8B8BE1D4, 0x4E4E8CED, 0x8E8E6FAB, 0xABABA212, 0x6F6F3EA2, 0xE6E6540D, 0xDBDBF252, 0x92927BBB, 0xB7B7B602, 0x6969CA2F, 0x3939D9A9, 0xD3D30CD7, 0xA7A72361, 0xA2A2AD1E, 0xC3C399B4, 0x6C6C4450, 0x07070504, 0x04047FF6, 0x272746C2, 0xACACA716, 0xD0D07625, 0x50501386, 0xDCDCF756, 0x84841A55, 0xE1E15109, 0x7A7A25BE, 0x1313EF91 }; const u32bit Twofish::MDS1[256] = { 0xA9D93939, 0x67901717, 0xB3719C9C, 0xE8D2A6A6, 0x04050707, 0xFD985252, 0xA3658080, 0x76DFE4E4, 0x9A084545, 0x92024B4B, 0x80A0E0E0, 0x78665A5A, 0xE4DDAFAF, 0xDDB06A6A, 0xD1BF6363, 0x38362A2A, 0x0D54E6E6, 0xC6432020, 0x3562CCCC, 0x98BEF2F2, 0x181E1212, 0xF724EBEB, 0xECD7A1A1, 0x6C774141, 0x43BD2828, 0x7532BCBC, 0x37D47B7B, 0x269B8888, 0xFA700D0D, 0x13F94444, 0x94B1FBFB, 0x485A7E7E, 0xF27A0303, 0xD0E48C8C, 0x8B47B6B6, 0x303C2424, 0x84A5E7E7, 0x54416B6B, 0xDF06DDDD, 0x23C56060, 0x1945FDFD, 0x5BA33A3A, 0x3D68C2C2, 0x59158D8D, 0xF321ECEC, 0xAE316666, 0xA23E6F6F, 0x82165757, 0x63951010, 0x015BEFEF, 0x834DB8B8, 0x2E918686, 0xD9B56D6D, 0x511F8383, 0x9B53AAAA, 0x7C635D5D, 0xA63B6868, 0xEB3FFEFE, 0xA5D63030, 0xBE257A7A, 0x16A7ACAC, 0x0C0F0909, 0xE335F0F0, 0x6123A7A7, 0xC0F09090, 0x8CAFE9E9, 0x3A809D9D, 0xF5925C5C, 0x73810C0C, 0x2C273131, 0x2576D0D0, 0x0BE75656, 0xBB7B9292, 0x4EE9CECE, 0x89F10101, 0x6B9F1E1E, 0x53A93434, 0x6AC4F1F1, 0xB499C3C3, 0xF1975B5B, 0xE1834747, 0xE66B1818, 0xBDC82222, 0x450E9898, 0xE26E1F1F, 0xF4C9B3B3, 0xB62F7474, 0x66CBF8F8, 0xCCFF9999, 0x95EA1414, 0x03ED5858, 0x56F7DCDC, 0xD4E18B8B, 0x1C1B1515, 0x1EADA2A2, 0xD70CD3D3, 0xFB2BE2E2, 0xC31DC8C8, 0x8E195E5E, 0xB5C22C2C, 0xE9894949, 0xCF12C1C1, 0xBF7E9595, 0xBA207D7D, 0xEA641111, 0x77840B0B, 0x396DC5C5, 0xAF6A8989, 0x33D17C7C, 0xC9A17171, 0x62CEFFFF, 0x7137BBBB, 0x81FB0F0F, 0x793DB5B5, 0x0951E1E1, 0xADDC3E3E, 0x242D3F3F, 0xCDA47676, 0xF99D5555, 0xD8EE8282, 0xE5864040, 0xC5AE7878, 0xB9CD2525, 0x4D049696, 0x44557777, 0x080A0E0E, 0x86135050, 0xE730F7F7, 0xA1D33737, 0x1D40FAFA, 0xAA346161, 0xED8C4E4E, 0x06B3B0B0, 0x706C5454, 0xB22A7373, 0xD2523B3B, 0x410B9F9F, 0x7B8B0202, 0xA088D8D8, 0x114FF3F3, 0x3167CBCB, 0xC2462727, 0x27C06767, 0x90B4FCFC, 0x20283838, 0xF67F0404, 0x60784848, 0xFF2EE5E5, 0x96074C4C, 0x5C4B6565, 0xB1C72B2B, 0xAB6F8E8E, 0x9E0D4242, 0x9CBBF5F5, 0x52F2DBDB, 0x1BF34A4A, 0x5FA63D3D, 0x9359A4A4, 0x0ABCB9B9, 0xEF3AF9F9, 0x91EF1313, 0x85FE0808, 0x49019191, 0xEE611616, 0x2D7CDEDE, 0x4FB22121, 0x8F42B1B1, 0x3BDB7272, 0x47B82F2F, 0x8748BFBF, 0x6D2CAEAE, 0x46E3C0C0, 0xD6573C3C, 0x3E859A9A, 0x6929A9A9, 0x647D4F4F, 0x2A948181, 0xCE492E2E, 0xCB17C6C6, 0x2FCA6969, 0xFCC3BDBD, 0x975CA3A3, 0x055EE8E8, 0x7AD0EDED, 0xAC87D1D1, 0x7F8E0505, 0xD5BA6464, 0x1AA8A5A5, 0x4BB72626, 0x0EB9BEBE, 0xA7608787, 0x5AF8D5D5, 0x28223636, 0x14111B1B, 0x3FDE7575, 0x2979D9D9, 0x88AAEEEE, 0x3C332D2D, 0x4C5F7979, 0x02B6B7B7, 0xB896CACA, 0xDA583535, 0xB09CC4C4, 0x17FC4343, 0x551A8484, 0x1FF64D4D, 0x8A1C5959, 0x7D38B2B2, 0x57AC3333, 0xC718CFCF, 0x8DF40606, 0x74695353, 0xB7749B9B, 0xC4F59797, 0x9F56ADAD, 0x72DAE3E3, 0x7ED5EAEA, 0x154AF4F4, 0x229E8F8F, 0x12A2ABAB, 0x584E6262, 0x07E85F5F, 0x99E51D1D, 0x34392323, 0x6EC1F6F6, 0x50446C6C, 0xDE5D3232, 0x68724646, 0x6526A0A0, 0xBC93CDCD, 0xDB03DADA, 0xF8C6BABA, 0xC8FA9E9E, 0xA882D6D6, 0x2BCF6E6E, 0x40507070, 0xDCEB8585, 0xFE750A0A, 0x328A9393, 0xA48DDFDF, 0xCA4C2929, 0x10141C1C, 0x2173D7D7, 0xF0CCB4B4, 0xD309D4D4, 0x5D108A8A, 0x0FE25151, 0x00000000, 0x6F9A1919, 0x9DE01A1A, 0x368F9494, 0x42E6C7C7, 0x4AECC9C9, 0x5EFDD2D2, 0xC1AB7F7F, 0xE0D8A8A8 }; const u32bit Twofish::MDS2[256] = { 0xBC75BC32, 0xECF3EC21, 0x20C62043, 0xB3F4B3C9, 0xDADBDA03, 0x027B028B, 0xE2FBE22B, 0x9EC89EFA, 0xC94AC9EC, 0xD4D3D409, 0x18E6186B, 0x1E6B1E9F, 0x9845980E, 0xB27DB238, 0xA6E8A6D2, 0x264B26B7, 0x3CD63C57, 0x9332938A, 0x82D882EE, 0x52FD5298, 0x7B377BD4, 0xBB71BB37, 0x5BF15B97, 0x47E14783, 0x2430243C, 0x510F51E2, 0xBAF8BAC6, 0x4A1B4AF3, 0xBF87BF48, 0x0DFA0D70, 0xB006B0B3, 0x753F75DE, 0xD25ED2FD, 0x7DBA7D20, 0x66AE6631, 0x3A5B3AA3, 0x598A591C, 0x00000000, 0xCDBCCD93, 0x1A9D1AE0, 0xAE6DAE2C, 0x7FC17FAB, 0x2BB12BC7, 0xBE0EBEB9, 0xE080E0A0, 0x8A5D8A10, 0x3BD23B52, 0x64D564BA, 0xD8A0D888, 0xE784E7A5, 0x5F075FE8, 0x1B141B11, 0x2CB52CC2, 0xFC90FCB4, 0x312C3127, 0x80A38065, 0x73B2732A, 0x0C730C81, 0x794C795F, 0x6B546B41, 0x4B924B02, 0x53745369, 0x9436948F, 0x8351831F, 0x2A382A36, 0xC4B0C49C, 0x22BD22C8, 0xD55AD5F8, 0xBDFCBDC3, 0x48604878, 0xFF62FFCE, 0x4C964C07, 0x416C4177, 0xC742C7E6, 0xEBF7EB24, 0x1C101C14, 0x5D7C5D63, 0x36283622, 0x672767C0, 0xE98CE9AF, 0x441344F9, 0x149514EA, 0xF59CF5BB, 0xCFC7CF18, 0x3F243F2D, 0xC046C0E3, 0x723B72DB, 0x5470546C, 0x29CA294C, 0xF0E3F035, 0x088508FE, 0xC6CBC617, 0xF311F34F, 0x8CD08CE4, 0xA493A459, 0xCAB8CA96, 0x68A6683B, 0xB883B84D, 0x38203828, 0xE5FFE52E, 0xAD9FAD56, 0x0B770B84, 0xC8C3C81D, 0x99CC99FF, 0x580358ED, 0x196F199A, 0x0E080E0A, 0x95BF957E, 0x70407050, 0xF7E7F730, 0x6E2B6ECF, 0x1FE21F6E, 0xB579B53D, 0x090C090F, 0x61AA6134, 0x57825716, 0x9F419F0B, 0x9D3A9D80, 0x11EA1164, 0x25B925CD, 0xAFE4AFDD, 0x459A4508, 0xDFA4DF8D, 0xA397A35C, 0xEA7EEAD5, 0x35DA3558, 0xED7AEDD0, 0x431743FC, 0xF866F8CB, 0xFB94FBB1, 0x37A137D3, 0xFA1DFA40, 0xC23DC268, 0xB4F0B4CC, 0x32DE325D, 0x9CB39C71, 0x560B56E7, 0xE372E3DA, 0x87A78760, 0x151C151B, 0xF9EFF93A, 0x63D163BF, 0x345334A9, 0x9A3E9A85, 0xB18FB142, 0x7C337CD1, 0x8826889B, 0x3D5F3DA6, 0xA1ECA1D7, 0xE476E4DF, 0x812A8194, 0x91499101, 0x0F810FFB, 0xEE88EEAA, 0x16EE1661, 0xD721D773, 0x97C497F5, 0xA51AA5A8, 0xFEEBFE3F, 0x6DD96DB5, 0x78C578AE, 0xC539C56D, 0x1D991DE5, 0x76CD76A4, 0x3EAD3EDC, 0xCB31CB67, 0xB68BB647, 0xEF01EF5B, 0x1218121E, 0x602360C5, 0x6ADD6AB0, 0x4D1F4DF6, 0xCE4ECEE9, 0xDE2DDE7C, 0x55F9559D, 0x7E487E5A, 0x214F21B2, 0x03F2037A, 0xA065A026, 0x5E8E5E19, 0x5A785A66, 0x655C654B, 0x6258624E, 0xFD19FD45, 0x068D06F4, 0x40E54086, 0xF298F2BE, 0x335733AC, 0x17671790, 0x057F058E, 0xE805E85E, 0x4F644F7D, 0x89AF896A, 0x10631095, 0x74B6742F, 0x0AFE0A75, 0x5CF55C92, 0x9BB79B74, 0x2D3C2D33, 0x30A530D6, 0x2ECE2E49, 0x49E94989, 0x46684672, 0x77447755, 0xA8E0A8D8, 0x964D9604, 0x284328BD, 0xA969A929, 0xD929D979, 0x862E8691, 0xD1ACD187, 0xF415F44A, 0x8D598D15, 0xD6A8D682, 0xB90AB9BC, 0x429E420D, 0xF66EF6C1, 0x2F472FB8, 0xDDDFDD06, 0x23342339, 0xCC35CC62, 0xF16AF1C4, 0xC1CFC112, 0x85DC85EB, 0x8F228F9E, 0x71C971A1, 0x90C090F0, 0xAA9BAA53, 0x018901F1, 0x8BD48BE1, 0x4EED4E8C, 0x8EAB8E6F, 0xAB12ABA2, 0x6FA26F3E, 0xE60DE654, 0xDB52DBF2, 0x92BB927B, 0xB702B7B6, 0x692F69CA, 0x39A939D9, 0xD3D7D30C, 0xA761A723, 0xA21EA2AD, 0xC3B4C399, 0x6C506C44, 0x07040705, 0x04F6047F, 0x27C22746, 0xAC16ACA7, 0xD025D076, 0x50865013, 0xDC56DCF7, 0x8455841A, 0xE109E151, 0x7ABE7A25, 0x139113EF }; const u32bit Twofish::MDS3[256] = { 0xD939A9D9, 0x90176790, 0x719CB371, 0xD2A6E8D2, 0x05070405, 0x9852FD98, 0x6580A365, 0xDFE476DF, 0x08459A08, 0x024B9202, 0xA0E080A0, 0x665A7866, 0xDDAFE4DD, 0xB06ADDB0, 0xBF63D1BF, 0x362A3836, 0x54E60D54, 0x4320C643, 0x62CC3562, 0xBEF298BE, 0x1E12181E, 0x24EBF724, 0xD7A1ECD7, 0x77416C77, 0xBD2843BD, 0x32BC7532, 0xD47B37D4, 0x9B88269B, 0x700DFA70, 0xF94413F9, 0xB1FB94B1, 0x5A7E485A, 0x7A03F27A, 0xE48CD0E4, 0x47B68B47, 0x3C24303C, 0xA5E784A5, 0x416B5441, 0x06DDDF06, 0xC56023C5, 0x45FD1945, 0xA33A5BA3, 0x68C23D68, 0x158D5915, 0x21ECF321, 0x3166AE31, 0x3E6FA23E, 0x16578216, 0x95106395, 0x5BEF015B, 0x4DB8834D, 0x91862E91, 0xB56DD9B5, 0x1F83511F, 0x53AA9B53, 0x635D7C63, 0x3B68A63B, 0x3FFEEB3F, 0xD630A5D6, 0x257ABE25, 0xA7AC16A7, 0x0F090C0F, 0x35F0E335, 0x23A76123, 0xF090C0F0, 0xAFE98CAF, 0x809D3A80, 0x925CF592, 0x810C7381, 0x27312C27, 0x76D02576, 0xE7560BE7, 0x7B92BB7B, 0xE9CE4EE9, 0xF10189F1, 0x9F1E6B9F, 0xA93453A9, 0xC4F16AC4, 0x99C3B499, 0x975BF197, 0x8347E183, 0x6B18E66B, 0xC822BDC8, 0x0E98450E, 0x6E1FE26E, 0xC9B3F4C9, 0x2F74B62F, 0xCBF866CB, 0xFF99CCFF, 0xEA1495EA, 0xED5803ED, 0xF7DC56F7, 0xE18BD4E1, 0x1B151C1B, 0xADA21EAD, 0x0CD3D70C, 0x2BE2FB2B, 0x1DC8C31D, 0x195E8E19, 0xC22CB5C2, 0x8949E989, 0x12C1CF12, 0x7E95BF7E, 0x207DBA20, 0x6411EA64, 0x840B7784, 0x6DC5396D, 0x6A89AF6A, 0xD17C33D1, 0xA171C9A1, 0xCEFF62CE, 0x37BB7137, 0xFB0F81FB, 0x3DB5793D, 0x51E10951, 0xDC3EADDC, 0x2D3F242D, 0xA476CDA4, 0x9D55F99D, 0xEE82D8EE, 0x8640E586, 0xAE78C5AE, 0xCD25B9CD, 0x04964D04, 0x55774455, 0x0A0E080A, 0x13508613, 0x30F7E730, 0xD337A1D3, 0x40FA1D40, 0x3461AA34, 0x8C4EED8C, 0xB3B006B3, 0x6C54706C, 0x2A73B22A, 0x523BD252, 0x0B9F410B, 0x8B027B8B, 0x88D8A088, 0x4FF3114F, 0x67CB3167, 0x4627C246, 0xC06727C0, 0xB4FC90B4, 0x28382028, 0x7F04F67F, 0x78486078, 0x2EE5FF2E, 0x074C9607, 0x4B655C4B, 0xC72BB1C7, 0x6F8EAB6F, 0x0D429E0D, 0xBBF59CBB, 0xF2DB52F2, 0xF34A1BF3, 0xA63D5FA6, 0x59A49359, 0xBCB90ABC, 0x3AF9EF3A, 0xEF1391EF, 0xFE0885FE, 0x01914901, 0x6116EE61, 0x7CDE2D7C, 0xB2214FB2, 0x42B18F42, 0xDB723BDB, 0xB82F47B8, 0x48BF8748, 0x2CAE6D2C, 0xE3C046E3, 0x573CD657, 0x859A3E85, 0x29A96929, 0x7D4F647D, 0x94812A94, 0x492ECE49, 0x17C6CB17, 0xCA692FCA, 0xC3BDFCC3, 0x5CA3975C, 0x5EE8055E, 0xD0ED7AD0, 0x87D1AC87, 0x8E057F8E, 0xBA64D5BA, 0xA8A51AA8, 0xB7264BB7, 0xB9BE0EB9, 0x6087A760, 0xF8D55AF8, 0x22362822, 0x111B1411, 0xDE753FDE, 0x79D92979, 0xAAEE88AA, 0x332D3C33, 0x5F794C5F, 0xB6B702B6, 0x96CAB896, 0x5835DA58, 0x9CC4B09C, 0xFC4317FC, 0x1A84551A, 0xF64D1FF6, 0x1C598A1C, 0x38B27D38, 0xAC3357AC, 0x18CFC718, 0xF4068DF4, 0x69537469, 0x749BB774, 0xF597C4F5, 0x56AD9F56, 0xDAE372DA, 0xD5EA7ED5, 0x4AF4154A, 0x9E8F229E, 0xA2AB12A2, 0x4E62584E, 0xE85F07E8, 0xE51D99E5, 0x39233439, 0xC1F66EC1, 0x446C5044, 0x5D32DE5D, 0x72466872, 0x26A06526, 0x93CDBC93, 0x03DADB03, 0xC6BAF8C6, 0xFA9EC8FA, 0x82D6A882, 0xCF6E2BCF, 0x50704050, 0xEB85DCEB, 0x750AFE75, 0x8A93328A, 0x8DDFA48D, 0x4C29CA4C, 0x141C1014, 0x73D72173, 0xCCB4F0CC, 0x09D4D309, 0x108A5D10, 0xE2510FE2, 0x00000000, 0x9A196F9A, 0xE01A9DE0, 0x8F94368F, 0xE6C742E6, 0xECC94AEC, 0xFDD25EFD, 0xAB7FC1AB, 0xD8A8E0D8 }; } /* * Twofish * (C) 1999-2007 Jack Lloyd * * The key schedule implemenation is based on a public domain * implementation by Matthew Skala * * Distributed under the terms of the Botan license */ namespace Botan { /* * Twofish Encryption */ void Twofish::encrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u32bit A = load_le<u32bit>(in, 0) ^ RK[0]; u32bit B = load_le<u32bit>(in, 1) ^ RK[1]; u32bit C = load_le<u32bit>(in, 2) ^ RK[2]; u32bit D = load_le<u32bit>(in, 3) ^ RK[3]; for(size_t j = 0; j != 16; j += 2) { u32bit X, Y; X = SB[ get_byte(3, A)] ^ SB[256+get_byte(2, A)] ^ SB[512+get_byte(1, A)] ^ SB[768+get_byte(0, A)]; Y = SB[ get_byte(0, B)] ^ SB[256+get_byte(3, B)] ^ SB[512+get_byte(2, B)] ^ SB[768+get_byte(1, B)]; X += Y; Y += X + RK[2*j + 9]; X += RK[2*j + 8]; C = rotate_right(C ^ X, 1); D = rotate_left(D, 1) ^ Y; X = SB[ get_byte(3, C)] ^ SB[256+get_byte(2, C)] ^ SB[512+get_byte(1, C)] ^ SB[768+get_byte(0, C)]; Y = SB[ get_byte(0, D)] ^ SB[256+get_byte(3, D)] ^ SB[512+get_byte(2, D)] ^ SB[768+get_byte(1, D)]; X += Y; Y += X + RK[2*j + 11]; X += RK[2*j + 10]; A = rotate_right(A ^ X, 1); B = rotate_left(B, 1) ^ Y; } C ^= RK[4]; D ^= RK[5]; A ^= RK[6]; B ^= RK[7]; store_le(out, C, D, A, B); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * Twofish Decryption */ void Twofish::decrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u32bit A = load_le<u32bit>(in, 0) ^ RK[4]; u32bit B = load_le<u32bit>(in, 1) ^ RK[5]; u32bit C = load_le<u32bit>(in, 2) ^ RK[6]; u32bit D = load_le<u32bit>(in, 3) ^ RK[7]; for(size_t j = 0; j != 16; j += 2) { u32bit X, Y; X = SB[ get_byte(3, A)] ^ SB[256+get_byte(2, A)] ^ SB[512+get_byte(1, A)] ^ SB[768+get_byte(0, A)]; Y = SB[ get_byte(0, B)] ^ SB[256+get_byte(3, B)] ^ SB[512+get_byte(2, B)] ^ SB[768+get_byte(1, B)]; X += Y; Y += X + RK[39 - 2*j]; X += RK[38 - 2*j]; C = rotate_left(C, 1) ^ X; D = rotate_right(D ^ Y, 1); X = SB[ get_byte(3, C)] ^ SB[256+get_byte(2, C)] ^ SB[512+get_byte(1, C)] ^ SB[768+get_byte(0, C)]; Y = SB[ get_byte(0, D)] ^ SB[256+get_byte(3, D)] ^ SB[512+get_byte(2, D)] ^ SB[768+get_byte(1, D)]; X += Y; Y += X + RK[37 - 2*j]; X += RK[36 - 2*j]; A = rotate_left(A, 1) ^ X; B = rotate_right(B ^ Y, 1); } C ^= RK[0]; D ^= RK[1]; A ^= RK[2]; B ^= RK[3]; store_le(out, C, D, A, B); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * Twofish Key Schedule */ void Twofish::key_schedule(const byte key[], size_t length) { SecureVector<byte> S(16); for(size_t i = 0; i != length; ++i) rs_mul(&S[4*(i/8)], key[i], i); if(length == 16) { for(size_t i = 0; i != 256; ++i) { SB[ i] = MDS0[Q0[Q0[i]^S[ 0]]^S[ 4]]; SB[256+i] = MDS1[Q0[Q1[i]^S[ 1]]^S[ 5]]; SB[512+i] = MDS2[Q1[Q0[i]^S[ 2]]^S[ 6]]; SB[768+i] = MDS3[Q1[Q1[i]^S[ 3]]^S[ 7]]; } for(size_t i = 0; i != 40; i += 2) { u32bit X = MDS0[Q0[Q0[i ]^key[ 8]]^key[ 0]] ^ MDS1[Q0[Q1[i ]^key[ 9]]^key[ 1]] ^ MDS2[Q1[Q0[i ]^key[10]]^key[ 2]] ^ MDS3[Q1[Q1[i ]^key[11]]^key[ 3]]; u32bit Y = MDS0[Q0[Q0[i+1]^key[12]]^key[ 4]] ^ MDS1[Q0[Q1[i+1]^key[13]]^key[ 5]] ^ MDS2[Q1[Q0[i+1]^key[14]]^key[ 6]] ^ MDS3[Q1[Q1[i+1]^key[15]]^key[ 7]]; Y = rotate_left(Y, 8); X += Y; Y += X; RK[i] = X; RK[i+1] = rotate_left(Y, 9); } } else if(length == 24) { for(size_t i = 0; i != 256; ++i) { SB[ i] = MDS0[Q0[Q0[Q1[i]^S[ 0]]^S[ 4]]^S[ 8]]; SB[256+i] = MDS1[Q0[Q1[Q1[i]^S[ 1]]^S[ 5]]^S[ 9]]; SB[512+i] = MDS2[Q1[Q0[Q0[i]^S[ 2]]^S[ 6]]^S[10]]; SB[768+i] = MDS3[Q1[Q1[Q0[i]^S[ 3]]^S[ 7]]^S[11]]; } for(size_t i = 0; i != 40; i += 2) { u32bit X = MDS0[Q0[Q0[Q1[i ]^key[16]]^key[ 8]]^key[ 0]] ^ MDS1[Q0[Q1[Q1[i ]^key[17]]^key[ 9]]^key[ 1]] ^ MDS2[Q1[Q0[Q0[i ]^key[18]]^key[10]]^key[ 2]] ^ MDS3[Q1[Q1[Q0[i ]^key[19]]^key[11]]^key[ 3]]; u32bit Y = MDS0[Q0[Q0[Q1[i+1]^key[20]]^key[12]]^key[ 4]] ^ MDS1[Q0[Q1[Q1[i+1]^key[21]]^key[13]]^key[ 5]] ^ MDS2[Q1[Q0[Q0[i+1]^key[22]]^key[14]]^key[ 6]] ^ MDS3[Q1[Q1[Q0[i+1]^key[23]]^key[15]]^key[ 7]]; Y = rotate_left(Y, 8); X += Y; Y += X; RK[i] = X; RK[i+1] = rotate_left(Y, 9); } } else if(length == 32) { for(size_t i = 0; i != 256; ++i) { SB[ i] = MDS0[Q0[Q0[Q1[Q1[i]^S[ 0]]^S[ 4]]^S[ 8]]^S[12]]; SB[256+i] = MDS1[Q0[Q1[Q1[Q0[i]^S[ 1]]^S[ 5]]^S[ 9]]^S[13]]; SB[512+i] = MDS2[Q1[Q0[Q0[Q0[i]^S[ 2]]^S[ 6]]^S[10]]^S[14]]; SB[768+i] = MDS3[Q1[Q1[Q0[Q1[i]^S[ 3]]^S[ 7]]^S[11]]^S[15]]; } for(size_t i = 0; i != 40; i += 2) { u32bit X = MDS0[Q0[Q0[Q1[Q1[i ]^key[24]]^key[16]]^key[ 8]]^key[ 0]] ^ MDS1[Q0[Q1[Q1[Q0[i ]^key[25]]^key[17]]^key[ 9]]^key[ 1]] ^ MDS2[Q1[Q0[Q0[Q0[i ]^key[26]]^key[18]]^key[10]]^key[ 2]] ^ MDS3[Q1[Q1[Q0[Q1[i ]^key[27]]^key[19]]^key[11]]^key[ 3]]; u32bit Y = MDS0[Q0[Q0[Q1[Q1[i+1]^key[28]]^key[20]]^key[12]]^key[ 4]] ^ MDS1[Q0[Q1[Q1[Q0[i+1]^key[29]]^key[21]]^key[13]]^key[ 5]] ^ MDS2[Q1[Q0[Q0[Q0[i+1]^key[30]]^key[22]]^key[14]]^key[ 6]] ^ MDS3[Q1[Q1[Q0[Q1[i+1]^key[31]]^key[23]]^key[15]]^key[ 7]]; Y = rotate_left(Y, 8); X += Y; Y += X; RK[i] = X; RK[i+1] = rotate_left(Y, 9); } } } /* * Do one column of the RS matrix multiplcation */ void Twofish::rs_mul(byte S[4], byte key, size_t offset) { if(key) { byte X = POLY_TO_EXP[key - 1]; byte RS1 = RS[(4*offset ) % 32]; byte RS2 = RS[(4*offset+1) % 32]; byte RS3 = RS[(4*offset+2) % 32]; byte RS4 = RS[(4*offset+3) % 32]; S[0] ^= EXP_TO_POLY[(X + POLY_TO_EXP[RS1 - 1]) % 255]; S[1] ^= EXP_TO_POLY[(X + POLY_TO_EXP[RS2 - 1]) % 255]; S[2] ^= EXP_TO_POLY[(X + POLY_TO_EXP[RS3 - 1]) % 255]; S[3] ^= EXP_TO_POLY[(X + POLY_TO_EXP[RS4 - 1]) % 255]; } } /* * Clear memory of sensitive data */ void Twofish::clear() { zeroise(SB); zeroise(RK); } } /* * XTEA * (C) 1999-2009 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { void xtea_encrypt_4(const byte in[32], byte out[32], const u32bit EK[64]) { u32bit L0, R0, L1, R1, L2, R2, L3, R3; load_be(in, L0, R0, L1, R1, L2, R2, L3, R3); for(size_t i = 0; i != 32; ++i) { L0 += (((R0 << 4) ^ (R0 >> 5)) + R0) ^ EK[2*i]; L1 += (((R1 << 4) ^ (R1 >> 5)) + R1) ^ EK[2*i]; L2 += (((R2 << 4) ^ (R2 >> 5)) + R2) ^ EK[2*i]; L3 += (((R3 << 4) ^ (R3 >> 5)) + R3) ^ EK[2*i]; R0 += (((L0 << 4) ^ (L0 >> 5)) + L0) ^ EK[2*i+1]; R1 += (((L1 << 4) ^ (L1 >> 5)) + L1) ^ EK[2*i+1]; R2 += (((L2 << 4) ^ (L2 >> 5)) + L2) ^ EK[2*i+1]; R3 += (((L3 << 4) ^ (L3 >> 5)) + L3) ^ EK[2*i+1]; } store_be(out, L0, R0, L1, R1, L2, R2, L3, R3); } void xtea_decrypt_4(const byte in[32], byte out[32], const u32bit EK[64]) { u32bit L0, R0, L1, R1, L2, R2, L3, R3; load_be(in, L0, R0, L1, R1, L2, R2, L3, R3); for(size_t i = 0; i != 32; ++i) { R0 -= (((L0 << 4) ^ (L0 >> 5)) + L0) ^ EK[63 - 2*i]; R1 -= (((L1 << 4) ^ (L1 >> 5)) + L1) ^ EK[63 - 2*i]; R2 -= (((L2 << 4) ^ (L2 >> 5)) + L2) ^ EK[63 - 2*i]; R3 -= (((L3 << 4) ^ (L3 >> 5)) + L3) ^ EK[63 - 2*i]; L0 -= (((R0 << 4) ^ (R0 >> 5)) + R0) ^ EK[62 - 2*i]; L1 -= (((R1 << 4) ^ (R1 >> 5)) + R1) ^ EK[62 - 2*i]; L2 -= (((R2 << 4) ^ (R2 >> 5)) + R2) ^ EK[62 - 2*i]; L3 -= (((R3 << 4) ^ (R3 >> 5)) + R3) ^ EK[62 - 2*i]; } store_be(out, L0, R0, L1, R1, L2, R2, L3, R3); } } /* * XTEA Encryption */ void XTEA::encrypt_n(const byte in[], byte out[], size_t blocks) const { while(blocks >= 4) { xtea_encrypt_4(in, out, &(this->EK[0])); in += 4 * BLOCK_SIZE; out += 4 * BLOCK_SIZE; blocks -= 4; } for(size_t i = 0; i != blocks; ++i) { u32bit L = load_be<u32bit>(in, 0); u32bit R = load_be<u32bit>(in, 1); for(size_t j = 0; j != 32; ++j) { L += (((R << 4) ^ (R >> 5)) + R) ^ EK[2*j]; R += (((L << 4) ^ (L >> 5)) + L) ^ EK[2*j+1]; } store_be(out, L, R); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * XTEA Decryption */ void XTEA::decrypt_n(const byte in[], byte out[], size_t blocks) const { while(blocks >= 4) { xtea_decrypt_4(in, out, &(this->EK[0])); in += 4 * BLOCK_SIZE; out += 4 * BLOCK_SIZE; blocks -= 4; } for(size_t i = 0; i != blocks; ++i) { u32bit L = load_be<u32bit>(in, 0); u32bit R = load_be<u32bit>(in, 1); for(size_t j = 0; j != 32; ++j) { R -= (((L << 4) ^ (L >> 5)) + L) ^ EK[63 - 2*j]; L -= (((R << 4) ^ (R >> 5)) + R) ^ EK[62 - 2*j]; } store_be(out, L, R); in += BLOCK_SIZE; out += BLOCK_SIZE; } } /* * XTEA Key Schedule */ void XTEA::key_schedule(const byte key[], size_t) { SecureVector<u32bit> UK(4); for(size_t i = 0; i != 4; ++i) UK[i] = load_be<u32bit>(key, i); u32bit D = 0; for(size_t i = 0; i != 64; i += 2) { EK[i ] = D + UK[D % 4]; D += 0x9E3779B9; EK[i+1] = D + UK[(D >> 11) % 4]; } } } /* * XTEA in SIMD * (C) 2009 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { void xtea_encrypt_8(const byte in[64], byte out[64], const u32bit EK[64]) { SIMD_32 L0 = SIMD_32::load_be(in ); SIMD_32 R0 = SIMD_32::load_be(in + 16); SIMD_32 L1 = SIMD_32::load_be(in + 32); SIMD_32 R1 = SIMD_32::load_be(in + 48); SIMD_32::transpose(L0, R0, L1, R1); for(size_t i = 0; i != 32; i += 2) { SIMD_32 K0(EK[2*i ]); SIMD_32 K1(EK[2*i+1]); SIMD_32 K2(EK[2*i+2]); SIMD_32 K3(EK[2*i+3]); L0 += (((R0 << 4) ^ (R0 >> 5)) + R0) ^ K0; L1 += (((R1 << 4) ^ (R1 >> 5)) + R1) ^ K0; R0 += (((L0 << 4) ^ (L0 >> 5)) + L0) ^ K1; R1 += (((L1 << 4) ^ (L1 >> 5)) + L1) ^ K1; L0 += (((R0 << 4) ^ (R0 >> 5)) + R0) ^ K2; L1 += (((R1 << 4) ^ (R1 >> 5)) + R1) ^ K2; R0 += (((L0 << 4) ^ (L0 >> 5)) + L0) ^ K3; R1 += (((L1 << 4) ^ (L1 >> 5)) + L1) ^ K3; } SIMD_32::transpose(L0, R0, L1, R1); L0.store_be(out); R0.store_be(out + 16); L1.store_be(out + 32); R1.store_be(out + 48); } void xtea_decrypt_8(const byte in[64], byte out[64], const u32bit EK[64]) { SIMD_32 L0 = SIMD_32::load_be(in ); SIMD_32 R0 = SIMD_32::load_be(in + 16); SIMD_32 L1 = SIMD_32::load_be(in + 32); SIMD_32 R1 = SIMD_32::load_be(in + 48); SIMD_32::transpose(L0, R0, L1, R1); for(size_t i = 0; i != 32; i += 2) { SIMD_32 K0(EK[63 - 2*i]); SIMD_32 K1(EK[62 - 2*i]); SIMD_32 K2(EK[61 - 2*i]); SIMD_32 K3(EK[60 - 2*i]); R0 -= (((L0 << 4) ^ (L0 >> 5)) + L0) ^ K0; R1 -= (((L1 << 4) ^ (L1 >> 5)) + L1) ^ K0; L0 -= (((R0 << 4) ^ (R0 >> 5)) + R0) ^ K1; L1 -= (((R1 << 4) ^ (R1 >> 5)) + R1) ^ K1; R0 -= (((L0 << 4) ^ (L0 >> 5)) + L0) ^ K2; R1 -= (((L1 << 4) ^ (L1 >> 5)) + L1) ^ K2; L0 -= (((R0 << 4) ^ (R0 >> 5)) + R0) ^ K3; L1 -= (((R1 << 4) ^ (R1 >> 5)) + R1) ^ K3; } SIMD_32::transpose(L0, R0, L1, R1); L0.store_be(out); R0.store_be(out + 16); L1.store_be(out + 32); R1.store_be(out + 48); } } /* * XTEA Encryption */ void XTEA_SIMD::encrypt_n(const byte in[], byte out[], size_t blocks) const { const u32bit* KS = &(this->get_EK()[0]); while(blocks >= 8) { xtea_encrypt_8(in, out, KS); in += 8 * BLOCK_SIZE; out += 8 * BLOCK_SIZE; blocks -= 8; } if(blocks) XTEA::encrypt_n(in, out, blocks); } /* * XTEA Decryption */ void XTEA_SIMD::decrypt_n(const byte in[], byte out[], size_t blocks) const { const u32bit* KS = &(this->get_EK()[0]); while(blocks >= 8) { xtea_decrypt_8(in, out, KS); in += 8 * BLOCK_SIZE; out += 8 * BLOCK_SIZE; blocks -= 8; } if(blocks) XTEA::decrypt_n(in, out, blocks); } } /* * Certificate Store * (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { Certificate_Store* Certificate_Store_Memory::clone() const { return new Certificate_Store_Memory(*this); } void Certificate_Store_Memory::add_certificate(const X509_Certificate& cert) { for(size_t i = 0; i != certs.size(); ++i) { if(certs[i] == cert) return; } certs.push_back(cert); } std::vector<X509_Certificate> Certificate_Store_Memory::find_cert_by_subject_and_key_id( const X509_DN& subject_dn, const MemoryRegion<byte>& key_id) const { std::vector<X509_Certificate> result; for(size_t i = 0; i != certs.size(); ++i) { // Only compare key ids if set in both call and in the cert if(key_id.size()) { MemoryVector<byte> skid = certs[i].subject_key_id(); if(skid.size() && skid != key_id) // no match continue; } if(certs[i].subject_dn() == subject_dn) result.push_back(certs[i]); } return result; } void Certificate_Store_Memory::add_crl(const X509_CRL& crl) { X509_DN crl_issuer = crl.issuer_dn(); for(size_t i = 0; i != crls.size(); ++i) { // Found an update of a previously existing one; replace it if(crls[i].issuer_dn() == crl_issuer) { if(crls[i].this_update() < crl.this_update()) { crls[i] = crl; return; } } } // Totally new CRL, add to the list crls.push_back(crl); } std::vector<X509_CRL> Certificate_Store_Memory::find_crl_by_subject_and_key_id( const X509_DN& issuer_dn, const MemoryRegion<byte>& key_id) const { std::vector<X509_CRL> result; for(size_t i = 0; i != crls.size(); ++i) { // Only compare key ids if set in both call and in the CRL if(key_id.size()) { MemoryVector<byte> akid = crls[i].authority_key_id(); if(akid.size() && akid != key_id) // no match continue; } if(crls[i].issuer_dn() == issuer_dn) result.push_back(crls[i]); } return result; } } /* * PKCS #10 * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * PKCS10_Request Constructor */ PKCS10_Request::PKCS10_Request(DataSource& in) : X509_Object(in, "CERTIFICATE REQUEST/NEW CERTIFICATE REQUEST") { do_decode(); } /* * PKCS10_Request Constructor */ PKCS10_Request::PKCS10_Request(const std::string& in) : X509_Object(in, "CERTIFICATE REQUEST/NEW CERTIFICATE REQUEST") { do_decode(); } /* * Deocde the CertificateRequestInfo */ void PKCS10_Request::force_decode() { BER_Decoder cert_req_info(tbs_bits); size_t version; cert_req_info.decode(version); if(version != 0) throw Decoding_Error("Unknown version code in PKCS #10 request: " + to_string(version)); X509_DN dn_subject; cert_req_info.decode(dn_subject); info.add(dn_subject.contents()); BER_Object public_key = cert_req_info.get_next_object(); if(public_key.type_tag != SEQUENCE || public_key.class_tag != CONSTRUCTED) throw BER_Bad_Tag("PKCS10_Request: Unexpected tag for public key", public_key.type_tag, public_key.class_tag); info.add("X509.Certificate.public_key", PEM_Code::encode( ASN1::put_in_sequence(public_key.value), "PUBLIC KEY" ) ); BER_Object attr_bits = cert_req_info.get_next_object(); if(attr_bits.type_tag == 0 && attr_bits.class_tag == ASN1_Tag(CONSTRUCTED | CONTEXT_SPECIFIC)) { BER_Decoder attributes(attr_bits.value); while(attributes.more_items()) { Attribute attr; attributes.decode(attr); handle_attribute(attr); } attributes.verify_end(); } else if(attr_bits.type_tag != NO_OBJECT) throw BER_Bad_Tag("PKCS10_Request: Unexpected tag for attributes", attr_bits.type_tag, attr_bits.class_tag); cert_req_info.verify_end(); if(!this->check_signature(subject_public_key())) throw Decoding_Error("PKCS #10 request: Bad signature detected"); } /* * Handle attributes in a PKCS #10 request */ void PKCS10_Request::handle_attribute(const Attribute& attr) { BER_Decoder value(attr.parameters); if(attr.oid == OIDS::lookup("PKCS9.EmailAddress")) { ASN1_String email; value.decode(email); info.add("RFC822", email.value()); } else if(attr.oid == OIDS::lookup("PKCS9.ChallengePassword")) { ASN1_String challenge_password; value.decode(challenge_password); info.add("PKCS9.ChallengePassword", challenge_password.value()); } else if(attr.oid == OIDS::lookup("PKCS9.ExtensionRequest")) { Extensions extensions; value.decode(extensions).verify_end(); Data_Store issuer_info; extensions.contents_to(info, issuer_info); } } /* * Return the challenge password (if any) */ std::string PKCS10_Request::challenge_password() const { return info.get1("PKCS9.ChallengePassword"); } /* * Return the name of the requestor */ X509_DN PKCS10_Request::subject_dn() const { return create_dn(info); } /* * Return the public key of the requestor */ MemoryVector<byte> PKCS10_Request::raw_public_key() const { DataSource_Memory source(info.get1("X509.Certificate.public_key")); return PEM_Code::decode_check_label(source, "PUBLIC KEY"); } /* * Return the public key of the requestor */ Public_Key* PKCS10_Request::subject_public_key() const { DataSource_Memory source(info.get1("X509.Certificate.public_key")); return X509::load_key(source); } /* * Return the alternative names of the requestor */ AlternativeName PKCS10_Request::subject_alt_name() const { return create_alt_name(info); } /* * Return the key constraints (if any) */ Key_Constraints PKCS10_Request::constraints() const { return Key_Constraints(info.get1_u32bit("X509v3.KeyUsage", NO_CONSTRAINTS)); } /* * Return the extendend key constraints (if any) */ std::vector<OID> PKCS10_Request::ex_constraints() const { std::vector<std::string> oids = info.get("X509v3.ExtendedKeyUsage"); std::vector<OID> result; for(size_t i = 0; i != oids.size(); ++i) result.push_back(OID(oids[i])); return result; } /* * Return is a CA certificate is requested */ bool PKCS10_Request::is_CA() const { return (info.get1_u32bit("X509v3.BasicConstraints.is_ca") > 0); } /* * Return the desired path limit (if any) */ u32bit PKCS10_Request::path_limit() const { return info.get1_u32bit("X509v3.BasicConstraints.path_constraint", 0); } } /* * X.509 Certificate Authority * (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> #include <typeinfo> #include <iterator> #include <memory> #include <set> namespace Botan { /* * Load the certificate and private key */ X509_CA::X509_CA(const X509_Certificate& c, const Private_Key& key, const std::string& hash_fn) : cert(c) { if(!cert.is_CA_cert()) throw Invalid_Argument("X509_CA: This certificate is not for a CA"); signer = choose_sig_format(key, hash_fn, ca_sig_algo); } /* * X509_CA Destructor */ X509_CA::~X509_CA() { delete signer; } /* * Sign a PKCS #10 certificate request */ X509_Certificate X509_CA::sign_request(const PKCS10_Request& req, RandomNumberGenerator& rng, const X509_Time& not_before, const X509_Time& not_after) { Key_Constraints constraints; if(req.is_CA()) constraints = Key_Constraints(KEY_CERT_SIGN | CRL_SIGN); else { std::auto_ptr<Public_Key> key(req.subject_public_key()); constraints = X509::find_constraints(*key, req.constraints()); } Extensions extensions; extensions.add( new Cert_Extension::Basic_Constraints(req.is_CA(), req.path_limit()), true); extensions.add(new Cert_Extension::Key_Usage(constraints), true); extensions.add(new Cert_Extension::Authority_Key_ID(cert.subject_key_id())); extensions.add(new Cert_Extension::Subject_Key_ID(req.raw_public_key())); extensions.add( new Cert_Extension::Subject_Alternative_Name(req.subject_alt_name())); extensions.add( new Cert_Extension::Extended_Key_Usage(req.ex_constraints())); return make_cert(signer, rng, ca_sig_algo, req.raw_public_key(), not_before, not_after, cert.subject_dn(), req.subject_dn(), extensions); } /* * Create a new certificate */ X509_Certificate X509_CA::make_cert(PK_Signer* signer, RandomNumberGenerator& rng, const AlgorithmIdentifier& sig_algo, const MemoryRegion<byte>& pub_key, const X509_Time& not_before, const X509_Time& not_after, const X509_DN& issuer_dn, const X509_DN& subject_dn, const Extensions& extensions) { const size_t X509_CERT_VERSION = 3; const size_t SERIAL_BITS = 128; BigInt serial_no(rng, SERIAL_BITS); DataSource_Memory source(X509_Object::make_signed(signer, rng, sig_algo, DER_Encoder().start_cons(SEQUENCE) .start_explicit(0) .encode(X509_CERT_VERSION-1) .end_explicit() .encode(serial_no) .encode(sig_algo) .encode(issuer_dn) .start_cons(SEQUENCE) .encode(not_before) .encode(not_after) .end_cons() .encode(subject_dn) .raw_bytes(pub_key) .start_explicit(3) .start_cons(SEQUENCE) .encode(extensions) .end_cons() .end_explicit() .end_cons() .get_contents() )); return X509_Certificate(source); } /* * Create a new, empty CRL */ X509_CRL X509_CA::new_crl(RandomNumberGenerator& rng, u32bit next_update) const { std::vector<CRL_Entry> empty; return make_crl(empty, 1, next_update, rng); } /* * Update a CRL with new entries */ X509_CRL X509_CA::update_crl(const X509_CRL& crl, const std::vector<CRL_Entry>& new_revoked, RandomNumberGenerator& rng, u32bit next_update) const { std::vector<CRL_Entry> revoked = crl.get_revoked(); std::copy(new_revoked.begin(), new_revoked.end(), std::back_inserter(revoked)); return make_crl(revoked, crl.crl_number() + 1, next_update, rng); } /* * Create a CRL */ X509_CRL X509_CA::make_crl(const std::vector<CRL_Entry>& revoked, u32bit crl_number, u32bit next_update, RandomNumberGenerator& rng) const { const size_t X509_CRL_VERSION = 2; if(next_update == 0) next_update = timespec_to_u32bit("7d"); // Totally stupid: ties encoding logic to the return of std::time!! const u64bit current_time = system_time(); Extensions extensions; extensions.add( new Cert_Extension::Authority_Key_ID(cert.subject_key_id())); extensions.add(new Cert_Extension::CRL_Number(crl_number)); DataSource_Memory source(X509_Object::make_signed(signer, rng, ca_sig_algo, DER_Encoder().start_cons(SEQUENCE) .encode(X509_CRL_VERSION-1) .encode(ca_sig_algo) .encode(cert.issuer_dn()) .encode(X509_Time(current_time)) .encode(X509_Time(current_time + next_update)) .encode_if(revoked.size() > 0, DER_Encoder() .start_cons(SEQUENCE) .encode_list(revoked) .end_cons() ) .start_explicit(0) .start_cons(SEQUENCE) .encode(extensions) .end_cons() .end_explicit() .end_cons() .get_contents() )); return X509_CRL(source); } /* * Return the CA's certificate */ X509_Certificate X509_CA::ca_certificate() const { return cert; } /* * Choose a signing format for the key */ PK_Signer* choose_sig_format(const Private_Key& key, const std::string& hash_fn, AlgorithmIdentifier& sig_algo) { std::string padding; const std::string algo_name = key.algo_name(); const HashFunction* proto_hash = retrieve_hash(hash_fn); if(!proto_hash) throw Algorithm_Not_Found(hash_fn); if(key.max_input_bits() < proto_hash->output_length()*8) throw Invalid_Argument("Key is too small for chosen hash function"); if(algo_name == "RSA") padding = "EMSA3"; else if(algo_name == "DSA") padding = "EMSA1"; else if(algo_name == "ECDSA") padding = "EMSA1_BSI"; else throw Invalid_Argument("Unknown X.509 signing key type: " + algo_name); Signature_Format format = (key.message_parts() > 1) ? DER_SEQUENCE : IEEE_1363; padding = padding + '(' + proto_hash->name() + ')'; sig_algo.oid = OIDS::lookup(algo_name + "/" + padding); sig_algo.parameters = key.algorithm_identifier().parameters; return new PK_Signer(key, padding, format); } } /* * X.509 Certificate Extensions * (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> #include <memory> namespace Botan { /* * List of X.509 Certificate Extensions */ Certificate_Extension* Extensions::get_extension(const OID& oid) { #define X509_EXTENSION(NAME, TYPE) \ if(OIDS::name_of(oid, NAME)) \ return new Cert_Extension::TYPE(); X509_EXTENSION("X509v3.KeyUsage", Key_Usage); X509_EXTENSION("X509v3.BasicConstraints", Basic_Constraints); X509_EXTENSION("X509v3.SubjectKeyIdentifier", Subject_Key_ID); X509_EXTENSION("X509v3.AuthorityKeyIdentifier", Authority_Key_ID); X509_EXTENSION("X509v3.ExtendedKeyUsage", Extended_Key_Usage); X509_EXTENSION("X509v3.IssuerAlternativeName", Issuer_Alternative_Name); X509_EXTENSION("X509v3.SubjectAlternativeName", Subject_Alternative_Name); X509_EXTENSION("X509v3.CRLNumber", CRL_Number); X509_EXTENSION("X509v3.CertificatePolicies", Certificate_Policies); X509_EXTENSION("X509v3.ReasonCode", CRL_ReasonCode); return 0; } /* * Extensions Copy Constructor */ Extensions::Extensions(const Extensions& extensions) : ASN1_Object() { *this = extensions; } /* * Extensions Assignment Operator */ Extensions& Extensions::operator=(const Extensions& other) { for(size_t i = 0; i != extensions.size(); ++i) delete extensions[i].first; extensions.clear(); for(size_t i = 0; i != other.extensions.size(); ++i) extensions.push_back( std::make_pair(other.extensions[i].first->copy(), other.extensions[i].second)); return (*this); } /* * Return the OID of this extension */ OID Certificate_Extension::oid_of() const { return OIDS::lookup(oid_name()); } void Extensions::add(Certificate_Extension* extn, bool critical) { extensions.push_back(std::make_pair(extn, critical)); } /* * Encode an Extensions list */ void Extensions::encode_into(DER_Encoder& to_object) const { for(size_t i = 0; i != extensions.size(); ++i) { const Certificate_Extension* ext = extensions[i].first; const bool is_critical = extensions[i].second; const bool should_encode = ext->should_encode(); if(should_encode) { to_object.start_cons(SEQUENCE) .encode(ext->oid_of()) .encode_optional(is_critical, false) .encode(ext->encode_inner(), OCTET_STRING) .end_cons(); } } } /* * Decode a list of Extensions */ void Extensions::decode_from(BER_Decoder& from_source) { for(size_t i = 0; i != extensions.size(); ++i) delete extensions[i].first; extensions.clear(); BER_Decoder sequence = from_source.start_cons(SEQUENCE); while(sequence.more_items()) { OID oid; MemoryVector<byte> value; bool critical; sequence.start_cons(SEQUENCE) .decode(oid) .decode_optional(critical, BOOLEAN, UNIVERSAL, false) .decode(value, OCTET_STRING) .verify_end() .end_cons(); Certificate_Extension* ext = get_extension(oid); if(!ext) { if(!critical || !should_throw) continue; throw Decoding_Error("Encountered unknown X.509 extension marked " "as critical; OID = " + oid.as_string()); } ext->decode_inner(value); extensions.push_back(std::make_pair(ext, critical)); } sequence.verify_end(); } /* * Write the extensions to an info store */ void Extensions::contents_to(Data_Store& subject_info, Data_Store& issuer_info) const { for(size_t i = 0; i != extensions.size(); ++i) extensions[i].first->contents_to(subject_info, issuer_info); } /* * Delete an Extensions list */ Extensions::~Extensions() { for(size_t i = 0; i != extensions.size(); ++i) delete extensions[i].first; } namespace Cert_Extension { /* * Checked accessor for the path_limit member */ size_t Basic_Constraints::get_path_limit() const { if(!is_ca) throw Invalid_State("Basic_Constraints::get_path_limit: Not a CA"); return path_limit; } /* * Encode the extension */ MemoryVector<byte> Basic_Constraints::encode_inner() const { return DER_Encoder() .start_cons(SEQUENCE) .encode_if(is_ca, DER_Encoder() .encode(is_ca) .encode_optional(path_limit, NO_CERT_PATH_LIMIT) ) .end_cons() .get_contents(); } /* * Decode the extension */ void Basic_Constraints::decode_inner(const MemoryRegion<byte>& in) { BER_Decoder(in) .start_cons(SEQUENCE) .decode_optional(is_ca, BOOLEAN, UNIVERSAL, false) .decode_optional(path_limit, INTEGER, UNIVERSAL, NO_CERT_PATH_LIMIT) .verify_end() .end_cons(); if(is_ca == false) path_limit = 0; } /* * Return a textual representation */ void Basic_Constraints::contents_to(Data_Store& subject, Data_Store&) const { subject.add("X509v3.BasicConstraints.is_ca", (is_ca ? 1 : 0)); subject.add("X509v3.BasicConstraints.path_constraint", path_limit); } /* * Encode the extension */ MemoryVector<byte> Key_Usage::encode_inner() const { if(constraints == NO_CONSTRAINTS) throw Encoding_Error("Cannot encode zero usage constraints"); const size_t unused_bits = low_bit(constraints) - 1; MemoryVector<byte> der; der.push_back(BIT_STRING); der.push_back(2 + ((unused_bits < 8) ? 1 : 0)); der.push_back(unused_bits % 8); der.push_back((constraints >> 8) & 0xFF); if(constraints & 0xFF) der.push_back(constraints & 0xFF); return der; } /* * Decode the extension */ void Key_Usage::decode_inner(const MemoryRegion<byte>& in) { BER_Decoder ber(in); BER_Object obj = ber.get_next_object(); if(obj.type_tag != BIT_STRING || obj.class_tag != UNIVERSAL) throw BER_Bad_Tag("Bad tag for usage constraint", obj.type_tag, obj.class_tag); if(obj.value.size() != 2 && obj.value.size() != 3) throw BER_Decoding_Error("Bad size for BITSTRING in usage constraint"); if(obj.value[0] >= 8) throw BER_Decoding_Error("Invalid unused bits in usage constraint"); obj.value[obj.value.size()-1] &= (0xFF << obj.value[0]); u16bit usage = 0; for(size_t i = 1; i != obj.value.size(); ++i) usage = (obj.value[i] << 8) | usage; constraints = Key_Constraints(usage); } /* * Return a textual representation */ void Key_Usage::contents_to(Data_Store& subject, Data_Store&) const { subject.add("X509v3.KeyUsage", constraints); } /* * Encode the extension */ MemoryVector<byte> Subject_Key_ID::encode_inner() const { return DER_Encoder().encode(key_id, OCTET_STRING).get_contents(); } /* * Decode the extension */ void Subject_Key_ID::decode_inner(const MemoryRegion<byte>& in) { BER_Decoder(in).decode(key_id, OCTET_STRING).verify_end(); } /* * Return a textual representation */ void Subject_Key_ID::contents_to(Data_Store& subject, Data_Store&) const { subject.add("X509v3.SubjectKeyIdentifier", key_id); } /* * Subject_Key_ID Constructor */ Subject_Key_ID::Subject_Key_ID(const MemoryRegion<byte>& pub_key) { SHA_160 hash; key_id = hash.process(pub_key); } /* * Encode the extension */ MemoryVector<byte> Authority_Key_ID::encode_inner() const { return DER_Encoder() .start_cons(SEQUENCE) .encode(key_id, OCTET_STRING, ASN1_Tag(0), CONTEXT_SPECIFIC) .end_cons() .get_contents(); } /* * Decode the extension */ void Authority_Key_ID::decode_inner(const MemoryRegion<byte>& in) { BER_Decoder(in) .start_cons(SEQUENCE) .decode_optional_string(key_id, OCTET_STRING, 0); } /* * Return a textual representation */ void Authority_Key_ID::contents_to(Data_Store&, Data_Store& issuer) const { if(key_id.size()) issuer.add("X509v3.AuthorityKeyIdentifier", key_id); } /* * Encode the extension */ MemoryVector<byte> Alternative_Name::encode_inner() const { return DER_Encoder().encode(alt_name).get_contents(); } /* * Decode the extension */ void Alternative_Name::decode_inner(const MemoryRegion<byte>& in) { BER_Decoder(in).decode(alt_name); } /* * Return a textual representation */ void Alternative_Name::contents_to(Data_Store& subject_info, Data_Store& issuer_info) const { std::multimap<std::string, std::string> contents = get_alt_name().contents(); if(oid_name_str == "X509v3.SubjectAlternativeName") subject_info.add(contents); else if(oid_name_str == "X509v3.IssuerAlternativeName") issuer_info.add(contents); else throw Internal_Error("In Alternative_Name, unknown type " + oid_name_str); } /* * Alternative_Name Constructor */ Alternative_Name::Alternative_Name(const AlternativeName& alt_name, const std::string& oid_name_str, const std::string& config_name_str) { this->alt_name = alt_name; this->oid_name_str = oid_name_str; this->config_name_str = config_name_str; } /* * Subject_Alternative_Name Constructor */ Subject_Alternative_Name::Subject_Alternative_Name( const AlternativeName& name) : Alternative_Name(name, "X509v3.SubjectAlternativeName", "subject_alternative_name") { } /* * Issuer_Alternative_Name Constructor */ Issuer_Alternative_Name::Issuer_Alternative_Name(const AlternativeName& name) : Alternative_Name(name, "X509v3.IssuerAlternativeName", "issuer_alternative_name") { } /* * Encode the extension */ MemoryVector<byte> Extended_Key_Usage::encode_inner() const { return DER_Encoder() .start_cons(SEQUENCE) .encode_list(oids) .end_cons() .get_contents(); } /* * Decode the extension */ void Extended_Key_Usage::decode_inner(const MemoryRegion<byte>& in) { BER_Decoder(in) .start_cons(SEQUENCE) .decode_list(oids) .end_cons(); } /* * Return a textual representation */ void Extended_Key_Usage::contents_to(Data_Store& subject, Data_Store&) const { for(size_t i = 0; i != oids.size(); ++i) subject.add("X509v3.ExtendedKeyUsage", oids[i].as_string()); } namespace { /* * A policy specifier */ class Policy_Information : public ASN1_Object { public: OID oid; Policy_Information() {} Policy_Information(const OID& oid) : oid(oid) {} void encode_into(DER_Encoder& codec) const { codec.start_cons(SEQUENCE) .encode(oid) .end_cons(); } void decode_from(BER_Decoder& codec) { codec.start_cons(SEQUENCE) .decode(oid) .discard_remaining() .end_cons(); } }; } /* * Encode the extension */ MemoryVector<byte> Certificate_Policies::encode_inner() const { std::vector<Policy_Information> policies; for(size_t i = 0; i != oids.size(); ++i) policies.push_back(oids[i]); return DER_Encoder() .start_cons(SEQUENCE) .encode_list(policies) .end_cons() .get_contents(); } /* * Decode the extension */ void Certificate_Policies::decode_inner(const MemoryRegion<byte>& in) { std::vector<Policy_Information> policies; BER_Decoder(in) .start_cons(SEQUENCE) .decode_list(policies) .end_cons(); oids.clear(); for(size_t i = 0; i != policies.size(); ++i) oids.push_back(policies[i].oid); } /* * Return a textual representation */ void Certificate_Policies::contents_to(Data_Store& info, Data_Store&) const { for(size_t i = 0; i != oids.size(); ++i) info.add("X509v3.ExtendedKeyUsage", oids[i].as_string()); } /* * Checked accessor for the crl_number member */ size_t CRL_Number::get_crl_number() const { if(!has_value) throw Invalid_State("CRL_Number::get_crl_number: Not set"); return crl_number; } /* * Copy a CRL_Number extension */ CRL_Number* CRL_Number::copy() const { if(!has_value) throw Invalid_State("CRL_Number::copy: Not set"); return new CRL_Number(crl_number); } /* * Encode the extension */ MemoryVector<byte> CRL_Number::encode_inner() const { return DER_Encoder().encode(crl_number).get_contents(); } /* * Decode the extension */ void CRL_Number::decode_inner(const MemoryRegion<byte>& in) { BER_Decoder(in).decode(crl_number); } /* * Return a textual representation */ void CRL_Number::contents_to(Data_Store& info, Data_Store&) const { info.add("X509v3.CRLNumber", crl_number); } /* * Encode the extension */ MemoryVector<byte> CRL_ReasonCode::encode_inner() const { return DER_Encoder() .encode(static_cast<size_t>(reason), ENUMERATED, UNIVERSAL) .get_contents(); } /* * Decode the extension */ void CRL_ReasonCode::decode_inner(const MemoryRegion<byte>& in) { size_t reason_code = 0; BER_Decoder(in).decode(reason_code, ENUMERATED, UNIVERSAL); reason = static_cast<CRL_Code>(reason_code); } /* * Return a textual representation */ void CRL_ReasonCode::contents_to(Data_Store& info, Data_Store&) const { info.add("X509v3.CRLReasonCode", reason); } } } /* * X.509 SIGNED Object * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> #include <memory> namespace Botan { /* * Create a generic X.509 object */ X509_Object::X509_Object(DataSource& stream, const std::string& labels) { init(stream, labels); } /* * Createa a generic X.509 object */ X509_Object::X509_Object(const std::string& file, const std::string& labels) { DataSource_Stream stream(file, true); init(stream, labels); } /* * Read a PEM or BER X.509 object */ void X509_Object::init(DataSource& in, const std::string& labels) { PEM_labels_allowed = split_on(labels, '/'); if(PEM_labels_allowed.size() < 1) throw Invalid_Argument("Bad labels argument to X509_Object"); PEM_label_pref = PEM_labels_allowed[0]; std::sort(PEM_labels_allowed.begin(), PEM_labels_allowed.end()); try { if(ASN1::maybe_BER(in) && !PEM_Code::matches(in)) decode_info(in); else { std::string got_label; DataSource_Memory ber(PEM_Code::decode(in, got_label)); if(!std::binary_search(PEM_labels_allowed.begin(), PEM_labels_allowed.end(), got_label)) throw Decoding_Error("Invalid PEM label: " + got_label); decode_info(ber); } } catch(Decoding_Error& e) { throw Decoding_Error(PEM_label_pref + " decoding failed: " + e.what()); } } /* * Read a BER encoded X.509 object */ void X509_Object::decode_info(DataSource& source) { BER_Decoder(source) .start_cons(SEQUENCE) .start_cons(SEQUENCE) .raw_bytes(tbs_bits) .end_cons() .decode(sig_algo) .decode(sig, BIT_STRING) .verify_end() .end_cons(); } /* * Return a BER or PEM encoded X.509 object */ void X509_Object::encode(Pipe& out, X509_Encoding encoding) const { if(encoding == PEM) out.write(this->PEM_encode()); else out.write(this->BER_encode()); } /* * Return a BER encoded X.509 object */ MemoryVector<byte> X509_Object::BER_encode() const { return DER_Encoder() .start_cons(SEQUENCE) .start_cons(SEQUENCE) .raw_bytes(tbs_bits) .end_cons() .encode(sig_algo) .encode(sig, BIT_STRING) .end_cons() .get_contents(); } /* * Return a PEM encoded X.509 object */ std::string X509_Object::PEM_encode() const { return PEM_Code::encode(BER_encode(), PEM_label_pref); } /* * Return the TBS data */ MemoryVector<byte> X509_Object::tbs_data() const { return ASN1::put_in_sequence(tbs_bits); } /* * Return the signature of this object */ MemoryVector<byte> X509_Object::signature() const { return sig; } /* * Return the algorithm used to sign this object */ AlgorithmIdentifier X509_Object::signature_algorithm() const { return sig_algo; } /* * Return the hash used in generating the signature */ std::string X509_Object::hash_used_for_signature() const { std::vector<std::string> sig_info = split_on(OIDS::lookup(sig_algo.oid), '/'); if(sig_info.size() != 2) throw Internal_Error("Invalid name format found for " + sig_algo.oid.as_string()); std::vector<std::string> pad_and_hash = parse_algorithm_name(sig_info[1]); if(pad_and_hash.size() != 2) throw Internal_Error("Invalid name format " + sig_info[1]); return pad_and_hash[1]; } /* * Check the signature on an object */ bool X509_Object::check_signature(Public_Key* pub_key) const { std::auto_ptr<Public_Key> key(pub_key); return check_signature(*key); } /* * Check the signature on an object */ bool X509_Object::check_signature(Public_Key& pub_key) const { try { std::vector<std::string> sig_info = split_on(OIDS::lookup(sig_algo.oid), '/'); if(sig_info.size() != 2 || sig_info[0] != pub_key.algo_name()) return false; std::string padding = sig_info[1]; Signature_Format format = (pub_key.message_parts() >= 2) ? DER_SEQUENCE : IEEE_1363; PK_Verifier verifier(pub_key, padding, format); return verifier.verify_message(tbs_data(), signature()); } catch(...) { return false; } } /* * Apply the X.509 SIGNED macro */ MemoryVector<byte> X509_Object::make_signed(PK_Signer* signer, RandomNumberGenerator& rng, const AlgorithmIdentifier& algo, const MemoryRegion<byte>& tbs_bits) { return DER_Encoder() .start_cons(SEQUENCE) .raw_bytes(tbs_bits) .encode(algo) .encode(signer->sign_message(tbs_bits, rng), BIT_STRING) .end_cons() .get_contents(); } /* * Try to decode the actual information */ void X509_Object::do_decode() { try { force_decode(); } catch(Decoding_Error& e) { throw Decoding_Error(PEM_label_pref + " decoding failed (" + e.what() + ")"); } catch(Invalid_Argument& e) { throw Decoding_Error(PEM_label_pref + " decoding failed (" + e.what() + ")"); } } } /* * X.509 Certificates * (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> #include <iterator> #include <sstream> namespace Botan { namespace { /* * Lookup each OID in the vector */ std::vector<std::string> lookup_oids(const std::vector<std::string>& in) { std::vector<std::string> out; std::vector<std::string>::const_iterator i = in.begin(); while(i != in.end()) { out.push_back(OIDS::lookup(OID(*i))); ++i; } return out; } } /* * X509_Certificate Constructor */ X509_Certificate::X509_Certificate(DataSource& in) : X509_Object(in, "CERTIFICATE/X509 CERTIFICATE") { self_signed = false; do_decode(); } /* * X509_Certificate Constructor */ X509_Certificate::X509_Certificate(const std::string& in) : X509_Object(in, "CERTIFICATE/X509 CERTIFICATE") { self_signed = false; do_decode(); } /* * Decode the TBSCertificate data */ void X509_Certificate::force_decode() { size_t version; BigInt serial_bn; AlgorithmIdentifier sig_algo_inner; X509_DN dn_issuer, dn_subject; X509_Time start, end; BER_Decoder tbs_cert(tbs_bits); tbs_cert.decode_optional(version, ASN1_Tag(0), ASN1_Tag(CONSTRUCTED | CONTEXT_SPECIFIC)) .decode(serial_bn) .decode(sig_algo_inner) .decode(dn_issuer) .start_cons(SEQUENCE) .decode(start) .decode(end) .verify_end() .end_cons() .decode(dn_subject); if(version > 2) throw Decoding_Error("Unknown X.509 cert version " + Botan::to_string(version)); if(sig_algo != sig_algo_inner) throw Decoding_Error("Algorithm identifier mismatch"); self_signed = (dn_subject == dn_issuer); subject.add(dn_subject.contents()); issuer.add(dn_issuer.contents()); BER_Object public_key = tbs_cert.get_next_object(); if(public_key.type_tag != SEQUENCE || public_key.class_tag != CONSTRUCTED) throw BER_Bad_Tag("X509_Certificate: Unexpected tag for public key", public_key.type_tag, public_key.class_tag); MemoryVector<byte> v2_issuer_key_id, v2_subject_key_id; tbs_cert.decode_optional_string(v2_issuer_key_id, BIT_STRING, 1); tbs_cert.decode_optional_string(v2_subject_key_id, BIT_STRING, 2); BER_Object v3_exts_data = tbs_cert.get_next_object(); if(v3_exts_data.type_tag == 3 && v3_exts_data.class_tag == ASN1_Tag(CONSTRUCTED | CONTEXT_SPECIFIC)) { Extensions extensions; BER_Decoder(v3_exts_data.value).decode(extensions).verify_end(); extensions.contents_to(subject, issuer); } else if(v3_exts_data.type_tag != NO_OBJECT) throw BER_Bad_Tag("Unknown tag in X.509 cert", v3_exts_data.type_tag, v3_exts_data.class_tag); if(tbs_cert.more_items()) throw Decoding_Error("TBSCertificate has more items that expected"); subject.add("X509.Certificate.version", version); subject.add("X509.Certificate.serial", BigInt::encode(serial_bn)); subject.add("X509.Certificate.start", start.readable_string()); subject.add("X509.Certificate.end", end.readable_string()); issuer.add("X509.Certificate.v2.key_id", v2_issuer_key_id); subject.add("X509.Certificate.v2.key_id", v2_subject_key_id); subject.add("X509.Certificate.public_key", PEM_Code::encode( ASN1::put_in_sequence(public_key.value), "PUBLIC KEY" ) ); if(is_CA_cert() && !subject.has_value("X509v3.BasicConstraints.path_constraint")) { const size_t limit = (x509_version() < 3) ? Cert_Extension::NO_CERT_PATH_LIMIT : 0; subject.add("X509v3.BasicConstraints.path_constraint", limit); } } /* * Return the X.509 version in use */ u32bit X509_Certificate::x509_version() const { return (subject.get1_u32bit("X509.Certificate.version") + 1); } /* * Return the time this cert becomes valid */ std::string X509_Certificate::start_time() const { return subject.get1("X509.Certificate.start"); } /* * Return the time this cert becomes invalid */ std::string X509_Certificate::end_time() const { return subject.get1("X509.Certificate.end"); } /* * Return information about the subject */ std::vector<std::string> X509_Certificate::subject_info(const std::string& what) const { return subject.get(X509_DN::deref_info_field(what)); } /* * Return information about the issuer */ std::vector<std::string> X509_Certificate::issuer_info(const std::string& what) const { return issuer.get(X509_DN::deref_info_field(what)); } /* * Return the public key in this certificate */ Public_Key* X509_Certificate::subject_public_key() const { DataSource_Memory source(subject.get1("X509.Certificate.public_key")); return X509::load_key(source); } /* * Check if the certificate is for a CA */ bool X509_Certificate::is_CA_cert() const { if(!subject.get1_u32bit("X509v3.BasicConstraints.is_ca")) return false; if((constraints() & KEY_CERT_SIGN) || (constraints() == NO_CONSTRAINTS)) return true; return false; } /* * Return the path length constraint */ u32bit X509_Certificate::path_limit() const { return subject.get1_u32bit("X509v3.BasicConstraints.path_constraint", 0); } /* * Return the key usage constraints */ Key_Constraints X509_Certificate::constraints() const { return Key_Constraints(subject.get1_u32bit("X509v3.KeyUsage", NO_CONSTRAINTS)); } /* * Return the list of extended key usage OIDs */ std::vector<std::string> X509_Certificate::ex_constraints() const { return lookup_oids(subject.get("X509v3.ExtendedKeyUsage")); } /* * Return the list of certificate policies */ std::vector<std::string> X509_Certificate::policies() const { return lookup_oids(subject.get("X509v3.CertificatePolicies")); } /* * Return the authority key id */ MemoryVector<byte> X509_Certificate::authority_key_id() const { return issuer.get1_memvec("X509v3.AuthorityKeyIdentifier"); } /* * Return the subject key id */ MemoryVector<byte> X509_Certificate::subject_key_id() const { return subject.get1_memvec("X509v3.SubjectKeyIdentifier"); } /* * Return the certificate serial number */ MemoryVector<byte> X509_Certificate::serial_number() const { return subject.get1_memvec("X509.Certificate.serial"); } /* * Return the distinguished name of the issuer */ X509_DN X509_Certificate::issuer_dn() const { return create_dn(issuer); } /* * Return the distinguished name of the subject */ X509_DN X509_Certificate::subject_dn() const { return create_dn(subject); } /* * Compare two certificates for equality */ bool X509_Certificate::operator==(const X509_Certificate& other) const { return (sig == other.sig && sig_algo == other.sig_algo && self_signed == other.self_signed && issuer == other.issuer && subject == other.subject); } /* * X.509 Certificate Comparison */ bool operator!=(const X509_Certificate& cert1, const X509_Certificate& cert2) { return !(cert1 == cert2); } std::string X509_Certificate::to_string() const { const char* dn_fields[] = { "Name", "Email", "Organization", "Organizational Unit", "Locality", "State", "Country", "IP", "DNS", "URI", "PKIX.XMPPAddr", 0 }; std::ostringstream out; for(size_t i = 0; dn_fields[i]; ++i) { const std::vector<std::string> vals = this->subject_info(dn_fields[i]); if(vals.empty()) continue; out << "Subject " << dn_fields[i] << ":"; for(size_t j = 0; j != vals.size(); ++j) out << " " << vals[j]; out << "\n"; } for(size_t i = 0; dn_fields[i]; ++i) { const std::vector<std::string> vals = this->issuer_info(dn_fields[i]); if(vals.empty()) continue; out << "Issuer " << dn_fields[i] << ":"; for(size_t j = 0; j != vals.size(); ++j) out << " " << vals[j]; out << "\n"; } out << "Version: " << this->x509_version() << "\n"; out << "Not valid before: " << this->start_time() << "\n"; out << "Not valid after: " << this->end_time() << "\n"; out << "Constraints:\n"; Key_Constraints constraints = this->constraints(); if(constraints == NO_CONSTRAINTS) out << " None\n"; else { if(constraints & DIGITAL_SIGNATURE) out << " Digital Signature\n"; if(constraints & NON_REPUDIATION) out << " Non-Repuidation\n"; if(constraints & KEY_ENCIPHERMENT) out << " Key Encipherment\n"; if(constraints & DATA_ENCIPHERMENT) out << " Data Encipherment\n"; if(constraints & KEY_AGREEMENT) out << " Key Agreement\n"; if(constraints & KEY_CERT_SIGN) out << " Cert Sign\n"; if(constraints & CRL_SIGN) out << " CRL Sign\n"; } std::vector<std::string> policies = this->policies(); if(policies.size()) { out << "Policies: " << "\n"; for(size_t i = 0; i != policies.size(); i++) out << " " << policies[i] << "\n"; } std::vector<std::string> ex_constraints = this->ex_constraints(); if(ex_constraints.size()) { out << "Extended Constraints:\n"; for(size_t i = 0; i != ex_constraints.size(); i++) out << " " << ex_constraints[i] << "\n"; } out << "Signature algorithm: " << OIDS::lookup(this->signature_algorithm().oid) << "\n"; out << "Serial number: " << hex_encode(this->serial_number()) << "\n"; if(this->authority_key_id().size()) out << "Authority keyid: " << hex_encode(this->authority_key_id()) << "\n"; if(this->subject_key_id().size()) out << "Subject keyid: " << hex_encode(this->subject_key_id()) << "\n"; X509_PublicKey* pubkey = this->subject_public_key(); out << "Public Key:\n" << X509::PEM_encode(*pubkey); delete pubkey; return out.str(); } /* * Create and populate a X509_DN */ X509_DN create_dn(const Data_Store& info) { class DN_Matcher : public Data_Store::Matcher { public: bool operator()(const std::string& key, const std::string&) const { if(key.find("X520.") != std::string::npos) return true; return false; } }; std::multimap<std::string, std::string> names = info.search_with(DN_Matcher()); X509_DN dn; std::multimap<std::string, std::string>::iterator i; for(i = names.begin(); i != names.end(); ++i) dn.add_attribute(i->first, i->second); return dn; } /* * Create and populate an AlternativeName */ AlternativeName create_alt_name(const Data_Store& info) { class AltName_Matcher : public Data_Store::Matcher { public: bool operator()(const std::string& key, const std::string&) const { for(size_t i = 0; i != matches.size(); ++i) if(key.compare(matches[i]) == 0) return true; return false; } AltName_Matcher(const std::string& match_any_of) { matches = split_on(match_any_of, '/'); } private: std::vector<std::string> matches; }; std::multimap<std::string, std::string> names = info.search_with(AltName_Matcher("RFC822/DNS/URI/IP")); AlternativeName alt_name; std::multimap<std::string, std::string>::iterator i; for(i = names.begin(); i != names.end(); ++i) alt_name.add_attribute(i->first, i->second); return alt_name; } } /* * CRL Entry * (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Create a CRL_Entry */ CRL_Entry::CRL_Entry(bool t_on_unknown_crit) : throw_on_unknown_critical(t_on_unknown_crit) { reason = UNSPECIFIED; } /* * Create a CRL_Entry */ CRL_Entry::CRL_Entry(const X509_Certificate& cert, CRL_Code why) : throw_on_unknown_critical(false) { serial = cert.serial_number(); time = X509_Time(system_time()); reason = why; } /* * Compare two CRL_Entrys for equality */ bool operator==(const CRL_Entry& a1, const CRL_Entry& a2) { if(a1.serial_number() != a2.serial_number()) return false; if(a1.expire_time() != a2.expire_time()) return false; if(a1.reason_code() != a2.reason_code()) return false; return true; } /* * Compare two CRL_Entrys for inequality */ bool operator!=(const CRL_Entry& a1, const CRL_Entry& a2) { return !(a1 == a2); } /* * DER encode a CRL_Entry */ void CRL_Entry::encode_into(DER_Encoder& der) const { Extensions extensions; extensions.add(new Cert_Extension::CRL_ReasonCode(reason)); der.start_cons(SEQUENCE) .encode(BigInt::decode(serial)) .encode(time) .start_cons(SEQUENCE) .encode(extensions) .end_cons() .end_cons(); } /* * Decode a BER encoded CRL_Entry */ void CRL_Entry::decode_from(BER_Decoder& source) { BigInt serial_number_bn; reason = UNSPECIFIED; BER_Decoder entry = source.start_cons(SEQUENCE); entry.decode(serial_number_bn).decode(time); if(entry.more_items()) { Extensions extensions(throw_on_unknown_critical); entry.decode(extensions); Data_Store info; extensions.contents_to(info, info); reason = CRL_Code(info.get1_u32bit("X509v3.CRLReasonCode")); } entry.end_cons(); serial = BigInt::encode(serial_number_bn); } } /* * X.509 CRL * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Load a X.509 CRL */ X509_CRL::X509_CRL(DataSource& in, bool touc) : X509_Object(in, "X509 CRL/CRL"), throw_on_unknown_critical(touc) { do_decode(); } /* * Load a X.509 CRL */ X509_CRL::X509_CRL(const std::string& in, bool touc) : X509_Object(in, "CRL/X509 CRL"), throw_on_unknown_critical(touc) { do_decode(); } /* * Decode the TBSCertList data */ void X509_CRL::force_decode() { BER_Decoder tbs_crl(tbs_bits); size_t version; tbs_crl.decode_optional(version, INTEGER, UNIVERSAL); if(version != 0 && version != 1) throw X509_CRL_Error("Unknown X.509 CRL version " + to_string(version+1)); AlgorithmIdentifier sig_algo_inner; tbs_crl.decode(sig_algo_inner); if(sig_algo != sig_algo_inner) throw X509_CRL_Error("Algorithm identifier mismatch"); X509_DN dn_issuer; tbs_crl.decode(dn_issuer); info.add(dn_issuer.contents()); X509_Time start, end; tbs_crl.decode(start).decode(end); info.add("X509.CRL.start", start.readable_string()); info.add("X509.CRL.end", end.readable_string()); BER_Object next = tbs_crl.get_next_object(); if(next.type_tag == SEQUENCE && next.class_tag == CONSTRUCTED) { BER_Decoder cert_list(next.value); while(cert_list.more_items()) { CRL_Entry entry(throw_on_unknown_critical); cert_list.decode(entry); revoked.push_back(entry); } next = tbs_crl.get_next_object(); } if(next.type_tag == 0 && next.class_tag == ASN1_Tag(CONSTRUCTED | CONTEXT_SPECIFIC)) { BER_Decoder crl_options(next.value); Extensions extensions(throw_on_unknown_critical); crl_options.decode(extensions).verify_end(); extensions.contents_to(info, info); next = tbs_crl.get_next_object(); } if(next.type_tag != NO_OBJECT) throw X509_CRL_Error("Unknown tag in CRL"); tbs_crl.verify_end(); } /* * Return the list of revoked certificates */ std::vector<CRL_Entry> X509_CRL::get_revoked() const { return revoked; } /* * Return the distinguished name of the issuer */ X509_DN X509_CRL::issuer_dn() const { return create_dn(info); } /* * Return the key identifier of the issuer */ MemoryVector<byte> X509_CRL::authority_key_id() const { return info.get1_memvec("X509v3.AuthorityKeyIdentifier"); } /* * Return the CRL number of this CRL */ u32bit X509_CRL::crl_number() const { return info.get1_u32bit("X509v3.CRLNumber"); } /* * Return the issue data of the CRL */ X509_Time X509_CRL::this_update() const { return info.get1("X509.CRL.start"); } /* * Return the date when a new CRL will be issued */ X509_Time X509_CRL::next_update() const { return info.get1("X509.CRL.end"); } } /* * X.509 Certificate Options * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Set when the certificate should become valid */ void X509_Cert_Options::not_before(const std::string& time_string) { start = X509_Time(time_string); } /* * Set when the certificate should expire */ void X509_Cert_Options::not_after(const std::string& time_string) { end = X509_Time(time_string); } /* * Set key constraint information */ void X509_Cert_Options::add_constraints(Key_Constraints usage) { constraints = usage; } /* * Set key constraint information */ void X509_Cert_Options::add_ex_constraint(const OID& oid) { ex_constraints.push_back(oid); } /* * Set key constraint information */ void X509_Cert_Options::add_ex_constraint(const std::string& oid_str) { ex_constraints.push_back(OIDS::lookup(oid_str)); } /* * Mark this certificate for CA usage */ void X509_Cert_Options::CA_key(size_t limit) { is_CA = true; path_limit = limit; } /* * Do basic sanity checks */ void X509_Cert_Options::sanity_check() const { if(common_name == "" || country == "") throw Encoding_Error("X.509 certificate: name and country MUST be set"); if(country.size() != 2) throw Encoding_Error("Invalid ISO country code: " + country); if(start >= end) throw Encoding_Error("X509_Cert_Options: invalid time constraints"); } /* * Initialize the certificate options */ X509_Cert_Options::X509_Cert_Options(const std::string& initial_opts, u32bit expiration_time_in_seconds) { is_CA = false; path_limit = 0; constraints = NO_CONSTRAINTS; const u64bit now = system_time(); start = X509_Time(now); end = X509_Time(now + expiration_time_in_seconds); if(initial_opts == "") return; std::vector<std::string> parsed = split_on(initial_opts, '/'); if(parsed.size() > 4) throw Invalid_Argument("X.509 cert options: Too many names: " + initial_opts); if(parsed.size() >= 1) common_name = parsed[0]; if(parsed.size() >= 2) country = parsed[1]; if(parsed.size() >= 3) organization = parsed[2]; if(parsed.size() == 4) org_unit = parsed[3]; } } /* * PKCS #10/Self Signed Cert Creation * (C) 1999-2008 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <memory> namespace Botan { namespace { /* * Load information from the X509_Cert_Options */ void load_info(const X509_Cert_Options& opts, X509_DN& subject_dn, AlternativeName& subject_alt) { subject_dn.add_attribute("X520.CommonName", opts.common_name); subject_dn.add_attribute("X520.Country", opts.country); subject_dn.add_attribute("X520.State", opts.state); subject_dn.add_attribute("X520.Locality", opts.locality); subject_dn.add_attribute("X520.Organization", opts.organization); subject_dn.add_attribute("X520.OrganizationalUnit", opts.org_unit); subject_dn.add_attribute("X520.SerialNumber", opts.serial_number); subject_alt = AlternativeName(opts.email, opts.uri, opts.dns, opts.ip); subject_alt.add_othername(OIDS::lookup("PKIX.XMPPAddr"), opts.xmpp, UTF8_STRING); } } namespace X509 { /* * Create a new self-signed X.509 certificate */ X509_Certificate create_self_signed_cert(const X509_Cert_Options& opts, const Private_Key& key, const std::string& hash_fn, RandomNumberGenerator& rng) { AlgorithmIdentifier sig_algo; X509_DN subject_dn; AlternativeName subject_alt; opts.sanity_check(); MemoryVector<byte> pub_key = X509::BER_encode(key); std::auto_ptr<PK_Signer> signer(choose_sig_format(key, hash_fn, sig_algo)); load_info(opts, subject_dn, subject_alt); Key_Constraints constraints; if(opts.is_CA) constraints = Key_Constraints(KEY_CERT_SIGN | CRL_SIGN); else constraints = find_constraints(key, opts.constraints); Extensions extensions; extensions.add( new Cert_Extension::Basic_Constraints(opts.is_CA, opts.path_limit), true); extensions.add(new Cert_Extension::Key_Usage(constraints), true); extensions.add(new Cert_Extension::Subject_Key_ID(pub_key)); extensions.add( new Cert_Extension::Subject_Alternative_Name(subject_alt)); extensions.add( new Cert_Extension::Extended_Key_Usage(opts.ex_constraints)); return X509_CA::make_cert(signer.get(), rng, sig_algo, pub_key, opts.start, opts.end, subject_dn, subject_dn, extensions); } /* * Create a PKCS #10 certificate request */ PKCS10_Request create_cert_req(const X509_Cert_Options& opts, const Private_Key& key, const std::string& hash_fn, RandomNumberGenerator& rng) { AlgorithmIdentifier sig_algo; X509_DN subject_dn; AlternativeName subject_alt; opts.sanity_check(); MemoryVector<byte> pub_key = X509::BER_encode(key); std::auto_ptr<PK_Signer> signer(choose_sig_format(key, hash_fn, sig_algo)); load_info(opts, subject_dn, subject_alt); const size_t PKCS10_VERSION = 0; Extensions extensions; extensions.add( new Cert_Extension::Basic_Constraints(opts.is_CA, opts.path_limit)); extensions.add( new Cert_Extension::Key_Usage( opts.is_CA ? Key_Constraints(KEY_CERT_SIGN | CRL_SIGN) : find_constraints(key, opts.constraints) ) ); extensions.add( new Cert_Extension::Extended_Key_Usage(opts.ex_constraints)); extensions.add( new Cert_Extension::Subject_Alternative_Name(subject_alt)); DER_Encoder tbs_req; tbs_req.start_cons(SEQUENCE) .encode(PKCS10_VERSION) .encode(subject_dn) .raw_bytes(pub_key) .start_explicit(0); if(opts.challenge != "") { ASN1_String challenge(opts.challenge, DIRECTORY_STRING); tbs_req.encode( Attribute("PKCS9.ChallengePassword", DER_Encoder().encode(challenge).get_contents() ) ); } tbs_req.encode( Attribute("PKCS9.ExtensionRequest", DER_Encoder() .start_cons(SEQUENCE) .encode(extensions) .end_cons() .get_contents() ) ) .end_explicit() .end_cons(); DataSource_Memory source( X509_Object::make_signed(signer.get(), rng, sig_algo, tbs_req.get_contents()) ); return PKCS10_Request(source); } } } /* * X.509 Certificate Store * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> #include <memory> namespace Botan { namespace { /* * Do a validity check */ s32bit validity_check(const X509_Time& start, const X509_Time& end, u64bit current_time, u32bit slack) { const s32bit NOT_YET_VALID = -1, VALID_TIME = 0, EXPIRED = 1; if(start.cmp(current_time + slack) > 0) return NOT_YET_VALID; if(end.cmp(current_time - slack) < 0) return EXPIRED; return VALID_TIME; } /* * Compare the value of unique ID fields */ bool compare_ids(const MemoryVector<byte>& id1, const MemoryVector<byte>& id2) { if(!id1.size() || !id2.size()) return true; return (id1 == id2); } /* * Check a particular usage restriction */ bool check_usage(const X509_Certificate& cert, X509_Store::Cert_Usage usage, X509_Store::Cert_Usage check_for, Key_Constraints constraints) { if((usage & check_for) == 0) return true; if(cert.constraints() == NO_CONSTRAINTS) return true; if(cert.constraints() & constraints) return true; return false; } /* * Check a particular usage restriction */ bool check_usage(const X509_Certificate& cert, X509_Store::Cert_Usage usage, X509_Store::Cert_Usage check_for, const std::string& usage_oid) { if((usage & check_for) == 0) return true; const std::vector<std::string> constraints = cert.ex_constraints(); if(constraints.empty()) return true; return std::binary_search(constraints.begin(), constraints.end(), usage_oid); } /* * Check the usage restrictions */ X509_Code usage_check(const X509_Certificate& cert, X509_Store::Cert_Usage usage) { if(usage == X509_Store::ANY) return VERIFIED; if(!check_usage(cert, usage, X509_Store::CRL_SIGNING, CRL_SIGN)) return CA_CERT_NOT_FOR_CRL_ISSUER; if(!check_usage(cert, usage, X509_Store::TLS_SERVER, "PKIX.ServerAuth")) return INVALID_USAGE; if(!check_usage(cert, usage, X509_Store::TLS_CLIENT, "PKIX.ClientAuth")) return INVALID_USAGE; if(!check_usage(cert, usage, X509_Store::CODE_SIGNING, "PKIX.CodeSigning")) return INVALID_USAGE; if(!check_usage(cert, usage, X509_Store::EMAIL_PROTECTION, "PKIX.EmailProtection")) return INVALID_USAGE; if(!check_usage(cert, usage, X509_Store::TIME_STAMPING, "PKIX.TimeStamping")) return INVALID_USAGE; return VERIFIED; } } /* * Define equality for revocation data */ bool X509_Store::CRL_Data::operator==(const CRL_Data& other) const { if(issuer != other.issuer) return false; if(serial != other.serial) return false; return compare_ids(auth_key_id, other.auth_key_id); } /* * Define inequality for revocation data */ bool X509_Store::CRL_Data::operator!=(const CRL_Data& other) const { return !((*this) == other); } /* * Define an ordering for revocation data */ bool X509_Store::CRL_Data::operator<(const X509_Store::CRL_Data& other) const { if(*this == other) return false; const MemoryVector<byte>& serial1 = serial; const MemoryVector<byte>& key_id1 = auth_key_id; const MemoryVector<byte>& serial2 = other.serial; const MemoryVector<byte>& key_id2 = other.auth_key_id; if(compare_ids(key_id1, key_id2) == false) { if(std::lexicographical_compare(key_id1.begin(), key_id1.end(), key_id2.begin(), key_id2.end())) return true; if(std::lexicographical_compare(key_id2.begin(), key_id2.end(), key_id1.begin(), key_id1.end())) return false; } if(compare_ids(serial1, serial2) == false) { if(std::lexicographical_compare(serial1.begin(), serial1.end(), serial2.begin(), serial2.end())) return true; if(std::lexicographical_compare(serial2.begin(), serial2.end(), serial1.begin(), serial1.end())) return false; } return (issuer < other.issuer); } /* * X509_Store Constructor */ X509_Store::X509_Store(u32bit slack, u32bit cache_timeout) { revoked_info_valid = true; validation_cache_timeout = cache_timeout; time_slack = slack; } /* * X509_Store Copy Constructor */ X509_Store::X509_Store(const X509_Store& other) { certs = other.certs; revoked = other.revoked; revoked_info_valid = other.revoked_info_valid; for(size_t j = 0; j != other.stores.size(); ++j) stores[j] = other.stores[j]->clone(); time_slack = other.time_slack; } /* * X509_Store Destructor */ X509_Store::~X509_Store() { for(size_t j = 0; j != stores.size(); ++j) delete stores[j]; } /* * Verify a certificate's authenticity */ X509_Code X509_Store::validate_cert(const X509_Certificate& cert, Cert_Usage cert_usage) { recompute_revoked_info(); std::vector<size_t> indexes; X509_Code chaining_result = construct_cert_chain(cert, indexes); if(chaining_result != VERIFIED) return chaining_result; const u64bit current_time = system_time(); s32bit time_check = validity_check(cert.start_time(), cert.end_time(), current_time, time_slack); if(time_check < 0) return CERT_NOT_YET_VALID; else if(time_check > 0) return CERT_HAS_EXPIRED; X509_Code sig_check_result = check_sig(cert, certs[indexes[0]]); if(sig_check_result != VERIFIED) return sig_check_result; if(is_revoked(cert)) return CERT_IS_REVOKED; for(size_t j = 0; j != indexes.size() - 1; ++j) { const X509_Certificate& current_cert = certs[indexes[j]].cert; time_check = validity_check(current_cert.start_time(), current_cert.end_time(), current_time, time_slack); if(time_check < 0) return CERT_NOT_YET_VALID; else if(time_check > 0) return CERT_HAS_EXPIRED; sig_check_result = check_sig(certs[indexes[j]], certs[indexes[j+1]]); if(sig_check_result != VERIFIED) return sig_check_result; } return usage_check(cert, cert_usage); } /* * Find this certificate */ size_t X509_Store::find_cert(const X509_DN& subject_dn, const MemoryRegion<byte>& subject_key_id) const { for(size_t j = 0; j != certs.size(); ++j) { const X509_Certificate& this_cert = certs[j].cert; if(compare_ids(this_cert.subject_key_id(), subject_key_id) && this_cert.subject_dn() == subject_dn) return j; } return NO_CERT_FOUND; } /* * Find the parent of this certificate */ size_t X509_Store::find_parent_of(const X509_Certificate& cert) { const X509_DN issuer_dn = cert.issuer_dn(); const MemoryVector<byte> auth_key_id = cert.authority_key_id(); size_t index = find_cert(issuer_dn, auth_key_id); if(index != NO_CERT_FOUND) return index; for(size_t j = 0; j != stores.size(); ++j) { std::vector<X509_Certificate> got = stores[j]->find_cert_by_subject_and_key_id(issuer_dn, auth_key_id); for(size_t k = 0; k != got.size(); ++k) add_cert(got[k]); } return find_cert(issuer_dn, auth_key_id); } /* * Construct a chain of certificate relationships */ X509_Code X509_Store::construct_cert_chain(const X509_Certificate& end_cert, std::vector<size_t>& indexes, bool need_full_chain) { size_t parent = find_parent_of(end_cert); while(true) { if(parent == NO_CERT_FOUND) return CERT_ISSUER_NOT_FOUND; indexes.push_back(parent); if(certs[parent].is_verified(validation_cache_timeout)) if(certs[parent].verify_result() != VERIFIED) return certs[parent].verify_result(); const X509_Certificate& parent_cert = certs[parent].cert; if(!parent_cert.is_CA_cert()) return CA_CERT_NOT_FOR_CERT_ISSUER; if(certs[parent].is_trusted()) break; if(parent_cert.is_self_signed()) return CANNOT_ESTABLISH_TRUST; if(parent_cert.path_limit() < indexes.size() - 1) return CERT_CHAIN_TOO_LONG; parent = find_parent_of(parent_cert); } if(need_full_chain) return VERIFIED; while(true) { if(indexes.size() < 2) break; const size_t cert = indexes.back(); if(certs[cert].is_verified(validation_cache_timeout)) { if(certs[cert].verify_result() != VERIFIED) throw Internal_Error("X509_Store::construct_cert_chain"); indexes.pop_back(); } else break; } const size_t last_cert = indexes.back(); const size_t parent_of_last_cert = find_parent_of(certs[last_cert].cert); if(parent_of_last_cert == NO_CERT_FOUND) return CERT_ISSUER_NOT_FOUND; indexes.push_back(parent_of_last_cert); return VERIFIED; } /* * Check the CAs signature on a certificate */ X509_Code X509_Store::check_sig(const Cert_Info& cert_info, const Cert_Info& ca_cert_info) const { if(cert_info.is_verified(validation_cache_timeout)) return cert_info.verify_result(); const X509_Certificate& cert = cert_info.cert; const X509_Certificate& ca_cert = ca_cert_info.cert; X509_Code verify_code = check_sig(cert, ca_cert.subject_public_key()); cert_info.set_result(verify_code); return verify_code; } /* * Check a CA's signature */ X509_Code X509_Store::check_sig(const X509_Object& object, Public_Key* key) { std::auto_ptr<Public_Key> pub_key(key); try { std::vector<std::string> sig_info = split_on(OIDS::lookup(object.signature_algorithm().oid), '/'); if(sig_info.size() != 2 || sig_info[0] != pub_key->algo_name()) return SIGNATURE_ERROR; std::string padding = sig_info[1]; Signature_Format format; if(key->message_parts() >= 2) format = DER_SEQUENCE; else format = IEEE_1363; PK_Verifier verifier(*pub_key.get(), padding, format); bool valid = verifier.verify_message(object.tbs_data(), object.signature()); if(valid) return VERIFIED; else return SIGNATURE_ERROR; } catch(Lookup_Error) { return CA_CERT_CANNOT_SIGN; } catch(Decoding_Error) { return CERT_FORMAT_ERROR; } catch(Exception) {} return UNKNOWN_X509_ERROR; } /* * Recompute the revocation status of the certs */ void X509_Store::recompute_revoked_info() const { if(revoked_info_valid) return; for(size_t j = 0; j != certs.size(); ++j) { if((certs[j].is_verified(validation_cache_timeout)) && (certs[j].verify_result() != VERIFIED)) continue; if(is_revoked(certs[j].cert)) certs[j].set_result(CERT_IS_REVOKED); } revoked_info_valid = true; } /* * Check if a certificate is revoked */ bool X509_Store::is_revoked(const X509_Certificate& cert) const { CRL_Data revoked_info; revoked_info.issuer = cert.issuer_dn(); revoked_info.serial = cert.serial_number(); revoked_info.auth_key_id = cert.authority_key_id(); if(std::binary_search(revoked.begin(), revoked.end(), revoked_info)) return true; return false; } /* * Construct a path back to a root for this cert */ std::vector<X509_Certificate> X509_Store::get_cert_chain(const X509_Certificate& cert) { std::vector<X509_Certificate> result; std::vector<size_t> indexes; X509_Code chaining_result = construct_cert_chain(cert, indexes, true); if(chaining_result != VERIFIED) throw Invalid_State("X509_Store::get_cert_chain: Can't construct chain"); for(size_t j = 0; j != indexes.size(); ++j) result.push_back(certs[indexes[j]].cert); return result; } /* * Add a certificate store to the list of stores */ void X509_Store::add_new_certstore(Certificate_Store* certstore) { stores.push_back(certstore); } /* * Add a certificate to the store */ void X509_Store::add_cert(const X509_Certificate& cert, bool trusted) { if(trusted && !cert.is_self_signed()) throw Invalid_Argument("X509_Store: Trusted certs must be self-signed"); if(find_cert(cert.subject_dn(), cert.subject_key_id()) == NO_CERT_FOUND) { revoked_info_valid = false; Cert_Info info(cert, trusted); certs.push_back(info); } else if(trusted) { for(size_t j = 0; j != certs.size(); ++j) { const X509_Certificate& this_cert = certs[j].cert; if(this_cert == cert) certs[j].trusted = trusted; } } } /* * Add one or more certificates to the store */ void X509_Store::do_add_certs(DataSource& source, bool trusted) { while(!source.end_of_data()) { try { X509_Certificate cert(source); add_cert(cert, trusted); } catch(Decoding_Error) {} catch(Invalid_Argument) {} } } /* * Add one or more certificates to the store */ void X509_Store::add_certs(DataSource& source) { do_add_certs(source, false); } /* * Add one or more certificates to the store */ void X509_Store::add_trusted_certs(DataSource& source) { do_add_certs(source, true); } /* * Add one or more certificates to the store */ X509_Code X509_Store::add_crl(const X509_CRL& crl) { s32bit time_check = validity_check(crl.this_update(), crl.next_update(), system_time(), time_slack); if(time_check < 0) return CRL_NOT_YET_VALID; else if(time_check > 0) return CRL_HAS_EXPIRED; size_t cert_index = NO_CERT_FOUND; for(size_t j = 0; j != certs.size(); ++j) { const X509_Certificate& this_cert = certs[j].cert; if(compare_ids(this_cert.subject_key_id(), crl.authority_key_id())) { if(this_cert.subject_dn() == crl.issuer_dn()) cert_index = j; } } if(cert_index == NO_CERT_FOUND) return CRL_ISSUER_NOT_FOUND; const X509_Certificate& ca_cert = certs[cert_index].cert; X509_Code verify_result = validate_cert(ca_cert, CRL_SIGNING); if(verify_result != VERIFIED) return verify_result; verify_result = check_sig(crl, ca_cert.subject_public_key()); if(verify_result != VERIFIED) return verify_result; std::vector<CRL_Entry> revoked_certs = crl.get_revoked(); for(size_t j = 0; j != revoked_certs.size(); ++j) { CRL_Data revoked_info; revoked_info.issuer = crl.issuer_dn(); revoked_info.serial = revoked_certs[j].serial_number(); revoked_info.auth_key_id = crl.authority_key_id(); std::vector<CRL_Data>::iterator p = std::find(revoked.begin(), revoked.end(), revoked_info); if(revoked_certs[j].reason_code() == REMOVE_FROM_CRL) { if(p == revoked.end()) continue; revoked.erase(p); } else { if(p != revoked.end()) continue; revoked.push_back(revoked_info); } } std::sort(revoked.begin(), revoked.end()); revoked_info_valid = false; return VERIFIED; } /* * PEM encode the set of certificates */ std::string X509_Store::PEM_encode() const { std::string cert_store; for(size_t j = 0; j != certs.size(); ++j) cert_store += certs[j].cert.PEM_encode(); return cert_store; } /* * Create a Cert_Info structure */ X509_Store::Cert_Info::Cert_Info(const X509_Certificate& c, bool t) : cert(c), trusted(t) { checked = false; result = UNKNOWN_X509_ERROR; last_checked = 0; } /* * Return the verification results */ X509_Code X509_Store::Cert_Info::verify_result() const { if(!checked) throw Invalid_State("Cert_Info::verify_result() called; not checked"); return result; } /* * Set the verification results */ void X509_Store::Cert_Info::set_result(X509_Code code) const { result = code; last_checked = system_time(); checked = true; } /* * Check if this certificate can be trusted */ bool X509_Store::Cert_Info::is_trusted() const { return trusted; } /* * Check if this certificate has been verified */ bool X509_Store::Cert_Info::is_verified(u32bit timeout) const { if(!checked) return false; if(result != VERIFIED && result != CERT_NOT_YET_VALID) return true; const u64bit current_time = system_time(); if(current_time > last_checked + timeout) checked = false; return checked; } } /* * Adler32 * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { void adler32_update(const byte input[], size_t length, u16bit& S1, u16bit& S2) { u32bit S1x = S1; u32bit S2x = S2; while(length >= 16) { S1x += input[ 0]; S2x += S1x; S1x += input[ 1]; S2x += S1x; S1x += input[ 2]; S2x += S1x; S1x += input[ 3]; S2x += S1x; S1x += input[ 4]; S2x += S1x; S1x += input[ 5]; S2x += S1x; S1x += input[ 6]; S2x += S1x; S1x += input[ 7]; S2x += S1x; S1x += input[ 8]; S2x += S1x; S1x += input[ 9]; S2x += S1x; S1x += input[10]; S2x += S1x; S1x += input[11]; S2x += S1x; S1x += input[12]; S2x += S1x; S1x += input[13]; S2x += S1x; S1x += input[14]; S2x += S1x; S1x += input[15]; S2x += S1x; input += 16; length -= 16; } for(size_t j = 0; j != length; ++j) { S1x += input[j]; S2x += S1x; } S1 = S1x % 65521; S2 = S2x % 65521; } } /* * Update an Adler32 Checksum */ void Adler32::add_data(const byte input[], size_t length) { const size_t PROCESS_AMOUNT = 5552; while(length >= PROCESS_AMOUNT) { adler32_update(input, PROCESS_AMOUNT, S1, S2); input += PROCESS_AMOUNT; length -= PROCESS_AMOUNT; } adler32_update(input, length, S1, S2); } /* * Finalize an Adler32 Checksum */ void Adler32::final_result(byte output[]) { store_be(output, S2, S1); clear(); } } /* * CRC24 * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Update a CRC24 Checksum */ void CRC24::add_data(const byte input[], size_t length) { const u32bit TABLE[256] = { 0x00000000, 0x00864CFB, 0x008AD50D, 0x000C99F6, 0x0093E6E1, 0x0015AA1A, 0x001933EC, 0x009F7F17, 0x00A18139, 0x0027CDC2, 0x002B5434, 0x00AD18CF, 0x003267D8, 0x00B42B23, 0x00B8B2D5, 0x003EFE2E, 0x00C54E89, 0x00430272, 0x004F9B84, 0x00C9D77F, 0x0056A868, 0x00D0E493, 0x00DC7D65, 0x005A319E, 0x0064CFB0, 0x00E2834B, 0x00EE1ABD, 0x00685646, 0x00F72951, 0x007165AA, 0x007DFC5C, 0x00FBB0A7, 0x000CD1E9, 0x008A9D12, 0x008604E4, 0x0000481F, 0x009F3708, 0x00197BF3, 0x0015E205, 0x0093AEFE, 0x00AD50D0, 0x002B1C2B, 0x002785DD, 0x00A1C926, 0x003EB631, 0x00B8FACA, 0x00B4633C, 0x00322FC7, 0x00C99F60, 0x004FD39B, 0x00434A6D, 0x00C50696, 0x005A7981, 0x00DC357A, 0x00D0AC8C, 0x0056E077, 0x00681E59, 0x00EE52A2, 0x00E2CB54, 0x006487AF, 0x00FBF8B8, 0x007DB443, 0x00712DB5, 0x00F7614E, 0x0019A3D2, 0x009FEF29, 0x009376DF, 0x00153A24, 0x008A4533, 0x000C09C8, 0x0000903E, 0x0086DCC5, 0x00B822EB, 0x003E6E10, 0x0032F7E6, 0x00B4BB1D, 0x002BC40A, 0x00AD88F1, 0x00A11107, 0x00275DFC, 0x00DCED5B, 0x005AA1A0, 0x00563856, 0x00D074AD, 0x004F0BBA, 0x00C94741, 0x00C5DEB7, 0x0043924C, 0x007D6C62, 0x00FB2099, 0x00F7B96F, 0x0071F594, 0x00EE8A83, 0x0068C678, 0x00645F8E, 0x00E21375, 0x0015723B, 0x00933EC0, 0x009FA736, 0x0019EBCD, 0x008694DA, 0x0000D821, 0x000C41D7, 0x008A0D2C, 0x00B4F302, 0x0032BFF9, 0x003E260F, 0x00B86AF4, 0x002715E3, 0x00A15918, 0x00ADC0EE, 0x002B8C15, 0x00D03CB2, 0x00567049, 0x005AE9BF, 0x00DCA544, 0x0043DA53, 0x00C596A8, 0x00C90F5E, 0x004F43A5, 0x0071BD8B, 0x00F7F170, 0x00FB6886, 0x007D247D, 0x00E25B6A, 0x00641791, 0x00688E67, 0x00EEC29C, 0x003347A4, 0x00B50B5F, 0x00B992A9, 0x003FDE52, 0x00A0A145, 0x0026EDBE, 0x002A7448, 0x00AC38B3, 0x0092C69D, 0x00148A66, 0x00181390, 0x009E5F6B, 0x0001207C, 0x00876C87, 0x008BF571, 0x000DB98A, 0x00F6092D, 0x007045D6, 0x007CDC20, 0x00FA90DB, 0x0065EFCC, 0x00E3A337, 0x00EF3AC1, 0x0069763A, 0x00578814, 0x00D1C4EF, 0x00DD5D19, 0x005B11E2, 0x00C46EF5, 0x0042220E, 0x004EBBF8, 0x00C8F703, 0x003F964D, 0x00B9DAB6, 0x00B54340, 0x00330FBB, 0x00AC70AC, 0x002A3C57, 0x0026A5A1, 0x00A0E95A, 0x009E1774, 0x00185B8F, 0x0014C279, 0x00928E82, 0x000DF195, 0x008BBD6E, 0x00872498, 0x00016863, 0x00FAD8C4, 0x007C943F, 0x00700DC9, 0x00F64132, 0x00693E25, 0x00EF72DE, 0x00E3EB28, 0x0065A7D3, 0x005B59FD, 0x00DD1506, 0x00D18CF0, 0x0057C00B, 0x00C8BF1C, 0x004EF3E7, 0x00426A11, 0x00C426EA, 0x002AE476, 0x00ACA88D, 0x00A0317B, 0x00267D80, 0x00B90297, 0x003F4E6C, 0x0033D79A, 0x00B59B61, 0x008B654F, 0x000D29B4, 0x0001B042, 0x0087FCB9, 0x001883AE, 0x009ECF55, 0x009256A3, 0x00141A58, 0x00EFAAFF, 0x0069E604, 0x00657FF2, 0x00E33309, 0x007C4C1E, 0x00FA00E5, 0x00F69913, 0x0070D5E8, 0x004E2BC6, 0x00C8673D, 0x00C4FECB, 0x0042B230, 0x00DDCD27, 0x005B81DC, 0x0057182A, 0x00D154D1, 0x0026359F, 0x00A07964, 0x00ACE092, 0x002AAC69, 0x00B5D37E, 0x00339F85, 0x003F0673, 0x00B94A88, 0x0087B4A6, 0x0001F85D, 0x000D61AB, 0x008B2D50, 0x00145247, 0x00921EBC, 0x009E874A, 0x0018CBB1, 0x00E37B16, 0x006537ED, 0x0069AE1B, 0x00EFE2E0, 0x00709DF7, 0x00F6D10C, 0x00FA48FA, 0x007C0401, 0x0042FA2F, 0x00C4B6D4, 0x00C82F22, 0x004E63D9, 0x00D11CCE, 0x00575035, 0x005BC9C3, 0x00DD8538 }; u32bit tmp = crc; while(length >= 16) { tmp = TABLE[((tmp >> 16) ^ input[ 0]) & 0xFF] ^ (tmp << 8); tmp = TABLE[((tmp >> 16) ^ input[ 1]) & 0xFF] ^ (tmp << 8); tmp = TABLE[((tmp >> 16) ^ input[ 2]) & 0xFF] ^ (tmp << 8); tmp = TABLE[((tmp >> 16) ^ input[ 3]) & 0xFF] ^ (tmp << 8); tmp = TABLE[((tmp >> 16) ^ input[ 4]) & 0xFF] ^ (tmp << 8); tmp = TABLE[((tmp >> 16) ^ input[ 5]) & 0xFF] ^ (tmp << 8); tmp = TABLE[((tmp >> 16) ^ input[ 6]) & 0xFF] ^ (tmp << 8); tmp = TABLE[((tmp >> 16) ^ input[ 7]) & 0xFF] ^ (tmp << 8); tmp = TABLE[((tmp >> 16) ^ input[ 8]) & 0xFF] ^ (tmp << 8); tmp = TABLE[((tmp >> 16) ^ input[ 9]) & 0xFF] ^ (tmp << 8); tmp = TABLE[((tmp >> 16) ^ input[10]) & 0xFF] ^ (tmp << 8); tmp = TABLE[((tmp >> 16) ^ input[11]) & 0xFF] ^ (tmp << 8); tmp = TABLE[((tmp >> 16) ^ input[12]) & 0xFF] ^ (tmp << 8); tmp = TABLE[((tmp >> 16) ^ input[13]) & 0xFF] ^ (tmp << 8); tmp = TABLE[((tmp >> 16) ^ input[14]) & 0xFF] ^ (tmp << 8); tmp = TABLE[((tmp >> 16) ^ input[15]) & 0xFF] ^ (tmp << 8); input += 16; length -= 16; } for(size_t i = 0; i != length; ++i) tmp = TABLE[((tmp >> 16) ^ input[i]) & 0xFF] ^ (tmp << 8); crc = tmp; } /* * Finalize a CRC24 Checksum */ void CRC24::final_result(byte output[]) { for(size_t i = 0; i != 3; ++i) output[i] = get_byte(i+1, crc); clear(); } } /* * CRC32 * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Update a CRC32 Checksum */ void CRC32::add_data(const byte input[], size_t length) { const u32bit TABLE[256] = { 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F, 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45, 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D }; u32bit tmp = crc; while(length >= 16) { tmp = TABLE[(tmp ^ input[ 0]) & 0xFF] ^ (tmp >> 8); tmp = TABLE[(tmp ^ input[ 1]) & 0xFF] ^ (tmp >> 8); tmp = TABLE[(tmp ^ input[ 2]) & 0xFF] ^ (tmp >> 8); tmp = TABLE[(tmp ^ input[ 3]) & 0xFF] ^ (tmp >> 8); tmp = TABLE[(tmp ^ input[ 4]) & 0xFF] ^ (tmp >> 8); tmp = TABLE[(tmp ^ input[ 5]) & 0xFF] ^ (tmp >> 8); tmp = TABLE[(tmp ^ input[ 6]) & 0xFF] ^ (tmp >> 8); tmp = TABLE[(tmp ^ input[ 7]) & 0xFF] ^ (tmp >> 8); tmp = TABLE[(tmp ^ input[ 8]) & 0xFF] ^ (tmp >> 8); tmp = TABLE[(tmp ^ input[ 9]) & 0xFF] ^ (tmp >> 8); tmp = TABLE[(tmp ^ input[10]) & 0xFF] ^ (tmp >> 8); tmp = TABLE[(tmp ^ input[11]) & 0xFF] ^ (tmp >> 8); tmp = TABLE[(tmp ^ input[12]) & 0xFF] ^ (tmp >> 8); tmp = TABLE[(tmp ^ input[13]) & 0xFF] ^ (tmp >> 8); tmp = TABLE[(tmp ^ input[14]) & 0xFF] ^ (tmp >> 8); tmp = TABLE[(tmp ^ input[15]) & 0xFF] ^ (tmp >> 8); input += 16; length -= 16; } for(size_t i = 0; i != length; ++i) tmp = TABLE[(tmp ^ input[i]) & 0xFF] ^ (tmp >> 8); crc = tmp; } /* * Finalize a CRC32 Checksum */ void CRC32::final_result(byte output[]) { crc ^= 0xFFFFFFFF; store_be(crc, output); clear(); } } /* * Base64 Encoding and Decoding * (C) 2010 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <stdexcept> namespace Botan { namespace { static const byte BIN_TO_BASE64[64] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' }; void do_base64_encode(char out[4], const byte in[3]) { out[0] = BIN_TO_BASE64[((in[0] & 0xFC) >> 2)]; out[1] = BIN_TO_BASE64[((in[0] & 0x03) << 4) | (in[1] >> 4)]; out[2] = BIN_TO_BASE64[((in[1] & 0x0F) << 2) | (in[2] >> 6)]; out[3] = BIN_TO_BASE64[((in[2] & 0x3F) )]; } } size_t base64_encode(char out[], const byte in[], size_t input_length, size_t& input_consumed, bool final_inputs) { input_consumed = 0; size_t input_remaining = input_length; size_t output_produced = 0; while(input_remaining >= 3) { do_base64_encode(out + output_produced, in + input_consumed); input_consumed += 3; output_produced += 4; input_remaining -= 3; } if(final_inputs && input_remaining) { byte remainder[3] = { 0 }; for(size_t i = 0; i != input_remaining; ++i) remainder[i] = in[input_consumed + i]; do_base64_encode(out + output_produced, remainder); size_t empty_bits = 8 * (3 - input_remaining); size_t index = output_produced + 4 - 1; while(empty_bits >= 8) { out[index--] = '='; empty_bits -= 6; } input_consumed += input_remaining; output_produced += 4; } return output_produced; } std::string base64_encode(const byte input[], size_t input_length) { std::string output((round_up<size_t>(input_length, 3) / 3) * 4, 0); size_t consumed = 0; size_t produced = base64_encode(&output[0], input, input_length, consumed, true); BOTAN_ASSERT_EQUAL(consumed, input_length, "Did not consume all input"); BOTAN_ASSERT_EQUAL(produced, output.size(), "Did not produce right amount"); return output; } std::string base64_encode(const MemoryRegion<byte>& input) { return base64_encode(&input[0], input.size()); } size_t base64_decode(byte output[], const char input[], size_t input_length, size_t& input_consumed, bool final_inputs, bool ignore_ws) { /* * Base64 Decoder Lookup Table * Warning: assumes ASCII encodings */ static const byte BASE64_TO_BIN[256] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x80, 0x80, 0xFF, 0xFF, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3E, 0xFF, 0xFF, 0xFF, 0x3F, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0xFF, 0xFF, 0xFF, 0x81, 0xFF, 0xFF, 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; byte* out_ptr = output; byte decode_buf[4]; size_t decode_buf_pos = 0; size_t final_truncate = 0; clear_mem(output, input_length * 3 / 4); for(size_t i = 0; i != input_length; ++i) { const byte bin = BASE64_TO_BIN[static_cast<byte>(input[i])]; if(bin <= 0x3F) { decode_buf[decode_buf_pos] = bin; decode_buf_pos += 1; } else if(!(bin == 0x81 || (bin == 0x80 && ignore_ws))) { std::string bad_char(1, input[i]); if(bad_char == "\t") bad_char = "\\t"; else if(bad_char == "\n") bad_char = "\\n"; else if(bad_char == "\r") bad_char = "\\r"; throw std::invalid_argument( std::string("base64_decode: invalid base64 character '") + bad_char + "'"); } /* * If we're at the end of the input, pad with 0s and truncate */ if(final_inputs && (i == input_length - 1)) { if(decode_buf_pos) { for(size_t i = decode_buf_pos; i != 4; ++i) decode_buf[i] = 0; final_truncate = (4 - decode_buf_pos); decode_buf_pos = 4; } } if(decode_buf_pos == 4) { out_ptr[0] = (decode_buf[0] << 2) | (decode_buf[1] >> 4); out_ptr[1] = (decode_buf[1] << 4) | (decode_buf[2] >> 2); out_ptr[2] = (decode_buf[2] << 6) | decode_buf[3]; out_ptr += 3; decode_buf_pos = 0; input_consumed = i+1; } } while(input_consumed < input_length && BASE64_TO_BIN[static_cast<byte>(input[input_consumed])] == 0x80) { ++input_consumed; } size_t written = (out_ptr - output) - final_truncate; return written; } size_t base64_decode(byte output[], const char input[], size_t input_length, bool ignore_ws) { size_t consumed = 0; size_t written = base64_decode(output, input, input_length, consumed, true, ignore_ws); if(consumed != input_length) throw std::invalid_argument("base64_decode: input did not have full bytes"); return written; } size_t base64_decode(byte output[], const std::string& input, bool ignore_ws) { return base64_decode(output, &input[0], input.length(), ignore_ws); } SecureVector<byte> base64_decode(const char input[], size_t input_length, bool ignore_ws) { SecureVector<byte> bin((round_up<size_t>(input_length, 4) * 3) / 4); size_t written = base64_decode(&bin[0], input, input_length, ignore_ws); bin.resize(written); return bin; } SecureVector<byte> base64_decode(const std::string& input, bool ignore_ws) { return base64_decode(&input[0], input.size(), ignore_ws); } } /* * Hex Encoding and Decoding * (C) 2010 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <stdexcept> namespace Botan { void hex_encode(char output[], const byte input[], size_t input_length, bool uppercase) { static const byte BIN_TO_HEX_UPPER[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; static const byte BIN_TO_HEX_LOWER[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; const byte* tbl = uppercase ? BIN_TO_HEX_UPPER : BIN_TO_HEX_LOWER; for(size_t i = 0; i != input_length; ++i) { byte x = input[i]; output[2*i ] = tbl[(x >> 4) & 0x0F]; output[2*i+1] = tbl[(x ) & 0x0F]; } } std::string hex_encode(const MemoryRegion<byte>& input, bool uppercase) { return hex_encode(&input[0], input.size(), uppercase); } std::string hex_encode(const byte input[], size_t input_length, bool uppercase) { std::string output(2 * input_length, 0); if(input_length) hex_encode(&output[0], input, input_length, uppercase); return output; } size_t hex_decode(byte output[], const char input[], size_t input_length, size_t& input_consumed, bool ignore_ws) { /* * Mapping of hex characters to either their binary equivalent * or to an error code. * If valid hex (0-9 A-F a-f), the value. * If whitespace, then 0x80 * Otherwise 0xFF * Warning: this table assumes ASCII character encodings */ static const byte HEX_TO_BIN[256] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x80, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; byte* out_ptr = output; bool top_nibble = true; clear_mem(output, input_length / 2); for(size_t i = 0; i != input_length; ++i) { const byte bin = HEX_TO_BIN[static_cast<byte>(input[i])]; if(bin >= 0x10) { if(bin == 0x80 && ignore_ws) continue; std::string bad_char(1, input[i]); if(bad_char == "\t") bad_char = "\\t"; else if(bad_char == "\n") bad_char = "\\n"; throw std::invalid_argument( std::string("hex_decode: invalid hex character '") + bad_char + "'"); } *out_ptr |= bin << (top_nibble*4); top_nibble = !top_nibble; if(top_nibble) ++out_ptr; } input_consumed = input_length; size_t written = (out_ptr - output); /* * We only got half of a byte at the end; zap the half-written * output and mark it as unread */ if(!top_nibble) { *out_ptr = 0; input_consumed -= 1; } return written; } size_t hex_decode(byte output[], const char input[], size_t input_length, bool ignore_ws) { size_t consumed = 0; size_t written = hex_decode(output, input, input_length, consumed, ignore_ws); if(consumed != input_length) throw std::invalid_argument("hex_decode: input did not have full bytes"); return written; } size_t hex_decode(byte output[], const std::string& input, bool ignore_ws) { return hex_decode(output, &input[0], input.length(), ignore_ws); } SecureVector<byte> hex_decode(const char input[], size_t input_length, bool ignore_ws) { SecureVector<byte> bin(1 + input_length / 2); size_t written = hex_decode(&bin[0], input, input_length, ignore_ws); bin.resize(written); return bin; } SecureVector<byte> hex_decode(const std::string& input, bool ignore_ws) { return hex_decode(&input[0], input.size(), ignore_ws); } } /* * OpenPGP Codec * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * OpenPGP Base64 encoding */ std::string PGP_encode( const byte input[], size_t length, const std::string& label, const std::map<std::string, std::string>& headers) { const std::string PGP_HEADER = "-----BEGIN PGP " + label + "-----\n"; const std::string PGP_TRAILER = "-----END PGP " + label + "-----\n"; const size_t PGP_WIDTH = 64; std::string pgp_encoded = PGP_HEADER; if(headers.find("Version") != headers.end()) pgp_encoded += "Version: " + headers.find("Version")->second + '\n'; std::map<std::string, std::string>::const_iterator i = headers.begin(); while(i != headers.end()) { if(i->first != "Version") pgp_encoded += i->first + ": " + i->second + '\n'; ++i; } pgp_encoded += '\n'; Pipe pipe(new Fork( new Base64_Encoder(true, PGP_WIDTH), new Chain(new Hash_Filter(new CRC24), new Base64_Encoder) ) ); pipe.process_msg(input, length); pgp_encoded += pipe.read_all_as_string(0); pgp_encoded += '=' + pipe.read_all_as_string(1) + '\n'; pgp_encoded += PGP_TRAILER; return pgp_encoded; } /* * OpenPGP Base64 encoding */ std::string PGP_encode(const byte input[], size_t length, const std::string& type) { std::map<std::string, std::string> empty; return PGP_encode(input, length, type, empty); } /* * OpenPGP Base64 decoding */ SecureVector<byte> PGP_decode(DataSource& source, std::string& label, std::map<std::string, std::string>& headers) { const size_t RANDOM_CHAR_LIMIT = 5; const std::string PGP_HEADER1 = "-----BEGIN PGP "; const std::string PGP_HEADER2 = "-----"; size_t position = 0; while(position != PGP_HEADER1.length()) { byte b; if(!source.read_byte(b)) throw Decoding_Error("PGP: No PGP header found"); if(b == PGP_HEADER1[position]) ++position; else if(position >= RANDOM_CHAR_LIMIT) throw Decoding_Error("PGP: Malformed PGP header"); else position = 0; } position = 0; while(position != PGP_HEADER2.length()) { byte b; if(!source.read_byte(b)) throw Decoding_Error("PGP: No PGP header found"); if(b == PGP_HEADER2[position]) ++position; else if(position) throw Decoding_Error("PGP: Malformed PGP header"); if(position == 0) label += static_cast<char>(b); } headers.clear(); bool end_of_headers = false; while(!end_of_headers) { std::string this_header; byte b = 0; while(b != '\n') { if(!source.read_byte(b)) throw Decoding_Error("PGP: Bad armor header"); if(b != '\n') this_header += static_cast<char>(b); } end_of_headers = true; for(size_t j = 0; j != this_header.length(); ++j) if(!Charset::is_space(this_header[j])) end_of_headers = false; if(!end_of_headers) { std::string::size_type pos = this_header.find(": "); if(pos == std::string::npos) throw Decoding_Error("OpenPGP: Bad headers"); std::string key = this_header.substr(0, pos); std::string value = this_header.substr(pos + 2, std::string::npos); headers[key] = value; } } Pipe base64(new Base64_Decoder, new Fork(0, new Chain(new Hash_Filter(new CRC24), new Base64_Encoder) ) ); base64.start_msg(); const std::string PGP_TRAILER = "-----END PGP " + label + "-----"; position = 0; bool newline_seen = 0; std::string crc; while(position != PGP_TRAILER.length()) { byte b; if(!source.read_byte(b)) throw Decoding_Error("PGP: No PGP trailer found"); if(b == PGP_TRAILER[position]) ++position; else if(position) throw Decoding_Error("PGP: Malformed PGP trailer"); if(b == '=' && newline_seen) { while(b != '\n') { if(!source.read_byte(b)) throw Decoding_Error("PGP: Bad CRC tail"); if(b != '\n') crc += static_cast<char>(b); } } else if(b == '\n') newline_seen = true; else if(position == 0) { base64.write(b); newline_seen = false; } } base64.end_msg(); if(crc != "" && crc != base64.read_all_as_string(1)) throw Decoding_Error("PGP: Corrupt CRC"); return base64.read_all(); } /* * OpenPGP Base64 decoding */ SecureVector<byte> PGP_decode(DataSource& source, std::string& label) { std::map<std::string, std::string> ignored; return PGP_decode(source, label, ignored); } } /* * PEM Encoding/Decoding * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace PEM_Code { /* * PEM encode BER/DER-encoded objects */ std::string encode(const byte der[], size_t length, const std::string& label, size_t width) { const std::string PEM_HEADER = "-----BEGIN " + label + "-----\n"; const std::string PEM_TRAILER = "-----END " + label + "-----\n"; Pipe pipe(new Base64_Encoder(true, width)); pipe.process_msg(der, length); return (PEM_HEADER + pipe.read_all_as_string() + PEM_TRAILER); } /* * PEM encode BER/DER-encoded objects */ std::string encode(const MemoryRegion<byte>& data, const std::string& label, size_t width) { return encode(&data[0], data.size(), label, width); } /* * Decode PEM down to raw BER/DER */ SecureVector<byte> decode_check_label(DataSource& source, const std::string& label_want) { std::string label_got; SecureVector<byte> ber = decode(source, label_got); if(label_got != label_want) throw Decoding_Error("PEM: Label mismatch, wanted " + label_want + ", got " + label_got); return ber; } /* * Decode PEM down to raw BER/DER */ SecureVector<byte> decode(DataSource& source, std::string& label) { const size_t RANDOM_CHAR_LIMIT = 8; const std::string PEM_HEADER1 = "-----BEGIN "; const std::string PEM_HEADER2 = "-----"; size_t position = 0; while(position != PEM_HEADER1.length()) { byte b; if(!source.read_byte(b)) throw Decoding_Error("PEM: No PEM header found"); if(b == PEM_HEADER1[position]) ++position; else if(position >= RANDOM_CHAR_LIMIT) throw Decoding_Error("PEM: Malformed PEM header"); else position = 0; } position = 0; while(position != PEM_HEADER2.length()) { byte b; if(!source.read_byte(b)) throw Decoding_Error("PEM: No PEM header found"); if(b == PEM_HEADER2[position]) ++position; else if(position) throw Decoding_Error("PEM: Malformed PEM header"); if(position == 0) label += static_cast<char>(b); } Pipe base64(new Base64_Decoder); base64.start_msg(); const std::string PEM_TRAILER = "-----END " + label + "-----"; position = 0; while(position != PEM_TRAILER.length()) { byte b; if(!source.read_byte(b)) throw Decoding_Error("PEM: No PEM trailer found"); if(b == PEM_TRAILER[position]) ++position; else if(position) throw Decoding_Error("PEM: Malformed PEM trailer"); if(position == 0) base64.write(b); } base64.end_msg(); return base64.read_all(); } /* * Search for a PEM signature */ bool matches(DataSource& source, const std::string& extra, size_t search_range) { const std::string PEM_HEADER = "-----BEGIN " + extra; SecureVector<byte> search_buf(search_range); size_t got = source.peek(&search_buf[0], search_buf.size(), 0); if(got < PEM_HEADER.length()) return false; size_t index = 0; for(size_t j = 0; j != got; ++j) { if(search_buf[j] == PEM_HEADER[index]) ++index; else index = 0; if(index == PEM_HEADER.size()) return true; } return false; } } } /* * Rivest's Package Tranform * * (C) 2009 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { void aont_package(RandomNumberGenerator& rng, BlockCipher* cipher, const byte input[], size_t input_len, byte output[]) { const size_t BLOCK_SIZE = cipher->block_size(); if(!cipher->valid_keylength(BLOCK_SIZE)) throw Invalid_Argument("AONT::package: Invalid cipher"); // The all-zero string which is used both as the CTR IV and as K0 const std::string all_zeros(BLOCK_SIZE*2, '0'); SymmetricKey package_key(rng, BLOCK_SIZE); Pipe pipe(new StreamCipher_Filter(new CTR_BE(cipher), package_key)); pipe.process_msg(input, input_len); pipe.read(output, pipe.remaining()); // Set K0 (the all zero key) cipher->set_key(SymmetricKey(all_zeros)); SecureVector<byte> buf(BLOCK_SIZE); const size_t blocks = (input_len + BLOCK_SIZE - 1) / BLOCK_SIZE; byte* final_block = output + input_len; clear_mem(final_block, BLOCK_SIZE); // XOR the hash blocks into the final block for(size_t i = 0; i != blocks; ++i) { const size_t left = std::min<size_t>(BLOCK_SIZE, input_len - BLOCK_SIZE * i); zeroise(buf); copy_mem(&buf[0], output + (BLOCK_SIZE * i), left); for(size_t j = 0; j != sizeof(i); ++j) buf[BLOCK_SIZE - 1 - j] ^= get_byte(sizeof(i)-1-j, i); cipher->encrypt(buf); xor_buf(final_block, buf, BLOCK_SIZE); } // XOR the random package key into the final block xor_buf(final_block, package_key.begin(), BLOCK_SIZE); } void aont_unpackage(BlockCipher* cipher, const byte input[], size_t input_len, byte output[]) { const size_t BLOCK_SIZE = cipher->block_size(); if(!cipher->valid_keylength(BLOCK_SIZE)) throw Invalid_Argument("AONT::unpackage: Invalid cipher"); if(input_len < BLOCK_SIZE) throw Invalid_Argument("AONT::unpackage: Input too short"); // The all-zero string which is used both as the CTR IV and as K0 const std::string all_zeros(BLOCK_SIZE*2, '0'); cipher->set_key(SymmetricKey(all_zeros)); SecureVector<byte> package_key(BLOCK_SIZE); SecureVector<byte> buf(BLOCK_SIZE); // Copy the package key (masked with the block hashes) copy_mem(&package_key[0], input + (input_len - BLOCK_SIZE), BLOCK_SIZE); const size_t blocks = ((input_len - 1) / BLOCK_SIZE); // XOR the blocks into the package key bits for(size_t i = 0; i != blocks; ++i) { const size_t left = std::min<size_t>(BLOCK_SIZE, input_len - BLOCK_SIZE * (i+1)); zeroise(buf); copy_mem(&buf[0], input + (BLOCK_SIZE * i), left); for(size_t j = 0; j != sizeof(i); ++j) buf[BLOCK_SIZE - 1 - j] ^= get_byte(sizeof(i)-1-j, i); cipher->encrypt(buf); xor_buf(&package_key[0], buf, BLOCK_SIZE); } Pipe pipe(new StreamCipher_Filter(new CTR_BE(cipher), package_key)); pipe.process_msg(input, input_len - BLOCK_SIZE); pipe.read(output, pipe.remaining()); } } /* * Cryptobox Message Routines * (C) 2009 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace CryptoBox { namespace { /* First 24 bits of SHA-256("Botan Cryptobox"), followed by 8 0 bits for later use as flags, etc if needed */ const u32bit CRYPTOBOX_VERSION_CODE = 0xEFC22400; const size_t VERSION_CODE_LEN = 4; const size_t CIPHER_KEY_LEN = 32; const size_t CIPHER_IV_LEN = 16; const size_t MAC_KEY_LEN = 32; const size_t MAC_OUTPUT_LEN = 20; const size_t PBKDF_SALT_LEN = 10; const size_t PBKDF_ITERATIONS = 8 * 1024; const size_t PBKDF_OUTPUT_LEN = CIPHER_KEY_LEN + CIPHER_IV_LEN + MAC_KEY_LEN; } std::string encrypt(const byte input[], size_t input_len, const std::string& passphrase, RandomNumberGenerator& rng) { SecureVector<byte> pbkdf_salt(PBKDF_SALT_LEN); rng.randomize(&pbkdf_salt[0], pbkdf_salt.size()); PKCS5_PBKDF2 pbkdf(new HMAC(new SHA_512)); OctetString master_key = pbkdf.derive_key( PBKDF_OUTPUT_LEN, passphrase, &pbkdf_salt[0], pbkdf_salt.size(), PBKDF_ITERATIONS); const byte* mk = master_key.begin(); SymmetricKey cipher_key(&mk[0], CIPHER_KEY_LEN); SymmetricKey mac_key(&mk[CIPHER_KEY_LEN], MAC_KEY_LEN); InitializationVector iv(&mk[CIPHER_KEY_LEN + MAC_KEY_LEN], CIPHER_IV_LEN); Pipe pipe(get_cipher("Serpent/CTR-BE", cipher_key, iv, ENCRYPTION), new Fork( 0, new MAC_Filter(new HMAC(new SHA_512), mac_key, MAC_OUTPUT_LEN))); pipe.process_msg(input, input_len); /* Output format is: version # (4 bytes) salt (10 bytes) mac (20 bytes) ciphertext */ const size_t ciphertext_len = pipe.remaining(0); SecureVector<byte> out_buf(VERSION_CODE_LEN + PBKDF_SALT_LEN + MAC_OUTPUT_LEN + ciphertext_len); for(size_t i = 0; i != VERSION_CODE_LEN; ++i) out_buf[i] = get_byte(i, CRYPTOBOX_VERSION_CODE); copy_mem(&out_buf[VERSION_CODE_LEN], &pbkdf_salt[0], PBKDF_SALT_LEN); pipe.read(&out_buf[VERSION_CODE_LEN + PBKDF_SALT_LEN], MAC_OUTPUT_LEN, 1); pipe.read(&out_buf[VERSION_CODE_LEN + PBKDF_SALT_LEN + MAC_OUTPUT_LEN], ciphertext_len, 0); return PEM_Code::encode(out_buf, "BOTAN CRYPTOBOX MESSAGE"); } std::string decrypt(const byte input[], size_t input_len, const std::string& passphrase) { DataSource_Memory input_src(input, input_len); SecureVector<byte> ciphertext = PEM_Code::decode_check_label(input_src, "BOTAN CRYPTOBOX MESSAGE"); if(ciphertext.size() < (VERSION_CODE_LEN + PBKDF_SALT_LEN + MAC_OUTPUT_LEN)) throw Decoding_Error("Invalid CryptoBox input"); for(size_t i = 0; i != VERSION_CODE_LEN; ++i) if(ciphertext[i] != get_byte(i, CRYPTOBOX_VERSION_CODE)) throw Decoding_Error("Bad CryptoBox version"); const byte* pbkdf_salt = &ciphertext[VERSION_CODE_LEN]; PKCS5_PBKDF2 pbkdf(new HMAC(new SHA_512)); OctetString master_key = pbkdf.derive_key( PBKDF_OUTPUT_LEN, passphrase, pbkdf_salt, PBKDF_SALT_LEN, PBKDF_ITERATIONS); const byte* mk = master_key.begin(); SymmetricKey cipher_key(&mk[0], CIPHER_KEY_LEN); SymmetricKey mac_key(&mk[CIPHER_KEY_LEN], MAC_KEY_LEN); InitializationVector iv(&mk[CIPHER_KEY_LEN + MAC_KEY_LEN], CIPHER_IV_LEN); Pipe pipe(new Fork( get_cipher("Serpent/CTR-BE", cipher_key, iv, DECRYPTION), new MAC_Filter(new HMAC(new SHA_512), mac_key, MAC_OUTPUT_LEN))); const size_t ciphertext_offset = VERSION_CODE_LEN + PBKDF_SALT_LEN + MAC_OUTPUT_LEN; pipe.process_msg(&ciphertext[ciphertext_offset], ciphertext.size() - ciphertext_offset); byte computed_mac[MAC_OUTPUT_LEN]; pipe.read(computed_mac, MAC_OUTPUT_LEN, 1); if(!same_mem(computed_mac, &ciphertext[VERSION_CODE_LEN + PBKDF_SALT_LEN], MAC_OUTPUT_LEN)) throw Decoding_Error("CryptoBox integrity failure"); return pipe.read_all_as_string(0); } std::string decrypt(const std::string& input, const std::string& passphrase) { return decrypt(reinterpret_cast<const byte*>(&input[0]), input.size(), passphrase); } } } /* * Format Preserving Encryption using the scheme FE1 from the paper * "Format-Preserving Encryption" by Bellare, Rogaway, et al * (http://eprint.iacr.org/2009/251) * * (C) 2009 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <stdexcept> namespace Botan { namespace FPE { namespace { // Normally FPE is for SSNs, CC#s, etc, nothing too big const size_t MAX_N_BYTES = 128/8; /* * Factor n into a and b which are as close together as possible. * Assumes n is composed mostly of small factors which is the case for * typical uses of FPE (typically, n is a power of 10) * * Want a >= b since the safe number of rounds is 2+log_a(b); if a >= b * then this is always 3 */ void factor(BigInt n, BigInt& a, BigInt& b) { a = 1; b = 1; size_t n_low_zero = low_zero_bits(n); a <<= (n_low_zero / 2); b <<= n_low_zero - (n_low_zero / 2); n >>= n_low_zero; for(size_t i = 0; i != PRIME_TABLE_SIZE; ++i) { while(n % PRIMES[i] == 0) { a *= PRIMES[i]; if(a > b) std::swap(a, b); n /= PRIMES[i]; } } if(a > b) std::swap(a, b); a *= n; if(a < b) std::swap(a, b); if(a <= 1 || b <= 1) throw std::runtime_error("Could not factor n for use in FPE"); } /* * According to a paper by Rogaway, Bellare, etc, the min safe number * of rounds to use for FPE is 2+log_a(b). If a >= b then log_a(b) <= 1 * so 3 rounds is safe. The FPE factorization routine should always * return a >= b, so just confirm that and return 3. */ size_t rounds(const BigInt& a, const BigInt& b) { if(a < b) throw std::logic_error("FPE rounds: a < b"); return 3; } /* * A simple round function based on HMAC(SHA-256) */ class FPE_Encryptor { public: FPE_Encryptor(const SymmetricKey& key, const BigInt& n, const MemoryRegion<byte>& tweak); ~FPE_Encryptor() { delete mac; } BigInt operator()(size_t i, const BigInt& R); private: MessageAuthenticationCode* mac; SecureVector<byte> mac_n_t; }; FPE_Encryptor::FPE_Encryptor(const SymmetricKey& key, const BigInt& n, const MemoryRegion<byte>& tweak) { mac = new HMAC(new SHA_256); mac->set_key(key); SecureVector<byte> n_bin = BigInt::encode(n); if(n_bin.size() > MAX_N_BYTES) throw std::runtime_error("N is too large for FPE encryption"); mac->update_be(static_cast<u32bit>(n_bin.size())); mac->update(&n_bin[0], n_bin.size()); mac->update_be(static_cast<u32bit>(tweak.size())); mac->update(&tweak[0], tweak.size()); mac_n_t = mac->final(); } BigInt FPE_Encryptor::operator()(size_t round_no, const BigInt& R) { SecureVector<byte> r_bin = BigInt::encode(R); mac->update(mac_n_t); mac->update_be(static_cast<u32bit>(round_no)); mac->update_be(static_cast<u32bit>(r_bin.size())); mac->update(&r_bin[0], r_bin.size()); SecureVector<byte> X = mac->final(); return BigInt(&X[0], X.size()); } } /* * Generic Z_n FPE encryption, FE1 scheme */ BigInt fe1_encrypt(const BigInt& n, const BigInt& X0, const SymmetricKey& key, const MemoryRegion<byte>& tweak) { FPE_Encryptor F(key, n, tweak); BigInt a, b; factor(n, a, b); const size_t r = rounds(a, b); BigInt X = X0; for(size_t i = 0; i != r; ++i) { BigInt L = X / b; BigInt R = X % b; BigInt W = (L + F(i, R)) % a; X = a * R + W; } return X; } /* * Generic Z_n FPE decryption, FD1 scheme */ BigInt fe1_decrypt(const BigInt& n, const BigInt& X0, const SymmetricKey& key, const MemoryRegion<byte>& tweak) { FPE_Encryptor F(key, n, tweak); BigInt a, b; factor(n, a, b); const size_t r = rounds(a, b); BigInt X = X0; for(size_t i = 0; i != r; ++i) { BigInt W = X % a; BigInt R = X / a; BigInt L = (W - F(r-i-1, R)) % a; X = b * L + R; } return X; } } } /* * AES Key Wrap (RFC 3394) * (C) 2011 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <memory> namespace Botan { namespace { BlockCipher* make_aes(size_t keylength, Algorithm_Factory& af) { if(keylength == 16) return af.make_block_cipher("AES-128"); else if(keylength == 24) return af.make_block_cipher("AES-192"); else if(keylength == 32) return af.make_block_cipher("AES-256"); else throw std::invalid_argument("Bad KEK length for NIST keywrap"); } } SecureVector<byte> rfc3394_keywrap(const MemoryRegion<byte>& key, const SymmetricKey& kek, Algorithm_Factory& af) { if(key.size() % 8 != 0) throw std::invalid_argument("Bad input key size for NIST key wrap"); std::auto_ptr<BlockCipher> aes(make_aes(kek.length(), af)); aes->set_key(kek); const size_t n = key.size() / 8; SecureVector<byte> R((n + 1) * 8); SecureVector<byte> A(16); for(size_t i = 0; i != 8; ++i) A[i] = 0xA6; copy_mem(&R[8], key.begin(), key.size()); for(size_t j = 0; j <= 5; ++j) { for(size_t i = 1; i <= n; ++i) { const u32bit t = (n * j) + i; copy_mem(&A[8], &R[8*i], 8); aes->encrypt(&A[0]); copy_mem(&R[8*i], &A[8], 8); byte t_buf[4] = { 0 }; store_be(t, t_buf); xor_buf(&A[4], &t_buf[0], 4); } } copy_mem(&R[0], &A[0], 8); return R; } SecureVector<byte> rfc3394_keyunwrap(const MemoryRegion<byte>& key, const SymmetricKey& kek, Algorithm_Factory& af) { if(key.size() < 16 || key.size() % 8 != 0) throw std::invalid_argument("Bad input key size for NIST key unwrap"); std::auto_ptr<BlockCipher> aes(make_aes(kek.length(), af)); aes->set_key(kek); const size_t n = (key.size() - 8) / 8; SecureVector<byte> R(n * 8); SecureVector<byte> A(16); for(size_t i = 0; i != 8; ++i) A[i] = key[i]; copy_mem(&R[0], key.begin() + 8, key.size() - 8); for(size_t j = 0; j <= 5; ++j) { for(size_t i = n; i != 0; --i) { const u32bit t = (5 - j) * n + i; byte t_buf[4] = { 0 }; store_be(t, t_buf); xor_buf(&A[4], &t_buf[0], 4); copy_mem(&A[8], &R[8*(i-1)], 8); aes->decrypt(&A[0]); copy_mem(&R[8*(i-1)], &A[8], 8); } } if(load_be<u64bit>(&A[0], 0) != 0xA6A6A6A6A6A6A6A6) throw Integrity_Failure("NIST key unwrap failed"); return R; } } /* * SRP-6a (RFC 5054 compatatible) * (C) 2011,2012 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <memory> namespace Botan { namespace { BigInt hash_seq(const std::string& hash_id, size_t pad_to, const BigInt& in1, const BigInt& in2) { std::auto_ptr<HashFunction> hash_fn( global_state().algorithm_factory().make_hash_function(hash_id)); hash_fn->update(BigInt::encode_1363(in1, pad_to)); hash_fn->update(BigInt::encode_1363(in2, pad_to)); return BigInt::decode(hash_fn->final()); } BigInt hash_seq(const std::string& hash_id, size_t pad_to, const BigInt& in1, const BigInt& in2, const BigInt& in3) { std::auto_ptr<HashFunction> hash_fn( global_state().algorithm_factory().make_hash_function(hash_id)); hash_fn->update(BigInt::encode_1363(in1, pad_to)); hash_fn->update(BigInt::encode_1363(in2, pad_to)); hash_fn->update(BigInt::encode_1363(in3, pad_to)); return BigInt::decode(hash_fn->final()); } BigInt compute_x(const std::string& hash_id, const std::string& identifier, const std::string& password, const MemoryRegion<byte>& salt) { std::auto_ptr<HashFunction> hash_fn( global_state().algorithm_factory().make_hash_function(hash_id)); hash_fn->update(identifier); hash_fn->update(":"); hash_fn->update(password); SecureVector<byte> inner_h = hash_fn->final(); hash_fn->update(salt); hash_fn->update(inner_h); SecureVector<byte> outer_h = hash_fn->final(); return BigInt::decode(outer_h); } } std::string srp6_group_identifier(const BigInt& N, const BigInt& g) { /* This function assumes that only one 'standard' SRP parameter set has been defined for a particular bitsize. As of this writing that is the case. */ try { const std::string group_name = "modp/srp/" + to_string(N.bits()); DL_Group group(group_name); if(group.get_p() == N && group.get_g() == g) return group_name; throw std::runtime_error("Unknown SRP params"); } catch(...) { throw Invalid_Argument("Bad SRP group parameters"); } } std::pair<BigInt, SymmetricKey> srp6_client_agree(const std::string& identifier, const std::string& password, const std::string& group_id, const std::string& hash_id, const MemoryRegion<byte>& salt, const BigInt& B, RandomNumberGenerator& rng) { DL_Group group(group_id); const BigInt& g = group.get_g(); const BigInt& p = group.get_p(); const size_t p_bytes = group.get_p().bytes(); if(B <= 0 || B >= p) throw std::runtime_error("Invalid SRP parameter from server"); BigInt k = hash_seq(hash_id, p_bytes, p, g); BigInt a(rng, 256); BigInt A = power_mod(g, a, p); BigInt u = hash_seq(hash_id, p_bytes, A, B); const BigInt x = compute_x(hash_id, identifier, password, salt); BigInt S = power_mod((B - (k * power_mod(g, x, p))) % p, (a + (u * x)), p); SymmetricKey Sk(BigInt::encode_1363(S, p_bytes)); return std::make_pair(A, Sk); } BigInt generate_srp6_verifier(const std::string& identifier, const std::string& password, const MemoryRegion<byte>& salt, const std::string& group_id, const std::string& hash_id) { const BigInt x = compute_x(hash_id, identifier, password, salt); DL_Group group(group_id); return power_mod(group.get_g(), x, group.get_p()); } BigInt SRP6_Server_Session::step1(const BigInt& v, const std::string& group_id, const std::string& hash_id, RandomNumberGenerator& rng) { DL_Group group(group_id); const BigInt& g = group.get_g(); const BigInt& p = group.get_p(); p_bytes = p.bytes(); BigInt k = hash_seq(hash_id, p_bytes, p, g); BigInt b(rng, 256); B = (v*k + power_mod(g, b, p)) % p; this->v = v; this->b = b; this->p = p; this->hash_id = hash_id; return B; } SymmetricKey SRP6_Server_Session::step2(const BigInt& A) { if(A <= 0 || A >= p) throw std::runtime_error("Invalid SRP parameter from client"); BigInt u = hash_seq(hash_id, p_bytes, A, B); BigInt S = power_mod(A * power_mod(v, u, p), b, p); return BigInt::encode_1363(S, p_bytes); } } /* * RTSS (threshold secret sharing) * (C) 2009 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <memory> namespace Botan { namespace { /** Table for GF(2^8) arithmetic (exponentials) */ const byte RTSS_EXP[256] = { 0x01, 0x03, 0x05, 0x0F, 0x11, 0x33, 0x55, 0xFF, 0x1A, 0x2E, 0x72, 0x96, 0xA1, 0xF8, 0x13, 0x35, 0x5F, 0xE1, 0x38, 0x48, 0xD8, 0x73, 0x95, 0xA4, 0xF7, 0x02, 0x06, 0x0A, 0x1E, 0x22, 0x66, 0xAA, 0xE5, 0x34, 0x5C, 0xE4, 0x37, 0x59, 0xEB, 0x26, 0x6A, 0xBE, 0xD9, 0x70, 0x90, 0xAB, 0xE6, 0x31, 0x53, 0xF5, 0x04, 0x0C, 0x14, 0x3C, 0x44, 0xCC, 0x4F, 0xD1, 0x68, 0xB8, 0xD3, 0x6E, 0xB2, 0xCD, 0x4C, 0xD4, 0x67, 0xA9, 0xE0, 0x3B, 0x4D, 0xD7, 0x62, 0xA6, 0xF1, 0x08, 0x18, 0x28, 0x78, 0x88, 0x83, 0x9E, 0xB9, 0xD0, 0x6B, 0xBD, 0xDC, 0x7F, 0x81, 0x98, 0xB3, 0xCE, 0x49, 0xDB, 0x76, 0x9A, 0xB5, 0xC4, 0x57, 0xF9, 0x10, 0x30, 0x50, 0xF0, 0x0B, 0x1D, 0x27, 0x69, 0xBB, 0xD6, 0x61, 0xA3, 0xFE, 0x19, 0x2B, 0x7D, 0x87, 0x92, 0xAD, 0xEC, 0x2F, 0x71, 0x93, 0xAE, 0xE9, 0x20, 0x60, 0xA0, 0xFB, 0x16, 0x3A, 0x4E, 0xD2, 0x6D, 0xB7, 0xC2, 0x5D, 0xE7, 0x32, 0x56, 0xFA, 0x15, 0x3F, 0x41, 0xC3, 0x5E, 0xE2, 0x3D, 0x47, 0xC9, 0x40, 0xC0, 0x5B, 0xED, 0x2C, 0x74, 0x9C, 0xBF, 0xDA, 0x75, 0x9F, 0xBA, 0xD5, 0x64, 0xAC, 0xEF, 0x2A, 0x7E, 0x82, 0x9D, 0xBC, 0xDF, 0x7A, 0x8E, 0x89, 0x80, 0x9B, 0xB6, 0xC1, 0x58, 0xE8, 0x23, 0x65, 0xAF, 0xEA, 0x25, 0x6F, 0xB1, 0xC8, 0x43, 0xC5, 0x54, 0xFC, 0x1F, 0x21, 0x63, 0xA5, 0xF4, 0x07, 0x09, 0x1B, 0x2D, 0x77, 0x99, 0xB0, 0xCB, 0x46, 0xCA, 0x45, 0xCF, 0x4A, 0xDE, 0x79, 0x8B, 0x86, 0x91, 0xA8, 0xE3, 0x3E, 0x42, 0xC6, 0x51, 0xF3, 0x0E, 0x12, 0x36, 0x5A, 0xEE, 0x29, 0x7B, 0x8D, 0x8C, 0x8F, 0x8A, 0x85, 0x94, 0xA7, 0xF2, 0x0D, 0x17, 0x39, 0x4B, 0xDD, 0x7C, 0x84, 0x97, 0xA2, 0xFD, 0x1C, 0x24, 0x6C, 0xB4, 0xC7, 0x52, 0xF6, 0x01 }; /** Table for GF(2^8) arithmetic (logarithms) */ const byte RTSS_LOG[] = { 0x90, 0x00, 0x19, 0x01, 0x32, 0x02, 0x1A, 0xC6, 0x4B, 0xC7, 0x1B, 0x68, 0x33, 0xEE, 0xDF, 0x03, 0x64, 0x04, 0xE0, 0x0E, 0x34, 0x8D, 0x81, 0xEF, 0x4C, 0x71, 0x08, 0xC8, 0xF8, 0x69, 0x1C, 0xC1, 0x7D, 0xC2, 0x1D, 0xB5, 0xF9, 0xB9, 0x27, 0x6A, 0x4D, 0xE4, 0xA6, 0x72, 0x9A, 0xC9, 0x09, 0x78, 0x65, 0x2F, 0x8A, 0x05, 0x21, 0x0F, 0xE1, 0x24, 0x12, 0xF0, 0x82, 0x45, 0x35, 0x93, 0xDA, 0x8E, 0x96, 0x8F, 0xDB, 0xBD, 0x36, 0xD0, 0xCE, 0x94, 0x13, 0x5C, 0xD2, 0xF1, 0x40, 0x46, 0x83, 0x38, 0x66, 0xDD, 0xFD, 0x30, 0xBF, 0x06, 0x8B, 0x62, 0xB3, 0x25, 0xE2, 0x98, 0x22, 0x88, 0x91, 0x10, 0x7E, 0x6E, 0x48, 0xC3, 0xA3, 0xB6, 0x1E, 0x42, 0x3A, 0x6B, 0x28, 0x54, 0xFA, 0x85, 0x3D, 0xBA, 0x2B, 0x79, 0x0A, 0x15, 0x9B, 0x9F, 0x5E, 0xCA, 0x4E, 0xD4, 0xAC, 0xE5, 0xF3, 0x73, 0xA7, 0x57, 0xAF, 0x58, 0xA8, 0x50, 0xF4, 0xEA, 0xD6, 0x74, 0x4F, 0xAE, 0xE9, 0xD5, 0xE7, 0xE6, 0xAD, 0xE8, 0x2C, 0xD7, 0x75, 0x7A, 0xEB, 0x16, 0x0B, 0xF5, 0x59, 0xCB, 0x5F, 0xB0, 0x9C, 0xA9, 0x51, 0xA0, 0x7F, 0x0C, 0xF6, 0x6F, 0x17, 0xC4, 0x49, 0xEC, 0xD8, 0x43, 0x1F, 0x2D, 0xA4, 0x76, 0x7B, 0xB7, 0xCC, 0xBB, 0x3E, 0x5A, 0xFB, 0x60, 0xB1, 0x86, 0x3B, 0x52, 0xA1, 0x6C, 0xAA, 0x55, 0x29, 0x9D, 0x97, 0xB2, 0x87, 0x90, 0x61, 0xBE, 0xDC, 0xFC, 0xBC, 0x95, 0xCF, 0xCD, 0x37, 0x3F, 0x5B, 0xD1, 0x53, 0x39, 0x84, 0x3C, 0x41, 0xA2, 0x6D, 0x47, 0x14, 0x2A, 0x9E, 0x5D, 0x56, 0xF2, 0xD3, 0xAB, 0x44, 0x11, 0x92, 0xD9, 0x23, 0x20, 0x2E, 0x89, 0xB4, 0x7C, 0xB8, 0x26, 0x77, 0x99, 0xE3, 0xA5, 0x67, 0x4A, 0xED, 0xDE, 0xC5, 0x31, 0xFE, 0x18, 0x0D, 0x63, 0x8C, 0x80, 0xC0, 0xF7, 0x70, 0x07 }; byte gfp_mul(byte x, byte y) { if(x == 0 || y == 0) return 0; return RTSS_EXP[(RTSS_LOG[x] + RTSS_LOG[y]) % 255]; } byte rtss_hash_id(const std::string& hash_name) { if(hash_name == "SHA-160") return 1; else if(hash_name == "SHA-256") return 2; else throw Invalid_Argument("RTSS only supports SHA-1 and SHA-256"); } HashFunction* get_rtss_hash_by_id(byte id) { if(id == 1) return new SHA_160; else if(id == 2) return new SHA_256; else throw Decoding_Error("Bad RTSS hash identifier"); } } RTSS_Share::RTSS_Share(const std::string& hex_input) { contents = hex_decode(hex_input); } byte RTSS_Share::share_id() const { if(!initialized()) throw Invalid_State("RTSS_Share::share_id not initialized"); return contents[20]; } std::string RTSS_Share::to_string() const { return hex_encode(&contents[0], contents.size()); } std::vector<RTSS_Share> RTSS_Share::split(byte M, byte N, const byte S[], u16bit S_len, const byte identifier[16], RandomNumberGenerator& rng) { if(M == 0 || N == 0 || M > N) throw Encoding_Error("RTSS_Share::split: M == 0 or N == 0 or M > N"); SHA_256 hash; // always use SHA-256 when generating shares std::vector<RTSS_Share> shares(N); // Create RTSS header in each share for(byte i = 0; i != N; ++i) { shares[i].contents += std::make_pair(identifier, 16); shares[i].contents += rtss_hash_id(hash.name()); shares[i].contents += M; shares[i].contents += get_byte(0, S_len); shares[i].contents += get_byte(1, S_len); } // Choose sequential values for X starting from 1 for(byte i = 0; i != N; ++i) shares[i].contents.push_back(i+1); // secret = S || H(S) SecureVector<byte> secret(S, S_len); secret += hash.process(S, S_len); for(size_t i = 0; i != secret.size(); ++i) { std::vector<byte> coefficients(M-1); rng.randomize(&coefficients[0], coefficients.size()); for(byte j = 0; j != N; ++j) { const byte X = j + 1; byte sum = secret[i]; byte X_i = X; for(size_t k = 0; k != coefficients.size(); ++k) { sum ^= gfp_mul(X_i, coefficients[k]); X_i = gfp_mul(X_i, X); } shares[j].contents.push_back(sum); } } return shares; } SecureVector<byte> RTSS_Share::reconstruct(const std::vector<RTSS_Share>& shares) { const size_t RTSS_HEADER_SIZE = 20; for(size_t i = 0; i != shares.size(); ++i) { if(shares[i].size() != shares[0].size()) throw Decoding_Error("Different sized RTSS shares detected"); if(shares[i].share_id() == 0) throw Decoding_Error("Invalid (id = 0) RTSS share detected"); if(shares[i].size() < RTSS_HEADER_SIZE) throw Decoding_Error("Missing or malformed RTSS header"); if(!same_mem(&shares[0].contents[0], &shares[i].contents[0], RTSS_HEADER_SIZE)) throw Decoding_Error("Different RTSS headers detected"); } if(shares.size() < shares[0].contents[17]) throw Decoding_Error("Insufficient shares to do TSS reconstruction"); u16bit secret_len = make_u16bit(shares[0].contents[18], shares[0].contents[19]); byte hash_id = shares[0].contents[16]; std::auto_ptr<HashFunction> hash(get_rtss_hash_by_id(hash_id)); if(shares[0].size() != secret_len + hash->output_length() + RTSS_HEADER_SIZE + 1) throw Decoding_Error("Bad RTSS length field in header"); std::vector<byte> V(shares.size()); SecureVector<byte> secret; for(size_t i = RTSS_HEADER_SIZE + 1; i != shares[0].size(); ++i) { for(size_t j = 0; j != V.size(); ++j) V[j] = shares[j].contents[i]; byte r = 0; for(size_t k = 0; k != shares.size(); ++k) { // L_i function: byte r2 = 1; for(size_t l = 0; l != shares.size(); ++l) { if(k == l) continue; byte share_k = shares[k].share_id(); byte share_l = shares[l].share_id(); if(share_k == share_l) throw Decoding_Error("Duplicate shares found in RTSS recovery"); byte div = RTSS_EXP[(255 + RTSS_LOG[share_l] - RTSS_LOG[share_k ^ share_l]) % 255]; r2 = gfp_mul(r2, div); } r ^= gfp_mul(V[k], r2); } secret.push_back(r); } if(secret.size() != secret_len + hash->output_length()) throw Decoding_Error("Bad length in RTSS output"); hash->update(&secret[0], secret_len); SecureVector<byte> hash_check = hash->final(); if(!same_mem(&hash_check[0], &secret[secret_len], hash->output_length())) throw Decoding_Error("RTSS hash check failed"); return SecureVector<byte>(&secret[0], secret_len); } } /* * Core Engine * (C) 1999-2007,2011 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <memory> #if defined(BOTAN_HAS_ECB) #endif #if defined(BOTAN_HAS_CBC) #endif #if defined(BOTAN_HAS_CTS) #endif #if defined(BOTAN_HAS_CFB) #endif #if defined(BOTAN_HAS_OFB) #endif #if defined(BOTAN_HAS_CTR_BE) #endif #if defined(BOTAN_HAS_EAX) #endif #if defined(BOTAN_HAS_XTS) #endif namespace Botan { namespace { /** * Get a block cipher padding method by name */ BlockCipherModePaddingMethod* get_bc_pad(const std::string& algo_spec, const std::string& def_if_empty) { #if defined(BOTAN_HAS_CIPHER_MODE_PADDING) if(algo_spec == "NoPadding" || (algo_spec == "" && def_if_empty == "NoPadding")) return new Null_Padding; if(algo_spec == "PKCS7" || (algo_spec == "" && def_if_empty == "PKCS7")) return new PKCS7_Padding; if(algo_spec == "OneAndZeros") return new OneAndZeros_Padding; if(algo_spec == "X9.23") return new ANSI_X923_Padding; #endif throw Algorithm_Not_Found(algo_spec); } } Keyed_Filter* get_cipher_mode(const BlockCipher* block_cipher, Cipher_Dir direction, const std::string& mode, const std::string& padding) { #if defined(BOTAN_HAS_OFB) if(mode == "OFB") return new StreamCipher_Filter(new OFB(block_cipher->clone())); #endif #if defined(BOTAN_HAS_CTR_BE) if(mode == "CTR-BE") return new StreamCipher_Filter(new CTR_BE(block_cipher->clone())); #endif #if defined(BOTAN_HAS_ECB) if(mode == "ECB" || mode == "") { if(direction == ENCRYPTION) return new ECB_Encryption(block_cipher->clone(), get_bc_pad(padding, "NoPadding")); else return new ECB_Decryption(block_cipher->clone(), get_bc_pad(padding, "NoPadding")); } #endif if(mode == "CBC") { if(padding == "CTS") { #if defined(BOTAN_HAS_CTS) if(direction == ENCRYPTION) return new CTS_Encryption(block_cipher->clone()); else return new CTS_Decryption(block_cipher->clone()); #else return 0; #endif } #if defined(BOTAN_HAS_CBC) if(direction == ENCRYPTION) return new CBC_Encryption(block_cipher->clone(), get_bc_pad(padding, "PKCS7")); else return new CBC_Decryption(block_cipher->clone(), get_bc_pad(padding, "PKCS7")); #else return 0; #endif } #if defined(BOTAN_HAS_XTS) if(mode == "XTS") { if(direction == ENCRYPTION) return new XTS_Encryption(block_cipher->clone()); else return new XTS_Decryption(block_cipher->clone()); } #endif if(mode.find("CFB") != std::string::npos || mode.find("EAX") != std::string::npos) { size_t bits = 0; std::vector<std::string> algo_info = parse_algorithm_name(mode); std::string mode_name = algo_info[0]; if(algo_info.size() == 1) bits = 8 * block_cipher->block_size(); else if(algo_info.size() == 2) bits = to_u32bit(algo_info[1]); else return 0; #if defined(BOTAN_HAS_CFB) if(mode_name == "CFB") { if(direction == ENCRYPTION) return new CFB_Encryption(block_cipher->clone(), bits); else return new CFB_Decryption(block_cipher->clone(), bits); } #endif #if defined(BOTAN_HAS_EAX) if(mode_name == "EAX") { if(direction == ENCRYPTION) return new EAX_Encryption(block_cipher->clone(), bits); else return new EAX_Decryption(block_cipher->clone(), bits); } #endif } return 0; } /* * Get a cipher object */ Keyed_Filter* Core_Engine::get_cipher(const std::string& algo_spec, Cipher_Dir direction, Algorithm_Factory& af) { std::vector<std::string> algo_parts = split_on(algo_spec, '/'); if(algo_parts.empty()) throw Invalid_Algorithm_Name(algo_spec); const std::string cipher_name = algo_parts[0]; // check if it is a stream cipher first (easy case) const StreamCipher* stream_cipher = af.prototype_stream_cipher(cipher_name); if(stream_cipher) return new StreamCipher_Filter(stream_cipher->clone()); const BlockCipher* block_cipher = af.prototype_block_cipher(cipher_name); if(!block_cipher) return 0; if(algo_parts.size() >= 4) return 0; // 4 part mode, not something we know about if(algo_parts.size() < 2) throw Lookup_Error("Cipher specification '" + algo_spec + "' is missing mode identifier"); std::string mode = algo_parts[1]; std::string padding; if(algo_parts.size() == 3) padding = algo_parts[2]; else padding = (mode == "CBC") ? "PKCS7" : "NoPadding"; if(mode == "ECB" && padding == "CTS") return 0; else if((mode != "CBC" && mode != "ECB") && padding != "NoPadding") throw Invalid_Algorithm_Name(algo_spec); Keyed_Filter* filt = get_cipher_mode(block_cipher, direction, mode, padding); if(filt) return filt; throw Algorithm_Not_Found(cipher_name + "/" + mode + "/" + padding); } } /* * PK Operations * (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ #if defined(BOTAN_HAS_RSA) #endif #if defined(BOTAN_HAS_RW) #endif #if defined(BOTAN_HAS_DSA) #endif #if defined(BOTAN_HAS_ECDSA) #endif #if defined(BOTAN_HAS_ELGAMAL) #endif #if defined(BOTAN_HAS_GOST_34_10_2001) #endif #if defined(BOTAN_HAS_NYBERG_RUEPPEL) #endif #if defined(BOTAN_HAS_DIFFIE_HELLMAN) #endif #if defined(BOTAN_HAS_ECDH) #endif namespace Botan { PK_Ops::Encryption* Core_Engine::get_encryption_op(const Public_Key& key) const { #if defined(BOTAN_HAS_RSA) if(const RSA_PublicKey* s = dynamic_cast<const RSA_PublicKey*>(&key)) return new RSA_Public_Operation(*s); #endif #if defined(BOTAN_HAS_ELGAMAL) if(const ElGamal_PublicKey* s = dynamic_cast<const ElGamal_PublicKey*>(&key)) return new ElGamal_Encryption_Operation(*s); #endif return 0; } PK_Ops::Decryption* Core_Engine::get_decryption_op(const Private_Key& key) const { #if defined(BOTAN_HAS_RSA) if(const RSA_PrivateKey* s = dynamic_cast<const RSA_PrivateKey*>(&key)) return new RSA_Private_Operation(*s); #endif #if defined(BOTAN_HAS_ELGAMAL) if(const ElGamal_PrivateKey* s = dynamic_cast<const ElGamal_PrivateKey*>(&key)) return new ElGamal_Decryption_Operation(*s); #endif return 0; } PK_Ops::Key_Agreement* Core_Engine::get_key_agreement_op(const Private_Key& key) const { #if defined(BOTAN_HAS_DIFFIE_HELLMAN) if(const DH_PrivateKey* dh = dynamic_cast<const DH_PrivateKey*>(&key)) return new DH_KA_Operation(*dh); #endif #if defined(BOTAN_HAS_ECDH) if(const ECDH_PrivateKey* ecdh = dynamic_cast<const ECDH_PrivateKey*>(&key)) return new ECDH_KA_Operation(*ecdh); #endif return 0; } PK_Ops::Signature* Core_Engine::get_signature_op(const Private_Key& key) const { #if defined(BOTAN_HAS_RSA) if(const RSA_PrivateKey* s = dynamic_cast<const RSA_PrivateKey*>(&key)) return new RSA_Private_Operation(*s); #endif #if defined(BOTAN_HAS_RW) if(const RW_PrivateKey* s = dynamic_cast<const RW_PrivateKey*>(&key)) return new RW_Signature_Operation(*s); #endif #if defined(BOTAN_HAS_DSA) if(const DSA_PrivateKey* s = dynamic_cast<const DSA_PrivateKey*>(&key)) return new DSA_Signature_Operation(*s); #endif #if defined(BOTAN_HAS_ECDSA) if(const ECDSA_PrivateKey* s = dynamic_cast<const ECDSA_PrivateKey*>(&key)) return new ECDSA_Signature_Operation(*s); #endif #if defined(BOTAN_HAS_GOST_34_10_2001) if(const GOST_3410_PrivateKey* s = dynamic_cast<const GOST_3410_PrivateKey*>(&key)) return new GOST_3410_Signature_Operation(*s); #endif #if defined(BOTAN_HAS_NYBERG_RUEPPEL) if(const NR_PrivateKey* s = dynamic_cast<const NR_PrivateKey*>(&key)) return new NR_Signature_Operation(*s); #endif return 0; } PK_Ops::Verification* Core_Engine::get_verify_op(const Public_Key& key) const { #if defined(BOTAN_HAS_RSA) if(const RSA_PublicKey* s = dynamic_cast<const RSA_PublicKey*>(&key)) return new RSA_Public_Operation(*s); #endif #if defined(BOTAN_HAS_RW) if(const RW_PublicKey* s = dynamic_cast<const RW_PublicKey*>(&key)) return new RW_Verification_Operation(*s); #endif #if defined(BOTAN_HAS_DSA) if(const DSA_PublicKey* s = dynamic_cast<const DSA_PublicKey*>(&key)) return new DSA_Verification_Operation(*s); #endif #if defined(BOTAN_HAS_ECDSA) if(const ECDSA_PublicKey* s = dynamic_cast<const ECDSA_PublicKey*>(&key)) return new ECDSA_Verification_Operation(*s); #endif #if defined(BOTAN_HAS_GOST_34_10_2001) if(const GOST_3410_PublicKey* s = dynamic_cast<const GOST_3410_PublicKey*>(&key)) return new GOST_3410_Verification_Operation(*s); #endif #if defined(BOTAN_HAS_NYBERG_RUEPPEL) if(const NR_PublicKey* s = dynamic_cast<const NR_PublicKey*>(&key)) return new NR_Verification_Operation(*s); #endif return 0; } } /* * Modular Exponentiation * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Choose a modular exponentation algorithm */ Modular_Exponentiator* Core_Engine::mod_exp(const BigInt& n, Power_Mod::Usage_Hints hints) const { if(n.is_odd()) return new Montgomery_Exponentiator(n, hints); return new Fixed_Window_Exponentiator(n, hints); } } /* * Block Cipher Lookup * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #if defined(BOTAN_HAS_AES) #endif #if defined(BOTAN_HAS_BLOWFISH) #endif #if defined(BOTAN_HAS_CAMELLIA) #endif #if defined(BOTAN_HAS_CAST) #endif #if defined(BOTAN_HAS_CASCADE) #endif #if defined(BOTAN_HAS_DES) #endif #if defined(BOTAN_HAS_GOST_28147_89) #endif #if defined(BOTAN_HAS_IDEA) #endif #if defined(BOTAN_HAS_KASUMI) #endif #if defined(BOTAN_HAS_LION) #endif #if defined(BOTAN_HAS_LUBY_RACKOFF) #endif #if defined(BOTAN_HAS_MARS) #endif #if defined(BOTAN_HAS_MISTY1) #endif #if defined(BOTAN_HAS_NOEKEON) #endif #if defined(BOTAN_HAS_RC2) #endif #if defined(BOTAN_HAS_RC5) #endif #if defined(BOTAN_HAS_RC6) #endif #if defined(BOTAN_HAS_SAFER) #endif #if defined(BOTAN_HAS_SEED) #endif #if defined(BOTAN_HAS_SERPENT) #endif #if defined(BOTAN_HAS_SKIPJACK) #endif #if defined(BOTAN_HAS_SQUARE) #endif #if defined(BOTAN_HAS_TEA) #endif #if defined(BOTAN_HAS_TWOFISH) #endif #if defined(BOTAN_HAS_XTEA) #endif namespace Botan { /* * Look for an algorithm with this name */ BlockCipher* Core_Engine::find_block_cipher(const SCAN_Name& request, Algorithm_Factory& af) const { #if defined(BOTAN_HAS_AES) if(request.algo_name() == "AES-128") return new AES_128; if(request.algo_name() == "AES-192") return new AES_192; if(request.algo_name() == "AES-256") return new AES_256; #endif #if defined(BOTAN_HAS_BLOWFISH) if(request.algo_name() == "Blowfish") return new Blowfish; #endif #if defined(BOTAN_HAS_CAMELLIA) if(request.algo_name() == "Camellia-128") return new Camellia_128; if(request.algo_name() == "Camellia-192") return new Camellia_192; if(request.algo_name() == "Camellia-256") return new Camellia_256; #endif #if defined(BOTAN_HAS_CAST) if(request.algo_name() == "CAST-128") return new CAST_128; if(request.algo_name() == "CAST-256") return new CAST_256; #endif #if defined(BOTAN_HAS_DES) if(request.algo_name() == "DES") return new DES; if(request.algo_name() == "DESX") return new DESX; if(request.algo_name() == "TripleDES") return new TripleDES; #endif #if defined(BOTAN_HAS_GOST_28147_89) if(request.algo_name() == "GOST-28147-89") return new GOST_28147_89(request.arg(0, "R3411_94_TestParam")); #endif #if defined(BOTAN_HAS_IDEA) if(request.algo_name() == "IDEA") return new IDEA; #endif #if defined(BOTAN_HAS_KASUMI) if(request.algo_name() == "KASUMI") return new KASUMI; #endif #if defined(BOTAN_HAS_MARS) if(request.algo_name() == "MARS") return new MARS; #endif #if defined(BOTAN_HAS_MISTY1) if(request.algo_name() == "MISTY1") return new MISTY1(request.arg_as_integer(0, 8)); #endif #if defined(BOTAN_HAS_NOEKEON) if(request.algo_name() == "Noekeon") return new Noekeon; #endif #if defined(BOTAN_HAS_RC2) if(request.algo_name() == "RC2") return new RC2; #endif #if defined(BOTAN_HAS_RC5) if(request.algo_name() == "RC5") return new RC5(request.arg_as_integer(0, 12)); #endif #if defined(BOTAN_HAS_RC6) if(request.algo_name() == "RC6") return new RC6; #endif #if defined(BOTAN_HAS_SAFER) if(request.algo_name() == "SAFER-SK") return new SAFER_SK(request.arg_as_integer(0, 10)); #endif #if defined(BOTAN_HAS_SEED) if(request.algo_name() == "SEED") return new SEED; #endif #if defined(BOTAN_HAS_SERPENT) if(request.algo_name() == "Serpent") return new Serpent; #endif #if defined(BOTAN_HAS_SKIPJACK) if(request.algo_name() == "Skipjack") return new Skipjack; #endif #if defined(BOTAN_HAS_SQUARE) if(request.algo_name() == "Square") return new Square; #endif #if defined(BOTAN_HAS_TEA) if(request.algo_name() == "TEA") return new TEA; #endif #if defined(BOTAN_HAS_TWOFISH) if(request.algo_name() == "Twofish") return new Twofish; #endif #if defined(BOTAN_HAS_XTEA) if(request.algo_name() == "XTEA") return new XTEA; #endif #if defined(BOTAN_HAS_LUBY_RACKOFF) if(request.algo_name() == "Luby-Rackoff" && request.arg_count() == 1) { const HashFunction* hash = af.prototype_hash_function(request.arg(0)); if(hash) return new LubyRackoff(hash->clone()); } #endif #if defined(BOTAN_HAS_CASCADE) if(request.algo_name() == "Cascade" && request.arg_count() == 2) { const BlockCipher* c1 = af.prototype_block_cipher(request.arg(0)); const BlockCipher* c2 = af.prototype_block_cipher(request.arg(1)); if(c1 && c2) return new Cascade_Cipher(c1->clone(), c2->clone()); } #endif #if defined(BOTAN_HAS_LION) if(request.algo_name() == "Lion" && request.arg_count_between(2, 3)) { const size_t block_size = request.arg_as_integer(2, 1024); const HashFunction* hash = af.prototype_hash_function(request.arg(0)); const StreamCipher* stream_cipher = af.prototype_stream_cipher(request.arg(1)); if(!hash || !stream_cipher) return 0; return new Lion(hash->clone(), stream_cipher->clone(), block_size); } #endif return 0; } } /* * Hash Algorithms Lookup * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <memory> #if defined(BOTAN_HAS_ADLER32) #endif #if defined(BOTAN_HAS_CRC24) #endif #if defined(BOTAN_HAS_CRC32) #endif #if defined(BOTAN_HAS_BMW_512) #endif #if defined(BOTAN_HAS_GOST_34_11) #endif #if defined(BOTAN_HAS_HAS_160) #endif #if defined(BOTAN_HAS_KECCAK) #endif #if defined(BOTAN_HAS_MD2) #endif #if defined(BOTAN_HAS_MD4) #endif #if defined(BOTAN_HAS_MD5) #endif #if defined(BOTAN_HAS_RIPEMD_128) #endif #if defined(BOTAN_HAS_RIPEMD_160) #endif #if defined(BOTAN_HAS_SHA1) #endif #if defined(BOTAN_HAS_SHA2_32) #endif #if defined(BOTAN_HAS_SHA2_64) #endif #if defined(BOTAN_HAS_SKEIN_512) #endif #if defined(BOTAN_HAS_TIGER) #endif #if defined(BOTAN_HAS_WHIRLPOOL) #endif #if defined(BOTAN_HAS_PARALLEL_HASH) #endif #if defined(BOTAN_HAS_COMB4P) #endif namespace Botan { /* * Look for an algorithm with this name */ HashFunction* Core_Engine::find_hash(const SCAN_Name& request, Algorithm_Factory& af) const { #if defined(BOTAN_HAS_ADLER32) if(request.algo_name() == "Adler32") return new Adler32; #endif #if defined(BOTAN_HAS_CRC24) if(request.algo_name() == "CRC24") return new CRC24; #endif #if defined(BOTAN_HAS_CRC32) if(request.algo_name() == "CRC32") return new CRC32; #endif #if defined(BOTAN_HAS_BMW_512) if(request.algo_name() == "BMW-512") return new BMW_512; #endif #if defined(BOTAN_HAS_GOST_34_11) if(request.algo_name() == "GOST-34.11") return new GOST_34_11; #endif #if defined(BOTAN_HAS_HAS_160) if(request.algo_name() == "HAS-160") return new HAS_160; #endif #if defined(BOTAN_HAS_KECCAK) if(request.algo_name() == "Keccak-1600") return new Keccak_1600(request.arg_as_integer(0, 512)); #endif #if defined(BOTAN_HAS_MD2) if(request.algo_name() == "MD2") return new MD2; #endif #if defined(BOTAN_HAS_MD4) if(request.algo_name() == "MD4") return new MD4; #endif #if defined(BOTAN_HAS_MD5) if(request.algo_name() == "MD5") return new MD5; #endif #if defined(BOTAN_HAS_RIPEMD_128) if(request.algo_name() == "RIPEMD-128") return new RIPEMD_128; #endif #if defined(BOTAN_HAS_RIPEMD_160) if(request.algo_name() == "RIPEMD-160") return new RIPEMD_160; #endif #if defined(BOTAN_HAS_SHA1) if(request.algo_name() == "SHA-160") return new SHA_160; #endif #if defined(BOTAN_HAS_SHA2_32) if(request.algo_name() == "SHA-224") return new SHA_224; if(request.algo_name() == "SHA-256") return new SHA_256; #endif #if defined(BOTAN_HAS_SHA2_64) if(request.algo_name() == "SHA-384") return new SHA_384; if(request.algo_name() == "SHA-512") return new SHA_512; #endif #if defined(BOTAN_HAS_TIGER) if(request.algo_name() == "Tiger") return new Tiger(request.arg_as_integer(0, 24), // hash output request.arg_as_integer(1, 3)); // # passes #endif #if defined(BOTAN_HAS_SKEIN_512) if(request.algo_name() == "Skein-512") return new Skein_512(request.arg_as_integer(0, 512), request.arg(1, "")); #endif #if defined(BOTAN_HAS_WHIRLPOOL) if(request.algo_name() == "Whirlpool") return new Whirlpool; #endif #if defined(BOTAN_HAS_COMB4P) if(request.algo_name() == "Comb4P" && request.arg_count() == 2) { const HashFunction* h1 = af.prototype_hash_function(request.arg(0)); const HashFunction* h2 = af.prototype_hash_function(request.arg(1)); if(h1 && h2) return new Comb4P(h1->clone(), h2->clone()); } #endif #if defined(BOTAN_HAS_PARALLEL_HASH) if(request.algo_name() == "Parallel") { std::vector<const HashFunction*> hash_prototypes; /* First pass, just get the prototypes (no memory allocation). Then if all were found, replace each prototype with a newly created clone */ for(size_t i = 0; i != request.arg_count(); ++i) { const HashFunction* hash = af.prototype_hash_function(request.arg(i)); if(!hash) return 0; hash_prototypes.push_back(hash); } std::vector<HashFunction*> hashes; for(size_t i = 0; i != hash_prototypes.size(); ++i) hashes.push_back(hash_prototypes[i]->clone()); return new Parallel(hashes); } #endif return 0; } } /* * MAC Lookup * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #if defined(BOTAN_HAS_CBC_MAC) #endif #if defined(BOTAN_HAS_CMAC) #endif #if defined(BOTAN_HAS_HMAC) #endif #if defined(BOTAN_HAS_SSL3_MAC) #endif #if defined(BOTAN_HAS_ANSI_X919_MAC) #endif namespace Botan { /* * Look for an algorithm with this name */ MessageAuthenticationCode* Core_Engine::find_mac(const SCAN_Name& request, Algorithm_Factory& af) const { #if defined(BOTAN_HAS_CBC_MAC) if(request.algo_name() == "CBC-MAC" && request.arg_count() == 1) return new CBC_MAC(af.make_block_cipher(request.arg(0))); #endif #if defined(BOTAN_HAS_CMAC) if(request.algo_name() == "CMAC" && request.arg_count() == 1) return new CMAC(af.make_block_cipher(request.arg(0))); #endif #if defined(BOTAN_HAS_HMAC) if(request.algo_name() == "HMAC" && request.arg_count() == 1) return new HMAC(af.make_hash_function(request.arg(0))); #endif #if defined(BOTAN_HAS_SSL3_MAC) if(request.algo_name() == "SSL3-MAC" && request.arg_count() == 1) return new SSL3_MAC(af.make_hash_function(request.arg(0))); #endif #if defined(BOTAN_HAS_ANSI_X919_MAC) if(request.algo_name() == "X9.19-MAC" && request.arg_count() == 0) return new ANSI_X919_MAC(af.make_block_cipher("DES")); #endif return 0; } } /* * PBKDF Lookup * (C) 2010 Jack Lloyd * * Distributed under the terms of the Botan license */ #if defined(BOTAN_HAS_PBKDF1) #endif #if defined(BOTAN_HAS_PBKDF2) #endif #if defined(BOTAN_HAS_PGPS2K) #endif namespace Botan { PBKDF* Core_Engine::find_pbkdf(const SCAN_Name& algo_spec, Algorithm_Factory& af) const { #if defined(BOTAN_HAS_PBKDF1) if(algo_spec.algo_name() == "PBKDF1" && algo_spec.arg_count() == 1) return new PKCS5_PBKDF1(af.make_hash_function(algo_spec.arg(0))); #endif #if defined(BOTAN_HAS_PBKDF2) if(algo_spec.algo_name() == "PBKDF2" && algo_spec.arg_count() == 1) { if(const MessageAuthenticationCode* mac_proto = af.prototype_mac(algo_spec.arg(0))) return new PKCS5_PBKDF2(mac_proto->clone()); return new PKCS5_PBKDF2(af.make_mac("HMAC(" + algo_spec.arg(0) + ")")); } #endif #if defined(BOTAN_HAS_PGPS2K) if(algo_spec.algo_name() == "OpenPGP-S2K" && algo_spec.arg_count() == 1) return new OpenPGP_S2K(af.make_hash_function(algo_spec.arg(0))); #endif return 0; } } /* * Stream Cipher Lookup * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #if defined(BOTAN_HAS_ARC4) #endif #if defined(BOTAN_HAS_SALSA20) #endif #if defined(BOTAN_HAS_TURING) #endif #if defined(BOTAN_HAS_WID_WAKE) #endif namespace Botan { /* * Look for an algorithm with this name */ StreamCipher* Core_Engine::find_stream_cipher(const SCAN_Name& request, Algorithm_Factory&) const { #if defined(BOTAN_HAS_ARC4) if(request.algo_name() == "ARC4") return new ARC4(request.arg_as_integer(0, 0)); if(request.algo_name() == "RC4_drop") return new ARC4(768); #endif #if defined(BOTAN_HAS_SALSA20) if(request.algo_name() == "Salsa20") return new Salsa20; #endif #if defined(BOTAN_HAS_TURING) if(request.algo_name() == "Turing") return new Turing; #endif #if defined(BOTAN_HAS_WID_WAKE) if(request.algo_name() == "WiderWake4+1-BE") return new WiderWake_41_BE; #endif return 0; } } /** * Dynamically Loaded Engine * (C) 2010 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { extern "C" { typedef Engine* (*creator_func)(void); typedef u32bit (*module_version_func)(void); } } Dynamically_Loaded_Engine::Dynamically_Loaded_Engine( const std::string& library_path) : engine(0) { lib = new Dynamically_Loaded_Library(library_path); try { module_version_func get_version = lib->resolve<module_version_func>("module_version"); const u32bit mod_version = get_version(); if(mod_version != 20101003) throw std::runtime_error("Incompatible version in " + library_path + " of " + to_string(mod_version)); creator_func creator = lib->resolve<creator_func>("create_engine"); engine = creator(); if(!engine) throw std::runtime_error("Creator function in " + library_path + " failed"); } catch(...) { delete lib; lib = 0; throw; } } Dynamically_Loaded_Engine::~Dynamically_Loaded_Engine() { delete engine; delete lib; } } /* * Engine * (C) 2010 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { BlockCipher* Engine::find_block_cipher(const SCAN_Name&, Algorithm_Factory&) const { return 0; } StreamCipher* Engine::find_stream_cipher(const SCAN_Name&, Algorithm_Factory&) const { return 0; } HashFunction* Engine::find_hash(const SCAN_Name&, Algorithm_Factory&) const { return 0; } MessageAuthenticationCode* Engine::find_mac(const SCAN_Name&, Algorithm_Factory&) const { return 0; } PBKDF* Engine::find_pbkdf(const SCAN_Name&, Algorithm_Factory&) const { return 0; } Modular_Exponentiator* Engine::mod_exp(const BigInt&, Power_Mod::Usage_Hints) const { return 0; } Keyed_Filter* Engine::get_cipher(const std::string&, Cipher_Dir, Algorithm_Factory&) { return 0; } PK_Ops::Key_Agreement* Engine::get_key_agreement_op(const Private_Key&) const { return 0; } PK_Ops::Signature* Engine::get_signature_op(const Private_Key&) const { return 0; } PK_Ops::Verification* Engine::get_verify_op(const Public_Key&) const { return 0; } PK_Ops::Encryption* Engine::get_encryption_op(const Public_Key&) const { return 0; } PK_Ops::Decryption* Engine::get_decryption_op(const Private_Key&) const { return 0; } } /* * SIMD Engine * (C) 1999-2009 Jack Lloyd * * Distributed under the terms of the Botan license */ #if defined(BOTAN_HAS_AES_SSSE3) #endif #if defined(BOTAN_HAS_SERPENT_SIMD) #endif #if defined(BOTAN_HAS_NOEKEON_SIMD) #endif #if defined(BOTAN_HAS_XTEA_SIMD) #endif #if defined(BOTAN_HAS_IDEA_SSE2) #endif #if defined(BOTAN_HAS_SHA1_SSE2) #endif namespace Botan { BlockCipher* SIMD_Engine::find_block_cipher(const SCAN_Name& request, Algorithm_Factory&) const { #if defined(BOTAN_HAS_AES_SSSE3) if(request.algo_name() == "AES-128" && CPUID::has_ssse3()) return new AES_128_SSSE3; if(request.algo_name() == "AES-192" && CPUID::has_ssse3()) return new AES_192_SSSE3; if(request.algo_name() == "AES-256" && CPUID::has_ssse3()) return new AES_256_SSSE3; #endif #if defined(BOTAN_HAS_IDEA_SSE2) if(request.algo_name() == "IDEA" && CPUID::has_sse2()) return new IDEA_SSE2; #endif #if defined(BOTAN_HAS_NOEKEON_SIMD) if(request.algo_name() == "Noekeon" && SIMD_32::enabled()) return new Noekeon_SIMD; #endif #if defined(BOTAN_HAS_SERPENT_SIMD) if(request.algo_name() == "Serpent" && SIMD_32::enabled()) return new Serpent_SIMD; #endif #if defined(BOTAN_HAS_XTEA_SIMD) if(request.algo_name() == "XTEA" && SIMD_32::enabled()) return new XTEA_SIMD; #endif return 0; } HashFunction* SIMD_Engine::find_hash(const SCAN_Name& request, Algorithm_Factory&) const { #if defined(BOTAN_HAS_SHA1_SSE2) if(request.algo_name() == "SHA-160" && CPUID::has_sse2()) return new SHA_160_SSE2; #endif return 0; } } /* * /dev/random EntropySource * (C) 1999-2009 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <sys/types.h> #include <sys/select.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> #include <string.h> namespace Botan { /** Close the device, if open */ void Device_EntropySource::Device_Reader::close() { if(fd > 0) { ::close(fd); fd = -1; } } /** Read bytes from a device file */ size_t Device_EntropySource::Device_Reader::get(byte out[], size_t length, size_t ms_wait_time) { if(fd < 0) return 0; if(fd >= FD_SETSIZE) return 0; fd_set read_set; FD_ZERO(&read_set); FD_SET(fd, &read_set); struct ::timeval timeout; timeout.tv_sec = (ms_wait_time / 1000); timeout.tv_usec = (ms_wait_time % 1000) * 1000; if(::select(fd + 1, &read_set, 0, 0, &timeout) < 0) return 0; if(!(FD_ISSET(fd, &read_set))) return 0; const ssize_t got = ::read(fd, out, length); if(got <= 0) return 0; return static_cast<size_t>(got); } /** Attempt to open a device */ Device_EntropySource::Device_Reader::fd_type Device_EntropySource::Device_Reader::open(const std::string& pathname) { #ifndef O_NONBLOCK #define O_NONBLOCK 0 #endif #ifndef O_NOCTTY #define O_NOCTTY 0 #endif const int flags = O_RDONLY | O_NONBLOCK | O_NOCTTY; return ::open(pathname.c_str(), flags); } /** Device_EntropySource constructor Open a file descriptor to each (available) device in fsnames */ Device_EntropySource::Device_EntropySource( const std::vector<std::string>& fsnames) { for(size_t i = 0; i != fsnames.size(); ++i) { Device_Reader::fd_type fd = Device_Reader::open(fsnames[i]); if(fd > 0) devices.push_back(Device_Reader(fd)); } } /** Device_EntropySource destructor: close all open devices */ Device_EntropySource::~Device_EntropySource() { for(size_t i = 0; i != devices.size(); ++i) devices[i].close(); } /** * Gather entropy from a RNG device */ void Device_EntropySource::poll(Entropy_Accumulator& accum) { const size_t ENTROPY_BITS_PER_BYTE = 7; const size_t go_get = std::min<size_t>( accum.desired_remaining_bits() / ENTROPY_BITS_PER_BYTE, 32); const size_t read_wait_ms = std::max<size_t>(go_get, 100); MemoryRegion<byte>& io_buffer = accum.get_io_buffer(go_get); for(size_t i = 0; i != devices.size(); ++i) { size_t got = devices[i].get(&io_buffer[0], io_buffer.size(), read_wait_ms); if(got) { accum.add(&io_buffer[0], got, ENTROPY_BITS_PER_BYTE); break; } } } } /* * EGD EntropySource * (C) 1999-2009 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <cstring> #include <stdexcept> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <sys/socket.h> #include <sys/un.h> #ifndef PF_LOCAL #define PF_LOCAL PF_UNIX #endif namespace Botan { EGD_EntropySource::EGD_Socket::EGD_Socket(const std::string& path) : socket_path(path), m_fd(-1) { } /** * Attempt a connection to an EGD/PRNGD socket */ int EGD_EntropySource::EGD_Socket::open_socket(const std::string& path) { int fd = ::socket(PF_LOCAL, SOCK_STREAM, 0); if(fd >= 0) { sockaddr_un addr; std::memset(&addr, 0, sizeof(addr)); addr.sun_family = PF_LOCAL; if(sizeof(addr.sun_path) < path.length() + 1) throw std::invalid_argument("EGD socket path is too long"); std::strncpy(addr.sun_path, path.c_str(), sizeof(addr.sun_path)); int len = sizeof(addr.sun_family) + std::strlen(addr.sun_path) + 1; if(::connect(fd, reinterpret_cast<struct ::sockaddr*>(&addr), len) < 0) { ::close(fd); fd = -1; } } return fd; } /** * Attempt to read entropy from EGD */ size_t EGD_EntropySource::EGD_Socket::read(byte outbuf[], size_t length) { if(length == 0) return 0; if(m_fd < 0) { m_fd = open_socket(socket_path); if(m_fd < 0) return 0; } try { // 1 == EGD command for non-blocking read byte egd_read_command[2] = { 1, static_cast<byte>(std::min<size_t>(length, 255)) }; if(::write(m_fd, egd_read_command, 2) != 2) throw std::runtime_error("Writing entropy read command to EGD failed"); byte out_len = 0; if(::read(m_fd, &out_len, 1) != 1) throw std::runtime_error("Reading response length from EGD failed"); if(out_len > egd_read_command[1]) throw std::runtime_error("Bogus length field received from EGD"); ssize_t count = ::read(m_fd, outbuf, out_len); if(count != out_len) throw std::runtime_error("Reading entropy result from EGD failed"); return static_cast<size_t>(count); } catch(std::exception) { this->close(); // Will attempt to reopen next poll } return 0; } void EGD_EntropySource::EGD_Socket::close() { if(m_fd > 0) { ::close(m_fd); m_fd = -1; } } /** * EGD_EntropySource constructor */ EGD_EntropySource::EGD_EntropySource(const std::vector<std::string>& paths) { for(size_t i = 0; i != paths.size(); ++i) sockets.push_back(EGD_Socket(paths[i])); } EGD_EntropySource::~EGD_EntropySource() { for(size_t i = 0; i != sockets.size(); ++i) sockets[i].close(); sockets.clear(); } /** * Gather Entropy from EGD */ void EGD_EntropySource::poll(Entropy_Accumulator& accum) { size_t go_get = std::min<size_t>(accum.desired_remaining_bits() / 8, 32); MemoryRegion<byte>& io_buffer = accum.get_io_buffer(go_get); for(size_t i = 0; i != sockets.size(); ++i) { size_t got = sockets[i].read(&io_buffer[0], io_buffer.size()); if(got) { accum.add(&io_buffer[0], got, 6); break; } } } } /* * High Resolution Timestamp Entropy Source * (C) 1999-2009,2011 Jack Lloyd * * Distributed under the terms of the Botan license */ #if defined(BOTAN_TARGET_OS_IS_WINDOWS) #include <windows.h> #endif namespace Botan { /* * Get the timestamp */ void High_Resolution_Timestamp::poll(Entropy_Accumulator& accum) { // If Windows, grab the Performance Counter (usually TSC or PIT) #if defined(BOTAN_TARGET_OS_IS_WINDOWS) { LARGE_INTEGER tv; ::QueryPerformanceCounter(&tv); accum.add(tv.QuadPart, 0); } #endif #if defined(BOTAN_TARGET_OS_HAS_CLOCK_GETTIME) #define CLOCK_POLL(src) \ do { \ struct timespec ts; \ clock_gettime(src, &ts); \ accum.add(&ts, sizeof(ts), 0); \ } while(0) #if defined(CLOCK_REALTIME) CLOCK_POLL(CLOCK_REALTIME); #endif #if defined(CLOCK_MONOTONIC) CLOCK_POLL(CLOCK_MONOTONIC); #endif #if defined(CLOCK_MONOTONIC_RAW) CLOCK_POLL(CLOCK_MONOTONIC_RAW); #endif #if defined(CLOCK_PROCESS_CPUTIME_ID) CLOCK_POLL(CLOCK_PROCESS_CPUTIME_ID); #endif #if defined(CLOCK_THREAD_CPUTIME_ID) CLOCK_POLL(CLOCK_THREAD_CPUTIME_ID); #endif #undef CLOCK_POLL #endif #if BOTAN_USE_GCC_INLINE_ASM u64bit rtc = 0; #if defined(BOTAN_TARGET_CPU_IS_X86_FAMILY) if(CPUID::has_rdtsc()) // not availble on all x86 CPUs { u32bit rtc_low = 0, rtc_high = 0; asm volatile("rdtsc" : "=d" (rtc_high), "=a" (rtc_low)); rtc = (static_cast<u64bit>(rtc_high) << 32) | rtc_low; } #elif defined(BOTAN_TARGET_CPU_IS_PPC_FAMILY) u32bit rtc_low = 0, rtc_high = 0; asm volatile("mftbu %0; mftb %1" : "=r" (rtc_high), "=r" (rtc_low)); rtc = (static_cast<u64bit>(rtc_high) << 32) | rtc_low; #elif defined(BOTAN_TARGET_ARCH_IS_ALPHA) asm volatile("rpcc %0" : "=r" (rtc)); #elif defined(BOTAN_TARGET_ARCH_IS_SPARC64) && !defined(BOTAN_TARGET_OS_IS_OPENBSD) asm volatile("rd %%tick, %0" : "=r" (rtc)); #elif defined(BOTAN_TARGET_ARCH_IS_IA64) asm volatile("mov %0=ar.itc" : "=r" (rtc)); #elif defined(BOTAN_TARGET_ARCH_IS_S390X) asm volatile("stck 0(%0)" : : "a" (&rtc) : "memory", "cc"); #elif defined(BOTAN_TARGET_ARCH_IS_HPPA) asm volatile("mfctl 16,%0" : "=r" (rtc)); // 64-bit only? #endif // Don't count the timestamp as contributing entropy accum.add(rtc, 0); #endif } } /* * FTW EntropySource * (C) 1999-2008,2012 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <cstring> #include <deque> #ifndef _POSIX_C_SOURCE #define _POSIX_C_SOURCE 199309 #endif #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <dirent.h> #include <fcntl.h> namespace Botan { /** * Returns file descriptors. Until it doesn't */ class File_Descriptor_Source { public: /** * @return next file descriptor, or -1 if done */ virtual int next_fd() = 0; virtual ~File_Descriptor_Source() {} }; namespace { class Directory_Walker : public File_Descriptor_Source { public: Directory_Walker(const std::string& root) : m_cur_dir(std::make_pair<DIR*, std::string>(0, "")) { if(DIR* root_dir = ::opendir(root.c_str())) m_cur_dir = std::make_pair(root_dir, root); } ~Directory_Walker() { if(m_cur_dir.first) ::closedir(m_cur_dir.first); } int next_fd(); private: void add_directory(const std::string& dirname) { m_dirlist.push_back(dirname); } std::pair<struct dirent*, std::string> get_next_dirent(); std::pair<DIR*, std::string> m_cur_dir; std::deque<std::string> m_dirlist; }; std::pair<struct dirent*, std::string> Directory_Walker::get_next_dirent() { while(m_cur_dir.first) { struct dirent* dir = ::readdir(m_cur_dir.first); if(dir) return std::make_pair<struct dirent*, std::string>(dir, m_cur_dir.second); ::closedir(m_cur_dir.first); m_cur_dir = std::make_pair<DIR*, std::string>(0, ""); while(!m_dirlist.empty() && m_cur_dir.first == 0) { const std::string next_dir_name = m_dirlist[0]; m_dirlist.pop_front(); if(DIR* next_dir = ::opendir(next_dir_name.c_str())) m_cur_dir = std::make_pair(next_dir, next_dir_name); } } return std::make_pair<struct dirent*, std::string>(0, ""); // nothing left } int Directory_Walker::next_fd() { while(true) { std::pair<struct dirent*, std::string> entry = get_next_dirent(); if(!entry.first) break; // no more dirs const std::string filename = entry.first->d_name; if(filename == "." || filename == "..") continue; const std::string full_path = entry.second + '/' + filename; struct stat stat_buf; if(::lstat(full_path.c_str(), &stat_buf) == -1) continue; if(S_ISDIR(stat_buf.st_mode)) { add_directory(full_path); } else if(S_ISREG(stat_buf.st_mode) && (stat_buf.st_mode & S_IROTH)) { int fd = ::open(full_path.c_str(), O_RDONLY | O_NOCTTY); if(fd > 0) return fd; } } return -1; } } /** * FTW_EntropySource Constructor */ FTW_EntropySource::FTW_EntropySource(const std::string& p) : path(p) { dir = 0; } /** * FTW_EntropySource Destructor */ FTW_EntropySource::~FTW_EntropySource() { delete dir; } void FTW_EntropySource::poll(Entropy_Accumulator& accum) { const size_t MAX_FILES_READ_PER_POLL = 2048; if(!dir) dir = new Directory_Walker(path); MemoryRegion<byte>& io_buffer = accum.get_io_buffer(4096); for(size_t i = 0; i != MAX_FILES_READ_PER_POLL; ++i) { int fd = dir->next_fd(); // If we've exhaused this walk of the directory, halt the poll if(fd == -1) { delete dir; dir = 0; break; } ssize_t got = ::read(fd, &io_buffer[0], io_buffer.size()); ::close(fd); if(got > 0) accum.add(&io_buffer[0], got, .001); if(accum.polling_goal_achieved()) break; } } } /* * Entropy Source Using Intel's rdrand instruction * (C) 2012 Jack Lloyd * * Distributed under the terms of the Botan license */ #if !defined(BOTAN_USE_GCC_INLINE_ASM) #include <immintrin.h> #endif namespace Botan { /* * Get the timestamp */ void Intel_Rdrand::poll(Entropy_Accumulator& accum) { if(!CPUID::has_rdrand()) return; /* * Put an upper bound on the total entropy we're willing to claim * for any one polling of rdrand to prevent it from swamping our * poll. Internally, the rdrand system is a DRGB that reseeds at a * somewhat unpredictable rate (the current conditions are * documented, but that might not be true for different * implementations, eg on Haswell or a future AMD chip, so I don't * want to assume). This limit ensures we're going to poll at least * one other source so we have some diversity in our inputs. */ const size_t POLL_UPPER_BOUND = 96; const size_t RDRAND_POLLS = 32; const double ENTROPY_PER_POLL = static_cast<double>(POLL_UPPER_BOUND) / (RDRAND_POLLS * 4); for(size_t i = 0; i != RDRAND_POLLS; ++i) { unsigned int r; #if BOTAN_USE_GCC_INLINE_ASM int cf = 0; // Encoding of rdrand %eax asm(".byte 0x0F, 0xC7, 0xF0; adcl $0,%1" : "=a" (r), "=r" (cf) : "0" (r), "1" (cf) : "cc"); #else int cf = _rdrand32_step(&r); #endif if(cf == 1) accum.add(r, ENTROPY_PER_POLL); } } } /* * Unix EntropySource * (C) 1999-2009 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> #include <sys/time.h> #include <sys/stat.h> #include <sys/resource.h> #include <unistd.h> namespace Botan { namespace { /** * Sort ordering by priority */ bool Unix_Program_Cmp(const Unix_Program& a, const Unix_Program& b) { if(a.priority == b.priority) return (a.name_and_args < b.name_and_args); return (a.priority < b.priority); } } /** * Unix_EntropySource Constructor */ Unix_EntropySource::Unix_EntropySource(const std::vector<std::string>& path) : PATH(path) { std::vector<Unix_Program> default_sources = get_default_sources(); add_sources(&default_sources[0], default_sources.size()); } /** * Add sources to the list */ void Unix_EntropySource::add_sources(const Unix_Program srcs[], size_t count) { sources.insert(sources.end(), srcs, srcs + count); std::sort(sources.begin(), sources.end(), Unix_Program_Cmp); } /** * Poll for entropy on a generic Unix system, first by grabbing various * statistics (stat on common files, getrusage, etc), and then, if more * is required, by exec'ing various programs like uname and rpcinfo and * reading the output. */ void Unix_EntropySource::poll(Entropy_Accumulator& accum) { const char* stat_targets[] = { "/", "/tmp", "/var/tmp", "/usr", "/home", "/etc/passwd", ".", "..", 0 }; for(size_t i = 0; stat_targets[i]; i++) { struct stat statbuf; clear_mem(&statbuf, 1); ::stat(stat_targets[i], &statbuf); accum.add(&statbuf, sizeof(statbuf), .005); } accum.add(::getpid(), 0); accum.add(::getppid(), 0); accum.add(::getuid(), 0); accum.add(::getgid(), 0); accum.add(::getpgrp(), 0); struct ::rusage usage; ::getrusage(RUSAGE_SELF, &usage); accum.add(usage, .005); ::getrusage(RUSAGE_CHILDREN, &usage); accum.add(usage, .005); const size_t MINIMAL_WORKING = 16; MemoryRegion<byte>& io_buffer = accum.get_io_buffer(DEFAULT_BUFFERSIZE); for(size_t i = 0; i != sources.size(); i++) { DataSource_Command pipe(sources[i].name_and_args, PATH); size_t got_from_src = 0; while(!pipe.end_of_data()) { size_t got_this_loop = pipe.read(&io_buffer[0], io_buffer.size()); got_from_src += got_this_loop; accum.add(&io_buffer[0], got_this_loop, .005); } sources[i].working = (got_from_src >= MINIMAL_WORKING) ? true : false; if(accum.polling_goal_achieved()) break; } } } /* * Unix Command Execution * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <sys/time.h> #include <sys/types.h> #include <sys/wait.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> namespace Botan { namespace { /** * Attempt to execute the command */ void do_exec(const std::vector<std::string>& arg_list, const std::vector<std::string>& paths) { const size_t args = arg_list.size() - 1; const char* arg1 = (args >= 1) ? arg_list[1].c_str() : 0; const char* arg2 = (args >= 2) ? arg_list[2].c_str() : 0; const char* arg3 = (args >= 3) ? arg_list[3].c_str() : 0; const char* arg4 = (args >= 4) ? arg_list[4].c_str() : 0; for(size_t j = 0; j != paths.size(); j++) { const std::string full_path = paths[j] + "/" + arg_list[0]; const char* fsname = full_path.c_str(); ::execl(fsname, fsname, arg1, arg2, arg3, arg4, NULL); } } } /** * Local information about the pipe */ struct pipe_wrapper { int fd; pid_t pid; pipe_wrapper(int f, pid_t p) : fd(f), pid(p) {} ~pipe_wrapper() { ::close(fd); } }; /** * Read from the pipe */ size_t DataSource_Command::read(byte buf[], size_t length) { if(end_of_data()) return 0; fd_set set; FD_ZERO(&set); FD_SET(pipe->fd, &set); struct ::timeval tv; tv.tv_sec = 0; tv.tv_usec = MAX_BLOCK_USECS; ssize_t got = 0; if(::select(pipe->fd + 1, &set, 0, 0, &tv) == 1) { if(FD_ISSET(pipe->fd, &set)) got = ::read(pipe->fd, buf, length); } if(got <= 0) { shutdown_pipe(); return 0; } return static_cast<size_t>(got); } /** * Peek at the pipe contents */ size_t DataSource_Command::peek(byte[], size_t, size_t) const { if(end_of_data()) throw Invalid_State("DataSource_Command: Cannot peek when out of data"); throw Stream_IO_Error("Cannot peek/seek on a command pipe"); } /** * Check if we reached EOF */ bool DataSource_Command::end_of_data() const { return (pipe) ? false : true; } /** * Return the Unix file descriptor of the pipe */ int DataSource_Command::fd() const { if(!pipe) return -1; return pipe->fd; } /** * Return a human-readable ID for this stream */ std::string DataSource_Command::id() const { return "Unix command: " + arg_list[0]; } /** * Create the pipe */ void DataSource_Command::create_pipe(const std::vector<std::string>& paths) { bool found_something = false; for(size_t j = 0; j != paths.size(); j++) { const std::string full_path = paths[j] + "/" + arg_list[0]; if(::access(full_path.c_str(), X_OK) == 0) { found_something = true; break; } } if(!found_something) return; int pipe_fd[2]; if(::pipe(pipe_fd) != 0) return; pid_t pid = ::fork(); if(pid == -1) { ::close(pipe_fd[0]); ::close(pipe_fd[1]); } else if(pid > 0) { pipe = new pipe_wrapper(pipe_fd[0], pid); ::close(pipe_fd[1]); } else { if(dup2(pipe_fd[1], STDOUT_FILENO) == -1) ::exit(127); if(close(pipe_fd[0]) != 0 || close(pipe_fd[1]) != 0) ::exit(127); if(close(STDERR_FILENO) != 0) ::exit(127); do_exec(arg_list, paths); ::exit(127); } } /** * Shutdown the pipe */ void DataSource_Command::shutdown_pipe() { if(pipe) { pid_t reaped = waitpid(pipe->pid, 0, WNOHANG); if(reaped == 0) { kill(pipe->pid, SIGTERM); struct ::timeval tv; tv.tv_sec = 0; tv.tv_usec = KILL_WAIT; select(0, 0, 0, 0, &tv); reaped = ::waitpid(pipe->pid, 0, WNOHANG); if(reaped == 0) { ::kill(pipe->pid, SIGKILL); do reaped = ::waitpid(pipe->pid, 0, 0); while(reaped == -1); } } delete pipe; pipe = 0; } } /** * DataSource_Command Constructor */ DataSource_Command::DataSource_Command(const std::string& prog_and_args, const std::vector<std::string>& paths) : MAX_BLOCK_USECS(100000), KILL_WAIT(10000) { arg_list = split_on(prog_and_args, ' '); if(arg_list.size() == 0) throw Invalid_Argument("DataSource_Command: No command given"); if(arg_list.size() > 5) throw Invalid_Argument("DataSource_Command: Too many args"); pipe = 0; create_pipe(paths); } /** * DataSource_Command Destructor */ DataSource_Command::~DataSource_Command() { if(!end_of_data()) shutdown_pipe(); } } /* * Program List for Unix_EntropySource * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /** * Default Commands for Entropy Gathering */ std::vector<Unix_Program> Unix_EntropySource::get_default_sources() { std::vector<Unix_Program> srcs; srcs.push_back(Unix_Program("netstat -in", 1)); srcs.push_back(Unix_Program("pfstat", 1)); srcs.push_back(Unix_Program("vmstat -s", 1)); srcs.push_back(Unix_Program("vmstat", 1)); srcs.push_back(Unix_Program("arp -a -n", 2)); srcs.push_back(Unix_Program("ifconfig -a", 2)); srcs.push_back(Unix_Program("iostat", 2)); srcs.push_back(Unix_Program("ipcs -a", 2)); srcs.push_back(Unix_Program("mpstat", 2)); srcs.push_back(Unix_Program("netstat -an", 2)); srcs.push_back(Unix_Program("netstat -s", 2)); srcs.push_back(Unix_Program("nfsstat", 2)); srcs.push_back(Unix_Program("portstat", 2)); srcs.push_back(Unix_Program("procinfo -a", 2)); srcs.push_back(Unix_Program("pstat -T", 2)); srcs.push_back(Unix_Program("pstat -s", 2)); srcs.push_back(Unix_Program("uname -a", 2)); srcs.push_back(Unix_Program("uptime", 2)); srcs.push_back(Unix_Program("listarea", 3)); srcs.push_back(Unix_Program("listdev", 3)); srcs.push_back(Unix_Program("ps -A", 3)); srcs.push_back(Unix_Program("sysinfo", 3)); srcs.push_back(Unix_Program("finger", 4)); srcs.push_back(Unix_Program("mailstats", 4)); srcs.push_back(Unix_Program("rpcinfo -p localhost", 4)); srcs.push_back(Unix_Program("who", 4)); srcs.push_back(Unix_Program("df -l", 4)); srcs.push_back(Unix_Program("dmesg", 4)); srcs.push_back(Unix_Program("last -5", 4)); srcs.push_back(Unix_Program("ls -alni /proc", 4)); srcs.push_back(Unix_Program("ls -alni /tmp", 4)); srcs.push_back(Unix_Program("pstat -f", 4)); srcs.push_back(Unix_Program("ps -elf", 5)); srcs.push_back(Unix_Program("ps aux", 5)); srcs.push_back(Unix_Program("lsof -n", 6)); srcs.push_back(Unix_Program("sar -A", 6)); return srcs; } } /* * Filters * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> namespace Botan { /* * StreamCipher_Filter Constructor */ StreamCipher_Filter::StreamCipher_Filter(StreamCipher* stream_cipher) : buffer(DEFAULT_BUFFERSIZE) { cipher = stream_cipher; } /* * StreamCipher_Filter Constructor */ StreamCipher_Filter::StreamCipher_Filter(StreamCipher* stream_cipher, const SymmetricKey& key) : buffer(DEFAULT_BUFFERSIZE) { cipher = stream_cipher; cipher->set_key(key); } /* * StreamCipher_Filter Constructor */ StreamCipher_Filter::StreamCipher_Filter(const std::string& sc_name) : buffer(DEFAULT_BUFFERSIZE) { Algorithm_Factory& af = global_state().algorithm_factory(); cipher = af.make_stream_cipher(sc_name); } /* * StreamCipher_Filter Constructor */ StreamCipher_Filter::StreamCipher_Filter(const std::string& sc_name, const SymmetricKey& key) : buffer(DEFAULT_BUFFERSIZE) { Algorithm_Factory& af = global_state().algorithm_factory(); cipher = af.make_stream_cipher(sc_name); cipher->set_key(key); } /* * Set the IV of a stream cipher */ void StreamCipher_Filter::set_iv(const InitializationVector& iv) { cipher->set_iv(iv.begin(), iv.length()); } /* * Write data into a StreamCipher_Filter */ void StreamCipher_Filter::write(const byte input[], size_t length) { while(length) { size_t copied = std::min<size_t>(length, buffer.size()); cipher->cipher(input, &buffer[0], copied); send(buffer, copied); input += copied; length -= copied; } } /* * Hash_Filter Constructor */ Hash_Filter::Hash_Filter(const std::string& algo_spec, size_t len) : OUTPUT_LENGTH(len) { Algorithm_Factory& af = global_state().algorithm_factory(); hash = af.make_hash_function(algo_spec); } /* * Complete a calculation by a Hash_Filter */ void Hash_Filter::end_msg() { SecureVector<byte> output = hash->final(); if(OUTPUT_LENGTH) send(output, std::min<size_t>(OUTPUT_LENGTH, output.size())); else send(output); } /* * MAC_Filter Constructor */ MAC_Filter::MAC_Filter(const std::string& mac_name, size_t len) : OUTPUT_LENGTH(len) { Algorithm_Factory& af = global_state().algorithm_factory(); mac = af.make_mac(mac_name); } /* * MAC_Filter Constructor */ MAC_Filter::MAC_Filter(const std::string& mac_name, const SymmetricKey& key, size_t len) : OUTPUT_LENGTH(len) { Algorithm_Factory& af = global_state().algorithm_factory(); mac = af.make_mac(mac_name); mac->set_key(key); } /* * Complete a calculation by a MAC_Filter */ void MAC_Filter::end_msg() { SecureVector<byte> output = mac->final(); if(OUTPUT_LENGTH) send(output, std::min<size_t>(OUTPUT_LENGTH, output.size())); else send(output); } } /* * Basic Filters * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { void Keyed_Filter::set_iv(const InitializationVector& iv) { if(iv.length() != 0) throw Invalid_IV_Length(name(), iv.length()); } /* * Chain Constructor */ Chain::Chain(Filter* f1, Filter* f2, Filter* f3, Filter* f4) { if(f1) { attach(f1); incr_owns(); } if(f2) { attach(f2); incr_owns(); } if(f3) { attach(f3); incr_owns(); } if(f4) { attach(f4); incr_owns(); } } /* * Chain Constructor */ Chain::Chain(Filter* filters[], size_t count) { for(size_t j = 0; j != count; ++j) if(filters[j]) { attach(filters[j]); incr_owns(); } } std::string Chain::name() const { return "Chain"; } /* * Fork Constructor */ Fork::Fork(Filter* f1, Filter* f2, Filter* f3, Filter* f4) { Filter* filters[4] = { f1, f2, f3, f4 }; set_next(filters, 4); } /* * Fork Constructor */ Fork::Fork(Filter* filters[], size_t count) { set_next(filters, count); } std::string Fork::name() const { return "Fork"; } } /* * Buffered Filter * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <stdexcept> namespace Botan { /* * Buffered_Filter Constructor */ Buffered_Filter::Buffered_Filter(size_t b, size_t f) : main_block_mod(b), final_minimum(f) { if(main_block_mod == 0) throw std::invalid_argument("main_block_mod == 0"); if(final_minimum > main_block_mod) throw std::invalid_argument("final_minimum > main_block_mod"); buffer.resize(2 * main_block_mod); buffer_pos = 0; } /* * Buffer input into blocks, trying to minimize copying */ void Buffered_Filter::write(const byte input[], size_t input_size) { if(!input_size) return; if(buffer_pos + input_size >= main_block_mod + final_minimum) { size_t to_copy = std::min<size_t>(buffer.size() - buffer_pos, input_size); copy_mem(&buffer[buffer_pos], input, to_copy); buffer_pos += to_copy; input += to_copy; input_size -= to_copy; size_t total_to_consume = round_down(std::min(buffer_pos, buffer_pos + input_size - final_minimum), main_block_mod); buffered_block(&buffer[0], total_to_consume); buffer_pos -= total_to_consume; copy_mem(&buffer[0], &buffer[total_to_consume], buffer_pos); } if(input_size >= final_minimum) { size_t full_blocks = (input_size - final_minimum) / main_block_mod; size_t to_copy = full_blocks * main_block_mod; if(to_copy) { buffered_block(input, to_copy); input += to_copy; input_size -= to_copy; } } copy_mem(&buffer[buffer_pos], input, input_size); buffer_pos += input_size; } /* * Finish/flush operation */ void Buffered_Filter::end_msg() { if(buffer_pos < final_minimum) throw std::runtime_error("Buffered filter end_msg without enough input"); size_t spare_blocks = (buffer_pos - final_minimum) / main_block_mod; if(spare_blocks) { size_t spare_bytes = main_block_mod * spare_blocks; buffered_block(&buffer[0], spare_bytes); buffered_final(&buffer[spare_bytes], buffer_pos - spare_bytes); } else { buffered_final(&buffer[0], buffer_pos); } buffer_pos = 0; } } /* * Base64 Encoder/Decoder * (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> namespace Botan { /* * Base64_Encoder Constructor */ Base64_Encoder::Base64_Encoder(bool breaks, size_t length, bool t_n) : line_length(breaks ? length : 0), trailing_newline(t_n && breaks), in(48), out(64), position(0), out_position(0) { } /* * Encode and send a block */ void Base64_Encoder::encode_and_send(const byte input[], size_t length, bool final_inputs) { while(length) { const size_t proc = std::min(length, in.size()); size_t consumed = 0; size_t produced = base64_encode(reinterpret_cast<char*>(&out[0]), input, proc, consumed, final_inputs); do_output(&out[0], produced); // FIXME: s/proc/consumed/? input += proc; length -= proc; } } /* * Handle the output */ void Base64_Encoder::do_output(const byte input[], size_t length) { if(line_length == 0) send(input, length); else { size_t remaining = length, offset = 0; while(remaining) { size_t sent = std::min(line_length - out_position, remaining); send(input + offset, sent); out_position += sent; remaining -= sent; offset += sent; if(out_position == line_length) { send('\n'); out_position = 0; } } } } /* * Convert some data into Base64 */ void Base64_Encoder::write(const byte input[], size_t length) { in.copy(position, input, length); if(position + length >= in.size()) { encode_and_send(&in[0], in.size()); input += (in.size() - position); length -= (in.size() - position); while(length >= in.size()) { encode_and_send(input, in.size()); input += in.size(); length -= in.size(); } in.copy(input, length); position = 0; } position += length; } /* * Flush buffers */ void Base64_Encoder::end_msg() { encode_and_send(&in[0], position, true); if(trailing_newline || (out_position && line_length)) send('\n'); out_position = position = 0; } /* * Base64_Decoder Constructor */ Base64_Decoder::Base64_Decoder(Decoder_Checking c) : checking(c), in(64), out(48), position(0) { } /* * Convert some data from Base64 */ void Base64_Decoder::write(const byte input[], size_t length) { while(length) { size_t to_copy = std::min<size_t>(length, in.size() - position); copy_mem(&in[position], input, to_copy); position += to_copy; size_t consumed = 0; size_t written = base64_decode(&out[0], reinterpret_cast<const char*>(&in[0]), position, consumed, false, checking != FULL_CHECK); send(out, written); if(consumed != position) { copy_mem(&in[0], &in[consumed], position - consumed); position = position - consumed; } else position = 0; length -= to_copy; input += to_copy; } } /* * Flush buffers */ void Base64_Decoder::end_msg() { size_t consumed = 0; size_t written = base64_decode(&out[0], reinterpret_cast<const char*>(&in[0]), position, consumed, true, checking != FULL_CHECK); send(out, written); const bool not_full_bytes = consumed != position; position = 0; if(not_full_bytes) throw std::invalid_argument("Base64_Decoder: Input not full bytes"); } } /* * Hex Encoder/Decoder * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> namespace Botan { /** * Size used for internal buffer in hex encoder/decoder */ const size_t HEX_CODEC_BUFFER_SIZE = 256; /* * Hex_Encoder Constructor */ Hex_Encoder::Hex_Encoder(bool breaks, size_t length, Case c) : casing(c), line_length(breaks ? length : 0) { in.resize(HEX_CODEC_BUFFER_SIZE); out.resize(2*in.size()); counter = position = 0; } /* * Hex_Encoder Constructor */ Hex_Encoder::Hex_Encoder(Case c) : casing(c), line_length(0) { in.resize(HEX_CODEC_BUFFER_SIZE); out.resize(2*in.size()); counter = position = 0; } /* * Encode and send a block */ void Hex_Encoder::encode_and_send(const byte block[], size_t length) { hex_encode(reinterpret_cast<char*>(&out[0]), block, length, casing == Uppercase); if(line_length == 0) send(out, 2*length); else { size_t remaining = 2*length, offset = 0; while(remaining) { size_t sent = std::min(line_length - counter, remaining); send(&out[offset], sent); counter += sent; remaining -= sent; offset += sent; if(counter == line_length) { send('\n'); counter = 0; } } } } /* * Convert some data into hex format */ void Hex_Encoder::write(const byte input[], size_t length) { in.copy(position, input, length); if(position + length >= in.size()) { encode_and_send(&in[0], in.size()); input += (in.size() - position); length -= (in.size() - position); while(length >= in.size()) { encode_and_send(input, in.size()); input += in.size(); length -= in.size(); } in.copy(input, length); position = 0; } position += length; } /* * Flush buffers */ void Hex_Encoder::end_msg() { encode_and_send(&in[0], position); if(counter && line_length) send('\n'); counter = position = 0; } /* * Hex_Decoder Constructor */ Hex_Decoder::Hex_Decoder(Decoder_Checking c) : checking(c) { in.resize(HEX_CODEC_BUFFER_SIZE); out.resize(in.size() / 2); position = 0; } /* * Convert some data from hex format */ void Hex_Decoder::write(const byte input[], size_t length) { while(length) { size_t to_copy = std::min<size_t>(length, in.size() - position); copy_mem(&in[position], input, to_copy); position += to_copy; size_t consumed = 0; size_t written = hex_decode(&out[0], reinterpret_cast<const char*>(&in[0]), position, consumed, checking != FULL_CHECK); send(out, written); if(consumed != position) { copy_mem(&in[0], &in[consumed], position - consumed); position = position - consumed; } else position = 0; length -= to_copy; input += to_copy; } } /* * Flush buffers */ void Hex_Decoder::end_msg() { size_t consumed = 0; size_t written = hex_decode(&out[0], reinterpret_cast<const char*>(&in[0]), position, consumed, checking != FULL_CHECK); send(out, written); const bool not_full_bytes = consumed != position; position = 0; if(not_full_bytes) throw std::invalid_argument("Hex_Decoder: Input not full bytes"); } } /* * DataSink * (C) 1999-2007 Jack Lloyd * 2005 Matthew Gregan * * Distributed under the terms of the Botan license */ #include <fstream> namespace Botan { /* * Write to a stream */ void DataSink_Stream::write(const byte out[], size_t length) { sink.write(reinterpret_cast<const char*>(out), length); if(!sink.good()) throw Stream_IO_Error("DataSink_Stream: Failure writing to " + identifier); } /* * DataSink_Stream Constructor */ DataSink_Stream::DataSink_Stream(std::ostream& out, const std::string& name) : identifier(name), sink_p(0), sink(out) { } /* * DataSink_Stream Constructor */ DataSink_Stream::DataSink_Stream(const std::string& path, bool use_binary) : identifier(path), sink_p(new std::ofstream( path.c_str(), use_binary ? std::ios::binary : std::ios::out)), sink(*sink_p) { if(!sink.good()) { delete sink_p; throw Stream_IO_Error("DataSink_Stream: Failure opening " + path); } } /* * DataSink_Stream Destructor */ DataSink_Stream::~DataSink_Stream() { delete sink_p; } } /* * DataSource * (C) 1999-2007 Jack Lloyd * 2005 Matthew Gregan * * Distributed under the terms of the Botan license */ #include <fstream> #include <algorithm> namespace Botan { /* * Read a single byte from the DataSource */ size_t DataSource::read_byte(byte& out) { return read(&out, 1); } /* * Peek a single byte from the DataSource */ size_t DataSource::peek_byte(byte& out) const { return peek(&out, 1, 0); } /* * Discard the next N bytes of the data */ size_t DataSource::discard_next(size_t n) { size_t discarded = 0; byte dummy; for(size_t j = 0; j != n; ++j) discarded += read_byte(dummy); return discarded; } /* * Read from a memory buffer */ size_t DataSource_Memory::read(byte out[], size_t length) { size_t got = std::min<size_t>(source.size() - offset, length); copy_mem(out, &source[offset], got); offset += got; return got; } /* * Peek into a memory buffer */ size_t DataSource_Memory::peek(byte out[], size_t length, size_t peek_offset) const { const size_t bytes_left = source.size() - offset; if(peek_offset >= bytes_left) return 0; size_t got = std::min(bytes_left - peek_offset, length); copy_mem(out, &source[offset + peek_offset], got); return got; } /* * Check if the memory buffer is empty */ bool DataSource_Memory::end_of_data() const { return (offset == source.size()); } /* * DataSource_Memory Constructor */ DataSource_Memory::DataSource_Memory(const byte in[], size_t length) : source(in, length) { offset = 0; } /* * DataSource_Memory Constructor */ DataSource_Memory::DataSource_Memory(const MemoryRegion<byte>& in) : source(in) { offset = 0; } /* * DataSource_Memory Constructor */ DataSource_Memory::DataSource_Memory(const std::string& in) : source(reinterpret_cast<const byte*>(in.data()), in.length()) { offset = 0; } /* * Read from a stream */ size_t DataSource_Stream::read(byte out[], size_t length) { source.read(reinterpret_cast<char*>(out), length); if(source.bad()) throw Stream_IO_Error("DataSource_Stream::read: Source failure"); size_t got = source.gcount(); total_read += got; return got; } /* * Peek into a stream */ size_t DataSource_Stream::peek(byte out[], size_t length, size_t offset) const { if(end_of_data()) throw Invalid_State("DataSource_Stream: Cannot peek when out of data"); size_t got = 0; if(offset) { SecureVector<byte> buf(offset); source.read(reinterpret_cast<char*>(&buf[0]), buf.size()); if(source.bad()) throw Stream_IO_Error("DataSource_Stream::peek: Source failure"); got = source.gcount(); } if(got == offset) { source.read(reinterpret_cast<char*>(out), length); if(source.bad()) throw Stream_IO_Error("DataSource_Stream::peek: Source failure"); got = source.gcount(); } if(source.eof()) source.clear(); source.seekg(total_read, std::ios::beg); return got; } /* * Check if the stream is empty or in error */ bool DataSource_Stream::end_of_data() const { return (!source.good()); } /* * Return a human-readable ID for this stream */ std::string DataSource_Stream::id() const { return identifier; } /* * DataSource_Stream Constructor */ DataSource_Stream::DataSource_Stream(const std::string& path, bool use_binary) : identifier(path), source_p(new std::ifstream( path.c_str(), use_binary ? std::ios::binary : std::ios::in)), source(*source_p), total_read(0) { if(!source.good()) { delete source_p; throw Stream_IO_Error("DataSource: Failure opening file " + path); } } /* * DataSource_Stream Constructor */ DataSource_Stream::DataSource_Stream(std::istream& in, const std::string& name) : identifier(name), source_p(0), source(in), total_read(0) { } /* * DataSource_Stream Destructor */ DataSource_Stream::~DataSource_Stream() { delete source_p; } } /* * Pipe I/O for Unix * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <unistd.h> namespace Botan { /* * Write data from a pipe into a Unix fd */ int operator<<(int fd, Pipe& pipe) { SecureVector<byte> buffer(DEFAULT_BUFFERSIZE); while(pipe.remaining()) { size_t got = pipe.read(&buffer[0], buffer.size()); size_t position = 0; while(got) { ssize_t ret = write(fd, &buffer[position], got); if(ret == -1) throw Stream_IO_Error("Pipe output operator (unixfd) has failed"); position += ret; got -= ret; } } return fd; } /* * Read data from a Unix fd into a pipe */ int operator>>(int fd, Pipe& pipe) { SecureVector<byte> buffer(DEFAULT_BUFFERSIZE); while(true) { ssize_t ret = read(fd, &buffer[0], buffer.size()); if(ret == 0) break; if(ret == -1) throw Stream_IO_Error("Pipe input operator (unixfd) has failed"); pipe.write(&buffer[0], ret); } return fd; } } /* * Filter * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Filter Constructor */ Filter::Filter() { next.resize(1); port_num = 0; filter_owns = 0; owned = false; } /* * Send data to all ports */ void Filter::send(const byte input[], size_t length) { bool nothing_attached = true; for(size_t j = 0; j != total_ports(); ++j) if(next[j]) { if(write_queue.size()) next[j]->write(&write_queue[0], write_queue.size()); next[j]->write(input, length); nothing_attached = false; } if(nothing_attached) write_queue += std::make_pair(input, length); else write_queue.clear(); } /* * Start a new message */ void Filter::new_msg() { start_msg(); for(size_t j = 0; j != total_ports(); ++j) if(next[j]) next[j]->new_msg(); } /* * End the current message */ void Filter::finish_msg() { end_msg(); for(size_t j = 0; j != total_ports(); ++j) if(next[j]) next[j]->finish_msg(); } /* * Attach a filter to the current port */ void Filter::attach(Filter* new_filter) { if(new_filter) { Filter* last = this; while(last->get_next()) last = last->get_next(); last->next[last->current_port()] = new_filter; } } /* * Set the active port on a filter */ void Filter::set_port(size_t new_port) { if(new_port >= total_ports()) throw Invalid_Argument("Filter: Invalid port number"); port_num = new_port; } /* * Return the next Filter in the logical chain */ Filter* Filter::get_next() const { if(port_num < next.size()) return next[port_num]; return 0; } /* * Set the next Filters */ void Filter::set_next(Filter* filters[], size_t size) { while(size && filters && filters[size-1] == 0) --size; next.clear(); next.resize(size); port_num = 0; filter_owns = 0; for(size_t j = 0; j != size; ++j) next[j] = filters[j]; } /* * Return the total number of ports */ size_t Filter::total_ports() const { return next.size(); } } /* * CBC Mode * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> namespace Botan { /* * CBC Encryption Constructor */ CBC_Encryption::CBC_Encryption(BlockCipher* ciph, BlockCipherModePaddingMethod* pad) : Buffered_Filter(ciph->block_size(), 0), cipher(ciph), padder(pad) { if(!padder->valid_blocksize(cipher->block_size())) throw Invalid_Block_Size(name(), padder->name()); state.resize(cipher->block_size()); } /* * CBC Encryption Constructor */ CBC_Encryption::CBC_Encryption(BlockCipher* ciph, BlockCipherModePaddingMethod* pad, const SymmetricKey& key, const InitializationVector& iv) : Buffered_Filter(ciph->block_size(), 0), cipher(ciph), padder(pad) { if(!padder->valid_blocksize(cipher->block_size())) throw Invalid_Block_Size(name(), padder->name()); state.resize(cipher->block_size()); set_key(key); set_iv(iv); } /* * Set the IV */ void CBC_Encryption::set_iv(const InitializationVector& iv) { if(!valid_iv_length(iv.length())) throw Invalid_IV_Length(name(), iv.length()); state = iv.bits_of(); buffer_reset(); } /* * Encrypt in CBC mode */ void CBC_Encryption::buffered_block(const byte input[], size_t length) { size_t blocks = length / state.size(); for(size_t i = 0; i != blocks; ++i) { xor_buf(state, input + i * cipher->block_size(), state.size()); cipher->encrypt(state); send(state, state.size()); } } /* * Finish encrypting in CBC mode */ void CBC_Encryption::buffered_final(const byte input[], size_t length) { if(length % cipher->block_size() == 0) buffered_block(input, length); else if(length != 0) throw Encoding_Error(name() + ": Did not pad to full blocksize"); } void CBC_Encryption::write(const byte input[], size_t input_length) { Buffered_Filter::write(input, input_length); } void CBC_Encryption::end_msg() { size_t last_block = current_position() % cipher->block_size(); SecureVector<byte> padding(cipher->block_size()); padder->pad(padding, padding.size(), last_block); size_t pad_bytes = padder->pad_bytes(cipher->block_size(), last_block); if(pad_bytes) Buffered_Filter::write(padding, pad_bytes); Buffered_Filter::end_msg(); } /* * Return a CBC mode name */ std::string CBC_Encryption::name() const { return (cipher->name() + "/CBC/" + padder->name()); } /* * CBC Decryption Constructor */ CBC_Decryption::CBC_Decryption(BlockCipher* ciph, BlockCipherModePaddingMethod* pad) : Buffered_Filter(ciph->parallel_bytes(), ciph->block_size()), cipher(ciph), padder(pad) { if(!padder->valid_blocksize(cipher->block_size())) throw Invalid_Block_Size(name(), padder->name()); state.resize(cipher->block_size()); temp.resize(buffered_block_size()); } /* * CBC Decryption Constructor */ CBC_Decryption::CBC_Decryption(BlockCipher* ciph, BlockCipherModePaddingMethod* pad, const SymmetricKey& key, const InitializationVector& iv) : Buffered_Filter(ciph->parallel_bytes(), ciph->block_size()), cipher(ciph), padder(pad) { if(!padder->valid_blocksize(cipher->block_size())) throw Invalid_Block_Size(name(), padder->name()); state.resize(cipher->block_size()); temp.resize(buffered_block_size()); set_key(key); set_iv(iv); } /* * Set the IV */ void CBC_Decryption::set_iv(const InitializationVector& iv) { if(!valid_iv_length(iv.length())) throw Invalid_IV_Length(name(), iv.length()); state = iv.bits_of(); buffer_reset(); } /* * Decrypt in CBC mode */ void CBC_Decryption::buffered_block(const byte input[], size_t length) { const size_t blocks_in_temp = temp.size() / cipher->block_size(); size_t blocks = length / cipher->block_size(); while(blocks) { size_t to_proc = std::min<size_t>(blocks, blocks_in_temp); cipher->decrypt_n(input, &temp[0], to_proc); xor_buf(temp, state, cipher->block_size()); for(size_t i = 1; i < to_proc; ++i) xor_buf(&temp[i * cipher->block_size()], input + (i-1) * cipher->block_size(), cipher->block_size()); copy_mem(&state[0], input + (to_proc - 1) * cipher->block_size(), cipher->block_size()); send(temp, to_proc * cipher->block_size()); input += to_proc * cipher->block_size(); blocks -= to_proc; } } /* * Finish encrypting in CBC mode */ void CBC_Decryption::buffered_final(const byte input[], size_t length) { if(length == 0 || length % cipher->block_size() != 0) throw Decoding_Error(name() + ": Ciphertext not multiple of block size"); size_t extra_blocks = (length - 1) / cipher->block_size(); buffered_block(input, extra_blocks * cipher->block_size()); input += extra_blocks * cipher->block_size(); cipher->decrypt(input, temp); xor_buf(temp, state, cipher->block_size()); send(temp, padder->unpad(temp, cipher->block_size())); copy_mem(&state[0], input, state.size()); // save for IV chaining } /* * Decrypt in CBC mode */ void CBC_Decryption::write(const byte input[], size_t length) { Buffered_Filter::write(input, length); } /* * Finish decrypting in CBC mode */ void CBC_Decryption::end_msg() { Buffered_Filter::end_msg(); } /* * Return a CBC mode name */ std::string CBC_Decryption::name() const { return (cipher->name() + "/CBC/" + padder->name()); } } /* * CFB Mode * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> namespace Botan { /* * CFB Encryption Constructor */ CFB_Encryption::CFB_Encryption(BlockCipher* ciph, size_t fback_bits) { cipher = ciph; feedback = fback_bits ? fback_bits / 8: cipher->block_size(); buffer.resize(cipher->block_size()); state.resize(cipher->block_size()); position = 0; if(feedback == 0 || fback_bits % 8 != 0 || feedback > cipher->block_size()) throw Invalid_Argument("CFB_Encryption: Invalid feedback size " + to_string(fback_bits)); } /* * CFB Encryption Constructor */ CFB_Encryption::CFB_Encryption(BlockCipher* ciph, const SymmetricKey& key, const InitializationVector& iv, size_t fback_bits) { cipher = ciph; feedback = fback_bits ? fback_bits / 8: cipher->block_size(); buffer.resize(cipher->block_size()); state.resize(cipher->block_size()); position = 0; if(feedback == 0 || fback_bits % 8 != 0 || feedback > cipher->block_size()) throw Invalid_Argument("CFB_Encryption: Invalid feedback size " + to_string(fback_bits)); set_key(key); set_iv(iv); } void CFB_Encryption::set_iv(const InitializationVector& iv) { if(!valid_iv_length(iv.length())) throw Invalid_IV_Length(name(), iv.length()); state = iv.bits_of(); zeroise(buffer); position = 0; cipher->encrypt(state, buffer); } /* * Encrypt data in CFB mode */ void CFB_Encryption::write(const byte input[], size_t length) { while(length) { size_t xored = std::min(feedback - position, length); xor_buf(&buffer[position], input, xored); send(&buffer[position], xored); input += xored; length -= xored; position += xored; if(position == feedback) { for(size_t j = 0; j != cipher->block_size() - feedback; ++j) state[j] = state[j + feedback]; state.copy(cipher->block_size() - feedback, buffer, feedback); cipher->encrypt(state, buffer); position = 0; } } } /* * CFB Decryption Constructor */ CFB_Decryption::CFB_Decryption(BlockCipher* ciph, size_t fback_bits) { cipher = ciph; feedback = fback_bits ? fback_bits / 8: cipher->block_size(); buffer.resize(cipher->block_size()); state.resize(cipher->block_size()); position = 0; if(feedback == 0 || fback_bits % 8 != 0 || feedback > cipher->block_size()) throw Invalid_Argument("CFB_Decryption: Invalid feedback size " + to_string(fback_bits)); } /* * CFB Decryption Constructor */ CFB_Decryption::CFB_Decryption(BlockCipher* ciph, const SymmetricKey& key, const InitializationVector& iv, size_t fback_bits) { cipher = ciph; feedback = fback_bits ? fback_bits / 8: cipher->block_size(); buffer.resize(cipher->block_size()); state.resize(cipher->block_size()); position = 0; if(feedback == 0 || fback_bits % 8 != 0 || feedback > cipher->block_size()) throw Invalid_Argument("CFB_Decryption: Invalid feedback size " + to_string(fback_bits)); set_key(key); set_iv(iv); } void CFB_Decryption::set_iv(const InitializationVector& iv) { if(!valid_iv_length(iv.length())) throw Invalid_IV_Length(name(), iv.length()); state = iv.bits_of(); zeroise(buffer); position = 0; cipher->encrypt(state, buffer); } /* * Decrypt data in CFB mode */ void CFB_Decryption::write(const byte input[], size_t length) { while(length) { size_t xored = std::min(feedback - position, length); xor_buf(&buffer[position], input, xored); send(&buffer[position], xored); buffer.copy(position, input, xored); input += xored; length -= xored; position += xored; if(position == feedback) { for(size_t j = 0; j != cipher->block_size() - feedback; ++j) state[j] = state[j + feedback]; state.copy(cipher->block_size() - feedback, buffer, feedback); cipher->encrypt(state, buffer); position = 0; } } } } /* * CTS Mode * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> namespace Botan { /* * CTS Encryption Constructor */ CTS_Encryption::CTS_Encryption(BlockCipher* ciph) : cipher(ciph) { buffer.resize(2 * cipher->block_size()); state.resize(cipher->block_size()); position = 0; } /* * CTS Encryption Constructor */ CTS_Encryption::CTS_Encryption(BlockCipher* ciph, const SymmetricKey& key, const InitializationVector& iv) : cipher(ciph) { buffer.resize(2 * cipher->block_size()); state.resize(cipher->block_size()); position = 0; set_key(key); set_iv(iv); } /* * Set the IV */ void CTS_Encryption::set_iv(const InitializationVector& iv) { if(!valid_iv_length(iv.length())) throw Invalid_IV_Length(name(), iv.length()); state = iv.bits_of(); zeroise(buffer); position = 0; } /* * Encrypt a block */ void CTS_Encryption::encrypt(const byte block[]) { xor_buf(state, block, cipher->block_size()); cipher->encrypt(state); send(state, cipher->block_size()); } /* * Encrypt in CTS mode */ void CTS_Encryption::write(const byte input[], size_t length) { size_t copied = std::min<size_t>(buffer.size() - position, length); buffer.copy(position, input, copied); length -= copied; input += copied; position += copied; if(length == 0) return; encrypt(&buffer[0]); if(length > cipher->block_size()) { encrypt(&buffer[cipher->block_size()]); while(length > 2*cipher->block_size()) { encrypt(input); length -= cipher->block_size(); input += cipher->block_size(); } position = 0; } else { copy_mem(&buffer[0], &buffer[cipher->block_size()], cipher->block_size()); position = cipher->block_size(); } buffer.copy(position, input, length); position += length; } /* * Finish encrypting in CTS mode */ void CTS_Encryption::end_msg() { if(position < cipher->block_size() + 1) throw Encoding_Error(name() + ": insufficient data to encrypt"); xor_buf(state, buffer, cipher->block_size()); cipher->encrypt(state); SecureVector<byte> cn = state; clear_mem(&buffer[position], buffer.size() - position); encrypt(&buffer[cipher->block_size()]); send(cn, position - cipher->block_size()); } /* * CTS Decryption Constructor */ CTS_Decryption::CTS_Decryption(BlockCipher* ciph) : cipher(ciph) { buffer.resize(2 * cipher->block_size()); state.resize(cipher->block_size()); temp.resize(cipher->block_size()); position = 0; } /* * CTS Decryption Constructor */ CTS_Decryption::CTS_Decryption(BlockCipher* ciph, const SymmetricKey& key, const InitializationVector& iv) : cipher(ciph) { buffer.resize(2 * cipher->block_size()); state.resize(cipher->block_size()); temp.resize(cipher->block_size()); position = 0; set_key(key); set_iv(iv); } /* * Set the IV */ void CTS_Decryption::set_iv(const InitializationVector& iv) { if(!valid_iv_length(iv.length())) throw Invalid_IV_Length(name(), iv.length()); state = iv.bits_of(); zeroise(buffer); position = 0; } /* * Decrypt a block */ void CTS_Decryption::decrypt(const byte block[]) { cipher->decrypt(block, &temp[0]); xor_buf(temp, state, cipher->block_size()); send(temp, cipher->block_size()); state.copy(block, cipher->block_size()); } /* * Decrypt in CTS mode */ void CTS_Decryption::write(const byte input[], size_t length) { size_t copied = std::min<size_t>(buffer.size() - position, length); buffer.copy(position, input, copied); length -= copied; input += copied; position += copied; if(length == 0) return; decrypt(buffer); if(length > cipher->block_size()) { decrypt(&buffer[cipher->block_size()]); while(length > 2*cipher->block_size()) { decrypt(input); length -= cipher->block_size(); input += cipher->block_size(); } position = 0; } else { copy_mem(&buffer[0], &buffer[cipher->block_size()], cipher->block_size()); position = cipher->block_size(); } buffer.copy(position, input, length); position += length; } /* * Finish decrypting in CTS mode */ void CTS_Decryption::end_msg() { cipher->decrypt(buffer, temp); xor_buf(temp, &buffer[cipher->block_size()], position - cipher->block_size()); SecureVector<byte> xn = temp; copy_mem(&buffer[position], &xn[position - cipher->block_size()], buffer.size() - position); cipher->decrypt(&buffer[cipher->block_size()], temp); xor_buf(temp, state, cipher->block_size()); send(temp, cipher->block_size()); send(xn, position - cipher->block_size()); } } /* * EAX Mode Encryption * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> namespace Botan { namespace { /* * EAX MAC-based PRF */ SecureVector<byte> eax_prf(byte tag, size_t BLOCK_SIZE, MessageAuthenticationCode* mac, const byte in[], size_t length) { for(size_t i = 0; i != BLOCK_SIZE - 1; ++i) mac->update(0); mac->update(tag); mac->update(in, length); return mac->final(); } } /* * EAX_Base Constructor */ EAX_Base::EAX_Base(BlockCipher* cipher, size_t tag_size) : BLOCK_SIZE(cipher->block_size()), TAG_SIZE(tag_size ? tag_size / 8 : BLOCK_SIZE), cipher_name(cipher->name()), ctr_buf(DEFAULT_BUFFERSIZE) { cmac = new CMAC(cipher->clone()); ctr = new CTR_BE(cipher); // takes ownership if(tag_size % 8 != 0 || TAG_SIZE == 0 || TAG_SIZE > cmac->output_length()) throw Invalid_Argument(name() + ": Bad tag size " + to_string(tag_size)); } /* * Check if a keylength is valid for EAX */ bool EAX_Base::valid_keylength(size_t n) const { if(!ctr->valid_keylength(n)) return false; return true; } /* * Set the EAX key */ void EAX_Base::set_key(const SymmetricKey& key) { /* * These could share the key schedule, which is one nice part of EAX, * but it's much easier to ignore that here... */ ctr->set_key(key); cmac->set_key(key); header_mac = eax_prf(1, BLOCK_SIZE, cmac, 0, 0); } /* * Do setup at the start of each message */ void EAX_Base::start_msg() { for(size_t i = 0; i != BLOCK_SIZE - 1; ++i) cmac->update(0); cmac->update(2); } /* * Set the EAX nonce */ void EAX_Base::set_iv(const InitializationVector& iv) { nonce_mac = eax_prf(0, BLOCK_SIZE, cmac, iv.begin(), iv.length()); ctr->set_iv(&nonce_mac[0], nonce_mac.size()); } /* * Set the EAX header */ void EAX_Base::set_header(const byte header[], size_t length) { header_mac = eax_prf(1, BLOCK_SIZE, cmac, header, length); } /* * Return the name of this cipher mode */ std::string EAX_Base::name() const { return (cipher_name + "/EAX"); } /* * Encrypt in EAX mode */ void EAX_Encryption::write(const byte input[], size_t length) { while(length) { size_t copied = std::min<size_t>(length, ctr_buf.size()); ctr->cipher(input, &ctr_buf[0], copied); cmac->update(&ctr_buf[0], copied); send(ctr_buf, copied); input += copied; length -= copied; } } /* * Finish encrypting in EAX mode */ void EAX_Encryption::end_msg() { SecureVector<byte> data_mac = cmac->final(); xor_buf(data_mac, nonce_mac, data_mac.size()); xor_buf(data_mac, header_mac, data_mac.size()); send(data_mac, TAG_SIZE); } } /* * EAX Mode Encryption * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> namespace Botan { /* * EAX_Decryption Constructor */ EAX_Decryption::EAX_Decryption(BlockCipher* ciph, size_t tag_size) : EAX_Base(ciph, tag_size) { queue.resize(2*TAG_SIZE + DEFAULT_BUFFERSIZE); queue_start = queue_end = 0; } /* * EAX_Decryption Constructor */ EAX_Decryption::EAX_Decryption(BlockCipher* ciph, const SymmetricKey& key, const InitializationVector& iv, size_t tag_size) : EAX_Base(ciph, tag_size) { set_key(key); set_iv(iv); queue.resize(2*TAG_SIZE + DEFAULT_BUFFERSIZE); queue_start = queue_end = 0; } /* * Decrypt in EAX mode */ void EAX_Decryption::write(const byte input[], size_t length) { while(length) { const size_t copied = std::min<size_t>(length, queue.size() - queue_end); queue.copy(queue_end, input, copied); input += copied; length -= copied; queue_end += copied; while((queue_end - queue_start) > TAG_SIZE) { size_t removed = (queue_end - queue_start) - TAG_SIZE; do_write(&queue[queue_start], removed); queue_start += removed; } if(queue_start + TAG_SIZE == queue_end && queue_start >= queue.size() / 2) { SecureVector<byte> queue_data(TAG_SIZE); queue_data.copy(&queue[queue_start], TAG_SIZE); queue.copy(&queue_data[0], TAG_SIZE); queue_start = 0; queue_end = TAG_SIZE; } } } /* * Decrypt in EAX mode */ void EAX_Decryption::do_write(const byte input[], size_t length) { while(length) { size_t copied = std::min<size_t>(length, ctr_buf.size()); /* Process same block with cmac and ctr at the same time to help cache locality. */ cmac->update(input, copied); ctr->cipher(input, &ctr_buf[0], copied); send(ctr_buf, copied); input += copied; length -= copied; } } /* * Finish decrypting in EAX mode */ void EAX_Decryption::end_msg() { if((queue_end - queue_start) != TAG_SIZE) throw Decoding_Error(name() + ": Message authentication failure"); SecureVector<byte> data_mac = cmac->final(); for(size_t j = 0; j != TAG_SIZE; ++j) if(queue[queue_start+j] != (data_mac[j] ^ nonce_mac[j] ^ header_mac[j])) throw Decoding_Error(name() + ": Message authentication failure"); queue_start = queue_end = 0; } } /* * ECB Mode * (C) 1999-2009 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * ECB_Encryption Constructor */ ECB_Encryption::ECB_Encryption(BlockCipher* ciph, BlockCipherModePaddingMethod* pad) : Buffered_Filter(ciph->parallel_bytes(), 0) { cipher = ciph; padder = pad; temp.resize(buffered_block_size()); } /* * ECB_Encryption Constructor */ ECB_Encryption::ECB_Encryption(BlockCipher* ciph, BlockCipherModePaddingMethod* pad, const SymmetricKey& key) : Buffered_Filter(ciph->parallel_bytes(), 0) { cipher = ciph; padder = pad; temp.resize(buffered_block_size()); cipher->set_key(key); } /* * ECB_Encryption Destructor */ ECB_Encryption::~ECB_Encryption() { delete cipher; delete padder; } /* * Return an ECB mode name */ std::string ECB_Encryption::name() const { return (cipher->name() + "/ECB/" + padder->name()); } /* * Encrypt in ECB mode */ void ECB_Encryption::write(const byte input[], size_t length) { Buffered_Filter::write(input, length); } /* * Finish encrypting in ECB mode */ void ECB_Encryption::end_msg() { size_t last_block = current_position() % cipher->block_size(); SecureVector<byte> padding(cipher->block_size()); padder->pad(padding, padding.size(), last_block); size_t pad_bytes = padder->pad_bytes(cipher->block_size(), last_block); if(pad_bytes) Buffered_Filter::write(padding, pad_bytes); Buffered_Filter::end_msg(); } void ECB_Encryption::buffered_block(const byte input[], size_t input_length) { const size_t blocks_in_temp = temp.size() / cipher->block_size(); size_t blocks = input_length / cipher->block_size(); while(blocks) { size_t to_proc = std::min(blocks, blocks_in_temp); cipher->encrypt_n(input, &temp[0], to_proc); send(temp, to_proc * cipher->block_size()); input += to_proc * cipher->block_size(); blocks -= to_proc; } } void ECB_Encryption::buffered_final(const byte input[], size_t input_length) { if(input_length % cipher->block_size() == 0) buffered_block(input, input_length); else if(input_length != 0) throw Encoding_Error(name() + ": Did not pad to full blocksize"); } /* * ECB_Decryption Constructor */ ECB_Decryption::ECB_Decryption(BlockCipher* ciph, BlockCipherModePaddingMethod* pad) : Buffered_Filter(ciph->parallel_bytes(), 1) { cipher = ciph; padder = pad; temp.resize(buffered_block_size()); } /* * ECB_Decryption Constructor */ ECB_Decryption::ECB_Decryption(BlockCipher* ciph, BlockCipherModePaddingMethod* pad, const SymmetricKey& key) : Buffered_Filter(ciph->parallel_bytes(), 1) { cipher = ciph; padder = pad; temp.resize(buffered_block_size()); cipher->set_key(key); } /* * ECB_Decryption Destructor */ ECB_Decryption::~ECB_Decryption() { delete cipher; delete padder; } /* * Return an ECB mode name */ std::string ECB_Decryption::name() const { return (cipher->name() + "/ECB/" + padder->name()); } /* * Decrypt in ECB mode */ void ECB_Decryption::write(const byte input[], size_t length) { Buffered_Filter::write(input, length); } /* * Finish decrypting in ECB mode */ void ECB_Decryption::end_msg() { Buffered_Filter::end_msg(); } /* * Decrypt in ECB mode */ void ECB_Decryption::buffered_block(const byte input[], size_t length) { const size_t blocks_in_temp = temp.size() / cipher->block_size(); size_t blocks = length / cipher->block_size(); while(blocks) { size_t to_proc = std::min(blocks, blocks_in_temp); cipher->decrypt_n(input, &temp[0], to_proc); send(temp, to_proc * cipher->block_size()); input += to_proc * cipher->block_size(); blocks -= to_proc; } } /* * Finish encrypting in ECB mode */ void ECB_Decryption::buffered_final(const byte input[], size_t length) { if(length == 0 || length % cipher->block_size() != 0) throw Decoding_Error(name() + ": Ciphertext not multiple of block size"); size_t extra_blocks = (length - 1) / cipher->block_size(); buffered_block(input, extra_blocks * cipher->block_size()); input += extra_blocks * cipher->block_size(); cipher->decrypt(input, temp); send(temp, padder->unpad(temp, cipher->block_size())); } } /* * CBC Padding Methods * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Default amount of padding */ size_t BlockCipherModePaddingMethod::pad_bytes(size_t bs, size_t pos) const { return (bs - pos); } /* * Pad with PKCS #7 Method */ void PKCS7_Padding::pad(byte block[], size_t size, size_t position) const { const size_t bytes_remaining = size - position; const byte pad_value = static_cast<byte>(bytes_remaining); BOTAN_ASSERT_EQUAL(pad_value, bytes_remaining, "Overflow in PKCS7_Padding"); for(size_t j = 0; j != size; ++j) block[j] = pad_value; } /* * Unpad with PKCS #7 Method */ size_t PKCS7_Padding::unpad(const byte block[], size_t size) const { size_t position = block[size-1]; if(position > size) throw Decoding_Error(name()); for(size_t j = size-position; j != size-1; ++j) if(block[j] != position) throw Decoding_Error(name()); return (size-position); } /* * Query if the size is valid for this method */ bool PKCS7_Padding::valid_blocksize(size_t size) const { if(size > 0 && size < 256) return true; else return false; } /* * Pad with ANSI X9.23 Method */ void ANSI_X923_Padding::pad(byte block[], size_t size, size_t position) const { for(size_t j = 0; j != size-position; ++j) block[j] = 0; block[size-position-1] = static_cast<byte>(size-position); } /* * Unpad with ANSI X9.23 Method */ size_t ANSI_X923_Padding::unpad(const byte block[], size_t size) const { size_t position = block[size-1]; if(position > size) throw Decoding_Error(name()); for(size_t j = size-position; j != size-1; ++j) if(block[j] != 0) throw Decoding_Error(name()); return (size-position); } /* * Query if the size is valid for this method */ bool ANSI_X923_Padding::valid_blocksize(size_t size) const { if(size > 0 && size < 256) return true; else return false; } /* * Pad with One and Zeros Method */ void OneAndZeros_Padding::pad(byte block[], size_t size, size_t) const { block[0] = 0x80; for(size_t j = 1; j != size; ++j) block[j] = 0x00; } /* * Unpad with One and Zeros Method */ size_t OneAndZeros_Padding::unpad(const byte block[], size_t size) const { while(size) { if(block[size-1] == 0x80) break; if(block[size-1] != 0x00) throw Decoding_Error(name()); size--; } if(!size) throw Decoding_Error(name()); return (size-1); } /* * Query if the size is valid for this method */ bool OneAndZeros_Padding::valid_blocksize(size_t size) const { return (size > 0); } } /* * XTS Mode * (C) 2009 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> #include <stdexcept> namespace Botan { namespace { void poly_double(byte tweak[], size_t size) { const byte polynomial = (size == 16) ? 0x87 : 0x1B; byte carry = 0; for(size_t i = 0; i != size; ++i) { byte carry2 = (tweak[i] >> 7); tweak[i] = (tweak[i] << 1) | carry; carry = carry2; } if(carry) tweak[0] ^= polynomial; } /* XTS needs to process at least 2 blocks in parallel because block_size+1 bytes are needed at the end */ size_t xts_parallelism(BlockCipher* cipher) { return std::max<size_t>(cipher->parallel_bytes(), 2 * cipher->block_size()); } } /* * XTS_Encryption constructor */ XTS_Encryption::XTS_Encryption(BlockCipher* ciph) : Buffered_Filter(xts_parallelism(ciph), ciph->block_size() + 1), cipher(ciph) { if(cipher->block_size() != 8 && cipher->block_size() != 16) throw std::invalid_argument("Bad cipher for XTS: " + cipher->name()); cipher2 = cipher->clone(); tweak.resize(buffered_block_size()); } /* * XTS_Encryption constructor */ XTS_Encryption::XTS_Encryption(BlockCipher* ciph, const SymmetricKey& key, const InitializationVector& iv) : Buffered_Filter(xts_parallelism(ciph), ciph->block_size() + 1), cipher(ciph) { if(cipher->block_size() != 8 && cipher->block_size() != 16) throw std::invalid_argument("Bad cipher for XTS: " + cipher->name()); cipher2 = cipher->clone(); tweak.resize(buffered_block_size()); set_key(key); set_iv(iv); } /* * Return the name */ std::string XTS_Encryption::name() const { return (cipher->name() + "/XTS"); } /* * Set new tweak */ void XTS_Encryption::set_iv(const InitializationVector& iv) { if(!valid_iv_length(iv.length())) throw Invalid_IV_Length(name(), iv.length()); const size_t blocks_in_tweak = tweak.size() / cipher->block_size(); tweak.copy(iv.begin(), iv.length()); cipher2->encrypt(tweak); for(size_t i = 1; i < blocks_in_tweak; ++i) { tweak.copy(i*cipher->block_size(), &tweak[(i-1)*cipher->block_size()], cipher->block_size()); poly_double(&tweak[i*cipher->block_size()], cipher->block_size()); } } void XTS_Encryption::set_key(const SymmetricKey& key) { size_t key_half = key.length() / 2; if(key.length() % 2 == 1 || !cipher->valid_keylength(key_half)) throw Invalid_Key_Length(name(), key.length()); cipher->set_key(key.begin(), key_half); cipher2->set_key(key.begin() + key_half, key_half); } /* * Encrypt in XTS mode */ void XTS_Encryption::write(const byte input[], size_t length) { Buffered_Filter::write(input, length); } /* * Finish encrypting in XTS mode */ void XTS_Encryption::end_msg() { Buffered_Filter::end_msg(); } void XTS_Encryption::buffered_block(const byte input[], size_t length) { const size_t blocks_in_tweak = tweak.size() / cipher->block_size(); size_t blocks = length / cipher->block_size(); SecureVector<byte> temp(tweak.size()); while(blocks) { size_t to_proc = std::min(blocks, blocks_in_tweak); size_t to_proc_bytes = to_proc * cipher->block_size(); xor_buf(temp, input, tweak, to_proc_bytes); cipher->encrypt_n(&temp[0], &temp[0], to_proc); xor_buf(temp, tweak, to_proc_bytes); send(temp, to_proc_bytes); tweak.copy(&tweak[(to_proc-1)*cipher->block_size()], cipher->block_size()); poly_double(&tweak[0], cipher->block_size()); for(size_t i = 1; i < blocks_in_tweak; ++i) { tweak.copy(i*cipher->block_size(), &tweak[(i-1)*cipher->block_size()], cipher->block_size()); poly_double(&tweak[i*cipher->block_size()], cipher->block_size()); } input += to_proc * cipher->block_size(); blocks -= to_proc; } } /* * Finish encrypting in XTS mode */ void XTS_Encryption::buffered_final(const byte input[], size_t length) { if(length <= cipher->block_size()) throw Encoding_Error("XTS_Encryption: insufficient data to encrypt"); if(length % cipher->block_size() == 0) { buffered_block(input, length); } else { // steal ciphertext size_t leftover_blocks = ((length / cipher->block_size()) - 1) * cipher->block_size(); buffered_block(input, leftover_blocks); input += leftover_blocks; length -= leftover_blocks; SecureVector<byte> temp(input, length); xor_buf(temp, tweak, cipher->block_size()); cipher->encrypt(temp); xor_buf(temp, tweak, cipher->block_size()); poly_double(&tweak[0], cipher->block_size()); for(size_t i = 0; i != length - cipher->block_size(); ++i) std::swap(temp[i], temp[i + cipher->block_size()]); xor_buf(temp, tweak, cipher->block_size()); cipher->encrypt(temp); xor_buf(temp, tweak, cipher->block_size()); send(temp, temp.size()); } buffer_reset(); } /* * XTS_Decryption constructor */ XTS_Decryption::XTS_Decryption(BlockCipher* ciph) : Buffered_Filter(xts_parallelism(ciph), ciph->block_size() + 1), cipher(ciph) { if(cipher->block_size() != 8 && cipher->block_size() != 16) throw std::invalid_argument("Bad cipher for XTS: " + cipher->name()); cipher2 = ciph->clone(); tweak.resize(buffered_block_size()); } /* * XTS_Decryption constructor */ XTS_Decryption::XTS_Decryption(BlockCipher* ciph, const SymmetricKey& key, const InitializationVector& iv) : Buffered_Filter(xts_parallelism(ciph), ciph->block_size() + 1), cipher(ciph) { if(cipher->block_size() != 8 && cipher->block_size() != 16) throw std::invalid_argument("Bad cipher for XTS: " + cipher->name()); cipher2 = ciph->clone(); tweak.resize(buffered_block_size()); set_key(key); set_iv(iv); } /* * Return the name */ std::string XTS_Decryption::name() const { return (cipher->name() + "/XTS"); } /* * Set new tweak */ void XTS_Decryption::set_iv(const InitializationVector& iv) { if(!valid_iv_length(iv.length())) throw Invalid_IV_Length(name(), iv.length()); const size_t blocks_in_tweak = tweak.size() / cipher->block_size(); tweak.copy(iv.begin(), iv.length()); cipher2->encrypt(tweak); for(size_t i = 1; i < blocks_in_tweak; ++i) { tweak.copy(i*cipher->block_size(), &tweak[(i-1)*cipher->block_size()], cipher->block_size()); poly_double(&tweak[i*cipher->block_size()], cipher->block_size()); } } void XTS_Decryption::set_key(const SymmetricKey& key) { size_t key_half = key.length() / 2; if(key.length() % 2 == 1 || !cipher->valid_keylength(key_half)) throw Invalid_Key_Length(name(), key.length()); cipher->set_key(key.begin(), key_half); cipher2->set_key(key.begin() + key_half, key_half); } /* * Decrypt in XTS mode */ void XTS_Decryption::write(const byte input[], size_t length) { Buffered_Filter::write(input, length); } /* * Finish decrypting in XTS mode */ void XTS_Decryption::end_msg() { Buffered_Filter::end_msg(); } void XTS_Decryption::buffered_block(const byte input[], size_t input_length) { const size_t blocks_in_tweak = tweak.size() / cipher->block_size(); size_t blocks = input_length / cipher->block_size(); SecureVector<byte> temp(tweak.size()); while(blocks) { size_t to_proc = std::min(blocks, blocks_in_tweak); size_t to_proc_bytes = to_proc * cipher->block_size(); xor_buf(temp, input, tweak, to_proc_bytes); cipher->decrypt_n(&temp[0], &temp[0], to_proc); xor_buf(temp, tweak, to_proc_bytes); send(temp, to_proc_bytes); tweak.copy(&tweak[(to_proc-1)*cipher->block_size()], cipher->block_size()); poly_double(&tweak[0], cipher->block_size()); for(size_t i = 1; i < blocks_in_tweak; ++i) { tweak.copy(i*cipher->block_size(), &tweak[(i-1)*cipher->block_size()], cipher->block_size()); poly_double(&tweak[i*cipher->block_size()], cipher->block_size()); } input += to_proc * cipher->block_size(); blocks -= to_proc; } } void XTS_Decryption::buffered_final(const byte input[], size_t length) { if(length <= cipher->block_size()) throw Decoding_Error("XTS_Decryption: insufficient data to decrypt"); if(length % cipher->block_size() == 0) { buffered_block(input, length); } else { size_t leftover_blocks = ((length / cipher->block_size()) - 1) * cipher->block_size(); buffered_block(input, leftover_blocks); input += leftover_blocks; length -= leftover_blocks; SecureVector<byte> temp(input, length); SecureVector<byte> tweak_copy(&tweak[0], cipher->block_size()); poly_double(&tweak_copy[0], cipher->block_size()); xor_buf(temp, tweak_copy, cipher->block_size()); cipher->decrypt(temp); xor_buf(temp, tweak_copy, cipher->block_size()); for(size_t i = 0; i != length - cipher->block_size(); ++i) std::swap(temp[i], temp[i + cipher->block_size()]); xor_buf(temp, tweak, cipher->block_size()); cipher->decrypt(temp); xor_buf(temp, tweak, cipher->block_size()); send(temp, length); } buffer_reset(); } } /* * Pipe Output Buffer * (C) 1999-2007,2011 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Read data from a message */ size_t Output_Buffers::read(byte output[], size_t length, Pipe::message_id msg) { SecureQueue* q = get(msg); if(q) return q->read(output, length); return 0; } /* * Peek at data in a message */ size_t Output_Buffers::peek(byte output[], size_t length, size_t stream_offset, Pipe::message_id msg) const { SecureQueue* q = get(msg); if(q) return q->peek(output, length, stream_offset); return 0; } /* * Check available bytes in a message */ size_t Output_Buffers::remaining(Pipe::message_id msg) const { SecureQueue* q = get(msg); if(q) return q->size(); return 0; } /* * Add a new output queue */ void Output_Buffers::add(SecureQueue* queue) { BOTAN_ASSERT(queue, "argument was NULL"); BOTAN_ASSERT(buffers.size() < buffers.max_size(), "No more room in container"); buffers.push_back(queue); } /* * Retire old output queues */ void Output_Buffers::retire() { for(size_t i = 0; i != buffers.size(); ++i) if(buffers[i] && buffers[i]->size() == 0) { delete buffers[i]; buffers[i] = 0; } while(buffers.size() && !buffers[0]) { buffers.pop_front(); offset = offset + Pipe::message_id(1); } } /* * Get a particular output queue */ SecureQueue* Output_Buffers::get(Pipe::message_id msg) const { if(msg < offset) return 0; BOTAN_ASSERT(msg < message_count(), "Message number out of range"); return buffers[msg-offset]; } /* * Return the total number of messages */ Pipe::message_id Output_Buffers::message_count() const { return (offset + buffers.size()); } /* * Output_Buffers Constructor */ Output_Buffers::Output_Buffers() { offset = 0; } /* * Output_Buffers Destructor */ Output_Buffers::~Output_Buffers() { for(size_t j = 0; j != buffers.size(); ++j) delete buffers[j]; } } /* * Pipe * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { /* * A Filter that does nothing */ class Null_Filter : public Filter { public: void write(const byte input[], size_t length) { send(input, length); } std::string name() const { return "Null"; } }; } /* * Pipe Constructor */ Pipe::Pipe(Filter* f1, Filter* f2, Filter* f3, Filter* f4) { init(); append(f1); append(f2); append(f3); append(f4); } /* * Pipe Constructor */ Pipe::Pipe(Filter* filter_array[], size_t count) { init(); for(size_t j = 0; j != count; ++j) append(filter_array[j]); } /* * Pipe Destructor */ Pipe::~Pipe() { destruct(pipe); delete outputs; } /* * Initialize the Pipe */ void Pipe::init() { outputs = new Output_Buffers; pipe = 0; default_read = 0; inside_msg = false; } /* * Reset the Pipe */ void Pipe::reset() { destruct(pipe); pipe = 0; inside_msg = false; } /* * Destroy the Pipe */ void Pipe::destruct(Filter* to_kill) { if(!to_kill || dynamic_cast<SecureQueue*>(to_kill)) return; for(size_t j = 0; j != to_kill->total_ports(); ++j) destruct(to_kill->next[j]); delete to_kill; } /* * Test if the Pipe has any data in it */ bool Pipe::end_of_data() const { return (remaining() == 0); } /* * Set the default read message */ void Pipe::set_default_msg(message_id msg) { if(msg >= message_count()) throw Invalid_Argument("Pipe::set_default_msg: msg number is too high"); default_read = msg; } /* * Process a full message at once */ void Pipe::process_msg(const byte input[], size_t length) { start_msg(); write(input, length); end_msg(); } /* * Process a full message at once */ void Pipe::process_msg(const MemoryRegion<byte>& input) { process_msg(&input[0], input.size()); } /* * Process a full message at once */ void Pipe::process_msg(const std::string& input) { process_msg(reinterpret_cast<const byte*>(input.data()), input.length()); } /* * Process a full message at once */ void Pipe::process_msg(DataSource& input) { start_msg(); write(input); end_msg(); } /* * Start a new message */ void Pipe::start_msg() { if(inside_msg) throw Invalid_State("Pipe::start_msg: Message was already started"); if(pipe == 0) pipe = new Null_Filter; find_endpoints(pipe); pipe->new_msg(); inside_msg = true; } /* * End the current message */ void Pipe::end_msg() { if(!inside_msg) throw Invalid_State("Pipe::end_msg: Message was already ended"); pipe->finish_msg(); clear_endpoints(pipe); if(dynamic_cast<Null_Filter*>(pipe)) { delete pipe; pipe = 0; } inside_msg = false; outputs->retire(); } /* * Find the endpoints of the Pipe */ void Pipe::find_endpoints(Filter* f) { for(size_t j = 0; j != f->total_ports(); ++j) if(f->next[j] && !dynamic_cast<SecureQueue*>(f->next[j])) find_endpoints(f->next[j]); else { SecureQueue* q = new SecureQueue; f->next[j] = q; outputs->add(q); } } /* * Remove the SecureQueues attached to the Filter */ void Pipe::clear_endpoints(Filter* f) { if(!f) return; for(size_t j = 0; j != f->total_ports(); ++j) { if(f->next[j] && dynamic_cast<SecureQueue*>(f->next[j])) f->next[j] = 0; clear_endpoints(f->next[j]); } } /* * Append a Filter to the Pipe */ void Pipe::append(Filter* filter) { if(inside_msg) throw Invalid_State("Cannot append to a Pipe while it is processing"); if(!filter) return; if(dynamic_cast<SecureQueue*>(filter)) throw Invalid_Argument("Pipe::append: SecureQueue cannot be used"); if(filter->owned) throw Invalid_Argument("Filters cannot be shared among multiple Pipes"); filter->owned = true; if(!pipe) pipe = filter; else pipe->attach(filter); } /* * Prepend a Filter to the Pipe */ void Pipe::prepend(Filter* filter) { if(inside_msg) throw Invalid_State("Cannot prepend to a Pipe while it is processing"); if(!filter) return; if(dynamic_cast<SecureQueue*>(filter)) throw Invalid_Argument("Pipe::prepend: SecureQueue cannot be used"); if(filter->owned) throw Invalid_Argument("Filters cannot be shared among multiple Pipes"); filter->owned = true; if(pipe) filter->attach(pipe); pipe = filter; } /* * Pop a Filter off the Pipe */ void Pipe::pop() { if(inside_msg) throw Invalid_State("Cannot pop off a Pipe while it is processing"); if(!pipe) return; if(pipe->total_ports() > 1) throw Invalid_State("Cannot pop off a Filter with multiple ports"); Filter* f = pipe; size_t owns = f->owns(); pipe = pipe->next[0]; delete f; while(owns--) { f = pipe; pipe = pipe->next[0]; delete f; } } /* * Return the number of messages in this Pipe */ Pipe::message_id Pipe::message_count() const { return outputs->message_count(); } /* * Static Member Variables */ const Pipe::message_id Pipe::LAST_MESSAGE = static_cast<Pipe::message_id>(-2); const Pipe::message_id Pipe::DEFAULT_MESSAGE = static_cast<Pipe::message_id>(-1); } /* * Pipe I/O * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <iostream> namespace Botan { /* * Write data from a pipe into an ostream */ std::ostream& operator<<(std::ostream& stream, Pipe& pipe) { SecureVector<byte> buffer(DEFAULT_BUFFERSIZE); while(stream.good() && pipe.remaining()) { size_t got = pipe.read(&buffer[0], buffer.size()); stream.write(reinterpret_cast<const char*>(&buffer[0]), got); } if(!stream.good()) throw Stream_IO_Error("Pipe output operator (iostream) has failed"); return stream; } /* * Read data from an istream into a pipe */ std::istream& operator>>(std::istream& stream, Pipe& pipe) { SecureVector<byte> buffer(DEFAULT_BUFFERSIZE); while(stream.good()) { stream.read(reinterpret_cast<char*>(&buffer[0]), buffer.size()); pipe.write(&buffer[0], stream.gcount()); } if(stream.bad() || (stream.fail() && !stream.eof())) throw Stream_IO_Error("Pipe input operator (iostream) has failed"); return stream; } } /* * Pipe Reading/Writing * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Look up the canonical ID for a queue */ Pipe::message_id Pipe::get_message_no(const std::string& func_name, message_id msg) const { if(msg == DEFAULT_MESSAGE) msg = default_msg(); else if(msg == LAST_MESSAGE) msg = message_count() - 1; if(msg >= message_count()) throw Invalid_Message_Number(func_name, msg); return msg; } /* * Write into a Pipe */ void Pipe::write(const byte input[], size_t length) { if(!inside_msg) throw Invalid_State("Cannot write to a Pipe while it is not processing"); pipe->write(input, length); } /* * Write into a Pipe */ void Pipe::write(const MemoryRegion<byte>& input) { write(&input[0], input.size()); } /* * Write a string into a Pipe */ void Pipe::write(const std::string& str) { write(reinterpret_cast<const byte*>(str.data()), str.size()); } /* * Write a single byte into a Pipe */ void Pipe::write(byte input) { write(&input, 1); } /* * Write the contents of a DataSource into a Pipe */ void Pipe::write(DataSource& source) { SecureVector<byte> buffer(DEFAULT_BUFFERSIZE); while(!source.end_of_data()) { size_t got = source.read(&buffer[0], buffer.size()); write(&buffer[0], got); } } /* * Read some data from the pipe */ size_t Pipe::read(byte output[], size_t length, message_id msg) { return outputs->read(output, length, get_message_no("read", msg)); } /* * Read some data from the pipe */ size_t Pipe::read(byte output[], size_t length) { return read(output, length, DEFAULT_MESSAGE); } /* * Read a single byte from the pipe */ size_t Pipe::read(byte& out, message_id msg) { return read(&out, 1, msg); } /* * Return all data in the pipe */ SecureVector<byte> Pipe::read_all(message_id msg) { msg = ((msg != DEFAULT_MESSAGE) ? msg : default_msg()); SecureVector<byte> buffer(remaining(msg)); size_t got = read(&buffer[0], buffer.size(), msg); buffer.resize(got); return buffer; } /* * Return all data in the pipe as a string */ std::string Pipe::read_all_as_string(message_id msg) { msg = ((msg != DEFAULT_MESSAGE) ? msg : default_msg()); SecureVector<byte> buffer(DEFAULT_BUFFERSIZE); std::string str; str.reserve(remaining(msg)); while(true) { size_t got = read(&buffer[0], buffer.size(), msg); if(got == 0) break; str.append(reinterpret_cast<const char*>(&buffer[0]), got); } return str; } /* * Find out how many bytes are ready to read */ size_t Pipe::remaining(message_id msg) const { return outputs->remaining(get_message_no("remaining", msg)); } /* * Peek at some data in the pipe */ size_t Pipe::peek(byte output[], size_t length, size_t offset, message_id msg) const { return outputs->peek(output, length, offset, get_message_no("peek", msg)); } /* * Peek at some data in the pipe */ size_t Pipe::peek(byte output[], size_t length, size_t offset) const { return peek(output, length, offset, DEFAULT_MESSAGE); } /* * Peek at a byte in the pipe */ size_t Pipe::peek(byte& out, size_t offset, message_id msg) const { return peek(&out, 1, offset, msg); } } /* * PK Filters * (C) 1999-2009 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Append to the buffer */ void PK_Encryptor_Filter::write(const byte input[], size_t length) { buffer += std::make_pair(input, length); } /* * Encrypt the message */ void PK_Encryptor_Filter::end_msg() { send(cipher->encrypt(buffer, rng)); buffer.clear(); } /* * Append to the buffer */ void PK_Decryptor_Filter::write(const byte input[], size_t length) { buffer += std::make_pair(input, length); } /* * Decrypt the message */ void PK_Decryptor_Filter::end_msg() { send(cipher->decrypt(buffer)); buffer.clear(); } /* * Add more data */ void PK_Signer_Filter::write(const byte input[], size_t length) { signer->update(input, length); } /* * Sign the message */ void PK_Signer_Filter::end_msg() { send(signer->signature(rng)); } /* * Add more data */ void PK_Verifier_Filter::write(const byte input[], size_t length) { verifier->update(input, length); } /* * Verify the message */ void PK_Verifier_Filter::end_msg() { if(signature.empty()) throw Invalid_State("PK_Verifier_Filter: No signature to check against"); bool is_valid = verifier->check_signature(signature); send((is_valid ? 1 : 0)); } /* * Set the signature to check */ void PK_Verifier_Filter::set_signature(const byte sig[], size_t length) { signature.resize(length); copy_mem(&signature[0], sig, length); } /* * Set the signature to check */ void PK_Verifier_Filter::set_signature(const MemoryRegion<byte>& sig) { signature = sig; } /* * PK_Verifier_Filter Constructor */ PK_Verifier_Filter::PK_Verifier_Filter(PK_Verifier* v, const byte sig[], size_t length) : verifier(v), signature(sig, length) { } /* * PK_Verifier_Filter Constructor */ PK_Verifier_Filter::PK_Verifier_Filter(PK_Verifier* v, const MemoryRegion<byte>& sig) : verifier(v), signature(sig) { } } /* * SecureQueue * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> namespace Botan { /** * A node in a SecureQueue */ class SecureQueueNode { public: SecureQueueNode() : buffer(DEFAULT_BUFFERSIZE) { next = 0; start = end = 0; } ~SecureQueueNode() { next = 0; start = end = 0; } size_t write(const byte input[], size_t length) { size_t copied = std::min<size_t>(length, buffer.size() - end); copy_mem(&buffer[end], input, copied); end += copied; return copied; } size_t read(byte output[], size_t length) { size_t copied = std::min(length, end - start); copy_mem(output, &buffer[start], copied); start += copied; return copied; } size_t peek(byte output[], size_t length, size_t offset = 0) { const size_t left = end - start; if(offset >= left) return 0; size_t copied = std::min(length, left - offset); copy_mem(output, &buffer[start + offset], copied); return copied; } size_t size() const { return (end - start); } private: friend class SecureQueue; SecureQueueNode* next; SecureVector<byte> buffer; size_t start, end; }; /* * Create a SecureQueue */ SecureQueue::SecureQueue() { set_next(0, 0); head = tail = new SecureQueueNode; } /* * Copy a SecureQueue */ SecureQueue::SecureQueue(const SecureQueue& input) : Fanout_Filter(), DataSource() { set_next(0, 0); head = tail = new SecureQueueNode; SecureQueueNode* temp = input.head; while(temp) { write(&temp->buffer[temp->start], temp->end - temp->start); temp = temp->next; } } /* * Destroy this SecureQueue */ void SecureQueue::destroy() { SecureQueueNode* temp = head; while(temp) { SecureQueueNode* holder = temp->next; delete temp; temp = holder; } head = tail = 0; } /* * Copy a SecureQueue */ SecureQueue& SecureQueue::operator=(const SecureQueue& input) { destroy(); head = tail = new SecureQueueNode; SecureQueueNode* temp = input.head; while(temp) { write(&temp->buffer[temp->start], temp->end - temp->start); temp = temp->next; } return (*this); } /* * Add some bytes to the queue */ void SecureQueue::write(const byte input[], size_t length) { if(!head) head = tail = new SecureQueueNode; while(length) { const size_t n = tail->write(input, length); input += n; length -= n; if(length) { tail->next = new SecureQueueNode; tail = tail->next; } } } /* * Read some bytes from the queue */ size_t SecureQueue::read(byte output[], size_t length) { size_t got = 0; while(length && head) { const size_t n = head->read(output, length); output += n; got += n; length -= n; if(head->size() == 0) { SecureQueueNode* holder = head->next; delete head; head = holder; } } return got; } /* * Read data, but do not remove it from queue */ size_t SecureQueue::peek(byte output[], size_t length, size_t offset) const { SecureQueueNode* current = head; while(offset && current) { if(offset >= current->size()) { offset -= current->size(); current = current->next; } else break; } size_t got = 0; while(length && current) { const size_t n = current->peek(output, length, offset); offset = 0; output += n; got += n; length -= n; current = current->next; } return got; } /* * Return how many bytes the queue holds */ size_t SecureQueue::size() const { SecureQueueNode* current = head; size_t count = 0; while(current) { count += current->size(); current = current->next; } return count; } /* * Test if the queue has any data in it */ bool SecureQueue::end_of_data() const { return (size() == 0); } } /* * Blue Midnight Wish 512 (Round 2 tweaked) * (C) 2009 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { inline u64bit S0(u64bit X) { return (X >> 1) ^ (X << 3) ^ rotate_left(X, 4) ^ rotate_left(X, 37); } inline u64bit S1(u64bit X) { return (X >> 1) ^ (X << 2) ^ rotate_left(X, 13) ^ rotate_left(X, 43); } inline u64bit S2(u64bit X) { return (X >> 2) ^ (X << 1) ^ rotate_left(X, 19) ^ rotate_left(X, 53); } inline u64bit S3(u64bit X) { return (X >> 2) ^ (X << 2) ^ rotate_left(X, 28) ^ rotate_left(X, 59); } inline u64bit S4(u64bit X) { return (X >> 1) ^ X; } /** * Blue Midnight Wish 512 compression function */ void BMW_512_compress(u64bit H[16], const u64bit M[16], u64bit Q[32]) { const size_t EXPAND_1_ROUNDS = 2; for(size_t i = 0; i != 16; ++i) Q[i] = H[i] ^ M[i]; Q[16] = Q[ 5] - Q[ 7] + Q[10] + Q[13] + Q[14]; Q[17] = Q[ 6] - Q[ 8] + Q[11] + Q[14] - Q[15]; Q[18] = Q[ 0] + Q[ 7] + Q[ 9] - Q[12] + Q[15]; Q[19] = Q[ 0] - Q[ 1] + Q[ 8] - Q[10] + Q[13]; Q[20] = Q[ 1] + Q[ 2] + Q[ 9] - Q[11] - Q[14]; Q[21] = Q[ 3] - Q[ 2] + Q[10] - Q[12] + Q[15]; Q[22] = Q[ 4] - Q[ 0] - Q[ 3] - Q[11] + Q[13]; Q[23] = Q[ 1] - Q[ 4] - Q[ 5] - Q[12] - Q[14]; Q[24] = Q[ 2] - Q[ 5] - Q[ 6] + Q[13] - Q[15]; Q[25] = Q[ 0] - Q[ 3] + Q[ 6] - Q[ 7] + Q[14]; Q[26] = Q[ 8] - Q[ 1] - Q[ 4] - Q[ 7] + Q[15]; Q[27] = Q[ 8] - Q[ 0] - Q[ 2] - Q[ 5] + Q[ 9]; Q[28] = Q[ 1] + Q[ 3] - Q[ 6] - Q[ 9] + Q[10]; Q[29] = Q[ 2] + Q[ 4] + Q[ 7] + Q[10] + Q[11]; Q[30] = Q[ 3] - Q[ 5] + Q[ 8] - Q[11] - Q[12]; Q[31] = Q[12] - Q[ 4] - Q[ 6] - Q[ 9] + Q[13]; Q[ 0] = S0(Q[16]) + H[ 1]; Q[ 1] = S1(Q[17]) + H[ 2]; Q[ 2] = S2(Q[18]) + H[ 3]; Q[ 3] = S3(Q[19]) + H[ 4]; Q[ 4] = S4(Q[20]) + H[ 5]; Q[ 5] = S0(Q[21]) + H[ 6]; Q[ 6] = S1(Q[22]) + H[ 7]; Q[ 7] = S2(Q[23]) + H[ 8]; Q[ 8] = S3(Q[24]) + H[ 9]; Q[ 9] = S4(Q[25]) + H[10]; Q[10] = S0(Q[26]) + H[11]; Q[11] = S1(Q[27]) + H[12]; Q[12] = S2(Q[28]) + H[13]; Q[13] = S3(Q[29]) + H[14]; Q[14] = S4(Q[30]) + H[15]; Q[15] = S0(Q[31]) + H[ 0]; for(size_t i = 16; i != 16 + EXPAND_1_ROUNDS; ++i) { Q[i] = S1(Q[i-16]) + S2(Q[i-15]) + S3(Q[i-14]) + S0(Q[i-13]) + S1(Q[i-12]) + S2(Q[i-11]) + S3(Q[i-10]) + S0(Q[i- 9]) + S1(Q[i- 8]) + S2(Q[i- 7]) + S3(Q[i- 6]) + S0(Q[i- 5]) + S1(Q[i- 4]) + S2(Q[i- 3]) + S3(Q[i- 2]) + S0(Q[i- 1]) + ((rotate_left(M[(i-16) % 16], ((i-16)%16) + 1) + rotate_left(M[(i-13) % 16], ((i-13)%16) + 1) - rotate_left(M[(i- 6) % 16], ((i-6)%16) + 1) + (0x0555555555555555 * i)) ^ H[(i-16+7)%16]); } for(size_t i = 16 + EXPAND_1_ROUNDS; i != 32; ++i) { Q[i] = Q[i-16] + rotate_left(Q[i-15], 5) + Q[i-14] + rotate_left(Q[i-13], 11) + Q[i-12] + rotate_left(Q[i-11], 27) + Q[i-10] + rotate_left(Q[i- 9], 32) + Q[i- 8] + rotate_left(Q[i- 7], 37) + Q[i- 6] + rotate_left(Q[i- 5], 43) + Q[i- 4] + rotate_left(Q[i- 3], 53) + S4(Q[i - 2]) + ((Q[i-1] >> 2) ^ Q[i-1]) + ((rotate_left(M[(i-16) % 16], ((i-16)%16 + 1)) + rotate_left(M[(i-13) % 16], ((i-13)%16 + 1)) - rotate_left(M[(i- 6) % 16], ((i-6)%16 + 1)) + (0x0555555555555555 * i)) ^ H[(i-16+7)%16]); } u64bit XL = Q[16] ^ Q[17] ^ Q[18] ^ Q[19] ^ Q[20] ^ Q[21] ^ Q[22] ^ Q[23]; u64bit XH = Q[24] ^ Q[25] ^ Q[26] ^ Q[27] ^ Q[28] ^ Q[29] ^ Q[30] ^ Q[31]; XH ^= XL; H[ 0] = ((XH << 5) ^ (Q[16] >> 5) ^ M[0]) + (XL ^ Q[24] ^ Q[0]); H[ 1] = ((XH >> 7) ^ (Q[17] << 8) ^ M[1]) + (XL ^ Q[25] ^ Q[1]); H[ 2] = ((XH >> 5) ^ (Q[18] << 5) ^ M[2]) + (XL ^ Q[26] ^ Q[2]); H[ 3] = ((XH >> 1) ^ (Q[19] << 5) ^ M[3]) + (XL ^ Q[27] ^ Q[3]); H[ 4] = ((XH >> 3) ^ (Q[20] ) ^ M[4]) + (XL ^ Q[28] ^ Q[4]); H[ 5] = ((XH << 6) ^ (Q[21] >> 6) ^ M[5]) + (XL ^ Q[29] ^ Q[5]); H[ 6] = ((XH >> 4) ^ (Q[22] << 6) ^ M[6]) + (XL ^ Q[30] ^ Q[6]); H[ 7] = ((XH >> 11) ^ (Q[23] << 2) ^ M[7]) + (XL ^ Q[31] ^ Q[7]); H[ 8] = rotate_left(H[4], 9) + (XH ^ Q[24] ^ M[ 8]) + ((XL << 8) ^ Q[23] ^ Q[ 8]); H[ 9] = rotate_left(H[5], 10) + (XH ^ Q[25] ^ M[ 9]) + ((XL >> 6) ^ Q[16] ^ Q[ 9]); H[10] = rotate_left(H[6], 11) + (XH ^ Q[26] ^ M[10]) + ((XL << 6) ^ Q[17] ^ Q[10]); H[11] = rotate_left(H[7], 12) + (XH ^ Q[27] ^ M[11]) + ((XL << 4) ^ Q[18] ^ Q[11]); H[12] = rotate_left(H[0], 13) + (XH ^ Q[28] ^ M[12]) + ((XL >> 3) ^ Q[19] ^ Q[12]); H[13] = rotate_left(H[1], 14) + (XH ^ Q[29] ^ M[13]) + ((XL >> 4) ^ Q[20] ^ Q[13]); H[14] = rotate_left(H[2], 15) + (XH ^ Q[30] ^ M[14]) + ((XL >> 7) ^ Q[21] ^ Q[14]); H[15] = rotate_left(H[3], 16) + (XH ^ Q[31] ^ M[15]) + ((XL >> 2) ^ Q[22] ^ Q[15]); } } void BMW_512::compress_n(const byte input[], size_t blocks) { for(size_t i = 0; i != blocks; ++i) { load_le(&M[0], input, M.size()); BMW_512_compress(&H[0], &M[0], &Q[0]); input += hash_block_size(); } } /* * Copy out the digest */ void BMW_512::copy_out(byte output[]) { u64bit final[16] = { 0xAAAAAAAAAAAAAAA0, 0xAAAAAAAAAAAAAAA1, 0xAAAAAAAAAAAAAAA2, 0xAAAAAAAAAAAAAAA3, 0xAAAAAAAAAAAAAAA4, 0xAAAAAAAAAAAAAAA5, 0xAAAAAAAAAAAAAAA6, 0xAAAAAAAAAAAAAAA7, 0xAAAAAAAAAAAAAAA8, 0xAAAAAAAAAAAAAAA9, 0xAAAAAAAAAAAAAAAA, 0xAAAAAAAAAAAAAAAB, 0xAAAAAAAAAAAAAAAC, 0xAAAAAAAAAAAAAAAD, 0xAAAAAAAAAAAAAAAE, 0xAAAAAAAAAAAAAAAF }; BMW_512_compress(final, &H[0], &Q[0]); for(size_t i = 0; i != output_length(); i += 8) store_le(final[8 + i/8], output + i); } /* * Clear memory of sensitive data */ void BMW_512::clear() { MDx_HashFunction::clear(); zeroise(M); zeroise(Q); H[ 0] = 0x8081828384858687; H[ 1] = 0x88898A8B8C8D8E8F; H[ 2] = 0x9091929394959697; H[ 3] = 0x98999A9B9C9D9E9F; H[ 4] = 0xA0A1A2A3A4A5A6A7; H[ 5] = 0xA8A9AAABACADAEAF; H[ 6] = 0xB0B1B2B3B4B5B6B7; H[ 7] = 0xB8B9BABBBCBDBEBF; H[ 8] = 0xC0C1C2C3C4C5C6C7; H[ 9] = 0xC8C9CACBCCCDCECF; H[10] = 0xD0D1D2D3D4D5D6D7; H[11] = 0xD8D9DADBDCDDDEDF; H[12] = 0xE0E1E2E3E4E5E6E7; H[13] = 0xE8E9EAEBECEDEEEF; H[14] = 0xF0F1F2F3F4F5F6F7; H[15] = 0xF8F9FAFBFCFDFEFF; } } /* * Comb4P hash combiner * (C) 2010 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <stdexcept> namespace Botan { namespace { void comb4p_round(MemoryRegion<byte>& out, const MemoryRegion<byte>& in, byte round_no, HashFunction* h1, HashFunction* h2) { h1->update(round_no); h2->update(round_no); h1->update(&in[0], in.size()); h2->update(&in[0], in.size()); SecureVector<byte> h_buf = h1->final(); xor_buf(&out[0], &h_buf[0], std::min(out.size(), h_buf.size())); h_buf = h2->final(); xor_buf(&out[0], &h_buf[0], std::min(out.size(), h_buf.size())); } } Comb4P::Comb4P(HashFunction* h1, HashFunction* h2) : hash1(h1), hash2(h2) { if(hash1->name() == hash2->name()) throw std::invalid_argument("Comb4P: Must use two distinct hashes"); if(hash1->output_length() != hash2->output_length()) throw std::invalid_argument("Comb4P: Incompatible hashes " + hash1->name() + " and " + hash2->name()); clear(); } size_t Comb4P::hash_block_size() const { if(hash1->hash_block_size() == hash2->hash_block_size()) return hash1->hash_block_size(); /* * Return LCM of the block sizes? This would probably be OK for * HMAC, which is the main thing relying on knowing the block size. */ return 0; } void Comb4P::clear() { hash1->clear(); hash2->clear(); // Prep for processing next message, if any hash1->update(0); hash2->update(0); } void Comb4P::add_data(const byte input[], size_t length) { hash1->update(input, length); hash2->update(input, length); } void Comb4P::final_result(byte out[]) { SecureVector<byte> h1 = hash1->final(); SecureVector<byte> h2 = hash2->final(); // First round xor_buf(&h1[0], &h2[0], std::min(h1.size(), h2.size())); // Second round comb4p_round(h2, h1, 1, hash1, hash2); // Third round comb4p_round(h1, h2, 2, hash1, hash2); copy_mem(out , &h1[0], h1.size()); copy_mem(out + h1.size(), &h2[0], h2.size()); // Prep for processing next message, if any hash1->update(0); hash2->update(0); } } /* * GOST 34.11 * (C) 2009 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /** * GOST 34.11 Constructor */ GOST_34_11::GOST_34_11() : cipher(GOST_28147_89_Params("R3411_CryptoPro")), buffer(32), sum(32), hash(32) { count = 0; position = 0; } void GOST_34_11::clear() { cipher.clear(); zeroise(sum); zeroise(hash); count = 0; position = 0; } /** * Hash additional inputs */ void GOST_34_11::add_data(const byte input[], size_t length) { count += length; if(position) { buffer.copy(position, input, length); if(position + length >= hash_block_size()) { compress_n(&buffer[0], 1); input += (hash_block_size() - position); length -= (hash_block_size() - position); position = 0; } } const size_t full_blocks = length / hash_block_size(); const size_t remaining = length % hash_block_size(); if(full_blocks) compress_n(input, full_blocks); buffer.copy(position, input + full_blocks * hash_block_size(), remaining); position += remaining; } /** * The GOST 34.11 compression function */ void GOST_34_11::compress_n(const byte input[], size_t blocks) { for(size_t i = 0; i != blocks; ++i) { for(u16bit j = 0, carry = 0; j != 32; ++j) { u16bit s = sum[j] + input[32*i+j] + carry; carry = get_byte(0, s); sum[j] = get_byte(1, s); } byte S[32] = { 0 }; u64bit U[4], V[4]; load_be(U, &hash[0], 4); load_be(V, input + 32*i, 4); for(size_t j = 0; j != 4; ++j) { byte key[32] = { 0 }; // P transformation for(size_t k = 0; k != 4; ++k) for(size_t l = 0; l != 8; ++l) key[4*l+k] = get_byte(l, U[k]) ^ get_byte(l, V[k]); cipher.set_key(key, 32); cipher.encrypt(&hash[8*j], S + 8*j); if(j == 3) break; // A(x) u64bit A_U = U[0]; U[0] = U[1]; U[1] = U[2]; U[2] = U[3]; U[3] = U[0] ^ A_U; if(j == 1) // C_3 { U[0] ^= 0x00FF00FF00FF00FF; U[1] ^= 0xFF00FF00FF00FF00; U[2] ^= 0x00FFFF00FF0000FF; U[3] ^= 0xFF000000FFFF00FF; } // A(A(x)) u64bit AA_V_1 = V[0] ^ V[1]; u64bit AA_V_2 = V[1] ^ V[2]; V[0] = V[2]; V[1] = V[3]; V[2] = AA_V_1; V[3] = AA_V_2; } byte S2[32] = { 0 }; // 12 rounds of psi S2[ 0] = S[24]; S2[ 1] = S[25]; S2[ 2] = S[26]; S2[ 3] = S[27]; S2[ 4] = S[28]; S2[ 5] = S[29]; S2[ 6] = S[30]; S2[ 7] = S[31]; S2[ 8] = S[ 0] ^ S[ 2] ^ S[ 4] ^ S[ 6] ^ S[24] ^ S[30]; S2[ 9] = S[ 1] ^ S[ 3] ^ S[ 5] ^ S[ 7] ^ S[25] ^ S[31]; S2[10] = S[ 0] ^ S[ 8] ^ S[24] ^ S[26] ^ S[30]; S2[11] = S[ 1] ^ S[ 9] ^ S[25] ^ S[27] ^ S[31]; S2[12] = S[ 0] ^ S[ 4] ^ S[ 6] ^ S[10] ^ S[24] ^ S[26] ^ S[28] ^ S[30]; S2[13] = S[ 1] ^ S[ 5] ^ S[ 7] ^ S[11] ^ S[25] ^ S[27] ^ S[29] ^ S[31]; S2[14] = S[ 0] ^ S[ 4] ^ S[ 8] ^ S[12] ^ S[24] ^ S[26] ^ S[28]; S2[15] = S[ 1] ^ S[ 5] ^ S[ 9] ^ S[13] ^ S[25] ^ S[27] ^ S[29]; S2[16] = S[ 2] ^ S[ 6] ^ S[10] ^ S[14] ^ S[26] ^ S[28] ^ S[30]; S2[17] = S[ 3] ^ S[ 7] ^ S[11] ^ S[15] ^ S[27] ^ S[29] ^ S[31]; S2[18] = S[ 0] ^ S[ 2] ^ S[ 6] ^ S[ 8] ^ S[12] ^ S[16] ^ S[24] ^ S[28]; S2[19] = S[ 1] ^ S[ 3] ^ S[ 7] ^ S[ 9] ^ S[13] ^ S[17] ^ S[25] ^ S[29]; S2[20] = S[ 2] ^ S[ 4] ^ S[ 8] ^ S[10] ^ S[14] ^ S[18] ^ S[26] ^ S[30]; S2[21] = S[ 3] ^ S[ 5] ^ S[ 9] ^ S[11] ^ S[15] ^ S[19] ^ S[27] ^ S[31]; S2[22] = S[ 0] ^ S[ 2] ^ S[10] ^ S[12] ^ S[16] ^ S[20] ^ S[24] ^ S[28] ^ S[30]; S2[23] = S[ 1] ^ S[ 3] ^ S[11] ^ S[13] ^ S[17] ^ S[21] ^ S[25] ^ S[29] ^ S[31]; S2[24] = S[ 0] ^ S[ 6] ^ S[12] ^ S[14] ^ S[18] ^ S[22] ^ S[24] ^ S[26]; S2[25] = S[ 1] ^ S[ 7] ^ S[13] ^ S[15] ^ S[19] ^ S[23] ^ S[25] ^ S[27]; S2[26] = S[ 2] ^ S[ 8] ^ S[14] ^ S[16] ^ S[20] ^ S[24] ^ S[26] ^ S[28]; S2[27] = S[ 3] ^ S[ 9] ^ S[15] ^ S[17] ^ S[21] ^ S[25] ^ S[27] ^ S[29]; S2[28] = S[ 4] ^ S[10] ^ S[16] ^ S[18] ^ S[22] ^ S[26] ^ S[28] ^ S[30]; S2[29] = S[ 5] ^ S[11] ^ S[17] ^ S[19] ^ S[23] ^ S[27] ^ S[29] ^ S[31]; S2[30] = S[ 0] ^ S[ 2] ^ S[ 4] ^ S[12] ^ S[18] ^ S[20] ^ S[28]; S2[31] = S[ 1] ^ S[ 3] ^ S[ 5] ^ S[13] ^ S[19] ^ S[21] ^ S[29]; xor_buf(S, S2, input + 32*i, 32); S2[0] = S[0] ^ S[2] ^ S[4] ^ S[6] ^ S[24] ^ S[30]; S2[1] = S[1] ^ S[3] ^ S[5] ^ S[7] ^ S[25] ^ S[31]; copy_mem(S, S+2, 30); S[30] = S2[0]; S[31] = S2[1]; xor_buf(S, &hash[0], 32); // 61 rounds of psi S2[ 0] = S[ 2] ^ S[ 6] ^ S[14] ^ S[20] ^ S[22] ^ S[26] ^ S[28] ^ S[30]; S2[ 1] = S[ 3] ^ S[ 7] ^ S[15] ^ S[21] ^ S[23] ^ S[27] ^ S[29] ^ S[31]; S2[ 2] = S[ 0] ^ S[ 2] ^ S[ 6] ^ S[ 8] ^ S[16] ^ S[22] ^ S[28]; S2[ 3] = S[ 1] ^ S[ 3] ^ S[ 7] ^ S[ 9] ^ S[17] ^ S[23] ^ S[29]; S2[ 4] = S[ 2] ^ S[ 4] ^ S[ 8] ^ S[10] ^ S[18] ^ S[24] ^ S[30]; S2[ 5] = S[ 3] ^ S[ 5] ^ S[ 9] ^ S[11] ^ S[19] ^ S[25] ^ S[31]; S2[ 6] = S[ 0] ^ S[ 2] ^ S[10] ^ S[12] ^ S[20] ^ S[24] ^ S[26] ^ S[30]; S2[ 7] = S[ 1] ^ S[ 3] ^ S[11] ^ S[13] ^ S[21] ^ S[25] ^ S[27] ^ S[31]; S2[ 8] = S[ 0] ^ S[ 6] ^ S[12] ^ S[14] ^ S[22] ^ S[24] ^ S[26] ^ S[28] ^ S[30]; S2[ 9] = S[ 1] ^ S[ 7] ^ S[13] ^ S[15] ^ S[23] ^ S[25] ^ S[27] ^ S[29] ^ S[31]; S2[10] = S[ 0] ^ S[ 4] ^ S[ 6] ^ S[ 8] ^ S[14] ^ S[16] ^ S[26] ^ S[28]; S2[11] = S[ 1] ^ S[ 5] ^ S[ 7] ^ S[ 9] ^ S[15] ^ S[17] ^ S[27] ^ S[29]; S2[12] = S[ 2] ^ S[ 6] ^ S[ 8] ^ S[10] ^ S[16] ^ S[18] ^ S[28] ^ S[30]; S2[13] = S[ 3] ^ S[ 7] ^ S[ 9] ^ S[11] ^ S[17] ^ S[19] ^ S[29] ^ S[31]; S2[14] = S[ 0] ^ S[ 2] ^ S[ 6] ^ S[ 8] ^ S[10] ^ S[12] ^ S[18] ^ S[20] ^ S[24]; S2[15] = S[ 1] ^ S[ 3] ^ S[ 7] ^ S[ 9] ^ S[11] ^ S[13] ^ S[19] ^ S[21] ^ S[25]; S2[16] = S[ 2] ^ S[ 4] ^ S[ 8] ^ S[10] ^ S[12] ^ S[14] ^ S[20] ^ S[22] ^ S[26]; S2[17] = S[ 3] ^ S[ 5] ^ S[ 9] ^ S[11] ^ S[13] ^ S[15] ^ S[21] ^ S[23] ^ S[27]; S2[18] = S[ 4] ^ S[ 6] ^ S[10] ^ S[12] ^ S[14] ^ S[16] ^ S[22] ^ S[24] ^ S[28]; S2[19] = S[ 5] ^ S[ 7] ^ S[11] ^ S[13] ^ S[15] ^ S[17] ^ S[23] ^ S[25] ^ S[29]; S2[20] = S[ 6] ^ S[ 8] ^ S[12] ^ S[14] ^ S[16] ^ S[18] ^ S[24] ^ S[26] ^ S[30]; S2[21] = S[ 7] ^ S[ 9] ^ S[13] ^ S[15] ^ S[17] ^ S[19] ^ S[25] ^ S[27] ^ S[31]; S2[22] = S[ 0] ^ S[ 2] ^ S[ 4] ^ S[ 6] ^ S[ 8] ^ S[10] ^ S[14] ^ S[16] ^ S[18] ^ S[20] ^ S[24] ^ S[26] ^ S[28] ^ S[30]; S2[23] = S[ 1] ^ S[ 3] ^ S[ 5] ^ S[ 7] ^ S[ 9] ^ S[11] ^ S[15] ^ S[17] ^ S[19] ^ S[21] ^ S[25] ^ S[27] ^ S[29] ^ S[31]; S2[24] = S[ 0] ^ S[ 8] ^ S[10] ^ S[12] ^ S[16] ^ S[18] ^ S[20] ^ S[22] ^ S[24] ^ S[26] ^ S[28]; S2[25] = S[ 1] ^ S[ 9] ^ S[11] ^ S[13] ^ S[17] ^ S[19] ^ S[21] ^ S[23] ^ S[25] ^ S[27] ^ S[29]; S2[26] = S[ 2] ^ S[10] ^ S[12] ^ S[14] ^ S[18] ^ S[20] ^ S[22] ^ S[24] ^ S[26] ^ S[28] ^ S[30]; S2[27] = S[ 3] ^ S[11] ^ S[13] ^ S[15] ^ S[19] ^ S[21] ^ S[23] ^ S[25] ^ S[27] ^ S[29] ^ S[31]; S2[28] = S[ 0] ^ S[ 2] ^ S[ 6] ^ S[12] ^ S[14] ^ S[16] ^ S[20] ^ S[22] ^ S[26] ^ S[28]; S2[29] = S[ 1] ^ S[ 3] ^ S[ 7] ^ S[13] ^ S[15] ^ S[17] ^ S[21] ^ S[23] ^ S[27] ^ S[29]; S2[30] = S[ 2] ^ S[ 4] ^ S[ 8] ^ S[14] ^ S[16] ^ S[18] ^ S[22] ^ S[24] ^ S[28] ^ S[30]; S2[31] = S[ 3] ^ S[ 5] ^ S[ 9] ^ S[15] ^ S[17] ^ S[19] ^ S[23] ^ S[25] ^ S[29] ^ S[31]; hash.copy(S2, 32); } } /** * Produce the final GOST 34.11 output */ void GOST_34_11::final_result(byte out[]) { if(position) { clear_mem(&buffer[0] + position, buffer.size() - position); compress_n(&buffer[0], 1); } SecureVector<byte> length_buf(32); const u64bit bit_count = count * 8; store_le(bit_count, &length_buf[0]); SecureVector<byte> sum_buf = sum; compress_n(&length_buf[0], 1); compress_n(&sum_buf[0], 1); copy_mem(out, &hash[0], 32); clear(); } } /* * HAS-160 * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace HAS_160_F { /* * HAS-160 F1 Function */ inline void F1(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, u32bit msg, u32bit rot) { E += rotate_left(A, rot) + (D ^ (B & (C ^ D))) + msg; B = rotate_left(B, 10); } /* * HAS-160 F2 Function */ inline void F2(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, u32bit msg, u32bit rot) { E += rotate_left(A, rot) + (B ^ C ^ D) + msg + 0x5A827999; B = rotate_left(B, 17); } /* * HAS-160 F3 Function */ inline void F3(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, u32bit msg, u32bit rot) { E += rotate_left(A, rot) + (C ^ (B | ~D)) + msg + 0x6ED9EBA1; B = rotate_left(B, 25); } /* * HAS-160 F4 Function */ inline void F4(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, u32bit msg, u32bit rot) { E += rotate_left(A, rot) + (B ^ C ^ D) + msg + 0x8F1BBCDC; B = rotate_left(B, 30); } } /* * HAS-160 Compression Function */ void HAS_160::compress_n(const byte input[], size_t blocks) { using namespace HAS_160_F; u32bit A = digest[0], B = digest[1], C = digest[2], D = digest[3], E = digest[4]; for(size_t i = 0; i != blocks; ++i) { load_le(&X[0], input, 16); X[16] = X[ 0] ^ X[ 1] ^ X[ 2] ^ X[ 3]; X[17] = X[ 4] ^ X[ 5] ^ X[ 6] ^ X[ 7]; X[18] = X[ 8] ^ X[ 9] ^ X[10] ^ X[11]; X[19] = X[12] ^ X[13] ^ X[14] ^ X[15]; F1(A,B,C,D,E,X[18], 5); F1(E,A,B,C,D,X[ 0],11); F1(D,E,A,B,C,X[ 1], 7); F1(C,D,E,A,B,X[ 2],15); F1(B,C,D,E,A,X[ 3], 6); F1(A,B,C,D,E,X[19],13); F1(E,A,B,C,D,X[ 4], 8); F1(D,E,A,B,C,X[ 5],14); F1(C,D,E,A,B,X[ 6], 7); F1(B,C,D,E,A,X[ 7],12); F1(A,B,C,D,E,X[16], 9); F1(E,A,B,C,D,X[ 8],11); F1(D,E,A,B,C,X[ 9], 8); F1(C,D,E,A,B,X[10],15); F1(B,C,D,E,A,X[11], 6); F1(A,B,C,D,E,X[17],12); F1(E,A,B,C,D,X[12], 9); F1(D,E,A,B,C,X[13],14); F1(C,D,E,A,B,X[14], 5); F1(B,C,D,E,A,X[15],13); X[16] = X[ 3] ^ X[ 6] ^ X[ 9] ^ X[12]; X[17] = X[ 2] ^ X[ 5] ^ X[ 8] ^ X[15]; X[18] = X[ 1] ^ X[ 4] ^ X[11] ^ X[14]; X[19] = X[ 0] ^ X[ 7] ^ X[10] ^ X[13]; F2(A,B,C,D,E,X[18], 5); F2(E,A,B,C,D,X[ 3],11); F2(D,E,A,B,C,X[ 6], 7); F2(C,D,E,A,B,X[ 9],15); F2(B,C,D,E,A,X[12], 6); F2(A,B,C,D,E,X[19],13); F2(E,A,B,C,D,X[15], 8); F2(D,E,A,B,C,X[ 2],14); F2(C,D,E,A,B,X[ 5], 7); F2(B,C,D,E,A,X[ 8],12); F2(A,B,C,D,E,X[16], 9); F2(E,A,B,C,D,X[11],11); F2(D,E,A,B,C,X[14], 8); F2(C,D,E,A,B,X[ 1],15); F2(B,C,D,E,A,X[ 4], 6); F2(A,B,C,D,E,X[17],12); F2(E,A,B,C,D,X[ 7], 9); F2(D,E,A,B,C,X[10],14); F2(C,D,E,A,B,X[13], 5); F2(B,C,D,E,A,X[ 0],13); X[16] = X[ 5] ^ X[ 7] ^ X[12] ^ X[14]; X[17] = X[ 0] ^ X[ 2] ^ X[ 9] ^ X[11]; X[18] = X[ 4] ^ X[ 6] ^ X[13] ^ X[15]; X[19] = X[ 1] ^ X[ 3] ^ X[ 8] ^ X[10]; F3(A,B,C,D,E,X[18], 5); F3(E,A,B,C,D,X[12],11); F3(D,E,A,B,C,X[ 5], 7); F3(C,D,E,A,B,X[14],15); F3(B,C,D,E,A,X[ 7], 6); F3(A,B,C,D,E,X[19],13); F3(E,A,B,C,D,X[ 0], 8); F3(D,E,A,B,C,X[ 9],14); F3(C,D,E,A,B,X[ 2], 7); F3(B,C,D,E,A,X[11],12); F3(A,B,C,D,E,X[16], 9); F3(E,A,B,C,D,X[ 4],11); F3(D,E,A,B,C,X[13], 8); F3(C,D,E,A,B,X[ 6],15); F3(B,C,D,E,A,X[15], 6); F3(A,B,C,D,E,X[17],12); F3(E,A,B,C,D,X[ 8], 9); F3(D,E,A,B,C,X[ 1],14); F3(C,D,E,A,B,X[10], 5); F3(B,C,D,E,A,X[ 3],13); X[16] = X[ 2] ^ X[ 7] ^ X[ 8] ^ X[13]; X[17] = X[ 3] ^ X[ 4] ^ X[ 9] ^ X[14]; X[18] = X[ 0] ^ X[ 5] ^ X[10] ^ X[15]; X[19] = X[ 1] ^ X[ 6] ^ X[11] ^ X[12]; F4(A,B,C,D,E,X[18], 5); F4(E,A,B,C,D,X[ 7],11); F4(D,E,A,B,C,X[ 2], 7); F4(C,D,E,A,B,X[13],15); F4(B,C,D,E,A,X[ 8], 6); F4(A,B,C,D,E,X[19],13); F4(E,A,B,C,D,X[ 3], 8); F4(D,E,A,B,C,X[14],14); F4(C,D,E,A,B,X[ 9], 7); F4(B,C,D,E,A,X[ 4],12); F4(A,B,C,D,E,X[16], 9); F4(E,A,B,C,D,X[15],11); F4(D,E,A,B,C,X[10], 8); F4(C,D,E,A,B,X[ 5],15); F4(B,C,D,E,A,X[ 0], 6); F4(A,B,C,D,E,X[17],12); F4(E,A,B,C,D,X[11], 9); F4(D,E,A,B,C,X[ 6],14); F4(C,D,E,A,B,X[ 1], 5); F4(B,C,D,E,A,X[12],13); A = (digest[0] += A); B = (digest[1] += B); C = (digest[2] += C); D = (digest[3] += D); E = (digest[4] += E); input += hash_block_size(); } } /* * Copy out the digest */ void HAS_160::copy_out(byte output[]) { for(size_t i = 0; i != output_length(); i += 4) store_le(digest[i/4], output + i); } /* * Clear memory of sensitive data */ void HAS_160::clear() { MDx_HashFunction::clear(); zeroise(X); digest[0] = 0x67452301; digest[1] = 0xEFCDAB89; digest[2] = 0x98BADCFE; digest[3] = 0x10325476; digest[4] = 0xC3D2E1F0; } } /* * Keccak * (C) 2010 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { void keccak_f_1600(u64bit A[25]) { static const u64bit RC[24] = { 0x0000000000000001, 0x0000000000008082, 0x800000000000808A, 0x8000000080008000, 0x000000000000808B, 0x0000000080000001, 0x8000000080008081, 0x8000000000008009, 0x000000000000008A, 0x0000000000000088, 0x0000000080008009, 0x000000008000000A, 0x000000008000808B, 0x800000000000008B, 0x8000000000008089, 0x8000000000008003, 0x8000000000008002, 0x8000000000000080, 0x000000000000800A, 0x800000008000000A, 0x8000000080008081, 0x8000000000008080, 0x0000000080000001, 0x8000000080008008 }; for(size_t i = 0; i != 24; ++i) { const u64bit C0 = A[0] ^ A[5] ^ A[10] ^ A[15] ^ A[20]; const u64bit C1 = A[1] ^ A[6] ^ A[11] ^ A[16] ^ A[21]; const u64bit C2 = A[2] ^ A[7] ^ A[12] ^ A[17] ^ A[22]; const u64bit C3 = A[3] ^ A[8] ^ A[13] ^ A[18] ^ A[23]; const u64bit C4 = A[4] ^ A[9] ^ A[14] ^ A[19] ^ A[24]; const u64bit D0 = rotate_left(C0, 1) ^ C3; const u64bit D1 = rotate_left(C1, 1) ^ C4; const u64bit D2 = rotate_left(C2, 1) ^ C0; const u64bit D3 = rotate_left(C3, 1) ^ C1; const u64bit D4 = rotate_left(C4, 1) ^ C2; const u64bit B00 = A[ 0] ^ D1; const u64bit B01 = rotate_left(A[ 6] ^ D2, 44); const u64bit B02 = rotate_left(A[12] ^ D3, 43); const u64bit B03 = rotate_left(A[18] ^ D4, 21); const u64bit B04 = rotate_left(A[24] ^ D0, 14); const u64bit B05 = rotate_left(A[ 3] ^ D4, 28); const u64bit B06 = rotate_left(A[ 9] ^ D0, 20); const u64bit B07 = rotate_left(A[10] ^ D1, 3); const u64bit B08 = rotate_left(A[16] ^ D2, 45); const u64bit B09 = rotate_left(A[22] ^ D3, 61); const u64bit B10 = rotate_left(A[ 1] ^ D2, 1); const u64bit B11 = rotate_left(A[ 7] ^ D3, 6); const u64bit B12 = rotate_left(A[13] ^ D4, 25); const u64bit B13 = rotate_left(A[19] ^ D0, 8); const u64bit B14 = rotate_left(A[20] ^ D1, 18); const u64bit B15 = rotate_left(A[ 4] ^ D0, 27); const u64bit B16 = rotate_left(A[ 5] ^ D1, 36); const u64bit B17 = rotate_left(A[11] ^ D2, 10); const u64bit B18 = rotate_left(A[17] ^ D3, 15); const u64bit B19 = rotate_left(A[23] ^ D4, 56); const u64bit B20 = rotate_left(A[ 2] ^ D3, 62); const u64bit B21 = rotate_left(A[ 8] ^ D4, 55); const u64bit B22 = rotate_left(A[14] ^ D0, 39); const u64bit B23 = rotate_left(A[15] ^ D1, 41); const u64bit B24 = rotate_left(A[21] ^ D2, 2); A[ 0] = B00 ^ (~B01 & B02); A[ 1] = B01 ^ (~B02 & B03); A[ 2] = B02 ^ (~B03 & B04); A[ 3] = B03 ^ (~B04 & B00); A[ 4] = B04 ^ (~B00 & B01); A[ 5] = B05 ^ (~B06 & B07); A[ 6] = B06 ^ (~B07 & B08); A[ 7] = B07 ^ (~B08 & B09); A[ 8] = B08 ^ (~B09 & B05); A[ 9] = B09 ^ (~B05 & B06); A[10] = B10 ^ (~B11 & B12); A[11] = B11 ^ (~B12 & B13); A[12] = B12 ^ (~B13 & B14); A[13] = B13 ^ (~B14 & B10); A[14] = B14 ^ (~B10 & B11); A[15] = B15 ^ (~B16 & B17); A[16] = B16 ^ (~B17 & B18); A[17] = B17 ^ (~B18 & B19); A[18] = B18 ^ (~B19 & B15); A[19] = B19 ^ (~B15 & B16); A[20] = B20 ^ (~B21 & B22); A[21] = B21 ^ (~B22 & B23); A[22] = B22 ^ (~B23 & B24); A[23] = B23 ^ (~B24 & B20); A[24] = B24 ^ (~B20 & B21); A[0] ^= RC[i]; } } } Keccak_1600::Keccak_1600(size_t output_bits) : output_bits(output_bits), bitrate(1600 - 2*output_bits), S(25), S_pos(0) { // We only support the parameters for the SHA-3 proposal if(output_bits != 224 && output_bits != 256 && output_bits != 384 && output_bits != 512) throw Invalid_Argument("Keccak_1600: Invalid output length " + to_string(output_bits)); } std::string Keccak_1600::name() const { return "Keccak-1600(" + to_string(output_bits) + ")"; } HashFunction* Keccak_1600::clone() const { return new Keccak_1600(output_bits); } void Keccak_1600::clear() { zeroise(S); S_pos = 0; } void Keccak_1600::add_data(const byte input[], size_t length) { if(length == 0) return; while(length) { size_t to_take = std::min(length, bitrate / 8 - S_pos); length -= to_take; while(to_take && S_pos % 8) { S[S_pos / 8] ^= static_cast<u64bit>(input[0]) << (8 * (S_pos % 8)); ++S_pos; ++input; --to_take; } while(to_take && to_take % 8 == 0) { S[S_pos / 8] ^= load_le<u64bit>(input, 0); S_pos += 8; input += 8; to_take -= 8; } while(to_take) { S[S_pos / 8] ^= static_cast<u64bit>(input[0]) << (8 * (S_pos % 8)); ++S_pos; ++input; --to_take; } if(S_pos == bitrate / 8) { keccak_f_1600(&S[0]); S_pos = 0; } } } void Keccak_1600::final_result(byte output[]) { MemoryVector<byte> padding(bitrate / 8 - S_pos); padding[0] = 0x01; padding[padding.size()-1] |= 0x80; add_data(padding, padding.size()); /* * We never have to run the permutation again because we only support * limited output lengths */ for(size_t i = 0; i != output_bits/8; ++i) output[i] = get_byte(7 - (i % 8), S[i/8]); clear(); } } /* * MD2 * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /** * MD2 Compression Function */ void MD2::hash(const byte input[]) { static const byte SBOX[256] = { 0x29, 0x2E, 0x43, 0xC9, 0xA2, 0xD8, 0x7C, 0x01, 0x3D, 0x36, 0x54, 0xA1, 0xEC, 0xF0, 0x06, 0x13, 0x62, 0xA7, 0x05, 0xF3, 0xC0, 0xC7, 0x73, 0x8C, 0x98, 0x93, 0x2B, 0xD9, 0xBC, 0x4C, 0x82, 0xCA, 0x1E, 0x9B, 0x57, 0x3C, 0xFD, 0xD4, 0xE0, 0x16, 0x67, 0x42, 0x6F, 0x18, 0x8A, 0x17, 0xE5, 0x12, 0xBE, 0x4E, 0xC4, 0xD6, 0xDA, 0x9E, 0xDE, 0x49, 0xA0, 0xFB, 0xF5, 0x8E, 0xBB, 0x2F, 0xEE, 0x7A, 0xA9, 0x68, 0x79, 0x91, 0x15, 0xB2, 0x07, 0x3F, 0x94, 0xC2, 0x10, 0x89, 0x0B, 0x22, 0x5F, 0x21, 0x80, 0x7F, 0x5D, 0x9A, 0x5A, 0x90, 0x32, 0x27, 0x35, 0x3E, 0xCC, 0xE7, 0xBF, 0xF7, 0x97, 0x03, 0xFF, 0x19, 0x30, 0xB3, 0x48, 0xA5, 0xB5, 0xD1, 0xD7, 0x5E, 0x92, 0x2A, 0xAC, 0x56, 0xAA, 0xC6, 0x4F, 0xB8, 0x38, 0xD2, 0x96, 0xA4, 0x7D, 0xB6, 0x76, 0xFC, 0x6B, 0xE2, 0x9C, 0x74, 0x04, 0xF1, 0x45, 0x9D, 0x70, 0x59, 0x64, 0x71, 0x87, 0x20, 0x86, 0x5B, 0xCF, 0x65, 0xE6, 0x2D, 0xA8, 0x02, 0x1B, 0x60, 0x25, 0xAD, 0xAE, 0xB0, 0xB9, 0xF6, 0x1C, 0x46, 0x61, 0x69, 0x34, 0x40, 0x7E, 0x0F, 0x55, 0x47, 0xA3, 0x23, 0xDD, 0x51, 0xAF, 0x3A, 0xC3, 0x5C, 0xF9, 0xCE, 0xBA, 0xC5, 0xEA, 0x26, 0x2C, 0x53, 0x0D, 0x6E, 0x85, 0x28, 0x84, 0x09, 0xD3, 0xDF, 0xCD, 0xF4, 0x41, 0x81, 0x4D, 0x52, 0x6A, 0xDC, 0x37, 0xC8, 0x6C, 0xC1, 0xAB, 0xFA, 0x24, 0xE1, 0x7B, 0x08, 0x0C, 0xBD, 0xB1, 0x4A, 0x78, 0x88, 0x95, 0x8B, 0xE3, 0x63, 0xE8, 0x6D, 0xE9, 0xCB, 0xD5, 0xFE, 0x3B, 0x00, 0x1D, 0x39, 0xF2, 0xEF, 0xB7, 0x0E, 0x66, 0x58, 0xD0, 0xE4, 0xA6, 0x77, 0x72, 0xF8, 0xEB, 0x75, 0x4B, 0x0A, 0x31, 0x44, 0x50, 0xB4, 0x8F, 0xED, 0x1F, 0x1A, 0xDB, 0x99, 0x8D, 0x33, 0x9F, 0x11, 0x83, 0x14 }; X.copy(16, input, hash_block_size()); xor_buf(&X[32], &X[0], &X[16], hash_block_size()); byte T = 0; for(size_t i = 0; i != 18; ++i) { for(size_t k = 0; k != 48; k += 8) { T = X[k ] ^= SBOX[T]; T = X[k+1] ^= SBOX[T]; T = X[k+2] ^= SBOX[T]; T = X[k+3] ^= SBOX[T]; T = X[k+4] ^= SBOX[T]; T = X[k+5] ^= SBOX[T]; T = X[k+6] ^= SBOX[T]; T = X[k+7] ^= SBOX[T]; } T += static_cast<byte>(i); } T = checksum[15]; for(size_t i = 0; i != hash_block_size(); ++i) T = checksum[i] ^= SBOX[input[i] ^ T]; } /** * Update the hash */ void MD2::add_data(const byte input[], size_t length) { buffer.copy(position, input, length); if(position + length >= hash_block_size()) { hash(&buffer[0]); input += (hash_block_size() - position); length -= (hash_block_size() - position); while(length >= hash_block_size()) { hash(input); input += hash_block_size(); length -= hash_block_size(); } buffer.copy(input, length); position = 0; } position += length; } /** * Finalize a MD2 Hash */ void MD2::final_result(byte output[]) { for(size_t i = position; i != hash_block_size(); ++i) buffer[i] = static_cast<byte>(hash_block_size() - position); hash(&buffer[0]); hash(&checksum[0]); copy_mem(output, &X[0], output_length()); clear(); } /** * Clear memory of sensitive data */ void MD2::clear() { zeroise(X); zeroise(checksum); zeroise(buffer); position = 0; } } /* * MD4 * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { /* * MD4 FF Function */ inline void FF(u32bit& A, u32bit B, u32bit C, u32bit D, u32bit M, byte S) { A += (D ^ (B & (C ^ D))) + M; A = rotate_left(A, S); } /* * MD4 GG Function */ inline void GG(u32bit& A, u32bit B, u32bit C, u32bit D, u32bit M, byte S) { A += ((B & C) | (D & (B | C))) + M + 0x5A827999; A = rotate_left(A, S); } /* * MD4 HH Function */ inline void HH(u32bit& A, u32bit B, u32bit C, u32bit D, u32bit M, byte S) { A += (B ^ C ^ D) + M + 0x6ED9EBA1; A = rotate_left(A, S); } } /* * MD4 Compression Function */ void MD4::compress_n(const byte input[], size_t blocks) { u32bit A = digest[0], B = digest[1], C = digest[2], D = digest[3]; for(size_t i = 0; i != blocks; ++i) { load_le(&M[0], input, M.size()); FF(A,B,C,D,M[ 0], 3); FF(D,A,B,C,M[ 1], 7); FF(C,D,A,B,M[ 2],11); FF(B,C,D,A,M[ 3],19); FF(A,B,C,D,M[ 4], 3); FF(D,A,B,C,M[ 5], 7); FF(C,D,A,B,M[ 6],11); FF(B,C,D,A,M[ 7],19); FF(A,B,C,D,M[ 8], 3); FF(D,A,B,C,M[ 9], 7); FF(C,D,A,B,M[10],11); FF(B,C,D,A,M[11],19); FF(A,B,C,D,M[12], 3); FF(D,A,B,C,M[13], 7); FF(C,D,A,B,M[14],11); FF(B,C,D,A,M[15],19); GG(A,B,C,D,M[ 0], 3); GG(D,A,B,C,M[ 4], 5); GG(C,D,A,B,M[ 8], 9); GG(B,C,D,A,M[12],13); GG(A,B,C,D,M[ 1], 3); GG(D,A,B,C,M[ 5], 5); GG(C,D,A,B,M[ 9], 9); GG(B,C,D,A,M[13],13); GG(A,B,C,D,M[ 2], 3); GG(D,A,B,C,M[ 6], 5); GG(C,D,A,B,M[10], 9); GG(B,C,D,A,M[14],13); GG(A,B,C,D,M[ 3], 3); GG(D,A,B,C,M[ 7], 5); GG(C,D,A,B,M[11], 9); GG(B,C,D,A,M[15],13); HH(A,B,C,D,M[ 0], 3); HH(D,A,B,C,M[ 8], 9); HH(C,D,A,B,M[ 4],11); HH(B,C,D,A,M[12],15); HH(A,B,C,D,M[ 2], 3); HH(D,A,B,C,M[10], 9); HH(C,D,A,B,M[ 6],11); HH(B,C,D,A,M[14],15); HH(A,B,C,D,M[ 1], 3); HH(D,A,B,C,M[ 9], 9); HH(C,D,A,B,M[ 5],11); HH(B,C,D,A,M[13],15); HH(A,B,C,D,M[ 3], 3); HH(D,A,B,C,M[11], 9); HH(C,D,A,B,M[ 7],11); HH(B,C,D,A,M[15],15); A = (digest[0] += A); B = (digest[1] += B); C = (digest[2] += C); D = (digest[3] += D); input += hash_block_size(); } } /* * Copy out the digest */ void MD4::copy_out(byte output[]) { for(size_t i = 0; i != output_length(); i += 4) store_le(digest[i/4], output + i); } /* * Clear memory of sensitive data */ void MD4::clear() { MDx_HashFunction::clear(); zeroise(M); digest[0] = 0x67452301; digest[1] = 0xEFCDAB89; digest[2] = 0x98BADCFE; digest[3] = 0x10325476; } } /* * MD5 * (C) 1999-2008 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { /* * MD5 FF Function */ inline void FF(u32bit& A, u32bit B, u32bit C, u32bit D, u32bit msg, byte S, u32bit magic) { A += (D ^ (B & (C ^ D))) + msg + magic; A = rotate_left(A, S) + B; } /* * MD5 GG Function */ inline void GG(u32bit& A, u32bit B, u32bit C, u32bit D, u32bit msg, byte S, u32bit magic) { A += (C ^ (D & (B ^ C))) + msg + magic; A = rotate_left(A, S) + B; } /* * MD5 HH Function */ inline void HH(u32bit& A, u32bit B, u32bit C, u32bit D, u32bit msg, byte S, u32bit magic) { A += (B ^ C ^ D) + msg + magic; A = rotate_left(A, S) + B; } /* * MD5 II Function */ inline void II(u32bit& A, u32bit B, u32bit C, u32bit D, u32bit msg, byte S, u32bit magic) { A += (C ^ (B | ~D)) + msg + magic; A = rotate_left(A, S) + B; } } /* * MD5 Compression Function */ void MD5::compress_n(const byte input[], size_t blocks) { u32bit A = digest[0], B = digest[1], C = digest[2], D = digest[3]; for(size_t i = 0; i != blocks; ++i) { load_le(&M[0], input, M.size()); FF(A,B,C,D,M[ 0], 7,0xD76AA478); FF(D,A,B,C,M[ 1],12,0xE8C7B756); FF(C,D,A,B,M[ 2],17,0x242070DB); FF(B,C,D,A,M[ 3],22,0xC1BDCEEE); FF(A,B,C,D,M[ 4], 7,0xF57C0FAF); FF(D,A,B,C,M[ 5],12,0x4787C62A); FF(C,D,A,B,M[ 6],17,0xA8304613); FF(B,C,D,A,M[ 7],22,0xFD469501); FF(A,B,C,D,M[ 8], 7,0x698098D8); FF(D,A,B,C,M[ 9],12,0x8B44F7AF); FF(C,D,A,B,M[10],17,0xFFFF5BB1); FF(B,C,D,A,M[11],22,0x895CD7BE); FF(A,B,C,D,M[12], 7,0x6B901122); FF(D,A,B,C,M[13],12,0xFD987193); FF(C,D,A,B,M[14],17,0xA679438E); FF(B,C,D,A,M[15],22,0x49B40821); GG(A,B,C,D,M[ 1], 5,0xF61E2562); GG(D,A,B,C,M[ 6], 9,0xC040B340); GG(C,D,A,B,M[11],14,0x265E5A51); GG(B,C,D,A,M[ 0],20,0xE9B6C7AA); GG(A,B,C,D,M[ 5], 5,0xD62F105D); GG(D,A,B,C,M[10], 9,0x02441453); GG(C,D,A,B,M[15],14,0xD8A1E681); GG(B,C,D,A,M[ 4],20,0xE7D3FBC8); GG(A,B,C,D,M[ 9], 5,0x21E1CDE6); GG(D,A,B,C,M[14], 9,0xC33707D6); GG(C,D,A,B,M[ 3],14,0xF4D50D87); GG(B,C,D,A,M[ 8],20,0x455A14ED); GG(A,B,C,D,M[13], 5,0xA9E3E905); GG(D,A,B,C,M[ 2], 9,0xFCEFA3F8); GG(C,D,A,B,M[ 7],14,0x676F02D9); GG(B,C,D,A,M[12],20,0x8D2A4C8A); HH(A,B,C,D,M[ 5], 4,0xFFFA3942); HH(D,A,B,C,M[ 8],11,0x8771F681); HH(C,D,A,B,M[11],16,0x6D9D6122); HH(B,C,D,A,M[14],23,0xFDE5380C); HH(A,B,C,D,M[ 1], 4,0xA4BEEA44); HH(D,A,B,C,M[ 4],11,0x4BDECFA9); HH(C,D,A,B,M[ 7],16,0xF6BB4B60); HH(B,C,D,A,M[10],23,0xBEBFBC70); HH(A,B,C,D,M[13], 4,0x289B7EC6); HH(D,A,B,C,M[ 0],11,0xEAA127FA); HH(C,D,A,B,M[ 3],16,0xD4EF3085); HH(B,C,D,A,M[ 6],23,0x04881D05); HH(A,B,C,D,M[ 9], 4,0xD9D4D039); HH(D,A,B,C,M[12],11,0xE6DB99E5); HH(C,D,A,B,M[15],16,0x1FA27CF8); HH(B,C,D,A,M[ 2],23,0xC4AC5665); II(A,B,C,D,M[ 0], 6,0xF4292244); II(D,A,B,C,M[ 7],10,0x432AFF97); II(C,D,A,B,M[14],15,0xAB9423A7); II(B,C,D,A,M[ 5],21,0xFC93A039); II(A,B,C,D,M[12], 6,0x655B59C3); II(D,A,B,C,M[ 3],10,0x8F0CCC92); II(C,D,A,B,M[10],15,0xFFEFF47D); II(B,C,D,A,M[ 1],21,0x85845DD1); II(A,B,C,D,M[ 8], 6,0x6FA87E4F); II(D,A,B,C,M[15],10,0xFE2CE6E0); II(C,D,A,B,M[ 6],15,0xA3014314); II(B,C,D,A,M[13],21,0x4E0811A1); II(A,B,C,D,M[ 4], 6,0xF7537E82); II(D,A,B,C,M[11],10,0xBD3AF235); II(C,D,A,B,M[ 2],15,0x2AD7D2BB); II(B,C,D,A,M[ 9],21,0xEB86D391); A = (digest[0] += A); B = (digest[1] += B); C = (digest[2] += C); D = (digest[3] += D); input += hash_block_size(); } } /* * Copy out the digest */ void MD5::copy_out(byte output[]) { for(size_t i = 0; i != output_length(); i += 4) store_le(digest[i/4], output + i); } /* * Clear memory of sensitive data */ void MD5::clear() { MDx_HashFunction::clear(); zeroise(M); digest[0] = 0x67452301; digest[1] = 0xEFCDAB89; digest[2] = 0x98BADCFE; digest[3] = 0x10325476; } } /* * Merkle-Damgard Hash Function * (C) 1999-2008 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * MDx_HashFunction Constructor */ MDx_HashFunction::MDx_HashFunction(size_t block_len, bool byte_end, bool bit_end, size_t cnt_size) : buffer(block_len), BIG_BYTE_ENDIAN(byte_end), BIG_BIT_ENDIAN(bit_end), COUNT_SIZE(cnt_size) { count = position = 0; } /* * Clear memory of sensitive data */ void MDx_HashFunction::clear() { zeroise(buffer); count = position = 0; } /* * Update the hash */ void MDx_HashFunction::add_data(const byte input[], size_t length) { count += length; if(position) { buffer.copy(position, input, length); if(position + length >= buffer.size()) { compress_n(&buffer[0], 1); input += (buffer.size() - position); length -= (buffer.size() - position); position = 0; } } const size_t full_blocks = length / buffer.size(); const size_t remaining = length % buffer.size(); if(full_blocks) compress_n(input, full_blocks); buffer.copy(position, input + full_blocks * buffer.size(), remaining); position += remaining; } /* * Finalize a hash */ void MDx_HashFunction::final_result(byte output[]) { buffer[position] = (BIG_BIT_ENDIAN ? 0x80 : 0x01); for(size_t i = position+1; i != buffer.size(); ++i) buffer[i] = 0; if(position >= buffer.size() - COUNT_SIZE) { compress_n(&buffer[0], 1); zeroise(buffer); } write_count(&buffer[buffer.size() - COUNT_SIZE]); compress_n(&buffer[0], 1); copy_out(output); clear(); } /* * Write the count bits to the buffer */ void MDx_HashFunction::write_count(byte out[]) { if(COUNT_SIZE < 8) throw Invalid_State("MDx_HashFunction::write_count: COUNT_SIZE < 8"); if(COUNT_SIZE >= output_length() || COUNT_SIZE >= hash_block_size()) throw Invalid_Argument("MDx_HashFunction: COUNT_SIZE is too big"); const u64bit bit_count = count * 8; if(BIG_BYTE_ENDIAN) store_be(bit_count, out + COUNT_SIZE - 8); else store_le(bit_count, out + COUNT_SIZE - 8); } } /* * Parallel * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Update the hash */ void Parallel::add_data(const byte input[], size_t length) { for(size_t i = 0; i != hashes.size(); ++i) hashes[i]->update(input, length); } /* * Finalize the hash */ void Parallel::final_result(byte hash[]) { size_t offset = 0; for(size_t i = 0; i != hashes.size(); ++i) { hashes[i]->final(hash + offset); offset += hashes[i]->output_length(); } } /* * Return output size */ size_t Parallel::output_length() const { size_t sum = 0; for(size_t i = 0; i != hashes.size(); ++i) sum += hashes[i]->output_length(); return sum; } /* * Return the name of this type */ std::string Parallel::name() const { std::string hash_names; for(size_t i = 0; i != hashes.size(); ++i) { if(i) hash_names += ','; hash_names += hashes[i]->name(); } return "Parallel(" + hash_names + ")"; } /* * Return a clone of this object */ HashFunction* Parallel::clone() const { std::vector<HashFunction*> hash_copies; for(size_t i = 0; i != hashes.size(); ++i) hash_copies.push_back(hashes[i]->clone()); return new Parallel(hash_copies); } /* * Clear memory of sensitive data */ void Parallel::clear() { for(size_t i = 0; i != hashes.size(); ++i) hashes[i]->clear(); } /* * Parallel Constructor */ Parallel::Parallel(const std::vector<HashFunction*>& hash_in) : hashes(hash_in) { } /* * Parallel Destructor */ Parallel::~Parallel() { for(size_t i = 0; i != hashes.size(); ++i) delete hashes[i]; } } /* * RIPEMD-128 * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace RIPEMD_128_F { /* * RIPEMD-128 F1 Function */ inline void F1(u32bit& A, u32bit B, u32bit C, u32bit D, u32bit msg, u32bit shift) { A += (B ^ C ^ D) + msg; A = rotate_left(A, shift); } /* * RIPEMD-128 F2 Function */ inline void F2(u32bit& A, u32bit B, u32bit C, u32bit D, u32bit msg, u32bit shift, u32bit magic) { A += (D ^ (B & (C ^ D))) + msg + magic; A = rotate_left(A, shift); } /* * RIPEMD-128 F3 Function */ inline void F3(u32bit& A, u32bit B, u32bit C, u32bit D, u32bit msg, u32bit shift, u32bit magic) { A += (D ^ (B | ~C)) + msg + magic; A = rotate_left(A, shift); } /* * RIPEMD-128 F4 Function */ inline void F4(u32bit& A, u32bit B, u32bit C, u32bit D, u32bit msg, u32bit shift, u32bit magic) { A += (C ^ (D & (B ^ C))) + msg + magic; A = rotate_left(A, shift); } } /* * RIPEMD-128 Compression Function */ void RIPEMD_128::compress_n(const byte input[], size_t blocks) { using namespace RIPEMD_128_F; const u32bit MAGIC2 = 0x5A827999, MAGIC3 = 0x6ED9EBA1, MAGIC4 = 0x8F1BBCDC, MAGIC5 = 0x50A28BE6, MAGIC6 = 0x5C4DD124, MAGIC7 = 0x6D703EF3; for(size_t i = 0; i != blocks; ++i) { load_le(&M[0], input, M.size()); u32bit A1 = digest[0], A2 = A1, B1 = digest[1], B2 = B1, C1 = digest[2], C2 = C1, D1 = digest[3], D2 = D1; F1(A1,B1,C1,D1,M[ 0],11 ); F4(A2,B2,C2,D2,M[ 5], 8,MAGIC5); F1(D1,A1,B1,C1,M[ 1],14 ); F4(D2,A2,B2,C2,M[14], 9,MAGIC5); F1(C1,D1,A1,B1,M[ 2],15 ); F4(C2,D2,A2,B2,M[ 7], 9,MAGIC5); F1(B1,C1,D1,A1,M[ 3],12 ); F4(B2,C2,D2,A2,M[ 0],11,MAGIC5); F1(A1,B1,C1,D1,M[ 4], 5 ); F4(A2,B2,C2,D2,M[ 9],13,MAGIC5); F1(D1,A1,B1,C1,M[ 5], 8 ); F4(D2,A2,B2,C2,M[ 2],15,MAGIC5); F1(C1,D1,A1,B1,M[ 6], 7 ); F4(C2,D2,A2,B2,M[11],15,MAGIC5); F1(B1,C1,D1,A1,M[ 7], 9 ); F4(B2,C2,D2,A2,M[ 4], 5,MAGIC5); F1(A1,B1,C1,D1,M[ 8],11 ); F4(A2,B2,C2,D2,M[13], 7,MAGIC5); F1(D1,A1,B1,C1,M[ 9],13 ); F4(D2,A2,B2,C2,M[ 6], 7,MAGIC5); F1(C1,D1,A1,B1,M[10],14 ); F4(C2,D2,A2,B2,M[15], 8,MAGIC5); F1(B1,C1,D1,A1,M[11],15 ); F4(B2,C2,D2,A2,M[ 8],11,MAGIC5); F1(A1,B1,C1,D1,M[12], 6 ); F4(A2,B2,C2,D2,M[ 1],14,MAGIC5); F1(D1,A1,B1,C1,M[13], 7 ); F4(D2,A2,B2,C2,M[10],14,MAGIC5); F1(C1,D1,A1,B1,M[14], 9 ); F4(C2,D2,A2,B2,M[ 3],12,MAGIC5); F1(B1,C1,D1,A1,M[15], 8 ); F4(B2,C2,D2,A2,M[12], 6,MAGIC5); F2(A1,B1,C1,D1,M[ 7], 7,MAGIC2); F3(A2,B2,C2,D2,M[ 6], 9,MAGIC6); F2(D1,A1,B1,C1,M[ 4], 6,MAGIC2); F3(D2,A2,B2,C2,M[11],13,MAGIC6); F2(C1,D1,A1,B1,M[13], 8,MAGIC2); F3(C2,D2,A2,B2,M[ 3],15,MAGIC6); F2(B1,C1,D1,A1,M[ 1],13,MAGIC2); F3(B2,C2,D2,A2,M[ 7], 7,MAGIC6); F2(A1,B1,C1,D1,M[10],11,MAGIC2); F3(A2,B2,C2,D2,M[ 0],12,MAGIC6); F2(D1,A1,B1,C1,M[ 6], 9,MAGIC2); F3(D2,A2,B2,C2,M[13], 8,MAGIC6); F2(C1,D1,A1,B1,M[15], 7,MAGIC2); F3(C2,D2,A2,B2,M[ 5], 9,MAGIC6); F2(B1,C1,D1,A1,M[ 3],15,MAGIC2); F3(B2,C2,D2,A2,M[10],11,MAGIC6); F2(A1,B1,C1,D1,M[12], 7,MAGIC2); F3(A2,B2,C2,D2,M[14], 7,MAGIC6); F2(D1,A1,B1,C1,M[ 0],12,MAGIC2); F3(D2,A2,B2,C2,M[15], 7,MAGIC6); F2(C1,D1,A1,B1,M[ 9],15,MAGIC2); F3(C2,D2,A2,B2,M[ 8],12,MAGIC6); F2(B1,C1,D1,A1,M[ 5], 9,MAGIC2); F3(B2,C2,D2,A2,M[12], 7,MAGIC6); F2(A1,B1,C1,D1,M[ 2],11,MAGIC2); F3(A2,B2,C2,D2,M[ 4], 6,MAGIC6); F2(D1,A1,B1,C1,M[14], 7,MAGIC2); F3(D2,A2,B2,C2,M[ 9],15,MAGIC6); F2(C1,D1,A1,B1,M[11],13,MAGIC2); F3(C2,D2,A2,B2,M[ 1],13,MAGIC6); F2(B1,C1,D1,A1,M[ 8],12,MAGIC2); F3(B2,C2,D2,A2,M[ 2],11,MAGIC6); F3(A1,B1,C1,D1,M[ 3],11,MAGIC3); F2(A2,B2,C2,D2,M[15], 9,MAGIC7); F3(D1,A1,B1,C1,M[10],13,MAGIC3); F2(D2,A2,B2,C2,M[ 5], 7,MAGIC7); F3(C1,D1,A1,B1,M[14], 6,MAGIC3); F2(C2,D2,A2,B2,M[ 1],15,MAGIC7); F3(B1,C1,D1,A1,M[ 4], 7,MAGIC3); F2(B2,C2,D2,A2,M[ 3],11,MAGIC7); F3(A1,B1,C1,D1,M[ 9],14,MAGIC3); F2(A2,B2,C2,D2,M[ 7], 8,MAGIC7); F3(D1,A1,B1,C1,M[15], 9,MAGIC3); F2(D2,A2,B2,C2,M[14], 6,MAGIC7); F3(C1,D1,A1,B1,M[ 8],13,MAGIC3); F2(C2,D2,A2,B2,M[ 6], 6,MAGIC7); F3(B1,C1,D1,A1,M[ 1],15,MAGIC3); F2(B2,C2,D2,A2,M[ 9],14,MAGIC7); F3(A1,B1,C1,D1,M[ 2],14,MAGIC3); F2(A2,B2,C2,D2,M[11],12,MAGIC7); F3(D1,A1,B1,C1,M[ 7], 8,MAGIC3); F2(D2,A2,B2,C2,M[ 8],13,MAGIC7); F3(C1,D1,A1,B1,M[ 0],13,MAGIC3); F2(C2,D2,A2,B2,M[12], 5,MAGIC7); F3(B1,C1,D1,A1,M[ 6], 6,MAGIC3); F2(B2,C2,D2,A2,M[ 2],14,MAGIC7); F3(A1,B1,C1,D1,M[13], 5,MAGIC3); F2(A2,B2,C2,D2,M[10],13,MAGIC7); F3(D1,A1,B1,C1,M[11],12,MAGIC3); F2(D2,A2,B2,C2,M[ 0],13,MAGIC7); F3(C1,D1,A1,B1,M[ 5], 7,MAGIC3); F2(C2,D2,A2,B2,M[ 4], 7,MAGIC7); F3(B1,C1,D1,A1,M[12], 5,MAGIC3); F2(B2,C2,D2,A2,M[13], 5,MAGIC7); F4(A1,B1,C1,D1,M[ 1],11,MAGIC4); F1(A2,B2,C2,D2,M[ 8],15 ); F4(D1,A1,B1,C1,M[ 9],12,MAGIC4); F1(D2,A2,B2,C2,M[ 6], 5 ); F4(C1,D1,A1,B1,M[11],14,MAGIC4); F1(C2,D2,A2,B2,M[ 4], 8 ); F4(B1,C1,D1,A1,M[10],15,MAGIC4); F1(B2,C2,D2,A2,M[ 1],11 ); F4(A1,B1,C1,D1,M[ 0],14,MAGIC4); F1(A2,B2,C2,D2,M[ 3],14 ); F4(D1,A1,B1,C1,M[ 8],15,MAGIC4); F1(D2,A2,B2,C2,M[11],14 ); F4(C1,D1,A1,B1,M[12], 9,MAGIC4); F1(C2,D2,A2,B2,M[15], 6 ); F4(B1,C1,D1,A1,M[ 4], 8,MAGIC4); F1(B2,C2,D2,A2,M[ 0],14 ); F4(A1,B1,C1,D1,M[13], 9,MAGIC4); F1(A2,B2,C2,D2,M[ 5], 6 ); F4(D1,A1,B1,C1,M[ 3],14,MAGIC4); F1(D2,A2,B2,C2,M[12], 9 ); F4(C1,D1,A1,B1,M[ 7], 5,MAGIC4); F1(C2,D2,A2,B2,M[ 2],12 ); F4(B1,C1,D1,A1,M[15], 6,MAGIC4); F1(B2,C2,D2,A2,M[13], 9 ); F4(A1,B1,C1,D1,M[14], 8,MAGIC4); F1(A2,B2,C2,D2,M[ 9],12 ); F4(D1,A1,B1,C1,M[ 5], 6,MAGIC4); F1(D2,A2,B2,C2,M[ 7], 5 ); F4(C1,D1,A1,B1,M[ 6], 5,MAGIC4); F1(C2,D2,A2,B2,M[10],15 ); F4(B1,C1,D1,A1,M[ 2],12,MAGIC4); F1(B2,C2,D2,A2,M[14], 8 ); D2 = digest[1] + C1 + D2; digest[1] = digest[2] + D1 + A2; digest[2] = digest[3] + A1 + B2; digest[3] = digest[0] + B1 + C2; digest[0] = D2; input += hash_block_size(); } } /* * Copy out the digest */ void RIPEMD_128::copy_out(byte output[]) { for(size_t i = 0; i != output_length(); i += 4) store_le(digest[i/4], output + i); } /* * Clear memory of sensitive data */ void RIPEMD_128::clear() { MDx_HashFunction::clear(); zeroise(M); digest[0] = 0x67452301; digest[1] = 0xEFCDAB89; digest[2] = 0x98BADCFE; digest[3] = 0x10325476; } } /* * RIPEMD-160 * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { /* * RIPEMD-160 F1 Function */ inline void F1(u32bit& A, u32bit B, u32bit& C, u32bit D, u32bit E, u32bit msg, u32bit shift) { A += (B ^ C ^ D) + msg; A = rotate_left(A, shift) + E; C = rotate_left(C, 10); } /* * RIPEMD-160 F2 Function */ inline void F2(u32bit& A, u32bit B, u32bit& C, u32bit D, u32bit E, u32bit msg, u32bit shift, u32bit magic) { A += (D ^ (B & (C ^ D))) + msg + magic; A = rotate_left(A, shift) + E; C = rotate_left(C, 10); } /* * RIPEMD-160 F3 Function */ inline void F3(u32bit& A, u32bit B, u32bit& C, u32bit D, u32bit E, u32bit msg, u32bit shift, u32bit magic) { A += (D ^ (B | ~C)) + msg + magic; A = rotate_left(A, shift) + E; C = rotate_left(C, 10); } /* * RIPEMD-160 F4 Function */ inline void F4(u32bit& A, u32bit B, u32bit& C, u32bit D, u32bit E, u32bit msg, u32bit shift, u32bit magic) { A += (C ^ (D & (B ^ C))) + msg + magic; A = rotate_left(A, shift) + E; C = rotate_left(C, 10); } /* * RIPEMD-160 F5 Function */ inline void F5(u32bit& A, u32bit B, u32bit& C, u32bit D, u32bit E, u32bit msg, u32bit shift, u32bit magic) { A += (B ^ (C | ~D)) + msg + magic; A = rotate_left(A, shift) + E; C = rotate_left(C, 10); } } /* * RIPEMD-160 Compression Function */ void RIPEMD_160::compress_n(const byte input[], size_t blocks) { const u32bit MAGIC2 = 0x5A827999, MAGIC3 = 0x6ED9EBA1, MAGIC4 = 0x8F1BBCDC, MAGIC5 = 0xA953FD4E, MAGIC6 = 0x50A28BE6, MAGIC7 = 0x5C4DD124, MAGIC8 = 0x6D703EF3, MAGIC9 = 0x7A6D76E9; for(size_t i = 0; i != blocks; ++i) { load_le(&M[0], input, M.size()); u32bit A1 = digest[0], A2 = A1, B1 = digest[1], B2 = B1, C1 = digest[2], C2 = C1, D1 = digest[3], D2 = D1, E1 = digest[4], E2 = E1; F1(A1,B1,C1,D1,E1,M[ 0],11 ); F5(A2,B2,C2,D2,E2,M[ 5], 8,MAGIC6); F1(E1,A1,B1,C1,D1,M[ 1],14 ); F5(E2,A2,B2,C2,D2,M[14], 9,MAGIC6); F1(D1,E1,A1,B1,C1,M[ 2],15 ); F5(D2,E2,A2,B2,C2,M[ 7], 9,MAGIC6); F1(C1,D1,E1,A1,B1,M[ 3],12 ); F5(C2,D2,E2,A2,B2,M[ 0],11,MAGIC6); F1(B1,C1,D1,E1,A1,M[ 4], 5 ); F5(B2,C2,D2,E2,A2,M[ 9],13,MAGIC6); F1(A1,B1,C1,D1,E1,M[ 5], 8 ); F5(A2,B2,C2,D2,E2,M[ 2],15,MAGIC6); F1(E1,A1,B1,C1,D1,M[ 6], 7 ); F5(E2,A2,B2,C2,D2,M[11],15,MAGIC6); F1(D1,E1,A1,B1,C1,M[ 7], 9 ); F5(D2,E2,A2,B2,C2,M[ 4], 5,MAGIC6); F1(C1,D1,E1,A1,B1,M[ 8],11 ); F5(C2,D2,E2,A2,B2,M[13], 7,MAGIC6); F1(B1,C1,D1,E1,A1,M[ 9],13 ); F5(B2,C2,D2,E2,A2,M[ 6], 7,MAGIC6); F1(A1,B1,C1,D1,E1,M[10],14 ); F5(A2,B2,C2,D2,E2,M[15], 8,MAGIC6); F1(E1,A1,B1,C1,D1,M[11],15 ); F5(E2,A2,B2,C2,D2,M[ 8],11,MAGIC6); F1(D1,E1,A1,B1,C1,M[12], 6 ); F5(D2,E2,A2,B2,C2,M[ 1],14,MAGIC6); F1(C1,D1,E1,A1,B1,M[13], 7 ); F5(C2,D2,E2,A2,B2,M[10],14,MAGIC6); F1(B1,C1,D1,E1,A1,M[14], 9 ); F5(B2,C2,D2,E2,A2,M[ 3],12,MAGIC6); F1(A1,B1,C1,D1,E1,M[15], 8 ); F5(A2,B2,C2,D2,E2,M[12], 6,MAGIC6); F2(E1,A1,B1,C1,D1,M[ 7], 7,MAGIC2); F4(E2,A2,B2,C2,D2,M[ 6], 9,MAGIC7); F2(D1,E1,A1,B1,C1,M[ 4], 6,MAGIC2); F4(D2,E2,A2,B2,C2,M[11],13,MAGIC7); F2(C1,D1,E1,A1,B1,M[13], 8,MAGIC2); F4(C2,D2,E2,A2,B2,M[ 3],15,MAGIC7); F2(B1,C1,D1,E1,A1,M[ 1],13,MAGIC2); F4(B2,C2,D2,E2,A2,M[ 7], 7,MAGIC7); F2(A1,B1,C1,D1,E1,M[10],11,MAGIC2); F4(A2,B2,C2,D2,E2,M[ 0],12,MAGIC7); F2(E1,A1,B1,C1,D1,M[ 6], 9,MAGIC2); F4(E2,A2,B2,C2,D2,M[13], 8,MAGIC7); F2(D1,E1,A1,B1,C1,M[15], 7,MAGIC2); F4(D2,E2,A2,B2,C2,M[ 5], 9,MAGIC7); F2(C1,D1,E1,A1,B1,M[ 3],15,MAGIC2); F4(C2,D2,E2,A2,B2,M[10],11,MAGIC7); F2(B1,C1,D1,E1,A1,M[12], 7,MAGIC2); F4(B2,C2,D2,E2,A2,M[14], 7,MAGIC7); F2(A1,B1,C1,D1,E1,M[ 0],12,MAGIC2); F4(A2,B2,C2,D2,E2,M[15], 7,MAGIC7); F2(E1,A1,B1,C1,D1,M[ 9],15,MAGIC2); F4(E2,A2,B2,C2,D2,M[ 8],12,MAGIC7); F2(D1,E1,A1,B1,C1,M[ 5], 9,MAGIC2); F4(D2,E2,A2,B2,C2,M[12], 7,MAGIC7); F2(C1,D1,E1,A1,B1,M[ 2],11,MAGIC2); F4(C2,D2,E2,A2,B2,M[ 4], 6,MAGIC7); F2(B1,C1,D1,E1,A1,M[14], 7,MAGIC2); F4(B2,C2,D2,E2,A2,M[ 9],15,MAGIC7); F2(A1,B1,C1,D1,E1,M[11],13,MAGIC2); F4(A2,B2,C2,D2,E2,M[ 1],13,MAGIC7); F2(E1,A1,B1,C1,D1,M[ 8],12,MAGIC2); F4(E2,A2,B2,C2,D2,M[ 2],11,MAGIC7); F3(D1,E1,A1,B1,C1,M[ 3],11,MAGIC3); F3(D2,E2,A2,B2,C2,M[15], 9,MAGIC8); F3(C1,D1,E1,A1,B1,M[10],13,MAGIC3); F3(C2,D2,E2,A2,B2,M[ 5], 7,MAGIC8); F3(B1,C1,D1,E1,A1,M[14], 6,MAGIC3); F3(B2,C2,D2,E2,A2,M[ 1],15,MAGIC8); F3(A1,B1,C1,D1,E1,M[ 4], 7,MAGIC3); F3(A2,B2,C2,D2,E2,M[ 3],11,MAGIC8); F3(E1,A1,B1,C1,D1,M[ 9],14,MAGIC3); F3(E2,A2,B2,C2,D2,M[ 7], 8,MAGIC8); F3(D1,E1,A1,B1,C1,M[15], 9,MAGIC3); F3(D2,E2,A2,B2,C2,M[14], 6,MAGIC8); F3(C1,D1,E1,A1,B1,M[ 8],13,MAGIC3); F3(C2,D2,E2,A2,B2,M[ 6], 6,MAGIC8); F3(B1,C1,D1,E1,A1,M[ 1],15,MAGIC3); F3(B2,C2,D2,E2,A2,M[ 9],14,MAGIC8); F3(A1,B1,C1,D1,E1,M[ 2],14,MAGIC3); F3(A2,B2,C2,D2,E2,M[11],12,MAGIC8); F3(E1,A1,B1,C1,D1,M[ 7], 8,MAGIC3); F3(E2,A2,B2,C2,D2,M[ 8],13,MAGIC8); F3(D1,E1,A1,B1,C1,M[ 0],13,MAGIC3); F3(D2,E2,A2,B2,C2,M[12], 5,MAGIC8); F3(C1,D1,E1,A1,B1,M[ 6], 6,MAGIC3); F3(C2,D2,E2,A2,B2,M[ 2],14,MAGIC8); F3(B1,C1,D1,E1,A1,M[13], 5,MAGIC3); F3(B2,C2,D2,E2,A2,M[10],13,MAGIC8); F3(A1,B1,C1,D1,E1,M[11],12,MAGIC3); F3(A2,B2,C2,D2,E2,M[ 0],13,MAGIC8); F3(E1,A1,B1,C1,D1,M[ 5], 7,MAGIC3); F3(E2,A2,B2,C2,D2,M[ 4], 7,MAGIC8); F3(D1,E1,A1,B1,C1,M[12], 5,MAGIC3); F3(D2,E2,A2,B2,C2,M[13], 5,MAGIC8); F4(C1,D1,E1,A1,B1,M[ 1],11,MAGIC4); F2(C2,D2,E2,A2,B2,M[ 8],15,MAGIC9); F4(B1,C1,D1,E1,A1,M[ 9],12,MAGIC4); F2(B2,C2,D2,E2,A2,M[ 6], 5,MAGIC9); F4(A1,B1,C1,D1,E1,M[11],14,MAGIC4); F2(A2,B2,C2,D2,E2,M[ 4], 8,MAGIC9); F4(E1,A1,B1,C1,D1,M[10],15,MAGIC4); F2(E2,A2,B2,C2,D2,M[ 1],11,MAGIC9); F4(D1,E1,A1,B1,C1,M[ 0],14,MAGIC4); F2(D2,E2,A2,B2,C2,M[ 3],14,MAGIC9); F4(C1,D1,E1,A1,B1,M[ 8],15,MAGIC4); F2(C2,D2,E2,A2,B2,M[11],14,MAGIC9); F4(B1,C1,D1,E1,A1,M[12], 9,MAGIC4); F2(B2,C2,D2,E2,A2,M[15], 6,MAGIC9); F4(A1,B1,C1,D1,E1,M[ 4], 8,MAGIC4); F2(A2,B2,C2,D2,E2,M[ 0],14,MAGIC9); F4(E1,A1,B1,C1,D1,M[13], 9,MAGIC4); F2(E2,A2,B2,C2,D2,M[ 5], 6,MAGIC9); F4(D1,E1,A1,B1,C1,M[ 3],14,MAGIC4); F2(D2,E2,A2,B2,C2,M[12], 9,MAGIC9); F4(C1,D1,E1,A1,B1,M[ 7], 5,MAGIC4); F2(C2,D2,E2,A2,B2,M[ 2],12,MAGIC9); F4(B1,C1,D1,E1,A1,M[15], 6,MAGIC4); F2(B2,C2,D2,E2,A2,M[13], 9,MAGIC9); F4(A1,B1,C1,D1,E1,M[14], 8,MAGIC4); F2(A2,B2,C2,D2,E2,M[ 9],12,MAGIC9); F4(E1,A1,B1,C1,D1,M[ 5], 6,MAGIC4); F2(E2,A2,B2,C2,D2,M[ 7], 5,MAGIC9); F4(D1,E1,A1,B1,C1,M[ 6], 5,MAGIC4); F2(D2,E2,A2,B2,C2,M[10],15,MAGIC9); F4(C1,D1,E1,A1,B1,M[ 2],12,MAGIC4); F2(C2,D2,E2,A2,B2,M[14], 8,MAGIC9); F5(B1,C1,D1,E1,A1,M[ 4], 9,MAGIC5); F1(B2,C2,D2,E2,A2,M[12], 8 ); F5(A1,B1,C1,D1,E1,M[ 0],15,MAGIC5); F1(A2,B2,C2,D2,E2,M[15], 5 ); F5(E1,A1,B1,C1,D1,M[ 5], 5,MAGIC5); F1(E2,A2,B2,C2,D2,M[10],12 ); F5(D1,E1,A1,B1,C1,M[ 9],11,MAGIC5); F1(D2,E2,A2,B2,C2,M[ 4], 9 ); F5(C1,D1,E1,A1,B1,M[ 7], 6,MAGIC5); F1(C2,D2,E2,A2,B2,M[ 1],12 ); F5(B1,C1,D1,E1,A1,M[12], 8,MAGIC5); F1(B2,C2,D2,E2,A2,M[ 5], 5 ); F5(A1,B1,C1,D1,E1,M[ 2],13,MAGIC5); F1(A2,B2,C2,D2,E2,M[ 8],14 ); F5(E1,A1,B1,C1,D1,M[10],12,MAGIC5); F1(E2,A2,B2,C2,D2,M[ 7], 6 ); F5(D1,E1,A1,B1,C1,M[14], 5,MAGIC5); F1(D2,E2,A2,B2,C2,M[ 6], 8 ); F5(C1,D1,E1,A1,B1,M[ 1],12,MAGIC5); F1(C2,D2,E2,A2,B2,M[ 2],13 ); F5(B1,C1,D1,E1,A1,M[ 3],13,MAGIC5); F1(B2,C2,D2,E2,A2,M[13], 6 ); F5(A1,B1,C1,D1,E1,M[ 8],14,MAGIC5); F1(A2,B2,C2,D2,E2,M[14], 5 ); F5(E1,A1,B1,C1,D1,M[11],11,MAGIC5); F1(E2,A2,B2,C2,D2,M[ 0],15 ); F5(D1,E1,A1,B1,C1,M[ 6], 8,MAGIC5); F1(D2,E2,A2,B2,C2,M[ 3],13 ); F5(C1,D1,E1,A1,B1,M[15], 5,MAGIC5); F1(C2,D2,E2,A2,B2,M[ 9],11 ); F5(B1,C1,D1,E1,A1,M[13], 6,MAGIC5); F1(B2,C2,D2,E2,A2,M[11],11 ); C1 = digest[1] + C1 + D2; digest[1] = digest[2] + D1 + E2; digest[2] = digest[3] + E1 + A2; digest[3] = digest[4] + A1 + B2; digest[4] = digest[0] + B1 + C2; digest[0] = C1; input += hash_block_size(); } } /* * Copy out the digest */ void RIPEMD_160::copy_out(byte output[]) { for(size_t i = 0; i != output_length(); i += 4) store_le(digest[i/4], output + i); } /* * Clear memory of sensitive data */ void RIPEMD_160::clear() { MDx_HashFunction::clear(); zeroise(M); digest[0] = 0x67452301; digest[1] = 0xEFCDAB89; digest[2] = 0x98BADCFE; digest[3] = 0x10325476; digest[4] = 0xC3D2E1F0; } } /* * SHA-160 * (C) 1999-2008,2011 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace SHA1_F { namespace { /* * SHA-160 F1 Function */ inline void F1(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, u32bit msg) { E += (D ^ (B & (C ^ D))) + msg + 0x5A827999 + rotate_left(A, 5); B = rotate_left(B, 30); } /* * SHA-160 F2 Function */ inline void F2(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, u32bit msg) { E += (B ^ C ^ D) + msg + 0x6ED9EBA1 + rotate_left(A, 5); B = rotate_left(B, 30); } /* * SHA-160 F3 Function */ inline void F3(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, u32bit msg) { E += ((B & C) | ((B | C) & D)) + msg + 0x8F1BBCDC + rotate_left(A, 5); B = rotate_left(B, 30); } /* * SHA-160 F4 Function */ inline void F4(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, u32bit msg) { E += (B ^ C ^ D) + msg + 0xCA62C1D6 + rotate_left(A, 5); B = rotate_left(B, 30); } } } /* * SHA-160 Compression Function */ void SHA_160::compress_n(const byte input[], size_t blocks) { using namespace SHA1_F; u32bit A = digest[0], B = digest[1], C = digest[2], D = digest[3], E = digest[4]; for(size_t i = 0; i != blocks; ++i) { load_be(&W[0], input, 16); for(size_t j = 16; j != 80; j += 8) { W[j ] = rotate_left((W[j-3] ^ W[j-8] ^ W[j-14] ^ W[j-16]), 1); W[j+1] = rotate_left((W[j-2] ^ W[j-7] ^ W[j-13] ^ W[j-15]), 1); W[j+2] = rotate_left((W[j-1] ^ W[j-6] ^ W[j-12] ^ W[j-14]), 1); W[j+3] = rotate_left((W[j ] ^ W[j-5] ^ W[j-11] ^ W[j-13]), 1); W[j+4] = rotate_left((W[j+1] ^ W[j-4] ^ W[j-10] ^ W[j-12]), 1); W[j+5] = rotate_left((W[j+2] ^ W[j-3] ^ W[j- 9] ^ W[j-11]), 1); W[j+6] = rotate_left((W[j+3] ^ W[j-2] ^ W[j- 8] ^ W[j-10]), 1); W[j+7] = rotate_left((W[j+4] ^ W[j-1] ^ W[j- 7] ^ W[j- 9]), 1); } F1(A, B, C, D, E, W[ 0]); F1(E, A, B, C, D, W[ 1]); F1(D, E, A, B, C, W[ 2]); F1(C, D, E, A, B, W[ 3]); F1(B, C, D, E, A, W[ 4]); F1(A, B, C, D, E, W[ 5]); F1(E, A, B, C, D, W[ 6]); F1(D, E, A, B, C, W[ 7]); F1(C, D, E, A, B, W[ 8]); F1(B, C, D, E, A, W[ 9]); F1(A, B, C, D, E, W[10]); F1(E, A, B, C, D, W[11]); F1(D, E, A, B, C, W[12]); F1(C, D, E, A, B, W[13]); F1(B, C, D, E, A, W[14]); F1(A, B, C, D, E, W[15]); F1(E, A, B, C, D, W[16]); F1(D, E, A, B, C, W[17]); F1(C, D, E, A, B, W[18]); F1(B, C, D, E, A, W[19]); F2(A, B, C, D, E, W[20]); F2(E, A, B, C, D, W[21]); F2(D, E, A, B, C, W[22]); F2(C, D, E, A, B, W[23]); F2(B, C, D, E, A, W[24]); F2(A, B, C, D, E, W[25]); F2(E, A, B, C, D, W[26]); F2(D, E, A, B, C, W[27]); F2(C, D, E, A, B, W[28]); F2(B, C, D, E, A, W[29]); F2(A, B, C, D, E, W[30]); F2(E, A, B, C, D, W[31]); F2(D, E, A, B, C, W[32]); F2(C, D, E, A, B, W[33]); F2(B, C, D, E, A, W[34]); F2(A, B, C, D, E, W[35]); F2(E, A, B, C, D, W[36]); F2(D, E, A, B, C, W[37]); F2(C, D, E, A, B, W[38]); F2(B, C, D, E, A, W[39]); F3(A, B, C, D, E, W[40]); F3(E, A, B, C, D, W[41]); F3(D, E, A, B, C, W[42]); F3(C, D, E, A, B, W[43]); F3(B, C, D, E, A, W[44]); F3(A, B, C, D, E, W[45]); F3(E, A, B, C, D, W[46]); F3(D, E, A, B, C, W[47]); F3(C, D, E, A, B, W[48]); F3(B, C, D, E, A, W[49]); F3(A, B, C, D, E, W[50]); F3(E, A, B, C, D, W[51]); F3(D, E, A, B, C, W[52]); F3(C, D, E, A, B, W[53]); F3(B, C, D, E, A, W[54]); F3(A, B, C, D, E, W[55]); F3(E, A, B, C, D, W[56]); F3(D, E, A, B, C, W[57]); F3(C, D, E, A, B, W[58]); F3(B, C, D, E, A, W[59]); F4(A, B, C, D, E, W[60]); F4(E, A, B, C, D, W[61]); F4(D, E, A, B, C, W[62]); F4(C, D, E, A, B, W[63]); F4(B, C, D, E, A, W[64]); F4(A, B, C, D, E, W[65]); F4(E, A, B, C, D, W[66]); F4(D, E, A, B, C, W[67]); F4(C, D, E, A, B, W[68]); F4(B, C, D, E, A, W[69]); F4(A, B, C, D, E, W[70]); F4(E, A, B, C, D, W[71]); F4(D, E, A, B, C, W[72]); F4(C, D, E, A, B, W[73]); F4(B, C, D, E, A, W[74]); F4(A, B, C, D, E, W[75]); F4(E, A, B, C, D, W[76]); F4(D, E, A, B, C, W[77]); F4(C, D, E, A, B, W[78]); F4(B, C, D, E, A, W[79]); A = (digest[0] += A); B = (digest[1] += B); C = (digest[2] += C); D = (digest[3] += D); E = (digest[4] += E); input += hash_block_size(); } } /* * Copy out the digest */ void SHA_160::copy_out(byte output[]) { for(size_t i = 0; i != output_length(); i += 4) store_be(digest[i/4], output + i); } /* * Clear memory of sensitive data */ void SHA_160::clear() { MDx_HashFunction::clear(); zeroise(W); digest[0] = 0x67452301; digest[1] = 0xEFCDAB89; digest[2] = 0x98BADCFE; digest[3] = 0x10325476; digest[4] = 0xC3D2E1F0; } } /* * SHA-1 using SSE2 * (C) 2009-2011 Jack Lloyd * * Distributed under the terms of the Botan license * * Based on public domain code by Dean Gaudet * (http://arctic.org/~dean/crypto/sha1.html) */ #include <emmintrin.h> namespace Botan { namespace SHA1_SSE2_F { namespace { /* * First 16 bytes just need byte swapping. Preparing just means * adding in the round constants. */ #define prep00_15(P, W) \ do { \ W = _mm_shufflehi_epi16(W, _MM_SHUFFLE(2, 3, 0, 1)); \ W = _mm_shufflelo_epi16(W, _MM_SHUFFLE(2, 3, 0, 1)); \ W = _mm_or_si128(_mm_slli_epi16(W, 8), \ _mm_srli_epi16(W, 8)); \ P.u128 = _mm_add_epi32(W, K00_19); \ } while(0) /* For each multiple of 4, t, we want to calculate this: W[t+0] = rol(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16], 1); W[t+1] = rol(W[t-2] ^ W[t-7] ^ W[t-13] ^ W[t-15], 1); W[t+2] = rol(W[t-1] ^ W[t-6] ^ W[t-12] ^ W[t-14], 1); W[t+3] = rol(W[t] ^ W[t-5] ^ W[t-11] ^ W[t-13], 1); we'll actually calculate this: W[t+0] = rol(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16], 1); W[t+1] = rol(W[t-2] ^ W[t-7] ^ W[t-13] ^ W[t-15], 1); W[t+2] = rol(W[t-1] ^ W[t-6] ^ W[t-12] ^ W[t-14], 1); W[t+3] = rol( 0 ^ W[t-5] ^ W[t-11] ^ W[t-13], 1); W[t+3] ^= rol(W[t+0], 1); the parameters are: W0 = &W[t-16]; W1 = &W[t-12]; W2 = &W[t- 8]; W3 = &W[t- 4]; and on output: prepared = W0 + K W0 = W[t]..W[t+3] */ /* note that there is a step here where i want to do a rol by 1, which * normally would look like this: * * r1 = psrld r0,$31 * r0 = pslld r0,$1 * r0 = por r0,r1 * * but instead i do this: * * r1 = pcmpltd r0,zero * r0 = paddd r0,r0 * r0 = psub r0,r1 * * because pcmpltd and paddd are availabe in both MMX units on * efficeon, pentium-m, and opteron but shifts are available in * only one unit. */ #define prep(prep, XW0, XW1, XW2, XW3, K) \ do { \ __m128i r0, r1, r2, r3; \ \ /* load W[t-4] 16-byte aligned, and shift */ \ r3 = _mm_srli_si128((XW3), 4); \ r0 = (XW0); \ /* get high 64-bits of XW0 into low 64-bits */ \ r1 = _mm_shuffle_epi32((XW0), _MM_SHUFFLE(1,0,3,2)); \ /* load high 64-bits of r1 */ \ r1 = _mm_unpacklo_epi64(r1, (XW1)); \ r2 = (XW2); \ \ r0 = _mm_xor_si128(r1, r0); \ r2 = _mm_xor_si128(r3, r2); \ r0 = _mm_xor_si128(r2, r0); \ /* unrotated W[t]..W[t+2] in r0 ... still need W[t+3] */ \ \ r2 = _mm_slli_si128(r0, 12); \ r1 = _mm_cmplt_epi32(r0, _mm_setzero_si128()); \ r0 = _mm_add_epi32(r0, r0); /* shift left by 1 */ \ r0 = _mm_sub_epi32(r0, r1); /* r0 has W[t]..W[t+2] */ \ \ r3 = _mm_srli_epi32(r2, 30); \ r2 = _mm_slli_epi32(r2, 2); \ \ r0 = _mm_xor_si128(r0, r3); \ r0 = _mm_xor_si128(r0, r2); /* r0 now has W[t+3] */ \ \ (XW0) = r0; \ (prep).u128 = _mm_add_epi32(r0, K); \ } while(0) /* * SHA-160 F1 Function */ inline void F1(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, u32bit msg) { E += (D ^ (B & (C ^ D))) + msg + rotate_left(A, 5); B = rotate_left(B, 30); } /* * SHA-160 F2 Function */ inline void F2(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, u32bit msg) { E += (B ^ C ^ D) + msg + rotate_left(A, 5); B = rotate_left(B, 30); } /* * SHA-160 F3 Function */ inline void F3(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, u32bit msg) { E += ((B & C) | ((B | C) & D)) + msg + rotate_left(A, 5); B = rotate_left(B, 30); } /* * SHA-160 F4 Function */ inline void F4(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, u32bit msg) { E += (B ^ C ^ D) + msg + rotate_left(A, 5); B = rotate_left(B, 30); } } } /* * SHA-160 Compression Function using SSE for message expansion */ void SHA_160_SSE2::compress_n(const byte input_bytes[], size_t blocks) { using namespace SHA1_SSE2_F; const __m128i K00_19 = _mm_set1_epi32(0x5A827999); const __m128i K20_39 = _mm_set1_epi32(0x6ED9EBA1); const __m128i K40_59 = _mm_set1_epi32(0x8F1BBCDC); const __m128i K60_79 = _mm_set1_epi32(0xCA62C1D6); u32bit A = digest[0], B = digest[1], C = digest[2], D = digest[3], E = digest[4]; const __m128i* input = reinterpret_cast<const __m128i*>(input_bytes); for(size_t i = 0; i != blocks; ++i) { union v4si { u32bit u32[4]; __m128i u128; }; v4si P0, P1, P2, P3; __m128i W0 = _mm_loadu_si128(&input[0]); prep00_15(P0, W0); __m128i W1 = _mm_loadu_si128(&input[1]); prep00_15(P1, W1); __m128i W2 = _mm_loadu_si128(&input[2]); prep00_15(P2, W2); __m128i W3 = _mm_loadu_si128(&input[3]); prep00_15(P3, W3); /* Using SSE4; slower on Core2 and Nehalem #define GET_P_32(P, i) _mm_extract_epi32(P.u128, i) Much slower on all tested platforms #define GET_P_32(P,i) _mm_cvtsi128_si32(_mm_srli_si128(P.u128, i*4)) */ #define GET_P_32(P, i) P.u32[i] F1(A, B, C, D, E, GET_P_32(P0, 0)); F1(E, A, B, C, D, GET_P_32(P0, 1)); F1(D, E, A, B, C, GET_P_32(P0, 2)); F1(C, D, E, A, B, GET_P_32(P0, 3)); prep(P0, W0, W1, W2, W3, K00_19); F1(B, C, D, E, A, GET_P_32(P1, 0)); F1(A, B, C, D, E, GET_P_32(P1, 1)); F1(E, A, B, C, D, GET_P_32(P1, 2)); F1(D, E, A, B, C, GET_P_32(P1, 3)); prep(P1, W1, W2, W3, W0, K20_39); F1(C, D, E, A, B, GET_P_32(P2, 0)); F1(B, C, D, E, A, GET_P_32(P2, 1)); F1(A, B, C, D, E, GET_P_32(P2, 2)); F1(E, A, B, C, D, GET_P_32(P2, 3)); prep(P2, W2, W3, W0, W1, K20_39); F1(D, E, A, B, C, GET_P_32(P3, 0)); F1(C, D, E, A, B, GET_P_32(P3, 1)); F1(B, C, D, E, A, GET_P_32(P3, 2)); F1(A, B, C, D, E, GET_P_32(P3, 3)); prep(P3, W3, W0, W1, W2, K20_39); F1(E, A, B, C, D, GET_P_32(P0, 0)); F1(D, E, A, B, C, GET_P_32(P0, 1)); F1(C, D, E, A, B, GET_P_32(P0, 2)); F1(B, C, D, E, A, GET_P_32(P0, 3)); prep(P0, W0, W1, W2, W3, K20_39); F2(A, B, C, D, E, GET_P_32(P1, 0)); F2(E, A, B, C, D, GET_P_32(P1, 1)); F2(D, E, A, B, C, GET_P_32(P1, 2)); F2(C, D, E, A, B, GET_P_32(P1, 3)); prep(P1, W1, W2, W3, W0, K20_39); F2(B, C, D, E, A, GET_P_32(P2, 0)); F2(A, B, C, D, E, GET_P_32(P2, 1)); F2(E, A, B, C, D, GET_P_32(P2, 2)); F2(D, E, A, B, C, GET_P_32(P2, 3)); prep(P2, W2, W3, W0, W1, K40_59); F2(C, D, E, A, B, GET_P_32(P3, 0)); F2(B, C, D, E, A, GET_P_32(P3, 1)); F2(A, B, C, D, E, GET_P_32(P3, 2)); F2(E, A, B, C, D, GET_P_32(P3, 3)); prep(P3, W3, W0, W1, W2, K40_59); F2(D, E, A, B, C, GET_P_32(P0, 0)); F2(C, D, E, A, B, GET_P_32(P0, 1)); F2(B, C, D, E, A, GET_P_32(P0, 2)); F2(A, B, C, D, E, GET_P_32(P0, 3)); prep(P0, W0, W1, W2, W3, K40_59); F2(E, A, B, C, D, GET_P_32(P1, 0)); F2(D, E, A, B, C, GET_P_32(P1, 1)); F2(C, D, E, A, B, GET_P_32(P1, 2)); F2(B, C, D, E, A, GET_P_32(P1, 3)); prep(P1, W1, W2, W3, W0, K40_59); F3(A, B, C, D, E, GET_P_32(P2, 0)); F3(E, A, B, C, D, GET_P_32(P2, 1)); F3(D, E, A, B, C, GET_P_32(P2, 2)); F3(C, D, E, A, B, GET_P_32(P2, 3)); prep(P2, W2, W3, W0, W1, K40_59); F3(B, C, D, E, A, GET_P_32(P3, 0)); F3(A, B, C, D, E, GET_P_32(P3, 1)); F3(E, A, B, C, D, GET_P_32(P3, 2)); F3(D, E, A, B, C, GET_P_32(P3, 3)); prep(P3, W3, W0, W1, W2, K60_79); F3(C, D, E, A, B, GET_P_32(P0, 0)); F3(B, C, D, E, A, GET_P_32(P0, 1)); F3(A, B, C, D, E, GET_P_32(P0, 2)); F3(E, A, B, C, D, GET_P_32(P0, 3)); prep(P0, W0, W1, W2, W3, K60_79); F3(D, E, A, B, C, GET_P_32(P1, 0)); F3(C, D, E, A, B, GET_P_32(P1, 1)); F3(B, C, D, E, A, GET_P_32(P1, 2)); F3(A, B, C, D, E, GET_P_32(P1, 3)); prep(P1, W1, W2, W3, W0, K60_79); F3(E, A, B, C, D, GET_P_32(P2, 0)); F3(D, E, A, B, C, GET_P_32(P2, 1)); F3(C, D, E, A, B, GET_P_32(P2, 2)); F3(B, C, D, E, A, GET_P_32(P2, 3)); prep(P2, W2, W3, W0, W1, K60_79); F4(A, B, C, D, E, GET_P_32(P3, 0)); F4(E, A, B, C, D, GET_P_32(P3, 1)); F4(D, E, A, B, C, GET_P_32(P3, 2)); F4(C, D, E, A, B, GET_P_32(P3, 3)); prep(P3, W3, W0, W1, W2, K60_79); F4(B, C, D, E, A, GET_P_32(P0, 0)); F4(A, B, C, D, E, GET_P_32(P0, 1)); F4(E, A, B, C, D, GET_P_32(P0, 2)); F4(D, E, A, B, C, GET_P_32(P0, 3)); F4(C, D, E, A, B, GET_P_32(P1, 0)); F4(B, C, D, E, A, GET_P_32(P1, 1)); F4(A, B, C, D, E, GET_P_32(P1, 2)); F4(E, A, B, C, D, GET_P_32(P1, 3)); F4(D, E, A, B, C, GET_P_32(P2, 0)); F4(C, D, E, A, B, GET_P_32(P2, 1)); F4(B, C, D, E, A, GET_P_32(P2, 2)); F4(A, B, C, D, E, GET_P_32(P2, 3)); F4(E, A, B, C, D, GET_P_32(P3, 0)); F4(D, E, A, B, C, GET_P_32(P3, 1)); F4(C, D, E, A, B, GET_P_32(P3, 2)); F4(B, C, D, E, A, GET_P_32(P3, 3)); A = (digest[0] += A); B = (digest[1] += B); C = (digest[2] += C); D = (digest[3] += D); E = (digest[4] += E); input += (hash_block_size() / 16); } #undef GET_P_32 } #undef prep00_15 #undef prep } /* * SHA-{224,256} * (C) 1999-2010 Jack Lloyd * 2007 FlexSecure GmbH * * Distributed under the terms of the Botan license */ namespace Botan { namespace { namespace SHA2_32 { /* * SHA-256 Rho Function */ inline u32bit rho(u32bit X, u32bit rot1, u32bit rot2, u32bit rot3) { return (rotate_right(X, rot1) ^ rotate_right(X, rot2) ^ rotate_right(X, rot3)); } /* * SHA-256 Sigma Function */ inline u32bit sigma(u32bit X, u32bit rot1, u32bit rot2, u32bit shift) { return (rotate_right(X, rot1) ^ rotate_right(X, rot2) ^ (X >> shift)); } /* * SHA-256 F1 Function * * Use a macro as many compilers won't inline a function this big, * even though it is much faster if inlined. */ #define SHA2_32_F(A, B, C, D, E, F, G, H, M1, M2, M3, M4, magic) \ do { \ H += magic + rho(E, 6, 11, 25) + ((E & F) ^ (~E & G)) + M1; \ D += H; \ H += rho(A, 2, 13, 22) + ((A & B) | ((A | B) & C)); \ M1 += sigma(M2, 17, 19, 10) + M3 + sigma(M4, 7, 18, 3); \ } while(0); /* * SHA-224 / SHA-256 compression function */ void compress(MemoryRegion<u32bit>& digest, const byte input[], size_t blocks) { u32bit A = digest[0], B = digest[1], C = digest[2], D = digest[3], E = digest[4], F = digest[5], G = digest[6], H = digest[7]; for(size_t i = 0; i != blocks; ++i) { u32bit W00 = load_be<u32bit>(input, 0); u32bit W01 = load_be<u32bit>(input, 1); u32bit W02 = load_be<u32bit>(input, 2); u32bit W03 = load_be<u32bit>(input, 3); u32bit W04 = load_be<u32bit>(input, 4); u32bit W05 = load_be<u32bit>(input, 5); u32bit W06 = load_be<u32bit>(input, 6); u32bit W07 = load_be<u32bit>(input, 7); u32bit W08 = load_be<u32bit>(input, 8); u32bit W09 = load_be<u32bit>(input, 9); u32bit W10 = load_be<u32bit>(input, 10); u32bit W11 = load_be<u32bit>(input, 11); u32bit W12 = load_be<u32bit>(input, 12); u32bit W13 = load_be<u32bit>(input, 13); u32bit W14 = load_be<u32bit>(input, 14); u32bit W15 = load_be<u32bit>(input, 15); SHA2_32_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x428A2F98); SHA2_32_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x71374491); SHA2_32_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0xB5C0FBCF); SHA2_32_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0xE9B5DBA5); SHA2_32_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x3956C25B); SHA2_32_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x59F111F1); SHA2_32_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x923F82A4); SHA2_32_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0xAB1C5ED5); SHA2_32_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0xD807AA98); SHA2_32_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0x12835B01); SHA2_32_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0x243185BE); SHA2_32_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0x550C7DC3); SHA2_32_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0x72BE5D74); SHA2_32_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0x80DEB1FE); SHA2_32_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0x9BDC06A7); SHA2_32_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0xC19BF174); SHA2_32_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0xE49B69C1); SHA2_32_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0xEFBE4786); SHA2_32_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x0FC19DC6); SHA2_32_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x240CA1CC); SHA2_32_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x2DE92C6F); SHA2_32_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x4A7484AA); SHA2_32_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x5CB0A9DC); SHA2_32_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x76F988DA); SHA2_32_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0x983E5152); SHA2_32_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0xA831C66D); SHA2_32_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0xB00327C8); SHA2_32_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0xBF597FC7); SHA2_32_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0xC6E00BF3); SHA2_32_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xD5A79147); SHA2_32_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0x06CA6351); SHA2_32_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0x14292967); SHA2_32_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x27B70A85); SHA2_32_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x2E1B2138); SHA2_32_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x4D2C6DFC); SHA2_32_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x53380D13); SHA2_32_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x650A7354); SHA2_32_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x766A0ABB); SHA2_32_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x81C2C92E); SHA2_32_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x92722C85); SHA2_32_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0xA2BFE8A1); SHA2_32_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0xA81A664B); SHA2_32_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0xC24B8B70); SHA2_32_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0xC76C51A3); SHA2_32_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0xD192E819); SHA2_32_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xD6990624); SHA2_32_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0xF40E3585); SHA2_32_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0x106AA070); SHA2_32_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x19A4C116); SHA2_32_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x1E376C08); SHA2_32_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x2748774C); SHA2_32_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x34B0BCB5); SHA2_32_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x391C0CB3); SHA2_32_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x4ED8AA4A); SHA2_32_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x5B9CCA4F); SHA2_32_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x682E6FF3); SHA2_32_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0x748F82EE); SHA2_32_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0x78A5636F); SHA2_32_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0x84C87814); SHA2_32_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0x8CC70208); SHA2_32_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0x90BEFFFA); SHA2_32_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xA4506CEB); SHA2_32_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0xBEF9A3F7); SHA2_32_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0xC67178F2); A = (digest[0] += A); B = (digest[1] += B); C = (digest[2] += C); D = (digest[3] += D); E = (digest[4] += E); F = (digest[5] += F); G = (digest[6] += G); H = (digest[7] += H); input += 64; } } } } /* * SHA-224 compression function */ void SHA_224::compress_n(const byte input[], size_t blocks) { SHA2_32::compress(digest, input, blocks); } /* * Copy out the digest */ void SHA_224::copy_out(byte output[]) { for(size_t i = 0; i != output_length(); i += 4) store_be(digest[i/4], output + i); } /* * Clear memory of sensitive data */ void SHA_224::clear() { MDx_HashFunction::clear(); digest[0] = 0xC1059ED8; digest[1] = 0x367CD507; digest[2] = 0x3070DD17; digest[3] = 0xF70E5939; digest[4] = 0xFFC00B31; digest[5] = 0x68581511; digest[6] = 0x64F98FA7; digest[7] = 0xBEFA4FA4; } /* * SHA-256 compression function */ void SHA_256::compress_n(const byte input[], size_t blocks) { SHA2_32::compress(digest, input, blocks); } /* * Copy out the digest */ void SHA_256::copy_out(byte output[]) { for(size_t i = 0; i != output_length(); i += 4) store_be(digest[i/4], output + i); } /* * Clear memory of sensitive data */ void SHA_256::clear() { MDx_HashFunction::clear(); digest[0] = 0x6A09E667; digest[1] = 0xBB67AE85; digest[2] = 0x3C6EF372; digest[3] = 0xA54FF53A; digest[4] = 0x510E527F; digest[5] = 0x9B05688C; digest[6] = 0x1F83D9AB; digest[7] = 0x5BE0CD19; } } /* * SHA-{384,512} * (C) 1999-2011 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { namespace SHA2_64 { /* * SHA-{384,512} Rho Function */ inline u64bit rho(u64bit X, u32bit rot1, u32bit rot2, u32bit rot3) { return (rotate_right(X, rot1) ^ rotate_right(X, rot2) ^ rotate_right(X, rot3)); } /* * SHA-{384,512} Sigma Function */ inline u64bit sigma(u64bit X, u32bit rot1, u32bit rot2, u32bit shift) { return (rotate_right(X, rot1) ^ rotate_right(X, rot2) ^ (X >> shift)); } /* * SHA-512 F1 Function * * Use a macro as many compilers won't inline a function this big, * even though it is much faster if inlined. */ #define SHA2_64_F(A, B, C, D, E, F, G, H, M1, M2, M3, M4, magic) \ do { \ H += magic + rho(E, 14, 18, 41) + ((E & F) ^ (~E & G)) + M1; \ D += H; \ H += rho(A, 28, 34, 39) + ((A & B) | ((A | B) & C)); \ M1 += sigma(M2, 19, 61, 6) + M3 + sigma(M4, 1, 8, 7); \ } while(0); /* * SHA-{384,512} Compression Function */ void compress(MemoryRegion<u64bit>& digest, const byte input[], size_t blocks) { u64bit A = digest[0], B = digest[1], C = digest[2], D = digest[3], E = digest[4], F = digest[5], G = digest[6], H = digest[7]; for(size_t i = 0; i != blocks; ++i) { u64bit W00 = load_be<u64bit>(input, 0); u64bit W01 = load_be<u64bit>(input, 1); u64bit W02 = load_be<u64bit>(input, 2); u64bit W03 = load_be<u64bit>(input, 3); u64bit W04 = load_be<u64bit>(input, 4); u64bit W05 = load_be<u64bit>(input, 5); u64bit W06 = load_be<u64bit>(input, 6); u64bit W07 = load_be<u64bit>(input, 7); u64bit W08 = load_be<u64bit>(input, 8); u64bit W09 = load_be<u64bit>(input, 9); u64bit W10 = load_be<u64bit>(input, 10); u64bit W11 = load_be<u64bit>(input, 11); u64bit W12 = load_be<u64bit>(input, 12); u64bit W13 = load_be<u64bit>(input, 13); u64bit W14 = load_be<u64bit>(input, 14); u64bit W15 = load_be<u64bit>(input, 15); SHA2_64_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x428A2F98D728AE22); SHA2_64_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x7137449123EF65CD); SHA2_64_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0xB5C0FBCFEC4D3B2F); SHA2_64_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0xE9B5DBA58189DBBC); SHA2_64_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x3956C25BF348B538); SHA2_64_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x59F111F1B605D019); SHA2_64_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x923F82A4AF194F9B); SHA2_64_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0xAB1C5ED5DA6D8118); SHA2_64_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0xD807AA98A3030242); SHA2_64_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0x12835B0145706FBE); SHA2_64_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0x243185BE4EE4B28C); SHA2_64_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0x550C7DC3D5FFB4E2); SHA2_64_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0x72BE5D74F27B896F); SHA2_64_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0x80DEB1FE3B1696B1); SHA2_64_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0x9BDC06A725C71235); SHA2_64_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0xC19BF174CF692694); SHA2_64_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0xE49B69C19EF14AD2); SHA2_64_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0xEFBE4786384F25E3); SHA2_64_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x0FC19DC68B8CD5B5); SHA2_64_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x240CA1CC77AC9C65); SHA2_64_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x2DE92C6F592B0275); SHA2_64_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x4A7484AA6EA6E483); SHA2_64_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x5CB0A9DCBD41FBD4); SHA2_64_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x76F988DA831153B5); SHA2_64_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0x983E5152EE66DFAB); SHA2_64_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0xA831C66D2DB43210); SHA2_64_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0xB00327C898FB213F); SHA2_64_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0xBF597FC7BEEF0EE4); SHA2_64_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0xC6E00BF33DA88FC2); SHA2_64_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xD5A79147930AA725); SHA2_64_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0x06CA6351E003826F); SHA2_64_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0x142929670A0E6E70); SHA2_64_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x27B70A8546D22FFC); SHA2_64_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x2E1B21385C26C926); SHA2_64_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x4D2C6DFC5AC42AED); SHA2_64_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x53380D139D95B3DF); SHA2_64_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x650A73548BAF63DE); SHA2_64_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x766A0ABB3C77B2A8); SHA2_64_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x81C2C92E47EDAEE6); SHA2_64_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x92722C851482353B); SHA2_64_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0xA2BFE8A14CF10364); SHA2_64_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0xA81A664BBC423001); SHA2_64_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0xC24B8B70D0F89791); SHA2_64_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0xC76C51A30654BE30); SHA2_64_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0xD192E819D6EF5218); SHA2_64_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xD69906245565A910); SHA2_64_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0xF40E35855771202A); SHA2_64_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0x106AA07032BBD1B8); SHA2_64_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x19A4C116B8D2D0C8); SHA2_64_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x1E376C085141AB53); SHA2_64_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x2748774CDF8EEB99); SHA2_64_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x34B0BCB5E19B48A8); SHA2_64_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x391C0CB3C5C95A63); SHA2_64_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x4ED8AA4AE3418ACB); SHA2_64_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x5B9CCA4F7763E373); SHA2_64_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x682E6FF3D6B2B8A3); SHA2_64_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0x748F82EE5DEFB2FC); SHA2_64_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0x78A5636F43172F60); SHA2_64_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0x84C87814A1F0AB72); SHA2_64_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0x8CC702081A6439EC); SHA2_64_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0x90BEFFFA23631E28); SHA2_64_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xA4506CEBDE82BDE9); SHA2_64_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0xBEF9A3F7B2C67915); SHA2_64_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0xC67178F2E372532B); SHA2_64_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0xCA273ECEEA26619C); SHA2_64_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0xD186B8C721C0C207); SHA2_64_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0xEADA7DD6CDE0EB1E); SHA2_64_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0xF57D4F7FEE6ED178); SHA2_64_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x06F067AA72176FBA); SHA2_64_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x0A637DC5A2C898A6); SHA2_64_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x113F9804BEF90DAE); SHA2_64_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x1B710B35131C471B); SHA2_64_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0x28DB77F523047D84); SHA2_64_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0x32CAAB7B40C72493); SHA2_64_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0x3C9EBE0A15C9BEBC); SHA2_64_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0x431D67C49C100D4C); SHA2_64_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0x4CC5D4BECB3E42B6); SHA2_64_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0x597F299CFC657E2A); SHA2_64_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0x5FCB6FAB3AD6FAEC); SHA2_64_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0x6C44198C4A475817); A = (digest[0] += A); B = (digest[1] += B); C = (digest[2] += C); D = (digest[3] += D); E = (digest[4] += E); F = (digest[5] += F); G = (digest[6] += G); H = (digest[7] += H); input += 128; } } } } /* * SHA-384 compression function */ void SHA_384::compress_n(const byte input[], size_t blocks) { SHA2_64::compress(digest, input, blocks); } /* * Copy out the digest */ void SHA_384::copy_out(byte output[]) { for(size_t i = 0; i != output_length(); i += 8) store_be(digest[i/8], output + i); } /* * Clear memory of sensitive data */ void SHA_384::clear() { MDx_HashFunction::clear(); digest[0] = 0xCBBB9D5DC1059ED8; digest[1] = 0x629A292A367CD507; digest[2] = 0x9159015A3070DD17; digest[3] = 0x152FECD8F70E5939; digest[4] = 0x67332667FFC00B31; digest[5] = 0x8EB44A8768581511; digest[6] = 0xDB0C2E0D64F98FA7; digest[7] = 0x47B5481DBEFA4FA4; } /* * SHA-512 compression function */ void SHA_512::compress_n(const byte input[], size_t blocks) { SHA2_64::compress(digest, input, blocks); } /* * Copy out the digest */ void SHA_512::copy_out(byte output[]) { for(size_t i = 0; i != output_length(); i += 8) store_be(digest[i/8], output + i); } /* * Clear memory of sensitive data */ void SHA_512::clear() { MDx_HashFunction::clear(); digest[0] = 0x6A09E667F3BCC908; digest[1] = 0xBB67AE8584CAA73B; digest[2] = 0x3C6EF372FE94F82B; digest[3] = 0xA54FF53A5F1D36F1; digest[4] = 0x510E527FADE682D1; digest[5] = 0x9B05688C2B3E6C1F; digest[6] = 0x1F83D9ABFB41BD6B; digest[7] = 0x5BE0CD19137E2179; } } /* * The Skein-512 hash function * (C) 2009-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> namespace Botan { namespace { enum type_code { SKEIN_KEY = 0, SKEIN_CONFIG = 4, SKEIN_PERSONALIZATION = 8, SKEIN_PUBLIC_KEY = 12, SKEIN_KEY_IDENTIFIER = 16, SKEIN_NONCE = 20, SKEIN_MSG = 48, SKEIN_OUTPUT = 63 }; void ubi_512(MemoryRegion<u64bit>& H, MemoryRegion<u64bit>& T, const byte msg[], size_t msg_len) { do { const size_t to_proc = std::min<size_t>(msg_len, 64); T[0] += to_proc; u64bit M[8] = { 0 }; load_le(M, msg, to_proc / 8); if(to_proc % 8) { for(size_t j = 0; j != to_proc % 8; ++j) M[to_proc/8] |= static_cast<u64bit>(msg[8*(to_proc/8)+j]) << (8*j); } H[8] = H[0] ^ H[1] ^ H[2] ^ H[3] ^ H[4] ^ H[5] ^ H[6] ^ H[7] ^ 0x1BD11BDAA9FC1A22; T[2] = T[0] ^ T[1]; u64bit X0 = M[0] + H[0]; u64bit X1 = M[1] + H[1]; u64bit X2 = M[2] + H[2]; u64bit X3 = M[3] + H[3]; u64bit X4 = M[4] + H[4]; u64bit X5 = M[5] + H[5] + T[0]; u64bit X6 = M[6] + H[6] + T[1]; u64bit X7 = M[7] + H[7]; #define THREEFISH_ROUND(I1,I2,I3,I4,I5,I6,I7,I8,ROT1,ROT2,ROT3,ROT4) \ do { \ X##I1 += X##I2; X##I2 = rotate_left(X##I2, ROT1) ^ X##I1; \ X##I3 += X##I4; X##I4 = rotate_left(X##I4, ROT2) ^ X##I3; \ X##I5 += X##I6; X##I6 = rotate_left(X##I6, ROT3) ^ X##I5; \ X##I7 += X##I8; X##I8 = rotate_left(X##I8, ROT4) ^ X##I7; \ } while(0); #define THREEFISH_INJECT_KEY(r) \ do { \ X0 += H[(r ) % 9]; \ X1 += H[(r+1) % 9]; \ X2 += H[(r+2) % 9]; \ X3 += H[(r+3) % 9]; \ X4 += H[(r+4) % 9]; \ X5 += H[(r+5) % 9] + T[(r ) % 3]; \ X6 += H[(r+6) % 9] + T[(r+1) % 3]; \ X7 += H[(r+7) % 9] + (r); \ } while(0); #define THREEFISH_8_ROUNDS(R1,R2) \ do { \ THREEFISH_ROUND(0,1,2,3,4,5,6,7, 46,36,19,37); \ THREEFISH_ROUND(2,1,4,7,6,5,0,3, 33,27,14,42); \ THREEFISH_ROUND(4,1,6,3,0,5,2,7, 17,49,36,39); \ THREEFISH_ROUND(6,1,0,7,2,5,4,3, 44, 9,54,56); \ \ THREEFISH_INJECT_KEY(R1); \ \ THREEFISH_ROUND(0,1,2,3,4,5,6,7, 39,30,34,24); \ THREEFISH_ROUND(2,1,4,7,6,5,0,3, 13,50,10,17); \ THREEFISH_ROUND(4,1,6,3,0,5,2,7, 25,29,39,43); \ THREEFISH_ROUND(6,1,0,7,2,5,4,3, 8,35,56,22); \ \ THREEFISH_INJECT_KEY(R2); \ } while(0); THREEFISH_8_ROUNDS(1,2); THREEFISH_8_ROUNDS(3,4); THREEFISH_8_ROUNDS(5,6); THREEFISH_8_ROUNDS(7,8); THREEFISH_8_ROUNDS(9,10); THREEFISH_8_ROUNDS(11,12); THREEFISH_8_ROUNDS(13,14); THREEFISH_8_ROUNDS(15,16); THREEFISH_8_ROUNDS(17,18); // message feed forward H[0] = X0 ^ M[0]; H[1] = X1 ^ M[1]; H[2] = X2 ^ M[2]; H[3] = X3 ^ M[3]; H[4] = X4 ^ M[4]; H[5] = X5 ^ M[5]; H[6] = X6 ^ M[6]; H[7] = X7 ^ M[7]; // clear first flag if set T[1] &= ~(static_cast<u64bit>(1) << 62); msg_len -= to_proc; msg += to_proc; } while(msg_len); } void reset_tweak(MemoryRegion<u64bit>& T, type_code type, bool final) { T[0] = 0; T[1] = (static_cast<u64bit>(type) << 56) | (static_cast<u64bit>(1) << 62) | (static_cast<u64bit>(final) << 63); } void initial_block(MemoryRegion<u64bit>& H, MemoryRegion<u64bit>& T, size_t output_bits, const std::string& personalization) { zeroise(H); // ASCII("SHA3") followed by version (0x0001) code byte config_str[32] = { 0x53, 0x48, 0x41, 0x33, 0x01, 0x00, 0 }; store_le(u32bit(output_bits), config_str + 8); reset_tweak(T, SKEIN_CONFIG, true); ubi_512(H, T, config_str, sizeof(config_str)); if(personalization != "") { /* This is a limitation of this implementation, and not of the algorithm specification. Could be fixed relatively easily, but doesn't seem worth the trouble. */ if(personalization.length() > 64) throw Invalid_Argument("Skein personalization must be <= 64 bytes"); const byte* bits = reinterpret_cast<const byte*>(personalization.data()); reset_tweak(T, SKEIN_PERSONALIZATION, true); ubi_512(H, T, bits, personalization.length()); } reset_tweak(T, SKEIN_MSG, false); } } Skein_512::Skein_512(size_t arg_output_bits, const std::string& arg_personalization) : personalization(arg_personalization), output_bits(arg_output_bits), H(9), T(3), buffer(64), buf_pos(0) { if(output_bits == 0 || output_bits % 8 != 0 || output_bits > 64*1024) throw Invalid_Argument("Bad output bits size for Skein-512"); initial_block(H, T, output_bits, personalization); } std::string Skein_512::name() const { if(personalization != "") return "Skein-512(" + to_string(output_bits) + "," + personalization + ")"; return "Skein-512(" + to_string(output_bits) + ")"; } HashFunction* Skein_512::clone() const { return new Skein_512(output_bits, personalization); } void Skein_512::clear() { zeroise(H); zeroise(T); zeroise(buffer); buf_pos = 0; } void Skein_512::add_data(const byte input[], size_t length) { if(length == 0) return; if(buf_pos) { buffer.copy(buf_pos, input, length); if(buf_pos + length > 64) { ubi_512(H, T, &buffer[0], buffer.size()); input += (64 - buf_pos); length -= (64 - buf_pos); buf_pos = 0; } } const size_t full_blocks = (length - 1) / 64; if(full_blocks) ubi_512(H, T, input, 64*full_blocks); length -= full_blocks * 64; buffer.copy(buf_pos, input + full_blocks * 64, length); buf_pos += length; } void Skein_512::final_result(byte out[]) { T[1] |= (static_cast<u64bit>(1) << 63); // final block flag for(size_t i = buf_pos; i != buffer.size(); ++i) buffer[i] = 0; ubi_512(H, T, &buffer[0], buf_pos); byte counter[8] = { 0 }; size_t out_bytes = output_bits / 8; SecureVector<u64bit> H_out(9); while(out_bytes) { const size_t to_proc = std::min<size_t>(out_bytes, 64); H_out.copy(&H[0], 8); reset_tweak(T, SKEIN_OUTPUT, true); ubi_512(H_out, T, counter, sizeof(counter)); for(size_t i = 0; i != to_proc; ++i) out[i] = get_byte(7-i%8, H_out[i/8]); out_bytes -= to_proc; out += to_proc; for(size_t i = 0; i != sizeof(counter); ++i) if(++counter[i]) break; } buf_pos = 0; initial_block(H, T, output_bits, personalization); } } /* * S-Box Tables for Tiger * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { const u64bit Tiger::SBOX1[256] = { 0x02AAB17CF7E90C5E, 0xAC424B03E243A8EC, 0x72CD5BE30DD5FCD3, 0x6D019B93F6F97F3A, 0xCD9978FFD21F9193, 0x7573A1C9708029E2, 0xB164326B922A83C3, 0x46883EEE04915870, 0xEAACE3057103ECE6, 0xC54169B808A3535C, 0x4CE754918DDEC47C, 0x0AA2F4DFDC0DF40C, 0x10B76F18A74DBEFA, 0xC6CCB6235AD1AB6A, 0x13726121572FE2FF, 0x1A488C6F199D921E, 0x4BC9F9F4DA0007CA, 0x26F5E6F6E85241C7, 0x859079DBEA5947B6, 0x4F1885C5C99E8C92, 0xD78E761EA96F864B, 0x8E36428C52B5C17D, 0x69CF6827373063C1, 0xB607C93D9BB4C56E, 0x7D820E760E76B5EA, 0x645C9CC6F07FDC42, 0xBF38A078243342E0, 0x5F6B343C9D2E7D04, 0xF2C28AEB600B0EC6, 0x6C0ED85F7254BCAC, 0x71592281A4DB4FE5, 0x1967FA69CE0FED9F, 0xFD5293F8B96545DB, 0xC879E9D7F2A7600B, 0x860248920193194E, 0xA4F9533B2D9CC0B3, 0x9053836C15957613, 0xDB6DCF8AFC357BF1, 0x18BEEA7A7A370F57, 0x037117CA50B99066, 0x6AB30A9774424A35, 0xF4E92F02E325249B, 0x7739DB07061CCAE1, 0xD8F3B49CECA42A05, 0xBD56BE3F51382F73, 0x45FAED5843B0BB28, 0x1C813D5C11BF1F83, 0x8AF0E4B6D75FA169, 0x33EE18A487AD9999, 0x3C26E8EAB1C94410, 0xB510102BC0A822F9, 0x141EEF310CE6123B, 0xFC65B90059DDB154, 0xE0158640C5E0E607, 0x884E079826C3A3CF, 0x930D0D9523C535FD, 0x35638D754E9A2B00, 0x4085FCCF40469DD5, 0xC4B17AD28BE23A4C, 0xCAB2F0FC6A3E6A2E, 0x2860971A6B943FCD, 0x3DDE6EE212E30446, 0x6222F32AE01765AE, 0x5D550BB5478308FE, 0xA9EFA98DA0EDA22A, 0xC351A71686C40DA7, 0x1105586D9C867C84, 0xDCFFEE85FDA22853, 0xCCFBD0262C5EEF76, 0xBAF294CB8990D201, 0xE69464F52AFAD975, 0x94B013AFDF133E14, 0x06A7D1A32823C958, 0x6F95FE5130F61119, 0xD92AB34E462C06C0, 0xED7BDE33887C71D2, 0x79746D6E6518393E, 0x5BA419385D713329, 0x7C1BA6B948A97564, 0x31987C197BFDAC67, 0xDE6C23C44B053D02, 0x581C49FED002D64D, 0xDD474D6338261571, 0xAA4546C3E473D062, 0x928FCE349455F860, 0x48161BBACAAB94D9, 0x63912430770E6F68, 0x6EC8A5E602C6641C, 0x87282515337DDD2B, 0x2CDA6B42034B701B, 0xB03D37C181CB096D, 0xE108438266C71C6F, 0x2B3180C7EB51B255, 0xDF92B82F96C08BBC, 0x5C68C8C0A632F3BA, 0x5504CC861C3D0556, 0xABBFA4E55FB26B8F, 0x41848B0AB3BACEB4, 0xB334A273AA445D32, 0xBCA696F0A85AD881, 0x24F6EC65B528D56C, 0x0CE1512E90F4524A, 0x4E9DD79D5506D35A, 0x258905FAC6CE9779, 0x2019295B3E109B33, 0xF8A9478B73A054CC, 0x2924F2F934417EB0, 0x3993357D536D1BC4, 0x38A81AC21DB6FF8B, 0x47C4FBF17D6016BF, 0x1E0FAADD7667E3F5, 0x7ABCFF62938BEB96, 0xA78DAD948FC179C9, 0x8F1F98B72911E50D, 0x61E48EAE27121A91, 0x4D62F7AD31859808, 0xECEBA345EF5CEAEB, 0xF5CEB25EBC9684CE, 0xF633E20CB7F76221, 0xA32CDF06AB8293E4, 0x985A202CA5EE2CA4, 0xCF0B8447CC8A8FB1, 0x9F765244979859A3, 0xA8D516B1A1240017, 0x0BD7BA3EBB5DC726, 0xE54BCA55B86ADB39, 0x1D7A3AFD6C478063, 0x519EC608E7669EDD, 0x0E5715A2D149AA23, 0x177D4571848FF194, 0xEEB55F3241014C22, 0x0F5E5CA13A6E2EC2, 0x8029927B75F5C361, 0xAD139FABC3D6E436, 0x0D5DF1A94CCF402F, 0x3E8BD948BEA5DFC8, 0xA5A0D357BD3FF77E, 0xA2D12E251F74F645, 0x66FD9E525E81A082, 0x2E0C90CE7F687A49, 0xC2E8BCBEBA973BC5, 0x000001BCE509745F, 0x423777BBE6DAB3D6, 0xD1661C7EAEF06EB5, 0xA1781F354DAACFD8, 0x2D11284A2B16AFFC, 0xF1FC4F67FA891D1F, 0x73ECC25DCB920ADA, 0xAE610C22C2A12651, 0x96E0A810D356B78A, 0x5A9A381F2FE7870F, 0xD5AD62EDE94E5530, 0xD225E5E8368D1427, 0x65977B70C7AF4631, 0x99F889B2DE39D74F, 0x233F30BF54E1D143, 0x9A9675D3D9A63C97, 0x5470554FF334F9A8, 0x166ACB744A4F5688, 0x70C74CAAB2E4AEAD, 0xF0D091646F294D12, 0x57B82A89684031D1, 0xEFD95A5A61BE0B6B, 0x2FBD12E969F2F29A, 0x9BD37013FEFF9FE8, 0x3F9B0404D6085A06, 0x4940C1F3166CFE15, 0x09542C4DCDF3DEFB, 0xB4C5218385CD5CE3, 0xC935B7DC4462A641, 0x3417F8A68ED3B63F, 0xB80959295B215B40, 0xF99CDAEF3B8C8572, 0x018C0614F8FCB95D, 0x1B14ACCD1A3ACDF3, 0x84D471F200BB732D, 0xC1A3110E95E8DA16, 0x430A7220BF1A82B8, 0xB77E090D39DF210E, 0x5EF4BD9F3CD05E9D, 0x9D4FF6DA7E57A444, 0xDA1D60E183D4A5F8, 0xB287C38417998E47, 0xFE3EDC121BB31886, 0xC7FE3CCC980CCBEF, 0xE46FB590189BFD03, 0x3732FD469A4C57DC, 0x7EF700A07CF1AD65, 0x59C64468A31D8859, 0x762FB0B4D45B61F6, 0x155BAED099047718, 0x68755E4C3D50BAA6, 0xE9214E7F22D8B4DF, 0x2ADDBF532EAC95F4, 0x32AE3909B4BD0109, 0x834DF537B08E3450, 0xFA209DA84220728D, 0x9E691D9B9EFE23F7, 0x0446D288C4AE8D7F, 0x7B4CC524E169785B, 0x21D87F0135CA1385, 0xCEBB400F137B8AA5, 0x272E2B66580796BE, 0x3612264125C2B0DE, 0x057702BDAD1EFBB2, 0xD4BABB8EACF84BE9, 0x91583139641BC67B, 0x8BDC2DE08036E024, 0x603C8156F49F68ED, 0xF7D236F7DBEF5111, 0x9727C4598AD21E80, 0xA08A0896670A5FD7, 0xCB4A8F4309EBA9CB, 0x81AF564B0F7036A1, 0xC0B99AA778199ABD, 0x959F1EC83FC8E952, 0x8C505077794A81B9, 0x3ACAAF8F056338F0, 0x07B43F50627A6778, 0x4A44AB49F5ECCC77, 0x3BC3D6E4B679EE98, 0x9CC0D4D1CF14108C, 0x4406C00B206BC8A0, 0x82A18854C8D72D89, 0x67E366B35C3C432C, 0xB923DD61102B37F2, 0x56AB2779D884271D, 0xBE83E1B0FF1525AF, 0xFB7C65D4217E49A9, 0x6BDBE0E76D48E7D4, 0x08DF828745D9179E, 0x22EA6A9ADD53BD34, 0xE36E141C5622200A, 0x7F805D1B8CB750EE, 0xAFE5C7A59F58E837, 0xE27F996A4FB1C23C, 0xD3867DFB0775F0D0, 0xD0E673DE6E88891A, 0x123AEB9EAFB86C25, 0x30F1D5D5C145B895, 0xBB434A2DEE7269E7, 0x78CB67ECF931FA38, 0xF33B0372323BBF9C, 0x52D66336FB279C74, 0x505F33AC0AFB4EAA, 0xE8A5CD99A2CCE187, 0x534974801E2D30BB, 0x8D2D5711D5876D90, 0x1F1A412891BC038E, 0xD6E2E71D82E56648, 0x74036C3A497732B7, 0x89B67ED96361F5AB, 0xFFED95D8F1EA02A2, 0xE72B3BD61464D43D, 0xA6300F170BDC4820, 0xEBC18760ED78A77A }; const u64bit Tiger::SBOX2[256] = { 0xE6A6BE5A05A12138, 0xB5A122A5B4F87C98, 0x563C6089140B6990, 0x4C46CB2E391F5DD5, 0xD932ADDBC9B79434, 0x08EA70E42015AFF5, 0xD765A6673E478CF1, 0xC4FB757EAB278D99, 0xDF11C6862D6E0692, 0xDDEB84F10D7F3B16, 0x6F2EF604A665EA04, 0x4A8E0F0FF0E0DFB3, 0xA5EDEEF83DBCBA51, 0xFC4F0A2A0EA4371E, 0xE83E1DA85CB38429, 0xDC8FF882BA1B1CE2, 0xCD45505E8353E80D, 0x18D19A00D4DB0717, 0x34A0CFEDA5F38101, 0x0BE77E518887CAF2, 0x1E341438B3C45136, 0xE05797F49089CCF9, 0xFFD23F9DF2591D14, 0x543DDA228595C5CD, 0x661F81FD99052A33, 0x8736E641DB0F7B76, 0x15227725418E5307, 0xE25F7F46162EB2FA, 0x48A8B2126C13D9FE, 0xAFDC541792E76EEA, 0x03D912BFC6D1898F, 0x31B1AAFA1B83F51B, 0xF1AC2796E42AB7D9, 0x40A3A7D7FCD2EBAC, 0x1056136D0AFBBCC5, 0x7889E1DD9A6D0C85, 0xD33525782A7974AA, 0xA7E25D09078AC09B, 0xBD4138B3EAC6EDD0, 0x920ABFBE71EB9E70, 0xA2A5D0F54FC2625C, 0xC054E36B0B1290A3, 0xF6DD59FF62FE932B, 0x3537354511A8AC7D, 0xCA845E9172FADCD4, 0x84F82B60329D20DC, 0x79C62CE1CD672F18, 0x8B09A2ADD124642C, 0xD0C1E96A19D9E726, 0x5A786A9B4BA9500C, 0x0E020336634C43F3, 0xC17B474AEB66D822, 0x6A731AE3EC9BAAC2, 0x8226667AE0840258, 0x67D4567691CAECA5, 0x1D94155C4875ADB5, 0x6D00FD985B813FDF, 0x51286EFCB774CD06, 0x5E8834471FA744AF, 0xF72CA0AEE761AE2E, 0xBE40E4CDAEE8E09A, 0xE9970BBB5118F665, 0x726E4BEB33DF1964, 0x703B000729199762, 0x4631D816F5EF30A7, 0xB880B5B51504A6BE, 0x641793C37ED84B6C, 0x7B21ED77F6E97D96, 0x776306312EF96B73, 0xAE528948E86FF3F4, 0x53DBD7F286A3F8F8, 0x16CADCE74CFC1063, 0x005C19BDFA52C6DD, 0x68868F5D64D46AD3, 0x3A9D512CCF1E186A, 0x367E62C2385660AE, 0xE359E7EA77DCB1D7, 0x526C0773749ABE6E, 0x735AE5F9D09F734B, 0x493FC7CC8A558BA8, 0xB0B9C1533041AB45, 0x321958BA470A59BD, 0x852DB00B5F46C393, 0x91209B2BD336B0E5, 0x6E604F7D659EF19F, 0xB99A8AE2782CCB24, 0xCCF52AB6C814C4C7, 0x4727D9AFBE11727B, 0x7E950D0C0121B34D, 0x756F435670AD471F, 0xF5ADD442615A6849, 0x4E87E09980B9957A, 0x2ACFA1DF50AEE355, 0xD898263AFD2FD556, 0xC8F4924DD80C8FD6, 0xCF99CA3D754A173A, 0xFE477BACAF91BF3C, 0xED5371F6D690C12D, 0x831A5C285E687094, 0xC5D3C90A3708A0A4, 0x0F7F903717D06580, 0x19F9BB13B8FDF27F, 0xB1BD6F1B4D502843, 0x1C761BA38FFF4012, 0x0D1530C4E2E21F3B, 0x8943CE69A7372C8A, 0xE5184E11FEB5CE66, 0x618BDB80BD736621, 0x7D29BAD68B574D0B, 0x81BB613E25E6FE5B, 0x071C9C10BC07913F, 0xC7BEEB7909AC2D97, 0xC3E58D353BC5D757, 0xEB017892F38F61E8, 0xD4EFFB9C9B1CC21A, 0x99727D26F494F7AB, 0xA3E063A2956B3E03, 0x9D4A8B9A4AA09C30, 0x3F6AB7D500090FB4, 0x9CC0F2A057268AC0, 0x3DEE9D2DEDBF42D1, 0x330F49C87960A972, 0xC6B2720287421B41, 0x0AC59EC07C00369C, 0xEF4EAC49CB353425, 0xF450244EEF0129D8, 0x8ACC46E5CAF4DEB6, 0x2FFEAB63989263F7, 0x8F7CB9FE5D7A4578, 0x5BD8F7644E634635, 0x427A7315BF2DC900, 0x17D0C4AA2125261C, 0x3992486C93518E50, 0xB4CBFEE0A2D7D4C3, 0x7C75D6202C5DDD8D, 0xDBC295D8E35B6C61, 0x60B369D302032B19, 0xCE42685FDCE44132, 0x06F3DDB9DDF65610, 0x8EA4D21DB5E148F0, 0x20B0FCE62FCD496F, 0x2C1B912358B0EE31, 0xB28317B818F5A308, 0xA89C1E189CA6D2CF, 0x0C6B18576AAADBC8, 0xB65DEAA91299FAE3, 0xFB2B794B7F1027E7, 0x04E4317F443B5BEB, 0x4B852D325939D0A6, 0xD5AE6BEEFB207FFC, 0x309682B281C7D374, 0xBAE309A194C3B475, 0x8CC3F97B13B49F05, 0x98A9422FF8293967, 0x244B16B01076FF7C, 0xF8BF571C663D67EE, 0x1F0D6758EEE30DA1, 0xC9B611D97ADEB9B7, 0xB7AFD5887B6C57A2, 0x6290AE846B984FE1, 0x94DF4CDEACC1A5FD, 0x058A5BD1C5483AFF, 0x63166CC142BA3C37, 0x8DB8526EB2F76F40, 0xE10880036F0D6D4E, 0x9E0523C9971D311D, 0x45EC2824CC7CD691, 0x575B8359E62382C9, 0xFA9E400DC4889995, 0xD1823ECB45721568, 0xDAFD983B8206082F, 0xAA7D29082386A8CB, 0x269FCD4403B87588, 0x1B91F5F728BDD1E0, 0xE4669F39040201F6, 0x7A1D7C218CF04ADE, 0x65623C29D79CE5CE, 0x2368449096C00BB1, 0xAB9BF1879DA503BA, 0xBC23ECB1A458058E, 0x9A58DF01BB401ECC, 0xA070E868A85F143D, 0x4FF188307DF2239E, 0x14D565B41A641183, 0xEE13337452701602, 0x950E3DCF3F285E09, 0x59930254B9C80953, 0x3BF299408930DA6D, 0xA955943F53691387, 0xA15EDECAA9CB8784, 0x29142127352BE9A0, 0x76F0371FFF4E7AFB, 0x0239F450274F2228, 0xBB073AF01D5E868B, 0xBFC80571C10E96C1, 0xD267088568222E23, 0x9671A3D48E80B5B0, 0x55B5D38AE193BB81, 0x693AE2D0A18B04B8, 0x5C48B4ECADD5335F, 0xFD743B194916A1CA, 0x2577018134BE98C4, 0xE77987E83C54A4AD, 0x28E11014DA33E1B9, 0x270CC59E226AA213, 0x71495F756D1A5F60, 0x9BE853FB60AFEF77, 0xADC786A7F7443DBF, 0x0904456173B29A82, 0x58BC7A66C232BD5E, 0xF306558C673AC8B2, 0x41F639C6B6C9772A, 0x216DEFE99FDA35DA, 0x11640CC71C7BE615, 0x93C43694565C5527, 0xEA038E6246777839, 0xF9ABF3CE5A3E2469, 0x741E768D0FD312D2, 0x0144B883CED652C6, 0xC20B5A5BA33F8552, 0x1AE69633C3435A9D, 0x97A28CA4088CFDEC, 0x8824A43C1E96F420, 0x37612FA66EEEA746, 0x6B4CB165F9CF0E5A, 0x43AA1C06A0ABFB4A, 0x7F4DC26FF162796B, 0x6CBACC8E54ED9B0F, 0xA6B7FFEFD2BB253E, 0x2E25BC95B0A29D4F, 0x86D6A58BDEF1388C, 0xDED74AC576B6F054, 0x8030BDBC2B45805D, 0x3C81AF70E94D9289, 0x3EFF6DDA9E3100DB, 0xB38DC39FDFCC8847, 0x123885528D17B87E, 0xF2DA0ED240B1B642, 0x44CEFADCD54BF9A9, 0x1312200E433C7EE6, 0x9FFCC84F3A78C748, 0xF0CD1F72248576BB, 0xEC6974053638CFE4, 0x2BA7B67C0CEC4E4C, 0xAC2F4DF3E5CE32ED, 0xCB33D14326EA4C11, 0xA4E9044CC77E58BC, 0x5F513293D934FCEF, 0x5DC9645506E55444, 0x50DE418F317DE40A, 0x388CB31A69DDE259, 0x2DB4A83455820A86, 0x9010A91E84711AE9, 0x4DF7F0B7B1498371, 0xD62A2EABC0977179, 0x22FAC097AA8D5C0E }; const u64bit Tiger::SBOX3[256] = { 0xF49FCC2FF1DAF39B, 0x487FD5C66FF29281, 0xE8A30667FCDCA83F, 0x2C9B4BE3D2FCCE63, 0xDA3FF74B93FBBBC2, 0x2FA165D2FE70BA66, 0xA103E279970E93D4, 0xBECDEC77B0E45E71, 0xCFB41E723985E497, 0xB70AAA025EF75017, 0xD42309F03840B8E0, 0x8EFC1AD035898579, 0x96C6920BE2B2ABC5, 0x66AF4163375A9172, 0x2174ABDCCA7127FB, 0xB33CCEA64A72FF41, 0xF04A4933083066A5, 0x8D970ACDD7289AF5, 0x8F96E8E031C8C25E, 0xF3FEC02276875D47, 0xEC7BF310056190DD, 0xF5ADB0AEBB0F1491, 0x9B50F8850FD58892, 0x4975488358B74DE8, 0xA3354FF691531C61, 0x0702BBE481D2C6EE, 0x89FB24057DEDED98, 0xAC3075138596E902, 0x1D2D3580172772ED, 0xEB738FC28E6BC30D, 0x5854EF8F63044326, 0x9E5C52325ADD3BBE, 0x90AA53CF325C4623, 0xC1D24D51349DD067, 0x2051CFEEA69EA624, 0x13220F0A862E7E4F, 0xCE39399404E04864, 0xD9C42CA47086FCB7, 0x685AD2238A03E7CC, 0x066484B2AB2FF1DB, 0xFE9D5D70EFBF79EC, 0x5B13B9DD9C481854, 0x15F0D475ED1509AD, 0x0BEBCD060EC79851, 0xD58C6791183AB7F8, 0xD1187C5052F3EEE4, 0xC95D1192E54E82FF, 0x86EEA14CB9AC6CA2, 0x3485BEB153677D5D, 0xDD191D781F8C492A, 0xF60866BAA784EBF9, 0x518F643BA2D08C74, 0x8852E956E1087C22, 0xA768CB8DC410AE8D, 0x38047726BFEC8E1A, 0xA67738B4CD3B45AA, 0xAD16691CEC0DDE19, 0xC6D4319380462E07, 0xC5A5876D0BA61938, 0x16B9FA1FA58FD840, 0x188AB1173CA74F18, 0xABDA2F98C99C021F, 0x3E0580AB134AE816, 0x5F3B05B773645ABB, 0x2501A2BE5575F2F6, 0x1B2F74004E7E8BA9, 0x1CD7580371E8D953, 0x7F6ED89562764E30, 0xB15926FF596F003D, 0x9F65293DA8C5D6B9, 0x6ECEF04DD690F84C, 0x4782275FFF33AF88, 0xE41433083F820801, 0xFD0DFE409A1AF9B5, 0x4325A3342CDB396B, 0x8AE77E62B301B252, 0xC36F9E9F6655615A, 0x85455A2D92D32C09, 0xF2C7DEA949477485, 0x63CFB4C133A39EBA, 0x83B040CC6EBC5462, 0x3B9454C8FDB326B0, 0x56F56A9E87FFD78C, 0x2DC2940D99F42BC6, 0x98F7DF096B096E2D, 0x19A6E01E3AD852BF, 0x42A99CCBDBD4B40B, 0xA59998AF45E9C559, 0x366295E807D93186, 0x6B48181BFAA1F773, 0x1FEC57E2157A0A1D, 0x4667446AF6201AD5, 0xE615EBCACFB0F075, 0xB8F31F4F68290778, 0x22713ED6CE22D11E, 0x3057C1A72EC3C93B, 0xCB46ACC37C3F1F2F, 0xDBB893FD02AAF50E, 0x331FD92E600B9FCF, 0xA498F96148EA3AD6, 0xA8D8426E8B6A83EA, 0xA089B274B7735CDC, 0x87F6B3731E524A11, 0x118808E5CBC96749, 0x9906E4C7B19BD394, 0xAFED7F7E9B24A20C, 0x6509EADEEB3644A7, 0x6C1EF1D3E8EF0EDE, 0xB9C97D43E9798FB4, 0xA2F2D784740C28A3, 0x7B8496476197566F, 0x7A5BE3E6B65F069D, 0xF96330ED78BE6F10, 0xEEE60DE77A076A15, 0x2B4BEE4AA08B9BD0, 0x6A56A63EC7B8894E, 0x02121359BA34FEF4, 0x4CBF99F8283703FC, 0x398071350CAF30C8, 0xD0A77A89F017687A, 0xF1C1A9EB9E423569, 0x8C7976282DEE8199, 0x5D1737A5DD1F7ABD, 0x4F53433C09A9FA80, 0xFA8B0C53DF7CA1D9, 0x3FD9DCBC886CCB77, 0xC040917CA91B4720, 0x7DD00142F9D1DCDF, 0x8476FC1D4F387B58, 0x23F8E7C5F3316503, 0x032A2244E7E37339, 0x5C87A5D750F5A74B, 0x082B4CC43698992E, 0xDF917BECB858F63C, 0x3270B8FC5BF86DDA, 0x10AE72BB29B5DD76, 0x576AC94E7700362B, 0x1AD112DAC61EFB8F, 0x691BC30EC5FAA427, 0xFF246311CC327143, 0x3142368E30E53206, 0x71380E31E02CA396, 0x958D5C960AAD76F1, 0xF8D6F430C16DA536, 0xC8FFD13F1BE7E1D2, 0x7578AE66004DDBE1, 0x05833F01067BE646, 0xBB34B5AD3BFE586D, 0x095F34C9A12B97F0, 0x247AB64525D60CA8, 0xDCDBC6F3017477D1, 0x4A2E14D4DECAD24D, 0xBDB5E6D9BE0A1EEB, 0x2A7E70F7794301AB, 0xDEF42D8A270540FD, 0x01078EC0A34C22C1, 0xE5DE511AF4C16387, 0x7EBB3A52BD9A330A, 0x77697857AA7D6435, 0x004E831603AE4C32, 0xE7A21020AD78E312, 0x9D41A70C6AB420F2, 0x28E06C18EA1141E6, 0xD2B28CBD984F6B28, 0x26B75F6C446E9D83, 0xBA47568C4D418D7F, 0xD80BADBFE6183D8E, 0x0E206D7F5F166044, 0xE258A43911CBCA3E, 0x723A1746B21DC0BC, 0xC7CAA854F5D7CDD3, 0x7CAC32883D261D9C, 0x7690C26423BA942C, 0x17E55524478042B8, 0xE0BE477656A2389F, 0x4D289B5E67AB2DA0, 0x44862B9C8FBBFD31, 0xB47CC8049D141365, 0x822C1B362B91C793, 0x4EB14655FB13DFD8, 0x1ECBBA0714E2A97B, 0x6143459D5CDE5F14, 0x53A8FBF1D5F0AC89, 0x97EA04D81C5E5B00, 0x622181A8D4FDB3F3, 0xE9BCD341572A1208, 0x1411258643CCE58A, 0x9144C5FEA4C6E0A4, 0x0D33D06565CF620F, 0x54A48D489F219CA1, 0xC43E5EAC6D63C821, 0xA9728B3A72770DAF, 0xD7934E7B20DF87EF, 0xE35503B61A3E86E5, 0xCAE321FBC819D504, 0x129A50B3AC60BFA6, 0xCD5E68EA7E9FB6C3, 0xB01C90199483B1C7, 0x3DE93CD5C295376C, 0xAED52EDF2AB9AD13, 0x2E60F512C0A07884, 0xBC3D86A3E36210C9, 0x35269D9B163951CE, 0x0C7D6E2AD0CDB5FA, 0x59E86297D87F5733, 0x298EF221898DB0E7, 0x55000029D1A5AA7E, 0x8BC08AE1B5061B45, 0xC2C31C2B6C92703A, 0x94CC596BAF25EF42, 0x0A1D73DB22540456, 0x04B6A0F9D9C4179A, 0xEFFDAFA2AE3D3C60, 0xF7C8075BB49496C4, 0x9CC5C7141D1CD4E3, 0x78BD1638218E5534, 0xB2F11568F850246A, 0xEDFABCFA9502BC29, 0x796CE5F2DA23051B, 0xAAE128B0DC93537C, 0x3A493DA0EE4B29AE, 0xB5DF6B2C416895D7, 0xFCABBD25122D7F37, 0x70810B58105DC4B1, 0xE10FDD37F7882A90, 0x524DCAB5518A3F5C, 0x3C9E85878451255B, 0x4029828119BD34E2, 0x74A05B6F5D3CECCB, 0xB610021542E13ECA, 0x0FF979D12F59E2AC, 0x6037DA27E4F9CC50, 0x5E92975A0DF1847D, 0xD66DE190D3E623FE, 0x5032D6B87B568048, 0x9A36B7CE8235216E, 0x80272A7A24F64B4A, 0x93EFED8B8C6916F7, 0x37DDBFF44CCE1555, 0x4B95DB5D4B99BD25, 0x92D3FDA169812FC0, 0xFB1A4A9A90660BB6, 0x730C196946A4B9B2, 0x81E289AA7F49DA68, 0x64669A0F83B1A05F, 0x27B3FF7D9644F48B, 0xCC6B615C8DB675B3, 0x674F20B9BCEBBE95, 0x6F31238275655982, 0x5AE488713E45CF05, 0xBF619F9954C21157, 0xEABAC46040A8EAE9, 0x454C6FE9F2C0C1CD, 0x419CF6496412691C, 0xD3DC3BEF265B0F70, 0x6D0E60F5C3578A9E }; const u64bit Tiger::SBOX4[256] = { 0x5B0E608526323C55, 0x1A46C1A9FA1B59F5, 0xA9E245A17C4C8FFA, 0x65CA5159DB2955D7, 0x05DB0A76CE35AFC2, 0x81EAC77EA9113D45, 0x528EF88AB6AC0A0D, 0xA09EA253597BE3FF, 0x430DDFB3AC48CD56, 0xC4B3A67AF45CE46F, 0x4ECECFD8FBE2D05E, 0x3EF56F10B39935F0, 0x0B22D6829CD619C6, 0x17FD460A74DF2069, 0x6CF8CC8E8510ED40, 0xD6C824BF3A6ECAA7, 0x61243D581A817049, 0x048BACB6BBC163A2, 0xD9A38AC27D44CC32, 0x7FDDFF5BAAF410AB, 0xAD6D495AA804824B, 0xE1A6A74F2D8C9F94, 0xD4F7851235DEE8E3, 0xFD4B7F886540D893, 0x247C20042AA4BFDA, 0x096EA1C517D1327C, 0xD56966B4361A6685, 0x277DA5C31221057D, 0x94D59893A43ACFF7, 0x64F0C51CCDC02281, 0x3D33BCC4FF6189DB, 0xE005CB184CE66AF1, 0xFF5CCD1D1DB99BEA, 0xB0B854A7FE42980F, 0x7BD46A6A718D4B9F, 0xD10FA8CC22A5FD8C, 0xD31484952BE4BD31, 0xC7FA975FCB243847, 0x4886ED1E5846C407, 0x28CDDB791EB70B04, 0xC2B00BE2F573417F, 0x5C9590452180F877, 0x7A6BDDFFF370EB00, 0xCE509E38D6D9D6A4, 0xEBEB0F00647FA702, 0x1DCC06CF76606F06, 0xE4D9F28BA286FF0A, 0xD85A305DC918C262, 0x475B1D8732225F54, 0x2D4FB51668CCB5FE, 0xA679B9D9D72BBA20, 0x53841C0D912D43A5, 0x3B7EAA48BF12A4E8, 0x781E0E47F22F1DDF, 0xEFF20CE60AB50973, 0x20D261D19DFFB742, 0x16A12B03062A2E39, 0x1960EB2239650495, 0x251C16FED50EB8B8, 0x9AC0C330F826016E, 0xED152665953E7671, 0x02D63194A6369570, 0x5074F08394B1C987, 0x70BA598C90B25CE1, 0x794A15810B9742F6, 0x0D5925E9FCAF8C6C, 0x3067716CD868744E, 0x910AB077E8D7731B, 0x6A61BBDB5AC42F61, 0x93513EFBF0851567, 0xF494724B9E83E9D5, 0xE887E1985C09648D, 0x34B1D3C675370CFD, 0xDC35E433BC0D255D, 0xD0AAB84234131BE0, 0x08042A50B48B7EAF, 0x9997C4EE44A3AB35, 0x829A7B49201799D0, 0x263B8307B7C54441, 0x752F95F4FD6A6CA6, 0x927217402C08C6E5, 0x2A8AB754A795D9EE, 0xA442F7552F72943D, 0x2C31334E19781208, 0x4FA98D7CEAEE6291, 0x55C3862F665DB309, 0xBD0610175D53B1F3, 0x46FE6CB840413F27, 0x3FE03792DF0CFA59, 0xCFE700372EB85E8F, 0xA7BE29E7ADBCE118, 0xE544EE5CDE8431DD, 0x8A781B1B41F1873E, 0xA5C94C78A0D2F0E7, 0x39412E2877B60728, 0xA1265EF3AFC9A62C, 0xBCC2770C6A2506C5, 0x3AB66DD5DCE1CE12, 0xE65499D04A675B37, 0x7D8F523481BFD216, 0x0F6F64FCEC15F389, 0x74EFBE618B5B13C8, 0xACDC82B714273E1D, 0xDD40BFE003199D17, 0x37E99257E7E061F8, 0xFA52626904775AAA, 0x8BBBF63A463D56F9, 0xF0013F1543A26E64, 0xA8307E9F879EC898, 0xCC4C27A4150177CC, 0x1B432F2CCA1D3348, 0xDE1D1F8F9F6FA013, 0x606602A047A7DDD6, 0xD237AB64CC1CB2C7, 0x9B938E7225FCD1D3, 0xEC4E03708E0FF476, 0xFEB2FBDA3D03C12D, 0xAE0BCED2EE43889A, 0x22CB8923EBFB4F43, 0x69360D013CF7396D, 0x855E3602D2D4E022, 0x073805BAD01F784C, 0x33E17A133852F546, 0xDF4874058AC7B638, 0xBA92B29C678AA14A, 0x0CE89FC76CFAADCD, 0x5F9D4E0908339E34, 0xF1AFE9291F5923B9, 0x6E3480F60F4A265F, 0xEEBF3A2AB29B841C, 0xE21938A88F91B4AD, 0x57DFEFF845C6D3C3, 0x2F006B0BF62CAAF2, 0x62F479EF6F75EE78, 0x11A55AD41C8916A9, 0xF229D29084FED453, 0x42F1C27B16B000E6, 0x2B1F76749823C074, 0x4B76ECA3C2745360, 0x8C98F463B91691BD, 0x14BCC93CF1ADE66A, 0x8885213E6D458397, 0x8E177DF0274D4711, 0xB49B73B5503F2951, 0x10168168C3F96B6B, 0x0E3D963B63CAB0AE, 0x8DFC4B5655A1DB14, 0xF789F1356E14DE5C, 0x683E68AF4E51DAC1, 0xC9A84F9D8D4B0FD9, 0x3691E03F52A0F9D1, 0x5ED86E46E1878E80, 0x3C711A0E99D07150, 0x5A0865B20C4E9310, 0x56FBFC1FE4F0682E, 0xEA8D5DE3105EDF9B, 0x71ABFDB12379187A, 0x2EB99DE1BEE77B9C, 0x21ECC0EA33CF4523, 0x59A4D7521805C7A1, 0x3896F5EB56AE7C72, 0xAA638F3DB18F75DC, 0x9F39358DABE9808E, 0xB7DEFA91C00B72AC, 0x6B5541FD62492D92, 0x6DC6DEE8F92E4D5B, 0x353F57ABC4BEEA7E, 0x735769D6DA5690CE, 0x0A234AA642391484, 0xF6F9508028F80D9D, 0xB8E319A27AB3F215, 0x31AD9C1151341A4D, 0x773C22A57BEF5805, 0x45C7561A07968633, 0xF913DA9E249DBE36, 0xDA652D9B78A64C68, 0x4C27A97F3BC334EF, 0x76621220E66B17F4, 0x967743899ACD7D0B, 0xF3EE5BCAE0ED6782, 0x409F753600C879FC, 0x06D09A39B5926DB6, 0x6F83AEB0317AC588, 0x01E6CA4A86381F21, 0x66FF3462D19F3025, 0x72207C24DDFD3BFB, 0x4AF6B6D3E2ECE2EB, 0x9C994DBEC7EA08DE, 0x49ACE597B09A8BC4, 0xB38C4766CF0797BA, 0x131B9373C57C2A75, 0xB1822CCE61931E58, 0x9D7555B909BA1C0C, 0x127FAFDD937D11D2, 0x29DA3BADC66D92E4, 0xA2C1D57154C2ECBC, 0x58C5134D82F6FE24, 0x1C3AE3515B62274F, 0xE907C82E01CB8126, 0xF8ED091913E37FCB, 0x3249D8F9C80046C9, 0x80CF9BEDE388FB63, 0x1881539A116CF19E, 0x5103F3F76BD52457, 0x15B7E6F5AE47F7A8, 0xDBD7C6DED47E9CCF, 0x44E55C410228BB1A, 0xB647D4255EDB4E99, 0x5D11882BB8AAFC30, 0xF5098BBB29D3212A, 0x8FB5EA14E90296B3, 0x677B942157DD025A, 0xFB58E7C0A390ACB5, 0x89D3674C83BD4A01, 0x9E2DA4DF4BF3B93B, 0xFCC41E328CAB4829, 0x03F38C96BA582C52, 0xCAD1BDBD7FD85DB2, 0xBBB442C16082AE83, 0xB95FE86BA5DA9AB0, 0xB22E04673771A93F, 0x845358C9493152D8, 0xBE2A488697B4541E, 0x95A2DC2DD38E6966, 0xC02C11AC923C852B, 0x2388B1990DF2A87B, 0x7C8008FA1B4F37BE, 0x1F70D0C84D54E503, 0x5490ADEC7ECE57D4, 0x002B3C27D9063A3A, 0x7EAEA3848030A2BF, 0xC602326DED2003C0, 0x83A7287D69A94086, 0xC57A5FCB30F57A8A, 0xB56844E479EBE779, 0xA373B40F05DCBCE9, 0xD71A786E88570EE2, 0x879CBACDBDE8F6A0, 0x976AD1BCC164A32F, 0xAB21E25E9666D78B, 0x901063AAE5E5C33C, 0x9818B34448698D90, 0xE36487AE3E1E8ABB, 0xAFBDF931893BDCB4, 0x6345A0DC5FBBD519, 0x8628FE269B9465CA, 0x1E5D01603F9C51EC, 0x4DE44006A15049B7, 0xBF6C70E5F776CBB1, 0x411218F2EF552BED, 0xCB0C0708705A36A3, 0xE74D14754F986044, 0xCD56D9430EA8280E, 0xC12591D7535F5065, 0xC83223F1720AEF96, 0xC3A0396F7363A51F }; } /* * Tiger * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { /* * Tiger Mixing Function */ inline void mix(MemoryRegion<u64bit>& X) { X[0] -= X[7] ^ 0xA5A5A5A5A5A5A5A5; X[1] ^= X[0]; X[2] += X[1]; X[3] -= X[2] ^ ((~X[1]) << 19); X[4] ^= X[3]; X[5] += X[4]; X[6] -= X[5] ^ ((~X[4]) >> 23); X[7] ^= X[6]; X[0] += X[7]; X[1] -= X[0] ^ ((~X[7]) << 19); X[2] ^= X[1]; X[3] += X[2]; X[4] -= X[3] ^ ((~X[2]) >> 23); X[5] ^= X[4]; X[6] += X[5]; X[7] -= X[6] ^ 0x0123456789ABCDEF; } } /* * Tiger Compression Function */ void Tiger::compress_n(const byte input[], size_t blocks) { u64bit A = digest[0], B = digest[1], C = digest[2]; for(size_t i = 0; i != blocks; ++i) { load_le(&X[0], input, X.size()); pass(A, B, C, X, 5); mix(X); pass(C, A, B, X, 7); mix(X); pass(B, C, A, X, 9); for(size_t j = 3; j != passes; ++j) { mix(X); pass(A, B, C, X, 9); u64bit T = A; A = C; C = B; B = T; } A = (digest[0] ^= A); B = digest[1] = B - digest[1]; C = (digest[2] += C); input += hash_block_size(); } } /* * Copy out the digest */ void Tiger::copy_out(byte output[]) { for(size_t i = 0; i != output_length(); ++i) output[i] = get_byte(7 - (i % 8), digest[i/8]); } /* * Tiger Pass */ void Tiger::pass(u64bit& A, u64bit& B, u64bit& C, const MemoryRegion<u64bit>& X, byte mul) { C ^= X[0]; A -= SBOX1[get_byte(7, C)] ^ SBOX2[get_byte(5, C)] ^ SBOX3[get_byte(3, C)] ^ SBOX4[get_byte(1, C)]; B += SBOX1[get_byte(0, C)] ^ SBOX2[get_byte(2, C)] ^ SBOX3[get_byte(4, C)] ^ SBOX4[get_byte(6, C)]; B *= mul; A ^= X[1]; B -= SBOX1[get_byte(7, A)] ^ SBOX2[get_byte(5, A)] ^ SBOX3[get_byte(3, A)] ^ SBOX4[get_byte(1, A)]; C += SBOX1[get_byte(0, A)] ^ SBOX2[get_byte(2, A)] ^ SBOX3[get_byte(4, A)] ^ SBOX4[get_byte(6, A)]; C *= mul; B ^= X[2]; C -= SBOX1[get_byte(7, B)] ^ SBOX2[get_byte(5, B)] ^ SBOX3[get_byte(3, B)] ^ SBOX4[get_byte(1, B)]; A += SBOX1[get_byte(0, B)] ^ SBOX2[get_byte(2, B)] ^ SBOX3[get_byte(4, B)] ^ SBOX4[get_byte(6, B)]; A *= mul; C ^= X[3]; A -= SBOX1[get_byte(7, C)] ^ SBOX2[get_byte(5, C)] ^ SBOX3[get_byte(3, C)] ^ SBOX4[get_byte(1, C)]; B += SBOX1[get_byte(0, C)] ^ SBOX2[get_byte(2, C)] ^ SBOX3[get_byte(4, C)] ^ SBOX4[get_byte(6, C)]; B *= mul; A ^= X[4]; B -= SBOX1[get_byte(7, A)] ^ SBOX2[get_byte(5, A)] ^ SBOX3[get_byte(3, A)] ^ SBOX4[get_byte(1, A)]; C += SBOX1[get_byte(0, A)] ^ SBOX2[get_byte(2, A)] ^ SBOX3[get_byte(4, A)] ^ SBOX4[get_byte(6, A)]; C *= mul; B ^= X[5]; C -= SBOX1[get_byte(7, B)] ^ SBOX2[get_byte(5, B)] ^ SBOX3[get_byte(3, B)] ^ SBOX4[get_byte(1, B)]; A += SBOX1[get_byte(0, B)] ^ SBOX2[get_byte(2, B)] ^ SBOX3[get_byte(4, B)] ^ SBOX4[get_byte(6, B)]; A *= mul; C ^= X[6]; A -= SBOX1[get_byte(7, C)] ^ SBOX2[get_byte(5, C)] ^ SBOX3[get_byte(3, C)] ^ SBOX4[get_byte(1, C)]; B += SBOX1[get_byte(0, C)] ^ SBOX2[get_byte(2, C)] ^ SBOX3[get_byte(4, C)] ^ SBOX4[get_byte(6, C)]; B *= mul; A ^= X[7]; B -= SBOX1[get_byte(7, A)] ^ SBOX2[get_byte(5, A)] ^ SBOX3[get_byte(3, A)] ^ SBOX4[get_byte(1, A)]; C += SBOX1[get_byte(0, A)] ^ SBOX2[get_byte(2, A)] ^ SBOX3[get_byte(4, A)] ^ SBOX4[get_byte(6, A)]; C *= mul; } /* * Clear memory of sensitive data */ void Tiger::clear() { MDx_HashFunction::clear(); zeroise(X); digest[0] = 0x0123456789ABCDEF; digest[1] = 0xFEDCBA9876543210; digest[2] = 0xF096A5B4C3B2E187; } /* * Return the name of this type */ std::string Tiger::name() const { return "Tiger(" + to_string(output_length()) + "," + to_string(passes) + ")"; } /* * Tiger Constructor */ Tiger::Tiger(size_t hash_len, size_t passes) : MDx_HashFunction(64, false, false), X(8), digest(3), hash_len(hash_len), passes(passes) { if(output_length() != 16 && output_length() != 20 && output_length() != 24) throw Invalid_Argument("Tiger: Illegal hash output size: " + to_string(output_length())); if(passes < 3) throw Invalid_Argument("Tiger: Invalid number of passes: " + to_string(passes)); clear(); } } /* * Diffusion Tables for Whirlpool * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { const u64bit Whirlpool::C0[256] = { 0x18186018C07830D8, 0x23238C2305AF4626, 0xC6C63FC67EF991B8, 0xE8E887E8136FCDFB, 0x878726874CA113CB, 0xB8B8DAB8A9626D11, 0x0101040108050209, 0x4F4F214F426E9E0D, 0x3636D836ADEE6C9B, 0xA6A6A2A6590451FF, 0xD2D26FD2DEBDB90C, 0xF5F5F3F5FB06F70E, 0x7979F979EF80F296, 0x6F6FA16F5FCEDE30, 0x91917E91FCEF3F6D, 0x52525552AA07A4F8, 0x60609D6027FDC047, 0xBCBCCABC89766535, 0x9B9B569BACCD2B37, 0x8E8E028E048C018A, 0xA3A3B6A371155BD2, 0x0C0C300C603C186C, 0x7B7BF17BFF8AF684, 0x3535D435B5E16A80, 0x1D1D741DE8693AF5, 0xE0E0A7E05347DDB3, 0xD7D77BD7F6ACB321, 0xC2C22FC25EED999C, 0x2E2EB82E6D965C43, 0x4B4B314B627A9629, 0xFEFEDFFEA321E15D, 0x575741578216AED5, 0x15155415A8412ABD, 0x7777C1779FB6EEE8, 0x3737DC37A5EB6E92, 0xE5E5B3E57B56D79E, 0x9F9F469F8CD92313, 0xF0F0E7F0D317FD23, 0x4A4A354A6A7F9420, 0xDADA4FDA9E95A944, 0x58587D58FA25B0A2, 0xC9C903C906CA8FCF, 0x2929A429558D527C, 0x0A0A280A5022145A, 0xB1B1FEB1E14F7F50, 0xA0A0BAA0691A5DC9, 0x6B6BB16B7FDAD614, 0x85852E855CAB17D9, 0xBDBDCEBD8173673C, 0x5D5D695DD234BA8F, 0x1010401080502090, 0xF4F4F7F4F303F507, 0xCBCB0BCB16C08BDD, 0x3E3EF83EEDC67CD3, 0x0505140528110A2D, 0x676781671FE6CE78, 0xE4E4B7E47353D597, 0x27279C2725BB4E02, 0x4141194132588273, 0x8B8B168B2C9D0BA7, 0xA7A7A6A7510153F6, 0x7D7DE97DCF94FAB2, 0x95956E95DCFB3749, 0xD8D847D88E9FAD56, 0xFBFBCBFB8B30EB70, 0xEEEE9FEE2371C1CD, 0x7C7CED7CC791F8BB, 0x6666856617E3CC71, 0xDDDD53DDA68EA77B, 0x17175C17B84B2EAF, 0x4747014702468E45, 0x9E9E429E84DC211A, 0xCACA0FCA1EC589D4, 0x2D2DB42D75995A58, 0xBFBFC6BF9179632E, 0x07071C07381B0E3F, 0xADAD8EAD012347AC, 0x5A5A755AEA2FB4B0, 0x838336836CB51BEF, 0x3333CC3385FF66B6, 0x636391633FF2C65C, 0x02020802100A0412, 0xAAAA92AA39384993, 0x7171D971AFA8E2DE, 0xC8C807C80ECF8DC6, 0x19196419C87D32D1, 0x494939497270923B, 0xD9D943D9869AAF5F, 0xF2F2EFF2C31DF931, 0xE3E3ABE34B48DBA8, 0x5B5B715BE22AB6B9, 0x88881A8834920DBC, 0x9A9A529AA4C8293E, 0x262698262DBE4C0B, 0x3232C8328DFA64BF, 0xB0B0FAB0E94A7D59, 0xE9E983E91B6ACFF2, 0x0F0F3C0F78331E77, 0xD5D573D5E6A6B733, 0x80803A8074BA1DF4, 0xBEBEC2BE997C6127, 0xCDCD13CD26DE87EB, 0x3434D034BDE46889, 0x48483D487A759032, 0xFFFFDBFFAB24E354, 0x7A7AF57AF78FF48D, 0x90907A90F4EA3D64, 0x5F5F615FC23EBE9D, 0x202080201DA0403D, 0x6868BD6867D5D00F, 0x1A1A681AD07234CA, 0xAEAE82AE192C41B7, 0xB4B4EAB4C95E757D, 0x54544D549A19A8CE, 0x93937693ECE53B7F, 0x222288220DAA442F, 0x64648D6407E9C863, 0xF1F1E3F1DB12FF2A, 0x7373D173BFA2E6CC, 0x12124812905A2482, 0x40401D403A5D807A, 0x0808200840281048, 0xC3C32BC356E89B95, 0xECEC97EC337BC5DF, 0xDBDB4BDB9690AB4D, 0xA1A1BEA1611F5FC0, 0x8D8D0E8D1C830791, 0x3D3DF43DF5C97AC8, 0x97976697CCF1335B, 0x0000000000000000, 0xCFCF1BCF36D483F9, 0x2B2BAC2B4587566E, 0x7676C57697B3ECE1, 0x8282328264B019E6, 0xD6D67FD6FEA9B128, 0x1B1B6C1BD87736C3, 0xB5B5EEB5C15B7774, 0xAFAF86AF112943BE, 0x6A6AB56A77DFD41D, 0x50505D50BA0DA0EA, 0x45450945124C8A57, 0xF3F3EBF3CB18FB38, 0x3030C0309DF060AD, 0xEFEF9BEF2B74C3C4, 0x3F3FFC3FE5C37EDA, 0x55554955921CAAC7, 0xA2A2B2A2791059DB, 0xEAEA8FEA0365C9E9, 0x656589650FECCA6A, 0xBABAD2BAB9686903, 0x2F2FBC2F65935E4A, 0xC0C027C04EE79D8E, 0xDEDE5FDEBE81A160, 0x1C1C701CE06C38FC, 0xFDFDD3FDBB2EE746, 0x4D4D294D52649A1F, 0x92927292E4E03976, 0x7575C9758FBCEAFA, 0x06061806301E0C36, 0x8A8A128A249809AE, 0xB2B2F2B2F940794B, 0xE6E6BFE66359D185, 0x0E0E380E70361C7E, 0x1F1F7C1FF8633EE7, 0x6262956237F7C455, 0xD4D477D4EEA3B53A, 0xA8A89AA829324D81, 0x96966296C4F43152, 0xF9F9C3F99B3AEF62, 0xC5C533C566F697A3, 0x2525942535B14A10, 0x59597959F220B2AB, 0x84842A8454AE15D0, 0x7272D572B7A7E4C5, 0x3939E439D5DD72EC, 0x4C4C2D4C5A619816, 0x5E5E655ECA3BBC94, 0x7878FD78E785F09F, 0x3838E038DDD870E5, 0x8C8C0A8C14860598, 0xD1D163D1C6B2BF17, 0xA5A5AEA5410B57E4, 0xE2E2AFE2434DD9A1, 0x616199612FF8C24E, 0xB3B3F6B3F1457B42, 0x2121842115A54234, 0x9C9C4A9C94D62508, 0x1E1E781EF0663CEE, 0x4343114322528661, 0xC7C73BC776FC93B1, 0xFCFCD7FCB32BE54F, 0x0404100420140824, 0x51515951B208A2E3, 0x99995E99BCC72F25, 0x6D6DA96D4FC4DA22, 0x0D0D340D68391A65, 0xFAFACFFA8335E979, 0xDFDF5BDFB684A369, 0x7E7EE57ED79BFCA9, 0x242490243DB44819, 0x3B3BEC3BC5D776FE, 0xABAB96AB313D4B9A, 0xCECE1FCE3ED181F0, 0x1111441188552299, 0x8F8F068F0C890383, 0x4E4E254E4A6B9C04, 0xB7B7E6B7D1517366, 0xEBEB8BEB0B60CBE0, 0x3C3CF03CFDCC78C1, 0x81813E817CBF1FFD, 0x94946A94D4FE3540, 0xF7F7FBF7EB0CF31C, 0xB9B9DEB9A1676F18, 0x13134C13985F268B, 0x2C2CB02C7D9C5851, 0xD3D36BD3D6B8BB05, 0xE7E7BBE76B5CD38C, 0x6E6EA56E57CBDC39, 0xC4C437C46EF395AA, 0x03030C03180F061B, 0x565645568A13ACDC, 0x44440D441A49885E, 0x7F7FE17FDF9EFEA0, 0xA9A99EA921374F88, 0x2A2AA82A4D825467, 0xBBBBD6BBB16D6B0A, 0xC1C123C146E29F87, 0x53535153A202A6F1, 0xDCDC57DCAE8BA572, 0x0B0B2C0B58271653, 0x9D9D4E9D9CD32701, 0x6C6CAD6C47C1D82B, 0x3131C43195F562A4, 0x7474CD7487B9E8F3, 0xF6F6FFF6E309F115, 0x464605460A438C4C, 0xACAC8AAC092645A5, 0x89891E893C970FB5, 0x14145014A04428B4, 0xE1E1A3E15B42DFBA, 0x16165816B04E2CA6, 0x3A3AE83ACDD274F7, 0x6969B9696FD0D206, 0x09092409482D1241, 0x7070DD70A7ADE0D7, 0xB6B6E2B6D954716F, 0xD0D067D0CEB7BD1E, 0xEDED93ED3B7EC7D6, 0xCCCC17CC2EDB85E2, 0x424215422A578468, 0x98985A98B4C22D2C, 0xA4A4AAA4490E55ED, 0x2828A0285D885075, 0x5C5C6D5CDA31B886, 0xF8F8C7F8933FED6B, 0x8686228644A411C2 }; const u64bit Whirlpool::C1[256] = { 0xD818186018C07830, 0x2623238C2305AF46, 0xB8C6C63FC67EF991, 0xFBE8E887E8136FCD, 0xCB878726874CA113, 0x11B8B8DAB8A9626D, 0x0901010401080502, 0x0D4F4F214F426E9E, 0x9B3636D836ADEE6C, 0xFFA6A6A2A6590451, 0x0CD2D26FD2DEBDB9, 0x0EF5F5F3F5FB06F7, 0x967979F979EF80F2, 0x306F6FA16F5FCEDE, 0x6D91917E91FCEF3F, 0xF852525552AA07A4, 0x4760609D6027FDC0, 0x35BCBCCABC897665, 0x379B9B569BACCD2B, 0x8A8E8E028E048C01, 0xD2A3A3B6A371155B, 0x6C0C0C300C603C18, 0x847B7BF17BFF8AF6, 0x803535D435B5E16A, 0xF51D1D741DE8693A, 0xB3E0E0A7E05347DD, 0x21D7D77BD7F6ACB3, 0x9CC2C22FC25EED99, 0x432E2EB82E6D965C, 0x294B4B314B627A96, 0x5DFEFEDFFEA321E1, 0xD5575741578216AE, 0xBD15155415A8412A, 0xE87777C1779FB6EE, 0x923737DC37A5EB6E, 0x9EE5E5B3E57B56D7, 0x139F9F469F8CD923, 0x23F0F0E7F0D317FD, 0x204A4A354A6A7F94, 0x44DADA4FDA9E95A9, 0xA258587D58FA25B0, 0xCFC9C903C906CA8F, 0x7C2929A429558D52, 0x5A0A0A280A502214, 0x50B1B1FEB1E14F7F, 0xC9A0A0BAA0691A5D, 0x146B6BB16B7FDAD6, 0xD985852E855CAB17, 0x3CBDBDCEBD817367, 0x8F5D5D695DD234BA, 0x9010104010805020, 0x07F4F4F7F4F303F5, 0xDDCBCB0BCB16C08B, 0xD33E3EF83EEDC67C, 0x2D0505140528110A, 0x78676781671FE6CE, 0x97E4E4B7E47353D5, 0x0227279C2725BB4E, 0x7341411941325882, 0xA78B8B168B2C9D0B, 0xF6A7A7A6A7510153, 0xB27D7DE97DCF94FA, 0x4995956E95DCFB37, 0x56D8D847D88E9FAD, 0x70FBFBCBFB8B30EB, 0xCDEEEE9FEE2371C1, 0xBB7C7CED7CC791F8, 0x716666856617E3CC, 0x7BDDDD53DDA68EA7, 0xAF17175C17B84B2E, 0x454747014702468E, 0x1A9E9E429E84DC21, 0xD4CACA0FCA1EC589, 0x582D2DB42D75995A, 0x2EBFBFC6BF917963, 0x3F07071C07381B0E, 0xACADAD8EAD012347, 0xB05A5A755AEA2FB4, 0xEF838336836CB51B, 0xB63333CC3385FF66, 0x5C636391633FF2C6, 0x1202020802100A04, 0x93AAAA92AA393849, 0xDE7171D971AFA8E2, 0xC6C8C807C80ECF8D, 0xD119196419C87D32, 0x3B49493949727092, 0x5FD9D943D9869AAF, 0x31F2F2EFF2C31DF9, 0xA8E3E3ABE34B48DB, 0xB95B5B715BE22AB6, 0xBC88881A8834920D, 0x3E9A9A529AA4C829, 0x0B262698262DBE4C, 0xBF3232C8328DFA64, 0x59B0B0FAB0E94A7D, 0xF2E9E983E91B6ACF, 0x770F0F3C0F78331E, 0x33D5D573D5E6A6B7, 0xF480803A8074BA1D, 0x27BEBEC2BE997C61, 0xEBCDCD13CD26DE87, 0x893434D034BDE468, 0x3248483D487A7590, 0x54FFFFDBFFAB24E3, 0x8D7A7AF57AF78FF4, 0x6490907A90F4EA3D, 0x9D5F5F615FC23EBE, 0x3D202080201DA040, 0x0F6868BD6867D5D0, 0xCA1A1A681AD07234, 0xB7AEAE82AE192C41, 0x7DB4B4EAB4C95E75, 0xCE54544D549A19A8, 0x7F93937693ECE53B, 0x2F222288220DAA44, 0x6364648D6407E9C8, 0x2AF1F1E3F1DB12FF, 0xCC7373D173BFA2E6, 0x8212124812905A24, 0x7A40401D403A5D80, 0x4808082008402810, 0x95C3C32BC356E89B, 0xDFECEC97EC337BC5, 0x4DDBDB4BDB9690AB, 0xC0A1A1BEA1611F5F, 0x918D8D0E8D1C8307, 0xC83D3DF43DF5C97A, 0x5B97976697CCF133, 0x0000000000000000, 0xF9CFCF1BCF36D483, 0x6E2B2BAC2B458756, 0xE17676C57697B3EC, 0xE68282328264B019, 0x28D6D67FD6FEA9B1, 0xC31B1B6C1BD87736, 0x74B5B5EEB5C15B77, 0xBEAFAF86AF112943, 0x1D6A6AB56A77DFD4, 0xEA50505D50BA0DA0, 0x5745450945124C8A, 0x38F3F3EBF3CB18FB, 0xAD3030C0309DF060, 0xC4EFEF9BEF2B74C3, 0xDA3F3FFC3FE5C37E, 0xC755554955921CAA, 0xDBA2A2B2A2791059, 0xE9EAEA8FEA0365C9, 0x6A656589650FECCA, 0x03BABAD2BAB96869, 0x4A2F2FBC2F65935E, 0x8EC0C027C04EE79D, 0x60DEDE5FDEBE81A1, 0xFC1C1C701CE06C38, 0x46FDFDD3FDBB2EE7, 0x1F4D4D294D52649A, 0x7692927292E4E039, 0xFA7575C9758FBCEA, 0x3606061806301E0C, 0xAE8A8A128A249809, 0x4BB2B2F2B2F94079, 0x85E6E6BFE66359D1, 0x7E0E0E380E70361C, 0xE71F1F7C1FF8633E, 0x556262956237F7C4, 0x3AD4D477D4EEA3B5, 0x81A8A89AA829324D, 0x5296966296C4F431, 0x62F9F9C3F99B3AEF, 0xA3C5C533C566F697, 0x102525942535B14A, 0xAB59597959F220B2, 0xD084842A8454AE15, 0xC57272D572B7A7E4, 0xEC3939E439D5DD72, 0x164C4C2D4C5A6198, 0x945E5E655ECA3BBC, 0x9F7878FD78E785F0, 0xE53838E038DDD870, 0x988C8C0A8C148605, 0x17D1D163D1C6B2BF, 0xE4A5A5AEA5410B57, 0xA1E2E2AFE2434DD9, 0x4E616199612FF8C2, 0x42B3B3F6B3F1457B, 0x342121842115A542, 0x089C9C4A9C94D625, 0xEE1E1E781EF0663C, 0x6143431143225286, 0xB1C7C73BC776FC93, 0x4FFCFCD7FCB32BE5, 0x2404041004201408, 0xE351515951B208A2, 0x2599995E99BCC72F, 0x226D6DA96D4FC4DA, 0x650D0D340D68391A, 0x79FAFACFFA8335E9, 0x69DFDF5BDFB684A3, 0xA97E7EE57ED79BFC, 0x19242490243DB448, 0xFE3B3BEC3BC5D776, 0x9AABAB96AB313D4B, 0xF0CECE1FCE3ED181, 0x9911114411885522, 0x838F8F068F0C8903, 0x044E4E254E4A6B9C, 0x66B7B7E6B7D15173, 0xE0EBEB8BEB0B60CB, 0xC13C3CF03CFDCC78, 0xFD81813E817CBF1F, 0x4094946A94D4FE35, 0x1CF7F7FBF7EB0CF3, 0x18B9B9DEB9A1676F, 0x8B13134C13985F26, 0x512C2CB02C7D9C58, 0x05D3D36BD3D6B8BB, 0x8CE7E7BBE76B5CD3, 0x396E6EA56E57CBDC, 0xAAC4C437C46EF395, 0x1B03030C03180F06, 0xDC565645568A13AC, 0x5E44440D441A4988, 0xA07F7FE17FDF9EFE, 0x88A9A99EA921374F, 0x672A2AA82A4D8254, 0x0ABBBBD6BBB16D6B, 0x87C1C123C146E29F, 0xF153535153A202A6, 0x72DCDC57DCAE8BA5, 0x530B0B2C0B582716, 0x019D9D4E9D9CD327, 0x2B6C6CAD6C47C1D8, 0xA43131C43195F562, 0xF37474CD7487B9E8, 0x15F6F6FFF6E309F1, 0x4C464605460A438C, 0xA5ACAC8AAC092645, 0xB589891E893C970F, 0xB414145014A04428, 0xBAE1E1A3E15B42DF, 0xA616165816B04E2C, 0xF73A3AE83ACDD274, 0x066969B9696FD0D2, 0x4109092409482D12, 0xD77070DD70A7ADE0, 0x6FB6B6E2B6D95471, 0x1ED0D067D0CEB7BD, 0xD6EDED93ED3B7EC7, 0xE2CCCC17CC2EDB85, 0x68424215422A5784, 0x2C98985A98B4C22D, 0xEDA4A4AAA4490E55, 0x752828A0285D8850, 0x865C5C6D5CDA31B8, 0x6BF8F8C7F8933FED, 0xC28686228644A411 }; const u64bit Whirlpool::C2[256] = { 0x30D818186018C078, 0x462623238C2305AF, 0x91B8C6C63FC67EF9, 0xCDFBE8E887E8136F, 0x13CB878726874CA1, 0x6D11B8B8DAB8A962, 0x0209010104010805, 0x9E0D4F4F214F426E, 0x6C9B3636D836ADEE, 0x51FFA6A6A2A65904, 0xB90CD2D26FD2DEBD, 0xF70EF5F5F3F5FB06, 0xF2967979F979EF80, 0xDE306F6FA16F5FCE, 0x3F6D91917E91FCEF, 0xA4F852525552AA07, 0xC04760609D6027FD, 0x6535BCBCCABC8976, 0x2B379B9B569BACCD, 0x018A8E8E028E048C, 0x5BD2A3A3B6A37115, 0x186C0C0C300C603C, 0xF6847B7BF17BFF8A, 0x6A803535D435B5E1, 0x3AF51D1D741DE869, 0xDDB3E0E0A7E05347, 0xB321D7D77BD7F6AC, 0x999CC2C22FC25EED, 0x5C432E2EB82E6D96, 0x96294B4B314B627A, 0xE15DFEFEDFFEA321, 0xAED5575741578216, 0x2ABD15155415A841, 0xEEE87777C1779FB6, 0x6E923737DC37A5EB, 0xD79EE5E5B3E57B56, 0x23139F9F469F8CD9, 0xFD23F0F0E7F0D317, 0x94204A4A354A6A7F, 0xA944DADA4FDA9E95, 0xB0A258587D58FA25, 0x8FCFC9C903C906CA, 0x527C2929A429558D, 0x145A0A0A280A5022, 0x7F50B1B1FEB1E14F, 0x5DC9A0A0BAA0691A, 0xD6146B6BB16B7FDA, 0x17D985852E855CAB, 0x673CBDBDCEBD8173, 0xBA8F5D5D695DD234, 0x2090101040108050, 0xF507F4F4F7F4F303, 0x8BDDCBCB0BCB16C0, 0x7CD33E3EF83EEDC6, 0x0A2D050514052811, 0xCE78676781671FE6, 0xD597E4E4B7E47353, 0x4E0227279C2725BB, 0x8273414119413258, 0x0BA78B8B168B2C9D, 0x53F6A7A7A6A75101, 0xFAB27D7DE97DCF94, 0x374995956E95DCFB, 0xAD56D8D847D88E9F, 0xEB70FBFBCBFB8B30, 0xC1CDEEEE9FEE2371, 0xF8BB7C7CED7CC791, 0xCC716666856617E3, 0xA77BDDDD53DDA68E, 0x2EAF17175C17B84B, 0x8E45474701470246, 0x211A9E9E429E84DC, 0x89D4CACA0FCA1EC5, 0x5A582D2DB42D7599, 0x632EBFBFC6BF9179, 0x0E3F07071C07381B, 0x47ACADAD8EAD0123, 0xB4B05A5A755AEA2F, 0x1BEF838336836CB5, 0x66B63333CC3385FF, 0xC65C636391633FF2, 0x041202020802100A, 0x4993AAAA92AA3938, 0xE2DE7171D971AFA8, 0x8DC6C8C807C80ECF, 0x32D119196419C87D, 0x923B494939497270, 0xAF5FD9D943D9869A, 0xF931F2F2EFF2C31D, 0xDBA8E3E3ABE34B48, 0xB6B95B5B715BE22A, 0x0DBC88881A883492, 0x293E9A9A529AA4C8, 0x4C0B262698262DBE, 0x64BF3232C8328DFA, 0x7D59B0B0FAB0E94A, 0xCFF2E9E983E91B6A, 0x1E770F0F3C0F7833, 0xB733D5D573D5E6A6, 0x1DF480803A8074BA, 0x6127BEBEC2BE997C, 0x87EBCDCD13CD26DE, 0x68893434D034BDE4, 0x903248483D487A75, 0xE354FFFFDBFFAB24, 0xF48D7A7AF57AF78F, 0x3D6490907A90F4EA, 0xBE9D5F5F615FC23E, 0x403D202080201DA0, 0xD00F6868BD6867D5, 0x34CA1A1A681AD072, 0x41B7AEAE82AE192C, 0x757DB4B4EAB4C95E, 0xA8CE54544D549A19, 0x3B7F93937693ECE5, 0x442F222288220DAA, 0xC86364648D6407E9, 0xFF2AF1F1E3F1DB12, 0xE6CC7373D173BFA2, 0x248212124812905A, 0x807A40401D403A5D, 0x1048080820084028, 0x9B95C3C32BC356E8, 0xC5DFECEC97EC337B, 0xAB4DDBDB4BDB9690, 0x5FC0A1A1BEA1611F, 0x07918D8D0E8D1C83, 0x7AC83D3DF43DF5C9, 0x335B97976697CCF1, 0x0000000000000000, 0x83F9CFCF1BCF36D4, 0x566E2B2BAC2B4587, 0xECE17676C57697B3, 0x19E68282328264B0, 0xB128D6D67FD6FEA9, 0x36C31B1B6C1BD877, 0x7774B5B5EEB5C15B, 0x43BEAFAF86AF1129, 0xD41D6A6AB56A77DF, 0xA0EA50505D50BA0D, 0x8A5745450945124C, 0xFB38F3F3EBF3CB18, 0x60AD3030C0309DF0, 0xC3C4EFEF9BEF2B74, 0x7EDA3F3FFC3FE5C3, 0xAAC755554955921C, 0x59DBA2A2B2A27910, 0xC9E9EAEA8FEA0365, 0xCA6A656589650FEC, 0x6903BABAD2BAB968, 0x5E4A2F2FBC2F6593, 0x9D8EC0C027C04EE7, 0xA160DEDE5FDEBE81, 0x38FC1C1C701CE06C, 0xE746FDFDD3FDBB2E, 0x9A1F4D4D294D5264, 0x397692927292E4E0, 0xEAFA7575C9758FBC, 0x0C3606061806301E, 0x09AE8A8A128A2498, 0x794BB2B2F2B2F940, 0xD185E6E6BFE66359, 0x1C7E0E0E380E7036, 0x3EE71F1F7C1FF863, 0xC4556262956237F7, 0xB53AD4D477D4EEA3, 0x4D81A8A89AA82932, 0x315296966296C4F4, 0xEF62F9F9C3F99B3A, 0x97A3C5C533C566F6, 0x4A102525942535B1, 0xB2AB59597959F220, 0x15D084842A8454AE, 0xE4C57272D572B7A7, 0x72EC3939E439D5DD, 0x98164C4C2D4C5A61, 0xBC945E5E655ECA3B, 0xF09F7878FD78E785, 0x70E53838E038DDD8, 0x05988C8C0A8C1486, 0xBF17D1D163D1C6B2, 0x57E4A5A5AEA5410B, 0xD9A1E2E2AFE2434D, 0xC24E616199612FF8, 0x7B42B3B3F6B3F145, 0x42342121842115A5, 0x25089C9C4A9C94D6, 0x3CEE1E1E781EF066, 0x8661434311432252, 0x93B1C7C73BC776FC, 0xE54FFCFCD7FCB32B, 0x0824040410042014, 0xA2E351515951B208, 0x2F2599995E99BCC7, 0xDA226D6DA96D4FC4, 0x1A650D0D340D6839, 0xE979FAFACFFA8335, 0xA369DFDF5BDFB684, 0xFCA97E7EE57ED79B, 0x4819242490243DB4, 0x76FE3B3BEC3BC5D7, 0x4B9AABAB96AB313D, 0x81F0CECE1FCE3ED1, 0x2299111144118855, 0x03838F8F068F0C89, 0x9C044E4E254E4A6B, 0x7366B7B7E6B7D151, 0xCBE0EBEB8BEB0B60, 0x78C13C3CF03CFDCC, 0x1FFD81813E817CBF, 0x354094946A94D4FE, 0xF31CF7F7FBF7EB0C, 0x6F18B9B9DEB9A167, 0x268B13134C13985F, 0x58512C2CB02C7D9C, 0xBB05D3D36BD3D6B8, 0xD38CE7E7BBE76B5C, 0xDC396E6EA56E57CB, 0x95AAC4C437C46EF3, 0x061B03030C03180F, 0xACDC565645568A13, 0x885E44440D441A49, 0xFEA07F7FE17FDF9E, 0x4F88A9A99EA92137, 0x54672A2AA82A4D82, 0x6B0ABBBBD6BBB16D, 0x9F87C1C123C146E2, 0xA6F153535153A202, 0xA572DCDC57DCAE8B, 0x16530B0B2C0B5827, 0x27019D9D4E9D9CD3, 0xD82B6C6CAD6C47C1, 0x62A43131C43195F5, 0xE8F37474CD7487B9, 0xF115F6F6FFF6E309, 0x8C4C464605460A43, 0x45A5ACAC8AAC0926, 0x0FB589891E893C97, 0x28B414145014A044, 0xDFBAE1E1A3E15B42, 0x2CA616165816B04E, 0x74F73A3AE83ACDD2, 0xD2066969B9696FD0, 0x124109092409482D, 0xE0D77070DD70A7AD, 0x716FB6B6E2B6D954, 0xBD1ED0D067D0CEB7, 0xC7D6EDED93ED3B7E, 0x85E2CCCC17CC2EDB, 0x8468424215422A57, 0x2D2C98985A98B4C2, 0x55EDA4A4AAA4490E, 0x50752828A0285D88, 0xB8865C5C6D5CDA31, 0xED6BF8F8C7F8933F, 0x11C28686228644A4 }; const u64bit Whirlpool::C3[256] = { 0x7830D818186018C0, 0xAF462623238C2305, 0xF991B8C6C63FC67E, 0x6FCDFBE8E887E813, 0xA113CB878726874C, 0x626D11B8B8DAB8A9, 0x0502090101040108, 0x6E9E0D4F4F214F42, 0xEE6C9B3636D836AD, 0x0451FFA6A6A2A659, 0xBDB90CD2D26FD2DE, 0x06F70EF5F5F3F5FB, 0x80F2967979F979EF, 0xCEDE306F6FA16F5F, 0xEF3F6D91917E91FC, 0x07A4F852525552AA, 0xFDC04760609D6027, 0x766535BCBCCABC89, 0xCD2B379B9B569BAC, 0x8C018A8E8E028E04, 0x155BD2A3A3B6A371, 0x3C186C0C0C300C60, 0x8AF6847B7BF17BFF, 0xE16A803535D435B5, 0x693AF51D1D741DE8, 0x47DDB3E0E0A7E053, 0xACB321D7D77BD7F6, 0xED999CC2C22FC25E, 0x965C432E2EB82E6D, 0x7A96294B4B314B62, 0x21E15DFEFEDFFEA3, 0x16AED55757415782, 0x412ABD15155415A8, 0xB6EEE87777C1779F, 0xEB6E923737DC37A5, 0x56D79EE5E5B3E57B, 0xD923139F9F469F8C, 0x17FD23F0F0E7F0D3, 0x7F94204A4A354A6A, 0x95A944DADA4FDA9E, 0x25B0A258587D58FA, 0xCA8FCFC9C903C906, 0x8D527C2929A42955, 0x22145A0A0A280A50, 0x4F7F50B1B1FEB1E1, 0x1A5DC9A0A0BAA069, 0xDAD6146B6BB16B7F, 0xAB17D985852E855C, 0x73673CBDBDCEBD81, 0x34BA8F5D5D695DD2, 0x5020901010401080, 0x03F507F4F4F7F4F3, 0xC08BDDCBCB0BCB16, 0xC67CD33E3EF83EED, 0x110A2D0505140528, 0xE6CE78676781671F, 0x53D597E4E4B7E473, 0xBB4E0227279C2725, 0x5882734141194132, 0x9D0BA78B8B168B2C, 0x0153F6A7A7A6A751, 0x94FAB27D7DE97DCF, 0xFB374995956E95DC, 0x9FAD56D8D847D88E, 0x30EB70FBFBCBFB8B, 0x71C1CDEEEE9FEE23, 0x91F8BB7C7CED7CC7, 0xE3CC716666856617, 0x8EA77BDDDD53DDA6, 0x4B2EAF17175C17B8, 0x468E454747014702, 0xDC211A9E9E429E84, 0xC589D4CACA0FCA1E, 0x995A582D2DB42D75, 0x79632EBFBFC6BF91, 0x1B0E3F07071C0738, 0x2347ACADAD8EAD01, 0x2FB4B05A5A755AEA, 0xB51BEF838336836C, 0xFF66B63333CC3385, 0xF2C65C636391633F, 0x0A04120202080210, 0x384993AAAA92AA39, 0xA8E2DE7171D971AF, 0xCF8DC6C8C807C80E, 0x7D32D119196419C8, 0x70923B4949394972, 0x9AAF5FD9D943D986, 0x1DF931F2F2EFF2C3, 0x48DBA8E3E3ABE34B, 0x2AB6B95B5B715BE2, 0x920DBC88881A8834, 0xC8293E9A9A529AA4, 0xBE4C0B262698262D, 0xFA64BF3232C8328D, 0x4A7D59B0B0FAB0E9, 0x6ACFF2E9E983E91B, 0x331E770F0F3C0F78, 0xA6B733D5D573D5E6, 0xBA1DF480803A8074, 0x7C6127BEBEC2BE99, 0xDE87EBCDCD13CD26, 0xE468893434D034BD, 0x75903248483D487A, 0x24E354FFFFDBFFAB, 0x8FF48D7A7AF57AF7, 0xEA3D6490907A90F4, 0x3EBE9D5F5F615FC2, 0xA0403D202080201D, 0xD5D00F6868BD6867, 0x7234CA1A1A681AD0, 0x2C41B7AEAE82AE19, 0x5E757DB4B4EAB4C9, 0x19A8CE54544D549A, 0xE53B7F93937693EC, 0xAA442F222288220D, 0xE9C86364648D6407, 0x12FF2AF1F1E3F1DB, 0xA2E6CC7373D173BF, 0x5A24821212481290, 0x5D807A40401D403A, 0x2810480808200840, 0xE89B95C3C32BC356, 0x7BC5DFECEC97EC33, 0x90AB4DDBDB4BDB96, 0x1F5FC0A1A1BEA161, 0x8307918D8D0E8D1C, 0xC97AC83D3DF43DF5, 0xF1335B97976697CC, 0x0000000000000000, 0xD483F9CFCF1BCF36, 0x87566E2B2BAC2B45, 0xB3ECE17676C57697, 0xB019E68282328264, 0xA9B128D6D67FD6FE, 0x7736C31B1B6C1BD8, 0x5B7774B5B5EEB5C1, 0x2943BEAFAF86AF11, 0xDFD41D6A6AB56A77, 0x0DA0EA50505D50BA, 0x4C8A574545094512, 0x18FB38F3F3EBF3CB, 0xF060AD3030C0309D, 0x74C3C4EFEF9BEF2B, 0xC37EDA3F3FFC3FE5, 0x1CAAC75555495592, 0x1059DBA2A2B2A279, 0x65C9E9EAEA8FEA03, 0xECCA6A656589650F, 0x686903BABAD2BAB9, 0x935E4A2F2FBC2F65, 0xE79D8EC0C027C04E, 0x81A160DEDE5FDEBE, 0x6C38FC1C1C701CE0, 0x2EE746FDFDD3FDBB, 0x649A1F4D4D294D52, 0xE0397692927292E4, 0xBCEAFA7575C9758F, 0x1E0C360606180630, 0x9809AE8A8A128A24, 0x40794BB2B2F2B2F9, 0x59D185E6E6BFE663, 0x361C7E0E0E380E70, 0x633EE71F1F7C1FF8, 0xF7C4556262956237, 0xA3B53AD4D477D4EE, 0x324D81A8A89AA829, 0xF4315296966296C4, 0x3AEF62F9F9C3F99B, 0xF697A3C5C533C566, 0xB14A102525942535, 0x20B2AB59597959F2, 0xAE15D084842A8454, 0xA7E4C57272D572B7, 0xDD72EC3939E439D5, 0x6198164C4C2D4C5A, 0x3BBC945E5E655ECA, 0x85F09F7878FD78E7, 0xD870E53838E038DD, 0x8605988C8C0A8C14, 0xB2BF17D1D163D1C6, 0x0B57E4A5A5AEA541, 0x4DD9A1E2E2AFE243, 0xF8C24E616199612F, 0x457B42B3B3F6B3F1, 0xA542342121842115, 0xD625089C9C4A9C94, 0x663CEE1E1E781EF0, 0x5286614343114322, 0xFC93B1C7C73BC776, 0x2BE54FFCFCD7FCB3, 0x1408240404100420, 0x08A2E351515951B2, 0xC72F2599995E99BC, 0xC4DA226D6DA96D4F, 0x391A650D0D340D68, 0x35E979FAFACFFA83, 0x84A369DFDF5BDFB6, 0x9BFCA97E7EE57ED7, 0xB44819242490243D, 0xD776FE3B3BEC3BC5, 0x3D4B9AABAB96AB31, 0xD181F0CECE1FCE3E, 0x5522991111441188, 0x8903838F8F068F0C, 0x6B9C044E4E254E4A, 0x517366B7B7E6B7D1, 0x60CBE0EBEB8BEB0B, 0xCC78C13C3CF03CFD, 0xBF1FFD81813E817C, 0xFE354094946A94D4, 0x0CF31CF7F7FBF7EB, 0x676F18B9B9DEB9A1, 0x5F268B13134C1398, 0x9C58512C2CB02C7D, 0xB8BB05D3D36BD3D6, 0x5CD38CE7E7BBE76B, 0xCBDC396E6EA56E57, 0xF395AAC4C437C46E, 0x0F061B03030C0318, 0x13ACDC565645568A, 0x49885E44440D441A, 0x9EFEA07F7FE17FDF, 0x374F88A9A99EA921, 0x8254672A2AA82A4D, 0x6D6B0ABBBBD6BBB1, 0xE29F87C1C123C146, 0x02A6F153535153A2, 0x8BA572DCDC57DCAE, 0x2716530B0B2C0B58, 0xD327019D9D4E9D9C, 0xC1D82B6C6CAD6C47, 0xF562A43131C43195, 0xB9E8F37474CD7487, 0x09F115F6F6FFF6E3, 0x438C4C464605460A, 0x2645A5ACAC8AAC09, 0x970FB589891E893C, 0x4428B414145014A0, 0x42DFBAE1E1A3E15B, 0x4E2CA616165816B0, 0xD274F73A3AE83ACD, 0xD0D2066969B9696F, 0x2D12410909240948, 0xADE0D77070DD70A7, 0x54716FB6B6E2B6D9, 0xB7BD1ED0D067D0CE, 0x7EC7D6EDED93ED3B, 0xDB85E2CCCC17CC2E, 0x578468424215422A, 0xC22D2C98985A98B4, 0x0E55EDA4A4AAA449, 0x8850752828A0285D, 0x31B8865C5C6D5CDA, 0x3FED6BF8F8C7F893, 0xA411C28686228644 }; const u64bit Whirlpool::C4[256] = { 0xC07830D818186018, 0x05AF462623238C23, 0x7EF991B8C6C63FC6, 0x136FCDFBE8E887E8, 0x4CA113CB87872687, 0xA9626D11B8B8DAB8, 0x0805020901010401, 0x426E9E0D4F4F214F, 0xADEE6C9B3636D836, 0x590451FFA6A6A2A6, 0xDEBDB90CD2D26FD2, 0xFB06F70EF5F5F3F5, 0xEF80F2967979F979, 0x5FCEDE306F6FA16F, 0xFCEF3F6D91917E91, 0xAA07A4F852525552, 0x27FDC04760609D60, 0x89766535BCBCCABC, 0xACCD2B379B9B569B, 0x048C018A8E8E028E, 0x71155BD2A3A3B6A3, 0x603C186C0C0C300C, 0xFF8AF6847B7BF17B, 0xB5E16A803535D435, 0xE8693AF51D1D741D, 0x5347DDB3E0E0A7E0, 0xF6ACB321D7D77BD7, 0x5EED999CC2C22FC2, 0x6D965C432E2EB82E, 0x627A96294B4B314B, 0xA321E15DFEFEDFFE, 0x8216AED557574157, 0xA8412ABD15155415, 0x9FB6EEE87777C177, 0xA5EB6E923737DC37, 0x7B56D79EE5E5B3E5, 0x8CD923139F9F469F, 0xD317FD23F0F0E7F0, 0x6A7F94204A4A354A, 0x9E95A944DADA4FDA, 0xFA25B0A258587D58, 0x06CA8FCFC9C903C9, 0x558D527C2929A429, 0x5022145A0A0A280A, 0xE14F7F50B1B1FEB1, 0x691A5DC9A0A0BAA0, 0x7FDAD6146B6BB16B, 0x5CAB17D985852E85, 0x8173673CBDBDCEBD, 0xD234BA8F5D5D695D, 0x8050209010104010, 0xF303F507F4F4F7F4, 0x16C08BDDCBCB0BCB, 0xEDC67CD33E3EF83E, 0x28110A2D05051405, 0x1FE6CE7867678167, 0x7353D597E4E4B7E4, 0x25BB4E0227279C27, 0x3258827341411941, 0x2C9D0BA78B8B168B, 0x510153F6A7A7A6A7, 0xCF94FAB27D7DE97D, 0xDCFB374995956E95, 0x8E9FAD56D8D847D8, 0x8B30EB70FBFBCBFB, 0x2371C1CDEEEE9FEE, 0xC791F8BB7C7CED7C, 0x17E3CC7166668566, 0xA68EA77BDDDD53DD, 0xB84B2EAF17175C17, 0x02468E4547470147, 0x84DC211A9E9E429E, 0x1EC589D4CACA0FCA, 0x75995A582D2DB42D, 0x9179632EBFBFC6BF, 0x381B0E3F07071C07, 0x012347ACADAD8EAD, 0xEA2FB4B05A5A755A, 0x6CB51BEF83833683, 0x85FF66B63333CC33, 0x3FF2C65C63639163, 0x100A041202020802, 0x39384993AAAA92AA, 0xAFA8E2DE7171D971, 0x0ECF8DC6C8C807C8, 0xC87D32D119196419, 0x7270923B49493949, 0x869AAF5FD9D943D9, 0xC31DF931F2F2EFF2, 0x4B48DBA8E3E3ABE3, 0xE22AB6B95B5B715B, 0x34920DBC88881A88, 0xA4C8293E9A9A529A, 0x2DBE4C0B26269826, 0x8DFA64BF3232C832, 0xE94A7D59B0B0FAB0, 0x1B6ACFF2E9E983E9, 0x78331E770F0F3C0F, 0xE6A6B733D5D573D5, 0x74BA1DF480803A80, 0x997C6127BEBEC2BE, 0x26DE87EBCDCD13CD, 0xBDE468893434D034, 0x7A75903248483D48, 0xAB24E354FFFFDBFF, 0xF78FF48D7A7AF57A, 0xF4EA3D6490907A90, 0xC23EBE9D5F5F615F, 0x1DA0403D20208020, 0x67D5D00F6868BD68, 0xD07234CA1A1A681A, 0x192C41B7AEAE82AE, 0xC95E757DB4B4EAB4, 0x9A19A8CE54544D54, 0xECE53B7F93937693, 0x0DAA442F22228822, 0x07E9C86364648D64, 0xDB12FF2AF1F1E3F1, 0xBFA2E6CC7373D173, 0x905A248212124812, 0x3A5D807A40401D40, 0x4028104808082008, 0x56E89B95C3C32BC3, 0x337BC5DFECEC97EC, 0x9690AB4DDBDB4BDB, 0x611F5FC0A1A1BEA1, 0x1C8307918D8D0E8D, 0xF5C97AC83D3DF43D, 0xCCF1335B97976697, 0x0000000000000000, 0x36D483F9CFCF1BCF, 0x4587566E2B2BAC2B, 0x97B3ECE17676C576, 0x64B019E682823282, 0xFEA9B128D6D67FD6, 0xD87736C31B1B6C1B, 0xC15B7774B5B5EEB5, 0x112943BEAFAF86AF, 0x77DFD41D6A6AB56A, 0xBA0DA0EA50505D50, 0x124C8A5745450945, 0xCB18FB38F3F3EBF3, 0x9DF060AD3030C030, 0x2B74C3C4EFEF9BEF, 0xE5C37EDA3F3FFC3F, 0x921CAAC755554955, 0x791059DBA2A2B2A2, 0x0365C9E9EAEA8FEA, 0x0FECCA6A65658965, 0xB9686903BABAD2BA, 0x65935E4A2F2FBC2F, 0x4EE79D8EC0C027C0, 0xBE81A160DEDE5FDE, 0xE06C38FC1C1C701C, 0xBB2EE746FDFDD3FD, 0x52649A1F4D4D294D, 0xE4E0397692927292, 0x8FBCEAFA7575C975, 0x301E0C3606061806, 0x249809AE8A8A128A, 0xF940794BB2B2F2B2, 0x6359D185E6E6BFE6, 0x70361C7E0E0E380E, 0xF8633EE71F1F7C1F, 0x37F7C45562629562, 0xEEA3B53AD4D477D4, 0x29324D81A8A89AA8, 0xC4F4315296966296, 0x9B3AEF62F9F9C3F9, 0x66F697A3C5C533C5, 0x35B14A1025259425, 0xF220B2AB59597959, 0x54AE15D084842A84, 0xB7A7E4C57272D572, 0xD5DD72EC3939E439, 0x5A6198164C4C2D4C, 0xCA3BBC945E5E655E, 0xE785F09F7878FD78, 0xDDD870E53838E038, 0x148605988C8C0A8C, 0xC6B2BF17D1D163D1, 0x410B57E4A5A5AEA5, 0x434DD9A1E2E2AFE2, 0x2FF8C24E61619961, 0xF1457B42B3B3F6B3, 0x15A5423421218421, 0x94D625089C9C4A9C, 0xF0663CEE1E1E781E, 0x2252866143431143, 0x76FC93B1C7C73BC7, 0xB32BE54FFCFCD7FC, 0x2014082404041004, 0xB208A2E351515951, 0xBCC72F2599995E99, 0x4FC4DA226D6DA96D, 0x68391A650D0D340D, 0x8335E979FAFACFFA, 0xB684A369DFDF5BDF, 0xD79BFCA97E7EE57E, 0x3DB4481924249024, 0xC5D776FE3B3BEC3B, 0x313D4B9AABAB96AB, 0x3ED181F0CECE1FCE, 0x8855229911114411, 0x0C8903838F8F068F, 0x4A6B9C044E4E254E, 0xD1517366B7B7E6B7, 0x0B60CBE0EBEB8BEB, 0xFDCC78C13C3CF03C, 0x7CBF1FFD81813E81, 0xD4FE354094946A94, 0xEB0CF31CF7F7FBF7, 0xA1676F18B9B9DEB9, 0x985F268B13134C13, 0x7D9C58512C2CB02C, 0xD6B8BB05D3D36BD3, 0x6B5CD38CE7E7BBE7, 0x57CBDC396E6EA56E, 0x6EF395AAC4C437C4, 0x180F061B03030C03, 0x8A13ACDC56564556, 0x1A49885E44440D44, 0xDF9EFEA07F7FE17F, 0x21374F88A9A99EA9, 0x4D8254672A2AA82A, 0xB16D6B0ABBBBD6BB, 0x46E29F87C1C123C1, 0xA202A6F153535153, 0xAE8BA572DCDC57DC, 0x582716530B0B2C0B, 0x9CD327019D9D4E9D, 0x47C1D82B6C6CAD6C, 0x95F562A43131C431, 0x87B9E8F37474CD74, 0xE309F115F6F6FFF6, 0x0A438C4C46460546, 0x092645A5ACAC8AAC, 0x3C970FB589891E89, 0xA04428B414145014, 0x5B42DFBAE1E1A3E1, 0xB04E2CA616165816, 0xCDD274F73A3AE83A, 0x6FD0D2066969B969, 0x482D124109092409, 0xA7ADE0D77070DD70, 0xD954716FB6B6E2B6, 0xCEB7BD1ED0D067D0, 0x3B7EC7D6EDED93ED, 0x2EDB85E2CCCC17CC, 0x2A57846842421542, 0xB4C22D2C98985A98, 0x490E55EDA4A4AAA4, 0x5D8850752828A028, 0xDA31B8865C5C6D5C, 0x933FED6BF8F8C7F8, 0x44A411C286862286 }; const u64bit Whirlpool::C5[256] = { 0x18C07830D8181860, 0x2305AF462623238C, 0xC67EF991B8C6C63F, 0xE8136FCDFBE8E887, 0x874CA113CB878726, 0xB8A9626D11B8B8DA, 0x0108050209010104, 0x4F426E9E0D4F4F21, 0x36ADEE6C9B3636D8, 0xA6590451FFA6A6A2, 0xD2DEBDB90CD2D26F, 0xF5FB06F70EF5F5F3, 0x79EF80F2967979F9, 0x6F5FCEDE306F6FA1, 0x91FCEF3F6D91917E, 0x52AA07A4F8525255, 0x6027FDC04760609D, 0xBC89766535BCBCCA, 0x9BACCD2B379B9B56, 0x8E048C018A8E8E02, 0xA371155BD2A3A3B6, 0x0C603C186C0C0C30, 0x7BFF8AF6847B7BF1, 0x35B5E16A803535D4, 0x1DE8693AF51D1D74, 0xE05347DDB3E0E0A7, 0xD7F6ACB321D7D77B, 0xC25EED999CC2C22F, 0x2E6D965C432E2EB8, 0x4B627A96294B4B31, 0xFEA321E15DFEFEDF, 0x578216AED5575741, 0x15A8412ABD151554, 0x779FB6EEE87777C1, 0x37A5EB6E923737DC, 0xE57B56D79EE5E5B3, 0x9F8CD923139F9F46, 0xF0D317FD23F0F0E7, 0x4A6A7F94204A4A35, 0xDA9E95A944DADA4F, 0x58FA25B0A258587D, 0xC906CA8FCFC9C903, 0x29558D527C2929A4, 0x0A5022145A0A0A28, 0xB1E14F7F50B1B1FE, 0xA0691A5DC9A0A0BA, 0x6B7FDAD6146B6BB1, 0x855CAB17D985852E, 0xBD8173673CBDBDCE, 0x5DD234BA8F5D5D69, 0x1080502090101040, 0xF4F303F507F4F4F7, 0xCB16C08BDDCBCB0B, 0x3EEDC67CD33E3EF8, 0x0528110A2D050514, 0x671FE6CE78676781, 0xE47353D597E4E4B7, 0x2725BB4E0227279C, 0x4132588273414119, 0x8B2C9D0BA78B8B16, 0xA7510153F6A7A7A6, 0x7DCF94FAB27D7DE9, 0x95DCFB374995956E, 0xD88E9FAD56D8D847, 0xFB8B30EB70FBFBCB, 0xEE2371C1CDEEEE9F, 0x7CC791F8BB7C7CED, 0x6617E3CC71666685, 0xDDA68EA77BDDDD53, 0x17B84B2EAF17175C, 0x4702468E45474701, 0x9E84DC211A9E9E42, 0xCA1EC589D4CACA0F, 0x2D75995A582D2DB4, 0xBF9179632EBFBFC6, 0x07381B0E3F07071C, 0xAD012347ACADAD8E, 0x5AEA2FB4B05A5A75, 0x836CB51BEF838336, 0x3385FF66B63333CC, 0x633FF2C65C636391, 0x02100A0412020208, 0xAA39384993AAAA92, 0x71AFA8E2DE7171D9, 0xC80ECF8DC6C8C807, 0x19C87D32D1191964, 0x497270923B494939, 0xD9869AAF5FD9D943, 0xF2C31DF931F2F2EF, 0xE34B48DBA8E3E3AB, 0x5BE22AB6B95B5B71, 0x8834920DBC88881A, 0x9AA4C8293E9A9A52, 0x262DBE4C0B262698, 0x328DFA64BF3232C8, 0xB0E94A7D59B0B0FA, 0xE91B6ACFF2E9E983, 0x0F78331E770F0F3C, 0xD5E6A6B733D5D573, 0x8074BA1DF480803A, 0xBE997C6127BEBEC2, 0xCD26DE87EBCDCD13, 0x34BDE468893434D0, 0x487A75903248483D, 0xFFAB24E354FFFFDB, 0x7AF78FF48D7A7AF5, 0x90F4EA3D6490907A, 0x5FC23EBE9D5F5F61, 0x201DA0403D202080, 0x6867D5D00F6868BD, 0x1AD07234CA1A1A68, 0xAE192C41B7AEAE82, 0xB4C95E757DB4B4EA, 0x549A19A8CE54544D, 0x93ECE53B7F939376, 0x220DAA442F222288, 0x6407E9C86364648D, 0xF1DB12FF2AF1F1E3, 0x73BFA2E6CC7373D1, 0x12905A2482121248, 0x403A5D807A40401D, 0x0840281048080820, 0xC356E89B95C3C32B, 0xEC337BC5DFECEC97, 0xDB9690AB4DDBDB4B, 0xA1611F5FC0A1A1BE, 0x8D1C8307918D8D0E, 0x3DF5C97AC83D3DF4, 0x97CCF1335B979766, 0x0000000000000000, 0xCF36D483F9CFCF1B, 0x2B4587566E2B2BAC, 0x7697B3ECE17676C5, 0x8264B019E6828232, 0xD6FEA9B128D6D67F, 0x1BD87736C31B1B6C, 0xB5C15B7774B5B5EE, 0xAF112943BEAFAF86, 0x6A77DFD41D6A6AB5, 0x50BA0DA0EA50505D, 0x45124C8A57454509, 0xF3CB18FB38F3F3EB, 0x309DF060AD3030C0, 0xEF2B74C3C4EFEF9B, 0x3FE5C37EDA3F3FFC, 0x55921CAAC7555549, 0xA2791059DBA2A2B2, 0xEA0365C9E9EAEA8F, 0x650FECCA6A656589, 0xBAB9686903BABAD2, 0x2F65935E4A2F2FBC, 0xC04EE79D8EC0C027, 0xDEBE81A160DEDE5F, 0x1CE06C38FC1C1C70, 0xFDBB2EE746FDFDD3, 0x4D52649A1F4D4D29, 0x92E4E03976929272, 0x758FBCEAFA7575C9, 0x06301E0C36060618, 0x8A249809AE8A8A12, 0xB2F940794BB2B2F2, 0xE66359D185E6E6BF, 0x0E70361C7E0E0E38, 0x1FF8633EE71F1F7C, 0x6237F7C455626295, 0xD4EEA3B53AD4D477, 0xA829324D81A8A89A, 0x96C4F43152969662, 0xF99B3AEF62F9F9C3, 0xC566F697A3C5C533, 0x2535B14A10252594, 0x59F220B2AB595979, 0x8454AE15D084842A, 0x72B7A7E4C57272D5, 0x39D5DD72EC3939E4, 0x4C5A6198164C4C2D, 0x5ECA3BBC945E5E65, 0x78E785F09F7878FD, 0x38DDD870E53838E0, 0x8C148605988C8C0A, 0xD1C6B2BF17D1D163, 0xA5410B57E4A5A5AE, 0xE2434DD9A1E2E2AF, 0x612FF8C24E616199, 0xB3F1457B42B3B3F6, 0x2115A54234212184, 0x9C94D625089C9C4A, 0x1EF0663CEE1E1E78, 0x4322528661434311, 0xC776FC93B1C7C73B, 0xFCB32BE54FFCFCD7, 0x0420140824040410, 0x51B208A2E3515159, 0x99BCC72F2599995E, 0x6D4FC4DA226D6DA9, 0x0D68391A650D0D34, 0xFA8335E979FAFACF, 0xDFB684A369DFDF5B, 0x7ED79BFCA97E7EE5, 0x243DB44819242490, 0x3BC5D776FE3B3BEC, 0xAB313D4B9AABAB96, 0xCE3ED181F0CECE1F, 0x1188552299111144, 0x8F0C8903838F8F06, 0x4E4A6B9C044E4E25, 0xB7D1517366B7B7E6, 0xEB0B60CBE0EBEB8B, 0x3CFDCC78C13C3CF0, 0x817CBF1FFD81813E, 0x94D4FE354094946A, 0xF7EB0CF31CF7F7FB, 0xB9A1676F18B9B9DE, 0x13985F268B13134C, 0x2C7D9C58512C2CB0, 0xD3D6B8BB05D3D36B, 0xE76B5CD38CE7E7BB, 0x6E57CBDC396E6EA5, 0xC46EF395AAC4C437, 0x03180F061B03030C, 0x568A13ACDC565645, 0x441A49885E44440D, 0x7FDF9EFEA07F7FE1, 0xA921374F88A9A99E, 0x2A4D8254672A2AA8, 0xBBB16D6B0ABBBBD6, 0xC146E29F87C1C123, 0x53A202A6F1535351, 0xDCAE8BA572DCDC57, 0x0B582716530B0B2C, 0x9D9CD327019D9D4E, 0x6C47C1D82B6C6CAD, 0x3195F562A43131C4, 0x7487B9E8F37474CD, 0xF6E309F115F6F6FF, 0x460A438C4C464605, 0xAC092645A5ACAC8A, 0x893C970FB589891E, 0x14A04428B4141450, 0xE15B42DFBAE1E1A3, 0x16B04E2CA6161658, 0x3ACDD274F73A3AE8, 0x696FD0D2066969B9, 0x09482D1241090924, 0x70A7ADE0D77070DD, 0xB6D954716FB6B6E2, 0xD0CEB7BD1ED0D067, 0xED3B7EC7D6EDED93, 0xCC2EDB85E2CCCC17, 0x422A578468424215, 0x98B4C22D2C98985A, 0xA4490E55EDA4A4AA, 0x285D8850752828A0, 0x5CDA31B8865C5C6D, 0xF8933FED6BF8F8C7, 0x8644A411C2868622 }; const u64bit Whirlpool::C6[256] = { 0x6018C07830D81818, 0x8C2305AF46262323, 0x3FC67EF991B8C6C6, 0x87E8136FCDFBE8E8, 0x26874CA113CB8787, 0xDAB8A9626D11B8B8, 0x0401080502090101, 0x214F426E9E0D4F4F, 0xD836ADEE6C9B3636, 0xA2A6590451FFA6A6, 0x6FD2DEBDB90CD2D2, 0xF3F5FB06F70EF5F5, 0xF979EF80F2967979, 0xA16F5FCEDE306F6F, 0x7E91FCEF3F6D9191, 0x5552AA07A4F85252, 0x9D6027FDC0476060, 0xCABC89766535BCBC, 0x569BACCD2B379B9B, 0x028E048C018A8E8E, 0xB6A371155BD2A3A3, 0x300C603C186C0C0C, 0xF17BFF8AF6847B7B, 0xD435B5E16A803535, 0x741DE8693AF51D1D, 0xA7E05347DDB3E0E0, 0x7BD7F6ACB321D7D7, 0x2FC25EED999CC2C2, 0xB82E6D965C432E2E, 0x314B627A96294B4B, 0xDFFEA321E15DFEFE, 0x41578216AED55757, 0x5415A8412ABD1515, 0xC1779FB6EEE87777, 0xDC37A5EB6E923737, 0xB3E57B56D79EE5E5, 0x469F8CD923139F9F, 0xE7F0D317FD23F0F0, 0x354A6A7F94204A4A, 0x4FDA9E95A944DADA, 0x7D58FA25B0A25858, 0x03C906CA8FCFC9C9, 0xA429558D527C2929, 0x280A5022145A0A0A, 0xFEB1E14F7F50B1B1, 0xBAA0691A5DC9A0A0, 0xB16B7FDAD6146B6B, 0x2E855CAB17D98585, 0xCEBD8173673CBDBD, 0x695DD234BA8F5D5D, 0x4010805020901010, 0xF7F4F303F507F4F4, 0x0BCB16C08BDDCBCB, 0xF83EEDC67CD33E3E, 0x140528110A2D0505, 0x81671FE6CE786767, 0xB7E47353D597E4E4, 0x9C2725BB4E022727, 0x1941325882734141, 0x168B2C9D0BA78B8B, 0xA6A7510153F6A7A7, 0xE97DCF94FAB27D7D, 0x6E95DCFB37499595, 0x47D88E9FAD56D8D8, 0xCBFB8B30EB70FBFB, 0x9FEE2371C1CDEEEE, 0xED7CC791F8BB7C7C, 0x856617E3CC716666, 0x53DDA68EA77BDDDD, 0x5C17B84B2EAF1717, 0x014702468E454747, 0x429E84DC211A9E9E, 0x0FCA1EC589D4CACA, 0xB42D75995A582D2D, 0xC6BF9179632EBFBF, 0x1C07381B0E3F0707, 0x8EAD012347ACADAD, 0x755AEA2FB4B05A5A, 0x36836CB51BEF8383, 0xCC3385FF66B63333, 0x91633FF2C65C6363, 0x0802100A04120202, 0x92AA39384993AAAA, 0xD971AFA8E2DE7171, 0x07C80ECF8DC6C8C8, 0x6419C87D32D11919, 0x39497270923B4949, 0x43D9869AAF5FD9D9, 0xEFF2C31DF931F2F2, 0xABE34B48DBA8E3E3, 0x715BE22AB6B95B5B, 0x1A8834920DBC8888, 0x529AA4C8293E9A9A, 0x98262DBE4C0B2626, 0xC8328DFA64BF3232, 0xFAB0E94A7D59B0B0, 0x83E91B6ACFF2E9E9, 0x3C0F78331E770F0F, 0x73D5E6A6B733D5D5, 0x3A8074BA1DF48080, 0xC2BE997C6127BEBE, 0x13CD26DE87EBCDCD, 0xD034BDE468893434, 0x3D487A7590324848, 0xDBFFAB24E354FFFF, 0xF57AF78FF48D7A7A, 0x7A90F4EA3D649090, 0x615FC23EBE9D5F5F, 0x80201DA0403D2020, 0xBD6867D5D00F6868, 0x681AD07234CA1A1A, 0x82AE192C41B7AEAE, 0xEAB4C95E757DB4B4, 0x4D549A19A8CE5454, 0x7693ECE53B7F9393, 0x88220DAA442F2222, 0x8D6407E9C8636464, 0xE3F1DB12FF2AF1F1, 0xD173BFA2E6CC7373, 0x4812905A24821212, 0x1D403A5D807A4040, 0x2008402810480808, 0x2BC356E89B95C3C3, 0x97EC337BC5DFECEC, 0x4BDB9690AB4DDBDB, 0xBEA1611F5FC0A1A1, 0x0E8D1C8307918D8D, 0xF43DF5C97AC83D3D, 0x6697CCF1335B9797, 0x0000000000000000, 0x1BCF36D483F9CFCF, 0xAC2B4587566E2B2B, 0xC57697B3ECE17676, 0x328264B019E68282, 0x7FD6FEA9B128D6D6, 0x6C1BD87736C31B1B, 0xEEB5C15B7774B5B5, 0x86AF112943BEAFAF, 0xB56A77DFD41D6A6A, 0x5D50BA0DA0EA5050, 0x0945124C8A574545, 0xEBF3CB18FB38F3F3, 0xC0309DF060AD3030, 0x9BEF2B74C3C4EFEF, 0xFC3FE5C37EDA3F3F, 0x4955921CAAC75555, 0xB2A2791059DBA2A2, 0x8FEA0365C9E9EAEA, 0x89650FECCA6A6565, 0xD2BAB9686903BABA, 0xBC2F65935E4A2F2F, 0x27C04EE79D8EC0C0, 0x5FDEBE81A160DEDE, 0x701CE06C38FC1C1C, 0xD3FDBB2EE746FDFD, 0x294D52649A1F4D4D, 0x7292E4E039769292, 0xC9758FBCEAFA7575, 0x1806301E0C360606, 0x128A249809AE8A8A, 0xF2B2F940794BB2B2, 0xBFE66359D185E6E6, 0x380E70361C7E0E0E, 0x7C1FF8633EE71F1F, 0x956237F7C4556262, 0x77D4EEA3B53AD4D4, 0x9AA829324D81A8A8, 0x6296C4F431529696, 0xC3F99B3AEF62F9F9, 0x33C566F697A3C5C5, 0x942535B14A102525, 0x7959F220B2AB5959, 0x2A8454AE15D08484, 0xD572B7A7E4C57272, 0xE439D5DD72EC3939, 0x2D4C5A6198164C4C, 0x655ECA3BBC945E5E, 0xFD78E785F09F7878, 0xE038DDD870E53838, 0x0A8C148605988C8C, 0x63D1C6B2BF17D1D1, 0xAEA5410B57E4A5A5, 0xAFE2434DD9A1E2E2, 0x99612FF8C24E6161, 0xF6B3F1457B42B3B3, 0x842115A542342121, 0x4A9C94D625089C9C, 0x781EF0663CEE1E1E, 0x1143225286614343, 0x3BC776FC93B1C7C7, 0xD7FCB32BE54FFCFC, 0x1004201408240404, 0x5951B208A2E35151, 0x5E99BCC72F259999, 0xA96D4FC4DA226D6D, 0x340D68391A650D0D, 0xCFFA8335E979FAFA, 0x5BDFB684A369DFDF, 0xE57ED79BFCA97E7E, 0x90243DB448192424, 0xEC3BC5D776FE3B3B, 0x96AB313D4B9AABAB, 0x1FCE3ED181F0CECE, 0x4411885522991111, 0x068F0C8903838F8F, 0x254E4A6B9C044E4E, 0xE6B7D1517366B7B7, 0x8BEB0B60CBE0EBEB, 0xF03CFDCC78C13C3C, 0x3E817CBF1FFD8181, 0x6A94D4FE35409494, 0xFBF7EB0CF31CF7F7, 0xDEB9A1676F18B9B9, 0x4C13985F268B1313, 0xB02C7D9C58512C2C, 0x6BD3D6B8BB05D3D3, 0xBBE76B5CD38CE7E7, 0xA56E57CBDC396E6E, 0x37C46EF395AAC4C4, 0x0C03180F061B0303, 0x45568A13ACDC5656, 0x0D441A49885E4444, 0xE17FDF9EFEA07F7F, 0x9EA921374F88A9A9, 0xA82A4D8254672A2A, 0xD6BBB16D6B0ABBBB, 0x23C146E29F87C1C1, 0x5153A202A6F15353, 0x57DCAE8BA572DCDC, 0x2C0B582716530B0B, 0x4E9D9CD327019D9D, 0xAD6C47C1D82B6C6C, 0xC43195F562A43131, 0xCD7487B9E8F37474, 0xFFF6E309F115F6F6, 0x05460A438C4C4646, 0x8AAC092645A5ACAC, 0x1E893C970FB58989, 0x5014A04428B41414, 0xA3E15B42DFBAE1E1, 0x5816B04E2CA61616, 0xE83ACDD274F73A3A, 0xB9696FD0D2066969, 0x2409482D12410909, 0xDD70A7ADE0D77070, 0xE2B6D954716FB6B6, 0x67D0CEB7BD1ED0D0, 0x93ED3B7EC7D6EDED, 0x17CC2EDB85E2CCCC, 0x15422A5784684242, 0x5A98B4C22D2C9898, 0xAAA4490E55EDA4A4, 0xA0285D8850752828, 0x6D5CDA31B8865C5C, 0xC7F8933FED6BF8F8, 0x228644A411C28686 }; const u64bit Whirlpool::C7[256] = { 0x186018C07830D818, 0x238C2305AF462623, 0xC63FC67EF991B8C6, 0xE887E8136FCDFBE8, 0x8726874CA113CB87, 0xB8DAB8A9626D11B8, 0x0104010805020901, 0x4F214F426E9E0D4F, 0x36D836ADEE6C9B36, 0xA6A2A6590451FFA6, 0xD26FD2DEBDB90CD2, 0xF5F3F5FB06F70EF5, 0x79F979EF80F29679, 0x6FA16F5FCEDE306F, 0x917E91FCEF3F6D91, 0x525552AA07A4F852, 0x609D6027FDC04760, 0xBCCABC89766535BC, 0x9B569BACCD2B379B, 0x8E028E048C018A8E, 0xA3B6A371155BD2A3, 0x0C300C603C186C0C, 0x7BF17BFF8AF6847B, 0x35D435B5E16A8035, 0x1D741DE8693AF51D, 0xE0A7E05347DDB3E0, 0xD77BD7F6ACB321D7, 0xC22FC25EED999CC2, 0x2EB82E6D965C432E, 0x4B314B627A96294B, 0xFEDFFEA321E15DFE, 0x5741578216AED557, 0x155415A8412ABD15, 0x77C1779FB6EEE877, 0x37DC37A5EB6E9237, 0xE5B3E57B56D79EE5, 0x9F469F8CD923139F, 0xF0E7F0D317FD23F0, 0x4A354A6A7F94204A, 0xDA4FDA9E95A944DA, 0x587D58FA25B0A258, 0xC903C906CA8FCFC9, 0x29A429558D527C29, 0x0A280A5022145A0A, 0xB1FEB1E14F7F50B1, 0xA0BAA0691A5DC9A0, 0x6BB16B7FDAD6146B, 0x852E855CAB17D985, 0xBDCEBD8173673CBD, 0x5D695DD234BA8F5D, 0x1040108050209010, 0xF4F7F4F303F507F4, 0xCB0BCB16C08BDDCB, 0x3EF83EEDC67CD33E, 0x05140528110A2D05, 0x6781671FE6CE7867, 0xE4B7E47353D597E4, 0x279C2725BB4E0227, 0x4119413258827341, 0x8B168B2C9D0BA78B, 0xA7A6A7510153F6A7, 0x7DE97DCF94FAB27D, 0x956E95DCFB374995, 0xD847D88E9FAD56D8, 0xFBCBFB8B30EB70FB, 0xEE9FEE2371C1CDEE, 0x7CED7CC791F8BB7C, 0x66856617E3CC7166, 0xDD53DDA68EA77BDD, 0x175C17B84B2EAF17, 0x47014702468E4547, 0x9E429E84DC211A9E, 0xCA0FCA1EC589D4CA, 0x2DB42D75995A582D, 0xBFC6BF9179632EBF, 0x071C07381B0E3F07, 0xAD8EAD012347ACAD, 0x5A755AEA2FB4B05A, 0x8336836CB51BEF83, 0x33CC3385FF66B633, 0x6391633FF2C65C63, 0x020802100A041202, 0xAA92AA39384993AA, 0x71D971AFA8E2DE71, 0xC807C80ECF8DC6C8, 0x196419C87D32D119, 0x4939497270923B49, 0xD943D9869AAF5FD9, 0xF2EFF2C31DF931F2, 0xE3ABE34B48DBA8E3, 0x5B715BE22AB6B95B, 0x881A8834920DBC88, 0x9A529AA4C8293E9A, 0x2698262DBE4C0B26, 0x32C8328DFA64BF32, 0xB0FAB0E94A7D59B0, 0xE983E91B6ACFF2E9, 0x0F3C0F78331E770F, 0xD573D5E6A6B733D5, 0x803A8074BA1DF480, 0xBEC2BE997C6127BE, 0xCD13CD26DE87EBCD, 0x34D034BDE4688934, 0x483D487A75903248, 0xFFDBFFAB24E354FF, 0x7AF57AF78FF48D7A, 0x907A90F4EA3D6490, 0x5F615FC23EBE9D5F, 0x2080201DA0403D20, 0x68BD6867D5D00F68, 0x1A681AD07234CA1A, 0xAE82AE192C41B7AE, 0xB4EAB4C95E757DB4, 0x544D549A19A8CE54, 0x937693ECE53B7F93, 0x2288220DAA442F22, 0x648D6407E9C86364, 0xF1E3F1DB12FF2AF1, 0x73D173BFA2E6CC73, 0x124812905A248212, 0x401D403A5D807A40, 0x0820084028104808, 0xC32BC356E89B95C3, 0xEC97EC337BC5DFEC, 0xDB4BDB9690AB4DDB, 0xA1BEA1611F5FC0A1, 0x8D0E8D1C8307918D, 0x3DF43DF5C97AC83D, 0x976697CCF1335B97, 0x0000000000000000, 0xCF1BCF36D483F9CF, 0x2BAC2B4587566E2B, 0x76C57697B3ECE176, 0x82328264B019E682, 0xD67FD6FEA9B128D6, 0x1B6C1BD87736C31B, 0xB5EEB5C15B7774B5, 0xAF86AF112943BEAF, 0x6AB56A77DFD41D6A, 0x505D50BA0DA0EA50, 0x450945124C8A5745, 0xF3EBF3CB18FB38F3, 0x30C0309DF060AD30, 0xEF9BEF2B74C3C4EF, 0x3FFC3FE5C37EDA3F, 0x554955921CAAC755, 0xA2B2A2791059DBA2, 0xEA8FEA0365C9E9EA, 0x6589650FECCA6A65, 0xBAD2BAB9686903BA, 0x2FBC2F65935E4A2F, 0xC027C04EE79D8EC0, 0xDE5FDEBE81A160DE, 0x1C701CE06C38FC1C, 0xFDD3FDBB2EE746FD, 0x4D294D52649A1F4D, 0x927292E4E0397692, 0x75C9758FBCEAFA75, 0x061806301E0C3606, 0x8A128A249809AE8A, 0xB2F2B2F940794BB2, 0xE6BFE66359D185E6, 0x0E380E70361C7E0E, 0x1F7C1FF8633EE71F, 0x62956237F7C45562, 0xD477D4EEA3B53AD4, 0xA89AA829324D81A8, 0x966296C4F4315296, 0xF9C3F99B3AEF62F9, 0xC533C566F697A3C5, 0x25942535B14A1025, 0x597959F220B2AB59, 0x842A8454AE15D084, 0x72D572B7A7E4C572, 0x39E439D5DD72EC39, 0x4C2D4C5A6198164C, 0x5E655ECA3BBC945E, 0x78FD78E785F09F78, 0x38E038DDD870E538, 0x8C0A8C148605988C, 0xD163D1C6B2BF17D1, 0xA5AEA5410B57E4A5, 0xE2AFE2434DD9A1E2, 0x6199612FF8C24E61, 0xB3F6B3F1457B42B3, 0x21842115A5423421, 0x9C4A9C94D625089C, 0x1E781EF0663CEE1E, 0x4311432252866143, 0xC73BC776FC93B1C7, 0xFCD7FCB32BE54FFC, 0x0410042014082404, 0x515951B208A2E351, 0x995E99BCC72F2599, 0x6DA96D4FC4DA226D, 0x0D340D68391A650D, 0xFACFFA8335E979FA, 0xDF5BDFB684A369DF, 0x7EE57ED79BFCA97E, 0x2490243DB4481924, 0x3BEC3BC5D776FE3B, 0xAB96AB313D4B9AAB, 0xCE1FCE3ED181F0CE, 0x1144118855229911, 0x8F068F0C8903838F, 0x4E254E4A6B9C044E, 0xB7E6B7D1517366B7, 0xEB8BEB0B60CBE0EB, 0x3CF03CFDCC78C13C, 0x813E817CBF1FFD81, 0x946A94D4FE354094, 0xF7FBF7EB0CF31CF7, 0xB9DEB9A1676F18B9, 0x134C13985F268B13, 0x2CB02C7D9C58512C, 0xD36BD3D6B8BB05D3, 0xE7BBE76B5CD38CE7, 0x6EA56E57CBDC396E, 0xC437C46EF395AAC4, 0x030C03180F061B03, 0x5645568A13ACDC56, 0x440D441A49885E44, 0x7FE17FDF9EFEA07F, 0xA99EA921374F88A9, 0x2AA82A4D8254672A, 0xBBD6BBB16D6B0ABB, 0xC123C146E29F87C1, 0x535153A202A6F153, 0xDC57DCAE8BA572DC, 0x0B2C0B582716530B, 0x9D4E9D9CD327019D, 0x6CAD6C47C1D82B6C, 0x31C43195F562A431, 0x74CD7487B9E8F374, 0xF6FFF6E309F115F6, 0x4605460A438C4C46, 0xAC8AAC092645A5AC, 0x891E893C970FB589, 0x145014A04428B414, 0xE1A3E15B42DFBAE1, 0x165816B04E2CA616, 0x3AE83ACDD274F73A, 0x69B9696FD0D20669, 0x092409482D124109, 0x70DD70A7ADE0D770, 0xB6E2B6D954716FB6, 0xD067D0CEB7BD1ED0, 0xED93ED3B7EC7D6ED, 0xCC17CC2EDB85E2CC, 0x4215422A57846842, 0x985A98B4C22D2C98, 0xA4AAA4490E55EDA4, 0x28A0285D88507528, 0x5C6D5CDA31B8865C, 0xF8C7F8933FED6BF8, 0x86228644A411C286 }; } /* * Whirlpool * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Whirlpool Compression Function */ void Whirlpool::compress_n(const byte in[], size_t blocks) { static const u64bit RC[10] = { 0x1823C6E887B8014F, 0x36A6D2F5796F9152, 0x60BC9B8EA30C7B35, 0x1DE0D7C22E4BFE57, 0x157737E59FF04ADA, 0x58C9290AB1A06B85, 0xBD5D10F4CB3E0567, 0xE427418BA77D95D8, 0xFBEE7C66DD17479E, 0xCA2DBF07AD5A8333 }; for(size_t i = 0; i != blocks; ++i) { load_be(&M[0], in, M.size()); u64bit K0, K1, K2, K3, K4, K5, K6, K7; K0 = digest[0]; K1 = digest[1]; K2 = digest[2]; K3 = digest[3]; K4 = digest[4]; K5 = digest[5]; K6 = digest[6]; K7 = digest[7]; u64bit B0, B1, B2, B3, B4, B5, B6, B7; B0 = K0 ^ M[0]; B1 = K1 ^ M[1]; B2 = K2 ^ M[2]; B3 = K3 ^ M[3]; B4 = K4 ^ M[4]; B5 = K5 ^ M[5]; B6 = K6 ^ M[6]; B7 = K7 ^ M[7]; for(size_t j = 0; j != 10; ++j) { u64bit T0, T1, T2, T3, T4, T5, T6, T7; T0 = C0[get_byte(0, K0)] ^ C1[get_byte(1, K7)] ^ C2[get_byte(2, K6)] ^ C3[get_byte(3, K5)] ^ C4[get_byte(4, K4)] ^ C5[get_byte(5, K3)] ^ C6[get_byte(6, K2)] ^ C7[get_byte(7, K1)] ^ RC[j]; T1 = C0[get_byte(0, K1)] ^ C1[get_byte(1, K0)] ^ C2[get_byte(2, K7)] ^ C3[get_byte(3, K6)] ^ C4[get_byte(4, K5)] ^ C5[get_byte(5, K4)] ^ C6[get_byte(6, K3)] ^ C7[get_byte(7, K2)]; T2 = C0[get_byte(0, K2)] ^ C1[get_byte(1, K1)] ^ C2[get_byte(2, K0)] ^ C3[get_byte(3, K7)] ^ C4[get_byte(4, K6)] ^ C5[get_byte(5, K5)] ^ C6[get_byte(6, K4)] ^ C7[get_byte(7, K3)]; T3 = C0[get_byte(0, K3)] ^ C1[get_byte(1, K2)] ^ C2[get_byte(2, K1)] ^ C3[get_byte(3, K0)] ^ C4[get_byte(4, K7)] ^ C5[get_byte(5, K6)] ^ C6[get_byte(6, K5)] ^ C7[get_byte(7, K4)]; T4 = C0[get_byte(0, K4)] ^ C1[get_byte(1, K3)] ^ C2[get_byte(2, K2)] ^ C3[get_byte(3, K1)] ^ C4[get_byte(4, K0)] ^ C5[get_byte(5, K7)] ^ C6[get_byte(6, K6)] ^ C7[get_byte(7, K5)]; T5 = C0[get_byte(0, K5)] ^ C1[get_byte(1, K4)] ^ C2[get_byte(2, K3)] ^ C3[get_byte(3, K2)] ^ C4[get_byte(4, K1)] ^ C5[get_byte(5, K0)] ^ C6[get_byte(6, K7)] ^ C7[get_byte(7, K6)]; T6 = C0[get_byte(0, K6)] ^ C1[get_byte(1, K5)] ^ C2[get_byte(2, K4)] ^ C3[get_byte(3, K3)] ^ C4[get_byte(4, K2)] ^ C5[get_byte(5, K1)] ^ C6[get_byte(6, K0)] ^ C7[get_byte(7, K7)]; T7 = C0[get_byte(0, K7)] ^ C1[get_byte(1, K6)] ^ C2[get_byte(2, K5)] ^ C3[get_byte(3, K4)] ^ C4[get_byte(4, K3)] ^ C5[get_byte(5, K2)] ^ C6[get_byte(6, K1)] ^ C7[get_byte(7, K0)]; K0 = T0; K1 = T1; K2 = T2; K3 = T3; K4 = T4; K5 = T5; K6 = T6; K7 = T7; T0 = C0[get_byte(0, B0)] ^ C1[get_byte(1, B7)] ^ C2[get_byte(2, B6)] ^ C3[get_byte(3, B5)] ^ C4[get_byte(4, B4)] ^ C5[get_byte(5, B3)] ^ C6[get_byte(6, B2)] ^ C7[get_byte(7, B1)] ^ K0; T1 = C0[get_byte(0, B1)] ^ C1[get_byte(1, B0)] ^ C2[get_byte(2, B7)] ^ C3[get_byte(3, B6)] ^ C4[get_byte(4, B5)] ^ C5[get_byte(5, B4)] ^ C6[get_byte(6, B3)] ^ C7[get_byte(7, B2)] ^ K1; T2 = C0[get_byte(0, B2)] ^ C1[get_byte(1, B1)] ^ C2[get_byte(2, B0)] ^ C3[get_byte(3, B7)] ^ C4[get_byte(4, B6)] ^ C5[get_byte(5, B5)] ^ C6[get_byte(6, B4)] ^ C7[get_byte(7, B3)] ^ K2; T3 = C0[get_byte(0, B3)] ^ C1[get_byte(1, B2)] ^ C2[get_byte(2, B1)] ^ C3[get_byte(3, B0)] ^ C4[get_byte(4, B7)] ^ C5[get_byte(5, B6)] ^ C6[get_byte(6, B5)] ^ C7[get_byte(7, B4)] ^ K3; T4 = C0[get_byte(0, B4)] ^ C1[get_byte(1, B3)] ^ C2[get_byte(2, B2)] ^ C3[get_byte(3, B1)] ^ C4[get_byte(4, B0)] ^ C5[get_byte(5, B7)] ^ C6[get_byte(6, B6)] ^ C7[get_byte(7, B5)] ^ K4; T5 = C0[get_byte(0, B5)] ^ C1[get_byte(1, B4)] ^ C2[get_byte(2, B3)] ^ C3[get_byte(3, B2)] ^ C4[get_byte(4, B1)] ^ C5[get_byte(5, B0)] ^ C6[get_byte(6, B7)] ^ C7[get_byte(7, B6)] ^ K5; T6 = C0[get_byte(0, B6)] ^ C1[get_byte(1, B5)] ^ C2[get_byte(2, B4)] ^ C3[get_byte(3, B3)] ^ C4[get_byte(4, B2)] ^ C5[get_byte(5, B1)] ^ C6[get_byte(6, B0)] ^ C7[get_byte(7, B7)] ^ K6; T7 = C0[get_byte(0, B7)] ^ C1[get_byte(1, B6)] ^ C2[get_byte(2, B5)] ^ C3[get_byte(3, B4)] ^ C4[get_byte(4, B3)] ^ C5[get_byte(5, B2)] ^ C6[get_byte(6, B1)] ^ C7[get_byte(7, B0)] ^ K7; B0 = T0; B1 = T1; B2 = T2; B3 = T3; B4 = T4; B5 = T5; B6 = T6; B7 = T7; } digest[0] ^= B0 ^ M[0]; digest[1] ^= B1 ^ M[1]; digest[2] ^= B2 ^ M[2]; digest[3] ^= B3 ^ M[3]; digest[4] ^= B4 ^ M[4]; digest[5] ^= B5 ^ M[5]; digest[6] ^= B6 ^ M[6]; digest[7] ^= B7 ^ M[7]; in += hash_block_size(); } } /* * Copy out the digest */ void Whirlpool::copy_out(byte output[]) { for(size_t i = 0; i != output_length(); i += 8) store_be(digest[i/8], output + i); } /* * Clear memory of sensitive data */ void Whirlpool::clear() { MDx_HashFunction::clear(); zeroise(M); zeroise(digest); } } /* * KDF Base Class * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Derive a key */ SecureVector<byte> KDF::derive_key(size_t key_len, const MemoryRegion<byte>& secret, const std::string& salt) const { return derive_key(key_len, &secret[0], secret.size(), reinterpret_cast<const byte*>(salt.data()), salt.length()); } /* * Derive a key */ SecureVector<byte> KDF::derive_key(size_t key_len, const MemoryRegion<byte>& secret, const byte salt[], size_t salt_len) const { return derive_key(key_len, &secret[0], secret.size(), salt, salt_len); } /* * Derive a key */ SecureVector<byte> KDF::derive_key(size_t key_len, const MemoryRegion<byte>& secret, const MemoryRegion<byte>& salt) const { return derive_key(key_len, &secret[0], secret.size(), &salt[0], salt.size()); } /* * Derive a key */ SecureVector<byte> KDF::derive_key(size_t key_len, const byte secret[], size_t secret_len, const std::string& salt) const { return derive_key(key_len, secret, secret_len, reinterpret_cast<const byte*>(salt.data()), salt.length()); } /* * Derive a key */ SecureVector<byte> KDF::derive_key(size_t key_len, const byte secret[], size_t secret_len, const byte salt[], size_t salt_len) const { return derive(key_len, secret, secret_len, salt, salt_len); } } /* * KDF1 * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * KDF1 Key Derivation Mechanism */ SecureVector<byte> KDF1::derive(size_t, const byte secret[], size_t secret_len, const byte P[], size_t P_len) const { hash->update(secret, secret_len); hash->update(P, P_len); return hash->final(); } } /* * KDF2 * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * KDF2 Key Derivation Mechanism */ SecureVector<byte> KDF2::derive(size_t out_len, const byte secret[], size_t secret_len, const byte P[], size_t P_len) const { SecureVector<byte> output; u32bit counter = 1; while(out_len && counter) { hash->update(secret, secret_len); hash->update_be(counter); hash->update(P, P_len); SecureVector<byte> hash_result = hash->final(); size_t added = std::min(hash_result.size(), out_len); output += std::make_pair(&hash_result[0], added); out_len -= added; ++counter; } return output; } } /* * MGF1 * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> #include <memory> namespace Botan { /* * MGF1 Mask Generation Function */ void MGF1::mask(const byte in[], size_t in_len, byte out[], size_t out_len) const { u32bit counter = 0; while(out_len) { hash->update(in, in_len); hash->update_be(counter); SecureVector<byte> buffer = hash->final(); size_t xored = std::min<size_t>(buffer.size(), out_len); xor_buf(out, &buffer[0], xored); out += xored; out_len -= xored; ++counter; } } /* * MGF1 Constructor */ MGF1::MGF1(HashFunction* h) : hash(h) { if(!hash) throw Invalid_Argument("MGF1 given null hash object"); } /* * MGF1 Destructor */ MGF1::~MGF1() { delete hash; } } /* * SSLv3 PRF * (C) 2004-2006 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <memory> namespace Botan { namespace { /* * Return the next inner hash */ OctetString next_hash(size_t where, size_t want, HashFunction& md5, HashFunction& sha1, const byte secret[], size_t secret_len, const byte seed[], size_t seed_len) { BOTAN_ASSERT(want <= md5.output_length(), "Desired output too large"); const byte ASCII_A_CHAR = 0x41; for(size_t j = 0; j != where + 1; j++) sha1.update(static_cast<byte>(ASCII_A_CHAR + where)); sha1.update(secret, secret_len); sha1.update(seed, seed_len); SecureVector<byte> sha1_hash = sha1.final(); md5.update(secret, secret_len); md5.update(sha1_hash); SecureVector<byte> md5_hash = md5.final(); return OctetString(&md5_hash[0], want); } } /* * SSL3 PRF */ SecureVector<byte> SSL3_PRF::derive(size_t key_len, const byte secret[], size_t secret_len, const byte seed[], size_t seed_len) const { if(key_len > 416) throw Invalid_Argument("SSL3_PRF: Requested key length is too large"); MD5 md5; SHA_160 sha1; OctetString output; int counter = 0; while(key_len) { const size_t produce = std::min<size_t>(key_len, md5.output_length()); output = output + next_hash(counter++, produce, md5, sha1, secret, secret_len, seed, seed_len); key_len -= produce; } return output.bits_of(); } } /* * TLS v1.0 and v1.2 PRFs * (C) 2004-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { /* * TLS PRF P_hash function */ void P_hash(MemoryRegion<byte>& output, MessageAuthenticationCode* mac, const byte secret[], size_t secret_len, const byte seed[], size_t seed_len) { mac->set_key(secret, secret_len); SecureVector<byte> A(seed, seed_len); size_t offset = 0; while(offset != output.size()) { const size_t this_block_len = std::min<size_t>(mac->output_length(), output.size() - offset); A = mac->process(A); mac->update(A); mac->update(seed, seed_len); SecureVector<byte> block = mac->final(); xor_buf(&output[offset], &block[0], this_block_len); offset += this_block_len; } } } /* * TLS PRF Constructor and Destructor */ TLS_PRF::TLS_PRF() { hmac_md5 = new HMAC(new MD5); hmac_sha1 = new HMAC(new SHA_160); } TLS_PRF::~TLS_PRF() { delete hmac_md5; delete hmac_sha1; } /* * TLS PRF */ SecureVector<byte> TLS_PRF::derive(size_t key_len, const byte secret[], size_t secret_len, const byte seed[], size_t seed_len) const { SecureVector<byte> output(key_len); size_t S1_len = (secret_len + 1) / 2, S2_len = (secret_len + 1) / 2; const byte* S1 = secret; const byte* S2 = secret + (secret_len - S2_len); P_hash(output, hmac_md5, S1, S1_len, seed, seed_len); P_hash(output, hmac_sha1, S2, S2_len, seed, seed_len); return output; } /* * TLS v1.2 PRF Constructor and Destructor */ TLS_12_PRF::TLS_12_PRF(MessageAuthenticationCode* mac) : hmac(mac) { } TLS_12_PRF::~TLS_12_PRF() { delete hmac; } SecureVector<byte> TLS_12_PRF::derive(size_t key_len, const byte secret[], size_t secret_len, const byte seed[], size_t seed_len) const { SecureVector<byte> output(key_len); P_hash(output, hmac, secret, secret_len, seed, seed_len); return output; } } /* * X9.42 PRF * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> #include <memory> namespace Botan { namespace { /* * Encode an integer as an OCTET STRING */ MemoryVector<byte> encode_x942_int(u32bit n) { byte n_buf[4] = { 0 }; store_be(n, n_buf); return DER_Encoder().encode(n_buf, 4, OCTET_STRING).get_contents(); } } /* * X9.42 PRF */ SecureVector<byte> X942_PRF::derive(size_t key_len, const byte secret[], size_t secret_len, const byte salt[], size_t salt_len) const { SHA_160 hash; const OID kek_algo(key_wrap_oid); SecureVector<byte> key; u32bit counter = 1; while(key.size() != key_len && counter) { hash.update(secret, secret_len); hash.update( DER_Encoder().start_cons(SEQUENCE) .start_cons(SEQUENCE) .encode(kek_algo) .raw_bytes(encode_x942_int(counter)) .end_cons() .encode_if(salt_len != 0, DER_Encoder() .start_explicit(0) .encode(salt, salt_len, OCTET_STRING) .end_explicit() ) .start_explicit(2) .raw_bytes(encode_x942_int(static_cast<u32bit>(8 * key_len))) .end_explicit() .end_cons().get_contents() ); SecureVector<byte> digest = hash.final(); const size_t needed = std::min(digest.size(), key_len - key.size()); key += std::make_pair(&digest[0], needed); ++counter; } return key; } /* * X9.42 Constructor */ X942_PRF::X942_PRF(const std::string& oid) { if(OIDS::have_oid(oid)) key_wrap_oid = OIDS::lookup(oid).as_string(); else key_wrap_oid = oid; } } /* * PBKDF/EMSA/EME/KDF/MGF Retrieval * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #if defined(BOTAN_HAS_MGF1) #endif #if defined(BOTAN_HAS_EMSA1) #endif #if defined(BOTAN_HAS_EMSA1_BSI) #endif #if defined(BOTAN_HAS_EMSA2) #endif #if defined(BOTAN_HAS_EMSA3) #endif #if defined(BOTAN_HAS_EMSA4) #endif #if defined(BOTAN_HAS_EMSA_RAW) #endif #if defined(BOTAN_HAS_EME1) #endif #if defined(BOTAN_HAS_EME_PKCS1v15) #endif #if defined(BOTAN_HAS_KDF1) #endif #if defined(BOTAN_HAS_KDF2) #endif #if defined(BOTAN_HAS_X942_PRF) #endif #if defined(BOTAN_HAS_SSL_V3_PRF) #endif #if defined(BOTAN_HAS_TLS_V10_PRF) #endif namespace Botan { /* * Get a PBKDF algorithm by name */ PBKDF* get_pbkdf(const std::string& algo_spec) { Algorithm_Factory& af = global_state().algorithm_factory(); if(PBKDF* pbkdf = af.make_pbkdf(algo_spec)) return pbkdf; throw Algorithm_Not_Found(algo_spec); } /* * Get an EMSA by name */ EMSA* get_emsa(const std::string& algo_spec) { SCAN_Name request(algo_spec); Algorithm_Factory& af = global_state().algorithm_factory(); #if defined(BOTAN_HAS_EMSA_RAW) if(request.algo_name() == "Raw" && request.arg_count() == 0) return new EMSA_Raw; #endif #if defined(BOTAN_HAS_EMSA1) if(request.algo_name() == "EMSA1" && request.arg_count() == 1) return new EMSA1(af.make_hash_function(request.arg(0))); #endif #if defined(BOTAN_HAS_EMSA1_BSI) if(request.algo_name() == "EMSA1_BSI" && request.arg_count() == 1) return new EMSA1_BSI(af.make_hash_function(request.arg(0))); #endif #if defined(BOTAN_HAS_EMSA2) if(request.algo_name() == "EMSA2" && request.arg_count() == 1) return new EMSA2(af.make_hash_function(request.arg(0))); #endif #if defined(BOTAN_HAS_EMSA3) if(request.algo_name() == "EMSA3" && request.arg_count() == 1) { if(request.arg(0) == "Raw") return new EMSA3_Raw; return new EMSA3(af.make_hash_function(request.arg(0))); } #endif #if defined(BOTAN_HAS_EMSA4) if(request.algo_name() == "EMSA4" && request.arg_count_between(1, 3)) { // 3 args: Hash, MGF, salt size (MGF is hardcoded MGF1 in Botan) if(request.arg_count() == 1) return new EMSA4(af.make_hash_function(request.arg(0))); if(request.arg_count() == 2 && request.arg(1) != "MGF1") return new EMSA4(af.make_hash_function(request.arg(0))); if(request.arg_count() == 3) return new EMSA4(af.make_hash_function(request.arg(0)), request.arg_as_integer(2, 0)); } #endif throw Algorithm_Not_Found(algo_spec); } /* * Get an EME by name */ EME* get_eme(const std::string& algo_spec) { SCAN_Name request(algo_spec); Algorithm_Factory& af = global_state().algorithm_factory(); if(request.algo_name() == "Raw") return 0; // No padding #if defined(BOTAN_HAS_EME_PKCS1v15) if(request.algo_name() == "PKCS1v15" && request.arg_count() == 0) return new EME_PKCS1v15; #endif #if defined(BOTAN_HAS_EME1) if(request.algo_name() == "EME1" && request.arg_count_between(1, 2)) { if(request.arg_count() == 1 || (request.arg_count() == 2 && request.arg(1) == "MGF1")) { return new EME1(af.make_hash_function(request.arg(0))); } } #endif throw Algorithm_Not_Found(algo_spec); } /* * Get an KDF by name */ KDF* get_kdf(const std::string& algo_spec) { SCAN_Name request(algo_spec); Algorithm_Factory& af = global_state().algorithm_factory(); if(request.algo_name() == "Raw") return 0; // No KDF #if defined(BOTAN_HAS_KDF1) if(request.algo_name() == "KDF1" && request.arg_count() == 1) return new KDF1(af.make_hash_function(request.arg(0))); #endif #if defined(BOTAN_HAS_KDF2) if(request.algo_name() == "KDF2" && request.arg_count() == 1) return new KDF2(af.make_hash_function(request.arg(0))); #endif #if defined(BOTAN_HAS_X942_PRF) if(request.algo_name() == "X9.42-PRF" && request.arg_count() == 1) return new X942_PRF(request.arg(0)); // OID #endif #if defined(BOTAN_HAS_TLS_V10_PRF) if(request.algo_name() == "TLS-PRF" && request.arg_count() == 0) return new TLS_PRF; #endif #if defined(BOTAN_HAS_SSL_V3_PRF) if(request.algo_name() == "SSL3-PRF" && request.arg_count() == 0) return new SSL3_PRF; #endif throw Algorithm_Not_Found(algo_spec); } } /* * Global PRNG * (C) 2008-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ #if defined(BOTAN_HAS_RANDPOOL) #endif #if defined(BOTAN_HAS_HMAC_RNG) #endif #if defined(BOTAN_HAS_X931_RNG) #endif #if defined(BOTAN_HAS_ENTROPY_SRC_HIGH_RESOLUTION_TIMER) #endif #if defined(BOTAN_HAS_ENTROPY_SRC_RDRAND) #endif #if defined(BOTAN_HAS_ENTROPY_SRC_DEV_RANDOM) #endif #if defined(BOTAN_HAS_ENTROPY_SRC_EGD) #endif #if defined(BOTAN_HAS_ENTROPY_SRC_UNIX) #endif #if defined(BOTAN_HAS_ENTROPY_SRC_BEOS) #endif #if defined(BOTAN_HAS_ENTROPY_SRC_CAPI) #endif #if defined(BOTAN_HAS_ENTROPY_SRC_WIN32) #endif #if defined(BOTAN_HAS_ENTROPY_SRC_FTW) #endif namespace Botan { namespace { /** * Add any known entropy sources to this RNG */ void add_entropy_sources(RandomNumberGenerator* rng) { #if defined(BOTAN_HAS_ENTROPY_SRC_HIGH_RESOLUTION_TIMER) rng->add_entropy_source(new High_Resolution_Timestamp); #endif #if defined(BOTAN_HAS_ENTROPY_SRC_RDRAND) rng->add_entropy_source(new Intel_Rdrand); #endif #if defined(BOTAN_HAS_ENTROPY_SRC_DEV_RANDOM) rng->add_entropy_source( new Device_EntropySource( split_on("/dev/random:/dev/srandom:/dev/urandom", ':') ) ); #endif #if defined(BOTAN_HAS_ENTROPY_SRC_EGD) rng->add_entropy_source( new EGD_EntropySource(split_on("/var/run/egd-pool:/dev/egd-pool", ':')) ); #endif #if defined(BOTAN_HAS_ENTROPY_SRC_CAPI) rng->add_entropy_source(new Win32_CAPI_EntropySource); #endif #if defined(BOTAN_HAS_ENTROPY_SRC_FTW) rng->add_entropy_source(new FTW_EntropySource("/proc")); #endif #if defined(BOTAN_HAS_ENTROPY_SRC_WIN32) rng->add_entropy_source(new Win32_EntropySource); #endif #if defined(BOTAN_HAS_ENTROPY_SRC_BEOS) rng->add_entropy_source(new BeOS_EntropySource); #endif #if defined(BOTAN_HAS_ENTROPY_SRC_UNIX) rng->add_entropy_source( new Unix_EntropySource(split_on("/bin:/sbin:/usr/bin:/usr/sbin", ':')) ); #endif } class Serialized_PRNG : public RandomNumberGenerator { public: void randomize(byte out[], size_t len) { Mutex_Holder lock(mutex); rng->randomize(out, len); } bool is_seeded() const { Mutex_Holder lock(mutex); return rng->is_seeded(); } void clear() { Mutex_Holder lock(mutex); rng->clear(); } std::string name() const { Mutex_Holder lock(mutex); return rng->name(); } void reseed(size_t poll_bits) { Mutex_Holder lock(mutex); rng->reseed(poll_bits); } void add_entropy_source(EntropySource* es) { Mutex_Holder lock(mutex); rng->add_entropy_source(es); } void add_entropy(const byte in[], size_t len) { Mutex_Holder lock(mutex); rng->add_entropy(in, len); } // We do not own the mutex; Library_State does Serialized_PRNG(RandomNumberGenerator* r, Mutex* m) : mutex(m), rng(r) {} ~Serialized_PRNG() { delete rng; } private: Mutex* mutex; RandomNumberGenerator* rng; }; } RandomNumberGenerator* Library_State::make_global_rng(Algorithm_Factory& af, Mutex* mutex) { RandomNumberGenerator* rng = 0; #if defined(BOTAN_HAS_HMAC_RNG) rng = new HMAC_RNG(af.make_mac("HMAC(SHA-512)"), af.make_mac("HMAC(SHA-256)")); #elif defined(BOTAN_HAS_RANDPOOL) rng = new Randpool(af.make_block_cipher("AES-256"), af.make_mac("HMAC(SHA-256)")); #endif if(!rng) throw Internal_Error("No usable RNG found enabled in build"); /* If X9.31 is available, use it to wrap the other RNG as a failsafe */ #if defined(BOTAN_HAS_X931_RNG) rng = new ANSI_X931_RNG(af.make_block_cipher("AES-256"), rng); #endif add_entropy_sources(rng); rng->reseed(256); return new Serialized_PRNG(rng, mutex); } } /* * Global State Management * (C) 2010 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * @todo There should probably be a lock to avoid racy manipulation * of the state among different threads */ namespace Global_State_Management { /* * Botan's global state */ namespace { Library_State* global_lib_state = 0; } /* * Access the global state object */ Library_State& global_state() { /* Lazy initialization. Botan still needs to be deinitialized later on or memory might leak. */ if(!global_lib_state) { global_lib_state = new Library_State; global_lib_state->initialize(true); } return (*global_lib_state); } /* * Set a new global state object */ void set_global_state(Library_State* new_state) { delete swap_global_state(new_state); } /* * Set a new global state object unless one already existed */ bool set_global_state_unless_set(Library_State* new_state) { if(global_lib_state) { delete new_state; return false; } else { delete swap_global_state(new_state); return true; } } /* * Swap two global state objects */ Library_State* swap_global_state(Library_State* new_state) { Library_State* old_state = global_lib_state; global_lib_state = new_state; return old_state; } /* * Query if library is initialized */ bool global_state_exists() { return (global_lib_state != 0); } } } /* * Default Initialization Function * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Library Initialization */ void LibraryInitializer::initialize(const std::string& arg_string) { bool thread_safe = false; const std::vector<std::string> arg_list = split_on(arg_string, ' '); for(size_t i = 0; i != arg_list.size(); ++i) { if(arg_list[i].size() == 0) continue; std::string name, value; if(arg_list[i].find('=') == std::string::npos) { name = arg_list[i]; value = "true"; } else { std::vector<std::string> name_and_value = split_on(arg_list[i], '='); name = name_and_value[0]; value = name_and_value[1]; } bool is_on = (value == "1" || value == "true" || value == "yes" || value == "on"); if(name == "thread_safe") thread_safe = is_on; } try { /* This two stage initialization process is because Library_State's constructor will implicitly refer to global state through the allocators and so forth, so global_state() has to be a valid reference before initialize() can be called. Yeah, gross. */ Global_State_Management::set_global_state(new Library_State); global_state().initialize(thread_safe); } catch(...) { deinitialize(); throw; } } /* * Library Shutdown */ void LibraryInitializer::deinitialize() { Global_State_Management::set_global_state(0); } } /* * Library Internal/Global State * (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> #if defined(BOTAN_HAS_SELFTESTS) #endif #if defined(BOTAN_HAS_MUTEX_PTHREAD) #elif defined(BOTAN_HAS_MUTEX_WIN32) #endif #if defined(BOTAN_HAS_ALLOC_MMAP) #endif #if defined(BOTAN_HAS_ENGINE_ASSEMBLER) #endif #if defined(BOTAN_HAS_ENGINE_AES_ISA) #endif #if defined(BOTAN_HAS_ENGINE_SIMD) #endif #if defined(BOTAN_HAS_ENGINE_GNU_MP) #endif #if defined(BOTAN_HAS_ENGINE_OPENSSL) #endif namespace Botan { /* * Get a new mutex object */ Mutex* Library_State::get_mutex() const { return mutex_factory->make(); } /* * Get an allocator by its name */ Allocator* Library_State::get_allocator(const std::string& type) const { Mutex_Holder lock(allocator_lock); if(type != "") return search_map<std::string, Allocator*>(alloc_factory, type, 0); if(!cached_default_allocator) { cached_default_allocator = search_map<std::string, Allocator*>(alloc_factory, default_allocator_name, 0); } return cached_default_allocator; } /* * Create a new name to object mapping */ void Library_State::add_allocator(Allocator* allocator) { Mutex_Holder lock(allocator_lock); allocator->init(); allocators.push_back(allocator); alloc_factory[allocator->type()] = allocator; } /* * Set the default allocator type */ void Library_State::set_default_allocator(const std::string& type) { Mutex_Holder lock(allocator_lock); if(type == "") return; default_allocator_name = type; cached_default_allocator = 0; } /* * Get a configuration value */ std::string Library_State::get(const std::string& section, const std::string& key) const { Mutex_Holder lock(config_lock); return search_map<std::string, std::string>(config, section + "/" + key, ""); } /* * See if a particular option has been set */ bool Library_State::is_set(const std::string& section, const std::string& key) const { Mutex_Holder lock(config_lock); return config.count(section + "/" + key) != 0; } /* * Set a configuration value */ void Library_State::set(const std::string& section, const std::string& key, const std::string& value, bool overwrite) { Mutex_Holder lock(config_lock); std::string full_key = section + "/" + key; std::map<std::string, std::string>::const_iterator i = config.find(full_key); if(overwrite || i == config.end() || i->second == "") config[full_key] = value; } /* * Add an alias */ void Library_State::add_alias(const std::string& key, const std::string& value) { set("alias", key, value); } /* * Dereference an alias to a fixed name */ std::string Library_State::deref_alias(const std::string& key) const { std::string result = key; while(is_set("alias", result)) result = get("alias", result); return result; } /* * Return a reference to the Algorithm_Factory */ Algorithm_Factory& Library_State::algorithm_factory() const { if(!m_algorithm_factory) throw Invalid_State("Uninitialized in Library_State::algorithm_factory"); return *m_algorithm_factory; } /* * Return a reference to the global PRNG */ RandomNumberGenerator& Library_State::global_rng() { Mutex_Holder lock(global_rng_lock); if(!global_rng_ptr) global_rng_ptr = make_global_rng(algorithm_factory(), global_rng_lock); return *global_rng_ptr; } /* * Load a set of modules */ void Library_State::initialize(bool thread_safe) { CPUID::initialize(); if(mutex_factory) throw Invalid_State("Library_State has already been initialized"); if(!thread_safe) { mutex_factory = new Noop_Mutex_Factory; } else { #if defined(BOTAN_HAS_MUTEX_PTHREAD) mutex_factory = new Pthread_Mutex_Factory; #elif defined(BOTAN_HAS_MUTEX_WIN32) mutex_factory = new Win32_Mutex_Factory; #else throw Invalid_State("Could not find a thread-safe mutex object to use"); #endif } allocator_lock = get_mutex(); config_lock = get_mutex(); global_rng_lock = get_mutex(); default_allocator_name = has_mlock() ? "locking" : "malloc"; add_allocator(new Malloc_Allocator); add_allocator(new Locking_Allocator(get_mutex())); #if defined(BOTAN_HAS_ALLOC_MMAP) add_allocator(new MemoryMapping_Allocator(get_mutex())); #endif load_default_config(); m_algorithm_factory = new Algorithm_Factory(*mutex_factory); #if defined(BOTAN_HAS_ENGINE_GNU_MP) algorithm_factory().add_engine(new GMP_Engine); #endif #if defined(BOTAN_HAS_ENGINE_OPENSSL) algorithm_factory().add_engine(new OpenSSL_Engine); #endif #if defined(BOTAN_HAS_ENGINE_AES_ISA) algorithm_factory().add_engine(new AES_ISA_Engine); #endif #if defined(BOTAN_HAS_ENGINE_SIMD) algorithm_factory().add_engine(new SIMD_Engine); #endif #if defined(BOTAN_HAS_ENGINE_ASSEMBLER) algorithm_factory().add_engine(new Assembler_Engine); #endif algorithm_factory().add_engine(new Core_Engine); #if defined(BOTAN_HAS_SELFTESTS) confirm_startup_self_tests(algorithm_factory()); #endif } /* * Library_State Constructor */ Library_State::Library_State() { mutex_factory = 0; allocator_lock = config_lock = 0; cached_default_allocator = 0; m_algorithm_factory = 0; global_rng_lock = 0; global_rng_ptr = 0; } /* * Library_State Destructor */ Library_State::~Library_State() { delete m_algorithm_factory; delete global_rng_ptr; cached_default_allocator = 0; for(size_t i = 0; i != allocators.size(); ++i) { allocators[i]->destroy(); delete allocators[i]; } delete global_rng_lock; delete allocator_lock; delete mutex_factory; delete config_lock; } } /* * Algorithm Retrieval * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Query if an algorithm exists */ bool have_algorithm(const std::string& name) { Algorithm_Factory& af = global_state().algorithm_factory(); if(af.prototype_block_cipher(name)) return true; if(af.prototype_stream_cipher(name)) return true; if(af.prototype_hash_function(name)) return true; if(af.prototype_mac(name)) return true; return false; } /* * Query the block size of a cipher or hash */ size_t block_size_of(const std::string& name) { Algorithm_Factory& af = global_state().algorithm_factory(); if(const BlockCipher* cipher = af.prototype_block_cipher(name)) return cipher->block_size(); if(const HashFunction* hash = af.prototype_hash_function(name)) return hash->hash_block_size(); throw Algorithm_Not_Found(name); } /* * Query the output_length() of a hash or MAC */ size_t output_length_of(const std::string& name) { Algorithm_Factory& af = global_state().algorithm_factory(); if(const HashFunction* hash = af.prototype_hash_function(name)) return hash->output_length(); if(const MessageAuthenticationCode* mac = af.prototype_mac(name)) return mac->output_length(); throw Algorithm_Not_Found(name); } /* * Query the minimum allowed key length of an algorithm implementation */ size_t min_keylength_of(const std::string& name) { Algorithm_Factory& af = global_state().algorithm_factory(); if(const BlockCipher* bc = af.prototype_block_cipher(name)) return bc->key_spec().minimum_keylength(); if(const StreamCipher* sc = af.prototype_stream_cipher(name)) return sc->key_spec().minimum_keylength(); if(const MessageAuthenticationCode* mac = af.prototype_mac(name)) return mac->key_spec().minimum_keylength(); throw Algorithm_Not_Found(name); } /* * Query the maximum allowed keylength of an algorithm implementation */ size_t max_keylength_of(const std::string& name) { Algorithm_Factory& af = global_state().algorithm_factory(); if(const BlockCipher* bc = af.prototype_block_cipher(name)) return bc->key_spec().maximum_keylength(); if(const StreamCipher* sc = af.prototype_stream_cipher(name)) return sc->key_spec().maximum_keylength(); if(const MessageAuthenticationCode* mac = af.prototype_mac(name)) return mac->key_spec().maximum_keylength(); throw Algorithm_Not_Found(name); } /* * Query the number of byte a valid key must be a multiple of */ size_t keylength_multiple_of(const std::string& name) { Algorithm_Factory& af = global_state().algorithm_factory(); if(const BlockCipher* bc = af.prototype_block_cipher(name)) return bc->key_spec().keylength_multiple(); if(const StreamCipher* sc = af.prototype_stream_cipher(name)) return sc->key_spec().keylength_multiple(); if(const MessageAuthenticationCode* mac = af.prototype_mac(name)) return mac->key_spec().keylength_multiple(); throw Algorithm_Not_Found(name); } /* * Get a cipher object */ Keyed_Filter* get_cipher(const std::string& algo_spec, Cipher_Dir direction) { Algorithm_Factory& af = global_state().algorithm_factory(); Algorithm_Factory::Engine_Iterator i(af); while(Engine* engine = i.next()) { if(Keyed_Filter* algo = engine->get_cipher(algo_spec, direction, af)) return algo; } throw Algorithm_Not_Found(algo_spec); } /* * Get a cipher object */ Keyed_Filter* get_cipher(const std::string& algo_spec, const SymmetricKey& key, const InitializationVector& iv, Cipher_Dir direction) { Keyed_Filter* cipher = get_cipher(algo_spec, direction); cipher->set_key(key); if(iv.length()) cipher->set_iv(iv); return cipher; } /* * Get a cipher object */ Keyed_Filter* get_cipher(const std::string& algo_spec, const SymmetricKey& key, Cipher_Dir direction) { return get_cipher(algo_spec, key, InitializationVector(), direction); } } /* * OID Registry * (C) 1999-2008 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace OIDS { /* * Register an OID to string mapping */ void add_oid(const OID& oid, const std::string& name) { const std::string oid_str = oid.as_string(); if(!global_state().is_set("oid2str", oid_str)) global_state().set("oid2str", oid_str, name); if(!global_state().is_set("str2oid", name)) global_state().set("str2oid", name, oid_str); } /* * Do an OID to string lookup */ std::string lookup(const OID& oid) { std::string name = global_state().get("oid2str", oid.as_string()); if(name == "") return oid.as_string(); return name; } /* * Do a string to OID lookup */ OID lookup(const std::string& name) { std::string value = global_state().get("str2oid", name); if(value != "") return OID(value); try { return OID(name); } catch(...) { throw Lookup_Error("No object identifier found for " + name); } } /* * Check to see if an OID exists in the table */ bool have_oid(const std::string& name) { return global_state().is_set("str2oid", name); } /* * Check to see if an OID exists in the table */ bool name_of(const OID& oid, const std::string& name) { return (oid == lookup(name)); } } } /* * Default Policy * (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { /* * OID loading helper function */ void add_oid(Library_State& config, const std::string& oid_str, const std::string& name) { if(!config.is_set("oid2str", oid_str)) config.set("oid2str", oid_str, name); if(!config.is_set("str2oid", name)) config.set("str2oid", name, oid_str); } /* * Load all of the default OIDs */ void set_default_oids(Library_State& config) { /* Public key types */ add_oid(config, "1.2.840.113549.1.1.1", "RSA"); add_oid(config, "2.5.8.1.1", "RSA"); // RSA alternate add_oid(config, "1.2.840.10040.4.1", "DSA"); add_oid(config, "1.2.840.10046.2.1", "DH"); add_oid(config, "1.3.6.1.4.1.3029.1.2.1", "ElGamal"); add_oid(config, "1.3.6.1.4.1.25258.1.1", "RW"); add_oid(config, "1.3.6.1.4.1.25258.1.2", "NR"); // X9.62 ecPublicKey, valid for ECDSA and ECDH (RFC 3279 sec 2.3.5) add_oid(config, "1.2.840.10045.2.1", "ECDSA"); /* * This is an OID defined for ECDH keys though rarely used for such. * In this configuration it is accepted on decoding, but not used for * encoding. You can enable it for encoding by calling * global_state().set("str2oid", "ECDH", "1.3.132.1.12") * from your application code. */ config.set("oid2str", "1.3.132.1.12", "ECDH"); add_oid(config, "1.2.643.2.2.19", "GOST-34.10"); // RFC 4491 /* Ciphers */ add_oid(config, "1.3.14.3.2.7", "DES/CBC"); add_oid(config, "1.2.840.113549.3.7", "TripleDES/CBC"); add_oid(config, "1.2.840.113549.3.2", "RC2/CBC"); add_oid(config, "1.2.840.113533.7.66.10", "CAST-128/CBC"); add_oid(config, "2.16.840.1.101.3.4.1.2", "AES-128/CBC"); add_oid(config, "2.16.840.1.101.3.4.1.22", "AES-192/CBC"); add_oid(config, "2.16.840.1.101.3.4.1.42", "AES-256/CBC"); add_oid(config, "1.2.410.200004.1.4", "SEED/CBC"); // RFC 4010 add_oid(config, "1.3.6.1.4.1.25258.3.1", "Serpent/CBC"); /* Hash Functions */ add_oid(config, "1.2.840.113549.2.5", "MD5"); add_oid(config, "1.3.6.1.4.1.11591.12.2", "Tiger(24,3)"); add_oid(config, "1.3.14.3.2.26", "SHA-160"); add_oid(config, "2.16.840.1.101.3.4.2.4", "SHA-224"); add_oid(config, "2.16.840.1.101.3.4.2.1", "SHA-256"); add_oid(config, "2.16.840.1.101.3.4.2.2", "SHA-384"); add_oid(config, "2.16.840.1.101.3.4.2.3", "SHA-512"); /* MACs */ add_oid(config, "1.2.840.113549.2.7", "HMAC(SHA-1)"); add_oid(config, "1.2.840.113549.2.8", "HMAC(SHA-224)"); add_oid(config, "1.2.840.113549.2.9", "HMAC(SHA-256)"); add_oid(config, "1.2.840.113549.2.10", "HMAC(SHA-384)"); add_oid(config, "1.2.840.113549.2.11", "HMAC(SHA-512)"); /* Key Wrap */ add_oid(config, "1.2.840.113549.1.9.16.3.6", "KeyWrap.TripleDES"); add_oid(config, "1.2.840.113549.1.9.16.3.7", "KeyWrap.RC2"); add_oid(config, "1.2.840.113533.7.66.15", "KeyWrap.CAST-128"); add_oid(config, "2.16.840.1.101.3.4.1.5", "KeyWrap.AES-128"); add_oid(config, "2.16.840.1.101.3.4.1.25", "KeyWrap.AES-192"); add_oid(config, "2.16.840.1.101.3.4.1.45", "KeyWrap.AES-256"); /* Compression */ add_oid(config, "1.2.840.113549.1.9.16.3.8", "Compression.Zlib"); /* Public key signature schemes */ add_oid(config, "1.2.840.113549.1.1.1", "RSA/EME-PKCS1-v1_5"); add_oid(config, "1.2.840.113549.1.1.2", "RSA/EMSA3(MD2)"); add_oid(config, "1.2.840.113549.1.1.4", "RSA/EMSA3(MD5)"); add_oid(config, "1.2.840.113549.1.1.5", "RSA/EMSA3(SHA-160)"); add_oid(config, "1.2.840.113549.1.1.11", "RSA/EMSA3(SHA-256)"); add_oid(config, "1.2.840.113549.1.1.12", "RSA/EMSA3(SHA-384)"); add_oid(config, "1.2.840.113549.1.1.13", "RSA/EMSA3(SHA-512)"); add_oid(config, "1.3.36.3.3.1.2", "RSA/EMSA3(RIPEMD-160)"); add_oid(config, "1.2.840.10040.4.3", "DSA/EMSA1(SHA-160)"); add_oid(config, "2.16.840.1.101.3.4.3.1", "DSA/EMSA1(SHA-224)"); add_oid(config, "2.16.840.1.101.3.4.3.2", "DSA/EMSA1(SHA-256)"); add_oid(config, "0.4.0.127.0.7.1.1.4.1.1", "ECDSA/EMSA1_BSI(SHA-160)"); add_oid(config, "0.4.0.127.0.7.1.1.4.1.2", "ECDSA/EMSA1_BSI(SHA-224)"); add_oid(config, "0.4.0.127.0.7.1.1.4.1.3", "ECDSA/EMSA1_BSI(SHA-256)"); add_oid(config, "0.4.0.127.0.7.1.1.4.1.4", "ECDSA/EMSA1_BSI(SHA-384)"); add_oid(config, "0.4.0.127.0.7.1.1.4.1.5", "ECDSA/EMSA1_BSI(SHA-512)"); add_oid(config, "0.4.0.127.0.7.1.1.4.1.6", "ECDSA/EMSA1_BSI(RIPEMD-160)"); add_oid(config, "1.2.840.10045.4.1", "ECDSA/EMSA1(SHA-160)"); add_oid(config, "1.2.840.10045.4.3.1", "ECDSA/EMSA1(SHA-224)"); add_oid(config, "1.2.840.10045.4.3.2", "ECDSA/EMSA1(SHA-256)"); add_oid(config, "1.2.840.10045.4.3.3", "ECDSA/EMSA1(SHA-384)"); add_oid(config, "1.2.840.10045.4.3.4", "ECDSA/EMSA1(SHA-512)"); add_oid(config, "1.2.643.2.2.3", "GOST-34.10/EMSA1(GOST-R-34.11-94)"); add_oid(config, "1.3.6.1.4.1.25258.2.1.1.1", "RW/EMSA2(RIPEMD-160)"); add_oid(config, "1.3.6.1.4.1.25258.2.1.1.2", "RW/EMSA2(SHA-160)"); add_oid(config, "1.3.6.1.4.1.25258.2.1.1.3", "RW/EMSA2(SHA-224)"); add_oid(config, "1.3.6.1.4.1.25258.2.1.1.4", "RW/EMSA2(SHA-256)"); add_oid(config, "1.3.6.1.4.1.25258.2.1.1.5", "RW/EMSA2(SHA-384)"); add_oid(config, "1.3.6.1.4.1.25258.2.1.1.6", "RW/EMSA2(SHA-512)"); add_oid(config, "1.3.6.1.4.1.25258.2.1.2.1", "RW/EMSA4(RIPEMD-160)"); add_oid(config, "1.3.6.1.4.1.25258.2.1.2.2", "RW/EMSA4(SHA-160)"); add_oid(config, "1.3.6.1.4.1.25258.2.1.2.3", "RW/EMSA4(SHA-224)"); add_oid(config, "1.3.6.1.4.1.25258.2.1.2.4", "RW/EMSA4(SHA-256)"); add_oid(config, "1.3.6.1.4.1.25258.2.1.2.5", "RW/EMSA4(SHA-384)"); add_oid(config, "1.3.6.1.4.1.25258.2.1.2.6", "RW/EMSA4(SHA-512)"); add_oid(config, "1.3.6.1.4.1.25258.2.2.1.1", "NR/EMSA2(RIPEMD-160)"); add_oid(config, "1.3.6.1.4.1.25258.2.2.1.2", "NR/EMSA2(SHA-160)"); add_oid(config, "1.3.6.1.4.1.25258.2.2.1.3", "NR/EMSA2(SHA-224)"); add_oid(config, "1.3.6.1.4.1.25258.2.2.1.4", "NR/EMSA2(SHA-256)"); add_oid(config, "1.3.6.1.4.1.25258.2.2.1.5", "NR/EMSA2(SHA-384)"); add_oid(config, "1.3.6.1.4.1.25258.2.2.1.6", "NR/EMSA2(SHA-512)"); add_oid(config, "2.5.4.3", "X520.CommonName"); add_oid(config, "2.5.4.4", "X520.Surname"); add_oid(config, "2.5.4.5", "X520.SerialNumber"); add_oid(config, "2.5.4.6", "X520.Country"); add_oid(config, "2.5.4.7", "X520.Locality"); add_oid(config, "2.5.4.8", "X520.State"); add_oid(config, "2.5.4.10", "X520.Organization"); add_oid(config, "2.5.4.11", "X520.OrganizationalUnit"); add_oid(config, "2.5.4.12", "X520.Title"); add_oid(config, "2.5.4.42", "X520.GivenName"); add_oid(config, "2.5.4.43", "X520.Initials"); add_oid(config, "2.5.4.44", "X520.GenerationalQualifier"); add_oid(config, "2.5.4.46", "X520.DNQualifier"); add_oid(config, "2.5.4.65", "X520.Pseudonym"); add_oid(config, "1.2.840.113549.1.5.12", "PKCS5.PBKDF2"); add_oid(config, "1.2.840.113549.1.5.1", "PBE-PKCS5v15(MD2,DES/CBC)"); add_oid(config, "1.2.840.113549.1.5.4", "PBE-PKCS5v15(MD2,RC2/CBC)"); add_oid(config, "1.2.840.113549.1.5.3", "PBE-PKCS5v15(MD5,DES/CBC)"); add_oid(config, "1.2.840.113549.1.5.6", "PBE-PKCS5v15(MD5,RC2/CBC)"); add_oid(config, "1.2.840.113549.1.5.10", "PBE-PKCS5v15(SHA-160,DES/CBC)"); add_oid(config, "1.2.840.113549.1.5.11", "PBE-PKCS5v15(SHA-160,RC2/CBC)"); add_oid(config, "1.2.840.113549.1.5.13", "PBE-PKCS5v20"); add_oid(config, "1.2.840.113549.1.9.1", "PKCS9.EmailAddress"); add_oid(config, "1.2.840.113549.1.9.2", "PKCS9.UnstructuredName"); add_oid(config, "1.2.840.113549.1.9.3", "PKCS9.ContentType"); add_oid(config, "1.2.840.113549.1.9.4", "PKCS9.MessageDigest"); add_oid(config, "1.2.840.113549.1.9.7", "PKCS9.ChallengePassword"); add_oid(config, "1.2.840.113549.1.9.14", "PKCS9.ExtensionRequest"); add_oid(config, "1.2.840.113549.1.7.1", "CMS.DataContent"); add_oid(config, "1.2.840.113549.1.7.2", "CMS.SignedData"); add_oid(config, "1.2.840.113549.1.7.3", "CMS.EnvelopedData"); add_oid(config, "1.2.840.113549.1.7.5", "CMS.DigestedData"); add_oid(config, "1.2.840.113549.1.7.6", "CMS.EncryptedData"); add_oid(config, "1.2.840.113549.1.9.16.1.2", "CMS.AuthenticatedData"); add_oid(config, "1.2.840.113549.1.9.16.1.9", "CMS.CompressedData"); add_oid(config, "2.5.29.14", "X509v3.SubjectKeyIdentifier"); add_oid(config, "2.5.29.15", "X509v3.KeyUsage"); add_oid(config, "2.5.29.17", "X509v3.SubjectAlternativeName"); add_oid(config, "2.5.29.18", "X509v3.IssuerAlternativeName"); add_oid(config, "2.5.29.19", "X509v3.BasicConstraints"); add_oid(config, "2.5.29.20", "X509v3.CRLNumber"); add_oid(config, "2.5.29.21", "X509v3.ReasonCode"); add_oid(config, "2.5.29.23", "X509v3.HoldInstructionCode"); add_oid(config, "2.5.29.24", "X509v3.InvalidityDate"); add_oid(config, "2.5.29.32", "X509v3.CertificatePolicies"); add_oid(config, "2.5.29.35", "X509v3.AuthorityKeyIdentifier"); add_oid(config, "2.5.29.36", "X509v3.PolicyConstraints"); add_oid(config, "2.5.29.37", "X509v3.ExtendedKeyUsage"); add_oid(config, "2.5.29.32.0", "X509v3.AnyPolicy"); add_oid(config, "1.3.6.1.5.5.7.3.1", "PKIX.ServerAuth"); add_oid(config, "1.3.6.1.5.5.7.3.2", "PKIX.ClientAuth"); add_oid(config, "1.3.6.1.5.5.7.3.3", "PKIX.CodeSigning"); add_oid(config, "1.3.6.1.5.5.7.3.4", "PKIX.EmailProtection"); add_oid(config, "1.3.6.1.5.5.7.3.5", "PKIX.IPsecEndSystem"); add_oid(config, "1.3.6.1.5.5.7.3.6", "PKIX.IPsecTunnel"); add_oid(config, "1.3.6.1.5.5.7.3.7", "PKIX.IPsecUser"); add_oid(config, "1.3.6.1.5.5.7.3.8", "PKIX.TimeStamping"); add_oid(config, "1.3.6.1.5.5.7.3.9", "PKIX.OCSPSigning"); add_oid(config, "1.3.6.1.5.5.7.8.5", "PKIX.XMPPAddr"); /* ECC domain parameters */ add_oid(config, "1.3.132.0.6", "secp112r1"); add_oid(config, "1.3.132.0.7", "secp112r2"); add_oid(config, "1.3.132.0.8", "secp160r1"); add_oid(config, "1.3.132.0.9", "secp160k1"); add_oid(config, "1.3.132.0.10", "secp256k1"); add_oid(config, "1.3.132.0.28", "secp128r1"); add_oid(config, "1.3.132.0.29", "secp128r2"); add_oid(config, "1.3.132.0.30", "secp160r2"); add_oid(config, "1.3.132.0.31", "secp192k1"); add_oid(config, "1.3.132.0.32", "secp224k1"); add_oid(config, "1.3.132.0.33", "secp224r1"); add_oid(config, "1.3.132.0.34", "secp384r1"); add_oid(config, "1.3.132.0.35", "secp521r1"); add_oid(config, "1.2.840.10045.3.1.1", "secp192r1"); add_oid(config, "1.2.840.10045.3.1.2", "x962_p192v2"); add_oid(config, "1.2.840.10045.3.1.3", "x962_p192v3"); add_oid(config, "1.2.840.10045.3.1.4", "x962_p239v1"); add_oid(config, "1.2.840.10045.3.1.5", "x962_p239v2"); add_oid(config, "1.2.840.10045.3.1.6", "x962_p239v3"); add_oid(config, "1.2.840.10045.3.1.7", "secp256r1"); add_oid(config, "1.3.36.3.3.2.8.1.1.1", "brainpool160r1"); add_oid(config, "1.3.36.3.3.2.8.1.1.3", "brainpool192r1"); add_oid(config, "1.3.36.3.3.2.8.1.1.5", "brainpool224r1"); add_oid(config, "1.3.36.3.3.2.8.1.1.7", "brainpool256r1"); add_oid(config, "1.3.36.3.3.2.8.1.1.9", "brainpool320r1"); add_oid(config, "1.3.36.3.3.2.8.1.1.11", "brainpool384r1"); add_oid(config, "1.3.36.3.3.2.8.1.1.13", "brainpool512r1"); add_oid(config, "1.2.643.2.2.35.1", "gost_256A"); add_oid(config, "1.2.643.2.2.36.0", "gost_256A"); /* CVC */ add_oid(config, "0.4.0.127.0.7.3.1.2.1", "CertificateHolderAuthorizationTemplate"); } /* * Set the default algorithm aliases */ void set_default_aliases(Library_State& config) { config.add_alias("OpenPGP.Cipher.1", "IDEA"); config.add_alias("OpenPGP.Cipher.2", "TripleDES"); config.add_alias("OpenPGP.Cipher.3", "CAST-128"); config.add_alias("OpenPGP.Cipher.4", "Blowfish"); config.add_alias("OpenPGP.Cipher.5", "SAFER-SK(13)"); config.add_alias("OpenPGP.Cipher.7", "AES-128"); config.add_alias("OpenPGP.Cipher.8", "AES-192"); config.add_alias("OpenPGP.Cipher.9", "AES-256"); config.add_alias("OpenPGP.Cipher.10", "Twofish"); config.add_alias("OpenPGP.Digest.1", "MD5"); config.add_alias("OpenPGP.Digest.2", "SHA-1"); config.add_alias("OpenPGP.Digest.3", "RIPEMD-160"); config.add_alias("OpenPGP.Digest.5", "MD2"); config.add_alias("OpenPGP.Digest.6", "Tiger(24,3)"); config.add_alias("OpenPGP.Digest.8", "SHA-256"); config.add_alias("TLS.Digest.0", "Parallel(MD5,SHA-160)"); config.add_alias("EME-PKCS1-v1_5", "PKCS1v15"); config.add_alias("OAEP-MGF1", "EME1"); config.add_alias("EME-OAEP", "EME1"); config.add_alias("X9.31", "EMSA2"); config.add_alias("EMSA-PKCS1-v1_5", "EMSA3"); config.add_alias("PSS-MGF1", "EMSA4"); config.add_alias("EMSA-PSS", "EMSA4"); config.add_alias("3DES", "TripleDES"); config.add_alias("DES-EDE", "TripleDES"); config.add_alias("CAST5", "CAST-128"); config.add_alias("SHA1", "SHA-160"); config.add_alias("SHA-1", "SHA-160"); config.add_alias("MARK-4", "ARC4(256)"); config.add_alias("OMAC", "CMAC"); config.add_alias("GOST", "GOST-28147-89"); } /* * Set the built-in discrete log groups */ void set_default_dl_groups(Library_State& config) { config.set("dl", "modp/ietf/768", "-----BEGIN X942 DH PARAMETERS-----" "MIHIAmEA///////////JD9qiIWjCNMTGYouA3BzRKQJOCIpnzHQCC76mOxObIlFK" "CHmONATd75UZs806QxswKwpt8l8UN0/hNW1tUcJF5IW1dmJefsb0TELppjo2IP//" "////////AgECAmB//////////+SH7VEQtGEaYmMxRcBuDmiUgScERTPmOgEF31Md" "ic2RKKUEPMcaAm73yozZ5p0hjZgVhTb5L4obp/Catrao4SLyQtq7MS8/Y3omIXTT" "HRsQf/////////8=" "-----END X942 DH PARAMETERS-----"); config.set("dl", "modp/ietf/1024", "-----BEGIN X942 DH PARAMETERS-----" "MIIBCgKBgQD//////////8kP2qIhaMI0xMZii4DcHNEpAk4IimfMdAILvqY7E5si" "UUoIeY40BN3vlRmzzTpDGzArCm3yXxQ3T+E1bW1RwkXkhbV2Yl5+xvRMQummN+1r" "C/9ctvQGt+3uOGv7Womfpa6fJBF8Sx/mSShmUezmU4H//////////wIBAgKBgH//" "////////5IftURC0YRpiYzFFwG4OaJSBJwRFM+Y6AQXfUx2JzZEopQQ8xxoCbvfK" "jNnmnSGNmBWFNvkvihun8Jq2tqjhIvJC2rsxLz9jeiYhdNMb9rWF/65begNb9vcc" "Nf2tRM/S10+SCL4lj/MklDMo9nMpwP//////////" "-----END X942 DH PARAMETERS-----"); config.set("dl", "modp/srp/1024", "-----BEGIN X942 DH PARAMETERS-----" "MIIBCgKBgQDurwq5rbON1pwz+Ar6j8XoYHJhh3X/PAueojFMnCVldtZ033SW6oHT" "ODtIE9aSxuDg1djiULmL5I5JXB1gidrRXcfXtGFU1rbOjvStabFdSYJVmyl7zxiF" "xSn1ZmYOV+xo7bw8BXJswC/Uy/SXbqqa/VE4/oN2Q1ufxh0vwOsG4wIBAgKBgHdX" "hVzW2cbrThn8BX1H4vQwOTDDuv+eBc9RGKZOErK7azpvukt1QOmcHaQJ60ljcHBq" "7HEoXMXyRySuDrBE7Wiu4+vaMKprW2dHela02K6kwSrNlL3njELilPqzMwcr9jR2" "3h4CuTZgF+pl+ku3VU1+qJx/Qbshrc/jDpfgdYNx" "-----END X942 DH PARAMETERS-----"); config.set("dl", "modp/ietf/1536", "-----BEGIN X942 DH PARAMETERS-----" "MIIBigKBwQD//////////8kP2qIhaMI0xMZii4DcHNEpAk4IimfMdAILvqY7E5si" "UUoIeY40BN3vlRmzzTpDGzArCm3yXxQ3T+E1bW1RwkXkhbV2Yl5+xvRMQummN+1r" "C/9ctvQGt+3uOGv7Womfpa6fJBF8Sx/mSShmUezkWz3CAHy4oWO/BZjaSDYcVdOa" "aRY/qP0kz1+DZV0j3KOtlhxi81YghVK7ntUpB3CWlm1nDDVOSryYBPF0bAjKI3Mn" "//////////8CAQICgcB//////////+SH7VEQtGEaYmMxRcBuDmiUgScERTPmOgEF" "31Mdic2RKKUEPMcaAm73yozZ5p0hjZgVhTb5L4obp/Catrao4SLyQtq7MS8/Y3om" "IXTTG/a1hf+uW3oDW/b3HDX9rUTP0tdPkgi+JY/zJJQzKPZyLZ7hAD5cULHfgsxt" "JBsOKunNNIsf1H6SZ6/Bsq6R7lHWyw4xeasQQqldz2qUg7hLSzazhhqnJV5MAni6" "NgRlEbmT//////////8=" "-----END X942 DH PARAMETERS-----"); config.set("dl", "modp/srp/1536", "-----BEGIN DH PARAMETERS-----" "MIHHAoHBAJ3vPK+5OSd6sfEqhheke7vbpR30maxMgL7uqWFLGcxNX09fVW4ny95R" "xqlL5GB6KRVYkDug0PhDgLZVu5oi6NzfAop87Gfw0IE0sci5eYkUm2CeC+O6tj1H" "VIOB28Wx/HZOP0tT3Z2hFYv9PiucjPVu3wGVOTSWJ9sv1T0kt8SGZXcuQ31sf4zk" "QnNK98y3roN8Jkrjqb64f4ov6bi1KS5aAh//XpFHnoznoowkQsbzFRgPk0maI03P" "duP+0TX5uwIBAg==" "-----END DH PARAMETERS-----"); config.set("dl", "modp/ietf/2048", "-----BEGIN X942 DH PARAMETERS-----" "MIICDAKCAQEA///////////JD9qiIWjCNMTGYouA3BzRKQJOCIpnzHQCC76mOxOb" "IlFKCHmONATd75UZs806QxswKwpt8l8UN0/hNW1tUcJF5IW1dmJefsb0TELppjft" "awv/XLb0Brft7jhr+1qJn6WunyQRfEsf5kkoZlHs5Fs9wgB8uKFjvwWY2kg2HFXT" "mmkWP6j9JM9fg2VdI9yjrZYcYvNWIIVSu57VKQdwlpZtZww1Tkq8mATxdGwIyhgh" "fDKQXkYuNs474553LBgOhgObJ4Oi7Aeij7XFXfBvTFLJ3ivL9pVYFxg5lUl86pVq" "5RXSJhiY+gUQFXKOWoqsqmj//////////wIBAgKCAQB//////////+SH7VEQtGEa" "YmMxRcBuDmiUgScERTPmOgEF31Mdic2RKKUEPMcaAm73yozZ5p0hjZgVhTb5L4ob" "p/Catrao4SLyQtq7MS8/Y3omIXTTG/a1hf+uW3oDW/b3HDX9rUTP0tdPkgi+JY/z" "JJQzKPZyLZ7hAD5cULHfgsxtJBsOKunNNIsf1H6SZ6/Bsq6R7lHWyw4xeasQQqld" "z2qUg7hLSzazhhqnJV5MAni6NgRlDBC+GUgvIxcbZx3xzzuWDAdDAc2TwdF2A9FH" "2uKu+DemKWTvFeX7SqwLjBzKpL51SrVyiukTDEx9AogKuUctRVZVNH//////////" "-----END X942 DH PARAMETERS-----"); config.set("dl", "modp/srp/2048", "-----BEGIN X942 DH PARAMETERS-----" "MIICDAKCAQEArGvbQTJKmpvxZt5eE4lYL69ytmUZh+4H/DGSlD21YFCjcynLtKCZ" "7YGT4HV3Z6E91SMSq0sDMQ3Nf0ip2gT9UOgIOWntt2ewz2CVF5oWOrNmGgX71fqq" "6CkYqZYvC5O4Vfl5k+yXXuqoDXQK2/T/dHNZ0EHVwz6nHSgeRGsUdzvKl7Q6I/uA" "Fna9IHpDbGSB8dK5B4cXRhpbnTLmiPh3SFRFI7UksNV9Xqd6J3XS7PoDLPvb9S+z" "eGFgJ5AE5Xrmr4dOcwPOUymczAQce8MI2CpWmPOo0MOCca41+Onb+7aUtcgD2J96" "5DXeI21SX1R1m2XjcvzWjvIPpxEfnkr/cwIBAgKCAQBWNe2gmSVNTfizby8JxKwX" "17lbMozD9wP+GMlKHtqwKFG5lOXaUEz2wMnwOruz0J7qkYlVpYGYhua/pFTtAn6o" "dAQctPbbs9hnsEqLzQsdWbMNAv3q/VV0FIxUyxeFydwq/LzJ9kuvdVQGugVt+n+6" "OazoIOrhn1OOlA8iNYo7neVL2h0R/cALO16QPSG2MkD46VyDw4ujDS3OmXNEfDuk" "KiKR2pJYar6vU70Tuul2fQGWfe36l9m8MLATyAJyvXNXw6c5gecplM5mAg494YRs" "FStMedRoYcE41xr8dO3920pa5AHsT71yGu8RtqkvqjrNsvG5fmtHeQfTiI/PJX+5" "-----END X942 DH PARAMETERS-----"); config.set("dl", "modp/ietf/3072", "-----BEGIN X942 DH PARAMETERS-----" "MIIDDAKCAYEA///////////JD9qiIWjCNMTGYouA3BzRKQJOCIpnzHQCC76mOxOb" "IlFKCHmONATd75UZs806QxswKwpt8l8UN0/hNW1tUcJF5IW1dmJefsb0TELppjft" "awv/XLb0Brft7jhr+1qJn6WunyQRfEsf5kkoZlHs5Fs9wgB8uKFjvwWY2kg2HFXT" "mmkWP6j9JM9fg2VdI9yjrZYcYvNWIIVSu57VKQdwlpZtZww1Tkq8mATxdGwIyhgh" "fDKQXkYuNs474553LBgOhgObJ4Oi7Aeij7XFXfBvTFLJ3ivL9pVYFxg5lUl86pVq" "5RXSJhiY+gUQFXKOWoqqxC2tMxcNBFB6M6hVIavfHLpk7PuFBFjb7wqK6nFXXQYM" "fbOXD4Wm4eTHq/WujNsJM9cejJTgSiVhnc7j0iYa0u5r8S/6BtmKCGTYdgJzPshq" "ZFIfKxgXeyAMu+EXV3phXWx3CYjAutlG4gjiT6B05asxQ9tb/OD9EI5LgtEgqTrS" "yv//////////AgECAoIBgH//////////5IftURC0YRpiYzFFwG4OaJSBJwRFM+Y6" "AQXfUx2JzZEopQQ8xxoCbvfKjNnmnSGNmBWFNvkvihun8Jq2tqjhIvJC2rsxLz9j" "eiYhdNMb9rWF/65begNb9vccNf2tRM/S10+SCL4lj/MklDMo9nItnuEAPlxQsd+C" "zG0kGw4q6c00ix/UfpJnr8GyrpHuUdbLDjF5qxBCqV3PapSDuEtLNrOGGqclXkwC" "eLo2BGUMEL4ZSC8jFxtnHfHPO5YMB0MBzZPB0XYD0Ufa4q74N6YpZO8V5ftKrAuM" "HMqkvnVKtXKK6RMMTH0CiAq5Ry1FVWIW1pmLhoIoPRnUKpDV745dMnZ9woIsbfeF" "RXU4q66DBj7Zy4fC03DyY9X610ZthJnrj0ZKcCUSsM7ncekTDWl3NfiX/QNsxQQy" "bDsBOZ9kNTIpD5WMC72QBl3wi6u9MK62O4TEYF1so3EEcSfQOnLVmKHtrf5wfohH" "JcFokFSdaWV//////////w==" "-----END X942 DH PARAMETERS-----"); config.set("dl", "modp/srp/3072", "-----BEGIN DH PARAMETERS-----" "MIIBiAKCAYEA///////////JD9qiIWjCNMTGYouA3BzRKQJOCIpnzHQCC76mOxOb" "IlFKCHmONATd75UZs806QxswKwpt8l8UN0/hNW1tUcJF5IW1dmJefsb0TELppjft" "awv/XLb0Brft7jhr+1qJn6WunyQRfEsf5kkoZlHs5Fs9wgB8uKFjvwWY2kg2HFXT" "mmkWP6j9JM9fg2VdI9yjrZYcYvNWIIVSu57VKQdwlpZtZww1Tkq8mATxdGwIyhgh" "fDKQXkYuNs474553LBgOhgObJ4Oi7Aeij7XFXfBvTFLJ3ivL9pVYFxg5lUl86pVq" "5RXSJhiY+gUQFXKOWoqqxC2tMxcNBFB6M6hVIavfHLpk7PuFBFjb7wqK6nFXXQYM" "fbOXD4Wm4eTHq/WujNsJM9cejJTgSiVhnc7j0iYa0u5r8S/6BtmKCGTYdgJzPshq" "ZFIfKxgXeyAMu+EXV3phXWx3CYjAutlG4gjiT6B05asxQ9tb/OD9EI5LgtEgqTrS" "yv//////////AgEF" "-----END DH PARAMETERS-----"); config.set("dl", "modp/ietf/4096", "-----BEGIN X942 DH PARAMETERS-----" "MIIEDAKCAgEA///////////JD9qiIWjCNMTGYouA3BzRKQJOCIpnzHQCC76mOxOb" "IlFKCHmONATd75UZs806QxswKwpt8l8UN0/hNW1tUcJF5IW1dmJefsb0TELppjft" "awv/XLb0Brft7jhr+1qJn6WunyQRfEsf5kkoZlHs5Fs9wgB8uKFjvwWY2kg2HFXT" "mmkWP6j9JM9fg2VdI9yjrZYcYvNWIIVSu57VKQdwlpZtZww1Tkq8mATxdGwIyhgh" "fDKQXkYuNs474553LBgOhgObJ4Oi7Aeij7XFXfBvTFLJ3ivL9pVYFxg5lUl86pVq" "5RXSJhiY+gUQFXKOWoqqxC2tMxcNBFB6M6hVIavfHLpk7PuFBFjb7wqK6nFXXQYM" "fbOXD4Wm4eTHq/WujNsJM9cejJTgSiVhnc7j0iYa0u5r8S/6BtmKCGTYdgJzPshq" "ZFIfKxgXeyAMu+EXV3phXWx3CYjAutlG4gjiT6B05asxQ9tb/OD9EI5LgtEgqSEI" "ARpyPBKnh+bXiHGaEL26WyaZwycYavTiPBqUaDS2FQvaJYPpyirUTOjbu8LbBN6O" "+S6O/BQfvsqmKHxZR05rwF2ZspZPoJDDoiM7oYZRW+ftH2EpcM7i16+4G912IXBI" "HNAGkSfVsFqpk7TqmI2P3cGG/7fckKbAj030Nck0BjGZ//////////8CAQICggIA" "f//////////kh+1RELRhGmJjMUXAbg5olIEnBEUz5joBBd9THYnNkSilBDzHGgJu" "98qM2eadIY2YFYU2+S+KG6fwmra2qOEi8kLauzEvP2N6JiF00xv2tYX/rlt6A1v2" "9xw1/a1Ez9LXT5IIviWP8ySUMyj2ci2e4QA+XFCx34LMbSQbDirpzTSLH9R+kmev" "wbKuke5R1ssOMXmrEEKpXc9qlIO4S0s2s4YapyVeTAJ4ujYEZQwQvhlILyMXG2cd" "8c87lgwHQwHNk8HRdgPRR9rirvg3pilk7xXl+0qsC4wcyqS+dUq1corpEwxMfQKI" "CrlHLUVVYhbWmYuGgig9GdQqkNXvjl0ydn3Cgixt94VFdTirroMGPtnLh8LTcPJj" "1frXRm2EmeuPRkpwJRKwzudx6RMNaXc1+Jf9A2zFBDJsOwE5n2Q1MikPlYwLvZAG" "XfCLq70wrrY7hMRgXWyjcQRxJ9A6ctWYoe2t/nB+iEclwWiQVJCEAI05HglTw/Nr" "xDjNCF7dLZNM4ZOMNXpxHg1KNBpbCoXtEsH05RVqJnRt3eFtgm9HfJdHfgoP32VT" "FD4so6c14C7M2Usn0Ehh0RGd0MMorfP2j7CUuGdxa9fcDe67ELgkDmgDSJPq2C1U" "ydp1TEbH7uDDf9vuSFNgR6b6GuSaAxjM//////////8=" "-----END X942 DH PARAMETERS-----"); config.set("dl", "modp/srp/4096", "-----BEGIN DH PARAMETERS-----" "MIICCAKCAgEA///////////JD9qiIWjCNMTGYouA3BzRKQJOCIpnzHQCC76mOxOb" "IlFKCHmONATd75UZs806QxswKwpt8l8UN0/hNW1tUcJF5IW1dmJefsb0TELppjft" "awv/XLb0Brft7jhr+1qJn6WunyQRfEsf5kkoZlHs5Fs9wgB8uKFjvwWY2kg2HFXT" "mmkWP6j9JM9fg2VdI9yjrZYcYvNWIIVSu57VKQdwlpZtZww1Tkq8mATxdGwIyhgh" "fDKQXkYuNs474553LBgOhgObJ4Oi7Aeij7XFXfBvTFLJ3ivL9pVYFxg5lUl86pVq" "5RXSJhiY+gUQFXKOWoqqxC2tMxcNBFB6M6hVIavfHLpk7PuFBFjb7wqK6nFXXQYM" "fbOXD4Wm4eTHq/WujNsJM9cejJTgSiVhnc7j0iYa0u5r8S/6BtmKCGTYdgJzPshq" "ZFIfKxgXeyAMu+EXV3phXWx3CYjAutlG4gjiT6B05asxQ9tb/OD9EI5LgtEgqSEI" "ARpyPBKnh+bXiHGaEL26WyaZwycYavTiPBqUaDS2FQvaJYPpyirUTOjbu8LbBN6O" "+S6O/BQfvsqmKHxZR05rwF2ZspZPoJDDoiM7oYZRW+ftH2EpcM7i16+4G912IXBI" "HNAGkSfVsFqpk7TqmI2P3cGG/7fckKbAj030Nck0BjGZ//////////8CAQU=" "-----END DH PARAMETERS-----"); config.set("dl", "modp/ietf/6144", "-----BEGIN X942 DH PARAMETERS-----" "MIIGDAKCAwEA///////////JD9qiIWjCNMTGYouA3BzRKQJOCIpnzHQCC76mOxOb" "IlFKCHmONATd75UZs806QxswKwpt8l8UN0/hNW1tUcJF5IW1dmJefsb0TELppjft" "awv/XLb0Brft7jhr+1qJn6WunyQRfEsf5kkoZlHs5Fs9wgB8uKFjvwWY2kg2HFXT" "mmkWP6j9JM9fg2VdI9yjrZYcYvNWIIVSu57VKQdwlpZtZww1Tkq8mATxdGwIyhgh" "fDKQXkYuNs474553LBgOhgObJ4Oi7Aeij7XFXfBvTFLJ3ivL9pVYFxg5lUl86pVq" "5RXSJhiY+gUQFXKOWoqqxC2tMxcNBFB6M6hVIavfHLpk7PuFBFjb7wqK6nFXXQYM" "fbOXD4Wm4eTHq/WujNsJM9cejJTgSiVhnc7j0iYa0u5r8S/6BtmKCGTYdgJzPshq" "ZFIfKxgXeyAMu+EXV3phXWx3CYjAutlG4gjiT6B05asxQ9tb/OD9EI5LgtEgqSEI" "ARpyPBKnh+bXiHGaEL26WyaZwycYavTiPBqUaDS2FQvaJYPpyirUTOjbu8LbBN6O" "+S6O/BQfvsqmKHxZR05rwF2ZspZPoJDDoiM7oYZRW+ftH2EpcM7i16+4G912IXBI" "HNAGkSfVsFqpk7TqmI2P3cGG/7fckKbAj030Nck0AoSSNsP6tNJ8cCbB1NyyYCZG" "3sl1HnY9uje9+P+UBq2eUw7l2zgvQTABrrBqU+2QJ9gxF5cnsIZaiRjaPtvrz5sU" "7UTObLrO1Lsb238UR+bMJUszIFFRK9evQm+49AE3jNK/WYPKAcZLkuzwMuoV0XId" "A/SC185udP721V5wL0aYDIK1qEAxkAscnlnnyX++x+jzI6l6fjbMiL4PHUW3/1ha" "xUvUB7IrQVSqzI9tfr9I4dgUzF7SD4A34KeXFe7ym+MoBqHVi7fF2nb1UKo9ih+/" "8OsZzLGjE9Vc2lbJ7C7yljI4f+jXbjwEaAQ+j2Y/SGDuEr8tWwt0dNbmlPkebcxA" "JP//////////AoIDAH//////////5IftURC0YRpiYzFFwG4OaJSBJwRFM+Y6AQXf" "Ux2JzZEopQQ8xxoCbvfKjNnmnSGNmBWFNvkvihun8Jq2tqjhIvJC2rsxLz9jeiYh" "dNMb9rWF/65begNb9vccNf2tRM/S10+SCL4lj/MklDMo9nItnuEAPlxQsd+CzG0k" "Gw4q6c00ix/UfpJnr8GyrpHuUdbLDjF5qxBCqV3PapSDuEtLNrOGGqclXkwCeLo2" "BGUMEL4ZSC8jFxtnHfHPO5YMB0MBzZPB0XYD0Ufa4q74N6YpZO8V5ftKrAuMHMqk" "vnVKtXKK6RMMTH0CiAq5Ry1FVWIW1pmLhoIoPRnUKpDV745dMnZ9woIsbfeFRXU4" "q66DBj7Zy4fC03DyY9X610ZthJnrj0ZKcCUSsM7ncekTDWl3NfiX/QNsxQQybDsB" "OZ9kNTIpD5WMC72QBl3wi6u9MK62O4TEYF1so3EEcSfQOnLVmKHtrf5wfohHJcFo" "kFSQhACNOR4JU8Pza8Q4zQhe3S2TTOGTjDV6cR4NSjQaWwqF7RLB9OUVaiZ0bd3h" "bYJvR3yXR34KD99lUxQ+LKOnNeAuzNlLJ9BIYdERndDDKK3z9o+wlLhncWvX3A3u" "uxC4JA5oA0iT6tgtVMnadUxGx+7gw3/b7khTYEem+hrkmgFCSRth/VppPjgTYOpu" "WTATI29kuo87Ht0b3vx/ygNWzymHcu2cF6CYANdYNSn2yBPsGIvLk9hDLUSMbR9t" "9efNinaiZzZdZ2pdje2/iiPzZhKlmZAoqJXr16E33HoAm8ZpX6zB5QDjJcl2eBl1" "Cui5DoH6QWvnNzp/e2qvOBejTAZBWtQgGMgFjk8s8+S/32P0eZHUvT8bZkRfB46i" "2/+sLWKl6gPZFaCqVWZHtr9fpHDsCmYvaQfAG/BTy4r3eU3xlANQ6sXb4u07eqhV" "HsUP3/h1jOZY0Ynqrm0rZPYXeUsZHD/0a7ceAjQCH0ezH6Qwdwlflq2Fujprc0p8" "jzbmIBJ//////////wIBAg==" "-----END X942 DH PARAMETERS-----"); config.set("dl", "modp/srp/6144", "-----BEGIN DH PARAMETERS-----" "MIIDCAKCAwEA///////////JD9qiIWjCNMTGYouA3BzRKQJOCIpnzHQCC76mOxOb" "IlFKCHmONATd75UZs806QxswKwpt8l8UN0/hNW1tUcJF5IW1dmJefsb0TELppjft" "awv/XLb0Brft7jhr+1qJn6WunyQRfEsf5kkoZlHs5Fs9wgB8uKFjvwWY2kg2HFXT" "mmkWP6j9JM9fg2VdI9yjrZYcYvNWIIVSu57VKQdwlpZtZww1Tkq8mATxdGwIyhgh" "fDKQXkYuNs474553LBgOhgObJ4Oi7Aeij7XFXfBvTFLJ3ivL9pVYFxg5lUl86pVq" "5RXSJhiY+gUQFXKOWoqqxC2tMxcNBFB6M6hVIavfHLpk7PuFBFjb7wqK6nFXXQYM" "fbOXD4Wm4eTHq/WujNsJM9cejJTgSiVhnc7j0iYa0u5r8S/6BtmKCGTYdgJzPshq" "ZFIfKxgXeyAMu+EXV3phXWx3CYjAutlG4gjiT6B05asxQ9tb/OD9EI5LgtEgqSEI" "ARpyPBKnh+bXiHGaEL26WyaZwycYavTiPBqUaDS2FQvaJYPpyirUTOjbu8LbBN6O" "+S6O/BQfvsqmKHxZR05rwF2ZspZPoJDDoiM7oYZRW+ftH2EpcM7i16+4G912IXBI" "HNAGkSfVsFqpk7TqmI2P3cGG/7fckKbAj030Nck0AoSSNsP6tNJ8cCbB1NyyYCZG" "3sl1HnY9uje9+P+UBq2eUw7l2zgvQTABrrBqU+2QJ9gxF5cnsIZaiRjaPtvrz5sU" "7UTObLrO1Lsb238UR+bMJUszIFFRK9evQm+49AE3jNK/WYPKAcZLkuzwMuoV0XId" "A/SC185udP721V5wL0aYDIK1qEAxkAscnlnnyX++x+jzI6l6fjbMiL4PHUW3/1ha" "xUvUB7IrQVSqzI9tfr9I4dgUzF7SD4A34KeXFe7ym+MoBqHVi7fF2nb1UKo9ih+/" "8OsZzLGjE9Vc2lbJ7C7yljI4f+jXbjwEaAQ+j2Y/SGDuEr8tWwt0dNbmlPkebcxA" "JP//////////AgEF" "-----END DH PARAMETERS-----"); config.set("dl", "modp/ietf/8192", "-----BEGIN X942 DH PARAMETERS-----" "MIIIDAKCBAEA///////////JD9qiIWjCNMTGYouA3BzRKQJOCIpnzHQCC76mOxOb" "IlFKCHmONATd75UZs806QxswKwpt8l8UN0/hNW1tUcJF5IW1dmJefsb0TELppjft" "awv/XLb0Brft7jhr+1qJn6WunyQRfEsf5kkoZlHs5Fs9wgB8uKFjvwWY2kg2HFXT" "mmkWP6j9JM9fg2VdI9yjrZYcYvNWIIVSu57VKQdwlpZtZww1Tkq8mATxdGwIyhgh" "fDKQXkYuNs474553LBgOhgObJ4Oi7Aeij7XFXfBvTFLJ3ivL9pVYFxg5lUl86pVq" "5RXSJhiY+gUQFXKOWoqqxC2tMxcNBFB6M6hVIavfHLpk7PuFBFjb7wqK6nFXXQYM" "fbOXD4Wm4eTHq/WujNsJM9cejJTgSiVhnc7j0iYa0u5r8S/6BtmKCGTYdgJzPshq" "ZFIfKxgXeyAMu+EXV3phXWx3CYjAutlG4gjiT6B05asxQ9tb/OD9EI5LgtEgqSEI" "ARpyPBKnh+bXiHGaEL26WyaZwycYavTiPBqUaDS2FQvaJYPpyirUTOjbu8LbBN6O" "+S6O/BQfvsqmKHxZR05rwF2ZspZPoJDDoiM7oYZRW+ftH2EpcM7i16+4G912IXBI" "HNAGkSfVsFqpk7TqmI2P3cGG/7fckKbAj030Nck0AoSSNsP6tNJ8cCbB1NyyYCZG" "3sl1HnY9uje9+P+UBq2eUw7l2zgvQTABrrBqU+2QJ9gxF5cnsIZaiRjaPtvrz5sU" "7UTObLrO1Lsb238UR+bMJUszIFFRK9evQm+49AE3jNK/WYPKAcZLkuzwMuoV0XId" "A/SC185udP721V5wL0aYDIK1qEAxkAscnlnnyX++x+jzI6l6fjbMiL4PHUW3/1ha" "xUvUB7IrQVSqzI9tfr9I4dgUzF7SD4A34KeXFe7ym+MoBqHVi7fF2nb1UKo9ih+/" "8OsZzLGjE9Vc2lbJ7C7yljI4f+jXbjwEaAQ+j2Y/SGDuEr8tWwt0dNbmlPkebb4R" "WXSjkm8S/uXkOHd8tqky34zYvsTQc7kxujvIMraNndMAdB+nv4r8R+0ldvaTa6Qk" "ZjqrY5xa5PVoNCO0dCvxyXgjjxbL451lLeP9uL78hIrZIiIuBKQDfAcT61eoGiPw" "xzRz/GRs6jBrS8vIhi+Dhd36nUt/osCH6HloMwPtW906Bis89bOieKZtKhP4P0T4" "Ld8xDuB0q2o2RZfomaAlXcFk8xzFCEaFHfmrSBld7X6hsdUQvX7nTXP682vDHs+i" "aDWQRvTrh5+SQAlDi0gcbNeImgAu1e44K8kZDab8Am5HlVjkR1Z36aqeMFDidlaU" "38gfVuiAuW5xYMmA3Zjt09///////////wKCBAB//////////+SH7VEQtGEaYmMx" "RcBuDmiUgScERTPmOgEF31Mdic2RKKUEPMcaAm73yozZ5p0hjZgVhTb5L4obp/Ca" "trao4SLyQtq7MS8/Y3omIXTTG/a1hf+uW3oDW/b3HDX9rUTP0tdPkgi+JY/zJJQz" "KPZyLZ7hAD5cULHfgsxtJBsOKunNNIsf1H6SZ6/Bsq6R7lHWyw4xeasQQqldz2qU" "g7hLSzazhhqnJV5MAni6NgRlDBC+GUgvIxcbZx3xzzuWDAdDAc2TwdF2A9FH2uKu" "+DemKWTvFeX7SqwLjBzKpL51SrVyiukTDEx9AogKuUctRVViFtaZi4aCKD0Z1CqQ" "1e+OXTJ2fcKCLG33hUV1OKuugwY+2cuHwtNw8mPV+tdGbYSZ649GSnAlErDO53Hp" "Ew1pdzX4l/0DbMUEMmw7ATmfZDUyKQ+VjAu9kAZd8IurvTCutjuExGBdbKNxBHEn" "0Dpy1Zih7a3+cH6IRyXBaJBUkIQAjTkeCVPD82vEOM0IXt0tk0zhk4w1enEeDUo0" "GlsKhe0SwfTlFWomdG3d4W2Cb0d8l0d+Cg/fZVMUPiyjpzXgLszZSyfQSGHREZ3Q" "wyit8/aPsJS4Z3Fr19wN7rsQuCQOaANIk+rYLVTJ2nVMRsfu4MN/2+5IU2BHpvoa" "5JoBQkkbYf1aaT44E2DqblkwEyNvZLqPOx7dG978f8oDVs8ph3LtnBegmADXWDUp" "9sgT7BiLy5PYQy1EjG0fbfXnzYp2omc2XWdqXY3tv4oj82YSpZmQKKiV69ehN9x6" "AJvGaV+sweUA4yXJdngZdQrouQ6B+kFr5zc6f3tqrzgXo0wGQVrUIBjIBY5PLPPk" "v99j9HmR1L0/G2ZEXweOotv/rC1ipeoD2RWgqlVmR7a/X6Rw7ApmL2kHwBvwU8uK" "93lN8ZQDUOrF2+LtO3qoVR7FD9/4dYzmWNGJ6q5tK2T2F3lLGRw/9Gu3HgI0Ah9H" "sx+kMHcJX5athbo6a3NKfI823wisulHJN4l/cvIcO75bVJlvxmxfYmg53JjdHeQZ" "W0bO6YA6D9PfxX4j9pK7e0m10hIzHVWxzi1yerQaEdo6FfjkvBHHi2XxzrKW8f7c" "X35CRWyRERcCUgG+A4n1q9QNEfhjmjn+MjZ1GDWl5eRDF8HC7v1Opb/RYEP0PLQZ" "gfat7p0DFZ562dE8UzaVCfwfonwW75iHcDpVtRsiy/RM0BKu4LJ5jmKEI0KO/NWk" "DK72v1DY6ohev3Omuf15teGPZ9E0GsgjenXDz8kgBKHFpA42a8RNABdq9xwV5IyG" "034BNyPKrHIjqzv01U8YKHE7K0pv5A+rdEBctziwZMBuzHbp7///////////AgEC" "-----END X942 DH PARAMETERS-----"); config.set("dl", "modp/srp/8192", "-----BEGIN DH PARAMETERS-----" "MIIECAKCBAEA///////////JD9qiIWjCNMTGYouA3BzRKQJOCIpnzHQCC76mOxOb" "IlFKCHmONATd75UZs806QxswKwpt8l8UN0/hNW1tUcJF5IW1dmJefsb0TELppjft" "awv/XLb0Brft7jhr+1qJn6WunyQRfEsf5kkoZlHs5Fs9wgB8uKFjvwWY2kg2HFXT" "mmkWP6j9JM9fg2VdI9yjrZYcYvNWIIVSu57VKQdwlpZtZww1Tkq8mATxdGwIyhgh" "fDKQXkYuNs474553LBgOhgObJ4Oi7Aeij7XFXfBvTFLJ3ivL9pVYFxg5lUl86pVq" "5RXSJhiY+gUQFXKOWoqqxC2tMxcNBFB6M6hVIavfHLpk7PuFBFjb7wqK6nFXXQYM" "fbOXD4Wm4eTHq/WujNsJM9cejJTgSiVhnc7j0iYa0u5r8S/6BtmKCGTYdgJzPshq" "ZFIfKxgXeyAMu+EXV3phXWx3CYjAutlG4gjiT6B05asxQ9tb/OD9EI5LgtEgqSEI" "ARpyPBKnh+bXiHGaEL26WyaZwycYavTiPBqUaDS2FQvaJYPpyirUTOjbu8LbBN6O" "+S6O/BQfvsqmKHxZR05rwF2ZspZPoJDDoiM7oYZRW+ftH2EpcM7i16+4G912IXBI" "HNAGkSfVsFqpk7TqmI2P3cGG/7fckKbAj030Nck0AoSSNsP6tNJ8cCbB1NyyYCZG" "3sl1HnY9uje9+P+UBq2eUw7l2zgvQTABrrBqU+2QJ9gxF5cnsIZaiRjaPtvrz5sU" "7UTObLrO1Lsb238UR+bMJUszIFFRK9evQm+49AE3jNK/WYPKAcZLkuzwMuoV0XId" "A/SC185udP721V5wL0aYDIK1qEAxkAscnlnnyX++x+jzI6l6fjbMiL4PHUW3/1ha" "xUvUB7IrQVSqzI9tfr9I4dgUzF7SD4A34KeXFe7ym+MoBqHVi7fF2nb1UKo9ih+/" "8OsZzLGjE9Vc2lbJ7C7yljI4f+jXbjwEaAQ+j2Y/SGDuEr8tWwt0dNbmlPkebb4R" "WXSjkm8S/uXkOHd8tqky34zYvsTQc7kxujvIMraNndMAdB+nv4r8R+0ldvaTa6Qk" "ZjqrY5xa5PVoNCO0dCvxyXgjjxbL451lLeP9uL78hIrZIiIuBKQDfAcT61eoGiPw" "xzRz/GRs6jBrS8vIhi+Dhd36nUt/osCH6HloMwPtW906Bis89bOieKZtKhP4P0T4" "Ld8xDuB0q2o2RZfomaAlXcFk8xzFCEaFHfmrSBld7X6hsdUQvX7nTXP682vDHs+i" "aDWQRvTrh5+SQAlDi0gcbNeImgAu1e44K8kZDab8Am5HlVjkR1Z36aqeMFDidlaU" "38gfVuiAuW5xYMmA3Zjt09///////////wIBEw==" "-----END DH PARAMETERS-----"); config.set("dl", "dsa/jce/512", "-----BEGIN DSA PARAMETERS-----" "MIGdAkEA/KaCzo4Syrom78z3EQ5SbbB4sF7ey80etKII864WF64B81uRpH5t9jQT" "xeEu0ImbzRMqzVDZkVG9xD7nN1kuFwIVAJYu3cw2nLqOuyYO5rahJtk0bjjFAkEA" "3gtU76vylwh+5iPVylWIxkgo70/eT/uuHs0gBndrBbEbgeo83pvDlkwWh8UyW/Q9" "fM76DQqGvl3/3dDRFD3NdQ==" "-----END DSA PARAMETERS-----"); config.set("dl", "dsa/jce/768", "-----BEGIN DSA PARAMETERS-----" "MIHdAmEA6eZCWZ01XzfJf/01ZxILjiXJzUPpJ7OpZw++xdiQFBki0sOzrSSACTeZ" "hp0ehGqrSfqwrSbSzmoiIZ1HC859d31KIfvpwnC1f2BwAvPO+Dk2lM9F7jaIwRqM" "VqsSej2vAhUAnNvYTJ8awvOND4D0KrlS5zOL9RECYQDe7p717RUWzn5pXmcrjO5F" "5s17NuDmOF+JS6hhY/bz5sbU6KgRRtQBfe/dccvZD6Akdlm4i3zByJT0gmn9Txqs" "CjBTjf9rP8ds+xMcnnlltYhYqwpDtVczWRKoqlR/lWg=" "-----END DSA PARAMETERS-----"); config.set("dl", "dsa/jce/1024", "-----BEGIN DSA PARAMETERS-----" "MIIBHgKBgQD9f1OBHXUSKVLfSpwu7OTn9hG3UjzvRADDHj+AtlEmaUVdQCJR+1k9" "jVj6v8X1ujD2y5tVbNeBO4AdNG/yZmC3a5lQpaSfn+gEexAiwk+7qdf+t8Yb+DtX" "58aophUPBPuD9tPFHsMCNVQTWhaRMvZ1864rYdcq7/IiAxmd0UgBxwIVAJdgUI8V" "IwvMspK5gqLrhAvwWBz1AoGARpYDUS4wJ4zTlHWV2yLuyYJqYyKtyXNE9B10DDJX" "JMj577qn1NgD/4xgnc0QDrxb38+tfGpCX66nhuogUOvpg1HqH9of3yTWlHqmuaoj" "dmlTgC9NfUqOy6BtGXaKJJH/sW0O+cQ6mbX3FnL/bwoktETQc20E04oaEyLa9s3Y" "jJ0=" "-----END DSA PARAMETERS-----"); config.set("dl", "dsa/botan/2048", "-----BEGIN DSA PARAMETERS-----" "MIICLAKCAQEAkcSKT9+898Aq6V59oSYSK13Shk9Vm4fo50oobVL1m9HeaN/WRdDg" "DGDAgAMYkZgDdO61lKUyv9Z7mgnqxLhmOgeRDmjzlGX7cEDSXfE5MuusQ0elMOy6" "YchU+biA08DDZgCAWHxFVm2t4mvVo5S+CTtMDyS1r/747GxbPlf7iQJam8FnaZMh" "MeFtPJTvyrGNDfBhIDzFPmEDvHLVWUv9QMplOA9EqahR3LB1SV/AM6ilgHGhvXj+" "BS9mVVZI60txnSr+i0iA+NrW8VgYuhePiSdMhwvpuW6wjEbEAEDMLv4d+xsYaN0x" "nePDSjKmOrbrEiQgmkGWgMx5AtFyjU354QIhAIzX1FD4bwrZTu5M5GmodW0evRBY" "JBlD6v+ws1RYXpJNAoIBAA2fXgdhtNvRgz1qsalhoJlsXyIwP3LYTBQPZ8Qx2Uq1" "cVvqgaDJjTnOS8941rnryJXTT+idlAkdWEhhXvFfXobxHZb2yWniA936WDVkIKSc" "tES1lbkBqTPP4HZ7WU8YoHt/kd7NukRriJkPePL/kfL+fNQ/0uRtGOraH3u2YCxh" "f27zpLKE8v2boQo2BC3o+oeiyjZZf+yBFXoUheRAQd8CgwERy4gLvm7UlIFIhvll" "zcMTX1zPE4Nyi/ZbgG+WksCxDWxMCcdabKO0ATyxarLBBfa+I66pAA6rIXiYX5cs" "mAV+HIbkTnIYaI6krg82NtzKdFydzU5q/7Z8y8E9YTE=" "-----END DSA PARAMETERS-----"); config.set("dl", "dsa/botan/3072", "-----BEGIN DSA PARAMETERS-----" "MIIDLAKCAYEA5LUIgHWWY1heFCRgyi2d/xMviuTIQN2jomZoiRJP5WOLhOiim3rz" "+hIJvmv8S1By7Tsrc4e68/hX9HioAijvNgC3az3Pth0g00RlslBtLK+H3259wM6R" "vS0Wekb2rcwxxTHk+cervbkq3fNbCoBsZikqX14X6WTdCZkDczrEKKs12A6m9oW/" "uovkBo5UGK5eytno/wc94rY+Tn6tNciptwtb1Hz7iNNztm83kxk5sKtxvVWVgJCG" "2gFVM30YWg5Ps2pRmxtiArhZHmACRJzxzTpmOE9tIHOxzXO+ypO68eGmEX0COPIi" "rh7X/tGFqJDn9n+rj+uXU8wTSlGD3+h64llfe1wtn7tCJJ/dWVE+HTOWs+sv2GaE" "8oWoRI/nV6ApiBxAdguU75Gb35dAw4OJWZ7FGm6btRmo4GhJHpzgovz+PLYNZs8N" "+tIKjsaEBIaEphREV1vRck1zUrRKdgB3s71r04XOWwpyUMwL92jagpI4Buuc+7E4" "hDcxthggjHWbAiEAs+vTZOxp74zzuvZDt1c0sWM5suSeXN4bWcHp+0DuDFsCggGA" "K+0h7vg5ZKIwrom7px2ffDnFL8gim047x+WUTTKdoQ8BDqyee69sAJ/E6ylgcj4r" "Vt9GY+TDrIAOkljeL3ZJ0gZ4KJP4Ze/KSY0u7zAHTqXop6smJxKk2UovOwuaku5A" "D7OKPMWaXcfkNtXABLIuNQKDgbUck0B+sy1K4P1Cy0XhLQ7O6KJiOO3iCCp7FSIR" "PGbO+NdFxs88uUX4TS9N4W1Epx3hmCcOE/A1U8iLjTI60LlIob8hA6lJl5tu0W+1" "88lT2Vt8jojKZ9z1pjb7nKOdkkIV96iE7Wx+48ltjZcVQnl0t8Q1EoLhPTdz99KL" "RS8QiSoTx1hzKN6kgntrNpsqjcFyrcWD9R8qZZjFSD5bxGewL5HQWcQC0Y4sJoD3" "dqoG9JKAoscsF8xC1bbnQMXEsas8UcLtCSviotiwU65Xc9FCXtKwjwbi3VBZLfGk" "eMFVkc39EVZP+I/zi3IdQjkv2kcyEtz9jS2IqXagCv/m//tDCjWeZMorNRyiQSOU" "-----END DSA PARAMETERS-----"); config.set("ec", "secp112r1", "-----BEGIN EC PARAMETERS-----" "MHQCAQEwGgYHKoZIzj0BAQIPANt8Kr9i415mgHa+rSCLMCAEDtt8Kr9i415mgHa+" "rSCIBA5lnvi6BDkW7t6JEXArIgQdBAlIcjmZWl7na1X5wvCYqJzlr4ckwKI+Dg/3" "dQACDwDbfCq/YuNedijfrGVhxQIBAQ==" "-----END EC PARAMETERS-----"); config.set("ec", "secp112r2", "-----BEGIN EC PARAMETERS-----" "MHMCAQEwGgYHKoZIzj0BAQIPANt8Kr9i415mgHa+rSCLMCAEDmEnwkwF84oKqvZc" "DvAsBA5R3vGBXbXtdPzDTIXXCQQdBEujCrXokrThZJ3QkoZDrc1G9YguN0fe826V" "bpcCDjbfCq/YuNdZfKEFINBLAgEB" "-----END EC PARAMETERS-----"); config.set("ec", "secp128r1", "-----BEGIN EC PARAMETERS-----" "MIGAAgEBMBwGByqGSM49AQECEQD////9////////////////MCQEEP////3/////" "//////////wEEOh1ecEQefQ92CSZPCzuXtMEIQQWH/dSi4mbLQwoYHylLFuGz1rI" "OVuv6xPALaKS3e16gwIRAP////4AAAAAdaMNG5A4oRUCAQE=" "-----END EC PARAMETERS-----"); config.set("ec", "secp128r2", "-----BEGIN EC PARAMETERS-----" "MH8CAQEwHAYHKoZIzj0BAQIRAP////3///////////////8wJAQQ1gMZmNGzu/6/" "Wcybv/mu4QQQXu78o4DQKRncLGVYu22KXQQhBHtqpdheVymD5vsyp83rwUAntpFq" "iU067nEG/oBfw0tEAhA/////f////74AJHIGE7WjAgEE" "-----END EC PARAMETERS-----"); config.set("ec", "secp160k1", "-----BEGIN EC PARAMETERS-----" "MIGYAgEBMCAGByqGSM49AQECFQD////////////////////+//+sczAsBBQAAAAA" "AAAAAAAAAAAAAAAAAAAAAAQUAAAAAAAAAAAAAAAAAAAAAAAAAAcEKQQ7TDgs43qh" "kqQBnnYwNvT13U1+u5OM+TUxj9zta8KChlMXM8PwPE/uAhUBAAAAAAAAAAAAAbj6" "Ft+rmsoWtrMCAQE=" "-----END EC PARAMETERS-----"); config.set("ec", "secp160r1", "-----BEGIN EC PARAMETERS-----" "MIGYAgEBMCAGByqGSM49AQECFQD/////////////////////f////zAsBBT/////" "////////////////f////AQUHJe+/FS9eotlrPifgdTUrcVl+kUEKQRKlrVojvVz" "KEZkaYlow4u5E8v8giOmKFUxaJR9WdzJEgQjUTd6xfsyAhUBAAAAAAAAAAAAAfTI" "+Seu08p1IlcCAQE=" "-----END EC PARAMETERS-----"); config.set("ec", "secp160r2", "-----BEGIN EC PARAMETERS-----" "MIGYAgEBMCAGByqGSM49AQECFQD////////////////////+//+sczAsBBT/////" "///////////////+//+scAQUtOE00/tZ64urVydJBGZNWvUDiLoEKQRS3LA0KToR" "fh9P8Rsw9xmdMUTObf6v/vLjMfKW4HH6DfmYLP6n1D8uAhUBAAAAAAAAAAAAADUe" "54aoGPOhoWsCAQE=" "-----END EC PARAMETERS-----"); config.set("ec", "secp192k1", "-----BEGIN EC PARAMETERS-----" "MIGwAgEBMCQGByqGSM49AQECGQD//////////////////////////v//7jcwNAQY" "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBgAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" "AAMEMQTbT/EOwFfpriawfQKAt/Q0HaXRsergbH2bLy9tnFYop4RBY9AVvoY0QIKq" "iNleL50CGQD///////////////4m8vwXD2lGanTe/Y0CAQE=" "-----END EC PARAMETERS-----"); config.set("ec", "secp192r1", "-----BEGIN EC PARAMETERS-----" "MIGwAgEBMCQGByqGSM49AQECGQD////////////////////+//////////8wNAQY" "/////////////////////v/////////8BBhkIQUZ5ZyA5w+n6atyJDBJ/rje7MFG" "ubEEMQQYjagOsDCQ9ny/IOtDoYgA9P8K/YL/EBIHGSuV/8jaeGMQEe1rJM3Vc/l3" "oR55SBECGQD///////////////+Z3vg2FGvJsbTSKDECAQE=" "-----END EC PARAMETERS-----"); config.set("ec", "secp224k1", "-----BEGIN EC PARAMETERS-----" "MIHIAgEBMCgGByqGSM49AQECHQD///////////////////////////////7//+Vt" "MDwEHAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEHAAAAAAAAAAAAAAAAAAA" "AAAAAAAAAAAAAAAAAAUEOQShRVszTfCZ3zD8KKFppGfp5HB1qQ9+ZQ62t6Rcfgif" "7X+6NEKCyvvW9+MZ98CwvVniykvbVW1hpQIdAQAAAAAAAAAAAAAAAAAB3OjS7GGE" "yvCpcXafsfcCAQE=" "-----END EC PARAMETERS-----"); config.set("ec", "secp224r1", "-----BEGIN EC PARAMETERS-----" "MIHIAgEBMCgGByqGSM49AQECHQD/////////////////////AAAAAAAAAAAAAAAB" "MDwEHP////////////////////7///////////////4EHLQFCoUMBLOr9UEyVlBE" "sLfXv9i6Jws5QyNV/7QEOQS3Dgy9a7S/fzITkLlKA8HTVsIRIjQygNYRXB0hvTdj" "iLX3I/tMIt/mzUN1oFoHR2RE1YGZhQB+NAIdAP//////////////////FqLguPA+" "E90pRVxcKj0CAQE=" "-----END EC PARAMETERS-----"); config.set("ec", "secp256k1", "-----BEGIN EC PARAMETERS-----" "MIHgAgEBMCwGByqGSM49AQECIQD////////////////////////////////////+" "///8LzBEBCAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQgAAAAAAAA" "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAcEQQR5vmZ++dy7rFWgYpXOhwsHApv8" "2y3OKNlZ8oFbFvgXmEg62ncmo8RlXaT7/A4RCKj9F7RIpoVUGZxH0I/7ENS4AiEA" "/////////////////////rqu3OavSKA7v9JejNA2QUECAQE=" "-----END EC PARAMETERS-----"); config.set("ec", "secp256r1", "-----BEGIN EC PARAMETERS-----" "MIHgAgEBMCwGByqGSM49AQECIQD/////AAAAAQAAAAAAAAAAAAAAAP//////////" "/////zBEBCD/////AAAAAQAAAAAAAAAAAAAAAP///////////////AQgWsY12Ko6" "k+ez671VdpiGvGUdBrDMU7D2O848PifSYEsEQQRrF9Hy4SxCR/i85uVjpEDydwN9" "gS3rM6D0oTlF2JjClk/jQuL+Gn+bjufrSnwPnhYrzjNXazFezsu2QGg3v1H1AiEA" "/////wAAAAD//////////7zm+q2nF56E87nKwvxjJVECAQE=" "-----END EC PARAMETERS-----"); config.set("ec", "secp384r1", "-----BEGIN EC PARAMETERS-----" "MIIBQAIBATA8BgcqhkjOPQEBAjEA////////////////////////////////////" "//////7/////AAAAAAAAAAD/////MGQEMP//////////////////////////////" "///////////+/////wAAAAAAAAAA/////AQwszEvp+I+5+SYjgVr4/gtGRgdnG7+" "gUESAxQIj1ATh1rGVjmNii7RnSqFyO3T7CrvBGEEqofKIr6LBTeOscce8yCtdG4d" "O2KLp5uYWfdB4IJUKjhVAvJdv1UpbDpUXjhydgq3NhfeSpYmLG9dnpi/kpLcKfj0" "Hb0omhR86doxE7XwuMAKYLHOHX6BnXpDHXyQ6g5fAjEA////////////////////" "////////////x2NNgfQ3Ld9YGg2ySLCneuzsGWrMxSlzAgEB" "-----END EC PARAMETERS-----"); config.set("ec", "secp521r1", "-----BEGIN EC PARAMETERS-----" "MIIBrAIBATBNBgcqhkjOPQEBAkIB////////////////////////////////////" "//////////////////////////////////////////////////8wgYgEQgH/////" "////////////////////////////////////////////////////////////////" "/////////////////ARCAFGVPrlhjhyaH5KaIaC2hUDuotpyW5mzFfO4tImRjvEJ" "4VYZOVHsfpN7FlLAvTuxvwc1c9+IPSw08e9FH9RrUD8ABIGFBADGhY4GtwQE6c2e" "PstmI5W0QpxkgTkFP7Uh+CivYGtNPbqhS1537+dZKP4dwSei/6jeM0izwYVqQpv5" "fn4xwuW9ZgEYOSlqeJo7wARcil+0LH0b2Zj1RElXm0RoF6+9Fyc+ZiyX7nKZXvQm" "QMVQuQE/rQdhNTxwhqJywkCIvpR2n9FmUAJCAf//////////////////////////" "////////////////+lGGh4O/L5Zrf8wBSPcJpdA7tcm4iZxHrrtvtx6ROGQJAgEB" "-----END EC PARAMETERS-----"); config.set("ec", "1.3.6.1.4.1.8301.3.1.2.9.0.38", "-----BEGIN EC PARAMETERS-----" "MIIBrAIBATBNBgcqhkjOPQEBAkIB////////////////////////////////////" "//////////////////////////////////////////////////8wgYgEQgH/////" "////////////////////////////////////////////////////////////////" "/////////////////ARCAFGVPrlhjhyaH5KaIaC2hUDuotpyW5mzFfO4tImRjvEJ" "4VYZOVHsfpN7FlLAvTuxvwc1c9+IPSw08e9FH9RrUD8ABIGFBADGhY4GtwQE6c2e" "PstmI5W0QpxkgTkFP7Uh+CivYGtNPbqhS1537+dZKP4dwSei/6jeM0izwYVqQpv5" "fn4xwuW9ZgEYOSlqeJo7wARcil+0LH0b2Zj1RElXm0RoF6+9Fyc+ZiyX7nKZXvQm" "QMVQuQE/rQdhNTxwhqJywkCIvpR2n9FmUAJCAf//////////////////////////" "////////////////+lGGh4O/L5Zrf8wBSPcJpdA7tcm4iZxHrrtvtx6ROGQJAgEB" "-----END EC PARAMETERS-----"); config.set("ec", "brainpool160r1", "-----BEGIN EC PARAMETERS-----" "MIGYAgEBMCAGByqGSM49AQECFQDpXkpfc3BZ3GDfx62Vs9gTlRViDzAsBBQ0Dnvi" "ooDrdOK+YbradF2X6PfDAAQUHliahZVCNBITT6otveyVyNhnXlgEKQS+1a8W6j9q" "T2KTjEYx61r3vbzbwxZny0d6Go7DOPlHQWacl2MW2mMhAhUA6V5KX3NwWdxg31mR" "1FApQJ5g/AkCAQE=" "-----END EC PARAMETERS-----"); config.set("ec", "brainpool192r1", "-----BEGIN EC PARAMETERS-----" "MIGwAgEBMCQGByqGSM49AQECGQDDAvQdkyo2zaejRjCT0Y23j85HbeGoYpcwNAQY" "apEXQHax4OGcOcAx/oaFwcrgQOXGmijvBBhGmijvfCjMo9xyHQRPRJa8yn70FG+/" "JckEMQTAoGR+qrakh1OwM8VssPCQCi9cSFM3X9YUtpCGar1buItfSCjBSQAC5nc/" "ovopm48CGQDDAvQdkyo2zaejRi+enpFrW+jxAprErMECAQE=" "-----END EC PARAMETERS-----"); config.set("ec", "brainpool224r1", "-----BEGIN EC PARAMETERS-----" "MIHIAgEBMCgGByqGSM49AQECHQDXwTSqJkNmhioYMCV10deHsJ8HV5faifV+yMD/" "MDwEHGil5iypzmwcKZgDpsFTC1FOGCrYsAQqWcrSn0MEHCWA9jzP5EE4hwcTsakj" "aeM+ITXSZtuzcjhsQAsEOQQNkCmtLH5c9DQII7KofcaMnkzjF0webv3uEsB9WKpW" "93LAcm8kxrieTs2sJDVLnpnKo/bTdhQCzQIdANfBNKomQ2aGKhgwJXXQ+5jRFrxL" "bd68o6Wnk58CAQE=" "-----END EC PARAMETERS-----"); config.set("ec", "brainpool256r1", "-----BEGIN EC PARAMETERS-----" "MIHgAgEBMCwGByqGSM49AQECIQCp+1fboe6pvD5mCpCdg41ybjv2I9UmICggE0gd" "H25TdzBEBCB9Wgl1/CwwV+72dTBBev/n+4BVwSbcXGzpSktE8zC12QQgJtxcbOlK" "S0TzMLXZu9d8v5WEFilc9+HOa8zcGP+MB7YEQQSL0q65y35XyyxLSC/8gbevud4n" "4eO9I8I6RFO9ms4yYlR++DXD2sT9l/hGGhRhHcnCd0UTLe2OVFwdVMcvBGmXAiEA" "qftX26Huqbw+ZgqQnYONcYw5eqO1Yab3kB4OgpdIVqcCAQE=" "-----END EC PARAMETERS-----"); config.set("ec", "brainpool320r1", "-----BEGIN EC PARAMETERS-----" "MIIBEAIBATA0BgcqhkjOPQEBAikA015HIDa8T7fhPHhe0gHgZfmPz6b29A3vT5K5" "7HiT7Cj81BKx8bMuJzBUBCg+4wtWj7qw+IPM69RtPzu4oqc1E/XredpmGQ6whf+p" "9JLzdal9hg60BChSCIOUnf28QtOtGYZAaIpv4T9BNJVUtJrMMdzNiEU5gW9etKyP" "sfGmBFEEQ71+mvtT2LhSibzEjuW/5vIBN9EKCH6254ceKhClmccQr40NOeIGERT9" "0FVF7BzIq0CTJH93J14HQ//tEXGC6qnHeHeqrGrH01JF0WkujuECKQDTXkcgNrxP" "t+E8eF7SAeBl+Y/PpbaPEqMtSC7H7oZY6YaRVVtExZMRAgEB" "-----END EC PARAMETERS-----"); config.set("ec", "brainpool384r1", "-----BEGIN EC PARAMETERS-----" "MIIBQAIBATA8BgcqhkjOPQEBAjEAjLkegqM4bSgPXW9+UOZB3xUvcQntVFa0ErHa" "GX+3ESOs06cpkB0acYdHABMxB+xTMGQEMHvDgsY9jBUMPHIICs4Fr6DCvqKOT7In" "hxORZe+6kfkPiqWBSlA61OsEqMfdIs4oJgQwBKjH3SLOKCaLObVUFvBEfC+3feEH" "3NKmLogOpT7rYtV8tDkCldvJlDq3hpb6UEwRBGEEHRxk8GjPRf+ipjqBt8E/a4hH" "o+d+8U/j23/K/gy9EOjoJuA0NtZGqu+HsuJH1K8eir4ddSD5wqRcseuOlc/VUmK3" "Cyn+7Fhk4ZwFT/mRKSgORkYhd5GBEUKCA0EmPFMVAjEAjLkegqM4bSgPXW9+UOZB" "3xUvcQntVFazHxZubKwEJafPOrava3/DEDuIMgLpBGVlAgEB" "-----END EC PARAMETERS-----"); config.set("ec", "brainpool512r1", "-----BEGIN EC PARAMETERS-----" "MIIBogIBATBMBgcqhkjOPQEBAkEAqt2duNvpxIs/1OauM8n8B8swjbOzydIO1mOc" "ynAzCHF9TZsAm8ZoQq7NoSrmo4DmKIH/Ly2CxoUoqmBWWDpI8zCBhARAeDCjMYtg" "O4niMnFFrCNMxZTL3Y09+RYQqDRByuqYY7wt7V1aqCU6oQou8cmLmsi1fxEXpyvy" "x7nnwaxNd/yUygRAPfkWEKg0QcrqmGO8Le1dWqglOqEKLvHJi5rItX8RF6cr8se5" "58GsTXf8lMrcCD5nmEBQt1665d0oCb1jgBb3IwSBgQSBruS92C7ZZFohMi6cTGqT" "he2fcLXZFsG0O2Lu9NAJjv87H3ji0NSNUNFoe5O5fV98bVBHQGpeaIs1Igm8ufgi" "fd44XVZjMuzA6r+pz3gi/fIJ9wAkpXsaoADFW4gfgRGy3N5JSl9IXlvKS9iKJ2Ou" "0corL6jwVAZ4zR4POtgIkgJBAKrdnbjb6cSLP9TmrjPJ/AfLMI2zs8nSDtZjnMpw" "MwhwVT5cQUypJhlBhmEZf6wQRx2x04EIXdrdtYeWgpypAGkCAQE=" "-----END EC PARAMETERS-----"); config.set("ec", "x962_p192v2", "-----BEGIN EC PARAMETERS-----" "MIGwAgEBMCQGByqGSM49AQECGQD////////////////////+//////////8wNAQY" "/////////////////////v/////////8BBjMItbfuVxrJeScDWNkpOWYDDk6ohZo" "2VMEMQTuorrn4Ul4QvLed2nP6cmJwHKtaW9IA0pldNEdabbsemcruCoIPfLysIR9" "6XCy3hUCGQD///////////////5fsack3IBBhkjY3TECAQE=" "-----END EC PARAMETERS-----"); config.set("ec", "x962_p192v3", "-----BEGIN EC PARAMETERS-----" "MIGwAgEBMCQGByqGSM49AQECGQD////////////////////+//////////8wNAQY" "/////////////////////v/////////8BBgiEj3COVoFyqdCPa7MyUdgp9RiJWvV" "aRYEMQR9KXeBAMZaHaF4NxZYjc4ri0rujiKPGJY4qQ8iY3M3M0tJ3LZqbcj5l4rK" "dkipQ7ACGQD///////////////96YtAxyD9ClPZA7BMCAQE=" "-----END EC PARAMETERS-----"); config.set("ec", "x962_p239v1", "-----BEGIN EC PARAMETERS-----" "MIHSAgEBMCkGByqGSM49AQECHn///////////////3///////4AAAAAAAH//////" "/zBABB5///////////////9///////+AAAAAAAB///////wEHmsBbDvc8YlB0NZU" "khR1ynGp2y+yfR03eWGFwpQsCgQ9BA/6ljzcqIFszDO4ZCvt+QXD01hXPT8n+707" "PLmqr33r6OTpCl2ubkBUylMLoEZUs2gYziJrOfzLewLxrgIef///////////////" "f///nl6an12QcfvRUiaIkJ0LAgEB" "-----END EC PARAMETERS-----"); config.set("ec", "x962_p239v2", "-----BEGIN EC PARAMETERS-----" "MIHSAgEBMCkGByqGSM49AQECHn///////////////3///////4AAAAAAAH//////" "/zBABB5///////////////9///////+AAAAAAAB///////wEHmF/q2gyV2y7/tUN" "mfAknD/uWLlLoAOMeuhMjIMvLAQ9BDivCdmHJ3BRIMkhu16eJilqPNzy81dXoOr9" "h7gw51sBJeTb6g7HIG2g/AHZsIEyn7VV3m70YCN9/4vkugIef///////////////" "gAAAz6foWUN31BTAOCG8WCBjAgEB" "-----END EC PARAMETERS-----"); config.set("ec", "x962_p239v3", "-----BEGIN EC PARAMETERS-----" "MIHSAgEBMCkGByqGSM49AQECHn///////////////3///////4AAAAAAAH//////" "/zBABB5///////////////9///////+AAAAAAAB///////wEHiVXBfoqMGZUsfTL" "A9anUKMMJQEC1JiHF9m6FattPgQ9BGdoro4Yu5LPzwBclJqixtlIU9DmYLv4VLHJ" "UF/pWhYH5omPOQwGvB1VK60ibztvz+SLboGEma8Y4+1s8wIef///////////////" "f///l13rQbOmBXw8QyFGUmVRAgEB" "-----END EC PARAMETERS-----"); config.set("ec", "gost_256A", "-----BEGIN EC PARAMETERS-----" "MIHgAgEBMCwGByqGSM49AQECIQD/////////////////////////////////////" "///9lzBEBCD////////////////////////////////////////9lAQgAAAAAAAA" "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKYEQQQAAAAAAAAAAAAAAAAAAAAAAAAA" "AAAAAAAAAAAAAAAAAY2R5HHgmJzaJ99QWkU/K3Y1KU8t3yPjsSKsyZyenx4UAiEA" "/////////////////////2xhEHCZWtEARYQbCbdhuJMCAQE=" "-----END EC PARAMETERS-----"); } } /* * Set the default policy */ void Library_State::load_default_config() { set_default_aliases(*this); set_default_oids(*this); set_default_dl_groups(*this); } } /* * SCAN Name Abstraction * (C) 2008-2009 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <stdexcept> namespace Botan { namespace { std::string make_arg( const std::vector<std::pair<size_t, std::string> >& name, size_t start) { std::string output = name[start].second; size_t level = name[start].first; size_t paren_depth = 0; for(size_t i = start + 1; i != name.size(); ++i) { if(name[i].first <= name[start].first) break; if(name[i].first > level) { output += '(' + name[i].second; ++paren_depth; } else if(name[i].first < level) { output += ")," + name[i].second; --paren_depth; } else { if(output[output.size() - 1] != '(') output += ","; output += name[i].second; } level = name[i].first; } for(size_t i = 0; i != paren_depth; ++i) output += ')'; return output; } std::pair<size_t, std::string> deref_aliases(const std::pair<size_t, std::string>& in) { return std::make_pair(in.first, global_state().deref_alias(in.second)); } } SCAN_Name::SCAN_Name(std::string algo_spec) { orig_algo_spec = algo_spec; std::vector<std::pair<size_t, std::string> > name; size_t level = 0; std::pair<size_t, std::string> accum = std::make_pair(level, ""); std::string decoding_error = "Bad SCAN name '" + algo_spec + "': "; algo_spec = global_state().deref_alias(algo_spec); for(size_t i = 0; i != algo_spec.size(); ++i) { char c = algo_spec[i]; if(c == '/' || c == ',' || c == '(' || c == ')') { if(c == '(') ++level; else if(c == ')') { if(level == 0) throw Decoding_Error(decoding_error + "Mismatched parens"); --level; } if(c == '/' && level > 0) accum.second.push_back(c); else { if(accum.second != "") name.push_back(deref_aliases(accum)); accum = std::make_pair(level, ""); } } else accum.second.push_back(c); } if(accum.second != "") name.push_back(deref_aliases(accum)); if(level != 0) throw Decoding_Error(decoding_error + "Missing close paren"); if(name.size() == 0) throw Decoding_Error(decoding_error + "Empty name"); alg_name = name[0].second; bool in_modes = false; for(size_t i = 1; i != name.size(); ++i) { if(name[i].first == 0) { mode_info.push_back(make_arg(name, i)); in_modes = true; } else if(name[i].first == 1 && !in_modes) args.push_back(make_arg(name, i)); } } std::string SCAN_Name::algo_name_and_args() const { std::string out; out = algo_name(); if(arg_count()) { out += '('; for(size_t i = 0; i != arg_count(); ++i) { out += arg(i); if(i != arg_count() - 1) out += ','; } out += ')'; } return out; } std::string SCAN_Name::arg(size_t i) const { if(i >= arg_count()) throw std::range_error("SCAN_Name::argument - i out of range"); return args[i]; } std::string SCAN_Name::arg(size_t i, const std::string& def_value) const { if(i >= arg_count()) return def_value; return args[i]; } size_t SCAN_Name::arg_as_integer(size_t i, size_t def_value) const { if(i >= arg_count()) return def_value; return to_u32bit(args[i]); } } /* * CBC-MAC * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> namespace Botan { /* * Update an CBC-MAC Calculation */ void CBC_MAC::add_data(const byte input[], size_t length) { size_t xored = std::min(output_length() - position, length); xor_buf(&state[position], input, xored); position += xored; if(position < output_length()) return; e->encrypt(state); input += xored; length -= xored; while(length >= output_length()) { xor_buf(state, input, output_length()); e->encrypt(state); input += output_length(); length -= output_length(); } xor_buf(state, input, length); position = length; } /* * Finalize an CBC-MAC Calculation */ void CBC_MAC::final_result(byte mac[]) { if(position) e->encrypt(state); copy_mem(mac, &state[0], state.size()); zeroise(state); position = 0; } /* * CBC-MAC Key Schedule */ void CBC_MAC::key_schedule(const byte key[], size_t length) { e->set_key(key, length); } /* * Clear memory of sensitive data */ void CBC_MAC::clear() { e->clear(); zeroise(state); position = 0; } /* * Return the name of this type */ std::string CBC_MAC::name() const { return "CBC-MAC(" + e->name() + ")"; } /* * Return a clone of this object */ MessageAuthenticationCode* CBC_MAC::clone() const { return new CBC_MAC(e->clone()); } /* * CBC-MAC Constructor */ CBC_MAC::CBC_MAC(BlockCipher* e_in) : e(e_in), state(e->block_size()) { position = 0; } /* * CBC-MAC Destructor */ CBC_MAC::~CBC_MAC() { delete e; } } /* * CMAC * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Perform CMAC's multiplication in GF(2^n) */ SecureVector<byte> CMAC::poly_double(const MemoryRegion<byte>& in, byte polynomial) { const byte poly_xor = (in[0] & 0x80) ? polynomial : 0; SecureVector<byte> out = in; byte carry = 0; for(size_t i = out.size(); i != 0; --i) { byte temp = out[i-1]; out[i-1] = (temp << 1) | carry; carry = (temp >> 7); } out[out.size()-1] ^= poly_xor; return out; } /* * Update an CMAC Calculation */ void CMAC::add_data(const byte input[], size_t length) { buffer.copy(position, input, length); if(position + length > output_length()) { xor_buf(state, buffer, output_length()); e->encrypt(state); input += (output_length() - position); length -= (output_length() - position); while(length > output_length()) { xor_buf(state, input, output_length()); e->encrypt(state); input += output_length(); length -= output_length(); } buffer.copy(input, length); position = 0; } position += length; } /* * Finalize an CMAC Calculation */ void CMAC::final_result(byte mac[]) { xor_buf(state, buffer, position); if(position == output_length()) { xor_buf(state, B, output_length()); } else { state[position] ^= 0x80; xor_buf(state, P, output_length()); } e->encrypt(state); for(size_t i = 0; i != output_length(); ++i) mac[i] = state[i]; zeroise(state); zeroise(buffer); position = 0; } /* * CMAC Key Schedule */ void CMAC::key_schedule(const byte key[], size_t length) { clear(); e->set_key(key, length); e->encrypt(B); B = poly_double(B, polynomial); P = poly_double(B, polynomial); } /* * Clear memory of sensitive data */ void CMAC::clear() { e->clear(); zeroise(state); zeroise(buffer); zeroise(B); zeroise(P); position = 0; } /* * Return the name of this type */ std::string CMAC::name() const { return "CMAC(" + e->name() + ")"; } /* * Return a clone of this object */ MessageAuthenticationCode* CMAC::clone() const { return new CMAC(e->clone()); } /* * CMAC Constructor */ CMAC::CMAC(BlockCipher* e_in) : e(e_in) { if(e->block_size() == 16) polynomial = 0x87; else if(e->block_size() == 8) polynomial = 0x1B; else throw Invalid_Argument("CMAC cannot use the cipher " + e->name()); state.resize(output_length()); buffer.resize(output_length()); B.resize(output_length()); P.resize(output_length()); position = 0; } /* * CMAC Destructor */ CMAC::~CMAC() { delete e; } } /* * HMAC * (C) 1999-2007 Jack Lloyd * 2007 Yves Jerschow * * Distributed under the terms of the Botan license */ namespace Botan { /* * Update a HMAC Calculation */ void HMAC::add_data(const byte input[], size_t length) { hash->update(input, length); } /* * Finalize a HMAC Calculation */ void HMAC::final_result(byte mac[]) { hash->final(mac); hash->update(o_key); hash->update(mac, output_length()); hash->final(mac); hash->update(i_key); } /* * HMAC Key Schedule */ void HMAC::key_schedule(const byte key[], size_t length) { hash->clear(); std::fill(i_key.begin(), i_key.end(), 0x36); std::fill(o_key.begin(), o_key.end(), 0x5C); if(length > hash->hash_block_size()) { SecureVector<byte> hmac_key = hash->process(key, length); xor_buf(i_key, hmac_key, hmac_key.size()); xor_buf(o_key, hmac_key, hmac_key.size()); } else { xor_buf(i_key, key, length); xor_buf(o_key, key, length); } hash->update(i_key); } /* * Clear memory of sensitive data */ void HMAC::clear() { hash->clear(); zeroise(i_key); zeroise(o_key); } /* * Return the name of this type */ std::string HMAC::name() const { return "HMAC(" + hash->name() + ")"; } /* * Return a clone of this object */ MessageAuthenticationCode* HMAC::clone() const { return new HMAC(hash->clone()); } /* * HMAC Constructor */ HMAC::HMAC(HashFunction* hash_in) : hash(hash_in) { if(hash->hash_block_size() == 0) throw Invalid_Argument("HMAC cannot be used with " + hash->name()); i_key.resize(hash->hash_block_size()); o_key.resize(hash->hash_block_size()); } } /* * Message Authentication Code base class * (C) 1999-2008 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Default (deterministic) MAC verification operation */ bool MessageAuthenticationCode::verify_mac(const byte mac[], size_t length) { SecureVector<byte> our_mac = final(); if(our_mac.size() != length) return false; return same_mem(&our_mac[0], &mac[0], length); } } /* * SSL3-MAC * (C) 1999-2004 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Update a SSL3-MAC Calculation */ void SSL3_MAC::add_data(const byte input[], size_t length) { hash->update(input, length); } /* * Finalize a SSL3-MAC Calculation */ void SSL3_MAC::final_result(byte mac[]) { hash->final(mac); hash->update(o_key); hash->update(mac, output_length()); hash->final(mac); hash->update(i_key); } /* * SSL3-MAC Key Schedule */ void SSL3_MAC::key_schedule(const byte key[], size_t length) { hash->clear(); std::fill(i_key.begin(), i_key.end(), 0x36); std::fill(o_key.begin(), o_key.end(), 0x5C); i_key.copy(key, length); o_key.copy(key, length); hash->update(i_key); } /* * Clear memory of sensitive data */ void SSL3_MAC::clear() { hash->clear(); zeroise(i_key); zeroise(o_key); } /* * Return the name of this type */ std::string SSL3_MAC::name() const { return "SSL3-MAC(" + hash->name() + ")"; } /* * Return a clone of this object */ MessageAuthenticationCode* SSL3_MAC::clone() const { return new SSL3_MAC(hash->clone()); } /* * SSL3-MAC Constructor */ SSL3_MAC::SSL3_MAC(HashFunction* hash_in) : hash(hash_in) { if(hash->hash_block_size() == 0) throw Invalid_Argument("SSL3-MAC cannot be used with " + hash->name()); // Quirk to deal with specification bug const size_t INNER_HASH_LENGTH = (hash->name() == "SHA-160") ? 60 : hash->hash_block_size(); i_key.resize(INNER_HASH_LENGTH); o_key.resize(INNER_HASH_LENGTH); } } /* * ANSI X9.19 MAC * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> namespace Botan { /* * Update an ANSI X9.19 MAC Calculation */ void ANSI_X919_MAC::add_data(const byte input[], size_t length) { size_t xored = std::min(8 - position, length); xor_buf(&state[position], input, xored); position += xored; if(position < 8) return; e->encrypt(state); input += xored; length -= xored; while(length >= 8) { xor_buf(state, input, 8); e->encrypt(state); input += 8; length -= 8; } xor_buf(state, input, length); position = length; } /* * Finalize an ANSI X9.19 MAC Calculation */ void ANSI_X919_MAC::final_result(byte mac[]) { if(position) e->encrypt(state); d->decrypt(state, mac); e->encrypt(mac); zeroise(state); position = 0; } /* * ANSI X9.19 MAC Key Schedule */ void ANSI_X919_MAC::key_schedule(const byte key[], size_t length) { e->set_key(key, 8); if(length == 8) d->set_key(key, 8); else d->set_key(key + 8, 8); } /* * Clear memory of sensitive data */ void ANSI_X919_MAC::clear() { e->clear(); d->clear(); zeroise(state); position = 0; } std::string ANSI_X919_MAC::name() const { return "X9.19-MAC"; } MessageAuthenticationCode* ANSI_X919_MAC::clone() const { return new ANSI_X919_MAC(e->clone()); } /* * ANSI X9.19 MAC Constructor */ ANSI_X919_MAC::ANSI_X919_MAC(BlockCipher* e_in) : e(e_in), d(e->clone()), state(e->block_size()), position(0) { if(e->name() != "DES") throw Invalid_Argument("ANSI X9.19 MAC only supports DES"); } /* * ANSI X9.19 MAC Destructor le*/ ANSI_X919_MAC::~ANSI_X919_MAC() { delete e; delete d; } } /* * BigInt Encoding/Decoding * (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Encode a BigInt */ void BigInt::encode(byte output[], const BigInt& n, Base base) { if(base == Binary) n.binary_encode(output); else if(base == Hexadecimal) { SecureVector<byte> binary(n.encoded_size(Binary)); n.binary_encode(&binary[0]); hex_encode(reinterpret_cast<char*>(output), &binary[0], binary.size()); } else if(base == Octal) { BigInt copy = n; const size_t output_size = n.encoded_size(Octal); for(size_t j = 0; j != output_size; ++j) { output[output_size - 1 - j] = Charset::digit2char(static_cast<byte>(copy % 8)); copy /= 8; } } else if(base == Decimal) { BigInt copy = n; BigInt remainder; copy.set_sign(Positive); const size_t output_size = n.encoded_size(Decimal); for(size_t j = 0; j != output_size; ++j) { divide(copy, 10, copy, remainder); output[output_size - 1 - j] = Charset::digit2char(static_cast<byte>(remainder.word_at(0))); if(copy.is_zero()) break; } } else throw Invalid_Argument("Unknown BigInt encoding method"); } /* * Encode a BigInt */ SecureVector<byte> BigInt::encode(const BigInt& n, Base base) { SecureVector<byte> output(n.encoded_size(base)); encode(&output[0], n, base); if(base != Binary) for(size_t j = 0; j != output.size(); ++j) if(output[j] == 0) output[j] = '0'; return output; } /* * Encode a BigInt, with leading 0s if needed */ SecureVector<byte> BigInt::encode_1363(const BigInt& n, size_t bytes) { const size_t n_bytes = n.bytes(); if(n_bytes > bytes) throw Encoding_Error("encode_1363: n is too large to encode properly"); const size_t leading_0s = bytes - n_bytes; SecureVector<byte> output(bytes); encode(&output[leading_0s], n, Binary); return output; } /* * Decode a BigInt */ BigInt BigInt::decode(const MemoryRegion<byte>& buf, Base base) { return BigInt::decode(&buf[0], buf.size(), base); } /* * Decode a BigInt */ BigInt BigInt::decode(const byte buf[], size_t length, Base base) { BigInt r; if(base == Binary) r.binary_decode(buf, length); else if(base == Hexadecimal) { SecureVector<byte> binary; if(length % 2) { // Handle lack of leading 0 const char buf0_with_leading_0[2] = { '0', buf[0] }; binary = hex_decode(buf0_with_leading_0, 2); binary += hex_decode(reinterpret_cast<const char*>(&buf[1]), length - 1, false); } else binary = hex_decode(reinterpret_cast<const char*>(buf), length, false); r.binary_decode(&binary[0], binary.size()); } else if(base == Decimal || base == Octal) { const size_t RADIX = ((base == Decimal) ? 10 : 8); for(size_t j = 0; j != length; ++j) { if(Charset::is_space(buf[j])) continue; if(!Charset::is_digit(buf[j])) throw Invalid_Argument("BigInt::decode: " "Invalid character in decimal input"); byte x = Charset::char2digit(buf[j]); if(x >= RADIX) { if(RADIX == 10) throw Invalid_Argument("BigInt: Invalid decimal string"); else throw Invalid_Argument("BigInt: Invalid octal string"); } r *= RADIX; r += x; } } else throw Invalid_Argument("Unknown BigInt decoding method"); return r; } } /* * BigInt Input/Output * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <iostream> namespace Botan { /* * Write the BigInt into a stream */ std::ostream& operator<<(std::ostream& stream, const BigInt& n) { BigInt::Base base = BigInt::Decimal; if(stream.flags() & std::ios::hex) base = BigInt::Hexadecimal; else if(stream.flags() & std::ios::oct) base = BigInt::Octal; if(n == 0) stream.write("0", 1); else { if(n < 0) stream.write("-", 1); SecureVector<byte> buffer = BigInt::encode(n, base); size_t skip = 0; while(buffer[skip] == '0' && skip < buffer.size()) ++skip; stream.write(reinterpret_cast<const char*>(&buffer[0]) + skip, buffer.size() - skip); } if(!stream.good()) throw Stream_IO_Error("BigInt output operator has failed"); return stream; } /* * Read the BigInt from a stream */ std::istream& operator>>(std::istream& stream, BigInt& n) { std::string str; std::getline(stream, str); if(stream.bad() || (stream.fail() && !stream.eof())) throw Stream_IO_Error("BigInt input operator has failed"); n = BigInt(str); return stream; } } /* * BigInt Assignment Operators * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> namespace Botan { /* * Addition Operator */ BigInt& BigInt::operator+=(const BigInt& y) { const size_t x_sw = sig_words(), y_sw = y.sig_words(); const size_t reg_size = std::max(x_sw, y_sw) + 1; grow_to(reg_size); if(sign() == y.sign()) bigint_add2(get_reg(), reg_size - 1, y.data(), y_sw); else { s32bit relative_size = bigint_cmp(data(), x_sw, y.data(), y_sw); if(relative_size < 0) { SecureVector<word> z(reg_size - 1); bigint_sub3(z, y.data(), reg_size - 1, data(), x_sw); copy_mem(&reg[0], &z[0], z.size()); set_sign(y.sign()); } else if(relative_size == 0) { zeroise(reg); set_sign(Positive); } else if(relative_size > 0) bigint_sub2(get_reg(), x_sw, y.data(), y_sw); } return (*this); } /* * Subtraction Operator */ BigInt& BigInt::operator-=(const BigInt& y) { const size_t x_sw = sig_words(), y_sw = y.sig_words(); s32bit relative_size = bigint_cmp(data(), x_sw, y.data(), y_sw); const size_t reg_size = std::max(x_sw, y_sw) + 1; grow_to(reg_size); if(relative_size < 0) { if(sign() == y.sign()) bigint_sub2_rev(get_reg(), y.data(), y_sw); else bigint_add2(get_reg(), reg_size - 1, y.data(), y_sw); set_sign(y.reverse_sign()); } else if(relative_size == 0) { if(sign() == y.sign()) { clear(); set_sign(Positive); } else bigint_shl1(get_reg(), x_sw, 0, 1); } else if(relative_size > 0) { if(sign() == y.sign()) bigint_sub2(get_reg(), x_sw, y.data(), y_sw); else bigint_add2(get_reg(), reg_size - 1, y.data(), y_sw); } return (*this); } /* * Multiplication Operator */ BigInt& BigInt::operator*=(const BigInt& y) { const size_t x_sw = sig_words(), y_sw = y.sig_words(); set_sign((sign() == y.sign()) ? Positive : Negative); if(x_sw == 0 || y_sw == 0) { clear(); set_sign(Positive); } else if(x_sw == 1 && y_sw) { grow_to(y_sw + 2); bigint_linmul3(get_reg(), y.data(), y_sw, word_at(0)); } else if(y_sw == 1 && x_sw) { grow_to(x_sw + 2); bigint_linmul2(get_reg(), x_sw, y.word_at(0)); } else { grow_to(size() + y.size()); SecureVector<word> z(data(), x_sw); SecureVector<word> workspace(size()); bigint_mul(get_reg(), size(), workspace, z, z.size(), x_sw, y.data(), y.size(), y_sw); } return (*this); } /* * Division Operator */ BigInt& BigInt::operator/=(const BigInt& y) { if(y.sig_words() == 1 && power_of_2(y.word_at(0))) (*this) >>= (y.bits() - 1); else (*this) = (*this) / y; return (*this); } /* * Modulo Operator */ BigInt& BigInt::operator%=(const BigInt& mod) { return (*this = (*this) % mod); } /* * Modulo Operator */ word BigInt::operator%=(word mod) { if(mod == 0) throw BigInt::DivideByZero(); if(power_of_2(mod)) { word result = (word_at(0) & (mod - 1)); clear(); grow_to(2); get_reg()[0] = result; return result; } word remainder = 0; for(size_t j = sig_words(); j > 0; --j) remainder = bigint_modop(remainder, word_at(j-1), mod); clear(); grow_to(2); if(remainder && sign() == BigInt::Negative) get_reg()[0] = mod - remainder; else get_reg()[0] = remainder; set_sign(BigInt::Positive); return word_at(0); } /* * Left Shift Operator */ BigInt& BigInt::operator<<=(size_t shift) { if(shift) { const size_t shift_words = shift / MP_WORD_BITS, shift_bits = shift % MP_WORD_BITS, words = sig_words(); grow_to(words + shift_words + (shift_bits ? 1 : 0)); bigint_shl1(get_reg(), words, shift_words, shift_bits); } return (*this); } /* * Right Shift Operator */ BigInt& BigInt::operator>>=(size_t shift) { if(shift) { const size_t shift_words = shift / MP_WORD_BITS, shift_bits = shift % MP_WORD_BITS; bigint_shr1(get_reg(), sig_words(), shift_words, shift_bits); if(is_zero()) set_sign(Positive); } return (*this); } } /* * BigInt Binary Operators * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> namespace Botan { /* * Addition Operator */ BigInt operator+(const BigInt& x, const BigInt& y) { const size_t x_sw = x.sig_words(), y_sw = y.sig_words(); BigInt z(x.sign(), std::max(x_sw, y_sw) + 1); if((x.sign() == y.sign())) bigint_add3(z.get_reg(), x.data(), x_sw, y.data(), y_sw); else { s32bit relative_size = bigint_cmp(x.data(), x_sw, y.data(), y_sw); if(relative_size < 0) { bigint_sub3(z.get_reg(), y.data(), y_sw, x.data(), x_sw); z.set_sign(y.sign()); } else if(relative_size == 0) z.set_sign(BigInt::Positive); else if(relative_size > 0) bigint_sub3(z.get_reg(), x.data(), x_sw, y.data(), y_sw); } return z; } /* * Subtraction Operator */ BigInt operator-(const BigInt& x, const BigInt& y) { const size_t x_sw = x.sig_words(), y_sw = y.sig_words(); s32bit relative_size = bigint_cmp(x.data(), x_sw, y.data(), y_sw); BigInt z(BigInt::Positive, std::max(x_sw, y_sw) + 1); if(relative_size < 0) { if(x.sign() == y.sign()) bigint_sub3(z.get_reg(), y.data(), y_sw, x.data(), x_sw); else bigint_add3(z.get_reg(), x.data(), x_sw, y.data(), y_sw); z.set_sign(y.reverse_sign()); } else if(relative_size == 0) { if(x.sign() != y.sign()) bigint_shl2(z.get_reg(), x.data(), x_sw, 0, 1); } else if(relative_size > 0) { if(x.sign() == y.sign()) bigint_sub3(z.get_reg(), x.data(), x_sw, y.data(), y_sw); else bigint_add3(z.get_reg(), x.data(), x_sw, y.data(), y_sw); z.set_sign(x.sign()); } return z; } /* * Multiplication Operator */ BigInt operator*(const BigInt& x, const BigInt& y) { const size_t x_sw = x.sig_words(), y_sw = y.sig_words(); BigInt z(BigInt::Positive, x.size() + y.size()); if(x_sw == 1 && y_sw) bigint_linmul3(z.get_reg(), y.data(), y_sw, x.word_at(0)); else if(y_sw == 1 && x_sw) bigint_linmul3(z.get_reg(), x.data(), x_sw, y.word_at(0)); else if(x_sw && y_sw) { SecureVector<word> workspace(z.size()); bigint_mul(z.get_reg(), z.size(), workspace, x.data(), x.size(), x_sw, y.data(), y.size(), y_sw); } if(x_sw && y_sw && x.sign() != y.sign()) z.flip_sign(); return z; } /* * Division Operator */ BigInt operator/(const BigInt& x, const BigInt& y) { BigInt q, r; divide(x, y, q, r); return q; } /* * Modulo Operator */ BigInt operator%(const BigInt& n, const BigInt& mod) { if(mod.is_zero()) throw BigInt::DivideByZero(); if(mod.is_negative()) throw Invalid_Argument("BigInt::operator%: modulus must be > 0"); if(n.is_positive() && mod.is_positive() && n < mod) return n; BigInt q, r; divide(n, mod, q, r); return r; } /* * Modulo Operator */ word operator%(const BigInt& n, word mod) { if(mod == 0) throw BigInt::DivideByZero(); if(power_of_2(mod)) return (n.word_at(0) & (mod - 1)); word remainder = 0; for(size_t j = n.sig_words(); j > 0; --j) remainder = bigint_modop(remainder, n.word_at(j-1), mod); if(remainder && n.sign() == BigInt::Negative) return mod - remainder; return remainder; } /* * Left Shift Operator */ BigInt operator<<(const BigInt& x, size_t shift) { if(shift == 0) return x; const size_t shift_words = shift / MP_WORD_BITS, shift_bits = shift % MP_WORD_BITS; const size_t x_sw = x.sig_words(); BigInt y(x.sign(), x_sw + shift_words + (shift_bits ? 1 : 0)); bigint_shl2(y.get_reg(), x.data(), x_sw, shift_words, shift_bits); return y; } /* * Right Shift Operator */ BigInt operator>>(const BigInt& x, size_t shift) { if(shift == 0) return x; if(x.bits() <= shift) return 0; const size_t shift_words = shift / MP_WORD_BITS, shift_bits = shift % MP_WORD_BITS, x_sw = x.sig_words(); BigInt y(x.sign(), x_sw - shift_words); bigint_shr2(y.get_reg(), x.data(), x_sw, shift_words, shift_bits); return y; } } /* * BigInt Random Generation * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Construct a BigInt of a specific form */ BigInt::BigInt(NumberType type, size_t bits) { set_sign(Positive); if(type == Power2) set_bit(bits); else throw Invalid_Argument("BigInt(NumberType): Unknown type"); } /* * Randomize this number */ void BigInt::randomize(RandomNumberGenerator& rng, size_t bitsize) { set_sign(Positive); if(bitsize == 0) clear(); else { SecureVector<byte> array = rng.random_vec((bitsize + 7) / 8); if(bitsize % 8) array[0] &= 0xFF >> (8 - (bitsize % 8)); array[0] |= 0x80 >> ((bitsize % 8) ? (8 - bitsize % 8) : 0); binary_decode(&array[0], array.size()); } } /* * Generate a random integer within given range */ BigInt BigInt::random_integer(RandomNumberGenerator& rng, const BigInt& min, const BigInt& max) { BigInt range = max - min; if(range <= 0) throw Invalid_Argument("random_integer: invalid min/max values"); return (min + (BigInt(rng, range.bits() + 2) % range)); } } /* * BigInt Base * (C) 1999-2011 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Construct a BigInt from a regular number */ BigInt::BigInt(u64bit n) { set_sign(Positive); if(n == 0) return; const size_t limbs_needed = sizeof(u64bit) / sizeof(word); reg.resize(4*limbs_needed); for(size_t i = 0; i != limbs_needed; ++i) reg[i] = ((n >> (i*MP_WORD_BITS)) & MP_WORD_MASK); } /* * Construct a BigInt of the specified size */ BigInt::BigInt(Sign s, size_t size) { reg.resize(round_up<size_t>(size, 8)); signedness = s; } /* * Construct a BigInt from a "raw" BigInt */ BigInt::BigInt(const BigInt& b) { const size_t b_words = b.sig_words(); if(b_words) { reg.resize(round_up<size_t>(b_words, 8)); reg.copy(b.data(), b_words); set_sign(b.sign()); } else { reg.resize(2); set_sign(Positive); } } /* * Construct a BigInt from a string */ BigInt::BigInt(const std::string& str) { Base base = Decimal; size_t markers = 0; bool negative = false; if(str.length() > 0 && str[0] == '-') { markers += 1; negative = true; } if(str.length() > markers + 2 && str[markers ] == '0' && str[markers + 1] == 'x') { markers += 2; base = Hexadecimal; } else if(str.length() > markers + 1 && str[markers] == '0') { markers += 1; base = Octal; } *this = decode(reinterpret_cast<const byte*>(str.data()) + markers, str.length() - markers, base); if(negative) set_sign(Negative); else set_sign(Positive); } /* * Construct a BigInt from an encoded BigInt */ BigInt::BigInt(const byte input[], size_t length, Base base) { set_sign(Positive); *this = decode(input, length, base); } /* * Construct a BigInt from an encoded BigInt */ BigInt::BigInt(RandomNumberGenerator& rng, size_t bits) { set_sign(Positive); randomize(rng, bits); } /* * Swap this BigInt with another */ void BigInt::swap(BigInt& other) { reg.swap(other.reg); std::swap(signedness, other.signedness); } /* * Grow the internal storage */ void BigInt::grow_reg(size_t n) { reg.resize(round_up<size_t>(size() + n, 8)); } /* * Grow the internal storage */ void BigInt::grow_to(size_t n) { if(n > size()) reg.resize(round_up<size_t>(n, 8)); } /* * Comparison Function */ s32bit BigInt::cmp(const BigInt& n, bool check_signs) const { if(check_signs) { if(n.is_positive() && this->is_negative()) return -1; if(n.is_negative() && this->is_positive()) return 1; if(n.is_negative() && this->is_negative()) return (-bigint_cmp(data(), sig_words(), n.data(), n.sig_words())); } return bigint_cmp(data(), sig_words(), n.data(), n.sig_words()); } /* * Return byte n of this number */ byte BigInt::byte_at(size_t n) const { const size_t WORD_BYTES = sizeof(word); size_t word_num = n / WORD_BYTES, byte_num = n % WORD_BYTES; if(word_num >= size()) return 0; else return get_byte(WORD_BYTES - byte_num - 1, reg[word_num]); } /* * Return bit n of this number */ bool BigInt::get_bit(size_t n) const { return ((word_at(n / MP_WORD_BITS) >> (n % MP_WORD_BITS)) & 1); } /* * Return bits {offset...offset+length} */ u32bit BigInt::get_substring(size_t offset, size_t length) const { if(length > 32) throw Invalid_Argument("BigInt::get_substring: Substring size too big"); u64bit piece = 0; for(size_t i = 0; i != 8; ++i) { const byte part = byte_at((offset / 8) + (7-i)); piece = (piece << 8) | part; } const u64bit mask = (static_cast<u64bit>(1) << length) - 1; const size_t shift = (offset % 8); return static_cast<u32bit>((piece >> shift) & mask); } /* * Convert this number to a u32bit, if possible */ u32bit BigInt::to_u32bit() const { if(is_negative()) throw Encoding_Error("BigInt::to_u32bit: Number is negative"); if(bits() >= 32) throw Encoding_Error("BigInt::to_u32bit: Number is too big to convert"); u32bit out = 0; for(u32bit j = 0; j != 4; ++j) out = (out << 8) | byte_at(3-j); return out; } /* * Set bit number n */ void BigInt::set_bit(size_t n) { const size_t which = n / MP_WORD_BITS; const word mask = static_cast<word>(1) << (n % MP_WORD_BITS); if(which >= size()) grow_to(which + 1); reg[which] |= mask; } /* * Clear bit number n */ void BigInt::clear_bit(size_t n) { const size_t which = n / MP_WORD_BITS; const word mask = static_cast<word>(1) << (n % MP_WORD_BITS); if(which < size()) reg[which] &= ~mask; } /* * Clear all but the lowest n bits */ void BigInt::mask_bits(size_t n) { if(n == 0) { clear(); return; } if(n >= bits()) return; const size_t top_word = n / MP_WORD_BITS; const word mask = (static_cast<word>(1) << (n % MP_WORD_BITS)) - 1; if(top_word < size()) for(size_t i = top_word + 1; i != size(); ++i) reg[i] = 0; reg[top_word] &= mask; } /* * Count how many bytes are being used */ size_t BigInt::bytes() const { return (bits() + 7) / 8; } /* * Count how many bits are being used */ size_t BigInt::bits() const { const size_t words = sig_words(); if(words == 0) return 0; size_t full_words = words - 1, top_bits = MP_WORD_BITS; word top_word = word_at(full_words), mask = MP_WORD_TOP_BIT; while(top_bits && ((top_word & mask) == 0)) { mask >>= 1; top_bits--; } return (full_words * MP_WORD_BITS + top_bits); } /* * Calcluate the size in a certain base */ size_t BigInt::encoded_size(Base base) const { static const double LOG_2_BASE_10 = 0.30102999566; if(base == Binary) return bytes(); else if(base == Hexadecimal) return 2*bytes(); else if(base == Octal) return ((bits() + 2) / 3); else if(base == Decimal) return static_cast<size_t>((bits() * LOG_2_BASE_10) + 1); else throw Invalid_Argument("Unknown base for BigInt encoding"); } /* * Set the sign */ void BigInt::set_sign(Sign s) { if(is_zero()) signedness = Positive; else signedness = s; } /* * Reverse the value of the sign flag */ void BigInt::flip_sign() { set_sign(reverse_sign()); } /* * Return the opposite value of the current sign */ BigInt::Sign BigInt::reverse_sign() const { if(sign() == Positive) return Negative; return Positive; } /* * Return the negation of this number */ BigInt BigInt::operator-() const { BigInt x = (*this); x.flip_sign(); return x; } /* * Return the absolute value of this number */ BigInt BigInt::abs() const { BigInt x = (*this); x.set_sign(Positive); return x; } /* * Encode this number into bytes */ void BigInt::binary_encode(byte output[]) const { const size_t sig_bytes = bytes(); for(size_t i = 0; i != sig_bytes; ++i) output[sig_bytes-i-1] = byte_at(i); } /* * Set this number to the value in buf */ void BigInt::binary_decode(const byte buf[], size_t length) { const size_t WORD_BYTES = sizeof(word); clear(); reg.resize(round_up<size_t>((length / WORD_BYTES) + 1, 8)); for(size_t i = 0; i != length / WORD_BYTES; ++i) { const size_t top = length - WORD_BYTES*i; for(size_t j = WORD_BYTES; j > 0; --j) reg[i] = (reg[i] << 8) | buf[top - j]; } for(size_t i = 0; i != length % WORD_BYTES; ++i) reg[length / WORD_BYTES] = (reg[length / WORD_BYTES] << 8) | buf[i]; } /* * Set this number to the value in buf */ void BigInt::binary_decode(const MemoryRegion<byte>& buf) { binary_decode(buf, buf.size()); } } /* * Division Algorithm * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { /* * Handle signed operands, if necessary */ void sign_fixup(const BigInt& x, const BigInt& y, BigInt& q, BigInt& r) { if(x.sign() == BigInt::Negative) { q.flip_sign(); if(r.is_nonzero()) { --q; r = y.abs() - r; } } if(y.sign() == BigInt::Negative) q.flip_sign(); } } /* * Solve x = q * y + r */ void divide(const BigInt& x, const BigInt& y_arg, BigInt& q, BigInt& r) { if(y_arg.is_zero()) throw BigInt::DivideByZero(); BigInt y = y_arg; const size_t y_words = y.sig_words(); r = x; q = 0; r.set_sign(BigInt::Positive); y.set_sign(BigInt::Positive); s32bit compare = r.cmp(y); if(compare == 0) { q = 1; r = 0; } else if(compare > 0) { size_t shifts = 0; word y_top = y[y.sig_words()-1]; while(y_top < MP_WORD_TOP_BIT) { y_top <<= 1; ++shifts; } y <<= shifts; r <<= shifts; const size_t n = r.sig_words() - 1, t = y_words - 1; if(n < t) throw Internal_Error("BigInt division word sizes"); q.get_reg().resize(n - t + 1); if(n <= t) { while(r > y) { r -= y; ++q; } r >>= shifts; sign_fixup(x, y_arg, q, r); return; } BigInt temp = y << (MP_WORD_BITS * (n-t)); while(r >= temp) { r -= temp; ++q[n-t]; } for(size_t j = n; j != t; --j) { const word x_j0 = r.word_at(j); const word x_j1 = r.word_at(j-1); const word y_t = y.word_at(t); if(x_j0 == y_t) q[j-t-1] = MP_WORD_MAX; else q[j-t-1] = bigint_divop(x_j0, x_j1, y_t); while(bigint_divcore(q[j-t-1], y_t, y.word_at(t-1), x_j0, x_j1, r.word_at(j-2))) --q[j-t-1]; r -= (q[j-t-1] * y) << (MP_WORD_BITS * (j-t-1)); if(r.is_negative()) { r += y << (MP_WORD_BITS * (j-t-1)); --q[j-t-1]; } } r >>= shifts; } sign_fixup(x, y_arg, q, r); } } /* * Point arithmetic on elliptic curves over GF(p) * * (C) 2007 Martin Doering, Christoph Ludwig, Falko Strenzke * 2008-2011 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { PointGFp::PointGFp(const CurveGFp& curve) : curve(curve), ws(2 * (curve.get_p_words() + 2)) { coord_x = 0; coord_y = monty_mult(1, curve.get_r2()); coord_z = 0; } PointGFp::PointGFp(const CurveGFp& curve, const BigInt& x, const BigInt& y) : curve(curve), ws(2 * (curve.get_p_words() + 2)) { coord_x = monty_mult(x, curve.get_r2()); coord_y = monty_mult(y, curve.get_r2()); coord_z = monty_mult(1, curve.get_r2()); } // Montgomery multiplication void PointGFp::monty_mult(BigInt& z, const BigInt& x, const BigInt& y) const { //assert(&z != &x && &z != &y); if(x.is_zero() || y.is_zero()) { z = 0; return; } const BigInt& p = curve.get_p(); const size_t p_size = curve.get_p_words(); const word p_dash = curve.get_p_dash(); SecureVector<word>& z_reg = z.get_reg(); z_reg.resize(2*p_size+1); zeroise(z_reg); bigint_monty_mul(&z_reg[0], z_reg.size(), x.data(), x.size(), x.sig_words(), y.data(), y.size(), y.sig_words(), p.data(), p_size, p_dash, &ws[0]); } // Montgomery squaring void PointGFp::monty_sqr(BigInt& z, const BigInt& x) const { //assert(&z != &x); if(x.is_zero()) { z = 0; return; } const BigInt& p = curve.get_p(); const size_t p_size = curve.get_p_words(); const word p_dash = curve.get_p_dash(); SecureVector<word>& z_reg = z.get_reg(); z_reg.resize(2*p_size+1); zeroise(z_reg); bigint_monty_sqr(&z_reg[0], z_reg.size(), x.data(), x.size(), x.sig_words(), p.data(), p_size, p_dash, &ws[0]); } // Point addition void PointGFp::add(const PointGFp& rhs, std::vector<BigInt>& ws_bn) { if(is_zero()) { coord_x = rhs.coord_x; coord_y = rhs.coord_y; coord_z = rhs.coord_z; return; } else if(rhs.is_zero()) return; const BigInt& p = curve.get_p(); BigInt& rhs_z2 = ws_bn[0]; BigInt& U1 = ws_bn[1]; BigInt& S1 = ws_bn[2]; BigInt& lhs_z2 = ws_bn[3]; BigInt& U2 = ws_bn[4]; BigInt& S2 = ws_bn[5]; BigInt& H = ws_bn[6]; BigInt& r = ws_bn[7]; monty_sqr(rhs_z2, rhs.coord_z); monty_mult(U1, coord_x, rhs_z2); monty_mult(S1, coord_y, monty_mult(rhs.coord_z, rhs_z2)); monty_sqr(lhs_z2, coord_z); monty_mult(U2, rhs.coord_x, lhs_z2); monty_mult(S2, rhs.coord_y, monty_mult(coord_z, lhs_z2)); H = U2; H -= U1; if(H.is_negative()) H += p; r = S2; r -= S1; if(r.is_negative()) r += p; if(H.is_zero()) { if(r.is_zero()) { mult2(ws_bn); return; } *this = PointGFp(curve); // setting myself to zero return; } monty_sqr(U2, H); monty_mult(S2, U2, H); U2 = monty_mult(U1, U2); monty_sqr(coord_x, r); coord_x -= S2; coord_x -= (U2 << 1); while(coord_x.is_negative()) coord_x += p; U2 -= coord_x; if(U2.is_negative()) U2 += p; monty_mult(coord_y, r, U2); coord_y -= monty_mult(S1, S2); if(coord_y.is_negative()) coord_y += p; monty_mult(coord_z, monty_mult(coord_z, rhs.coord_z), H); } // *this *= 2 void PointGFp::mult2(std::vector<BigInt>& ws_bn) { if(is_zero()) return; else if(coord_y.is_zero()) { *this = PointGFp(curve); // setting myself to zero return; } const BigInt& p = curve.get_p(); BigInt& y_2 = ws_bn[0]; BigInt& S = ws_bn[1]; BigInt& z4 = ws_bn[2]; BigInt& a_z4 = ws_bn[3]; BigInt& M = ws_bn[4]; BigInt& U = ws_bn[5]; BigInt& x = ws_bn[6]; BigInt& y = ws_bn[7]; BigInt& z = ws_bn[8]; monty_sqr(y_2, coord_y); monty_mult(S, coord_x, y_2); S <<= 2; // * 4 while(S >= p) S -= p; monty_sqr(z4, monty_sqr(coord_z)); monty_mult(a_z4, curve.get_a_r(), z4); M = 3 * monty_sqr(coord_x); M += a_z4; while(M >= p) M -= p; monty_sqr(x, M); x -= (S << 1); while(x.is_negative()) x += p; monty_sqr(U, y_2); U <<= 3; while(U >= p) U -= p; S -= x; while(S.is_negative()) S += p; monty_mult(y, M, S); y -= U; if(y.is_negative()) y += p; monty_mult(z, coord_y, coord_z); z <<= 1; if(z >= p) z -= p; coord_x = x; coord_y = y; coord_z = z; } // arithmetic operators PointGFp& PointGFp::operator+=(const PointGFp& rhs) { std::vector<BigInt> ws(9); add(rhs, ws); return *this; } PointGFp& PointGFp::operator-=(const PointGFp& rhs) { PointGFp minus_rhs = PointGFp(rhs).negate(); if(is_zero()) *this = minus_rhs; else *this += minus_rhs; return *this; } PointGFp& PointGFp::operator*=(const BigInt& scalar) { *this = scalar * *this; return *this; } PointGFp multi_exponentiate(const PointGFp& p1, const BigInt& z1, const PointGFp& p2, const BigInt& z2) { const PointGFp p3 = p1 + p2; PointGFp H(p1.curve); // create as zero size_t bits_left = std::max(z1.bits(), z2.bits()); std::vector<BigInt> ws(9); while(bits_left) { H.mult2(ws); const bool z1_b = z1.get_bit(bits_left - 1); const bool z2_b = z2.get_bit(bits_left - 1); if(z1_b == true && z2_b == true) H.add(p3, ws); else if(z1_b) H.add(p1, ws); else if(z2_b) H.add(p2, ws); --bits_left; } if(z1.is_negative() != z2.is_negative()) H.negate(); return H; } PointGFp operator*(const BigInt& scalar, const PointGFp& point) { const CurveGFp& curve = point.get_curve(); if(scalar.is_zero()) return PointGFp(curve); // zero point std::vector<BigInt> ws(9); if(scalar.abs() <= 2) // special cases for small values { byte value = scalar.abs().byte_at(0); PointGFp result = point; if(value == 2) result.mult2(ws); if(scalar.is_negative()) result.negate(); return result; } const size_t scalar_bits = scalar.bits(); #if 0 PointGFp x1 = PointGFp(curve); PointGFp x2 = point; size_t bits_left = scalar_bits; // Montgomery Ladder while(bits_left) { const bool bit_set = scalar.get_bit(bits_left - 1); if(bit_set) { x1.add(x2, ws); x2.mult2(ws); } else { x2.add(x1, ws); x1.mult2(ws); } --bits_left; } if(scalar.is_negative()) x1.negate(); return x1; #else const size_t window_size = 4; std::vector<PointGFp> Ps(1 << window_size); Ps[0] = PointGFp(curve); Ps[1] = point; for(size_t i = 2; i != Ps.size(); ++i) { Ps[i] = Ps[i-1]; Ps[i].add(point, ws); } PointGFp H(curve); // create as zero size_t bits_left = scalar_bits; while(bits_left >= window_size) { for(size_t i = 0; i != window_size; ++i) H.mult2(ws); const u32bit nibble = scalar.get_substring(bits_left - window_size, window_size); H.add(Ps[nibble], ws); bits_left -= window_size; } while(bits_left) { H.mult2(ws); if(scalar.get_bit(bits_left-1)) H.add(point, ws); --bits_left; } if(scalar.is_negative()) H.negate(); return H; #endif } BigInt PointGFp::get_affine_x() const { if(is_zero()) throw Illegal_Transformation("Cannot convert zero point to affine"); const BigInt& r2 = curve.get_r2(); BigInt z2 = monty_sqr(coord_z); z2 = inverse_mod(z2, curve.get_p()); z2 = monty_mult(z2, r2); return monty_mult(coord_x, z2); } BigInt PointGFp::get_affine_y() const { if(is_zero()) throw Illegal_Transformation("Cannot convert zero point to affine"); const BigInt& r2 = curve.get_r2(); BigInt z3 = monty_mult(coord_z, monty_sqr(coord_z)); z3 = inverse_mod(z3, curve.get_p()); z3 = monty_mult(z3, r2); return monty_mult(coord_y, z3); } bool PointGFp::on_the_curve() const { /* Is the point still on the curve?? (If everything is correct, the point is always on its curve; then the function will return true. If somehow the state is corrupted, which suggests a fault attack (or internal computational error), then return false. */ if(is_zero()) return true; BigInt y2 = monty_mult(monty_sqr(coord_y), 1); BigInt x3 = monty_mult(coord_x, monty_sqr(coord_x)); BigInt ax = monty_mult(coord_x, curve.get_a_r()); const BigInt& b_r = curve.get_b_r(); BigInt z2 = monty_sqr(coord_z); if(coord_z == z2) // Is z equal to 1 (in Montgomery form)? { if(y2 != monty_mult(x3 + ax + b_r, 1)) return false; } BigInt z3 = monty_mult(coord_z, z2); BigInt ax_z4 = monty_mult(ax, monty_sqr(z2)); BigInt b_z6 = monty_mult(b_r, monty_sqr(z3)); if(y2 != monty_mult(x3 + ax_z4 + b_z6, 1)) return false; return true; } // swaps the states of *this and other, does not throw! void PointGFp::swap(PointGFp& other) { curve.swap(other.curve); coord_x.swap(other.coord_x); coord_y.swap(other.coord_y); coord_z.swap(other.coord_z); ws.swap(other.ws); } bool PointGFp::operator==(const PointGFp& other) const { if(get_curve() != other.get_curve()) return false; // If this is zero, only equal if other is also zero if(is_zero()) return other.is_zero(); return (get_affine_x() == other.get_affine_x() && get_affine_y() == other.get_affine_y()); } // encoding and decoding SecureVector<byte> EC2OSP(const PointGFp& point, byte format) { if(point.is_zero()) return SecureVector<byte>(1); // single 0 byte const size_t p_bytes = point.get_curve().get_p().bytes(); BigInt x = point.get_affine_x(); BigInt y = point.get_affine_y(); SecureVector<byte> bX = BigInt::encode_1363(x, p_bytes); SecureVector<byte> bY = BigInt::encode_1363(y, p_bytes); if(format == PointGFp::UNCOMPRESSED) { SecureVector<byte> result; result.push_back(0x04); result += bX; result += bY; return result; } else if(format == PointGFp::COMPRESSED) { SecureVector<byte> result; result.push_back(0x02 | static_cast<byte>(y.get_bit(0))); result += bX; return result; } else if(format == PointGFp::HYBRID) { SecureVector<byte> result; result.push_back(0x06 | static_cast<byte>(y.get_bit(0))); result += bX; result += bY; return result; } else throw Invalid_Argument("illegal point encoding format specification"); } namespace { BigInt decompress_point(bool yMod2, const BigInt& x, const CurveGFp& curve) { BigInt xpow3 = x * x * x; BigInt g = curve.get_a() * x; g += xpow3; g += curve.get_b(); g = g % curve.get_p(); BigInt z = ressol(g, curve.get_p()); if(z < 0) throw Illegal_Point("error during decompression"); if(z.get_bit(0) != yMod2) z = curve.get_p() - z; return z; } } PointGFp OS2ECP(const byte data[], size_t data_len, const CurveGFp& curve) { if(data_len <= 1) return PointGFp(curve); // return zero const byte pc = data[0]; BigInt x, y; if(pc == 2 || pc == 3) { //compressed form x = BigInt::decode(&data[1], data_len - 1); const bool y_mod_2 = ((pc & 0x01) == 1); y = decompress_point(y_mod_2, x, curve); } else if(pc == 4) { const size_t l = (data_len - 1) / 2; // uncompressed form x = BigInt::decode(&data[1], l); y = BigInt::decode(&data[l+1], l); } else if(pc == 6 || pc == 7) { const size_t l = (data_len - 1) / 2; // hybrid form x = BigInt::decode(&data[1], l); y = BigInt::decode(&data[l+1], l); const bool y_mod_2 = ((pc & 0x01) == 1); if(decompress_point(y_mod_2, x, curve) != y) throw Illegal_Point("OS2ECP: Decoding error in hybrid format"); } else throw Invalid_Argument("OS2ECP: Unknown format type"); PointGFp result(curve, x, y); if(!result.on_the_curve()) throw Illegal_Point("OS2ECP: Decoded point was not on the curve"); return result; } } /* * Lowest Level MPI Algorithms * (C) 1999-2010 Jack Lloyd * 2006 Luca Piccarreta * * Distributed under the terms of the Botan license */ namespace Botan { extern "C" { /* * Two Operand Addition, No Carry */ word bigint_add2_nc(word x[], size_t x_size, const word y[], size_t y_size) { word carry = 0; const size_t blocks = y_size - (y_size % 8); for(size_t i = 0; i != blocks; i += 8) carry = word8_add2(x + i, y + i, carry); for(size_t i = blocks; i != y_size; ++i) x[i] = word_add(x[i], y[i], &carry); for(size_t i = y_size; i != x_size; ++i) x[i] = word_add(x[i], 0, &carry); return carry; } /* * Three Operand Addition, No Carry */ word bigint_add3_nc(word z[], const word x[], size_t x_size, const word y[], size_t y_size) { if(x_size < y_size) { return bigint_add3_nc(z, y, y_size, x, x_size); } word carry = 0; const size_t blocks = y_size - (y_size % 8); for(size_t i = 0; i != blocks; i += 8) carry = word8_add3(z + i, x + i, y + i, carry); for(size_t i = blocks; i != y_size; ++i) z[i] = word_add(x[i], y[i], &carry); for(size_t i = y_size; i != x_size; ++i) z[i] = word_add(x[i], 0, &carry); return carry; } /* * Two Operand Addition */ void bigint_add2(word x[], size_t x_size, const word y[], size_t y_size) { if(bigint_add2_nc(x, x_size, y, y_size)) x[x_size] += 1; } /* * Three Operand Addition */ void bigint_add3(word z[], const word x[], size_t x_size, const word y[], size_t y_size) { z[(x_size > y_size ? x_size : y_size)] += bigint_add3_nc(z, x, x_size, y, y_size); } /* * Two Operand Subtraction */ word bigint_sub2(word x[], size_t x_size, const word y[], size_t y_size) { word borrow = 0; const size_t blocks = y_size - (y_size % 8); for(size_t i = 0; i != blocks; i += 8) borrow = word8_sub2(x + i, y + i, borrow); for(size_t i = blocks; i != y_size; ++i) x[i] = word_sub(x[i], y[i], &borrow); for(size_t i = y_size; i != x_size; ++i) x[i] = word_sub(x[i], 0, &borrow); return borrow; } /* * Two Operand Subtraction x = y - x */ void bigint_sub2_rev(word x[], const word y[], size_t y_size) { word borrow = 0; const size_t blocks = y_size - (y_size % 8); for(size_t i = 0; i != blocks; i += 8) borrow = word8_sub2_rev(x + i, y + i, borrow); for(size_t i = blocks; i != y_size; ++i) x[i] = word_sub(y[i], x[i], &borrow); if(borrow) throw Internal_Error("bigint_sub2_rev: x >= y"); } /* * Three Operand Subtraction */ word bigint_sub3(word z[], const word x[], size_t x_size, const word y[], size_t y_size) { word borrow = 0; const size_t blocks = y_size - (y_size % 8); for(size_t i = 0; i != blocks; i += 8) borrow = word8_sub3(z + i, x + i, y + i, borrow); for(size_t i = blocks; i != y_size; ++i) z[i] = word_sub(x[i], y[i], &borrow); for(size_t i = y_size; i != x_size; ++i) z[i] = word_sub(x[i], 0, &borrow); return borrow; } /* * Two Operand Linear Multiply */ void bigint_linmul2(word x[], size_t x_size, word y) { const size_t blocks = x_size - (x_size % 8); word carry = 0; for(size_t i = 0; i != blocks; i += 8) carry = word8_linmul2(x + i, y, carry); for(size_t i = blocks; i != x_size; ++i) x[i] = word_madd2(x[i], y, &carry); x[x_size] = carry; } /* * Three Operand Linear Multiply */ void bigint_linmul3(word z[], const word x[], size_t x_size, word y) { const size_t blocks = x_size - (x_size % 8); word carry = 0; for(size_t i = 0; i != blocks; i += 8) carry = word8_linmul3(z + i, x + i, y, carry); for(size_t i = blocks; i != x_size; ++i) z[i] = word_madd2(x[i], y, &carry); z[x_size] = carry; } } } /* * Comba Multiplication and Squaring * (C) 1999-2007,2011 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { extern "C" { /* * Comba 4x4 Squaring */ void bigint_comba_sqr4(word z[8], const word x[4]) { word w2 = 0, w1 = 0, w0 = 0; word3_muladd(&w2, &w1, &w0, x[ 0], x[ 0]); z[ 0] = w0; w0 = 0; word3_muladd_2(&w0, &w2, &w1, x[ 0], x[ 1]); z[ 1] = w1; w1 = 0; word3_muladd_2(&w1, &w0, &w2, x[ 0], x[ 2]); word3_muladd(&w1, &w0, &w2, x[ 1], x[ 1]); z[ 2] = w2; w2 = 0; word3_muladd_2(&w2, &w1, &w0, x[ 0], x[ 3]); word3_muladd_2(&w2, &w1, &w0, x[ 1], x[ 2]); z[ 3] = w0; w0 = 0; word3_muladd_2(&w0, &w2, &w1, x[ 1], x[ 3]); word3_muladd(&w0, &w2, &w1, x[ 2], x[ 2]); z[ 4] = w1; w1 = 0; word3_muladd_2(&w1, &w0, &w2, x[ 2], x[ 3]); z[ 5] = w2; w2 = 0; word3_muladd(&w2, &w1, &w0, x[ 3], x[ 3]); z[ 6] = w0; z[ 7] = w1; } /* * Comba 4x4 Multiplication */ void bigint_comba_mul4(word z[8], const word x[4], const word y[4]) { word w2 = 0, w1 = 0, w0 = 0; word3_muladd(&w2, &w1, &w0, x[ 0], y[ 0]); z[ 0] = w0; w0 = 0; word3_muladd(&w0, &w2, &w1, x[ 0], y[ 1]); word3_muladd(&w0, &w2, &w1, x[ 1], y[ 0]); z[ 1] = w1; w1 = 0; word3_muladd(&w1, &w0, &w2, x[ 0], y[ 2]); word3_muladd(&w1, &w0, &w2, x[ 1], y[ 1]); word3_muladd(&w1, &w0, &w2, x[ 2], y[ 0]); z[ 2] = w2; w2 = 0; word3_muladd(&w2, &w1, &w0, x[ 0], y[ 3]); word3_muladd(&w2, &w1, &w0, x[ 1], y[ 2]); word3_muladd(&w2, &w1, &w0, x[ 2], y[ 1]); word3_muladd(&w2, &w1, &w0, x[ 3], y[ 0]); z[ 3] = w0; w0 = 0; word3_muladd(&w0, &w2, &w1, x[ 1], y[ 3]); word3_muladd(&w0, &w2, &w1, x[ 2], y[ 2]); word3_muladd(&w0, &w2, &w1, x[ 3], y[ 1]); z[ 4] = w1; w1 = 0; word3_muladd(&w1, &w0, &w2, x[ 2], y[ 3]); word3_muladd(&w1, &w0, &w2, x[ 3], y[ 2]); z[ 5] = w2; w2 = 0; word3_muladd(&w2, &w1, &w0, x[ 3], y[ 3]); z[ 6] = w0; z[ 7] = w1; } /* * Comba 6x6 Squaring */ void bigint_comba_sqr6(word z[12], const word x[6]) { word w2 = 0, w1 = 0, w0 = 0; word3_muladd(&w2, &w1, &w0, x[ 0], x[ 0]); z[ 0] = w0; w0 = 0; word3_muladd_2(&w0, &w2, &w1, x[ 0], x[ 1]); z[ 1] = w1; w1 = 0; word3_muladd_2(&w1, &w0, &w2, x[ 0], x[ 2]); word3_muladd(&w1, &w0, &w2, x[ 1], x[ 1]); z[ 2] = w2; w2 = 0; word3_muladd_2(&w2, &w1, &w0, x[ 0], x[ 3]); word3_muladd_2(&w2, &w1, &w0, x[ 1], x[ 2]); z[ 3] = w0; w0 = 0; word3_muladd_2(&w0, &w2, &w1, x[ 0], x[ 4]); word3_muladd_2(&w0, &w2, &w1, x[ 1], x[ 3]); word3_muladd(&w0, &w2, &w1, x[ 2], x[ 2]); z[ 4] = w1; w1 = 0; word3_muladd_2(&w1, &w0, &w2, x[ 0], x[ 5]); word3_muladd_2(&w1, &w0, &w2, x[ 1], x[ 4]); word3_muladd_2(&w1, &w0, &w2, x[ 2], x[ 3]); z[ 5] = w2; w2 = 0; word3_muladd_2(&w2, &w1, &w0, x[ 1], x[ 5]); word3_muladd_2(&w2, &w1, &w0, x[ 2], x[ 4]); word3_muladd(&w2, &w1, &w0, x[ 3], x[ 3]); z[ 6] = w0; w0 = 0; word3_muladd_2(&w0, &w2, &w1, x[ 2], x[ 5]); word3_muladd_2(&w0, &w2, &w1, x[ 3], x[ 4]); z[ 7] = w1; w1 = 0; word3_muladd_2(&w1, &w0, &w2, x[ 3], x[ 5]); word3_muladd(&w1, &w0, &w2, x[ 4], x[ 4]); z[ 8] = w2; w2 = 0; word3_muladd_2(&w2, &w1, &w0, x[ 4], x[ 5]); z[ 9] = w0; w0 = 0; word3_muladd(&w0, &w2, &w1, x[ 5], x[ 5]); z[10] = w1; z[11] = w2; } /* * Comba 6x6 Multiplication */ void bigint_comba_mul6(word z[12], const word x[6], const word y[6]) { word w2 = 0, w1 = 0, w0 = 0; word3_muladd(&w2, &w1, &w0, x[ 0], y[ 0]); z[ 0] = w0; w0 = 0; word3_muladd(&w0, &w2, &w1, x[ 0], y[ 1]); word3_muladd(&w0, &w2, &w1, x[ 1], y[ 0]); z[ 1] = w1; w1 = 0; word3_muladd(&w1, &w0, &w2, x[ 0], y[ 2]); word3_muladd(&w1, &w0, &w2, x[ 1], y[ 1]); word3_muladd(&w1, &w0, &w2, x[ 2], y[ 0]); z[ 2] = w2; w2 = 0; word3_muladd(&w2, &w1, &w0, x[ 0], y[ 3]); word3_muladd(&w2, &w1, &w0, x[ 1], y[ 2]); word3_muladd(&w2, &w1, &w0, x[ 2], y[ 1]); word3_muladd(&w2, &w1, &w0, x[ 3], y[ 0]); z[ 3] = w0; w0 = 0; word3_muladd(&w0, &w2, &w1, x[ 0], y[ 4]); word3_muladd(&w0, &w2, &w1, x[ 1], y[ 3]); word3_muladd(&w0, &w2, &w1, x[ 2], y[ 2]); word3_muladd(&w0, &w2, &w1, x[ 3], y[ 1]); word3_muladd(&w0, &w2, &w1, x[ 4], y[ 0]); z[ 4] = w1; w1 = 0; word3_muladd(&w1, &w0, &w2, x[ 0], y[ 5]); word3_muladd(&w1, &w0, &w2, x[ 1], y[ 4]); word3_muladd(&w1, &w0, &w2, x[ 2], y[ 3]); word3_muladd(&w1, &w0, &w2, x[ 3], y[ 2]); word3_muladd(&w1, &w0, &w2, x[ 4], y[ 1]); word3_muladd(&w1, &w0, &w2, x[ 5], y[ 0]); z[ 5] = w2; w2 = 0; word3_muladd(&w2, &w1, &w0, x[ 1], y[ 5]); word3_muladd(&w2, &w1, &w0, x[ 2], y[ 4]); word3_muladd(&w2, &w1, &w0, x[ 3], y[ 3]); word3_muladd(&w2, &w1, &w0, x[ 4], y[ 2]); word3_muladd(&w2, &w1, &w0, x[ 5], y[ 1]); z[ 6] = w0; w0 = 0; word3_muladd(&w0, &w2, &w1, x[ 2], y[ 5]); word3_muladd(&w0, &w2, &w1, x[ 3], y[ 4]); word3_muladd(&w0, &w2, &w1, x[ 4], y[ 3]); word3_muladd(&w0, &w2, &w1, x[ 5], y[ 2]); z[ 7] = w1; w1 = 0; word3_muladd(&w1, &w0, &w2, x[ 3], y[ 5]); word3_muladd(&w1, &w0, &w2, x[ 4], y[ 4]); word3_muladd(&w1, &w0, &w2, x[ 5], y[ 3]); z[ 8] = w2; w2 = 0; word3_muladd(&w2, &w1, &w0, x[ 4], y[ 5]); word3_muladd(&w2, &w1, &w0, x[ 5], y[ 4]); z[ 9] = w0; w0 = 0; word3_muladd(&w0, &w2, &w1, x[ 5], y[ 5]); z[10] = w1; z[11] = w2; } /* * Comba 8x8 Squaring */ void bigint_comba_sqr8(word z[16], const word x[8]) { word w2 = 0, w1 = 0, w0 = 0; word3_muladd(&w2, &w1, &w0, x[ 0], x[ 0]); z[ 0] = w0; w0 = 0; word3_muladd_2(&w0, &w2, &w1, x[ 0], x[ 1]); z[ 1] = w1; w1 = 0; word3_muladd_2(&w1, &w0, &w2, x[ 0], x[ 2]); word3_muladd(&w1, &w0, &w2, x[ 1], x[ 1]); z[ 2] = w2; w2 = 0; word3_muladd_2(&w2, &w1, &w0, x[ 0], x[ 3]); word3_muladd_2(&w2, &w1, &w0, x[ 1], x[ 2]); z[ 3] = w0; w0 = 0; word3_muladd_2(&w0, &w2, &w1, x[ 0], x[ 4]); word3_muladd_2(&w0, &w2, &w1, x[ 1], x[ 3]); word3_muladd(&w0, &w2, &w1, x[ 2], x[ 2]); z[ 4] = w1; w1 = 0; word3_muladd_2(&w1, &w0, &w2, x[ 0], x[ 5]); word3_muladd_2(&w1, &w0, &w2, x[ 1], x[ 4]); word3_muladd_2(&w1, &w0, &w2, x[ 2], x[ 3]); z[ 5] = w2; w2 = 0; word3_muladd_2(&w2, &w1, &w0, x[ 0], x[ 6]); word3_muladd_2(&w2, &w1, &w0, x[ 1], x[ 5]); word3_muladd_2(&w2, &w1, &w0, x[ 2], x[ 4]); word3_muladd(&w2, &w1, &w0, x[ 3], x[ 3]); z[ 6] = w0; w0 = 0; word3_muladd_2(&w0, &w2, &w1, x[ 0], x[ 7]); word3_muladd_2(&w0, &w2, &w1, x[ 1], x[ 6]); word3_muladd_2(&w0, &w2, &w1, x[ 2], x[ 5]); word3_muladd_2(&w0, &w2, &w1, x[ 3], x[ 4]); z[ 7] = w1; w1 = 0; word3_muladd_2(&w1, &w0, &w2, x[ 1], x[ 7]); word3_muladd_2(&w1, &w0, &w2, x[ 2], x[ 6]); word3_muladd_2(&w1, &w0, &w2, x[ 3], x[ 5]); word3_muladd(&w1, &w0, &w2, x[ 4], x[ 4]); z[ 8] = w2; w2 = 0; word3_muladd_2(&w2, &w1, &w0, x[ 2], x[ 7]); word3_muladd_2(&w2, &w1, &w0, x[ 3], x[ 6]); word3_muladd_2(&w2, &w1, &w0, x[ 4], x[ 5]); z[ 9] = w0; w0 = 0; word3_muladd_2(&w0, &w2, &w1, x[ 3], x[ 7]); word3_muladd_2(&w0, &w2, &w1, x[ 4], x[ 6]); word3_muladd(&w0, &w2, &w1, x[ 5], x[ 5]); z[10] = w1; w1 = 0; word3_muladd_2(&w1, &w0, &w2, x[ 4], x[ 7]); word3_muladd_2(&w1, &w0, &w2, x[ 5], x[ 6]); z[11] = w2; w2 = 0; word3_muladd_2(&w2, &w1, &w0, x[ 5], x[ 7]); word3_muladd(&w2, &w1, &w0, x[ 6], x[ 6]); z[12] = w0; w0 = 0; word3_muladd_2(&w0, &w2, &w1, x[ 6], x[ 7]); z[13] = w1; w1 = 0; word3_muladd(&w1, &w0, &w2, x[ 7], x[ 7]); z[14] = w2; z[15] = w0; } /* * Comba 8x8 Multiplication */ void bigint_comba_mul8(word z[16], const word x[8], const word y[8]) { word w2 = 0, w1 = 0, w0 = 0; word3_muladd(&w2, &w1, &w0, x[ 0], y[ 0]); z[ 0] = w0; w0 = 0; word3_muladd(&w0, &w2, &w1, x[ 0], y[ 1]); word3_muladd(&w0, &w2, &w1, x[ 1], y[ 0]); z[ 1] = w1; w1 = 0; word3_muladd(&w1, &w0, &w2, x[ 0], y[ 2]); word3_muladd(&w1, &w0, &w2, x[ 1], y[ 1]); word3_muladd(&w1, &w0, &w2, x[ 2], y[ 0]); z[ 2] = w2; w2 = 0; word3_muladd(&w2, &w1, &w0, x[ 0], y[ 3]); word3_muladd(&w2, &w1, &w0, x[ 1], y[ 2]); word3_muladd(&w2, &w1, &w0, x[ 2], y[ 1]); word3_muladd(&w2, &w1, &w0, x[ 3], y[ 0]); z[ 3] = w0; w0 = 0; word3_muladd(&w0, &w2, &w1, x[ 0], y[ 4]); word3_muladd(&w0, &w2, &w1, x[ 1], y[ 3]); word3_muladd(&w0, &w2, &w1, x[ 2], y[ 2]); word3_muladd(&w0, &w2, &w1, x[ 3], y[ 1]); word3_muladd(&w0, &w2, &w1, x[ 4], y[ 0]); z[ 4] = w1; w1 = 0; word3_muladd(&w1, &w0, &w2, x[ 0], y[ 5]); word3_muladd(&w1, &w0, &w2, x[ 1], y[ 4]); word3_muladd(&w1, &w0, &w2, x[ 2], y[ 3]); word3_muladd(&w1, &w0, &w2, x[ 3], y[ 2]); word3_muladd(&w1, &w0, &w2, x[ 4], y[ 1]); word3_muladd(&w1, &w0, &w2, x[ 5], y[ 0]); z[ 5] = w2; w2 = 0; word3_muladd(&w2, &w1, &w0, x[ 0], y[ 6]); word3_muladd(&w2, &w1, &w0, x[ 1], y[ 5]); word3_muladd(&w2, &w1, &w0, x[ 2], y[ 4]); word3_muladd(&w2, &w1, &w0, x[ 3], y[ 3]); word3_muladd(&w2, &w1, &w0, x[ 4], y[ 2]); word3_muladd(&w2, &w1, &w0, x[ 5], y[ 1]); word3_muladd(&w2, &w1, &w0, x[ 6], y[ 0]); z[ 6] = w0; w0 = 0; word3_muladd(&w0, &w2, &w1, x[ 0], y[ 7]); word3_muladd(&w0, &w2, &w1, x[ 1], y[ 6]); word3_muladd(&w0, &w2, &w1, x[ 2], y[ 5]); word3_muladd(&w0, &w2, &w1, x[ 3], y[ 4]); word3_muladd(&w0, &w2, &w1, x[ 4], y[ 3]); word3_muladd(&w0, &w2, &w1, x[ 5], y[ 2]); word3_muladd(&w0, &w2, &w1, x[ 6], y[ 1]); word3_muladd(&w0, &w2, &w1, x[ 7], y[ 0]); z[ 7] = w1; w1 = 0; word3_muladd(&w1, &w0, &w2, x[ 1], y[ 7]); word3_muladd(&w1, &w0, &w2, x[ 2], y[ 6]); word3_muladd(&w1, &w0, &w2, x[ 3], y[ 5]); word3_muladd(&w1, &w0, &w2, x[ 4], y[ 4]); word3_muladd(&w1, &w0, &w2, x[ 5], y[ 3]); word3_muladd(&w1, &w0, &w2, x[ 6], y[ 2]); word3_muladd(&w1, &w0, &w2, x[ 7], y[ 1]); z[ 8] = w2; w2 = 0; word3_muladd(&w2, &w1, &w0, x[ 2], y[ 7]); word3_muladd(&w2, &w1, &w0, x[ 3], y[ 6]); word3_muladd(&w2, &w1, &w0, x[ 4], y[ 5]); word3_muladd(&w2, &w1, &w0, x[ 5], y[ 4]); word3_muladd(&w2, &w1, &w0, x[ 6], y[ 3]); word3_muladd(&w2, &w1, &w0, x[ 7], y[ 2]); z[ 9] = w0; w0 = 0; word3_muladd(&w0, &w2, &w1, x[ 3], y[ 7]); word3_muladd(&w0, &w2, &w1, x[ 4], y[ 6]); word3_muladd(&w0, &w2, &w1, x[ 5], y[ 5]); word3_muladd(&w0, &w2, &w1, x[ 6], y[ 4]); word3_muladd(&w0, &w2, &w1, x[ 7], y[ 3]); z[10] = w1; w1 = 0; word3_muladd(&w1, &w0, &w2, x[ 4], y[ 7]); word3_muladd(&w1, &w0, &w2, x[ 5], y[ 6]); word3_muladd(&w1, &w0, &w2, x[ 6], y[ 5]); word3_muladd(&w1, &w0, &w2, x[ 7], y[ 4]); z[11] = w2; w2 = 0; word3_muladd(&w2, &w1, &w0, x[ 5], y[ 7]); word3_muladd(&w2, &w1, &w0, x[ 6], y[ 6]); word3_muladd(&w2, &w1, &w0, x[ 7], y[ 5]); z[12] = w0; w0 = 0; word3_muladd(&w0, &w2, &w1, x[ 6], y[ 7]); word3_muladd(&w0, &w2, &w1, x[ 7], y[ 6]); z[13] = w1; w1 = 0; word3_muladd(&w1, &w0, &w2, x[ 7], y[ 7]); z[14] = w2; z[15] = w0; } /* * Comba 16x16 Squaring */ void bigint_comba_sqr16(word z[32], const word x[16]) { word w2 = 0, w1 = 0, w0 = 0; word3_muladd(&w2, &w1, &w0, x[ 0], x[ 0]); z[ 0] = w0; w0 = 0; word3_muladd_2(&w0, &w2, &w1, x[ 0], x[ 1]); z[ 1] = w1; w1 = 0; word3_muladd_2(&w1, &w0, &w2, x[ 0], x[ 2]); word3_muladd(&w1, &w0, &w2, x[ 1], x[ 1]); z[ 2] = w2; w2 = 0; word3_muladd_2(&w2, &w1, &w0, x[ 0], x[ 3]); word3_muladd_2(&w2, &w1, &w0, x[ 1], x[ 2]); z[ 3] = w0; w0 = 0; word3_muladd_2(&w0, &w2, &w1, x[ 0], x[ 4]); word3_muladd_2(&w0, &w2, &w1, x[ 1], x[ 3]); word3_muladd(&w0, &w2, &w1, x[ 2], x[ 2]); z[ 4] = w1; w1 = 0; word3_muladd_2(&w1, &w0, &w2, x[ 0], x[ 5]); word3_muladd_2(&w1, &w0, &w2, x[ 1], x[ 4]); word3_muladd_2(&w1, &w0, &w2, x[ 2], x[ 3]); z[ 5] = w2; w2 = 0; word3_muladd_2(&w2, &w1, &w0, x[ 0], x[ 6]); word3_muladd_2(&w2, &w1, &w0, x[ 1], x[ 5]); word3_muladd_2(&w2, &w1, &w0, x[ 2], x[ 4]); word3_muladd(&w2, &w1, &w0, x[ 3], x[ 3]); z[ 6] = w0; w0 = 0; word3_muladd_2(&w0, &w2, &w1, x[ 0], x[ 7]); word3_muladd_2(&w0, &w2, &w1, x[ 1], x[ 6]); word3_muladd_2(&w0, &w2, &w1, x[ 2], x[ 5]); word3_muladd_2(&w0, &w2, &w1, x[ 3], x[ 4]); z[ 7] = w1; w1 = 0; word3_muladd_2(&w1, &w0, &w2, x[ 0], x[ 8]); word3_muladd_2(&w1, &w0, &w2, x[ 1], x[ 7]); word3_muladd_2(&w1, &w0, &w2, x[ 2], x[ 6]); word3_muladd_2(&w1, &w0, &w2, x[ 3], x[ 5]); word3_muladd(&w1, &w0, &w2, x[ 4], x[ 4]); z[ 8] = w2; w2 = 0; word3_muladd_2(&w2, &w1, &w0, x[ 0], x[ 9]); word3_muladd_2(&w2, &w1, &w0, x[ 1], x[ 8]); word3_muladd_2(&w2, &w1, &w0, x[ 2], x[ 7]); word3_muladd_2(&w2, &w1, &w0, x[ 3], x[ 6]); word3_muladd_2(&w2, &w1, &w0, x[ 4], x[ 5]); z[ 9] = w0; w0 = 0; word3_muladd_2(&w0, &w2, &w1, x[ 0], x[10]); word3_muladd_2(&w0, &w2, &w1, x[ 1], x[ 9]); word3_muladd_2(&w0, &w2, &w1, x[ 2], x[ 8]); word3_muladd_2(&w0, &w2, &w1, x[ 3], x[ 7]); word3_muladd_2(&w0, &w2, &w1, x[ 4], x[ 6]); word3_muladd(&w0, &w2, &w1, x[ 5], x[ 5]); z[10] = w1; w1 = 0; word3_muladd_2(&w1, &w0, &w2, x[ 0], x[11]); word3_muladd_2(&w1, &w0, &w2, x[ 1], x[10]); word3_muladd_2(&w1, &w0, &w2, x[ 2], x[ 9]); word3_muladd_2(&w1, &w0, &w2, x[ 3], x[ 8]); word3_muladd_2(&w1, &w0, &w2, x[ 4], x[ 7]); word3_muladd_2(&w1, &w0, &w2, x[ 5], x[ 6]); z[11] = w2; w2 = 0; word3_muladd_2(&w2, &w1, &w0, x[ 0], x[12]); word3_muladd_2(&w2, &w1, &w0, x[ 1], x[11]); word3_muladd_2(&w2, &w1, &w0, x[ 2], x[10]); word3_muladd_2(&w2, &w1, &w0, x[ 3], x[ 9]); word3_muladd_2(&w2, &w1, &w0, x[ 4], x[ 8]); word3_muladd_2(&w2, &w1, &w0, x[ 5], x[ 7]); word3_muladd(&w2, &w1, &w0, x[ 6], x[ 6]); z[12] = w0; w0 = 0; word3_muladd_2(&w0, &w2, &w1, x[ 0], x[13]); word3_muladd_2(&w0, &w2, &w1, x[ 1], x[12]); word3_muladd_2(&w0, &w2, &w1, x[ 2], x[11]); word3_muladd_2(&w0, &w2, &w1, x[ 3], x[10]); word3_muladd_2(&w0, &w2, &w1, x[ 4], x[ 9]); word3_muladd_2(&w0, &w2, &w1, x[ 5], x[ 8]); word3_muladd_2(&w0, &w2, &w1, x[ 6], x[ 7]); z[13] = w1; w1 = 0; word3_muladd_2(&w1, &w0, &w2, x[ 0], x[14]); word3_muladd_2(&w1, &w0, &w2, x[ 1], x[13]); word3_muladd_2(&w1, &w0, &w2, x[ 2], x[12]); word3_muladd_2(&w1, &w0, &w2, x[ 3], x[11]); word3_muladd_2(&w1, &w0, &w2, x[ 4], x[10]); word3_muladd_2(&w1, &w0, &w2, x[ 5], x[ 9]); word3_muladd_2(&w1, &w0, &w2, x[ 6], x[ 8]); word3_muladd(&w1, &w0, &w2, x[ 7], x[ 7]); z[14] = w2; w2 = 0; word3_muladd_2(&w2, &w1, &w0, x[ 0], x[15]); word3_muladd_2(&w2, &w1, &w0, x[ 1], x[14]); word3_muladd_2(&w2, &w1, &w0, x[ 2], x[13]); word3_muladd_2(&w2, &w1, &w0, x[ 3], x[12]); word3_muladd_2(&w2, &w1, &w0, x[ 4], x[11]); word3_muladd_2(&w2, &w1, &w0, x[ 5], x[10]); word3_muladd_2(&w2, &w1, &w0, x[ 6], x[ 9]); word3_muladd_2(&w2, &w1, &w0, x[ 7], x[ 8]); z[15] = w0; w0 = 0; word3_muladd_2(&w0, &w2, &w1, x[ 1], x[15]); word3_muladd_2(&w0, &w2, &w1, x[ 2], x[14]); word3_muladd_2(&w0, &w2, &w1, x[ 3], x[13]); word3_muladd_2(&w0, &w2, &w1, x[ 4], x[12]); word3_muladd_2(&w0, &w2, &w1, x[ 5], x[11]); word3_muladd_2(&w0, &w2, &w1, x[ 6], x[10]); word3_muladd_2(&w0, &w2, &w1, x[ 7], x[ 9]); word3_muladd(&w0, &w2, &w1, x[ 8], x[ 8]); z[16] = w1; w1 = 0; word3_muladd_2(&w1, &w0, &w2, x[ 2], x[15]); word3_muladd_2(&w1, &w0, &w2, x[ 3], x[14]); word3_muladd_2(&w1, &w0, &w2, x[ 4], x[13]); word3_muladd_2(&w1, &w0, &w2, x[ 5], x[12]); word3_muladd_2(&w1, &w0, &w2, x[ 6], x[11]); word3_muladd_2(&w1, &w0, &w2, x[ 7], x[10]); word3_muladd_2(&w1, &w0, &w2, x[ 8], x[ 9]); z[17] = w2; w2 = 0; word3_muladd_2(&w2, &w1, &w0, x[ 3], x[15]); word3_muladd_2(&w2, &w1, &w0, x[ 4], x[14]); word3_muladd_2(&w2, &w1, &w0, x[ 5], x[13]); word3_muladd_2(&w2, &w1, &w0, x[ 6], x[12]); word3_muladd_2(&w2, &w1, &w0, x[ 7], x[11]); word3_muladd_2(&w2, &w1, &w0, x[ 8], x[10]); word3_muladd(&w2, &w1, &w0, x[ 9], x[ 9]); z[18] = w0; w0 = 0; word3_muladd_2(&w0, &w2, &w1, x[ 4], x[15]); word3_muladd_2(&w0, &w2, &w1, x[ 5], x[14]); word3_muladd_2(&w0, &w2, &w1, x[ 6], x[13]); word3_muladd_2(&w0, &w2, &w1, x[ 7], x[12]); word3_muladd_2(&w0, &w2, &w1, x[ 8], x[11]); word3_muladd_2(&w0, &w2, &w1, x[ 9], x[10]); z[19] = w1; w1 = 0; word3_muladd_2(&w1, &w0, &w2, x[ 5], x[15]); word3_muladd_2(&w1, &w0, &w2, x[ 6], x[14]); word3_muladd_2(&w1, &w0, &w2, x[ 7], x[13]); word3_muladd_2(&w1, &w0, &w2, x[ 8], x[12]); word3_muladd_2(&w1, &w0, &w2, x[ 9], x[11]); word3_muladd(&w1, &w0, &w2, x[10], x[10]); z[20] = w2; w2 = 0; word3_muladd_2(&w2, &w1, &w0, x[ 6], x[15]); word3_muladd_2(&w2, &w1, &w0, x[ 7], x[14]); word3_muladd_2(&w2, &w1, &w0, x[ 8], x[13]); word3_muladd_2(&w2, &w1, &w0, x[ 9], x[12]); word3_muladd_2(&w2, &w1, &w0, x[10], x[11]); z[21] = w0; w0 = 0; word3_muladd_2(&w0, &w2, &w1, x[ 7], x[15]); word3_muladd_2(&w0, &w2, &w1, x[ 8], x[14]); word3_muladd_2(&w0, &w2, &w1, x[ 9], x[13]); word3_muladd_2(&w0, &w2, &w1, x[10], x[12]); word3_muladd(&w0, &w2, &w1, x[11], x[11]); z[22] = w1; w1 = 0; word3_muladd_2(&w1, &w0, &w2, x[ 8], x[15]); word3_muladd_2(&w1, &w0, &w2, x[ 9], x[14]); word3_muladd_2(&w1, &w0, &w2, x[10], x[13]); word3_muladd_2(&w1, &w0, &w2, x[11], x[12]); z[23] = w2; w2 = 0; word3_muladd_2(&w2, &w1, &w0, x[ 9], x[15]); word3_muladd_2(&w2, &w1, &w0, x[10], x[14]); word3_muladd_2(&w2, &w1, &w0, x[11], x[13]); word3_muladd(&w2, &w1, &w0, x[12], x[12]); z[24] = w0; w0 = 0; word3_muladd_2(&w0, &w2, &w1, x[10], x[15]); word3_muladd_2(&w0, &w2, &w1, x[11], x[14]); word3_muladd_2(&w0, &w2, &w1, x[12], x[13]); z[25] = w1; w1 = 0; word3_muladd_2(&w1, &w0, &w2, x[11], x[15]); word3_muladd_2(&w1, &w0, &w2, x[12], x[14]); word3_muladd(&w1, &w0, &w2, x[13], x[13]); z[26] = w2; w2 = 0; word3_muladd_2(&w2, &w1, &w0, x[12], x[15]); word3_muladd_2(&w2, &w1, &w0, x[13], x[14]); z[27] = w0; w0 = 0; word3_muladd_2(&w0, &w2, &w1, x[13], x[15]); word3_muladd(&w0, &w2, &w1, x[14], x[14]); z[28] = w1; w1 = 0; word3_muladd_2(&w1, &w0, &w2, x[14], x[15]); z[29] = w2; w2 = 0; word3_muladd(&w2, &w1, &w0, x[15], x[15]); z[30] = w0; z[31] = w1; } /* * Comba 16x16 Multiplication */ void bigint_comba_mul16(word z[32], const word x[16], const word y[16]) { word w2 = 0, w1 = 0, w0 = 0; word3_muladd(&w2, &w1, &w0, x[ 0], y[ 0]); z[ 0] = w0; w0 = 0; word3_muladd(&w0, &w2, &w1, x[ 0], y[ 1]); word3_muladd(&w0, &w2, &w1, x[ 1], y[ 0]); z[ 1] = w1; w1 = 0; word3_muladd(&w1, &w0, &w2, x[ 0], y[ 2]); word3_muladd(&w1, &w0, &w2, x[ 1], y[ 1]); word3_muladd(&w1, &w0, &w2, x[ 2], y[ 0]); z[ 2] = w2; w2 = 0; word3_muladd(&w2, &w1, &w0, x[ 0], y[ 3]); word3_muladd(&w2, &w1, &w0, x[ 1], y[ 2]); word3_muladd(&w2, &w1, &w0, x[ 2], y[ 1]); word3_muladd(&w2, &w1, &w0, x[ 3], y[ 0]); z[ 3] = w0; w0 = 0; word3_muladd(&w0, &w2, &w1, x[ 0], y[ 4]); word3_muladd(&w0, &w2, &w1, x[ 1], y[ 3]); word3_muladd(&w0, &w2, &w1, x[ 2], y[ 2]); word3_muladd(&w0, &w2, &w1, x[ 3], y[ 1]); word3_muladd(&w0, &w2, &w1, x[ 4], y[ 0]); z[ 4] = w1; w1 = 0; word3_muladd(&w1, &w0, &w2, x[ 0], y[ 5]); word3_muladd(&w1, &w0, &w2, x[ 1], y[ 4]); word3_muladd(&w1, &w0, &w2, x[ 2], y[ 3]); word3_muladd(&w1, &w0, &w2, x[ 3], y[ 2]); word3_muladd(&w1, &w0, &w2, x[ 4], y[ 1]); word3_muladd(&w1, &w0, &w2, x[ 5], y[ 0]); z[ 5] = w2; w2 = 0; word3_muladd(&w2, &w1, &w0, x[ 0], y[ 6]); word3_muladd(&w2, &w1, &w0, x[ 1], y[ 5]); word3_muladd(&w2, &w1, &w0, x[ 2], y[ 4]); word3_muladd(&w2, &w1, &w0, x[ 3], y[ 3]); word3_muladd(&w2, &w1, &w0, x[ 4], y[ 2]); word3_muladd(&w2, &w1, &w0, x[ 5], y[ 1]); word3_muladd(&w2, &w1, &w0, x[ 6], y[ 0]); z[ 6] = w0; w0 = 0; word3_muladd(&w0, &w2, &w1, x[ 0], y[ 7]); word3_muladd(&w0, &w2, &w1, x[ 1], y[ 6]); word3_muladd(&w0, &w2, &w1, x[ 2], y[ 5]); word3_muladd(&w0, &w2, &w1, x[ 3], y[ 4]); word3_muladd(&w0, &w2, &w1, x[ 4], y[ 3]); word3_muladd(&w0, &w2, &w1, x[ 5], y[ 2]); word3_muladd(&w0, &w2, &w1, x[ 6], y[ 1]); word3_muladd(&w0, &w2, &w1, x[ 7], y[ 0]); z[ 7] = w1; w1 = 0; word3_muladd(&w1, &w0, &w2, x[ 0], y[ 8]); word3_muladd(&w1, &w0, &w2, x[ 1], y[ 7]); word3_muladd(&w1, &w0, &w2, x[ 2], y[ 6]); word3_muladd(&w1, &w0, &w2, x[ 3], y[ 5]); word3_muladd(&w1, &w0, &w2, x[ 4], y[ 4]); word3_muladd(&w1, &w0, &w2, x[ 5], y[ 3]); word3_muladd(&w1, &w0, &w2, x[ 6], y[ 2]); word3_muladd(&w1, &w0, &w2, x[ 7], y[ 1]); word3_muladd(&w1, &w0, &w2, x[ 8], y[ 0]); z[ 8] = w2; w2 = 0; word3_muladd(&w2, &w1, &w0, x[ 0], y[ 9]); word3_muladd(&w2, &w1, &w0, x[ 1], y[ 8]); word3_muladd(&w2, &w1, &w0, x[ 2], y[ 7]); word3_muladd(&w2, &w1, &w0, x[ 3], y[ 6]); word3_muladd(&w2, &w1, &w0, x[ 4], y[ 5]); word3_muladd(&w2, &w1, &w0, x[ 5], y[ 4]); word3_muladd(&w2, &w1, &w0, x[ 6], y[ 3]); word3_muladd(&w2, &w1, &w0, x[ 7], y[ 2]); word3_muladd(&w2, &w1, &w0, x[ 8], y[ 1]); word3_muladd(&w2, &w1, &w0, x[ 9], y[ 0]); z[ 9] = w0; w0 = 0; word3_muladd(&w0, &w2, &w1, x[ 0], y[10]); word3_muladd(&w0, &w2, &w1, x[ 1], y[ 9]); word3_muladd(&w0, &w2, &w1, x[ 2], y[ 8]); word3_muladd(&w0, &w2, &w1, x[ 3], y[ 7]); word3_muladd(&w0, &w2, &w1, x[ 4], y[ 6]); word3_muladd(&w0, &w2, &w1, x[ 5], y[ 5]); word3_muladd(&w0, &w2, &w1, x[ 6], y[ 4]); word3_muladd(&w0, &w2, &w1, x[ 7], y[ 3]); word3_muladd(&w0, &w2, &w1, x[ 8], y[ 2]); word3_muladd(&w0, &w2, &w1, x[ 9], y[ 1]); word3_muladd(&w0, &w2, &w1, x[10], y[ 0]); z[10] = w1; w1 = 0; word3_muladd(&w1, &w0, &w2, x[ 0], y[11]); word3_muladd(&w1, &w0, &w2, x[ 1], y[10]); word3_muladd(&w1, &w0, &w2, x[ 2], y[ 9]); word3_muladd(&w1, &w0, &w2, x[ 3], y[ 8]); word3_muladd(&w1, &w0, &w2, x[ 4], y[ 7]); word3_muladd(&w1, &w0, &w2, x[ 5], y[ 6]); word3_muladd(&w1, &w0, &w2, x[ 6], y[ 5]); word3_muladd(&w1, &w0, &w2, x[ 7], y[ 4]); word3_muladd(&w1, &w0, &w2, x[ 8], y[ 3]); word3_muladd(&w1, &w0, &w2, x[ 9], y[ 2]); word3_muladd(&w1, &w0, &w2, x[10], y[ 1]); word3_muladd(&w1, &w0, &w2, x[11], y[ 0]); z[11] = w2; w2 = 0; word3_muladd(&w2, &w1, &w0, x[ 0], y[12]); word3_muladd(&w2, &w1, &w0, x[ 1], y[11]); word3_muladd(&w2, &w1, &w0, x[ 2], y[10]); word3_muladd(&w2, &w1, &w0, x[ 3], y[ 9]); word3_muladd(&w2, &w1, &w0, x[ 4], y[ 8]); word3_muladd(&w2, &w1, &w0, x[ 5], y[ 7]); word3_muladd(&w2, &w1, &w0, x[ 6], y[ 6]); word3_muladd(&w2, &w1, &w0, x[ 7], y[ 5]); word3_muladd(&w2, &w1, &w0, x[ 8], y[ 4]); word3_muladd(&w2, &w1, &w0, x[ 9], y[ 3]); word3_muladd(&w2, &w1, &w0, x[10], y[ 2]); word3_muladd(&w2, &w1, &w0, x[11], y[ 1]); word3_muladd(&w2, &w1, &w0, x[12], y[ 0]); z[12] = w0; w0 = 0; word3_muladd(&w0, &w2, &w1, x[ 0], y[13]); word3_muladd(&w0, &w2, &w1, x[ 1], y[12]); word3_muladd(&w0, &w2, &w1, x[ 2], y[11]); word3_muladd(&w0, &w2, &w1, x[ 3], y[10]); word3_muladd(&w0, &w2, &w1, x[ 4], y[ 9]); word3_muladd(&w0, &w2, &w1, x[ 5], y[ 8]); word3_muladd(&w0, &w2, &w1, x[ 6], y[ 7]); word3_muladd(&w0, &w2, &w1, x[ 7], y[ 6]); word3_muladd(&w0, &w2, &w1, x[ 8], y[ 5]); word3_muladd(&w0, &w2, &w1, x[ 9], y[ 4]); word3_muladd(&w0, &w2, &w1, x[10], y[ 3]); word3_muladd(&w0, &w2, &w1, x[11], y[ 2]); word3_muladd(&w0, &w2, &w1, x[12], y[ 1]); word3_muladd(&w0, &w2, &w1, x[13], y[ 0]); z[13] = w1; w1 = 0; word3_muladd(&w1, &w0, &w2, x[ 0], y[14]); word3_muladd(&w1, &w0, &w2, x[ 1], y[13]); word3_muladd(&w1, &w0, &w2, x[ 2], y[12]); word3_muladd(&w1, &w0, &w2, x[ 3], y[11]); word3_muladd(&w1, &w0, &w2, x[ 4], y[10]); word3_muladd(&w1, &w0, &w2, x[ 5], y[ 9]); word3_muladd(&w1, &w0, &w2, x[ 6], y[ 8]); word3_muladd(&w1, &w0, &w2, x[ 7], y[ 7]); word3_muladd(&w1, &w0, &w2, x[ 8], y[ 6]); word3_muladd(&w1, &w0, &w2, x[ 9], y[ 5]); word3_muladd(&w1, &w0, &w2, x[10], y[ 4]); word3_muladd(&w1, &w0, &w2, x[11], y[ 3]); word3_muladd(&w1, &w0, &w2, x[12], y[ 2]); word3_muladd(&w1, &w0, &w2, x[13], y[ 1]); word3_muladd(&w1, &w0, &w2, x[14], y[ 0]); z[14] = w2; w2 = 0; word3_muladd(&w2, &w1, &w0, x[ 0], y[15]); word3_muladd(&w2, &w1, &w0, x[ 1], y[14]); word3_muladd(&w2, &w1, &w0, x[ 2], y[13]); word3_muladd(&w2, &w1, &w0, x[ 3], y[12]); word3_muladd(&w2, &w1, &w0, x[ 4], y[11]); word3_muladd(&w2, &w1, &w0, x[ 5], y[10]); word3_muladd(&w2, &w1, &w0, x[ 6], y[ 9]); word3_muladd(&w2, &w1, &w0, x[ 7], y[ 8]); word3_muladd(&w2, &w1, &w0, x[ 8], y[ 7]); word3_muladd(&w2, &w1, &w0, x[ 9], y[ 6]); word3_muladd(&w2, &w1, &w0, x[10], y[ 5]); word3_muladd(&w2, &w1, &w0, x[11], y[ 4]); word3_muladd(&w2, &w1, &w0, x[12], y[ 3]); word3_muladd(&w2, &w1, &w0, x[13], y[ 2]); word3_muladd(&w2, &w1, &w0, x[14], y[ 1]); word3_muladd(&w2, &w1, &w0, x[15], y[ 0]); z[15] = w0; w0 = 0; word3_muladd(&w0, &w2, &w1, x[ 1], y[15]); word3_muladd(&w0, &w2, &w1, x[ 2], y[14]); word3_muladd(&w0, &w2, &w1, x[ 3], y[13]); word3_muladd(&w0, &w2, &w1, x[ 4], y[12]); word3_muladd(&w0, &w2, &w1, x[ 5], y[11]); word3_muladd(&w0, &w2, &w1, x[ 6], y[10]); word3_muladd(&w0, &w2, &w1, x[ 7], y[ 9]); word3_muladd(&w0, &w2, &w1, x[ 8], y[ 8]); word3_muladd(&w0, &w2, &w1, x[ 9], y[ 7]); word3_muladd(&w0, &w2, &w1, x[10], y[ 6]); word3_muladd(&w0, &w2, &w1, x[11], y[ 5]); word3_muladd(&w0, &w2, &w1, x[12], y[ 4]); word3_muladd(&w0, &w2, &w1, x[13], y[ 3]); word3_muladd(&w0, &w2, &w1, x[14], y[ 2]); word3_muladd(&w0, &w2, &w1, x[15], y[ 1]); z[16] = w1; w1 = 0; word3_muladd(&w1, &w0, &w2, x[ 2], y[15]); word3_muladd(&w1, &w0, &w2, x[ 3], y[14]); word3_muladd(&w1, &w0, &w2, x[ 4], y[13]); word3_muladd(&w1, &w0, &w2, x[ 5], y[12]); word3_muladd(&w1, &w0, &w2, x[ 6], y[11]); word3_muladd(&w1, &w0, &w2, x[ 7], y[10]); word3_muladd(&w1, &w0, &w2, x[ 8], y[ 9]); word3_muladd(&w1, &w0, &w2, x[ 9], y[ 8]); word3_muladd(&w1, &w0, &w2, x[10], y[ 7]); word3_muladd(&w1, &w0, &w2, x[11], y[ 6]); word3_muladd(&w1, &w0, &w2, x[12], y[ 5]); word3_muladd(&w1, &w0, &w2, x[13], y[ 4]); word3_muladd(&w1, &w0, &w2, x[14], y[ 3]); word3_muladd(&w1, &w0, &w2, x[15], y[ 2]); z[17] = w2; w2 = 0; word3_muladd(&w2, &w1, &w0, x[ 3], y[15]); word3_muladd(&w2, &w1, &w0, x[ 4], y[14]); word3_muladd(&w2, &w1, &w0, x[ 5], y[13]); word3_muladd(&w2, &w1, &w0, x[ 6], y[12]); word3_muladd(&w2, &w1, &w0, x[ 7], y[11]); word3_muladd(&w2, &w1, &w0, x[ 8], y[10]); word3_muladd(&w2, &w1, &w0, x[ 9], y[ 9]); word3_muladd(&w2, &w1, &w0, x[10], y[ 8]); word3_muladd(&w2, &w1, &w0, x[11], y[ 7]); word3_muladd(&w2, &w1, &w0, x[12], y[ 6]); word3_muladd(&w2, &w1, &w0, x[13], y[ 5]); word3_muladd(&w2, &w1, &w0, x[14], y[ 4]); word3_muladd(&w2, &w1, &w0, x[15], y[ 3]); z[18] = w0; w0 = 0; word3_muladd(&w0, &w2, &w1, x[ 4], y[15]); word3_muladd(&w0, &w2, &w1, x[ 5], y[14]); word3_muladd(&w0, &w2, &w1, x[ 6], y[13]); word3_muladd(&w0, &w2, &w1, x[ 7], y[12]); word3_muladd(&w0, &w2, &w1, x[ 8], y[11]); word3_muladd(&w0, &w2, &w1, x[ 9], y[10]); word3_muladd(&w0, &w2, &w1, x[10], y[ 9]); word3_muladd(&w0, &w2, &w1, x[11], y[ 8]); word3_muladd(&w0, &w2, &w1, x[12], y[ 7]); word3_muladd(&w0, &w2, &w1, x[13], y[ 6]); word3_muladd(&w0, &w2, &w1, x[14], y[ 5]); word3_muladd(&w0, &w2, &w1, x[15], y[ 4]); z[19] = w1; w1 = 0; word3_muladd(&w1, &w0, &w2, x[ 5], y[15]); word3_muladd(&w1, &w0, &w2, x[ 6], y[14]); word3_muladd(&w1, &w0, &w2, x[ 7], y[13]); word3_muladd(&w1, &w0, &w2, x[ 8], y[12]); word3_muladd(&w1, &w0, &w2, x[ 9], y[11]); word3_muladd(&w1, &w0, &w2, x[10], y[10]); word3_muladd(&w1, &w0, &w2, x[11], y[ 9]); word3_muladd(&w1, &w0, &w2, x[12], y[ 8]); word3_muladd(&w1, &w0, &w2, x[13], y[ 7]); word3_muladd(&w1, &w0, &w2, x[14], y[ 6]); word3_muladd(&w1, &w0, &w2, x[15], y[ 5]); z[20] = w2; w2 = 0; word3_muladd(&w2, &w1, &w0, x[ 6], y[15]); word3_muladd(&w2, &w1, &w0, x[ 7], y[14]); word3_muladd(&w2, &w1, &w0, x[ 8], y[13]); word3_muladd(&w2, &w1, &w0, x[ 9], y[12]); word3_muladd(&w2, &w1, &w0, x[10], y[11]); word3_muladd(&w2, &w1, &w0, x[11], y[10]); word3_muladd(&w2, &w1, &w0, x[12], y[ 9]); word3_muladd(&w2, &w1, &w0, x[13], y[ 8]); word3_muladd(&w2, &w1, &w0, x[14], y[ 7]); word3_muladd(&w2, &w1, &w0, x[15], y[ 6]); z[21] = w0; w0 = 0; word3_muladd(&w0, &w2, &w1, x[ 7], y[15]); word3_muladd(&w0, &w2, &w1, x[ 8], y[14]); word3_muladd(&w0, &w2, &w1, x[ 9], y[13]); word3_muladd(&w0, &w2, &w1, x[10], y[12]); word3_muladd(&w0, &w2, &w1, x[11], y[11]); word3_muladd(&w0, &w2, &w1, x[12], y[10]); word3_muladd(&w0, &w2, &w1, x[13], y[ 9]); word3_muladd(&w0, &w2, &w1, x[14], y[ 8]); word3_muladd(&w0, &w2, &w1, x[15], y[ 7]); z[22] = w1; w1 = 0; word3_muladd(&w1, &w0, &w2, x[ 8], y[15]); word3_muladd(&w1, &w0, &w2, x[ 9], y[14]); word3_muladd(&w1, &w0, &w2, x[10], y[13]); word3_muladd(&w1, &w0, &w2, x[11], y[12]); word3_muladd(&w1, &w0, &w2, x[12], y[11]); word3_muladd(&w1, &w0, &w2, x[13], y[10]); word3_muladd(&w1, &w0, &w2, x[14], y[ 9]); word3_muladd(&w1, &w0, &w2, x[15], y[ 8]); z[23] = w2; w2 = 0; word3_muladd(&w2, &w1, &w0, x[ 9], y[15]); word3_muladd(&w2, &w1, &w0, x[10], y[14]); word3_muladd(&w2, &w1, &w0, x[11], y[13]); word3_muladd(&w2, &w1, &w0, x[12], y[12]); word3_muladd(&w2, &w1, &w0, x[13], y[11]); word3_muladd(&w2, &w1, &w0, x[14], y[10]); word3_muladd(&w2, &w1, &w0, x[15], y[ 9]); z[24] = w0; w0 = 0; word3_muladd(&w0, &w2, &w1, x[10], y[15]); word3_muladd(&w0, &w2, &w1, x[11], y[14]); word3_muladd(&w0, &w2, &w1, x[12], y[13]); word3_muladd(&w0, &w2, &w1, x[13], y[12]); word3_muladd(&w0, &w2, &w1, x[14], y[11]); word3_muladd(&w0, &w2, &w1, x[15], y[10]); z[25] = w1; w1 = 0; word3_muladd(&w1, &w0, &w2, x[11], y[15]); word3_muladd(&w1, &w0, &w2, x[12], y[14]); word3_muladd(&w1, &w0, &w2, x[13], y[13]); word3_muladd(&w1, &w0, &w2, x[14], y[12]); word3_muladd(&w1, &w0, &w2, x[15], y[11]); z[26] = w2; w2 = 0; word3_muladd(&w2, &w1, &w0, x[12], y[15]); word3_muladd(&w2, &w1, &w0, x[13], y[14]); word3_muladd(&w2, &w1, &w0, x[14], y[13]); word3_muladd(&w2, &w1, &w0, x[15], y[12]); z[27] = w0; w0 = 0; word3_muladd(&w0, &w2, &w1, x[13], y[15]); word3_muladd(&w0, &w2, &w1, x[14], y[14]); word3_muladd(&w0, &w2, &w1, x[15], y[13]); z[28] = w1; w1 = 0; word3_muladd(&w1, &w0, &w2, x[14], y[15]); word3_muladd(&w1, &w0, &w2, x[15], y[14]); z[29] = w2; w2 = 0; word3_muladd(&w2, &w1, &w0, x[15], y[15]); z[30] = w0; z[31] = w1; } } } /* * Karatsuba Multiplication/Squaring * (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { /* * Karatsuba Multiplication Operation */ void karatsuba_mul(word z[], const word x[], const word y[], size_t N, word workspace[]) { if(N < BOTAN_KARAT_MUL_THRESHOLD || N % 2) { if(N == 6) return bigint_comba_mul6(z, x, y); else if(N == 8) return bigint_comba_mul8(z, x, y); else if(N == 16) return bigint_comba_mul16(z, x, y); else return bigint_simple_mul(z, x, N, y, N); } const size_t N2 = N / 2; const word* x0 = x; const word* x1 = x + N2; const word* y0 = y; const word* y1 = y + N2; word* z0 = z; word* z1 = z + N; const s32bit cmp0 = bigint_cmp(x0, N2, x1, N2); const s32bit cmp1 = bigint_cmp(y1, N2, y0, N2); clear_mem(workspace, 2*N); if(cmp0 && cmp1) { if(cmp0 > 0) bigint_sub3(z0, x0, N2, x1, N2); else bigint_sub3(z0, x1, N2, x0, N2); if(cmp1 > 0) bigint_sub3(z1, y1, N2, y0, N2); else bigint_sub3(z1, y0, N2, y1, N2); karatsuba_mul(workspace, z0, z1, N2, workspace+N); } karatsuba_mul(z0, x0, y0, N2, workspace+N); karatsuba_mul(z1, x1, y1, N2, workspace+N); const size_t blocks_of_8 = N - (N % 8); word ws_carry = 0; for(size_t j = 0; j != blocks_of_8; j += 8) ws_carry = word8_add3(workspace + N + j, z0 + j, z1 + j, ws_carry); for(size_t j = blocks_of_8; j != N; ++j) workspace[N + j] = word_add(z0[j], z1[j], &ws_carry); word z_carry = 0; for(size_t j = 0; j != blocks_of_8; j += 8) z_carry = word8_add2(z + N2 + j, workspace + N + j, z_carry); for(size_t j = blocks_of_8; j != N; ++j) z[N2 + j] = word_add(z[N2 + j], workspace[N + j], &z_carry); z[N + N2] = word_add(z[N + N2], ws_carry, &z_carry); if(z_carry) for(size_t j = 1; j != N2; ++j) if(++z[N + N2 + j]) break; if((cmp0 == cmp1) || (cmp0 == 0) || (cmp1 == 0)) bigint_add2(z + N2, 2*N-N2, workspace, N); else bigint_sub2(z + N2, 2*N-N2, workspace, N); } /* * Karatsuba Squaring Operation */ void karatsuba_sqr(word z[], const word x[], size_t N, word workspace[]) { if(N < BOTAN_KARAT_SQR_THRESHOLD || N % 2) { if(N == 6) return bigint_comba_sqr6(z, x); else if(N == 8) return bigint_comba_sqr8(z, x); else if(N == 16) return bigint_comba_sqr16(z, x); else return bigint_simple_sqr(z, x, N); } const size_t N2 = N / 2; const word* x0 = x; const word* x1 = x + N2; word* z0 = z; word* z1 = z + N; const s32bit cmp = bigint_cmp(x0, N2, x1, N2); clear_mem(workspace, 2*N); if(cmp) { if(cmp > 0) bigint_sub3(z0, x0, N2, x1, N2); else bigint_sub3(z0, x1, N2, x0, N2); karatsuba_sqr(workspace, z0, N2, workspace+N); } karatsuba_sqr(z0, x0, N2, workspace+N); karatsuba_sqr(z1, x1, N2, workspace+N); const size_t blocks_of_8 = N - (N % 8); word ws_carry = 0; for(size_t j = 0; j != blocks_of_8; j += 8) ws_carry = word8_add3(workspace + N + j, z0 + j, z1 + j, ws_carry); for(size_t j = blocks_of_8; j != N; ++j) workspace[N + j] = word_add(z0[j], z1[j], &ws_carry); word z_carry = 0; for(size_t j = 0; j != blocks_of_8; j += 8) z_carry = word8_add2(z + N2 + j, workspace + N + j, z_carry); for(size_t j = blocks_of_8; j != N; ++j) z[N2 + j] = word_add(z[N2 + j], workspace[N + j], &z_carry); z[N + N2] = word_add(z[N + N2], ws_carry, &z_carry); if(z_carry) for(size_t j = 1; j != N2; ++j) if(++z[N + N2 + j]) break; /* * This is only actually required if cmp is != 0, however * if cmp==0 then workspace[0:N] == 0 and avoiding the jump * hides a timing channel. */ bigint_sub2(z + N2, 2*N-N2, workspace, N); } /* * Pick a good size for the Karatsuba multiply */ size_t karatsuba_size(size_t z_size, size_t x_size, size_t x_sw, size_t y_size, size_t y_sw) { if(x_sw > x_size || x_sw > y_size || y_sw > x_size || y_sw > y_size) return 0; if(((x_size == x_sw) && (x_size % 2)) || ((y_size == y_sw) && (y_size % 2))) return 0; const size_t start = (x_sw > y_sw) ? x_sw : y_sw; const size_t end = (x_size < y_size) ? x_size : y_size; if(start == end) { if(start % 2) return 0; return start; } for(size_t j = start; j <= end; ++j) { if(j % 2) continue; if(2*j > z_size) return 0; if(x_sw <= j && j <= x_size && y_sw <= j && j <= y_size) { if(j % 4 == 2 && (j+2) <= x_size && (j+2) <= y_size && 2*(j+2) <= z_size) return j+2; return j; } } return 0; } /* * Pick a good size for the Karatsuba squaring */ size_t karatsuba_size(size_t z_size, size_t x_size, size_t x_sw) { if(x_sw == x_size) { if(x_sw % 2) return 0; return x_sw; } for(size_t j = x_sw; j <= x_size; ++j) { if(j % 2) continue; if(2*j > z_size) return 0; if(j % 4 == 2 && (j+2) <= x_size && 2*(j+2) <= z_size) return j+2; return j; } return 0; } } /* * Multiplication Algorithm Dispatcher */ void bigint_mul(word z[], size_t z_size, word workspace[], const word x[], size_t x_size, size_t x_sw, const word y[], size_t y_size, size_t y_sw) { if(x_sw == 1) { bigint_linmul3(z, y, y_sw, x[0]); } else if(y_sw == 1) { bigint_linmul3(z, x, x_sw, y[0]); } else if(x_sw <= 4 && x_size >= 4 && y_sw <= 4 && y_size >= 4 && z_size >= 8) { bigint_comba_mul4(z, x, y); } else if(x_sw <= 6 && x_size >= 6 && y_sw <= 6 && y_size >= 6 && z_size >= 12) { bigint_comba_mul6(z, x, y); } else if(x_sw <= 8 && x_size >= 8 && y_sw <= 8 && y_size >= 8 && z_size >= 16) { bigint_comba_mul8(z, x, y); } else if(x_sw <= 16 && x_size >= 16 && y_sw <= 16 && y_size >= 16 && z_size >= 32) { bigint_comba_mul16(z, x, y); } else if(x_sw < BOTAN_KARAT_MUL_THRESHOLD || y_sw < BOTAN_KARAT_MUL_THRESHOLD || !workspace) { bigint_simple_mul(z, x, x_sw, y, y_sw); } else { const size_t N = karatsuba_size(z_size, x_size, x_sw, y_size, y_sw); if(N) { clear_mem(workspace, 2*N); karatsuba_mul(z, x, y, N, workspace); } else bigint_simple_mul(z, x, x_sw, y, y_sw); } } /* * Squaring Algorithm Dispatcher */ void bigint_sqr(word z[], size_t z_size, word workspace[], const word x[], size_t x_size, size_t x_sw) { if(x_sw == 1) { bigint_linmul3(z, x, x_sw, x[0]); } else if(x_sw <= 4 && x_size >= 4 && z_size >= 8) { bigint_comba_sqr4(z, x); } else if(x_sw <= 6 && x_size >= 6 && z_size >= 12) { bigint_comba_sqr6(z, x); } else if(x_sw <= 8 && x_size >= 8 && z_size >= 16) { bigint_comba_sqr8(z, x); } else if(x_sw <= 16 && x_size >= 16 && z_size >= 32) { bigint_comba_sqr16(z, x); } else if(x_size < BOTAN_KARAT_SQR_THRESHOLD || !workspace) { bigint_simple_sqr(z, x, x_sw); } else { const size_t N = karatsuba_size(z_size, x_size, x_sw); if(N) { clear_mem(workspace, 2*N); karatsuba_sqr(z, x, N, workspace); } else bigint_simple_sqr(z, x, x_sw); } } } /* * MP Misc Functions * (C) 1999-2008 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { extern "C" { /* * Core Division Operation */ size_t bigint_divcore(word q, word y2, word y1, word x3, word x2, word x1) { // Compute (y2,y1) * q word y3 = 0; y1 = word_madd2(q, y1, &y3); y2 = word_madd2(q, y2, &y3); // Return (y3,y2,y1) >? (x3,x2,x1) if(y3 > x3) return 1; if(y3 < x3) return 0; if(y2 > x2) return 1; if(y2 < x2) return 0; if(y1 > x1) return 1; if(y1 < x1) return 0; return 0; } /* * Compare two MP integers */ s32bit bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size) { if(x_size < y_size) { return (-bigint_cmp(y, y_size, x, x_size)); } while(x_size > y_size) { if(x[x_size-1]) return 1; x_size--; } for(size_t j = x_size; j > 0; --j) { if(x[j-1] > y[j-1]) return 1; if(x[j-1] < y[j-1]) return -1; } return 0; } /* * Do a 2-word/1-word Division */ word bigint_divop(word n1, word n0, word d) { word high = n1 % d, quotient = 0; for(size_t j = 0; j != MP_WORD_BITS; ++j) { word high_top_bit = (high & MP_WORD_TOP_BIT); high <<= 1; high |= (n0 >> (MP_WORD_BITS-1-j)) & 1; quotient <<= 1; if(high_top_bit || high >= d) { high -= d; quotient |= 1; } } return quotient; } /* * Do a 2-word/1-word Modulo */ word bigint_modop(word n1, word n0, word d) { word z = bigint_divop(n1, n0, d); word dummy = 0; z = word_madd2(z, d, &dummy); return (n0-z); } } } /* * Montgomery Reduction * (C) 1999-2011 Jack Lloyd * 2006 Luca Piccarreta * * Distributed under the terms of the Botan license */ namespace Botan { extern "C" { /* * Montgomery Reduction Algorithm */ void bigint_monty_redc(word z[], size_t z_size, const word p[], size_t p_size, word p_dash, word ws[]) { const size_t blocks_of_8 = p_size - (p_size % 8); for(size_t i = 0; i != p_size; ++i) { word* z_i = z + i; const word y = z_i[0] * p_dash; /* bigint_linmul3(ws, p, p_size, y); bigint_add2(z_i, z_size - i, ws, p_size+1); */ word carry = 0; for(size_t j = 0; j != blocks_of_8; j += 8) carry = word8_madd3(z_i + j, p + j, y, carry); for(size_t j = blocks_of_8; j != p_size; ++j) z_i[j] = word_madd3(p[j], y, z_i[j], &carry); word z_sum = z_i[p_size] + carry; carry = (z_sum < z_i[p_size]); z_i[p_size] = z_sum; for(size_t j = p_size + 1; carry && j != z_size - i; ++j) { ++z_i[j]; carry = !z_i[j]; } } word borrow = 0; for(size_t i = 0; i != p_size; ++i) ws[i] = word_sub(z[p_size + i], p[i], &borrow); ws[p_size] = word_sub(z[p_size+p_size], 0, &borrow); copy_mem(ws + p_size + 1, z + p_size, p_size + 1); copy_mem(z, ws + borrow*(p_size+1), p_size + 1); clear_mem(z + p_size + 1, z_size - p_size - 1); } void bigint_monty_mul(word z[], size_t z_size, const word x[], size_t x_size, size_t x_sw, const word y[], size_t y_size, size_t y_sw, const word p[], size_t p_size, word p_dash, word ws[]) { bigint_mul(&z[0], z_size, &ws[0], &x[0], x_size, x_sw, &y[0], y_size, y_sw); bigint_monty_redc(&z[0], z_size, &p[0], p_size, p_dash, &ws[0]); } void bigint_monty_sqr(word z[], size_t z_size, const word x[], size_t x_size, size_t x_sw, const word p[], size_t p_size, word p_dash, word ws[]) { bigint_sqr(&z[0], z_size, &ws[0], &x[0], x_size, x_sw); bigint_monty_redc(&z[0], z_size, &p[0], p_size, p_dash, &ws[0]); } } } /* * Simple O(N^2) Multiplication and Squaring * (C) 1999-2008 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { extern "C" { /* * Simple O(N^2) Multiplication */ void bigint_simple_mul(word z[], const word x[], size_t x_size, const word y[], size_t y_size) { const size_t x_size_8 = x_size - (x_size % 8); clear_mem(z, x_size + y_size); for(size_t i = 0; i != y_size; ++i) { const word y_i = y[i]; word carry = 0; for(size_t j = 0; j != x_size_8; j += 8) carry = word8_madd3(z + i + j, x + j, y_i, carry); for(size_t j = x_size_8; j != x_size; ++j) z[i+j] = word_madd3(x[j], y_i, z[i+j], &carry); z[x_size+i] = carry; } } /* * Simple O(N^2) Squaring * * This is exactly the same algorithm as bigint_simple_mul, however * because C/C++ compilers suck at alias analysis it is good to have * the version where the compiler knows that x == y * * There is an O(n^1.5) squaring algorithm specified in Handbook of * Applied Cryptography, chapter 14 * */ void bigint_simple_sqr(word z[], const word x[], size_t x_size) { const size_t x_size_8 = x_size - (x_size % 8); clear_mem(z, 2*x_size); for(size_t i = 0; i != x_size; ++i) { const word x_i = x[i]; word carry = 0; for(size_t j = 0; j != x_size_8; j += 8) carry = word8_madd3(z + i + j, x + j, x_i, carry); for(size_t j = x_size_8; j != x_size; ++j) z[i+j] = word_madd3(x[j], x_i, z[i+j], &carry); z[x_size+i] = carry; } } } } /* * MP Shift Algorithms * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { extern "C" { /* * Single Operand Left Shift */ void bigint_shl1(word x[], size_t x_size, size_t word_shift, size_t bit_shift) { if(word_shift) { for(size_t j = 1; j != x_size + 1; ++j) x[(x_size - j) + word_shift] = x[x_size - j]; clear_mem(x, word_shift); } if(bit_shift) { word carry = 0; for(size_t j = word_shift; j != x_size + word_shift + 1; ++j) { word temp = x[j]; x[j] = (temp << bit_shift) | carry; carry = (temp >> (MP_WORD_BITS - bit_shift)); } } } /* * Single Operand Right Shift */ void bigint_shr1(word x[], size_t x_size, size_t word_shift, size_t bit_shift) { if(x_size < word_shift) { clear_mem(x, x_size); return; } if(word_shift) { copy_mem(x, x + word_shift, x_size - word_shift); clear_mem(x + x_size - word_shift, word_shift); } if(bit_shift) { word carry = 0; size_t top = x_size - word_shift; while(top >= 4) { word w = x[top-1]; x[top-1] = (w >> bit_shift) | carry; carry = (w << (MP_WORD_BITS - bit_shift)); w = x[top-2]; x[top-2] = (w >> bit_shift) | carry; carry = (w << (MP_WORD_BITS - bit_shift)); w = x[top-3]; x[top-3] = (w >> bit_shift) | carry; carry = (w << (MP_WORD_BITS - bit_shift)); w = x[top-4]; x[top-4] = (w >> bit_shift) | carry; carry = (w << (MP_WORD_BITS - bit_shift)); top -= 4; } while(top) { word w = x[top-1]; x[top-1] = (w >> bit_shift) | carry; carry = (w << (MP_WORD_BITS - bit_shift)); top--; } } } /* * Two Operand Left Shift */ void bigint_shl2(word y[], const word x[], size_t x_size, size_t word_shift, size_t bit_shift) { for(size_t j = 0; j != x_size; ++j) y[j + word_shift] = x[j]; if(bit_shift) { word carry = 0; for(size_t j = word_shift; j != x_size + word_shift + 1; ++j) { word w = y[j]; y[j] = (w << bit_shift) | carry; carry = (w >> (MP_WORD_BITS - bit_shift)); } } } /* * Two Operand Right Shift */ void bigint_shr2(word y[], const word x[], size_t x_size, size_t word_shift, size_t bit_shift) { if(x_size < word_shift) return; for(size_t j = 0; j != x_size - word_shift; ++j) y[j] = x[j + word_shift]; if(bit_shift) { word carry = 0; for(size_t j = x_size - word_shift; j > 0; --j) { word w = y[j-1]; y[j-1] = (w >> bit_shift) | carry; carry = (w << (MP_WORD_BITS - bit_shift)); } } } } } /* * DSA Parameter Generation * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> #include <memory> namespace Botan { namespace { /* * Check if this size is allowed by FIPS 186-3 */ bool fips186_3_valid_size(size_t pbits, size_t qbits) { if(qbits == 160) return (pbits == 512 || pbits == 768 || pbits == 1024); if(qbits == 224) return (pbits == 2048); if(qbits == 256) return (pbits == 2048 || pbits == 3072); return false; } } /* * Attempt DSA prime generation with given seed */ bool generate_dsa_primes(RandomNumberGenerator& rng, Algorithm_Factory& af, BigInt& p, BigInt& q, size_t pbits, size_t qbits, const MemoryRegion<byte>& seed_c) { if(!fips186_3_valid_size(pbits, qbits)) throw Invalid_Argument( "FIPS 186-3 does not allow DSA domain parameters of " + to_string(pbits) + "/" + to_string(qbits) + " bits long"); if(seed_c.size() * 8 < qbits) throw Invalid_Argument( "Generating a DSA parameter set with a " + to_string(qbits) + "long q requires a seed at least as many bits long"); std::auto_ptr<HashFunction> hash( af.make_hash_function("SHA-" + to_string(qbits))); const size_t HASH_SIZE = hash->output_length(); class Seed { public: Seed(const MemoryRegion<byte>& s) : seed(s) {} operator MemoryRegion<byte>& () { return seed; } Seed& operator++() { for(size_t j = seed.size(); j > 0; --j) if(++seed[j-1]) break; return (*this); } private: SecureVector<byte> seed; }; Seed seed(seed_c); q.binary_decode(hash->process(seed)); q.set_bit(qbits-1); q.set_bit(0); if(!check_prime(q, rng)) return false; const size_t n = (pbits-1) / (HASH_SIZE * 8), b = (pbits-1) % (HASH_SIZE * 8); BigInt X; SecureVector<byte> V(HASH_SIZE * (n+1)); for(size_t j = 0; j != 4096; ++j) { for(size_t k = 0; k <= n; ++k) { ++seed; hash->update(seed); hash->final(&V[HASH_SIZE * (n-k)]); } X.binary_decode(&V[HASH_SIZE - 1 - b/8], V.size() - (HASH_SIZE - 1 - b/8)); X.set_bit(pbits-1); p = X - (X % (2*q) - 1); if(p.bits() == pbits && check_prime(p, rng)) return true; } return false; } /* * Generate DSA Primes */ SecureVector<byte> generate_dsa_primes(RandomNumberGenerator& rng, Algorithm_Factory& af, BigInt& p, BigInt& q, size_t pbits, size_t qbits) { while(true) { SecureVector<byte> seed = rng.random_vec(qbits / 8); if(generate_dsa_primes(rng, af, p, q, pbits, qbits, seed)) return seed; } } } /* * Jacobi Function * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Calculate the Jacobi symbol */ s32bit jacobi(const BigInt& a, const BigInt& n) { if(a.is_negative()) throw Invalid_Argument("jacobi: first argument must be non-negative"); if(n.is_even() || n < 2) throw Invalid_Argument("jacobi: second argument must be odd and > 1"); BigInt x = a, y = n; s32bit J = 1; while(y > 1) { x %= y; if(x > y / 2) { x = y - x; if(y % 4 == 3) J = -J; } if(x.is_zero()) return 0; size_t shifts = low_zero_bits(x); x >>= shifts; if(shifts % 2) { word y_mod_8 = y % 8; if(y_mod_8 == 3 || y_mod_8 == 5) J = -J; } if(x % 4 == 3 && y % 4 == 3) J = -J; std::swap(x, y); } return J; } } /* * Prime Generation * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> namespace Botan { /* * Generate a random prime */ BigInt random_prime(RandomNumberGenerator& rng, size_t bits, const BigInt& coprime, size_t equiv, size_t modulo) { if(bits <= 1) throw Invalid_Argument("random_prime: Can't make a prime of " + to_string(bits) + " bits"); else if(bits == 2) return ((rng.next_byte() % 2) ? 2 : 3); else if(bits == 3) return ((rng.next_byte() % 2) ? 5 : 7); else if(bits == 4) return ((rng.next_byte() % 2) ? 11 : 13); if(coprime <= 0) throw Invalid_Argument("random_prime: coprime must be > 0"); if(modulo % 2 == 1 || modulo == 0) throw Invalid_Argument("random_prime: Invalid modulo value"); if(equiv >= modulo || equiv % 2 == 0) throw Invalid_Argument("random_prime: equiv must be < modulo, and odd"); while(true) { BigInt p(rng, bits); // Force lowest and two top bits on p.set_bit(bits - 1); p.set_bit(bits - 2); p.set_bit(0); if(p % modulo != equiv) p += (modulo - p % modulo) + equiv; const size_t sieve_size = std::min(bits / 2, PRIME_TABLE_SIZE); SecureVector<size_t> sieve(sieve_size); for(size_t j = 0; j != sieve.size(); ++j) sieve[j] = p % PRIMES[j]; size_t counter = 0; while(true) { if(counter == 4096 || p.bits() > bits) break; bool passes_sieve = true; ++counter; p += modulo; if(p.bits() > bits) break; for(size_t j = 0; j != sieve.size(); ++j) { sieve[j] = (sieve[j] + modulo) % PRIMES[j]; if(sieve[j] == 0) passes_sieve = false; } if(!passes_sieve || gcd(p - 1, coprime) != 1) continue; if(check_prime(p, rng)) return p; } } } /* * Generate a random safe prime */ BigInt random_safe_prime(RandomNumberGenerator& rng, size_t bits) { if(bits <= 64) throw Invalid_Argument("random_safe_prime: Can't make a prime of " + to_string(bits) + " bits"); BigInt p; do p = (random_prime(rng, bits - 1) << 1) + 1; while(!check_prime(p, rng)); return p; } } /* * Fused and Important MP Algorithms * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> namespace Botan { /* * Square a BigInt */ BigInt square(const BigInt& x) { const size_t x_sw = x.sig_words(); BigInt z(BigInt::Positive, round_up<size_t>(2*x_sw, 16)); SecureVector<word> workspace(z.size()); bigint_sqr(z.get_reg(), z.size(), workspace, x.data(), x.size(), x_sw); return z; } /* * Multiply-Add Operation */ BigInt mul_add(const BigInt& a, const BigInt& b, const BigInt& c) { if(c.is_negative() || c.is_zero()) throw Invalid_Argument("mul_add: Third argument must be > 0"); BigInt::Sign sign = BigInt::Positive; if(a.sign() != b.sign()) sign = BigInt::Negative; const size_t a_sw = a.sig_words(); const size_t b_sw = b.sig_words(); const size_t c_sw = c.sig_words(); BigInt r(sign, std::max(a.size() + b.size(), c_sw) + 1); SecureVector<word> workspace(r.size()); bigint_mul(r.get_reg(), r.size(), workspace, a.data(), a.size(), a_sw, b.data(), b.size(), b_sw); const size_t r_size = std::max(r.sig_words(), c_sw); bigint_add2(r.get_reg(), r_size, c.data(), c_sw); return r; } /* * Subtract-Multiply Operation */ BigInt sub_mul(const BigInt& a, const BigInt& b, const BigInt& c) { if(a.is_negative() || b.is_negative()) throw Invalid_Argument("sub_mul: First two arguments must be >= 0"); BigInt r = a; r -= b; r *= c; return r; } } /* * Number Theory Functions * (C) 1999-2011 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> namespace Botan { namespace { /* * Miller-Rabin Primality Tester */ class MillerRabin_Test { public: bool is_witness(const BigInt& nonce); MillerRabin_Test(const BigInt& num); private: BigInt n, r, n_minus_1; size_t s; Fixed_Exponent_Power_Mod pow_mod; Modular_Reducer reducer; }; /* * Miller-Rabin Test, as described in Handbook of Applied Cryptography * section 4.24 */ bool MillerRabin_Test::is_witness(const BigInt& a) { if(a < 2 || a >= n_minus_1) throw Invalid_Argument("Bad size for nonce in Miller-Rabin test"); BigInt y = pow_mod(a); if(y == 1 || y == n_minus_1) return false; for(size_t i = 1; i != s; ++i) { y = reducer.square(y); if(y == 1) // found a non-trivial square root return true; if(y == n_minus_1) // -1, trivial square root, so give up return false; } if(y != n_minus_1) // fails Fermat test return true; return false; } /* * Miller-Rabin Constructor */ MillerRabin_Test::MillerRabin_Test(const BigInt& num) { if(num.is_even() || num < 3) throw Invalid_Argument("MillerRabin_Test: Invalid number for testing"); n = num; n_minus_1 = n - 1; s = low_zero_bits(n_minus_1); r = n_minus_1 >> s; pow_mod = Fixed_Exponent_Power_Mod(r, n); reducer = Modular_Reducer(n); } /* * Miller-Rabin Iterations */ size_t miller_rabin_test_iterations(size_t bits, size_t level) { struct mapping { size_t bits; size_t verify_iter; size_t check_iter; }; static const mapping tests[] = { { 50, 55, 25 }, { 100, 38, 22 }, { 160, 32, 18 }, { 163, 31, 17 }, { 168, 30, 16 }, { 177, 29, 16 }, { 181, 28, 15 }, { 185, 27, 15 }, { 190, 26, 15 }, { 195, 25, 14 }, { 201, 24, 14 }, { 208, 23, 14 }, { 215, 22, 13 }, { 222, 21, 13 }, { 231, 20, 13 }, { 241, 19, 12 }, { 252, 18, 12 }, { 264, 17, 12 }, { 278, 16, 11 }, { 294, 15, 10 }, { 313, 14, 9 }, { 334, 13, 8 }, { 360, 12, 8 }, { 392, 11, 7 }, { 430, 10, 7 }, { 479, 9, 6 }, { 542, 8, 6 }, { 626, 7, 5 }, { 746, 6, 4 }, { 926, 5, 3 }, { 1232, 4, 2 }, { 1853, 3, 2 }, { 0, 0, 0 } }; for(size_t i = 0; tests[i].bits; ++i) { if(bits <= tests[i].bits) { if(level >= 2) return tests[i].verify_iter; else if(level == 1) return tests[i].check_iter; else if(level == 0) return std::max<size_t>(tests[i].check_iter / 4, 1); } } return level > 0 ? 2 : 1; // for large inputs } } /* * Return the number of 0 bits at the end of n */ size_t low_zero_bits(const BigInt& n) { size_t low_zero = 0; if(n.is_positive() && n.is_nonzero()) { for(size_t i = 0; i != n.size(); ++i) { word x = n[i]; if(x) { low_zero += ctz(x); break; } else low_zero += BOTAN_MP_WORD_BITS; } } return low_zero; } /* * Calculate the GCD */ BigInt gcd(const BigInt& a, const BigInt& b) { if(a.is_zero() || b.is_zero()) return 0; if(a == 1 || b == 1) return 1; BigInt x = a, y = b; x.set_sign(BigInt::Positive); y.set_sign(BigInt::Positive); size_t shift = std::min(low_zero_bits(x), low_zero_bits(y)); x >>= shift; y >>= shift; while(x.is_nonzero()) { x >>= low_zero_bits(x); y >>= low_zero_bits(y); if(x >= y) { x -= y; x >>= 1; } else { y -= x; y >>= 1; } } return (y << shift); } /* * Calculate the LCM */ BigInt lcm(const BigInt& a, const BigInt& b) { return ((a * b) / gcd(a, b)); } /* * Find the Modular Inverse */ BigInt inverse_mod(const BigInt& n, const BigInt& mod) { if(mod.is_zero()) throw BigInt::DivideByZero(); if(mod.is_negative() || n.is_negative()) throw Invalid_Argument("inverse_mod: arguments must be non-negative"); if(n.is_zero() || (n.is_even() && mod.is_even())) return 0; BigInt x = mod, y = n, u = mod, v = n; BigInt A = 1, B = 0, C = 0, D = 1; while(u.is_nonzero()) { size_t zero_bits = low_zero_bits(u); u >>= zero_bits; for(size_t i = 0; i != zero_bits; ++i) { if(A.is_odd() || B.is_odd()) { A += y; B -= x; } A >>= 1; B >>= 1; } zero_bits = low_zero_bits(v); v >>= zero_bits; for(size_t i = 0; i != zero_bits; ++i) { if(C.is_odd() || D.is_odd()) { C += y; D -= x; } C >>= 1; D >>= 1; } if(u >= v) { u -= v; A -= C; B -= D; } else { v -= u; C -= A; D -= B; } } if(v != 1) return 0; while(D.is_negative()) D += mod; while(D >= mod) D -= mod; return D; } /* * Modular Exponentiation */ BigInt power_mod(const BigInt& base, const BigInt& exp, const BigInt& mod) { Power_Mod pow_mod(mod); pow_mod.set_base(base); pow_mod.set_exponent(exp); return pow_mod.execute(); } /* * Test for primaility using Miller-Rabin */ bool primality_test(const BigInt& n, RandomNumberGenerator& rng, size_t level) { const size_t PREF_NONCE_BITS = 64; if(n == 2) return true; if(n <= 1 || n.is_even()) return false; // Fast path testing for small numbers (<= 65521) if(n <= PRIMES[PRIME_TABLE_SIZE-1]) { const word num = n.word_at(0); for(size_t i = 0; PRIMES[i]; ++i) { if(num == PRIMES[i]) return true; if(num < PRIMES[i]) return false; } return false; } if(level > 2) level = 2; const size_t NONCE_BITS = std::min(n.bits() - 2, PREF_NONCE_BITS); MillerRabin_Test mr(n); const size_t tests = miller_rabin_test_iterations(n.bits(), level); BigInt nonce; for(size_t i = 0; i != tests; ++i) { while(nonce < 2 || nonce >= (n-1)) nonce.randomize(rng, NONCE_BITS); if(mr.is_witness(nonce)) return false; } return true; } } /* * Modular Exponentiation Proxy * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Power_Mod Constructor */ Power_Mod::Power_Mod(const BigInt& n, Usage_Hints hints) { core = 0; set_modulus(n, hints); } /* * Power_Mod Copy Constructor */ Power_Mod::Power_Mod(const Power_Mod& other) { core = 0; if(other.core) core = other.core->copy(); } /* * Power_Mod Assignment Operator */ Power_Mod& Power_Mod::operator=(const Power_Mod& other) { delete core; core = 0; if(other.core) core = other.core->copy(); return (*this); } /* * Power_Mod Destructor */ Power_Mod::~Power_Mod() { delete core; } /* * Set the modulus */ void Power_Mod::set_modulus(const BigInt& n, Usage_Hints hints) const { delete core; core = 0; if(n != 0) { Algorithm_Factory::Engine_Iterator i(global_state().algorithm_factory()); while(const Engine* engine = i.next()) { core = engine->mod_exp(n, hints); if(core) break; } if(!core) throw Lookup_Error("Power_Mod: Unable to find a working engine"); } } /* * Set the base */ void Power_Mod::set_base(const BigInt& b) const { if(b.is_zero() || b.is_negative()) throw Invalid_Argument("Power_Mod::set_base: arg must be > 0"); if(!core) throw Internal_Error("Power_Mod::set_base: core was NULL"); core->set_base(b); } /* * Set the exponent */ void Power_Mod::set_exponent(const BigInt& e) const { if(e.is_negative()) throw Invalid_Argument("Power_Mod::set_exponent: arg must be > 0"); if(!core) throw Internal_Error("Power_Mod::set_exponent: core was NULL"); core->set_exponent(e); } /* * Compute the result */ BigInt Power_Mod::execute() const { if(!core) throw Internal_Error("Power_Mod::execute: core was NULL"); return core->execute(); } /* * Try to choose a good window size */ size_t Power_Mod::window_bits(size_t exp_bits, size_t, Power_Mod::Usage_Hints hints) { static const size_t wsize[][2] = { { 1434, 7 }, { 539, 6 }, { 197, 4 }, { 70, 3 }, { 25, 2 }, { 0, 0 } }; size_t window_bits = 1; if(exp_bits) { for(size_t j = 0; wsize[j][0]; ++j) { if(exp_bits >= wsize[j][0]) { window_bits += wsize[j][1]; break; } } } if(hints & Power_Mod::BASE_IS_FIXED) window_bits += 2; if(hints & Power_Mod::EXP_IS_LARGE) ++window_bits; return window_bits; } namespace { /* * Choose potentially useful hints */ Power_Mod::Usage_Hints choose_base_hints(const BigInt& b, const BigInt& n) { if(b == 2) return Power_Mod::Usage_Hints(Power_Mod::BASE_IS_2 | Power_Mod::BASE_IS_SMALL); const size_t b_bits = b.bits(); const size_t n_bits = n.bits(); if(b_bits < n_bits / 32) return Power_Mod::BASE_IS_SMALL; if(b_bits > n_bits / 4) return Power_Mod::BASE_IS_LARGE; return Power_Mod::NO_HINTS; } /* * Choose potentially useful hints */ Power_Mod::Usage_Hints choose_exp_hints(const BigInt& e, const BigInt& n) { const size_t e_bits = e.bits(); const size_t n_bits = n.bits(); if(e_bits < n_bits / 32) return Power_Mod::BASE_IS_SMALL; if(e_bits > n_bits / 4) return Power_Mod::BASE_IS_LARGE; return Power_Mod::NO_HINTS; } } /* * Fixed_Exponent_Power_Mod Constructor */ Fixed_Exponent_Power_Mod::Fixed_Exponent_Power_Mod(const BigInt& e, const BigInt& n, Usage_Hints hints) : Power_Mod(n, Usage_Hints(hints | EXP_IS_FIXED | choose_exp_hints(e, n))) { set_exponent(e); } /* * Fixed_Base_Power_Mod Constructor */ Fixed_Base_Power_Mod::Fixed_Base_Power_Mod(const BigInt& b, const BigInt& n, Usage_Hints hints) : Power_Mod(n, Usage_Hints(hints | BASE_IS_FIXED | choose_base_hints(b, n))) { set_base(b); } } /* * Fixed Window Exponentiation * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <vector> namespace Botan { /* * Set the exponent */ void Fixed_Window_Exponentiator::set_exponent(const BigInt& e) { exp = e; } /* * Set the base */ void Fixed_Window_Exponentiator::set_base(const BigInt& base) { window_bits = Power_Mod::window_bits(exp.bits(), base.bits(), hints); g.resize((1 << window_bits)); g[0] = 1; g[1] = base; for(size_t i = 2; i != g.size(); ++i) g[i] = reducer.multiply(g[i-1], g[0]); } /* * Compute the result */ BigInt Fixed_Window_Exponentiator::execute() const { const size_t exp_nibbles = (exp.bits() + window_bits - 1) / window_bits; BigInt x = 1; for(size_t i = exp_nibbles; i > 0; --i) { for(size_t j = 0; j != window_bits; ++j) x = reducer.square(x); const u32bit nibble = exp.get_substring(window_bits*(i-1), window_bits); x = reducer.multiply(x, g[nibble]); } return x; } /* * Fixed_Window_Exponentiator Constructor */ Fixed_Window_Exponentiator::Fixed_Window_Exponentiator(const BigInt& n, Power_Mod::Usage_Hints hints) { reducer = Modular_Reducer(n); this->hints = hints; window_bits = 0; } } /* * Montgomery Exponentiation * (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Set the exponent */ void Montgomery_Exponentiator::set_exponent(const BigInt& exp) { this->exp = exp; exp_bits = exp.bits(); } /* * Set the base */ void Montgomery_Exponentiator::set_base(const BigInt& base) { window_bits = Power_Mod::window_bits(exp.bits(), base.bits(), hints); g.resize((1 << window_bits)); SecureVector<word> z(2 * (mod_words + 1)); SecureVector<word> workspace(z.size()); g[0] = 1; bigint_monty_mul(&z[0], z.size(), g[0].data(), g[0].size(), g[0].sig_words(), R2.data(), R2.size(), R2.sig_words(), modulus.data(), mod_words, mod_prime, &workspace[0]); g[0].assign(&z[0], mod_words + 1); g[1] = (base >= modulus) ? (base % modulus) : base; bigint_monty_mul(&z[0], z.size(), g[1].data(), g[1].size(), g[1].sig_words(), R2.data(), R2.size(), R2.sig_words(), modulus.data(), mod_words, mod_prime, &workspace[0]); g[1].assign(&z[0], mod_words + 1); const BigInt& x = g[1]; const size_t x_sig = x.sig_words(); for(size_t i = 1; i != g.size(); ++i) { const BigInt& y = g[i-1]; const size_t y_sig = y.sig_words(); zeroise(z); bigint_monty_mul(&z[0], z.size(), x.data(), x.size(), x_sig, y.data(), y.size(), y_sig, modulus.data(), mod_words, mod_prime, &workspace[0]); g[i].assign(&z[0], mod_words + 1); } } /* * Compute the result */ BigInt Montgomery_Exponentiator::execute() const { const size_t exp_nibbles = (exp_bits + window_bits - 1) / window_bits; BigInt x = R_mod; SecureVector<word> z(2 * (mod_words + 1)); SecureVector<word> workspace(2 * (mod_words + 1)); for(size_t i = exp_nibbles; i > 0; --i) { for(size_t k = 0; k != window_bits; ++k) { zeroise(z); bigint_monty_sqr(&z[0], z.size(), x.data(), x.size(), x.sig_words(), modulus.data(), mod_words, mod_prime, &workspace[0]); x.assign(&z[0], mod_words + 1); } const u32bit nibble = exp.get_substring(window_bits*(i-1), window_bits); const BigInt& y = g[nibble]; zeroise(z); bigint_monty_mul(&z[0], z.size(), x.data(), x.size(), x.sig_words(), y.data(), y.size(), y.sig_words(), modulus.data(), mod_words, mod_prime, &workspace[0]); x.assign(&z[0], mod_words + 1); } x.get_reg().resize(2*mod_words+1); bigint_monty_redc(&x[0], x.size(), modulus.data(), mod_words, mod_prime, &workspace[0]); x.get_reg().resize(mod_words+1); return x; } /* * Montgomery_Exponentiator Constructor */ Montgomery_Exponentiator::Montgomery_Exponentiator(const BigInt& mod, Power_Mod::Usage_Hints hints) { // Montgomery reduction only works for positive odd moduli if(!mod.is_positive() || mod.is_even()) throw Invalid_Argument("Montgomery_Exponentiator: invalid modulus"); window_bits = 0; this->hints = hints; modulus = mod; mod_words = modulus.sig_words(); BigInt r(BigInt::Power2, mod_words * BOTAN_MP_WORD_BITS); mod_prime = (((r * inverse_mod(r, mod)) - 1) / mod).word_at(0); R_mod = r % modulus; R2 = (R_mod * R_mod) % modulus; } } /* * Small Primes Table * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { const u16bit PRIMES[PRIME_TABLE_SIZE+1] = { 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 5003, 5009, 5011, 5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087, 5099, 5101, 5107, 5113, 5119, 5147, 5153, 5167, 5171, 5179, 5189, 5197, 5209, 5227, 5231, 5233, 5237, 5261, 5273, 5279, 5281, 5297, 5303, 5309, 5323, 5333, 5347, 5351, 5381, 5387, 5393, 5399, 5407, 5413, 5417, 5419, 5431, 5437, 5441, 5443, 5449, 5471, 5477, 5479, 5483, 5501, 5503, 5507, 5519, 5521, 5527, 5531, 5557, 5563, 5569, 5573, 5581, 5591, 5623, 5639, 5641, 5647, 5651, 5653, 5657, 5659, 5669, 5683, 5689, 5693, 5701, 5711, 5717, 5737, 5741, 5743, 5749, 5779, 5783, 5791, 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849, 5851, 5857, 5861, 5867, 5869, 5879, 5881, 5897, 5903, 5923, 5927, 5939, 5953, 5981, 5987, 6007, 6011, 6029, 6037, 6043, 6047, 6053, 6067, 6073, 6079, 6089, 6091, 6101, 6113, 6121, 6131, 6133, 6143, 6151, 6163, 6173, 6197, 6199, 6203, 6211, 6217, 6221, 6229, 6247, 6257, 6263, 6269, 6271, 6277, 6287, 6299, 6301, 6311, 6317, 6323, 6329, 6337, 6343, 6353, 6359, 6361, 6367, 6373, 6379, 6389, 6397, 6421, 6427, 6449, 6451, 6469, 6473, 6481, 6491, 6521, 6529, 6547, 6551, 6553, 6563, 6569, 6571, 6577, 6581, 6599, 6607, 6619, 6637, 6653, 6659, 6661, 6673, 6679, 6689, 6691, 6701, 6703, 6709, 6719, 6733, 6737, 6761, 6763, 6779, 6781, 6791, 6793, 6803, 6823, 6827, 6829, 6833, 6841, 6857, 6863, 6869, 6871, 6883, 6899, 6907, 6911, 6917, 6947, 6949, 6959, 6961, 6967, 6971, 6977, 6983, 6991, 6997, 7001, 7013, 7019, 7027, 7039, 7043, 7057, 7069, 7079, 7103, 7109, 7121, 7127, 7129, 7151, 7159, 7177, 7187, 7193, 7207, 7211, 7213, 7219, 7229, 7237, 7243, 7247, 7253, 7283, 7297, 7307, 7309, 7321, 7331, 7333, 7349, 7351, 7369, 7393, 7411, 7417, 7433, 7451, 7457, 7459, 7477, 7481, 7487, 7489, 7499, 7507, 7517, 7523, 7529, 7537, 7541, 7547, 7549, 7559, 7561, 7573, 7577, 7583, 7589, 7591, 7603, 7607, 7621, 7639, 7643, 7649, 7669, 7673, 7681, 7687, 7691, 7699, 7703, 7717, 7723, 7727, 7741, 7753, 7757, 7759, 7789, 7793, 7817, 7823, 7829, 7841, 7853, 7867, 7873, 7877, 7879, 7883, 7901, 7907, 7919, 7927, 7933, 7937, 7949, 7951, 7963, 7993, 8009, 8011, 8017, 8039, 8053, 8059, 8069, 8081, 8087, 8089, 8093, 8101, 8111, 8117, 8123, 8147, 8161, 8167, 8171, 8179, 8191, 8209, 8219, 8221, 8231, 8233, 8237, 8243, 8263, 8269, 8273, 8287, 8291, 8293, 8297, 8311, 8317, 8329, 8353, 8363, 8369, 8377, 8387, 8389, 8419, 8423, 8429, 8431, 8443, 8447, 8461, 8467, 8501, 8513, 8521, 8527, 8537, 8539, 8543, 8563, 8573, 8581, 8597, 8599, 8609, 8623, 8627, 8629, 8641, 8647, 8663, 8669, 8677, 8681, 8689, 8693, 8699, 8707, 8713, 8719, 8731, 8737, 8741, 8747, 8753, 8761, 8779, 8783, 8803, 8807, 8819, 8821, 8831, 8837, 8839, 8849, 8861, 8863, 8867, 8887, 8893, 8923, 8929, 8933, 8941, 8951, 8963, 8969, 8971, 8999, 9001, 9007, 9011, 9013, 9029, 9041, 9043, 9049, 9059, 9067, 9091, 9103, 9109, 9127, 9133, 9137, 9151, 9157, 9161, 9173, 9181, 9187, 9199, 9203, 9209, 9221, 9227, 9239, 9241, 9257, 9277, 9281, 9283, 9293, 9311, 9319, 9323, 9337, 9341, 9343, 9349, 9371, 9377, 9391, 9397, 9403, 9413, 9419, 9421, 9431, 9433, 9437, 9439, 9461, 9463, 9467, 9473, 9479, 9491, 9497, 9511, 9521, 9533, 9539, 9547, 9551, 9587, 9601, 9613, 9619, 9623, 9629, 9631, 9643, 9649, 9661, 9677, 9679, 9689, 9697, 9719, 9721, 9733, 9739, 9743, 9749, 9767, 9769, 9781, 9787, 9791, 9803, 9811, 9817, 9829, 9833, 9839, 9851, 9857, 9859, 9871, 9883, 9887, 9901, 9907, 9923, 9929, 9931, 9941, 9949, 9967, 9973, 10007, 10009, 10037, 10039, 10061, 10067, 10069, 10079, 10091, 10093, 10099, 10103, 10111, 10133, 10139, 10141, 10151, 10159, 10163, 10169, 10177, 10181, 10193, 10211, 10223, 10243, 10247, 10253, 10259, 10267, 10271, 10273, 10289, 10301, 10303, 10313, 10321, 10331, 10333, 10337, 10343, 10357, 10369, 10391, 10399, 10427, 10429, 10433, 10453, 10457, 10459, 10463, 10477, 10487, 10499, 10501, 10513, 10529, 10531, 10559, 10567, 10589, 10597, 10601, 10607, 10613, 10627, 10631, 10639, 10651, 10657, 10663, 10667, 10687, 10691, 10709, 10711, 10723, 10729, 10733, 10739, 10753, 10771, 10781, 10789, 10799, 10831, 10837, 10847, 10853, 10859, 10861, 10867, 10883, 10889, 10891, 10903, 10909, 10937, 10939, 10949, 10957, 10973, 10979, 10987, 10993, 11003, 11027, 11047, 11057, 11059, 11069, 11071, 11083, 11087, 11093, 11113, 11117, 11119, 11131, 11149, 11159, 11161, 11171, 11173, 11177, 11197, 11213, 11239, 11243, 11251, 11257, 11261, 11273, 11279, 11287, 11299, 11311, 11317, 11321, 11329, 11351, 11353, 11369, 11383, 11393, 11399, 11411, 11423, 11437, 11443, 11447, 11467, 11471, 11483, 11489, 11491, 11497, 11503, 11519, 11527, 11549, 11551, 11579, 11587, 11593, 11597, 11617, 11621, 11633, 11657, 11677, 11681, 11689, 11699, 11701, 11717, 11719, 11731, 11743, 11777, 11779, 11783, 11789, 11801, 11807, 11813, 11821, 11827, 11831, 11833, 11839, 11863, 11867, 11887, 11897, 11903, 11909, 11923, 11927, 11933, 11939, 11941, 11953, 11959, 11969, 11971, 11981, 11987, 12007, 12011, 12037, 12041, 12043, 12049, 12071, 12073, 12097, 12101, 12107, 12109, 12113, 12119, 12143, 12149, 12157, 12161, 12163, 12197, 12203, 12211, 12227, 12239, 12241, 12251, 12253, 12263, 12269, 12277, 12281, 12289, 12301, 12323, 12329, 12343, 12347, 12373, 12377, 12379, 12391, 12401, 12409, 12413, 12421, 12433, 12437, 12451, 12457, 12473, 12479, 12487, 12491, 12497, 12503, 12511, 12517, 12527, 12539, 12541, 12547, 12553, 12569, 12577, 12583, 12589, 12601, 12611, 12613, 12619, 12637, 12641, 12647, 12653, 12659, 12671, 12689, 12697, 12703, 12713, 12721, 12739, 12743, 12757, 12763, 12781, 12791, 12799, 12809, 12821, 12823, 12829, 12841, 12853, 12889, 12893, 12899, 12907, 12911, 12917, 12919, 12923, 12941, 12953, 12959, 12967, 12973, 12979, 12983, 13001, 13003, 13007, 13009, 13033, 13037, 13043, 13049, 13063, 13093, 13099, 13103, 13109, 13121, 13127, 13147, 13151, 13159, 13163, 13171, 13177, 13183, 13187, 13217, 13219, 13229, 13241, 13249, 13259, 13267, 13291, 13297, 13309, 13313, 13327, 13331, 13337, 13339, 13367, 13381, 13397, 13399, 13411, 13417, 13421, 13441, 13451, 13457, 13463, 13469, 13477, 13487, 13499, 13513, 13523, 13537, 13553, 13567, 13577, 13591, 13597, 13613, 13619, 13627, 13633, 13649, 13669, 13679, 13681, 13687, 13691, 13693, 13697, 13709, 13711, 13721, 13723, 13729, 13751, 13757, 13759, 13763, 13781, 13789, 13799, 13807, 13829, 13831, 13841, 13859, 13873, 13877, 13879, 13883, 13901, 13903, 13907, 13913, 13921, 13931, 13933, 13963, 13967, 13997, 13999, 14009, 14011, 14029, 14033, 14051, 14057, 14071, 14081, 14083, 14087, 14107, 14143, 14149, 14153, 14159, 14173, 14177, 14197, 14207, 14221, 14243, 14249, 14251, 14281, 14293, 14303, 14321, 14323, 14327, 14341, 14347, 14369, 14387, 14389, 14401, 14407, 14411, 14419, 14423, 14431, 14437, 14447, 14449, 14461, 14479, 14489, 14503, 14519, 14533, 14537, 14543, 14549, 14551, 14557, 14561, 14563, 14591, 14593, 14621, 14627, 14629, 14633, 14639, 14653, 14657, 14669, 14683, 14699, 14713, 14717, 14723, 14731, 14737, 14741, 14747, 14753, 14759, 14767, 14771, 14779, 14783, 14797, 14813, 14821, 14827, 14831, 14843, 14851, 14867, 14869, 14879, 14887, 14891, 14897, 14923, 14929, 14939, 14947, 14951, 14957, 14969, 14983, 15013, 15017, 15031, 15053, 15061, 15073, 15077, 15083, 15091, 15101, 15107, 15121, 15131, 15137, 15139, 15149, 15161, 15173, 15187, 15193, 15199, 15217, 15227, 15233, 15241, 15259, 15263, 15269, 15271, 15277, 15287, 15289, 15299, 15307, 15313, 15319, 15329, 15331, 15349, 15359, 15361, 15373, 15377, 15383, 15391, 15401, 15413, 15427, 15439, 15443, 15451, 15461, 15467, 15473, 15493, 15497, 15511, 15527, 15541, 15551, 15559, 15569, 15581, 15583, 15601, 15607, 15619, 15629, 15641, 15643, 15647, 15649, 15661, 15667, 15671, 15679, 15683, 15727, 15731, 15733, 15737, 15739, 15749, 15761, 15767, 15773, 15787, 15791, 15797, 15803, 15809, 15817, 15823, 15859, 15877, 15881, 15887, 15889, 15901, 15907, 15913, 15919, 15923, 15937, 15959, 15971, 15973, 15991, 16001, 16007, 16033, 16057, 16061, 16063, 16067, 16069, 16073, 16087, 16091, 16097, 16103, 16111, 16127, 16139, 16141, 16183, 16187, 16189, 16193, 16217, 16223, 16229, 16231, 16249, 16253, 16267, 16273, 16301, 16319, 16333, 16339, 16349, 16361, 16363, 16369, 16381, 16411, 16417, 16421, 16427, 16433, 16447, 16451, 16453, 16477, 16481, 16487, 16493, 16519, 16529, 16547, 16553, 16561, 16567, 16573, 16603, 16607, 16619, 16631, 16633, 16649, 16651, 16657, 16661, 16673, 16691, 16693, 16699, 16703, 16729, 16741, 16747, 16759, 16763, 16787, 16811, 16823, 16829, 16831, 16843, 16871, 16879, 16883, 16889, 16901, 16903, 16921, 16927, 16931, 16937, 16943, 16963, 16979, 16981, 16987, 16993, 17011, 17021, 17027, 17029, 17033, 17041, 17047, 17053, 17077, 17093, 17099, 17107, 17117, 17123, 17137, 17159, 17167, 17183, 17189, 17191, 17203, 17207, 17209, 17231, 17239, 17257, 17291, 17293, 17299, 17317, 17321, 17327, 17333, 17341, 17351, 17359, 17377, 17383, 17387, 17389, 17393, 17401, 17417, 17419, 17431, 17443, 17449, 17467, 17471, 17477, 17483, 17489, 17491, 17497, 17509, 17519, 17539, 17551, 17569, 17573, 17579, 17581, 17597, 17599, 17609, 17623, 17627, 17657, 17659, 17669, 17681, 17683, 17707, 17713, 17729, 17737, 17747, 17749, 17761, 17783, 17789, 17791, 17807, 17827, 17837, 17839, 17851, 17863, 17881, 17891, 17903, 17909, 17911, 17921, 17923, 17929, 17939, 17957, 17959, 17971, 17977, 17981, 17987, 17989, 18013, 18041, 18043, 18047, 18049, 18059, 18061, 18077, 18089, 18097, 18119, 18121, 18127, 18131, 18133, 18143, 18149, 18169, 18181, 18191, 18199, 18211, 18217, 18223, 18229, 18233, 18251, 18253, 18257, 18269, 18287, 18289, 18301, 18307, 18311, 18313, 18329, 18341, 18353, 18367, 18371, 18379, 18397, 18401, 18413, 18427, 18433, 18439, 18443, 18451, 18457, 18461, 18481, 18493, 18503, 18517, 18521, 18523, 18539, 18541, 18553, 18583, 18587, 18593, 18617, 18637, 18661, 18671, 18679, 18691, 18701, 18713, 18719, 18731, 18743, 18749, 18757, 18773, 18787, 18793, 18797, 18803, 18839, 18859, 18869, 18899, 18911, 18913, 18917, 18919, 18947, 18959, 18973, 18979, 19001, 19009, 19013, 19031, 19037, 19051, 19069, 19073, 19079, 19081, 19087, 19121, 19139, 19141, 19157, 19163, 19181, 19183, 19207, 19211, 19213, 19219, 19231, 19237, 19249, 19259, 19267, 19273, 19289, 19301, 19309, 19319, 19333, 19373, 19379, 19381, 19387, 19391, 19403, 19417, 19421, 19423, 19427, 19429, 19433, 19441, 19447, 19457, 19463, 19469, 19471, 19477, 19483, 19489, 19501, 19507, 19531, 19541, 19543, 19553, 19559, 19571, 19577, 19583, 19597, 19603, 19609, 19661, 19681, 19687, 19697, 19699, 19709, 19717, 19727, 19739, 19751, 19753, 19759, 19763, 19777, 19793, 19801, 19813, 19819, 19841, 19843, 19853, 19861, 19867, 19889, 19891, 19913, 19919, 19927, 19937, 19949, 19961, 19963, 19973, 19979, 19991, 19993, 19997, 20011, 20021, 20023, 20029, 20047, 20051, 20063, 20071, 20089, 20101, 20107, 20113, 20117, 20123, 20129, 20143, 20147, 20149, 20161, 20173, 20177, 20183, 20201, 20219, 20231, 20233, 20249, 20261, 20269, 20287, 20297, 20323, 20327, 20333, 20341, 20347, 20353, 20357, 20359, 20369, 20389, 20393, 20399, 20407, 20411, 20431, 20441, 20443, 20477, 20479, 20483, 20507, 20509, 20521, 20533, 20543, 20549, 20551, 20563, 20593, 20599, 20611, 20627, 20639, 20641, 20663, 20681, 20693, 20707, 20717, 20719, 20731, 20743, 20747, 20749, 20753, 20759, 20771, 20773, 20789, 20807, 20809, 20849, 20857, 20873, 20879, 20887, 20897, 20899, 20903, 20921, 20929, 20939, 20947, 20959, 20963, 20981, 20983, 21001, 21011, 21013, 21017, 21019, 21023, 21031, 21059, 21061, 21067, 21089, 21101, 21107, 21121, 21139, 21143, 21149, 21157, 21163, 21169, 21179, 21187, 21191, 21193, 21211, 21221, 21227, 21247, 21269, 21277, 21283, 21313, 21317, 21319, 21323, 21341, 21347, 21377, 21379, 21383, 21391, 21397, 21401, 21407, 21419, 21433, 21467, 21481, 21487, 21491, 21493, 21499, 21503, 21517, 21521, 21523, 21529, 21557, 21559, 21563, 21569, 21577, 21587, 21589, 21599, 21601, 21611, 21613, 21617, 21647, 21649, 21661, 21673, 21683, 21701, 21713, 21727, 21737, 21739, 21751, 21757, 21767, 21773, 21787, 21799, 21803, 21817, 21821, 21839, 21841, 21851, 21859, 21863, 21871, 21881, 21893, 21911, 21929, 21937, 21943, 21961, 21977, 21991, 21997, 22003, 22013, 22027, 22031, 22037, 22039, 22051, 22063, 22067, 22073, 22079, 22091, 22093, 22109, 22111, 22123, 22129, 22133, 22147, 22153, 22157, 22159, 22171, 22189, 22193, 22229, 22247, 22259, 22271, 22273, 22277, 22279, 22283, 22291, 22303, 22307, 22343, 22349, 22367, 22369, 22381, 22391, 22397, 22409, 22433, 22441, 22447, 22453, 22469, 22481, 22483, 22501, 22511, 22531, 22541, 22543, 22549, 22567, 22571, 22573, 22613, 22619, 22621, 22637, 22639, 22643, 22651, 22669, 22679, 22691, 22697, 22699, 22709, 22717, 22721, 22727, 22739, 22741, 22751, 22769, 22777, 22783, 22787, 22807, 22811, 22817, 22853, 22859, 22861, 22871, 22877, 22901, 22907, 22921, 22937, 22943, 22961, 22963, 22973, 22993, 23003, 23011, 23017, 23021, 23027, 23029, 23039, 23041, 23053, 23057, 23059, 23063, 23071, 23081, 23087, 23099, 23117, 23131, 23143, 23159, 23167, 23173, 23189, 23197, 23201, 23203, 23209, 23227, 23251, 23269, 23279, 23291, 23293, 23297, 23311, 23321, 23327, 23333, 23339, 23357, 23369, 23371, 23399, 23417, 23431, 23447, 23459, 23473, 23497, 23509, 23531, 23537, 23539, 23549, 23557, 23561, 23563, 23567, 23581, 23593, 23599, 23603, 23609, 23623, 23627, 23629, 23633, 23663, 23669, 23671, 23677, 23687, 23689, 23719, 23741, 23743, 23747, 23753, 23761, 23767, 23773, 23789, 23801, 23813, 23819, 23827, 23831, 23833, 23857, 23869, 23873, 23879, 23887, 23893, 23899, 23909, 23911, 23917, 23929, 23957, 23971, 23977, 23981, 23993, 24001, 24007, 24019, 24023, 24029, 24043, 24049, 24061, 24071, 24077, 24083, 24091, 24097, 24103, 24107, 24109, 24113, 24121, 24133, 24137, 24151, 24169, 24179, 24181, 24197, 24203, 24223, 24229, 24239, 24247, 24251, 24281, 24317, 24329, 24337, 24359, 24371, 24373, 24379, 24391, 24407, 24413, 24419, 24421, 24439, 24443, 24469, 24473, 24481, 24499, 24509, 24517, 24527, 24533, 24547, 24551, 24571, 24593, 24611, 24623, 24631, 24659, 24671, 24677, 24683, 24691, 24697, 24709, 24733, 24749, 24763, 24767, 24781, 24793, 24799, 24809, 24821, 24841, 24847, 24851, 24859, 24877, 24889, 24907, 24917, 24919, 24923, 24943, 24953, 24967, 24971, 24977, 24979, 24989, 25013, 25031, 25033, 25037, 25057, 25073, 25087, 25097, 25111, 25117, 25121, 25127, 25147, 25153, 25163, 25169, 25171, 25183, 25189, 25219, 25229, 25237, 25243, 25247, 25253, 25261, 25301, 25303, 25307, 25309, 25321, 25339, 25343, 25349, 25357, 25367, 25373, 25391, 25409, 25411, 25423, 25439, 25447, 25453, 25457, 25463, 25469, 25471, 25523, 25537, 25541, 25561, 25577, 25579, 25583, 25589, 25601, 25603, 25609, 25621, 25633, 25639, 25643, 25657, 25667, 25673, 25679, 25693, 25703, 25717, 25733, 25741, 25747, 25759, 25763, 25771, 25793, 25799, 25801, 25819, 25841, 25847, 25849, 25867, 25873, 25889, 25903, 25913, 25919, 25931, 25933, 25939, 25943, 25951, 25969, 25981, 25997, 25999, 26003, 26017, 26021, 26029, 26041, 26053, 26083, 26099, 26107, 26111, 26113, 26119, 26141, 26153, 26161, 26171, 26177, 26183, 26189, 26203, 26209, 26227, 26237, 26249, 26251, 26261, 26263, 26267, 26293, 26297, 26309, 26317, 26321, 26339, 26347, 26357, 26371, 26387, 26393, 26399, 26407, 26417, 26423, 26431, 26437, 26449, 26459, 26479, 26489, 26497, 26501, 26513, 26539, 26557, 26561, 26573, 26591, 26597, 26627, 26633, 26641, 26647, 26669, 26681, 26683, 26687, 26693, 26699, 26701, 26711, 26713, 26717, 26723, 26729, 26731, 26737, 26759, 26777, 26783, 26801, 26813, 26821, 26833, 26839, 26849, 26861, 26863, 26879, 26881, 26891, 26893, 26903, 26921, 26927, 26947, 26951, 26953, 26959, 26981, 26987, 26993, 27011, 27017, 27031, 27043, 27059, 27061, 27067, 27073, 27077, 27091, 27103, 27107, 27109, 27127, 27143, 27179, 27191, 27197, 27211, 27239, 27241, 27253, 27259, 27271, 27277, 27281, 27283, 27299, 27329, 27337, 27361, 27367, 27397, 27407, 27409, 27427, 27431, 27437, 27449, 27457, 27479, 27481, 27487, 27509, 27527, 27529, 27539, 27541, 27551, 27581, 27583, 27611, 27617, 27631, 27647, 27653, 27673, 27689, 27691, 27697, 27701, 27733, 27737, 27739, 27743, 27749, 27751, 27763, 27767, 27773, 27779, 27791, 27793, 27799, 27803, 27809, 27817, 27823, 27827, 27847, 27851, 27883, 27893, 27901, 27917, 27919, 27941, 27943, 27947, 27953, 27961, 27967, 27983, 27997, 28001, 28019, 28027, 28031, 28051, 28057, 28069, 28081, 28087, 28097, 28099, 28109, 28111, 28123, 28151, 28163, 28181, 28183, 28201, 28211, 28219, 28229, 28277, 28279, 28283, 28289, 28297, 28307, 28309, 28319, 28349, 28351, 28387, 28393, 28403, 28409, 28411, 28429, 28433, 28439, 28447, 28463, 28477, 28493, 28499, 28513, 28517, 28537, 28541, 28547, 28549, 28559, 28571, 28573, 28579, 28591, 28597, 28603, 28607, 28619, 28621, 28627, 28631, 28643, 28649, 28657, 28661, 28663, 28669, 28687, 28697, 28703, 28711, 28723, 28729, 28751, 28753, 28759, 28771, 28789, 28793, 28807, 28813, 28817, 28837, 28843, 28859, 28867, 28871, 28879, 28901, 28909, 28921, 28927, 28933, 28949, 28961, 28979, 29009, 29017, 29021, 29023, 29027, 29033, 29059, 29063, 29077, 29101, 29123, 29129, 29131, 29137, 29147, 29153, 29167, 29173, 29179, 29191, 29201, 29207, 29209, 29221, 29231, 29243, 29251, 29269, 29287, 29297, 29303, 29311, 29327, 29333, 29339, 29347, 29363, 29383, 29387, 29389, 29399, 29401, 29411, 29423, 29429, 29437, 29443, 29453, 29473, 29483, 29501, 29527, 29531, 29537, 29567, 29569, 29573, 29581, 29587, 29599, 29611, 29629, 29633, 29641, 29663, 29669, 29671, 29683, 29717, 29723, 29741, 29753, 29759, 29761, 29789, 29803, 29819, 29833, 29837, 29851, 29863, 29867, 29873, 29879, 29881, 29917, 29921, 29927, 29947, 29959, 29983, 29989, 30011, 30013, 30029, 30047, 30059, 30071, 30089, 30091, 30097, 30103, 30109, 30113, 30119, 30133, 30137, 30139, 30161, 30169, 30181, 30187, 30197, 30203, 30211, 30223, 30241, 30253, 30259, 30269, 30271, 30293, 30307, 30313, 30319, 30323, 30341, 30347, 30367, 30389, 30391, 30403, 30427, 30431, 30449, 30467, 30469, 30491, 30493, 30497, 30509, 30517, 30529, 30539, 30553, 30557, 30559, 30577, 30593, 30631, 30637, 30643, 30649, 30661, 30671, 30677, 30689, 30697, 30703, 30707, 30713, 30727, 30757, 30763, 30773, 30781, 30803, 30809, 30817, 30829, 30839, 30841, 30851, 30853, 30859, 30869, 30871, 30881, 30893, 30911, 30931, 30937, 30941, 30949, 30971, 30977, 30983, 31013, 31019, 31033, 31039, 31051, 31063, 31069, 31079, 31081, 31091, 31121, 31123, 31139, 31147, 31151, 31153, 31159, 31177, 31181, 31183, 31189, 31193, 31219, 31223, 31231, 31237, 31247, 31249, 31253, 31259, 31267, 31271, 31277, 31307, 31319, 31321, 31327, 31333, 31337, 31357, 31379, 31387, 31391, 31393, 31397, 31469, 31477, 31481, 31489, 31511, 31513, 31517, 31531, 31541, 31543, 31547, 31567, 31573, 31583, 31601, 31607, 31627, 31643, 31649, 31657, 31663, 31667, 31687, 31699, 31721, 31723, 31727, 31729, 31741, 31751, 31769, 31771, 31793, 31799, 31817, 31847, 31849, 31859, 31873, 31883, 31891, 31907, 31957, 31963, 31973, 31981, 31991, 32003, 32009, 32027, 32029, 32051, 32057, 32059, 32063, 32069, 32077, 32083, 32089, 32099, 32117, 32119, 32141, 32143, 32159, 32173, 32183, 32189, 32191, 32203, 32213, 32233, 32237, 32251, 32257, 32261, 32297, 32299, 32303, 32309, 32321, 32323, 32327, 32341, 32353, 32359, 32363, 32369, 32371, 32377, 32381, 32401, 32411, 32413, 32423, 32429, 32441, 32443, 32467, 32479, 32491, 32497, 32503, 32507, 32531, 32533, 32537, 32561, 32563, 32569, 32573, 32579, 32587, 32603, 32609, 32611, 32621, 32633, 32647, 32653, 32687, 32693, 32707, 32713, 32717, 32719, 32749, 32771, 32779, 32783, 32789, 32797, 32801, 32803, 32831, 32833, 32839, 32843, 32869, 32887, 32909, 32911, 32917, 32933, 32939, 32941, 32957, 32969, 32971, 32983, 32987, 32993, 32999, 33013, 33023, 33029, 33037, 33049, 33053, 33071, 33073, 33083, 33091, 33107, 33113, 33119, 33149, 33151, 33161, 33179, 33181, 33191, 33199, 33203, 33211, 33223, 33247, 33287, 33289, 33301, 33311, 33317, 33329, 33331, 33343, 33347, 33349, 33353, 33359, 33377, 33391, 33403, 33409, 33413, 33427, 33457, 33461, 33469, 33479, 33487, 33493, 33503, 33521, 33529, 33533, 33547, 33563, 33569, 33577, 33581, 33587, 33589, 33599, 33601, 33613, 33617, 33619, 33623, 33629, 33637, 33641, 33647, 33679, 33703, 33713, 33721, 33739, 33749, 33751, 33757, 33767, 33769, 33773, 33791, 33797, 33809, 33811, 33827, 33829, 33851, 33857, 33863, 33871, 33889, 33893, 33911, 33923, 33931, 33937, 33941, 33961, 33967, 33997, 34019, 34031, 34033, 34039, 34057, 34061, 34123, 34127, 34129, 34141, 34147, 34157, 34159, 34171, 34183, 34211, 34213, 34217, 34231, 34253, 34259, 34261, 34267, 34273, 34283, 34297, 34301, 34303, 34313, 34319, 34327, 34337, 34351, 34361, 34367, 34369, 34381, 34403, 34421, 34429, 34439, 34457, 34469, 34471, 34483, 34487, 34499, 34501, 34511, 34513, 34519, 34537, 34543, 34549, 34583, 34589, 34591, 34603, 34607, 34613, 34631, 34649, 34651, 34667, 34673, 34679, 34687, 34693, 34703, 34721, 34729, 34739, 34747, 34757, 34759, 34763, 34781, 34807, 34819, 34841, 34843, 34847, 34849, 34871, 34877, 34883, 34897, 34913, 34919, 34939, 34949, 34961, 34963, 34981, 35023, 35027, 35051, 35053, 35059, 35069, 35081, 35083, 35089, 35099, 35107, 35111, 35117, 35129, 35141, 35149, 35153, 35159, 35171, 35201, 35221, 35227, 35251, 35257, 35267, 35279, 35281, 35291, 35311, 35317, 35323, 35327, 35339, 35353, 35363, 35381, 35393, 35401, 35407, 35419, 35423, 35437, 35447, 35449, 35461, 35491, 35507, 35509, 35521, 35527, 35531, 35533, 35537, 35543, 35569, 35573, 35591, 35593, 35597, 35603, 35617, 35671, 35677, 35729, 35731, 35747, 35753, 35759, 35771, 35797, 35801, 35803, 35809, 35831, 35837, 35839, 35851, 35863, 35869, 35879, 35897, 35899, 35911, 35923, 35933, 35951, 35963, 35969, 35977, 35983, 35993, 35999, 36007, 36011, 36013, 36017, 36037, 36061, 36067, 36073, 36083, 36097, 36107, 36109, 36131, 36137, 36151, 36161, 36187, 36191, 36209, 36217, 36229, 36241, 36251, 36263, 36269, 36277, 36293, 36299, 36307, 36313, 36319, 36341, 36343, 36353, 36373, 36383, 36389, 36433, 36451, 36457, 36467, 36469, 36473, 36479, 36493, 36497, 36523, 36527, 36529, 36541, 36551, 36559, 36563, 36571, 36583, 36587, 36599, 36607, 36629, 36637, 36643, 36653, 36671, 36677, 36683, 36691, 36697, 36709, 36713, 36721, 36739, 36749, 36761, 36767, 36779, 36781, 36787, 36791, 36793, 36809, 36821, 36833, 36847, 36857, 36871, 36877, 36887, 36899, 36901, 36913, 36919, 36923, 36929, 36931, 36943, 36947, 36973, 36979, 36997, 37003, 37013, 37019, 37021, 37039, 37049, 37057, 37061, 37087, 37097, 37117, 37123, 37139, 37159, 37171, 37181, 37189, 37199, 37201, 37217, 37223, 37243, 37253, 37273, 37277, 37307, 37309, 37313, 37321, 37337, 37339, 37357, 37361, 37363, 37369, 37379, 37397, 37409, 37423, 37441, 37447, 37463, 37483, 37489, 37493, 37501, 37507, 37511, 37517, 37529, 37537, 37547, 37549, 37561, 37567, 37571, 37573, 37579, 37589, 37591, 37607, 37619, 37633, 37643, 37649, 37657, 37663, 37691, 37693, 37699, 37717, 37747, 37781, 37783, 37799, 37811, 37813, 37831, 37847, 37853, 37861, 37871, 37879, 37889, 37897, 37907, 37951, 37957, 37963, 37967, 37987, 37991, 37993, 37997, 38011, 38039, 38047, 38053, 38069, 38083, 38113, 38119, 38149, 38153, 38167, 38177, 38183, 38189, 38197, 38201, 38219, 38231, 38237, 38239, 38261, 38273, 38281, 38287, 38299, 38303, 38317, 38321, 38327, 38329, 38333, 38351, 38371, 38377, 38393, 38431, 38447, 38449, 38453, 38459, 38461, 38501, 38543, 38557, 38561, 38567, 38569, 38593, 38603, 38609, 38611, 38629, 38639, 38651, 38653, 38669, 38671, 38677, 38693, 38699, 38707, 38711, 38713, 38723, 38729, 38737, 38747, 38749, 38767, 38783, 38791, 38803, 38821, 38833, 38839, 38851, 38861, 38867, 38873, 38891, 38903, 38917, 38921, 38923, 38933, 38953, 38959, 38971, 38977, 38993, 39019, 39023, 39041, 39043, 39047, 39079, 39089, 39097, 39103, 39107, 39113, 39119, 39133, 39139, 39157, 39161, 39163, 39181, 39191, 39199, 39209, 39217, 39227, 39229, 39233, 39239, 39241, 39251, 39293, 39301, 39313, 39317, 39323, 39341, 39343, 39359, 39367, 39371, 39373, 39383, 39397, 39409, 39419, 39439, 39443, 39451, 39461, 39499, 39503, 39509, 39511, 39521, 39541, 39551, 39563, 39569, 39581, 39607, 39619, 39623, 39631, 39659, 39667, 39671, 39679, 39703, 39709, 39719, 39727, 39733, 39749, 39761, 39769, 39779, 39791, 39799, 39821, 39827, 39829, 39839, 39841, 39847, 39857, 39863, 39869, 39877, 39883, 39887, 39901, 39929, 39937, 39953, 39971, 39979, 39983, 39989, 40009, 40013, 40031, 40037, 40039, 40063, 40087, 40093, 40099, 40111, 40123, 40127, 40129, 40151, 40153, 40163, 40169, 40177, 40189, 40193, 40213, 40231, 40237, 40241, 40253, 40277, 40283, 40289, 40343, 40351, 40357, 40361, 40387, 40423, 40427, 40429, 40433, 40459, 40471, 40483, 40487, 40493, 40499, 40507, 40519, 40529, 40531, 40543, 40559, 40577, 40583, 40591, 40597, 40609, 40627, 40637, 40639, 40693, 40697, 40699, 40709, 40739, 40751, 40759, 40763, 40771, 40787, 40801, 40813, 40819, 40823, 40829, 40841, 40847, 40849, 40853, 40867, 40879, 40883, 40897, 40903, 40927, 40933, 40939, 40949, 40961, 40973, 40993, 41011, 41017, 41023, 41039, 41047, 41051, 41057, 41077, 41081, 41113, 41117, 41131, 41141, 41143, 41149, 41161, 41177, 41179, 41183, 41189, 41201, 41203, 41213, 41221, 41227, 41231, 41233, 41243, 41257, 41263, 41269, 41281, 41299, 41333, 41341, 41351, 41357, 41381, 41387, 41389, 41399, 41411, 41413, 41443, 41453, 41467, 41479, 41491, 41507, 41513, 41519, 41521, 41539, 41543, 41549, 41579, 41593, 41597, 41603, 41609, 41611, 41617, 41621, 41627, 41641, 41647, 41651, 41659, 41669, 41681, 41687, 41719, 41729, 41737, 41759, 41761, 41771, 41777, 41801, 41809, 41813, 41843, 41849, 41851, 41863, 41879, 41887, 41893, 41897, 41903, 41911, 41927, 41941, 41947, 41953, 41957, 41959, 41969, 41981, 41983, 41999, 42013, 42017, 42019, 42023, 42043, 42061, 42071, 42073, 42083, 42089, 42101, 42131, 42139, 42157, 42169, 42179, 42181, 42187, 42193, 42197, 42209, 42221, 42223, 42227, 42239, 42257, 42281, 42283, 42293, 42299, 42307, 42323, 42331, 42337, 42349, 42359, 42373, 42379, 42391, 42397, 42403, 42407, 42409, 42433, 42437, 42443, 42451, 42457, 42461, 42463, 42467, 42473, 42487, 42491, 42499, 42509, 42533, 42557, 42569, 42571, 42577, 42589, 42611, 42641, 42643, 42649, 42667, 42677, 42683, 42689, 42697, 42701, 42703, 42709, 42719, 42727, 42737, 42743, 42751, 42767, 42773, 42787, 42793, 42797, 42821, 42829, 42839, 42841, 42853, 42859, 42863, 42899, 42901, 42923, 42929, 42937, 42943, 42953, 42961, 42967, 42979, 42989, 43003, 43013, 43019, 43037, 43049, 43051, 43063, 43067, 43093, 43103, 43117, 43133, 43151, 43159, 43177, 43189, 43201, 43207, 43223, 43237, 43261, 43271, 43283, 43291, 43313, 43319, 43321, 43331, 43391, 43397, 43399, 43403, 43411, 43427, 43441, 43451, 43457, 43481, 43487, 43499, 43517, 43541, 43543, 43573, 43577, 43579, 43591, 43597, 43607, 43609, 43613, 43627, 43633, 43649, 43651, 43661, 43669, 43691, 43711, 43717, 43721, 43753, 43759, 43777, 43781, 43783, 43787, 43789, 43793, 43801, 43853, 43867, 43889, 43891, 43913, 43933, 43943, 43951, 43961, 43963, 43969, 43973, 43987, 43991, 43997, 44017, 44021, 44027, 44029, 44041, 44053, 44059, 44071, 44087, 44089, 44101, 44111, 44119, 44123, 44129, 44131, 44159, 44171, 44179, 44189, 44201, 44203, 44207, 44221, 44249, 44257, 44263, 44267, 44269, 44273, 44279, 44281, 44293, 44351, 44357, 44371, 44381, 44383, 44389, 44417, 44449, 44453, 44483, 44491, 44497, 44501, 44507, 44519, 44531, 44533, 44537, 44543, 44549, 44563, 44579, 44587, 44617, 44621, 44623, 44633, 44641, 44647, 44651, 44657, 44683, 44687, 44699, 44701, 44711, 44729, 44741, 44753, 44771, 44773, 44777, 44789, 44797, 44809, 44819, 44839, 44843, 44851, 44867, 44879, 44887, 44893, 44909, 44917, 44927, 44939, 44953, 44959, 44963, 44971, 44983, 44987, 45007, 45013, 45053, 45061, 45077, 45083, 45119, 45121, 45127, 45131, 45137, 45139, 45161, 45179, 45181, 45191, 45197, 45233, 45247, 45259, 45263, 45281, 45289, 45293, 45307, 45317, 45319, 45329, 45337, 45341, 45343, 45361, 45377, 45389, 45403, 45413, 45427, 45433, 45439, 45481, 45491, 45497, 45503, 45523, 45533, 45541, 45553, 45557, 45569, 45587, 45589, 45599, 45613, 45631, 45641, 45659, 45667, 45673, 45677, 45691, 45697, 45707, 45737, 45751, 45757, 45763, 45767, 45779, 45817, 45821, 45823, 45827, 45833, 45841, 45853, 45863, 45869, 45887, 45893, 45943, 45949, 45953, 45959, 45971, 45979, 45989, 46021, 46027, 46049, 46051, 46061, 46073, 46091, 46093, 46099, 46103, 46133, 46141, 46147, 46153, 46171, 46181, 46183, 46187, 46199, 46219, 46229, 46237, 46261, 46271, 46273, 46279, 46301, 46307, 46309, 46327, 46337, 46349, 46351, 46381, 46399, 46411, 46439, 46441, 46447, 46451, 46457, 46471, 46477, 46489, 46499, 46507, 46511, 46523, 46549, 46559, 46567, 46573, 46589, 46591, 46601, 46619, 46633, 46639, 46643, 46649, 46663, 46679, 46681, 46687, 46691, 46703, 46723, 46727, 46747, 46751, 46757, 46769, 46771, 46807, 46811, 46817, 46819, 46829, 46831, 46853, 46861, 46867, 46877, 46889, 46901, 46919, 46933, 46957, 46993, 46997, 47017, 47041, 47051, 47057, 47059, 47087, 47093, 47111, 47119, 47123, 47129, 47137, 47143, 47147, 47149, 47161, 47189, 47207, 47221, 47237, 47251, 47269, 47279, 47287, 47293, 47297, 47303, 47309, 47317, 47339, 47351, 47353, 47363, 47381, 47387, 47389, 47407, 47417, 47419, 47431, 47441, 47459, 47491, 47497, 47501, 47507, 47513, 47521, 47527, 47533, 47543, 47563, 47569, 47581, 47591, 47599, 47609, 47623, 47629, 47639, 47653, 47657, 47659, 47681, 47699, 47701, 47711, 47713, 47717, 47737, 47741, 47743, 47777, 47779, 47791, 47797, 47807, 47809, 47819, 47837, 47843, 47857, 47869, 47881, 47903, 47911, 47917, 47933, 47939, 47947, 47951, 47963, 47969, 47977, 47981, 48017, 48023, 48029, 48049, 48073, 48079, 48091, 48109, 48119, 48121, 48131, 48157, 48163, 48179, 48187, 48193, 48197, 48221, 48239, 48247, 48259, 48271, 48281, 48299, 48311, 48313, 48337, 48341, 48353, 48371, 48383, 48397, 48407, 48409, 48413, 48437, 48449, 48463, 48473, 48479, 48481, 48487, 48491, 48497, 48523, 48527, 48533, 48539, 48541, 48563, 48571, 48589, 48593, 48611, 48619, 48623, 48647, 48649, 48661, 48673, 48677, 48679, 48731, 48733, 48751, 48757, 48761, 48767, 48779, 48781, 48787, 48799, 48809, 48817, 48821, 48823, 48847, 48857, 48859, 48869, 48871, 48883, 48889, 48907, 48947, 48953, 48973, 48989, 48991, 49003, 49009, 49019, 49031, 49033, 49037, 49043, 49057, 49069, 49081, 49103, 49109, 49117, 49121, 49123, 49139, 49157, 49169, 49171, 49177, 49193, 49199, 49201, 49207, 49211, 49223, 49253, 49261, 49277, 49279, 49297, 49307, 49331, 49333, 49339, 49363, 49367, 49369, 49391, 49393, 49409, 49411, 49417, 49429, 49433, 49451, 49459, 49463, 49477, 49481, 49499, 49523, 49529, 49531, 49537, 49547, 49549, 49559, 49597, 49603, 49613, 49627, 49633, 49639, 49663, 49667, 49669, 49681, 49697, 49711, 49727, 49739, 49741, 49747, 49757, 49783, 49787, 49789, 49801, 49807, 49811, 49823, 49831, 49843, 49853, 49871, 49877, 49891, 49919, 49921, 49927, 49937, 49939, 49943, 49957, 49991, 49993, 49999, 50021, 50023, 50033, 50047, 50051, 50053, 50069, 50077, 50087, 50093, 50101, 50111, 50119, 50123, 50129, 50131, 50147, 50153, 50159, 50177, 50207, 50221, 50227, 50231, 50261, 50263, 50273, 50287, 50291, 50311, 50321, 50329, 50333, 50341, 50359, 50363, 50377, 50383, 50387, 50411, 50417, 50423, 50441, 50459, 50461, 50497, 50503, 50513, 50527, 50539, 50543, 50549, 50551, 50581, 50587, 50591, 50593, 50599, 50627, 50647, 50651, 50671, 50683, 50707, 50723, 50741, 50753, 50767, 50773, 50777, 50789, 50821, 50833, 50839, 50849, 50857, 50867, 50873, 50891, 50893, 50909, 50923, 50929, 50951, 50957, 50969, 50971, 50989, 50993, 51001, 51031, 51043, 51047, 51059, 51061, 51071, 51109, 51131, 51133, 51137, 51151, 51157, 51169, 51193, 51197, 51199, 51203, 51217, 51229, 51239, 51241, 51257, 51263, 51283, 51287, 51307, 51329, 51341, 51343, 51347, 51349, 51361, 51383, 51407, 51413, 51419, 51421, 51427, 51431, 51437, 51439, 51449, 51461, 51473, 51479, 51481, 51487, 51503, 51511, 51517, 51521, 51539, 51551, 51563, 51577, 51581, 51593, 51599, 51607, 51613, 51631, 51637, 51647, 51659, 51673, 51679, 51683, 51691, 51713, 51719, 51721, 51749, 51767, 51769, 51787, 51797, 51803, 51817, 51827, 51829, 51839, 51853, 51859, 51869, 51871, 51893, 51899, 51907, 51913, 51929, 51941, 51949, 51971, 51973, 51977, 51991, 52009, 52021, 52027, 52051, 52057, 52067, 52069, 52081, 52103, 52121, 52127, 52147, 52153, 52163, 52177, 52181, 52183, 52189, 52201, 52223, 52237, 52249, 52253, 52259, 52267, 52289, 52291, 52301, 52313, 52321, 52361, 52363, 52369, 52379, 52387, 52391, 52433, 52453, 52457, 52489, 52501, 52511, 52517, 52529, 52541, 52543, 52553, 52561, 52567, 52571, 52579, 52583, 52609, 52627, 52631, 52639, 52667, 52673, 52691, 52697, 52709, 52711, 52721, 52727, 52733, 52747, 52757, 52769, 52783, 52807, 52813, 52817, 52837, 52859, 52861, 52879, 52883, 52889, 52901, 52903, 52919, 52937, 52951, 52957, 52963, 52967, 52973, 52981, 52999, 53003, 53017, 53047, 53051, 53069, 53077, 53087, 53089, 53093, 53101, 53113, 53117, 53129, 53147, 53149, 53161, 53171, 53173, 53189, 53197, 53201, 53231, 53233, 53239, 53267, 53269, 53279, 53281, 53299, 53309, 53323, 53327, 53353, 53359, 53377, 53381, 53401, 53407, 53411, 53419, 53437, 53441, 53453, 53479, 53503, 53507, 53527, 53549, 53551, 53569, 53591, 53593, 53597, 53609, 53611, 53617, 53623, 53629, 53633, 53639, 53653, 53657, 53681, 53693, 53699, 53717, 53719, 53731, 53759, 53773, 53777, 53783, 53791, 53813, 53819, 53831, 53849, 53857, 53861, 53881, 53887, 53891, 53897, 53899, 53917, 53923, 53927, 53939, 53951, 53959, 53987, 53993, 54001, 54011, 54013, 54037, 54049, 54059, 54083, 54091, 54101, 54121, 54133, 54139, 54151, 54163, 54167, 54181, 54193, 54217, 54251, 54269, 54277, 54287, 54293, 54311, 54319, 54323, 54331, 54347, 54361, 54367, 54371, 54377, 54401, 54403, 54409, 54413, 54419, 54421, 54437, 54443, 54449, 54469, 54493, 54497, 54499, 54503, 54517, 54521, 54539, 54541, 54547, 54559, 54563, 54577, 54581, 54583, 54601, 54617, 54623, 54629, 54631, 54647, 54667, 54673, 54679, 54709, 54713, 54721, 54727, 54751, 54767, 54773, 54779, 54787, 54799, 54829, 54833, 54851, 54869, 54877, 54881, 54907, 54917, 54919, 54941, 54949, 54959, 54973, 54979, 54983, 55001, 55009, 55021, 55049, 55051, 55057, 55061, 55073, 55079, 55103, 55109, 55117, 55127, 55147, 55163, 55171, 55201, 55207, 55213, 55217, 55219, 55229, 55243, 55249, 55259, 55291, 55313, 55331, 55333, 55337, 55339, 55343, 55351, 55373, 55381, 55399, 55411, 55439, 55441, 55457, 55469, 55487, 55501, 55511, 55529, 55541, 55547, 55579, 55589, 55603, 55609, 55619, 55621, 55631, 55633, 55639, 55661, 55663, 55667, 55673, 55681, 55691, 55697, 55711, 55717, 55721, 55733, 55763, 55787, 55793, 55799, 55807, 55813, 55817, 55819, 55823, 55829, 55837, 55843, 55849, 55871, 55889, 55897, 55901, 55903, 55921, 55927, 55931, 55933, 55949, 55967, 55987, 55997, 56003, 56009, 56039, 56041, 56053, 56081, 56087, 56093, 56099, 56101, 56113, 56123, 56131, 56149, 56167, 56171, 56179, 56197, 56207, 56209, 56237, 56239, 56249, 56263, 56267, 56269, 56299, 56311, 56333, 56359, 56369, 56377, 56383, 56393, 56401, 56417, 56431, 56437, 56443, 56453, 56467, 56473, 56477, 56479, 56489, 56501, 56503, 56509, 56519, 56527, 56531, 56533, 56543, 56569, 56591, 56597, 56599, 56611, 56629, 56633, 56659, 56663, 56671, 56681, 56687, 56701, 56711, 56713, 56731, 56737, 56747, 56767, 56773, 56779, 56783, 56807, 56809, 56813, 56821, 56827, 56843, 56857, 56873, 56891, 56893, 56897, 56909, 56911, 56921, 56923, 56929, 56941, 56951, 56957, 56963, 56983, 56989, 56993, 56999, 57037, 57041, 57047, 57059, 57073, 57077, 57089, 57097, 57107, 57119, 57131, 57139, 57143, 57149, 57163, 57173, 57179, 57191, 57193, 57203, 57221, 57223, 57241, 57251, 57259, 57269, 57271, 57283, 57287, 57301, 57329, 57331, 57347, 57349, 57367, 57373, 57383, 57389, 57397, 57413, 57427, 57457, 57467, 57487, 57493, 57503, 57527, 57529, 57557, 57559, 57571, 57587, 57593, 57601, 57637, 57641, 57649, 57653, 57667, 57679, 57689, 57697, 57709, 57713, 57719, 57727, 57731, 57737, 57751, 57773, 57781, 57787, 57791, 57793, 57803, 57809, 57829, 57839, 57847, 57853, 57859, 57881, 57899, 57901, 57917, 57923, 57943, 57947, 57973, 57977, 57991, 58013, 58027, 58031, 58043, 58049, 58057, 58061, 58067, 58073, 58099, 58109, 58111, 58129, 58147, 58151, 58153, 58169, 58171, 58189, 58193, 58199, 58207, 58211, 58217, 58229, 58231, 58237, 58243, 58271, 58309, 58313, 58321, 58337, 58363, 58367, 58369, 58379, 58391, 58393, 58403, 58411, 58417, 58427, 58439, 58441, 58451, 58453, 58477, 58481, 58511, 58537, 58543, 58549, 58567, 58573, 58579, 58601, 58603, 58613, 58631, 58657, 58661, 58679, 58687, 58693, 58699, 58711, 58727, 58733, 58741, 58757, 58763, 58771, 58787, 58789, 58831, 58889, 58897, 58901, 58907, 58909, 58913, 58921, 58937, 58943, 58963, 58967, 58979, 58991, 58997, 59009, 59011, 59021, 59023, 59029, 59051, 59053, 59063, 59069, 59077, 59083, 59093, 59107, 59113, 59119, 59123, 59141, 59149, 59159, 59167, 59183, 59197, 59207, 59209, 59219, 59221, 59233, 59239, 59243, 59263, 59273, 59281, 59333, 59341, 59351, 59357, 59359, 59369, 59377, 59387, 59393, 59399, 59407, 59417, 59419, 59441, 59443, 59447, 59453, 59467, 59471, 59473, 59497, 59509, 59513, 59539, 59557, 59561, 59567, 59581, 59611, 59617, 59621, 59627, 59629, 59651, 59659, 59663, 59669, 59671, 59693, 59699, 59707, 59723, 59729, 59743, 59747, 59753, 59771, 59779, 59791, 59797, 59809, 59833, 59863, 59879, 59887, 59921, 59929, 59951, 59957, 59971, 59981, 59999, 60013, 60017, 60029, 60037, 60041, 60077, 60083, 60089, 60091, 60101, 60103, 60107, 60127, 60133, 60139, 60149, 60161, 60167, 60169, 60209, 60217, 60223, 60251, 60257, 60259, 60271, 60289, 60293, 60317, 60331, 60337, 60343, 60353, 60373, 60383, 60397, 60413, 60427, 60443, 60449, 60457, 60493, 60497, 60509, 60521, 60527, 60539, 60589, 60601, 60607, 60611, 60617, 60623, 60631, 60637, 60647, 60649, 60659, 60661, 60679, 60689, 60703, 60719, 60727, 60733, 60737, 60757, 60761, 60763, 60773, 60779, 60793, 60811, 60821, 60859, 60869, 60887, 60889, 60899, 60901, 60913, 60917, 60919, 60923, 60937, 60943, 60953, 60961, 61001, 61007, 61027, 61031, 61043, 61051, 61057, 61091, 61099, 61121, 61129, 61141, 61151, 61153, 61169, 61211, 61223, 61231, 61253, 61261, 61283, 61291, 61297, 61331, 61333, 61339, 61343, 61357, 61363, 61379, 61381, 61403, 61409, 61417, 61441, 61463, 61469, 61471, 61483, 61487, 61493, 61507, 61511, 61519, 61543, 61547, 61553, 61559, 61561, 61583, 61603, 61609, 61613, 61627, 61631, 61637, 61643, 61651, 61657, 61667, 61673, 61681, 61687, 61703, 61717, 61723, 61729, 61751, 61757, 61781, 61813, 61819, 61837, 61843, 61861, 61871, 61879, 61909, 61927, 61933, 61949, 61961, 61967, 61979, 61981, 61987, 61991, 62003, 62011, 62017, 62039, 62047, 62053, 62057, 62071, 62081, 62099, 62119, 62129, 62131, 62137, 62141, 62143, 62171, 62189, 62191, 62201, 62207, 62213, 62219, 62233, 62273, 62297, 62299, 62303, 62311, 62323, 62327, 62347, 62351, 62383, 62401, 62417, 62423, 62459, 62467, 62473, 62477, 62483, 62497, 62501, 62507, 62533, 62539, 62549, 62563, 62581, 62591, 62597, 62603, 62617, 62627, 62633, 62639, 62653, 62659, 62683, 62687, 62701, 62723, 62731, 62743, 62753, 62761, 62773, 62791, 62801, 62819, 62827, 62851, 62861, 62869, 62873, 62897, 62903, 62921, 62927, 62929, 62939, 62969, 62971, 62981, 62983, 62987, 62989, 63029, 63031, 63059, 63067, 63073, 63079, 63097, 63103, 63113, 63127, 63131, 63149, 63179, 63197, 63199, 63211, 63241, 63247, 63277, 63281, 63299, 63311, 63313, 63317, 63331, 63337, 63347, 63353, 63361, 63367, 63377, 63389, 63391, 63397, 63409, 63419, 63421, 63439, 63443, 63463, 63467, 63473, 63487, 63493, 63499, 63521, 63527, 63533, 63541, 63559, 63577, 63587, 63589, 63599, 63601, 63607, 63611, 63617, 63629, 63647, 63649, 63659, 63667, 63671, 63689, 63691, 63697, 63703, 63709, 63719, 63727, 63737, 63743, 63761, 63773, 63781, 63793, 63799, 63803, 63809, 63823, 63839, 63841, 63853, 63857, 63863, 63901, 63907, 63913, 63929, 63949, 63977, 63997, 64007, 64013, 64019, 64033, 64037, 64063, 64067, 64081, 64091, 64109, 64123, 64151, 64153, 64157, 64171, 64187, 64189, 64217, 64223, 64231, 64237, 64271, 64279, 64283, 64301, 64303, 64319, 64327, 64333, 64373, 64381, 64399, 64403, 64433, 64439, 64451, 64453, 64483, 64489, 64499, 64513, 64553, 64567, 64577, 64579, 64591, 64601, 64609, 64613, 64621, 64627, 64633, 64661, 64663, 64667, 64679, 64693, 64709, 64717, 64747, 64763, 64781, 64783, 64793, 64811, 64817, 64849, 64853, 64871, 64877, 64879, 64891, 64901, 64919, 64921, 64927, 64937, 64951, 64969, 64997, 65003, 65011, 65027, 65029, 65033, 65053, 65063, 65071, 65089, 65099, 65101, 65111, 65119, 65123, 65129, 65141, 65147, 65167, 65171, 65173, 65179, 65183, 65203, 65213, 65239, 65257, 65267, 65269, 65287, 65293, 65309, 65323, 65327, 65353, 65357, 65371, 65381, 65393, 65407, 65413, 65419, 65423, 65437, 65447, 65449, 65479, 65497, 65519, 65521, 0 }; } /* * Modular Reducer * (C) 1999-2011 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Modular_Reducer Constructor */ Modular_Reducer::Modular_Reducer(const BigInt& mod) { if(mod <= 0) throw Invalid_Argument("Modular_Reducer: modulus must be positive"); modulus = mod; mod_words = modulus.sig_words(); modulus_2 = Botan::square(modulus); mu = BigInt(BigInt::Power2, 2 * MP_WORD_BITS * mod_words) / modulus; } /* * Barrett Reduction */ BigInt Modular_Reducer::reduce(const BigInt& x) const { if(mod_words == 0) throw Invalid_State("Modular_Reducer: Never initalized"); if(x.cmp(modulus, false) < 0) { if(x.is_negative()) return x + modulus; // make positive return x; } else if(x.cmp(modulus_2, false) < 0) { BigInt t1 = x; t1.set_sign(BigInt::Positive); t1 >>= (MP_WORD_BITS * (mod_words - 1)); t1 *= mu; t1 >>= (MP_WORD_BITS * (mod_words + 1)); t1 *= modulus; t1.mask_bits(MP_WORD_BITS * (mod_words + 1)); BigInt t2 = x; t2.set_sign(BigInt::Positive); t2.mask_bits(MP_WORD_BITS * (mod_words + 1)); t2 -= t1; if(t2.is_negative()) { BigInt b_to_k1(BigInt::Power2, MP_WORD_BITS * (mod_words + 1)); t2 += b_to_k1; } while(t2 >= modulus) t2 -= modulus; if(x.is_positive()) return t2; else return (modulus - t2); } else { // too big, fall back to normal division return (x % modulus); } } } /* * Shanks-Tonnelli (RESSOL) * (C) 2007-2008 Falko Strenzke, FlexSecure GmbH * (C) 2008 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Shanks-Tonnelli algorithm */ BigInt ressol(const BigInt& a, const BigInt& p) { if(a < 0) throw Invalid_Argument("ressol(): a to solve for must be positive"); if(p <= 1) throw Invalid_Argument("ressol(): prime must be > 1"); if(a == 0) return 0; if(p == 2) return a; if(jacobi(a, p) != 1) // not a quadratic residue return -BigInt(1); if(p % 4 == 3) return power_mod(a, ((p+1) >> 2), p); size_t s = low_zero_bits(p - 1); BigInt q = p >> s; q -= 1; q >>= 1; Modular_Reducer mod_p(p); BigInt r = power_mod(a, q, p); BigInt n = mod_p.multiply(a, mod_p.square(r)); r = mod_p.multiply(r, a); if(n == 1) return r; // find random non quadratic residue z BigInt z = 2; while(jacobi(z, p) == 1) // while z quadratic residue ++z; BigInt c = power_mod(z, (q << 1) + 1, p); while(n > 1) { q = n; size_t i = 0; while(q != 1) { q = mod_p.square(q); ++i; } if(s <= i) return -BigInt(1); c = power_mod(c, BigInt(BigInt::Power2, s-i-1), p); r = mod_p.multiply(r, c); c = mod_p.square(c); n = mod_p.multiply(n, c); s = i; } return r; } } /* * No-Op Mutex Factory * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * No-Op Mutex Factory */ Mutex* Noop_Mutex_Factory::make() { class Noop_Mutex : public Mutex { public: class Mutex_State_Error : public Internal_Error { public: Mutex_State_Error(const std::string& where) : Internal_Error("Noop_Mutex::" + where + ": " + "Mutex is already " + where + "ed") {} }; void lock() { if(locked) throw Mutex_State_Error("lock"); locked = true; } void unlock() { if(!locked) throw Mutex_State_Error("unlock"); locked = false; } Noop_Mutex() { locked = false; } private: bool locked; }; return new Noop_Mutex; } } /* * Pthread Mutex * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #ifndef _POSIX_C_SOURCE #define _POSIX_C_SOURCE 199506 #endif #include <pthread.h> namespace Botan { /* * Pthread Mutex Factory */ Mutex* Pthread_Mutex_Factory::make() { class Pthread_Mutex : public Mutex { public: void lock() { if(pthread_mutex_lock(&mutex) != 0) throw Invalid_State("Pthread_Mutex::lock: Error occured"); } void unlock() { if(pthread_mutex_unlock(&mutex) != 0) throw Invalid_State("Pthread_Mutex::unlock: Error occured"); } Pthread_Mutex() { if(pthread_mutex_init(&mutex, 0) != 0) throw Invalid_State("Pthread_Mutex: initialization failed"); } ~Pthread_Mutex() { if(pthread_mutex_destroy(&mutex) != 0) throw Invalid_State("~Pthread_Mutex: mutex is still locked"); } private: pthread_mutex_t mutex; }; return new Pthread_Mutex(); } } /* * Bcrypt Password Hashing * (C) 2010 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { std::string bcrypt_base64_encode(const byte input[], size_t length) { // Bcrypt uses a non-standard base64 alphabet const byte OPENBSD_BASE64_SUB[256] = { 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x38, 0x80, 0x80, 0x80, 0x39, 0x79, 0x7A, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x2E, 0x2F, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 }; std::string b64 = base64_encode(input, length); while(b64.size() && b64[b64.size()-1] == '=') b64 = b64.substr(0, b64.size() - 1); for(size_t i = 0; i != b64.size(); ++i) b64[i] = OPENBSD_BASE64_SUB[static_cast<byte>(b64[i])]; return b64; } MemoryVector<byte> bcrypt_base64_decode(std::string input) { const byte OPENBSD_BASE64_SUB[256] = { 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x41, 0x42, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2B, 0x2F, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x61, 0x62, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x30, 0x31, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 }; for(size_t i = 0; i != input.size(); ++i) input[i] = OPENBSD_BASE64_SUB[static_cast<byte>(input[i])]; return base64_decode(input); } std::string make_bcrypt(const std::string& pass, const MemoryRegion<byte>& salt, u16bit work_factor) { const byte magic[24] = { 0x4F, 0x72, 0x70, 0x68, 0x65, 0x61, 0x6E, 0x42, 0x65, 0x68, 0x6F, 0x6C, 0x64, 0x65, 0x72, 0x53, 0x63, 0x72, 0x79, 0x44, 0x6F, 0x75, 0x62, 0x74 }; MemoryVector<byte> ctext(magic, 24); Blowfish blowfish; // Include the trailing NULL byte blowfish.eks_key_schedule(reinterpret_cast<const byte*>(pass.c_str()), pass.length() + 1, salt, work_factor); for(size_t i = 0; i != 64; ++i) blowfish.encrypt_n(&ctext[0], &ctext[0], 3); std::string salt_b64 = bcrypt_base64_encode(&salt[0], salt.size()); return "$2a$" + to_string(work_factor, 2) + "$" + salt_b64.substr(0, 22) + bcrypt_base64_encode(&ctext[0], ctext.size() - 1); } } std::string generate_bcrypt(const std::string& pass, RandomNumberGenerator& rng, u16bit work_factor) { return make_bcrypt(pass, rng.random_vec(16), work_factor); } bool check_bcrypt(const std::string& pass, const std::string& hash) { if(hash.size() != 60 || hash[0] != '$' || hash[1] != '2' || hash[2] != 'a' || hash[3] != '$' || hash[6] != '$') { return false; } const u16bit workfactor = to_u32bit(hash.substr(4, 2)); MemoryVector<byte> salt = bcrypt_base64_decode(hash.substr(7, 22)); const std::string compare = make_bcrypt(pass, salt, workfactor); return (hash == compare); } } /* * Passhash9 Password Hashing * (C) 2010 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { const std::string MAGIC_PREFIX = "$9$"; const size_t WORKFACTOR_BYTES = 2; const size_t ALGID_BYTES = 1; const size_t SALT_BYTES = 12; // 96 bits of salt const size_t PASSHASH9_PBKDF_OUTPUT_LEN = 24; // 192 bits output const size_t WORK_FACTOR_SCALE = 10000; MessageAuthenticationCode* get_pbkdf_prf(byte alg_id) { Algorithm_Factory& af = global_state().algorithm_factory(); try { if(alg_id == 0) return af.make_mac("HMAC(SHA-1)"); else if(alg_id == 1) return af.make_mac("HMAC(SHA-256)"); else if(alg_id == 2) return af.make_mac("CMAC(Blowfish)"); } catch(Algorithm_Not_Found) {} return 0; } } std::string generate_passhash9(const std::string& pass, RandomNumberGenerator& rng, u16bit work_factor, byte alg_id) { MessageAuthenticationCode* prf = get_pbkdf_prf(alg_id); if(!prf) throw Invalid_Argument("Passhash9: Algorithm id " + to_string(alg_id) + " is not defined"); PKCS5_PBKDF2 kdf(prf); // takes ownership of pointer SecureVector<byte> salt(SALT_BYTES); rng.randomize(&salt[0], salt.size()); const size_t kdf_iterations = WORK_FACTOR_SCALE * work_factor; SecureVector<byte> pbkdf2_output = kdf.derive_key(PASSHASH9_PBKDF_OUTPUT_LEN, pass, &salt[0], salt.size(), kdf_iterations).bits_of(); Pipe pipe(new Base64_Encoder); pipe.start_msg(); pipe.write(alg_id); pipe.write(get_byte(0, work_factor)); pipe.write(get_byte(1, work_factor)); pipe.write(salt); pipe.write(pbkdf2_output); pipe.end_msg(); return MAGIC_PREFIX + pipe.read_all_as_string(); } bool check_passhash9(const std::string& pass, const std::string& hash) { const size_t BINARY_LENGTH = ALGID_BYTES + WORKFACTOR_BYTES + PASSHASH9_PBKDF_OUTPUT_LEN + SALT_BYTES; const size_t BASE64_LENGTH = MAGIC_PREFIX.size() + (BINARY_LENGTH * 8) / 6; if(hash.size() != BASE64_LENGTH) return false; for(size_t i = 0; i != MAGIC_PREFIX.size(); ++i) if(hash[i] != MAGIC_PREFIX[i]) return false; Pipe pipe(new Base64_Decoder); pipe.start_msg(); pipe.write(hash.c_str() + MAGIC_PREFIX.size()); pipe.end_msg(); SecureVector<byte> bin = pipe.read_all(); if(bin.size() != BINARY_LENGTH) return false; byte alg_id = bin[0]; const size_t kdf_iterations = WORK_FACTOR_SCALE * load_be<u16bit>(&bin[ALGID_BYTES], 0); if(kdf_iterations == 0) return false; MessageAuthenticationCode* pbkdf_prf = get_pbkdf_prf(alg_id); if(pbkdf_prf == 0) return false; // unknown algorithm, reject PKCS5_PBKDF2 kdf(pbkdf_prf); // takes ownership of pointer SecureVector<byte> cmp = kdf.derive_key( PASSHASH9_PBKDF_OUTPUT_LEN, pass, &bin[ALGID_BYTES + WORKFACTOR_BYTES], SALT_BYTES, kdf_iterations).bits_of(); return same_mem(&cmp[0], &bin[ALGID_BYTES + WORKFACTOR_BYTES + SALT_BYTES], PASSHASH9_PBKDF_OUTPUT_LEN); } } /* * PBE Retrieval * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #if defined(BOTAN_HAS_PBE_PKCS_V15) #endif #if defined(BOTAN_HAS_PBE_PKCS_V20) #endif namespace Botan { /* * Get an encryption PBE, set new parameters */ PBE* get_pbe(const std::string& algo_spec) { SCAN_Name request(algo_spec); const std::string pbe = request.algo_name(); std::string digest_name = request.arg(0); const std::string cipher = request.arg(1); std::vector<std::string> cipher_spec = split_on(cipher, '/'); if(cipher_spec.size() != 2) throw Invalid_Argument("PBE: Invalid cipher spec " + cipher); const std::string cipher_algo = global_state().deref_alias(cipher_spec[0]); const std::string cipher_mode = cipher_spec[1]; if(cipher_mode != "CBC") throw Invalid_Argument("PBE: Invalid cipher mode " + cipher); Algorithm_Factory& af = global_state().algorithm_factory(); const BlockCipher* block_cipher = af.prototype_block_cipher(cipher_algo); if(!block_cipher) throw Algorithm_Not_Found(cipher_algo); const HashFunction* hash_function = af.prototype_hash_function(digest_name); if(!hash_function) throw Algorithm_Not_Found(digest_name); if(request.arg_count() != 2) throw Invalid_Algorithm_Name(algo_spec); #if defined(BOTAN_HAS_PBE_PKCS_V15) if(pbe == "PBE-PKCS5v15") return new PBE_PKCS5v15(block_cipher->clone(), hash_function->clone(), ENCRYPTION); #endif #if defined(BOTAN_HAS_PBE_PKCS_V20) if(pbe == "PBE-PKCS5v20") return new PBE_PKCS5v20(block_cipher->clone(), hash_function->clone()); #endif throw Algorithm_Not_Found(algo_spec); } /* * Get a decryption PBE, decode parameters */ PBE* get_pbe(const OID& pbe_oid, DataSource& params) { SCAN_Name request(OIDS::lookup(pbe_oid)); const std::string pbe = request.algo_name(); #if defined(BOTAN_HAS_PBE_PKCS_V15) if(pbe == "PBE-PKCS5v15") { if(request.arg_count() != 2) throw Invalid_Algorithm_Name(request.as_string()); std::string digest_name = request.arg(0); const std::string cipher = request.arg(1); std::vector<std::string> cipher_spec = split_on(cipher, '/'); if(cipher_spec.size() != 2) throw Invalid_Argument("PBE: Invalid cipher spec " + cipher); const std::string cipher_algo = global_state().deref_alias(cipher_spec[0]); const std::string cipher_mode = cipher_spec[1]; if(cipher_mode != "CBC") throw Invalid_Argument("PBE: Invalid cipher mode " + cipher); Algorithm_Factory& af = global_state().algorithm_factory(); const BlockCipher* block_cipher = af.prototype_block_cipher(cipher_algo); if(!block_cipher) throw Algorithm_Not_Found(cipher_algo); const HashFunction* hash_function = af.prototype_hash_function(digest_name); if(!hash_function) throw Algorithm_Not_Found(digest_name); PBE* pbe = new PBE_PKCS5v15(block_cipher->clone(), hash_function->clone(), DECRYPTION); pbe->decode_params(params); return pbe; } #endif #if defined(BOTAN_HAS_PBE_PKCS_V20) if(pbe == "PBE-PKCS5v20") return new PBE_PKCS5v20(params); #endif throw Algorithm_Not_Found(pbe_oid.as_string()); } } /* * PKCS #5 PBES1 * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> namespace Botan { /* * Encrypt some bytes using PBES1 */ void PBE_PKCS5v15::write(const byte input[], size_t length) { pipe.write(input, length); flush_pipe(true); } /* * Start encrypting with PBES1 */ void PBE_PKCS5v15::start_msg() { if(direction == ENCRYPTION) pipe.append(new CBC_Encryption(block_cipher->clone(), new PKCS7_Padding, key, iv)); else pipe.append(new CBC_Decryption(block_cipher->clone(), new PKCS7_Padding, key, iv)); pipe.start_msg(); if(pipe.message_count() > 1) pipe.set_default_msg(pipe.default_msg() + 1); } /* * Finish encrypting with PBES1 */ void PBE_PKCS5v15::end_msg() { pipe.end_msg(); flush_pipe(false); pipe.reset(); } /* * Flush the pipe */ void PBE_PKCS5v15::flush_pipe(bool safe_to_skip) { if(safe_to_skip && pipe.remaining() < 64) return; SecureVector<byte> buffer(DEFAULT_BUFFERSIZE); while(pipe.remaining()) { size_t got = pipe.read(&buffer[0], buffer.size()); send(buffer, got); } } /* * Set the passphrase to use */ void PBE_PKCS5v15::set_key(const std::string& passphrase) { PKCS5_PBKDF1 pbkdf(hash_function->clone()); SecureVector<byte> key_and_iv = pbkdf.derive_key(16, passphrase, &salt[0], salt.size(), iterations).bits_of(); key.resize(8); iv.resize(8); copy_mem(&key[0], &key_and_iv[0], 8); copy_mem(&iv[0], &key_and_iv[8], 8); } /* * Create a new set of PBES1 parameters */ void PBE_PKCS5v15::new_params(RandomNumberGenerator& rng) { iterations = 10000; salt = rng.random_vec(8); } /* * Encode PKCS#5 PBES1 parameters */ MemoryVector<byte> PBE_PKCS5v15::encode_params() const { return DER_Encoder() .start_cons(SEQUENCE) .encode(salt, OCTET_STRING) .encode(iterations) .end_cons() .get_contents(); } /* * Decode PKCS#5 PBES1 parameters */ void PBE_PKCS5v15::decode_params(DataSource& source) { BER_Decoder(source) .start_cons(SEQUENCE) .decode(salt, OCTET_STRING) .decode(iterations) .verify_end() .end_cons(); if(salt.size() != 8) throw Decoding_Error("PBES1: Encoded salt is not 8 octets"); } /* * Return an OID for this PBES1 type */ OID PBE_PKCS5v15::get_oid() const { const OID base_pbes1_oid("1.2.840.113549.1.5"); const std::string cipher = block_cipher->name(); const std::string digest = hash_function->name(); if(cipher == "DES" && digest == "MD2") return (base_pbes1_oid + 1); else if(cipher == "DES" && digest == "MD5") return (base_pbes1_oid + 3); else if(cipher == "DES" && digest == "SHA-160") return (base_pbes1_oid + 10); else if(cipher == "RC2" && digest == "MD2") return (base_pbes1_oid + 4); else if(cipher == "RC2" && digest == "MD5") return (base_pbes1_oid + 6); else if(cipher == "RC2" && digest == "SHA-160") return (base_pbes1_oid + 11); else throw Internal_Error("PBE-PKCS5 v1.5: get_oid() has run out of options"); } std::string PBE_PKCS5v15::name() const { return "PBE-PKCS5v15(" + block_cipher->name() + "," + hash_function->name() + ")"; } /* * PKCS#5 v1.5 PBE Constructor */ PBE_PKCS5v15::PBE_PKCS5v15(BlockCipher* cipher, HashFunction* hash, Cipher_Dir dir) : direction(dir), block_cipher(cipher), hash_function(hash) { if(cipher->name() != "DES" && cipher->name() != "RC2") { throw Invalid_Argument("PBE_PKCS5v1.5: Unknown cipher " + cipher->name()); } if(hash->name() != "MD2" && hash->name() != "MD5" && hash->name() != "SHA-160") { throw Invalid_Argument("PBE_PKCS5v1.5: Unknown hash " + hash->name()); } } PBE_PKCS5v15::~PBE_PKCS5v15() { delete block_cipher; delete hash_function; } } /* * PKCS #5 PBES2 * (C) 1999-2008 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> #include <memory> namespace Botan { /* * Encrypt some bytes using PBES2 */ void PBE_PKCS5v20::write(const byte input[], size_t length) { pipe.write(input, length); flush_pipe(true); } /* * Start encrypting with PBES2 */ void PBE_PKCS5v20::start_msg() { if(direction == ENCRYPTION) pipe.append(new CBC_Encryption(block_cipher->clone(), new PKCS7_Padding, key, iv)); else pipe.append(new CBC_Decryption(block_cipher->clone(), new PKCS7_Padding, key, iv)); pipe.start_msg(); if(pipe.message_count() > 1) pipe.set_default_msg(pipe.default_msg() + 1); } /* * Finish encrypting with PBES2 */ void PBE_PKCS5v20::end_msg() { pipe.end_msg(); flush_pipe(false); pipe.reset(); } /* * Flush the pipe */ void PBE_PKCS5v20::flush_pipe(bool safe_to_skip) { if(safe_to_skip && pipe.remaining() < 64) return; SecureVector<byte> buffer(DEFAULT_BUFFERSIZE); while(pipe.remaining()) { size_t got = pipe.read(&buffer[0], buffer.size()); send(buffer, got); } } /* * Set the passphrase to use */ void PBE_PKCS5v20::set_key(const std::string& passphrase) { PKCS5_PBKDF2 pbkdf(new HMAC(hash_function->clone())); key = pbkdf.derive_key(key_length, passphrase, &salt[0], salt.size(), iterations).bits_of(); } /* * Create a new set of PBES2 parameters */ void PBE_PKCS5v20::new_params(RandomNumberGenerator& rng) { iterations = 10000; key_length = block_cipher->maximum_keylength(); salt = rng.random_vec(12); iv = rng.random_vec(block_cipher->block_size()); } /* * Encode PKCS#5 PBES2 parameters */ MemoryVector<byte> PBE_PKCS5v20::encode_params() const { return DER_Encoder() .start_cons(SEQUENCE) .encode( AlgorithmIdentifier("PKCS5.PBKDF2", DER_Encoder() .start_cons(SEQUENCE) .encode(salt, OCTET_STRING) .encode(iterations) .encode(key_length) .end_cons() .get_contents() ) ) .encode( AlgorithmIdentifier(block_cipher->name() + "/CBC", DER_Encoder() .encode(iv, OCTET_STRING) .get_contents() ) ) .end_cons() .get_contents(); } /* * Decode PKCS#5 PBES2 parameters */ void PBE_PKCS5v20::decode_params(DataSource& source) { AlgorithmIdentifier kdf_algo, enc_algo; BER_Decoder(source) .start_cons(SEQUENCE) .decode(kdf_algo) .decode(enc_algo) .verify_end() .end_cons(); if(kdf_algo.oid == OIDS::lookup("PKCS5.PBKDF2")) { BER_Decoder(kdf_algo.parameters) .start_cons(SEQUENCE) .decode(salt, OCTET_STRING) .decode(iterations) .decode_optional(key_length, INTEGER, UNIVERSAL) .verify_end() .end_cons(); } else throw Decoding_Error("PBE-PKCS5 v2.0: Unknown KDF algorithm " + kdf_algo.oid.as_string()); Algorithm_Factory& af = global_state().algorithm_factory(); std::string cipher = OIDS::lookup(enc_algo.oid); std::vector<std::string> cipher_spec = split_on(cipher, '/'); if(cipher_spec.size() != 2) throw Decoding_Error("PBE-PKCS5 v2.0: Invalid cipher spec " + cipher); if(!known_cipher(cipher_spec[0]) || cipher_spec[1] != "CBC") throw Decoding_Error("PBE-PKCS5 v2.0: Don't know param format for " + cipher); BER_Decoder(enc_algo.parameters).decode(iv, OCTET_STRING).verify_end(); block_cipher = af.make_block_cipher(cipher_spec[0]); hash_function = af.make_hash_function("SHA-160"); if(key_length == 0) key_length = block_cipher->maximum_keylength(); if(salt.size() < 8) throw Decoding_Error("PBE-PKCS5 v2.0: Encoded salt is too small"); } /* * Return an OID for PBES2 */ OID PBE_PKCS5v20::get_oid() const { return OIDS::lookup("PBE-PKCS5v20"); } /* * Check if this is a known PBES2 cipher */ bool PBE_PKCS5v20::known_cipher(const std::string& algo) { if(algo == "AES-128" || algo == "AES-192" || algo == "AES-256") return true; if(algo == "DES" || algo == "TripleDES") return true; return false; } std::string PBE_PKCS5v20::name() const { return "PBE-PKCS5v20(" + block_cipher->name() + "," + hash_function->name() + ")"; } /* * PKCS#5 v2.0 PBE Constructor */ PBE_PKCS5v20::PBE_PKCS5v20(BlockCipher* cipher, HashFunction* digest) : direction(ENCRYPTION), block_cipher(cipher), hash_function(digest) { if(!known_cipher(block_cipher->name())) throw Invalid_Argument("PBE-PKCS5 v2.0: Invalid cipher " + cipher->name()); if(hash_function->name() != "SHA-160") throw Invalid_Argument("PBE-PKCS5 v2.0: Invalid digest " + digest->name()); } /* * PKCS#5 v2.0 PBE Constructor */ PBE_PKCS5v20::PBE_PKCS5v20(DataSource& params) : direction(DECRYPTION) { hash_function = 0; block_cipher = 0; decode_params(params); } PBE_PKCS5v20::~PBE_PKCS5v20() { delete hash_function; delete block_cipher; } } /* * PBKDF1 * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Return a PKCS#5 PBKDF1 derived key */ OctetString PKCS5_PBKDF1::derive_key(size_t key_len, const std::string& passphrase, const byte salt[], size_t salt_size, size_t iterations) const { if(iterations == 0) throw Invalid_Argument("PKCS5_PBKDF1: Invalid iteration count"); if(key_len > hash->output_length()) throw Invalid_Argument("PKCS5_PBKDF1: Requested output length too long"); hash->update(passphrase); hash->update(salt, salt_size); SecureVector<byte> key = hash->final(); for(size_t j = 1; j != iterations; ++j) { hash->update(key); hash->final(&key[0]); } return OctetString(&key[0], std::min<size_t>(key_len, key.size())); } } /* * PBKDF2 * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Return a PKCS #5 PBKDF2 derived key */ OctetString PKCS5_PBKDF2::derive_key(size_t key_len, const std::string& passphrase, const byte salt[], size_t salt_size, size_t iterations) const { if(iterations == 0) throw Invalid_Argument("PKCS#5 PBKDF2: Invalid iteration count"); try { mac->set_key(reinterpret_cast<const byte*>(passphrase.data()), passphrase.length()); } catch(Invalid_Key_Length) { throw Exception(name() + " cannot accept passphrases of length " + to_string(passphrase.length())); } SecureVector<byte> key(key_len); byte* T = &key[0]; SecureVector<byte> U(mac->output_length()); u32bit counter = 1; while(key_len) { size_t T_size = std::min<size_t>(mac->output_length(), key_len); mac->update(salt, salt_size); mac->update_be(counter); mac->final(&U[0]); xor_buf(T, U, T_size); for(size_t j = 1; j != iterations; ++j) { mac->update(U); mac->final(&U[0]); xor_buf(T, U, T_size); } key_len -= T_size; T += T_size; ++counter; } return key; } } /* * OpenPGP S2K * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Derive a key using the OpenPGP S2K algorithm */ OctetString OpenPGP_S2K::derive_key(size_t key_len, const std::string& passphrase, const byte salt_buf[], size_t salt_size, size_t iterations) const { SecureVector<byte> key(key_len), hash_buf; size_t pass = 0, generated = 0, total_size = passphrase.size() + salt_size; size_t to_hash = std::max(iterations, total_size); hash->clear(); while(key_len > generated) { for(size_t j = 0; j != pass; ++j) hash->update(0); size_t left = to_hash; while(left >= total_size) { hash->update(salt_buf, salt_size); hash->update(passphrase); left -= total_size; } if(left <= salt_size) hash->update(salt_buf, left); else { hash->update(salt_buf, salt_size); left -= salt_size; hash->update(reinterpret_cast<const byte*>(passphrase.data()), left); } hash_buf = hash->final(); key.copy(generated, &hash_buf[0], hash->output_length()); generated += hash->output_length(); ++pass; } return key; } } /* * EME Base Class * (C) 1999-2008 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Encode a message */ SecureVector<byte> EME::encode(const byte msg[], size_t msg_len, size_t key_bits, RandomNumberGenerator& rng) const { return pad(msg, msg_len, key_bits, rng); } /* * Encode a message */ SecureVector<byte> EME::encode(const MemoryRegion<byte>& msg, size_t key_bits, RandomNumberGenerator& rng) const { return pad(&msg[0], msg.size(), key_bits, rng); } /* * Decode a message */ SecureVector<byte> EME::decode(const byte msg[], size_t msg_len, size_t key_bits) const { return unpad(msg, msg_len, key_bits); } /* * Decode a message */ SecureVector<byte> EME::decode(const MemoryRegion<byte>& msg, size_t key_bits) const { return unpad(&msg[0], msg.size(), key_bits); } } /* * EME1 (aka OAEP) * (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <memory> namespace Botan { /* * EME1 Pad Operation */ SecureVector<byte> EME1::pad(const byte in[], size_t in_length, size_t key_length, RandomNumberGenerator& rng) const { key_length /= 8; if(in_length > key_length - 2*Phash.size() - 1) throw Invalid_Argument("EME1: Input is too large"); SecureVector<byte> out(key_length); rng.randomize(&out[0], Phash.size()); out.copy(Phash.size(), &Phash[0], Phash.size()); out[out.size() - in_length - 1] = 0x01; out.copy(out.size() - in_length, in, in_length); mgf->mask(&out[0], Phash.size(), &out[Phash.size()], out.size() - Phash.size()); mgf->mask(&out[Phash.size()], out.size() - Phash.size(), &out[0], Phash.size()); return out; } /* * EME1 Unpad Operation */ SecureVector<byte> EME1::unpad(const byte in[], size_t in_length, size_t key_length) const { /* Must be careful about error messages here; if an attacker can distinguish them, it is easy to use the differences as an oracle to find the secret key, as described in "A Chosen Ciphertext Attack on RSA Optimal Asymmetric Encryption Padding (OAEP) as Standardized in PKCS #1 v2.0", James Manger, Crypto 2001 Also have to be careful about timing attacks! Pointed out by Falko Strenzke. */ key_length /= 8; // Invalid input: truncate to zero length input, causing later // checks to fail if(in_length > key_length) in_length = 0; SecureVector<byte> input(key_length); input.copy(key_length - in_length, in, in_length); mgf->mask(&input[Phash.size()], input.size() - Phash.size(), &input[0], Phash.size()); mgf->mask(&input[0], Phash.size(), &input[Phash.size()], input.size() - Phash.size()); bool waiting_for_delim = true; bool bad_input = false; size_t delim_idx = 2 * Phash.size(); /* * GCC 4.5 on x86-64 compiles this in a way that is still vunerable * to timing analysis. Other compilers, or GCC on other platforms, * may or may not. */ for(size_t i = delim_idx; i != input.size(); ++i) { const bool zero_p = !input[i]; const bool one_p = input[i] == 0x01; const bool add_1 = waiting_for_delim && zero_p; bad_input |= waiting_for_delim && !(zero_p || one_p); delim_idx += add_1; waiting_for_delim &= zero_p; } // If we never saw any non-zero byte, then it's not valid input bad_input |= waiting_for_delim; bad_input |= !same_mem(&input[Phash.size()], &Phash[0], Phash.size()); if(bad_input) throw Decoding_Error("Invalid EME1 encoding"); return SecureVector<byte>(input + delim_idx + 1, input.size() - delim_idx - 1); } /* * Return the max input size for a given key size */ size_t EME1::maximum_input_size(size_t keybits) const { if(keybits / 8 > 2*Phash.size() + 1) return ((keybits / 8) - 2*Phash.size() - 1); else return 0; } /* * EME1 Constructor */ EME1::EME1(HashFunction* hash, const std::string& P) { Phash = hash->process(P); mgf = new MGF1(hash); } } /* * PKCS1 EME * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * PKCS1 Pad Operation */ SecureVector<byte> EME_PKCS1v15::pad(const byte in[], size_t inlen, size_t olen, RandomNumberGenerator& rng) const { olen /= 8; if(olen < 10) throw Encoding_Error("PKCS1: Output space too small"); if(inlen > olen - 10) throw Encoding_Error("PKCS1: Input is too large"); SecureVector<byte> out(olen); out[0] = 0x02; for(size_t j = 1; j != olen - inlen - 1; ++j) while(out[j] == 0) out[j] = rng.next_byte(); out.copy(olen - inlen, in, inlen); return out; } /* * PKCS1 Unpad Operation */ SecureVector<byte> EME_PKCS1v15::unpad(const byte in[], size_t inlen, size_t key_len) const { if(inlen != key_len / 8 || inlen < 10 || in[0] != 0x02) throw Decoding_Error("PKCS1::unpad"); size_t seperator = 0; for(size_t j = 0; j != inlen; ++j) if(in[j] == 0) { seperator = j; break; } if(seperator < 9) throw Decoding_Error("PKCS1::unpad"); return SecureVector<byte>(in + seperator + 1, inlen - seperator - 1); } /* * Return the max input size for a given key size */ size_t EME_PKCS1v15::maximum_input_size(size_t keybits) const { if(keybits / 8 > 10) return ((keybits / 8) - 10); else return 0; } } /* * EMSA1 * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { SecureVector<byte> emsa1_encoding(const MemoryRegion<byte>& msg, size_t output_bits) { if(8*msg.size() <= output_bits) return msg; size_t shift = 8*msg.size() - output_bits; size_t byte_shift = shift / 8, bit_shift = shift % 8; SecureVector<byte> digest(msg.size() - byte_shift); for(size_t j = 0; j != msg.size() - byte_shift; ++j) digest[j] = msg[j]; if(bit_shift) { byte carry = 0; for(size_t j = 0; j != digest.size(); ++j) { byte temp = digest[j]; digest[j] = (temp >> bit_shift) | carry; carry = (temp << (8 - bit_shift)); } } return digest; } } /* * EMSA1 Update Operation */ void EMSA1::update(const byte input[], size_t length) { hash->update(input, length); } /* * Return the raw (unencoded) data */ SecureVector<byte> EMSA1::raw_data() { return hash->final(); } /* * EMSA1 Encode Operation */ SecureVector<byte> EMSA1::encoding_of(const MemoryRegion<byte>& msg, size_t output_bits, RandomNumberGenerator&) { if(msg.size() != hash->output_length()) throw Encoding_Error("EMSA1::encoding_of: Invalid size for input"); return emsa1_encoding(msg, output_bits); } /* * EMSA1 Decode/Verify Operation */ bool EMSA1::verify(const MemoryRegion<byte>& coded, const MemoryRegion<byte>& raw, size_t key_bits) { try { if(raw.size() != hash->output_length()) throw Encoding_Error("EMSA1::encoding_of: Invalid size for input"); SecureVector<byte> our_coding = emsa1_encoding(raw, key_bits); if(our_coding == coded) return true; if(our_coding[0] != 0) return false; if(our_coding.size() <= coded.size()) return false; size_t offset = 0; while(our_coding[offset] == 0 && offset < our_coding.size()) ++offset; if(our_coding.size() - offset != coded.size()) return false; for(size_t j = 0; j != coded.size(); ++j) if(coded[j] != our_coding[j+offset]) return false; return true; } catch(Invalid_Argument) { return false; } } } /* * EMSA1 BSI * (C) 1999-2008 Jack Lloyd * 2008 Falko Strenzke, FlexSecure GmbH * * Distributed under the terms of the Botan license */ namespace Botan { /* * EMSA1 BSI Encode Operation */ SecureVector<byte> EMSA1_BSI::encoding_of(const MemoryRegion<byte>& msg, size_t output_bits, RandomNumberGenerator&) { if(msg.size() != hash_ptr()->output_length()) throw Encoding_Error("EMSA1_BSI::encoding_of: Invalid size for input"); if(8*msg.size() <= output_bits) return msg; throw Encoding_Error("EMSA1_BSI::encoding_of: max key input size exceeded"); } } /* * EMSA2 * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { /* * EMSA2 Encode Operation */ SecureVector<byte> emsa2_encoding(const MemoryRegion<byte>& msg, size_t output_bits, const MemoryRegion<byte>& empty_hash, byte hash_id) { const size_t HASH_SIZE = empty_hash.size(); size_t output_length = (output_bits + 1) / 8; if(msg.size() != HASH_SIZE) throw Encoding_Error("EMSA2::encoding_of: Bad input length"); if(output_length < HASH_SIZE + 4) throw Encoding_Error("EMSA2::encoding_of: Output length is too small"); bool empty = true; for(size_t j = 0; j != HASH_SIZE; ++j) if(empty_hash[j] != msg[j]) empty = false; SecureVector<byte> output(output_length); output[0] = (empty ? 0x4B : 0x6B); output[output_length - 3 - HASH_SIZE] = 0xBA; set_mem(&output[1], output_length - 4 - HASH_SIZE, 0xBB); output.copy(output_length - (HASH_SIZE + 2), &msg[0], msg.size()); output[output_length-2] = hash_id; output[output_length-1] = 0xCC; return output; } } /* * EMSA2 Update Operation */ void EMSA2::update(const byte input[], size_t length) { hash->update(input, length); } /* * Return the raw (unencoded) data */ SecureVector<byte> EMSA2::raw_data() { return hash->final(); } /* * EMSA2 Encode Operation */ SecureVector<byte> EMSA2::encoding_of(const MemoryRegion<byte>& msg, size_t output_bits, RandomNumberGenerator&) { return emsa2_encoding(msg, output_bits, empty_hash, hash_id); } /* * EMSA2 Verify Operation */ bool EMSA2::verify(const MemoryRegion<byte>& coded, const MemoryRegion<byte>& raw, size_t key_bits) { try { return (coded == emsa2_encoding(raw, key_bits, empty_hash, hash_id)); } catch(...) { return false; } } /* * EMSA2 Constructor */ EMSA2::EMSA2(HashFunction* hash_in) : hash(hash_in) { empty_hash = hash->final(); hash_id = ieee1363_hash_id(hash->name()); if(hash_id == 0) { delete hash; throw Encoding_Error("EMSA2 cannot be used with " + hash->name()); } } } /* * EMSA3 and EMSA3_Raw * (C) 1999-2008 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { /* * EMSA3 Encode Operation */ SecureVector<byte> emsa3_encoding(const MemoryRegion<byte>& msg, size_t output_bits, const byte hash_id[], size_t hash_id_length) { size_t output_length = output_bits / 8; if(output_length < hash_id_length + msg.size() + 10) throw Encoding_Error("emsa3_encoding: Output length is too small"); SecureVector<byte> T(output_length); const size_t P_LENGTH = output_length - msg.size() - hash_id_length - 2; T[0] = 0x01; set_mem(&T[1], P_LENGTH, 0xFF); T[P_LENGTH+1] = 0x00; T.copy(P_LENGTH+2, hash_id, hash_id_length); T.copy(output_length-msg.size(), &msg[0], msg.size()); return T; } } /* * EMSA3 Update Operation */ void EMSA3::update(const byte input[], size_t length) { hash->update(input, length); } /* * Return the raw (unencoded) data */ SecureVector<byte> EMSA3::raw_data() { return hash->final(); } /* * EMSA3 Encode Operation */ SecureVector<byte> EMSA3::encoding_of(const MemoryRegion<byte>& msg, size_t output_bits, RandomNumberGenerator&) { if(msg.size() != hash->output_length()) throw Encoding_Error("EMSA3::encoding_of: Bad input length"); return emsa3_encoding(msg, output_bits, &hash_id[0], hash_id.size()); } /* * Default signature decoding */ bool EMSA3::verify(const MemoryRegion<byte>& coded, const MemoryRegion<byte>& raw, size_t key_bits) { if(raw.size() != hash->output_length()) return false; try { return (coded == emsa3_encoding(raw, key_bits, &hash_id[0], hash_id.size())); } catch(...) { return false; } } /* * EMSA3 Constructor */ EMSA3::EMSA3(HashFunction* hash_in) : hash(hash_in) { hash_id = pkcs_hash_id(hash->name()); } /* * EMSA3 Destructor */ EMSA3::~EMSA3() { delete hash; } /* * EMSA3_Raw Update Operation */ void EMSA3_Raw::update(const byte input[], size_t length) { message += std::make_pair(input, length); } /* * Return the raw (unencoded) data */ SecureVector<byte> EMSA3_Raw::raw_data() { SecureVector<byte> ret; std::swap(ret, message); return ret; } /* * EMSA3_Raw Encode Operation */ SecureVector<byte> EMSA3_Raw::encoding_of(const MemoryRegion<byte>& msg, size_t output_bits, RandomNumberGenerator&) { return emsa3_encoding(msg, output_bits, 0, 0); } /* * Default signature decoding */ bool EMSA3_Raw::verify(const MemoryRegion<byte>& coded, const MemoryRegion<byte>& raw, size_t key_bits) { try { return (coded == emsa3_encoding(raw, key_bits, 0, 0)); } catch(...) { return false; } } } /* * EMSA4 * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * EMSA4 Update Operation */ void EMSA4::update(const byte input[], size_t length) { hash->update(input, length); } /* * Return the raw (unencoded) data */ SecureVector<byte> EMSA4::raw_data() { return hash->final(); } /* * EMSA4 Encode Operation */ SecureVector<byte> EMSA4::encoding_of(const MemoryRegion<byte>& msg, size_t output_bits, RandomNumberGenerator& rng) { const size_t HASH_SIZE = hash->output_length(); if(msg.size() != HASH_SIZE) throw Encoding_Error("EMSA4::encoding_of: Bad input length"); if(output_bits < 8*HASH_SIZE + 8*SALT_SIZE + 9) throw Encoding_Error("EMSA4::encoding_of: Output length is too small"); const size_t output_length = (output_bits + 7) / 8; SecureVector<byte> salt = rng.random_vec(SALT_SIZE); for(size_t j = 0; j != 8; ++j) hash->update(0); hash->update(msg); hash->update(salt, SALT_SIZE); SecureVector<byte> H = hash->final(); SecureVector<byte> EM(output_length); EM[output_length - HASH_SIZE - SALT_SIZE - 2] = 0x01; EM.copy(output_length - 1 - HASH_SIZE - SALT_SIZE, salt, SALT_SIZE); mgf->mask(H, HASH_SIZE, EM, output_length - HASH_SIZE - 1); EM[0] &= 0xFF >> (8 * ((output_bits + 7) / 8) - output_bits); EM.copy(output_length - 1 - HASH_SIZE, H, HASH_SIZE); EM[output_length-1] = 0xBC; return EM; } /* * EMSA4 Decode/Verify Operation */ bool EMSA4::verify(const MemoryRegion<byte>& const_coded, const MemoryRegion<byte>& raw, size_t key_bits) { const size_t HASH_SIZE = hash->output_length(); const size_t KEY_BYTES = (key_bits + 7) / 8; if(key_bits < 8*HASH_SIZE + 9) return false; if(raw.size() != HASH_SIZE) return false; if(const_coded.size() > KEY_BYTES || const_coded.size() <= 1) return false; if(const_coded[const_coded.size()-1] != 0xBC) return false; SecureVector<byte> coded = const_coded; if(coded.size() < KEY_BYTES) { SecureVector<byte> temp(KEY_BYTES); temp.copy(KEY_BYTES - coded.size(), coded, coded.size()); coded = temp; } const size_t TOP_BITS = 8 * ((key_bits + 7) / 8) - key_bits; if(TOP_BITS > 8 - high_bit(coded[0])) return false; SecureVector<byte> DB(&coded[0], coded.size() - HASH_SIZE - 1); SecureVector<byte> H(&coded[coded.size() - HASH_SIZE - 1], HASH_SIZE); mgf->mask(H, H.size(), DB, coded.size() - H.size() - 1); DB[0] &= 0xFF >> TOP_BITS; size_t salt_offset = 0; for(size_t j = 0; j != DB.size(); ++j) { if(DB[j] == 0x01) { salt_offset = j + 1; break; } if(DB[j]) return false; } if(salt_offset == 0) return false; SecureVector<byte> salt(&DB[salt_offset], DB.size() - salt_offset); for(size_t j = 0; j != 8; ++j) hash->update(0); hash->update(raw); hash->update(salt); SecureVector<byte> H2 = hash->final(); return (H == H2); } /* * EMSA4 Constructor */ EMSA4::EMSA4(HashFunction* h) : SALT_SIZE(h->output_length()), hash(h) { mgf = new MGF1(hash->clone()); } /* * EMSA4 Constructor */ EMSA4::EMSA4(HashFunction* h, size_t salt_size) : SALT_SIZE(salt_size), hash(h) { mgf = new MGF1(hash->clone()); } } /* * EMSA-Raw * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * EMSA-Raw Encode Operation */ void EMSA_Raw::update(const byte input[], size_t length) { message += std::make_pair(input, length); } /* * Return the raw (unencoded) data */ SecureVector<byte> EMSA_Raw::raw_data() { SecureVector<byte> output; std::swap(message, output); return output; } /* * EMSA-Raw Encode Operation */ SecureVector<byte> EMSA_Raw::encoding_of(const MemoryRegion<byte>& msg, size_t, RandomNumberGenerator&) { return msg; } /* * EMSA-Raw Verify Operation */ bool EMSA_Raw::verify(const MemoryRegion<byte>& coded, const MemoryRegion<byte>& raw, size_t) { if(coded.size() == raw.size()) return (coded == raw); if(coded.size() > raw.size()) return false; // handle zero padding differences const size_t leading_zeros_expected = raw.size() - coded.size(); bool same_modulo_leading_zeros = true; for(size_t i = 0; i != leading_zeros_expected; ++i) if(raw[i]) same_modulo_leading_zeros = false; if(!same_mem(&coded[0], &raw[leading_zeros_expected], coded.size())) same_modulo_leading_zeros = false; return same_modulo_leading_zeros; } } /* * Hash Function Identification * (C) 1999-2008 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { const byte MD2_PKCS_ID[] = { 0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 }; const byte MD5_PKCS_ID[] = { 0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 }; const byte RIPEMD_128_PKCS_ID[] = { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x24, 0x03, 0x02, 0x02, 0x05, 0x00, 0x04, 0x14 }; const byte RIPEMD_160_PKCS_ID[] = { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x24, 0x03, 0x02, 0x01, 0x05, 0x00, 0x04, 0x14 }; const byte SHA_160_PKCS_ID[] = { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14 }; const byte SHA_224_PKCS_ID[] = { 0x30, 0x2D, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1C }; const byte SHA_256_PKCS_ID[] = { 0x30, 0x31, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20 }; const byte SHA_384_PKCS_ID[] = { 0x30, 0x41, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30 }; const byte SHA_512_PKCS_ID[] = { 0x30, 0x51, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40 }; const byte TIGER_PKCS_ID[] = { 0x30, 0x29, 0x30, 0x0D, 0x06, 0x09, 0x2B, 0x06, 0x01, 0x04, 0x01, 0xDA, 0x47, 0x0C, 0x02, 0x05, 0x00, 0x04, 0x18 }; } /* * HashID as specified by PKCS */ MemoryVector<byte> pkcs_hash_id(const std::string& name) { // Special case for SSL/TLS RSA signatures if(name == "Parallel(MD5,SHA-160)") return MemoryVector<byte>(); if(name == "MD2") return MemoryVector<byte>(MD2_PKCS_ID, sizeof(MD2_PKCS_ID)); if(name == "MD5") return MemoryVector<byte>(MD5_PKCS_ID, sizeof(MD5_PKCS_ID)); if(name == "RIPEMD-128") return MemoryVector<byte>(RIPEMD_128_PKCS_ID, sizeof(RIPEMD_128_PKCS_ID)); if(name == "RIPEMD-160") return MemoryVector<byte>(RIPEMD_160_PKCS_ID, sizeof(RIPEMD_160_PKCS_ID)); if(name == "SHA-160") return MemoryVector<byte>(SHA_160_PKCS_ID, sizeof(SHA_160_PKCS_ID)); if(name == "SHA-224") return MemoryVector<byte>(SHA_224_PKCS_ID, sizeof(SHA_224_PKCS_ID)); if(name == "SHA-256") return MemoryVector<byte>(SHA_256_PKCS_ID, sizeof(SHA_256_PKCS_ID)); if(name == "SHA-384") return MemoryVector<byte>(SHA_384_PKCS_ID, sizeof(SHA_384_PKCS_ID)); if(name == "SHA-512") return MemoryVector<byte>(SHA_512_PKCS_ID, sizeof(SHA_512_PKCS_ID)); if(name == "Tiger(24,3)") return MemoryVector<byte>(TIGER_PKCS_ID, sizeof(TIGER_PKCS_ID)); throw Invalid_Argument("No PKCS #1 identifier for " + name); } /* * HashID as specified by IEEE 1363/X9.31 */ byte ieee1363_hash_id(const std::string& name) { if(name == "SHA-160") return 0x33; if(name == "SHA-224") return 0x38; if(name == "SHA-256") return 0x34; if(name == "SHA-384") return 0x36; if(name == "SHA-512") return 0x35; if(name == "RIPEMD-160") return 0x31; if(name == "RIPEMD-128") return 0x32; if(name == "Whirlpool") return 0x37; return 0; } } /* * Blinding for public key operations * (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Blinder Constructor */ Blinder::Blinder(const BigInt& e, const BigInt& d, const BigInt& n) { if(e < 1 || d < 1 || n < 1) throw Invalid_Argument("Blinder: Arguments too small"); reducer = Modular_Reducer(n); this->e = e; this->d = d; } /* * Blind a number */ BigInt Blinder::blind(const BigInt& i) const { if(!reducer.initialized()) return i; e = reducer.square(e); d = reducer.square(d); return reducer.multiply(i, e); } /* * Unblind a number */ BigInt Blinder::unblind(const BigInt& i) const { if(!reducer.initialized()) return i; return reducer.multiply(i, d); } } /* * Diffie-Hellman * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * DH_PublicKey Constructor */ DH_PublicKey::DH_PublicKey(const DL_Group& grp, const BigInt& y1) { group = grp; y = y1; } /* * Return the public value for key agreement */ MemoryVector<byte> DH_PublicKey::public_value() const { return BigInt::encode_1363(y, group_p().bytes()); } /* * Create a DH private key */ DH_PrivateKey::DH_PrivateKey(RandomNumberGenerator& rng, const DL_Group& grp, const BigInt& x_arg) { group = grp; x = x_arg; if(x == 0) { const BigInt& p = group_p(); x.randomize(rng, 2 * dl_work_factor(p.bits())); } if(y == 0) y = power_mod(group_g(), x, group_p()); if(x == 0) gen_check(rng); else load_check(rng); } /* * Load a DH private key */ DH_PrivateKey::DH_PrivateKey(const AlgorithmIdentifier& alg_id, const MemoryRegion<byte>& key_bits, RandomNumberGenerator& rng) : DL_Scheme_PrivateKey(alg_id, key_bits, DL_Group::ANSI_X9_42) { if(y == 0) y = power_mod(group_g(), x, group_p()); load_check(rng); } /* * Return the public value for key agreement */ MemoryVector<byte> DH_PrivateKey::public_value() const { return DH_PublicKey::public_value(); } DH_KA_Operation::DH_KA_Operation(const DH_PrivateKey& dh) : p(dh.group_p()), powermod_x_p(dh.get_x(), p) { BigInt k(global_state().global_rng(), p.bits() - 1); blinder = Blinder(k, powermod_x_p(inverse_mod(k, p)), p); } SecureVector<byte> DH_KA_Operation::agree(const byte w[], size_t w_len) { BigInt input = BigInt::decode(w, w_len); if(input <= 1 || input >= p - 1) throw Invalid_Argument("DH agreement - invalid key provided"); BigInt r = blinder.unblind(powermod_x_p(blinder.blind(input))); return BigInt::encode_1363(r, p.bytes()); } } /* * DL Scheme * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { AlgorithmIdentifier DL_Scheme_PublicKey::algorithm_identifier() const { return AlgorithmIdentifier(get_oid(), group.DER_encode(group_format())); } MemoryVector<byte> DL_Scheme_PublicKey::x509_subject_public_key() const { return DER_Encoder().encode(y).get_contents(); } DL_Scheme_PublicKey::DL_Scheme_PublicKey(const AlgorithmIdentifier& alg_id, const MemoryRegion<byte>& key_bits, DL_Group::Format format) { DataSource_Memory source(alg_id.parameters); group.BER_decode(source, format); BER_Decoder(key_bits).decode(y); } MemoryVector<byte> DL_Scheme_PrivateKey::pkcs8_private_key() const { return DER_Encoder().encode(x).get_contents(); } DL_Scheme_PrivateKey::DL_Scheme_PrivateKey(const AlgorithmIdentifier& alg_id, const MemoryRegion<byte>& key_bits, DL_Group::Format format) { DataSource_Memory source(alg_id.parameters); group.BER_decode(source, format); BER_Decoder(key_bits).decode(x); } /* * Check Public DL Parameters */ bool DL_Scheme_PublicKey::check_key(RandomNumberGenerator& rng, bool strong) const { if(y < 2 || y >= group_p()) return false; if(!group.verify_group(rng, strong)) return false; return true; } /* * Check DL Scheme Private Parameters */ bool DL_Scheme_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const { const BigInt& p = group_p(); const BigInt& g = group_g(); if(y < 2 || y >= p || x < 2 || x >= p) return false; if(!group.verify_group(rng, strong)) return false; if(!strong) return true; if(y != power_mod(g, x, p)) return false; return true; } } /* * Discrete Logarithm Parameters * (C) 1999-2008 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * DL_Group Constructor */ DL_Group::DL_Group() { initialized = false; } /* * DL_Group Constructor */ DL_Group::DL_Group(const std::string& type) { std::string grp_contents = global_state().get("dl", type); if(grp_contents == "") throw Invalid_Argument("DL_Group: Unknown group " + type); DataSource_Memory pem(grp_contents); PEM_decode(pem); } /* * DL_Group Constructor */ DL_Group::DL_Group(RandomNumberGenerator& rng, PrimeType type, size_t pbits, size_t qbits) { if(pbits < 512) throw Invalid_Argument("DL_Group: prime size " + to_string(pbits) + " is too small"); if(type == Strong) { p = random_safe_prime(rng, pbits); q = (p - 1) / 2; g = 2; } else if(type == Prime_Subgroup) { if(!qbits) qbits = 2 * dl_work_factor(pbits); q = random_prime(rng, qbits); BigInt X; while(p.bits() != pbits || !check_prime(p, rng)) { X.randomize(rng, pbits); p = X - (X % (2*q) - 1); } g = make_dsa_generator(p, q); } else if(type == DSA_Kosherizer) { qbits = qbits ? qbits : ((pbits <= 1024) ? 160 : 256); generate_dsa_primes(rng, global_state().algorithm_factory(), p, q, pbits, qbits); g = make_dsa_generator(p, q); } initialized = true; } /* * DL_Group Constructor */ DL_Group::DL_Group(RandomNumberGenerator& rng, const MemoryRegion<byte>& seed, size_t pbits, size_t qbits) { if(!generate_dsa_primes(rng, global_state().algorithm_factory(), p, q, pbits, qbits, seed)) throw Invalid_Argument("DL_Group: The seed given does not " "generate a DSA group"); g = make_dsa_generator(p, q); initialized = true; } /* * DL_Group Constructor */ DL_Group::DL_Group(const BigInt& p1, const BigInt& g1) { initialize(p1, 0, g1); } /* * DL_Group Constructor */ DL_Group::DL_Group(const BigInt& p1, const BigInt& q1, const BigInt& g1) { initialize(p1, q1, g1); } /* * DL_Group Initializer */ void DL_Group::initialize(const BigInt& p1, const BigInt& q1, const BigInt& g1) { if(p1 < 3) throw Invalid_Argument("DL_Group: Prime invalid"); if(g1 < 2 || g1 >= p1) throw Invalid_Argument("DL_Group: Generator invalid"); if(q1 < 0 || q1 >= p1) throw Invalid_Argument("DL_Group: Subgroup invalid"); p = p1; g = g1; q = q1; initialized = true; } /* * Verify that the group has been set */ void DL_Group::init_check() const { if(!initialized) throw Invalid_State("DLP group cannot be used uninitialized"); } /* * Verify the parameters */ bool DL_Group::verify_group(RandomNumberGenerator& rng, bool strong) const { init_check(); if(g < 2 || p < 3 || q < 0) return false; if((q != 0) && ((p - 1) % q != 0)) return false; if(!strong) return true; if(!check_prime(p, rng)) return false; if((q > 0) && !check_prime(q, rng)) return false; return true; } /* * Return the prime */ const BigInt& DL_Group::get_p() const { init_check(); return p; } /* * Return the generator */ const BigInt& DL_Group::get_g() const { init_check(); return g; } /* * Return the subgroup */ const BigInt& DL_Group::get_q() const { init_check(); if(q == 0) throw Invalid_State("DLP group has no q prime specified"); return q; } /* * DER encode the parameters */ SecureVector<byte> DL_Group::DER_encode(Format format) const { init_check(); if((q == 0) && (format != PKCS_3)) throw Encoding_Error("The ANSI DL parameter formats require a subgroup"); if(format == ANSI_X9_57) { return DER_Encoder() .start_cons(SEQUENCE) .encode(p) .encode(q) .encode(g) .end_cons() .get_contents(); } else if(format == ANSI_X9_42) { return DER_Encoder() .start_cons(SEQUENCE) .encode(p) .encode(g) .encode(q) .end_cons() .get_contents(); } else if(format == PKCS_3) { return DER_Encoder() .start_cons(SEQUENCE) .encode(p) .encode(g) .end_cons() .get_contents(); } throw Invalid_Argument("Unknown DL_Group encoding " + to_string(format)); } /* * PEM encode the parameters */ std::string DL_Group::PEM_encode(Format format) const { SecureVector<byte> encoding = DER_encode(format); if(format == PKCS_3) return PEM_Code::encode(encoding, "DH PARAMETERS"); else if(format == ANSI_X9_57) return PEM_Code::encode(encoding, "DSA PARAMETERS"); else if(format == ANSI_X9_42) return PEM_Code::encode(encoding, "X942 DH PARAMETERS"); else throw Invalid_Argument("Unknown DL_Group encoding " + to_string(format)); } /* * Decode BER encoded parameters */ void DL_Group::BER_decode(DataSource& source, Format format) { BigInt new_p, new_q, new_g; BER_Decoder decoder(source); BER_Decoder ber = decoder.start_cons(SEQUENCE); if(format == ANSI_X9_57) { ber.decode(new_p) .decode(new_q) .decode(new_g) .verify_end(); } else if(format == ANSI_X9_42) { ber.decode(new_p) .decode(new_g) .decode(new_q) .discard_remaining(); } else if(format == PKCS_3) { ber.decode(new_p) .decode(new_g) .discard_remaining(); } else throw Invalid_Argument("Unknown DL_Group encoding " + to_string(format)); initialize(new_p, new_q, new_g); } /* * Decode PEM encoded parameters */ void DL_Group::PEM_decode(DataSource& source) { std::string label; DataSource_Memory ber(PEM_Code::decode(source, label)); if(label == "DH PARAMETERS") BER_decode(ber, PKCS_3); else if(label == "DSA PARAMETERS") BER_decode(ber, ANSI_X9_57); else if(label == "X942 DH PARAMETERS") BER_decode(ber, ANSI_X9_42); else throw Decoding_Error("DL_Group: Invalid PEM label " + label); } /* * Create generator of the q-sized subgroup (DSA style generator) */ BigInt DL_Group::make_dsa_generator(const BigInt& p, const BigInt& q) { BigInt g, e = (p - 1) / q; BOTAN_ASSERT(e > 0, "q does not divide p, invalid group"); for(size_t i = 0; i != PRIME_TABLE_SIZE; ++i) { g = power_mod(PRIMES[i], e, p); if(g > 1) return g; } throw Internal_Error("DL_Group: Couldn't create a suitable generator"); } } /* * DLIES * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * DLIES_Encryptor Constructor */ DLIES_Encryptor::DLIES_Encryptor(const PK_Key_Agreement_Key& key, KDF* kdf_obj, MessageAuthenticationCode* mac_obj, size_t mac_kl) : ka(key, "Raw"), kdf(kdf_obj), mac(mac_obj), mac_keylen(mac_kl) { my_key = key.public_value(); } DLIES_Encryptor::~DLIES_Encryptor() { delete kdf; delete mac; } /* * DLIES Encryption */ SecureVector<byte> DLIES_Encryptor::enc(const byte in[], size_t length, RandomNumberGenerator&) const { if(length > maximum_input_size()) throw Invalid_Argument("DLIES: Plaintext too large"); if(other_key.empty()) throw Invalid_State("DLIES: The other key was never set"); SecureVector<byte> out(my_key.size() + length + mac->output_length()); out.copy(&my_key[0], my_key.size()); out.copy(my_key.size(), in, length); SecureVector<byte> vz = my_key; vz += ka.derive_key(0, other_key).bits_of(); const size_t K_LENGTH = length + mac_keylen; OctetString K = kdf->derive_key(K_LENGTH, vz); if(K.length() != K_LENGTH) throw Encoding_Error("DLIES: KDF did not provide sufficient output"); byte* C = &out[my_key.size()]; xor_buf(C, K.begin() + mac_keylen, length); mac->set_key(K.begin(), mac_keylen); mac->update(C, length); for(size_t j = 0; j != 8; ++j) mac->update(0); mac->final(C + length); return out; } /* * Set the other parties public key */ void DLIES_Encryptor::set_other_key(const MemoryRegion<byte>& ok) { other_key = ok; } /* * Return the max size, in bytes, of a message */ size_t DLIES_Encryptor::maximum_input_size() const { return 32; } /* * DLIES_Decryptor Constructor */ DLIES_Decryptor::DLIES_Decryptor(const PK_Key_Agreement_Key& key, KDF* kdf_obj, MessageAuthenticationCode* mac_obj, size_t mac_kl) : ka(key, "Raw"), kdf(kdf_obj), mac(mac_obj), mac_keylen(mac_kl) { my_key = key.public_value(); } DLIES_Decryptor::~DLIES_Decryptor() { delete kdf; delete mac; } /* * DLIES Decryption */ SecureVector<byte> DLIES_Decryptor::dec(const byte msg[], size_t length) const { if(length < my_key.size() + mac->output_length()) throw Decoding_Error("DLIES decryption: ciphertext is too short"); const size_t CIPHER_LEN = length - my_key.size() - mac->output_length(); SecureVector<byte> v(msg, my_key.size()); SecureVector<byte> C(msg + my_key.size(), CIPHER_LEN); SecureVector<byte> T(msg + my_key.size() + CIPHER_LEN, mac->output_length()); SecureVector<byte> vz(msg, my_key.size()); vz += ka.derive_key(0, v).bits_of(); const size_t K_LENGTH = C.size() + mac_keylen; OctetString K = kdf->derive_key(K_LENGTH, vz); if(K.length() != K_LENGTH) throw Encoding_Error("DLIES: KDF did not provide sufficient output"); mac->set_key(K.begin(), mac_keylen); mac->update(C); for(size_t j = 0; j != 8; ++j) mac->update(0); SecureVector<byte> T2 = mac->final(); if(T != T2) throw Decoding_Error("DLIES: message authentication failed"); xor_buf(C, K.begin() + mac_keylen, C.size()); return C; } } /* * DSA * (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * DSA_PublicKey Constructor */ DSA_PublicKey::DSA_PublicKey(const DL_Group& grp, const BigInt& y1) { group = grp; y = y1; } /* * Create a DSA private key */ DSA_PrivateKey::DSA_PrivateKey(RandomNumberGenerator& rng, const DL_Group& grp, const BigInt& x_arg) { group = grp; x = x_arg; if(x == 0) x = BigInt::random_integer(rng, 2, group_q() - 1); y = power_mod(group_g(), x, group_p()); if(x_arg == 0) gen_check(rng); else load_check(rng); } DSA_PrivateKey::DSA_PrivateKey(const AlgorithmIdentifier& alg_id, const MemoryRegion<byte>& key_bits, RandomNumberGenerator& rng) : DL_Scheme_PrivateKey(alg_id, key_bits, DL_Group::ANSI_X9_57) { y = power_mod(group_g(), x, group_p()); load_check(rng); } /* * Check Private DSA Parameters */ bool DSA_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const { if(!DL_Scheme_PrivateKey::check_key(rng, strong) || x >= group_q()) return false; if(!strong) return true; return KeyPair::signature_consistency_check(rng, *this, "EMSA1(SHA-1)"); } DSA_Signature_Operation::DSA_Signature_Operation(const DSA_PrivateKey& dsa) : q(dsa.group_q()), x(dsa.get_x()), powermod_g_p(dsa.group_g(), dsa.group_p()), mod_q(dsa.group_q()) { } SecureVector<byte> DSA_Signature_Operation::sign(const byte msg[], size_t msg_len, RandomNumberGenerator& rng) { rng.add_entropy(msg, msg_len); BigInt i(msg, msg_len); BigInt r = 0, s = 0; while(r == 0 || s == 0) { BigInt k; do k.randomize(rng, q.bits()); while(k >= q); r = mod_q.reduce(powermod_g_p(k)); s = mod_q.multiply(inverse_mod(k, q), mul_add(x, r, i)); } SecureVector<byte> output(2*q.bytes()); r.binary_encode(&output[output.size() / 2 - r.bytes()]); s.binary_encode(&output[output.size() - s.bytes()]); return output; } DSA_Verification_Operation::DSA_Verification_Operation(const DSA_PublicKey& dsa) : q(dsa.group_q()), y(dsa.get_y()) { powermod_g_p = Fixed_Base_Power_Mod(dsa.group_g(), dsa.group_p()); powermod_y_p = Fixed_Base_Power_Mod(y, dsa.group_p()); mod_p = Modular_Reducer(dsa.group_p()); mod_q = Modular_Reducer(dsa.group_q()); } bool DSA_Verification_Operation::verify(const byte msg[], size_t msg_len, const byte sig[], size_t sig_len) { const BigInt& q = mod_q.get_modulus(); if(sig_len != 2*q.bytes() || msg_len > q.bytes()) return false; BigInt r(sig, q.bytes()); BigInt s(sig + q.bytes(), q.bytes()); BigInt i(msg, msg_len); if(r <= 0 || r >= q || s <= 0 || s >= q) return false; s = inverse_mod(s, q); s = mod_p.multiply(powermod_g_p(mod_q.multiply(s, i)), powermod_y_p(mod_q.multiply(s, r))); return (mod_q.reduce(s) == r); } } /* * ECC Domain Parameters * * (C) 2007 Falko Strenzke, FlexSecure GmbH * 2008 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { EC_Group::EC_Group(const OID& domain_oid) { std::string pem = global_state().get("ec", OIDS::lookup(domain_oid)); if(pem == "") throw Lookup_Error("No ECC domain data for " + domain_oid.as_string()); *this = EC_Group(pem); oid = domain_oid.as_string(); } EC_Group::EC_Group(const std::string& str) { if(str == "") return; // no initialization / uninitialized try { DataSource_Memory input(str); SecureVector<byte> ber = PEM_Code::decode_check_label(input, "EC PARAMETERS"); *this = EC_Group(ber); } catch(Decoding_Error) // hmm, not PEM? { *this = EC_Group(OIDS::lookup(str)); } } EC_Group::EC_Group(const MemoryRegion<byte>& ber_data) { BER_Decoder ber(ber_data); BER_Object obj = ber.get_next_object(); if(obj.type_tag == NULL_TAG) throw Decoding_Error("Cannot handle ImplicitCA ECDSA parameters"); else if(obj.type_tag == OBJECT_ID) { OID dom_par_oid; BER_Decoder(ber_data).decode(dom_par_oid); *this = EC_Group(dom_par_oid); } else if(obj.type_tag == SEQUENCE) { BigInt p, a, b; SecureVector<byte> sv_base_point; BER_Decoder(ber_data) .start_cons(SEQUENCE) .decode_and_check<size_t>(1, "Unknown ECC param version code") .start_cons(SEQUENCE) .decode_and_check(OID("1.2.840.10045.1.1"), "Only prime ECC fields supported") .decode(p) .end_cons() .start_cons(SEQUENCE) .decode_octet_string_bigint(a) .decode_octet_string_bigint(b) .end_cons() .decode(sv_base_point, OCTET_STRING) .decode(order) .decode(cofactor) .end_cons() .verify_end(); curve = CurveGFp(p, a, b); base_point = OS2ECP(sv_base_point, curve); } else throw Decoding_Error("Unexpected tag while decoding ECC domain params"); } SecureVector<byte> EC_Group::DER_encode(EC_Group_Encoding form) const { if(form == EC_DOMPAR_ENC_EXPLICIT) { const size_t ecpVers1 = 1; OID curve_type("1.2.840.10045.1.1"); const size_t p_bytes = curve.get_p().bytes(); return DER_Encoder() .start_cons(SEQUENCE) .encode(ecpVers1) .start_cons(SEQUENCE) .encode(curve_type) .encode(curve.get_p()) .end_cons() .start_cons(SEQUENCE) .encode(BigInt::encode_1363(curve.get_a(), p_bytes), OCTET_STRING) .encode(BigInt::encode_1363(curve.get_b(), p_bytes), OCTET_STRING) .end_cons() .encode(EC2OSP(base_point, PointGFp::UNCOMPRESSED), OCTET_STRING) .encode(order) .encode(cofactor) .end_cons() .get_contents(); } else if(form == EC_DOMPAR_ENC_OID) return DER_Encoder().encode(get_oid()).get_contents(); else if(form == EC_DOMPAR_ENC_IMPLICITCA) return DER_Encoder().encode_null().get_contents(); else throw Internal_Error("EC_Group::DER_encode: Unknown encoding"); } std::string EC_Group::PEM_encode() const { SecureVector<byte> der = DER_encode(EC_DOMPAR_ENC_EXPLICIT); return PEM_Code::encode(der, "EC PARAMETERS"); } } /* * ECC Key implemenation * (C) 2007 Manuel Hartl, FlexSecure GmbH * Falko Strenzke, FlexSecure GmbH * 2008-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { EC_PublicKey::EC_PublicKey(const EC_Group& dom_par, const PointGFp& pub_point) : domain_params(dom_par), public_key(pub_point), domain_encoding(EC_DOMPAR_ENC_EXPLICIT) { if(domain().get_curve() != public_point().get_curve()) throw Invalid_Argument("EC_PublicKey: curve mismatch in constructor"); } EC_PublicKey::EC_PublicKey(const AlgorithmIdentifier& alg_id, const MemoryRegion<byte>& key_bits) { domain_params = EC_Group(alg_id.parameters); domain_encoding = EC_DOMPAR_ENC_EXPLICIT; public_key = OS2ECP(key_bits, domain().get_curve()); } bool EC_PublicKey::check_key(RandomNumberGenerator&, bool) const { return public_point().on_the_curve(); } AlgorithmIdentifier EC_PublicKey::algorithm_identifier() const { return AlgorithmIdentifier(get_oid(), DER_domain()); } MemoryVector<byte> EC_PublicKey::x509_subject_public_key() const { return EC2OSP(public_point(), PointGFp::COMPRESSED); } void EC_PublicKey::set_parameter_encoding(EC_Group_Encoding form) { if(form != EC_DOMPAR_ENC_EXPLICIT && form != EC_DOMPAR_ENC_IMPLICITCA && form != EC_DOMPAR_ENC_OID) throw Invalid_Argument("Invalid encoding form for EC-key object specified"); if((form == EC_DOMPAR_ENC_OID) && (domain_params.get_oid() == "")) throw Invalid_Argument("Invalid encoding form OID specified for " "EC-key object whose corresponding domain " "parameters are without oid"); domain_encoding = form; } const BigInt& EC_PrivateKey::private_value() const { if(private_key == 0) throw Invalid_State("EC_PrivateKey::private_value - uninitialized"); return private_key; } /** * EC_PrivateKey constructor */ EC_PrivateKey::EC_PrivateKey(RandomNumberGenerator& rng, const EC_Group& ec_group, const BigInt& x) { domain_params = ec_group; domain_encoding = EC_DOMPAR_ENC_EXPLICIT; if(x == 0) private_key = BigInt::random_integer(rng, 1, domain().get_order()); else private_key = x; public_key = domain().get_base_point() * private_key; BOTAN_ASSERT(public_key.on_the_curve(), "ECC private key was not on the curve"); } MemoryVector<byte> EC_PrivateKey::pkcs8_private_key() const { return DER_Encoder() .start_cons(SEQUENCE) .encode(static_cast<size_t>(1)) .encode(BigInt::encode_1363(private_key, private_key.bytes()), OCTET_STRING) .end_cons() .get_contents(); } EC_PrivateKey::EC_PrivateKey(const AlgorithmIdentifier& alg_id, const MemoryRegion<byte>& key_bits) { domain_params = EC_Group(alg_id.parameters); domain_encoding = EC_DOMPAR_ENC_EXPLICIT; BER_Decoder(key_bits) .start_cons(SEQUENCE) .decode_and_check<size_t>(1, "Unknown version code for ECC key") .decode_octet_string_bigint(private_key); // possibly group params and public key follow public_key = domain().get_base_point() * private_key; BOTAN_ASSERT(public_key.on_the_curve(), "Loaded ECC private key not on the curve"); } } /* * ECDH implemenation * (C) 2007 Manuel Hartl, FlexSecure GmbH * 2007 Falko Strenzke, FlexSecure GmbH * 2008-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { ECDH_KA_Operation::ECDH_KA_Operation(const ECDH_PrivateKey& key) : curve(key.domain().get_curve()), cofactor(key.domain().get_cofactor()) { l_times_priv = inverse_mod(cofactor, key.domain().get_order()) * key.private_value(); } SecureVector<byte> ECDH_KA_Operation::agree(const byte w[], size_t w_len) { PointGFp point = OS2ECP(w, w_len, curve); PointGFp S = (cofactor * point) * l_times_priv; BOTAN_ASSERT(S.on_the_curve(), "ECDH agreed value not on the curve"); return BigInt::encode_1363(S.get_affine_x(), curve.get_p().bytes()); } } /* * ECDSA implemenation * (C) 2007 Manuel Hartl, FlexSecure GmbH * 2007 Falko Strenzke, FlexSecure GmbH * 2008-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { bool ECDSA_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const { if(!public_point().on_the_curve()) return false; if(!strong) return true; return KeyPair::signature_consistency_check(rng, *this, "EMSA1(SHA-1)"); } ECDSA_Signature_Operation::ECDSA_Signature_Operation(const ECDSA_PrivateKey& ecdsa) : base_point(ecdsa.domain().get_base_point()), order(ecdsa.domain().get_order()), x(ecdsa.private_value()), mod_order(order) { } SecureVector<byte> ECDSA_Signature_Operation::sign(const byte msg[], size_t msg_len, RandomNumberGenerator& rng) { rng.add_entropy(msg, msg_len); BigInt m(msg, msg_len); BigInt r = 0, s = 0; while(r == 0 || s == 0) { // This contortion is necessary for the tests BigInt k; k.randomize(rng, order.bits()); while(k >= order) k.randomize(rng, order.bits() - 1); PointGFp k_times_P = base_point * k; r = mod_order.reduce(k_times_P.get_affine_x()); s = mod_order.multiply(inverse_mod(k, order), mul_add(x, r, m)); } SecureVector<byte> output(2*order.bytes()); r.binary_encode(&output[output.size() / 2 - r.bytes()]); s.binary_encode(&output[output.size() - s.bytes()]); return output; } ECDSA_Verification_Operation::ECDSA_Verification_Operation(const ECDSA_PublicKey& ecdsa) : base_point(ecdsa.domain().get_base_point()), public_point(ecdsa.public_point()), order(ecdsa.domain().get_order()) { } bool ECDSA_Verification_Operation::verify(const byte msg[], size_t msg_len, const byte sig[], size_t sig_len) { if(sig_len != order.bytes()*2) return false; BigInt e(msg, msg_len); BigInt r(sig, sig_len / 2); BigInt s(sig + sig_len / 2, sig_len / 2); if(r <= 0 || r >= order || s <= 0 || s >= order) return false; BigInt w = inverse_mod(s, order); PointGFp R = w * multi_exponentiate(base_point, e, public_point, r); if(R.is_zero()) return false; return (R.get_affine_x() % order == r); } } /* * ElGamal * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * ElGamal_PublicKey Constructor */ ElGamal_PublicKey::ElGamal_PublicKey(const DL_Group& grp, const BigInt& y1) { group = grp; y = y1; } /* * ElGamal_PrivateKey Constructor */ ElGamal_PrivateKey::ElGamal_PrivateKey(RandomNumberGenerator& rng, const DL_Group& grp, const BigInt& x_arg) { group = grp; x = x_arg; if(x == 0) x.randomize(rng, 2 * dl_work_factor(group_p().bits())); y = power_mod(group_g(), x, group_p()); if(x_arg == 0) gen_check(rng); else load_check(rng); } ElGamal_PrivateKey::ElGamal_PrivateKey(const AlgorithmIdentifier& alg_id, const MemoryRegion<byte>& key_bits, RandomNumberGenerator& rng) : DL_Scheme_PrivateKey(alg_id, key_bits, DL_Group::ANSI_X9_42) { y = power_mod(group_g(), x, group_p()); load_check(rng); } /* * Check Private ElGamal Parameters */ bool ElGamal_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const { if(!DL_Scheme_PrivateKey::check_key(rng, strong)) return false; if(!strong) return true; return KeyPair::encryption_consistency_check(rng, *this, "EME1(SHA-1)"); } ElGamal_Encryption_Operation::ElGamal_Encryption_Operation(const ElGamal_PublicKey& key) { const BigInt& p = key.group_p(); powermod_g_p = Fixed_Base_Power_Mod(key.group_g(), p); powermod_y_p = Fixed_Base_Power_Mod(key.get_y(), p); mod_p = Modular_Reducer(p); } SecureVector<byte> ElGamal_Encryption_Operation::encrypt(const byte msg[], size_t msg_len, RandomNumberGenerator& rng) { const BigInt& p = mod_p.get_modulus(); BigInt m(msg, msg_len); if(m >= p) throw Invalid_Argument("ElGamal encryption: Input is too large"); BigInt k(rng, 2 * dl_work_factor(p.bits())); BigInt a = powermod_g_p(k); BigInt b = mod_p.multiply(m, powermod_y_p(k)); SecureVector<byte> output(2*p.bytes()); a.binary_encode(&output[p.bytes() - a.bytes()]); b.binary_encode(&output[output.size() / 2 + (p.bytes() - b.bytes())]); return output; } ElGamal_Decryption_Operation::ElGamal_Decryption_Operation(const ElGamal_PrivateKey& key) { const BigInt& p = key.group_p(); powermod_x_p = Fixed_Exponent_Power_Mod(key.get_x(), p); mod_p = Modular_Reducer(p); BigInt k(global_state().global_rng(), p.bits() - 1); blinder = Blinder(k, powermod_x_p(k), p); } SecureVector<byte> ElGamal_Decryption_Operation::decrypt(const byte msg[], size_t msg_len) { const BigInt& p = mod_p.get_modulus(); const size_t p_bytes = p.bytes(); if(msg_len != 2 * p_bytes) throw Invalid_Argument("ElGamal decryption: Invalid message"); BigInt a(msg, p_bytes); BigInt b(msg + p_bytes, p_bytes); if(a >= p || b >= p) throw Invalid_Argument("ElGamal decryption: Invalid message"); a = blinder.blind(a); BigInt r = mod_p.multiply(b, inverse_mod(powermod_x_p(a), p)); return BigInt::encode(blinder.unblind(r)); } } /* * GOST 34.10-2001 implemenation * (C) 2007 Falko Strenzke, FlexSecure GmbH * Manuel Hartl, FlexSecure GmbH * (C) 2008-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { MemoryVector<byte> GOST_3410_PublicKey::x509_subject_public_key() const { // Trust CryptoPro to come up with something obnoxious const BigInt x = public_point().get_affine_x(); const BigInt y = public_point().get_affine_y(); size_t part_size = std::max(x.bytes(), y.bytes()); MemoryVector<byte> bits(2*part_size); x.binary_encode(&bits[part_size - x.bytes()]); y.binary_encode(&bits[2*part_size - y.bytes()]); // Keys are stored in little endian format (WTF) for(size_t i = 0; i != part_size / 2; ++i) { std::swap(bits[i], bits[part_size-1-i]); std::swap(bits[part_size+i], bits[2*part_size-1-i]); } return DER_Encoder().encode(bits, OCTET_STRING).get_contents(); } AlgorithmIdentifier GOST_3410_PublicKey::algorithm_identifier() const { MemoryVector<byte> params = DER_Encoder().start_cons(SEQUENCE) .encode(OID(domain().get_oid())) .end_cons() .get_contents(); return AlgorithmIdentifier(get_oid(), params); } GOST_3410_PublicKey::GOST_3410_PublicKey(const AlgorithmIdentifier& alg_id, const MemoryRegion<byte>& key_bits) { OID ecc_param_id; // Also includes hash and cipher OIDs... brilliant design guys BER_Decoder(alg_id.parameters).start_cons(SEQUENCE).decode(ecc_param_id); domain_params = EC_Group(ecc_param_id); SecureVector<byte> bits; BER_Decoder(key_bits).decode(bits, OCTET_STRING); const size_t part_size = bits.size() / 2; // Keys are stored in little endian format (WTF) for(size_t i = 0; i != part_size / 2; ++i) { std::swap(bits[i], bits[part_size-1-i]); std::swap(bits[part_size+i], bits[2*part_size-1-i]); } BigInt x(&bits[0], part_size); BigInt y(&bits[part_size], part_size); public_key = PointGFp(domain().get_curve(), x, y); BOTAN_ASSERT(public_key.on_the_curve(), "Loaded GOST 34.10 public key not on the curve"); } namespace { BigInt decode_le(const byte msg[], size_t msg_len) { SecureVector<byte> msg_le(msg, msg_len); for(size_t i = 0; i != msg_le.size() / 2; ++i) std::swap(msg_le[i], msg_le[msg_le.size()-1-i]); return BigInt(&msg_le[0], msg_le.size()); } } GOST_3410_Signature_Operation::GOST_3410_Signature_Operation( const GOST_3410_PrivateKey& gost_3410) : base_point(gost_3410.domain().get_base_point()), order(gost_3410.domain().get_order()), x(gost_3410.private_value()) { } SecureVector<byte> GOST_3410_Signature_Operation::sign(const byte msg[], size_t msg_len, RandomNumberGenerator& rng) { BigInt k; do k.randomize(rng, order.bits()-1); while(k >= order); BigInt e = decode_le(msg, msg_len); e %= order; if(e == 0) e = 1; PointGFp k_times_P = base_point * k; BOTAN_ASSERT(k_times_P.on_the_curve(), "GOST 34.10 k*g not on the curve"); BigInt r = k_times_P.get_affine_x() % order; BigInt s = (r*x + k*e) % order; if(r == 0 || s == 0) throw Invalid_State("GOST 34.10: r == 0 || s == 0"); SecureVector<byte> output(2*order.bytes()); s.binary_encode(&output[output.size() / 2 - s.bytes()]); r.binary_encode(&output[output.size() - r.bytes()]); return output; } GOST_3410_Verification_Operation::GOST_3410_Verification_Operation(const GOST_3410_PublicKey& gost) : base_point(gost.domain().get_base_point()), public_point(gost.public_point()), order(gost.domain().get_order()) { } bool GOST_3410_Verification_Operation::verify(const byte msg[], size_t msg_len, const byte sig[], size_t sig_len) { if(sig_len != order.bytes()*2) return false; BigInt e = decode_le(msg, msg_len); BigInt s(sig, sig_len / 2); BigInt r(sig + sig_len / 2, sig_len / 2); if(r <= 0 || r >= order || s <= 0 || s >= order) return false; e %= order; if(e == 0) e = 1; BigInt v = inverse_mod(e, order); BigInt z1 = (s*v) % order; BigInt z2 = (-r*v) % order; PointGFp R = multi_exponentiate(base_point, z1, public_point, z2); if(R.is_zero()) return false; return (R.get_affine_x() == r); } } /* * IF Scheme * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { AlgorithmIdentifier IF_Scheme_PublicKey::algorithm_identifier() const { return AlgorithmIdentifier(get_oid(), AlgorithmIdentifier::USE_NULL_PARAM); } MemoryVector<byte> IF_Scheme_PublicKey::x509_subject_public_key() const { return DER_Encoder() .start_cons(SEQUENCE) .encode(n) .encode(e) .end_cons() .get_contents(); } IF_Scheme_PublicKey::IF_Scheme_PublicKey(const AlgorithmIdentifier&, const MemoryRegion<byte>& key_bits) { BER_Decoder(key_bits) .start_cons(SEQUENCE) .decode(n) .decode(e) .verify_end() .end_cons(); } /* * Check IF Scheme Public Parameters */ bool IF_Scheme_PublicKey::check_key(RandomNumberGenerator&, bool) const { if(n < 35 || n.is_even() || e < 2) return false; return true; } MemoryVector<byte> IF_Scheme_PrivateKey::pkcs8_private_key() const { return DER_Encoder() .start_cons(SEQUENCE) .encode(static_cast<size_t>(0)) .encode(n) .encode(e) .encode(d) .encode(p) .encode(q) .encode(d1) .encode(d2) .encode(c) .end_cons() .get_contents(); } IF_Scheme_PrivateKey::IF_Scheme_PrivateKey(RandomNumberGenerator& rng, const AlgorithmIdentifier&, const MemoryRegion<byte>& key_bits) { BER_Decoder(key_bits) .start_cons(SEQUENCE) .decode_and_check<size_t>(0, "Unknown PKCS #1 key format version") .decode(n) .decode(e) .decode(d) .decode(p) .decode(q) .decode(d1) .decode(d2) .decode(c) .end_cons(); load_check(rng); } IF_Scheme_PrivateKey::IF_Scheme_PrivateKey(RandomNumberGenerator& rng, const BigInt& prime1, const BigInt& prime2, const BigInt& exp, const BigInt& d_exp, const BigInt& mod) { p = prime1; q = prime2; e = exp; d = d_exp; n = mod.is_nonzero() ? mod : p * q; if(d == 0) { BigInt inv_for_d = lcm(p - 1, q - 1); if(e.is_even()) inv_for_d >>= 1; d = inverse_mod(e, inv_for_d); } d1 = d % (p - 1); d2 = d % (q - 1); c = inverse_mod(q, p); load_check(rng); } /* * Check IF Scheme Private Parameters */ bool IF_Scheme_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const { if(n < 35 || n.is_even() || e < 2 || d < 2 || p < 3 || q < 3 || p*q != n) return false; if(!strong) return true; if(d1 != d % (p - 1) || d2 != d % (q - 1) || c != inverse_mod(q, p)) return false; if(!check_prime(p, rng) || !check_prime(q, rng)) return false; return true; } } /* * Keypair Checks * (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace KeyPair { /* * Check an encryption key pair for consistency */ bool encryption_consistency_check(RandomNumberGenerator& rng, const Private_Key& key, const std::string& padding) { PK_Encryptor_EME encryptor(key, padding); PK_Decryptor_EME decryptor(key, padding); /* Weird corner case, if the key is too small to encrypt anything at all. This can happen with very small RSA keys with PSS */ if(encryptor.maximum_input_size() == 0) return true; SecureVector<byte> plaintext = rng.random_vec(encryptor.maximum_input_size() - 1); SecureVector<byte> ciphertext = encryptor.encrypt(plaintext, rng); if(ciphertext == plaintext) return false; SecureVector<byte> decrypted = decryptor.decrypt(ciphertext); return (plaintext == decrypted); } /* * Check a signature key pair for consistency */ bool signature_consistency_check(RandomNumberGenerator& rng, const Private_Key& key, const std::string& padding) { PK_Signer signer(key, padding); PK_Verifier verifier(key, padding); SecureVector<byte> message = rng.random_vec(16); SecureVector<byte> signature; try { signature = signer.sign_message(message, rng); } catch(Encoding_Error) { return false; } if(!verifier.verify_message(message, signature)) return false; // Now try to check a corrupt signature, ensure it does not succeed ++message[0]; if(verifier.verify_message(message, signature)) return false; return true; } } } /* * Nyberg-Rueppel * (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { NR_PublicKey::NR_PublicKey(const AlgorithmIdentifier& alg_id, const MemoryRegion<byte>& key_bits) : DL_Scheme_PublicKey(alg_id, key_bits, DL_Group::ANSI_X9_57) { } /* * NR_PublicKey Constructor */ NR_PublicKey::NR_PublicKey(const DL_Group& grp, const BigInt& y1) { group = grp; y = y1; } /* * Create a NR private key */ NR_PrivateKey::NR_PrivateKey(RandomNumberGenerator& rng, const DL_Group& grp, const BigInt& x_arg) { group = grp; x = x_arg; if(x == 0) x = BigInt::random_integer(rng, 2, group_q() - 1); y = power_mod(group_g(), x, group_p()); if(x_arg == 0) gen_check(rng); else load_check(rng); } NR_PrivateKey::NR_PrivateKey(const AlgorithmIdentifier& alg_id, const MemoryRegion<byte>& key_bits, RandomNumberGenerator& rng) : DL_Scheme_PrivateKey(alg_id, key_bits, DL_Group::ANSI_X9_57) { y = power_mod(group_g(), x, group_p()); load_check(rng); } /* * Check Private Nyberg-Rueppel Parameters */ bool NR_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const { if(!DL_Scheme_PrivateKey::check_key(rng, strong) || x >= group_q()) return false; if(!strong) return true; return KeyPair::signature_consistency_check(rng, *this, "EMSA1(SHA-1)"); } NR_Signature_Operation::NR_Signature_Operation(const NR_PrivateKey& nr) : q(nr.group_q()), x(nr.get_x()), powermod_g_p(nr.group_g(), nr.group_p()), mod_q(nr.group_q()) { } SecureVector<byte> NR_Signature_Operation::sign(const byte msg[], size_t msg_len, RandomNumberGenerator& rng) { rng.add_entropy(msg, msg_len); BigInt f(msg, msg_len); if(f >= q) throw Invalid_Argument("NR_Signature_Operation: Input is out of range"); BigInt c, d; while(c == 0) { BigInt k; do k.randomize(rng, q.bits()); while(k >= q); c = mod_q.reduce(powermod_g_p(k) + f); d = mod_q.reduce(k - x * c); } SecureVector<byte> output(2*q.bytes()); c.binary_encode(&output[output.size() / 2 - c.bytes()]); d.binary_encode(&output[output.size() - d.bytes()]); return output; } NR_Verification_Operation::NR_Verification_Operation(const NR_PublicKey& nr) : q(nr.group_q()), y(nr.get_y()) { powermod_g_p = Fixed_Base_Power_Mod(nr.group_g(), nr.group_p()); powermod_y_p = Fixed_Base_Power_Mod(y, nr.group_p()); mod_p = Modular_Reducer(nr.group_p()); mod_q = Modular_Reducer(nr.group_q()); } SecureVector<byte> NR_Verification_Operation::verify_mr(const byte msg[], size_t msg_len) { const BigInt& q = mod_q.get_modulus(); if(msg_len != 2*q.bytes()) throw Invalid_Argument("NR verification: Invalid signature"); BigInt c(msg, q.bytes()); BigInt d(msg + q.bytes(), q.bytes()); if(c.is_zero() || c >= q || d >= q) throw Invalid_Argument("NR verification: Invalid signature"); BigInt i = mod_p.multiply(powermod_g_p(d), powermod_y_p(c)); return BigInt::encode(mod_q.reduce(c - i)); } } /* * PK Key * (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ #if defined(BOTAN_HAS_RSA) #endif #if defined(BOTAN_HAS_DSA) #endif #if defined(BOTAN_HAS_DIFFIE_HELLMAN) #endif #if defined(BOTAN_HAS_ECDSA) #endif #if defined(BOTAN_HAS_GOST_34_10_2001) #endif #if defined(BOTAN_HAS_NYBERG_RUEPPEL) #endif #if defined(BOTAN_HAS_RW) #endif #if defined(BOTAN_HAS_ELGAMAL) #endif #if defined(BOTAN_HAS_ECDH) #endif namespace Botan { Public_Key* make_public_key(const AlgorithmIdentifier& alg_id, const MemoryRegion<byte>& key_bits) { const std::string alg_name = OIDS::lookup(alg_id.oid); if(alg_name == "") throw Decoding_Error("Unknown algorithm OID: " + alg_id.oid.as_string()); #if defined(BOTAN_HAS_RSA) if(alg_name == "RSA") return new RSA_PublicKey(alg_id, key_bits); #endif #if defined(BOTAN_HAS_RW) if(alg_name == "RW") return new RW_PublicKey(alg_id, key_bits); #endif #if defined(BOTAN_HAS_DSA) if(alg_name == "DSA") return new DSA_PublicKey(alg_id, key_bits); #endif #if defined(BOTAN_HAS_DIFFIE_HELLMAN) if(alg_name == "DH") return new DH_PublicKey(alg_id, key_bits); #endif #if defined(BOTAN_HAS_NYBERG_RUEPPEL) if(alg_name == "NR") return new NR_PublicKey(alg_id, key_bits); #endif #if defined(BOTAN_HAS_ELGAMAL) if(alg_name == "ElGamal") return new ElGamal_PublicKey(alg_id, key_bits); #endif #if defined(BOTAN_HAS_ECDSA) if(alg_name == "ECDSA") return new ECDSA_PublicKey(alg_id, key_bits); #endif #if defined(BOTAN_HAS_GOST_34_10_2001) if(alg_name == "GOST-34.10") return new GOST_3410_PublicKey(alg_id, key_bits); #endif #if defined(BOTAN_HAS_ECDH) if(alg_name == "ECDH") return new ECDH_PublicKey(alg_id, key_bits); #endif return 0; } Private_Key* make_private_key(const AlgorithmIdentifier& alg_id, const MemoryRegion<byte>& key_bits, RandomNumberGenerator& rng) { const std::string alg_name = OIDS::lookup(alg_id.oid); if(alg_name == "") throw Decoding_Error("Unknown algorithm OID: " + alg_id.oid.as_string()); #if defined(BOTAN_HAS_RSA) if(alg_name == "RSA") return new RSA_PrivateKey(alg_id, key_bits, rng); #endif #if defined(BOTAN_HAS_RW) if(alg_name == "RW") return new RW_PrivateKey(alg_id, key_bits, rng); #endif #if defined(BOTAN_HAS_DSA) if(alg_name == "DSA") return new DSA_PrivateKey(alg_id, key_bits, rng); #endif #if defined(BOTAN_HAS_DIFFIE_HELLMAN) if(alg_name == "DH") return new DH_PrivateKey(alg_id, key_bits, rng); #endif #if defined(BOTAN_HAS_NYBERG_RUEPPEL) if(alg_name == "NR") return new NR_PrivateKey(alg_id, key_bits, rng); #endif #if defined(BOTAN_HAS_ELGAMAL) if(alg_name == "ElGamal") return new ElGamal_PrivateKey(alg_id, key_bits, rng); #endif #if defined(BOTAN_HAS_ECDSA) if(alg_name == "ECDSA") return new ECDSA_PrivateKey(alg_id, key_bits); #endif #if defined(BOTAN_HAS_GOST_34_10_2001) if(alg_name == "GOST-34.10") return new GOST_3410_PrivateKey(alg_id, key_bits); #endif #if defined(BOTAN_HAS_ECDH) if(alg_name == "ECDH") return new ECDH_PrivateKey(alg_id, key_bits); #endif return 0; } } /* * PK Key Types * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Default OID access */ OID Public_Key::get_oid() const { try { return OIDS::lookup(algo_name()); } catch(Lookup_Error) { throw Lookup_Error("PK algo " + algo_name() + " has no defined OIDs"); } } /* * Run checks on a loaded public key */ void Public_Key::load_check(RandomNumberGenerator& rng) const { if(!check_key(rng, BOTAN_PUBLIC_KEY_STRONG_CHECKS_ON_LOAD)) throw Invalid_Argument(algo_name() + ": Invalid public key"); } /* * Run checks on a loaded private key */ void Private_Key::load_check(RandomNumberGenerator& rng) const { if(!check_key(rng, BOTAN_PRIVATE_KEY_STRONG_CHECKS_ON_LOAD)) throw Invalid_Argument(algo_name() + ": Invalid private key"); } /* * Run checks on a generated private key */ void Private_Key::gen_check(RandomNumberGenerator& rng) const { if(!check_key(rng, BOTAN_PRIVATE_KEY_STRONG_CHECKS_ON_GENERATE)) throw Self_Test_Failure(algo_name() + " private key generation failed"); } } /* * PKCS #8 * (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <memory> namespace Botan { namespace PKCS8 { namespace { /* * Get info from an EncryptedPrivateKeyInfo */ SecureVector<byte> PKCS8_extract(DataSource& source, AlgorithmIdentifier& pbe_alg_id) { SecureVector<byte> key_data; BER_Decoder(source) .start_cons(SEQUENCE) .decode(pbe_alg_id) .decode(key_data, OCTET_STRING) .verify_end(); return key_data; } /* * PEM decode and/or decrypt a private key */ SecureVector<byte> PKCS8_decode(DataSource& source, const User_Interface& ui, AlgorithmIdentifier& pk_alg_id) { AlgorithmIdentifier pbe_alg_id; SecureVector<byte> key_data, key; bool is_encrypted = true; try { if(ASN1::maybe_BER(source) && !PEM_Code::matches(source)) key_data = PKCS8_extract(source, pbe_alg_id); else { std::string label; key_data = PEM_Code::decode(source, label); if(label == "PRIVATE KEY") is_encrypted = false; else if(label == "ENCRYPTED PRIVATE KEY") { DataSource_Memory key_source(key_data); key_data = PKCS8_extract(key_source, pbe_alg_id); } else throw PKCS8_Exception("Unknown PEM label " + label); } if(key_data.empty()) throw PKCS8_Exception("No key data found"); } catch(Decoding_Error) { throw Decoding_Error("PKCS #8 private key decoding failed"); } if(!is_encrypted) key = key_data; const size_t MAX_TRIES = 3; size_t tries = 0; while(true) { try { if(MAX_TRIES && tries >= MAX_TRIES) break; if(is_encrypted) { DataSource_Memory params(pbe_alg_id.parameters); std::auto_ptr<PBE> pbe(get_pbe(pbe_alg_id.oid, params)); User_Interface::UI_Result result = User_Interface::OK; const std::string passphrase = ui.get_passphrase("PKCS #8 private key", source.id(), result); if(result == User_Interface::CANCEL_ACTION) break; pbe->set_key(passphrase); Pipe decryptor(pbe.release()); decryptor.process_msg(key_data); key = decryptor.read_all(); } BER_Decoder(key) .start_cons(SEQUENCE) .decode_and_check<size_t>(0, "Unknown PKCS #8 version number") .decode(pk_alg_id) .decode(key, OCTET_STRING) .discard_remaining() .end_cons(); break; } catch(Decoding_Error) { ++tries; } } if(key.empty()) throw Decoding_Error("PKCS #8 private key decoding failed"); return key; } } /* * BER encode a PKCS #8 private key, unencrypted */ SecureVector<byte> BER_encode(const Private_Key& key) { const size_t PKCS8_VERSION = 0; return DER_Encoder() .start_cons(SEQUENCE) .encode(PKCS8_VERSION) .encode(key.pkcs8_algorithm_identifier()) .encode(key.pkcs8_private_key(), OCTET_STRING) .end_cons() .get_contents(); } /* * PEM encode a PKCS #8 private key, unencrypted */ std::string PEM_encode(const Private_Key& key) { return PEM_Code::encode(PKCS8::BER_encode(key), "PRIVATE KEY"); } /* * BER encode a PKCS #8 private key, encrypted */ SecureVector<byte> BER_encode(const Private_Key& key, RandomNumberGenerator& rng, const std::string& pass, const std::string& pbe_algo) { const std::string DEFAULT_PBE = "PBE-PKCS5v20(SHA-1,AES-256/CBC)"; std::auto_ptr<PBE> pbe(get_pbe(((pbe_algo != "") ? pbe_algo : DEFAULT_PBE))); pbe->new_params(rng); pbe->set_key(pass); AlgorithmIdentifier pbe_algid(pbe->get_oid(), pbe->encode_params()); Pipe key_encrytor(pbe.release()); key_encrytor.process_msg(PKCS8::BER_encode(key)); return DER_Encoder() .start_cons(SEQUENCE) .encode(pbe_algid) .encode(key_encrytor.read_all(), OCTET_STRING) .end_cons() .get_contents(); } /* * PEM encode a PKCS #8 private key, encrypted */ std::string PEM_encode(const Private_Key& key, RandomNumberGenerator& rng, const std::string& pass, const std::string& pbe_algo) { if(pass == "") return PEM_encode(key); return PEM_Code::encode(PKCS8::BER_encode(key, rng, pass, pbe_algo), "ENCRYPTED PRIVATE KEY"); } /* * Extract a private key and return it */ Private_Key* load_key(DataSource& source, RandomNumberGenerator& rng, const User_Interface& ui) { AlgorithmIdentifier alg_id; SecureVector<byte> pkcs8_key = PKCS8_decode(source, ui, alg_id); const std::string alg_name = OIDS::lookup(alg_id.oid); if(alg_name == "" || alg_name == alg_id.oid.as_string()) throw PKCS8_Exception("Unknown algorithm OID: " + alg_id.oid.as_string()); return make_private_key(alg_id, pkcs8_key, rng); } /* * Extract a private key and return it */ Private_Key* load_key(const std::string& fsname, RandomNumberGenerator& rng, const User_Interface& ui) { DataSource_Stream source(fsname, true); return PKCS8::load_key(source, rng, ui); } /* * Extract a private key and return it */ Private_Key* load_key(DataSource& source, RandomNumberGenerator& rng, const std::string& pass) { return PKCS8::load_key(source, rng, User_Interface(pass)); } /* * Extract a private key and return it */ Private_Key* load_key(const std::string& fsname, RandomNumberGenerator& rng, const std::string& pass) { return PKCS8::load_key(fsname, rng, User_Interface(pass)); } /* * Make a copy of this private key */ Private_Key* copy_key(const Private_Key& key, RandomNumberGenerator& rng) { DataSource_Memory source(PEM_encode(key)); return PKCS8::load_key(source, rng); } } } /* * Public Key Base * (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <memory> namespace Botan { /* * PK_Encryptor_EME Constructor */ PK_Encryptor_EME::PK_Encryptor_EME(const Public_Key& key, const std::string& eme_name) { Algorithm_Factory::Engine_Iterator i(global_state().algorithm_factory()); while(const Engine* engine = i.next()) { op = engine->get_encryption_op(key); if(op) break; } if(!op) throw Lookup_Error("PK_Encryptor_EME: No working engine for " + key.algo_name()); eme = (eme_name == "Raw") ? 0 : get_eme(eme_name); } /* * Encrypt a message */ SecureVector<byte> PK_Encryptor_EME::enc(const byte in[], size_t length, RandomNumberGenerator& rng) const { if(eme) { SecureVector<byte> encoded = eme->encode(in, length, op->max_input_bits(), rng); if(8*(encoded.size() - 1) + high_bit(encoded[0]) > op->max_input_bits()) throw Invalid_Argument("PK_Encryptor_EME: Input is too large"); return op->encrypt(&encoded[0], encoded.size(), rng); } else { if(8*(length - 1) + high_bit(in[0]) > op->max_input_bits()) throw Invalid_Argument("PK_Encryptor_EME: Input is too large"); return op->encrypt(&in[0], length, rng); } } /* * Return the max size, in bytes, of a message */ size_t PK_Encryptor_EME::maximum_input_size() const { if(!eme) return (op->max_input_bits() / 8); else return eme->maximum_input_size(op->max_input_bits()); } /* * PK_Decryptor_EME Constructor */ PK_Decryptor_EME::PK_Decryptor_EME(const Private_Key& key, const std::string& eme_name) { Algorithm_Factory::Engine_Iterator i(global_state().algorithm_factory()); while(const Engine* engine = i.next()) { op = engine->get_decryption_op(key); if(op) break; } if(!op) throw Lookup_Error("PK_Decryptor_EME: No working engine for " + key.algo_name()); eme = (eme_name == "Raw") ? 0 : get_eme(eme_name); } /* * Decrypt a message */ SecureVector<byte> PK_Decryptor_EME::dec(const byte msg[], size_t length) const { try { SecureVector<byte> decrypted = op->decrypt(msg, length); if(eme) return eme->decode(decrypted, op->max_input_bits()); else return decrypted; } catch(Invalid_Argument) { throw Decoding_Error("PK_Decryptor_EME: Input is invalid"); } } /* * PK_Signer Constructor */ PK_Signer::PK_Signer(const Private_Key& key, const std::string& emsa_name, Signature_Format format, Fault_Protection prot) { Algorithm_Factory::Engine_Iterator i(global_state().algorithm_factory()); op = 0; verify_op = 0; while(const Engine* engine = i.next()) { if(!op) op = engine->get_signature_op(key); if(!verify_op && prot == ENABLE_FAULT_PROTECTION) verify_op = engine->get_verify_op(key); if(op && (verify_op || prot == DISABLE_FAULT_PROTECTION)) break; } if(!op || (!verify_op && prot == ENABLE_FAULT_PROTECTION)) throw Lookup_Error("PK_Signer: No working engine for " + key.algo_name()); emsa = get_emsa(emsa_name); sig_format = format; } /* * Sign a message */ SecureVector<byte> PK_Signer::sign_message(const byte msg[], size_t length, RandomNumberGenerator& rng) { update(msg, length); return signature(rng); } /* * Add more to the message to be signed */ void PK_Signer::update(const byte in[], size_t length) { emsa->update(in, length); } /* * Check the signature we just created, to help prevent fault attacks */ bool PK_Signer::self_test_signature(const MemoryRegion<byte>& msg, const MemoryRegion<byte>& sig) const { if(!verify_op) return true; // checking disabled, assume ok if(verify_op->with_recovery()) { SecureVector<byte> recovered = verify_op->verify_mr(&sig[0], sig.size()); if(msg.size() > recovered.size()) { size_t extra_0s = msg.size() - recovered.size(); for(size_t i = 0; i != extra_0s; ++i) if(msg[i] != 0) return false; return same_mem(&msg[extra_0s], &recovered[0], recovered.size()); } return (recovered == msg); } else return verify_op->verify(&msg[0], msg.size(), &sig[0], sig.size()); } /* * Create a signature */ SecureVector<byte> PK_Signer::signature(RandomNumberGenerator& rng) { SecureVector<byte> encoded = emsa->encoding_of(emsa->raw_data(), op->max_input_bits(), rng); SecureVector<byte> plain_sig = op->sign(&encoded[0], encoded.size(), rng); BOTAN_ASSERT(self_test_signature(encoded, plain_sig), "PK_Signer consistency check failed"); if(op->message_parts() == 1 || sig_format == IEEE_1363) return plain_sig; if(sig_format == DER_SEQUENCE) { if(plain_sig.size() % op->message_parts()) throw Encoding_Error("PK_Signer: strange signature size found"); const size_t SIZE_OF_PART = plain_sig.size() / op->message_parts(); std::vector<BigInt> sig_parts(op->message_parts()); for(size_t j = 0; j != sig_parts.size(); ++j) sig_parts[j].binary_decode(&plain_sig[SIZE_OF_PART*j], SIZE_OF_PART); return DER_Encoder() .start_cons(SEQUENCE) .encode_list(sig_parts) .end_cons() .get_contents(); } else throw Encoding_Error("PK_Signer: Unknown signature format " + to_string(sig_format)); } /* * PK_Verifier Constructor */ PK_Verifier::PK_Verifier(const Public_Key& key, const std::string& emsa_name, Signature_Format format) { Algorithm_Factory::Engine_Iterator i(global_state().algorithm_factory()); while(const Engine* engine = i.next()) { op = engine->get_verify_op(key); if(op) break; } if(!op) throw Lookup_Error("PK_Verifier: No working engine for " + key.algo_name()); emsa = get_emsa(emsa_name); sig_format = format; } /* * Set the signature format */ void PK_Verifier::set_input_format(Signature_Format format) { if(op->message_parts() == 1 && format != IEEE_1363) throw Invalid_State("PK_Verifier: This algorithm always uses IEEE 1363"); sig_format = format; } /* * Verify a message */ bool PK_Verifier::verify_message(const byte msg[], size_t msg_length, const byte sig[], size_t sig_length) { update(msg, msg_length); return check_signature(sig, sig_length); } /* * Append to the message */ void PK_Verifier::update(const byte in[], size_t length) { emsa->update(in, length); } /* * Check a signature */ bool PK_Verifier::check_signature(const byte sig[], size_t length) { try { if(sig_format == IEEE_1363) return validate_signature(emsa->raw_data(), sig, length); else if(sig_format == DER_SEQUENCE) { BER_Decoder decoder(sig, length); BER_Decoder ber_sig = decoder.start_cons(SEQUENCE); size_t count = 0; SecureVector<byte> real_sig; while(ber_sig.more_items()) { BigInt sig_part; ber_sig.decode(sig_part); real_sig += BigInt::encode_1363(sig_part, op->message_part_size()); ++count; } if(count != op->message_parts()) throw Decoding_Error("PK_Verifier: signature size invalid"); return validate_signature(emsa->raw_data(), &real_sig[0], real_sig.size()); } else throw Decoding_Error("PK_Verifier: Unknown signature format " + to_string(sig_format)); } catch(Invalid_Argument) { return false; } } /* * Verify a signature */ bool PK_Verifier::validate_signature(const MemoryRegion<byte>& msg, const byte sig[], size_t sig_len) { if(op->with_recovery()) { SecureVector<byte> output_of_key = op->verify_mr(sig, sig_len); return emsa->verify(output_of_key, msg, op->max_input_bits()); } else { Null_RNG rng; SecureVector<byte> encoded = emsa->encoding_of(msg, op->max_input_bits(), rng); return op->verify(&encoded[0], encoded.size(), sig, sig_len); } } /* * PK_Key_Agreement Constructor */ PK_Key_Agreement::PK_Key_Agreement(const PK_Key_Agreement_Key& key, const std::string& kdf_name) { Algorithm_Factory::Engine_Iterator i(global_state().algorithm_factory()); while(const Engine* engine = i.next()) { op = engine->get_key_agreement_op(key); if(op) break; } if(!op) throw Lookup_Error("PK_Key_Agreement: No working engine for " + key.algo_name()); kdf = (kdf_name == "Raw") ? 0 : get_kdf(kdf_name); } SymmetricKey PK_Key_Agreement::derive_key(size_t key_len, const byte in[], size_t in_len, const byte params[], size_t params_len) const { SecureVector<byte> z = op->agree(in, in_len); if(!kdf) return z; return kdf->derive_key(key_len, z, params, params_len); } } /* * KeyUsage * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace BER { /* * Decode a BER encoded KeyUsage */ void decode(BER_Decoder& source, Key_Constraints& key_usage) { BER_Object obj = source.get_next_object(); if(obj.type_tag != BIT_STRING || obj.class_tag != UNIVERSAL) throw BER_Bad_Tag("Bad tag for usage constraint", obj.type_tag, obj.class_tag); if(obj.value.size() != 2 && obj.value.size() != 3) throw BER_Decoding_Error("Bad size for BITSTRING in usage constraint"); if(obj.value[0] >= 8) throw BER_Decoding_Error("Invalid unused bits in usage constraint"); const byte mask = (0xFF << obj.value[0]); obj.value[obj.value.size()-1] &= mask; u16bit usage = 0; for(size_t j = 1; j != obj.value.size(); ++j) usage = (obj.value[j] << 8) | usage; key_usage = Key_Constraints(usage); } } } /* * RSA * (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Create a RSA private key */ RSA_PrivateKey::RSA_PrivateKey(RandomNumberGenerator& rng, size_t bits, size_t exp) { if(bits < 512) throw Invalid_Argument(algo_name() + ": Can't make a key that is only " + to_string(bits) + " bits long"); if(exp < 3 || exp % 2 == 0) throw Invalid_Argument(algo_name() + ": Invalid encryption exponent"); e = exp; do { p = random_prime(rng, (bits + 1) / 2, e); q = random_prime(rng, bits - p.bits(), e); n = p * q; } while(n.bits() != bits); d = inverse_mod(e, lcm(p - 1, q - 1)); d1 = d % (p - 1); d2 = d % (q - 1); c = inverse_mod(q, p); gen_check(rng); } /* * Check Private RSA Parameters */ bool RSA_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const { if(!IF_Scheme_PrivateKey::check_key(rng, strong)) return false; if(!strong) return true; if((e * d) % lcm(p - 1, q - 1) != 1) return false; return KeyPair::signature_consistency_check(rng, *this, "EMSA4(SHA-1)"); } RSA_Private_Operation::RSA_Private_Operation(const RSA_PrivateKey& rsa) : n(rsa.get_n()), q(rsa.get_q()), c(rsa.get_c()), powermod_e_n(rsa.get_e(), rsa.get_n()), powermod_d1_p(rsa.get_d1(), rsa.get_p()), powermod_d2_q(rsa.get_d2(), rsa.get_q()), mod_p(rsa.get_p()) { BigInt k(global_state().global_rng(), n.bits() - 1); blinder = Blinder(powermod_e_n(k), inverse_mod(k, n), n); } BigInt RSA_Private_Operation::private_op(const BigInt& m) const { if(m >= n) throw Invalid_Argument("RSA private op - input is too large"); BigInt j1 = powermod_d1_p(m); BigInt j2 = powermod_d2_q(m); j1 = mod_p.reduce(sub_mul(j1, j2, c)); return mul_add(j1, q, j2); } SecureVector<byte> RSA_Private_Operation::sign(const byte msg[], size_t msg_len, RandomNumberGenerator&) { /* We don't check signatures against powermod_e_n here because PK_Signer checks verification consistency for all signature algorithms. */ BigInt m(msg, msg_len); BigInt x = blinder.unblind(private_op(blinder.blind(m))); return BigInt::encode_1363(x, n.bytes()); } /* * RSA Decryption Operation */ SecureVector<byte> RSA_Private_Operation::decrypt(const byte msg[], size_t msg_len) { BigInt m(msg, msg_len); BigInt x = blinder.unblind(private_op(blinder.blind(m))); BOTAN_ASSERT(m == powermod_e_n(x), "RSA private op failed consistency check"); return BigInt::encode(x); } } /* * Rabin-Williams * (C) 1999-2008 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> namespace Botan { /* * Create a Rabin-Williams private key */ RW_PrivateKey::RW_PrivateKey(RandomNumberGenerator& rng, size_t bits, size_t exp) { if(bits < 512) throw Invalid_Argument(algo_name() + ": Can't make a key that is only " + to_string(bits) + " bits long"); if(exp < 2 || exp % 2 == 1) throw Invalid_Argument(algo_name() + ": Invalid encryption exponent"); e = exp; do { p = random_prime(rng, (bits + 1) / 2, e / 2, 3, 4); q = random_prime(rng, bits - p.bits(), e / 2, ((p % 8 == 3) ? 7 : 3), 8); n = p * q; } while(n.bits() != bits); d = inverse_mod(e, lcm(p - 1, q - 1) >> 1); d1 = d % (p - 1); d2 = d % (q - 1); c = inverse_mod(q, p); gen_check(rng); } /* * Check Private Rabin-Williams Parameters */ bool RW_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const { if(!IF_Scheme_PrivateKey::check_key(rng, strong)) return false; if(!strong) return true; if((e * d) % (lcm(p - 1, q - 1) / 2) != 1) return false; return KeyPair::signature_consistency_check(rng, *this, "EMSA2(SHA-1)"); } RW_Signature_Operation::RW_Signature_Operation(const RW_PrivateKey& rw) : n(rw.get_n()), e(rw.get_e()), q(rw.get_q()), c(rw.get_c()), powermod_d1_p(rw.get_d1(), rw.get_p()), powermod_d2_q(rw.get_d2(), rw.get_q()), mod_p(rw.get_p()) { } SecureVector<byte> RW_Signature_Operation::sign(const byte msg[], size_t msg_len, RandomNumberGenerator& rng) { if(!blinder.initialized()) { BigInt k(rng, n.bits() / 2); blinder = Blinder(power_mod(k, e, n), inverse_mod(k, n), n); } BigInt i(msg, msg_len); if(i >= n || i % 16 != 12) throw Invalid_Argument("Rabin-Williams: invalid input"); if(jacobi(i, n) != 1) i >>= 1; i = blinder.blind(i); BigInt j1 = powermod_d1_p(i); BigInt j2 = powermod_d2_q(i); j1 = mod_p.reduce(sub_mul(j1, j2, c)); BigInt r = blinder.unblind(mul_add(j1, q, j2)); r = std::min(r, n - r); return BigInt::encode_1363(r, n.bytes()); } SecureVector<byte> RW_Verification_Operation::verify_mr(const byte msg[], size_t msg_len) { BigInt m(msg, msg_len); if((m > (n >> 1)) || m.is_negative()) throw Invalid_Argument("RW signature verification: m > n / 2 || m < 0"); BigInt r = powermod_e_n(m); if(r % 16 == 12) return BigInt::encode(r); if(r % 8 == 6) return BigInt::encode(2*r); r = n - r; if(r % 16 == 12) return BigInt::encode(r); if(r % 8 == 6) return BigInt::encode(2*r); throw Invalid_Argument("RW signature verification: Invalid signature"); } } /* * Public Key Work Factor Functions * (C) 1999-2007,2012 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> #include <cmath> namespace Botan { size_t dl_work_factor(size_t bits) { /* Based on GNFS work factors. Constant is 1.43 times the asymptotic value; I'm not sure but I believe that came from a paper on 'real world' runtimes, but I don't remember where now. Sample return values: |512| -> 64 |1024| -> 86 |1536| -> 102 |2048| -> 116 |3072| -> 138 |4096| -> 155 |8192| -> 206 For DL algos, we use an exponent of twice the size of the result; the assumption is that an arbitrary discrete log on a group of size bits would take about 2^n effort, and thus using an exponent of size 2^(2*n) implies that all available attacks are about as easy (as e.g Pollard's kangaroo algorithm can compute the DL in sqrt(x) operations) while minimizing the exponent size for performance reasons. */ const size_t MIN_WORKFACTOR = 64; // approximates natural logarithm of p const double log_p = bits / 1.4426; const double strength = 2.76 * std::pow(log_p, 1.0/3.0) * std::pow(std::log(log_p), 2.0/3.0); return std::max(static_cast<size_t>(strength), MIN_WORKFACTOR); } } /* * X.509 Public Key * (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <memory> namespace Botan { namespace X509 { MemoryVector<byte> BER_encode(const Public_Key& key) { return DER_Encoder() .start_cons(SEQUENCE) .encode(key.algorithm_identifier()) .encode(key.x509_subject_public_key(), BIT_STRING) .end_cons() .get_contents(); } /* * PEM encode a X.509 public key */ std::string PEM_encode(const Public_Key& key) { return PEM_Code::encode(X509::BER_encode(key), "PUBLIC KEY"); } /* * Extract a public key and return it */ Public_Key* load_key(DataSource& source) { try { AlgorithmIdentifier alg_id; MemoryVector<byte> key_bits; if(ASN1::maybe_BER(source) && !PEM_Code::matches(source)) { BER_Decoder(source) .start_cons(SEQUENCE) .decode(alg_id) .decode(key_bits, BIT_STRING) .verify_end() .end_cons(); } else { DataSource_Memory ber( PEM_Code::decode_check_label(source, "PUBLIC KEY") ); BER_Decoder(ber) .start_cons(SEQUENCE) .decode(alg_id) .decode(key_bits, BIT_STRING) .verify_end() .end_cons(); } if(key_bits.empty()) throw Decoding_Error("X.509 public key decoding failed"); return make_public_key(alg_id, key_bits); } catch(Decoding_Error) { throw Decoding_Error("X.509 public key decoding failed"); } } /* * Extract a public key and return it */ Public_Key* load_key(const std::string& fsname) { DataSource_Stream source(fsname, true); return X509::load_key(source); } /* * Extract a public key and return it */ Public_Key* load_key(const MemoryRegion<byte>& mem) { DataSource_Memory source(mem); return X509::load_key(source); } /* * Make a copy of this public key */ Public_Key* copy_key(const Public_Key& key) { DataSource_Memory source(PEM_encode(key)); return X509::load_key(source); } /* * Find the allowable key constraints */ Key_Constraints find_constraints(const Public_Key& pub_key, Key_Constraints limits) { const std::string name = pub_key.algo_name(); size_t constraints = 0; if(name == "DH" || name == "ECDH") constraints |= KEY_AGREEMENT; if(name == "RSA" || name == "ElGamal") constraints |= KEY_ENCIPHERMENT | DATA_ENCIPHERMENT; if(name == "RSA" || name == "RW" || name == "NR" || name == "DSA" || name == "ECDSA") constraints |= DIGITAL_SIGNATURE | NON_REPUDIATION; if(limits) constraints &= limits; return Key_Constraints(constraints); } } } /* * HMAC_RNG * (C) 2008-2009 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> namespace Botan { namespace { void hmac_prf(MessageAuthenticationCode* prf, MemoryRegion<byte>& K, u32bit& counter, const std::string& label) { prf->update(K); prf->update(label); prf->update_be(counter); prf->final(&K[0]); ++counter; } } /* * Generate a buffer of random bytes */ void HMAC_RNG::randomize(byte out[], size_t length) { if(!is_seeded()) throw PRNG_Unseeded(name()); /* HMAC KDF as described in E-t-E, using a CTXinfo of "rng" */ while(length) { hmac_prf(prf, K, counter, "rng"); const size_t copied = std::min<size_t>(K.size(), length); copy_mem(out, &K[0], copied); out += copied; length -= copied; } } /* * Poll for entropy and reset the internal keys */ void HMAC_RNG::reseed(size_t poll_bits) { /* Using the terminology of E-t-E, XTR is the MAC function (normally HMAC) seeded with XTS (below) and we form SKM, the key material, by fast polling each source, and then slow polling as many as we think we need (in the following loop), and feeding all of the poll results, along with any optional user input, along with, finally, feedback of the current PRK value, into the extractor function. */ Entropy_Accumulator_BufferedComputation accum(*extractor, poll_bits); if(!entropy_sources.empty()) { size_t poll_attempt = 0; while(!accum.polling_goal_achieved() && poll_attempt < poll_bits) { const size_t src_idx = poll_attempt % entropy_sources.size(); entropy_sources[src_idx]->poll(accum); ++poll_attempt; } } /* * It is necessary to feed forward poll data. Otherwise, a good poll * (collecting a large amount of conditional entropy) followed by a * bad one (collecting little) would be unsafe. Do this by * generating new PRF outputs using the previous key and feeding * them into the extractor function. * * Cycle the RNG once (CTXinfo="rng"), then generate a new PRF * output using the CTXinfo "reseed". Provide these values as input * to the extractor function. */ hmac_prf(prf, K, counter, "rng"); extractor->update(K); // K is the CTXinfo=rng PRF output hmac_prf(prf, K, counter, "reseed"); extractor->update(K); // K is the CTXinfo=reseed PRF output /* Now derive the new PRK using everything that has been fed into the extractor, and set the PRF key to that */ prf->set_key(extractor->final()); // Now generate a new PRF output to use as the XTS extractor salt hmac_prf(prf, K, counter, "xts"); extractor->set_key(K); // Reset state zeroise(K); counter = 0; user_input_len = 0; /* Consider ourselves seeded once we've collected an estimated 128 bits of entropy in a single poll. */ if(seeded == false && accum.bits_collected() >= 128) seeded = true; } /* * Add user-supplied entropy to the extractor input */ void HMAC_RNG::add_entropy(const byte input[], size_t length) { const size_t USER_ENTROPY_WATERSHED = 64; extractor->update(input, length); user_input_len += length; /* * After we've accumulated at least USER_ENTROPY_WATERSHED bytes of * user input, reseed. This input will automatically have been * included if reseed was called already, as it's just included in * the extractor input. */ if(user_input_len >= USER_ENTROPY_WATERSHED) reseed(0); } /* * Add another entropy source to the list */ void HMAC_RNG::add_entropy_source(EntropySource* src) { entropy_sources.push_back(src); } /* * Clear memory of sensitive data */ void HMAC_RNG::clear() { extractor->clear(); prf->clear(); zeroise(K); counter = 0; user_input_len = 0; seeded = false; } /* * Return the name of this type */ std::string HMAC_RNG::name() const { return "HMAC_RNG(" + extractor->name() + "," + prf->name() + ")"; } /* * HMAC_RNG Constructor */ HMAC_RNG::HMAC_RNG(MessageAuthenticationCode* extractor_mac, MessageAuthenticationCode* prf_mac) : extractor(extractor_mac), prf(prf_mac) { if(!prf->valid_keylength(extractor->output_length()) || !extractor->valid_keylength(prf->output_length())) throw Invalid_Argument("HMAC_RNG: Bad algo combination " + extractor->name() + " and " + prf->name()); // First PRF inputs are all zero, as specified in section 2 K.resize(prf->output_length()); counter = 0; user_input_len = 0; seeded = false; /* Normally we want to feedback PRF output into the input to the extractor function to ensure a single bad poll does not damage the RNG, but obviously that is meaningless to do on the first poll. We will want to use the PRF before we set the first key (in reseed), and it is a pain to keep track if it is set or not. Since the first time it doesn't matter anyway, just set the PRF key to constant zero: randomize() will not produce output unless is_seeded() returns true, and that will only be the case if the estimated entropy counter is high enough. That variable is only set when a reseeding is performed. */ MemoryVector<byte> prf_key(extractor->output_length()); prf->set_key(prf_key); /* Use PRF("Botan HMAC_RNG XTS") as the intitial XTS key. This will be used during the first extraction sequence; XTS values after this one are generated using the PRF. If I understand the E-t-E paper correctly (specifically Section 4), using this fixed extractor key is safe to do. */ extractor->set_key(prf->process("Botan HMAC_RNG XTS")); } /* * HMAC_RNG Destructor */ HMAC_RNG::~HMAC_RNG() { delete extractor; delete prf; std::for_each(entropy_sources.begin(), entropy_sources.end(), del_fun<EntropySource>()); counter = 0; } } /* * Randpool * (C) 1999-2009 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> namespace Botan { namespace { /* * PRF based on a MAC */ enum RANDPOOL_PRF_TAG { CIPHER_KEY = 0, MAC_KEY = 1, GEN_OUTPUT = 2 }; } /* * Generate a buffer of random bytes */ void Randpool::randomize(byte out[], size_t length) { if(!is_seeded()) throw PRNG_Unseeded(name()); update_buffer(); while(length) { const size_t copied = std::min<size_t>(length, buffer.size()); copy_mem(out, &buffer[0], copied); out += copied; length -= copied; update_buffer(); } } /* * Refill the output buffer */ void Randpool::update_buffer() { for(size_t i = 0; i != counter.size(); ++i) if(++counter[i]) break; mac->update(static_cast<byte>(GEN_OUTPUT)); mac->update(counter); SecureVector<byte> mac_val = mac->final(); for(size_t i = 0; i != mac_val.size(); ++i) buffer[i % buffer.size()] ^= mac_val[i]; cipher->encrypt(buffer); if(counter[0] % ITERATIONS_BEFORE_RESEED == 0) mix_pool(); } /* * Mix the entropy pool */ void Randpool::mix_pool() { const size_t BLOCK_SIZE = cipher->block_size(); mac->update(static_cast<byte>(MAC_KEY)); mac->update(pool); mac->set_key(mac->final()); mac->update(static_cast<byte>(CIPHER_KEY)); mac->update(pool); cipher->set_key(mac->final()); xor_buf(pool, buffer, BLOCK_SIZE); cipher->encrypt(pool); for(size_t i = 1; i != POOL_BLOCKS; ++i) { const byte* previous_block = &pool[BLOCK_SIZE*(i-1)]; byte* this_block = &pool[BLOCK_SIZE*i]; xor_buf(this_block, previous_block, BLOCK_SIZE); cipher->encrypt(this_block); } update_buffer(); } /* * Reseed the internal state */ void Randpool::reseed(size_t poll_bits) { Entropy_Accumulator_BufferedComputation accum(*mac, poll_bits); if(!entropy_sources.empty()) { size_t poll_attempt = 0; while(!accum.polling_goal_achieved() && poll_attempt < poll_bits) { entropy_sources[poll_attempt % entropy_sources.size()]->poll(accum); ++poll_attempt; } } SecureVector<byte> mac_val = mac->final(); xor_buf(pool, mac_val, mac_val.size()); mix_pool(); if(accum.bits_collected() >= poll_bits) seeded = true; } /* * Add user-supplied entropy */ void Randpool::add_entropy(const byte input[], size_t length) { SecureVector<byte> mac_val = mac->process(input, length); xor_buf(pool, mac_val, mac_val.size()); mix_pool(); if(length) seeded = true; } /* * Add another entropy source to the list */ void Randpool::add_entropy_source(EntropySource* src) { entropy_sources.push_back(src); } /* * Clear memory of sensitive data */ void Randpool::clear() { cipher->clear(); mac->clear(); zeroise(pool); zeroise(buffer); zeroise(counter); seeded = false; } /* * Return the name of this type */ std::string Randpool::name() const { return "Randpool(" + cipher->name() + "," + mac->name() + ")"; } /* * Randpool Constructor */ Randpool::Randpool(BlockCipher* cipher_in, MessageAuthenticationCode* mac_in, size_t pool_blocks, size_t iter_before_reseed) : ITERATIONS_BEFORE_RESEED(iter_before_reseed), POOL_BLOCKS(pool_blocks), cipher(cipher_in), mac(mac_in) { const size_t BLOCK_SIZE = cipher->block_size(); const size_t OUTPUT_LENGTH = mac->output_length(); if(OUTPUT_LENGTH < BLOCK_SIZE || !cipher->valid_keylength(OUTPUT_LENGTH) || !mac->valid_keylength(OUTPUT_LENGTH)) { delete cipher; delete mac; throw Internal_Error("Randpool: Invalid algorithm combination " + cipher->name() + "/" + mac->name()); } buffer.resize(BLOCK_SIZE); pool.resize(POOL_BLOCKS * BLOCK_SIZE); counter.resize(12); seeded = false; } /* * Randpool Destructor */ Randpool::~Randpool() { delete cipher; delete mac; std::for_each(entropy_sources.begin(), entropy_sources.end(), del_fun<EntropySource>()); } } /* * Random Number Generator Base * (C) 1999-2008 Jack Lloyd * * Distributed under the terms of the Botan license */ #if defined(BOTAN_HAS_AUTO_SEEDING_RNG) #endif namespace Botan { /* * Get a single random byte */ byte RandomNumberGenerator::next_byte() { byte out; this->randomize(&out, 1); return out; } /* * Create and seed a new RNG object */ RandomNumberGenerator* RandomNumberGenerator::make_rng() { #if defined(BOTAN_HAS_AUTO_SEEDING_RNG) return new AutoSeeded_RNG; #endif throw Algorithm_Not_Found("RandomNumberGenerator::make_rng - no RNG found"); } } /* * ANSI X9.31 RNG * (C) 1999-2009 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> namespace Botan { /* * Generate a buffer of random bytes */ void ANSI_X931_RNG::randomize(byte out[], size_t length) { if(!is_seeded()) throw PRNG_Unseeded(name()); while(length) { if(position == R.size()) update_buffer(); const size_t copied = std::min<size_t>(length, R.size() - position); copy_mem(out, &R[position], copied); out += copied; length -= copied; position += copied; } } /* * Refill the internal state */ void ANSI_X931_RNG::update_buffer() { const size_t BLOCK_SIZE = cipher->block_size(); SecureVector<byte> DT = prng->random_vec(BLOCK_SIZE); cipher->encrypt(DT); xor_buf(&R[0], &V[0], &DT[0], BLOCK_SIZE); cipher->encrypt(R); xor_buf(&V[0], &R[0], &DT[0], BLOCK_SIZE); cipher->encrypt(V); position = 0; } /* * Reset V and the cipher key with new values */ void ANSI_X931_RNG::rekey() { const size_t BLOCK_SIZE = cipher->block_size(); if(prng->is_seeded()) { cipher->set_key(prng->random_vec(cipher->maximum_keylength())); if(V.size() != BLOCK_SIZE) V.resize(BLOCK_SIZE); prng->randomize(&V[0], V.size()); update_buffer(); } } /* * Reseed the internal state */ void ANSI_X931_RNG::reseed(size_t poll_bits) { prng->reseed(poll_bits); rekey(); } /* * Add a entropy source to the underlying PRNG */ void ANSI_X931_RNG::add_entropy_source(EntropySource* src) { prng->add_entropy_source(src); } /* * Add some entropy to the underlying PRNG */ void ANSI_X931_RNG::add_entropy(const byte input[], size_t length) { prng->add_entropy(input, length); rekey(); } /* * Check if the the PRNG is seeded */ bool ANSI_X931_RNG::is_seeded() const { return (V.size() > 0); } /* * Clear memory of sensitive data */ void ANSI_X931_RNG::clear() { cipher->clear(); prng->clear(); zeroise(R); V.clear(); position = 0; } /* * Return the name of this type */ std::string ANSI_X931_RNG::name() const { return "X9.31(" + cipher->name() + ")"; } /* * ANSI X931 RNG Constructor */ ANSI_X931_RNG::ANSI_X931_RNG(BlockCipher* cipher_in, RandomNumberGenerator* prng_in) { if(!prng_in || !cipher_in) throw Invalid_Argument("ANSI_X931_RNG constructor: NULL arguments"); cipher = cipher_in; prng = prng_in; R.resize(cipher->block_size()); position = 0; } /* * ANSI X931 RNG Destructor */ ANSI_X931_RNG::~ANSI_X931_RNG() { delete cipher; delete prng; } } /* * Startup Self Tests * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { /* * Perform a Known Answer Test */ bool test_filter_kat(Filter* filter, const std::string& input, const std::string& expected_output) { Pipe pipe(new Hex_Decoder, filter, new Hex_Encoder); pipe.process_msg(input); const std::string output = pipe.read_all_as_string(); return (output == expected_output); } } /* * Run a set of KATs */ std::map<std::string, bool> algorithm_kat(const SCAN_Name& algo_name, const std::map<std::string, std::string>& vars, Algorithm_Factory& af) { const std::string& algo = algo_name.algo_name_and_args(); std::vector<std::string> providers = af.providers_of(algo); std::map<std::string, bool> all_results; if(providers.empty()) // no providers, nothing to do return all_results; const std::string input = search_map(vars, std::string("input")); const std::string output = search_map(vars, std::string("output")); SymmetricKey key(search_map(vars, std::string("key"))); InitializationVector iv(search_map(vars, std::string("iv"))); for(size_t i = 0; i != providers.size(); ++i) { const std::string provider = providers[i]; if(const HashFunction* proto = af.prototype_hash_function(algo, provider)) { Filter* filt = new Hash_Filter(proto->clone()); all_results[provider] = test_filter_kat(filt, input, output); } else if(const MessageAuthenticationCode* proto = af.prototype_mac(algo, provider)) { Keyed_Filter* filt = new MAC_Filter(proto->clone(), key); all_results[provider] = test_filter_kat(filt, input, output); } else if(const StreamCipher* proto = af.prototype_stream_cipher(algo, provider)) { Keyed_Filter* filt = new StreamCipher_Filter(proto->clone()); filt->set_key(key); filt->set_iv(iv); all_results[provider] = test_filter_kat(filt, input, output); } else if(const BlockCipher* proto = af.prototype_block_cipher(algo, provider)) { Keyed_Filter* enc = get_cipher_mode(proto, ENCRYPTION, algo_name.cipher_mode(), algo_name.cipher_mode_pad()); Keyed_Filter* dec = get_cipher_mode(proto, DECRYPTION, algo_name.cipher_mode(), algo_name.cipher_mode_pad()); if(!enc || !dec) { delete enc; delete dec; continue; } enc->set_key(key); if(enc->valid_iv_length(iv.length())) enc->set_iv(iv); else if(!enc->valid_iv_length(0)) throw Invalid_IV_Length(algo, iv.length()); dec->set_key(key); if(dec->valid_iv_length(iv.length())) dec->set_iv(iv); else if(!dec->valid_iv_length(0)) throw Invalid_IV_Length(algo, iv.length()); bool enc_ok = test_filter_kat(enc, input, output); bool dec_ok = test_filter_kat(dec, output, input); all_results[provider] = enc_ok && dec_ok; } } return all_results; } namespace { void verify_results(const std::string& algo, const std::map<std::string, bool>& results) { for(std::map<std::string, bool>::const_iterator i = results.begin(); i != results.end(); ++i) { if(!i->second) throw Self_Test_Failure(algo + " self-test failed, provider "+ i->first); } } void hash_test(Algorithm_Factory& af, const std::string& name, const std::string& in, const std::string& out) { std::map<std::string, std::string> vars; vars["input"] = in; vars["output"] = out; verify_results(name, algorithm_kat(name, vars, af)); } void mac_test(Algorithm_Factory& af, const std::string& name, const std::string& in, const std::string& out, const std::string& key) { std::map<std::string, std::string> vars; vars["input"] = in; vars["output"] = out; vars["key"] = key; verify_results(name, algorithm_kat(name, vars, af)); } /* * Perform a KAT for a cipher */ void cipher_kat(Algorithm_Factory& af, const std::string& algo, const std::string& key_str, const std::string& iv_str, const std::string& in, const std::string& ecb_out, const std::string& cbc_out, const std::string& cfb_out, const std::string& ofb_out, const std::string& ctr_out) { SymmetricKey key(key_str); InitializationVector iv(iv_str); std::map<std::string, std::string> vars; vars["key"] = key_str; vars["iv"] = iv_str; vars["input"] = in; std::map<std::string, bool> results; vars["output"] = ecb_out; verify_results(algo + "/ECB", algorithm_kat(algo + "/ECB", vars, af)); vars["output"] = cbc_out; verify_results(algo + "/CBC", algorithm_kat(algo + "/CBC/NoPadding", vars, af)); vars["output"] = cfb_out; verify_results(algo + "/CFB", algorithm_kat(algo + "/CFB", vars, af)); vars["output"] = ofb_out; verify_results(algo + "/OFB", algorithm_kat(algo + "/OFB", vars, af)); vars["output"] = ctr_out; verify_results(algo + "/CTR", algorithm_kat(algo + "/CTR-BE", vars, af)); } } /* * Perform Self Tests */ bool passes_self_tests(Algorithm_Factory& af) { try { confirm_startup_self_tests(af); } catch(Self_Test_Failure) { return false; } return true; } /* * Perform Self Tests */ void confirm_startup_self_tests(Algorithm_Factory& af) { cipher_kat(af, "DES", "0123456789ABCDEF", "1234567890ABCDEF", "4E6F77206973207468652074696D6520666F7220616C6C20", "3FA40E8A984D48156A271787AB8883F9893D51EC4B563B53", "E5C7CDDE872BF27C43E934008C389C0F683788499A7C05F6", "F3096249C7F46E51A69E839B1A92F78403467133898EA622", "F3096249C7F46E5135F24A242EEB3D3F3D6D5BE3255AF8C3", "F3096249C7F46E51163A8CA0FFC94C27FA2F80F480B86F75"); cipher_kat(af, "TripleDES", "385D7189A5C3D485E1370AA5D408082B5CCCCB5E19F2D90E", "C141B5FCCD28DC8A", "6E1BD7C6120947A464A6AAB293A0F89A563D8D40D3461B68", "64EAAD4ACBB9CEAD6C7615E7C7E4792FE587D91F20C7D2F4", "6235A461AFD312973E3B4F7AA7D23E34E03371F8E8C376C9", "E26BA806A59B0330DE40CA38E77A3E494BE2B212F6DD624B", "E26BA806A59B03307DE2BCC25A08BA40A8BA335F5D604C62", "E26BA806A59B03303C62C2EFF32D3ACDD5D5F35EBCC53371"); cipher_kat(af, "AES-128", "2B7E151628AED2A6ABF7158809CF4F3C", "000102030405060708090A0B0C0D0E0F", "6BC1BEE22E409F96E93D7E117393172A" "AE2D8A571E03AC9C9EB76FAC45AF8E51", "3AD77BB40D7A3660A89ECAF32466EF97" "F5D3D58503B9699DE785895A96FDBAAF", "7649ABAC8119B246CEE98E9B12E9197D" "5086CB9B507219EE95DB113A917678B2", "3B3FD92EB72DAD20333449F8E83CFB4A" "C8A64537A0B3A93FCDE3CDAD9F1CE58B", "3B3FD92EB72DAD20333449F8E83CFB4A" "7789508D16918F03F53C52DAC54ED825", "3B3FD92EB72DAD20333449F8E83CFB4A" "010C041999E03F36448624483E582D0E"); hash_test(af, "SHA-1", "", "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709"); hash_test(af, "SHA-1", "616263", "A9993E364706816ABA3E25717850C26C9CD0D89D"); hash_test(af, "SHA-1", "6162636462636465636465666465666765666768666768696768696A" "68696A6B696A6B6C6A6B6C6D6B6C6D6E6C6D6E6F6D6E6F706E6F7071", "84983E441C3BD26EBAAE4AA1F95129E5E54670F1"); mac_test(af, "HMAC(SHA-1)", "4869205468657265", "B617318655057264E28BC0B6FB378C8EF146BE00", "0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B"); hash_test(af, "SHA-256", "", "E3B0C44298FC1C149AFBF4C8996FB924" "27AE41E4649B934CA495991B7852B855"); hash_test(af, "SHA-256", "616263", "BA7816BF8F01CFEA414140DE5DAE2223" "B00361A396177A9CB410FF61F20015AD"); hash_test(af, "SHA-256", "6162636462636465636465666465666765666768666768696768696A" "68696A6B696A6B6C6A6B6C6D6B6C6D6E6C6D6E6F6D6E6F706E6F7071", "248D6A61D20638B8E5C026930C3E6039" "A33CE45964FF2167F6ECEDD419DB06C1"); mac_test(af, "HMAC(SHA-256)", "4869205468657265", "198A607EB44BFBC69903A0F1CF2BBDC5" "BA0AA3F3D9AE3C1C7A3B1696A0B68CF7", "0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B" "0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B"); } } /* * Client Key Exchange Message * (C) 2004-2010 Jack Lloyd * * Released under the terms of the Botan license */ #include <memory> namespace Botan { /** * Create a new Client Key Exchange message */ Client_Key_Exchange::Client_Key_Exchange(RandomNumberGenerator& rng, Record_Writer& writer, HandshakeHash& hash, const Public_Key* pub_key, Version_Code using_version, Version_Code pref_version) { include_length = true; if(const DH_PublicKey* dh_pub = dynamic_cast<const DH_PublicKey*>(pub_key)) { DH_PrivateKey priv_key(rng, dh_pub->get_domain()); PK_Key_Agreement ka(priv_key, "Raw"); pre_master = ka.derive_key(0, dh_pub->public_value()).bits_of(); key_material = priv_key.public_value(); } else if(const RSA_PublicKey* rsa_pub = dynamic_cast<const RSA_PublicKey*>(pub_key)) { pre_master = rng.random_vec(48); pre_master[0] = (pref_version >> 8) & 0xFF; pre_master[1] = (pref_version ) & 0xFF; PK_Encryptor_EME encryptor(*rsa_pub, "PKCS1v15"); key_material = encryptor.encrypt(pre_master, rng); if(using_version == SSL_V3) include_length = false; } else throw Invalid_Argument("Client_Key_Exchange: Key not RSA or DH"); send(writer, hash); } /** * Read a Client Key Exchange message */ Client_Key_Exchange::Client_Key_Exchange(const MemoryRegion<byte>& contents, const CipherSuite& suite, Version_Code using_version) { include_length = true; if(using_version == SSL_V3 && (suite.kex_type() == TLS_ALGO_KEYEXCH_RSA)) include_length = false; deserialize(contents); } /** * Serialize a Client Key Exchange message */ SecureVector<byte> Client_Key_Exchange::serialize() const { if(include_length) { SecureVector<byte> buf; append_tls_length_value(buf, key_material, 2); return buf; } else return key_material; } /** * Deserialize a Client Key Exchange message */ void Client_Key_Exchange::deserialize(const MemoryRegion<byte>& buf) { if(include_length) { TLS_Data_Reader reader(buf); key_material = reader.get_range<byte>(2, 0, 65535); } else key_material = buf; } /** * Return the pre_master_secret */ SecureVector<byte> Client_Key_Exchange::pre_master_secret(RandomNumberGenerator& rng, const Private_Key* priv_key, Version_Code version) { if(const DH_PrivateKey* dh_priv = dynamic_cast<const DH_PrivateKey*>(priv_key)) { try { PK_Key_Agreement ka(*dh_priv, "Raw"); pre_master = ka.derive_key(0, key_material).bits_of(); } catch(...) { /* * Something failed in the DH computation. To avoid possible * timing attacks, randomize the pre-master output and carry * on, allowing the protocol to fail later in the finished * checks. */ pre_master = rng.random_vec(dh_priv->public_value().size()); } return pre_master; } else if(const RSA_PrivateKey* rsa_priv = dynamic_cast<const RSA_PrivateKey*>(priv_key)) { PK_Decryptor_EME decryptor(*rsa_priv, "PKCS1v15"); try { pre_master = decryptor.decrypt(key_material); if(pre_master.size() != 48 || make_u16bit(pre_master[0], pre_master[1]) != version) throw Decoding_Error("Client_Key_Exchange: Secret corrupted"); } catch(...) { pre_master = rng.random_vec(48); pre_master[0] = (version >> 8) & 0xFF; pre_master[1] = (version ) & 0xFF; } return pre_master; } else throw Invalid_Argument("Client_Key_Exchange: Bad key for decrypt"); } /** * Return the pre_master_secret */ SecureVector<byte> Client_Key_Exchange::pre_master_secret() const { return pre_master; } } /* * Certificate Request Message * (C) 2004-2006 Jack Lloyd * * Released under the terms of the Botan license */ namespace Botan { /** * Create a new Certificate Request message */ Certificate_Req::Certificate_Req(Record_Writer& writer, HandshakeHash& hash, const std::vector<X509_Certificate>& certs) { for(size_t i = 0; i != certs.size(); ++i) names.push_back(certs[i].subject_dn()); // FIXME: should be able to choose what to ask for types.push_back(RSA_CERT); types.push_back(DSS_CERT); send(writer, hash); } /** * Serialize a Certificate Request message */ SecureVector<byte> Certificate_Req::serialize() const { SecureVector<byte> buf; append_tls_length_value(buf, types, 1); DER_Encoder encoder; for(size_t i = 0; i != names.size(); ++i) encoder.encode(names[i]); append_tls_length_value(buf, encoder.get_contents(), 2); return buf; } /** * Deserialize a Certificate Request message */ void Certificate_Req::deserialize(const MemoryRegion<byte>& buf) { if(buf.size() < 4) throw Decoding_Error("Certificate_Req: Bad certificate request"); size_t types_size = buf[0]; if(buf.size() < types_size + 3) throw Decoding_Error("Certificate_Req: Bad certificate request"); for(size_t i = 0; i != types_size; ++i) types.push_back(static_cast<Certificate_Type>(buf[i+1])); size_t names_size = make_u16bit(buf[types_size+2], buf[types_size+3]); if(buf.size() != names_size + types_size + 3) throw Decoding_Error("Certificate_Req: Bad certificate request"); BER_Decoder decoder(&buf[types_size + 3], names_size); while(decoder.more_items()) { X509_DN name; decoder.decode(name); names.push_back(name); } } /** * Create a new Certificate message */ Certificate::Certificate(Record_Writer& writer, const std::vector<X509_Certificate>& cert_list, HandshakeHash& hash) { certs = cert_list; send(writer, hash); } /** * Serialize a Certificate message */ SecureVector<byte> Certificate::serialize() const { SecureVector<byte> buf(3); for(size_t i = 0; i != certs.size(); ++i) { SecureVector<byte> raw_cert = certs[i].BER_encode(); const size_t cert_size = raw_cert.size(); for(size_t i = 0; i != 3; ++i) buf.push_back(get_byte<u32bit>(i+1, cert_size)); buf += raw_cert; } const size_t buf_size = buf.size() - 3; for(size_t i = 0; i != 3; ++i) buf[i] = get_byte<u32bit>(i+1, buf_size); return buf; } /** * Deserialize a Certificate message */ void Certificate::deserialize(const MemoryRegion<byte>& buf) { if(buf.size() < 3) throw Decoding_Error("Certificate: Message malformed"); const size_t total_size = make_u32bit(0, buf[0], buf[1], buf[2]); SecureQueue queue; queue.write(&buf[3], buf.size() - 3); if(queue.size() != total_size) throw Decoding_Error("Certificate: Message malformed"); while(queue.size()) { if(queue.size() < 3) throw Decoding_Error("Certificate: Message malformed"); byte len[3]; queue.read(len, 3); const size_t cert_size = make_u32bit(0, len[0], len[1], len[2]); const size_t original_size = queue.size(); X509_Certificate cert(queue); if(queue.size() + cert_size != original_size) throw Decoding_Error("Certificate: Message malformed"); certs.push_back(cert); } } } /* * Certificate Verify Message * (C) 2004-2010 Jack Lloyd * * Released under the terms of the Botan license */ #include <memory> namespace Botan { /** * Create a new Certificate Verify message */ Certificate_Verify::Certificate_Verify(RandomNumberGenerator& rng, Record_Writer& writer, HandshakeHash& hash, const Private_Key* priv_key) { std::string padding = ""; Signature_Format format = IEEE_1363; if(priv_key->algo_name() == "RSA") padding = "EMSA3(TLS.Digest.0)"; else if(priv_key->algo_name() == "DSA") { padding = "EMSA1(SHA-1)"; format = DER_SEQUENCE; } else throw Invalid_Argument(priv_key->algo_name() + " is invalid/unknown for TLS signatures"); PK_Signer signer(*priv_key, padding, format); signature = signer.sign_message(hash.final(), rng); send(writer, hash); } /** * Serialize a Certificate Verify message */ SecureVector<byte> Certificate_Verify::serialize() const { SecureVector<byte> buf; const u16bit sig_len = signature.size(); buf.push_back(get_byte(0, sig_len)); buf.push_back(get_byte(1, sig_len)); buf += signature; return buf; } /** * Deserialize a Certificate Verify message */ void Certificate_Verify::deserialize(const MemoryRegion<byte>& buf) { TLS_Data_Reader reader(buf); signature = reader.get_range<byte>(2, 0, 65535); } /** * Verify a Certificate Verify message */ bool Certificate_Verify::verify(const X509_Certificate& cert, HandshakeHash& hash) { // FIXME: duplicate of Server_Key_Exchange::verify std::auto_ptr<Public_Key> key(cert.subject_public_key()); std::string padding = ""; Signature_Format format = IEEE_1363; if(key->algo_name() == "RSA") padding = "EMSA3(TLS.Digest.0)"; else if(key->algo_name() == "DSA") { padding = "EMSA1(SHA-1)"; format = DER_SEQUENCE; } else throw Invalid_Argument(key->algo_name() + " is invalid/unknown for TLS signatures"); PK_Verifier verifier(*key, padding, format); return verifier.verify_message(hash.final(), signature); } } /* * Finished Message * (C) 2004-2006 Jack Lloyd * * Released under the terms of the Botan license */ namespace Botan { /** * Create a new Finished message */ Finished::Finished(Record_Writer& writer, Version_Code version, Connection_Side side, const MemoryRegion<byte>& master_secret, HandshakeHash& hash) { verification_data = compute_verify(master_secret, hash, side, version); send(writer, hash); } /** * Serialize a Finished message */ SecureVector<byte> Finished::serialize() const { return verification_data; } /** * Deserialize a Finished message */ void Finished::deserialize(const MemoryRegion<byte>& buf) { verification_data = buf; } /** * Verify a Finished message */ bool Finished::verify(const MemoryRegion<byte>& secret, Version_Code version, const HandshakeHash& hash, Connection_Side side) { SecureVector<byte> computed = compute_verify(secret, hash, side, version); if(computed == verification_data) return true; return false; } /** * Compute the verify_data */ SecureVector<byte> Finished::compute_verify(const MemoryRegion<byte>& secret, HandshakeHash hash, Connection_Side side, Version_Code version) { if(version == SSL_V3) { const byte SSL_CLIENT_LABEL[] = { 0x43, 0x4C, 0x4E, 0x54 }; const byte SSL_SERVER_LABEL[] = { 0x53, 0x52, 0x56, 0x52 }; SecureVector<byte> ssl3_finished; if(side == CLIENT) hash.update(SSL_CLIENT_LABEL, sizeof(SSL_CLIENT_LABEL)); else hash.update(SSL_SERVER_LABEL, sizeof(SSL_SERVER_LABEL)); return hash.final_ssl3(secret); } else if(version == TLS_V10 || version == TLS_V11) { const byte TLS_CLIENT_LABEL[] = { 0x63, 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x66, 0x69, 0x6E, 0x69, 0x73, 0x68, 0x65, 0x64 }; const byte TLS_SERVER_LABEL[] = { 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x66, 0x69, 0x6E, 0x69, 0x73, 0x68, 0x65, 0x64 }; TLS_PRF prf; SecureVector<byte> input; if(side == CLIENT) input += std::make_pair(TLS_CLIENT_LABEL, sizeof(TLS_CLIENT_LABEL)); else input += std::make_pair(TLS_SERVER_LABEL, sizeof(TLS_SERVER_LABEL)); input += hash.final(); return prf.derive_key(12, secret, input); } else throw Invalid_Argument("Finished message: Unknown protocol version"); } } /* * TLS Hello Messages * (C) 2004-2010 Jack Lloyd * * Released under the terms of the Botan license */ namespace Botan { /* * Encode and send a Handshake message */ void HandshakeMessage::send(Record_Writer& writer, HandshakeHash& hash) const { SecureVector<byte> buf = serialize(); SecureVector<byte> send_buf(4); const size_t buf_size = buf.size(); send_buf[0] = type(); for(size_t i = 1; i != 4; ++i) send_buf[i] = get_byte<u32bit>(i, buf_size); send_buf += buf; hash.update(send_buf); writer.send(HANDSHAKE, &send_buf[0], send_buf.size()); writer.flush(); } /* * Create a new Hello Request message */ Hello_Request::Hello_Request(Record_Writer& writer) { HandshakeHash dummy; // FIXME: *UGLY* send(writer, dummy); } /* * Serialize a Hello Request message */ SecureVector<byte> Hello_Request::serialize() const { return SecureVector<byte>(); } /* * Deserialize a Hello Request message */ void Hello_Request::deserialize(const MemoryRegion<byte>& buf) { if(buf.size()) throw Decoding_Error("Hello_Request: Must be empty, and is not"); } /* * Create a new Client Hello message */ Client_Hello::Client_Hello(RandomNumberGenerator& rng, Record_Writer& writer, const TLS_Policy& policy, HandshakeHash& hash) { c_random = rng.random_vec(32); suites = policy.ciphersuites(); comp_algos = policy.compression(); c_version = policy.pref_version(); send(writer, hash); } /* * Serialize a Client Hello message */ SecureVector<byte> Client_Hello::serialize() const { SecureVector<byte> buf; buf.push_back(static_cast<byte>(c_version >> 8)); buf.push_back(static_cast<byte>(c_version )); buf += c_random; append_tls_length_value(buf, sess_id, 1); append_tls_length_value(buf, suites, 2); append_tls_length_value(buf, comp_algos, 1); return buf; } void Client_Hello::deserialize_sslv2(const MemoryRegion<byte>& buf) { if(buf.size() < 12 || buf[0] != 1) throw Decoding_Error("Client_Hello: SSLv2 hello corrupted"); const size_t cipher_spec_len = make_u16bit(buf[3], buf[4]); const size_t sess_id_len = make_u16bit(buf[5], buf[6]); const size_t challenge_len = make_u16bit(buf[7], buf[8]); const size_t expected_size = (9 + sess_id_len + cipher_spec_len + challenge_len); if(buf.size() != expected_size) throw Decoding_Error("Client_Hello: SSLv2 hello corrupted"); if(sess_id_len != 0 || cipher_spec_len % 3 != 0 || (challenge_len < 16 || challenge_len > 32)) { throw Decoding_Error("Client_Hello: SSLv2 hello corrupted"); } for(size_t i = 9; i != 9 + cipher_spec_len; i += 3) { if(buf[i] != 0) // a SSLv2 cipherspec; ignore it continue; suites.push_back(make_u16bit(buf[i+1], buf[i+2])); } c_version = static_cast<Version_Code>(make_u16bit(buf[1], buf[2])); c_random.resize(challenge_len); copy_mem(&c_random[0], &buf[9+cipher_spec_len+sess_id_len], challenge_len); } /* * Deserialize a Client Hello message */ void Client_Hello::deserialize(const MemoryRegion<byte>& buf) { if(buf.size() == 0) throw Decoding_Error("Client_Hello: Packet corrupted"); if(buf.size() < 41) throw Decoding_Error("Client_Hello: Packet corrupted"); TLS_Data_Reader reader(buf); c_version = static_cast<Version_Code>(reader.get_u16bit()); c_random = reader.get_fixed<byte>(32); sess_id = reader.get_range<byte>(1, 0, 32); suites = reader.get_range_vector<u16bit>(2, 1, 32767); comp_algos = reader.get_range_vector<byte>(1, 1, 255); if(reader.has_remaining()) { const u16bit all_extn_size = reader.get_u16bit(); if(reader.remaining_bytes() != all_extn_size) throw Decoding_Error("Client_Hello: Bad extension size"); while(reader.has_remaining()) { const u16bit extension_code = reader.get_u16bit(); const u16bit extension_size = reader.get_u16bit(); if(extension_code == TLSEXT_SERVER_NAME_INDICATION) { u16bit name_bytes = reader.get_u16bit(); while(name_bytes) { byte name_type = reader.get_byte(); name_bytes--; if(name_type == 0) // DNS { std::vector<byte> name = reader.get_range_vector<byte>(2, 1, 65535); requested_hostname.assign( reinterpret_cast<const char*>(&name[0]), name.size()); name_bytes -= (2 + name.size()); } else { reader.discard_next(name_bytes); name_bytes = 0; } } } else if(extension_code == TLSEXT_SRP_IDENTIFIER) { std::vector<byte> name = reader.get_range_vector<byte>(1, 1, 255); requested_srp_id.assign( reinterpret_cast<char*>(&name[0]), name.size()); } else { reader.discard_next(extension_size); } } } } /* * Check if we offered this ciphersuite */ bool Client_Hello::offered_suite(u16bit ciphersuite) const { for(size_t i = 0; i != suites.size(); ++i) if(suites[i] == ciphersuite) return true; return false; } /* * Create a new Server Hello message */ Server_Hello::Server_Hello(RandomNumberGenerator& rng, Record_Writer& writer, const TLS_Policy& policy, const std::vector<X509_Certificate>& certs, const Client_Hello& c_hello, Version_Code ver, HandshakeHash& hash) { bool have_rsa = false, have_dsa = false; for(size_t i = 0; i != certs.size(); ++i) { Public_Key* key = certs[i].subject_public_key(); if(key->algo_name() == "RSA") have_rsa = true; if(key->algo_name() == "DSA") have_dsa = true; } suite = policy.choose_suite(c_hello.ciphersuites(), have_rsa, have_dsa); if(suite == 0) throw TLS_Exception(PROTOCOL_VERSION, "Can't agree on a ciphersuite with client"); comp_algo = policy.choose_compression(c_hello.compression_algos()); s_version = ver; s_random = rng.random_vec(32); send(writer, hash); } /* * Serialize a Server Hello message */ SecureVector<byte> Server_Hello::serialize() const { SecureVector<byte> buf; buf.push_back(static_cast<byte>(s_version >> 8)); buf.push_back(static_cast<byte>(s_version )); buf += s_random; append_tls_length_value(buf, sess_id, 1); buf.push_back(get_byte(0, suite)); buf.push_back(get_byte(1, suite)); buf.push_back(comp_algo); return buf; } /* * Deserialize a Server Hello message */ void Server_Hello::deserialize(const MemoryRegion<byte>& buf) { if(buf.size() < 38) throw Decoding_Error("Server_Hello: Packet corrupted"); TLS_Data_Reader reader(buf); s_version = static_cast<Version_Code>(reader.get_u16bit()); if(s_version != SSL_V3 && s_version != TLS_V10 && s_version != TLS_V11) { throw TLS_Exception(PROTOCOL_VERSION, "Server_Hello: Unsupported server version"); } s_random = reader.get_fixed<byte>(32); sess_id = reader.get_range<byte>(1, 0, 32); suite = reader.get_u16bit(); comp_algo = reader.get_byte(); } /* * Create a new Server Hello Done message */ Server_Hello_Done::Server_Hello_Done(Record_Writer& writer, HandshakeHash& hash) { send(writer, hash); } /* * Serialize a Server Hello Done message */ SecureVector<byte> Server_Hello_Done::serialize() const { return SecureVector<byte>(); } /* * Deserialize a Server Hello Done message */ void Server_Hello_Done::deserialize(const MemoryRegion<byte>& buf) { if(buf.size()) throw Decoding_Error("Server_Hello_Done: Must be empty, and is not"); } } /* * TLS Record Reading * (C) 2004-2010 Jack Lloyd * * Released under the terms of the Botan license */ namespace Botan { /* * Reset the state */ void Record_Reader::reset() { cipher.reset(); delete mac; mac = 0; mac_size = 0; block_size = 0; iv_size = 0; major = minor = 0; seq_no = 0; } /* * Set the version to use */ void Record_Reader::set_version(Version_Code version) { if(version != SSL_V3 && version != TLS_V10 && version != TLS_V11) throw Invalid_Argument("Record_Reader: Invalid protocol version"); major = (version >> 8) & 0xFF; minor = (version & 0xFF); } /* * Set the keys for reading */ void Record_Reader::set_keys(const CipherSuite& suite, const SessionKeys& keys, Connection_Side side) { cipher.reset(); delete mac; mac = 0; seq_no = 0; SymmetricKey mac_key, cipher_key; InitializationVector iv; if(side == CLIENT) { cipher_key = keys.server_cipher_key(); iv = keys.server_iv(); mac_key = keys.server_mac_key(); } else { cipher_key = keys.client_cipher_key(); iv = keys.client_iv(); mac_key = keys.client_mac_key(); } const std::string cipher_algo = suite.cipher_algo(); const std::string mac_algo = suite.mac_algo(); if(have_block_cipher(cipher_algo)) { cipher.append(get_cipher( cipher_algo + "/CBC/NoPadding", cipher_key, iv, DECRYPTION) ); block_size = block_size_of(cipher_algo); if(major > 3 || (major == 3 && minor >= 2)) iv_size = block_size; else iv_size = 0; } else if(have_stream_cipher(cipher_algo)) { cipher.append(get_cipher(cipher_algo, cipher_key, DECRYPTION)); block_size = 0; iv_size = 0; } else throw Invalid_Argument("Record_Reader: Unknown cipher " + cipher_algo); if(have_hash(mac_algo)) { Algorithm_Factory& af = global_state().algorithm_factory(); if(major == 3 && minor == 0) mac = af.make_mac("SSL3-MAC(" + mac_algo + ")"); else mac = af.make_mac("HMAC(" + mac_algo + ")"); mac->set_key(mac_key); mac_size = mac->output_length(); } else throw Invalid_Argument("Record_Reader: Unknown hash " + mac_algo); } void Record_Reader::add_input(const byte input[], size_t input_size) { input_queue.write(input, input_size); } /* * Retrieve the next record */ size_t Record_Reader::get_record(byte& msg_type, MemoryRegion<byte>& output) { byte header[5] = { 0 }; const size_t have_in_queue = input_queue.size(); if(have_in_queue < sizeof(header)) return (sizeof(header) - have_in_queue); /* * We peek first to make sure we have the full record */ input_queue.peek(header, sizeof(header)); // SSLv2-format client hello? if(header[0] & 0x80 && header[2] == 1 && header[3] == 3) { size_t record_len = make_u16bit(header[0], header[1]) & 0x7FFF; if(have_in_queue < record_len + 2) return (record_len + 2 - have_in_queue); msg_type = HANDSHAKE; output.resize(record_len + 4); input_queue.read(&output[2], record_len + 2); output[0] = CLIENT_HELLO_SSLV2; output[1] = 0; output[2] = header[0] & 0x7F; output[3] = header[1]; return 0; } if(header[0] != CHANGE_CIPHER_SPEC && header[0] != ALERT && header[0] != HANDSHAKE && header[0] != APPLICATION_DATA) { throw TLS_Exception(UNEXPECTED_MESSAGE, "Record_Reader: Unknown record type"); } const u16bit version = make_u16bit(header[1], header[2]); const u16bit record_len = make_u16bit(header[3], header[4]); if(major && (header[1] != major || header[2] != minor)) throw TLS_Exception(PROTOCOL_VERSION, "Record_Reader: Got unexpected version"); // If insufficient data, return without doing anything if(have_in_queue < (sizeof(header) + record_len)) return (sizeof(header) + record_len - have_in_queue); SecureVector<byte> buffer(record_len); input_queue.read(header, sizeof(header)); // pull off the header input_queue.read(&buffer[0], buffer.size()); /* * We are handshaking, no crypto to do so return as-is * TODO: Check msg_type to confirm a handshake? */ if(mac_size == 0) { msg_type = header[0]; output = buffer; return 0; // got a full record } // Otherwise, decrypt, check MAC, return plaintext cipher.process_msg(buffer); SecureVector<byte> plaintext = cipher.read_all(Pipe::LAST_MESSAGE); size_t pad_size = 0; if(block_size) { byte pad_value = plaintext[plaintext.size()-1]; pad_size = pad_value + 1; /* * Check the padding; if it is wrong, then say we have 0 bytes of * padding, which should ensure that the MAC check below does not * suceed. This hides a timing channel. * * This particular countermeasure is recommended in the TLS 1.2 * spec (RFC 5246) in section 6.2.3.2 */ if(version == SSL_V3) { if(pad_value > block_size) pad_size = 0; } else { bool padding_good = true; for(size_t i = 0; i != pad_size; ++i) if(plaintext[plaintext.size()-i-1] != pad_value) padding_good = false; if(!padding_good) pad_size = 0; } } if(plaintext.size() < mac_size + pad_size + iv_size) throw Decoding_Error("Record_Reader: Record truncated"); const size_t mac_offset = plaintext.size() - (mac_size + pad_size); SecureVector<byte> received_mac(&plaintext[mac_offset], mac_size); const u16bit plain_length = plaintext.size() - (mac_size + pad_size + iv_size); mac->update_be(seq_no); mac->update(header[0]); // msg_type if(version != SSL_V3) for(size_t i = 0; i != 2; ++i) mac->update(get_byte(i, version)); mac->update_be(plain_length); mac->update(&plaintext[iv_size], plain_length); ++seq_no; SecureVector<byte> computed_mac = mac->final(); if(received_mac != computed_mac) throw TLS_Exception(BAD_RECORD_MAC, "Record_Reader: MAC failure"); msg_type = header[0]; output.resize(plain_length); copy_mem(&output[0], &plaintext[iv_size], plain_length); return 0; } } /* * TLS Record Writing * (C) 2004-2010 Jack Lloyd * * Released under the terms of the Botan license */ namespace Botan { /** * Record_Writer Constructor */ Record_Writer::Record_Writer(std::tr1::function<void (const byte[], size_t)> out) : output_fn(out), buffer(DEFAULT_BUFFERSIZE) { mac = 0; reset(); } /** * Reset the state */ void Record_Writer::reset() { cipher.reset(); delete mac; mac = 0; zeroise(buffer); buf_pos = 0; major = minor = buf_type = 0; block_size = 0; mac_size = 0; iv_size = 0; seq_no = 0; } /** * Set the version to use */ void Record_Writer::set_version(Version_Code version) { if(version != SSL_V3 && version != TLS_V10 && version != TLS_V11) throw Invalid_Argument("Record_Writer: Invalid protocol version"); major = (version >> 8) & 0xFF; minor = (version & 0xFF); } /** * Set the keys for writing */ void Record_Writer::set_keys(const CipherSuite& suite, const SessionKeys& keys, Connection_Side side) { cipher.reset(); delete mac; mac = 0; seq_no = 0; SymmetricKey mac_key, cipher_key; InitializationVector iv; if(side == CLIENT) { cipher_key = keys.client_cipher_key(); iv = keys.client_iv(); mac_key = keys.client_mac_key(); } else { cipher_key = keys.server_cipher_key(); iv = keys.server_iv(); mac_key = keys.server_mac_key(); } const std::string cipher_algo = suite.cipher_algo(); const std::string mac_algo = suite.mac_algo(); if(have_block_cipher(cipher_algo)) { cipher.append(get_cipher( cipher_algo + "/CBC/NoPadding", cipher_key, iv, ENCRYPTION) ); block_size = block_size_of(cipher_algo); if(major > 3 || (major == 3 && minor >= 2)) iv_size = block_size; else iv_size = 0; } else if(have_stream_cipher(cipher_algo)) { cipher.append(get_cipher(cipher_algo, cipher_key, ENCRYPTION)); block_size = 0; iv_size = 0; } else throw Invalid_Argument("Record_Writer: Unknown cipher " + cipher_algo); if(have_hash(mac_algo)) { Algorithm_Factory& af = global_state().algorithm_factory(); if(major == 3 && minor == 0) mac = af.make_mac("SSL3-MAC(" + mac_algo + ")"); else mac = af.make_mac("HMAC(" + mac_algo + ")"); mac->set_key(mac_key); mac_size = mac->output_length(); } else throw Invalid_Argument("Record_Writer: Unknown hash " + mac_algo); } /** * Send one or more records to the other side */ void Record_Writer::send(byte type, const byte input[], size_t length) { if(type != buf_type) flush(); const size_t BUFFER_SIZE = buffer.size(); buf_type = type; // FIXME: compression right here buffer.copy(buf_pos, input, length); if(buf_pos + length >= BUFFER_SIZE) { send_record(buf_type, &buffer[0], length); input += (BUFFER_SIZE - buf_pos); length -= (BUFFER_SIZE - buf_pos); while(length >= BUFFER_SIZE) { send_record(buf_type, input, BUFFER_SIZE); input += BUFFER_SIZE; length -= BUFFER_SIZE; } buffer.copy(input, length); buf_pos = 0; } buf_pos += length; } /** * Split buffer into records, and send them all */ void Record_Writer::flush() { const byte* buf_ptr = &buffer[0]; size_t offset = 0; while(offset != buf_pos) { size_t record_size = buf_pos - offset; if(record_size > MAX_PLAINTEXT_SIZE) record_size = MAX_PLAINTEXT_SIZE; send_record(buf_type, buf_ptr + offset, record_size); offset += record_size; } buf_type = 0; buf_pos = 0; } /** * Encrypt and send the record */ void Record_Writer::send_record(byte type, const byte buf[], size_t length) { if(length >= MAX_COMPRESSED_SIZE) throw TLS_Exception(INTERNAL_ERROR, "Record_Writer: Compressed packet is too big"); if(mac_size == 0) send_record(type, major, minor, buf, length); else { mac->update_be(seq_no); mac->update(type); if(major > 3 || (major == 3 && minor != 0)) { mac->update(major); mac->update(minor); } mac->update(get_byte<u16bit>(0, length)); mac->update(get_byte<u16bit>(1, length)); mac->update(buf, length); SecureVector<byte> buf_mac = mac->final(); // TODO: This could all use a single buffer cipher.start_msg(); if(iv_size) { RandomNumberGenerator& rng = global_state().global_rng(); SecureVector<byte> random_iv(iv_size); rng.randomize(&random_iv[0], random_iv.size()); cipher.write(random_iv); } cipher.write(buf, length); cipher.write(buf_mac); if(block_size) { size_t pad_val = (block_size - (1 + length + buf_mac.size())) % block_size; for(size_t i = 0; i != pad_val + 1; ++i) cipher.write(pad_val); } cipher.end_msg(); SecureVector<byte> output = cipher.read_all(Pipe::LAST_MESSAGE); send_record(type, major, minor, &output[0], output.size()); seq_no++; } } /** * Send a final record packet */ void Record_Writer::send_record(byte type, byte major, byte minor, const byte out[], size_t length) { if(length >= MAX_CIPHERTEXT_SIZE) throw TLS_Exception(INTERNAL_ERROR, "Record_Writer: Record is too big"); byte header[5] = { type, major, minor, 0 }; for(size_t i = 0; i != 2; ++i) header[i+3] = get_byte<u16bit>(i, length); output_fn(header, 5); output_fn(out, length); } /** * Send an alert */ void Record_Writer::alert(Alert_Level level, Alert_Type type) { byte alert[2] = { level, type }; send(ALERT, alert, sizeof(alert)); flush(); } } /* * Server Key Exchange Message * (C) 2004-2010 Jack Lloyd * * Released under the terms of the Botan license */ #include <memory> namespace Botan { /** * Create a new Server Key Exchange message */ Server_Key_Exchange::Server_Key_Exchange(RandomNumberGenerator& rng, Record_Writer& writer, const Public_Key* kex_key, const Private_Key* priv_key, const MemoryRegion<byte>& c_random, const MemoryRegion<byte>& s_random, HandshakeHash& hash) { const DH_PublicKey* dh_pub = dynamic_cast<const DH_PublicKey*>(kex_key); const RSA_PublicKey* rsa_pub = dynamic_cast<const RSA_PublicKey*>(kex_key); if(dh_pub) { params.push_back(dh_pub->get_domain().get_p()); params.push_back(dh_pub->get_domain().get_g()); params.push_back(BigInt::decode(dh_pub->public_value())); } else if(rsa_pub) { params.push_back(rsa_pub->get_n()); params.push_back(rsa_pub->get_e()); } else throw Invalid_Argument("Bad key for TLS key exchange: not DH or RSA"); std::string padding = ""; Signature_Format format = IEEE_1363; if(priv_key->algo_name() == "RSA") padding = "EMSA3(TLS.Digest.0)"; else if(priv_key->algo_name() == "DSA") { padding = "EMSA1(SHA-1)"; format = DER_SEQUENCE; } else throw Invalid_Argument(priv_key->algo_name() + " is invalid/unknown for TLS signatures"); PK_Signer signer(*priv_key, padding, format); signer.update(c_random); signer.update(s_random); signer.update(serialize_params()); signature = signer.signature(rng); send(writer, hash); } /** * Serialize a Server Key Exchange message */ SecureVector<byte> Server_Key_Exchange::serialize() const { SecureVector<byte> buf = serialize_params(); append_tls_length_value(buf, signature, 2); return buf; } /** * Serialize the ServerParams structure */ SecureVector<byte> Server_Key_Exchange::serialize_params() const { SecureVector<byte> buf; for(size_t i = 0; i != params.size(); ++i) append_tls_length_value(buf, BigInt::encode(params[i]), 2); return buf; } /** * Deserialize a Server Key Exchange message */ void Server_Key_Exchange::deserialize(const MemoryRegion<byte>& buf) { if(buf.size() < 6) throw Decoding_Error("Server_Key_Exchange: Packet corrupted"); SecureVector<byte> values[4]; size_t so_far = 0; for(size_t i = 0; i != 4; ++i) { const u16bit len = make_u16bit(buf[so_far], buf[so_far+1]); so_far += 2; if(len + so_far > buf.size()) throw Decoding_Error("Server_Key_Exchange: Packet corrupted"); values[i].resize(len); copy_mem(&values[i][0], &buf[so_far], len); so_far += len; if(i == 2 && so_far == buf.size()) break; } params.push_back(BigInt::decode(values[0])); params.push_back(BigInt::decode(values[1])); if(values[3].size()) { params.push_back(BigInt::decode(values[2])); signature = values[3]; } else signature = values[2]; } /** * Return the public key */ Public_Key* Server_Key_Exchange::key() const { if(params.size() == 2) return new RSA_PublicKey(params[0], params[1]); else if(params.size() == 3) return new DH_PublicKey(DL_Group(params[0], params[1]), params[2]); else throw Internal_Error("Server_Key_Exchange::key: No key set"); } /** * Verify a Server Key Exchange message */ bool Server_Key_Exchange::verify(const X509_Certificate& cert, const MemoryRegion<byte>& c_random, const MemoryRegion<byte>& s_random) const { std::auto_ptr<Public_Key> key(cert.subject_public_key()); std::string padding = ""; Signature_Format format = IEEE_1363; if(key->algo_name() == "RSA") padding = "EMSA3(TLS.Digest.0)"; else if(key->algo_name() == "DSA") { padding = "EMSA1(SHA-1)"; format = DER_SEQUENCE; } else throw Invalid_Argument(key->algo_name() + " is invalid/unknown for TLS signatures"); PK_Verifier verifier(*key, padding, format); SecureVector<byte> params_got = serialize_params(); verifier.update(c_random); verifier.update(s_random); verifier.update(params_got); return verifier.check_signature(signature); } } /* * TLS Client * (C) 2004-2010 Jack Lloyd * * Released under the terms of the Botan license */ namespace Botan { namespace { /** * Verify the state transition is allowed * FIXME: checks are wrong for session reuse (add a flag for that) */ void client_check_state(Handshake_Type new_msg, Handshake_State* state) { class State_Transition_Error : public Unexpected_Message { public: State_Transition_Error(const std::string& err) : Unexpected_Message("State transition error from " + err) {} }; if(new_msg == HELLO_REQUEST) { if(state->client_hello) throw State_Transition_Error("HelloRequest"); } else if(new_msg == SERVER_HELLO) { if(!state->client_hello || state->server_hello) throw State_Transition_Error("ServerHello"); } else if(new_msg == CERTIFICATE) { if(!state->server_hello || state->server_kex || state->cert_req || state->server_hello_done) throw State_Transition_Error("ServerCertificate"); } else if(new_msg == SERVER_KEX) { if(!state->server_hello || state->server_kex || state->cert_req || state->server_hello_done) throw State_Transition_Error("ServerKeyExchange"); } else if(new_msg == CERTIFICATE_REQUEST) { if(!state->server_certs || state->cert_req || state->server_hello_done) throw State_Transition_Error("CertificateRequest"); } else if(new_msg == SERVER_HELLO_DONE) { if(!state->server_hello || state->server_hello_done) throw State_Transition_Error("ServerHelloDone"); } else if(new_msg == HANDSHAKE_CCS) { if(!state->client_finished || state->server_finished) throw State_Transition_Error("ServerChangeCipherSpec"); } else if(new_msg == FINISHED) { if(!state->got_server_ccs) throw State_Transition_Error("ServerFinished"); } else throw Unexpected_Message("Unexpected message in handshake"); } } /** * TLS Client Constructor */ TLS_Client::TLS_Client(std::tr1::function<size_t (byte[], size_t)> input_fn, std::tr1::function<void (const byte[], size_t)> output_fn, const TLS_Policy& policy, RandomNumberGenerator& rng) : input_fn(input_fn), policy(policy), rng(rng), writer(output_fn) { initialize(); } void TLS_Client::add_client_cert(const X509_Certificate& cert, Private_Key* cert_key) { certs.push_back(std::make_pair(cert, cert_key)); } /** * TLS Client Destructor */ TLS_Client::~TLS_Client() { close(); for(size_t i = 0; i != certs.size(); i++) delete certs[i].second; delete state; } /** * Initialize a TLS client connection */ void TLS_Client::initialize() { std::string error_str; Alert_Type error_type = NO_ALERT_TYPE; try { state = 0; active = false; writer.set_version(policy.pref_version()); do_handshake(); } catch(TLS_Exception& e) { error_str = e.what(); error_type = e.type(); } catch(std::exception& e) { error_str = e.what(); error_type = HANDSHAKE_FAILURE; } if(error_type != NO_ALERT_TYPE) { if(active) { active = false; reader.reset(); writer.alert(FATAL, error_type); writer.reset(); } if(state) { delete state; state = 0; } throw Stream_IO_Error("TLS_Client: Handshake failed: " + error_str); } } /** * Return the peer's certificate chain */ std::vector<X509_Certificate> TLS_Client::peer_cert_chain() const { return peer_certs; } /** * Write to a TLS connection */ void TLS_Client::write(const byte buf[], size_t length) { if(!active) throw TLS_Exception(INTERNAL_ERROR, "TLS_Client::write called while closed"); writer.send(APPLICATION_DATA, buf, length); } /** * Read from a TLS connection */ size_t TLS_Client::read(byte out[], size_t length) { if(!active) return 0; writer.flush(); while(read_buf.size() == 0) { state_machine(); if(active == false) break; } size_t got = std::min<size_t>(read_buf.size(), length); read_buf.read(out, got); return got; } /** * Close a TLS connection */ void TLS_Client::close() { close(WARNING, CLOSE_NOTIFY); } /** * Check connection status */ bool TLS_Client::is_closed() const { if(!active) return true; return false; } /** * Close a TLS connection */ void TLS_Client::close(Alert_Level level, Alert_Type alert_code) { if(active) { try { writer.alert(level, alert_code); writer.flush(); } catch(...) {} active = false; } } /** * Iterate the TLS state machine */ void TLS_Client::state_machine() { byte rec_type = CONNECTION_CLOSED; SecureVector<byte> record(1024); size_t bytes_needed = reader.get_record(rec_type, record); while(bytes_needed) { size_t to_get = std::min<size_t>(record.size(), bytes_needed); size_t got = input_fn(&record[0], to_get); if(got == 0) { rec_type = CONNECTION_CLOSED; break; } reader.add_input(&record[0], got); bytes_needed = reader.get_record(rec_type, record); } if(rec_type == CONNECTION_CLOSED) { active = false; reader.reset(); writer.reset(); } else if(rec_type == APPLICATION_DATA) { if(active) read_buf.write(&record[0], record.size()); else throw Unexpected_Message("Application data before handshake done"); } else if(rec_type == HANDSHAKE || rec_type == CHANGE_CIPHER_SPEC) read_handshake(rec_type, record); else if(rec_type == ALERT) { Alert alert(record); if(alert.is_fatal() || alert.type() == CLOSE_NOTIFY) { if(alert.type() == CLOSE_NOTIFY) writer.alert(WARNING, CLOSE_NOTIFY); reader.reset(); writer.reset(); active = false; if(state) { delete state; state = 0; } } } else throw Unexpected_Message("Unknown message type received"); } /** * Split up and process handshake messages */ void TLS_Client::read_handshake(byte rec_type, const MemoryRegion<byte>& rec_buf) { if(rec_type == HANDSHAKE) { if(!state) return; state->queue.write(&rec_buf[0], rec_buf.size()); } while(true) { Handshake_Type type = HANDSHAKE_NONE; SecureVector<byte> contents; if(rec_type == HANDSHAKE) { if(state->queue.size() >= 4) { byte head[4] = { 0 }; state->queue.peek(head, 4); const size_t length = make_u32bit(0, head[1], head[2], head[3]); if(state->queue.size() >= length + 4) { type = static_cast<Handshake_Type>(head[0]); contents.resize(length); state->queue.read(head, 4); state->queue.read(&contents[0], contents.size()); } } } else if(rec_type == CHANGE_CIPHER_SPEC) { if(state->queue.size() == 0 && rec_buf.size() == 1 && rec_buf[0] == 1) type = HANDSHAKE_CCS; else throw Decoding_Error("Malformed ChangeCipherSpec message"); } else throw Decoding_Error("Unknown message type in handshake processing"); if(type == HANDSHAKE_NONE) break; process_handshake_msg(type, contents); if(type == HANDSHAKE_CCS || !state) break; } } /** * Process a handshake message */ void TLS_Client::process_handshake_msg(Handshake_Type type, const MemoryRegion<byte>& contents) { if(type == HELLO_REQUEST) return; if(state == 0) throw Unexpected_Message("Unexpected handshake message"); if(type != HANDSHAKE_CCS && type != HELLO_REQUEST && type != FINISHED) { state->hash.update(static_cast<byte>(type)); const size_t record_length = contents.size(); for(size_t i = 0; i != 3; i++) state->hash.update(get_byte<u32bit>(i+1, record_length)); state->hash.update(contents); } if(type == SERVER_HELLO) { client_check_state(type, state); state->server_hello = new Server_Hello(contents); if(!state->client_hello->offered_suite( state->server_hello->ciphersuite() ) ) throw TLS_Exception(HANDSHAKE_FAILURE, "TLS_Client: Server replied with bad ciphersuite"); state->version = state->server_hello->version(); if(state->version > state->client_hello->version()) throw TLS_Exception(HANDSHAKE_FAILURE, "TLS_Client: Server replied with bad version"); if(state->version < policy.min_version()) throw TLS_Exception(PROTOCOL_VERSION, "TLS_Client: Server is too old for specified policy"); writer.set_version(state->version); reader.set_version(state->version); state->suite = CipherSuite(state->server_hello->ciphersuite()); } else if(type == CERTIFICATE) { client_check_state(type, state); if(state->suite.sig_type() == TLS_ALGO_SIGNER_ANON) throw Unexpected_Message("Recived certificate from anonymous server"); state->server_certs = new Certificate(contents); peer_certs = state->server_certs->cert_chain(); if(peer_certs.size() == 0) throw TLS_Exception(HANDSHAKE_FAILURE, "TLS_Client: No certificates sent by server"); if(!policy.check_cert(peer_certs)) throw TLS_Exception(BAD_CERTIFICATE, "TLS_Client: Server certificate is not valid"); state->kex_pub = peer_certs[0].subject_public_key(); bool is_dsa = false, is_rsa = false; if(dynamic_cast<DSA_PublicKey*>(state->kex_pub)) is_dsa = true; else if(dynamic_cast<RSA_PublicKey*>(state->kex_pub)) is_rsa = true; else throw TLS_Exception(UNSUPPORTED_CERTIFICATE, "Unknown key type received in server kex"); if((is_dsa && state->suite.sig_type() != TLS_ALGO_SIGNER_DSA) || (is_rsa && state->suite.sig_type() != TLS_ALGO_SIGNER_RSA)) throw TLS_Exception(ILLEGAL_PARAMETER, "Certificate key type did not match ciphersuite"); } else if(type == SERVER_KEX) { client_check_state(type, state); if(state->suite.kex_type() == TLS_ALGO_KEYEXCH_NOKEX) throw Unexpected_Message("Unexpected key exchange from server"); state->server_kex = new Server_Key_Exchange(contents); if(state->kex_pub) delete state->kex_pub; state->kex_pub = state->server_kex->key(); bool is_dh = false, is_rsa = false; if(dynamic_cast<DH_PublicKey*>(state->kex_pub)) is_dh = true; else if(dynamic_cast<RSA_PublicKey*>(state->kex_pub)) is_rsa = true; else throw TLS_Exception(HANDSHAKE_FAILURE, "Unknown key type received in server kex"); if((is_dh && state->suite.kex_type() != TLS_ALGO_KEYEXCH_DH) || (is_rsa && state->suite.kex_type() != TLS_ALGO_KEYEXCH_RSA)) throw TLS_Exception(ILLEGAL_PARAMETER, "Certificate key type did not match ciphersuite"); if(state->suite.sig_type() != TLS_ALGO_SIGNER_ANON) { if(!state->server_kex->verify(peer_certs[0], state->client_hello->random(), state->server_hello->random())) throw TLS_Exception(DECRYPT_ERROR, "Bad signature on server key exchange"); } } else if(type == CERTIFICATE_REQUEST) { client_check_state(type, state); state->cert_req = new Certificate_Req(contents); state->do_client_auth = true; } else if(type == SERVER_HELLO_DONE) { client_check_state(type, state); state->server_hello_done = new Server_Hello_Done(contents); if(state->do_client_auth) { std::vector<X509_Certificate> send_certs; std::vector<Certificate_Type> types = state->cert_req->acceptable_types(); // FIXME: Fill in useful certs here, if any state->client_certs = new Certificate(writer, send_certs, state->hash); } state->client_kex = new Client_Key_Exchange(rng, writer, state->hash, state->kex_pub, state->version, state->client_hello->version()); if(state->do_client_auth) { Private_Key* key_matching_cert = 0; // FIXME state->client_verify = new Certificate_Verify(rng, writer, state->hash, key_matching_cert); } state->keys = SessionKeys(state->suite, state->version, state->client_kex->pre_master_secret(), state->client_hello->random(), state->server_hello->random()); writer.send(CHANGE_CIPHER_SPEC, 1); writer.flush(); writer.set_keys(state->suite, state->keys, CLIENT); state->client_finished = new Finished(writer, state->version, CLIENT, state->keys.master_secret(), state->hash); } else if(type == HANDSHAKE_CCS) { client_check_state(type, state); reader.set_keys(state->suite, state->keys, CLIENT); state->got_server_ccs = true; } else if(type == FINISHED) { client_check_state(type, state); state->server_finished = new Finished(contents); if(!state->server_finished->verify(state->keys.master_secret(), state->version, state->hash, SERVER)) throw TLS_Exception(DECRYPT_ERROR, "Finished message didn't verify"); delete state; state = 0; active = true; } else throw Unexpected_Message("Unknown handshake message received"); } /** * Perform a client-side TLS handshake */ void TLS_Client::do_handshake() { state = new Handshake_State; state->client_hello = new Client_Hello(rng, writer, policy, state->hash); while(true) { if(active && !state) break; if(!active && !state) throw TLS_Exception(HANDSHAKE_FAILURE, "TLS_Client: Handshake failed (do_handshake)"); state_machine(); } } } /* * TLS Handshake Hash * (C) 2004-2006 Jack Lloyd * * Released under the terms of the Botan license */ #include <memory> namespace Botan { /** * Return a TLS Handshake Hash */ SecureVector<byte> HandshakeHash::final() { MD5 md5; SHA_160 sha1; md5.update(data); sha1.update(data); SecureVector<byte> output; output += md5.final(); output += sha1.final(); return output; } /** * Return a SSLv3 Handshake Hash */ SecureVector<byte> HandshakeHash::final_ssl3(const MemoryRegion<byte>& secret) { const byte PAD_INNER = 0x36, PAD_OUTER = 0x5C; MD5 md5; SHA_160 sha1; md5.update(data); sha1.update(data); md5.update(secret); sha1.update(secret); for(size_t i = 0; i != 48; ++i) md5.update(PAD_INNER); for(size_t i = 0; i != 40; ++i) sha1.update(PAD_INNER); SecureVector<byte> inner_md5 = md5.final(), inner_sha1 = sha1.final(); md5.update(secret); sha1.update(secret); for(size_t i = 0; i != 48; ++i) md5.update(PAD_OUTER); for(size_t i = 0; i != 40; ++i) sha1.update(PAD_OUTER); md5.update(inner_md5); sha1.update(inner_sha1); SecureVector<byte> output; output += md5.final(); output += sha1.final(); return output; } } /* * Policies for TLS * (C) 2004-2010 Jack Lloyd * * Released under the terms of the Botan license */ namespace Botan { /* * Return allowed ciphersuites */ std::vector<u16bit> TLS_Policy::ciphersuites() const { return suite_list(allow_static_rsa(), allow_edh_rsa(), allow_edh_dsa()); } /* * Return allowed ciphersuites */ std::vector<u16bit> TLS_Policy::suite_list(bool use_rsa, bool use_edh_rsa, bool use_edh_dsa) const { std::vector<u16bit> suites; if(use_edh_dsa) { suites.push_back(TLS_DHE_DSS_WITH_AES_256_CBC_SHA); suites.push_back(TLS_DHE_DSS_WITH_AES_128_CBC_SHA); suites.push_back(TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA); suites.push_back(TLS_DHE_DSS_WITH_SEED_CBC_SHA); } if(use_edh_rsa) { suites.push_back(TLS_DHE_RSA_WITH_AES_256_CBC_SHA); suites.push_back(TLS_DHE_RSA_WITH_AES_128_CBC_SHA); suites.push_back(TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA); suites.push_back(TLS_DHE_RSA_WITH_SEED_CBC_SHA); } if(use_rsa) { suites.push_back(TLS_RSA_WITH_AES_256_CBC_SHA); suites.push_back(TLS_RSA_WITH_AES_128_CBC_SHA); suites.push_back(TLS_RSA_WITH_3DES_EDE_CBC_SHA); suites.push_back(TLS_RSA_WITH_SEED_CBC_SHA); suites.push_back(TLS_RSA_WITH_RC4_128_SHA); suites.push_back(TLS_RSA_WITH_RC4_128_MD5); } if(suites.size() == 0) throw TLS_Exception(INTERNAL_ERROR, "TLS_Policy error: All ciphersuites disabled"); suites.push_back(TLS_NO_RENEGOTIATION_SCSV); return suites; } /* * Return allowed compression algorithms */ std::vector<byte> TLS_Policy::compression() const { std::vector<byte> algs; algs.push_back(NO_COMPRESSION); return algs; } /* * Choose which ciphersuite to use */ u16bit TLS_Policy::choose_suite(const std::vector<u16bit>& c_suites, bool have_rsa, bool have_dsa) const { bool use_static_rsa = allow_static_rsa() && have_rsa; bool use_edh_rsa = allow_edh_rsa() && have_rsa; bool use_edh_dsa = allow_edh_dsa() && have_dsa; std::vector<u16bit> s_suites = suite_list(use_static_rsa, use_edh_rsa, use_edh_dsa); for(size_t i = 0; i != s_suites.size(); ++i) for(size_t j = 0; j != c_suites.size(); ++j) if(s_suites[i] == c_suites[j]) return s_suites[i]; return 0; } /* * Choose which compression algorithm to use */ byte TLS_Policy::choose_compression(const std::vector<byte>& c_comp) const { std::vector<byte> s_comp = compression(); for(size_t i = 0; i != s_comp.size(); ++i) for(size_t j = 0; j != c_comp.size(); ++j) if(s_comp[i] == c_comp[j]) return s_comp[i]; return NO_COMPRESSION; } /* * Return the group to use for empheral DH */ DL_Group TLS_Policy::dh_group() const { return DL_Group("modp/ietf/1024"); } } /* * TLS Server * (C) 2004-2010 Jack Lloyd * * Released under the terms of the Botan license */ namespace Botan { namespace { /* * Choose what version to respond with */ Version_Code choose_version(Version_Code client, Version_Code minimum) { if(client < minimum) throw TLS_Exception(PROTOCOL_VERSION, "Client version is unacceptable by policy"); if(client == SSL_V3 || client == TLS_V10 || client == TLS_V11) return client; return TLS_V11; } // FIXME: checks are wrong for session reuse (add a flag for that) /* * Verify the state transition is allowed */ void server_check_state(Handshake_Type new_msg, Handshake_State* state) { class State_Transition_Error : public Unexpected_Message { public: State_Transition_Error(const std::string& err) : Unexpected_Message("State transition error from " + err) {} }; if(new_msg == CLIENT_HELLO || new_msg == CLIENT_HELLO_SSLV2) { if(state->server_hello) throw State_Transition_Error("ClientHello"); } else if(new_msg == CERTIFICATE) { if(!state->do_client_auth || !state->cert_req || !state->server_hello_done || state->client_kex) throw State_Transition_Error("ClientCertificate"); } else if(new_msg == CLIENT_KEX) { if(!state->server_hello_done || state->client_verify || state->got_client_ccs) throw State_Transition_Error("ClientKeyExchange"); } else if(new_msg == CERTIFICATE_VERIFY) { if(!state->cert_req || !state->client_certs || !state->client_kex || state->got_client_ccs) throw State_Transition_Error("CertificateVerify"); } else if(new_msg == HANDSHAKE_CCS) { if(!state->client_kex || state->client_finished) throw State_Transition_Error("ClientChangeCipherSpec"); } else if(new_msg == FINISHED) { if(!state->got_client_ccs) throw State_Transition_Error("ClientFinished"); } else throw Unexpected_Message("Unexpected message in handshake"); } } /* * TLS Server Constructor */ TLS_Server::TLS_Server(std::tr1::function<size_t (byte[], size_t)> input_fn, std::tr1::function<void (const byte[], size_t)> output_fn, const TLS_Policy& policy, RandomNumberGenerator& rng, const X509_Certificate& cert, const Private_Key& cert_key) : input_fn(input_fn), policy(policy), rng(rng), writer(output_fn) { state = 0; cert_chain.push_back(cert); private_key = PKCS8::copy_key(cert_key, rng); try { active = false; writer.set_version(TLS_V10); do_handshake(); active = true; } catch(std::exception& e) { if(state) { delete state; state = 0; } writer.alert(FATAL, HANDSHAKE_FAILURE); throw Stream_IO_Error(std::string("TLS_Server: Handshake failed: ") + e.what()); } } /* * TLS Server Destructor */ TLS_Server::~TLS_Server() { close(); delete private_key; delete state; } /* * Return the peer's certificate chain */ std::vector<X509_Certificate> TLS_Server::peer_cert_chain() const { return peer_certs; } /* * Write to a TLS connection */ void TLS_Server::write(const byte buf[], size_t length) { if(!active) throw Internal_Error("TLS_Server::write called while closed"); writer.send(APPLICATION_DATA, buf, length); } /* * Read from a TLS connection */ size_t TLS_Server::read(byte out[], size_t length) { if(!active) throw Internal_Error("TLS_Server::read called while closed"); writer.flush(); while(read_buf.size() == 0) { state_machine(); if(active == false) break; } size_t got = std::min<size_t>(read_buf.size(), length); read_buf.read(out, got); return got; } /* * Check connection status */ bool TLS_Server::is_closed() const { if(!active) return true; return false; } /* * Close a TLS connection */ void TLS_Server::close() { close(WARNING, CLOSE_NOTIFY); } /* * Close a TLS connection */ void TLS_Server::close(Alert_Level level, Alert_Type alert_code) { if(active) { try { active = false; writer.alert(level, alert_code); writer.flush(); } catch(...) {} } } /* * Iterate the TLS state machine */ void TLS_Server::state_machine() { byte rec_type = CONNECTION_CLOSED; SecureVector<byte> record(1024); size_t bytes_needed = reader.get_record(rec_type, record); while(bytes_needed) { size_t to_get = std::min<size_t>(record.size(), bytes_needed); size_t got = input_fn(&record[0], to_get); if(got == 0) { rec_type = CONNECTION_CLOSED; break; } reader.add_input(&record[0], got); bytes_needed = reader.get_record(rec_type, record); } if(rec_type == CONNECTION_CLOSED) { active = false; reader.reset(); writer.reset(); } else if(rec_type == APPLICATION_DATA) { if(active) read_buf.write(&record[0], record.size()); else throw Unexpected_Message("Application data before handshake done"); } else if(rec_type == HANDSHAKE || rec_type == CHANGE_CIPHER_SPEC) read_handshake(rec_type, record); else if(rec_type == ALERT) { Alert alert(record); if(alert.is_fatal() || alert.type() == CLOSE_NOTIFY) { if(alert.type() == CLOSE_NOTIFY) writer.alert(WARNING, CLOSE_NOTIFY); reader.reset(); writer.reset(); active = false; } } else throw Unexpected_Message("Unknown message type received"); } /* * Split up and process handshake messages */ void TLS_Server::read_handshake(byte rec_type, const MemoryRegion<byte>& rec_buf) { if(rec_type == HANDSHAKE) { if(!state) state = new Handshake_State; state->queue.write(&rec_buf[0], rec_buf.size()); } while(true) { Handshake_Type type = HANDSHAKE_NONE; SecureVector<byte> contents; if(rec_type == HANDSHAKE) { if(state->queue.size() >= 4) { byte head[4] = { 0 }; state->queue.peek(head, 4); const size_t length = make_u32bit(0, head[1], head[2], head[3]); if(state->queue.size() >= length + 4) { type = static_cast<Handshake_Type>(head[0]); contents.resize(length); state->queue.read(head, 4); state->queue.read(&contents[0], contents.size()); } } } else if(rec_type == CHANGE_CIPHER_SPEC) { if(state->queue.size() == 0 && rec_buf.size() == 1 && rec_buf[0] == 1) type = HANDSHAKE_CCS; else throw Decoding_Error("Malformed ChangeCipherSpec message"); } else throw Decoding_Error("Unknown message type in handshake processing"); if(type == HANDSHAKE_NONE) break; process_handshake_msg(type, contents); if(type == HANDSHAKE_CCS || !state) break; } } /* * Process a handshake message */ void TLS_Server::process_handshake_msg(Handshake_Type type, const MemoryRegion<byte>& contents) { rng.add_entropy(&contents[0], contents.size()); if(state == 0) throw Unexpected_Message("Unexpected handshake message"); if(active && (type == CLIENT_HELLO || type == CLIENT_HELLO_SSLV2)) { delete state; state = 0; writer.alert(WARNING, NO_RENEGOTIATION); return; } if(type != HANDSHAKE_CCS && type != FINISHED) { if(type != CLIENT_HELLO_SSLV2) { state->hash.update(static_cast<byte>(type)); const size_t record_length = contents.size(); for(size_t i = 0; i != 3; i++) state->hash.update(get_byte<u32bit>(i+1, record_length)); } state->hash.update(contents); } if(type == CLIENT_HELLO || type == CLIENT_HELLO_SSLV2) { server_check_state(type, state); state->client_hello = new Client_Hello(contents, type); client_requested_hostname = state->client_hello->hostname(); state->version = choose_version(state->client_hello->version(), policy.min_version()); writer.set_version(state->version); reader.set_version(state->version); state->server_hello = new Server_Hello(rng, writer, policy, cert_chain, *(state->client_hello), state->version, state->hash); state->suite = CipherSuite(state->server_hello->ciphersuite()); if(state->suite.sig_type() != TLS_ALGO_SIGNER_ANON) { // FIXME: should choose certs based on sig type state->server_certs = new Certificate(writer, cert_chain, state->hash); } state->kex_priv = PKCS8::copy_key(*private_key, rng); if(state->suite.kex_type() != TLS_ALGO_KEYEXCH_NOKEX) { if(state->suite.kex_type() == TLS_ALGO_KEYEXCH_RSA) { state->kex_priv = new RSA_PrivateKey(rng, policy.rsa_export_keysize()); } else if(state->suite.kex_type() == TLS_ALGO_KEYEXCH_DH) { state->kex_priv = new DH_PrivateKey(rng, policy.dh_group()); } else throw Internal_Error("TLS_Server: Unknown ciphersuite kex type"); state->server_kex = new Server_Key_Exchange(rng, writer, state->kex_priv, private_key, state->client_hello->random(), state->server_hello->random(), state->hash); } if(policy.require_client_auth()) { state->do_client_auth = true; throw Internal_Error("Client auth not implemented"); // FIXME: send client auth request here } state->server_hello_done = new Server_Hello_Done(writer, state->hash); } else if(type == CERTIFICATE) { server_check_state(type, state); // FIXME: process this } else if(type == CLIENT_KEX) { server_check_state(type, state); state->client_kex = new Client_Key_Exchange(contents, state->suite, state->version); SecureVector<byte> pre_master = state->client_kex->pre_master_secret(rng, state->kex_priv, state->server_hello->version()); state->keys = SessionKeys(state->suite, state->version, pre_master, state->client_hello->random(), state->server_hello->random()); } else if(type == CERTIFICATE_VERIFY) { server_check_state(type, state); // FIXME: process this } else if(type == HANDSHAKE_CCS) { server_check_state(type, state); reader.set_keys(state->suite, state->keys, SERVER); state->got_client_ccs = true; } else if(type == FINISHED) { server_check_state(type, state); state->client_finished = new Finished(contents); if(!state->client_finished->verify(state->keys.master_secret(), state->version, state->hash, CLIENT)) throw TLS_Exception(DECRYPT_ERROR, "Finished message didn't verify"); state->hash.update(static_cast<byte>(type)); const size_t record_length = contents.size(); for(size_t i = 0; i != 3; i++) state->hash.update(get_byte<u32bit>(i+1, record_length)); state->hash.update(contents); writer.send(CHANGE_CIPHER_SPEC, 1); writer.flush(); writer.set_keys(state->suite, state->keys, SERVER); state->server_finished = new Finished(writer, state->version, SERVER, state->keys.master_secret(), state->hash); delete state; state = 0; active = true; } else throw Unexpected_Message("Unknown handshake message received"); } /* * Perform a server-side TLS handshake */ void TLS_Server::do_handshake() { while(true) { if(active && !state) break; state_machine(); if(!active && !state) throw TLS_Exception(HANDSHAKE_FAILURE, "TLS_Server: Handshake failed"); } } } /* * TLS Session Key * (C) 2004-2006 Jack Lloyd * * Released under the terms of the Botan license */ namespace Botan { /** * Return the client cipher key */ SymmetricKey SessionKeys::client_cipher_key() const { return c_cipher; } /** * Return the server cipher key */ SymmetricKey SessionKeys::server_cipher_key() const { return s_cipher; } /** * Return the client MAC key */ SymmetricKey SessionKeys::client_mac_key() const { return c_mac; } /** * Return the server MAC key */ SymmetricKey SessionKeys::server_mac_key() const { return s_mac; } /** * Return the client cipher IV */ InitializationVector SessionKeys::client_iv() const { return c_iv; } /** * Return the server cipher IV */ InitializationVector SessionKeys::server_iv() const { return s_iv; } /** * Return the TLS master secret */ SecureVector<byte> SessionKeys::master_secret() const { return master_sec; } /** * Generate SSLv3 session keys */ SymmetricKey SessionKeys::ssl3_keygen(size_t prf_gen, const MemoryRegion<byte>& pre_master, const MemoryRegion<byte>& client_random, const MemoryRegion<byte>& server_random) { SSL3_PRF prf; SecureVector<byte> salt; salt += client_random; salt += server_random; master_sec = prf.derive_key(48, pre_master, salt); salt.clear(); salt += server_random; salt += client_random; return prf.derive_key(prf_gen, master_sec, salt); } /** * Generate TLS 1.0 session keys */ SymmetricKey SessionKeys::tls1_keygen(size_t prf_gen, const MemoryRegion<byte>& pre_master, const MemoryRegion<byte>& client_random, const MemoryRegion<byte>& server_random) { const byte MASTER_SECRET_MAGIC[] = { 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x20, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74 }; const byte KEY_GEN_MAGIC[] = { 0x6B, 0x65, 0x79, 0x20, 0x65, 0x78, 0x70, 0x61, 0x6E, 0x73, 0x69, 0x6F, 0x6E }; TLS_PRF prf; SecureVector<byte> salt; salt += std::make_pair(MASTER_SECRET_MAGIC, sizeof(MASTER_SECRET_MAGIC)); salt += client_random; salt += server_random; master_sec = prf.derive_key(48, pre_master, salt); salt.clear(); salt += std::make_pair(KEY_GEN_MAGIC, sizeof(KEY_GEN_MAGIC)); salt += server_random; salt += client_random; return prf.derive_key(prf_gen, master_sec, salt); } /** * SessionKeys Constructor */ SessionKeys::SessionKeys(const CipherSuite& suite, Version_Code version, const MemoryRegion<byte>& pre_master_secret, const MemoryRegion<byte>& c_random, const MemoryRegion<byte>& s_random) { if(version != SSL_V3 && version != TLS_V10 && version != TLS_V11) throw Invalid_Argument("SessionKeys: Unknown version code"); const size_t mac_keylen = output_length_of(suite.mac_algo()); const size_t cipher_keylen = suite.cipher_keylen(); size_t cipher_ivlen = 0; if(have_block_cipher(suite.cipher_algo())) cipher_ivlen = block_size_of(suite.cipher_algo()); const size_t prf_gen = 2 * (mac_keylen + cipher_keylen + cipher_ivlen); SymmetricKey keyblock = (version == SSL_V3) ? ssl3_keygen(prf_gen, pre_master_secret, c_random, s_random) : tls1_keygen(prf_gen, pre_master_secret, c_random, s_random); const byte* key_data = keyblock.begin(); c_mac = SymmetricKey(key_data, mac_keylen); key_data += mac_keylen; s_mac = SymmetricKey(key_data, mac_keylen); key_data += mac_keylen; c_cipher = SymmetricKey(key_data, cipher_keylen); key_data += cipher_keylen; s_cipher = SymmetricKey(key_data, cipher_keylen); key_data += cipher_keylen; c_iv = InitializationVector(key_data, cipher_ivlen); key_data += cipher_ivlen; s_iv = InitializationVector(key_data, cipher_ivlen); } } /* * TLS Handshaking * (C) 2004-2006 Jack Lloyd * * Released under the terms of the Botan license */ namespace Botan { /** * Initialize the SSL/TLS Handshake State */ Handshake_State::Handshake_State() { client_hello = 0; server_hello = 0; server_certs = 0; server_kex = 0; cert_req = 0; server_hello_done = 0; client_certs = 0; client_kex = 0; client_verify = 0; client_finished = 0; server_finished = 0; kex_pub = 0; kex_priv = 0; do_client_auth = got_client_ccs = got_server_ccs = false; version = SSL_V3; } /** * Destroy the SSL/TLS Handshake State */ Handshake_State::~Handshake_State() { delete client_hello; delete server_hello; delete server_certs; delete server_kex; delete cert_req; delete server_hello_done; delete client_certs; delete client_kex; delete client_verify; delete client_finished; delete server_finished; delete kex_pub; delete kex_priv; } } /* * TLS Cipher Suites * (C) 2004-2010 Jack Lloyd * * Released under the terms of the Botan license */ namespace Botan { /** * Convert an SSL/TLS ciphersuite to algorithm fields */ TLS_Ciphersuite_Algos CipherSuite::lookup_ciphersuite(u16bit suite) { if(suite == TLS_RSA_WITH_RC4_128_MD5) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_RSA | TLS_ALGO_KEYEXCH_NOKEX | TLS_ALGO_MAC_MD5 | TLS_ALGO_CIPHER_RC4_128); if(suite == TLS_RSA_WITH_RC4_128_SHA) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_RSA | TLS_ALGO_KEYEXCH_NOKEX | TLS_ALGO_MAC_SHA1 | TLS_ALGO_CIPHER_RC4_128); if(suite == TLS_RSA_WITH_3DES_EDE_CBC_SHA) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_RSA | TLS_ALGO_KEYEXCH_NOKEX | TLS_ALGO_MAC_SHA1 | TLS_ALGO_CIPHER_3DES_CBC); if(suite == TLS_RSA_WITH_AES_128_CBC_SHA) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_RSA | TLS_ALGO_KEYEXCH_NOKEX | TLS_ALGO_MAC_SHA1 | TLS_ALGO_CIPHER_AES128_CBC); if(suite == TLS_RSA_WITH_AES_256_CBC_SHA) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_RSA | TLS_ALGO_KEYEXCH_NOKEX | TLS_ALGO_MAC_SHA1 | TLS_ALGO_CIPHER_AES256_CBC); if(suite == TLS_RSA_WITH_SEED_CBC_SHA) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_RSA | TLS_ALGO_KEYEXCH_NOKEX | TLS_ALGO_MAC_SHA1 | TLS_ALGO_CIPHER_SEED_CBC); if(suite == TLS_RSA_WITH_AES_128_CBC_SHA256) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_RSA | TLS_ALGO_KEYEXCH_NOKEX | TLS_ALGO_MAC_SHA256 | TLS_ALGO_CIPHER_AES128_CBC); if(suite == TLS_RSA_WITH_AES_256_CBC_SHA256) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_RSA | TLS_ALGO_KEYEXCH_NOKEX | TLS_ALGO_MAC_SHA256 | TLS_ALGO_CIPHER_AES256_CBC); if(suite == TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_DSA | TLS_ALGO_KEYEXCH_DH | TLS_ALGO_MAC_SHA1 | TLS_ALGO_CIPHER_3DES_CBC); if(suite == TLS_DHE_DSS_WITH_AES_128_CBC_SHA) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_DSA | TLS_ALGO_KEYEXCH_DH | TLS_ALGO_MAC_SHA1 | TLS_ALGO_CIPHER_AES128_CBC); if(suite == TLS_DHE_DSS_WITH_SEED_CBC_SHA) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_DSA | TLS_ALGO_KEYEXCH_DH | TLS_ALGO_MAC_SHA1 | TLS_ALGO_CIPHER_SEED_CBC); if(suite == TLS_DHE_DSS_WITH_AES_256_CBC_SHA) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_DSA | TLS_ALGO_KEYEXCH_DH | TLS_ALGO_MAC_SHA1 | TLS_ALGO_CIPHER_AES256_CBC); if(suite == TLS_DHE_DSS_WITH_AES_128_CBC_SHA256) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_DSA | TLS_ALGO_KEYEXCH_DH | TLS_ALGO_MAC_SHA256 | TLS_ALGO_CIPHER_AES128_CBC); if(suite == TLS_DHE_DSS_WITH_AES_256_CBC_SHA256) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_DSA | TLS_ALGO_KEYEXCH_DH | TLS_ALGO_MAC_SHA256 | TLS_ALGO_CIPHER_AES256_CBC); if(suite == TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_RSA | TLS_ALGO_KEYEXCH_DH | TLS_ALGO_MAC_SHA1 | TLS_ALGO_CIPHER_3DES_CBC); if(suite == TLS_DHE_RSA_WITH_AES_128_CBC_SHA) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_RSA | TLS_ALGO_KEYEXCH_DH | TLS_ALGO_MAC_SHA1 | TLS_ALGO_CIPHER_AES128_CBC); if(suite == TLS_DHE_DSS_WITH_SEED_CBC_SHA) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_RSA | TLS_ALGO_KEYEXCH_DH | TLS_ALGO_MAC_SHA1 | TLS_ALGO_CIPHER_SEED_CBC); if(suite == TLS_DHE_RSA_WITH_AES_256_CBC_SHA) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_RSA | TLS_ALGO_KEYEXCH_DH | TLS_ALGO_MAC_SHA1 | TLS_ALGO_CIPHER_AES256_CBC); if(suite == TLS_DHE_RSA_WITH_AES_128_CBC_SHA256) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_RSA | TLS_ALGO_KEYEXCH_DH | TLS_ALGO_MAC_SHA256 | TLS_ALGO_CIPHER_AES128_CBC); if(suite == TLS_DHE_RSA_WITH_AES_256_CBC_SHA256) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_RSA | TLS_ALGO_KEYEXCH_DH | TLS_ALGO_MAC_SHA256 | TLS_ALGO_CIPHER_AES256_CBC); if(suite == TLS_ECDHE_ECDSA_WITH_RC4_128_SHA) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_ECDSA | TLS_ALGO_KEYEXCH_ECDH | TLS_ALGO_MAC_SHA1 | TLS_ALGO_CIPHER_RC4_128); if(suite == TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_ECDSA | TLS_ALGO_KEYEXCH_ECDH | TLS_ALGO_MAC_SHA1 | TLS_ALGO_CIPHER_3DES_CBC); if(suite == TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_ECDSA | TLS_ALGO_KEYEXCH_ECDH | TLS_ALGO_MAC_SHA1 | TLS_ALGO_CIPHER_AES128_CBC); if(suite == TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_ECDSA | TLS_ALGO_KEYEXCH_ECDH | TLS_ALGO_MAC_SHA1 | TLS_ALGO_CIPHER_AES256_CBC); if(suite == TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_ECDSA | TLS_ALGO_KEYEXCH_ECDH | TLS_ALGO_MAC_SHA256 | TLS_ALGO_CIPHER_AES128_CBC); if(suite == TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_ECDSA | TLS_ALGO_KEYEXCH_ECDH | TLS_ALGO_MAC_SHA384 | TLS_ALGO_CIPHER_AES256_CBC); if(suite == TLS_ECDHE_RSA_WITH_RC4_128_SHA) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_RSA | TLS_ALGO_KEYEXCH_ECDH | TLS_ALGO_MAC_SHA1 | TLS_ALGO_CIPHER_RC4_128); if(suite == TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_RSA | TLS_ALGO_KEYEXCH_ECDH | TLS_ALGO_MAC_SHA1 | TLS_ALGO_CIPHER_3DES_CBC); if(suite == TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_RSA | TLS_ALGO_KEYEXCH_ECDH | TLS_ALGO_MAC_SHA1 | TLS_ALGO_CIPHER_AES128_CBC); if(suite == TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_RSA | TLS_ALGO_KEYEXCH_ECDH | TLS_ALGO_MAC_SHA1 | TLS_ALGO_CIPHER_AES256_CBC); if(suite == TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_ECDSA | TLS_ALGO_KEYEXCH_ECDH | TLS_ALGO_MAC_SHA256 | TLS_ALGO_CIPHER_AES128_CBC); if(suite == TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_ECDSA | TLS_ALGO_KEYEXCH_ECDH | TLS_ALGO_MAC_SHA384 | TLS_ALGO_CIPHER_AES256_CBC); return TLS_Ciphersuite_Algos(0); } namespace { std::pair<std::string, size_t> cipher_code_to_name(TLS_Ciphersuite_Algos algo) { if((algo & TLS_ALGO_CIPHER_MASK) == TLS_ALGO_CIPHER_RC4_128) return std::make_pair("ARC4", 16); if((algo & TLS_ALGO_CIPHER_MASK) == TLS_ALGO_CIPHER_3DES_CBC) return std::make_pair("3DES", 24); if((algo & TLS_ALGO_CIPHER_MASK) == TLS_ALGO_CIPHER_AES128_CBC) return std::make_pair("AES-128", 16); if((algo & TLS_ALGO_CIPHER_MASK) == TLS_ALGO_CIPHER_AES256_CBC) return std::make_pair("AES-256", 32); if((algo & TLS_ALGO_CIPHER_MASK) == TLS_ALGO_CIPHER_SEED_CBC) return std::make_pair("SEED", 16); throw TLS_Exception(INTERNAL_ERROR, "CipherSuite: Unknown cipher type " + to_string(algo)); } std::string mac_code_to_name(TLS_Ciphersuite_Algos algo) { if((algo & TLS_ALGO_MAC_MASK) == TLS_ALGO_MAC_MD5) return "MD5"; if((algo & TLS_ALGO_MAC_MASK) == TLS_ALGO_MAC_SHA1) return "SHA-1"; if((algo & TLS_ALGO_MAC_MASK) == TLS_ALGO_MAC_SHA256) return "SHA-256"; if((algo & TLS_ALGO_MAC_MASK) == TLS_ALGO_MAC_SHA384) return "SHA-384"; throw TLS_Exception(INTERNAL_ERROR, "CipherSuite: Unknown MAC type " + to_string(algo)); } } /** * CipherSuite Constructor */ CipherSuite::CipherSuite(u16bit suite_code) { if(suite_code == 0) return; TLS_Ciphersuite_Algos algos = lookup_ciphersuite(suite_code); if(algos == 0) throw Invalid_Argument("Unknown ciphersuite: " + to_string(suite_code)); sig_algo = TLS_Ciphersuite_Algos(algos & TLS_ALGO_SIGNER_MASK); kex_algo = TLS_Ciphersuite_Algos(algos & TLS_ALGO_KEYEXCH_MASK); std::pair<std::string, size_t> cipher_info = cipher_code_to_name(algos); cipher = cipher_info.first; cipher_key_length = cipher_info.second; mac = mac_code_to_name(algos); } } /* * ARC4 * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Combine cipher stream with message */ void ARC4::cipher(const byte in[], byte out[], size_t length) { while(length >= buffer.size() - position) { xor_buf(out, in, &buffer[position], buffer.size() - position); length -= (buffer.size() - position); in += (buffer.size() - position); out += (buffer.size() - position); generate(); } xor_buf(out, in, &buffer[position], length); position += length; } /* * Generate cipher stream */ void ARC4::generate() { byte SX, SY; for(size_t i = 0; i != buffer.size(); i += 4) { SX = state[X+1]; Y = (Y + SX) % 256; SY = state[Y]; state[X+1] = SY; state[Y] = SX; buffer[i] = state[(SX + SY) % 256]; SX = state[X+2]; Y = (Y + SX) % 256; SY = state[Y]; state[X+2] = SY; state[Y] = SX; buffer[i+1] = state[(SX + SY) % 256]; SX = state[X+3]; Y = (Y + SX) % 256; SY = state[Y]; state[X+3] = SY; state[Y] = SX; buffer[i+2] = state[(SX + SY) % 256]; X = (X + 4) % 256; SX = state[X]; Y = (Y + SX) % 256; SY = state[Y]; state[X] = SY; state[Y] = SX; buffer[i+3] = state[(SX + SY) % 256]; } position = 0; } /* * ARC4 Key Schedule */ void ARC4::key_schedule(const byte key[], size_t length) { clear(); for(size_t i = 0; i != 256; ++i) state[i] = static_cast<byte>(i); for(size_t i = 0, state_index = 0; i != 256; ++i) { state_index = (state_index + key[i % length] + state[i]) % 256; std::swap(state[i], state[state_index]); } for(size_t i = 0; i <= SKIP; i += buffer.size()) generate(); position += (SKIP % buffer.size()); } /* * Return the name of this type */ std::string ARC4::name() const { if(SKIP == 0) return "ARC4"; if(SKIP == 256) return "MARK-4"; else return "RC4_skip(" + to_string(SKIP) + ")"; } /* * Clear memory of sensitive data */ void ARC4::clear() { zeroise(state); zeroise(buffer); position = X = Y = 0; } /* * ARC4 Constructor */ ARC4::ARC4(size_t s) : SKIP(s), state(256), buffer(DEFAULT_BUFFERSIZE) { clear(); } } /* * Counter mode * (C) 1999-2011 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * CTR-BE Constructor */ CTR_BE::CTR_BE(BlockCipher* ciph) : permutation(ciph), counter(256 * permutation->block_size()), buffer(counter.size()), position(0) { } /* * CTR_BE Destructor */ CTR_BE::~CTR_BE() { delete permutation; } /* * Zeroize */ void CTR_BE::clear() { permutation->clear(); zeroise(buffer); zeroise(counter); position = 0; } /* * Set the key */ void CTR_BE::key_schedule(const byte key[], size_t key_len) { permutation->set_key(key, key_len); // Set a default all-zeros IV set_iv(0, 0); } /* * Return the name of this type */ std::string CTR_BE::name() const { return ("CTR-BE(" + permutation->name() + ")"); } /* * CTR-BE Encryption/Decryption */ void CTR_BE::cipher(const byte in[], byte out[], size_t length) { while(length >= buffer.size() - position) { xor_buf(out, in, &buffer[position], buffer.size() - position); length -= (buffer.size() - position); in += (buffer.size() - position); out += (buffer.size() - position); increment_counter(); } xor_buf(out, in, &buffer[position], length); position += length; } /* * Set CTR-BE IV */ void CTR_BE::set_iv(const byte iv[], size_t iv_len) { if(!valid_iv_length(iv_len)) throw Invalid_IV_Length(name(), iv_len); const size_t bs = permutation->block_size(); zeroise(counter); counter.copy(0, iv, iv_len); /* * Set counter blocks to IV, IV + 1, ... IV + 255 */ for(size_t i = 1; i != 256; ++i) { counter.copy(i*bs, &counter[(i-1)*bs], bs); for(size_t j = 0; j != bs; ++j) if(++counter[i*bs + (bs - 1 - j)]) break; } permutation->encrypt_n(&counter[0], &buffer[0], 256); position = 0; } /* * Increment the counter and update the buffer */ void CTR_BE::increment_counter() { const size_t bs = permutation->block_size(); /* * Each counter value always needs to be incremented by 256, * so we don't touch the lowest byte and instead treat it as * an increment of one starting with the next byte. */ for(size_t i = 0; i != 256; ++i) { for(size_t j = 1; j != bs; ++j) if(++counter[i*bs + (bs - 1 - j)]) break; } permutation->encrypt_n(&counter[0], &buffer[0], 256); position = 0; } } /* * OFB Mode * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <algorithm> namespace Botan { /* * OFB Constructor */ OFB::OFB(BlockCipher* ciph) : permutation(ciph) { position = 0; buffer.resize(permutation->block_size()); } /* * OFB Destructor */ OFB::~OFB() { delete permutation; } /* * Zeroize */ void OFB::clear() { permutation->clear(); zeroise(buffer); position = 0; } /* * Set the key */ void OFB::key_schedule(const byte key[], size_t key_len) { permutation->set_key(key, key_len); // Set a default all-zeros IV set_iv(0, 0); } /* * Return the name of this type */ std::string OFB::name() const { return ("OFB(" + permutation->name() + ")"); } /* * CTR-BE Encryption/Decryption */ void OFB::cipher(const byte in[], byte out[], size_t length) { while(length >= buffer.size() - position) { xor_buf(out, in, &buffer[position], buffer.size() - position); length -= (buffer.size() - position); in += (buffer.size() - position); out += (buffer.size() - position); permutation->encrypt(buffer); position = 0; } xor_buf(out, in, &buffer[position], length); position += length; } /* * Set CTR-BE IV */ void OFB::set_iv(const byte iv[], size_t iv_len) { if(!valid_iv_length(iv_len)) throw Invalid_IV_Length(name(), iv_len); zeroise(buffer); buffer.copy(0, iv, iv_len); permutation->encrypt(buffer); position = 0; } } /* * Salsa20 / XSalsa20 * (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { #define SALSA20_QUARTER_ROUND(x1, x2, x3, x4) \ do { \ x2 ^= rotate_left(x1 + x4, 7); \ x3 ^= rotate_left(x2 + x1, 9); \ x4 ^= rotate_left(x3 + x2, 13); \ x1 ^= rotate_left(x4 + x3, 18); \ } while(0) /* * Generate HSalsa20 cipher stream (for XSalsa20 IV setup) */ void hsalsa20(u32bit output[8], const u32bit input[16]) { u32bit x00 = input[ 0], x01 = input[ 1], x02 = input[ 2], x03 = input[ 3], x04 = input[ 4], x05 = input[ 5], x06 = input[ 6], x07 = input[ 7], x08 = input[ 8], x09 = input[ 9], x10 = input[10], x11 = input[11], x12 = input[12], x13 = input[13], x14 = input[14], x15 = input[15]; for(size_t i = 0; i != 10; ++i) { SALSA20_QUARTER_ROUND(x00, x04, x08, x12); SALSA20_QUARTER_ROUND(x05, x09, x13, x01); SALSA20_QUARTER_ROUND(x10, x14, x02, x06); SALSA20_QUARTER_ROUND(x15, x03, x07, x11); SALSA20_QUARTER_ROUND(x00, x01, x02, x03); SALSA20_QUARTER_ROUND(x05, x06, x07, x04); SALSA20_QUARTER_ROUND(x10, x11, x08, x09); SALSA20_QUARTER_ROUND(x15, x12, x13, x14); } output[0] = x00; output[1] = x05; output[2] = x10; output[3] = x15; output[4] = x06; output[5] = x07; output[6] = x08; output[7] = x09; } /* * Generate Salsa20 cipher stream */ void salsa20(byte output[64], const u32bit input[16]) { u32bit x00 = input[ 0], x01 = input[ 1], x02 = input[ 2], x03 = input[ 3], x04 = input[ 4], x05 = input[ 5], x06 = input[ 6], x07 = input[ 7], x08 = input[ 8], x09 = input[ 9], x10 = input[10], x11 = input[11], x12 = input[12], x13 = input[13], x14 = input[14], x15 = input[15]; for(size_t i = 0; i != 10; ++i) { SALSA20_QUARTER_ROUND(x00, x04, x08, x12); SALSA20_QUARTER_ROUND(x05, x09, x13, x01); SALSA20_QUARTER_ROUND(x10, x14, x02, x06); SALSA20_QUARTER_ROUND(x15, x03, x07, x11); SALSA20_QUARTER_ROUND(x00, x01, x02, x03); SALSA20_QUARTER_ROUND(x05, x06, x07, x04); SALSA20_QUARTER_ROUND(x10, x11, x08, x09); SALSA20_QUARTER_ROUND(x15, x12, x13, x14); } store_le(x00 + input[ 0], output + 4 * 0); store_le(x01 + input[ 1], output + 4 * 1); store_le(x02 + input[ 2], output + 4 * 2); store_le(x03 + input[ 3], output + 4 * 3); store_le(x04 + input[ 4], output + 4 * 4); store_le(x05 + input[ 5], output + 4 * 5); store_le(x06 + input[ 6], output + 4 * 6); store_le(x07 + input[ 7], output + 4 * 7); store_le(x08 + input[ 8], output + 4 * 8); store_le(x09 + input[ 9], output + 4 * 9); store_le(x10 + input[10], output + 4 * 10); store_le(x11 + input[11], output + 4 * 11); store_le(x12 + input[12], output + 4 * 12); store_le(x13 + input[13], output + 4 * 13); store_le(x14 + input[14], output + 4 * 14); store_le(x15 + input[15], output + 4 * 15); } } /* * Combine cipher stream with message */ void Salsa20::cipher(const byte in[], byte out[], size_t length) { while(length >= buffer.size() - position) { xor_buf(out, in, &buffer[position], buffer.size() - position); length -= (buffer.size() - position); in += (buffer.size() - position); out += (buffer.size() - position); salsa20(&buffer[0], &state[0]); ++state[8]; if(!state[8]) // if overflow in state[8] ++state[9]; // carry to state[9] position = 0; } xor_buf(out, in, &buffer[position], length); position += length; } /* * Salsa20 Key Schedule */ void Salsa20::key_schedule(const byte key[], size_t length) { static const u32bit TAU[] = { 0x61707865, 0x3120646e, 0x79622d36, 0x6b206574 }; static const u32bit SIGMA[] = { 0x61707865, 0x3320646e, 0x79622d32, 0x6b206574 }; clear(); if(length == 16) { state[0] = TAU[0]; state[1] = load_le<u32bit>(key, 0); state[2] = load_le<u32bit>(key, 1); state[3] = load_le<u32bit>(key, 2); state[4] = load_le<u32bit>(key, 3); state[5] = TAU[1]; state[10] = TAU[2]; state[11] = load_le<u32bit>(key, 0); state[12] = load_le<u32bit>(key, 1); state[13] = load_le<u32bit>(key, 2); state[14] = load_le<u32bit>(key, 3); state[15] = TAU[3]; } else if(length == 32) { state[0] = SIGMA[0]; state[1] = load_le<u32bit>(key, 0); state[2] = load_le<u32bit>(key, 1); state[3] = load_le<u32bit>(key, 2); state[4] = load_le<u32bit>(key, 3); state[5] = SIGMA[1]; state[10] = SIGMA[2]; state[11] = load_le<u32bit>(key, 4); state[12] = load_le<u32bit>(key, 5); state[13] = load_le<u32bit>(key, 6); state[14] = load_le<u32bit>(key, 7); state[15] = SIGMA[3]; } const byte ZERO[8] = { 0 }; set_iv(ZERO, sizeof(ZERO)); } /* * Return the name of this type */ void Salsa20::set_iv(const byte iv[], size_t length) { if(!valid_iv_length(length)) throw Invalid_IV_Length(name(), length); if(length == 8) { // Salsa20 state[6] = load_le<u32bit>(iv, 0); state[7] = load_le<u32bit>(iv, 1); } else { // XSalsa20 state[6] = load_le<u32bit>(iv, 0); state[7] = load_le<u32bit>(iv, 1); state[8] = load_le<u32bit>(iv, 2); state[9] = load_le<u32bit>(iv, 3); SecureVector<u32bit> hsalsa(8); hsalsa20(&hsalsa[0], &state[0]); state[ 1] = hsalsa[0]; state[ 2] = hsalsa[1]; state[ 3] = hsalsa[2]; state[ 4] = hsalsa[3]; state[ 6] = load_le<u32bit>(iv, 4); state[ 7] = load_le<u32bit>(iv, 5); state[11] = hsalsa[4]; state[12] = hsalsa[5]; state[13] = hsalsa[6]; state[14] = hsalsa[7]; } state[8] = 0; state[9] = 0; salsa20(&buffer[0], &state[0]); ++state[8]; if(!state[8]) // if overflow in state[8] ++state[9]; // carry to state[9] position = 0; } /* * Return the name of this type */ std::string Salsa20::name() const { return "Salsa20"; } /* * Clear memory of sensitive data */ void Salsa20::clear() { zeroise(state); zeroise(buffer); position = 0; } } /* * Stream Cipher * (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { void StreamCipher::set_iv(const byte[], size_t iv_len) { if(iv_len) throw Invalid_Argument("The stream cipher " + name() + " does not support resyncronization"); } bool StreamCipher::valid_iv_length(size_t iv_len) const { return (iv_len == 0); } } /* * Tables for Turing * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { const byte Turing::SBOX[256] = { 0x61, 0x51, 0xEB, 0x19, 0xB9, 0x5D, 0x60, 0x38, 0x7C, 0xB2, 0x06, 0x12, 0xC4, 0x5B, 0x16, 0x3B, 0x2B, 0x18, 0x83, 0xB0, 0x7F, 0x75, 0xFA, 0xA0, 0xE9, 0xDD, 0x6D, 0x7A, 0x6B, 0x68, 0x2D, 0x49, 0xB5, 0x1C, 0x90, 0xF7, 0xED, 0x9F, 0xE8, 0xCE, 0xAE, 0x77, 0xC2, 0x13, 0xFD, 0xCD, 0x3E, 0xCF, 0x37, 0x6A, 0xD4, 0xDB, 0x8E, 0x65, 0x1F, 0x1A, 0x87, 0xCB, 0x40, 0x15, 0x88, 0x0D, 0x35, 0xB3, 0x11, 0x0F, 0xD0, 0x30, 0x48, 0xF9, 0xA8, 0xAC, 0x85, 0x27, 0x0E, 0x8A, 0xE0, 0x50, 0x64, 0xA7, 0xCC, 0xE4, 0xF1, 0x98, 0xFF, 0xA1, 0x04, 0xDA, 0xD5, 0xBC, 0x1B, 0xBB, 0xD1, 0xFE, 0x31, 0xCA, 0xBA, 0xD9, 0x2E, 0xF3, 0x1D, 0x47, 0x4A, 0x3D, 0x71, 0x4C, 0xAB, 0x7D, 0x8D, 0xC7, 0x59, 0xB8, 0xC1, 0x96, 0x1E, 0xFC, 0x44, 0xC8, 0x7B, 0xDC, 0x5C, 0x78, 0x2A, 0x9D, 0xA5, 0xF0, 0x73, 0x22, 0x89, 0x05, 0xF4, 0x07, 0x21, 0x52, 0xA6, 0x28, 0x9A, 0x92, 0x69, 0x8F, 0xC5, 0xC3, 0xF5, 0xE1, 0xDE, 0xEC, 0x09, 0xF2, 0xD3, 0xAF, 0x34, 0x23, 0xAA, 0xDF, 0x7E, 0x82, 0x29, 0xC0, 0x24, 0x14, 0x03, 0x32, 0x4E, 0x39, 0x6F, 0xC6, 0xB1, 0x9B, 0xEA, 0x72, 0x79, 0x41, 0xD8, 0x26, 0x6C, 0x5E, 0x2C, 0xB4, 0xA2, 0x53, 0x57, 0xE2, 0x9C, 0x86, 0x54, 0x95, 0xB6, 0x80, 0x8C, 0x36, 0x67, 0xBD, 0x08, 0x93, 0x2F, 0x99, 0x5A, 0xF8, 0x3A, 0xD7, 0x56, 0x84, 0xD2, 0x01, 0xF6, 0x66, 0x4D, 0x55, 0x8B, 0x0C, 0x0B, 0x46, 0xB7, 0x3C, 0x45, 0x91, 0xA4, 0xE3, 0x70, 0xD6, 0xFB, 0xE6, 0x10, 0xA9, 0xC9, 0x00, 0x9E, 0xE7, 0x4F, 0x76, 0x25, 0x3F, 0x5F, 0xA3, 0x33, 0x20, 0x02, 0xEF, 0x62, 0x74, 0xEE, 0x17, 0x81, 0x42, 0x58, 0x0A, 0x4B, 0x63, 0xE5, 0xBE, 0x6E, 0xAD, 0xBF, 0x43, 0x94, 0x97 }; const u32bit Turing::Q_BOX[256] = { 0x1FAA1887, 0x4E5E435C, 0x9165C042, 0x250E6EF4, 0x5957EE20, 0xD484FED3, 0xA666C502, 0x7E54E8AE, 0xD12EE9D9, 0xFC1F38D4, 0x49829B5D, 0x1B5CDF3C, 0x74864249, 0xDA2E3963, 0x28F4429F, 0xC8432C35, 0x4AF40325, 0x9FC0DD70, 0xD8973DED, 0x1A02DC5E, 0xCD175B42, 0xF10012BF, 0x6694D78C, 0xACAAB26B, 0x4EC11B9A, 0x3F168146, 0xC0EA8EC5, 0xB38AC28F, 0x1FED5C0F, 0xAAB4101C, 0xEA2DB082, 0x470929E1, 0xE71843DE, 0x508299FC, 0xE72FBC4B, 0x2E3915DD, 0x9FA803FA, 0x9546B2DE, 0x3C233342, 0x0FCEE7C3, 0x24D607EF, 0x8F97EBAB, 0xF37F859B, 0xCD1F2E2F, 0xC25B71DA, 0x75E2269A, 0x1E39C3D1, 0xEDA56B36, 0xF8C9DEF2, 0x46C9FC5F, 0x1827B3A3, 0x70A56DDF, 0x0D25B510, 0x000F85A7, 0xB2E82E71, 0x68CB8816, 0x8F951E2A, 0x72F5F6AF, 0xE4CBC2B3, 0xD34FF55D, 0x2E6B6214, 0x220B83E3, 0xD39EA6F5, 0x6FE041AF, 0x6B2F1F17, 0xAD3B99EE, 0x16A65EC0, 0x757016C6, 0xBA7709A4, 0xB0326E01, 0xF4B280D9, 0x4BFB1418, 0xD6AFF227, 0xFD548203, 0xF56B9D96, 0x6717A8C0, 0x00D5BF6E, 0x10EE7888, 0xEDFCFE64, 0x1BA193CD, 0x4B0D0184, 0x89AE4930, 0x1C014F36, 0x82A87088, 0x5EAD6C2A, 0xEF22C678, 0x31204DE7, 0xC9C2E759, 0xD200248E, 0x303B446B, 0xB00D9FC2, 0x9914A895, 0x906CC3A1, 0x54FEF170, 0x34C19155, 0xE27B8A66, 0x131B5E69, 0xC3A8623E, 0x27BDFA35, 0x97F068CC, 0xCA3A6ACD, 0x4B55E936, 0x86602DB9, 0x51DF13C1, 0x390BB16D, 0x5A80B83C, 0x22B23763, 0x39D8A911, 0x2CB6BC13, 0xBF5579D7, 0x6C5C2FA8, 0xA8F4196E, 0xBCDB5476, 0x6864A866, 0x416E16AD, 0x897FC515, 0x956FEB3C, 0xF6C8A306, 0x216799D9, 0x171A9133, 0x6C2466DD, 0x75EB5DCD, 0xDF118F50, 0xE4AFB226, 0x26B9CEF3, 0xADB36189, 0x8A7A19B1, 0xE2C73084, 0xF77DED5C, 0x8B8BC58F, 0x06DDE421, 0xB41E47FB, 0xB1CC715E, 0x68C0FF99, 0x5D122F0F, 0xA4D25184, 0x097A5E6C, 0x0CBF18BC, 0xC2D7C6E0, 0x8BB7E420, 0xA11F523F, 0x35D9B8A2, 0x03DA1A6B, 0x06888C02, 0x7DD1E354, 0x6BBA7D79, 0x32CC7753, 0xE52D9655, 0xA9829DA1, 0x301590A7, 0x9BC1C149, 0x13537F1C, 0xD3779B69, 0x2D71F2B7, 0x183C58FA, 0xACDC4418, 0x8D8C8C76, 0x2620D9F0, 0x71A80D4D, 0x7A74C473, 0x449410E9, 0xA20E4211, 0xF9C8082B, 0x0A6B334A, 0xB5F68ED2, 0x8243CC1B, 0x453C0FF3, 0x9BE564A0, 0x4FF55A4F, 0x8740F8E7, 0xCCA7F15F, 0xE300FE21, 0x786D37D6, 0xDFD506F1, 0x8EE00973, 0x17BBDE36, 0x7A670FA8, 0x5C31AB9E, 0xD4DAB618, 0xCC1F52F5, 0xE358EB4F, 0x19B9E343, 0x3A8D77DD, 0xCDB93DA6, 0x140FD52D, 0x395412F8, 0x2BA63360, 0x37E53AD0, 0x80700F1C, 0x7624ED0B, 0x703DC1EC, 0xB7366795, 0xD6549D15, 0x66CE46D7, 0xD17ABE76, 0xA448E0A0, 0x28F07C02, 0xC31249B7, 0x6E9ED6BA, 0xEAA47F78, 0xBBCFFFBD, 0xC507CA84, 0xE965F4DA, 0x8E9F35DA, 0x6AD2AA44, 0x577452AC, 0xB5D674A7, 0x5461A46A, 0x6763152A, 0x9C12B7AA, 0x12615927, 0x7B4FB118, 0xC351758D, 0x7E81687B, 0x5F52F0B3, 0x2D4254ED, 0xD4C77271, 0x0431ACAB, 0xBEF94AEC, 0xFEE994CD, 0x9C4D9E81, 0xED623730, 0xCF8A21E8, 0x51917F0B, 0xA7A9B5D6, 0xB297ADF8, 0xEED30431, 0x68CAC921, 0xF1B35D46, 0x7A430A36, 0x51194022, 0x9ABCA65E, 0x85EC70BA, 0x39AEA8CC, 0x737BAE8B, 0x582924D5, 0x03098A5A, 0x92396B81, 0x18DE2522, 0x745C1CB8, 0xA1B8FE1D, 0x5DB3C697, 0x29164F83, 0x97C16376, 0x8419224C, 0x21203B35, 0x833AC0FE, 0xD966A19A, 0xAAF0B24F, 0x40FDA998, 0xE7D52D71, 0x390896A8, 0xCEE6053F, 0xD0B0D300, 0xFF99CBCC, 0x065E3D40 }; } /* * Turing * (C) 1999-2008 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { namespace { /* * Perform an N-way PHT */ inline void PHT(MemoryRegion<u32bit>& B) { u32bit sum = 0; for(size_t i = 0; i < B.size() - 1; ++i) sum += B[i]; B[B.size()-1] += sum; sum = B[B.size()-1]; for(size_t i = 0; i < B.size() - 1; ++i) B[i] += sum; } } /* * Combine cipher stream with message */ void Turing::cipher(const byte in[], byte out[], size_t length) { while(length >= buffer.size() - position) { xor_buf(out, in, &buffer[position], buffer.size() - position); length -= (buffer.size() - position); in += (buffer.size() - position); out += (buffer.size() - position); generate(); } xor_buf(out, in, &buffer[position], length); position += length; } /* * Generate cipher stream */ void Turing::generate() { // Table for Turing's polynomial multiplication static const u32bit MULT_TAB[256] = { 0x00000000, 0xD02B4367, 0xED5686CE, 0x3D7DC5A9, 0x97AC41D1, 0x478702B6, 0x7AFAC71F, 0xAAD18478, 0x631582EF, 0xB33EC188, 0x8E430421, 0x5E684746, 0xF4B9C33E, 0x24928059, 0x19EF45F0, 0xC9C40697, 0xC62A4993, 0x16010AF4, 0x2B7CCF5D, 0xFB578C3A, 0x51860842, 0x81AD4B25, 0xBCD08E8C, 0x6CFBCDEB, 0xA53FCB7C, 0x7514881B, 0x48694DB2, 0x98420ED5, 0x32938AAD, 0xE2B8C9CA, 0xDFC50C63, 0x0FEE4F04, 0xC154926B, 0x117FD10C, 0x2C0214A5, 0xFC2957C2, 0x56F8D3BA, 0x86D390DD, 0xBBAE5574, 0x6B851613, 0xA2411084, 0x726A53E3, 0x4F17964A, 0x9F3CD52D, 0x35ED5155, 0xE5C61232, 0xD8BBD79B, 0x089094FC, 0x077EDBF8, 0xD755989F, 0xEA285D36, 0x3A031E51, 0x90D29A29, 0x40F9D94E, 0x7D841CE7, 0xADAF5F80, 0x646B5917, 0xB4401A70, 0x893DDFD9, 0x59169CBE, 0xF3C718C6, 0x23EC5BA1, 0x1E919E08, 0xCEBADD6F, 0xCFA869D6, 0x1F832AB1, 0x22FEEF18, 0xF2D5AC7F, 0x58042807, 0x882F6B60, 0xB552AEC9, 0x6579EDAE, 0xACBDEB39, 0x7C96A85E, 0x41EB6DF7, 0x91C02E90, 0x3B11AAE8, 0xEB3AE98F, 0xD6472C26, 0x066C6F41, 0x09822045, 0xD9A96322, 0xE4D4A68B, 0x34FFE5EC, 0x9E2E6194, 0x4E0522F3, 0x7378E75A, 0xA353A43D, 0x6A97A2AA, 0xBABCE1CD, 0x87C12464, 0x57EA6703, 0xFD3BE37B, 0x2D10A01C, 0x106D65B5, 0xC04626D2, 0x0EFCFBBD, 0xDED7B8DA, 0xE3AA7D73, 0x33813E14, 0x9950BA6C, 0x497BF90B, 0x74063CA2, 0xA42D7FC5, 0x6DE97952, 0xBDC23A35, 0x80BFFF9C, 0x5094BCFB, 0xFA453883, 0x2A6E7BE4, 0x1713BE4D, 0xC738FD2A, 0xC8D6B22E, 0x18FDF149, 0x258034E0, 0xF5AB7787, 0x5F7AF3FF, 0x8F51B098, 0xB22C7531, 0x62073656, 0xABC330C1, 0x7BE873A6, 0x4695B60F, 0x96BEF568, 0x3C6F7110, 0xEC443277, 0xD139F7DE, 0x0112B4B9, 0xD31DD2E1, 0x03369186, 0x3E4B542F, 0xEE601748, 0x44B19330, 0x949AD057, 0xA9E715FE, 0x79CC5699, 0xB008500E, 0x60231369, 0x5D5ED6C0, 0x8D7595A7, 0x27A411DF, 0xF78F52B8, 0xCAF29711, 0x1AD9D476, 0x15379B72, 0xC51CD815, 0xF8611DBC, 0x284A5EDB, 0x829BDAA3, 0x52B099C4, 0x6FCD5C6D, 0xBFE61F0A, 0x7622199D, 0xA6095AFA, 0x9B749F53, 0x4B5FDC34, 0xE18E584C, 0x31A51B2B, 0x0CD8DE82, 0xDCF39DE5, 0x1249408A, 0xC26203ED, 0xFF1FC644, 0x2F348523, 0x85E5015B, 0x55CE423C, 0x68B38795, 0xB898C4F2, 0x715CC265, 0xA1778102, 0x9C0A44AB, 0x4C2107CC, 0xE6F083B4, 0x36DBC0D3, 0x0BA6057A, 0xDB8D461D, 0xD4630919, 0x04484A7E, 0x39358FD7, 0xE91ECCB0, 0x43CF48C8, 0x93E40BAF, 0xAE99CE06, 0x7EB28D61, 0xB7768BF6, 0x675DC891, 0x5A200D38, 0x8A0B4E5F, 0x20DACA27, 0xF0F18940, 0xCD8C4CE9, 0x1DA70F8E, 0x1CB5BB37, 0xCC9EF850, 0xF1E33DF9, 0x21C87E9E, 0x8B19FAE6, 0x5B32B981, 0x664F7C28, 0xB6643F4F, 0x7FA039D8, 0xAF8B7ABF, 0x92F6BF16, 0x42DDFC71, 0xE80C7809, 0x38273B6E, 0x055AFEC7, 0xD571BDA0, 0xDA9FF2A4, 0x0AB4B1C3, 0x37C9746A, 0xE7E2370D, 0x4D33B375, 0x9D18F012, 0xA06535BB, 0x704E76DC, 0xB98A704B, 0x69A1332C, 0x54DCF685, 0x84F7B5E2, 0x2E26319A, 0xFE0D72FD, 0xC370B754, 0x135BF433, 0xDDE1295C, 0x0DCA6A3B, 0x30B7AF92, 0xE09CECF5, 0x4A4D688D, 0x9A662BEA, 0xA71BEE43, 0x7730AD24, 0xBEF4ABB3, 0x6EDFE8D4, 0x53A22D7D, 0x83896E1A, 0x2958EA62, 0xF973A905, 0xC40E6CAC, 0x14252FCB, 0x1BCB60CF, 0xCBE023A8, 0xF69DE601, 0x26B6A566, 0x8C67211E, 0x5C4C6279, 0x6131A7D0, 0xB11AE4B7, 0x78DEE220, 0xA8F5A147, 0x958864EE, 0x45A32789, 0xEF72A3F1, 0x3F59E096, 0x0224253F, 0xD20F6658 }; /* I tried an implementation without precomputed LFSR offsets, since I thought that might allow (especially on x86-64) the use of leal to compute all the offsets.. However on my Core2 with GCC 4.3 it turned out significantly slower (238 Mib/s, versus 300 Mib/s with precomputed offsets) I also tried using byte vs u32bit for the offset variable (since x86 memory addressing modes can be odd), but it made things even slower (186 Mib/s) */ static const byte OFFSETS[221] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 14, 15, 16, 5, 6, 7, 8, 9, 10, 11, 12, 13, 0, 2, 3, 4, 10, 11, 12, 13, 14, 15, 16, 0, 1, 5, 7, 8, 9, 15, 16, 0, 1, 2, 3, 4, 5, 6, 10, 12, 13, 14, 3, 4, 5, 6, 7, 8, 9, 10, 11, 15, 0, 1, 2, 8, 9, 10, 11, 12, 13, 14, 15, 16, 3, 5, 6, 7, 13, 14, 15, 16, 0, 1, 2, 3, 4, 8, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 13, 15, 16, 0, 6, 7, 8, 9, 10, 11, 12, 13, 14, 1, 3, 4, 5, 11, 12, 13, 14, 15, 16, 0, 1, 2, 6, 8, 9, 10, 16, 0, 1, 2, 3, 4, 5, 6, 7, 11, 13, 14, 15, 4, 5, 6, 7, 8, 9, 10, 11, 12, 16, 1, 2, 3, 9, 10, 11, 12, 13, 14, 15, 16, 0, 4, 6, 7, 8, 14, 15, 16, 0, 1, 2, 3, 4, 5, 9, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9, 10, 14, 16, 0, 1, 7, 8, 9, 10, 11, 12, 13, 14, 15, 2, 4, 5, 6, 12, 13, 14, 15, 16, 0, 1, 2, 3, 7, 9, 10, 11 }; for(size_t i = 0; i != 17; ++i) { const byte* R_off = OFFSETS + 13*i; u32bit R0 = R[R_off[0]]; u32bit R1 = R[R_off[1]]; u32bit R2 = R[R_off[2]]; u32bit R3 = R[R_off[3]]; u32bit R4 = R[R_off[4]]; const u32bit R5 = R[R_off[5]]; const u32bit R6 = R[R_off[6]]; const u32bit R7 = R[R_off[7]]; const u32bit R8 = R[R_off[8]]; const u32bit R9 = R[R_off[9]]; const u32bit R10 = R[R_off[10]]; const u32bit R11 = R[R_off[11]]; const u32bit R12 = R[R_off[12]]; R[R_off[0]] = R0 = ((R0 << 8) ^ MULT_TAB[(R0 >> 24) & 0xFF]) ^ R11 ^ R4; u32bit A = R0; u32bit B = R10; u32bit C = R7; u32bit D = R2; u32bit E = R1; E += A + B + C + D; A += E; B += E; C += E; D += E; A = S0[get_byte(0, A)] ^ S1[get_byte(1, A)] ^ S2[get_byte(2, A)] ^ S3[get_byte(3, A)]; B = S0[get_byte(1, B)] ^ S1[get_byte(2, B)] ^ S2[get_byte(3, B)] ^ S3[get_byte(0, B)]; C = S0[get_byte(2, C)] ^ S1[get_byte(3, C)] ^ S2[get_byte(0, C)] ^ S3[get_byte(1, C)]; D = S0[get_byte(3, D)] ^ S1[get_byte(0, D)] ^ S2[get_byte(1, D)] ^ S3[get_byte(2, D)]; E = S0[get_byte(0, E)] ^ S1[get_byte(1, E)] ^ S2[get_byte(2, E)] ^ S3[get_byte(3, E)]; E += A + B + C + D; A += E; B += E; C += E; D += E; R[R_off[1]] = R1 = ((R1 << 8) ^ MULT_TAB[(R1 >> 24) & 0xFF]) ^ R12 ^ R5; R[R_off[2]] = R2 = ((R2 << 8) ^ MULT_TAB[(R2 >> 24) & 0xFF]) ^ R0 ^ R6; R[R_off[3]] = ((R3 << 8) ^ MULT_TAB[(R3 >> 24) & 0xFF]) ^ R1 ^ R7; E += R4; R[R_off[4]] = ((R4 << 8) ^ MULT_TAB[(R4 >> 24) & 0xFF]) ^ R2 ^ R8; A += R1; B += R12; C += R9; D += R5; store_be(A, &buffer[20*i + 0]); store_be(B, &buffer[20*i + 4]); store_be(C, &buffer[20*i + 8]); store_be(D, &buffer[20*i + 12]); store_be(E, &buffer[20*i + 16]); } position = 0; } /* * Turing's byte mixing step */ u32bit Turing::fixedS(u32bit W) { byte B = SBOX[get_byte(0, W)]; W ^= Q_BOX[B]; W &= 0x00FFFFFF; W |= B << 24; B = SBOX[get_byte(1, W)]; W ^= rotate_left(Q_BOX[B], 8); W &= 0xFF00FFFF; W |= B << 16; B = SBOX[get_byte(2, W)]; W ^= rotate_left(Q_BOX[B], 16); W &= 0xFFFF00FF; W |= B << 8; B = SBOX[get_byte(3, W)]; W ^= rotate_left(Q_BOX[B], 24); W &= 0xFFFFFF00; W |= B; return W; } /* * Turing Key Schedule */ void Turing::key_schedule(const byte key[], size_t length) { K.resize(length / 4); for(size_t i = 0; i != length; ++i) K[i/4] = (K[i/4] << 8) + key[i]; for(size_t i = 0; i != K.size(); ++i) K[i] = fixedS(K[i]); PHT(K); for(u32bit i = 0; i != 256; ++i) { u32bit W0 = 0, C0 = i; u32bit W1 = 0, C1 = i; u32bit W2 = 0, C2 = i; u32bit W3 = 0, C3 = i; for(size_t j = 0; j < K.size(); ++j) { C0 = SBOX[get_byte(0, K[j]) ^ C0]; C1 = SBOX[get_byte(1, K[j]) ^ C1]; C2 = SBOX[get_byte(2, K[j]) ^ C2]; C3 = SBOX[get_byte(3, K[j]) ^ C3]; W0 ^= rotate_left(Q_BOX[C0], j); W1 ^= rotate_left(Q_BOX[C1], j + 8); W2 ^= rotate_left(Q_BOX[C2], j + 16); W3 ^= rotate_left(Q_BOX[C3], j + 24); } S0[i] = (W0 & 0x00FFFFFF) | (C0 << 24); S1[i] = (W1 & 0xFF00FFFF) | (C1 << 16); S2[i] = (W2 & 0xFFFF00FF) | (C2 << 8); S3[i] = (W3 & 0xFFFFFF00) | C3; } set_iv(0, 0); } /* * Resynchronization */ void Turing::set_iv(const byte iv[], size_t length) { if(!valid_iv_length(length)) throw Invalid_IV_Length(name(), length); SecureVector<u32bit> IV(length / 4); for(size_t i = 0; i != length; ++i) IV[i/4] = (IV[i/4] << 8) + iv[i]; for(size_t i = 0; i != IV.size(); ++i) R[i] = IV[i] = fixedS(IV[i]); for(size_t i = 0; i != K.size(); ++i) R[i+IV.size()] = K[i]; R[K.size() + IV.size()] = (0x010203 << 8) | (K.size() << 4) | IV.size(); for(size_t i = K.size() + IV.size() + 1; i != 17; ++i) { const u32bit W = R[i-K.size()-IV.size()-1] + R[i-1]; R[i] = S0[get_byte(0, W)] ^ S1[get_byte(1, W)] ^ S2[get_byte(2, W)] ^ S3[get_byte(3, W)]; } PHT(R); generate(); } /* * Clear memory of sensitive data */ void Turing::clear() { zeroise(S0); zeroise(S1); zeroise(S2); zeroise(S3); zeroise(buffer); position = 0; } } /* * WiderWake * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Combine cipher stream with message */ void WiderWake_41_BE::cipher(const byte in[], byte out[], size_t length) { while(length >= buffer.size() - position) { xor_buf(out, in, &buffer[position], buffer.size() - position); length -= (buffer.size() - position); in += (buffer.size() - position); out += (buffer.size() - position); generate(buffer.size()); } xor_buf(out, in, &buffer[position], length); position += length; } /* * Generate cipher stream */ void WiderWake_41_BE::generate(size_t length) { u32bit R0 = state[0], R1 = state[1], R2 = state[2], R3 = state[3], R4 = state[4]; for(size_t i = 0; i != length; i += 8) { u32bit R0a; store_be(R3, &buffer[i]); R0a = R4 + R3; R3 += R2; R2 += R1; R1 += R0; R0a = (R0a >> 8) ^ T[(R0a & 0xFF)]; R1 = (R1 >> 8) ^ T[(R1 & 0xFF)]; R2 = (R2 >> 8) ^ T[(R2 & 0xFF)]; R3 = (R3 >> 8) ^ T[(R3 & 0xFF)]; R4 = R0; R0 = R0a; store_be(R3, &buffer[i + 4]); R0a = R4 + R3; R3 += R2; R2 += R1; R1 += R0; R0a = (R0a >> 8) ^ T[(R0a & 0xFF)]; R1 = (R1 >> 8) ^ T[(R1 & 0xFF)]; R2 = (R2 >> 8) ^ T[(R2 & 0xFF)]; R3 = (R3 >> 8) ^ T[(R3 & 0xFF)]; R4 = R0; R0 = R0a; } state[0] = R0; state[1] = R1; state[2] = R2; state[3] = R3; state[4] = R4; position = 0; } /* * WiderWake Key Schedule */ void WiderWake_41_BE::key_schedule(const byte key[], size_t) { for(size_t i = 0; i != 4; ++i) t_key[i] = load_be<u32bit>(key, i); static const u32bit MAGIC[8] = { 0x726A8F3B, 0xE69A3B5C, 0xD3C71FE5, 0xAB3C73D2, 0x4D3A8EB3, 0x0396D6E8, 0x3D4C2F7A, 0x9EE27CF3 }; for(size_t i = 0; i != 4; ++i) T[i] = t_key[i]; for(size_t i = 4; i != 256; ++i) { u32bit X = T[i-1] + T[i-4]; T[i] = (X >> 3) ^ MAGIC[X % 8]; } for(size_t i = 0; i != 23; ++i) T[i] += T[i+89]; u32bit X = T[33]; u32bit Z = (T[59] | 0x01000001) & 0xFF7FFFFF; for(size_t i = 0; i != 256; ++i) { X = (X & 0xFF7FFFFF) + Z; T[i] = (T[i] & 0x00FFFFFF) ^ X; } X = (T[X & 0xFF] ^ X) & 0xFF; Z = T[0]; T[0] = T[X]; for(size_t i = 1; i != 256; ++i) { T[X] = T[i]; X = (T[i ^ X] ^ X) & 0xFF; T[i] = T[X]; } T[X] = Z; position = 0; const byte ZEROS[8] = { 0 }; set_iv(ZEROS, sizeof(ZEROS)); } /* * Resynchronization */ void WiderWake_41_BE::set_iv(const byte iv[], size_t length) { if(!valid_iv_length(length)) throw Invalid_IV_Length(name(), length); for(size_t i = 0; i != 4; ++i) state[i] = t_key[i]; state[4] = load_be<u32bit>(iv, 0); state[0] ^= state[4]; state[2] ^= load_be<u32bit>(iv, 1); generate(8*4); generate(buffer.size()); } /* * Clear memory of sensitive data */ void WiderWake_41_BE::clear() { position = 0; zeroise(t_key); zeroise(state); zeroise(T); zeroise(buffer); } } /* * Runtime assertion checking * (C) 2010 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <sstream> namespace Botan { void assertion_failure(const char* expr_str, const char* msg, const char* func, const char* file, int line) { std::ostringstream format; format << "Assertion " << expr_str << " failed "; if(msg) format << "(" << msg << ") "; if(func) format << "in " << func << " "; format << "@" << file << ":" << line; throw Internal_Error(format.str()); } } /* * Character Set Handling * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <cctype> namespace Botan { namespace Charset { namespace { /* * Convert from UCS-2 to ISO 8859-1 */ std::string ucs2_to_latin1(const std::string& ucs2) { if(ucs2.size() % 2 == 1) throw Decoding_Error("UCS-2 string has an odd number of bytes"); std::string latin1; for(size_t i = 0; i != ucs2.size(); i += 2) { const byte c1 = ucs2[i]; const byte c2 = ucs2[i+1]; if(c1 != 0) throw Decoding_Error("UCS-2 has non-Latin1 characters"); latin1 += static_cast<char>(c2); } return latin1; } /* * Convert from UTF-8 to ISO 8859-1 */ std::string utf8_to_latin1(const std::string& utf8) { std::string iso8859; size_t position = 0; while(position != utf8.size()) { const byte c1 = static_cast<byte>(utf8[position++]); if(c1 <= 0x7F) iso8859 += static_cast<char>(c1); else if(c1 >= 0xC0 && c1 <= 0xC7) { if(position == utf8.size()) throw Decoding_Error("UTF-8: sequence truncated"); const byte c2 = static_cast<byte>(utf8[position++]); const byte iso_char = ((c1 & 0x07) << 6) | (c2 & 0x3F); if(iso_char <= 0x7F) throw Decoding_Error("UTF-8: sequence longer than needed"); iso8859 += static_cast<char>(iso_char); } else throw Decoding_Error("UTF-8: Unicode chars not in Latin1 used"); } return iso8859; } /* * Convert from ISO 8859-1 to UTF-8 */ std::string latin1_to_utf8(const std::string& iso8859) { std::string utf8; for(size_t i = 0; i != iso8859.size(); ++i) { const byte c = static_cast<byte>(iso8859[i]); if(c <= 0x7F) utf8 += static_cast<char>(c); else { utf8 += static_cast<char>((0xC0 | (c >> 6))); utf8 += static_cast<char>((0x80 | (c & 0x3F))); } } return utf8; } } /* * Perform character set transcoding */ std::string transcode(const std::string& str, Character_Set to, Character_Set from) { if(to == LOCAL_CHARSET) to = LATIN1_CHARSET; if(from == LOCAL_CHARSET) from = LATIN1_CHARSET; if(to == from) return str; if(from == LATIN1_CHARSET && to == UTF8_CHARSET) return latin1_to_utf8(str); if(from == UTF8_CHARSET && to == LATIN1_CHARSET) return utf8_to_latin1(str); if(from == UCS2_CHARSET && to == LATIN1_CHARSET) return ucs2_to_latin1(str); throw Invalid_Argument("Unknown transcoding operation from " + to_string(from) + " to " + to_string(to)); } /* * Check if a character represents a digit */ bool is_digit(char c) { if(c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9') return true; return false; } /* * Check if a character represents whitespace */ bool is_space(char c) { if(c == ' ' || c == '\t' || c == '\n' || c == '\r') return true; return false; } /* * Convert a character to a digit */ byte char2digit(char c) { switch(c) { case '0': return 0; case '1': return 1; case '2': return 2; case '3': return 3; case '4': return 4; case '5': return 5; case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9; } throw Invalid_Argument("char2digit: Input is not a digit character"); } /* * Convert a digit to a character */ char digit2char(byte b) { switch(b) { case 0: return '0'; case 1: return '1'; case 2: return '2'; case 3: return '3'; case 4: return '4'; case 5: return '5'; case 6: return '6'; case 7: return '7'; case 8: return '8'; case 9: return '9'; } throw Invalid_Argument("digit2char: Input is not a digit"); } /* * Case-insensitive character comparison */ bool caseless_cmp(char a, char b) { return (std::tolower(static_cast<unsigned char>(a)) == std::tolower(static_cast<unsigned char>(b))); } } } /* * Runtime CPU detection * (C) 2009-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ #if defined(BOTAN_TARGET_CPU_IS_PPC_FAMILY) #if defined(BOTAN_TARGET_OS_IS_DARWIN) #include <sys/sysctl.h> #endif #if defined(BOTAN_TARGET_OS_IS_OPENBSD) #include <sys/param.h> #include <sys/sysctl.h> #include <machine/cpu.h> #endif #endif #if defined(BOTAN_TARGET_CPU_IS_X86_FAMILY) #if defined(BOTAN_BUILD_COMPILER_IS_MSVC) #include <intrin.h> #define CALL_CPUID(type, out) do { __cpuid((int*)out, type); } while(0) #elif defined(BOTAN_BUILD_COMPILER_IS_INTEL) #include <ia32intrin.h> #define CALL_CPUID(type, out) do { __cpuid(out, type); } while(0) #elif defined(BOTAN_BUILD_COMPILER_IS_GCC) && (BOTAN_GCC_VERSION >= 430) // Only available starting in GCC 4.3 #include <cpuid.h> namespace { /* * Prevent inlining to work around GCC bug 44174 */ void __attribute__((__noinline__)) call_gcc_cpuid(Botan::u32bit type, Botan::u32bit out[4]) { __get_cpuid(type, out, out+1, out+2, out+3); } #define CALL_CPUID call_gcc_cpuid } #elif defined(BOTAN_TARGET_ARCH_IS_X86_64) && \ (defined(BOTAN_BUILD_COMPILER_IS_CLANG) || defined(BOTAN_BUILD_COMPILER_IS_GCC)) /* * We can't safely use this on x86-32 as some 32-bit ABIs use ebx as * a PIC register, and in theory there are some x86-32s still out * there that don't support cpuid at all; it requires strange * contortions to detect them. */ #define CALL_CPUID(type, out) \ asm("cpuid\n\t" : "=a" (out[0]), "=b" (out[1]), "=c" (out[2]), "=d" (out[3]) \ : "0" (type)) #else #warning "No method of calling CPUID for this compiler" #endif #endif #ifndef CALL_CPUID // In all other cases, just zeroize the supposed cpuid output #define CALL_CPUID(type, out) \ do { out[0] = out[1] = out[2] = out[3] = 0; } while(0); #endif namespace Botan { u64bit CPUID::x86_processor_flags = 0; size_t CPUID::cache_line = 32; bool CPUID::altivec_capable = false; namespace { u32bit get_x86_cache_line_size() { const u32bit INTEL_CPUID[3] = { 0x756E6547, 0x6C65746E, 0x49656E69 }; const u32bit AMD_CPUID[3] = { 0x68747541, 0x444D4163, 0x69746E65 }; u32bit cpuid[4] = { 0 }; CALL_CPUID(0, cpuid); if(same_mem(cpuid + 1, INTEL_CPUID, 3)) { CALL_CPUID(1, cpuid); return 8 * get_byte(2, cpuid[1]); } else if(same_mem(cpuid + 1, AMD_CPUID, 3)) { CALL_CPUID(0x80000005, cpuid); return get_byte(3, cpuid[2]); } else return 32; // default cache line guess } #if defined(BOTAN_TARGET_CPU_IS_PPC_FAMILY) bool altivec_check_sysctl() { #if defined(BOTAN_TARGET_OS_IS_DARWIN) || defined(BOTAN_TARGET_OS_IS_OPENBSD) #if defined(BOTAN_TARGET_OS_IS_OPENBSD) int sels[2] = { CTL_MACHDEP, CPU_ALTIVEC }; #else // From Apple's docs int sels[2] = { CTL_HW, HW_VECTORUNIT }; #endif int vector_type = 0; size_t length = sizeof(vector_type); int error = sysctl(sels, 2, &vector_type, &length, NULL, 0); if(error == 0 && vector_type > 0) return true; #endif return false; } bool altivec_check_pvr_emul() { bool altivec_capable = false; #if defined(BOTAN_TARGET_OS_IS_LINUX) || defined(BOTAN_TARGET_OS_IS_NETBSD) /* On PowerPC, MSR 287 is PVR, the Processor Version Number Normally it is only accessible to ring 0, but Linux and NetBSD (others, too, maybe?) will trap and emulate it for us. PVR identifiers for various AltiVec enabled CPUs. Taken from PearPC and Linux sources, mostly. */ const u16bit PVR_G4_7400 = 0x000C; const u16bit PVR_G5_970 = 0x0039; const u16bit PVR_G5_970FX = 0x003C; const u16bit PVR_G5_970MP = 0x0044; const u16bit PVR_G5_970GX = 0x0045; const u16bit PVR_POWER6 = 0x003E; const u16bit PVR_POWER7 = 0x003F; const u16bit PVR_CELL_PPU = 0x0070; // Motorola produced G4s with PVR 0x800[0123C] (at least) const u16bit PVR_G4_74xx_24 = 0x800; u32bit pvr = 0; asm volatile("mfspr %0, 287" : "=r" (pvr)); // Top 16 bit suffice to identify model pvr >>= 16; altivec_capable |= (pvr == PVR_G4_7400); altivec_capable |= ((pvr >> 4) == PVR_G4_74xx_24); altivec_capable |= (pvr == PVR_G5_970); altivec_capable |= (pvr == PVR_G5_970FX); altivec_capable |= (pvr == PVR_G5_970MP); altivec_capable |= (pvr == PVR_G5_970GX); altivec_capable |= (pvr == PVR_POWER6); altivec_capable |= (pvr == PVR_POWER7); altivec_capable |= (pvr == PVR_CELL_PPU); #endif return altivec_capable; } #endif } void CPUID::initialize() { u32bit cpuid[4] = { 0 }; CALL_CPUID(1, cpuid); x86_processor_flags = (static_cast<u64bit>(cpuid[2]) << 32) | cpuid[3]; #if defined(BOTAN_TARGET_ARCH_IS_X86_64) /* * If we don't have access to CPUID, we can still safely assume that * any x86-64 processor has SSE2. */ if(x86_processor_flags == 0) x86_processor_flags |= (1 << CPUID_SSE2_BIT); #endif cache_line = get_x86_cache_line_size(); altivec_capable = false; #if defined(BOTAN_TARGET_CPU_IS_PPC_FAMILY) if(altivec_check_sysctl() || altivec_check_pvr_emul()) altivec_capable = true; #endif } } /* * Data Store * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Default Matcher transform operation (identity) */ std::pair<std::string, std::string> Data_Store::Matcher::transform(const std::string& key, const std::string& value) const { return std::make_pair(key, value); } /* * Data_Store Equality Comparison */ bool Data_Store::operator==(const Data_Store& other) const { return (contents == other.contents); } /* * Check if this key has at least one value */ bool Data_Store::has_value(const std::string& key) const { return (contents.lower_bound(key) != contents.end()); } /* * Search based on an arbitrary predicate */ std::multimap<std::string, std::string> Data_Store::search_with(const Matcher& matcher) const { std::multimap<std::string, std::string> out; std::multimap<std::string, std::string>::const_iterator i = contents.begin(); while(i != contents.end()) { if(matcher(i->first, i->second)) { std::pair<std::string, std::string> p( matcher.transform(i->first, i->second)); multimap_insert(out, p.first, p.second); } ++i; } return out; } /* * Search based on key equality */ std::vector<std::string> Data_Store::get(const std::string& looking_for) const { typedef std::multimap<std::string, std::string>::const_iterator iter; std::pair<iter, iter> range = contents.equal_range(looking_for); std::vector<std::string> out; for(iter i = range.first; i != range.second; ++i) out.push_back(i->second); return out; } /* * Get a single atom */ std::string Data_Store::get1(const std::string& key) const { std::vector<std::string> vals = get(key); if(vals.empty()) throw Invalid_State("Data_Store::get1: Not values for " + key); if(vals.size() > 1) throw Invalid_State("Data_Store::get1: More than one value for " + key); return vals[0]; } /* * Get a single MemoryVector atom */ MemoryVector<byte> Data_Store::get1_memvec(const std::string& key) const { std::vector<std::string> vals = get(key); if(vals.empty()) return MemoryVector<byte>(); if(vals.size() > 1) throw Invalid_State("Data_Store::get1_memvec: Multiple values for " + key); return hex_decode(vals[0]); } /* * Get a single u32bit atom */ u32bit Data_Store::get1_u32bit(const std::string& key, u32bit default_val) const { std::vector<std::string> vals = get(key); if(vals.empty()) return default_val; else if(vals.size() > 1) throw Invalid_State("Data_Store::get1_u32bit: Multiple values for " + key); return to_u32bit(vals[0]); } /* * Insert a single key and value */ void Data_Store::add(const std::string& key, const std::string& val) { multimap_insert(contents, key, val); } /* * Insert a single key and value */ void Data_Store::add(const std::string& key, u32bit val) { add(key, to_string(val)); } /* * Insert a single key and value */ void Data_Store::add(const std::string& key, const MemoryRegion<byte>& val) { add(key, hex_encode(&val[0], val.size())); } /* * Insert a mapping of key/value pairs */ void Data_Store::add(const std::multimap<std::string, std::string>& in) { std::multimap<std::string, std::string>::const_iterator i = in.begin(); while(i != in.end()) { contents.insert(*i); ++i; } } } /** * Dynamically Loaded Object * (C) 2010 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <stdexcept> #if defined(BOTAN_TARGET_OS_HAS_DLOPEN) #include <dlfcn.h> #elif defined(BOTAN_TARGET_OS_HAS_LOADLIBRARY) #include <windows.h> #endif namespace Botan { namespace { void raise_runtime_loader_exception(const std::string& lib_name, const char* msg) { throw std::runtime_error("Failed to load " + lib_name + ": " + (msg ? msg : "Unknown error")); } } Dynamically_Loaded_Library::Dynamically_Loaded_Library( const std::string& library) : lib_name(library), lib(0) { #if defined(BOTAN_TARGET_OS_HAS_DLOPEN) lib = ::dlopen(lib_name.c_str(), RTLD_LAZY); if(!lib) raise_runtime_loader_exception(lib_name, dlerror()); #elif defined(BOTAN_TARGET_OS_HAS_LOADLIBRARY) lib = ::LoadLibraryA(lib_name.c_str()); if(!lib) raise_runtime_loader_exception(lib_name, "LoadLibrary failed"); #endif if(!lib) raise_runtime_loader_exception(lib_name, "Dynamic load not supported"); } Dynamically_Loaded_Library::~Dynamically_Loaded_Library() { #if defined(BOTAN_TARGET_OS_HAS_DLOPEN) ::dlclose(lib); #elif defined(BOTAN_TARGET_OS_HAS_LOADLIBRARY) ::FreeLibrary((HMODULE)lib); #endif } void* Dynamically_Loaded_Library::resolve_symbol(const std::string& symbol) { void* addr = 0; #if defined(BOTAN_TARGET_OS_HAS_DLOPEN) addr = ::dlsym(lib, symbol.c_str()); #elif defined(BOTAN_TARGET_OS_HAS_LOADLIBRARY) addr = reinterpret_cast<void*>(::GetProcAddress((HMODULE)lib, symbol.c_str())); #endif if(!addr) throw std::runtime_error("Failed to resolve symbol " + symbol + " in " + lib_name); return addr; } } /* * Memory Locking Functions * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ #if defined(BOTAN_TARGET_OS_HAS_POSIX_MLOCK) #include <sys/types.h> #include <sys/mman.h> #elif defined(BOTAN_TARGET_OS_HAS_WIN32_VIRTUAL_LOCK) #include <windows.h> #endif namespace Botan { bool has_mlock() { byte buf[4096]; if(!lock_mem(&buf, sizeof(buf))) return false; unlock_mem(&buf, sizeof(buf)); return true; } /* * Lock an area of memory into RAM */ bool lock_mem(void* ptr, size_t bytes) { #if defined(BOTAN_TARGET_OS_HAS_POSIX_MLOCK) return (::mlock(static_cast<char*>(ptr), bytes) == 0); #elif defined(BOTAN_TARGET_OS_HAS_WIN32_VIRTUAL_LOCK) return (::VirtualLock(ptr, bytes) != 0); #else return false; #endif } /* * Unlock a previously locked region of memory */ void unlock_mem(void* ptr, size_t bytes) { #if defined(BOTAN_TARGET_OS_HAS_POSIX_MLOCK) ::munlock(static_cast<char*>(ptr), bytes); #elif defined(BOTAN_TARGET_OS_HAS_WIN32_VIRTUAL_LOCK) ::VirtualUnlock(ptr, bytes); #endif } } /* * Parser Functions * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Convert a string into an integer */ u32bit to_u32bit(const std::string& number) { u32bit n = 0; for(std::string::const_iterator i = number.begin(); i != number.end(); ++i) { const u32bit OVERFLOW_MARK = 0xFFFFFFFF / 10; if(*i == ' ') continue; byte digit = Charset::char2digit(*i); if((n > OVERFLOW_MARK) || (n == OVERFLOW_MARK && digit > 5)) throw Decoding_Error("to_u32bit: Integer overflow"); n *= 10; n += digit; } return n; } /* * Convert an integer into a string */ std::string to_string(u64bit n, size_t min_len) { std::string lenstr; if(n) { while(n > 0) { lenstr = Charset::digit2char(n % 10) + lenstr; n /= 10; } } else lenstr = "0"; while(lenstr.size() < min_len) lenstr = "0" + lenstr; return lenstr; } /* * Convert a string into a time duration */ u32bit timespec_to_u32bit(const std::string& timespec) { if(timespec == "") return 0; const char suffix = timespec[timespec.size()-1]; std::string value = timespec.substr(0, timespec.size()-1); u32bit scale = 1; if(Charset::is_digit(suffix)) value += suffix; else if(suffix == 's') scale = 1; else if(suffix == 'm') scale = 60; else if(suffix == 'h') scale = 60 * 60; else if(suffix == 'd') scale = 24 * 60 * 60; else if(suffix == 'y') scale = 365 * 24 * 60 * 60; else throw Decoding_Error("timespec_to_u32bit: Bad input " + timespec); return scale * to_u32bit(value); } /* * Parse a SCAN-style algorithm name */ std::vector<std::string> parse_algorithm_name(const std::string& namex) { if(namex.find('(') == std::string::npos && namex.find(')') == std::string::npos) return std::vector<std::string>(1, namex); std::string name = namex, substring; std::vector<std::string> elems; size_t level = 0; elems.push_back(name.substr(0, name.find('('))); name = name.substr(name.find('(')); for(std::string::const_iterator i = name.begin(); i != name.end(); ++i) { char c = *i; if(c == '(') ++level; if(c == ')') { if(level == 1 && i == name.end() - 1) { if(elems.size() == 1) elems.push_back(substring.substr(1)); else elems.push_back(substring); return elems; } if(level == 0 || (level == 1 && i != name.end() - 1)) throw Invalid_Algorithm_Name(namex); --level; } if(c == ',' && level == 1) { if(elems.size() == 1) elems.push_back(substring.substr(1)); else elems.push_back(substring); substring.clear(); } else substring += c; } if(substring != "") throw Invalid_Algorithm_Name(namex); return elems; } /* * Split the string on slashes */ std::vector<std::string> split_on(const std::string& str, char delim) { std::vector<std::string> elems; if(str == "") return elems; std::string substr; for(std::string::const_iterator i = str.begin(); i != str.end(); ++i) { if(*i == delim) { if(substr != "") elems.push_back(substr); substr.clear(); } else substr += *i; } if(substr == "") throw Invalid_Argument("Unable to split string: " + str); elems.push_back(substr); return elems; } /* * Parse an ASN.1 OID string */ std::vector<u32bit> parse_asn1_oid(const std::string& oid) { std::string substring; std::vector<u32bit> oid_elems; for(std::string::const_iterator i = oid.begin(); i != oid.end(); ++i) { char c = *i; if(c == '.') { if(substring == "") throw Invalid_OID(oid); oid_elems.push_back(to_u32bit(substring)); substring.clear(); } else substring += c; } if(substring == "") throw Invalid_OID(oid); oid_elems.push_back(to_u32bit(substring)); if(oid_elems.size() < 2) throw Invalid_OID(oid); return oid_elems; } /* * X.500 String Comparison */ bool x500_name_cmp(const std::string& name1, const std::string& name2) { std::string::const_iterator p1 = name1.begin(); std::string::const_iterator p2 = name2.begin(); while((p1 != name1.end()) && Charset::is_space(*p1)) ++p1; while((p2 != name2.end()) && Charset::is_space(*p2)) ++p2; while(p1 != name1.end() && p2 != name2.end()) { if(Charset::is_space(*p1)) { if(!Charset::is_space(*p2)) return false; while((p1 != name1.end()) && Charset::is_space(*p1)) ++p1; while((p2 != name2.end()) && Charset::is_space(*p2)) ++p2; if(p1 == name1.end() && p2 == name2.end()) return true; } if(!Charset::caseless_cmp(*p1, *p2)) return false; ++p1; ++p2; } while((p1 != name1.end()) && Charset::is_space(*p1)) ++p1; while((p2 != name2.end()) && Charset::is_space(*p2)) ++p2; if((p1 != name1.end()) || (p2 != name2.end())) return false; return true; } /* * Convert a decimal-dotted string to binary IP */ u32bit string_to_ipv4(const std::string& str) { std::vector<std::string> parts = split_on(str, '.'); if(parts.size() != 4) throw Decoding_Error("Invalid IP string " + str); u32bit ip = 0; for(size_t i = 0; i != parts.size(); ++i) { u32bit octet = to_u32bit(parts[i]); if(octet > 255) throw Decoding_Error("Invalid IP string " + str); ip = (ip << 8) | (octet & 0xFF); } return ip; } /* * Convert an IP address to decimal-dotted string */ std::string ipv4_to_string(u32bit ip) { std::string str; for(size_t i = 0; i != sizeof(ip); ++i) { if(i) str += "."; str += to_string(get_byte(i, ip)); } return str; } } /* * Time Functions * (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <ctime> #if defined(BOTAN_TARGET_OS_HAS_WIN32_GET_SYSTEMTIME) #include <windows.h> #endif #if defined(BOTAN_TARGET_OS_HAS_GETTIMEOFDAY) #include <sys/time.h> #endif #if defined(BOTAN_TARGET_OS_HAS_CLOCK_GETTIME) #ifndef _POSIX_C_SOURCE #define _POSIX_C_SOURCE 199309 #endif #include <time.h> #ifndef CLOCK_REALTIME #define CLOCK_REALTIME 0 #endif #endif namespace Botan { namespace { /* * Combine a two time values into a single one */ u64bit combine_timers(u32bit seconds, u32bit parts, u32bit parts_hz) { static const u64bit NANOSECONDS_UNITS = 1000000000; u64bit res = seconds * NANOSECONDS_UNITS; res += parts * (NANOSECONDS_UNITS / parts_hz); return res; } std::tm do_gmtime(time_t time_val) { std::tm tm; #if defined(BOTAN_TARGET_OS_HAS_GMTIME_S) gmtime_s(&tm, &time_val); // Windows #elif defined(BOTAN_TARGET_OS_HAS_GMTIME_R) gmtime_r(&time_val, &tm); // Unix/SUSv2 #else std::tm* tm_p = std::gmtime(&time_val); if (tm_p == 0) throw Encoding_Error("time_t_to_tm could not convert"); tm = *tm_p; #endif return tm; } } /* * Get the system clock */ u64bit system_time() { return static_cast<u64bit>(std::time(0)); } /* * Convert a time_point to a calendar_point */ calendar_point calendar_value(u64bit a_time_t) { std::tm tm = do_gmtime(static_cast<std::time_t>(a_time_t)); return calendar_point(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); } u64bit get_nanoseconds_clock() { #if defined(BOTAN_TARGET_OS_HAS_CLOCK_GETTIME) struct ::timespec tv; ::clock_gettime(CLOCK_REALTIME, &tv); return combine_timers(tv.tv_sec, tv.tv_nsec, 1000000000); #elif defined(BOTAN_TARGET_OS_HAS_GETTIMEOFDAY) struct ::timeval tv; ::gettimeofday(&tv, 0); return combine_timers(tv.tv_sec, tv.tv_usec, 1000000); #elif defined(BOTAN_TARGET_OS_HAS_WIN32_GET_SYSTEMTIME) // Returns time since January 1, 1601 in 100-ns increments ::FILETIME tv; ::GetSystemTimeAsFileTime(&tv); u64bit tstamp = (static_cast<u64bit>(tv.dwHighDateTime) << 32) | tv.dwLowDateTime; return (tstamp * 100); // Scale to 1 nanosecond units #else return combine_timers(static_cast<u32bit>(std::time(0)), std::clock(), CLOCKS_PER_SEC); #endif } } /* * User Interface * (C) 1999-2007 Jack Lloyd * * Distributed under the terms of the Botan license */ namespace Botan { /* * Get a passphrase from the user */ std::string User_Interface::get_passphrase(const std::string&, const std::string&, UI_Result& action) const { action = OK; if(!first_try) action = CANCEL_ACTION; return preset_passphrase; } /* * User_Interface Constructor */ User_Interface::User_Interface(const std::string& preset) : preset_passphrase(preset) { first_try = true; } } /* * Version Information * (C) 1999-2011 Jack Lloyd * * Distributed under the terms of the Botan license */ #include <sstream> namespace Botan { /* These are intentionally compiled rather than inlined, so an application running against a shared library can test the true version they are running against. */ /* * Return the version as a string */ std::string version_string() { #define QUOTE(name) #name #define STR(macro) QUOTE(macro) return "Botan " STR(BOTAN_VERSION_MAJOR) "." STR(BOTAN_VERSION_MINOR) "." STR(BOTAN_VERSION_PATCH) " (" #if (BOTAN_VERSION_DATESTAMP == 0) "unreleased version" #else "released " STR(BOTAN_VERSION_DATESTAMP) #endif ", revision " BOTAN_VERSION_VC_REVISION ", distribution " BOTAN_DISTRIBUTION_INFO ")"; #undef STR #undef QUOTE } u32bit version_datestamp() { return BOTAN_VERSION_DATESTAMP; } /* * Return parts of the version as integers */ u32bit version_major() { return BOTAN_VERSION_MAJOR; } u32bit version_minor() { return BOTAN_VERSION_MINOR; } u32bit version_patch() { return BOTAN_VERSION_PATCH; } }
d4ba34f353d1cc1f4ea0711c4dfe707ba0c9457f
aa238e9d4f0b7ff0e86469b88bd877fde3f2c37f
/be/src/olap/column_file/segment_writer.cpp
8b7bd30c73ce349fb0bff0e1a1ae69f1acedffff
[ "Apache-2.0", "BSD-3-Clause", "dtoa", "bzip2-1.0.6", "BSL-1.0", "PSF-2.0" ]
permissive
tengxunshigou/palo
42474a5bcb3933443830c0ea6a1c56dd8f608c45
fb64a1a8e8ed612cd95d1ea0c67bf70804a1d2da
refs/heads/master
2021-01-16T21:13:50.190451
2017-08-11T14:09:07
2017-08-11T14:09:07
100,223,889
1
0
null
2017-08-14T03:18:55
2017-08-14T03:18:55
null
UTF-8
C++
false
false
10,588
cpp
// Copyright (c) 2017, Baidu.com, Inc. All Rights Reserved // Licensed to the Apache Software Foundation (ASF) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information // regarding copyright ownership. The ASF licenses this file // to you under the Apache License, Version 2.0 (the // "License"); you may not use this file except in compliance // with the License. You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, // software distributed under the License is distributed on an // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. See the License for the // specific language governing permissions and limitations // under the License. #include "olap/column_file/segment_writer.h" #include "olap/column_file/column_writer.h" #include "olap/column_file/out_stream.h" #include "olap/file_helper.h" #include "olap/utils.h" namespace palo { namespace column_file { SegmentWriter::SegmentWriter( const std::string& file_name, SmartOLAPTable table, uint32_t stream_buffer_size) : _file_name(file_name), _table(table), _stream_buffer_size(stream_buffer_size), _stream_factory(NULL), _row_count(0), _row_in_block(0), _block_count(0) {} SegmentWriter::~SegmentWriter() { SAFE_DELETE(_stream_factory); for (std::vector<ColumnWriter*>::iterator it = _root_writers.begin(); it != _root_writers.end(); ++it) { SAFE_DELETE(*it); } } OLAPStatus SegmentWriter::init(uint32_t write_mbytes_per_sec) { OLAPStatus res = OLAP_SUCCESS; // 创建factory _stream_factory = new(std::nothrow) OutStreamFactory(_table->compress_kind(), _stream_buffer_size); if (NULL == _stream_factory) { OLAP_LOG_WARNING("fail to allocate out stream factory"); return OLAP_ERR_MALLOC_ERROR; } // 创建writer for (uint32_t i = 0; i < _table->tablet_schema().size(); i++) { if (_table->tablet_schema()[i].is_root_column) { ColumnWriter* writer = ColumnWriter::create(i, _table->tablet_schema(), _stream_factory, _table->num_rows_per_row_block(), _table->bloom_filter_fpp()); if (NULL == writer) { OLAP_LOG_WARNING("fail to create writer"); return OLAP_ERR_MALLOC_ERROR; } else { _root_writers.push_back(writer); } res = writer->init(); if (OLAP_SUCCESS != res) { OLAP_LOG_WARNING("fail to initialize ColumnWriter. [res=%d]", res); return res; } } } _write_mbytes_per_sec = write_mbytes_per_sec; return OLAP_SUCCESS; } OLAPStatus SegmentWriter::write(RowCursor* row_cursor) { OLAPStatus res = OLAP_SUCCESS; // OLAP_LOG_DEBUG("row_count = %lu, row_in_block = %lu, block = %lu", // _row_count, _row_in_block, _block_count); if (_row_in_block == _table->num_rows_per_row_block()) { res = create_row_index_entry(); if (OLAP_SUCCESS != res) { OLAP_LOG_WARNING("fail to create row index entry"); } } for (std::vector<ColumnWriter*>::iterator it = _root_writers.begin(); it != _root_writers.end(); ++it) { res = (*it)->write(row_cursor); if (OLAP_UNLIKELY(OLAP_SUCCESS != res)) { OLAP_LOG_WARNING("fail to write row. [res=%d]", res); return res; } } ++_row_count; ++_row_in_block; return res; } uint64_t SegmentWriter::estimate_segment_size() { uint64_t result = 0; for (std::map<StreamName, OutStream*>::const_iterator it = _stream_factory->streams().begin(); it != _stream_factory->streams().end(); ++it) { result += it->second->get_total_buffer_size(); } for (std::vector<ColumnWriter*>::iterator it = _root_writers.begin(); it != _root_writers.end(); ++it) { result += (*it)->estimate_buffered_memory(); } return result; } OLAPStatus SegmentWriter::_make_file_header(ColumnDataHeaderMessage* file_header) { OLAPStatus res = OLAP_SUCCESS; file_header->set_number_of_rows(_row_count); file_header->set_compress_kind(_table->compress_kind()); file_header->set_stream_buffer_size(_stream_buffer_size); // TODO. 之前没设置 file_header->set_magic_string("COLUMN DATA"); file_header->set_version(1); file_header->set_num_rows_per_block(_table->num_rows_per_row_block()); // check if has bloom filter columns bool has_bf_column = false; uint32_t bf_hash_function_num = 0; uint32_t bf_bit_num = 0; for (std::vector<ColumnWriter*>::iterator it = _root_writers.begin(); it != _root_writers.end(); ++it) { (*it)->get_bloom_filter_info(&has_bf_column, &bf_hash_function_num, &bf_bit_num); if (has_bf_column) { file_header->set_bf_hash_function_num(bf_hash_function_num); file_header->set_bf_bit_num(bf_bit_num); break; } } for (std::vector<ColumnWriter*>::iterator it = _root_writers.begin(); it != _root_writers.end(); ++it) { // ColumnWriter::finalize will set: // * column_unique_id // * column_type // * column_encoding // * column_statistics res = (*it)->finalize(file_header); if (OLAP_UNLIKELY(OLAP_SUCCESS != res)) { OLAP_LOG_WARNING("fail to finalize row writer. [res=%d]", res); return res; } } uint64_t index_length = 0; uint64_t data_length = 0; for (std::map<StreamName, OutStream*>::const_iterator it = _stream_factory->streams().begin(); it != _stream_factory->streams().end(); ++it) { OutStream* stream = it->second; // 如果这个流没有被终止,flush if (!stream->is_suppressed()) { if (OLAP_SUCCESS != (res = stream->flush())) { OLAP_LOG_WARNING("fail to flush out stream. [res=%d]", res); return res; } } else { //如果被suspend,目前也就是present流,不写入信息 continue; } StreamInfoMessage* stream_info = file_header->add_stream_info(); stream_info->set_length(stream->get_stream_length()); stream_info->set_column_unique_id(it->first.unique_column_id()); stream_info->set_kind(it->first.kind()); if (it->first.kind() == StreamInfoMessage::ROW_INDEX || it->first.kind() == StreamInfoMessage::BLOOM_FILTER) { index_length += stream->get_stream_length(); } else { data_length += stream->get_stream_length(); } OLAP_LOG_DEBUG("stream id=%u, type=%d, length=%ld", it->first.unique_column_id(), it->first.kind(), stream->get_stream_length()); } file_header->set_index_length(index_length); file_header->set_data_length(data_length); return res; } // 之前所有的数据都缓存在内存里, 现在创建文件, 写入数据 OLAPStatus SegmentWriter::finalize(uint32_t* segment_file_size) { OLAPStatus res = OLAP_SUCCESS; FileHandler file_handle; FileHeader<ColumnDataHeaderMessage> file_header; if (OLAP_SUCCESS != (res = file_handle.open_with_mode( _file_name, O_CREAT | O_EXCL | O_WRONLY , S_IRUSR | S_IWUSR))) { OLAP_LOG_WARNING("fail to open file. [file_name=%s]", _file_name.c_str()); return res; } if (_row_in_block > 0) { res = create_row_index_entry(); if (OLAP_SUCCESS != res) { return res; } } res = _make_file_header(file_header.mutable_message()); if (OLAP_SUCCESS != res) { OLAP_LOG_WARNING("fail to make file header. [res=%d]", res); return res; } res = file_header.prepare(&file_handle); if (OLAP_SUCCESS != res) { OLAP_LOG_WARNING("write file header error. [err=%m]"); return res; } // 跳过FileHeader if (-1 == file_handle.seek(file_header.size(), SEEK_SET)) { OLAP_LOG_WARNING("lseek header file error. [err=%m]"); return OLAP_ERR_IO_ERROR; } uint32_t checksum = CRC32_INIT; // 写入数据 for (std::map<StreamName, OutStream*>::const_iterator it = _stream_factory->streams().begin(); it != _stream_factory->streams().end(); ++it) { OutStream* stream = it->second; // 输出没有被掐掉的流 if (!stream->is_suppressed()) { checksum = stream->crc32(checksum); OLAP_LOG_DEBUG("stream id = %u, type = %d", it->first.unique_column_id(), it->first.kind()); res = stream->write_to_file( &file_handle, _write_mbytes_per_sec); if (OLAP_SUCCESS != res) { OLAP_LOG_WARNING("fail to write stream to file. [res=%d]", res); return res; } } } uint64_t file_length = file_handle.tell(); file_header.set_file_length(file_length); file_header.set_checksum(checksum); *segment_file_size = file_length; // 写入更新之后的FileHeader res = file_header.serialize(&file_handle); if (OLAP_SUCCESS != res) { OLAP_LOG_WARNING("write file header error. [err=%m]"); return res; } res = file_handle.close(); if (OLAP_SUCCESS != res) { OLAP_LOG_WARNING("fail to close file. [err=%m]"); return res; } return res; } OLAPStatus SegmentWriter::create_row_index_entry() { OLAPStatus res = OLAP_SUCCESS; for (std::vector<ColumnWriter*>::iterator it = _root_writers.begin(); it != _root_writers.end(); ++it) { res = (*it)->create_row_index_entry(); if (OLAP_UNLIKELY(OLAP_SUCCESS != res)) { OLAP_LOG_WARNING("fail to create row index. [res=%d]", res); return res; } } OLAP_LOG_DEBUG("create row_index_entry, _block_count = %ld, _row_in_block = %ld", _block_count, _row_in_block); ++_block_count; _row_in_block = 0; return res; } } // namespace column_file } // namespace palo
570ebf347ed4dafa12c08596017f2f8b432a6a06
b710d1abde3befe7a663f5bdd9345d81420d2161
/source/HunterMage/Source/DlgExample/DlgExampleCharacter.cpp
137fb9e802ff64881468ad68511c3c4de7555a7c
[]
no_license
Qualm/Hunter-Mage
d92b69dd44383aaf419f4a91d7b0c949cdb4f344
fc5a799b326e438fbd0ec77376b9a9fb1bb219ae
refs/heads/master
2021-10-27T04:42:42.708168
2019-04-15T22:45:13
2019-04-15T22:45:13
null
0
0
null
null
null
null
UTF-8
C++
false
false
5,498
cpp
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved. #include "DlgExampleCharacter.h" #include "UObject/ConstructorHelpers.h" #include "Camera/CameraComponent.h" #include "Components/DecalComponent.h" #include "Components/CapsuleComponent.h" #include "GameFramework/CharacterMovementComponent.h" #include "GameFramework/PlayerController.h" #include "GameFramework/SpringArmComponent.h" #include "HeadMountedDisplayFunctionLibrary.h" #include "Materials/Material.h" ADlgExampleCharacter::ADlgExampleCharacter() { // Set size for player capsule GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f); // Don't rotate character to camera direction bUseControllerRotationPitch = false; bUseControllerRotationYaw = false; bUseControllerRotationRoll = false; // Configure character movement GetCharacterMovement()->bOrientRotationToMovement = true; // Rotate character to moving direction GetCharacterMovement()->RotationRate = FRotator(0.f, 640.f, 0.f); GetCharacterMovement()->bConstrainToPlane = true; GetCharacterMovement()->bSnapToPlaneAtStart = true; // Create a camera boom... CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom")); CameraBoom->SetupAttachment(RootComponent); CameraBoom->bAbsoluteRotation = true; // Don't want arm to rotate when character does CameraBoom->TargetArmLength = 800.f; CameraBoom->RelativeRotation = FRotator(-60.f, 0.f, 0.f); CameraBoom->bDoCollisionTest = false; // Don't want to pull camera in when it collides with level // Create a camera... TopDownCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("TopDownCamera")); TopDownCameraComponent->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); TopDownCameraComponent->bUsePawnControlRotation = false; // Camera does not rotate relative to arm // Create a decal in the world to show the cursor's location CursorToWorld = CreateDefaultSubobject<UDecalComponent>("CursorToWorld"); CursorToWorld->SetupAttachment(RootComponent); static ConstructorHelpers::FObjectFinder<UMaterial> DecalMaterialAsset(TEXT("Material'/Game/TopDownCPP/Blueprints/M_Cursor_Decal.M_Cursor_Decal'")); if (DecalMaterialAsset.Succeeded()) { CursorToWorld->SetDecalMaterial(DecalMaterialAsset.Object); } CursorToWorld->DecalSize = FVector(16.0f, 32.0f, 32.0f); CursorToWorld->SetRelativeRotation(FRotator(90.0f, 0.0f, 0.0f).Quaternion()); // Activate ticking in order to update the cursor every frame. PrimaryActorTick.bCanEverTick = true; PrimaryActorTick.bStartWithTickEnabled = true; } void ADlgExampleCharacter::Tick(float DeltaSeconds) { Super::Tick(DeltaSeconds); if (CursorToWorld != nullptr) { if (UHeadMountedDisplayFunctionLibrary::IsHeadMountedDisplayEnabled()) { if (UWorld* World = GetWorld()) { FHitResult HitResult; FCollisionQueryParams Params(NAME_None, FCollisionQueryParams::GetUnknownStatId()); FVector StartLocation = TopDownCameraComponent->GetComponentLocation(); FVector EndLocation = TopDownCameraComponent->GetComponentRotation().Vector() * 2000.0f; Params.AddIgnoredActor(this); World->LineTraceSingleByChannel(HitResult, StartLocation, EndLocation, ECC_Visibility, Params); FQuat SurfaceRotation = HitResult.ImpactNormal.ToOrientationRotator().Quaternion(); CursorToWorld->SetWorldLocationAndRotation(HitResult.Location, SurfaceRotation); } } else if (APlayerController* PC = Cast<APlayerController>(GetController())) { FHitResult TraceHitResult; PC->GetHitResultUnderCursor(ECC_Visibility, true, TraceHitResult); FVector CursorFV = TraceHitResult.ImpactNormal; FRotator CursorR = CursorFV.Rotation(); CursorToWorld->SetWorldLocation(TraceHitResult.Location); CursorToWorld->SetWorldRotation(CursorR); } } } bool ADlgExampleCharacter::ModifyIntValue_Implementation(const FName& ValueName, bool bDelta, int32 Value) { if (!DlgData.Integers.Contains(ValueName)) DlgData.Integers.Add(ValueName, 0); if (bDelta) DlgData.Integers[ValueName] += Value; else DlgData.Integers[ValueName] = Value; return true; } bool ADlgExampleCharacter::ModifyFloatValue_Implementation(const FName& ValueName, bool bDelta, float Value) { if (!DlgData.Floats.Contains(ValueName)) DlgData.Floats.Add(ValueName, 0.0f); if (bDelta) DlgData.Floats[ValueName] += Value; else DlgData.Floats[ValueName] = Value; return true; } bool ADlgExampleCharacter::ModifyBoolValue_Implementation(const FName& ValueName, bool bValue) { if (bValue) DlgData.TrueBools.Add(ValueName); else DlgData.TrueBools.Remove(ValueName); return true; } bool ADlgExampleCharacter::ModifyNameValue_Implementation(const FName& ValueName, const FName& NameValue) { if (DlgData.Names.Contains(ValueName)) DlgData.Names[ValueName] = NameValue; else DlgData.Names.Add(ValueName, NameValue); return true; } float ADlgExampleCharacter::GetFloatValue_Implementation(const FName& ValueName) const { return DlgData.Floats.Contains(ValueName) ? DlgData.Floats[ValueName] : 0.0f; } int32 ADlgExampleCharacter::GetIntValue_Implementation(const FName& ValueName) const { return DlgData.Integers.Contains(ValueName) ? DlgData.Integers[ValueName] : 0; } bool ADlgExampleCharacter::GetBoolValue_Implementation(const FName& ValueName) const { return DlgData.TrueBools.Contains(ValueName); } FName ADlgExampleCharacter::GetNameValue_Implementation(const FName& ValueName) const { return DlgData.Names.Contains(ValueName) ? DlgData.Names[ValueName] : NAME_None; }
aff59d48e853178622271d79ca86fa53b11d9911
87c2049cc153d5aaa0215f740072553f7476a22c
/geometry/Arc.cpp
5c3ecb7ab450dd034fda6299ba9653e95c4417e5
[]
no_license
Freel-Freel/qt_client
792af03327a949473e7820e5ac08e019490212da
27b7dbf1922bd30d59e3d2e356ac7244143c74aa
refs/heads/master
2020-03-27T03:26:56.694036
2018-08-24T09:26:27
2018-08-24T09:26:27
145,863,202
0
0
null
2018-08-23T14:13:41
2018-08-23T14:13:40
null
UTF-8
C++
false
false
5,350
cpp
/* * Arc.cpp * * Created on: 28.06.2018 * Author: walery */ #include "Arc.h" #include <QtGlobal> #include <QDebug> #include "mLine.h" #include <cmath> #include "myglobal.h" Arc::Arc() { // TODO Auto-generated constructor stub this->radious = qQNaN(); this->precicion = qQNaN(); this->points_number = NULL; clockWise = true; alfa = 0.0; angle = 0.0; beta_start = 0.0; } Arc::~Arc() { // TODO Auto-generated destructor stub } void Arc::setStart(QString x, QString y) { bool ok; x.replace(',','.'); start.x = x.toDouble(&ok); Q_ASSERT(ok); y.replace(',','.'); start.y = y.toDouble(&ok); Q_ASSERT(ok); } void Arc::setEnd(QString x, QString y) { bool ok; end.x = x.toDouble(&ok); Q_ASSERT(ok); end.y = y.toDouble(&ok); Q_ASSERT(ok); } void Arc::setCenter(QString x, QString y) { bool ok; center.x = x.toDouble(&ok); Q_ASSERT(ok); center.y = y.toDouble(&ok); Q_ASSERT(ok); mLine* line = new mLine(); this->radious = line->getLength(); } void Arc::setRadious(QString r) { bool ok; radious = r.toDouble(&ok); Q_ASSERT(ok); } void Arc::calculateRadious() { radious = sqrt(pow((start.x-center.x),2.0)+pow((start.y - center.y),2.0)); calculateAlfa(); } Point Arc::getPoint(size_t number) { // TODOH Arc::getPoint Point result; /* //= D$5-ACOS((B$5-$B$3+B$7*A11)/B$4) // double_t alfa1 = beta_start - acos((start.x-center.x+precicion*number)/radious); Point alphaP = new Point(start.x + precicion*number,start.y); double_t alfa1 = beta_start - calcAngleX(alphaP,center); double_t beta1 = beta_start - asin((start.y - center.y+precicion*number)/radious); */ result.x = center.x + radious*cos(beta_start - number*alfa); result.y = center.y + radious*sin(beta_start - number*alfa); return (result); } void Arc::setPrecicion(QString pstr) { bool ok; QString tstr = pstr.replace(',','.'); double_t precicion = tstr.toDouble(&ok); Q_ASSERT(ok); setPrecicion(precicion); } void Arc::setPrecicion(double_t precicion) { this->precicion = precicion; alfa = 2.0*asin(precicion/(2.0*radious)); } //TODO calculate void Arc::calculate() { if((std::isnan(radious)) || (radious == 0)){ calculateRadious(); } Q_ASSERT((radious != qQNaN()) && (radious != 0) ); #define I9 beta_startAbs #define I10 beta_endAbs //#define H3 vector // start point angle Point startdistance = start - center; double_t beta_startAbs = atan(startdistance.y/startdistance.x);// I9 double_t beta_startY = asin(startdistance.y/radious); // assert(enddistance.x <= radious); // Q_ASSERT(enddistance.x <= radious); Point enddistance = end - center; double_t beta_endAbs = atan(enddistance.y/enddistance.x); double_t beta_endY = acos(enddistance.y/radious); //, beta_end; double_t K9; double_t K10; //Start uint8_t H3 = 0; if(startdistance.x < 0) H3 |= 0x01; if(startdistance.y < 0) H3 |= 0x02; //=IF(H3=1;180+I9;IF(H3=0 AND(G3=0);I9;IF(H3=0 AND(G3=1);360+I9;0))) // End uint8_t H4 = 0; if(enddistance.x < 0) H4 |= 0x01; if(enddistance.y < 0) H4 |= 0x02; //=IF(H5=1;180+I10;IF(H5=0 AND(G5=0);I10;IF(H5=0 AND(G5=1);360+I10;0))) if(H3 & 0x01){ K9 = MyGlobal::PI + I9; }else if(H3 == 0){ K9 = I9; }else if(H3 == 2){ K9 = 2.0*MyGlobal::PI + I9; } beta_start = K9; if(H4 & 0x01){ K10 = MyGlobal::PI + I10; }else if(H4 == 0){ K10 = I10; }else if(H4 == 2){ K10 = 2.0*MyGlobal::PI + I10; } beta_end = K10; double_t L10 = beta_start-beta_end; if(clockWise){ //=IF((K9-K10)<0;360+(K9-K10);(K9-K10)) if(L10 < 0){ angle = 2.0*MyGlobal::PI + L10; }else{ angle = L10; } }else{ //=IF(L30>0;360-L30;L30) if(L10>0){ angle = 2.0*MyGlobal::PI - L10; }else{ angle = L10; } } angle = fabs(angle); // angle = std::abs(beta_start - beta_end); assert(alfa!=0); points_number = std::floor( angle/alfa); /* qDebug()<<"Arc[164] beta_start:"<<MyGlobal::DEGREES(beta_start)<< "\tbeta_end:"<<MyGlobal::DEGREES(beta_end)<<"\tangle:" <<MyGlobal::DEGREES(angle)<<"\tpoints_number:"<<points_number ; */ } double_t Arc::calcAngleX(Point &start, Point &center) { double_t angle; Q_ASSERT((radious != qQNaN()) && (radious != 0) ); Point startdistance = start - center; angle = acos(startdistance.x/radious); uint8_t vector = 0; if(startdistance.x < 0) vector |= 0x01; if(startdistance.y < 0) vector |= 0x02; switch(vector) { case 0: case 1: beta_start = angle; break; case 2: case 3: beta_start = 2.0*MyGlobal::PI - angle; break; } return angle; } double_t Arc::calcAngleY(Point &start, Point &center) { double_t angle; Q_ASSERT((radious != qQNaN()) && (radious != 0) ); Point startdistance = start - center; angle = asin(startdistance.y/radious); uint8_t vector = 0; if(startdistance.x < 0) vector |= 0x01; if(startdistance.y < 0) vector |= 0x02; switch(vector) { case 0: case 1: beta_start = angle; break; case 2: case 3: beta_start = 2.0*MyGlobal::PI - angle; break; } return angle; } void Arc::calculateAlfa() { Q_ASSERT((radious != qQNaN()) && (radious != 0) ); // Угол сегмента alfa = 2.0 * asin(precicion/(radious*2.0)); //=SQRT(SUMSQ(B3-B4;C3-C4)) // double_t chorda = 0; // mLine* dl = new mLine(start,end); // chorda = dl->getLength(); // angle = 2.0 * asin(chorda/(radious*2.0));//TODOH angle +180 }
70cf3d356338ee2c5c572a36bbb2b393520eb6ae
b24f54b0ae44cb2221b17f489441649e4b775811
/PSME/application/src/rest/endpoints/system/system_reset.cpp
2fbc9114c97a7fbd50c604375fc1047d6d3146cc
[]
no_license
gaberger/intelRSD
7e42ee0f077328d4499390f9ad83b5017db99b66
8d28936751f46a5396487ba52e699912bf633bdf
refs/heads/master
2021-05-06T06:39:49.763448
2017-12-05T10:46:06
2017-12-05T10:46:06
null
0
0
null
null
null
null
UTF-8
C++
false
false
3,356
cpp
/*! * @copyright * Copyright (c) 2015-2017 Intel Corporation * * @copyright * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * @copyright * http://www.apache.org/licenses/LICENSE-2.0 * * @copyright * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ #include "agent-framework/module/requests/common.hpp" #include "agent-framework/module/responses/common.hpp" #include "agent-framework/module/constants/compute.hpp" #include "psme/core/agent/agent_manager.hpp" #include "psme/rest/constants/constants.hpp" #include "psme/rest/validators/json_validator.hpp" #include "psme/rest/validators/schemas/reset.hpp" #include "psme/rest/endpoints/system/system_reset.hpp" #include "psme/rest/server/error/error_factory.hpp" #include "psme/rest/model/handlers/generic_handler_deps.hpp" #include "psme/rest/model/handlers/generic_handler.hpp" using namespace psme::rest; using namespace psme::rest::constants; using namespace psme::rest::validators; endpoint::SystemReset::SystemReset(const std::string& path) : EndpointBase(path) {} endpoint::SystemReset::~SystemReset() {} void endpoint::SystemReset::post(const server::Request& request, server::Response& response) { std::string agent_id{}; std::string system_uuid{}; std::string parent_uuid{}; // Gets necessary data from model and does not block system reference auto system = model::Find<agent_framework::model::System>(request.params[PathParam::SYSTEM_ID]).get(); agent_id = system.get_agent_id(); system_uuid = system.get_uuid(); parent_uuid = system.get_parent_uuid(); const auto& json = JsonValidator::validate_request_body<schema::ResetPostSchema>(request); agent_framework::model::attribute::Attributes attributes{}; if (json.is_member(constants::Common::RESET_TYPE)) { const auto& reset_type = json[constants::Common::RESET_TYPE].as_string(); attributes.set_value(agent_framework::model::literals::System::POWER_STATE, reset_type); } if (!attributes.empty()) { agent_framework::model::requests::SetComponentAttributes set_component_attributes_request{system_uuid, attributes}; const auto& gami_agent = psme::core::agent::AgentManager::get_instance()->get_agent(agent_id); auto set_system_attributes = [&, gami_agent] { // Call set component attribute method gami_agent->execute<agent_framework::model::responses::SetComponentAttributes>( set_component_attributes_request); psme::rest::model::handler::HandlerManager::get_instance()->get_handler( agent_framework::model::enums::Component::System)-> load(gami_agent, parent_uuid, agent_framework::model::enums::Component::Manager, system_uuid, false); }; gami_agent->execute_in_transaction(set_system_attributes); } response.set_status(server::status_2XX::NO_CONTENT); }
cafae6294d51336c41dea48279eb670dc000f066
3f75df57ae155e3eaada2885b12b78a63bbc43a1
/source/Geometry/Tesla/include/BeamCal01.hh
2881172d75ebaf525d6baef38211d1c9c4035298
[]
no_license
nkxuyin/mokka-cepc
52bb13455b6fc5961de678ad7cb695f754e49a47
61ce9f792a4cb8883f0d1cd1391884444b372dc0
refs/heads/master
2021-01-20T10:42:00.982704
2015-02-11T12:59:43
2015-02-11T12:59:43
24,243,983
0
1
null
null
null
null
UTF-8
C++
false
false
1,336
hh
#ifndef BeamCal01_hh #define BeamCal01_hh 1 /* * Second implementation of BeamCal detector based on the First implementation * A.Sailer * Apr. 2010 */ class G4LogicalVolume; class Database; #include "VSubDetectorDriver.hh" #include "G4Material.hh" #include <map> class BeamCal01 : public VSubDetectorDriver { public: BeamCal01(void): VSubDetectorDriver("BeamCal01", "BeamCal") {} ~BeamCal01(void) {} G4bool ContextualConstruct(const CGAGeometryEnvironment &env, G4LogicalVolume *worldLog); private: G4double tiltForw, tiltBack; G4double SegmdR; G4double SegmnRs; // G4double SegmdPhi[256]; // G4double SegmdPhiDA[256]; // G4double SegmnPhis[256]; // G4double SegmnPhisDA[256]; //std::vector<double> SegmdPhi, SegmdPhiDA, SegmnPhis, SegmnPhisDA; G4double dR, r, DArinner, DAStart; G4int nRs; G4double zCenter; G4ThreeVector posBack, posForw; G4double dLayer, length; G4Material *materialAir; G4Material *materialDiamond; G4Material *materialTungsten; G4Material *materialGold; G4Material *materialKapton; G4Material *materialGraphite; G4Material *materialSilicon; G4double posSens, pos_sensBack; G4double posAbs, pos_absBack; G4double posElectrode, pos_electrodeBack; G4double posPCB, pcbBack; typedef std::map<G4String, G4double> BeamCalValueMap; }; #endif
cce7efc1d2216c209717c97cfbf12bf2c70e0a0c
f54042e6c7c1b1fcb841f1d69aa8a363013855cc
/299 - Train Swaping.cpp
b8402117dd470e40cd9944eaf1ef7c8ea987ab26
[]
no_license
Shadikul-Islam/UVA
33959e9f9246f2161a493ba09615c8d4f1543d5c
fb2a6681978dd3faca9930c812e368b20c7ffc22
refs/heads/master
2021-09-24T12:02:53.548952
2018-10-09T17:27:49
2018-10-09T17:27:49
null
0
0
null
null
null
null
UTF-8
C++
false
false
535
cpp
#include<bits/stdc++.h> using namespace std; int main () { int arr[1001],i,j,temp,k,n,m,x,l; cin>>n; for(x=1;x<=n;x++) { cin>>l; for(m=0; m<l; m++) { cin>>arr[m]; } k=0; for(i=0; i<l; i++) { for(j=i; j<l-1; j++) { if(arr[i]>arr[j+1]) { k++; } } } cout<<"Optimal train swapping takes "<<k<<" swaps."<<endl; } return 0; }