00001
00002
00003 #ifndef SST_HMAC_H
00004 #define SST_HMAC_H
00005
00006 #include "sha2.h"
00007
00008 namespace SST {
00009
00010
00011
00012 #define HMACKEYLEN (256/8)
00013
00014
00015 #define HMACLEN (128/8)
00016
00017
00018 typedef SHA256_CTX hmac_ctx;
00019
00020
00021
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
00034 bool reset();
00035
00036 public:
00037 HMAC(const QByteArray &key);
00038 HMAC(const HMAC &other);
00039
00040
00041 qint64 writeData(const char *data, qint64 len);
00042
00043
00044 int outSize();
00045 QByteArray final();
00046
00047
00048 QByteArray final(int macsize);
00049
00050
00051
00052 inline void finalAppend(QByteArray &msg, int macsize = HMACLEN)
00053 { update(msg); msg += final(macsize); }
00054
00055
00056
00057 bool finalVerify(QByteArray &msg, int macsize = HMACLEN);
00058
00059
00061
00062
00063
00064
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
00071 void calcAppend(QByteArray &msg, int macsize = HMACLEN) const;
00072
00073
00074 bool calcVerify(QByteArray &msg, int macsize = HMACLEN) const;
00075 };
00076
00077 }
00078
00079 #endif // SST_HMAC_H