Skip to content

Commit

Permalink
Replacing gnet md5/sha functions with g_checksum functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
schoenw committed Mar 19, 2010
1 parent aad0849 commit 10a9474
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 45 deletions.
76 changes: 39 additions & 37 deletions src/security.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
* \param password password (not necessarily NUL terminated).
* \param password_len length of the password (must be positive).
* \param key pointer to memory large enough to hold the key.
* \param keylen length of the key (in/out)
*
* Convert the password into a key by implementing the algorithm
* defined in RFC 3414 appendix A.2.1 using MD5 as the oneway hash
Expand All @@ -149,42 +150,42 @@

void
gnet_snmp_password_to_key_md5(guchar *password, gsize password_len,
guchar *key)
guint8 *key, gsize *keylen)
{
GMD5 *gmd5;
GChecksum *gmd5;
guchar *cp, password_buf[64];
gulong password_index = 0;
gulong count = 0, i;

g_assert(password_len);

gmd5 = gnet_md5_new_incremental();
gmd5 = g_checksum_new(G_CHECKSUM_MD5);

/**********************************************/
/* Use while loop until we've done 1 Megabyte */
/**********************************************/

while (count < 1048576) {
cp = password_buf;
for(i = 0; i < 64; i++) {
for (i = 0; i < 64; i++) {
/*************************************************/
/* Take the next octet of the password, wrapping */
/* to the beginning of the password as necessary.*/
/*************************************************/
*cp++ = password[ password_index++ % password_len ];
}
gnet_md5_update(gmd5, (gchar *) password_buf, 64);
g_checksum_update(gmd5, (guchar *) password_buf, 64);
count += 64;
}
gnet_md5_final(gmd5);

g_memmove(key, gnet_md5_get_digest(gmd5), GNET_MD5_HASH_LENGTH);
gnet_md5_delete(gmd5);
g_checksum_get_digest(gmd5, key, keylen);
g_checksum_free(gmd5);
}

/** Localize a key using MD5.
*
* \param key pointer to memory which holds a key.
* \param keylen length of the key
* \param engineID pointer to memory which holds an SNMP engine ID.
* \param engineID_len length of the engine ID (between 5 and 32 inclusive).
*
Expand All @@ -194,27 +195,28 @@ gnet_snmp_password_to_key_md5(guchar *password, gsize password_len,
*/

void
gnet_snmp_localize_key_md5(guchar *key, guchar *engineID, gsize engineID_len)
gnet_snmp_localize_key_md5(guchar *key, gsize *keylen,
guchar *engineID, gsize engineID_len)
{
GMD5 *gmd5;
guchar password_buf[64];

g_assert(engineID_len > 4 && engineID_len < 33);
GChecksum *gmd5;

g_memmove(password_buf, key, 16);
g_memmove(password_buf+16, engineID, engineID_len);
g_memmove(password_buf+16+engineID_len, key, 16);
g_assert(key && keylen && engineID
&& engineID_len > 4 && engineID_len < 33);

gmd5 = gnet_md5_new((gchar *) password_buf, 32+engineID_len);
g_memmove(key, gnet_md5_get_digest(gmd5), GNET_MD5_HASH_LENGTH);
gnet_md5_delete(gmd5);
gmd5 = g_checksum_new(G_CHECKSUM_MD5);
g_checksum_update(gmd5, key, *keylen);
g_checksum_update(gmd5, engineID, engineID_len);
g_checksum_update(gmd5, key, *keylen);
g_checksum_get_digest(gmd5, key, keylen);
g_checksum_free(gmd5);
}

/** Convert password into a key using SHA.
*
* \param password password (not necessarily NUL terminated)
* \param password_len length of the password (must be positive)
* \param key pointer to memory large enough to hold the key
* \param keylen length of the key (in/out)
*
* Convert the password into a key by implementing the algorithm
* defined in RFC 3414 appendix A.2.1 using SHA as the oneway hash
Expand All @@ -223,16 +225,16 @@ gnet_snmp_localize_key_md5(guchar *key, guchar *engineID, gsize engineID_len)

void
gnet_snmp_password_to_key_sha(guchar *password, gsize password_len,
guchar *key)
guchar *key, gsize *keylen)
{
GSHA *gsha;
GChecksum *gsha;
guchar *cp, password_buf[64];
gulong password_index = 0;
gulong count = 0, i;

g_assert(password_len);

gsha = gnet_sha_new_incremental();
gsha = g_checksum_new(G_CHECKSUM_SHA1);

/**********************************************/
/* Use while loop until we've done 1 Megabyte */
Expand All @@ -247,18 +249,18 @@ gnet_snmp_password_to_key_sha(guchar *password, gsize password_len,
/*************************************************/
*cp++ = password[ password_index++ % password_len ];
}
gnet_sha_update(gsha, (gchar *) password_buf, 64);
g_checksum_update(gsha, (guchar *) password_buf, 64);
count += 64;
}
gnet_sha_final(gsha);

g_memmove(key, gnet_sha_get_digest(gsha), GNET_SHA_HASH_LENGTH);
gnet_sha_delete(gsha);
g_checksum_get_digest(gsha, key, keylen);
g_checksum_free(gsha);
}

/** Localize a key using SHA.
*
* \param key pointer to memory which holds a key
* \param keylen length of the key
* \param engineID pointer to memory which holds an SNMP engine ID
* \param engineID_len length of the engine ID (between 5 and 32 inclusive)
*
Expand All @@ -268,18 +270,18 @@ gnet_snmp_password_to_key_sha(guchar *password, gsize password_len,
*/

void
gnet_snmp_localize_key_sha(guchar *key, guchar *engineID, gsize engineID_len)
gnet_snmp_localize_key_sha(guchar *key, gsize *keylen,
guchar *engineID, gsize engineID_len)
{
GSHA *gsha;
guchar password_buf[72];

g_assert(engineID_len > 4 && engineID_len < 33);
GChecksum *gsha;

g_memmove(password_buf, key, 20);
g_memmove(password_buf+20, engineID, engineID_len);
g_memmove(password_buf+20+engineID_len, key, 20);
g_assert(key && keylen && engineID
&& engineID_len > 4 && engineID_len < 33);

gsha = gnet_sha_new((gchar *) password_buf, 40+engineID_len);
g_memmove(key, gnet_sha_get_digest(gsha), GNET_SHA_HASH_LENGTH);
gnet_sha_delete(gsha);
gsha = g_checksum_new(G_CHECKSUM_SHA1);
g_checksum_update(gsha, key, *keylen);
g_checksum_update(gsha, engineID, engineID_len);
g_checksum_update(gsha, key, *keylen);
g_checksum_get_digest(gsha, key, keylen);
g_checksum_free(gsha);
}
8 changes: 4 additions & 4 deletions src/security.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ struct _SNMP_AUTH


void gnet_snmp_password_to_key_md5 (guchar *password, gsize password_len,
guchar *key);
void gnet_snmp_localize_key_md5 (guchar *key,
guchar *key, gsize *keylen);
void gnet_snmp_localize_key_md5 (guchar *key, gsize *keylen,
guchar *engineID, gsize engineID_len);

void gnet_snmp_password_to_key_sha (guchar *password, gsize password_len,
guchar *key);
void gnet_snmp_localize_key_sha (guchar *key,
guchar *key, gsize *keylen);
void gnet_snmp_localize_key_sha (guchar *key, gsize *keylen,
guchar *engineID, gsize engineID_len);

#endif /* __GNET_SNMP_USM_H__ */
12 changes: 8 additions & 4 deletions tests/gsnmp-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ test_md5_key_localization()
{
char *password = "maplesyrup";
guchar key[GNET_MD5_HASH_LENGTH];
gsize keylen = GNET_MD5_HASH_LENGTH;

guchar engineid[] =
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Expand All @@ -33,10 +34,11 @@ test_md5_key_localization()
{ 0x52, 0x6f, 0x5e, 0xed, 0x9f, 0xcc, 0xe2, 0x6f,
0x89, 0x64, 0xc2, 0x93, 0x07, 0x87, 0xd8, 0x2b };

gnet_snmp_password_to_key_md5((guchar *) password, strlen(password), key);
gnet_snmp_password_to_key_md5((guchar *) password, strlen(password),
key, &keylen);
g_assert(memcmp(key, digest1, GNET_MD5_HASH_LENGTH) == 0);

gnet_snmp_localize_key_md5(key, engineid, G_N_ELEMENTS(engineid));
gnet_snmp_localize_key_md5(key, &keylen, engineid, G_N_ELEMENTS(engineid));
g_assert(memcmp(key, digest2, GNET_MD5_HASH_LENGTH) == 0);
}

Expand All @@ -50,6 +52,7 @@ test_sha_key_localization()
{
char *password = "maplesyrup";
guchar key[GNET_SHA_HASH_LENGTH];
gsize keylen = GNET_SHA_HASH_LENGTH;

guchar engineid[] =
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Expand All @@ -63,10 +66,11 @@ test_sha_key_localization()
{ 0x66, 0x95, 0xfe, 0xbc, 0x92, 0x88, 0xe3, 0x62, 0x82, 0x23,
0x5f, 0xc7, 0x15, 0x1f, 0x12, 0x84, 0x97, 0xb3, 0x8f, 0x3f };

gnet_snmp_password_to_key_sha((guchar *) password, strlen(password), key);
gnet_snmp_password_to_key_sha((guchar *) password, strlen(password),
key, &keylen);
g_assert(memcmp(key, digest1, GNET_SHA_HASH_LENGTH) == 0);

gnet_snmp_localize_key_sha(key, engineid, G_N_ELEMENTS(engineid));
gnet_snmp_localize_key_sha(key, &keylen, engineid, G_N_ELEMENTS(engineid));
g_assert(memcmp(key, digest2, GNET_SHA_HASH_LENGTH) == 0);
}

Expand Down

0 comments on commit 10a9474

Please sign in to comment.