-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstl.cpp
More file actions
71 lines (61 loc) · 2.19 KB
/
stl.cpp
File metadata and controls
71 lines (61 loc) · 2.19 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
#include <memory>
#include <stdexcept>
#include "api.h"
//
// This defines the version of sign that avoids a potential mishap with memory
// (either accidental memory overwrite or memory leak)
// It returns a unique_ptr, which we allocate (and hence we know will be long
// enough), and has a built in destructor (which will free the array when the
// caller is done with it)
namespace slh_dsa {
std::unique_ptr<unsigned char[]> key::sign(
const unsigned char *message, size_t len_message,
const void *context, size_t len_context,
const random& rand) {
size_t sig_len = len_signature();
std::unique_ptr<unsigned char[]>signature( new unsigned char[sig_len] );
sign_flag flag = sign_internal(signature.get(), sig_len,
0x00, // Not prehashed
context, len_context,
0, 0, // No hash OID
message, len_message, rand);
switch (flag) {
case sign_success:
return signature;
case sign_no_private_key:
throw std::runtime_error( "no SLH-DSA private key" );
case sign_bad_context_len:
throw std::runtime_error( "bad context" );
default:
throw std::runtime_error( "unknown error" );
}
}
// And the prehash version
std::unique_ptr<unsigned char[]> key::sign(
const unsigned char *message, size_t len_message,
const hash_type& hash,
const void *context, size_t len_context,
const random& rand) {
if (len_message != hash.length) {
// We insist that the length of the hash be what we expect
throw std::runtime_error( "bad hash length" );
}
size_t sig_len = len_signature();
std::unique_ptr<unsigned char[]>signature( new unsigned char[sig_len] );
sign_flag flag = sign_internal(signature.get(), sig_len,
0x01, // Prehashed
context, len_context,
hash.oid, hash.oid_length, // The hash OID
message, len_message, rand);
switch (flag) {
case sign_success:
return signature;
case sign_no_private_key:
throw std::runtime_error( "no SLH-DSA private key" );
case sign_bad_context_len:
throw std::runtime_error( "bad context" );
default:
throw std::runtime_error( "unknown error" );
}
}
} /* namespace slh_dsa */