Annotation of embedaddon/strongswan/src/libstrongswan/plugins/gcrypt/gcrypt_hasher.c, revision 1.1.1.1
1.1 misho 1: /*
2: * Copyright (C) 2009 Martin Willi
3: * HSR Hochschule fuer Technik Rapperswil
4: *
5: * This program is free software; you can redistribute it and/or modify it
6: * under the terms of the GNU General Public License as published by the
7: * Free Software Foundation; either version 2 of the License, or (at your
8: * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9: *
10: * This program is distributed in the hope that it will be useful, but
11: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13: * for more details.
14: */
15:
16: #include "gcrypt_hasher.h"
17:
18: #include <utils/debug.h>
19:
20: #include <gcrypt.h>
21:
22: typedef struct private_gcrypt_hasher_t private_gcrypt_hasher_t;
23:
24: /**
25: * Private data of gcrypt_hasher_t
26: */
27: struct private_gcrypt_hasher_t {
28:
29: /**
30: * Public part of this class.
31: */
32: gcrypt_hasher_t public;
33:
34: /**
35: * gcrypt hasher context
36: */
37: gcry_md_hd_t hd;
38: };
39:
40: METHOD(hasher_t, get_hash_size, size_t,
41: private_gcrypt_hasher_t *this)
42: {
43: return gcry_md_get_algo_dlen(gcry_md_get_algo(this->hd));
44: }
45:
46: METHOD(hasher_t, reset, bool,
47: private_gcrypt_hasher_t *this)
48: {
49: gcry_md_reset(this->hd);
50: return TRUE;
51: }
52:
53: METHOD(hasher_t, get_hash, bool,
54: private_gcrypt_hasher_t *this, chunk_t chunk, uint8_t *hash)
55: {
56: gcry_md_write(this->hd, chunk.ptr, chunk.len);
57: if (hash)
58: {
59: memcpy(hash, gcry_md_read(this->hd, 0), get_hash_size(this));
60: gcry_md_reset(this->hd);
61: }
62: return TRUE;
63: }
64:
65: METHOD(hasher_t, allocate_hash, bool,
66: private_gcrypt_hasher_t *this, chunk_t chunk, chunk_t *hash)
67: {
68: if (hash)
69: {
70: *hash = chunk_alloc(get_hash_size(this));
71: return get_hash(this, chunk, hash->ptr);
72: }
73: return get_hash(this, chunk, NULL);
74: }
75:
76: METHOD(hasher_t, destroy, void,
77: private_gcrypt_hasher_t *this)
78: {
79: gcry_md_close(this->hd);
80: free(this);
81: }
82:
83: /*
84: * Described in header
85: */
86: gcrypt_hasher_t *gcrypt_hasher_create(hash_algorithm_t algo)
87: {
88: private_gcrypt_hasher_t *this;
89: int gcrypt_alg;
90: gcry_error_t err;
91:
92: switch (algo)
93: {
94: case HASH_MD2:
95: gcrypt_alg = GCRY_MD_MD2;
96: break;
97: case HASH_MD4:
98: gcrypt_alg = GCRY_MD_MD4;
99: break;
100: case HASH_MD5:
101: gcrypt_alg = GCRY_MD_MD5;
102: break;
103: case HASH_SHA1:
104: gcrypt_alg = GCRY_MD_SHA1;
105: break;
106: case HASH_SHA224:
107: gcrypt_alg = GCRY_MD_SHA224;
108: break;
109: case HASH_SHA256:
110: gcrypt_alg = GCRY_MD_SHA256;
111: break;
112: case HASH_SHA384:
113: gcrypt_alg = GCRY_MD_SHA384;
114: break;
115: case HASH_SHA512:
116: gcrypt_alg = GCRY_MD_SHA512;
117: break;
118: default:
119: return NULL;
120: }
121:
122: INIT(this,
123: .public = {
124: .hasher = {
125: .get_hash = _get_hash,
126: .allocate_hash = _allocate_hash,
127: .get_hash_size = _get_hash_size,
128: .reset = _reset,
129: .destroy = _destroy,
130: },
131: },
132: );
133:
134: err = gcry_md_open(&this->hd, gcrypt_alg, 0);
135: if (err)
136: {
137: DBG1(DBG_LIB, "grcy_md_open(%N) failed: %s",
138: hash_algorithm_names, algo, gpg_strerror(err));
139: free(this);
140: return NULL;
141: }
142:
143: return &this->public;
144: }
145:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>