-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
121 lines (100 loc) · 3.08 KB
/
Copy pathmain.cpp
File metadata and controls
121 lines (100 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "binIO.h"
#include "RippaSSL/error.h"
#include "RippaSSL/Base.h"
#include "RippaSSL/Mac.h"
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cstdarg>
#include <vector>
#include <ios>
#include <iostream>
#include <sstream>
#include <openssl/evp.h>
#include <openssl/params.h>
#define SEOS2_CMAC_LEN 16
int main(int argc, char* argv[])
{
std::vector<uint8_t> iv;
std::vector<uint8_t> key;
RippaSSL::Algo algo;
int msgIdx;
if (argc != 4)
{
printf("Usage: binmac MODE KEY MESSAGE\n"
" The key shall be provided without spaces. The same applies"
" to the message and IV.\n"
" An example usage:\n"
" $ ./binmac AES128CBC 000102030405060708090A0B0C0D0E0F "
"000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D"
"0E0F\n");
printf("argc: %d\n", argc);
return 1;
}
{
char* myMode = argv[1];
if (!strcmp(myMode, "AES128CBC"))
{
algo = RippaSSL::Algo::AES128CBC;
}
else if(!strcmp(myMode, "AES256CBC"))
{
algo = RippaSSL::Algo::AES256CBC;
}
else
{
printf("Check your MODE input!\nPossible values are:\n"
" AES128CBC, AES256CBC\n");
return 1;
}
}
BinIO::readHexBinary(key, argv[2]);
if ((16 != key.size()) && (32 != key.size()))
{
printf("Wrong key length: %lu!\n", key.size());
return 1;
}
// the message is the last argument:
msgIdx = argc - 1;
// reads the input message and places it into buf:
std::vector<uint8_t> msgVector;
BinIO::readHexBinary(msgVector, argv[msgIdx]);
if (msgVector.size() < SEOS2_CMAC_LEN) {
printf("Error! Your message is way too short. Please pad your data.\n");
return 1;
}
std::vector<uint8_t> outputDigest;
// optionally prints the input message:
//for (size_t i = 0; i < msgVector.size(); ++i)
//{
// std::cout << static_cast<int>(msgVector[i]) << " ";
//}
//std::cout << std::endl;
// creates the relevant object:
try {
RippaSSL::Cmac myCmac {algo, RippaSSL::MacMode::CMAC, key, NULL};
myCmac.finalize(outputDigest, msgVector);
}
catch (RippaSSL::InputError_NULLPTR& nullPtr) {
printf("Error! The key pointer is invalid, or something nasty happened"
" while calling OpenSSL's EVP_CIPHER_CTX_new()!\n");
return 1;
}
catch (RippaSSL::OpenSSLError_CryptoInit& ci) {
printf("Error! OpenSSL failed to call its Init method!\n");
return 1;
}
catch (RippaSSL::OpenSSLError_CryptoUpdate& cu) {
printf("Error! OpenSSL failed to call its Update method!\n");
return 1;
}
catch (RippaSSL::OpenSSLError_CryptoFinalize& cf) {
printf("Error: OpenSSL failed to call its Finalize method!\n");
return 1;
}
// prints the result:
outputDigest.resize(SEOS2_CMAC_LEN);
printf("Result: ");
BinIO::printHexBinary(outputDigest);
return 0;
}