-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathowf.c
132 lines (116 loc) · 3.74 KB
/
owf.c
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
121
122
123
124
125
126
127
128
129
130
131
132
/*
* SPDX-License-Identifier: MIT
*/
#if defined(HAVE_CONFIG_H)
#include <config.h>
#endif
#include "owf.h"
#include "aes.h"
#include "utils.h"
#include <string.h>
#if defined(HAVE_OPENSSL)
#include <openssl/evp.h>
#include <assert.h>
#endif
void owf_128(const uint8_t* key, const uint8_t* input, uint8_t* output) {
#if defined(HAVE_OPENSSL)
const EVP_CIPHER* cipher = EVP_aes_128_ecb();
assert(cipher);
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
assert(ctx);
EVP_CIPHER_CTX_set_padding(ctx, 0);
EVP_EncryptInit_ex(ctx, cipher, NULL, key, NULL);
int len = 0;
EVP_EncryptUpdate(ctx, output, &len, input, IV_SIZE);
EVP_EncryptFinal_ex(ctx, output + IV_SIZE, &len);
EVP_CIPHER_CTX_free(ctx);
#else
aes_round_keys_t round_keys;
aes128_init_round_keys(&round_keys, key);
aes128_encrypt_block(&round_keys, input, output);
#endif
}
void owf_192(const uint8_t* key, const uint8_t* input, uint8_t* output) {
#if defined(HAVE_OPENSSL)
const EVP_CIPHER* cipher = EVP_aes_192_ecb();
assert(cipher);
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
assert(ctx);
EVP_CIPHER_CTX_set_padding(ctx, 0);
EVP_EncryptInit_ex(ctx, cipher, NULL, key, NULL);
int len = 0;
EVP_EncryptUpdate(ctx, output, &len, input, IV_SIZE);
uint8_t buf[16];
memcpy(buf, input, sizeof(buf));
buf[0] ^= 0x1;
EVP_EncryptUpdate(ctx, output + IV_SIZE, &len, buf, IV_SIZE);
EVP_EncryptFinal_ex(ctx, output + 2 * IV_SIZE, &len);
EVP_CIPHER_CTX_free(ctx);
#else
aes_round_keys_t round_keys;
aes192_init_round_keys(&round_keys, key);
aes192_encrypt_block(&round_keys, input, output);
uint8_t buf[16];
memcpy(buf, input, sizeof(buf));
buf[0] ^= 0x1;
aes192_encrypt_block(&round_keys, buf, output + 16);
#endif
}
void owf_256(const uint8_t* key, const uint8_t* input, uint8_t* output) {
#if defined(HAVE_OPENSSL)
const EVP_CIPHER* cipher = EVP_aes_256_ecb();
assert(cipher);
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
assert(ctx);
EVP_CIPHER_CTX_set_padding(ctx, 0);
EVP_EncryptInit_ex(ctx, cipher, NULL, key, NULL);
int len = 0;
EVP_EncryptUpdate(ctx, output, &len, input, IV_SIZE);
uint8_t buf[16];
memcpy(buf, input, sizeof(buf));
buf[0] ^= 0x1;
EVP_EncryptUpdate(ctx, output + IV_SIZE, &len, buf, IV_SIZE);
EVP_EncryptFinal_ex(ctx, output + 2 * IV_SIZE, &len);
EVP_CIPHER_CTX_free(ctx);
#else
aes_round_keys_t round_keys;
aes256_init_round_keys(&round_keys, key);
aes256_encrypt_block(&round_keys, input, output);
uint8_t buf[16];
memcpy(buf, input, sizeof(buf));
buf[0] ^= 0x1;
aes256_encrypt_block(&round_keys, buf, output + 16);
#endif
}
void owf_em_128(const uint8_t* key, const uint8_t* input, uint8_t* output) {
#if defined(HAVE_OPENSSL)
const EVP_CIPHER* cipher = EVP_aes_128_ecb();
assert(cipher);
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
assert(ctx);
EVP_CIPHER_CTX_set_padding(ctx, 0);
EVP_EncryptInit_ex(ctx, cipher, NULL, input, NULL);
int len = 0;
EVP_EncryptUpdate(ctx, output, &len, key, IV_SIZE);
EVP_EncryptFinal_ex(ctx, output + IV_SIZE, &len);
EVP_CIPHER_CTX_free(ctx);
xor_u8_array(output, key, output, 16);
#else
aes_round_keys_t round_keys;
aes128_init_round_keys(&round_keys, input);
aes128_encrypt_block(&round_keys, key, output);
xor_u8_array(output, key, output, 16);
#endif
}
void owf_em_192(const uint8_t* key, const uint8_t* input, uint8_t* output) {
aes_round_keys_t round_keys;
rijndael192_init_round_keys(&round_keys, input);
rijndael192_encrypt_block(&round_keys, key, output);
xor_u8_array(output, key, output, 24);
}
void owf_em_256(const uint8_t* key, const uint8_t* input, uint8_t* output) {
aes_round_keys_t round_keys;
rijndael256_init_round_keys(&round_keys, input);
rijndael256_encrypt_block(&round_keys, key, output);
xor_u8_array(output, key, output, 32);
}