Main Page | Class Hierarchy | Class List | File List | Class Members

/Users/baford/proj/netsteria/sst/lib/hmac.h

00001 /*** Simple HMAC-SHA256-128 implementation ***/
00002 
00003 #ifndef SST_HMAC_H
00004 #define SST_HMAC_H
00005 
00006 #include "sha2.h"
00007 
00008 namespace SST {
00009 
00010 
00011 // Length of symmetric key material for HMAC-SHA-256-128
00012 #define HMACKEYLEN      (256/8)
00013 
00014 // We use SHA-256 hashes truncated to 128 bits for HMAC generation
00015 #define HMACLEN         (128/8)
00016 
00017 
00018 typedef SHA256_CTX hmac_ctx;
00019 
00020 
00021 // Low-level functions
00022 void hmac_init(hmac_ctx *ctx, const uint8_t *hkey);
00023 void hmac_update(hmac_ctx *ctx, const void *data, size_t len);
00024 void hmac_final(hmac_ctx *ctx, const uint8_t *hkey,
00025                         uint8_t *outbuf, unsigned outlen);
00026 
00027 
00028 class HMAC : public SecureHash
00029 {
00030 private:
00031         hmac_ctx ictx, octx;
00032 
00033         // Not usable in this implementation of IODevice
00034         bool reset();
00035 
00036 public:
00037         HMAC(const QByteArray &key);
00038         HMAC(const HMAC &other);
00039 
00040         // Write data to the HMAC
00041         qint64 writeData(const char *data, qint64 len);
00042 
00043         // Produce the output, with the default (truncated) size of HMACLEN.
00044         int outSize();
00045         QByteArray final();
00046 
00047         // Produce the output with a specified size.
00048         QByteArray final(int macsize);
00049 
00050         // Update with the contents of a message
00051         // and append a MAC to the message.
00052         inline void finalAppend(QByteArray &msg, int macsize = HMACLEN)
00053                 { update(msg); msg += final(macsize); }
00054 
00055         // Update with the contents of a message minus its MAC trailer,
00056         // verify the MAC trailer, and cut it from the message.
00057         bool finalVerify(QByteArray &msg, int macsize = HMACLEN); 
00058 
00059 
00061         // These functions are const because they copy the HMAC state
00062         // to temporary storage before computing their result.
00063 
00064         // Compute an HMAC check over a message
00065         QByteArray calc(const void *msg, int msgsize,
00066                                 int macsize = HMACLEN) const;
00067         inline QByteArray calc(QByteArray &msg, int macsize = HMACLEN) const
00068                 { return calc(msg.data(), msg.size(), macsize); }
00069 
00070         // Compute and append HMAC check field to a message.
00071         void calcAppend(QByteArray &msg, int macsize = HMACLEN) const;
00072 
00073         // Strip and check the HMAC check field on receive.
00074         bool calcVerify(QByteArray &msg, int macsize = HMACLEN) const;
00075 };
00076 
00077 } // namespace SST
00078 
00079 #endif  // SST_HMAC_H

Generated on Wed Mar 28 11:48:05 2007 for SST by doxygen 1.3.4