Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
Move algorithm parameter tables to be global
Browse files Browse the repository at this point in the history
Also fix copy-and-paste naming error for signature table
  • Loading branch information
daviddrysdale committed Nov 12, 2014
1 parent 36dea03 commit 1946b6c
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 65 deletions.
21 changes: 1 addition & 20 deletions cipher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,13 @@ namespace test {

namespace {

struct CipherInfo {
CK_KEY_TYPE keytype;
CK_MECHANISM_TYPE keygen;
CK_MECHANISM_TYPE mode;
int blocksize;
bool has_iv;
int keylen;
};

map<string, CipherInfo> kCipherInfo = {
{"DES-ECB", {CKK_DES, CKM_DES_KEY_GEN, CKM_DES_ECB, 8, false, -1}},
{"DES-CBC", {CKK_DES, CKM_DES_KEY_GEN, CKM_DES_CBC, 8, true, -1}},
{"3DES-ECB", {CKK_DES3, CKM_DES3_KEY_GEN, CKM_DES3_ECB, 8, false, -1}},
{"3DES-CBC", {CKK_DES3, CKM_DES3_KEY_GEN, CKM_DES3_CBC, 8, true, -1}},
{"IDEA-ECB", {CKK_IDEA, CKM_IDEA_KEY_GEN, CKM_IDEA_ECB, 8, false, -1}},
{"IDEA-CBC", {CKK_IDEA, CKM_IDEA_KEY_GEN, CKM_IDEA_CBC, 8, true, -1}},
{"AES-ECB", {CKK_AES, CKM_AES_KEY_GEN, CKM_AES_ECB, 16, false, 16}},
{"AES-CBC", {CKK_AES, CKM_AES_KEY_GEN, CKM_AES_CBC, 16, true, 16}},
};

struct TestData {
string key; // Hex
string iv; // Hex
string plaintext; // Hex
string ciphertext; // Hex
};

map<string, vector<TestData> > kTestVectors = {
{ "DES-ECB", {{"8000000000000000", "", "0000000000000000", "95A8D72813DAA94D"},
{"4000000000000000", "", "0000000000000000", "0EEC1487DD8C26D5"}, }},
Expand Down
12 changes: 0 additions & 12 deletions digest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,6 @@ namespace pkcs11 {
namespace test {

namespace {
struct DigestInfo {
CK_MECHANISM_TYPE type;
int size;
};

map<string, DigestInfo> kDigestInfo = {
{"MD5", {CKM_MD5, 16}},
{"SHA-1", {CKM_SHA_1, 20}},
{"SHA-256", {CKM_SHA256, 256/8}},
{"SHA-384", {CKM_SHA384, 384/8}},
{"SHA-512", {CKM_SHA512, 512/8}},
};

struct TestData {
string input; // UTF-8
Expand Down
38 changes: 38 additions & 0 deletions globals.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,44 @@ const char* g_reset_user_pin = "12345678";
const char* g_so_pin = "sososo";
const char* g_reset_so_pin = "87654321";

// Algorithm information.
std::map<std::string, HmacInfo> kHmacInfo = {
{"MD5-HMAC", {CKM_MD5_HMAC, 16}},
{"SHA1-HMAC", {CKM_SHA_1_HMAC, 20}},
{"SHA256-HMAC", {CKM_SHA256_HMAC, 256/8}},
{"SHA384-HMAC", {CKM_SHA384_HMAC, 384/8}},
{"SHA512-HMAC", {CKM_SHA512_HMAC, 512/8}},
};

std::map<std::string, SignatureInfo> kSignatureInfo = {
// CKM_RSA_PKCS has restrictions on data sizes (see PKCS#11 s12.1.6 table 37).
{"RSA", {CKM_RSA_PKCS, 64}},
{"MD5-RSA", {CKM_MD5_RSA_PKCS, 1024}},
{"SHA1-RSA", {CKM_SHA1_RSA_PKCS, 1024}},
{"SHA256-RSA", {CKM_SHA256_RSA_PKCS, 1024}},
{"SHA384-RSA", {CKM_SHA384_RSA_PKCS, 1024}},
{"SHA512-RSA", {CKM_SHA512_RSA_PKCS, 1024}},
};

std::map<std::string, CipherInfo> kCipherInfo = {
{"DES-ECB", {CKK_DES, CKM_DES_KEY_GEN, CKM_DES_ECB, 8, false, -1}},
{"DES-CBC", {CKK_DES, CKM_DES_KEY_GEN, CKM_DES_CBC, 8, true, -1}},
{"3DES-ECB", {CKK_DES3, CKM_DES3_KEY_GEN, CKM_DES3_ECB, 8, false, -1}},
{"3DES-CBC", {CKK_DES3, CKM_DES3_KEY_GEN, CKM_DES3_CBC, 8, true, -1}},
{"IDEA-ECB", {CKK_IDEA, CKM_IDEA_KEY_GEN, CKM_IDEA_ECB, 8, false, -1}},
{"IDEA-CBC", {CKK_IDEA, CKM_IDEA_KEY_GEN, CKM_IDEA_CBC, 8, true, -1}},
{"AES-ECB", {CKK_AES, CKM_AES_KEY_GEN, CKM_AES_ECB, 16, false, 16}},
{"AES-CBC", {CKK_AES, CKM_AES_KEY_GEN, CKM_AES_CBC, 16, true, 16}},
};

std::map<std::string, DigestInfo> kDigestInfo = {
{"MD5", {CKM_MD5, 16}},
{"SHA-1", {CKM_SHA_1, 20}},
{"SHA-256", {CKM_SHA256, 256/8}},
{"SHA-384", {CKM_SHA384, 384/8}},
{"SHA-512", {CKM_SHA512, 512/8}},
};

// PKCS#11 s12 table 34: Mechanisms vs. Functions
std::set<CK_MECHANISM_TYPE> encrypt_decrypt_mechanisms = {
CKM_RSA_PKCS,
Expand Down
31 changes: 31 additions & 0 deletions globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <pkcs11.h>

#include <set>
#include <map>

namespace pkcs11 {
namespace test {
Expand Down Expand Up @@ -47,6 +48,36 @@ extern const char* g_reset_user_pin;
// Security Officer PIN after token reset. Only used if (g_token_flags & CKF_LOGIN_REQUIRED).
extern const char* g_reset_so_pin;

// Algorithm information. These tables are effectively const, but not marked as
// const so operator[] can be used for convenience.
struct HmacInfo {
CK_MECHANISM_TYPE hmac;
CK_ULONG mac_size;
};
extern std::map<std::string, HmacInfo> kHmacInfo;

struct SignatureInfo {
CK_MECHANISM_TYPE alg;
int max_data;
};
extern std::map<std::string, SignatureInfo> kSignatureInfo;

struct CipherInfo {
CK_KEY_TYPE keytype;
CK_MECHANISM_TYPE keygen;
CK_MECHANISM_TYPE mode;
int blocksize;
bool has_iv;
int keylen;
};
extern std::map<std::string, CipherInfo> kCipherInfo;

struct DigestInfo {
CK_MECHANISM_TYPE type;
int size;
};
extern std::map<std::string, DigestInfo> kDigestInfo;

// PKCS#11 mechanisms for encrypt/decrypt.
extern std::set<CK_MECHANISM_TYPE> encrypt_decrypt_mechanisms;
// PKCS#11 mechanisms for sign/verify.
Expand Down
13 changes: 0 additions & 13 deletions hmac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,6 @@ namespace test {

namespace {

struct HmacInfo {
CK_MECHANISM_TYPE hmac;
CK_ULONG mac_size;
};

map<string, HmacInfo> kHmacInfo = {
{"MD5-HMAC", {CKM_MD5_HMAC, 16}},
{"SHA1-HMAC", {CKM_SHA_1_HMAC, 20}},
{"SHA256-HMAC", {CKM_SHA256_HMAC, 256/8}},
{"SHA384-HMAC", {CKM_SHA384_HMAC, 384/8}},
{"SHA512-HMAC", {CKM_SHA512_HMAC, 512/8}},
};

struct TestData {
string key; // Hex
string data; // Hex
Expand Down
23 changes: 3 additions & 20 deletions sign.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,44 +28,27 @@
// C_VerifyRecover
#include "pkcs11test.h"

#include <map>

using namespace std; // So sue me

namespace pkcs11 {
namespace test {

namespace {

struct HmacInfo {
CK_MECHANISM_TYPE hmac;
int max_data;
};

map<string, HmacInfo> kHmacInfo = {
// CKM_RSA_PKCS has restrictions on data sizes (see PKCS#11 s12.1.6 table 37).
{"RSA", {CKM_RSA_PKCS, 64}},
{"MD5-RSA", {CKM_MD5_RSA_PKCS, 1024}},
{"SHA1-RSA", {CKM_SHA1_RSA_PKCS, 1024}},
{"SHA256-RSA", {CKM_SHA256_RSA_PKCS, 1024}},
{"SHA384-RSA", {CKM_SHA384_RSA_PKCS, 1024}},
{"SHA512-RSA", {CKM_SHA512_RSA_PKCS, 1024}},
};

class SignTest : public ReadOnlySessionTest,
public ::testing::WithParamInterface<string> {
public:
SignTest()
: info_(kHmacInfo[GetParam()]),
: info_(kSignatureInfo[GetParam()]),
public_attrs_({CKA_VERIFY}),
private_attrs_({CKA_SIGN}),
keypair_(session_, public_attrs_, private_attrs_),
datalen_(std::rand() % info_.max_data),
data_(randmalloc(datalen_)),
mechanism_({info_.hmac, NULL_PTR, 0}) {
mechanism_({info_.alg, NULL_PTR, 0}) {
}
protected:
HmacInfo info_;
SignatureInfo info_;
vector<CK_ATTRIBUTE_TYPE> public_attrs_;
vector<CK_ATTRIBUTE_TYPE> private_attrs_;
KeyPair keypair_;
Expand Down

0 comments on commit 1946b6c

Please sign in to comment.