-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuocrypt.c
108 lines (83 loc) · 2.51 KB
/
uocrypt.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
#include <stdio.h>
#include "uocrypt.h"
void usage(char *name) {
printf("Usage: %s <input file> [<output file>]\n", name);
}
void* xmalloc(size_t i) {
void *ptr;
ptr = malloc(i);
if (!ptr)
exit(255);
return ptr;
}
/* Close file handles and free alloc'd memory, if alloc'd */
void clean(FILE *in, FILE *out, char *outfile, int argc) {
/* Apparently these can be come null if the file is empty? */
if (in)
fclose(in);
if (out)
fclose(out);
if (argc == 2)
free(outfile);
}
/* Ripped from the manual
* http://www.gnupg.org/documentation/manuals/gcrypt/Error-Strings.html#Error-Strings */
void uocrypt_error(gcry_error_t err) {
if (err)
{
fprintf (stderr, "Failure: %s/%s\n",
gcry_strsource (err),
gcry_strerror (err));
abort();
}
}
/* Ripped from the manual
* http://www.gnupg.org/documentation/manuals/gcrypt/Initializing-the-library.html#Initializing-the-library */
void uocrypt_init() {
/* Version check should be the very first call because it
* makes sure that important subsystems are intialized. */
if (!gcry_check_version (GCRYPT_VERSION))
{
fputs ("libgcrypt version mismatch\n", stderr);
exit (2);
}
/* Disable secure memory. */
gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
/* ... If required, other initialization goes here. */
/* Tell Libgcrypt that initialization has completed. */
gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
if (!gcry_control (GCRYCTL_INITIALIZATION_FINISHED_P))
{
fputs ("libgcrypt has not been initialized\n", stderr);
abort ();
}
}
/* Print data in hex mostly for debugging */
void uocrypt_print(char *str, size_t len) {
unsigned int i;
for(i = 0; i < len; i++)
printf("%02x", (unsigned char)str[i]);
printf("\n");
}
/* Zero pad and strip new line and null terminator */
void uocrypt_zero_pad(char *input, char *pass, size_t len) {
unsigned int i;
/* Right pad the pass with 0's */
for (i = strlen(input) - 1; i < len; i++)
input[i] = '0';
memcpy(pass, input, len);
#if DEBUG
printf("DEBUG: Zero padded password=");
uocrypt_print(pass, len);
#endif
}
void uocrypt_hash_md5(char *pass, size_t len) {
char digest[16];
gcry_md_hash_buffer(GCRY_MD_MD5, digest, pass, len);
/* Copy back into pass */
memcpy(pass, digest, len);
#if DEBUG
printf("DEBUG: Hashed password=");
uocrypt_print(pass, len);
#endif
}