sara matheu
/
CurvasElipticas
Operaciones de generacion de claves, D-H, firma y validacion.
sha256.h@5:4f619b9a7bb2, 2015-02-20 (annotated)
- Committer:
- saranieves92
- Date:
- Fri Feb 20 18:37:50 2015 +0000
- Revision:
- 5:4f619b9a7bb2
- Parent:
- 3:74a69ff114ba
intento de rsa
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
saranieves92 | 3:74a69ff114ba | 1 | #ifndef SHA256_H |
saranieves92 | 3:74a69ff114ba | 2 | #define SHA256_H |
saranieves92 | 3:74a69ff114ba | 3 | #include <string> |
saranieves92 | 3:74a69ff114ba | 4 | |
saranieves92 | 3:74a69ff114ba | 5 | class SHA256 |
saranieves92 | 3:74a69ff114ba | 6 | { |
saranieves92 | 3:74a69ff114ba | 7 | protected: |
saranieves92 | 3:74a69ff114ba | 8 | typedef unsigned char uint8; |
saranieves92 | 3:74a69ff114ba | 9 | typedef unsigned int uint32; |
saranieves92 | 3:74a69ff114ba | 10 | typedef unsigned long long uint64; |
saranieves92 | 3:74a69ff114ba | 11 | |
saranieves92 | 3:74a69ff114ba | 12 | const static uint32 sha256_k[]; |
saranieves92 | 3:74a69ff114ba | 13 | static const unsigned int SHA224_256_BLOCK_SIZE = (512/8); |
saranieves92 | 3:74a69ff114ba | 14 | public: |
saranieves92 | 3:74a69ff114ba | 15 | void init(); |
saranieves92 | 3:74a69ff114ba | 16 | void update(const unsigned char *message, unsigned int len); |
saranieves92 | 3:74a69ff114ba | 17 | void final(unsigned char *digest); |
saranieves92 | 3:74a69ff114ba | 18 | static const unsigned int DIGEST_SIZE = ( 256 / 8); |
saranieves92 | 3:74a69ff114ba | 19 | |
saranieves92 | 3:74a69ff114ba | 20 | protected: |
saranieves92 | 3:74a69ff114ba | 21 | void transform(const unsigned char *message, unsigned int block_nb); |
saranieves92 | 3:74a69ff114ba | 22 | unsigned int m_tot_len; |
saranieves92 | 3:74a69ff114ba | 23 | unsigned int m_len; |
saranieves92 | 3:74a69ff114ba | 24 | unsigned char m_block[2*SHA224_256_BLOCK_SIZE]; |
saranieves92 | 3:74a69ff114ba | 25 | uint32 m_h[8]; |
saranieves92 | 3:74a69ff114ba | 26 | }; |
saranieves92 | 3:74a69ff114ba | 27 | |
saranieves92 | 3:74a69ff114ba | 28 | std::string sha256(std::string input); |
saranieves92 | 3:74a69ff114ba | 29 | |
saranieves92 | 3:74a69ff114ba | 30 | #define SHA2_SHFR(x, n) (x >> n) |
saranieves92 | 3:74a69ff114ba | 31 | #define SHA2_ROTR(x, n) ((x >> n) | (x << ((sizeof(x) << 3) - n))) |
saranieves92 | 3:74a69ff114ba | 32 | #define SHA2_ROTL(x, n) ((x << n) | (x >> ((sizeof(x) << 3) - n))) |
saranieves92 | 3:74a69ff114ba | 33 | #define SHA2_CH(x, y, z) ((x & y) ^ (~x & z)) |
saranieves92 | 3:74a69ff114ba | 34 | #define SHA2_MAJ(x, y, z) ((x & y) ^ (x & z) ^ (y & z)) |
saranieves92 | 3:74a69ff114ba | 35 | #define SHA256_F1(x) (SHA2_ROTR(x, 2) ^ SHA2_ROTR(x, 13) ^ SHA2_ROTR(x, 22)) |
saranieves92 | 3:74a69ff114ba | 36 | #define SHA256_F2(x) (SHA2_ROTR(x, 6) ^ SHA2_ROTR(x, 11) ^ SHA2_ROTR(x, 25)) |
saranieves92 | 3:74a69ff114ba | 37 | #define SHA256_F3(x) (SHA2_ROTR(x, 7) ^ SHA2_ROTR(x, 18) ^ SHA2_SHFR(x, 3)) |
saranieves92 | 3:74a69ff114ba | 38 | #define SHA256_F4(x) (SHA2_ROTR(x, 17) ^ SHA2_ROTR(x, 19) ^ SHA2_SHFR(x, 10)) |
saranieves92 | 3:74a69ff114ba | 39 | #define SHA2_UNPACK32(x, str) \ |
saranieves92 | 3:74a69ff114ba | 40 | { \ |
saranieves92 | 3:74a69ff114ba | 41 | *((str) + 3) = (uint8) ((x) ); \ |
saranieves92 | 3:74a69ff114ba | 42 | *((str) + 2) = (uint8) ((x) >> 8); \ |
saranieves92 | 3:74a69ff114ba | 43 | *((str) + 1) = (uint8) ((x) >> 16); \ |
saranieves92 | 3:74a69ff114ba | 44 | *((str) + 0) = (uint8) ((x) >> 24); \ |
saranieves92 | 3:74a69ff114ba | 45 | } |
saranieves92 | 3:74a69ff114ba | 46 | #define SHA2_PACK32(str, x) \ |
saranieves92 | 3:74a69ff114ba | 47 | { \ |
saranieves92 | 3:74a69ff114ba | 48 | *(x) = ((uint32) *((str) + 3) ) \ |
saranieves92 | 3:74a69ff114ba | 49 | | ((uint32) *((str) + 2) << 8) \ |
saranieves92 | 3:74a69ff114ba | 50 | | ((uint32) *((str) + 1) << 16) \ |
saranieves92 | 3:74a69ff114ba | 51 | | ((uint32) *((str) + 0) << 24); \ |
saranieves92 | 3:74a69ff114ba | 52 | } |
saranieves92 | 3:74a69ff114ba | 53 | #endif |