sara matheu
/
CurvasElipticas
Operaciones de generacion de claves, D-H, firma y validacion.
sha256.cpp@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 | #include <cstring> |
saranieves92 | 3:74a69ff114ba | 2 | #include <fstream> |
saranieves92 | 3:74a69ff114ba | 3 | #include "sha256.h" |
saranieves92 | 3:74a69ff114ba | 4 | |
saranieves92 | 3:74a69ff114ba | 5 | const unsigned int SHA256::sha256_k[64] = //UL = uint32 |
saranieves92 | 3:74a69ff114ba | 6 | {0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, |
saranieves92 | 3:74a69ff114ba | 7 | 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, |
saranieves92 | 3:74a69ff114ba | 8 | 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, |
saranieves92 | 3:74a69ff114ba | 9 | 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, |
saranieves92 | 3:74a69ff114ba | 10 | 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, |
saranieves92 | 3:74a69ff114ba | 11 | 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, |
saranieves92 | 3:74a69ff114ba | 12 | 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, |
saranieves92 | 3:74a69ff114ba | 13 | 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, |
saranieves92 | 3:74a69ff114ba | 14 | 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, |
saranieves92 | 3:74a69ff114ba | 15 | 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, |
saranieves92 | 3:74a69ff114ba | 16 | 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, |
saranieves92 | 3:74a69ff114ba | 17 | 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, |
saranieves92 | 3:74a69ff114ba | 18 | 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, |
saranieves92 | 3:74a69ff114ba | 19 | 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, |
saranieves92 | 3:74a69ff114ba | 20 | 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, |
saranieves92 | 3:74a69ff114ba | 21 | 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; |
saranieves92 | 3:74a69ff114ba | 22 | |
saranieves92 | 3:74a69ff114ba | 23 | void SHA256::transform(const unsigned char *message, unsigned int block_nb) |
saranieves92 | 3:74a69ff114ba | 24 | { |
saranieves92 | 3:74a69ff114ba | 25 | uint32 w[64]; |
saranieves92 | 3:74a69ff114ba | 26 | uint32 wv[8]; |
saranieves92 | 3:74a69ff114ba | 27 | uint32 t1, t2; |
saranieves92 | 3:74a69ff114ba | 28 | const unsigned char *sub_block; |
saranieves92 | 3:74a69ff114ba | 29 | int i; |
saranieves92 | 3:74a69ff114ba | 30 | int j; |
saranieves92 | 3:74a69ff114ba | 31 | for (i = 0; i < (int) block_nb; i++) { |
saranieves92 | 3:74a69ff114ba | 32 | sub_block = message + (i << 6); |
saranieves92 | 3:74a69ff114ba | 33 | for (j = 0; j < 16; j++) { |
saranieves92 | 3:74a69ff114ba | 34 | SHA2_PACK32(&sub_block[j << 2], &w[j]); |
saranieves92 | 3:74a69ff114ba | 35 | } |
saranieves92 | 3:74a69ff114ba | 36 | for (j = 16; j < 64; j++) { |
saranieves92 | 3:74a69ff114ba | 37 | w[j] = SHA256_F4(w[j - 2]) + w[j - 7] + SHA256_F3(w[j - 15]) + w[j - 16]; |
saranieves92 | 3:74a69ff114ba | 38 | } |
saranieves92 | 3:74a69ff114ba | 39 | for (j = 0; j < 8; j++) { |
saranieves92 | 3:74a69ff114ba | 40 | wv[j] = m_h[j]; |
saranieves92 | 3:74a69ff114ba | 41 | } |
saranieves92 | 3:74a69ff114ba | 42 | for (j = 0; j < 64; j++) { |
saranieves92 | 3:74a69ff114ba | 43 | t1 = wv[7] + SHA256_F2(wv[4]) + SHA2_CH(wv[4], wv[5], wv[6]) |
saranieves92 | 3:74a69ff114ba | 44 | + sha256_k[j] + w[j]; |
saranieves92 | 3:74a69ff114ba | 45 | t2 = SHA256_F1(wv[0]) + SHA2_MAJ(wv[0], wv[1], wv[2]); |
saranieves92 | 3:74a69ff114ba | 46 | wv[7] = wv[6]; |
saranieves92 | 3:74a69ff114ba | 47 | wv[6] = wv[5]; |
saranieves92 | 3:74a69ff114ba | 48 | wv[5] = wv[4]; |
saranieves92 | 3:74a69ff114ba | 49 | wv[4] = wv[3] + t1; |
saranieves92 | 3:74a69ff114ba | 50 | wv[3] = wv[2]; |
saranieves92 | 3:74a69ff114ba | 51 | wv[2] = wv[1]; |
saranieves92 | 3:74a69ff114ba | 52 | wv[1] = wv[0]; |
saranieves92 | 3:74a69ff114ba | 53 | wv[0] = t1 + t2; |
saranieves92 | 3:74a69ff114ba | 54 | } |
saranieves92 | 3:74a69ff114ba | 55 | for (j = 0; j < 8; j++) { |
saranieves92 | 3:74a69ff114ba | 56 | m_h[j] += wv[j]; |
saranieves92 | 3:74a69ff114ba | 57 | } |
saranieves92 | 3:74a69ff114ba | 58 | } |
saranieves92 | 3:74a69ff114ba | 59 | } |
saranieves92 | 3:74a69ff114ba | 60 | |
saranieves92 | 3:74a69ff114ba | 61 | void SHA256::init() |
saranieves92 | 3:74a69ff114ba | 62 | { |
saranieves92 | 3:74a69ff114ba | 63 | m_h[0] = 0x6a09e667; |
saranieves92 | 3:74a69ff114ba | 64 | m_h[1] = 0xbb67ae85; |
saranieves92 | 3:74a69ff114ba | 65 | m_h[2] = 0x3c6ef372; |
saranieves92 | 3:74a69ff114ba | 66 | m_h[3] = 0xa54ff53a; |
saranieves92 | 3:74a69ff114ba | 67 | m_h[4] = 0x510e527f; |
saranieves92 | 3:74a69ff114ba | 68 | m_h[5] = 0x9b05688c; |
saranieves92 | 3:74a69ff114ba | 69 | m_h[6] = 0x1f83d9ab; |
saranieves92 | 3:74a69ff114ba | 70 | m_h[7] = 0x5be0cd19; |
saranieves92 | 3:74a69ff114ba | 71 | m_len = 0; |
saranieves92 | 3:74a69ff114ba | 72 | m_tot_len = 0; |
saranieves92 | 3:74a69ff114ba | 73 | } |
saranieves92 | 3:74a69ff114ba | 74 | |
saranieves92 | 3:74a69ff114ba | 75 | void SHA256::update(const unsigned char *message, unsigned int len) |
saranieves92 | 3:74a69ff114ba | 76 | { |
saranieves92 | 3:74a69ff114ba | 77 | unsigned int block_nb; |
saranieves92 | 3:74a69ff114ba | 78 | unsigned int new_len, rem_len, tmp_len; |
saranieves92 | 3:74a69ff114ba | 79 | const unsigned char *shifted_message; |
saranieves92 | 3:74a69ff114ba | 80 | tmp_len = SHA224_256_BLOCK_SIZE - m_len; |
saranieves92 | 3:74a69ff114ba | 81 | rem_len = len < tmp_len ? len : tmp_len; |
saranieves92 | 3:74a69ff114ba | 82 | std::memcpy(&m_block[m_len], message, rem_len); |
saranieves92 | 3:74a69ff114ba | 83 | if (m_len + len < SHA224_256_BLOCK_SIZE) { |
saranieves92 | 3:74a69ff114ba | 84 | m_len += len; |
saranieves92 | 3:74a69ff114ba | 85 | return; |
saranieves92 | 3:74a69ff114ba | 86 | } |
saranieves92 | 3:74a69ff114ba | 87 | new_len = len - rem_len; |
saranieves92 | 3:74a69ff114ba | 88 | block_nb = new_len / SHA224_256_BLOCK_SIZE; |
saranieves92 | 3:74a69ff114ba | 89 | shifted_message = message + rem_len; |
saranieves92 | 3:74a69ff114ba | 90 | transform(m_block, 1); |
saranieves92 | 3:74a69ff114ba | 91 | transform(shifted_message, block_nb); |
saranieves92 | 3:74a69ff114ba | 92 | rem_len = new_len % SHA224_256_BLOCK_SIZE; |
saranieves92 | 3:74a69ff114ba | 93 | std::memcpy(m_block, &shifted_message[block_nb << 6], rem_len); |
saranieves92 | 3:74a69ff114ba | 94 | m_len = rem_len; |
saranieves92 | 3:74a69ff114ba | 95 | m_tot_len += (block_nb + 1) << 6; |
saranieves92 | 3:74a69ff114ba | 96 | } |
saranieves92 | 3:74a69ff114ba | 97 | |
saranieves92 | 3:74a69ff114ba | 98 | void SHA256::final(unsigned char *digest) |
saranieves92 | 3:74a69ff114ba | 99 | { |
saranieves92 | 3:74a69ff114ba | 100 | unsigned int block_nb; |
saranieves92 | 3:74a69ff114ba | 101 | unsigned int pm_len; |
saranieves92 | 3:74a69ff114ba | 102 | unsigned int len_b; |
saranieves92 | 3:74a69ff114ba | 103 | int i; |
saranieves92 | 3:74a69ff114ba | 104 | block_nb = (1 + ((SHA224_256_BLOCK_SIZE - 9) |
saranieves92 | 3:74a69ff114ba | 105 | < (m_len % SHA224_256_BLOCK_SIZE))); |
saranieves92 | 3:74a69ff114ba | 106 | len_b = (m_tot_len + m_len) << 3; |
saranieves92 | 3:74a69ff114ba | 107 | pm_len = block_nb << 6; |
saranieves92 | 3:74a69ff114ba | 108 | std::memset(m_block + m_len, 0, pm_len - m_len); |
saranieves92 | 3:74a69ff114ba | 109 | m_block[m_len] = 0x80; |
saranieves92 | 3:74a69ff114ba | 110 | SHA2_UNPACK32(len_b, m_block + pm_len - 4); |
saranieves92 | 3:74a69ff114ba | 111 | transform(m_block, block_nb); |
saranieves92 | 3:74a69ff114ba | 112 | for (i = 0 ; i < 8; i++) { |
saranieves92 | 3:74a69ff114ba | 113 | SHA2_UNPACK32(m_h[i], &digest[i << 2]); |
saranieves92 | 3:74a69ff114ba | 114 | } |
saranieves92 | 3:74a69ff114ba | 115 | } |
saranieves92 | 3:74a69ff114ba | 116 | |
saranieves92 | 3:74a69ff114ba | 117 | std::string sha256(std::string input) |
saranieves92 | 3:74a69ff114ba | 118 | { |
saranieves92 | 3:74a69ff114ba | 119 | unsigned char digest[SHA256::DIGEST_SIZE]; |
saranieves92 | 3:74a69ff114ba | 120 | std::memset(digest,0,SHA256::DIGEST_SIZE); |
saranieves92 | 3:74a69ff114ba | 121 | |
saranieves92 | 3:74a69ff114ba | 122 | SHA256 ctx = SHA256(); |
saranieves92 | 3:74a69ff114ba | 123 | ctx.init(); |
saranieves92 | 3:74a69ff114ba | 124 | ctx.update( (unsigned char*)input.c_str(), input.length()); |
saranieves92 | 3:74a69ff114ba | 125 | ctx.final(digest); |
saranieves92 | 3:74a69ff114ba | 126 | |
saranieves92 | 3:74a69ff114ba | 127 | char buf[2*SHA256::DIGEST_SIZE+1]; |
saranieves92 | 3:74a69ff114ba | 128 | buf[2*SHA256::DIGEST_SIZE] = 0; |
saranieves92 | 3:74a69ff114ba | 129 | for (int i = 0; i < SHA256::DIGEST_SIZE; i++) |
saranieves92 | 3:74a69ff114ba | 130 | std::sprintf(buf+i*2, "%02x", digest[i]); |
saranieves92 | 3:74a69ff114ba | 131 | return std::string(buf); |
saranieves92 | 3:74a69ff114ba | 132 | } |