00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef lint
00034 static char copyright[] =
00035 "@(#) Copyright (c) 2002-2004\n\
00036 Netherlands Forensic Institute. All rights reserved.\n";
00037 #endif
00038
00039 #ifdef HAVE_CONFIG_H
00040 #include <config.h>
00041 #endif
00042
00043 #include <stdlib.h>
00044 #include <string.h>
00045
00046 #include "hashcontainer.h"
00047 #include "rdd.h"
00048
00049 int rdd_new_hashcontainer(RDD_HASH_CONTAINER ** self)
00050 {
00051 RDD_HASH_CONTAINER * h = 0;
00052 int rc = RDD_OK;
00053
00054 if (self == 0) {
00055 return RDD_BADARG;
00056 }
00057
00058 if ((h = calloc(1, sizeof(RDD_HASH_CONTAINER))) == 0) {
00059 rc = RDD_NOMEM;
00060 goto error;
00061 }
00062
00063 *self = h;
00064 return RDD_OK;
00065
00066 error:
00067 *self = 0;
00068 if (h != 0) free(h);
00069 return rc;
00070
00071 }
00072
00073 int rdd_set_hash(RDD_HASH_CONTAINER * self, const char * hash_type, const uint8_t * hash)
00074 {
00075 if (self == 0) {
00076 return RDD_BADARG;
00077 }
00078 if (hash_type == 0) {
00079 return RDD_BADARG;
00080 }
00081 if (hash == 0) {
00082 return RDD_BADARG;
00083 }
00084
00085 if (!strcmp(hash_type, RDD_MD5)) {
00086 self->md5present = 1;
00087 memcpy(self->md5hash, hash, MD5_DIGEST_LENGTH);
00088 } else if (!strcmp(hash_type, RDD_SHA1)) {
00089 self->sha1present = 1;
00090 memcpy(self->sha1hash, hash, SHA_DIGEST_LENGTH);
00091 } else if (!strcmp(hash_type, RDD_SHA256)) {
00092 self->sha256present = 1;
00093 memcpy(self->sha256hash, hash, SHA256_DIGEST_LENGTH);
00094 } else if (!strcmp(hash_type, RDD_SHA384)) {
00095 self->sha384present = 1;
00096 memcpy(self->sha384hash, hash, SHA384_DIGEST_LENGTH);
00097 } else if (!strcmp(hash_type, RDD_SHA512)) {
00098 self->sha512present = 1;
00099 memcpy(self->sha512hash, hash, SHA512_DIGEST_LENGTH);
00100 } else {
00101 return RDD_BADARG;
00102 }
00103
00104 return RDD_OK;
00105 }
00106
00107 int rdd_get_hash(RDD_HASH_CONTAINER * self, const char * hash_type, uint8_t * hash)
00108 {
00109 if (self == 0) {
00110 return RDD_BADARG;
00111 }
00112 if (hash_type == 0) {
00113 return RDD_BADARG;
00114 }
00115 if (hash == 0) {
00116 return RDD_BADARG;
00117 }
00118
00119 if (!strcmp(hash_type, RDD_MD5)) {
00120 if (!self->md5present) {
00121 return RDD_NOTFOUND;
00122 }
00123 memcpy(hash, self->md5hash, MD5_DIGEST_LENGTH);
00124 } else if (!strcmp(hash_type, RDD_SHA1)) {
00125 if (!self->sha1present) {
00126 return RDD_NOTFOUND;
00127 }
00128 memcpy(hash, self->sha1hash, SHA_DIGEST_LENGTH);
00129 } else if (!strcmp(hash_type, RDD_SHA256)) {
00130 if (!self->sha256present) {
00131 return RDD_NOTFOUND;
00132 }
00133 memcpy(hash, self->sha256hash, SHA256_DIGEST_LENGTH);
00134 } else if (!strcmp(hash_type, RDD_SHA384)) {
00135 if (!self->sha384present) {
00136 return RDD_NOTFOUND;
00137 }
00138 memcpy(hash, self->sha384hash, SHA384_DIGEST_LENGTH);
00139 } else if (!strcmp(hash_type, RDD_SHA512)) {
00140 if (!self->sha512present) {
00141 return RDD_NOTFOUND;
00142 }
00143 memcpy(hash, self->sha512hash, SHA512_DIGEST_LENGTH);
00144 } else {
00145 return RDD_BADARG;
00146 }
00147
00148 return RDD_OK;
00149
00150 }
00151
00152 int rdd_hash_present(RDD_HASH_CONTAINER * self, const char * hash_type, int * present)
00153 {
00154 if (self == 0) {
00155 return RDD_BADARG;
00156 }
00157 if (hash_type == 0) {
00158 return RDD_BADARG;
00159 }
00160 if (present == 0) {
00161 return RDD_BADARG;
00162 }
00163
00164 if (!strcmp(hash_type, RDD_MD5)) {
00165 *present = self->md5present;
00166 } else if (!strcmp(hash_type, RDD_SHA1)) {
00167 *present = self->sha1present;
00168 } else if (!strcmp(hash_type, RDD_SHA256)) {
00169 *present = self->sha256present;
00170 } else if (!strcmp(hash_type, RDD_SHA384)) {
00171 *present = self->sha384present;
00172 } else if (!strcmp(hash_type, RDD_SHA512)) {
00173 *present = self->sha512present;
00174 } else {
00175 return RDD_BADARG;
00176 }
00177
00178
00179 return RDD_OK;
00180 }