first successful encrypt

master
Marc Wäckerlin 15 years ago
parent 9f09371c4b
commit 7e38f050f7
  1. 2
      COPYING
  2. 2
      INSTALL
  3. 100
      src/cryptoki.hxx

@ -1 +1 @@
/opt/local/share/automake-1.11/COPYING
/usr/share/automake-1.11/COPYING

@ -1 +1 @@
/opt/local/share/automake-1.11/INSTALL
/usr/share/automake-1.11/INSTALL

@ -35,6 +35,10 @@
/*! @defgroup cryptokitypes Cryptoki C++ Types and Auxiliary */
/*! @defgroup cryptokiexceptions Cryptoki Exceptions */
#ifndef CRYPTOKI_LOG
#define CRYPTOKI_LOG(X)
#endif
#ifndef CRYPTOKI_FN_LOG
#include <iostream>
#if __GNUC__ >= 2
@ -287,7 +291,7 @@ namespace cryptoki {
operator CK_ATTRIBUTE() const {
CK_ATTRIBUTE a;
a.type = type;
a.pValue = const_cast<char*>(value.begin().operator->());
a.pValue = const_cast<char*>(&value[0]);
a.ulValueLen = value.size();
return a;
}
@ -360,7 +364,7 @@ namespace cryptoki {
std::string readableValue() const {
switch (type) {
case CKA_CLASS:
switch (*((CK_OBJECT_CLASS*)value.begin().operator->())) {
switch (*((CK_OBJECT_CLASS*)&value[0])) {
case CKO_DATA: return "DATA";
case CKO_CERTIFICATE: return "CERTIFICATE";
case CKO_PUBLIC_KEY: return "PUBLIC_KEY";
@ -380,7 +384,7 @@ namespace cryptoki {
}
template<typename TYPE> TYPE to() const {
assert(sizeof(TYPE)==value.size());
return *reinterpret_cast<const TYPE*>(value.begin().operator->());
return *reinterpret_cast<const TYPE*>(&value[0]);
}
CK_ATTRIBUTE_TYPE type;
std::string value;
@ -963,8 +967,8 @@ namespace cryptoki {
//! calls @c C_InitToken
return check(_init->_fn->C_InitToken
(_slot,
(unsigned char*)pin.begin().operator->(), pin.size(),
(unsigned char*)label.fix().begin().operator->())
(unsigned char*)&pin[0], pin.size(),
(unsigned char*)label.&fix()[0])
CRYPTOKI_FN_LOG("C_InitToken"));
}
@endcode */
@ -1130,8 +1134,8 @@ namespace cryptoki {
//! calls @c C_Digest
check(_slot._init->_fn->C_Digest
(_session,
(unsigned char*)in.begin().operator->(), in.size(),
(unsigned char*)res.begin().operator->(), &size),
(unsigned char*)&in[0], in.size(),
(unsigned char*)&res[0], &size),
CRYPTOKI_FN_LOG("C_Digest"));
res.resize(size);
return res;
@ -1145,8 +1149,8 @@ namespace cryptoki {
//! calls @c C_DigestEncryptUpdate
check(_slot._init->_fn->C_DigestEncryptUpdate
(_session,
(unsigned char*)in.begin().operator->(), in.size(),
(unsigned char*)res.begin().operator->(), &size),
(unsigned char*)&in[0], in.size(),
(unsigned char*)&res[0], &size),
CRYPTOKI_FN_LOG("C_DigestEncryptUpdate"));
res.resize(size);
return res;
@ -1334,8 +1338,8 @@ namespace cryptoki {
//! calls @c C_Sign
check(_slot._init->_fn->C_Sign
(_session,
(unsigned char*)in.begin().operator->(), in.size(),
(unsigned char*)res.begin().operator->(), &size),
(unsigned char*)&in[0], in.size(),
(unsigned char*)&res[0], &size),
CRYPTOKI_FN_LOG("C_Sign"));
res.resize(size);
return res;
@ -1349,8 +1353,8 @@ namespace cryptoki {
//! calls @c C_SignEncryptUpdate
check(_slot._init->_fn->C_SignEncryptUpdate
(_session,
(unsigned char*)in.begin().operator->(), in.size(),
(unsigned char*)res.begin().operator->(), &size),
(unsigned char*)&in[0], in.size(),
(unsigned char*)&res[0], &size),
CRYPTOKI_FN_LOG("C_SignEncryptUpdate"));
res.resize(size);
return res;
@ -1374,8 +1378,8 @@ namespace cryptoki {
//! calls @c C_SignRecover
check(_slot._init->_fn->C_SignRecover
(_session,
(unsigned char*)in.begin().operator->(), in.size(),
(unsigned char*)res.begin().operator->(), &size),
(unsigned char*)&in[0], in.size(),
(unsigned char*)&res[0], &size),
CRYPTOKI_FN_LOG("C_SignRecover"));
res.resize(size);
return res;
@ -1420,8 +1424,8 @@ namespace cryptoki {
//! calls @c C_VerifyRecover
check(_slot._init->_fn->C_VerifyRecover
(_session,
(unsigned char*)in.begin().operator->(), in.size(),
(unsigned char*)res.begin().operator->(), &size),
(unsigned char*)&in[0], in.size(),
(unsigned char*)&res[0], &size),
CRYPTOKI_FN_LOG("C_VerifyRecover"));
res.resize(size);
return res;
@ -1485,8 +1489,18 @@ namespace cryptoki {
Functions. They provide a higher level simpler access. */
//@{
std::string decrypt(std::string data, CK_MECHANISM_TYPE type,
std::string param=std::string()) {
std::string encrypt(const std::string& data, CK_MECHANISM_TYPE type,
const std::string& param=std::string()) {
CRYPTOKI_LOG("log");
CRYPTOKI_LOG("encryptinit");
encryptinit(type, param);
CRYPTOKI_LOG("encrypt");
return encrypt(data);
//! @todo don't call encryptfinal()?
}
std::string decrypt(const std::string& data, CK_MECHANISM_TYPE type,
const std::string& param=std::string()) {
CRYPTOKI_LOG("log");
CRYPTOKI_LOG("decryptinit");
decryptinit(type, param);
@ -1550,7 +1564,7 @@ namespace cryptoki {
}
//! requires decryptinit to be called before
std::string decrypt(std::string in) {
std::string decrypt(const std::string& in) {
CRYPTOKI_LOG("log");
std::string res;
CK_ULONG size(0); // two calls, first to get minimum buffer length
@ -1580,8 +1594,8 @@ namespace cryptoki {
//! calls @c C_DecryptDigestUpdate
check(_session._slot._init->_fn->C_DecryptDigestUpdate
(_session._session,
(unsigned char*)in.begin().operator->(), in.size(),
(unsigned char*)res.begin().operator->(), &size),
(unsigned char*)&in[0], in.size(),
(unsigned char*)&res[0], &size),
CRYPTOKI_FN_LOG("C_DecryptDigestUpdate"));
res.resize(size);
return res;
@ -1604,8 +1618,8 @@ namespace cryptoki {
//! calls @c C_DecryptUpdate
check(_session._slot._init->_fn->C_DecryptUpdate
(_session._session,
(unsigned char*)in.begin().operator->(), in.size(),
(unsigned char*)res.begin().operator->(), &size),
(unsigned char*)&in[0], in.size(),
(unsigned char*)&res[0], &size),
CRYPTOKI_FN_LOG("C_DecryptUpdate"));
res.resize(size);
return res;
@ -1619,8 +1633,8 @@ namespace cryptoki {
//! calls @c C_DecryptVerifyUpdate
check(_session._slot._init->_fn->C_DecryptVerifyUpdate
(_session._session,
(unsigned char*)in.begin().operator->(), in.size(),
(unsigned char*)res.begin().operator->(), &size),
(unsigned char*)&in[0], in.size(),
(unsigned char*)&res[0], &size),
CRYPTOKI_FN_LOG("C_DecryptVerifyUpdate"));
res.resize(size);
return res;
@ -1656,27 +1670,35 @@ namespace cryptoki {
}
@endcode */
/*! @todo Not implemented:
@code
bool encryptinit() {
bool encryptinit(CK_MECHANISM_TYPE type, const std::string& param) {
CRYPTOKI_LOG("log");
CK_MECHANISM mech = {
type, param.size()?(void*)&param[0]:0, param.size()
};
CRYPTOKI_LOG("encryptinit: type="<<type<<"; mech=("<<mech.mechanism
<<", "<<mech.pParameter<<", "<<mech.ulParameterLen<<')');
//! calls @c C_EncryptInit
return check(_session._slot._init->_fn->C_EncryptInit(_session._session, CK_MECHANISM_PTR, CK_OBJECT_HANDLE),
return check(_session._slot._init->_fn->C_EncryptInit
(_session._session, &mech, _object),
CRYPTOKI_FN_LOG("C_EncryptInit"));
}
@endcode */
std::string encrypt(std::string in) {
std::string encrypt(const std::string& in) {
CRYPTOKI_LOG("log");
std::string res;
res.resize(in.size());
CK_ULONG size(res.size()); //! @todo check if size is ok
CK_ULONG size(0); // two calls, first to get minimum buffer length
CRYPTOKI_LOG("get size");
//! calls @c C_Encrypt
check(_session._slot._init->_fn->C_Encrypt
(_session._session,
(unsigned char*)in.begin().operator->(), in.size(),
(unsigned char*)res.begin().operator->(), &size),
(unsigned char*)&in[0], in.size(), 0, &size),
CRYPTOKI_FN_LOG("C_Decrypt"));
CRYPTOKI_LOG("maximum size is "<<size<<"Bytes");
res.resize(size, 0);
check(_session._slot._init->_fn->C_Encrypt
(_session._session,
(unsigned char*)&in[0], in.size(),
(unsigned char*)&res[0], &size),
CRYPTOKI_FN_LOG("C_Encrypt"));
res.resize(size);
return res;
@ -1700,8 +1722,8 @@ namespace cryptoki {
//! calls @c C_EncryptUpdate
check(_session._slot._init->_fn->C_EncryptUpdate
(_session._session,
(unsigned char*)in.begin().operator->(), in.size(),
(unsigned char*)res.begin().operator->(), &size),
(unsigned char*)&in[0], in.size(),
(unsigned char*)&res[0], &size),
CRYPTOKI_FN_LOG("C_EncryptUpdate"));
res.resize(size);
return res;

Loading…
Cancel
Save