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
|
#include "lib4.h"
#include "lib3.h"
#include "lib2.h"
#include "lib.h"
/**
* encrypt always with the same key, same nonce?
*
**/
int aes_ctr_edit(char *ciphertext, int ciphertext_length, int offset, char *newtext)
{
// do not allow greater ciphertexts
if((strlen(newtext)+offset) > ciphertext_length)
return 1;
// insert newtext at offset and reencrpyt
char *plaintext = malloc(ciphertext_length);
aes_ctr(ciphertext, ciphertext_length, plaintext, key, nonce);
memcpy(&plaintext[offset], newtext, strlen(newtext));
aes_ctr(plaintext, ciphertext_length, ciphertext, key, nonce);
return 0;
}
void sha1_hmac(unsigned int *mac, unsigned char *message, unsigned int msg_len,
unsigned char *key, unsigned int key_len)
{
char *res = malloc(msg_len + key_len);
memcpy(res, key, key_len);
memcpy(&res[key_len], message, msg_len);
SHA1Context sh;
SHA1Reset(&sh);
SHA1Input(&sh, res, (msg_len + key_len));
SHA1Result(&sh);
memcpy(mac, &(sh.Message_Digest), 20);
}
int sha1_hmac_verify(unsigned int *mac, unsigned char *msg, unsigned int msg_len,
unsigned char *key, unsigned int key_len)
{
unsigned int com_mac[5];
sha1_hmac(com_mac, msg, msg_len, key, key_len);
return !memcmp(com_mac, mac, 20);
}
int sha1_padding(unsigned long msg_len, char **result)
{
int i;
unsigned int padding_len = 64 - (msg_len % 64);
// enough sapce for the length of the message?
padding_len = padding_len < 9 ? padding_len+64 : padding_len;
(*result) = malloc(padding_len);
memset((*result), 0x00, padding_len);
// append 1
memset(&(*result)[0], 0x80, 1);
//(*result)[0] = 0x80;
// write the 8 byte msg_len at the end bytewise
for(i=0;i<8;i++) {
(*result)[padding_len-i-1] = (msg_len*8 >> (i*8)) & 0xFF;
}
return padding_len;
}
void sha1_hmac_forge(unsigned int *mac, unsigned char *text, unsigned int text_len,
unsigned int *sha1_registers)
{
SHA1Context sh;
SHA1Reset(&sh);
sha1_set_magic_nr(&sh, sha1_registers);
SHA1Input(&sh, text, text_len);
SHA1Result(&sh);
memcpy(mac, &(sh.Message_Digest), 20);
}
|